sm9verify.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/sm9.h>
  13. #include <gmssl/error.h>
  14. static const char *options = "[-in file] -pubmaster file -id str -sig file";
  15. int sm9verify_main(int argc, char **argv)
  16. {
  17. int ret = 1;
  18. char *prog = argv[0];
  19. char *infile = NULL;
  20. char *mpkfile = NULL;
  21. char *id = NULL;
  22. char *sigfile = NULL;
  23. FILE *infp = stdin;
  24. FILE *mpkfp = NULL;
  25. FILE *sigfp = NULL;
  26. SM9_SIGN_MASTER_KEY mpk;
  27. SM9_SIGN_CTX ctx;
  28. uint8_t buf[4096];
  29. size_t len;
  30. uint8_t sig[SM9_SIGNATURE_SIZE];
  31. size_t siglen;
  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, "-in")) {
  43. if (--argc < 1) goto bad;
  44. infile = *(++argv);
  45. if (!(infp = fopen(infile, "rb"))) {
  46. error_print();
  47. goto end;
  48. }
  49. } else if (!strcmp(*argv, "-pubmaster")) {
  50. if (--argc < 1) goto bad;
  51. mpkfile = *(++argv);
  52. if (!(mpkfp = fopen(mpkfile, "rb"))) {
  53. error_print();
  54. goto end;
  55. }
  56. } else if (!strcmp(*argv, "-id")) {
  57. if (--argc < 1) goto bad;
  58. id = *(++argv);
  59. } else if (!strcmp(*argv, "-sig")) {
  60. if (--argc < 1) goto bad;
  61. sigfile = *(++argv);
  62. if (!(sigfp = fopen(sigfile, "rb"))) {
  63. error_print();
  64. goto end;
  65. }
  66. } else {
  67. bad:
  68. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  69. return 1;
  70. }
  71. argc--;
  72. argv++;
  73. }
  74. if (!mpkfile || !id || !sigfile) {
  75. error_print();
  76. goto end;
  77. }
  78. if (sm9_sign_master_public_key_from_pem(&mpk, mpkfp) != 1) {
  79. error_print();
  80. goto end;
  81. }
  82. if ((siglen = fread(sig, 1, sizeof(sig), sigfp)) <= 0) {
  83. error_print();
  84. goto end;
  85. }
  86. if (sm9_verify_init(&ctx) != 1) {
  87. error_print();
  88. goto end;
  89. }
  90. while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
  91. if (sm9_verify_update(&ctx, buf, len) != 1) {
  92. error_print();
  93. goto end;
  94. }
  95. }
  96. if ((ret = sm9_verify_finish(&ctx, sig, siglen, &mpk, id, strlen(id))) != 1) {
  97. error_print();
  98. goto end;
  99. }
  100. printf("%s %s\n", prog, ret ? "success" : "failure");
  101. end:
  102. if (infile && infp) fclose(infp);
  103. if (mpkfile && mpkfp) fclose(mpkfp);
  104. if (sigfile && sigfp) fclose(sigfp);
  105. return ret;
  106. }