use of com.unboundid.asn1.ASN1BigInteger in project ldapsdk by pingidentity.
the class RSAPrivateKey method encode.
/**
* Encodes this RSA private key to an ASN.1 octet string.
*
* @return The ASN.1 octet string containing the encoded private key.
*/
@NotNull()
ASN1OctetString encode() {
final ArrayList<ASN1Element> elements = new ArrayList<>(9);
elements.add(new ASN1Integer(version.getIntValue()));
elements.add(new ASN1BigInteger(modulus));
elements.add(new ASN1BigInteger(publicExponent));
elements.add(new ASN1BigInteger(privateExponent));
elements.add(new ASN1BigInteger(prime1));
elements.add(new ASN1BigInteger(prime2));
elements.add(new ASN1BigInteger(exponent1));
elements.add(new ASN1BigInteger(exponent2));
elements.add(new ASN1BigInteger(coefficient));
if (!otherPrimeInfos.isEmpty()) {
final ArrayList<ASN1Element> otherElements = new ArrayList<>(otherPrimeInfos.size());
for (final BigInteger[] info : otherPrimeInfos) {
otherElements.add(new ASN1Sequence(new ASN1BigInteger(info[0]), new ASN1BigInteger(info[1]), new ASN1BigInteger(info[2])));
}
elements.add(new ASN1Sequence(otherElements));
}
return new ASN1OctetString(new ASN1Sequence(elements).encode());
}
use of com.unboundid.asn1.ASN1BigInteger in project ldapsdk by pingidentity.
the class RSAPublicKey method encode.
/**
* Encodes this RSA public key.
*
* @return The encoded representation of this RSA public key.
*/
@NotNull()
ASN1BitString encode() {
final ASN1Sequence publicKeySequence = new ASN1Sequence(new ASN1BigInteger(modulus), new ASN1BigInteger(publicExponent));
final boolean[] bits = ASN1BitString.getBitsForBytes(publicKeySequence.encode());
return new ASN1BitString(bits);
}
use of com.unboundid.asn1.ASN1BigInteger in project ldapsdk by pingidentity.
the class X509Certificate method encode.
/**
* Encodes this X.509 certificate to an ASN.1 element.
*
* @return The encoded X.509 certificate.
*
* @throws CertException If a problem is encountered while trying to encode
* the X.509 certificate.
*/
@NotNull()
ASN1Element encode() throws CertException {
try {
final ArrayList<ASN1Element> tbsCertificateElements = new ArrayList<>(10);
if (version != X509CertificateVersion.V1) {
tbsCertificateElements.add(new ASN1Element(TYPE_EXPLICIT_VERSION, new ASN1Integer(version.getIntValue()).encode()));
}
tbsCertificateElements.add(new ASN1BigInteger(serialNumber));
if (signatureAlgorithmParameters == null) {
tbsCertificateElements.add(new ASN1Sequence(new ASN1ObjectIdentifier(signatureAlgorithmOID)));
} else {
tbsCertificateElements.add(new ASN1Sequence(new ASN1ObjectIdentifier(signatureAlgorithmOID), signatureAlgorithmParameters));
}
tbsCertificateElements.add(encodeName(issuerDN));
tbsCertificateElements.add(encodeValiditySequence(notBefore, notAfter));
tbsCertificateElements.add(encodeName(subjectDN));
if (publicKeyAlgorithmParameters == null) {
tbsCertificateElements.add(new ASN1Sequence(new ASN1Sequence(new ASN1ObjectIdentifier(publicKeyAlgorithmOID)), encodedPublicKey));
} else {
tbsCertificateElements.add(new ASN1Sequence(new ASN1Sequence(new ASN1ObjectIdentifier(publicKeyAlgorithmOID), publicKeyAlgorithmParameters), encodedPublicKey));
}
if (issuerUniqueID != null) {
tbsCertificateElements.add(new ASN1BitString(TYPE_IMPLICIT_ISSUER_UNIQUE_ID, issuerUniqueID.getBits()));
}
if (subjectUniqueID != null) {
tbsCertificateElements.add(new ASN1BitString(TYPE_IMPLICIT_SUBJECT_UNIQUE_ID, subjectUniqueID.getBits()));
}
if (!extensions.isEmpty()) {
final ArrayList<ASN1Element> extensionElements = new ArrayList<>(extensions.size());
for (final X509CertificateExtension e : extensions) {
extensionElements.add(e.encode());
}
tbsCertificateElements.add(new ASN1Element(TYPE_EXPLICIT_EXTENSIONS, new ASN1Sequence(extensionElements).encode()));
}
final ArrayList<ASN1Element> certificateElements = new ArrayList<>(3);
certificateElements.add(new ASN1Sequence(tbsCertificateElements));
if (signatureAlgorithmParameters == null) {
certificateElements.add(new ASN1Sequence(new ASN1ObjectIdentifier(signatureAlgorithmOID)));
} else {
certificateElements.add(new ASN1Sequence(new ASN1ObjectIdentifier(signatureAlgorithmOID), signatureAlgorithmParameters));
}
certificateElements.add(signatureValue);
return new ASN1Sequence(certificateElements);
} catch (final Exception e) {
Debug.debugException(e);
throw new CertException(ERR_CERT_ENCODE_ERROR.get(toString(), StaticUtils.getExceptionMessage(e)), e);
}
}
use of com.unboundid.asn1.ASN1BigInteger in project ldapsdk by pingidentity.
the class X509CertificateTestCase method testDecodeMalformedCertSignatureAlgorithm.
/**
* Tests the behavior when trying to decode a certificate with a mismatch in
* the signature algorithm between the TBSCertificate and Certificate
* sequences.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test(expectedExceptions = { CertException.class })
public void testDecodeMalformedCertSignatureAlgorithm() throws Exception {
final long notBefore = System.currentTimeMillis();
final long notAfter = notBefore + (365L * 24L * 60L * 60L * 1000L);
final ASN1Sequence valueSequence = new ASN1Sequence(new ASN1Sequence(new ASN1Element((byte) 0xA0, new ASN1Integer(2).encode()), new ASN1BigInteger(12435L), new ASN1Sequence(new ASN1ObjectIdentifier(new OID("1.2.3.4")), new ASN1Null()), X509Certificate.encodeName(new DN("CN=issuer")), new ASN1Sequence(new ASN1GeneralizedTime(notBefore), new ASN1GeneralizedTime(notAfter)), X509Certificate.encodeName(new DN("CN=ldap.example.com")), new ASN1Sequence(new ASN1Sequence(new ASN1ObjectIdentifier(new OID("1.2.3.5")), new ASN1Null()), new ASN1BitString(new boolean[1024]))), new ASN1OctetString("not a valid sequence"), new ASN1BitString(new boolean[1024]));
new X509Certificate(valueSequence.encode());
}
use of com.unboundid.asn1.ASN1BigInteger in project ldapsdk by pingidentity.
the class X509CertificateTestCase method testDecodeMalformedPublicKey.
/**
* Tests the behavior when trying to decode a certificate with a malformed
* public key info structure.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test(expectedExceptions = { CertException.class })
public void testDecodeMalformedPublicKey() throws Exception {
final long notBefore = System.currentTimeMillis();
final long notAfter = notBefore + (365L * 24L * 60L * 60L * 1000L);
final ASN1Sequence valueSequence = new ASN1Sequence(new ASN1Sequence(new ASN1Element((byte) 0xA0, new ASN1Integer(2).encode()), new ASN1BigInteger(12435L), new ASN1Sequence(new ASN1ObjectIdentifier(new OID("1.2.3.4")), new ASN1Null()), X509Certificate.encodeName(new DN("CN=issuer")), new ASN1Sequence(new ASN1GeneralizedTime(notBefore), new ASN1GeneralizedTime(notAfter)), X509Certificate.encodeName(new DN("CN=ldap.example.com")), new ASN1OctetString("not a valid sequence")), new ASN1Sequence(new ASN1ObjectIdentifier(new OID("1.2.3.4")), new ASN1Null()), new ASN1BitString(new boolean[1024]));
new X509Certificate(valueSequence.encode());
}
Aggregations