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/AlgorithmIdentifierTest.java | 84 +++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/test/model/pki/AlgorithmIdentifierTest.java (limited to 'src/test/model/pki/AlgorithmIdentifierTest.java') diff --git a/src/test/model/pki/AlgorithmIdentifierTest.java b/src/test/model/pki/AlgorithmIdentifierTest.java new file mode 100644 index 0000000..8bcc80e --- /dev/null +++ b/src/test/model/pki/AlgorithmIdentifierTest.java @@ -0,0 +1,84 @@ +package model.pki; + +import model.asn1.ASN1Object; +import model.asn1.Null; +import model.asn1.ObjectIdentifier; +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 AlgorithmIdentifierTest { + @Test + void testConstructor() { + assertArrayEquals(OID_SHA256_WITH_RSA_ENCRYPTION, + new AlgorithmIdentifier( + ASN1Object.TAG_SEQUENCE, null, + new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_SHA256_WITH_RSA_ENCRYPTION), + new Null(Null.TAG, null)) + .getType().getInts()); + assertEquals(Null.TAG.getNumber(), + new AlgorithmIdentifier( + ASN1Object.TAG_SEQUENCE, null, + new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_SHA256_WITH_RSA_ENCRYPTION), + new Null(Null.TAG, null)) + .getParameters().getTag().getNumber()); + } + + @Test + void testParse() throws ParseException { + AlgorithmIdentifier parsed = new AlgorithmIdentifier(new BytesReader(new Byte[]{ + 0x30, 0x0d, // SEQUENCE AlgorithmIdentifier + 0x06, 0x09, 0x2a, -122, 0x48, -122, -9, 0x0d, // OID (rsaEncryption) + 0x01, 0x01, 0x01, + 0x05, 0x00, // Null (Parameter) + }), false); + assertArrayEquals(OID_RSA_ENCRYPTION, parsed.getType().getInts()); + assertEquals(Null.TAG.getNumber(), parsed.getParameters().getTag().getNumber()); + + parsed = new AlgorithmIdentifier(new BytesReader(new Byte[]{ + 0x30, 0x0D, // SEQUENCE AlgorithmIdentifier + 0x06, 0x09, 0x2A, -122, 0x48, -122, -9, 0x0D, // OID (sha256WithRsaEncryption) + 0x01, 0x01, 0x0B, + 0x05, 0x00 // Null (Parameter) + }), false); + assertArrayEquals(OID_SHA256_WITH_RSA_ENCRYPTION, parsed.getType().getInts()); + assertEquals(Null.TAG.getNumber(), parsed.getParameters().getTag().getNumber()); + + parsed = new AlgorithmIdentifier(new BytesReader(new Byte[]{ + 0x30, 0x0A, // SEQUENCE AlgorithmIdentifier + 0x06, 0x08, 0x2A, -122, 0x48, -50, 0x3D, 0x04, 0x03, 0x02 // OID (ecdsaWithSHA256) + }), false); + assertArrayEquals(OID_ECDSA_WITH_SHA256, parsed.getType().getInts()); + assertNull(parsed.getParameters()); + } + + @Test + void testParseFail() { + // No type + assertThrows(ParseException.class, () -> new AlgorithmIdentifier(new BytesReader(new Byte[]{ + 0x30, 0x0 + }), false)); + // Incorrect type tag (should be OID) + assertThrows(ParseException.class, () -> new AlgorithmIdentifier(new BytesReader(new Byte[]{ + 0x30, 0x0B, // SEQUENCE AlgorithmIdentifier + 0x07, 0x09, 0x2A, -122, 0x48, -122, -9, 0x0D, // Incorrect tag + 0x01, 0x01, 0x0B, + }), false)); + } + + @Test + void testEncode() throws ParseException { + assertArrayEquals(new Byte[]{ + 0x30, 0x0D, // SEQUENCE AlgorithmIdentifier + 0x06, 0x09, 0x2A, -122, 0x48, -122, -9, 0x0D, // OID (sha256WithRsaEncryption) + 0x01, 0x01, 0x0B, + 0x05, 0x00 // Null (Parameter) + }, new AlgorithmIdentifier( + ASN1Object.TAG_SEQUENCE, null, + new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_SHA256_WITH_RSA_ENCRYPTION), + new Null(Null.TAG, null)).encodeDER()); + } +} -- cgit v1.2.3