Search in sources :

Example 1 with ASN1GeneralizedTime

use of com.unboundid.asn1.ASN1GeneralizedTime in project ldapsdk by pingidentity.

the class X509CertificateTestCase method testDecodeMalformedSubjectUniqueID.

/**
 * Tests the behavior when trying to decode a certificate with a malformed
 * subject unique ID.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { CertException.class })
public void testDecodeMalformedSubjectUniqueID() 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 ASN1Element((byte) 0x82)), new ASN1Sequence(new ASN1ObjectIdentifier(new OID("1.2.3.4")), new ASN1Null()), new ASN1BitString(new boolean[1024]));
    new X509Certificate(valueSequence.encode());
}
Also used : ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Element(com.unboundid.asn1.ASN1Element) 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) ASN1ObjectIdentifier(com.unboundid.asn1.ASN1ObjectIdentifier) ASN1BitString(com.unboundid.asn1.ASN1BitString) ASN1Null(com.unboundid.asn1.ASN1Null) Test(org.testng.annotations.Test)

Example 2 with ASN1GeneralizedTime

use of com.unboundid.asn1.ASN1GeneralizedTime in project ldapsdk by pingidentity.

the class X509CertificateTestCase method testDecodeValidityMalformedNotAfter.

/**
 * Tests the behavior when trying to decode a certificate with a validity
 * sequence whose second element is neither a UTCTime nor a GeneralizedTime.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { CertException.class })
public void testDecodeValidityMalformedNotAfter() 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 ASN1OctetString("malformed 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 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)

Example 3 with ASN1GeneralizedTime

use of com.unboundid.asn1.ASN1GeneralizedTime in project ldapsdk by pingidentity.

the class X509CertificateTestCase method testDecodeMalformedSubjectDN.

/**
 * Tests the behavior when trying to decode a certificate with a malformed
 * subject DN.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { CertException.class })
public void testDecodeMalformedSubjectDN() 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)), new ASN1OctetString("malformed subject DN"), new ASN1Sequence(new ASN1Sequence(new ASN1ObjectIdentifier(new OID("1.2.3.5")), new ASN1Null()), new ASN1BitString(new boolean[1024]))), 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)

Example 4 with ASN1GeneralizedTime

use of com.unboundid.asn1.ASN1GeneralizedTime in project ldapsdk by pingidentity.

the class X509CertificateTestCase method testDecodeSignatureAlgorithmMismatch.

/**
 * 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 testDecodeSignatureAlgorithmMismatch() 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 ASN1Sequence(new ASN1ObjectIdentifier(new OID("1.2.3.4")), new ASN1Null()), new ASN1OctetString());
    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 5 with ASN1GeneralizedTime

use of com.unboundid.asn1.ASN1GeneralizedTime in project ldapsdk by pingidentity.

the class X509Certificate method encodeValiditySequence.

/**
 * Encodes the certificate validity sequence, using a UTC time encoding if
 * both notBefore and notAfter values fall within the range 1950-2049, and
 * using generalized time if either value falls outside that range.
 *
 * @param  notBefore  The notBefore value to include in the sequence.
 * @param  notAfter   The notAfter value to include in the sequence.
 *
 * @return  The encoded validity sequence.
 */
@NotNull()
static ASN1Sequence encodeValiditySequence(final long notBefore, final long notAfter) {
    final GregorianCalendar notBeforeCalendar = new GregorianCalendar();
    notBeforeCalendar.setTimeInMillis(notBefore);
    final int notBeforeYear = notBeforeCalendar.get(Calendar.YEAR);
    final GregorianCalendar notAfterCalendar = new GregorianCalendar();
    notAfterCalendar.setTimeInMillis(notAfter);
    final int notAfterYear = notAfterCalendar.get(Calendar.YEAR);
    if ((notBeforeYear >= 1950) && (notBeforeYear <= 2049) && (notAfterYear >= 1950) && (notAfterYear <= 2049)) {
        return new ASN1Sequence(new ASN1UTCTime(notBefore), new ASN1UTCTime(notAfter));
    } else {
        return new ASN1Sequence(new ASN1GeneralizedTime(notBefore), new ASN1GeneralizedTime(notAfter));
    }
}
Also used : ASN1Sequence(com.unboundid.asn1.ASN1Sequence) GregorianCalendar(java.util.GregorianCalendar) ASN1UTCTime(com.unboundid.asn1.ASN1UTCTime) ASN1GeneralizedTime(com.unboundid.asn1.ASN1GeneralizedTime) NotNull(com.unboundid.util.NotNull)

Aggregations

ASN1GeneralizedTime (org.bouncycastle.asn1.ASN1GeneralizedTime)24 ASN1GeneralizedTime (com.unboundid.asn1.ASN1GeneralizedTime)10 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)10 IOException (java.io.IOException)10 Date (java.util.Date)10 ASN1BigInteger (com.unboundid.asn1.ASN1BigInteger)9 ASN1BitString (com.unboundid.asn1.ASN1BitString)9 ASN1Element (com.unboundid.asn1.ASN1Element)9 ASN1Integer (com.unboundid.asn1.ASN1Integer)9 ASN1Null (com.unboundid.asn1.ASN1Null)9 ASN1ObjectIdentifier (com.unboundid.asn1.ASN1ObjectIdentifier)9 DN (com.unboundid.ldap.sdk.DN)9 OID (com.unboundid.util.OID)9 Test (org.testng.annotations.Test)9 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)7 DEROctetString (org.bouncycastle.asn1.DEROctetString)7 ASN1GeneralizedTime (com.github.zhenwei.core.asn1.ASN1GeneralizedTime)6 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)6 DERUTF8String (org.bouncycastle.asn1.DERUTF8String)6 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)5