List of usage examples for org.bouncycastle.asn1 DERSequence getEncoded
public byte[] getEncoded() throws IOException
From source file:bluecrystal.bcdeps.helper.DerEncoder.java
License:Open Source License
public static List<byte[]> extractCertArray(DERSequence certTree) { List<byte[]> ret = new ArrayList<byte[]>(); ASN1Encodable level0 = getAt(certTree, 3); if (level0 instanceof DERTaggedObject) { DERTaggedObject level0Tag = (DERTaggedObject) level0; ASN1Encodable level0Obj = level0Tag.getObject(); if (level0Obj instanceof DERSequence) { try { DERSequence level0Seq = (DERSequence) level0Obj; if (level0Seq.getObjectAt(2) instanceof DERBitString) { // achei o certificado byte[] b = level0Seq.getEncoded(); ret.add(b);/*from w w w . ja va 2s.c o m*/ } else { for (int i = 0; i < level0Seq.size(); i++) { ASN1Encodable objNdx = level0Seq.getObjectAt(i); if (objNdx instanceof DERSequence) { try { DERSequence objNdx2 = (DERSequence) objNdx; byte[] b = objNdx2.getEncoded(); ret.add(b); } catch (IOException e) { LOG.error("DER decoding error", e); } } } } } catch (IOException e) { LOG.error("DER decoding error", e); } } else if (level0Obj instanceof ASN1Sequence) { ASN1Sequence level0Seq = (ASN1Sequence) level0Obj; for (int i = 0; i < level0Seq.size(); i++) { ASN1Encodable objNdx = level0Seq.getObjectAt(i); if (objNdx instanceof DERSequence) { try { DERSequence objNdx2 = (DERSequence) objNdx; byte[] b = objNdx2.getEncoded(); ret.add(b); } catch (IOException e) { LOG.error("DER decoding error", e); } } } } } return ret; }
From source file:ca.trustpoint.m2m.M2mCertPath.java
License:Apache License
/** * Encode the CertPath using PKIPATH format. * * @return a byte array containing the binary encoding of the PkiPath object * @exception CertificateEncodingException if an exception occurs *///from ww w . ja v a 2s . c o m private byte[] encodePkiPath() throws CertificateEncodingException { ListIterator<M2mCertificate> li = certificates.listIterator(certificates.size()); ASN1EncodableVector encodedList = new ASN1EncodableVector(); // Get an encodable certificate vector. The certificates are encoded in reverse order (trust // anchor to target) according to PkiPath format. while (li.hasPrevious()) { M2mCertificate certificate = li.previous(); if (isDuplicateCertificate(certificate)) { throw new CertificateEncodingException("Duplicate certificate detected in path."); } try { encodedList.add(ASN1Primitive.fromByteArray(certificate.getEncoded())); } catch (IOException ex) { throw new CertificateEncodingException("Error encoding certificate data.", ex); } } // Wrap the data in a SEQUENCE DERSequence sequence = new DERSequence(encodedList); try { return sequence.getEncoded(); } catch (IOException ex) { throw new CertificateEncodingException("Error encoding certificate path.", ex); } }
From source file:com.goodvikings.cryptim.api.KeyRing.java
License:BEER-WARE LICENSE
private byte[] ASN1EncodeKeys() throws IOException, PGPException { JcaPGPKeyConverter converter = new JcaPGPKeyConverter(); PrivateKey priv = converter.getPrivateKey(kp.getPrivateKey()); PublicKey pub = converter.getPublicKey(kp.getPublicKey()); ASN1EncodableVector pubSeq = new ASN1EncodableVector(); for (String jid : keys.keySet()) { pubSeq.add(new DERSequence(new ASN1Encodable[] { new DERUTF8String(jid), new DERUTF8String(nicks.get(jid)), new DERUTCTime(keys.get(jid).getCreationTime()), new DEROctetString(converter.getPublicKey(keys.get(jid)).getEncoded()) })); }//from w ww. j a va 2 s . c o m DERSequence seq = new DERSequence(new ASN1Encodable[] { new DERSequence(new ASN1Encodable[] { new DERUTCTime(kp.getPublicKey().getCreationTime()), new DEROctetString(pub.getEncoded()) }), new DEROctetString(priv.getEncoded()), new DERSequence(pubSeq) }); return seq.getEncoded(); }
From source file:com.itextpdf.signatures.LtvVerification.java
License:Open Source License
private static byte[] buildOCSPResponse(byte[] BasicOCSPResponse) throws IOException { DEROctetString doctet = new DEROctetString(BasicOCSPResponse); ASN1EncodableVector v2 = new ASN1EncodableVector(); v2.add(OCSPObjectIdentifiers.id_pkix_ocsp_basic); v2.add(doctet);//ww w . j a v a 2 s .c om ASN1Enumerated den = new ASN1Enumerated(0); ASN1EncodableVector v3 = new ASN1EncodableVector(); v3.add(den); v3.add(new DERTaggedObject(true, 0, new DERSequence(v2))); DERSequence seq = new DERSequence(v3); return seq.getEncoded(); }
From source file:com.itextpdf.text.pdf.LtvVerification.java
License:Open Source License
private static byte[] buildOCSPResponse(byte[] BasicOCSPResponse) throws IOException { DEROctetString doctet = new DEROctetString(BasicOCSPResponse); ASN1EncodableVector v2 = new ASN1EncodableVector(); v2.add(OCSPObjectIdentifiers.id_pkix_ocsp_basic); v2.add(doctet);//from ww w .ja v a 2 s. c o m DEREnumerated den = new DEREnumerated(0); ASN1EncodableVector v3 = new ASN1EncodableVector(); v3.add(den); v3.add(new DERTaggedObject(true, 0, new DERSequence(v2))); DERSequence seq = new DERSequence(v3); return seq.getEncoded(); }
From source file:de.fichtelmax.asn1.ASN1PrinterTest.java
License:Open Source License
@Test public void printSequence() throws IOException { String text = "Hello World!"; String oid = "1.2.3.45.10982345"; long number = 12345678901l; DERUTF8String string = new DERUTF8String(text); DERObjectIdentifier objectIdentifier = new DERObjectIdentifier(oid); DERInteger integer = new DERInteger(number); DERSet set = new DERSet(new ASN1Encodable[] { objectIdentifier, integer }); DERSequence sequence = new DERSequence(new ASN1Encodable[] { string, set }); cut.print(sequence.getEncoded()); verify(out).println(contains(text)); verify(out).println(contains(oid));//w w w.j a va 2 s .c o m verify(out).println(contains(Long.toString(number))); }
From source file:dorkbox.build.util.jar.JarSignatureUtil.java
License:Apache License
/** * @return null if there is a problem with the certificate loading process. *//*w w w. j av a 2 s . com*/ public static final String extractSignatureHashFromSignatureBlock(byte[] signatureBlock) { ASN1InputStream sigStream = null; try { CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); InputStream signatureIn = new ByteArrayInputStream(signatureBlock); sigStream = new ASN1InputStream(signatureIn); ASN1Primitive signatureASN = sigStream.readObject(); ASN1Sequence seq = ASN1Sequence.getInstance(signatureASN); ASN1TaggedObject tagged = (ASN1TaggedObject) seq.getObjectAt(1); // Extract certificates SignedData newSignedData = SignedData.getInstance(tagged.getObject()); @SuppressWarnings("rawtypes") Enumeration newSigOjects = newSignedData.getCertificates().getObjects(); Object newSigElement = newSigOjects.nextElement(); if (newSigElement instanceof DERSequence) { DERSequence newSigDERElement = (DERSequence) newSigElement; InputStream newSigIn = new ByteArrayInputStream(newSigDERElement.getEncoded()); Certificate newSigCertificate = certFactory.generateCertificate(newSigIn); // certificate bytes byte[] newSigCertificateBytes = newSigCertificate.getEncoded(); String encodeToString = Base64Fast.encodeToString(newSigCertificateBytes, false); return encodeToString; } } catch (IOException e) { } catch (CertificateException e) { } finally { Sys.close(sigStream); } return null; }
From source file:dorkbox.build.util.jar.JarSignatureUtil.java
License:Apache License
/** * Verify that the two certificates MATCH from within a signature block (ie, * XXXXX.DSA in the META-INF directory). * * @return true if the two certificates are the same. false otherwise. *///from www . j av a2 s . co m public static final boolean compareCertificates(byte[] newSignatureContainerBytes, byte[] oldSignatureContainerBytes) { ASN1InputStream newSigStream = null; ASN1InputStream oldSigStream = null; try { CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); InputStream newSignatureIn = new ByteArrayInputStream(newSignatureContainerBytes); newSigStream = new ASN1InputStream(newSignatureIn); ASN1Primitive newSigASNPrim = newSigStream.readObject(); ContentInfo newSigContent = ContentInfo.getInstance(newSigASNPrim); InputStream oldSignatureIn = new ByteArrayInputStream(oldSignatureContainerBytes); oldSigStream = new ASN1InputStream(oldSignatureIn); ASN1Primitive oldSigASNPrim = oldSigStream.readObject(); ContentInfo oldSigContent = ContentInfo.getInstance(oldSigASNPrim); // Extract certificates SignedData newSignedData = SignedData.getInstance(newSigContent.getContent()); @SuppressWarnings("rawtypes") Enumeration newSigOjects = newSignedData.getCertificates().getObjects(); SignedData oldSignedData = SignedData.getInstance(oldSigContent.getContent()); @SuppressWarnings("rawtypes") Enumeration oldSigOjects = oldSignedData.getCertificates().getObjects(); Object newSigElement = newSigOjects.nextElement(); Object oldSigElement = oldSigOjects.nextElement(); if (newSigElement instanceof DERSequence && oldSigElement instanceof DERSequence) { DERSequence newSigDERElement = (DERSequence) newSigElement; InputStream newSigIn = new ByteArrayInputStream(newSigDERElement.getEncoded()); Certificate newSigCertificate = certFactory.generateCertificate(newSigIn); DERSequence oldSigDERElement = (DERSequence) oldSigElement; InputStream oldSigIn = new ByteArrayInputStream(oldSigDERElement.getEncoded()); Certificate oldSigCertificate = certFactory.generateCertificate(oldSigIn); // certificate bytes byte[] newSigCertificateBytes = newSigCertificate.getEncoded(); byte[] oldSigCertificateBytes = oldSigCertificate.getEncoded(); return Arrays.equals(newSigCertificateBytes, oldSigCertificateBytes); } } catch (IOException e) { } catch (CertificateException e) { } finally { Sys.close(newSigStream); Sys.close(oldSigStream); } return false; }
From source file:net.ripe.rpki.commons.provisioning.x509.pkcs10.RpkiCaCertificateRequestBuilder.java
License:BSD License
private Extensions createExtensions() throws IOException { // Make extension for SIA in request. See here: // http://www.bouncycastle.org/wiki/display/JA1/X.509+Public+Key+Certificate+and+Certification+Request+Generation List<Extension> extensions = new ArrayList<Extension>(); X509CertificateInformationAccessDescriptor[] descriptors = new X509CertificateInformationAccessDescriptor[] { new X509CertificateInformationAccessDescriptor( X509CertificateInformationAccessDescriptor.ID_AD_CA_REPOSITORY, caRepositoryUri), new X509CertificateInformationAccessDescriptor( X509CertificateInformationAccessDescriptor.ID_AD_RPKI_MANIFEST, manifestUri), }; AccessDescription[] subjectInformationAccess = X509CertificateInformationAccessDescriptor .convertAccessDescriptors(descriptors); DERSequence derSequence = new DERSequence(subjectInformationAccess); extensions.add(// w w w . j a v a 2 s. c o m new Extension(Extension.subjectInfoAccess, false, new DEROctetString(derSequence.getEncoded()))); KeyUsage keyUsage = new KeyUsage(KeyUsage.keyCertSign | KeyUsage.cRLSign); extensions.add(new Extension(Extension.keyUsage, true, new DEROctetString(keyUsage))); extensions.add( new Extension(Extension.basicConstraints, true, new DEROctetString(new BasicConstraints(true)))); return new Extensions(extensions.toArray(new Extension[extensions.size()])); }
From source file:net.sf.keystore_explorer.crypto.privatekey.OpenSslPvkUtil.java
License:Open Source License
/** * OpenSSL encode a private key./*from ww w. j a v a2 s. com*/ * * @return The encoding * @param privateKey * The private key * @throws CryptoException * Problem encountered while getting the encoded private key */ public static byte[] get(PrivateKey privateKey) throws CryptoException { // DER encoding for each key type is a sequence ASN1EncodableVector vec = new ASN1EncodableVector(); if (privateKey instanceof RSAPrivateCrtKey) { RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey; vec.add(new ASN1Integer(VERSION)); vec.add(new ASN1Integer(rsaPrivateKey.getModulus())); vec.add(new ASN1Integer(rsaPrivateKey.getPublicExponent())); vec.add(new ASN1Integer(rsaPrivateKey.getPrivateExponent())); vec.add(new ASN1Integer(rsaPrivateKey.getPrimeP())); vec.add(new ASN1Integer(rsaPrivateKey.getPrimeQ())); vec.add(new ASN1Integer(rsaPrivateKey.getPrimeExponentP())); vec.add(new ASN1Integer(rsaPrivateKey.getPrimeExponentQ())); vec.add(new ASN1Integer(rsaPrivateKey.getCrtCoefficient())); } else { DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey; DSAParams dsaParams = dsaPrivateKey.getParams(); BigInteger primeModulusP = dsaParams.getP(); BigInteger primeQ = dsaParams.getQ(); BigInteger generatorG = dsaParams.getG(); BigInteger secretExponentX = dsaPrivateKey.getX(); // Derive public key from private key parts, ie Y = G^X mod P BigInteger publicExponentY = generatorG.modPow(secretExponentX, primeModulusP); vec.add(new ASN1Integer(VERSION)); vec.add(new ASN1Integer(primeModulusP)); vec.add(new ASN1Integer(primeQ)); vec.add(new ASN1Integer(generatorG)); vec.add(new ASN1Integer(publicExponentY)); vec.add(new ASN1Integer(secretExponentX)); } DERSequence derSequence = new DERSequence(vec); try { return derSequence.getEncoded(); } catch (IOException ex) { throw new CryptoException(res.getString("NoDerEncodeOpenSslPrivateKey.exception.message"), ex); } }