aboutsummaryrefslogtreecommitdiff
path: root/src/test/model/csr/CertificationRequestInfoTest.java
blob: fe2633f0a6f59919cf71bf8a8ae9a791ced2ae23 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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());
    }
}