Search in sources :

Example 6 with ASN1Exception

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

the class EntityUtil method encodeX509CertificateChain.

/* -- Methods used to encode ASN.1 data structures required for entity authentication -- */
/**
 * Encode an ASN.1 set of certificates using the given DER encoder and the
 * given {@code X509Certificate} chain.
 *
 * @param encoder the DER encoder
 * @param certChain the X.509 certificate chain to encode
 * @throws ASN1Exception if an error occurs while encoding the given certificate chain
 */
public static void encodeX509CertificateChain(final DEREncoder encoder, X509Certificate[] certChain) throws ASN1Exception {
    try {
        int chainSize = certChain.length;
        encoder.startSetOf();
        for (int i = 0; i < chainSize; i++) {
            encoder.writeEncoded(certChain[i].getEncoded());
        }
        encoder.endSetOf();
    } catch (CertificateEncodingException e) {
        throw new ASN1Exception(e);
    }
}
Also used : ASN1Exception(org.wildfly.security.asn1.ASN1Exception) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Example 7 with ASN1Exception

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

the class EntityUtil method decodeX509CertificateChain.

/**
 * Decode the next element from the given DER decoder as an X.509 certificate chain.
 *
 * @param decoder the DER decoder
 * @return the X.509 certificate chain
 * @throws ASN1Exception if the next element from the given decoder is not an X.509
 * certificate chain or if an error occurs while decoding the X.509 certificate chain
 */
