sm9keygen.c 3.1 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 <string.h>
  11. #include <stdlib.h>
  12. #include <gmssl/mem.h>
  13. #include <gmssl/oid.h>
  14. #include <gmssl/sm9.h>
  15. #include <gmssl/error.h>
  16. static const char *options = "-alg (sm9sign|sm9encrypt) -in master_key.pem -inpass str -id str [-out pem] -outpass str";
  17. int sm9keygen_main(int argc, char **argv)
  18. {
  19. int ret = -1;
  20. char *prog = argv[0];
  21. char *alg = NULL;
  22. char *infile = NULL;
  23. char *inpass = NULL;
  24. char *id = NULL;
  25. char *outfile = NULL;
  26. char *outpass = NULL;
  27. int oid = 0;
  28. FILE *infp = stdin;
  29. FILE *outfp = stdout;
  30. SM9_SIGN_MASTER_KEY sign_msk;
  31. SM9_ENC_MASTER_KEY enc_msk;
  32. SM9_SIGN_KEY sign_key;
  33. SM9_ENC_KEY enc_key;
  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. fprintf(stdout, "usage: %s %s\n", prog, options);
  43. return 0;
  44. } else if (!strcmp(*argv, "-alg")) {
  45. if (--argc < 1) goto bad;
  46. alg = *(++argv);
  47. if ((oid = sm9_oid_from_name(alg)) < 1) {
  48. fprintf(stdout, "%s: invalid alg '%s', should be sm9sign or sm9encrypt\n", prog, alg);
  49. goto end;
  50. }
  51. } else if (!strcmp(*argv, "-in")) {
  52. if (--argc < 1) goto bad;
  53. infile = *(++argv);
  54. if (!(infp = fopen(infile, "rb"))) {
  55. error_print();
  56. goto end;
  57. }
  58. } else if (!strcmp(*argv, "-inpass")) {
  59. if (--argc < 1) goto bad;
  60. inpass = *(++argv);
  61. } else if (!strcmp(*argv, "-id")) {
  62. if (--argc < 1) goto bad;
  63. id = *(++argv);
  64. } else if (!strcmp(*argv, "-out")) {
  65. if (--argc < 1) goto bad;
  66. outfile = *(++argv);
  67. if (!(outfp = fopen(outfile, "wb"))) {
  68. error_print();
  69. goto end;
  70. }
  71. } else if (!strcmp(*argv, "-outpass")) {
  72. if (--argc < 1) goto bad;
  73. outpass = *(++argv);
  74. } else {
  75. bad:
  76. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  77. return 1;
  78. }
  79. argc--;
  80. argv++;
  81. }
  82. if (!id) {
  83. fprintf(stderr, "%s: option '-id' is required\n", prog);
  84. goto end;
  85. }
  86. if (!inpass || !outpass) {
  87. error_print();
  88. goto end;
  89. }
  90. switch (oid) {
  91. case OID_sm9sign:
  92. if (sm9_sign_master_key_info_decrypt_from_pem(&sign_msk, inpass, infp) != 1
  93. || sm9_sign_master_key_extract_key(&sign_msk, id, strlen(id), &sign_key) != 1
  94. || sm9_sign_key_info_encrypt_to_pem(&sign_key, outpass, outfp) != 1) {
  95. error_print();
  96. goto end;
  97. }
  98. break;
  99. case OID_sm9encrypt:
  100. if (sm9_enc_master_key_info_decrypt_from_pem(&enc_msk, inpass, infp) != 1
  101. || sm9_enc_master_key_extract_key(&enc_msk, id, strlen(id), &enc_key) != 1
  102. || sm9_enc_key_info_encrypt_to_pem(&enc_key, outpass, outfp) != 1) {
  103. error_print();
  104. goto end;
  105. }
  106. break;
  107. default:
  108. error_print();
  109. goto end;
  110. }
  111. ret = 0;
  112. end:
  113. gmssl_secure_clear(&sign_msk, sizeof(sign_msk));
  114. gmssl_secure_clear(&enc_msk, sizeof(enc_msk));
  115. gmssl_secure_clear(&sign_key, sizeof(sign_key));
  116. gmssl_secure_clear(&enc_key, sizeof(enc_key));
  117. if (infile && infp) fclose(infp);
  118. if (outfile && outfp) fclose(outfp);
  119. return 1;
  120. }