Search in sources :

Example 66 with ASN1Null

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

the class PKCS8PrivateKeyTestCase method testMinimalElementsNotDecodeable.

/**
 * Tests a private key with a minimal set of elements that uses an
 * unrecognized algorithm.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testMinimalElementsNotDecodeable() throws Exception {
    PKCS8PrivateKey privateKey = new PKCS8PrivateKey(PKCS8PrivateKeyVersion.V2, new OID("1.2.3.4"), new ASN1Null(), new ASN1OctetString("encoded-private-key"), null, null, null);
    assertNotNull(privateKey.getPKCS8PrivateKeyBytes());
    privateKey = new PKCS8PrivateKey(privateKey.encode().encode());
    assertNotNull(privateKey.getVersion());
    assertEquals(privateKey.getVersion(), PKCS8PrivateKeyVersion.V2);
    assertNotNull(privateKey.getPrivateKeyAlgorithmOID());
    assertEquals(privateKey.getPrivateKeyAlgorithmOID(), new OID("1.2.3.4"));
    assertNull(privateKey.getPrivateKeyAlgorithmName());
    assertNotNull(privateKey.getPrivateKeyAlgorithmNameOrOID());
    assertEquals(privateKey.getPrivateKeyAlgorithmNameOrOID(), "1.2.3.4");
    assertNotNull(privateKey.getPrivateKeyAlgorithmParameters());
    assertNotNull(privateKey.getEncodedPrivateKey());
    assertEquals(privateKey.getEncodedPrivateKey().getValue(), new ASN1OctetString("encoded-private-key").getValue());
    assertNull(privateKey.getDecodedPrivateKey());
    assertNotNull(privateKey.toString());
    assertNotNull(privateKey.toPEM());
    assertFalse(privateKey.toPEM().isEmpty());
    assertNotNull(privateKey.toPEMString());
    assertNotNull(privateKey.getPKCS8PrivateKeyBytes());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) OID(com.unboundid.util.OID) ASN1Null(com.unboundid.asn1.ASN1Null) Test(org.testng.annotations.Test)

Example 67 with ASN1Null

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

the class PKCS8PrivateKeyTestCase method testDecodeMalformedPublicKey.

/**
 * Tests the behavior when trying to decode a byte array that represents a
 * sequence with a malformed public key.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { CertException.class })
public void testDecodeMalformedPublicKey() throws Exception {
    final ASN1Sequence valueSequence = new ASN1Sequence(new ASN1Integer(0), new ASN1Sequence(new ASN1ObjectIdentifier(new OID("1.2.3.4")), new ASN1Null()), new ASN1OctetString("encoded-private-key"), new ASN1OctetString((byte) 0x81));
    new PKCS8PrivateKey(valueSequence.encode());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Integer(com.unboundid.asn1.ASN1Integer) OID(com.unboundid.util.OID) ASN1ObjectIdentifier(com.unboundid.asn1.ASN1ObjectIdentifier) ASN1Null(com.unboundid.asn1.ASN1Null) Test(org.testng.annotations.Test)

Example 68 with ASN1Null

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

the class PKCS8PrivateKeyTestCase method testEncodeWithInvalidAlgorithmIdentifierOID.

/**
 * Tests the behavior when trying to encode a private key with an algorithm
 * identifier that is not a valid OID.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { CertException.class })
public void testEncodeWithInvalidAlgorithmIdentifierOID() throws Exception {
    final PKCS8PrivateKey privateKey = new PKCS8PrivateKey(PKCS8PrivateKeyVersion.V2, new OID("1234.5678"), new ASN1Null(), new ASN1OctetString("encoded-private-key"), null, null, null);
    privateKey.encode();
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) OID(com.unboundid.util.OID) ASN1Null(com.unboundid.asn1.ASN1Null) Test(org.testng.annotations.Test)

Example 69 with ASN1Null

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

the class PKCS8PrivateKeyTestCase method testDecodeInvalidVersionNumber.

/**
 * Tests the behavior when trying to decode a byte array that represents a
 * sequence with an invalid version number.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { CertException.class })
public void testDecodeInvalidVersionNumber() throws Exception {
    final ASN1Sequence valueSequence = new ASN1Sequence(new ASN1Integer(999), new ASN1Sequence(new ASN1ObjectIdentifier(new OID("1.2.3.4")), new ASN1Null()), new ASN1OctetString("encoded-private-key"));
    new PKCS8PrivateKey(valueSequence.encode());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Integer(com.unboundid.asn1.ASN1Integer) OID(com.unboundid.util.OID) ASN1ObjectIdentifier(com.unboundid.asn1.ASN1ObjectIdentifier) ASN1Null(com.unboundid.asn1.ASN1Null) Test(org.testng.annotations.Test)

Example 70 with ASN1Null

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

the class PKCS8PrivateKeyTestCase method testMalformedRSAPrivateKey.

/**
 * Tests the behavior with a private key algorithm that indicates that it's an
 * RSA key, but with an encoded key that can't be parsed as an RSA private
 * key.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testMalformedRSAPrivateKey() throws Exception {
    PKCS8PrivateKey privateKey = new PKCS8PrivateKey(PKCS8PrivateKeyVersion.V1, PublicKeyAlgorithmIdentifier.RSA.getOID(), new ASN1Null(), new ASN1OctetString("malformed-rsa-private-key"), null, null, null);
    privateKey = new PKCS8PrivateKey(privateKey.encode().encode());
    assertNotNull(privateKey.getVersion());
    assertEquals(privateKey.getVersion(), PKCS8PrivateKeyVersion.V1);
    assertNotNull(privateKey.getPrivateKeyAlgorithmOID());
    assertEquals(privateKey.getPrivateKeyAlgorithmOID(), PublicKeyAlgorithmIdentifier.RSA.getOID());
    assertNotNull(privateKey.getPrivateKeyAlgorithmName());
    assertEquals(privateKey.getPrivateKeyAlgorithmName(), "RSA");
    assertNotNull(privateKey.getPrivateKeyAlgorithmNameOrOID());
    assertEquals(privateKey.getPrivateKeyAlgorithmNameOrOID(), "RSA");
    assertNotNull(privateKey.getPrivateKeyAlgorithmParameters());
    assertNotNull(privateKey.getEncodedPrivateKey());
    assertEquals(privateKey.getEncodedPrivateKey().getValue(), new ASN1OctetString("malformed-rsa-private-key").getValue());
    assertNull(privateKey.getDecodedPrivateKey());
    assertNull(privateKey.getAttributesElement());
    assertNull(privateKey.getPublicKey());
    assertNotNull(privateKey.toString());
    assertNotNull(privateKey.toPEM());
    assertFalse(privateKey.toPEM().isEmpty());
    assertNotNull(privateKey.toPEMString());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Null(com.unboundid.asn1.ASN1Null) Test(org.testng.annotations.Test)

Aggregations

ASN1Null (com.unboundid.asn1.ASN1Null)69 Test (org.testng.annotations.Test)65 ASN1BitString (com.unboundid.asn1.ASN1BitString)36 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)33 DN (com.unboundid.ldap.sdk.DN)33 OID (com.unboundid.util.OID)32 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)28 ASN1ObjectIdentifier (com.unboundid.asn1.ASN1ObjectIdentifier)23 ASN1Integer (com.unboundid.asn1.ASN1Integer)21 ASN1Element (com.unboundid.asn1.ASN1Element)20 ASN1BigInteger (com.unboundid.asn1.ASN1BigInteger)15 ASN1GeneralizedTime (com.unboundid.asn1.ASN1GeneralizedTime)9 ASN1UTCTime (com.unboundid.asn1.ASN1UTCTime)6 ArrayList (java.util.ArrayList)6 ASN1Null (com.github.zhenwei.core.asn1.ASN1Null)5 Date (java.util.Date)5 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)3 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)3 AlgorithmParameters (java.security.AlgorithmParameters)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3