pkeyparam.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "apps.h"
  12. #include "progs.h"
  13. #include <openssl/pem.h>
  14. #include <openssl/err.h>
  15. #include <openssl/evp.h>
  16. typedef enum OPTION_choice {
  17. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  18. OPT_IN, OPT_OUT, OPT_TEXT, OPT_NOOUT,
  19. OPT_ENGINE, OPT_CHECK
  20. } OPTION_CHOICE;
  21. const OPTIONS pkeyparam_options[] = {
  22. {"help", OPT_HELP, '-', "Display this summary"},
  23. {"in", OPT_IN, '<', "Input file"},
  24. {"out", OPT_OUT, '>', "Output file"},
  25. {"text", OPT_TEXT, '-', "Print parameters as text"},
  26. {"noout", OPT_NOOUT, '-', "Don't output encoded parameters"},
  27. #ifndef OPENSSL_NO_ENGINE
  28. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  29. #endif
  30. {"check", OPT_CHECK, '-', "Check key param consistency"},
  31. {NULL}
  32. };
  33. int pkeyparam_main(int argc, char **argv)
  34. {
  35. ENGINE *e = NULL;
  36. BIO *in = NULL, *out = NULL;
  37. EVP_PKEY *pkey = NULL;
  38. int text = 0, noout = 0, ret = 1, check = 0;
  39. OPTION_CHOICE o;
  40. char *infile = NULL, *outfile = NULL, *prog;
  41. prog = opt_init(argc, argv, pkeyparam_options);
  42. while ((o = opt_next()) != OPT_EOF) {
  43. switch (o) {
  44. case OPT_EOF:
  45. case OPT_ERR:
  46. opthelp:
  47. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  48. goto end;
  49. case OPT_HELP:
  50. opt_help(pkeyparam_options);
  51. ret = 0;
  52. goto end;
  53. case OPT_IN:
  54. infile = opt_arg();
  55. break;
  56. case OPT_OUT:
  57. outfile = opt_arg();
  58. break;
  59. case OPT_ENGINE:
  60. e = setup_engine(opt_arg(), 0);
  61. break;
  62. case OPT_TEXT:
  63. text = 1;
  64. break;
  65. case OPT_NOOUT:
  66. noout = 1;
  67. break;
  68. case OPT_CHECK:
  69. check = 1;
  70. break;
  71. }
  72. }
  73. argc = opt_num_rest();
  74. if (argc != 0)
  75. goto opthelp;
  76. in = bio_open_default(infile, 'r', FORMAT_PEM);
  77. if (in == NULL)
  78. goto end;
  79. out = bio_open_default(outfile, 'w', FORMAT_PEM);
  80. if (out == NULL)
  81. goto end;
  82. pkey = PEM_read_bio_Parameters(in, NULL);
  83. if (pkey == NULL) {
  84. BIO_printf(bio_err, "Error reading parameters\n");
  85. ERR_print_errors(bio_err);
  86. goto end;
  87. }
  88. if (check) {
  89. int r;
  90. EVP_PKEY_CTX *ctx;
  91. ctx = EVP_PKEY_CTX_new(pkey, e);
  92. if (ctx == NULL) {
  93. ERR_print_errors(bio_err);
  94. goto end;
  95. }
  96. r = EVP_PKEY_param_check(ctx);
  97. if (r == 1) {
  98. BIO_printf(out, "Parameters are valid\n");
  99. } else {
  100. /*
  101. * Note: at least for RSA keys if this function returns
  102. * -1, there will be no error reasons.
  103. */
  104. unsigned long err;
  105. BIO_printf(out, "Parameters are invalid\n");
  106. while ((err = ERR_peek_error()) != 0) {
  107. BIO_printf(out, "Detailed error: %s\n",
  108. ERR_reason_error_string(err));
  109. ERR_get_error(); /* remove err from error stack */
  110. }
  111. }
  112. EVP_PKEY_CTX_free(ctx);
  113. }
  114. if (!noout)
  115. PEM_write_bio_Parameters(out, pkey);
  116. if (text)
  117. EVP_PKEY_print_params(out, pkey, 0, NULL);
  118. ret = 0;
  119. end:
  120. EVP_PKEY_free(pkey);
  121. release_engine(e);
  122. BIO_free_all(out);
  123. BIO_free(in);
  124. return ret;
  125. }