2
0

pkeyparam.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. argc = opt_num_rest();
  86. if (argc != 0)
  87. goto opthelp;
  88. in = bio_open_default(infile, 'r', FORMAT_PEM);
  89. if (in == NULL)
  90. goto end;
  91. out = bio_open_default(outfile, 'w', FORMAT_PEM);
  92. if (out == NULL)
  93. goto end;
  94. pkey = PEM_read_bio_Parameters(in, NULL);
  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. ctx = EVP_PKEY_CTX_new(pkey, e);
  102. if (ctx == NULL) {
  103. ERR_print_errors(bio_err);
  104. goto end;
  105. }
  106. r = EVP_PKEY_param_check(ctx);
  107. if (r == 1) {
  108. BIO_printf(out, "Parameters are valid\n");
  109. } else {
  110. /*
  111. * Note: at least for RSA keys if this function returns
  112. * -1, there will be no error reasons.
  113. */
  114. BIO_printf(out, "Parameters are invalid\n");
  115. while ((err = ERR_peek_error()) != 0) {
  116. BIO_printf(out, "Detailed error: %s\n",
  117. ERR_reason_error_string(err));
  118. ERR_get_error(); /* remove err from error stack */
  119. }
  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. }