]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/pkeyparam.c
Check non-option arguments
[thirdparty/openssl.git] / apps / pkeyparam.c
1 /*
2 * Copyright 2006-2020 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_ERR = -1, OPT_EOF = 0, OPT_HELP,
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
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 unsigned long err;
56
57 prog = opt_init(argc, argv, pkeyparam_options);
58 while ((o = opt_next()) != OPT_EOF) {
59 switch (o) {
60 case OPT_EOF:
61 case OPT_ERR:
62 opthelp:
63 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
64 goto end;
65 case OPT_HELP:
66 opt_help(pkeyparam_options);
67 ret = 0;
68 goto end;
69 case OPT_IN:
70 infile = opt_arg();
71 break;
72 case OPT_OUT:
73 outfile = opt_arg();
74 break;
75 case OPT_ENGINE:
76 e = setup_engine(opt_arg(), 0);
77 break;
78 case OPT_TEXT:
79 text = 1;
80 break;
81 case OPT_NOOUT:
82 noout = 1;
83 break;
84 case OPT_CHECK:
85 check = 1;
86 break;
87 case OPT_PROV_CASES:
88 if (!opt_provider(o))
89 goto end;
90 break;
91 }
92 }
93
94 /* No extra arguments. */
95 argc = opt_num_rest();
96 if (argc != 0)
97 goto opthelp;
98
99 in = bio_open_default(infile, 'r', FORMAT_PEM);
100 if (in == NULL)
101 goto end;
102 out = bio_open_default(outfile, 'w', FORMAT_PEM);
103 if (out == NULL)
104 goto end;
105 pkey = PEM_read_bio_Parameters(in, NULL);
106 if (pkey == NULL) {
107 BIO_printf(bio_err, "Error reading parameters\n");
108 ERR_print_errors(bio_err);
109 goto end;
110 }
111
112 if (check) {
113 ctx = EVP_PKEY_CTX_new(pkey, e);
114 if (ctx == NULL) {
115 ERR_print_errors(bio_err);
116 goto end;
117 }
118
119 r = EVP_PKEY_param_check(ctx);
120
121 if (r == 1) {
122 BIO_printf(out, "Parameters are valid\n");
123 } else {
124 /*
125 * Note: at least for RSA keys if this function returns
126 * -1, there will be no error reasons.
127 */
128 BIO_printf(out, "Parameters are invalid\n");
129
130 while ((err = ERR_peek_error()) != 0) {
131 BIO_printf(out, "Detailed error: %s\n",
132 ERR_reason_error_string(err));
133 ERR_get_error(); /* remove err from error stack */
134 }
135 goto end;
136 }
137 }
138
139 if (!noout)
140 PEM_write_bio_Parameters(out, pkey);
141
142 if (text)
143 EVP_PKEY_print_params(out, pkey, 0, NULL);
144
145 ret = EXIT_SUCCESS;
146
147 end:
148 EVP_PKEY_CTX_free(ctx);
149 EVP_PKEY_free(pkey);
150 release_engine(e);
151 BIO_free_all(out);
152 BIO_free(in);
153
154 return ret;
155 }