Search in sources :

Example 41 with Asn1Object

use of com.android.hotspot2.asn1.Asn1Object in project jruby-openssl by jruby.

the class PEMInputOutput method readPrivateKey.

/**
 * c: PEM_read_PrivateKey + PEM_read_bio_PrivateKey
 * CAUTION: KeyPair#getPublic() may be null.
 */
public static KeyPair readPrivateKey(final Reader in, char[] passwd) throws PasswordRequiredException, IOException {
    final String BEG_STRING_ECPRIVATEKEY = BEF_G + PEM_STRING_ECPRIVATEKEY;
    final String BEG_STRING_PKCS8INF = BEF_G + PEM_STRING_PKCS8INF;
    final String BEG_STRING_PKCS8 = BEF_G + PEM_STRING_PKCS8;
    final BufferedReader reader = makeBuffered(in);
    String line;
    while ((line = reader.readLine()) != null) {
        if (line.indexOf(BEG_STRING_RSA) != -1) {
            try {
                return readKeyPair(reader, passwd, "RSA", BEF_E + PEM_STRING_RSA);
            } catch (Exception e) {
                throw mapReadException("problem creating RSA private key: ", e);
            }
        } else if (line.indexOf(BEG_STRING_DSA) != -1) {
            try {
                return readKeyPair(reader, passwd, "DSA", BEF_E + PEM_STRING_DSA);
            } catch (Exception e) {
                throw mapReadException("problem creating DSA private key: ", e);
            }
        } else if (line.indexOf(BEG_STRING_ECPRIVATEKEY) != -1) {
            try {
                return readKeyPair(reader, passwd, "ECDSA", BEF_E + PEM_STRING_ECPRIVATEKEY);
            } catch (Exception e) {
                throw mapReadException("problem creating DSA private key: ", e);
            }
        } else if (line.indexOf(BEG_STRING_PKCS8INF) != -1) {
            try {
                byte[] bytes = readBase64Bytes(reader, BEF_E + PEM_STRING_PKCS8INF);
                PrivateKeyInfo info = PrivateKeyInfo.getInstance(bytes);
                String type = getPrivateKeyTypeFromObjectId(info.getPrivateKeyAlgorithm().getAlgorithm());
                return org.jruby.ext.openssl.impl.PKey.readPrivateKey(((ASN1Object) info.parsePrivateKey()).getEncoded(ASN1Encoding.DER), type);
            } catch (Exception e) {
                throw mapReadException("problem creating private key: ", e);
            }
        } else if (line.indexOf(BEG_STRING_PKCS8) != -1) {
            try {
                byte[] bytes = readBase64Bytes(reader, BEF_E + PEM_STRING_PKCS8);
                EncryptedPrivateKeyInfo eIn = EncryptedPrivateKeyInfo.getInstance(bytes);
                AlgorithmIdentifier algId = eIn.getEncryptionAlgorithm();
                PrivateKey privKey;
                if (algId.getAlgorithm().toString().equals("1.2.840.113549.1.5.13")) {
                    // PBES2
                    privKey = derivePrivateKeyPBES2(eIn, algId, passwd);
                } else {
                    privKey = derivePrivateKeyPBES1(eIn, algId, passwd);
                }
                return new KeyPair(null, privKey);
            } catch (Exception e) {
                throw mapReadException("problem creating private key: ", e);
            }
        }
    }
    return null;
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) BufferedReader(java.io.BufferedReader) EncryptedPrivateKeyInfo(org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) DEROctetString(org.bouncycastle.asn1.DEROctetString) ASN1Object(org.bouncycastle.asn1.ASN1Object) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) CertificateEncodingException(java.security.cert.CertificateEncodingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) CMSException(org.bouncycastle.cms.CMSException) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) IOException(java.io.IOException) CRLException(java.security.cert.CRLException) CertificateException(java.security.cert.CertificateException) EncryptedPrivateKeyInfo(org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 42 with Asn1Object

use of com.android.hotspot2.asn1.Asn1Object in project pdfbox by apache.

the class CertInformationCollector method addTimestampCerts.

/**
 * Processes an embedded signed timestamp, that has been placed into a signature. The
 * certificates and its chain(s) will be processed the same way as the signature itself.
 *
 * @param signerInformation of the signature, to get unsigned attributes from it.
 * @throws IOException
 * @throws CertificateProccessingException
 */
private void addTimestampCerts(SignerInformation signerInformation) throws IOException, CertificateProccessingException {
    AttributeTable unsignedAttributes = signerInformation.getUnsignedAttributes();
    if (unsignedAttributes == null) {
        return;
    }
    Attribute tsAttribute = unsignedAttributes.get(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken);
    if (tsAttribute == null) {
        return;
    }
    ASN1Encodable obj0 = tsAttribute.getAttrValues().getObjectAt(0);
    if (!(obj0 instanceof ASN1Object)) {
        return;
    }
    ASN1Object tsSeq = (ASN1Object) obj0;
    try {
        CMSSignedData signedData = new CMSSignedData(tsSeq.getEncoded("DER"));
        rootCertInfo.tsaCerts = new CertSignatureInformation();
        processSignerStore(signedData, rootCertInfo.tsaCerts);
    } catch (CMSException e) {
        throw new IOException("Error parsing timestamp token", e);
    }
}
Also used : Attribute(org.bouncycastle.asn1.cms.Attribute) AttributeTable(org.bouncycastle.asn1.cms.AttributeTable) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) IOException(java.io.IOException) ASN1Object(org.bouncycastle.asn1.ASN1Object) CMSSignedData(org.bouncycastle.cms.CMSSignedData) CMSException(org.bouncycastle.cms.CMSException)

Example 43 with Asn1Object

use of com.android.hotspot2.asn1.Asn1Object in project xades4j by luisgoncalves.

the class CrlExtensionsUtils method getCrlNumber.

public static BigInteger getCrlNumber(X509CRL crl) throws IOException {
    byte[] crlNumEnc = crl.getExtensionValue(X509Extension.cRLNumber.getId());
    BigInteger crlNum = null;
    // XAdES 7.4.2: "The 'number' element is an optional hint ..."
    if (crlNumEnc != null) {
        ASN1Object derCrlNum = X509ExtensionUtil.fromExtensionValue(crlNumEnc);
        crlNum = CRLNumber.getInstance(derCrlNum).getCRLNumber();
    }
    return crlNum;
}
Also used : BigInteger(java.math.BigInteger) ASN1Object(org.bouncycastle.asn1.ASN1Object)

Aggregations

IOException (java.io.IOException)27 Asn1Object (com.android.hotspot2.asn1.Asn1Object)25 Asn1Constructed (com.android.hotspot2.asn1.Asn1Constructed)20 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)15 X509Certificate (java.security.cert.X509Certificate)12 Asn1Integer (com.android.hotspot2.asn1.Asn1Integer)10 DERBitString (com.android.org.bouncycastle.asn1.DERBitString)10 DERIA5String (com.android.org.bouncycastle.asn1.DERIA5String)10 DERPrintableString (com.android.org.bouncycastle.asn1.DERPrintableString)10 ByteBuffer (java.nio.ByteBuffer)10 ASN1Object (org.bouncycastle.asn1.ASN1Object)9 ByteArrayInputStream (java.io.ByteArrayInputStream)6 KeyPair (java.security.KeyPair)6 HashSet (java.util.HashSet)6 List (java.util.List)6 Set (java.util.Set)6 I18Name (com.android.anqp.I18Name)5 Asn1Octets (com.android.hotspot2.asn1.Asn1Octets)5 Asn1Oid (com.android.hotspot2.asn1.Asn1Oid)5