Search in sources :

Example 46 with DERUTF8String

use of com.github.zhenwei.core.asn1.DERUTF8String in project signer by demoiselle.

the class FieldOfApplication method parse.

@Override
public void parse(ASN1Primitive derObject) {
    if (derObject instanceof DERUTF8String) {
        DERUTF8String derUTF8String = (DERUTF8String) derObject;
        this.setValue(derUTF8String.getString());
    } else {
        this.setValue(derObject.toString());
    }
}
Also used : DERUTF8String(org.bouncycastle.asn1.DERUTF8String)

Example 47 with DERUTF8String

use of com.github.zhenwei.core.asn1.DERUTF8String in project signer by demoiselle.

the class OIDGeneric method getInstance.

/**
 * Instance for OIDGeneric.
 *
 * @param data Set of bytes with the contents of the certificate.
 * @return Object GenericOID
 * @throws IOException exception of input/output
 * @throws Exception   general exception
 */
public static OIDGeneric getInstance(byte[] data) throws IOException, Exception {
    is = new ASN1InputStream(data);
    DLSequence sequence = (DLSequence) is.readObject();
    ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) sequence.getObjectAt(0);
    DERTaggedObject taggedObject = (DERTaggedObject) sequence.getObjectAt(1);
    DERTaggedObject taggedObject2 = (DERTaggedObject) taggedObject.getObject();
    DEROctetString octet = null;
    DERPrintableString print = null;
    DERUTF8String utf8 = null;
    DERIA5String ia5 = null;
    try {
        octet = (DEROctetString) taggedObject2.getObject();
    } catch (Exception e) {
        try {
            print = (DERPrintableString) taggedObject2.getObject();
        } catch (Exception e1) {
            try {
                utf8 = (DERUTF8String) taggedObject2.getObject();
            } catch (Exception e2) {
                ia5 = (DERIA5String) taggedObject2.getObject();
            }
        }
    }
    String className = getPackageName() + oid.getId().replaceAll("[.]", "_");
    OIDGeneric oidGenerico;
    try {
        oidGenerico = (OIDGeneric) Class.forName(className).newInstance();
    } catch (InstantiationException e) {
        throw new Exception(coreMessagesBundle.getString("error.class.instance", className), e);
    } catch (IllegalAccessException e) {
        throw new Exception(coreMessagesBundle.getString("error.class.illegal.access", className), e);
    } catch (ClassNotFoundException e) {
        oidGenerico = new OIDGeneric();
    }
    oidGenerico.oid = oid.getId();
    if (octet != null) {
        oidGenerico.data = new String(octet.getOctets());
    } else {
        if (print != null) {
            oidGenerico.data = print.getString();
        } else {
            if (utf8 != null) {
                oidGenerico.data = utf8.getString();
            } else {
                oidGenerico.data = ia5.getString();
            }
        }
    }
    oidGenerico.initialize();
    return oidGenerico;
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) DERTaggedObject(org.bouncycastle.asn1.DERTaggedObject) DERIA5String(org.bouncycastle.asn1.DERIA5String) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) DEROctetString(org.bouncycastle.asn1.DEROctetString) DERPrintableString(org.bouncycastle.asn1.DERPrintableString) DEROctetString(org.bouncycastle.asn1.DEROctetString) IOException(java.io.IOException) DERIA5String(org.bouncycastle.asn1.DERIA5String) DLSequence(org.bouncycastle.asn1.DLSequence) DERPrintableString(org.bouncycastle.asn1.DERPrintableString) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 48 with DERUTF8String

use of com.github.zhenwei.core.asn1.DERUTF8String in project candlepin by candlepin.

the class JSSPKIUtilityTest method testCustomExtensions.

@Test
public void testCustomExtensions() throws Exception {
    JSSPKIUtility pki = this.buildJSSPKIUtility();
    Date start = new Date();
    Date end = Date.from(LocalDate.now().plusDays(365).atStartOfDay(ZoneId.systemDefault()).toInstant());
    String extOid = OIDUtil.REDHAT_OID + "." + OIDUtil.TOPLEVEL_NAMESPACES.get(OIDUtil.ENTITLEMENT_TYPE_KEY);
    X509ExtensionWrapper typeExtension = new X509ExtensionWrapper(extOid, false, "OrgLevel");
    Set<X509ExtensionWrapper> exts = Set.of(typeExtension);
    String byteExtOid = OIDUtil.REDHAT_OID + "." + OIDUtil.TOPLEVEL_NAMESPACES.get(OIDUtil.ENTITLEMENT_DATA_KEY);
    byte[] someBytes = new byte[] { 0xd, 0xe, 0xf, 0xa, 0xc, 0xe, 0xa, 0xc, 0xe };
    X509ByteExtensionWrapper byteExtension = new X509ByteExtensionWrapper(byteExtOid, false, someBytes);
    Set<X509ByteExtensionWrapper> byteExtensions = Set.of(byteExtension);
    X509Certificate cert = pki.createX509Certificate("cn=candlepinproject.org", exts, byteExtensions, start, end, subjectKeyPair, BigInteger.valueOf(2000L), "altName");
    assertNotNull(cert.getExtensionValue(extOid));
    ASN1OctetString value = (ASN1OctetString) ASN1OctetString.fromByteArray(cert.getExtensionValue(extOid));
    DERUTF8String actual = DERUTF8String.getInstance(value.getOctets());
    assertEquals("OrgLevel", actual.getString());
    value = (ASN1OctetString) ASN1OctetString.fromByteArray(cert.getExtensionValue(byteExtOid));
    ASN1OctetString actualBytes = ASN1OctetString.getInstance(value.getOctets());
    assertArrayEquals(someBytes, actualBytes.getOctets());
}
Also used : ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) X509ByteExtensionWrapper(org.candlepin.pki.X509ByteExtensionWrapper) X509ExtensionWrapper(org.candlepin.pki.X509ExtensionWrapper) DERBitString(org.bouncycastle.asn1.DERBitString) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) Date(java.util.Date) LocalDate(java.time.LocalDate) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.jupiter.api.Test)

