1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | #ifdef __rtems__ |
---|
3 | #include <machine/rtems-bsd-program.h> |
---|
4 | #include "rtems-bsd-openssl-namespace.h" |
---|
5 | #endif /* __rtems__ */ |
---|
6 | |
---|
7 | /* |
---|
8 | * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved. |
---|
9 | * |
---|
10 | * Licensed under the OpenSSL license (the "License"). You may not use |
---|
11 | * this file except in compliance with the License. You can obtain a copy |
---|
12 | * in the file LICENSE in the source distribution or at |
---|
13 | * https://www.openssl.org/source/license.html |
---|
14 | */ |
---|
15 | |
---|
16 | #include <stdio.h> |
---|
17 | #include <string.h> |
---|
18 | #include "apps.h" |
---|
19 | #include "progs.h" |
---|
20 | #include <openssl/pem.h> |
---|
21 | #include <openssl/err.h> |
---|
22 | #include <openssl/evp.h> |
---|
23 | |
---|
24 | typedef enum OPTION_choice { |
---|
25 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, |
---|
26 | OPT_INFORM, OPT_OUTFORM, OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE, |
---|
27 | OPT_IN, OPT_OUT, OPT_PUBIN, OPT_PUBOUT, OPT_TEXT_PUB, |
---|
28 | OPT_TEXT, OPT_NOOUT, OPT_MD, OPT_TRADITIONAL, OPT_CHECK, OPT_PUB_CHECK |
---|
29 | } OPTION_CHOICE; |
---|
30 | |
---|
31 | const OPTIONS pkey_options[] = { |
---|
32 | {"help", OPT_HELP, '-', "Display this summary"}, |
---|
33 | {"inform", OPT_INFORM, 'f', "Input format (DER or PEM)"}, |
---|
34 | {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"}, |
---|
35 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, |
---|
36 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, |
---|
37 | {"in", OPT_IN, 's', "Input key"}, |
---|
38 | {"out", OPT_OUT, '>', "Output file"}, |
---|
39 | {"pubin", OPT_PUBIN, '-', |
---|
40 | "Read public key from input (default is private key)"}, |
---|
41 | {"pubout", OPT_PUBOUT, '-', "Output public key, not private"}, |
---|
42 | {"text_pub", OPT_TEXT_PUB, '-', "Only output public key components"}, |
---|
43 | {"text", OPT_TEXT, '-', "Output in plaintext as well"}, |
---|
44 | {"noout", OPT_NOOUT, '-', "Don't output the key"}, |
---|
45 | {"", OPT_MD, '-', "Any supported cipher"}, |
---|
46 | {"traditional", OPT_TRADITIONAL, '-', |
---|
47 | "Use traditional format for private keys"}, |
---|
48 | #ifndef OPENSSL_NO_ENGINE |
---|
49 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
---|
50 | #endif |
---|
51 | {"check", OPT_CHECK, '-', "Check key consistency"}, |
---|
52 | {"pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency"}, |
---|
53 | {NULL} |
---|
54 | }; |
---|
55 | |
---|
56 | int pkey_main(int argc, char **argv) |
---|
57 | { |
---|
58 | BIO *in = NULL, *out = NULL; |
---|
59 | ENGINE *e = NULL; |
---|
60 | EVP_PKEY *pkey = NULL; |
---|
61 | const EVP_CIPHER *cipher = NULL; |
---|
62 | char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL; |
---|
63 | char *passinarg = NULL, *passoutarg = NULL, *prog; |
---|
64 | OPTION_CHOICE o; |
---|
65 | int informat = FORMAT_PEM, outformat = FORMAT_PEM; |
---|
66 | int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1; |
---|
67 | int private = 0, traditional = 0, check = 0, pub_check = 0; |
---|
68 | |
---|
69 | prog = opt_init(argc, argv, pkey_options); |
---|
70 | while ((o = opt_next()) != OPT_EOF) { |
---|
71 | switch (o) { |
---|
72 | case OPT_EOF: |
---|
73 | case OPT_ERR: |
---|
74 | opthelp: |
---|
75 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
---|
76 | goto end; |
---|
77 | case OPT_HELP: |
---|
78 | opt_help(pkey_options); |
---|
79 | ret = 0; |
---|
80 | goto end; |
---|
81 | case OPT_INFORM: |
---|
82 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat)) |
---|
83 | goto opthelp; |
---|
84 | break; |
---|
85 | case OPT_OUTFORM: |
---|
86 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) |
---|
87 | goto opthelp; |
---|
88 | break; |
---|
89 | case OPT_PASSIN: |
---|
90 | passinarg = opt_arg(); |
---|
91 | break; |
---|
92 | case OPT_PASSOUT: |
---|
93 | passoutarg = opt_arg(); |
---|
94 | break; |
---|
95 | case OPT_ENGINE: |
---|
96 | e = setup_engine(opt_arg(), 0); |
---|
97 | break; |
---|
98 | case OPT_IN: |
---|
99 | infile = opt_arg(); |
---|
100 | break; |
---|
101 | case OPT_OUT: |
---|
102 | outfile = opt_arg(); |
---|
103 | break; |
---|
104 | case OPT_PUBIN: |
---|
105 | pubin = pubout = pubtext = 1; |
---|
106 | break; |
---|
107 | case OPT_PUBOUT: |
---|
108 | pubout = 1; |
---|
109 | break; |
---|
110 | case OPT_TEXT_PUB: |
---|
111 | pubtext = text = 1; |
---|
112 | break; |
---|
113 | case OPT_TEXT: |
---|
114 | text = 1; |
---|
115 | break; |
---|
116 | case OPT_NOOUT: |
---|
117 | noout = 1; |
---|
118 | break; |
---|
119 | case OPT_TRADITIONAL: |
---|
120 | traditional = 1; |
---|
121 | break; |
---|
122 | case OPT_CHECK: |
---|
123 | check = 1; |
---|
124 | break; |
---|
125 | case OPT_PUB_CHECK: |
---|
126 | pub_check = 1; |
---|
127 | break; |
---|
128 | case OPT_MD: |
---|
129 | if (!opt_cipher(opt_unknown(), &cipher)) |
---|
130 | goto opthelp; |
---|
131 | } |
---|
132 | } |
---|
133 | argc = opt_num_rest(); |
---|
134 | if (argc != 0) |
---|
135 | goto opthelp; |
---|
136 | |
---|
137 | private = !noout && !pubout ? 1 : 0; |
---|
138 | if (text && !pubtext) |
---|
139 | private = 1; |
---|
140 | |
---|
141 | if (!app_passwd(passinarg, passoutarg, &passin, &passout)) { |
---|
142 | BIO_printf(bio_err, "Error getting passwords\n"); |
---|
143 | goto end; |
---|
144 | } |
---|
145 | |
---|
146 | out = bio_open_owner(outfile, outformat, private); |
---|
147 | if (out == NULL) |
---|
148 | goto end; |
---|
149 | |
---|
150 | if (pubin) |
---|
151 | pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key"); |
---|
152 | else |
---|
153 | pkey = load_key(infile, informat, 1, passin, e, "key"); |
---|
154 | if (pkey == NULL) |
---|
155 | goto end; |
---|
156 | |
---|
157 | if (check || pub_check) { |
---|
158 | int r; |
---|
159 | EVP_PKEY_CTX *ctx; |
---|
160 | |
---|
161 | ctx = EVP_PKEY_CTX_new(pkey, e); |
---|
162 | if (ctx == NULL) { |
---|
163 | ERR_print_errors(bio_err); |
---|
164 | goto end; |
---|
165 | } |
---|
166 | |
---|
167 | if (check) |
---|
168 | r = EVP_PKEY_check(ctx); |
---|
169 | else |
---|
170 | r = EVP_PKEY_public_check(ctx); |
---|
171 | |
---|
172 | if (r == 1) { |
---|
173 | BIO_printf(out, "Key is valid\n"); |
---|
174 | } else { |
---|
175 | /* |
---|
176 | * Note: at least for RSA keys if this function returns |
---|
177 | * -1, there will be no error reasons. |
---|
178 | */ |
---|
179 | unsigned long err; |
---|
180 | |
---|
181 | BIO_printf(out, "Key is invalid\n"); |
---|
182 | |
---|
183 | while ((err = ERR_peek_error()) != 0) { |
---|
184 | BIO_printf(out, "Detailed error: %s\n", |
---|
185 | ERR_reason_error_string(err)); |
---|
186 | ERR_get_error(); /* remove err from error stack */ |
---|
187 | } |
---|
188 | } |
---|
189 | EVP_PKEY_CTX_free(ctx); |
---|
190 | } |
---|
191 | |
---|
192 | if (!noout) { |
---|
193 | if (outformat == FORMAT_PEM) { |
---|
194 | if (pubout) { |
---|
195 | if (!PEM_write_bio_PUBKEY(out, pkey)) |
---|
196 | goto end; |
---|
197 | } else { |
---|
198 | assert(private); |
---|
199 | if (traditional) { |
---|
200 | if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher, |
---|
201 | NULL, 0, NULL, |
---|
202 | passout)) |
---|
203 | goto end; |
---|
204 | } else { |
---|
205 | if (!PEM_write_bio_PrivateKey(out, pkey, cipher, |
---|
206 | NULL, 0, NULL, passout)) |
---|
207 | goto end; |
---|
208 | } |
---|
209 | } |
---|
210 | } else if (outformat == FORMAT_ASN1) { |
---|
211 | if (pubout) { |
---|
212 | if (!i2d_PUBKEY_bio(out, pkey)) |
---|
213 | goto end; |
---|
214 | } else { |
---|
215 | assert(private); |
---|
216 | if (!i2d_PrivateKey_bio(out, pkey)) |
---|
217 | goto end; |
---|
218 | } |
---|
219 | } else { |
---|
220 | BIO_printf(bio_err, "Bad format specified for key\n"); |
---|
221 | goto end; |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | if (text) { |
---|
226 | if (pubtext) { |
---|
227 | if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0) |
---|
228 | goto end; |
---|
229 | } else { |
---|
230 | assert(private); |
---|
231 | if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0) |
---|
232 | goto end; |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | ret = 0; |
---|
237 | |
---|
238 | end: |
---|
239 | if (ret != 0) |
---|
240 | ERR_print_errors(bio_err); |
---|
241 | EVP_PKEY_free(pkey); |
---|
242 | release_engine(e); |
---|
243 | BIO_free_all(out); |
---|
244 | BIO_free(in); |
---|
245 | OPENSSL_free(passin); |
---|
246 | OPENSSL_free(passout); |
---|
247 | |
---|
248 | return ret; |
---|
249 | } |
---|
250 | #ifdef __rtems__ |
---|
251 | #include "rtems-bsd-openssl-pkey-data.h" |
---|
252 | #endif /* __rtems__ */ |
---|