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
|
package model.pki.cert;
import model.TestConstants;
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.pki.AlgorithmIdentifier;
import org.junit.jupiter.api.Test;
import static model.TestConstants.mutate;
import static org.junit.jupiter.api.Assertions.*;
public class CertificateTest {
@Test
void testConstructor() {
final Certificate certificate = new Certificate(ASN1Object.TAG_SEQUENCE, null,
TestConstants.CERT_GENERATED,
new AlgorithmIdentifier(ASN1Object.TAG_SEQUENCE, null,
new ObjectIdentifier(ObjectIdentifier.TAG, null, ObjectIdentifier.OID_RSA_ENCRYPTION),
new Null(Null.TAG, null)),
new BitString(BitString.TAG, null, 0, new Byte[]{1, 2, 3}));
assertEquals(TbsCertificate.VERSION_V3,
certificate.getCertificate().getVersion().getLong());
assertArrayEquals(ObjectIdentifier.OID_RSA_ENCRYPTION,
certificate.getSignatureAlgorithm().getType().getInts());
assertArrayEquals(new Byte[]{1, 2, 3},
certificate.getSignature().getConvertedVal());
}
@Test
void testParse() throws ParseException {
Certificate parsed = new Certificate(new BytesReader(TestConstants.CERT_L2_RSA), false);
assertEquals(TbsCertificate.VERSION_V3,
parsed.getCertificate().getVersion().getLong());
assertArrayEquals(ObjectIdentifier.OID_ECDSA_WITH_SHA512, parsed.getSignatureAlgorithm().getType().getInts());
assertNull(parsed.getSignatureAlgorithm().getParameters());
assertEquals(70, parsed.getSignature().getVal().length);
parsed = new Certificate(new BytesReader(TestConstants.CERT_L1_ECC), false);
assertEquals(TbsCertificate.VERSION_V3,
parsed.getCertificate().getVersion().getLong());
assertArrayEquals(ObjectIdentifier.OID_ECDSA_WITH_SHA256, parsed.getSignatureAlgorithm().getType().getInts());
assertNull(parsed.getSignatureAlgorithm().getParameters());
assertEquals(71, parsed.getSignature().getVal().length);
}
@Test
void testParseFail() {
// Incorrect certificate tag
assertThrows(ParseException.class, () ->
new Certificate(new BytesReader(mutate(TestConstants.CERT_L1_ECC, 4, 0x30, 0x31)), false)
);
// Incorrect signatureAlgorithm tag
assertThrows(ParseException.class, () ->
new Certificate(new BytesReader(mutate(TestConstants.CERT_L1_ECC, 349, 0x30, 0x31)), false)
);
// Incorrect signature tag
assertThrows(ParseException.class, () ->
new Certificate(new BytesReader(mutate(TestConstants.CERT_L1_ECC, 361, 0x3, 0x5)), false)
);
}
@Test
void testEncode() throws ParseException {
assertArrayEquals(TestConstants.CERT_V1,
new Certificate(new BytesReader(TestConstants.CERT_V1), false).encodeDER());
assertArrayEquals(TestConstants.CERT_L1_ECC,
new Certificate(new BytesReader(TestConstants.CERT_L1_ECC), false).encodeDER());
assertArrayEquals(TestConstants.CERT_L2_RSA,
new Certificate(new BytesReader(TestConstants.CERT_L2_RSA), false).encodeDER());
}
}
|