aboutsummaryrefslogtreecommitdiff
path: root/src/test/model/pki/SubjectPublicKeyInfoTest.java
blob: f1cfff0325078ff1ed2e0e1aa63955b4f4095342 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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);
        });
    }
}