Search in sources :

Example 41 with ASN1Set

use of com.unboundid.asn1.ASN1Set 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 42 with ASN1Set

use of com.unboundid.asn1.ASN1Set in project jruby-openssl by jruby.

the class Signed method algorithmIdentifiersFromASN1Set.

private static Set<AlgorithmIdentifier> algorithmIdentifiersFromASN1Set(ASN1Encodable content) {
    ASN1Set set = (ASN1Set) content;
    Set<AlgorithmIdentifier> result = new HashSet<AlgorithmIdentifier>();
    for (Enumeration<?> e = set.getObjects(); e.hasMoreElements(); ) {
        result.add(AlgorithmIdentifier.getInstance(e.nextElement()));
    }
    return result;
}
Also used : ASN1Set(org.bouncycastle.asn1.ASN1Set) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier) HashSet(java.util.HashSet)

Example 43 with ASN1Set

use of com.unboundid.asn1.ASN1Set in project jruby-openssl by jruby.

the class X509Request method initialize.

@JRubyMethod(name = "initialize", rest = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
    final Ruby runtime = context.runtime;
    if (Arity.checkArgumentCount(runtime, args, 0, 1) == 0)
        return this;
    try {
        request = new PKCS10Request(StringHelper.readX509PEM(context, args[0]));
    } catch (RuntimeException e) {
        debugStackTrace(runtime, e);
        throw newRequestError(runtime, "invalid certificate request data", e);
    }
    final String algorithm;
    final byte[] encoded;
    try {
        final PublicKey pkey = request.generatePublicKey();
        algorithm = pkey.getAlgorithm();
        encoded = pkey.getEncoded();
    } catch (IOException e) {
        throw newRequestError(runtime, e);
    } catch (GeneralSecurityException e) {
        throw newRequestError(runtime, e);
    }
    final RubyString enc = RubyString.newString(runtime, encoded);
    if ("RSA".equalsIgnoreCase(algorithm)) {
        this.public_key = newPKeyImplInstance(context, "RSA", enc);
    } else if ("DSA".equalsIgnoreCase(algorithm)) {
        this.public_key = newPKeyImplInstance(context, "DSA", enc);
    } else {
        throw runtime.newNotImplementedError("public key algorithm: " + algorithm);
    }
    this.subject = newName(context, request.getSubject());
    final Attribute[] attrs = request.getAttributes();
    try {
        // final RubyModule _ASN1 = _ASN1(runtime);
        if (attrs != null) {
            for (final Attribute attr : attrs) {
                final ASN1ObjectIdentifier type = attr.getAttrType();
                final ASN1Set values = attr.getAttrValues();
                attributes.add(newAttribute(context, type, values));
            }
        }
    } catch (IOException e) {
        throw newRequestError(runtime, e);
    }
    return this;
}
Also used : PKCS10Request(org.jruby.ext.openssl.impl.PKCS10Request) Attribute(org.bouncycastle.asn1.pkcs.Attribute) PublicKey(java.security.PublicKey) GeneralSecurityException(java.security.GeneralSecurityException) RubyString(org.jruby.RubyString) RubyString(org.jruby.RubyString) IOException(java.io.IOException) ASN1Set(org.bouncycastle.asn1.ASN1Set) Ruby(org.jruby.Ruby) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 44 with ASN1Set

use of com.unboundid.asn1.ASN1Set in project jruby-openssl by jruby.

the class Envelope method recipientInfosFromASN1Set.

private static Collection<RecipInfo> recipientInfosFromASN1Set(ASN1Encodable content) {
    ASN1Set set = (ASN1Set) content;
    Collection<RecipInfo> result = new ArrayList<RecipInfo>();
    for (Enumeration<?> e = set.getObjects(); e.hasMoreElements(); ) {
        result.add(RecipInfo.fromASN1((ASN1Encodable) e.nextElement()));
    }
    return result;
}
Also used : ASN1Set(org.bouncycastle.asn1.ASN1Set) ArrayList(java.util.ArrayList) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable)

Example 45 with ASN1Set

use of com.unboundid.asn1.ASN1Set in project xipki by xipki.

the class CaUtil method getExtensions.

public static Extensions getExtensions(CertificationRequestInfo csr) {
    notNull(csr, "csr");
    ASN1Set attrs = csr.getAttributes();
    for (int i = 0; i < attrs.size(); i++) {
        Attribute attr = Attribute.getInstance(attrs.getObjectAt(i));
        if (PKCSObjectIdentifiers.pkcs_9_at_extensionRequest.equals(attr.getAttrType())) {
            return Extensions.getInstance(attr.getAttributeValues()[0]);
        }
    }
    return null;
}
Also used : ASN1Set(org.bouncycastle.asn1.ASN1Set) Attribute(org.bouncycastle.asn1.pkcs.Attribute)

Aggregations

ASN1Set (org.bouncycastle.asn1.ASN1Set)67 ArrayList (java.util.ArrayList)51 ASN1Set (com.unboundid.asn1.ASN1Set)33 IOException (java.io.IOException)32 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)30 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)30 ASN1Set (com.github.zhenwei.core.asn1.ASN1Set)26 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)22 ASN1Element (com.unboundid.asn1.ASN1Element)21 NotNull (com.unboundid.util.NotNull)21 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)19 List (java.util.List)17 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)17 DEROctetString (org.bouncycastle.asn1.DEROctetString)16 Enumeration (java.util.Enumeration)14 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)14 OutputStream (java.io.OutputStream)12 Test (org.testng.annotations.Test)12 ASN1Enumerated (com.unboundid.asn1.ASN1Enumerated)11 X509Certificate (java.security.cert.X509Certificate)11