pkeyparam.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. 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(bio_err, "Parameters are invalid\n");
  115. ERR_print_errors(bio_err);
  116. goto end;
  117. }
  118. }
  119. if (!noout)
  120. PEM_write_bio_Parameters(out, pkey);
  121. if (text)
  122. EVP_PKEY_print_params(out, pkey, 0, NULL);
  123. ret = EXIT_SUCCESS;
  124. end:
  125. EVP_PKEY_CTX_free(ctx);
  126. EVP_PKEY_free(pkey);
  127. release_engine(e);
  128. BIO_free_all(out);
  129. BIO_free(in);
  130. return ret;
  131. }