Search in sources :

Example 11 with ASN1Encodable

use of com.github.zhenwei.core.asn1.ASN1Encodable in project ca3sCore by kuehne-trustable-de.

the class CertificateUtil method getSANList.

public Set<GeneralName> getSANList(Pkcs10RequestHolder p10ReqHolder) {
    Set<GeneralName> generalNameSet = new HashSet<>();
    for (Attribute attr : p10ReqHolder.getReqAttributes()) {
        if (PKCSObjectIdentifiers.pkcs_9_at_extensionRequest.equals(attr.getAttrType())) {
            ASN1Set valueSet = attr.getAttrValues();
            LOG.debug("ExtensionRequest / AttrValues has {} elements", valueSet.size());
            for (ASN1Encodable asn1Enc : valueSet) {
                DERSequence derSeq = (DERSequence) asn1Enc;
                LOG.debug("ExtensionRequest / DERSequence has {} elements", derSeq.size());
                LOG.debug("ExtensionRequest / DERSequence[0] is a  {}", derSeq.getObjectAt(0).getClass().getName());
                DERSequence derSeq2 = (DERSequence) derSeq.getObjectAt(0);
                LOG.debug("ExtensionRequest / DERSequence2 has {} elements", derSeq2.size());
                LOG.debug("ExtensionRequest / DERSequence2[0] is a  {}", derSeq2.getObjectAt(0).getClass().getName());
                ASN1ObjectIdentifier objId = (ASN1ObjectIdentifier) (derSeq2.getObjectAt(0));
                if (Extension.subjectAlternativeName.equals(objId)) {
                    DEROctetString derStr = (DEROctetString) derSeq2.getObjectAt(1);
                    GeneralNames names = GeneralNames.getInstance(derStr.getOctets());
                    LOG.debug("Attribute value SAN" + names);
                    LOG.debug("SAN values #" + names.getNames().length);
                    for (GeneralName gnSAN : names.getNames()) {
                        LOG.debug("GN " + gnSAN.toString());
                        generalNameSet.add(gnSAN);
                    }
                } else {
                    LOG.info("Unexpected Extensions Attribute value " + objId.getId());
                }
            }
        }
    }
    return generalNameSet;
}
Also used : DERSequence(org.bouncycastle.asn1.DERSequence) ASN1Set(org.bouncycastle.asn1.ASN1Set) Attribute(org.bouncycastle.asn1.pkcs.Attribute) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) DEROctetString(org.bouncycastle.asn1.DEROctetString)

Example 12 with ASN1Encodable

use of com.github.zhenwei.core.asn1.ASN1Encodable in project identity-credential by google.

the class Util method signatureDerToCose.

/*
     * From RFC 8152 section 8.1 ECDSA:
     *
     * The signature algorithm results in a pair of integers (R, S).  These
     * integers will be the same length as the length of the key used for
     * the signature process.  The signature is encoded by converting the
     * integers into byte strings of the same length as the key size.  The
     * length is rounded up to the nearest byte and is left padded with zero
     * bits to get to the correct length.  The two integers are then
     * concatenated together to form a byte string that is the resulting
     * signature.
     */
private static byte[] signatureDerToCose(byte[] signature, int keySize) {
    ASN1Primitive asn1;
    try {
        asn1 = new ASN1InputStream(new ByteArrayInputStream(signature)).readObject();
    } catch (IOException e) {
        throw new IllegalArgumentException("Error decoding DER signature", e);
    }
    if (!(asn1 instanceof ASN1Sequence)) {
        throw new IllegalArgumentException("Not a ASN1 sequence");
    }
    ASN1Encodable[] asn1Encodables = ((ASN1Sequence) asn1).toArray();
    if (asn1Encodables.length != 2) {
        throw new IllegalArgumentException("Expected two items in sequence");
    }
    if (!(asn1Encodables[0].toASN1Primitive() instanceof ASN1Integer)) {
        throw new IllegalArgumentException("First item is not an integer");
    }
    BigInteger r = ((ASN1Integer) asn1Encodables[0].toASN1Primitive()).getValue();
    if (!(asn1Encodables[1].toASN1Primitive() instanceof ASN1Integer)) {
        throw new IllegalArgumentException("Second item is not an integer");
    }
    BigInteger s = ((ASN1Integer) asn1Encodables[1].toASN1Primitive()).getValue();
    byte[] rBytes = stripLeadingZeroes(r.toByteArray());
    byte[] sBytes = stripLeadingZeroes(s.toByteArray());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        for (int n = 0; n < keySize - rBytes.length; n++) {
            baos.write(0x00);
        }
        baos.write(rBytes);
        for (int n = 0; n < keySize - sBytes.length; n++) {
            baos.write(0x00);
        }
        baos.write(sBytes);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return baos.toByteArray();
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) IOException(java.io.IOException) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ECPoint(java.security.spec.ECPoint) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ByteArrayInputStream(java.io.ByteArrayInputStream) BigInteger(java.math.BigInteger) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive)

