cmsencrypt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/cms.h>
  15. #include <gmssl/x509.h>
  16. #include <gmssl/rand.h>
  17. /*
  18. 签名的时候要提供签名者的证书,并且提供签名私钥
  19. 但是验证的时候假定CMS中已经包含签名者的证书了,但是我们要提供CA证书库
  20. 加密的时候要指定接收者的证书,并且可以有多个接收者
  21. 解密的时候只提供一个解密私钥,但是最好配合解密者的证书,从这个证书中找到解密者的名字
  22. 如果即加密又签名,那么输出的是SignedAndEnveloped
  23. CMS有PEM吗?
  24. cms -encrypt -rcpt a.pem -rcpt b.pem -rcpt c.pem -in file -sign -signcert a.pem -signcert b.pem
  25. -rcptcert -rcpt_cert -sign_cert b.pem -signkey
  26. 首先接收者可以有多个证书
  27. 这里面有个问题,因为我们要输出一个加密的对象,因此我们必须把输入的内容读取进来。
  28. EnvelopedData 是一个封装的SEQUENCE中,因此必须读取所有的内容。
  29. 如果是一个文件,就需要读取所有的文件内容,如果是一个stream ,也需要读取完整的内容到一个足够大的buffer中,如何设置这个buffer的大小呢
  30. 对于输入文件,如果输入有文件名的话,可以直接通过stat获取文件长度
  31. 但是如果对于stream的话,实际上我们是没有办法获得输入长度的,那么就直接准备一个buffer好了。
  32. 不要给自己找麻烦了,直接只支持文件输入吧
  33. encrypt
  34. */
  35. static const char *options = "-encrypt (-rcptcert pem)* -in file -out file";
  36. static int get_files_size(int argc, char **argv, const char *option, size_t *len)
  37. {
  38. char *prog = argv[0];
  39. char *file = NULL;
  40. FILE *fp = NULL;
  41. argc--;
  42. argv++;
  43. *len = 0;
  44. while (argc > 1) {
  45. if (!strcmp(*argv, option)) {
  46. size_t fsize;
  47. if (--argc < 1) {
  48. fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
  49. return -1;
  50. }
  51. file = *(++argv);
  52. if (!(fp = fopen(file, "rb"))) {
  53. fprintf(stderr, "%s: open '%s' failed : %s\n", prog, file, strerror(errno));
  54. return -1;
  55. }
  56. if (file_size(fp, &fsize) != 1) {
  57. fprintf(stderr, "%s: access '%s' failed : %s\n", prog, file, strerror(errno));
  58. fclose(fp);
  59. return -1;
  60. }
  61. *len += fsize;
  62. fclose(fp);
  63. }
  64. argc--;
  65. argv++;
  66. }
  67. return 1;
  68. }
  69. int cmsencrypt_main(int argc, char **argv)
  70. {
  71. int ret = 1;
  72. char *prog = argv[0];
  73. int op = 0;
  74. char *infile = NULL;
  75. char *outfile = NULL;
  76. FILE *infp = stdin;
  77. FILE *outfp = stdout;
  78. uint8_t *rcpt_certs = NULL;
  79. size_t rcpt_certs_len;
  80. uint8_t key[16];
  81. uint8_t iv[16];
  82. uint8_t *inbuf = NULL;
  83. size_t inlen;
  84. uint8_t *cms = NULL;
  85. size_t cmslen;
  86. uint8_t *cert;
  87. if (argc < 2) {
  88. fprintf(stderr, "usage: %s %s\n", prog, options);
  89. return 1;
  90. }
  91. // 预先统计证书缓冲大小和输入大小
  92. if (get_files_size(argc, argv, "-rcptcert", &rcpt_certs_len) != 1) {
  93. goto end;
  94. }
  95. if (rcpt_certs_len <= 0) {
  96. fprintf(stderr, "%s: invalid cert length\n", prog);
  97. goto end;
  98. }
  99. rcpt_certs_len = (rcpt_certs_len * 3)/4;
  100. if (!(rcpt_certs = malloc(rcpt_certs_len))) {
  101. fprintf(stderr, "%s: malloc failure\n", prog);
  102. goto end;
  103. }
  104. cert = rcpt_certs;
  105. if (get_files_size(argc, argv, "-in", &inlen) != 1) {
  106. goto end;
  107. }
  108. if (inlen <= 0) {
  109. fprintf(stderr, "%s: invalid input length\n", prog);
  110. goto end;
  111. }
  112. if (!(inbuf = malloc(inlen))) {
  113. fprintf(stderr, "%s: %s\n", prog, strerror(errno));
  114. goto end;
  115. }
  116. argc--;
  117. argv++;
  118. while (argc > 1) {
  119. if (!strcmp(*argv, "-help")) {
  120. printf("usage: %s %s\n", prog, options);
  121. ret = 0;
  122. goto end;
  123. } else if (!strcmp(*argv, "-rcptcert")) {
  124. char *certfile;
  125. FILE *certfp;
  126. size_t certlen;
  127. if (--argc < 1) goto bad;
  128. certfile = *(++argv);
  129. if (!(certfp = fopen(certfile, "rb"))) {
  130. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, certfile, strerror(errno));
  131. goto end;
  132. }
  133. if (x509_cert_from_pem(cert, &certlen, rcpt_certs_len, certfp) != 1) {
  134. fprintf(stderr, "%s: error\n", prog);
  135. fclose(certfp);
  136. goto end;
  137. }
  138. cert += certlen;
  139. fclose(certfp);
  140. } else if (!strcmp(*argv, "-in")) {
  141. if (--argc < 1) goto bad;
  142. infile = *(++argv);
  143. if (!(infp = fopen(infile, "rb"))) {
  144. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
  145. goto end;
  146. }
  147. if ((inlen = fread(inbuf, 1, inlen, infp)) <= 0) {
  148. fprintf(stderr, "%s: read data error: %s\n", prog, strerror(errno));
  149. goto end;
  150. }
  151. } else if (!strcmp(*argv, "-out")) {
  152. if (--argc < 1) goto bad;
  153. outfile = *(++argv);
  154. if (!(outfp = fopen(outfile, "wb"))) {
  155. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  156. goto end;
  157. }
  158. } else {
  159. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  160. goto end;
  161. bad:
  162. fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
  163. goto end;
  164. }
  165. argc--;
  166. argv++;
  167. }
  168. rcpt_certs_len = cert - rcpt_certs;
  169. if (rand_bytes(key, sizeof(key)) != 1
  170. || rand_bytes(iv, sizeof(iv)) != 1
  171. || cms_envelop(NULL, &cmslen, rcpt_certs, rcpt_certs_len,
  172. OID_sm4_cbc, key, sizeof(key), iv, sizeof(iv),
  173. OID_cms_data, inbuf, inlen, NULL, 0, NULL, 0) != 1) {
  174. fprintf(stderr, "%s: inner error\n", prog);
  175. goto end;
  176. }
  177. if (!(cms = malloc(cmslen))) {
  178. fprintf(stderr, "%s: malloc failure\n", prog);
  179. goto end;
  180. }
  181. if (cms_envelop(cms, &cmslen, rcpt_certs, rcpt_certs_len,
  182. OID_sm4_cbc, key, sizeof(key), iv, sizeof(iv),
  183. OID_cms_data, inbuf, inlen, NULL, 0, NULL, 0) != 1) {
  184. fprintf(stderr, "%s: inner error\n", prog);
  185. goto end;
  186. }
  187. if (cms_to_pem(cms, cmslen, outfp) != 1) {
  188. fprintf(stderr, "%s: output CMS failure\n", prog);
  189. goto end;
  190. }
  191. ret = 0;
  192. end:
  193. if (infile && infp) fclose(infp);
  194. if (outfile && outfp) fclose(outfp);
  195. if (rcpt_certs) free(rcpt_certs);
  196. if (cms) free(cms);
  197. return ret;
  198. }