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/pki/SubjectPublicKeyInfoTest.java | 115 +++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/test/model/pki/SubjectPublicKeyInfoTest.java (limited to 'src/test/model/pki/SubjectPublicKeyInfoTest.java') diff --git a/src/test/model/pki/SubjectPublicKeyInfoTest.java b/src/test/model/pki/SubjectPublicKeyInfoTest.java new file mode 100644 index 0000000..f1cfff0 --- /dev/null +++ b/src/test/model/pki/SubjectPublicKeyInfoTest.java @@ -0,0 +1,115 @@ +package model.pki; + +import model.asn1.ASN1Object; +import model.asn1.BitString; +import model.asn1.Null; +import model.asn1.ObjectIdentifier; +import model.asn1.exceptions.ParseException; +import model.asn1.parsing.BytesReader; +import model.csr.CertificationRequestInfo; +import model.csr.CertificationRequestInfoTest; +import org.junit.jupiter.api.Test; + +import static model.TestConstants.mutate; +import static org.junit.jupiter.api.Assertions.*; + +public class SubjectPublicKeyInfoTest { + private static final Byte[] RSA = new Byte[] { + 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, + }; + + private static final Byte[] ECC = new Byte[] { + 0x30, 0x59, // SEQUENCE SubjectPublicKeyInfo + 0x30, 0x13, // SEQUENCE AlgorithmIdentifier + 0x06, 0x07, 0x2A, -122, 0x48, -50, 0x3D, 0x02, 0x01, // OID (ecPublicKey) + 0x06, 0x08, 0x2A, -122, 0x48, -50, 0x3D, 0x03, 0x01, // OID Parameter (prime256v1) + 0x07, + 0x03, 0x42, // BIT STRING + 0x00, 0x04, 0x1D, -24, 0x71, -68, -35, 0x48, 0x70, + 0x26, 0x71, 0x6C, -35, 0x04, 0x5B, 0x3F, 0x5D, -34, + 0x14, 0x31, -117, 0x3F, 0x31, -128, 0x18, 0x2A, 0x33, + -27, 0x19, -122, 0x13, -42, -25, 0x48, 0x2F, -107, + 0x15, 0x3A, 0x59, -115, -19, 0x09, -28, 0x53, 0x1A, + -13, 0x61, -78, 0x35, 0x61, 0x6E, 0x66, 0x5F, 0x5F, + -49, 0x0A, -30, 0x65, 0x65, 0x3D, 0x22, 0x2B, 0x30, + 0x71, 0x2C, 0x24 + }; + + @Test + void testConstructor() { + assertArrayEquals(ObjectIdentifier.OID_SHA256_WITH_RSA_ENCRYPTION, + 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})) + .getAlgorithm().getType().getInts()); + assertArrayEquals(new Byte[]{ 1, 2, 3 }, + 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})) + .getSubjectPublicKey().getConvertedVal()); + } + + @Test + void testParse() throws ParseException { + SubjectPublicKeyInfo parsed = new SubjectPublicKeyInfo(new BytesReader(RSA), false); + assertArrayEquals(ObjectIdentifier.OID_RSA_ENCRYPTION, parsed.getAlgorithm().getType().getInts()); + assertEquals(Null.TAG.getNumber(), parsed.getAlgorithm().getParameters().getTag().getNumber()); + assertEquals(140, parsed.getSubjectPublicKey().getConvertedVal().length); + + parsed = new SubjectPublicKeyInfo(new BytesReader(ECC), false); + assertArrayEquals(ObjectIdentifier.OID_EC_PUBLIC_KEY, parsed.getAlgorithm().getType().getInts()); + assertEquals(ObjectIdentifier.TAG.getNumber(), parsed.getAlgorithm().getParameters().getTag().getNumber()); + assertArrayEquals(ObjectIdentifier.OID_PRIME256_V1, + ((ObjectIdentifier) parsed.getAlgorithm().getParameters()).getInts()); + assertEquals(65, parsed.getSubjectPublicKey().getConvertedVal().length); + } + + @Test + void testParseFail() { + // No algorithm + assertThrows(ParseException.class, () -> { + new SubjectPublicKeyInfo(new BytesReader(new Byte[]{ + 0x30, 0 + }), false); + }); + // Incorrect algorithm ID tag + assertThrows(ParseException.class, () -> { + new CertificationRequestInfo(new BytesReader(mutate(RSA, 3, 0x30, 0x31)), false); + }); + // Incorrect public key tag + assertThrows(ParseException.class, () -> { + new CertificationRequestInfo(new BytesReader(mutate(RSA, 18, BitString.TAG.getNumber(), 0x31)), + false); + }); + } +} -- cgit v1.2.3