pkeyparam.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include "apps.h"
  13. #include "progs.h"
  14. #include <openssl/pem.h>
  15. #include <openssl/err.h>
  16. #include <openssl/evp.h>
  17. typedef enum OPTION_choice {
  18. OPT_COMMON,
  19. OPT_IN, OPT_OUT, OPT_TEXT, OPT_NOOUT,
  20. OPT_ENGINE, OPT_CHECK,
  21. OPT_PROV_ENUM
  22. } OPTION_CHOICE;
  23. const OPTIONS pkeyparam_options[] = {
  24. OPT_SECTION("General"),
  25. {"help", OPT_HELP, '-', "Display this summary"},
  26. #ifndef OPENSSL_NO_ENGINE
  27. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  28. #endif
  29. {"check", OPT_CHECK, '-', "Check key param consistency"},
  30. OPT_SECTION("Input"),
  31. {"in", OPT_IN, '<', "Input file"},
  32. OPT_SECTION("Output"),
  33. {"out", OPT_OUT, '>', "Output file"},
  34. {"text", OPT_TEXT, '-', "Print parameters as text"},
  35. {"noout", OPT_NOOUT, '-', "Don't output encoded parameters"},
  36. OPT_PROV_OPTIONS,
  37. {NULL}
  38. };
  39. int pkeyparam_main(int argc, char **argv)
  40. {
  41. ENGINE *e = NULL;
  42. BIO *in = NULL, *out = NULL;
  43. EVP_PKEY *pkey = NULL;
  44. EVP_PKEY_CTX *ctx = NULL;
  45. int text = 0, noout = 0, ret = EXIT_FAILURE, check = 0, r;
  46. OPTION_CHOICE o;
  47. char *infile = NULL, *outfile = NULL, *prog;
  48. prog = opt_init(argc, argv, pkeyparam_options);
  49. while ((o = opt_next()) != OPT_EOF) {
  50. switch (o) {
  51. case OPT_EOF:
  52. case OPT_ERR:
  53. opthelp:
  54. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  55. goto end;
  56. case OPT_HELP:
  57. opt_help(pkeyparam_options);
  58. ret = 0;
  59. goto end;
  60. case OPT_IN:
  61. infile = opt_arg();
  62. break;
  63. case OPT_OUT:
  64. outfile = opt_arg();
  65. break;
  66. case OPT_ENGINE:
  67. e = setup_engine(opt_arg(), 0);
  68. break;
  69. case OPT_TEXT:
  70. text = 1;
  71. break;
  72. case OPT_NOOUT:
  73. noout = 1;
  74. break;
  75. case OPT_CHECK:
  76. check = 1;
  77. break;
  78. case OPT_PROV_CASES:
  79. if (!opt_provider(o))
  80. goto end;
  81. break;
  82. }
  83. }
  84. /* No extra arguments. */
  85. if (!opt_check_rest_arg(NULL))
  86. goto opthelp;
  87. in = bio_open_default(infile, 'r', FORMAT_PEM);
  88. if (in == NULL)
  89. goto end;
  90. out = bio_open_default(outfile, 'w', FORMAT_PEM);
  91. if (out == NULL)
  92. goto end;
  93. pkey = PEM_read_bio_Parameters_ex(in, NULL, app_get0_libctx(),
  94. app_get0_propq());
  95. if (pkey == NULL) {
  96. BIO_printf(bio_err, "Error reading parameters\n");
  97. ERR_print_errors(bio_err);
  98. goto end;
  99. }
  100. if (check) {
  101. if (e == NULL)
  102. ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), pkey,
  103. app_get0_propq());
  104. else
  105. ctx = EVP_PKEY_CTX_new(pkey, e);
  106. if (ctx == NULL) {
  107. ERR_print_errors(bio_err);
  108. goto end;
  109. }
  110. r = EVP_PKEY_param_check(ctx);
  111. if (r == 1) {
  112. BIO_printf(out, "Parameters are valid\n");
  113. } else {
  114. /*
  115. * Note: at least for RSA keys if this function returns
  116. * -1, there will be no error reasons.
  117. */
  118. BIO_printf(bio_err, "Parameters are invalid\n");
  119. ERR_print_errors(bio_err);
  120. goto end;
  121. }
  122. }
  123. if (!noout)
  124. PEM_write_bio_Parameters(out, pkey);
  125. if (text)
  126. EVP_PKEY_print_params(out, pkey, 0, NULL);
  127. ret = EXIT_SUCCESS;
  128. end:
  129. EVP_PKEY_CTX_free(ctx);
  130. EVP_PKEY_free(pkey);
  131. release_engine(e);
  132. BIO_free_all(out);
  133. BIO_free(in);
  134. return ret;
  135. }