Search in sources :

Example 56 with ASN1Integer

use of org.bouncycastle.asn1.ASN1Integer in project jruby-openssl by jruby.

the class PKey method readPrivateKey.

public static KeyPair readPrivateKey(final byte[] input, final String type) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    KeySpec pubSpec;
    KeySpec privSpec;
    ASN1Sequence seq = (ASN1Sequence) new ASN1InputStream(input).readObject();
    if (type.equals("RSA")) {
        ASN1Integer mod = (ASN1Integer) seq.getObjectAt(1);
        ASN1Integer pubExp = (ASN1Integer) seq.getObjectAt(2);
        ASN1Integer privExp = (ASN1Integer) seq.getObjectAt(3);
        ASN1Integer p1 = (ASN1Integer) seq.getObjectAt(4);
        ASN1Integer p2 = (ASN1Integer) seq.getObjectAt(5);
        ASN1Integer exp1 = (ASN1Integer) seq.getObjectAt(6);
        ASN1Integer exp2 = (ASN1Integer) seq.getObjectAt(7);
        ASN1Integer crtCoef = (ASN1Integer) seq.getObjectAt(8);
        pubSpec = new RSAPublicKeySpec(mod.getValue(), pubExp.getValue());
        privSpec = new RSAPrivateCrtKeySpec(mod.getValue(), pubExp.getValue(), privExp.getValue(), p1.getValue(), p2.getValue(), exp1.getValue(), exp2.getValue(), crtCoef.getValue());
    } else if (type.equals("DSA")) {
        ASN1Integer p = (ASN1Integer) seq.getObjectAt(1);
        ASN1Integer q = (ASN1Integer) seq.getObjectAt(2);
        ASN1Integer g = (ASN1Integer) seq.getObjectAt(3);
        ASN1Integer y = (ASN1Integer) seq.getObjectAt(4);
        ASN1Integer x = (ASN1Integer) seq.getObjectAt(5);
        privSpec = new DSAPrivateKeySpec(x.getValue(), p.getValue(), q.getValue(), g.getValue());
        pubSpec = new DSAPublicKeySpec(y.getValue(), p.getValue(), q.getValue(), g.getValue());
    } else if (type.equals("ECDSA")) {
        return readECPrivateKey(input);
    } else {
        throw new IllegalStateException("unsupported type: " + type);
    }
    KeyFactory fact = SecurityHelper.getKeyFactory(type);
    return new KeyPair(fact.generatePublic(pubSpec), fact.generatePrivate(privSpec));
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) KeyPair(java.security.KeyPair) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) KeySpec(java.security.spec.KeySpec) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 57 with ASN1Integer

use of org.bouncycastle.asn1.ASN1Integer in project jruby-openssl by jruby.

the class RecipInfo method asASN1.

