]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
8020d79b | 2 | * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved. |
3e4585c8 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
3e4585c8 | 8 | */ |
846e33c7 | 9 | |
3e4585c8 DSH |
10 | #include <stdio.h> |
11 | #include <string.h> | |
d1eec097 | 12 | #include <stdlib.h> |
3e4585c8 | 13 | #include "apps.h" |
dab2cd68 | 14 | #include "progs.h" |
3e4585c8 DSH |
15 | #include <openssl/pem.h> |
16 | #include <openssl/err.h> | |
17 | #include <openssl/evp.h> | |
18 | ||
7e1b7485 RS |
19 | typedef enum OPTION_choice { |
20 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
b0004708 | 21 | OPT_IN, OPT_OUT, OPT_TEXT, OPT_NOOUT, |
6bd4e3f2 P |
22 | OPT_ENGINE, OPT_CHECK, |
23 | OPT_PROV_ENUM | |
7e1b7485 RS |
24 | } OPTION_CHOICE; |
25 | ||
44c83ebd | 26 | const OPTIONS pkeyparam_options[] = { |
5388f986 | 27 | OPT_SECTION("General"), |
7e1b7485 | 28 | {"help", OPT_HELP, '-', "Display this summary"}, |
7e1b7485 RS |
29 | #ifndef OPENSSL_NO_ENGINE |
30 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
31 | #endif | |
b0004708 | 32 | {"check", OPT_CHECK, '-', "Check key param consistency"}, |
5388f986 RS |
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"}, | |
6bd4e3f2 P |
41 | |
42 | OPT_PROV_OPTIONS, | |
7e1b7485 RS |
43 | {NULL} |
44 | }; | |
3e4585c8 | 45 | |
7e1b7485 | 46 | int pkeyparam_main(int argc, char **argv) |
0f113f3e | 47 | { |
dd1abd44 | 48 | ENGINE *e = NULL; |
0f113f3e | 49 | BIO *in = NULL, *out = NULL; |
0f113f3e | 50 | EVP_PKEY *pkey = NULL; |
d1eec097 P |
51 | EVP_PKEY_CTX *ctx = NULL; |
52 | int text = 0, noout = 0, ret = EXIT_FAILURE, check = 0, r; | |
7e1b7485 | 53 | OPTION_CHOICE o; |
333b070e | 54 | char *infile = NULL, *outfile = NULL, *prog; |
7e1b7485 RS |
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: | |
03358517 | 61 | opthelp: |
7e1b7485 RS |
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: | |
dd1abd44 | 75 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
76 | break; |
77 | case OPT_TEXT: | |
0f113f3e | 78 | text = 1; |
7e1b7485 RS |
79 | break; |
80 | case OPT_NOOUT: | |
0f113f3e | 81 | noout = 1; |
7e1b7485 | 82 | break; |
b0004708 PY |
83 | case OPT_CHECK: |
84 | check = 1; | |
85 | break; | |
6bd4e3f2 P |
86 | case OPT_PROV_CASES: |
87 | if (!opt_provider(o)) | |
88 | goto end; | |
89 | break; | |
7e1b7485 | 90 | } |
0f113f3e | 91 | } |
021410ea RS |
92 | |
93 | /* No extra arguments. */ | |
7e1b7485 | 94 | argc = opt_num_rest(); |
03358517 KR |
95 | if (argc != 0) |
96 | goto opthelp; | |
3e4585c8 | 97 | |
bdd58d98 | 98 | in = bio_open_default(infile, 'r', FORMAT_PEM); |
7e1b7485 RS |
99 | if (in == NULL) |
100 | goto end; | |
bdd58d98 | 101 | out = bio_open_default(outfile, 'w', FORMAT_PEM); |
7e1b7485 RS |
102 | if (out == NULL) |
103 | goto end; | |
0f113f3e | 104 | pkey = PEM_read_bio_Parameters(in, NULL); |
2234212c | 105 | if (pkey == NULL) { |
0f113f3e MC |
106 | BIO_printf(bio_err, "Error reading parameters\n"); |
107 | ERR_print_errors(bio_err); | |
108 | goto end; | |
109 | } | |
3e4585c8 | 110 | |
b0004708 | 111 | if (check) { |
b0004708 PY |
112 | ctx = EVP_PKEY_CTX_new(pkey, e); |
113 | if (ctx == NULL) { | |
114 | ERR_print_errors(bio_err); | |
115 | goto end; | |
116 | } | |
117 | ||
118 | r = EVP_PKEY_param_check(ctx); | |
119 | ||
120 | if (r == 1) { | |
121 | BIO_printf(out, "Parameters are valid\n"); | |
122 | } else { | |
123 | /* | |
124 | * Note: at least for RSA keys if this function returns | |
125 | * -1, there will be no error reasons. | |
126 | */ | |
7f90026b DDO |
127 | BIO_printf(bio_err, "Parameters are invalid\n"); |
128 | ERR_print_errors(bio_err); | |
d1eec097 | 129 | goto end; |
b0004708 | 130 | } |
b0004708 PY |
131 | } |
132 | ||
0f113f3e MC |
133 | if (!noout) |
134 | PEM_write_bio_Parameters(out, pkey); | |
3e4585c8 | 135 | |
0f113f3e MC |
136 | if (text) |
137 | EVP_PKEY_print_params(out, pkey, 0, NULL); | |
3e4585c8 | 138 | |
d1eec097 | 139 | ret = EXIT_SUCCESS; |
3e4585c8 | 140 | |
0f113f3e | 141 | end: |
d1eec097 | 142 | EVP_PKEY_CTX_free(ctx); |
0f113f3e | 143 | EVP_PKEY_free(pkey); |
dd1abd44 | 144 | release_engine(e); |
0f113f3e MC |
145 | BIO_free_all(out); |
146 | BIO_free(in); | |
3e4585c8 | 147 | |
0f113f3e MC |
148 | return ret; |
149 | } |