2 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
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
14 #include <openssl/pem.h>
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
18 typedef enum OPTION_choice
{
19 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
20 OPT_IN
, OPT_OUT
, OPT_TEXT
, OPT_NOOUT
,
24 const OPTIONS pkeyparam_options
[] = {
25 {"help", OPT_HELP
, '-', "Display this summary"},
26 {"in", OPT_IN
, '<', "Input file"},
27 {"out", OPT_OUT
, '>', "Output file"},
28 {"text", OPT_TEXT
, '-', "Print parameters as text"},
29 {"noout", OPT_NOOUT
, '-', "Don't output encoded parameters"},
30 #ifndef OPENSSL_NO_ENGINE
31 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
33 {"check", OPT_CHECK
, '-', "Check key param consistency"},
37 int pkeyparam_main(int argc
, char **argv
)
40 BIO
*in
= NULL
, *out
= NULL
;
41 EVP_PKEY
*pkey
= NULL
;
42 int text
= 0, noout
= 0, ret
= 1, check
= 0;
44 char *infile
= NULL
, *outfile
= NULL
, *prog
;
46 prog
= opt_init(argc
, argv
, pkeyparam_options
);
47 while ((o
= opt_next()) != OPT_EOF
) {
52 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
55 opt_help(pkeyparam_options
);
65 e
= setup_engine(opt_arg(), 0);
78 argc
= opt_num_rest();
82 in
= bio_open_default(infile
, 'r', FORMAT_PEM
);
85 out
= bio_open_default(outfile
, 'w', FORMAT_PEM
);
88 pkey
= PEM_read_bio_Parameters(in
, NULL
);
90 BIO_printf(bio_err
, "Error reading parameters\n");
91 ERR_print_errors(bio_err
);
99 ctx
= EVP_PKEY_CTX_new(pkey
, e
);
101 ERR_print_errors(bio_err
);
105 r
= EVP_PKEY_param_check(ctx
);
108 BIO_printf(out
, "Parameters are valid\n");
111 * Note: at least for RSA keys if this function returns
112 * -1, there will be no error reasons.
116 BIO_printf(out
, "Parameters are invalid\n");
118 while ((err
= ERR_peek_error()) != 0) {
119 BIO_printf(out
, "Detailed error: %s\n",
120 ERR_reason_error_string(err
));
121 ERR_get_error(); /* remove err from error stack */
124 EVP_PKEY_CTX_free(ctx
);
128 PEM_write_bio_Parameters(out
, pkey
);
131 EVP_PKEY_print_params(out
, pkey
, 0, NULL
);