pkeyparam.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2006-2016 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 <openssl/pem.h>
  13. #include <openssl/err.h>
  14. #include <openssl/evp.h>
  15. typedef enum OPTION_choice {
  16. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  17. OPT_IN, OPT_OUT, OPT_TEXT, OPT_NOOUT, OPT_ENGINE
  18. } OPTION_CHOICE;
  19. const OPTIONS pkeyparam_options[] = {
  20. {"help", OPT_HELP, '-', "Display this summary"},
  21. {"in", OPT_IN, '<', "Input file"},
  22. {"out", OPT_OUT, '>', "Output file"},
  23. {"text", OPT_TEXT, '-', "Print parameters as text"},
  24. {"noout", OPT_NOOUT, '-', "Don't output encoded parameters"},
  25. #ifndef OPENSSL_NO_ENGINE
  26. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  27. #endif
  28. {NULL}
  29. };
  30. int pkeyparam_main(int argc, char **argv)
  31. {
  32. ENGINE *e = NULL;
  33. BIO *in = NULL, *out = NULL;
  34. EVP_PKEY *pkey = NULL;
  35. int text = 0, noout = 0, ret = 1;
  36. OPTION_CHOICE o;
  37. char *infile = NULL, *outfile = NULL, *prog;
  38. prog = opt_init(argc, argv, pkeyparam_options);
  39. while ((o = opt_next()) != OPT_EOF) {
  40. switch (o) {
  41. case OPT_EOF:
  42. case OPT_ERR:
  43. opthelp:
  44. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  45. goto end;
  46. case OPT_HELP:
  47. opt_help(pkeyparam_options);
  48. ret = 0;
  49. goto end;
  50. case OPT_IN:
  51. infile = opt_arg();
  52. break;
  53. case OPT_OUT:
  54. outfile = opt_arg();
  55. break;
  56. case OPT_ENGINE:
  57. e = setup_engine(opt_arg(), 0);
  58. break;
  59. case OPT_TEXT:
  60. text = 1;
  61. break;
  62. case OPT_NOOUT:
  63. noout = 1;
  64. break;
  65. }
  66. }
  67. argc = opt_num_rest();
  68. if (argc != 0)
  69. goto opthelp;
  70. in = bio_open_default(infile, 'r', FORMAT_PEM);
  71. if (in == NULL)
  72. goto end;
  73. out = bio_open_default(outfile, 'w', FORMAT_PEM);
  74. if (out == NULL)
  75. goto end;
  76. pkey = PEM_read_bio_Parameters(in, NULL);
  77. if (!pkey) {
  78. BIO_printf(bio_err, "Error reading parameters\n");
  79. ERR_print_errors(bio_err);
  80. goto end;
  81. }
  82. if (!noout)
  83. PEM_write_bio_Parameters(out, pkey);
  84. if (text)
  85. EVP_PKEY_print_params(out, pkey, 0, NULL);
  86. ret = 0;
  87. end:
  88. EVP_PKEY_free(pkey);
  89. release_engine(e);
  90. BIO_free_all(out);
  91. BIO_free(in);
  92. return ret;
  93. }