reqsign.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. #include <stdio.h>
  10. #include <errno.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <gmssl/hex.h>
  14. #include <gmssl/mem.h>
  15. #include <gmssl/rand.h>
  16. #include <gmssl/x509.h>
  17. #include <gmssl/x509_ext.h>
  18. #include <gmssl/x509_req.h>
  19. static const char *options =
  20. " [-in pem]"
  21. " [-req_sm2_id str | -req_sm2_id_hex hex]"
  22. " [-serial_len num]"
  23. " -days num"
  24. " -cacert pem -key file -pass pass"
  25. " [-sm2_id str | -sm2_id_hex hex]"
  26. " [-gen_authority_key_id]"
  27. " [-gen_subject_key_id]"
  28. " [-key_usage str]*"
  29. " [-subject_dns_name str]*"
  30. " [-issuer_dns_name str]*"
  31. " [-ca -path_len_constraint num]"
  32. " [-ext_key_usage str]*"
  33. " [-crl_http_uri uri] [-crl_ldap_uri uri]"
  34. " [-inhibit_any_policy num]"
  35. " [-ca_issuers_uri uri] [-ocsp_uri uri uri]"
  36. " [-out pem]";
  37. static char *usage =
  38. "Options\n"
  39. "\n"
  40. " -in pem | stdin Input CSR file in PEM format\n"
  41. " -req_sm2_id str CSR Owner's ID in SM2 signature algorithm\n"
  42. " -req_sm2_id_hex hex CSR Owner's ID in hex format\n"
  43. " When `-req_sm2_id` or `-req_sm2_id_hex` is specified,\n"
  44. " must use the same ID in other commands explicitly.\n"
  45. " If neither `-req_sm2_id` nor `-req_sm2_id_hex` is specified,\n"
  46. " the default string '1234567812345678' is used\n"
  47. " -serial_len num Serial number length in bytes\n"
  48. " -days num Validity peroid in days\n"
  49. " -cacert pem Issuer CA certificate\n"
  50. " -key pem Issuer private key file in PEM format\n"
  51. " -sm2_id str Authority's ID in SM2 signature algorithm\n"
  52. " -sm2_id_hex hex Authority's ID in hex format\n"
  53. " When `-sm2_id` or `-sm2_id_hex` is specified,\n"
  54. " must use the same ID in other commands explicitly.\n"
  55. " If neither `-sm2_id` nor `-sm2_id_hex` is specified,\n"
  56. " the default string '1234567812345678' is used\n"
  57. " -pass pass Password for decrypting private key file\n"
  58. " -out pem Output certificate file in PEM format\n"
  59. "\n"
  60. " Extension options\n"
  61. "\n"
  62. " -gen_authority_key_id Generate AuthorityKeyIdentifier extension use SM3\n"
  63. " -gen_subject_key_id Generate SubjectKeyIdentifier extension use SM3\n"
  64. " -key_usage str Add KeyUsage extension\n"
  65. " this option can be called multi-times\n"
  66. " avaiable values:\n"
  67. " digitalSignature\n"
  68. " nonRepudiation\n"
  69. " keyEncipherment\n"
  70. " dataEncipherment\n"
  71. " keyAgreement\n"
  72. " keyCertSign\n"
  73. " cRLSign\n"
  74. " encipherOnly\n"
  75. " decipherOnly\n"
  76. " -subject_dns_name str Add DNS name to SubjectAltName extension\n"
  77. " this option can be called multi-times\n"
  78. " -issuer_dns_name str Add DNS name to IssuerAltName extension\n"
  79. " this option can be called multi-times\n"
  80. " -ca Set cA of BasicConstaints extension\n"
  81. " -path_len_constraint num Set pathLenConstaint of BasicConstaints extension\n"
  82. " -ext_key_usage str Set ExtKeyUsage extension\n"
  83. " this option can be called multi-times\n"
  84. " avaiable values:\n"
  85. " anyExtendedKeyUsage\n"
  86. " serverAuth\n"
  87. " clientAuth\n"
  88. " codeSigning\n"
  89. " emailProtection\n"
  90. " timeStamping\n"
  91. " OCSPSigning\n"
  92. " -crl_http_uri uri Set HTTP URI of CRL of CRLDistributionPoints extension\n"
  93. " -crl_ldap_uri uri Set LDAP URI of CRL of CRLDistributionPoints extension\n"
  94. " -inhibit_any_policy num Set skipCerts number of InhibitAnyPolicy extension\n"
  95. " -ca_issuers_uri uri Set URI of the CA certificate in DER-encoding o FreshestCRL extension\n"
  96. " -ocsp_uri uri Set OCSP URI of FreshestCRL extension\n"
  97. "\n"
  98. "Examples\n"
  99. "\n"
  100. " # Generate self-signed root CA certificate\n"
  101. "\n"
  102. " gmssl sm2keygen -pass P@ssw0rd -out rootcakey.pem\n"
  103. " gmssl certgen -C CN -ST Beijing -L Haidian -O PKU -OU CS -CN ROOTCA -days 3650 \\\n"
  104. " -key rootcakey.pem -pass P@ssw0rd \\\n"
  105. " -ca -path_len_constraint 6 \\\n"
  106. " -key_usage keyCertSign -key_usage cRLSign \\\n"
  107. " -crl_http_uri http://pku.edu.cn/ca.crl \\\n"
  108. " -ca_issuers_uri http://pku.edu.cn/ca.crt -ocsp_uri http://ocsp.pku.edu.cn \\\n"
  109. " -out rootcacert.pem\n"
  110. "\n"
  111. " # Generate sub-CA certificate request\n"
  112. "\n"
  113. " gmssl sm2keygen -pass P@ssw0rd -out cakey.pem\n"
  114. " gmssl reqgen -C CN -ST Beijing -L Haidian -O PKU -OU CS -CN CA -key cakey.pem -pass P@ssw0rd -out careq.pem\n"
  115. "\n"
  116. " # Sign certificate request to generate sub-CA certificate\n"
  117. "\n"
  118. " gmssl reqsign -in careq.pem -serial_len 12 -days 365 \\\n"
  119. " -cacert rootcacert.pem -key rootcakey.pem -pass P@ssw0rd \\\n"
  120. " -ca -path_len_constraint 0 \\\n"
  121. " -key_usage keyCertSign -key_usage cRLSign \\\n"
  122. " -crl_http_uri http://pku.edu.cn/ca.crl \\\n"
  123. " -ca_issuers_uri http://pku.edu.cn/ca.crt -ocsp_uri http://ocsp.pku.edu.cn \\\n"
  124. " -out cacert.pem\n"
  125. "\n";
  126. static int ext_key_usage_set(int *usages, const char *usage_name)
  127. {
  128. int flag = 0;
  129. if (x509_key_usage_from_name(&flag, usage_name) != 1) {
  130. return -1;
  131. }
  132. *usages |= flag;
  133. return 1;
  134. }
  135. int reqsign_main(int argc, char **argv)
  136. {
  137. int ret = 1;
  138. char *prog = argv[0];
  139. char *str;
  140. // Input Req/CSR
  141. char *infile = NULL;
  142. FILE *infp = stdin;
  143. uint8_t req[512];
  144. size_t reqlen;
  145. char req_id[SM2_MAX_ID_LENGTH + 1] = {0};
  146. size_t req_id_len = 0;
  147. // SerialNumber
  148. uint8_t serial[20];
  149. int serial_len = 12;
  150. // Validity
  151. int days = 0;
  152. time_t not_before;
  153. time_t not_after;
  154. // Subject from Req
  155. const uint8_t *subject;
  156. size_t subject_len;
  157. SM2_KEY subject_public_key;
  158. // CA certficate and Private Key
  159. uint8_t *cacert = NULL;
  160. size_t cacertlen;
  161. FILE *keyfp = NULL;
  162. char *pass = NULL;
  163. SM2_KEY sm2_key;
  164. char signer_id[SM2_MAX_ID_LENGTH + 1] = {0};
  165. size_t signer_id_len = 0;
  166. // Issuer from CA certificate
  167. const uint8_t *issuer;
  168. size_t issuer_len;
  169. SM2_KEY issuer_public_key;
  170. // Output
  171. char *outfile = NULL;
  172. FILE *outfp = stdout;
  173. uint8_t *cert = NULL;
  174. size_t certlen = 0;
  175. uint8_t *p;
  176. // Extensions
  177. uint8_t exts[4096];
  178. size_t extslen = 0;
  179. // AuthorityKeyIdentifier
  180. int gen_authority_key_id = 0;
  181. // SubjectKeyIdentifier
  182. int gen_subject_key_id = 0;
  183. // KeyUsage
  184. int key_usage = 0;
  185. // SubjectAltName
  186. uint8_t subject_alt_name[2048];
  187. size_t subject_alt_name_len = 0;
  188. // IssuerAltName
  189. uint8_t issuer_alt_name[512];
  190. size_t issuer_alt_name_len = 0;
  191. // BasicConstraints
  192. int ca = -1;
  193. int path_len_constraint = -1;
  194. // ExtKeyUsageSyntax
  195. int ext_key_usages[12];
  196. size_t ext_key_usages_cnt = 0;
  197. // CRLDistributionPoints
  198. char *crl_http_uri = NULL;
  199. char *crl_ldap_uri = NULL;
  200. // InhibitAnyPolicy
  201. int inhibit_any_policy = -1;
  202. // FreshestCRL
  203. char *ca_issuers_uri = NULL;
  204. char *ocsp_uri = NULL;
  205. argc--;
  206. argv++;
  207. if (argc < 1) {
  208. fprintf(stderr, "usage: %s %s\n", prog, options);
  209. return 1;
  210. }
  211. while (argc >= 1) {
  212. if (!strcmp(*argv, "-help")) {
  213. printf("usage: gmssl %s %s\n\n", prog, options);
  214. printf("%s\n", usage);
  215. ret = 0;
  216. goto end;
  217. } else if (!strcmp(*argv, "-in")) {
  218. if (--argc < 1) goto bad;
  219. infile = *(++argv);
  220. if (!(infp = fopen(infile, "rb"))) {
  221. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
  222. goto end;
  223. }
  224. } else if (!strcmp(*argv, "-req_sm2_id")) {
  225. if (--argc < 1) goto bad;
  226. str = *(++argv);
  227. if (strlen(str) > sizeof(req_id) - 1) {
  228. fprintf(stderr, "%s: invalid `-req_sm2_id` length\n", prog);
  229. goto end;
  230. }
  231. strncpy(req_id, str, sizeof(req_id));
  232. req_id_len = strlen(str);
  233. } else if (!strcmp(*argv, "-req_sm2_id_hex")) {
  234. if (--argc < 1) goto bad;
  235. str = *(++argv);
  236. if (strlen(str) > (sizeof(req_id) - 1) * 2) {
  237. fprintf(stderr, "%s: invalid `-req_sm2_id_hex` length\n", prog);
  238. goto end;
  239. }
  240. if (hex_to_bytes(str, strlen(str), (uint8_t *)req_id, &req_id_len) != 1) {
  241. fprintf(stderr, "%s: invalid `-req_sm2_id_hex` value\n", prog);
  242. goto end;
  243. }
  244. } else if (!strcmp(*argv, "-out")) {
  245. if (--argc < 1) goto bad;
  246. outfile = *(++argv);
  247. if (!(outfp = fopen(outfile, "wb"))) {
  248. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  249. goto end;
  250. }
  251. } else if (!strcmp(*argv, "-serial_len")) {
  252. if (--argc < 1) goto bad;
  253. serial_len = atoi(*(++argv));
  254. if (serial_len <= 0 || serial_len > sizeof(serial)) {
  255. fprintf(stderr, "%s: invalid `-serial_len` value, need a number less than %zu\n", prog, sizeof(serial));
  256. goto end;
  257. }
  258. } else if (!strcmp(*argv, "-days")) {
  259. if (--argc < 1) goto bad;
  260. days = atoi(*(++argv));
  261. if (days <= 0) {
  262. fprintf(stderr, "%s: invalid `-days` value, need a positive number\n", prog);
  263. goto end;
  264. }
  265. } else if (!strcmp(*argv, "-cacert")) {
  266. if (--argc < 1) goto bad;
  267. str = *(++argv);
  268. if (x509_cert_new_from_file(&cacert, &cacertlen, str) != 1) {
  269. fprintf(stderr, "%s: load ca certificate '%s' failure\n", prog, str);
  270. goto end;
  271. }
  272. } else if (!strcmp(*argv, "-key")) {
  273. if (--argc < 1) goto bad;
  274. str = *(++argv);
  275. if (!(keyfp = fopen(str, "rb"))) {
  276. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, str, strerror(errno));
  277. goto end;
  278. }
  279. } else if (!strcmp(*argv, "-pass")) {
  280. if (--argc < 1) goto bad;
  281. pass = *(++argv);
  282. } else if (!strcmp(*argv, "-sm2_id")) {
  283. if (--argc < 1) goto bad;
  284. str = *(++argv);
  285. if (strlen(str) > sizeof(signer_id) - 1) {
  286. fprintf(stderr, "%s: invalid `-sm2_id` length\n", prog);
  287. goto end;
  288. }
  289. strncpy(signer_id, str, sizeof(signer_id));
  290. signer_id_len = strlen(str);
  291. } else if (!strcmp(*argv, "-sm2_id_hex")) {
  292. if (--argc < 1) goto bad;
  293. str = *(++argv);
  294. if (strlen(str) > (sizeof(signer_id) - 1) * 2) {
  295. fprintf(stderr, "%s: invalid `-sm2_id_hex` length\n", prog);
  296. goto end;
  297. }
  298. if (hex_to_bytes(str, strlen(str), (uint8_t *)signer_id, &signer_id_len) != 1) {
  299. fprintf(stderr, "%s: invalid `-sm2_id_hex` value\n", prog);
  300. goto end;
  301. }
  302. // following copy from certgen.c
  303. } else if (!strcmp(*argv, "-gen_authority_key_id")) {
  304. gen_authority_key_id = 1;
  305. } else if (!strcmp(*argv, "-gen_subject_key_id")) {
  306. gen_subject_key_id = 1;
  307. } else if (!strcmp(*argv, "-key_usage")) {
  308. if (--argc < 1) goto bad;
  309. str = *(++argv);
  310. if (ext_key_usage_set(&key_usage, str) != 1) {
  311. fprintf(stderr, "%s: invalid `-key_usage` value '%s'\n", prog, str);
  312. goto end;
  313. }
  314. } else if (!strcmp(*argv, "-subject_dns_name")) {
  315. if (--argc < 1) goto bad;
  316. str = *(++argv);
  317. if (x509_general_names_add_dns_name(
  318. subject_alt_name, &subject_alt_name_len, sizeof(subject_alt_name), str) != 1) {
  319. fprintf(stderr, "%s: inner error on processing `-subject_dns_name`\n", prog);
  320. goto end;
  321. }
  322. } else if (!strcmp(*argv, "-issuer_dns_name")) {
  323. if (--argc < 1) goto bad;
  324. str = *(++argv);
  325. if (x509_general_names_add_dns_name(
  326. issuer_alt_name, &issuer_alt_name_len, sizeof(issuer_alt_name), str) != 1) {
  327. fprintf(stderr, "%s: inner error on processing `-issuer_dns_name`\n", prog);
  328. goto end;
  329. }
  330. } else if (!strcmp(*argv, "-ca")) {
  331. ca = 1;
  332. } else if (!strcmp(*argv, "-path_len_constraint")) {
  333. if (--argc < 1) goto bad;
  334. path_len_constraint = atoi(*(++argv));
  335. if (path_len_constraint < 0) {
  336. fprintf(stderr, "%s: invalid `-path_len_constraint` value\n", prog);
  337. goto end;
  338. }
  339. } else if (!strcmp(*argv, "-ext_key_usage")) {
  340. if (--argc < 1) goto bad;
  341. str = *(++argv);
  342. if (x509_key_purpose_from_name(str) <= 0) {
  343. fprintf(stderr, "%s: invalid `-ext_key_usage` value '%s'\n", prog, str);
  344. goto end;
  345. }
  346. if (ext_key_usages_cnt >= sizeof(ext_key_usages)/sizeof(ext_key_usages[0])) {
  347. fprintf(stderr, "%s: too much `-ext_key_usage` options\n", prog);
  348. goto end;
  349. }
  350. ext_key_usages[ext_key_usages_cnt++] = x509_key_purpose_from_name(str);
  351. } else if (!strcmp(*argv, "-crl_http_uri")) {
  352. if (--argc < 1) goto bad;
  353. crl_http_uri = *(++argv);
  354. } else if (!strcmp(*argv, "-crl_ldap_uri")) {
  355. if (--argc < 1) goto bad;
  356. crl_ldap_uri = *(++argv);
  357. } else if (!strcmp(*argv, "-inhibit_any_policy")) {
  358. if (--argc < 1) goto bad;
  359. inhibit_any_policy = atoi(*(++argv));
  360. if (inhibit_any_policy < 0) {
  361. fprintf(stderr, "%s: invalid `-inhibit_any_policy` value\n", prog);
  362. goto end;
  363. }
  364. } else if (!strcmp(*argv, "-ca_issuers_uri")) {
  365. if (--argc < 1) goto bad;
  366. ca_issuers_uri = *(++argv);
  367. } else if (!strcmp(*argv, "-ocsp_uri")) {
  368. if (--argc < 1) goto bad;
  369. ocsp_uri = *(++argv);
  370. } else {
  371. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  372. goto end;
  373. bad:
  374. fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
  375. goto end;
  376. }
  377. argc--;
  378. argv++;
  379. }
  380. if (!days) {
  381. fprintf(stderr, "%s: '-days' option required\n", prog);
  382. printf("usage: gmssl %s %s\n\n", prog, options);
  383. goto end;
  384. }
  385. if (!cacert) {
  386. fprintf(stderr, "%s: '-cacert' option required\n", prog);
  387. printf("usage: gmssl %s %s\n\n", prog, options);
  388. goto end;
  389. }
  390. if (!keyfp) {
  391. fprintf(stderr, "%s: '-key' option required\n", prog);
  392. printf("usage: gmssl %s %s\n\n", prog, options);
  393. goto end;
  394. }
  395. if (!pass) {
  396. fprintf(stderr, "%s: '-pass' option required\n", prog);
  397. printf("usage: gmssl %s %s\n\n", prog, options);
  398. goto end;
  399. }
  400. if (x509_req_from_pem(req, &reqlen, sizeof(req), infp) != 1) {
  401. fprintf(stderr, "%s: parse CSR failure\n", prog);
  402. goto end;
  403. }
  404. if (!req_id_len) {
  405. strcpy(req_id, SM2_DEFAULT_ID);
  406. req_id_len = strlen(SM2_DEFAULT_ID);
  407. }
  408. if (x509_req_verify(req, reqlen, req_id, req_id_len) != 1) {
  409. fprintf(stderr, "%s: signature verification failure\n", prog);
  410. goto end;
  411. }
  412. if (x509_req_get_details(req, reqlen,
  413. NULL, &subject, &subject_len, &subject_public_key,
  414. NULL, NULL, NULL, NULL, NULL) != 1) {
  415. fprintf(stderr, "%s: parse CSR failure\n", prog);
  416. goto end;
  417. }
  418. if (x509_cert_get_subject(cacert, cacertlen, &issuer, &issuer_len) != 1
  419. || x509_cert_get_subject_public_key(cacert, cacertlen, &issuer_public_key) != 1) {
  420. fprintf(stderr, "%s: parse CA certificate failure\n", prog);
  421. goto end;
  422. }
  423. if (sm2_private_key_info_decrypt_from_pem(&sm2_key, pass, keyfp) != 1) {
  424. fprintf(stderr, "%s: load private key failure\n", prog);
  425. goto end;
  426. }
  427. if (sm2_public_key_equ(&sm2_key, &issuer_public_key) != 1) {
  428. fprintf(stderr, "%s: private key and CA certificate not match\n", prog);
  429. goto end;
  430. }
  431. if (!signer_id_len) {
  432. strcpy(signer_id, SM2_DEFAULT_ID);
  433. signer_id_len = strlen(SM2_DEFAULT_ID);
  434. }
  435. if (rand_bytes(serial, serial_len) != 1) {
  436. fprintf(stderr, "%s: random number generator error\n", prog);
  437. goto end;
  438. }
  439. time(&not_before);
  440. if (x509_validity_add_days(&not_after, not_before, days) != 1) {
  441. fprintf(stderr, "%s: set Validity failure\n", prog);
  442. goto end;
  443. }
  444. // following code copy from certgen.c
  445. // Extensions
  446. if (gen_authority_key_id) {
  447. if (x509_exts_add_default_authority_key_identifier(exts, &extslen, sizeof(exts), &sm2_key) != 1) {
  448. fprintf(stderr, "%s: set AuthorityKeyIdentifier extension failure\n", prog);
  449. goto end;
  450. }
  451. }
  452. if (gen_subject_key_id) {
  453. if (x509_exts_add_subject_key_identifier_ex(exts, &extslen, sizeof(exts), -1, &sm2_key) != 1) {
  454. fprintf(stderr, "%s: set SubjectKeyIdentifier extension failure\n", prog);
  455. goto end;
  456. }
  457. }
  458. if (key_usage) {
  459. if (x509_exts_add_key_usage(exts, &extslen, sizeof(exts), X509_critical, key_usage) != 1) {
  460. fprintf(stderr, "%s: set KeyUsage extension failure\n", prog);
  461. goto end;
  462. }
  463. }
  464. // no CertificatePolicies
  465. // no PolicyMappings
  466. if (subject_alt_name_len) {
  467. if (x509_exts_add_subject_alt_name(exts, &extslen, sizeof(exts),
  468. -1, subject_alt_name, subject_alt_name_len) != 1) {
  469. fprintf(stderr, "%s: set SubjectAltName extension failure\n", prog);
  470. goto end;
  471. }
  472. }
  473. if (issuer_alt_name_len) {
  474. if (x509_exts_add_issuer_alt_name(exts, &extslen, sizeof(exts),
  475. -1, issuer_alt_name, issuer_alt_name_len) != 1) {
  476. fprintf(stderr, "%s: set IssuerAltName extension failure\n", prog);
  477. goto end;
  478. }
  479. }
  480. // no SubjectDirectoryAttributes
  481. if (ca >= 0 || path_len_constraint >= 0) {
  482. if (x509_exts_add_basic_constraints(exts, &extslen, sizeof(exts),
  483. X509_critical, ca, path_len_constraint) != 1) {
  484. fprintf(stderr, "%s: set BasicConstraints extension failure\n", prog);
  485. goto end;
  486. }
  487. }
  488. // no NameConstraints
  489. // no PolicyConstraints
  490. if (ext_key_usages_cnt) {
  491. if (x509_exts_add_ext_key_usage(exts, &extslen, sizeof(exts),
  492. -1, ext_key_usages, ext_key_usages_cnt) != 1) {
  493. fprintf(stderr, "%s: set ExtKeyUsage extension failure\n", prog);
  494. goto end;
  495. }
  496. }
  497. if (crl_http_uri || crl_ldap_uri) {
  498. if (x509_exts_add_crl_distribution_points(exts, &extslen, sizeof(exts),
  499. -1,
  500. crl_http_uri, crl_http_uri ? strlen(crl_http_uri) : 0,
  501. crl_ldap_uri, crl_ldap_uri ? strlen(crl_ldap_uri) : 0) != 1) {
  502. fprintf(stderr, "%s: set CRLDistributionPoints extension failure\n", prog);
  503. return -1;
  504. }
  505. }
  506. if (inhibit_any_policy >= 0) {
  507. if (x509_exts_add_inhibit_any_policy(exts, &extslen, sizeof(exts),
  508. X509_critical, inhibit_any_policy) != 1) {
  509. fprintf(stderr, "%s: set InhibitAnyPolicy extension failure\n", prog);
  510. goto end;
  511. }
  512. }
  513. if (ca_issuers_uri || ocsp_uri) {
  514. if (x509_exts_add_authority_info_access(exts, &extslen, sizeof(exts), 0,
  515. ca_issuers_uri, ca_issuers_uri ? strlen(ca_issuers_uri) : 0,
  516. ocsp_uri, ocsp_uri ? strlen(ocsp_uri) : 0) != 1) {
  517. fprintf(stderr, "%s: set AuthorityInfoAccess extension failure\n", prog);
  518. goto end;
  519. }
  520. }
  521. if (x509_cert_sign_to_der(
  522. X509_version_v3,
  523. serial, serial_len,
  524. OID_sm2sign_with_sm3,
  525. issuer, issuer_len,
  526. not_before, not_after,
  527. subject, subject_len,
  528. &subject_public_key,
  529. NULL, 0,
  530. NULL, 0,
  531. exts, extslen,
  532. &sm2_key, signer_id, signer_id_len,
  533. NULL, &certlen) != 1) {
  534. fprintf(stderr, "%s: certificate generation failure\n", prog);
  535. goto end;
  536. }
  537. if (!(cert = malloc(certlen))) {
  538. fprintf(stderr, "%s: malloc failure\n", prog);
  539. goto end;
  540. }
  541. p = cert;
  542. certlen = 0;
  543. if (x509_cert_sign_to_der(
  544. X509_version_v3,
  545. serial, serial_len,
  546. OID_sm2sign_with_sm3,
  547. issuer, issuer_len,
  548. not_before, not_after,
  549. subject, subject_len,
  550. &subject_public_key,
  551. NULL, 0,
  552. NULL, 0,
  553. exts, extslen,
  554. &sm2_key, signer_id, signer_id_len,
  555. &p, &certlen) != 1) {
  556. fprintf(stderr, "%s: certificate generation failure\n", prog);
  557. goto end;
  558. }
  559. if (x509_cert_to_pem(cert, certlen, outfp) != 1) {
  560. fprintf(stderr, "%s: output certificate failed\n", prog);
  561. goto end;
  562. }
  563. ret = 0;
  564. end:
  565. gmssl_secure_clear(&sm2_key, sizeof(SM2_KEY));
  566. if (cert) free(cert);
  567. if (keyfp) fclose(keyfp);
  568. if (infile && infp) fclose(infp);
  569. if (outfile && outfp) fclose(outfp);
  570. return ret;
  571. }