Annotation of src/usr.bin/openssl/pkey.c, Revision 1.12
1.12 ! inoguchi 1: /* $OpenBSD: pkey.c,v 1.11 2019/02/05 11:26:21 inoguchi Exp $ */
1.1 jsing 2: /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3: * project 2006
4: */
5: /* ====================================================================
6: * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: *
12: * 1. Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: *
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in
17: * the documentation and/or other materials provided with the
18: * distribution.
19: *
20: * 3. All advertising materials mentioning features or use of this
21: * software must display the following acknowledgment:
22: * "This product includes software developed by the OpenSSL Project
23: * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24: *
25: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26: * endorse or promote products derived from this software without
27: * prior written permission. For written permission, please contact
28: * licensing@OpenSSL.org.
29: *
30: * 5. Products derived from this software may not be called "OpenSSL"
31: * nor may "OpenSSL" appear in their names without prior written
32: * permission of the OpenSSL Project.
33: *
34: * 6. Redistributions of any form whatsoever must retain the following
35: * acknowledgment:
36: * "This product includes software developed by the OpenSSL Project
37: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38: *
39: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50: * OF THE POSSIBILITY OF SUCH DAMAGE.
51: * ====================================================================
52: *
53: * This product includes cryptographic software written by Eric Young
54: * (eay@cryptsoft.com). This product includes software written by Tim
55: * Hudson (tjh@cryptsoft.com).
56: *
57: */
58:
1.12 ! inoguchi 59: #include <ctype.h>
1.1 jsing 60: #include <stdio.h>
61: #include <string.h>
62:
63: #include "apps.h"
64:
65: #include <openssl/err.h>
66: #include <openssl/evp.h>
67: #include <openssl/pem.h>
68:
1.11 inoguchi 69: static struct {
70: const EVP_CIPHER *cipher;
71: char *infile;
72: int informat;
73: int noout;
74: char *outfile;
75: int outformat;
76: char *passargin;
77: char *passargout;
78: int pubin;
79: int pubout;
80: int pubtext;
81: int text;
82: } pkey_config;
83:
84: static int
85: pkey_opt_cipher(int argc, char **argv, int *argsused)
86: {
87: char *name = argv[0];
88:
89: if (*name++ != '-')
90: return (1);
91:
92: if ((pkey_config.cipher = EVP_get_cipherbyname(name)) == NULL) {
93: BIO_printf(bio_err, "Unknown cipher %s\n", name);
94: return (1);
95: }
96:
97: *argsused = 1;
98: return (0);
99: }
100:
101: static struct option pkey_options[] = {
102: {
103: .name = "in",
104: .argname = "file",
105: .desc = "Input file (default stdin)",
106: .type = OPTION_ARG,
107: .opt.arg = &pkey_config.infile,
108: },
109: {
110: .name = "inform",
111: .argname = "format",
112: .desc = "Input format (DER or PEM (default))",
113: .type = OPTION_ARG_FORMAT,
114: .opt.value = &pkey_config.informat,
115: },
116: {
117: .name = "noout",
118: .desc = "Do not print encoded version of the key",
119: .type = OPTION_FLAG,
120: .opt.flag = &pkey_config.noout,
121: },
122: {
123: .name = "out",
124: .argname = "file",
125: .desc = "Output file (default stdout)",
126: .type = OPTION_ARG,
127: .opt.arg = &pkey_config.outfile,
128: },
129: {
130: .name = "outform",
131: .argname = "format",
132: .desc = "Output format (DER or PEM (default))",
133: .type = OPTION_ARG_FORMAT,
134: .opt.value = &pkey_config.outformat,
135: },
136: {
137: .name = "passin",
138: .argname = "src",
139: .desc = "Input file passphrase source",
140: .type = OPTION_ARG,
141: .opt.arg = &pkey_config.passargin,
142: },
143: {
144: .name = "passout",
145: .argname = "src",
146: .desc = "Output file passphrase source",
147: .type = OPTION_ARG,
148: .opt.arg = &pkey_config.passargout,
149: },
150: {
151: .name = "pubin",
152: .desc = "Expect a public key (default private key)",
153: .type = OPTION_VALUE,
154: .value = 1,
155: .opt.value = &pkey_config.pubin,
156: },
157: {
158: .name = "pubout",
159: .desc = "Output a public key (default private key)",
160: .type = OPTION_VALUE,
161: .value = 1,
162: .opt.value = &pkey_config.pubout,
163: },
164: {
165: .name = "text",
166: .desc = "Print the public/private key in plain text",
167: .type = OPTION_FLAG,
168: .opt.flag = &pkey_config.text,
169: },
170: {
171: .name = "text_pub",
172: .desc = "Print out only public key in plain text",
173: .type = OPTION_FLAG,
174: .opt.flag = &pkey_config.pubtext,
175: },
176: {
177: .name = NULL,
178: .type = OPTION_ARGV_FUNC,
179: .opt.argvfunc = pkey_opt_cipher,
180: },
181: { NULL }
182: };
183:
184: static void
185: show_ciphers(const OBJ_NAME *name, void *arg)
186: {
187: static int n;
1.12 ! inoguchi 188:
! 189: if (!islower((unsigned char)*name->name))
! 190: return;
1.11 inoguchi 191:
192: fprintf(stderr, " -%-24s%s", name->name, (++n % 3 ? "" : "\n"));
193: }
194:
195: static void
196: pkey_usage()
197: {
198: fprintf(stderr,
199: "usage: pkey [-ciphername] [-in file] [-inform fmt] [-noout] "
200: "[-out file]\n"
201: " [-outform fmt] [-passin src] [-passout src] [-pubin] "
202: "[-pubout] [-text]\n"
203: " [-text_pub]\n\n");
204: options_usage(pkey_options);
205: fprintf(stderr, "\n");
206:
207: fprintf(stderr, "Valid ciphername values:\n\n");
208: OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, show_ciphers, NULL);
209: fprintf(stderr, "\n");
210: }
211:
1.1 jsing 212: int
213: pkey_main(int argc, char **argv)
214: {
215: BIO *in = NULL, *out = NULL;
216: EVP_PKEY *pkey = NULL;
217: char *passin = NULL, *passout = NULL;
218: int ret = 1;
1.6 doug 219:
220: if (single_execution) {
1.9 deraadt 221: if (pledge("stdio cpath wpath rpath tty", NULL) == -1) {
1.6 doug 222: perror("pledge");
1.8 doug 223: exit(1);
224: }
1.6 doug 225: }
1.1 jsing 226:
1.11 inoguchi 227: memset(&pkey_config, 0, sizeof(pkey_config));
228: pkey_config.informat = FORMAT_PEM;
229: pkey_config.outformat = FORMAT_PEM;
1.1 jsing 230:
1.11 inoguchi 231: if (options_parse(argc, argv, pkey_options, NULL, NULL) != 0) {
232: pkey_usage();
233: goto end;
1.1 jsing 234: }
235:
1.11 inoguchi 236: if (pkey_config.pubtext)
237: pkey_config.text = 1;
238: if (pkey_config.pubin)
239: pkey_config.pubout = pkey_config.pubtext = 1;
1.1 jsing 240:
1.11 inoguchi 241: if (!app_passwd(bio_err, pkey_config.passargin, pkey_config.passargout,
242: &passin, &passout)) {
1.1 jsing 243: BIO_printf(bio_err, "Error getting passwords\n");
244: goto end;
245: }
1.11 inoguchi 246: if (pkey_config.outfile) {
247: if (!(out = BIO_new_file(pkey_config.outfile, "wb"))) {
1.1 jsing 248: BIO_printf(bio_err,
1.11 inoguchi 249: "Can't open output file %s\n", pkey_config.outfile);
1.1 jsing 250: goto end;
251: }
252: } else {
253: out = BIO_new_fp(stdout, BIO_NOCLOSE);
254: }
255:
1.11 inoguchi 256: if (pkey_config.pubin)
257: pkey = load_pubkey(bio_err, pkey_config.infile,
258: pkey_config.informat, 1, passin, "Public Key");
1.1 jsing 259: else
1.11 inoguchi 260: pkey = load_key(bio_err, pkey_config.infile,
261: pkey_config.informat, 1, passin, "key");
1.1 jsing 262: if (!pkey)
263: goto end;
264:
1.11 inoguchi 265: if (!pkey_config.noout) {
266: if (pkey_config.outformat == FORMAT_PEM) {
267: if (pkey_config.pubout)
1.1 jsing 268: PEM_write_bio_PUBKEY(out, pkey);
269: else
1.11 inoguchi 270: PEM_write_bio_PrivateKey(out, pkey,
271: pkey_config.cipher, NULL, 0, NULL, passout);
272: } else if (pkey_config.outformat == FORMAT_ASN1) {
273: if (pkey_config.pubout)
1.1 jsing 274: i2d_PUBKEY_bio(out, pkey);
275: else
276: i2d_PrivateKey_bio(out, pkey);
277: } else {
278: BIO_printf(bio_err, "Bad format specified for key\n");
279: goto end;
280: }
281:
282: }
1.11 inoguchi 283: if (pkey_config.text) {
284: if (pkey_config.pubtext)
1.1 jsing 285: EVP_PKEY_print_public(out, pkey, 0, NULL);
286: else
287: EVP_PKEY_print_private(out, pkey, 0, NULL);
288: }
289: ret = 0;
290:
1.10 jsing 291: end:
1.1 jsing 292: EVP_PKEY_free(pkey);
293: BIO_free_all(out);
294: BIO_free(in);
295: free(passin);
296: free(passout);
297:
298: return ret;
299: }