• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h>
16 #include <gmssl/rand.h>
17 #include <gmssl/error.h>
18 #include <gmssl/sm4.h>
19 #include <gmssl/cms.h>
20 
21 
test_cms_content_type(void)22 static int test_cms_content_type(void)
23 {
24 	int tests[] = {
25 		OID_cms_data,
26 		OID_cms_signed_data,
27 		OID_cms_enveloped_data,
28 		OID_cms_signed_and_enveloped_data,
29 		OID_cms_encrypted_data,
30 		OID_cms_key_agreement_info,
31 	};
32 	uint8_t buf[256];
33 	uint8_t *p = buf;
34 	const uint8_t *cp = buf;
35 	size_t len = 0;
36 	int i;
37 
38 	for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
39 		if (cms_content_type_to_der(tests[i], &p, &len) != 1) {
40 			error_print();
41 			return -1;
42 		}
43 		format_bytes(stderr, 0, 4, "", buf, len);
44 	}
45 	for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
46 		int oid;
47 		if (cms_content_type_from_der(&oid, &cp, &len) != 1
48 			|| asn1_check(oid == tests[i]) != 1) {
49 			error_print();
50 			return -1;
51 		}
52 		format_print(stderr, 0, 4, "%s\n", cms_content_type_name(oid));
53 	}
54 	(void)asn1_length_is_zero(len);
55 
56 	printf("%s() ok\n", __FUNCTION__);
57 	return 1;
58 }
59 
test_cms_content_info(void)60 static int test_cms_content_info(void)
61 {
62 	uint8_t buf[256];
63 	uint8_t *p = buf;
64 	const uint8_t *cp = buf;
65 	size_t len = 0;
66 	uint8_t data[20] = { 0x01,0x02 };
67 	int oid;
68 	const uint8_t *d;
69 	size_t dlen;
70 
71 	if (cms_content_info_to_der(OID_cms_data, data, sizeof(data), &p, &len) != 1
72 		|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
73 		|| asn1_length_is_zero(len) != 1) {
74 		error_print();
75 		return -1;
76 	}
77 	cms_content_info_print(stderr, 0, 0, "ContentInfo", d, dlen);
78 
79 	p = buf;
80 	cp = buf;
81 	len = 0;
82 
83 // 当类型为OID_cms_data, 数据是OCTET STRING,需要再解析一次
84 
85 	if (cms_content_info_to_der(OID_cms_data, data, sizeof(data), &p, &len) != 1
86 		|| cms_content_info_from_der(&oid, &d, &dlen, &cp, &len) != 1
87 		|| asn1_check(oid == OID_cms_data) != 1
88 //		|| asn1_check(dlen == sizeof(data)) != 1
89 //		|| asn1_check(memcmp(data, d, dlen) == 0) != 1
90 		|| asn1_length_is_zero(len) != 1) {
91 		error_print();
92 		return -1;
93 	}
94 
95 	printf("%s() ok\n", __FUNCTION__);
96 	return 1;
97 }
98 
test_cms_enced_content_info(void)99 static int test_cms_enced_content_info(void)
100 {
101 	uint8_t buf[256];
102 	uint8_t *p = buf;
103 	const uint8_t *cp = buf;
104 	size_t len = 0;
105 	uint8_t iv[16] = {0};
106 	uint8_t enced[32] = { 0x01,0x02 };
107 	const uint8_t *d;
108 	size_t dlen;
109 
110 	int oid;
111 	int cipher;
112 	const uint8_t *piv;
113 	size_t ivlen;
114 	const uint8_t *shared_info1;
115 	size_t shared_info1_len;
116 	const uint8_t *shared_info2;
117 	size_t shared_info2_len;
118 
119 	if (cms_enced_content_info_to_der(OID_cms_data,
120 			OID_sm4_cbc, iv, sizeof(iv), enced, sizeof(enced),
121 			NULL, 0, NULL, 0, &p, &len) != 1
122 		|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
123 		|| asn1_length_is_zero(len) != 1) {
124 		error_print();
125 		return -1;
126 	}
127 	cms_enced_content_info_print(stderr, 0, 0, "EncryptedContentInfo", d, dlen);
128 
129 	p = buf;
130 	cp = buf;
131 	len = 0;
132 
133 	if (cms_enced_content_info_to_der(OID_cms_data,
134 			OID_sm4_cbc, iv, sizeof(iv), enced, sizeof(enced),
135 			NULL, 0, NULL, 0, &p, &len) != 1
136 		|| cms_enced_content_info_from_der(&oid,
137 			&cipher, &piv, &ivlen, &d, &dlen,
138 			&shared_info1, &shared_info1_len,
139 			&shared_info2, &shared_info2_len, &cp, &len) != 1
140 		|| asn1_check(oid == OID_cms_data) != 1
141 		|| asn1_check(cipher == OID_sm4_cbc) != 1
142 		|| asn1_check(ivlen == sizeof(iv)) != 1
143 		|| asn1_check(dlen == sizeof(enced)) != 1
144 		|| asn1_check(shared_info1 == NULL) != 1
145 		|| asn1_check(shared_info1_len == 0) != 1
146 		|| asn1_check(shared_info2 == NULL) != 1
147 		|| asn1_check(shared_info2_len == 0) != 1
148 		|| asn1_length_is_zero(len) != 1) {
149 		error_print();
150 		return -1;
151 	}
152 
153 	printf("%s() ok\n", __FUNCTION__);
154 	return 1;
155 }
156 
test_cms_enced_content_info_encrypt(void)157 static int test_cms_enced_content_info_encrypt(void)
158 {
159 	uint8_t buf[256];
160 	uint8_t *p = buf;
161 	const uint8_t *cp = buf;
162 	size_t len = 0;
163 
164 	uint8_t key[16] = {0};
165 	uint8_t iv[16] = {1};
166 	uint8_t data[20] = {2};
167 
168 	const uint8_t *d;
169 	size_t dlen;
170 
171 	int oid;
172 	int cipher;
173 	const uint8_t *piv;
174 	size_t ivlen;
175 	uint8_t data2[256];
176 	const uint8_t *shared_info1;
177 	size_t shared_info1_len;
178 	const uint8_t *shared_info2;
179 	size_t shared_info2_len;
180 
181 	if (cms_enced_content_info_encrypt_to_der(
182 			OID_sm4_cbc,
183 			key, sizeof(key),
184 			iv, sizeof(iv),
185 			OID_cms_data, data, sizeof(data),
186 			NULL, 0,
187 			NULL, 0,
188 			&p, &len) != 1
189 		|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
190 		|| asn1_length_is_zero(len) != 1) {
191 		error_print();
192 		return -1;
193 	}
194 	cms_enced_content_info_print(stderr, 0, 0, "EncryptedContentInfo", d, dlen);
195 
196 	p = buf;
197 	cp = buf;
198 	len = 0;
199 
200 	if (cms_enced_content_info_encrypt_to_der(
201 			OID_sm4_cbc,
202 			key, sizeof(key),
203 			iv, sizeof(iv),
204 			OID_cms_data, data, sizeof(data),
205 			NULL, 0,
206 			NULL, 0,
207 			&p, &len) != 1
208 		// 显然这个解密函数是有问题的,在from_der的时候不知道密文的长度,因此无法知道需要的输出缓冲长度
209 		|| cms_enced_content_info_decrypt_from_der(
210 			&cipher,
211 			key, sizeof(key),
212 			&oid, data2, &dlen,
213 			&shared_info1, &shared_info1_len,
214 			&shared_info2, &shared_info2_len,
215 			&cp, &len) != 1
216 		|| asn1_check(cipher == OID_sm4_cbc) != 1
217 		|| asn1_check(oid = OID_cms_data) != 1
218 		|| asn1_check(dlen == sizeof(data)) != 1
219 		|| asn1_check(memcmp(data, data2, dlen) == 0) != 1
220 		|| asn1_check(shared_info1 == NULL) != 1
221 		|| asn1_check(shared_info2 == NULL) != 1
222 		|| asn1_length_is_zero(len) != 1) {
223 		error_print();
224 		return -1;
225 	}
226 
227 	printf("%s() ok\n", __FUNCTION__);
228 	return 1;
229 }
230 
test_cms_issuer_and_serial_number(void)231 static int test_cms_issuer_and_serial_number(void)
232 {
233 	uint8_t buf[256];
234 	uint8_t *p = buf;
235 	const uint8_t *cp = buf;
236 	size_t len = 0;
237 
238 	uint8_t issuer[256];
239 	size_t issuer_len;
240 	uint8_t serial[20] = {1};
241 
242 	const uint8_t *d;
243 	size_t dlen;
244 	const uint8_t *pissuer;
245 	const uint8_t *pserial;
246 	size_t serial_len;
247 
248 	if (x509_name_set(issuer, &issuer_len, sizeof(issuer),
249 			"CN", "Beijing", "Haidian", "PKU", "CS", "CA") != 1
250 		|| cms_issuer_and_serial_number_to_der(
251 			issuer, issuer_len, serial, sizeof(serial), &p, &len) != 1
252 		|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
253 		|| asn1_length_is_zero(len) != 1) {
254 		error_print();
255 		return -1;
256 	}
257 	cms_issuer_and_serial_number_print(stderr, 0, 0, "IssuerAndSerialNumber", d, dlen);
258 
259 	p = buf;
260 	cp = buf;
261 	len = 0;
262 
263 	if (x509_name_set(issuer, &issuer_len, sizeof(issuer),
264 			"CN", "Beijing", "Haidian", "PKU", "CS", "CA") != 1
265 		|| cms_issuer_and_serial_number_to_der(
266 			issuer, issuer_len, serial, sizeof(serial), &p, &len) != 1
267 		|| cms_issuer_and_serial_number_from_der(
268 			&pissuer, &issuer_len, &pserial, &serial_len, &cp, &len) != 1
269 		|| asn1_check(memcmp(pissuer, issuer, issuer_len) == 0) != 1
270 		|| asn1_check(serial_len == sizeof(serial)) != 1
271 		|| asn1_check(memcmp(serial, pserial, serial_len) == 0) != 1
272 		|| asn1_length_is_zero(len) != 1) {
273 		error_print();
274 		return -1;
275 	}
276 
277 	printf("%s() ok\n", __FUNCTION__);
278 	return 1;
279 }
280 
test_cms_signer_info(void)281 static int test_cms_signer_info(void)
282 {
283 	uint8_t buf[512];
284 	uint8_t *p = buf;
285 	const uint8_t *cp = buf;
286 	size_t len = 0;
287 	const uint8_t *d;
288 	size_t dlen;
289 
290 	uint8_t issuer_buf[256];
291 	size_t issuer_len;
292 	uint8_t serial_buf[20];
293 	uint8_t sig_buf[256];
294 	size_t siglen = sizeof(sig_buf);
295 
296 	int version;
297 	const uint8_t *issuer;
298 	const uint8_t *serial;
299 	size_t serial_len;
300 	int digest_alg;
301 	const uint8_t *auth_attrs;
302 	size_t auth_attrs_len;
303 	int sig_alg;
304 	const uint8_t *sig;
305 	const uint8_t *unauth_attrs;
306 	size_t unauth_attrs_len;
307 
308 
309 	if (x509_name_set(issuer_buf, &issuer_len, sizeof(issuer_buf),
310 		"CN", "Beijing", "Haidian", "PKU", "CS", "CA") != 1) {
311 		error_print();
312 		return -1;
313 	}
314 
315 	if (cms_signer_info_to_der(
316 			CMS_version_v1,
317 			issuer_buf, issuer_len,
318 			serial_buf, sizeof(serial_buf),
319 			OID_sm3,
320 			NULL, 0,
321 			OID_sm2sign_with_sm3,
322 			sig_buf, siglen,
323 			NULL, 0,
324 			&p, &len) != 1
325 		|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
326 		|| asn1_length_is_zero(len) != 1) {
327 		error_print();
328 		return -1;
329 	}
330 	cms_signer_info_print(stderr, 0, 0, "SignerInfo", d, dlen);
331 
332 	cp = p = buf; len = 0;
333 	if (cms_signer_info_to_der(
334 			CMS_version_v1,
335 			issuer_buf, issuer_len,
336 			serial_buf, sizeof(serial_buf),
337 			OID_sm3,
338 			NULL, 0,
339 			OID_sm2sign_with_sm3,
340 			sig_buf, siglen,
341 			NULL, 0,
342 			&p, &len) != 1
343 		|| cms_signer_info_from_der(
344 			&version,
345 			&issuer, &issuer_len,
346 			&serial, &serial_len,
347 			&digest_alg,
348 			&auth_attrs, &auth_attrs_len,
349 			&sig_alg,
350 			&sig, &siglen,
351 			&unauth_attrs, &unauth_attrs_len,
352 			&cp, &len) != 1
353 		|| asn1_length_is_zero(len) != 1) {
354 		error_print();
355 		return -1;
356 	}
357 
358 	printf("%s() ok\n", __FUNCTION__);
359 	return 1;
360 }
361 
test_cms_signer_info_sign(void)362 static int test_cms_signer_info_sign(void)
363 {
364 	uint8_t buf[1024];
365 	uint8_t *p = buf;
366 	const uint8_t *cp = buf;
367 	size_t len = 0;
368 	const uint8_t *d;
369 	size_t dlen;
370 
371 	SM2_KEY sm2_key;
372 	uint8_t serial_buf[20];
373 	uint8_t name[256];
374 	size_t namelen;
375 	time_t not_before, not_after;
376 	uint8_t certs[1024];
377 	size_t certslen;
378 
379 	SM3_CTX sm3_ctx;
380 
381 	const uint8_t *cert;
382 	size_t certlen;
383 	const uint8_t *serial;
384 	const uint8_t *issuer;
385 	const uint8_t *auth_attrs;
386 	const uint8_t *unauth_attrs;
387 	size_t serial_len, issuer_len, auth_attrs_len, unauth_attrs_len;
388 
389 	if (sm2_key_generate(&sm2_key) != 1
390 		|| rand_bytes(serial_buf, sizeof(serial_buf)) != 1
391 		|| x509_name_set(name, &namelen, sizeof(name), "CN", "Beijing", "Haidian", "PKU", "CS", "Alice") != 1
392 		|| time(&not_before) == -1
393 		|| x509_validity_add_days(&not_after, not_before, 365) != 1
394 		|| x509_cert_sign(certs, &certslen, sizeof(certs),
395 			X509_version_v3, serial_buf, sizeof(serial_buf),
396 			OID_sm2sign_with_sm3,
397 			name, namelen,
398 			not_before, not_after,
399 			name, namelen,
400 			&sm2_key, NULL, 0, NULL, 0, NULL, 0,
401 			&sm2_key, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1) {
402 		error_print();
403 		return -1;
404 	}
405 
406 	sm3_init(&sm3_ctx);
407 	sm3_update(&sm3_ctx, (uint8_t *)"hello", 5);
408 
409 	cp = p = buf; len = 0;
410 	if (cms_signer_info_sign_to_der(
411 			&sm3_ctx, &sm2_key,
412 			name, namelen, serial_buf, sizeof(serial_buf),
413 			NULL, 0, NULL, 0,
414 			&p, &len) != 1
415 		|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
416 		|| asn1_length_is_zero(len) != 1) {
417 		error_print();
418 		return -1;
419 	}
420 	cms_signer_info_print(stderr, 0, 0, "SignerInfo", d, dlen);
421 
422 	cp = p = buf; len = 0;
423 	if (cms_signer_info_sign_to_der(
424 			&sm3_ctx, &sm2_key,
425 			name, namelen, serial_buf, sizeof(serial_buf),
426 			NULL, 0, NULL, 0,
427 			&p, &len) != 1
428 		|| cms_signer_info_verify_from_der(
429 			&sm3_ctx, certs, certslen,
430 			&cert, &certlen,
431 			&issuer, &issuer_len,
432 			&serial, &serial_len,
433 			&auth_attrs, &auth_attrs_len,
434 			&unauth_attrs, &unauth_attrs_len,
435 			&cp, &len) != 1
436 		|| asn1_length_is_zero(len) != 1) {
437 		error_print();
438 		return -1;
439 	}
440 
441 	printf("%s() ok\n", __FUNCTION__);
442 	return 1;
443 }
444 
test_cms_signer_infos(void)445 static int test_cms_signer_infos(void)
446 {
447 	uint8_t buf[1280];
448 	uint8_t *p = buf;
449 	const uint8_t *cp = buf;
450 	size_t len = 0;
451 	const uint8_t *d;
452 	size_t dlen;
453 
454 	uint8_t signer_infos[1024];
455 	size_t signer_infos_len = 0;
456 
457 	SM3_CTX sm3_ctx;
458 	SM2_KEY sm2_key;
459 
460 	uint8_t issuer_buf[256];
461 	size_t issuer_len;
462 	uint8_t serial_buf[20];
463 
464 	sm2_key_generate(&sm2_key);
465 	sm3_init(&sm3_ctx);
466 	sm3_update(&sm3_ctx, (uint8_t *)"hello", 5);
467 	x509_name_set(issuer_buf, &issuer_len, sizeof(issuer_buf), "CN", "Beijing", "Haidian", "PKU", "CS", "CA");
468 
469 
470 	if (cms_signer_infos_add_signer_info(
471 			signer_infos, &signer_infos_len, sizeof(signer_infos),
472 			&sm3_ctx, &sm2_key,
473 			issuer_buf, issuer_len,
474 			serial_buf, sizeof(serial_buf),
475 			NULL, 0,
476 			NULL, 0) != 1
477 		|| cms_signer_infos_add_signer_info(
478 			signer_infos, &signer_infos_len, sizeof(signer_infos),
479 			&sm3_ctx, &sm2_key,
480 			issuer_buf, issuer_len,
481 			serial_buf, sizeof(serial_buf),
482 			NULL, 0,
483 			NULL, 0) != 1
484 		|| cms_signer_infos_add_signer_info(
485 			signer_infos, &signer_infos_len, sizeof(signer_infos),
486 			&sm3_ctx, &sm2_key,
487 			issuer_buf, issuer_len,
488 			serial_buf, sizeof(serial_buf),
489 			NULL, 0,
490 			NULL, 0) != 1
491 		|| cms_signer_infos_to_der(signer_infos, signer_infos_len, &p, &len) != 1
492 		|| cms_signer_infos_from_der(&d, &dlen, &cp, &len) != 1
493 		|| asn1_length_is_zero(len) != 1){
494 		error_print();
495 		return -1;
496 	}
497 	cms_signer_infos_print(stderr, 0, 0, "SET OF SignerInfo", d, dlen);
498 
499 
500 	printf("%s() ok\n", __FUNCTION__);
501 	return 1;
502 }
503 
test_cms_digest_algors(void)504 static int test_cms_digest_algors(void)
505 {
506 	uint8_t buf[512];
507 	uint8_t *p = buf;
508 	const uint8_t *cp = buf;
509 	size_t len = 0;
510 	const uint8_t *d;
511 	size_t dlen;
512 
513 	int oids[] = {
514 		OID_sm3,
515 		OID_md5,
516 		OID_sha1,
517 		OID_sha256,
518 		OID_sha512,
519 	};
520 
521 	int algs[16];
522 	size_t algs_cnt;
523 
524 	if (cms_digest_algors_to_der(oids, sizeof(oids)/sizeof(oids[0]), &p, &len) != 1
525 		|| asn1_set_from_der(&d, &dlen, &cp, &len) != 1
526 		|| asn1_length_is_zero(len) != 1) {
527 		error_print();
528 		return -1;
529 	}
530 	cms_digest_algors_print(stderr, 0, 0, "digestAlgorithms", d, dlen);
531 
532 	if (cms_digest_algors_to_der(oids, sizeof(oids)/sizeof(oids[0]), &p, &len) != 1
533 		|| cms_digest_algors_from_der(algs, &algs_cnt, sizeof(algs)/sizeof(algs[0]), &cp, &len) != 1
534 		|| asn1_check(algs_cnt == sizeof(oids)/sizeof(oids[0])) != 1
535 		|| asn1_check(memcmp(algs, oids, sizeof(oids)) == 0) != 1
536 		|| asn1_length_is_zero(len) != 1) {
537 		error_print();
538 		return -1;
539 	}
540 
541 	printf("%s() ok\n", __FUNCTION__);
542 	return 1;
543 }
544 
test_cms_signed_data(void)545 static int test_cms_signed_data(void)
546 {
547 	SM2_KEY sm2_key;
548 	uint8_t cert[4096];
549 	size_t certlen = 0;
550 	CMS_CERTS_AND_KEY signers[1];
551 	uint8_t data[48] = {0};
552 	uint8_t buf[4096];
553 	uint8_t *p = buf;
554 	const uint8_t *cp = buf;
555 	size_t len = 0;
556 	const uint8_t *d;
557 	size_t dlen;
558 
559 	sm2_key_generate(&sm2_key);
560 
561 	{
562 		uint8_t serial[20];
563 		size_t serial_len = sizeof(serial);
564 		uint8_t name[256];
565 		size_t namelen = 0;
566 		time_t not_before, not_after;
567 		uint8_t subject[256];
568 		size_t subject_len = 0;
569 		uint8_t *p = cert;
570 		const uint8_t *cp = cert;
571 
572 		rand_bytes(serial, sizeof(serial));
573 		x509_name_set(name, &namelen, sizeof(name), "CN", "Beijing", "Haidian", "PKU", "CS", "CA");
574 		time(&not_before);
575 		x509_validity_add_days(&not_after, not_before, 365);
576 
577 		if (x509_cert_sign(
578 			cert, &certlen, sizeof(cert),
579 			X509_version_v3,
580 			serial, sizeof(serial),
581 			OID_sm2sign_with_sm3,
582 			name, namelen,
583 			not_before, not_after,
584 			name, namelen,
585 			&sm2_key,
586 			NULL, 0,
587 			NULL, 0,
588 			NULL, 0,
589 			&sm2_key, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1) {
590 			error_print();
591 			return -1;
592 		}
593 	}
594 
595 	signers[0].certs = cert;
596 	signers[0].certs_len = certlen;
597 	signers[0].sign_key = &sm2_key;
598 
599 	if (cms_signed_data_sign_to_der(
600 			signers, sizeof(signers)/sizeof(signers[0]),
601 			OID_cms_data, data, sizeof(data),
602 			NULL, 0,
603 			&p, &len) != 1
604 		|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
605 		|| asn1_length_is_zero(len) != 1) {
606 		error_print();
607 		return -1;
608 	}
609 	cms_signed_data_print(stderr, 0, 0, "SignedData", d, dlen);
610 
611 	cp = p = buf; len = 0;
612 	{
613 		int content_type;
614 		const uint8_t *content;
615 		size_t content_len;
616 		const uint8_t *certs;
617 		size_t certslen;
618 		const uint8_t *crls;
619 		size_t crlslen;
620 		const uint8_t *signer_infos;
621 		size_t signer_infos_len;
622 
623 		if (cms_signed_data_sign_to_der(
624 				signers, sizeof(signers)/sizeof(signers[0]),
625 				OID_cms_data, data, sizeof(data),
626 				NULL, 0,
627 				&p, &len) != 1
628 			|| cms_signed_data_verify_from_der(
629 				NULL, 0,
630 				NULL, 0,
631 				&content_type, &content, &content_len,
632 				&certs, &certslen,
633 				&crls, &crlslen,
634 				&signer_infos, &signer_infos_len,
635 				&cp, &len) != 1
636 			|| asn1_length_is_zero(len) != 1) {
637 			error_print();
638 			return -1;
639 		}
640 	}
641 
642 	printf("%s() ok\n", __FUNCTION__);
643 	return 1;
644 }
645 
test_cms_recipient_info(void)646 static int test_cms_recipient_info(void)
647 {
648 	SM2_KEY sm2_key;
649 	uint8_t name[256];
650 	size_t namelen;
651 	uint8_t serial_buf[20];
652 	uint8_t in[16];
653 
654 	uint8_t buf[1024];
655 	uint8_t *p = buf;
656 	const uint8_t *cp = buf;
657 	size_t len = 0;
658 	const uint8_t *d;
659 	size_t dlen;
660 
661 	int version;
662 	const uint8_t *issuer;
663 	size_t issuer_len;
664 	const uint8_t *serial;
665 	size_t serial_len;
666 	int pke_algor;
667 	const uint8_t *params;
668 	size_t params_len;
669 	const uint8_t *enced_key;
670 	size_t enced_key_len;
671 
672 	uint8_t out[sizeof(in)];
673 	size_t outlen;
674 
675 	sm2_key_generate(&sm2_key);
676 	x509_name_set(name, &namelen, sizeof(name), "US", "CA", NULL, "BB", "AA", "CC");
677 	rand_bytes(serial_buf, sizeof(serial_buf));
678 	rand_bytes(in, sizeof(in));
679 
680 	if (cms_recipient_info_encrypt_to_der(&sm2_key,
681 			name, namelen,
682 			serial_buf, sizeof(serial_buf),
683 			in, sizeof(in),
684 			&p, &len) != 1
685 		|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
686 		|| asn1_length_is_zero(len) != 1) {
687 		error_print();
688 		return -1;
689 	}
690 	cms_recipient_info_print(stderr, 0, 0, "RecipientInfo", d, dlen);
691 
692 
693 	cp = p = buf; len = 0;
694 	if (cms_recipient_info_encrypt_to_der(&sm2_key,
695 			name, namelen,
696 			serial_buf, sizeof(serial_buf),
697 			in, sizeof(in),
698 			&p, &len) != 1
699 		|| cms_recipient_info_from_der(
700 			&version,
701 			&issuer, &issuer_len,
702 			&serial, &serial_len,
703 			&pke_algor, &params, &params_len,
704 			&enced_key, &enced_key_len,
705 			&cp, &len) != 1
706 		|| asn1_length_is_zero(len) != 1) {
707 		error_print();
708 		return -1;
709 	}
710 
711 
712 	cp = p = buf; len = 0;
713 	if (cms_recipient_info_encrypt_to_der(
714 			&sm2_key,
715 			name, namelen,
716 			serial_buf, sizeof(serial_buf),
717 			in, sizeof(in),
718 			&p, &len) != 1
719 		|| cms_recipient_info_decrypt_from_der(
720 			&sm2_key,
721 			name, namelen,
722 			serial_buf, sizeof(serial_buf),
723 			out, &outlen, sizeof(out),
724 			&cp, &len) != 1
725 		|| asn1_length_is_zero(len) != 1) {
726 		error_print();
727 		return -1;
728 	}
729 	if (sizeof(in) != outlen
730 		|| memcmp(in, out, outlen) != 0) {
731 		error_print();
732 		return -1;
733 	}
734 
735 	printf("%s() ok\n", __FUNCTION__);
736 	return 1;
737 }
738 
test_cms_enveloped_data(void)739 int test_cms_enveloped_data(void)
740 {
741 	SM2_KEY sm2_key1;
742 	uint8_t name1[256];
743 	size_t name1_len;
744 	uint8_t serial1[20];
745 	size_t serial1_len;
746 
747 	SM2_KEY sm2_key2;
748 	uint8_t name2[256];
749 	size_t name2_len;
750 	uint8_t serial2[20];
751 	size_t serial2_len;
752 
753 	time_t not_before, not_after;
754 
755 	uint8_t certs[2048];
756 	size_t certslen;
757 
758 	uint8_t key[16];
759 	uint8_t iv[16];
760 
761 	uint8_t in[80];
762 	uint8_t out[256];
763 	size_t outlen;
764 	size_t maxlen;
765 
766 	uint8_t buf[4096];
767 	uint8_t *p;
768 	const uint8_t *cp;
769 	size_t len;
770 	const uint8_t *d;
771 	size_t dlen;
772 
773 	// prepare keys and certs
774 
775 	if (time(&not_before) == -1
776 		|| x509_validity_add_days(&not_after, not_before, 365) != 1) {
777 		error_print();
778 		return -1;
779 	}
780 
781 	p = certs;
782 	certslen = 0;
783 	maxlen = sizeof(certs);
784 
785 	if (sm2_key_generate(&sm2_key1) != 1
786 		|| rand_bytes(serial1, sizeof(serial1)) != 1
787 		|| x509_name_set(name1, &name1_len, sizeof(name1), "CN", "Beijing", "Haidian", "PKU", "CS", "Alice") != 1
788 		|| x509_cert_sign(
789 			p, &len, maxlen,
790 			X509_version_v3,
791 			serial1, sizeof(serial1),
792 			OID_sm2sign_with_sm3,
793 			name1, name1_len,
794 			not_before, not_after,
795 			name1, name1_len,
796 			&sm2_key1, NULL, 0, NULL, 0, NULL, 0,
797 			&sm2_key1, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1) {
798 		error_print();
799 		return -1;
800 	}
801 	p += len;
802 	certslen += len;
803 	maxlen -= len;
804 
805 	if (sm2_key_generate(&sm2_key2) != 1
806 		|| rand_bytes(serial2, sizeof(serial2)) != 1
807 		|| x509_name_set(name2, &name2_len, sizeof(name2), "CN", "Beijing", "Haidian", "PKU", "CS", "Bob") != 1
808 		|| x509_cert_sign(
809 			p, &len, maxlen,
810 			X509_version_v3,
811 			serial2, sizeof(serial2),
812 			OID_sm2sign_with_sm3,
813 			name2, name2_len,
814 			not_before, not_after,
815 			name2, name2_len,
816 			&sm2_key2, NULL, 0, NULL, 0, NULL, 0,
817 			&sm2_key2, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1) {
818 		error_print();
819 		return -1;
820 	}
821 	p += len;
822 	certslen += len;
823 	maxlen -= len;
824 
825 	rand_bytes(key, sizeof(key));
826 	rand_bytes(iv, sizeof(iv));
827 	rand_bytes(in, sizeof(in));
828 
829 	// test
830 
831 	cp = p = buf; len = 0;
832 	if (cms_enveloped_data_encrypt_to_der(
833 			certs, certslen,
834 			OID_sm4_cbc, key, sizeof(key), iv, sizeof(iv),
835 			OID_cms_data, in, sizeof(in),
836 			NULL, 0, NULL, 0,
837 			&p, &len) != 1
838 		|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
839 		|| asn1_length_is_zero(len) != 1) {
840 		error_print();
841 		return -1;
842 	}
843 	cms_enveloped_data_print(stderr, 0, 0, "EnvelopedData", d, dlen);
844 
845 
846 	int content_type;
847 
848 
849 	cp = p = buf; len = 0;
850 	if (cms_enveloped_data_encrypt_to_der(
851 			certs, certslen,
852 			OID_sm4_cbc, key, sizeof(key), iv, sizeof(iv),
853 			OID_cms_data, in, sizeof(in),
854 			NULL, 0, NULL, 0,
855 			&p, &len) != 1) {
856 		error_print();
857 		return -1;
858 	}
859 
860 	const uint8_t *rcpt_infos;
861 	const uint8_t *shared_info1;
862 	const uint8_t *shared_info2;
863 	size_t rcpt_infos_len, shared_info1_len, shared_info2_len;
864 
865 	if (cms_enveloped_data_decrypt_from_der(
866 			&sm2_key1,
867 			name1, name1_len,
868 			serial1, sizeof(serial1),
869 			&content_type, out, &outlen,
870 			&rcpt_infos, &rcpt_infos_len,
871 			&shared_info1, &shared_info1_len,
872 			&shared_info2, &shared_info2_len,
873 			&cp, &len) != 1) {
874 		error_print();
875 		return -1;
876 	}
877 
878 	printf("%s() ok\n", __FUNCTION__);
879 	return 1;
880 }
881 
test_cms_signed_and_enveloped_data(void)882 static int test_cms_signed_and_enveloped_data(void)
883 {
884 /*
885 444 int cms_signed_and_enveloped_data_encipher_to_der(
886 445         const CMS_CERTS_AND_KEY *signers, size_t signers_cnt,
887 446         const uint8_t *rcpt_certs, size_t rcpt_certs_len,
888 447         int enc_algor, const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
889 448         int content_type, const uint8_t *content, size_t content_len,
890 449         const uint8_t *signers_crls, size_t signers_crls_len,
891 450         const uint8_t *shared_info1, size_t shared_info1_len,
892 451         const uint8_t *shared_info2, size_t shared_info2_len,
893 452         uint8_t **out, size_t *outlen);
894 453 int cms_signed_and_enveloped_data_decipher_from_der(
895 454         const SM2_KEY *rcpt_key,
896 455         const uint8_t *rcpt_issuer, size_t rcpt_issuer_len,
897 456         const uint8_t *rcpt_serial, size_t rcpt_serial_len,
898 457         int *content_type, uint8_t *content, size_t *content_len,
899 458         const uint8_t **prcpt_infos, size_t *prcpt_infos_len,
900 459         const uint8_t **shared_info1, size_t *shared_info1_len,
901 460         const uint8_t **shared_info2, size_t *shared_info2_len,
902 461         const uint8_t **certs, size_t *certs_len,
903 462         const uint8_t **crls, size_t *crls_len,
904 463         const uint8_t **psigner_infos, size_t *psigner_infos_len,
905 464         const uint8_t *extra_certs, size_t extra_certs_len,
906 465         const uint8_t *extra_crls, size_t extra_crls_len,
907 466         const uint8_t **in, size_t *inlen);
908 */
909 	SM2_KEY sign_key;
910 	SM2_KEY decr_key;
911 
912 
913 
914 	uint8_t sign_serial[20];
915 	uint8_t sign_name[256];
916 	size_t sign_name_len;
917 
918 
919 
920 
921 
922 
923 
924 
925 
926 	printf("%s() ok\n", __FUNCTION__);
927 	return 1;
928 }
929 
test_cms_key_agreement_info(void)930 static int test_cms_key_agreement_info(void)
931 {
932 	SM2_KEY sm2_key;
933 	uint8_t name[256];
934 	size_t namelen;
935 	uint8_t serial[20];
936 	time_t not_before, not_after;
937 	uint8_t cert[2048];
938 	size_t certlen;
939 
940 	uint8_t buf[4096];
941 	uint8_t *p;
942 	const uint8_t *cp;
943 	size_t len;
944 	const uint8_t *d;
945 	size_t dlen;
946 
947 	int version;
948 	SM2_KEY public_key;
949 	const uint8_t *pcert;
950 	size_t pcertlen;
951 	const uint8_t *id;
952 	size_t idlen;
953 
954 	if (sm2_key_generate(&sm2_key) != 1
955 		|| rand_bytes(serial, sizeof(serial)) != 1
956 		|| x509_name_set(name, &namelen, sizeof(name), "CN", "Beijing", "Haidian", "PKU", "CS", "Alice") != 1
957 		|| time(&not_before) == - 1
958 		|| x509_validity_add_days(&not_after, not_before, 365) != 1
959 		|| x509_cert_sign(
960 			cert, &certlen, sizeof(cert),
961 			X509_version_v3,
962 			serial, sizeof(serial),
963 			OID_sm2sign_with_sm3,
964 			name, namelen,
965 			not_before, not_after,
966 			name, namelen,
967 			&sm2_key, NULL, 0, NULL, 0, NULL, 0,
968 			&sm2_key, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1) {
969 		error_print();
970 		return -1;
971 	}
972 
973 	cp = p = buf; len = 0;
974 	if (cms_key_agreement_info_to_der(
975 			CMS_version_v1,
976 			&sm2_key,
977 			cert, certlen,
978 			(uint8_t *)SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH,
979 			&p, &len) != 1
980 		|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
981 		|| asn1_length_is_zero(len) != 1) {
982 		error_print();
983 		return -1;
984 	}
985 	cms_key_agreement_info_print(stderr, 0, 0, "KeyAgreementInfo", d, dlen);
986 
987 
988 	cp = p = buf; len = 0;
989 	if (cms_key_agreement_info_to_der(
990 			CMS_version_v1,
991 			&sm2_key,
992 			cert, certlen,
993 			(uint8_t *)SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH,
994 			&p, &len) != 1
995 		|| cms_key_agreement_info_from_der(
996 			&version,
997 			&public_key,
998 			&pcert, &pcertlen,
999 			&id, &idlen,
1000 			&cp, &len) != 1
1001 		|| asn1_check(version == CMS_version_v1) != 1
1002 		|| asn1_length_is_zero(len) != 1) {
1003 		error_print();
1004 		return -1;
1005 	}
1006 	if (sm2_public_key_equ(&sm2_key, &public_key) != 1) {
1007 		error_print();
1008 		return -1;
1009 	}
1010 	if (pcertlen != certlen
1011 		|| memcmp(pcert, cert, certlen) != 0
1012 		|| idlen != SM2_DEFAULT_ID_LENGTH
1013 		|| memcmp(SM2_DEFAULT_ID, id, idlen) != 0) {
1014 		error_print();
1015 		return -1;
1016 	}
1017 
1018 	printf("%s() ok\n", __FUNCTION__);
1019 	return 1;
1020 }
1021 
main(int argc,char ** argv)1022 int main(int argc, char **argv)
1023 {
1024 	if (test_cms_content_type() != 1) goto err;
1025 	if (test_cms_content_info() != 1) goto err;
1026 	if (test_cms_enced_content_info() != 1) goto err;
1027 	if (test_cms_enced_content_info_encrypt() != 1) goto err;
1028 	if (test_cms_issuer_and_serial_number() != 1) goto err;
1029 	if (test_cms_signer_info() != 1) goto err;
1030 	if (test_cms_signer_info_sign() != 1) goto err;
1031 	if (test_cms_signer_infos() != 1) goto err;
1032 	if (test_cms_digest_algors() != 1) goto err;
1033 	if (test_cms_signed_data() != 1) goto err;
1034 	if (test_cms_recipient_info() != 1) goto err;
1035 	if (test_cms_enveloped_data() != 1) goto err;
1036 	if (test_cms_key_agreement_info() != 1) goto err;
1037 
1038 	printf("%s all tests passed\n", __FILE__);
1039 	return 0;
1040 err:
1041 	error_print();
1042 	return -1;
1043 }
1044