Example 13 with ASN1Encodable

use of com.github.zhenwei.core.asn1.ASN1Encodable in project TLS-Scanner by tls-attacker.

the class OcspProbe method prepareNonceExtension.

private byte[] prepareNonceExtension() {
    Asn1Sequence innerExtensionSequence = new Asn1Sequence();
    Asn1ObjectIdentifier oid = new Asn1ObjectIdentifier();
    oid.setValue(NONCE.getOID());
    Asn1Sequence extensionSequence = new Asn1Sequence();
    innerExtensionSequence.addChild(oid);
    Asn1EncapsulatingOctetString encapsulatingOctetString = new Asn1EncapsulatingOctetString();
    // Nonce
    Asn1PrimitiveOctetString nonceOctetString = new Asn1PrimitiveOctetString();
    Random rand = new Random(STAPLED_NONCE_RANDOM_SEED);
    BigInteger nonce = new BigInteger(STAPLED_NONCE_RANDOM_BIT_LENGTH, rand);
    nonceOctetString.setValue(nonce.toByteArray());
    encapsulatingOctetString.addChild(nonceOctetString);
    innerExtensionSequence.addChild(encapsulatingOctetString);
    extensionSequence.addChild(innerExtensionSequence);
    List<Asn1Encodable> asn1Encodables = new LinkedList<>();
    asn1Encodables.add(extensionSequence);
    Asn1Encoder asn1Encoder = new Asn1Encoder(asn1Encodables);
    return asn1Encoder.encode();
}
Also used : Random(java.util.Random) Asn1EncapsulatingOctetString(de.rub.nds.asn1.model.Asn1EncapsulatingOctetString) Asn1ObjectIdentifier(de.rub.nds.asn1.model.Asn1ObjectIdentifier) Asn1PrimitiveOctetString(de.rub.nds.asn1.model.Asn1PrimitiveOctetString) BigInteger(java.math.BigInteger) Asn1Sequence(de.rub.nds.asn1.model.Asn1Sequence) Asn1Encoder(de.rub.nds.asn1.encoder.Asn1Encoder) Asn1Encodable(de.rub.nds.asn1.Asn1Encodable) LinkedList(java.util.LinkedList)

Example 14 with ASN1Encodable

use of com.github.zhenwei.core.asn1.ASN1Encodable in project wildfly-elytron by wildfly-security.

the class X500DirectoryAttribute method encodeTo.

public void encodeTo(final ASN1Encoder encoder) {
    encoder.startSequence();
    encoder.encodeObjectIdentifier(attributeType);
    encoder.startSet();
    for (ASN1Encodable value : values) {
        value.encodeTo(encoder);
    }
    encoder.endSet();
    encoder.endSequence();
}
Also used : ASN1Encodable(org.wildfly.security.asn1.ASN1Encodable)

Example 15 with ASN1Encodable

use of com.github.zhenwei.core.asn1.ASN1Encodable in project mercury by yellow013.

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)

Aggregations

ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)209 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)89 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)76 IOException (java.io.IOException)72 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)58 ArrayList (java.util.ArrayList)45 DEROctetString (org.bouncycastle.asn1.DEROctetString)43 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)42 DERSequence (org.bouncycastle.asn1.DERSequence)35 BigInteger (java.math.BigInteger)31 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)30 DERIA5String (org.bouncycastle.asn1.DERIA5String)30 X509Certificate (java.security.cert.X509Certificate)29 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)29 DERUTF8String (org.bouncycastle.asn1.DERUTF8String)29 GeneralName (org.bouncycastle.asn1.x509.GeneralName)26 List (java.util.List)25 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)24 HashSet (java.util.HashSet)24 ASN1TaggedObject (org.bouncycastle.asn1.ASN1TaggedObject)23