sm9sign.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 [-out file]";
  16. int sm9sign_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 *outfile = NULL;
  24. FILE *infp = stdin;
  25. FILE *keyfp = NULL;
  26. FILE *outfp = stdout;
  27. SM9_SIGN_KEY key;
  28. SM9_SIGN_CTX ctx;
  29. uint8_t buf[4096];
  30. size_t len;
  31. uint8_t sig[SM9_SIGNATURE_SIZE];
  32. size_t siglen;
  33. argc--;
  34. argv++;
  35. if (argc < 1) {
  36. fprintf(stderr, "usage: %s %s\n", prog, options);
  37. return 1;
  38. }
  39. while (argc > 0) {
  40. if (!strcmp(*argv, "-help")) {
  41. fprintf(stdout, "usage: %s %s\n", prog, options);
  42. return 0;
  43. } else if (!strcmp(*argv, "-in")) {
  44. if (--argc < 1) goto bad;
  45. infile = *(++argv);
  46. if (!(infp = fopen(infile, "rb"))) {
  47. error_print();
  48. goto end;
  49. }
  50. } else if (!strcmp(*argv, "-key")) {
  51. if (--argc < 1) goto bad;
  52. keyfile = *(++argv);
  53. if (!(keyfp = fopen(keyfile, "rb"))) {
  54. error_print();
  55. goto end;
  56. }
  57. } else if (!strcmp(*argv, "-pass")) {
  58. if (--argc < 1) goto bad;
  59. pass = *(++argv);
  60. } else if (!strcmp(*argv, "-out")) {
  61. if (--argc < 1) goto bad;
  62. outfile = *(++argv);
  63. if (!(outfp = fopen(outfile, "wb"))) {
  64. error_print();
  65. goto end;
  66. }
  67. } else {
  68. bad:
  69. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  70. return 1;
  71. }
  72. argc--;
  73. argv++;
  74. }
  75. if (!keyfile || !pass) {
  76. error_print();
  77. goto end;
  78. }
  79. if (sm9_sign_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
  80. error_print();
  81. return -1;
  82. }
  83. if (sm9_sign_init(&ctx) != 1) {
  84. error_print();
  85. goto end;
  86. }
  87. while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
  88. if (sm9_sign_update(&ctx, buf, len) != 1) {
  89. error_print();
  90. goto end;
  91. }
  92. }
  93. if (sm9_sign_finish(&ctx, &key, sig, &siglen) != 1) {
  94. error_print();
  95. goto end;
  96. }
  97. if (siglen != fwrite(sig, 1, siglen, outfp)) {
  98. error_print();
  99. goto end;
  100. }
  101. ret = 0;
  102. end:
  103. gmssl_secure_clear(&key, sizeof(key));
  104. gmssl_secure_clear(&ctx, sizeof(ctx));
  105. gmssl_secure_clear(buf, sizeof(buf));
  106. if (infile && infp) fclose(infp);
  107. if (outfile && outfp) fclose(outfp);
  108. return ret;
  109. }