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);
}
}
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);
}
}
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;
}
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());
}
}
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);
}
}
Aggregations