public ASN1Encodable asASN1() {
    ASN1EncodableVector vector = new ASN1EncodableVector();
    vector.add(new ASN1Integer(BigInteger.valueOf(getVersion())));
    vector.add(issuerAndSerial.toASN1Primitive());
    vector.add(keyEncAlgor.toASN1Primitive());
    vector.add(encKey.toASN1Primitive());
    return new DLSequence(vector);
}
Also used : DLSequence(org.bouncycastle.asn1.DLSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1Integer(org.bouncycastle.asn1.ASN1Integer)

Example 58 with ASN1Integer

use of org.bouncycastle.asn1.ASN1Integer in project jruby-openssl by jruby.

the class RecipInfo method fromASN1.

/**
 * RecipientInfo ::= SEQUENCE {
 *   version Version,
 *   issuerAndSerialNumber IssuerAndSerialNumber,
 *   keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,
 *   encryptedKey EncryptedKey }
 *
 * EncryptedKey ::= OCTET STRING
 */
public static RecipInfo fromASN1(ASN1Encodable content) {
    ASN1Sequence sequence = (ASN1Sequence) content;
    RecipInfo ri = new RecipInfo();
    ri.setVersion(((ASN1Integer) sequence.getObjectAt(0)).getValue().intValue());
    ri.setIssuerAndSerial(IssuerAndSerialNumber.getInstance(sequence.getObjectAt(1)));
    ri.setKeyEncAlgor(AlgorithmIdentifier.getInstance(sequence.getObjectAt(2)));
    ri.setEncKey((ASN1OctetString) sequence.getObjectAt(3));
    return ri;
}
Also used : ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ASN1Integer(org.bouncycastle.asn1.ASN1Integer)

Example 59 with ASN1Integer

use of org.bouncycastle.asn1.ASN1Integer in project jruby-openssl by jruby.

the class Signed method fromASN1.

/**
 * SignedData ::= SEQUENCE {
 *   version Version,
 *   digestAlgorithms DigestAlgorithmIdentifiers,
 *   contentInfo ContentInfo,
 *   certificates [0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL,
 *   crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
 *   signerInfos SignerInfos }
 *
 * Version ::= INTEGER
 *
 * DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmIdentifier
 *
 * SignerInfos ::= SET OF SignerInfo
 */
public static Signed fromASN1(ASN1Encodable content) throws PKCS7Exception {
    ASN1Sequence sequence = (ASN1Sequence) content;
    ASN1Integer version = (ASN1Integer) sequence.getObjectAt(0);
    ASN1Set digestAlgos = (ASN1Set) sequence.getObjectAt(1);
    ASN1Encodable contentInfo = sequence.getObjectAt(2);
    ASN1Encodable certificates = null;
    ASN1Encodable crls = null;
    int index = 3;
    ASN1Encodable tmp = sequence.getObjectAt(index);
    if ((tmp instanceof ASN1TaggedObject) && ((ASN1TaggedObject) tmp).getTagNo() == 0) {
        certificates = ((ASN1TaggedObject) tmp).getObject();
        index++;
    }
    tmp = sequence.getObjectAt(index);
    if ((tmp instanceof ASN1TaggedObject) && ((ASN1TaggedObject) tmp).getTagNo() == 1) {
        crls = ((ASN1TaggedObject) tmp).getObject();
        index++;
    }
    ASN1Set signerInfos = (ASN1Set) sequence.getObjectAt(index);
    Signed signed = new Signed();
    signed.setVersion(version.getValue().intValue());
    signed.setMdAlgs(algorithmIdentifiersFromASN1Set(digestAlgos));
    signed.setContents(PKCS7.fromASN1(contentInfo));
    if (certificates != null) {
        signed.setCert(certificatesFromASN1Set(certificates));
    }
    if (crls != null) {
        throw new RuntimeException("TODO: implement CRL part");
    }
    signed.setSignerInfo(signerInfosFromASN1Set(signerInfos));
    return signed;
}
Also used : ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ASN1Set(org.bouncycastle.asn1.ASN1Set) ASN1TaggedObject(org.bouncycastle.asn1.ASN1TaggedObject) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable)

Example 60 with ASN1Integer

use of org.bouncycastle.asn1.ASN1Integer in project jruby-openssl by jruby.

the class Signed method asASN1.

public ASN1Encodable asASN1() {
    ASN1EncodableVector vector = new ASN1EncodableVector();
    vector.add(new ASN1Integer(BigInteger.valueOf(version)));
    vector.add(digestAlgorithmsToASN1Set());
    if (contents == null) {
        contents = PKCS7.newEmpty();
    }
    vector.add(contents.asASN1());
    if (cert != null && cert.size() > 0) {
        if (cert.size() > 1) {
            vector.add(new DERTaggedObject(false, 0, certificatesToASN1Set()));
        } else {
            // Encode the signer certificate directly for OpenSSL compatibility.
            // OpenSSL does not support multiple signer signature.
            // And OpenSSL requires EXPLICIT tagging.
            vector.add(new DERTaggedObject(true, 0, firstCertificatesToASN1()));
        }
    }
    if (crl != null && crl.size() > 0) {
        vector.add(new DERTaggedObject(false, 1, crlsToASN1Set()));
    }
    vector.add(signerInfosToASN1Set());
    return new DLSequence(vector);
}
Also used : DLSequence(org.bouncycastle.asn1.DLSequence) DERTaggedObject(org.bouncycastle.asn1.DERTaggedObject) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1Integer(org.bouncycastle.asn1.ASN1Integer)

Aggregations

ASN1Integer (org.bouncycastle.asn1.ASN1Integer)127 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)56 BigInteger (java.math.BigInteger)54 DERSequence (org.bouncycastle.asn1.DERSequence)51 IOException (java.io.IOException)44 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)43 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)29 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)21 DEROctetString (org.bouncycastle.asn1.DEROctetString)21 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)20 ArrayList (java.util.ArrayList)18 DERUTF8String (org.bouncycastle.asn1.DERUTF8String)17 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)16 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)15 X509Certificate (java.security.cert.X509Certificate)14 Date (java.util.Date)12 DLSequence (org.bouncycastle.asn1.DLSequence)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 KeyPair (java.security.KeyPair)11 HashMap (java.util.HashMap)11