public static X509Certificate[] decodeX509CertificateChain(final DERDecoder decoder) throws ASN1Exception {
    if (decoder.peekType() != SET_TYPE) {
        throw saslEntity.asnUnexpectedTag();
    }
    byte[] certChain = decoder.drainElement();
    try {
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        // CertificateFactory#generateCertPath requires a DER SEQUE
        certChain[0] = SEQUENCE_TYPE;
        CertPath certPath = certFactory.generateCertPath(new ByteArrayInputStream(certChain));
        List<? extends Certificate> certs = certPath.getCertificates();
        return certs.toArray(new X509Certificate[certs.size()]);
    } catch (CertificateException e) {
        throw new ASN1Exception(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ASN1Exception(org.wildfly.security.asn1.ASN1Exception) CertificateException(java.security.cert.CertificateException) CertPath(java.security.cert.CertPath) CertificateFactory(java.security.cert.CertificateFactory)

Example 8 with ASN1Exception

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

the class EntityUtil method decodeTrustedAuthorities.

/**
 * Decode the next element from the given DER decoder as a trusted authorities element.
 *
 * @param decoder the DER decoder
 * @return the trusted authorities
 * @throws ASN1Exception if the next element from the given decoder is not a trusted authorities
 * element or if an error occurs while decoding the trusted authorities element
 */
public static List<TrustedAuthority> decodeTrustedAuthorities(final DERDecoder decoder) throws ASN1Exception {
    List<TrustedAuthority> trustedAuthorities = new ArrayList<TrustedAuthority>();
    TrustedAuthority trustedAuthority = null;
    decoder.startSequence();
    while (decoder.hasNextElement()) {
        out: {
            for (int trustedAuthorityType = 0; trustedAuthorityType <= 4; trustedAuthorityType++) {
                switch(trustedAuthorityType) {
                    case AUTHORITY_NAME:
                        if (decoder.isNextType(CONTEXT_SPECIFIC_MASK, trustedAuthorityType, true)) {
                            byte[] encodedName = decoder.drainElementValue();
                            trustedAuthority = new NameTrustedAuthority((new X500Principal(encodedName)).getName(X500Principal.CANONICAL));
                            break out;
                        }
                        break;
                    case AUTHORITY_CERTIFICATE:
                        if (decoder.isNextType(CONTEXT_SPECIFIC_MASK, trustedAuthorityType, true)) {
                            decoder.decodeImplicit(trustedAuthorityType);
                            byte[] cert = decoder.drainElement();
                            // Replace the trusted authority type tag with a DER SEQUENCE tag, as required by CertificateFactory#generateCertificate
                            cert[0] = SEQUENCE_TYPE;
                            try {
                                CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
                                trustedAuthority = new CertificateTrustedAuthority((X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(cert)));
                            } catch (CertificateException e) {
                                throw new ASN1Exception(e);
                            }
                            break out;
                        }
                        break;
                    case ISSUER_NAME_HASH:
                        if (decoder.isNextType(CONTEXT_SPECIFIC_MASK, trustedAuthorityType, false)) {
                            decoder.decodeImplicit(trustedAuthorityType);
                            trustedAuthority = new IssuerNameHashTrustedAuthority(decoder.decodeOctetString());
                            break out;
                        }
                        break;
                    case ISSUER_KEY_HASH:
                        if (decoder.isNextType(CONTEXT_SPECIFIC_MASK, trustedAuthorityType, false)) {
                            decoder.decodeImplicit(trustedAuthorityType);
                            trustedAuthority = new IssuerKeyHashTrustedAuthority(decoder.decodeOctetString());
                            break out;
                        }
                        break;
                    case PKCS_15_KEY_HASH:
                        if (decoder.isNextType(CONTEXT_SPECIFIC_MASK, trustedAuthorityType, false)) {
                            decoder.decodeImplicit(trustedAuthorityType);
                            trustedAuthority = new PKCS15KeyHashTrustedAuthority(decoder.decodeOctetString());
                            break out;
                        }
                        break;
                    default:
                        throw saslEntity.asnInvalidGeneralNameType();
                }
            }
        }
        trustedAuthorities.add(trustedAuthority);
    }
    decoder.endSequence();
    return trustedAuthorities;
}
Also used : ASN1Exception(org.wildfly.security.asn1.ASN1Exception) ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) ByteArrayInputStream(java.io.ByteArrayInputStream) TrustedAuthority(org.wildfly.security.x500.TrustedAuthority) X500Principal(javax.security.auth.x500.X500Principal)

Example 9 with ASN1Exception

use of com.github.zhenwei.core.asn1.ASN1Exception in project structure-project by wudskq.

the class KeyImpl method readObject.

private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
    try {
        EncryptionKey encKey = new EncryptionKey(new DerValue((byte[]) ois.readObject()));
        keyType = encKey.getEType();
        keyBytes = encKey.getBytes();
    } catch (Asn1Exception ae) {
        throw new IOException(ae.getMessage());
    }
}
Also used : DerValue(sun.security.util.DerValue) EncryptionKey(sun.security.krb5.EncryptionKey) Asn1Exception(sun.security.krb5.Asn1Exception)

Example 10 with ASN1Exception

use of com.github.zhenwei.core.asn1.ASN1Exception in project jdk8u_jdk by JetBrains.

the class KerberosTime method parse.

/**
     * Parse (unmarshal) a kerberostime from a DER input stream.  This form
     * parsing might be used when expanding a value which is part of
     * a constructed sequence and uses explicitly tagged type.
     *
     * @exception Asn1Exception on error.
     * @param data the Der input stream value, which contains
     *             one or more marshaled value.
     * @param explicitTag tag number.
     * @param optional indicates if this data field is optional
     * @return an instance of KerberosTime.
     *
     */
public static KerberosTime parse(DerInputStream data, byte explicitTag, boolean optional) throws Asn1Exception, IOException {
    if ((optional) && (((byte) data.peekByte() & (byte) 0x1F) != explicitTag))
        return null;
    DerValue der = data.getDerValue();
    if (explicitTag != (der.getTag() & (byte) 0x1F)) {
        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
    } else {
        DerValue subDer = der.getData().getDerValue();
        Date temp = subDer.getGeneralizedTime();
        return new KerberosTime(temp.getTime(), 0);
    }
}
Also used : DerValue(sun.security.util.DerValue) Asn1Exception(sun.security.krb5.Asn1Exception) Date(java.util.Date)

Aggregations

IOException (java.io.IOException)18 Asn1Exception (es.gob.jmulticard.asn1.Asn1Exception)16 ASN1Exception (com.unboundid.asn1.ASN1Exception)12 TlvException (es.gob.jmulticard.asn1.TlvException)12 Asn1Exception (sun.security.krb5.Asn1Exception)11 ASN1Element (com.unboundid.asn1.ASN1Element)7 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)7 NotNull (com.unboundid.util.NotNull)7 Iso7816FourCardException (es.gob.jmulticard.card.iso7816four.Iso7816FourCardException)7 CertificateException (java.security.cert.CertificateException)7 X509Certificate (java.security.cert.X509Certificate)7 Asn1Exception (org.kse.utilities.asn1.Asn1Exception)7 ASN1Exception (org.wildfly.security.asn1.ASN1Exception)6 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)5 Cdf (es.gob.jmulticard.asn1.der.pkcs15.Cdf)5 CryptoCardException (es.gob.jmulticard.card.CryptoCardException)5 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)4 DecoderObject (es.gob.jmulticard.asn1.DecoderObject)4 InvalidCardException (es.gob.jmulticard.card.InvalidCardException)4 DerValue (sun.security.util.DerValue)4