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
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include "apps.h"
14 #include "progs.h"
15 #include <openssl/pem.h>
16 #include <openssl/err.h>
17 #include <openssl/evp.h>
18
19 typedef enum OPTION_choice {
20 OPT_COMMON,
21 OPT_IN, OPT_OUT, OPT_TEXT, OPT_NOOUT,
22 OPT_ENGINE, OPT_CHECK,
23 OPT_PROV_ENUM
24 } OPTION_CHOICE;
25
26 const OPTIONS pkeyparam_options[] = {
27 OPT_SECTION("General"),
28 {"help", OPT_HELP, '-', "Display this summary"},
29 #ifndef OPENSSL_NO_ENGINE
30 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
31 #endif
32 {"check", OPT_CHECK, '-', "Check key param consistency"},
33
34 OPT_SECTION("Input"),
35 {"in", OPT_IN, '<', "Input file"},
36
37 OPT_SECTION("Output"),
38 {"out", OPT_OUT, '>', "Output file"},
39 {"text", OPT_TEXT, '-', "Print parameters as text"},
40 {"noout", OPT_NOOUT, '-', "Don't output encoded parameters"},
41
42 OPT_PROV_OPTIONS,
43 {NULL}
44 };
45
pkeyparam_main(int argc,char ** argv)46 int pkeyparam_main(int argc, char **argv)
47 {
48 ENGINE *e = NULL;
49 BIO *in = NULL, *out = NULL;
50 EVP_PKEY *pkey = NULL;
51 EVP_PKEY_CTX *ctx = NULL;
52 int text = 0, noout = 0, ret = EXIT_FAILURE, check = 0, r;
53 OPTION_CHOICE o;
54 char *infile = NULL, *outfile = NULL, *prog;
55
56 prog = opt_init(argc, argv, pkeyparam_options);
57 while ((o = opt_next()) != OPT_EOF) {
58 switch (o) {
59 case OPT_EOF:
60 case OPT_ERR:
61 opthelp:
62 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
63 goto end;
64 case OPT_HELP:
65 opt_help(pkeyparam_options);
66 ret = 0;
67 goto end;
68 case OPT_IN:
69 infile = opt_arg();
70 break;
71 case OPT_OUT:
72 outfile = opt_arg();
73 break;
74 case OPT_ENGINE:
75 e = setup_engine(opt_arg(), 0);
76 break;
77 case OPT_TEXT:
78 text = 1;
79 break;
80 case OPT_NOOUT:
81 noout = 1;
82 break;
83 case OPT_CHECK:
84 check = 1;
85 break;
86 case OPT_PROV_CASES:
87 if (!opt_provider(o))
88 goto end;
89 break;
90 }
91 }
92
93 /* No extra arguments. */
94 if (!opt_check_rest_arg(NULL))
95 goto opthelp;
96
97 in = bio_open_default(infile, 'r', FORMAT_PEM);
98 if (in == NULL)
99 goto end;
100 pkey = PEM_read_bio_Parameters_ex(in, NULL, app_get0_libctx(),
101 app_get0_propq());
102 if (pkey == NULL) {
103 BIO_printf(bio_err, "Error reading parameters\n");
104 ERR_print_errors(bio_err);
105 goto end;
106 }
107 out = bio_open_default(outfile, 'w', FORMAT_PEM);
108 if (out == NULL)
109 goto end;
110
111 if (check) {
112 if (e == NULL)
113 ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), pkey,
114 app_get0_propq());
115 else
116 ctx = EVP_PKEY_CTX_new(pkey, e);
117 if (ctx == NULL) {
118 ERR_print_errors(bio_err);
119 goto end;
120 }
121
122 r = EVP_PKEY_param_check(ctx);
123
124 if (r == 1) {
125 BIO_printf(out, "Parameters are valid\n");
126 } else {
127 /*
128 * Note: at least for RSA keys if this function returns
129 * -1, there will be no error reasons.
130 */
131 BIO_printf(bio_err, "Parameters are invalid\n");
132 ERR_print_errors(bio_err);
133 goto end;
134 }
135 }
136
137 if (!noout)
138 PEM_write_bio_Parameters(out, pkey);
139
140 if (text)
141 EVP_PKEY_print_params(out, pkey, 0, NULL);
142
143 ret = EXIT_SUCCESS;
144
145 end:
146 EVP_PKEY_CTX_free(ctx);
147 EVP_PKEY_free(pkey);
148 release_engine(e);
149 BIO_free_all(out);
150 BIO_free(in);
151
152 return ret;
153 }
154