cmsdecrypt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. static const char *options = "-key file -pass str -cert file -in file [-out file]";
  17. int cmsdecrypt_main(int argc, char **argv)
  18. {
  19. int ret = 1;
  20. char *prog = argv[0];
  21. char *keyfile = NULL;
  22. char *pass = NULL;
  23. char *certfile = NULL;
  24. char *infile = NULL;
  25. char *outfile = NULL;
  26. FILE *keyfp = NULL;
  27. FILE *certfp = NULL;
  28. FILE *infp = NULL;
  29. FILE *outfp = stdout;
  30. uint8_t cert[1024];
  31. size_t certlen;
  32. size_t inlen;
  33. uint8_t *cms = NULL;
  34. size_t cmslen, cms_maxlen;
  35. SM2_KEY key;
  36. int content_type;
  37. uint8_t *content = NULL;
  38. size_t content_len;
  39. const uint8_t *rcpt_infos;
  40. size_t rcpt_infos_len;
  41. const uint8_t *shared_info1;
  42. const uint8_t *shared_info2;
  43. size_t shared_info1_len, shared_info2_len;
  44. argc--;
  45. argv++;
  46. if (argc < 1) {
  47. fprintf(stderr, "usage: %s %s\n", prog, options);
  48. return 1;
  49. }
  50. while (argc > 1) {
  51. if (!strcmp(*argv, "-help")) {
  52. printf("usage: %s %s\n", prog, options);
  53. ret = 0;
  54. goto end;
  55. } else if (!strcmp(*argv, "-key")) {
  56. if (--argc < 1) goto bad;
  57. keyfile = *(++argv);
  58. if (!(keyfp = fopen(keyfile, "rb"))) {
  59. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
  60. goto end;
  61. }
  62. } else if (!strcmp(*argv, "-pass")) {
  63. if (--argc < 1) goto bad;
  64. pass = *(++argv);
  65. } else if (!strcmp(*argv, "-cert")) {
  66. if (--argc < 1) goto bad;
  67. certfile = *(++argv);
  68. if (!(certfp = fopen(certfile, "rb"))) {
  69. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, certfile, strerror(errno));
  70. goto end;
  71. }
  72. } else if (!strcmp(*argv, "-in")) {
  73. if (--argc < 1) goto bad;
  74. infile = *(++argv);
  75. if (!(infp = fopen(infile, "rb"))) {
  76. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
  77. goto end;
  78. }
  79. } else if (!strcmp(*argv, "-out")) {
  80. if (--argc < 1) goto bad;
  81. outfile = *(++argv);
  82. if (!(outfp = fopen(outfile, "wb"))) {
  83. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  84. goto end;
  85. }
  86. } else {
  87. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  88. goto end;
  89. bad:
  90. fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
  91. goto end;
  92. }
  93. argc--;
  94. argv++;
  95. }
  96. if (!keyfile) {
  97. fprintf(stderr, "%s: '-key' option required\n", prog);
  98. goto end;
  99. }
  100. if (!pass) {
  101. fprintf(stderr, "%s: '-pass' option required\n", prog);
  102. goto end;
  103. }
  104. if (!certfile) {
  105. fprintf(stderr, "%s: '-cert' option required\n", prog);
  106. goto end;
  107. }
  108. if (!infile) {
  109. fprintf(stderr, "%s: '-in' option required\n", prog);
  110. goto end;
  111. }
  112. if (sm2_private_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
  113. fprintf(stderr, "%s: private key decryption failure\n", prog);
  114. goto end;
  115. }
  116. if (x509_cert_from_pem(cert, &certlen, sizeof(cert), certfp) != 1) {
  117. fprintf(stderr, "%s: load certificate failure\n", prog);
  118. goto end;
  119. }
  120. if (file_size(infp, &inlen) != 1) {
  121. fprintf(stderr, "%s: get input length failed\n", prog);
  122. goto end;
  123. }
  124. cms_maxlen = (inlen * 3)/4 + 1;
  125. if (!(cms = malloc(cms_maxlen))) {
  126. fprintf(stderr, "%s: malloc failure\n", prog);
  127. goto end;
  128. }
  129. if (cms_from_pem(cms, &cmslen, cms_maxlen, infp) != 1) {
  130. fprintf(stderr, "%s: read CMS failure\n", prog);
  131. goto end;
  132. }
  133. if (!(content = malloc(cmslen))) {
  134. fprintf(stderr, "%s: malloc failure\n", prog);
  135. goto end;
  136. }
  137. if (cms_deenvelop(cms, cmslen,
  138. &key, cert, certlen,
  139. &content_type, content, &content_len,
  140. &rcpt_infos, &rcpt_infos_len,
  141. &shared_info1, &shared_info1_len,
  142. &shared_info2, &shared_info2_len) != 1) {
  143. fprintf(stderr, "%s: decryption failure\n", prog);
  144. goto end;
  145. }
  146. if (content_type != OID_cms_data) {
  147. fprintf(stderr, "%s: invalid CMS content type: %s\n", prog, cms_content_type_name(content_type));
  148. goto end;
  149. }
  150. if (fwrite(content, 1, content_len, outfp) != content_len) {
  151. fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
  152. goto end;
  153. }
  154. ret = 0;
  155. end:
  156. if (infile && infp) fclose(infp);
  157. if (outfile && outfp) fclose(outfp);
  158. if (keyfile && keyfp) fclose(keyfp);
  159. if (cms) free(cms);
  160. if (content) free(content);
  161. return ret;
  162. }