Search in sources :

Example 26 with DLSequence

use of org.bouncycastle.asn1.DLSequence in project netty by netty.

the class OcspUtils method findObject.

private static <T> T findObject(DLSequence sequence, ASN1ObjectIdentifier oid, Class<T> type) {
    for (ASN1Encodable element : sequence) {
        if (!(element instanceof DLSequence)) {
            continue;
        }
        DLSequence subSequence = (DLSequence) element;
        if (subSequence.size() != 2) {
            continue;
        }
        ASN1Encodable key = subSequence.getObjectAt(0);
        ASN1Encodable value = subSequence.getObjectAt(1);
        if (key.equals(oid) && type.isInstance(value)) {
            return type.cast(value);
        }
    }
    return null;
}
Also used : DLSequence(org.bouncycastle.asn1.DLSequence) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable)

Example 27 with DLSequence

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

the class PEMInputOutput method writeDHParameters.

public static void writeDHParameters(Writer _out, DHParameterSpec params) throws IOException {
    final BufferedWriter out = makeBuffered(_out);
    ASN1EncodableVector v = new ASN1EncodableVector();
    BigInteger value;
    if ((value = params.getP()) != null) {
        v.add(new ASN1Integer(value));
    }
    if ((value = params.getG()) != null) {
        v.add(new ASN1Integer(value));
    }
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ASN1OutputStream aOut = new ASN1OutputStream(bOut);
    aOut.writeObject(new DLSequence(v));
    out.write(BEF_G);
    out.write(PEM_STRING_DHPARAMS);
    out.write(AFT);
    out.newLine();
    writeEncoded(out, bOut.buffer(), bOut.size());
    out.write(BEF_E);
    out.write(PEM_STRING_DHPARAMS);
    out.write(AFT);
    out.newLine();
    out.flush();
}
Also used : DLSequence(org.bouncycastle.asn1.DLSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) BigInteger(java.math.BigInteger) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) ByteArrayOutputStream(org.jruby.ext.openssl.util.ByteArrayOutputStream) ASN1OutputStream(org.bouncycastle.asn1.ASN1OutputStream) BufferedWriter(java.io.BufferedWriter)

Example 28 with DLSequence

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

the class PEMInputOutput method writeX509Aux.

public static void writeX509Aux(final Writer _out, final X509AuxCertificate cert) throws IOException {
    BufferedWriter out = makeBuffered(_out);
    final byte[] encoding;
    final int encLen;
    try {
        if (cert.aux == null) {
            encoding = cert.getEncoded();
            encLen = encoding.length;
        } else {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] enc = cert.getEncoded();
            baos.write(enc, 0, enc.length);
            final X509Aux aux = cert.aux;
            ASN1EncodableVector a1 = new ASN1EncodableVector();
            if (aux.trust.size() > 0) {
                ASN1EncodableVector a2 = new ASN1EncodableVector();
                for (String trust : aux.trust) {
                    a2.add(new ASN1ObjectIdentifier(trust));
                }
                a1.add(new DLSequence(a2));
            }
            if (aux.reject.size() > 0) {
                ASN1EncodableVector a2 = new ASN1EncodableVector();
                for (String reject : aux.reject) {
                    a2.add(new ASN1ObjectIdentifier(reject));
                }
                a1.add(new DERTaggedObject(0, new DLSequence(a2)));
            }
            if (aux.alias != null) {
                a1.add(new DERUTF8String(aux.alias));
            }
            if (aux.keyid != null) {
                a1.add(new DEROctetString(aux.keyid));
            }
            if (aux.other.size() > 0) {
                ASN1EncodableVector a2 = new ASN1EncodableVector();
                for (ASN1Primitive other : aux.other) a2.add(other);
                a1.add(new DERTaggedObject(1, new DLSequence(a2)));
            }
            enc = new DLSequence(a1).getEncoded();
            baos.write(enc, 0, enc.length);
            encoding = baos.buffer();
            encLen = baos.size();
        }
    } catch (CertificateEncodingException e) {
        throw new IOException("problem with encoding object in write_X509_AUX", e);
    }
    out.write(BEF_G + PEM_STRING_X509_TRUSTED + AFT);
    out.newLine();
    writeEncoded(out, encoding, encLen);
    out.write(BEF_E + PEM_STRING_X509_TRUSTED + AFT);
    out.newLine();
    out.flush();
}
Also used : DERUTF8String(org.bouncycastle.asn1.DERUTF8String) DERTaggedObject(org.bouncycastle.asn1.DERTaggedObject) CertificateEncodingException(java.security.cert.CertificateEncodingException) ByteArrayOutputStream(org.jruby.ext.openssl.util.ByteArrayOutputStream) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) DEROctetString(org.bouncycastle.asn1.DEROctetString) IOException(java.io.IOException) DEROctetString(org.bouncycastle.asn1.DEROctetString) BufferedWriter(java.io.BufferedWriter) DLSequence(org.bouncycastle.asn1.DLSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 29 with DLSequence

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

the class PEMInputOutput method writeDSAPrivateKey.

public static void writeDSAPrivateKey(Writer _out, DSAPrivateKey obj, CipherSpec cipher, char[] passwd) throws IOException {
    BufferedWriter out = makeBuffered(_out);
    PrivateKeyInfo info = new PrivateKeyInfo((ASN1Sequence) new ASN1InputStream(getEncoded(obj)).readObject());
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ASN1OutputStream aOut = new ASN1OutputStream(bOut);
    DSAParameter p = DSAParameter.getInstance(info.getPrivateKeyAlgorithm().getParameters());
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(new ASN1Integer(BigInteger.ZERO));
    v.add(new ASN1Integer(p.getP()));
    v.add(new ASN1Integer(p.getQ()));
    v.add(new ASN1Integer(p.getG()));
    BigInteger x = obj.getX();
    BigInteger y = p.getG().modPow(x, p.getP());
    v.add(new ASN1Integer(y));
    v.add(new ASN1Integer(x));
    aOut.writeObject(new DLSequence(v));
    if (cipher != null && passwd != null) {
        writePemEncrypted(out, PEM_STRING_DSA, bOut.buffer(), bOut.size(), cipher, passwd);
    } else {
        writePemPlain(out, PEM_STRING_DSA, bOut.buffer(), bOut.size());
    }
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) DLSequence(org.bouncycastle.asn1.DLSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) BigInteger(java.math.BigInteger) ByteArrayOutputStream(org.jruby.ext.openssl.util.ByteArrayOutputStream) DSAParameter(org.bouncycastle.asn1.x509.DSAParameter) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) ASN1OutputStream(org.bouncycastle.asn1.ASN1OutputStream) EncryptedPrivateKeyInfo(org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) BufferedWriter(java.io.BufferedWriter)

