Search in sources :

Example 11 with ASN1BigInteger

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());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Element(com.unboundid.asn1.ASN1Element) ArrayList(java.util.ArrayList) ASN1BigInteger(com.unboundid.asn1.ASN1BigInteger) BigInteger(java.math.BigInteger) ASN1BigInteger(com.unboundid.asn1.ASN1BigInteger) ASN1Integer(com.unboundid.asn1.ASN1Integer) NotNull(com.unboundid.util.NotNull)

Example 12 with ASN1BigInteger

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);
}
Also used : ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1BigInteger(com.unboundid.asn1.ASN1BigInteger) ASN1BitString(com.unboundid.asn1.ASN1BitString) NotNull(com.unboundid.util.NotNull)

Example 13 with ASN1BigInteger

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);
    }
}
Also used : ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Element(com.unboundid.asn1.ASN1Element) ArrayList(java.util.ArrayList) ASN1BigInteger(com.unboundid.asn1.ASN1BigInteger) ASN1Integer(com.unboundid.asn1.ASN1Integer) ASN1ObjectIdentifier(com.unboundid.asn1.ASN1ObjectIdentifier) ASN1BitString(com.unboundid.asn1.ASN1BitString) ASN1Exception(com.unboundid.asn1.ASN1Exception) CertificateException(java.security.cert.CertificateException) NotNull(com.unboundid.util.NotNull)

Example 14 with ASN1BigInteger

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());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1BigInteger(com.unboundid.asn1.ASN1BigInteger) DN(com.unboundid.ldap.sdk.DN) ASN1GeneralizedTime(com.unboundid.asn1.ASN1GeneralizedTime) ASN1Integer(com.unboundid.asn1.ASN1Integer) OID(com.unboundid.util.OID) ASN1BitString(com.unboundid.asn1.ASN1BitString) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Element(com.unboundid.asn1.ASN1Element) ASN1ObjectIdentifier(com.unboundid.asn1.ASN1ObjectIdentifier) ASN1Null(com.unboundid.asn1.ASN1Null) Test(org.testng.annotations.Test)

Example 15 with ASN1BigInteger

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());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1BigInteger(com.unboundid.asn1.ASN1BigInteger) DN(com.unboundid.ldap.sdk.DN) ASN1GeneralizedTime(com.unboundid.asn1.ASN1GeneralizedTime) ASN1Integer(com.unboundid.asn1.ASN1Integer) OID(com.unboundid.util.OID) ASN1BitString(com.unboundid.asn1.ASN1BitString) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Element(com.unboundid.asn1.ASN1Element) ASN1ObjectIdentifier(com.unboundid.asn1.ASN1ObjectIdentifier) ASN1Null(com.unboundid.asn1.ASN1Null) Test(org.testng.annotations.Test)

Aggregations

ASN1BigInteger (com.unboundid.asn1.ASN1BigInteger)20 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)20 ASN1BitString (com.unboundid.asn1.ASN1BitString)18 ASN1Element (com.unboundid.asn1.ASN1Element)18 ASN1Integer (com.unboundid.asn1.ASN1Integer)18 ASN1ObjectIdentifier (com.unboundid.asn1.ASN1ObjectIdentifier)17 Test (org.testng.annotations.Test)16 ASN1Null (com.unboundid.asn1.ASN1Null)15 DN (com.unboundid.ldap.sdk.DN)15 OID (com.unboundid.util.OID)15 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)11 ASN1GeneralizedTime (com.unboundid.asn1.ASN1GeneralizedTime)9 ASN1UTCTime (com.unboundid.asn1.ASN1UTCTime)5 NotNull (com.unboundid.util.NotNull)4 ArrayList (java.util.ArrayList)3 ASN1Exception (com.unboundid.asn1.ASN1Exception)2 CertificateException (java.security.cert.CertificateException)2 BigInteger (java.math.BigInteger)1 Signature (java.security.Signature)1