From d342a45d98c4795b3a3fe1aaef5236ad4a782b55 Mon Sep 17 00:00:00 2001 From: Yuuta Liang Date: Thu, 12 Oct 2023 12:10:33 +0800 Subject: Implement data structures from X.680, X.501, X.509, and PKCS#10, with X.690 encoding / decoding support The implementation took four days, and it is still a little bit rough. Updated version should arrive soon. Signed-off-by: Yuuta Liang --- src/test/model/csr/AttributeTest.java | 53 +++++++ src/test/model/csr/AttributesTest.java | 69 ++++++++ .../model/csr/CertificationRequestInfoTest.java | 174 +++++++++++++++++++++ src/test/model/csr/CertificationRequestTest.java | 114 ++++++++++++++ src/test/model/csr/ValuesTest.java | 133 ++++++++++++++++ 5 files changed, 543 insertions(+) create mode 100644 src/test/model/csr/AttributeTest.java create mode 100644 src/test/model/csr/AttributesTest.java create mode 100644 src/test/model/csr/CertificationRequestInfoTest.java create mode 100644 src/test/model/csr/CertificationRequestTest.java create mode 100644 src/test/model/csr/ValuesTest.java (limited to 'src/test/model/csr') diff --git a/src/test/model/csr/AttributeTest.java b/src/test/model/csr/AttributeTest.java new file mode 100644 index 0000000..f4daa4c --- /dev/null +++ b/src/test/model/csr/AttributeTest.java @@ -0,0 +1,53 @@ +package model.csr; + +import model.TestConstants; +import model.asn1.exceptions.ParseException; +import model.asn1.parsing.BytesReader; +import org.junit.jupiter.api.Test; + +import static model.asn1.ObjectIdentifier.*; +import static org.junit.jupiter.api.Assertions.*; + +public class AttributeTest { + @Test + void testConstructor() throws ParseException { + assertArrayEquals(OID_EXTENSION_REQUEST, TestConstants.CSR_ATTR_2.getType().getInts()); + assertEquals(0x21, TestConstants.CSR_ATTR_2.getValues().getArray()[1].getLength()); + } + + @Test + void testParse() throws ParseException { + final Attribute parsed = new Attribute(new BytesReader(TestConstants.CSR_ATTR_VALUES_2_DER), false); + + assertArrayEquals(OID_EXTENSION_REQUEST, parsed.getType().getInts()); + assertEquals(2, parsed.getValues().getArray().length); + } + + @Test + void testParseFail() { + // No type + assertThrows(ParseException.class, () -> new Attribute(new BytesReader(new Byte[]{ + 0x30, 0x0 + }), false)); + // No value + assertThrows(ParseException.class, () -> new Attribute(new BytesReader(new Byte[]{ + 0x30, 0x5, 0x6, 0x3, 0x55, 0x4, 0x6 + }), false)); + // Incorrect type tag (should be OID) + assertThrows(ParseException.class, () -> new Attribute(new BytesReader(new Byte[]{ + 0x30, 0x9, 0x7, 0x3, 0x55, 0x4, 0x6, 0x13, 0x2, 0x49, 0x54 + }), false)); + // Incorrect value tag (should be SET) + assertThrows(ParseException.class, () -> new Attribute(new BytesReader(new Byte[]{ + 0x30, 13, + 0x06, 0x09, 0x1A, -122, 0x48, -122, -9, 0x0D, 0x01, 0x09, 0x0E, + 0x30, 0 + }), false)); + } + + @Test + void testEncode() { + assertArrayEquals(TestConstants.CSR_ATTR_1_DER, TestConstants.CSR_ATTR_1.encodeDER()); + assertArrayEquals(TestConstants.CSR_ATTR_VALUES_2_DER, TestConstants.CSR_ATTR_2.encodeDER()); + } +} diff --git a/src/test/model/csr/AttributesTest.java b/src/test/model/csr/AttributesTest.java new file mode 100644 index 0000000..86a0112 --- /dev/null +++ b/src/test/model/csr/AttributesTest.java @@ -0,0 +1,69 @@ +package model.csr; + +import model.TestConstants; +import model.asn1.ObjectIdentifier; +import model.asn1.exceptions.ParseException; +import model.asn1.parsing.BytesReader; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collection; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; + +public class AttributesTest { + @Test + void testConstructor() { + assertEquals("10.0.19045.2", + TestConstants.CSR_ATTRS_2.getArray()[1].getValues().getArray()[0].toString()); + assertArrayEquals(ObjectIdentifier.OID_EXTENSION_REQUEST, + TestConstants.CSR_ATTRS_2.getArray()[0].getType().getInts()); + } + + @Test + void testParse() throws ParseException { + final Attributes parsed = new Attributes(new BytesReader(new Byte[]{ + -96, 30, + 0x30, 0x1C, + 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, -126, 0x37, 0x0D, 0x02, 0x03, + 0x31, 0x0E, + 0x16, 0x0C, 0x31, 0x30, 0x2E, 0x30, 0x2E, 0x31, 0x39, 0x30, 0x34, 0x35, 0x2E, 0x32 + }), false); + assertEquals(1, parsed.getArray().length); + assertEquals("10.0.19045.2", parsed.getArray()[0].getValues().getArray()[0].toString()); + } + + @Test + void testParseFail() { + // Incorrect length + assertThrows(ParseException.class, () -> new Attributes(new BytesReader(new Byte[]{ + -96, 31, // Incorrect + 0x30, 0x1C, + 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, -126, 0x37, 0x0D, 0x02, 0x03, + 0x31, 0x0E, + 0x16, 0x0C, 0x31, 0x30, 0x2E, 0x30, 0x2E, 0x31, 0x39, 0x30, 0x34, 0x35, 0x2E, 0x32 + }), false)); + // Incorrect child item tag + assertThrows(ParseException.class, () -> new Attributes(new BytesReader(new Byte[]{ + -96, 30, + 0x31, 0x1C, // Incorrect + 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, -126, 0x37, 0x0D, 0x02, 0x03, + 0x31, 0x0E, + 0x16, 0x0C, 0x31, 0x30, 0x2E, 0x30, 0x2E, 0x31, 0x39, 0x30, 0x34, 0x35, 0x2E, 0x32 + }), false)); + } + + @Test + void testEncode() { + Byte[] a2 = TestConstants.CSR_ATTR_2.encodeDER(); + Byte[] a1 = TestConstants.CSR_ATTR_1.encodeDER(); + assertArrayEquals( + Stream.of(Arrays.asList(new Byte[]{ 0x31, (byte)(a2.length + a1.length) }), + Arrays.asList(a2), + Arrays.asList(a1)) + .flatMap(Collection::stream) + .toArray(Byte[]::new), + TestConstants.CSR_ATTRS_2.encodeDER()); + } +} diff --git a/src/test/model/csr/CertificationRequestInfoTest.java b/src/test/model/csr/CertificationRequestInfoTest.java new file mode 100644 index 0000000..fe2633f --- /dev/null +++ b/src/test/model/csr/CertificationRequestInfoTest.java @@ -0,0 +1,174 @@ +package model.csr; + +import model.TestConstants; +import model.asn1.*; +import model.asn1.exceptions.ParseException; +import model.asn1.parsing.BytesReader; +import model.pki.AlgorithmIdentifier; +import model.pki.SubjectPublicKeyInfo; +import org.junit.jupiter.api.Test; + +import static model.TestConstants.mutate; +import static org.junit.jupiter.api.Assertions.*; + +public class CertificationRequestInfoTest { + static final Byte[] CSR_1 = new Byte[] { + 0x30, -126, 0x02, 0x03, // SEQUENCE CertificationRequestInfo + 0x02, 0x01, 0x00, // Version + 0x30, 0x1c, // SEQUENCE Subject + 0x31, 0x1a, // RDN + 0x30, 0x18, // AttributeTypeAndValue + 0x06, 0x03, 0x55, 0x04, 0x03, // OID (CN) + 0x0c, 0x11, 0x4d, 0x49, 0x4b, 0x55, 0x2e, 0x41, // PrintableString (MIKU.AD.YUUTA.MOE) + 0x44, 0x2e, 0x59, 0x55, 0x55, 0x54, 0x41, 0x2e, + 0x4d, 0x4f, 0x45, + 0x30, -127, -97, // SEQUENCE SubjectPublicKeyInfo + 0x30, 0x0d, // SEQUENCE AlgorithmIdentifier + 0x06, 0x09, 0x2a, -122, 0x48, -122, -9, 0x0d, // OID (rsaEncryption) + 0x01, 0x01, 0x01, + 0x05, 0x00, // Null (Parameter) + 0x03, -127, -115, 0x00, 0x30, -127, -119, 0x02, // BIT STRING (subjectPublicKey) + -127, -127, 0x00, -67, -1, 0x4e, 0x6d, -22, + 0x62, 0x6a, 0x11, -120, 0x77, 0x0a, -92, 0x32, + -124, -37, 0x22, 0x2f, 0x3d, 0x5d, 0x2a, 0x63, + -71, -109, 0x11, -50, -92, 0x4f, -119, 0x3b, + 0x14, 0x3b, -54, 0x3c, -106, -42, 0x11, 0x42, + 0x78, -110, 0x68, -100, -25, -25, -50, 0x75, + -101, 0x21, 0x41, -34, -31, -85, -13, 0x1e, + 0x51, -81, 0x25, 0x4f, -1, 0x56, 0x77, 0x5e, + -30, 0x27, -104, 0x34, 0x67, -28, -56, 0x55, + 0x6a, 0x3c, 0x6f, -38, -85, -63, 0x5f, 0x16, + 0x7a, -93, -19, -35, 0x7f, 0x35, 0x0f, -47, + -7, -22, -12, -24, -48, 0x25, 0x6d, -114, + 0x66, 0x1a, 0x53, -77, 0x67, 0x32, -69, -39, + 0x57, -42, -65, -13, 0x5f, 0x6f, 0x53, 0x6d, + 0x62, -95, 0x42, 0x12, 0x7b, 0x13, 0x4f, 0x1a, + -26, 0x00, -72, -32, 0x2b, -83, 0x3c, 0x35, + -103, 0x18, 0x51, 0x02, 0x03, 0x01, 0x00, 0x01, + -96, -126, 0x01, 0x3c, // SEQUENCE (attributes) + 0x30, 0x1c, 0x06, 0x0a, // SEQUENCE (attribute) + 0x2b, 0x06, 0x01, 0x04, 0x01, -126, 0x37, 0x0d, + 0x02, 0x03, 0x31, 0x0e, 0x16, 0x0c, 0x31, 0x30, + 0x2e, 0x30, 0x2e, 0x31, 0x39, 0x30, 0x34, 0x35, + 0x2e, 0x32, + 0x30, 0x42, 0x06, // SEQUENCE (attribute) + 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, -126, 0x37, + 0x15, 0x14, 0x31, 0x35, 0x30, 0x33, 0x02, 0x01, + 0x05, 0x0c, 0x11, 0x4d, 0x49, 0x4b, 0x55, 0x2e, + 0x41, 0x44, 0x2e, 0x59, 0x55, 0x55, 0x54, 0x41, + 0x2e, 0x4d, 0x4f, 0x45, 0x0c, 0x12, 0x4d, 0x49, + 0x4b, 0x55, 0x5c, 0x41, 0x64, 0x6d, 0x69, 0x6e, + 0x69, 0x73, 0x74, 0x72, 0x61, 0x74, 0x6f, 0x72, + 0x0c, 0x07, 0x4d, 0x4d, 0x43, 0x2e, 0x45, 0x58, + 0x45, + 0x30, 0x66, // SEQUENCE (attribute) + 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, -126, + 0x37, 0x0d, 0x02, 0x02, 0x31, 0x58, 0x30, 0x56, + 0x02, 0x01, 0x00, 0x1e, 0x4e, 0x00, 0x4d, 0x00, + 0x69, 0x00, 0x63, 0x00, 0x72, 0x00, 0x6f, 0x00, + 0x73, 0x00, 0x6f, 0x00, 0x66, 0x00, 0x74, 0x00, + 0x20, 0x00, 0x53, 0x00, 0x6f, 0x00, 0x66, 0x00, + 0x74, 0x00, 0x77, 0x00, 0x61, 0x00, 0x72, 0x00, + 0x65, 0x00, 0x20, 0x00, 0x4b, 0x00, 0x65, 0x00, + 0x79, 0x00, 0x20, 0x00, 0x53, 0x00, 0x74, 0x00, + 0x6f, 0x00, 0x72, 0x00, 0x61, 0x00, 0x67, 0x00, + 0x65, 0x00, 0x20, 0x00, 0x50, 0x00, 0x72, 0x00, + 0x6f, 0x00, 0x76, 0x00, 0x69, 0x00, 0x64, 0x00, + 0x65, 0x00, 0x72, 0x03, 0x01, 0x00, + 0x30, 0x70, 0x06, 0x09, // SEQUENCE (attribute) + 0x2a, -122, 0x48, -122, -9, 0x0d, 0x01, 0x09, + 0x0e, 0x31, 0x63, 0x30, 0x61, 0x30, 0x0e, 0x06, + 0x03, 0x55, 0x1d, 0x0f, 0x01, 0x01, -1, 0x04, + 0x04, 0x03, 0x02, 0x05, -96, 0x30, 0x13, 0x06, + 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, + 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, + 0x03, 0x01, 0x30, 0x1b, 0x06, 0x09, 0x2b, 0x06, + 0x01, 0x04, 0x01, -126, 0x37, 0x15, 0x0a, 0x04, + 0x0e, 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, + 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x30, + 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, + 0x04, 0x14, -15, 0x3e, -110, -16, 0x4d, 0x1b, + -47, 0x6e, 0x53, 0x7f, -102, 0x1d, 0x19, -75, + 0x5e, -22, 0x64, 0x7f, 0x1f, -110, + }; + + @Test + void testConstructor() { + final CertificationRequestInfo info = new CertificationRequestInfo( + ASN1Object.TAG_SEQUENCE, null, + new Int(Int.TAG, null, CertificationRequestInfo.VERSION_V1), + TestConstants.NAME_2, + new SubjectPublicKeyInfo(ASN1Object.TAG_SEQUENCE, null, + new AlgorithmIdentifier(ASN1Object.TAG_SEQUENCE, null, + new ObjectIdentifier(ObjectIdentifier.TAG, null, + ObjectIdentifier.OID_SHA256_WITH_RSA_ENCRYPTION), + new Null(Null.TAG, null)), + new BitString(BitString.TAG, null, + 0, new Byte[]{ 1, 2, 3, 4, 5 })), + TestConstants.CSR_ATTRS_2); + assertEquals(CertificationRequestInfo.VERSION_V1, info.getVersion().getLong()); + assertEquals(3, info.getSubject().getRdnSequence().length); + assertArrayEquals(ObjectIdentifier.OID_SHA256_WITH_RSA_ENCRYPTION, + info.getSubjectPKInfo().getAlgorithm().getType().getInts()); + assertEquals(2, info.getAttributes().getArray().length); + } + + @Test + void testParse() throws ParseException { + final CertificationRequestInfo parsed = + new CertificationRequestInfo(new BytesReader(CSR_1), false); + assertEquals("CN=MIKU.AD.YUUTA.MOE", parsed.getSubject().toString()); + assertArrayEquals(ObjectIdentifier.OID_RSA_ENCRYPTION, + parsed.getSubjectPKInfo().getAlgorithm().getType().getInts()); + assertEquals(4, parsed.getAttributes().getArray().length); + assertEquals(1, parsed.getAttributes().getArray()[3].getValues().getArray().length); + } + + @Test + void testParseFail() { + // No version + assertThrows(ParseException.class, () -> { + new CertificationRequestInfo(new BytesReader(new Byte[]{ + 0x30, 0 + }), false); + }); + // Incorrect version tag + assertThrows(ParseException.class, () -> { + new CertificationRequestInfo(new BytesReader(mutate(CSR_1, 4, (byte) Int.TAG.getNumber(), 0x3)), + false); + }); + // Incorrect version + assertThrows(ParseException.class, () -> { + new CertificationRequestInfo(new BytesReader(mutate(CSR_1, 6, CertificationRequestInfo.VERSION_V1, 1)), + false); + }); + // No subject + assertThrows(ParseException.class, () -> { + Byte[] test = new Byte[5]; + test[0] = 0x30; + test[1] = 3; + System.arraycopy(CSR_1, 4, test, 2, 3); + new CertificationRequestInfo(new BytesReader(test), false); + }); + // Incorrect subject tag + assertThrows(ParseException.class, () -> { + new CertificationRequestInfo(new BytesReader(mutate(CSR_1, 7, 0x30, 0x31)), + false); + }); + // Incorrect subject pk info tag + assertThrows(ParseException.class, () -> { + new CertificationRequestInfo(new BytesReader(mutate(CSR_1, 37, 0x30, 0x31)), + false); + }); + // Incorrect attributes tag + assertThrows(ParseException.class, () -> { + new CertificationRequestInfo(new BytesReader(mutate(CSR_1, 199, -96, 0x31)), + false); + }); + } + + @Test + void testEncode() throws ParseException { + assertArrayEquals(CSR_1, new CertificationRequestInfo(new BytesReader(CSR_1), false).encodeDER()); + } +} diff --git a/src/test/model/csr/CertificationRequestTest.java b/src/test/model/csr/CertificationRequestTest.java new file mode 100644 index 0000000..962e90b --- /dev/null +++ b/src/test/model/csr/CertificationRequestTest.java @@ -0,0 +1,114 @@ +package model.csr; + +import model.TestConstants; +import model.asn1.*; +import model.asn1.exceptions.ParseException; +import model.asn1.parsing.BytesReader; +import model.pki.AlgorithmIdentifier; +import model.pki.SubjectPublicKeyInfo; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collection; +import java.util.stream.Stream; + +import static model.TestConstants.mutate; +import static org.junit.jupiter.api.Assertions.*; + +public class CertificationRequestTest { + private static final Byte[] CSR_1 = Stream.of( + // SEQUENCE (CertificationRequest) + Arrays.asList(new Byte[]{ 0x30, -126, 0x02, -102 }), + // SEQUENCE (CertificationRequestInfo) + Arrays.asList(CertificationRequestInfoTest.CSR_1), + // SEQUENCE (AlgorithmIdentifier) + Arrays.asList(new Byte[]{ + 0x30, 0x0D, 0x06, 0x09, 0x2A, -122, 0x48, -122, + -9, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00 + }), + // BIT STRING (Signature) + Arrays.asList(new Byte[]{ + 0x03, -127, -127, + 0x00, 0x6F, 0x61, 0x5C, -25, 0x29, 0x48, 0x3F, + -78, 0x1B, -117, 0x2C, -93, -114, 0x7D, -77, + 0x62, 0x14, 0x21, 0x4B, -99, 0x74, -95, -93, + 0x16, 0x38, 0x31, 0x40, 0x5E, 0x72, -77, -55, + 0x6D, -69, 0x19, -108, 0x52, -95, 0x19, -121, + -81, -71, 0x74, -123, 0x6B, -27, -20, 0x4C, + -126, 0x42, -89, 0x66, 0x6A, 0x52, -34, 0x62, + 0x72, 0x40, 0x2C, -79, 0x78, -117, -100, -70, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x7E, + })).flatMap(Collection::stream).toArray(Byte[]::new); + + @Test + void testConstructor() { + final CertificationRequest request = new CertificationRequest( + ASN1Object.TAG_SEQUENCE, null, + new CertificationRequestInfo( + ASN1Object.TAG_SEQUENCE, null, + new Int(Int.TAG, null, CertificationRequestInfo.VERSION_V1), + TestConstants.NAME_2, + new SubjectPublicKeyInfo(ASN1Object.TAG_SEQUENCE, null, + new AlgorithmIdentifier(ASN1Object.TAG_SEQUENCE, null, + new ObjectIdentifier(ObjectIdentifier.TAG, null, + ObjectIdentifier.OID_SHA256_WITH_RSA_ENCRYPTION), + new Null(Null.TAG, null)), + new BitString(BitString.TAG, null, + 0, new Byte[]{ 1, 2, 3, 4, 5 })), + TestConstants.CSR_ATTRS_2), + new AlgorithmIdentifier(ASN1Object.TAG_SEQUENCE, null, + new ObjectIdentifier(ObjectIdentifier.TAG, null, + ObjectIdentifier.OID_SHA256_WITH_RSA_ENCRYPTION), + new Null(Null.TAG, null)), + new BitString(BitString.TAG, null, + 0, new Byte[]{ 2, 4, 6, 8, 10 })); + + assertEquals(CertificationRequestInfo.VERSION_V1, + request.getCertificationRequestInfo().getVersion().getLong()); + assertEquals(3, + request.getCertificationRequestInfo().getSubject().getRdnSequence().length); + assertArrayEquals(ObjectIdentifier.OID_SHA256_WITH_RSA_ENCRYPTION, + request.getSignatureAlgorithm().getType().getInts()); + assertArrayEquals(new Byte[]{ 2, 4, 6, 8, 10 }, + request.getSignature().getConvertedVal()); + } + + @Test + void testParse() throws ParseException { + final CertificationRequest parsed = + new CertificationRequest(new BytesReader(CSR_1), false); + assertEquals("CN=MIKU.AD.YUUTA.MOE", + parsed.getCertificationRequestInfo().getSubject().toString()); + assertArrayEquals(ObjectIdentifier.OID_SHA256_WITH_RSA_ENCRYPTION, + parsed.getSignatureAlgorithm().getType().getInts()); + } + + @Test + void testParseFail() throws ParseException { + // Incorrect info tag + assertThrows(ParseException.class, () -> { + new CertificationRequest(new BytesReader(mutate(CSR_1, 4, 0x30, 0x31)), false); + }); + // Incorrect algorithm info tag + assertThrows(ParseException.class, () -> { + new CertificationRequest(new BytesReader(mutate(CSR_1, 523, 0x30, 0x31)), false); + }); + // Incorrect signature tag + assertThrows(ParseException.class, () -> { + new CertificationRequest(new BytesReader(mutate(CSR_1, 538, 0x3, 0x31)), false); + }); + } + + @Test + void testEncode() throws ParseException { + assertArrayEquals(CSR_1, new CertificationRequest(new BytesReader(CSR_1), false).encodeDER()); + } +} diff --git a/src/test/model/csr/ValuesTest.java b/src/test/model/csr/ValuesTest.java new file mode 100644 index 0000000..93229a4 --- /dev/null +++ b/src/test/model/csr/ValuesTest.java @@ -0,0 +1,133 @@ +package model.csr; + +import model.asn1.ASN1Object; +import model.asn1.Null; +import model.asn1.ObjectIdentifier; +import model.asn1.PrintableString; +import model.asn1.exceptions.ParseException; +import model.asn1.parsing.BytesReader; +import model.x501.AttributeTypeAndValue; +import model.x501.RelativeDistinguishedName; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static model.asn1.ASN1Object.TAG_SET; +import static model.asn1.ObjectIdentifier.OID_C; +import static model.asn1.ObjectIdentifier.OID_OU; +import static org.junit.jupiter.api.Assertions.*; + +public class ValuesTest { + // An opaque value of extended key usage block. + private ASN1Object extKeyUsage; + + // Example SubjectAlternativeName request attribute. + private ASN1Object san; + + private Values values; + + @BeforeEach + void setup() throws ParseException { + extKeyUsage = ASN1Object.parse(new BytesReader(new Byte[]{ + 0x30, 0x1F, // SEQUENCE (AttributeValue) + 0x30, 0x1D, // SEQUENCE + 0x06, 0x03, 0x55, 0x1D, 0x25, // 2.5.29.37 extKeyUsage + 0x04, 0x16, // OCTET STRING + 0x30, 0x14, // SEQUENCE + // 1.3.6.1.5.5.7.3.1 serverAuth + 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, + // 1.3.6.1.5.5.7.3.2 clientAuth + 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02 + }), false); + + san = ASN1Object.parse(new BytesReader(new Byte[]{ + 0x30, 0x21, // SEQUENCE (AttributeValue) + 0x30, 0x1F, // SEQUENCE + 0x06, 0x03, 0x55, 0x1D, 0x11, // 2.5.29.17 subjectAltName + 0x04, 0x18, // OCTET STRING + 0x30, 0x16, // SEQUENCE + -126, 0x14, // [2] + 0x6C, 0x70, 0x2D, 0x62, 0x32, 0x35, 0x35, 0x2E, 0x61, // lp-b255.yuuta.moe + 0x64, 0x2E, 0x79, 0x75, 0x75, 0x74, 0x61, 0x2E, 0x6D, 0x6F, 0x65 + }), false); + + values = new Values(TAG_SET, null, new ASN1Object[]{ + extKeyUsage, + san + }); + } + + @Test + void testConstructor() { + assertEquals(0x1F, values.getArray()[0].getLength()); + assertEquals(0x21, values.getArray()[1].getLength()); + } + + @Test + void testParse() throws ParseException { + assertEquals(0x1F, new Values(new BytesReader(new Byte[]{ + 0x31, 0x21, + 0x30, 0x1F, // SEQUENCE (AttributeValue) + 0x30, 0x1D, // SEQUENCE + 0x06, 0x03, 0x55, 0x1D, 0x25, // 2.5.29.37 extKeyUsage + 0x04, 0x16, // OCTET STRING + 0x30, 0x14, // SEQUENCE + // 1.3.6.1.5.5.7.3.1 serverAuth + 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, + // 1.3.6.1.5.5.7.3.2 clientAuth + 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02 + }), false).getArray()[0].getLength()); + assertEquals(0x21, new Values(new BytesReader(new Byte[]{ + 0x31, 0x23, + 0x30, 0x21, // SEQUENCE (AttributeValue) + 0x30, 0x1F, // SEQUENCE + 0x06, 0x03, 0x55, 0x1D, 0x11, // 2.5.29.17 subjectAltName + 0x04, 0x18, // OCTET STRING + 0x30, 0x16, // SEQUENCE + -126, 0x14, // [2] + 0x6C, 0x70, 0x2D, 0x62, 0x32, 0x35, 0x35, 0x2E, 0x61, // lp-b255.yuuta.moe + 0x64, 0x2E, 0x79, 0x75, 0x75, 0x74, 0x61, 0x2E, 0x6D, 0x6F, 0x65 + }), false).getArray()[0].getLength()); + } + + @Test + void testParseFail() { + // Incorrect length + assertThrows(ParseException.class, () -> new Values(new BytesReader(new Byte[]{ + 0x31, 0x29, // Incorrect length! + 0x30, 0x21, // SEQUENCE (AttributeValue) + 0x30, 0x1F, // SEQUENCE + 0x06, 0x03, 0x55, 0x1D, 0x11, // 2.5.29.17 subjectAltName + 0x04, 0x18, // OCTET STRING + 0x30, 0x16, // SEQUENCE + -126, 0x14, // [2] + 0x6C, 0x70, 0x2D, 0x62, 0x32, 0x35, 0x35, 0x2E, 0x61, // lp-b255.yuuta.moe + 0x64, 0x2E, 0x79, 0x75, 0x75, 0x74, 0x61, 0x2E, 0x6D, 0x6F, 0x65 + }), false)); + } + + @Test + void testEncode() { + assertArrayEquals(new Byte[]{ + 0x31, 68, + + 0x30, 0x1F, // SEQUENCE (AttributeValue) + 0x30, 0x1D, // SEQUENCE + 0x06, 0x03, 0x55, 0x1D, 0x25, // 2.5.29.37 extKeyUsage + 0x04, 0x16, // OCTET STRING + 0x30, 0x14, // SEQUENCE + // 1.3.6.1.5.5.7.3.1 serverAuth + 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, + // 1.3.6.1.5.5.7.3.2 clientAuth + 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, + + 0x30, 0x21, // SEQUENCE (AttributeValue) + 0x30, 0x1F, // SEQUENCE + 0x06, 0x03, 0x55, 0x1D, 0x11, // 2.5.29.17 subjectAltName + 0x04, 0x18, // OCTET STRING + 0x30, 0x16, // SEQUENCE + -126, 0x14, // [2] + 0x6C, 0x70, 0x2D, 0x62, 0x32, 0x35, 0x35, 0x2E, 0x61, // lp-b255.yuuta.moe + 0x64, 0x2E, 0x79, 0x75, 0x75, 0x74, 0x61, 0x2E, 0x6D, 0x6F, 0x65 + }, values.encodeDER()); + } +} -- cgit v1.2.3