Search in sources :

Example 31 with DLSequence

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

the class PKCS7 method sign.

/* c: PKCS7_sign
     *
     */
public static PKCS7 sign(X509AuxCertificate signcert, PrivateKey pkey, Collection<X509AuxCertificate> certs, BIO data, int flags) throws PKCS7Exception {
    PKCS7 p7 = new PKCS7();
    p7.setType(ASN1Registry.NID_pkcs7_signed);
    p7.contentNew(ASN1Registry.NID_pkcs7_data);
    SignerInfoWithPkey si = p7.addSignature(signcert, pkey, EVP.sha1());
    if ((flags & NOCERTS) == 0) {
        p7.addCertificate(signcert);
        if (certs != null) {
            for (X509AuxCertificate c : certs) {
                p7.addCertificate(c);
            }
        }
    }
    if ((flags & NOATTR) == 0) {
        si.addSignedAttribute(ASN1Registry.NID_pkcs9_contentType, OID_pkcs7_data);
        if ((flags & NOSMIMECAP) == 0) {
            ASN1EncodableVector smcap = new ASN1EncodableVector();
            smcap.add(new AlgorithmIdentifier(OID_des_ede3_cbc));
            smcap.add(new AlgorithmIdentifier(OID_rc2_cbc, new ASN1Integer(BI_128)));
            smcap.add(new AlgorithmIdentifier(OID_rc2_cbc, new ASN1Integer(BI_64)));
            smcap.add(new AlgorithmIdentifier(OID_rc2_cbc, new ASN1Integer(BI_40)));
            smcap.add(new AlgorithmIdentifier(OID_des_cbc));
            si.addSignedAttribute(ASN1Registry.NID_SMIMECapabilities, new DLSequence(smcap));
        }
    }
    if ((flags & STREAM) != 0) {
        return p7;
    }
    BIO p7bio = p7.dataInit(null);
    try {
        data.crlfCopy(p7bio, flags);
    } catch (IOException e) {
        throw new PKCS7Exception(F_PKCS7_SIGN, R_PKCS7_DATAFINAL_ERROR, e);
    }
    if ((flags & DETACHED) != 0) {
        p7.setDetached(1);
    }
    p7.dataFinal(p7bio);
    return p7;
}
Also used : DLSequence(org.bouncycastle.asn1.DLSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) IOException(java.io.IOException) X509AuxCertificate(org.jruby.ext.openssl.x509store.X509AuxCertificate) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 32 with DLSequence

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

the class PKCS7 method asASN1.

public ASN1Encodable asASN1() {
    ASN1EncodableVector vector = new ASN1EncodableVector();
    ASN1ObjectIdentifier contentType;
    if (data == null) {
        // OpenSSL behavior
        contentType = new ASN1ObjectIdentifier(EMPTY_PKCS7_OID);
    } else {
        contentType = ASN1Registry.nid2obj(getType());
    }
    vector.add(contentType);
    if (data != null) {
        vector.add(new DERTaggedObject(0, data.asASN1()));
    }
    return new DLSequence(vector);
}
Also used : DLSequence(org.bouncycastle.asn1.DLSequence) DERTaggedObject(org.bouncycastle.asn1.DERTaggedObject) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 33 with DLSequence

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

the class PKey method toDerDSAKey.

public static byte[] toDerDSAKey(DSAPublicKey pubKey, DSAPrivateKey privKey) throws IOException {
    if (pubKey != null && privKey == null) {
        return pubKey.getEncoded();
    }
    if (privKey != null && pubKey != null) {
        ASN1EncodableVector vec = new ASN1EncodableVector();
        final DSAParams params = privKey.getParams();
        vec.add(new ASN1Integer(BigInteger.ZERO));
        vec.add(new ASN1Integer(params.getP()));
        vec.add(new ASN1Integer(params.getQ()));
        vec.add(new ASN1Integer(params.getG()));
        vec.add(new ASN1Integer(pubKey.getY()));
        vec.add(new ASN1Integer(privKey.getX()));
        return new DLSequence(vec).getEncoded();
    }
    if (privKey == null) {
        throw new IllegalArgumentException("private key as well as public key are null");
    }
    return privKey.getEncoded();
}
Also used : DLSequence(org.bouncycastle.asn1.DLSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) DSAParams(java.security.interfaces.DSAParams) ASN1Integer(org.bouncycastle.asn1.ASN1Integer)

Example 34 with DLSequence

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

the class SignerInfoWithPkey method toASN1Object.

/**
 * Produce an object suitable for an ASN1OutputStream.
 * <pre>
 *  SignerInfo ::= SEQUENCE {
 *      version Version,
 *      issuerAndSerialNumber IssuerAndSerialNumber,
 *      digestAlgorithm DigestAlgorithmIdentifier,
 *      authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL,
 *      digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier,
 *      encryptedDigest EncryptedDigest,
 *      unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
 *  }
 *
 *  EncryptedDigest ::= OCTET STRING
 *
 *  DigestAlgorithmIdentifier ::= AlgorithmIdentifier
 *
 *  DigestEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
 * </pre>
 */
public ASN1Encodable toASN1Object() {
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(version);
    v.add(issuerAndSerialNumber);
    v.add(digAlgorithm);
    if (authenticatedAttributes != null) {
        v.add(new DERTaggedObject(false, 0, authenticatedAttributes));
    }
    v.add(digEncryptionAlgorithm);
    v.add(encryptedDigest);
    if (unauthenticatedAttributes != null) {
        v.add(new DERTaggedObject(false, 1, unauthenticatedAttributes));
    }
    return new DLSequence(v);
}
Also used : DLSequence(org.bouncycastle.asn1.DLSequence) DERTaggedObject(org.bouncycastle.asn1.DERTaggedObject) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector)

Example 35 with DLSequence

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

the class Envelope method asASN1.

public ASN1Encodable asASN1() {
    ASN1EncodableVector vector = new ASN1EncodableVector();
    vector.add(new ASN1Integer(BigInteger.valueOf(version)));
    vector.add(receipientInfosToASN1Set());
    vector.add(encData.asASN1());
    return new DLSequence(vector);
}
Also used : DLSequence(org.bouncycastle.asn1.DLSequence) 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