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/x501/AttributeTypeAndValueTest.java | 90 +++++++++++ src/test/model/x501/NameTest.java | 167 +++++++++++++++++++++ .../model/x501/RelativeDistinguishedNameTest.java | 95 ++++++++++++ 3 files changed, 352 insertions(+) create mode 100644 src/test/model/x501/AttributeTypeAndValueTest.java create mode 100644 src/test/model/x501/NameTest.java create mode 100644 src/test/model/x501/RelativeDistinguishedNameTest.java (limited to 'src/test/model/x501') diff --git a/src/test/model/x501/AttributeTypeAndValueTest.java b/src/test/model/x501/AttributeTypeAndValueTest.java new file mode 100644 index 0000000..ea9c17e --- /dev/null +++ b/src/test/model/x501/AttributeTypeAndValueTest.java @@ -0,0 +1,90 @@ +package model.x501; + +import model.asn1.*; +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 AttributeTypeAndValueTest { + @Test + void testConstructor() throws ParseException { + assertArrayEquals(OID_OU, + new AttributeTypeAndValue( + ASN1Object.TAG_SEQUENCE, null, + new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_OU), + new Null(Null.TAG, null)) + .getType().getInts()); + assertEquals("123", + new AttributeTypeAndValue( + ASN1Object.TAG_SEQUENCE, null, + new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_OU), + new PrintableString(PrintableString.TAG, null, "123")) + .getValue().toString()); + } + + @Test + void testParse() throws ParseException { + // C = IT + assertArrayEquals(OID_C, new AttributeTypeAndValue(new BytesReader(new Byte[]{ + 0x30, 0x9, 0x6, 0x3, 0x55, 0x4, 0x6, 0x13, 0x2, 0x49, 0x54 + }), false).getType().getInts()); + assertEquals("IT", ((PrintableString) new AttributeTypeAndValue(new BytesReader(new Byte[]{ + 0x30, 0x9, 0x6, 0x3, 0x55, 0x4, 0x6, 0x13, 0x2, 0x49, 0x54 + }), false).getValue()).getString()); + + // CN = Test ed25519 + assertArrayEquals(OID_CN, new AttributeTypeAndValue(new BytesReader(new Byte[]{ + 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, + 0x0C, 0x54, 0x65, 0x73, 0x74, 0x20, 0x65, 0x64, + 0x32, 0x35, 0x35, 0x31, 0x39 + }), false).getType().getInts()); + assertEquals("Test ed25519", ((ASN1String) new AttributeTypeAndValue(new BytesReader(new Byte[]{ + 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, + 0x0C, 0x54, 0x65, 0x73, 0x74, 0x20, 0x65, 0x64, + 0x32, 0x35, 0x35, 0x31, 0x39 + }), false).getValue()).getString()); + } + + @Test + void testParseFail() { + // No type + assertThrows(ParseException.class, () -> new AttributeTypeAndValue(new BytesReader(new Byte[]{ + 0x30, 0x0 + }), false)); + // No value + assertThrows(ParseException.class, () -> new AttributeTypeAndValue(new BytesReader(new Byte[]{ + 0x30, 0x5, 0x6, 0x3, 0x55, 0x4, 0x6 + }), false)); + // Incorrect type tag (should be OID) + assertThrows(ParseException.class, () -> new AttributeTypeAndValue(new BytesReader(new Byte[]{ + 0x30, 0x9, 0x7, 0x3, 0x55, 0x4, 0x6, 0x13, 0x2, 0x49, 0x54 + }), false)); + } + + @Test + void testEncode() throws ParseException { + assertArrayEquals(new Byte[]{ + 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, + 0x0C, 0x54, 0x65, 0x73, 0x74, 0x20, 0x65, 0x64, + 0x32, 0x35, 0x35, 0x31, 0x39 + }, new AttributeTypeAndValue(ASN1Object.TAG_SEQUENCE, null, + new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_CN), + new UTF8String(UTF8String.TAG, null, "Test ed25519")) + .encodeDER()); + } + + @Test + void testToString() throws ParseException { + assertEquals("CN=Test ed25519", new AttributeTypeAndValue(new BytesReader(new Byte[]{ + 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, + 0x0C, 0x54, 0x65, 0x73, 0x74, 0x20, 0x65, 0x64, + 0x32, 0x35, 0x35, 0x31, 0x39 + }), false).toString()); + assertEquals("C=IT", new AttributeTypeAndValue(new BytesReader(new Byte[]{ + 0x30, 0x9, 0x6, 0x3, 0x55, 0x4, 0x6, 0x13, 0x2, 0x49, 0x54 + }), false).toString()); + } +} diff --git a/src/test/model/x501/NameTest.java b/src/test/model/x501/NameTest.java new file mode 100644 index 0000000..c649798 --- /dev/null +++ b/src/test/model/x501/NameTest.java @@ -0,0 +1,167 @@ +package model.x501; + +import model.asn1.exceptions.ParseException; +import model.asn1.parsing.BytesReader; +import model.TestConstants; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class NameTest { + @Test + void testConstructor() { + assertEquals("users", + TestConstants.NAME_1.getRdnSequence()[1].getArray()[0].getValue().toString()); + assertEquals("CN", + TestConstants.NAME_2.getRdnSequence()[2].getArray()[0].getValue().toString()); + } + + @Test + void testParse() throws ParseException { + assertEquals("CA", new Name(new BytesReader(new Byte[]{ + 0x30, 45, // Name + + 0x31, 14, // RDN[0] + 0x30, 12, // KV[0] + 0x6, 3, 0x55, 0x04, 0x03, // CN + 0x13, 5, 'y', 'u', 'u', 't', 'a', // yuuta + + 0x31, 14, // RDN[1] + 0x30, 12, // KV[0] + 0x6, 3, 0x55, 0x04, 0xB, // OU + 0x13, 5, 'u', 's', 'e', 'r', 's', // users + + 0x31, 11, // RDN[2] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0x6, // C + 0x13, 2, 'C', 'A' // CA + }), false).getRdnSequence()[2].getArray()[0].getValue().toString()); + + assertEquals("SN=Qwq", new Name(new BytesReader(new Byte[]{ + 0x30, 38, // Name + + 0x31, 12, // RDN[0] + 0x30, 10, // KV[0] + 0x6, 3, 0x55, 0x04, 0x04, // CN + 0x13, 3, 'Q', 'w', 'q', // Qwq + + 0x31, 9, // RDN[1] + 0x30, 7, // KV[0] + 0x6, 3, 0x55, 0x04, 0xA, // O + 0x13, 2, 'I', 'T', // IT + + 0x31, 11, // RDN[2] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0x6, // C + 0x13, 2, 'C', 'N' // CN + }), false).getRdnSequence()[0].toString()); + } + + @Test + void testParseFail() { + assertThrows(ParseException.class, () -> new Name(new BytesReader(new Byte[]{ + 0x30, 38, // Name + + // Wrong tag here + 0x30, 12, // RDN[0] + 0x30, 10, // KV[0] + 0x6, 3, 0x55, 0x04, 0x04, // CN + 0x13, 3, 'Q', 'w', 'q', // Qwq + + 0x31, 9, // RDN[1] + 0x30, 7, // KV[0] + 0x6, 3, 0x55, 0x04, 0xA, // O + 0x13, 2, 'I', 'T', // IT + + 0x31, 11, // RDN[2] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0x6, // C + 0x13, 2, 'C', 'N' // CN + }), false)); + assertThrows(ParseException.class, () -> new Name(new BytesReader(new Byte[]{ + 0x30, 38, // Name + + 0x31, 12, // RDN[0] + 0x30, 10, // KV[0] + 0x6, 3, 0x55, 0x04, 0x04, // CN + 0x13, 3, 'Q', 'w', 'q', // Qwq + + 0x31, 9, // RDN[1] + // Wrong tag here + 0x31, 7, // KV[0] + 0x6, 3, 0x55, 0x04, 0xA, // O + 0x13, 2, 'I', 'T', // IT + + 0x31, 11, // RDN[2] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0x6, // C + 0x13, 2, 'C', 'N' // CN + }), false)); + assertThrows(ParseException.class, () -> new Name(new BytesReader(new Byte[]{ + 0x30, 38, // Name + + 0x31, 12, // RDN[0] + 0x30, 10, // KV[0] + 0x6, 3, 0x55, 0x04, 0x04, // CN + 0x13, 3, 'Q', 'w', 'q', // Qwq + + 0x31, 9, // RDN[1] + 0x30, 7, // KV[0] + 0x6, 3, 0x55, 0x04, 0xA, // O + 0x13, 2, 'I', 'T', // IT + + // Wrong tag here + 0x30, 11, // RDN[2] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0x6, // C + 0x13, 2, 'C', 'N' // CN + }), false)); + } + + @Test + void testEncode() { + assertArrayEquals(new Byte[]{ + 0x30, 45, // Name + + 0x31, 14, // RDN[0] + 0x30, 12, // KV[0] + 0x6, 3, 0x55, 0x04, 0x03, // CN + 0x13, 5, 'y', 'u', 'u', 't', 'a', // yuuta + + 0x31, 14, // RDN[1] + 0x30, 12, // KV[0] + 0x6, 3, 0x55, 0x04, 0xB, // OU + 0x13, 5, 'u', 's', 'e', 'r', 's', // users + + 0x31, 11, // RDN[2] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0x6, // C + 0x13, 2, 'C', 'A' // CA + }, TestConstants.NAME_1.encodeDER()); + + assertArrayEquals(new Byte[]{ + 0x30, 40, // Name + + 0x31, 12, // RDN[0] + 0x30, 10, // KV[0] + 0x6, 3, 0x55, 0x04, 0x04, // CN + 0x13, 3, 'Q', 'w', 'q', // Qwq + + 0x31, 11, // RDN[1] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0xA, // O + 0x13, 2, 'I', 'T', // IT + + 0x31, 11, // RDN[2] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0x6, // C + 0x13, 2, 'C', 'N' // CN + }, TestConstants.NAME_2.encodeDER()); + } + + @Test + void testToString() { + assertEquals("CN=yuuta,OU=users,C=CA", TestConstants.NAME_1.toString()); + assertEquals("SN=Qwq,O=IT,C=CN", TestConstants.NAME_2.toString()); + } +} diff --git a/src/test/model/x501/RelativeDistinguishedNameTest.java b/src/test/model/x501/RelativeDistinguishedNameTest.java new file mode 100644 index 0000000..d066010 --- /dev/null +++ b/src/test/model/x501/RelativeDistinguishedNameTest.java @@ -0,0 +1,95 @@ +package model.x501; + +import model.asn1.*; +import model.asn1.exceptions.ParseException; +import model.asn1.parsing.BytesReader; +import model.TestConstants; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +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 RelativeDistinguishedNameTest { + @Test + void testConstructor() throws ParseException { + assertArrayEquals(OID_OU, + new RelativeDistinguishedName(TAG_SET, null, + new AttributeTypeAndValue[]{ + new AttributeTypeAndValue( + ASN1Object.TAG_SEQUENCE, null, + new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_OU), + new Null(Null.TAG, null)) + }).getArray()[0].getType().getInts()); + assertEquals("123", + new RelativeDistinguishedName(TAG_SET, null, + new AttributeTypeAndValue[]{ + new AttributeTypeAndValue( + ASN1Object.TAG_SEQUENCE, null, + new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_OU), + new PrintableString(PrintableString.TAG, null, "123")) + }).getArray()[0].getValue().toString()); + } + + @Test + void testParse() throws ParseException { + assertEquals(1, TestConstants.L_MILANO.getArray().length); + assertEquals("Milano", TestConstants.L_MILANO.getArray()[0].getValue().toString()); + + assertEquals(2, TestConstants.L_MILANO_CN_TEST_ED25519.getArray().length); + assertEquals("Test ed25519", + TestConstants.L_MILANO_CN_TEST_ED25519.getArray()[0].getValue().toString()); + } + + @Test + void testParseFail() { + // Invalid child tag + assertThrows(ParseException.class, () -> + new RelativeDistinguishedName(new BytesReader(new Byte[] { + 0x31, 0x0F, + 0x31, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, + 0x06, 0x4D, 0x69, 0x6C, 0x61, 0x6E, 0x6F + }), false)); + assertThrows(ParseException.class, () -> + new RelativeDistinguishedName(new BytesReader(new Byte[] { + 0x31, 0x23, + // CN + 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, + 0x54, 0x65, 0x73, 0x74, 0x20, 0x65, 0x64, 0x32, + 0x35, 0x35, 0x31, 0x39, + // L + 0x31, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, + 0x06, 0x4D, 0x69, 0x6C, 0x61, 0x6E, 0x6F + }), false)); + } + + @Test + void testEncode() throws ParseException { + assertArrayEquals(TestConstants.combine((byte) 0x31, + TestConstants.CN_TEST_ED25519_DER, + TestConstants.L_MILANO_DER), + TestConstants.L_MILANO_CN_TEST_ED25519.encodeDER()); + assertArrayEquals(new Byte[]{ + 0x31, 15, + 0x30, 13, + 0x06, 3, 0x55, 0x04, 0x06, + 0x13, 6, '1', '2', '3', '1', '2', '3' + + }, new RelativeDistinguishedName(TAG_SET, null, + new AttributeTypeAndValue[]{ + new AttributeTypeAndValue( + ASN1Object.TAG_SEQUENCE, null, + new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_C), + new PrintableString(PrintableString.TAG, null, "123123"))}) + .encodeDER()); + } + + @Test + void testToString() { + assertEquals("L=Milano", TestConstants.L_MILANO.toString()); + assertEquals("CN=Test ed25519+L=Milano", TestConstants.L_MILANO_CN_TEST_ED25519.toString()); + } +} -- cgit v1.2.3