Search in sources :

Example 81 with ASN1Sequence

use of com.android.org.bouncycastle.asn1.ASN1Sequence in project jruby-openssl by jruby.

the class PKey method readRSAPrivateKey.

public static KeyPair readRSAPrivateKey(final KeyFactory rsaFactory, final byte[] input) throws IOException, InvalidKeySpecException {
    ASN1Sequence seq = (ASN1Sequence) new ASN1InputStream(input).readObject();
    if (seq.size() == 9) {
        BigInteger mod = ((ASN1Integer) seq.getObjectAt(1)).getValue();
        BigInteger pubexp = ((ASN1Integer) seq.getObjectAt(2)).getValue();
        BigInteger privexp = ((ASN1Integer) seq.getObjectAt(3)).getValue();
        BigInteger primep = ((ASN1Integer) seq.getObjectAt(4)).getValue();
        BigInteger primeq = ((ASN1Integer) seq.getObjectAt(5)).getValue();
        BigInteger primeep = ((ASN1Integer) seq.getObjectAt(6)).getValue();
        BigInteger primeeq = ((ASN1Integer) seq.getObjectAt(7)).getValue();
        BigInteger crtcoeff = ((ASN1Integer) seq.getObjectAt(8)).getValue();
        PrivateKey priv = rsaFactory.generatePrivate(new RSAPrivateCrtKeySpec(mod, pubexp, privexp, primep, primeq, primeep, primeeq, crtcoeff));
        PublicKey pub = rsaFactory.generatePublic(new RSAPublicKeySpec(mod, pubexp));
        return new KeyPair(pub, priv);
    }
    return null;
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) KeyPair(java.security.KeyPair) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) PrivateKey(java.security.PrivateKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) BigInteger(java.math.BigInteger) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Example 82 with ASN1Sequence

use of com.android.org.bouncycastle.asn1.ASN1Sequence in project jruby-openssl by jruby.

the class PKey method readRSAPublicKey.

public static PublicKey readRSAPublicKey(final KeyFactory rsaFactory, final byte[] input) throws IOException, InvalidKeySpecException {
    ASN1Sequence seq = (ASN1Sequence) new ASN1InputStream(input).readObject();
    if (seq.size() == 2) {
        BigInteger mod = ((ASN1Integer) seq.getObjectAt(0)).getValue();
        BigInteger pubexp = ((ASN1Integer) seq.getObjectAt(1)).getValue();
        return rsaFactory.generatePublic(new RSAPublicKeySpec(mod, pubexp));
    }
    return null;
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) BigInteger(java.math.BigInteger) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Example 83 with ASN1Sequence

use of com.android.org.bouncycastle.asn1.ASN1Sequence 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 84 with ASN1Sequence

use of com.android.org.bouncycastle.asn1.ASN1Sequence 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 85 with ASN1Sequence

use of com.android.org.bouncycastle.asn1.ASN1Sequence 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)

Aggregations

ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)202 IOException (java.io.IOException)70 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)56 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)49 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)41 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)36 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)36 ArrayList (java.util.ArrayList)35 DEROctetString (org.bouncycastle.asn1.DEROctetString)35 CRLDistPoint (org.bouncycastle.asn1.x509.CRLDistPoint)32 X509Certificate (java.security.cert.X509Certificate)31 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)31 DERSequence (org.bouncycastle.asn1.DERSequence)30 Enumeration (java.util.Enumeration)29 BigInteger (java.math.BigInteger)28 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)28 DERIA5String (org.bouncycastle.asn1.DERIA5String)28 DistributionPoint (org.bouncycastle.asn1.x509.DistributionPoint)28 DERUTF8String (org.bouncycastle.asn1.DERUTF8String)26 List (java.util.List)25