sm9decrypt.c 2.5 KB

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