Example 49 with DERUTF8String

use of com.github.zhenwei.core.asn1.DERUTF8String 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 50 with DERUTF8String

use of com.github.zhenwei.core.asn1.DERUTF8String in project jruby-openssl by jruby.

the class PEMInputOutput method readAuxCertificate.

private static X509AuxCertificate readAuxCertificate(final BufferedReader in, final String endMarker) throws IOException {
    final byte[] bytes = readBase64Bytes(in, endMarker);
    final ASN1InputStream asn1 = new ASN1InputStream(bytes);
    ByteArrayInputStream certBytes = new ByteArrayInputStream((asn1.readObject()).getEncoded());
    try {
        final X509Certificate cert = (X509Certificate) getX509CertificateFactory().generateCertificate(certBytes);
        final ASN1Sequence auxSeq = (ASN1Sequence) asn1.readObject();
        final X509Aux aux;
        if (auxSeq != null) {
            // X509Aux fields :
            final List<String> trust;
            final List<String> reject;
            final String alias;
            final byte[] keyid;
            final List<ASN1Primitive> other;
            int ix = 0;
            ASN1Encodable obj = null;
            if (auxSeq.size() > ix)
                obj = auxSeq.getObjectAt(ix);
            if (obj instanceof ASN1Sequence) {
                trust = new ArrayList<String>();
                final ASN1Sequence trustSeq = (ASN1Sequence) obj;
                for (int i = 0; i < trustSeq.size(); i++) {
                    trust.add(((ASN1ObjectIdentifier) trustSeq.getObjectAt(i)).getId());
                }
                // next obj
                obj = (auxSeq.size() > ++ix) ? auxSeq.getObjectAt(ix) : null;
            } else
                trust = Collections.emptyList();
            if (obj instanceof ASN1TaggedObject && ((ASN1TaggedObject) obj).getTagNo() == 0) {
                reject = new ArrayList<String>();
                final ASN1Sequence rejectSeq = (ASN1Sequence) ((ASN1TaggedObject) obj).getObject();
                for (int i = 0; i < rejectSeq.size(); i++) {
                    reject.add(((ASN1ObjectIdentifier) rejectSeq.getObjectAt(i)).getId());
                }
                // next obj
                obj = (auxSeq.size() > ++ix) ? auxSeq.getObjectAt(ix) : null;
            } else
                reject = Collections.emptyList();
            if (obj instanceof DERUTF8String) {
                alias = ((DERUTF8String) obj).getString();
                // next obj
                obj = (auxSeq.size() > ++ix) ? auxSeq.getObjectAt(ix) : null;
            } else
                alias = null;
            if (obj instanceof DEROctetString) {
                keyid = ((DEROctetString) obj).getOctets();
                // next obj
                obj = (auxSeq.size() > ++ix) ? auxSeq.getObjectAt(ix) : null;
            } else
                keyid = null;
            if (obj instanceof ASN1TaggedObject && ((ASN1TaggedObject) obj).getTagNo() == 1) {
                other = new ArrayList<ASN1Primitive>();
                final ASN1Sequence otherSeq = (ASN1Sequence) ((ASN1TaggedObject) obj).getObject();
                for (int i = 0; i < otherSeq.size(); i++) {
                    other.add((ASN1Primitive) otherSeq.getObjectAt(i));
                }
            // obj = ( auxSeq.size() > ++ix ) ? auxSeq.getObjectAt(ix) : null; // next obj
            } else
                other = Collections.emptyList();
            aux = new X509Aux(alias, keyid, Collections.unmodifiableList(trust), Collections.unmodifiableList(reject), Collections.unmodifiableList(other));
        } else {
            aux = null;
        }
        return new X509AuxCertificate(cert, aux);
    } catch (CertificateException e) {
        throw new IOException("failed to read aux cert: " + e, e);
    }
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) ASN1TaggedObject(org.bouncycastle.asn1.ASN1TaggedObject) CertificateException(java.security.cert.CertificateException) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) DEROctetString(org.bouncycastle.asn1.DEROctetString) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) DEROctetString(org.bouncycastle.asn1.DEROctetString) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ByteArrayInputStream(java.io.ByteArrayInputStream) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive)

Aggregations

DERUTF8String (org.bouncycastle.asn1.DERUTF8String)52 DERSequence (org.bouncycastle.asn1.DERSequence)28 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)20 DEROctetString (org.bouncycastle.asn1.DEROctetString)19 IOException (java.io.IOException)17 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)17 DERTaggedObject (org.bouncycastle.asn1.DERTaggedObject)17 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)15 DERIA5String (org.bouncycastle.asn1.DERIA5String)15 DERPrintableString (org.bouncycastle.asn1.DERPrintableString)12 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)11 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)11 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)10 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)10 DLSequence (org.bouncycastle.asn1.DLSequence)9 X500Name (org.bouncycastle.asn1.x500.X500Name)8 X509Certificate (java.security.cert.X509Certificate)7 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)7 Pair (android.util.Pair)5 Date (java.util.Date)5