Search in sources :

Example 11 with DLSequence

use of org.spongycastle.asn1.DLSequence in project jruby-openssl by jruby.

the class X509Cert method uniqueExtensions.

private Collection<X509Extension> uniqueExtensions() {
    final Map<ASN1ObjectIdentifier, X509Extension> unique = new LinkedHashMap<ASN1ObjectIdentifier, X509Extension>();
    for (X509Extension current : this.extensions) {
        final ASN1ObjectIdentifier oid = current.getRealObjectID();
        final X509Extension existing = unique.get(oid);
        if (existing == null) {
            unique.put(oid, current);
            continue;
        }
        // commonly used e.g. with subjectAltName || issuserAltName :
        if ("2.5.29.17".equals(oid.getId()) || "2.5.29.18".equals(oid.getId())) {
            final ASN1EncodableVector vec = new ASN1EncodableVector();
            try {
                GeneralName[] n1 = extRealNames(existing);
                for (int i = 0; i < n1.length; i++) vec.add(n1[i]);
                GeneralName[] n2 = extRealNames(current);
                for (int i = 0; i < n2.length; i++) vec.add(n2[i]);
                GeneralNames nn = GeneralNames.getInstance(new DLSequence(vec));
                final X509Extension existingDup = existing.clone();
                existingDup.setRealValue(nn);
                unique.put(oid, existingDup);
            } catch (IOException ex) {
                throw getRuntime().newIOErrorFromException(ex);
            }
            continue;
        }
        // TODO do we need special care for any others here ?!?
        final ASN1EncodableVector vec = new ASN1EncodableVector();
        try {
            final ASN1Encodable existingValue = existing.getRealValue();
            if (existingValue instanceof ASN1Sequence) {
                final ASN1Sequence seq = (ASN1Sequence) existingValue;
                for (int i = 0; i < seq.size(); i++) {
                    vec.add(seq.getObjectAt(i));
                }
            } else {
                vec.add(existingValue);
            }
            vec.add(current.getRealValue());
            // existing.setRealValue( new DLSequence(vec) );
            final X509Extension existingDup = existing.clone();
            existingDup.setRealValue(new DLSequence(vec));
            unique.put(oid, existingDup);
        } catch (IOException ex) {
            throw getRuntime().newIOErrorFromException(ex);
        }
    }
    return unique.values();
}
Also used : IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) GeneralNames(org.bouncycastle.asn1.x509.GeneralNames) DLSequence(org.bouncycastle.asn1.DLSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) GeneralName(org.bouncycastle.asn1.x509.GeneralName) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 12 with DLSequence

use of org.spongycastle.asn1.DLSequence in project jruby-openssl by jruby.

the class X509Name method to_der.

@JRubyMethod
public RubyString to_der(final ThreadContext context) {
    final Ruby runtime = context.runtime;
    final DLSequence seq;
    if (oids.size() > 0) {
        ASN1EncodableVector vec = new ASN1EncodableVector();
        ASN1EncodableVector sVec = new ASN1EncodableVector();
        ASN1ObjectIdentifier lastOid = null;
        for (int i = 0; i != oids.size(); i++) {
            final ASN1ObjectIdentifier oid = oids.get(i);
            ASN1EncodableVector v = new ASN1EncodableVector();
            v.add(oid);
            // TODO DO NOT USE DL types !
            // final String value = values.get(i);
            // final int type = RubyNumeric.fix2int(types.get(i));
            // v.add( convert(oid, value, type) );
            v.add(values.get(i));
            if (lastOid == null) {
                sVec.add(new DLSequence(v));
            } else {
                vec.add(new DLSet(sVec));
                sVec = new ASN1EncodableVector();
                sVec.add(new DLSequence(v));
            }
            lastOid = oid;
        }
        vec.add(new DLSet(sVec));
        seq = new DLSequence(vec);
    } else {
        seq = new DLSequence();
    }
    try {
        return StringHelper.newString(runtime, seq.getEncoded(ASN1Encoding.DER));
    } catch (IOException e) {
        throw newNameError(runtime, e);
    }
}
Also used : DLSequence(org.bouncycastle.asn1.DLSequence) DLSet(org.bouncycastle.asn1.DLSet) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) IOException(java.io.IOException) Ruby(org.jruby.Ruby) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 13 with DLSequence

use of org.spongycastle.asn1.DLSequence in project jruby-openssl by jruby.

the class PKey method toDerRSAKey.

public static byte[] toDerRSAKey(RSAPublicKey pubKey, RSAPrivateCrtKey privKey) throws IOException {
    ASN1EncodableVector vec = new ASN1EncodableVector();
    if (pubKey != null && privKey == null) {
        vec.add(new ASN1Integer(pubKey.getModulus()));
        vec.add(new ASN1Integer(pubKey.getPublicExponent()));
    } else {
        vec.add(new ASN1Integer(BigInteger.ZERO));
        vec.add(new ASN1Integer(privKey.getModulus()));
        vec.add(new ASN1Integer(privKey.getPublicExponent()));
        vec.add(new ASN1Integer(privKey.getPrivateExponent()));
        vec.add(new ASN1Integer(privKey.getPrimeP()));
        vec.add(new ASN1Integer(privKey.getPrimeQ()));
        vec.add(new ASN1Integer(privKey.getPrimeExponentP()));
        vec.add(new ASN1Integer(privKey.getPrimeExponentQ()));
        vec.add(new ASN1Integer(privKey.getCrtCoefficient()));
    }
    return new DLSequence(vec).getEncoded();
}
Also used : DLSequence(org.bouncycastle.asn1.DLSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1Integer(org.bouncycastle.asn1.ASN1Integer)

Example 14 with DLSequence

use of org.spongycastle.asn1.DLSequence 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 15 with DLSequence

use of org.spongycastle.asn1.DLSequence 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

DLSequence (org.bouncycastle.asn1.DLSequence)35 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)21 IOException (java.io.IOException)13 DERTaggedObject (org.bouncycastle.asn1.DERTaggedObject)13 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)12 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)9 DEROctetString (org.bouncycastle.asn1.DEROctetString)9 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)8 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)8 DERIA5String (org.bouncycastle.asn1.DERIA5String)6 DERUTF8String (org.bouncycastle.asn1.DERUTF8String)6 BigInteger (java.math.BigInteger)5 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)5 Pair (android.util.Pair)3 BufferedWriter (java.io.BufferedWriter)3 ASN1OutputStream (org.bouncycastle.asn1.ASN1OutputStream)3 JRubyMethod (org.jruby.anno.JRubyMethod)3 ByteArrayOutputStream (org.jruby.ext.openssl.util.ByteArrayOutputStream)3 CertificateEncodingException (java.security.cert.CertificateEncodingException)2 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)2