]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/pkeyparam.c
5521909d99bbcd0ac067bdc45105b317e7016e90
[thirdparty/openssl.git] / apps / pkeyparam.c
1 /*
2 * Copyright 2006-2018 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 } OPTION_CHOICE;
24
25 const OPTIONS pkeyparam_options[] = {
26 OPT_SECTION("General"),
27 {"help", OPT_HELP, '-', "Display this summary"},
28 #ifndef OPENSSL_NO_ENGINE
29 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
30 #endif
31 {"check", OPT_CHECK, '-', "Check key param consistency"},
32
33 OPT_SECTION("Input"),
34 {"in", OPT_IN, '<', "Input file"},
35
36 OPT_SECTION("Output"),
37 {"out", OPT_OUT, '>', "Output file"},
38 {"text", OPT_TEXT, '-', "Print parameters as text"},
39 {"noout", OPT_NOOUT, '-', "Don't output encoded parameters"},
40 {NULL}
41 };
42
43 int pkeyparam_main(int argc, char **argv)
44 {
45 ENGINE *e = NULL;
46 BIO *in = NULL, *out = NULL;
47 EVP_PKEY *pkey = NULL;
48 EVP_PKEY_CTX *ctx = NULL;
49 int text = 0, noout = 0, ret = EXIT_FAILURE, check = 0, r;
50 OPTION_CHOICE o;
51 char *infile = NULL, *outfile = NULL, *prog;
52 unsigned long err;
53
54 prog = opt_init(argc, argv, pkeyparam_options);
55 while ((o = opt_next()) != OPT_EOF) {
56 switch (o) {
57 case OPT_EOF:
58 case OPT_ERR:
59 opthelp:
60 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
61 goto end;
62 case OPT_HELP:
63 opt_help(pkeyparam_options);
64 ret = 0;
65 goto end;
66 case OPT_IN:
67 infile = opt_arg();
68 break;
69 case OPT_OUT:
70 outfile = opt_arg();
71 break;
72 case OPT_ENGINE:
73 e = setup_engine(opt_arg(), 0);
74 break;
75 case OPT_TEXT:
76 text = 1;
77 break;
78 case OPT_NOOUT:
79 noout = 1;
80 break;
81 case OPT_CHECK:
82 check = 1;
83 break;
84 }
85 }
86 argc = opt_num_rest();
87 if (argc != 0)
88 goto opthelp;
89
90 in = bio_open_default(infile, 'r', FORMAT_PEM);
91 if (in == NULL)
92 goto end;
93 out = bio_open_default(outfile, 'w', FORMAT_PEM);
94 if (out == NULL)
95 goto end;
96 pkey = PEM_read_bio_Parameters(in, NULL);
97 if (pkey == NULL) {
98 BIO_printf(bio_err, "Error reading parameters\n");
99 ERR_print_errors(bio_err);
100 goto end;
101 }
102
103 if (check) {
104 ctx = EVP_PKEY_CTX_new(pkey, e);
105 if (ctx == NULL) {
106 ERR_print_errors(bio_err);
107 goto end;
108 }
109
110 r = EVP_PKEY_param_check(ctx);
111
112 if (r == 1) {
113 BIO_printf(out, "Parameters are valid\n");
114 } else {
115 /*
116 * Note: at least for RSA keys if this function returns
117 * -1, there will be no error reasons.
118 */
119 BIO_printf(out, "Parameters are invalid\n");
120
121 while ((err = ERR_peek_error()) != 0) {
122 BIO_printf(out, "Detailed error: %s\n",
123 ERR_reason_error_string(err));
124 ERR_get_error(); /* remove err from error stack */
125 }
126 goto end;
127 }
128 }
129
130 if (!noout)
131 PEM_write_bio_Parameters(out, pkey);
132
133 if (text)
134 EVP_PKEY_print_params(out, pkey, 0, NULL);
135
136 ret = EXIT_SUCCESS;
137
138 end:
139 EVP_PKEY_CTX_free(ctx);
140 EVP_PKEY_free(pkey);
141 release_engine(e);
142 BIO_free_all(out);
143 BIO_free(in);
144
145 return ret;
146 }