aboutsummaryrefslogtreecommitdiff
path: root/src/test/model/csr/CertificationRequestTest.java
blob: 962e90b7aa72221db2a7950ab104822edf7a206c (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
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 java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;

import static model.TestConstants.mutate;
import static org.junit.jupiter.api.Assertions.*;

public class CertificationRequestTest {
    private static final Byte[] CSR_1 = Stream.of(
            // SEQUENCE (CertificationRequest)
            Arrays.asList(new Byte[]{ 0x30, -126, 0x02, -102 }),
            // SEQUENCE (CertificationRequestInfo)
            Arrays.asList(CertificationRequestInfoTest.CSR_1),
            // SEQUENCE (AlgorithmIdentifier)
            Arrays.asList(new Byte[]{
                    0x30, 0x0D, 0x06, 0x09, 0x2A, -122, 0x48, -122,
                      -9, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00
            }),
            // BIT STRING (Signature)
            Arrays.asList(new Byte[]{
                    0x03, -127, -127,
                    0x00, 0x6F, 0x61, 0x5C,  -25, 0x29, 0x48, 0x3F,
                     -78, 0x1B, -117, 0x2C,  -93, -114, 0x7D,  -77,
                    0x62, 0x14, 0x21, 0x4B,  -99, 0x74,  -95,  -93,
                    0x16, 0x38, 0x31, 0x40, 0x5E, 0x72,  -77,  -55,
                    0x6D,  -69, 0x19, -108, 0x52,  -95, 0x19, -121,
                     -81,  -71, 0x74, -123, 0x6B,  -27,  -20, 0x4C,
                    -126, 0x42,  -89, 0x66, 0x6A, 0x52,  -34, 0x62,
                    0x72, 0x40, 0x2C,  -79, 0x78, -117, -100,  -70,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x7E,
            })).flatMap(Collection::stream).toArray(Byte[]::new);

    @Test
    void testConstructor() {
        final CertificationRequest request = new CertificationRequest(
                ASN1Object.TAG_SEQUENCE, null,
                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),
                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[]{ 2, 4, 6, 8, 10 }));

        assertEquals(CertificationRequestInfo.VERSION_V1,
                request.getCertificationRequestInfo().getVersion().getLong());
        assertEquals(3,
                request.getCertificationRequestInfo().getSubject().getRdnSequence().length);
        assertArrayEquals(ObjectIdentifier.OID_SHA256_WITH_RSA_ENCRYPTION,
                request.getSignatureAlgorithm().getType().getInts());
        assertArrayEquals(new Byte[]{ 2, 4, 6, 8, 10 },
                request.getSignature().getConvertedVal());
    }

    @Test
    void testParse() throws ParseException {
        final CertificationRequest parsed =
                new CertificationRequest(new BytesReader(CSR_1), false);
        assertEquals("CN=MIKU.AD.YUUTA.MOE",
                parsed.getCertificationRequestInfo().getSubject().toString());
        assertArrayEquals(ObjectIdentifier.OID_SHA256_WITH_RSA_ENCRYPTION,
                parsed.getSignatureAlgorithm().getType().getInts());
    }

    @Test
    void testParseFail() throws ParseException {
        // Incorrect info tag
        assertThrows(ParseException.class, () -> {
            new CertificationRequest(new BytesReader(mutate(CSR_1, 4, 0x30, 0x31)), false);
        });
        // Incorrect algorithm info tag
        assertThrows(ParseException.class, () -> {
            new CertificationRequest(new BytesReader(mutate(CSR_1, 523, 0x30, 0x31)), false);
        });
        // Incorrect signature tag
        assertThrows(ParseException.class, () -> {
            new CertificationRequest(new BytesReader(mutate(CSR_1, 538, 0x3, 0x31)), false);
        });
    }

    @Test
    void testEncode() throws ParseException {
        assertArrayEquals(CSR_1, new CertificationRequest(new BytesReader(CSR_1), false).encodeDER());
    }
}