1 /* $OpenBSD: pkcs8.c,v 1.13 2018/08/24 22:56:45 jmc Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
65 #include <openssl/err.h>
66 #include <openssl/evp.h>
67 #include <openssl/pem.h>
68 #include <openssl/pkcs12.h>
71 const EVP_CIPHER
*cipher
;
85 pkcs8_opt_v1(char *arg
)
87 if ((pkcs8_config
.pbe_nid
= OBJ_txt2nid(arg
)) == NID_undef
) {
88 fprintf(stderr
, "Unknown PBE algorithm '%s'\n", arg
);
96 pkcs8_opt_v2(char *arg
)
98 if ((pkcs8_config
.cipher
= EVP_get_cipherbyname(arg
)) == NULL
) {
99 fprintf(stderr
, "Unknown cipher '%s'\n", arg
);
106 static struct option pkcs8_options
[] = {
110 .desc
= "Input file (default stdin)",
112 .opt
.arg
= &pkcs8_config
.infile
,
116 .argname
= "der | pem",
117 .desc
= "Input format (default PEM)",
118 .type
= OPTION_ARG_FORMAT
,
119 .opt
.value
= &pkcs8_config
.informat
,
123 .desc
= "Use or expect unencrypted private key",
125 .opt
.flag
= &pkcs8_config
.nocrypt
,
129 .desc
= "Use 1 as iteration count",
130 .type
= OPTION_VALUE
,
132 .opt
.value
= &pkcs8_config
.iter
,
137 .desc
= "Output file (default stdout)",
139 .opt
.arg
= &pkcs8_config
.outfile
,
143 .argname
= "der | pem",
144 .desc
= "Output format (default PEM)",
145 .type
= OPTION_ARG_FORMAT
,
146 .opt
.value
= &pkcs8_config
.outformat
,
151 .desc
= "Input file passphrase source",
153 .opt
.arg
= &pkcs8_config
.passargin
,
158 .desc
= "Output file passphrase source",
160 .opt
.arg
= &pkcs8_config
.passargout
,
164 .desc
= "Read traditional format key and write PKCS#8 format"
167 .opt
.flag
= &pkcs8_config
.topk8
,
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
,
179 .desc
= "Use PKCS#5 v2.0 with given cipher",
180 .type
= OPTION_ARG_FUNC
,
181 .opt
.argfunc
= pkcs8_opt_v2
,
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");
193 options_usage(pkcs8_options
);
197 pkcs8_main(int argc
, char **argv
)
199 BIO
*in
= NULL
, *out
= NULL
;
201 PKCS8_PRIV_KEY_INFO
*p8inf
= NULL
;
202 EVP_PKEY
*pkey
= NULL
;
203 char pass
[50], *passin
= NULL
, *passout
= NULL
, *p8pass
= NULL
;
206 if (single_execution
) {
207 if (pledge("stdio cpath wpath rpath tty", NULL
) == -1) {
213 memset(&pkcs8_config
, 0, sizeof(pkcs8_config
));
215 pkcs8_config
.iter
= PKCS12_DEFAULT_ITER
;
216 pkcs8_config
.informat
= FORMAT_PEM
;
217 pkcs8_config
.outformat
= FORMAT_PEM
;
218 pkcs8_config
.pbe_nid
= -1;
220 if (options_parse(argc
, argv
, pkcs8_options
, NULL
, NULL
) != 0) {
225 if (!app_passwd(bio_err
, pkcs8_config
.passargin
,
226 pkcs8_config
.passargout
, &passin
, &passout
)) {
227 BIO_printf(bio_err
, "Error getting passwords\n");
230 if ((pkcs8_config
.pbe_nid
== -1) && !pkcs8_config
.cipher
)
231 pkcs8_config
.pbe_nid
= NID_pbeWithMD5AndDES_CBC
;
233 if (pkcs8_config
.infile
) {
234 if (!(in
= BIO_new_file(pkcs8_config
.infile
, "rb"))) {
236 "Can't open input file '%s'\n",
237 pkcs8_config
.infile
);
241 in
= BIO_new_fp(stdin
, BIO_NOCLOSE
);
243 if (pkcs8_config
.outfile
) {
244 if (!(out
= BIO_new_file(pkcs8_config
.outfile
, "wb"))) {
245 BIO_printf(bio_err
, "Can't open output file '%s'\n",
246 pkcs8_config
.outfile
);
250 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
252 if (pkcs8_config
.topk8
) {
253 pkey
= load_key(bio_err
, pkcs8_config
.infile
,
254 pkcs8_config
.informat
, 1, passin
, "key");
257 if (!(p8inf
= EVP_PKEY2PKCS8(pkey
))) {
258 BIO_printf(bio_err
, "Error converting key\n");
259 ERR_print_errors(bio_err
);
262 if (pkcs8_config
.nocrypt
) {
263 if (pkcs8_config
.outformat
== FORMAT_PEM
)
264 PEM_write_bio_PKCS8_PRIV_KEY_INFO(out
, p8inf
);
265 else if (pkcs8_config
.outformat
== FORMAT_ASN1
)
266 i2d_PKCS8_PRIV_KEY_INFO_bio(out
, p8inf
);
269 "Bad format specified for key\n");
277 if (EVP_read_pw_string(pass
, sizeof pass
,
278 "Enter Encryption Password:", 1))
281 if (!(p8
= PKCS8_encrypt(pkcs8_config
.pbe_nid
,
282 pkcs8_config
.cipher
, p8pass
, strlen(p8pass
),
283 NULL
, 0, pkcs8_config
.iter
, p8inf
))) {
284 BIO_printf(bio_err
, "Error encrypting key\n");
285 ERR_print_errors(bio_err
);
288 if (pkcs8_config
.outformat
== FORMAT_PEM
)
289 PEM_write_bio_PKCS8(out
, p8
);
290 else if (pkcs8_config
.outformat
== FORMAT_ASN1
)
291 i2d_PKCS8_bio(out
, p8
);
294 "Bad format specified for key\n");
302 if (pkcs8_config
.nocrypt
) {
303 if (pkcs8_config
.informat
== FORMAT_PEM
)
304 p8inf
= PEM_read_bio_PKCS8_PRIV_KEY_INFO(in
, NULL
,
306 else if (pkcs8_config
.informat
== FORMAT_ASN1
)
307 p8inf
= d2i_PKCS8_PRIV_KEY_INFO_bio(in
, NULL
);
309 BIO_printf(bio_err
, "Bad format specified for key\n");
313 if (pkcs8_config
.informat
== FORMAT_PEM
)
314 p8
= PEM_read_bio_PKCS8(in
, NULL
, NULL
, NULL
);
315 else if (pkcs8_config
.informat
== FORMAT_ASN1
)
316 p8
= d2i_PKCS8_bio(in
, NULL
);
318 BIO_printf(bio_err
, "Bad format specified for key\n");
323 BIO_printf(bio_err
, "Error reading key\n");
324 ERR_print_errors(bio_err
);
331 EVP_read_pw_string(pass
, sizeof pass
,
332 "Enter Password:", 0);
334 p8inf
= PKCS8_decrypt(p8
, p8pass
, strlen(p8pass
));
338 BIO_printf(bio_err
, "Error decrypting key\n");
339 ERR_print_errors(bio_err
);
342 if (!(pkey
= EVP_PKCS82PKEY(p8inf
))) {
343 BIO_printf(bio_err
, "Error converting key\n");
344 ERR_print_errors(bio_err
);
347 if (pkcs8_config
.outformat
== FORMAT_PEM
)
348 PEM_write_bio_PrivateKey(out
, pkey
, NULL
, NULL
, 0, NULL
,
350 else if (pkcs8_config
.outformat
== FORMAT_ASN1
)
351 i2d_PrivateKey_bio(out
, pkey
);
353 BIO_printf(bio_err
, "Bad format specified for key\n");
360 PKCS8_PRIV_KEY_INFO_free(p8inf
);