Annotation of src/usr.bin/openssl/pkcs8.c, Revision 1.17
1.17 ! tb 1: /* $OpenBSD: pkcs8.c,v 1.16 2023/03/06 14:32:06 tb Exp $ */
1.1 jsing 2: /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3: * project 1999-2004.
4: */
5: /* ====================================================================
6: * Copyright (c) 1999 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:
59: #include <stdio.h>
60: #include <string.h>
61:
62: #include "apps.h"
1.5 deraadt 63: #include "progs.h"
1.1 jsing 64:
65: #include <openssl/err.h>
66: #include <openssl/evp.h>
67: #include <openssl/pem.h>
68: #include <openssl/pkcs12.h>
69:
1.4 doug 70: static struct {
71: const EVP_CIPHER *cipher;
72: char *infile;
73: int informat;
74: int iter;
75: int nocrypt;
76: char *outfile;
77: int outformat;
78: char *passargin;
79: char *passargout;
80: int pbe_nid;
81: int topk8;
1.16 tb 82: } cfg;
1.4 doug 83:
84: static int
85: pkcs8_opt_v1(char *arg)
86: {
1.16 tb 87: if ((cfg.pbe_nid = OBJ_txt2nid(arg)) == NID_undef) {
1.4 doug 88: fprintf(stderr, "Unknown PBE algorithm '%s'\n", arg);
89: return (1);
90: }
91:
92: return (0);
93: }
94:
95: static int
96: pkcs8_opt_v2(char *arg)
97: {
1.16 tb 98: if ((cfg.cipher = EVP_get_cipherbyname(arg)) == NULL) {
1.4 doug 99: fprintf(stderr, "Unknown cipher '%s'\n", arg);
100: return (1);
101: }
102:
103: return (0);
104: }
105:
1.14 guenther 106: static const struct option pkcs8_options[] = {
1.4 doug 107: {
108: .name = "in",
109: .argname = "file",
110: .desc = "Input file (default stdin)",
111: .type = OPTION_ARG,
1.16 tb 112: .opt.arg = &cfg.infile,
1.4 doug 113: },
114: {
115: .name = "inform",
1.13 jmc 116: .argname = "der | pem",
117: .desc = "Input format (default PEM)",
1.4 doug 118: .type = OPTION_ARG_FORMAT,
1.16 tb 119: .opt.value = &cfg.informat,
1.4 doug 120: },
121: {
122: .name = "nocrypt",
123: .desc = "Use or expect unencrypted private key",
124: .type = OPTION_FLAG,
1.16 tb 125: .opt.flag = &cfg.nocrypt,
1.4 doug 126: },
127: {
128: .name = "noiter",
129: .desc = "Use 1 as iteration count",
130: .type = OPTION_VALUE,
131: .value = 1,
1.16 tb 132: .opt.value = &cfg.iter,
1.4 doug 133: },
134: {
135: .name = "out",
136: .argname = "file",
137: .desc = "Output file (default stdout)",
138: .type = OPTION_ARG,
1.16 tb 139: .opt.arg = &cfg.outfile,
1.4 doug 140: },
141: {
142: .name = "outform",
1.13 jmc 143: .argname = "der | pem",
144: .desc = "Output format (default PEM)",
1.4 doug 145: .type = OPTION_ARG_FORMAT,
1.16 tb 146: .opt.value = &cfg.outformat,
1.4 doug 147: },
148: {
149: .name = "passin",
150: .argname = "source",
151: .desc = "Input file passphrase source",
152: .type = OPTION_ARG,
1.16 tb 153: .opt.arg = &cfg.passargin,
1.4 doug 154: },
155: {
156: .name = "passout",
157: .argname = "source",
158: .desc = "Output file passphrase source",
159: .type = OPTION_ARG,
1.16 tb 160: .opt.arg = &cfg.passargout,
1.4 doug 161: },
162: {
163: .name = "topk8",
164: .desc = "Read traditional format key and write PKCS#8 format"
165: " key",
166: .type = OPTION_FLAG,
1.16 tb 167: .opt.flag = &cfg.topk8,
1.4 doug 168: },
169: {
170: .name = "v1",
171: .argname = "algorithm",
172: .desc = "Use PKCS#5 v1.5 or PKCS#12 with given algorithm",
173: .type = OPTION_ARG_FUNC,
174: .opt.argfunc = pkcs8_opt_v1,
175: },
176: {
177: .name = "v2",
178: .argname = "cipher",
179: .desc = "Use PKCS#5 v2.0 with given cipher",
180: .type = OPTION_ARG_FUNC,
181: .opt.argfunc = pkcs8_opt_v2,
182: },
183: { NULL },
184: };
185:
186: static void
1.17 ! tb 187: pkcs8_usage(void)
1.4 doug 188: {
1.13 jmc 189: fprintf(stderr, "usage: pkcs8 [-in file] [inform der | pem] "
190: "[-nocrypt] [-noiter]\n"
191: " [-out file] [-outform der | pem] [-passin arg]\n"
192: " [-passout arg] [-topk8] [-v1 alg] [-v2 alg]\n\n");
1.4 doug 193: options_usage(pkcs8_options);
194: }
1.1 jsing 195:
196: int
197: pkcs8_main(int argc, char **argv)
198: {
199: BIO *in = NULL, *out = NULL;
200: X509_SIG *p8 = NULL;
201: PKCS8_PRIV_KEY_INFO *p8inf = NULL;
202: EVP_PKEY *pkey = NULL;
203: char pass[50], *passin = NULL, *passout = NULL, *p8pass = NULL;
204: int ret = 1;
1.7 doug 205:
1.15 joshua 206: if (pledge("stdio cpath wpath rpath tty", NULL) == -1) {
207: perror("pledge");
208: exit(1);
1.7 doug 209: }
1.1 jsing 210:
1.16 tb 211: memset(&cfg, 0, sizeof(cfg));
1.1 jsing 212:
1.16 tb 213: cfg.iter = PKCS12_DEFAULT_ITER;
214: cfg.informat = FORMAT_PEM;
215: cfg.outformat = FORMAT_PEM;
216: cfg.pbe_nid = -1;
1.4 doug 217:
218: if (options_parse(argc, argv, pkcs8_options, NULL, NULL) != 0) {
219: pkcs8_usage();
220: return (1);
1.1 jsing 221: }
222:
1.16 tb 223: if (!app_passwd(bio_err, cfg.passargin,
224: cfg.passargout, &passin, &passout)) {
1.1 jsing 225: BIO_printf(bio_err, "Error getting passwords\n");
226: goto end;
227: }
1.16 tb 228: if ((cfg.pbe_nid == -1) && !cfg.cipher)
229: cfg.pbe_nid = NID_pbeWithMD5AndDES_CBC;
1.1 jsing 230:
1.16 tb 231: if (cfg.infile) {
232: if (!(in = BIO_new_file(cfg.infile, "rb"))) {
1.1 jsing 233: BIO_printf(bio_err,
1.4 doug 234: "Can't open input file '%s'\n",
1.16 tb 235: cfg.infile);
1.1 jsing 236: goto end;
237: }
238: } else
239: in = BIO_new_fp(stdin, BIO_NOCLOSE);
240:
1.16 tb 241: if (cfg.outfile) {
242: if (!(out = BIO_new_file(cfg.outfile, "wb"))) {
1.4 doug 243: BIO_printf(bio_err, "Can't open output file '%s'\n",
1.16 tb 244: cfg.outfile);
1.1 jsing 245: goto end;
246: }
247: } else {
248: out = BIO_new_fp(stdout, BIO_NOCLOSE);
249: }
1.16 tb 250: if (cfg.topk8) {
251: pkey = load_key(bio_err, cfg.infile,
252: cfg.informat, 1, passin, "key");
1.1 jsing 253: if (!pkey)
254: goto end;
1.12 tb 255: if (!(p8inf = EVP_PKEY2PKCS8(pkey))) {
1.1 jsing 256: BIO_printf(bio_err, "Error converting key\n");
257: ERR_print_errors(bio_err);
258: goto end;
259: }
1.16 tb 260: if (cfg.nocrypt) {
261: if (cfg.outformat == FORMAT_PEM)
1.1 jsing 262: PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
1.16 tb 263: else if (cfg.outformat == FORMAT_ASN1)
1.1 jsing 264: i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
265: else {
1.4 doug 266: BIO_printf(bio_err,
267: "Bad format specified for key\n");
1.1 jsing 268: goto end;
269: }
270: } else {
271: if (passout)
272: p8pass = passout;
273: else {
274: p8pass = pass;
1.4 doug 275: if (EVP_read_pw_string(pass, sizeof pass,
276: "Enter Encryption Password:", 1))
1.1 jsing 277: goto end;
278: }
1.16 tb 279: if (!(p8 = PKCS8_encrypt(cfg.pbe_nid,
280: cfg.cipher, p8pass, strlen(p8pass),
281: NULL, 0, cfg.iter, p8inf))) {
1.1 jsing 282: BIO_printf(bio_err, "Error encrypting key\n");
283: ERR_print_errors(bio_err);
284: goto end;
285: }
1.16 tb 286: if (cfg.outformat == FORMAT_PEM)
1.1 jsing 287: PEM_write_bio_PKCS8(out, p8);
1.16 tb 288: else if (cfg.outformat == FORMAT_ASN1)
1.1 jsing 289: i2d_PKCS8_bio(out, p8);
290: else {
1.4 doug 291: BIO_printf(bio_err,
292: "Bad format specified for key\n");
1.1 jsing 293: goto end;
294: }
295: }
296:
297: ret = 0;
298: goto end;
299: }
1.16 tb 300: if (cfg.nocrypt) {
301: if (cfg.informat == FORMAT_PEM)
1.4 doug 302: p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL,
303: NULL, NULL);
1.16 tb 304: else if (cfg.informat == FORMAT_ASN1)
1.1 jsing 305: p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
306: else {
307: BIO_printf(bio_err, "Bad format specified for key\n");
308: goto end;
309: }
310: } else {
1.16 tb 311: if (cfg.informat == FORMAT_PEM)
1.1 jsing 312: p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
1.16 tb 313: else if (cfg.informat == FORMAT_ASN1)
1.1 jsing 314: p8 = d2i_PKCS8_bio(in, NULL);
315: else {
316: BIO_printf(bio_err, "Bad format specified for key\n");
317: goto end;
318: }
319:
320: if (!p8) {
321: BIO_printf(bio_err, "Error reading key\n");
322: ERR_print_errors(bio_err);
323: goto end;
324: }
325: if (passin)
326: p8pass = passin;
327: else {
328: p8pass = pass;
1.4 doug 329: EVP_read_pw_string(pass, sizeof pass,
330: "Enter Password:", 0);
1.1 jsing 331: }
332: p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
333: }
334:
335: if (!p8inf) {
336: BIO_printf(bio_err, "Error decrypting key\n");
337: ERR_print_errors(bio_err);
338: goto end;
339: }
340: if (!(pkey = EVP_PKCS82PKEY(p8inf))) {
341: BIO_printf(bio_err, "Error converting key\n");
342: ERR_print_errors(bio_err);
343: goto end;
344: }
1.16 tb 345: if (cfg.outformat == FORMAT_PEM)
1.4 doug 346: PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL,
347: passout);
1.16 tb 348: else if (cfg.outformat == FORMAT_ASN1)
1.1 jsing 349: i2d_PrivateKey_bio(out, pkey);
350: else {
351: BIO_printf(bio_err, "Bad format specified for key\n");
352: goto end;
353: }
354: ret = 0;
355:
1.11 jsing 356: end:
1.1 jsing 357: X509_SIG_free(p8);
358: PKCS8_PRIV_KEY_INFO_free(p8inf);
359: EVP_PKEY_free(pkey);
360: BIO_free_all(out);
361: BIO_free(in);
362: free(passin);
363: free(passout);
364:
365: return ret;
366: }