nx_x509_pubkey_alg_test.nx source
↩ module page · 88 lines · 4100 B
1// nx_x509_pubkey_alg_test.nx -- KAT for pubkey-alg OID identification.
2//
3// expect_exit: 0
4// license_tier: ORIGINAL
5
6import "nx_syscalls.nx"
7import "nx_x509_pubkey_alg.nx"
8
9func main() -> i64 {
10 // ---- Test A: Ed25519 SPKI AlgorithmIdentifier ----
11 // SEQUENCE { OID 1.3.101.112 } = 30 05 06 03 2B 65 70
12 let ed: *u8 = sys_mmap(16)
13 ed[0]=0x30; ed[1]=0x05; ed[2]=0x06; ed[3]=0x03
14 ed[4]=0x2B; ed[5]=0x65; ed[6]=0x70
15 if nx_x509_pubkey_alg_identify(ed, 0, 7) != NX_X509_PUBKEY_ALG_ED25519 { return 1 }
16
17 // ---- Test B: EC P-256 (id-ecPublicKey + prime256v1 curve) ----
18 // SEQUENCE { OID 1.2.840.10045.2.1, OID 1.2.840.10045.3.1.7 }
19 // = 30 13 06 07 2A 86 48 CE 3D 02 01
20 // 06 08 2A 86 48 CE 3D 03 01 07
21 let ec256: *u8 = sys_mmap(32)
22 ec256[0]=0x30; ec256[1]=0x13
23 ec256[2]=0x06; ec256[3]=0x07
24 ec256[4]=0x2A; ec256[5]=0x86; ec256[6]=0x48; ec256[7]=0xCE
25 ec256[8]=0x3D; ec256[9]=0x02; ec256[10]=0x01
26 ec256[11]=0x06; ec256[12]=0x08
27 ec256[13]=0x2A; ec256[14]=0x86; ec256[15]=0x48; ec256[16]=0xCE
28 ec256[17]=0x3D; ec256[18]=0x03; ec256[19]=0x01; ec256[20]=0x07
29 if nx_x509_pubkey_alg_identify(ec256, 0, 21) != NX_X509_PUBKEY_ALG_EC_P256 { return 2 }
30
31 // ---- Test C: id-ecPublicKey + WRONG curve (P-384: 2A 86 48 CE 3D 03 01 22) -> UNKNOWN ----
32 let ec384: *u8 = sys_mmap(32)
33 ec384[0]=0x30; ec384[1]=0x13
34 ec384[2]=0x06; ec384[3]=0x07
35 ec384[4]=0x2A; ec384[5]=0x86; ec384[6]=0x48; ec384[7]=0xCE
36 ec384[8]=0x3D; ec384[9]=0x02; ec384[10]=0x01
37 ec384[11]=0x06; ec384[12]=0x05
38 ec384[13]=0x2B; ec384[14]=0x81; ec384[15]=0x04; ec384[16]=0x00; ec384[17]=0x22
39 if nx_x509_pubkey_alg_identify(ec384, 0, 18) != NX_X509_PUBKEY_ALG_UNKNOWN { return 3 }
40
41 // ---- Test D: id-ecPublicKey with NO parameters -> UNKNOWN ----
42 let ec_noparam: *u8 = sys_mmap(16)
43 ec_noparam[0]=0x30; ec_noparam[1]=0x09
44 ec_noparam[2]=0x06; ec_noparam[3]=0x07
45 ec_noparam[4]=0x2A; ec_noparam[5]=0x86; ec_noparam[6]=0x48; ec_noparam[7]=0xCE
46 ec_noparam[8]=0x3D; ec_noparam[9]=0x02; ec_noparam[10]=0x01
47 if nx_x509_pubkey_alg_identify(ec_noparam, 0, 11) != NX_X509_PUBKEY_ALG_UNKNOWN { return 4 }
48
49 // ---- Test E: RSA SPKI alg (rsaEncryption 1.2.840.113549.1.1.1) -> UNKNOWN ----
50 let rsa: *u8 = sys_mmap(32)
51 rsa[0]=0x30; rsa[1]=0x0D
52 rsa[2]=0x06; rsa[3]=0x09
53 rsa[4]=0x2A; rsa[5]=0x86; rsa[6]=0x48; rsa[7]=0x86
54 rsa[8]=0xF7; rsa[9]=0x0D; rsa[10]=0x01; rsa[11]=0x01; rsa[12]=0x01
55 rsa[13]=0x05; rsa[14]=0x00 // NULL parameters
56 if nx_x509_pubkey_alg_identify(rsa, 0, 15) != NX_X509_PUBKEY_ALG_UNKNOWN { return 5 }
57
58 // ---- Test F: bad outer tag -> UNKNOWN ----
59 let bad: *u8 = sys_mmap(8)
60 bad[0]=0x99; bad[1]=0x00; bad[2]=0x00; bad[3]=0x00
61 if nx_x509_pubkey_alg_identify(bad, 0, 4) != NX_X509_PUBKEY_ALG_UNKNOWN { return 6 }
62
63 // ---- Test G: too short -> UNKNOWN ----
64 if nx_x509_pubkey_alg_identify(bad, 0, 2) != NX_X509_PUBKEY_ALG_UNKNOWN { return 7 }
65
66 // ---- Test H: bad inner tag -> UNKNOWN ----
67 let bad2: *u8 = sys_mmap(8)
68 bad2[0]=0x30; bad2[1]=0x03; bad2[2]=0x99; bad2[3]=0x01; bad2[4]=0x00
69 if nx_x509_pubkey_alg_identify(bad2, 0, 5) != NX_X509_PUBKEY_ALG_UNKNOWN { return 8 }
70
71 // ---- Test I: identify at nonzero offset ----
72 let bigger: *u8 = sys_mmap(32)
73 var i: i64 = 0
74 while i < 32 { bigger[i] = 0xCC; i = i + 1 }
75 i = 0
76 while i < 7 { bigger[10 + i] = ed[i]; i = i + 1 }
77 if nx_x509_pubkey_alg_identify(bigger, 10, 7) != NX_X509_PUBKEY_ALG_ED25519 { return 9 }
78
79 // ---- Test J: is_supported gate ----
80 if nx_x509_pubkey_alg_is_supported(NX_X509_PUBKEY_ALG_UNKNOWN) != 0 { return 10 }
81 if nx_x509_pubkey_alg_is_supported(NX_X509_PUBKEY_ALG_ED25519) != 1 { return 11 }
82 if nx_x509_pubkey_alg_is_supported(NX_X509_PUBKEY_ALG_EC_P256) != 1 { return 12 }
83 if nx_x509_pubkey_alg_is_supported(NX_X509_PUBKEY_ALG_N) != 0 { return 13 }
84 if nx_x509_pubkey_alg_is_supported(0 - 1) != 0 { return 14 }
85 if nx_x509_pubkey_alg_is_supported(999) != 0 { return 15 }
86
87 return 0
88}