pkeyparam.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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(in, NULL);
  94. if (pkey == NULL) {
  95. BIO_printf(bio_err, "Error reading parameters\n");
  96. ERR_print_errors(bio_err);
  97. goto end;
  98. }
  99. if (check) {
  100. ctx = EVP_PKEY_CTX_new(pkey, e);
  101. if (ctx == NULL) {
  102. ERR_print_errors(bio_err);
  103. goto end;
  104. }
  105. r = EVP_PKEY_param_check(ctx);
  106. if (r == 1) {
  107. BIO_printf(out, "Parameters are valid\n");
  108. } else {
  109. /*
  110. * Note: at least for RSA keys if this function returns
  111. * -1, there will be no error reasons.
  112. */
  113. BIO_printf(bio_err, "Parameters are invalid\n");
  114. ERR_print_errors(bio_err);
  115. goto end;
  116. }
  117. }
  118. if (!noout)
  119. PEM_write_bio_Parameters(out, pkey);
  120. if (text)
  121. EVP_PKEY_print_params(out, pkey, 0, NULL);
  122. ret = EXIT_SUCCESS;
  123. end:
  124. EVP_PKEY_CTX_free(ctx);
  125. EVP_PKEY_free(pkey);
  126. release_engine(e);
  127. BIO_free_all(out);
  128. BIO_free(in);
  129. return ret;
  130. }