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