pkeyparam.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright 2006-2020 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_ERR = -1, OPT_EOF = 0, OPT_HELP,
  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. unsigned long err;
  49. prog = opt_init(argc, argv, pkeyparam_options);
  50. while ((o = opt_next()) != OPT_EOF) {
  51. switch (o) {
  52. case OPT_EOF:
  53. case OPT_ERR:
  54. opthelp:
  55. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  56. goto end;
  57. case OPT_HELP:
  58. opt_help(pkeyparam_options);
  59. ret = 0;
  60. goto end;
  61. case OPT_IN:
  62. infile = opt_arg();
  63. break;
  64. case OPT_OUT:
  65. outfile = opt_arg();
  66. break;
  67. case OPT_ENGINE:
  68. e = setup_engine(opt_arg(), 0);
  69. break;
  70. case OPT_TEXT:
  71. text = 1;
  72. break;
  73. case OPT_NOOUT:
  74. noout = 1;
  75. break;
  76. case OPT_CHECK:
  77. check = 1;
  78. break;
  79. case OPT_PROV_CASES:
  80. if (!opt_provider(o))
  81. goto end;
  82. break;
  83. }
  84. }
  85. /* No extra arguments. */
  86. argc = opt_num_rest();
  87. if (argc != 0)
  88. goto opthelp;
  89. in = bio_open_default(infile, 'r', FORMAT_PEM);
  90. if (in == NULL)
  91. goto end;
  92. out = bio_open_default(outfile, 'w', FORMAT_PEM);
  93. if (out == NULL)
  94. goto end;
  95. pkey = PEM_read_bio_Parameters(in, NULL);
  96. if (pkey == NULL) {
  97. BIO_printf(bio_err, "Error reading parameters\n");
  98. ERR_print_errors(bio_err);
  99. goto end;
  100. }
  101. if (check) {
  102. ctx = EVP_PKEY_CTX_new(pkey, e);
  103. if (ctx == NULL) {
  104. ERR_print_errors(bio_err);
  105. goto end;
  106. }
  107. r = EVP_PKEY_param_check(ctx);
  108. if (r == 1) {
  109. BIO_printf(out, "Parameters are valid\n");
  110. } else {
  111. /*
  112. * Note: at least for RSA keys if this function returns
  113. * -1, there will be no error reasons.
  114. */
  115. BIO_printf(out, "Parameters are invalid\n");
  116. while ((err = ERR_peek_error()) != 0) {
  117. BIO_printf(out, "Detailed error: %s\n",
  118. ERR_reason_error_string(err));
  119. ERR_get_error(); /* remove err from error stack */
  120. }
  121. goto end;
  122. }
  123. }
  124. if (!noout)
  125. PEM_write_bio_Parameters(out, pkey);
  126. if (text)
  127. EVP_PKEY_print_params(out, pkey, 0, NULL);
  128. ret = EXIT_SUCCESS;
  129. end:
  130. EVP_PKEY_CTX_free(ctx);
  131. EVP_PKEY_free(pkey);
  132. release_engine(e);
  133. BIO_free_all(out);
  134. BIO_free(in);
  135. return ret;
  136. }