sm2sign.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/sm2.h>
  14. #include <gmssl/mem.h>
  15. static const char *options = "-key pem -pass str [-id str] [-in file] [-out file]";
  16. int sm2sign_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 *id = SM2_DEFAULT_ID;
  23. char *infile = NULL;
  24. char *outfile = NULL;
  25. FILE *keyfp = NULL;
  26. FILE *infp = stdin;
  27. FILE *outfp = stdout;
  28. SM2_KEY key;
  29. SM2_SIGN_CTX sign_ctx;
  30. uint8_t buf[4096];
  31. size_t len;
  32. uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
  33. size_t siglen;
  34. argc--;
  35. argv++;
  36. if (argc < 1) {
  37. fprintf(stderr, "usage: %s %s\n", prog, options);
  38. return 1;
  39. }
  40. while (argc > 0) {
  41. if (!strcmp(*argv, "-help")) {
  42. printf("usage: %s %s\n", prog, options);
  43. ret = 0;
  44. goto end;
  45. } else if (!strcmp(*argv, "-key")) {
  46. if (--argc < 1) goto bad;
  47. keyfile = *(++argv);
  48. if (!(keyfp = fopen(keyfile, "rb"))) {
  49. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
  50. goto end;
  51. }
  52. } else if (!strcmp(*argv, "-pass")) {
  53. if (--argc < 1) goto bad;
  54. pass = *(++argv);
  55. } else if (!strcmp(*argv, "-id")) {
  56. if (--argc < 1) goto bad;
  57. id = *(++argv);
  58. } else if (!strcmp(*argv, "-in")) {
  59. if (--argc < 1) goto bad;
  60. infile = *(++argv);
  61. if (!(infp = fopen(infile, "rb"))) {
  62. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
  63. goto end;
  64. }
  65. } else if (!strcmp(*argv, "-out")) {
  66. if (--argc < 1) goto bad;
  67. outfile = *(++argv);
  68. if (!(outfp = fopen(outfile, "wb"))) {
  69. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  70. goto end;
  71. }
  72. } else {
  73. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  74. goto end;
  75. bad:
  76. fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
  77. goto end;
  78. }
  79. argc--;
  80. argv++;
  81. }
  82. if (!keyfile) {
  83. fprintf(stderr, "%s: '-key' option required\n", prog);
  84. goto end;
  85. }
  86. if (!pass) {
  87. fprintf(stderr, "%s: '-pass' option required\n", prog);
  88. goto end;
  89. }
  90. if (sm2_private_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
  91. fprintf(stderr, "%s: private key decryption failure\n", prog);
  92. goto end;
  93. }
  94. if (sm2_sign_init(&sign_ctx, &key, id, strlen(id)) != 1) {
  95. fprintf(stderr, "%s: inner error\n", prog);
  96. goto end;
  97. }
  98. while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
  99. if (sm2_sign_update(&sign_ctx, buf, len) != 1) {
  100. fprintf(stderr, "%s: inner error\n", prog);
  101. goto end;
  102. }
  103. }
  104. if (sm2_sign_finish(&sign_ctx, sig, &siglen) != 1) {
  105. fprintf(stderr, "%s: inner error\n", prog);
  106. goto end;
  107. }
  108. if (fwrite(sig, 1, siglen, outfp) != siglen) {
  109. fprintf(stderr, "%s: output signature failed : %s\n", prog, strerror(errno));
  110. goto end;
  111. }
  112. ret = 0;
  113. end:
  114. gmssl_secure_clear(&key, sizeof(key));
  115. gmssl_secure_clear(&sign_ctx, sizeof(sign_ctx));
  116. if (keyfp) fclose(keyfp);
  117. if (infile && infp) fclose(infp);
  118. if (outfile && outfp) fclose(outfp);
  119. return ret;
  120. }