cmssign.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/file.h>
  14. #include <gmssl/x509.h>
  15. #include <gmssl/cms.h>
  16. #include <gmssl/error.h>
  17. /*
  18. 302 typedef struct {
  19. 303 uint8_t *certs;
  20. 304 size_t certs_len;
  21. 305 SM2_KEY *sign_key;
  22. 306 } CMS_CERTS_AND_KEY;
  23. 输出长度主要由输入长度和
  24. */
  25. static const char *options = "-key file -pass str -cert file -in file [-out file]";
  26. int cmssign_main(int argc, char **argv)
  27. {
  28. int ret = 1;
  29. char *prog = argv[0];
  30. char *keyfile = NULL;
  31. char *pass = NULL;
  32. char *certfile = NULL;
  33. char *infile = NULL;
  34. char *outfile = NULL;
  35. FILE *keyfp = NULL;
  36. FILE *certfp = NULL;
  37. FILE *infp = NULL;
  38. FILE *outfp = stdout;
  39. SM2_KEY key;
  40. uint8_t cert[1024];
  41. size_t certlen;
  42. uint8_t *in = NULL;
  43. size_t inlen;
  44. uint8_t *cms = NULL;
  45. size_t cmslen, cms_maxlen;
  46. CMS_CERTS_AND_KEY cert_and_key;
  47. argc--;
  48. argv++;
  49. if (argc < 1) {
  50. fprintf(stderr, "usage: %s %s\n", prog, options);
  51. return 1;
  52. }
  53. while (argc > 1) {
  54. if (!strcmp(*argv, "-help")) {
  55. printf("usage: %s %s\n", prog, options);
  56. ret = 0;
  57. goto end;
  58. } else if (!strcmp(*argv, "-key")) {
  59. if (--argc < 1) goto bad;
  60. keyfile = *(++argv);
  61. if (!(keyfp = fopen(keyfile, "rb"))) {
  62. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
  63. goto end;
  64. }
  65. } else if (!strcmp(*argv, "-pass")) {
  66. if (--argc < 1) goto bad;
  67. pass = *(++argv);
  68. } else if (!strcmp(*argv, "-cert")) {
  69. if (--argc < 1) goto bad;
  70. certfile = *(++argv);
  71. if (!(certfp = fopen(certfile, "rb"))) {
  72. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, certfile, strerror(errno));
  73. goto end;
  74. }
  75. } else if (!strcmp(*argv, "-in")) {
  76. if (--argc < 1) goto bad;
  77. infile = *(++argv);
  78. if (!(infp = fopen(infile, "rb"))) {
  79. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
  80. goto end;
  81. }
  82. } else if (!strcmp(*argv, "-out")) {
  83. if (--argc < 1) goto bad;
  84. outfile = *(++argv);
  85. if (!(outfp = fopen(outfile, "wb"))) {
  86. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  87. goto end;
  88. }
  89. } else {
  90. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  91. goto end;
  92. bad:
  93. fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
  94. goto end;
  95. }
  96. argc--;
  97. argv++;
  98. }
  99. if (!keyfile) {
  100. fprintf(stderr, "%s: '-key' option required\n", prog);
  101. goto end;
  102. }
  103. if (!pass) {
  104. fprintf(stderr, "%s: '-pass' option required\n", prog);
  105. goto end;
  106. }
  107. if (!certfile) {
  108. fprintf(stderr, "%s: '-cert' option required\n", prog);
  109. goto end;
  110. }
  111. if (!infile) {
  112. fprintf(stderr, "%s: '-in' option required\n", prog);
  113. goto end;
  114. }
  115. if (sm2_private_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
  116. fprintf(stderr, "%s: private key decryption failure\n", prog);
  117. goto end;
  118. }
  119. if (x509_cert_from_pem(cert, &certlen, sizeof(cert), certfp) != 1) {
  120. fprintf(stderr, "%s: load certificate failure\n", prog);
  121. goto end;
  122. }
  123. {
  124. SM2_KEY public_key;
  125. if (x509_cert_get_subject_public_key(cert, certlen, &public_key) != 1) {
  126. fprintf(stderr, "%s: parse certficate failure\n", prog);
  127. goto end;
  128. }
  129. if (sm2_public_key_equ(&key, &public_key) != 1) {
  130. fprintf(stderr, "%s: key and cert are not match!\n", prog);
  131. goto end;
  132. }
  133. }
  134. cert_and_key.certs = cert;
  135. cert_and_key.certs_len = certlen;
  136. cert_and_key.sign_key = &key;
  137. if (file_size(infp, &inlen) != 1) {
  138. fprintf(stderr, "%s: get input length failed\n", prog);
  139. goto end;
  140. }
  141. if (!(in = malloc(inlen))) {
  142. fprintf(stderr, "%s: malloc failure\n", prog);
  143. goto end;
  144. }
  145. if (fread(in, 1, inlen, infp) != inlen) {
  146. fprintf(stderr, "%s: read file error : %s\n", prog, strerror(errno));
  147. goto end;
  148. }
  149. cms_maxlen = (inlen * 4)/3 + 4096; // 主要由SignerInfos,其中的DN长度决定
  150. if (!(cms = malloc(cms_maxlen))) {
  151. fprintf(stderr, "%s: malloc failure\n", prog);
  152. goto end;
  153. }
  154. if (cms_sign(cms, &cmslen, &cert_and_key, 1, OID_cms_data, in, inlen, NULL, 0) != 1) {
  155. fprintf(stderr, "%s: sign failure\n", prog);
  156. goto end;
  157. }
  158. if (cms_to_pem(cms, cmslen, outfp) != 1) {
  159. fprintf(stderr, "%s: output failure\n", prog);
  160. goto end;
  161. }
  162. ret = 0;
  163. end:
  164. if (infile && infp) fclose(infp);
  165. if (outfile && outfp) fclose(outfp);
  166. if (keyfile && keyfp) fclose(keyfp);
  167. if (cms) free(cms);
  168. if (in) free(in);
  169. return ret;
  170. }