Example 30 with DLSequence

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

the class NetscapeSPKI method toDER.

private byte[] toDER() throws IOException {
    ASN1Sequence b = (ASN1Sequence) ((NetscapeCertRequest) cert).toASN1Primitive();
    ASN1ObjectIdentifier encType = (ASN1ObjectIdentifier) ((ASN1Sequence) ((ASN1Sequence) ((ASN1Sequence) b.getObjectAt(0)).getObjectAt(0)).getObjectAt(0)).getObjectAt(0);
    ASN1ObjectIdentifier sigAlg = ((AlgorithmIdentifier) b.getObjectAt(1)).getAlgorithm();
    DERBitString sig = (DERBitString) b.getObjectAt(2);
    DERBitString publicKey = new DERBitString(((PKey) public_key).to_der().convertToString().getBytes());
    DERIA5String encodedChallenge = new DERIA5String(this.challenge.toString());
    ASN1EncodableVector v1 = new ASN1EncodableVector();
    ASN1EncodableVector v1_2 = new ASN1EncodableVector();
    ASN1EncodableVector v2 = new ASN1EncodableVector();
    ASN1EncodableVector v3 = new ASN1EncodableVector();
    ASN1EncodableVector v4 = new ASN1EncodableVector();
    v4.add(encType);
    v4.add(DERNull.INSTANCE);
    v3.add(new DLSequence(v4));
    v3.add(publicKey);
    v2.add(new DLSequence(v3));
    v2.add(encodedChallenge);
    v1.add(new DLSequence(v2));
    v1_2.add(sigAlg);
    v1_2.add(DERNull.INSTANCE);
    v1.add(new DLSequence(v1_2));
    v1.add(sig);
    return new DLSequence(v1).getEncoded();
}
Also used : ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) DERIA5String(org.bouncycastle.asn1.DERIA5String) DLSequence(org.bouncycastle.asn1.DLSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) DERBitString(org.bouncycastle.asn1.DERBitString) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Aggregations

DLSequence (org.bouncycastle.asn1.DLSequence)37 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)21 DERTaggedObject (org.bouncycastle.asn1.DERTaggedObject)14 IOException (java.io.IOException)13 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)12 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)9 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)9 DEROctetString (org.bouncycastle.asn1.DEROctetString)9 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)8 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)6 DERIA5String (org.bouncycastle.asn1.DERIA5String)6 DERUTF8String (org.bouncycastle.asn1.DERUTF8String)6 BigInteger (java.math.BigInteger)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