1 /*
2 * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the License); you may
5 * not use this file except in compliance with the License.
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 */
9
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <gmssl/oid.h>
15 #include <gmssl/x509_alg.h>
16 #include <gmssl/x509.h>
17 #include <gmssl/rand.h>
18 #include <gmssl/error.h>
19
20
test_x509_digest_algor(void)21 static int test_x509_digest_algor(void)
22 {
23 char *names[] = {
24 "sm3",
25 "md5",
26 "sha1",
27 "sha224",
28 "sha256",
29 "sha384",
30 "sha512",
31 };
32 uint8_t buf[256];
33 uint8_t *p = buf;
34 const uint8_t *cp = buf;
35 size_t len = 0;
36 int oid;
37 int i;
38
39 format_print(stderr, 0, 0, "DER\n");
40 for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
41 oid = x509_digest_algor_from_name(names[i]);
42 if (x509_digest_algor_to_der(oid, &p, &len) != 1) {
43 error_print();
44 return -1;
45 }
46 format_bytes(stderr, 0, 4, "", buf, len);
47 }
48
49 format_print(stderr, 0, 0, "OID\n");
50 for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
51 if (x509_digest_algor_from_der(&oid, &cp, &len) != 1) {
52 error_print();
53 return -1;
54 }
55 if (oid != x509_digest_algor_from_name(names[i])) {
56 error_print();
57 return 1;
58 }
59 format_print(stderr, 0, 4, "%s\n", x509_digest_algor_name(oid));
60 }
61 printf("%s() ok\n", __FUNCTION__);
62 return 1;
63 }
64
test_x509_encryption_algor(void)65 static int test_x509_encryption_algor(void)
66 {
67 char *names[] = {
68 "sm4-cbc",
69 "aes128-cbc",
70 "aes192-cbc",
71 "aes256-cbc",
72 };
73 uint8_t iv[16] = {0};
74 uint8_t buf[256];
75 uint8_t *p = buf;
76 const uint8_t *cp = buf;
77 size_t len = 0;
78 int oid;
79 const uint8_t *params;
80 size_t paramslen;
81 int i;
82
83 format_print(stderr, 0, 0, "DER\n");
84 for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
85 oid = x509_encryption_algor_from_name(names[i]);
86 if (x509_encryption_algor_to_der(oid, iv, sizeof(iv), &p, &len) != 1) {
87 error_print();
88 return -1;
89 }
90 format_bytes(stderr, 0, 4, "", buf, len);
91 }
92 format_print(stderr, 0, 0, "OID\n");
93 for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
94 if (x509_encryption_algor_from_der(&oid, ¶ms, ¶mslen, &cp, &len) != 1
95 || asn1_check(params != NULL) != 1
96 || asn1_check(paramslen == sizeof(iv)) != 1) {
97 error_print();
98 return -1;
99 }
100 format_print(stderr, 0, 4, "%s\n", x509_encryption_algor_name(oid));
101 }
102 printf("%s() ok\n", __FUNCTION__);
103 return 1;
104 }
105
test_x509_signature_algor(void)106 static int test_x509_signature_algor(void)
107 {
108 char *names[] = {
109 "sm2sign-with-sm3",
110 "rsasign-with-sm3",
111 "ecdsa-with-sha1",
112 "ecdsa-with-sha224",
113 "ecdsa-with-sha256",
114 "ecdsa-with-sha384",
115 "ecdsa-with-sha512",
116 "sha1WithRSAEncryption",
117 "sha224WithRSAEncryption",
118 "sha256WithRSAEncryption",
119 "sha384WithRSAEncryption",
120 "sha512WithRSAEncryption",
121 };
122 uint8_t buf[256];
123 uint8_t *p = buf;
124 const uint8_t *cp = buf;
125 size_t len = 0;
126 int oid;
127 int i;
128
129 format_print(stderr, 0, 0, "DER\n");
130 for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
131 oid = x509_signature_algor_from_name(names[i]);
132 if (x509_signature_algor_to_der(oid, &p, &len) != 1) {
133 error_print();
134 return -1;
135 }
136 format_bytes(stderr, 0, 4, "", buf, len);
137 }
138 format_print(stderr, 0, 0, "OID\n");
139 for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
140 if (x509_signature_algor_from_der(&oid, &cp, &len) != 1) {
141 error_print();
142 return -1;
143 }
144 format_print(stderr, 0, 4, "%s\n", x509_signature_algor_name(oid));
145 }
146 printf("%s() ok\n", __FUNCTION__);
147 return 1;
148 }
149
test_x509_public_key_encryption_algor(void)150 static int test_x509_public_key_encryption_algor(void)
151 {
152 char *names[] = {
153 "sm2encrypt",
154 // "rsaesOAEP",
155 // "rsaEncryption",
156 };
157 uint8_t buf[256];
158 uint8_t *p = buf;
159 const uint8_t *cp = buf;
160 size_t len = 0;
161 int oid;
162 const uint8_t *params;
163 size_t paramslen;
164 int i;
165
166 format_print(stderr, 0, 0, "DER\n");
167 for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
168 oid = x509_public_key_encryption_algor_from_name(names[i]);
169 if (x509_public_key_encryption_algor_to_der(oid, &p, &len) != 1) {
170 error_print();
171 return -1;
172 }
173 format_bytes(stderr, 0, 4, "", buf, len);
174 }
175 format_print(stderr, 0, 0, "OID\n");
176 for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
177 if (x509_public_key_encryption_algor_from_der(&oid, ¶ms, ¶mslen, &cp, &len) != 1) {
178 error_print();
179 return -1;
180 }
181 format_print(stderr, 0, 4, "%s\n", x509_public_key_encryption_algor_name(oid));
182 }
183 printf("%s() ok\n", __FUNCTION__);
184 return 1;
185 }
186
main(void)187 int main(void)
188 {
189 if (test_x509_digest_algor() != 1) goto err;
190 if (test_x509_encryption_algor() != 1) goto err;
191 if (test_x509_signature_algor() != 1) goto err;
192 if (test_x509_public_key_encryption_algor() != 1) goto err;
193 printf("%s all tests passed!\n", __FILE__);
194 return 0;
195 err:
196 error_print();
197 return 1;
198 }
199