sm2decrypt.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/mem.h>
  14. #include <gmssl/sm2.h>
  15. static const char *options = "-key pem -pass str [-in file] [-out file]";
  16. int sm2decrypt_main(int argc, char **argv)
  17. {
  18. int ret = 1;
  19. char *prog = argv[0];
  20. char *keyfile = NULL;
  21. char *pass = NULL;
  22. char *infile = NULL;
  23. char *outfile = NULL;
  24. FILE *keyfp = NULL;
  25. FILE *infp = stdin;
  26. FILE *outfp = stdout;
  27. SM2_KEY key;
  28. uint8_t inbuf[SM2_MAX_CIPHERTEXT_SIZE];
  29. uint8_t outbuf[SM2_MAX_CIPHERTEXT_SIZE];
  30. size_t inlen, outlen;
  31. argc--;
  32. argv++;
  33. if (argc < 1) {
  34. fprintf(stderr, "usage: %s %s\n", prog, options);
  35. return 1;
  36. }
  37. while (argc > 0) {
  38. if (!strcmp(*argv, "-help")) {
  39. printf("usage: %s %s\n", prog, options);
  40. ret = 0;
  41. goto end;
  42. } else if (!strcmp(*argv, "-key")) {
  43. if (--argc < 1) goto bad;
  44. keyfile = *(++argv);
  45. if (!(keyfp = fopen(keyfile, "rb"))) {
  46. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
  47. goto end;
  48. }
  49. } else if (!strcmp(*argv, "-pass")) {
  50. if (--argc < 1) goto bad;
  51. pass = *(++argv);
  52. } else if (!strcmp(*argv, "-in")) {
  53. if (--argc < 1) goto bad;
  54. infile = *(++argv);
  55. if (!(infp = fopen(infile, "rb"))) {
  56. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
  57. goto end;
  58. }
  59. } else if (!strcmp(*argv, "-out")) {
  60. if (--argc < 1) goto bad;
  61. outfile = *(++argv);
  62. if (!(outfp = fopen(outfile, "wb"))) {
  63. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  64. goto end;
  65. }
  66. } else {
  67. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  68. goto end;
  69. bad:
  70. fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
  71. goto end;
  72. }
  73. argc--;
  74. argv++;
  75. }
  76. if (!keyfile) {
  77. fprintf(stderr, "%s: '-key' option required\n", prog);
  78. goto end;
  79. }
  80. if (!pass) {
  81. fprintf(stderr, "%s: '-pass' option required\n", prog);
  82. goto end;
  83. }
  84. if (sm2_private_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
  85. fprintf(stderr, "%s: private key decryption failure\n", prog);
  86. goto end;
  87. }
  88. if ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) <= 0) {
  89. fprintf(stderr, "%s: read input failed : %s\n", prog, strerror(errno));
  90. goto end;
  91. }
  92. if (sm2_decrypt(&key, inbuf, inlen, outbuf, &outlen) != 1) {
  93. fprintf(stderr, "%s: decryption failure\n", prog);
  94. goto end;
  95. }
  96. if (outlen != fwrite(outbuf, 1, outlen, outfp)) {
  97. fprintf(stderr, "%s: output plaintext failed : %s\n", prog, strerror(errno));
  98. goto end;
  99. }
  100. ret = 0;
  101. end:
  102. gmssl_secure_clear(&key, sizeof(key));
  103. if (keyfp) fclose(keyfp);
  104. if (infile && infp) fclose(infp);
  105. if (outfile && outfp) fclose(outfp);
  106. return ret;
  107. }