sm9setup.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/oid.h>
  13. #include <gmssl/mem.h>
  14. #include <gmssl/sm9.h>
  15. #include <gmssl/error.h>
  16. static const char *options = "-alg (sm9sign|sm9encrypt) [-pass password] [-out pem] [-pubout pem]";
  17. int sm9setup_main(int argc, char **argv)
  18. {
  19. int ret = 1;
  20. char *prog = argv[0];
  21. char *alg = NULL;
  22. char *pass = NULL;
  23. char *outfile = NULL;
  24. char *puboutfile = NULL;
  25. int oid;
  26. FILE *outfp = stdout;
  27. FILE *puboutfp = stdout;
  28. SM9_SIGN_MASTER_KEY sign_msk;
  29. SM9_ENC_MASTER_KEY enc_msk;
  30. argc--;
  31. argv++;
  32. if (argc < 1) {
  33. fprintf(stderr, "usage: %s %s\n", prog, options);
  34. return 1;
  35. }
  36. while (argc > 0) {
  37. if (!strcmp(*argv, "-help")) {
  38. fprintf(stdout, "usage: %s %s\n", prog, options);
  39. return 0;
  40. } else if (!strcmp(*argv, "-alg")) {
  41. if (--argc < 1) goto bad;
  42. alg = *(++argv);
  43. if ((oid = sm9_oid_from_name(alg)) < 1) {
  44. fprintf(stdout, "%s: invalid alg '%s', should be sm9sign or sm9encrypt\n", prog, alg);
  45. goto end;
  46. }
  47. } else if (!strcmp(*argv, "-pass")) {
  48. if (--argc < 1) goto bad;
  49. pass = *(++argv);
  50. } else if (!strcmp(*argv, "-out")) {
  51. if (--argc < 1) goto bad;
  52. outfile = *(++argv);
  53. if (!(outfp = fopen(outfile, "wb"))) {
  54. error_print();
  55. goto end;
  56. }
  57. } else if (!strcmp(*argv, "-pubout")) {
  58. if (--argc < 1) goto bad;
  59. puboutfile = *(++argv);
  60. if (!(puboutfp = fopen(puboutfile, "wb"))) {
  61. error_print();
  62. goto end;
  63. }
  64. } else {
  65. bad:
  66. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  67. return 1;
  68. }
  69. argc--;
  70. argv++;
  71. }
  72. if (!alg) {
  73. error_print();
  74. return -1;
  75. }
  76. if (!pass) {
  77. error_print();
  78. return -1;
  79. }
  80. switch (oid) {
  81. case OID_sm9sign:
  82. if (sm9_sign_master_key_generate(&sign_msk) != 1
  83. || sm9_sign_master_key_info_encrypt_to_pem(&sign_msk, pass, outfp) != 1
  84. || sm9_sign_master_public_key_to_pem(&sign_msk, puboutfp) != 1) {
  85. error_print();
  86. goto end;
  87. }
  88. break;
  89. case OID_sm9encrypt:
  90. if (sm9_enc_master_key_generate(&enc_msk) != 1
  91. || sm9_enc_master_key_info_encrypt_to_pem(&enc_msk, pass, outfp) != 1
  92. || sm9_enc_master_public_key_to_pem(&enc_msk, puboutfp) != 1) {
  93. error_print();
  94. goto end;
  95. }
  96. break;
  97. default:
  98. error_print();
  99. goto end;
  100. }
  101. ret = 0;
  102. end:
  103. gmssl_secure_clear(&sign_msk, sizeof(sign_msk));
  104. gmssl_secure_clear(&enc_msk, sizeof(enc_msk));
  105. if (outfile && outfp) fclose(outfp);
  106. if (puboutfile && puboutfp) fclose(puboutfp);
  107. return 1;
  108. }