1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved. |
---|
5 | * |
---|
6 | * Licensed under the OpenSSL license (the "License"). You may not use |
---|
7 | * this file except in compliance with the License. You can obtain a copy |
---|
8 | * in the file LICENSE in the source distribution or at |
---|
9 | * https://www.openssl.org/source/license.html |
---|
10 | */ |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | #include <string.h> |
---|
14 | #include "apps.h" |
---|
15 | #include "progs.h" |
---|
16 | #include <openssl/pem.h> |
---|
17 | #include <openssl/err.h> |
---|
18 | #include <openssl/evp.h> |
---|
19 | |
---|
20 | typedef enum OPTION_choice { |
---|
21 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, |
---|
22 | OPT_IN, OPT_OUT, OPT_TEXT, OPT_NOOUT, |
---|
23 | OPT_ENGINE, OPT_CHECK |
---|
24 | } OPTION_CHOICE; |
---|
25 | |
---|
26 | const OPTIONS pkeyparam_options[] = { |
---|
27 | {"help", OPT_HELP, '-', "Display this summary"}, |
---|
28 | {"in", OPT_IN, '<', "Input file"}, |
---|
29 | {"out", OPT_OUT, '>', "Output file"}, |
---|
30 | {"text", OPT_TEXT, '-', "Print parameters as text"}, |
---|
31 | {"noout", OPT_NOOUT, '-', "Don't output encoded parameters"}, |
---|
32 | #ifndef OPENSSL_NO_ENGINE |
---|
33 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
---|
34 | #endif |
---|
35 | {"check", OPT_CHECK, '-', "Check key param consistency"}, |
---|
36 | {NULL} |
---|
37 | }; |
---|
38 | |
---|
39 | int pkeyparam_main(int argc, char **argv) |
---|
40 | { |
---|
41 | ENGINE *e = NULL; |
---|
42 | BIO *in = NULL, *out = NULL; |
---|
43 | EVP_PKEY *pkey = NULL; |
---|
44 | int text = 0, noout = 0, ret = 1, check = 0; |
---|
45 | OPTION_CHOICE o; |
---|
46 | char *infile = NULL, *outfile = NULL, *prog; |
---|
47 | |
---|
48 | prog = opt_init(argc, argv, pkeyparam_options); |
---|
49 | while ((o = opt_next()) != OPT_EOF) { |
---|
50 | switch (o) { |
---|
51 | case OPT_EOF: |
---|
52 | case OPT_ERR: |
---|
53 | opthelp: |
---|
54 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
---|
55 | goto end; |
---|
56 | case OPT_HELP: |
---|
57 | opt_help(pkeyparam_options); |
---|
58 | ret = 0; |
---|
59 | goto end; |
---|
60 | case OPT_IN: |
---|
61 | infile = opt_arg(); |
---|
62 | break; |
---|
63 | case OPT_OUT: |
---|
64 | outfile = opt_arg(); |
---|
65 | break; |
---|
66 | case OPT_ENGINE: |
---|
67 | e = setup_engine(opt_arg(), 0); |
---|
68 | break; |
---|
69 | case OPT_TEXT: |
---|
70 | text = 1; |
---|
71 | break; |
---|
72 | case OPT_NOOUT: |
---|
73 | noout = 1; |
---|
74 | break; |
---|
75 | case OPT_CHECK: |
---|
76 | check = 1; |
---|
77 | break; |
---|
78 | } |
---|
79 | } |
---|
80 | argc = opt_num_rest(); |
---|
81 | if (argc != 0) |
---|
82 | goto opthelp; |
---|
83 | |
---|
84 | in = bio_open_default(infile, 'r', FORMAT_PEM); |
---|
85 | if (in == NULL) |
---|
86 | goto end; |
---|
87 | out = bio_open_default(outfile, 'w', FORMAT_PEM); |
---|
88 | if (out == NULL) |
---|
89 | goto end; |
---|
90 | pkey = PEM_read_bio_Parameters(in, NULL); |
---|
91 | if (pkey == NULL) { |
---|
92 | BIO_printf(bio_err, "Error reading parameters\n"); |
---|
93 | ERR_print_errors(bio_err); |
---|
94 | goto end; |
---|
95 | } |
---|
96 | |
---|
97 | if (check) { |
---|
98 | int r; |
---|
99 | EVP_PKEY_CTX *ctx; |
---|
100 | |
---|
101 | ctx = EVP_PKEY_CTX_new(pkey, e); |
---|
102 | if (ctx == NULL) { |
---|
103 | ERR_print_errors(bio_err); |
---|
104 | goto end; |
---|
105 | } |
---|
106 | |
---|
107 | r = EVP_PKEY_param_check(ctx); |
---|
108 | |
---|
109 | if (r == 1) { |
---|
110 | BIO_printf(out, "Parameters are valid\n"); |
---|
111 | } else { |
---|
112 | /* |
---|
113 | * Note: at least for RSA keys if this function returns |
---|
114 | * -1, there will be no error reasons. |
---|
115 | */ |
---|
116 | unsigned long err; |
---|
117 | |
---|
118 | BIO_printf(out, "Parameters are invalid\n"); |
---|
119 | |
---|
120 | while ((err = ERR_peek_error()) != 0) { |
---|
121 | BIO_printf(out, "Detailed error: %s\n", |
---|
122 | ERR_reason_error_string(err)); |
---|
123 | ERR_get_error(); /* remove err from error stack */ |
---|
124 | } |
---|
125 | } |
---|
126 | EVP_PKEY_CTX_free(ctx); |
---|
127 | } |
---|
128 | |
---|
129 | if (!noout) |
---|
130 | PEM_write_bio_Parameters(out, pkey); |
---|
131 | |
---|
132 | if (text) |
---|
133 | EVP_PKEY_print_params(out, pkey, 0, NULL); |
---|
134 | |
---|
135 | ret = 0; |
---|
136 | |
---|
137 | end: |
---|
138 | EVP_PKEY_free(pkey); |
---|
139 | release_engine(e); |
---|
140 | BIO_free_all(out); |
---|
141 | BIO_free(in); |
---|
142 | |
---|
143 | return ret; |
---|
144 | } |
---|