xref: /minix3/crypto/external/bsd/openssl/dist/apps/pkcs12.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* pkcs12.c */
2*0a6a1f1dSLionel Sambuc /*
3*0a6a1f1dSLionel Sambuc  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4ebfedea0SLionel Sambuc  * project.
5ebfedea0SLionel Sambuc  */
6ebfedea0SLionel Sambuc /* ====================================================================
7ebfedea0SLionel Sambuc  * Copyright (c) 1999-2006 The OpenSSL Project.  All rights reserved.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc  * are met:
12ebfedea0SLionel Sambuc  *
13ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
18ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
19ebfedea0SLionel Sambuc  *    distribution.
20ebfedea0SLionel Sambuc  *
21ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
22ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
23ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
24ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25ebfedea0SLionel Sambuc  *
26ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
28ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
29ebfedea0SLionel Sambuc  *    licensing@OpenSSL.org.
30ebfedea0SLionel Sambuc  *
31ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
32ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
33ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
34ebfedea0SLionel Sambuc  *
35ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
36ebfedea0SLionel Sambuc  *    acknowledgment:
37ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
38ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39ebfedea0SLionel Sambuc  *
40ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
52ebfedea0SLionel Sambuc  * ====================================================================
53ebfedea0SLionel Sambuc  *
54ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
55ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
56ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
57ebfedea0SLionel Sambuc  *
58ebfedea0SLionel Sambuc  */
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc #include <openssl/opensslconf.h>
61ebfedea0SLionel Sambuc #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA1)
62ebfedea0SLionel Sambuc 
63ebfedea0SLionel Sambuc # include <stdio.h>
64ebfedea0SLionel Sambuc # include <stdlib.h>
65ebfedea0SLionel Sambuc # include <string.h>
66ebfedea0SLionel Sambuc # include "apps.h"
67ebfedea0SLionel Sambuc # include <openssl/crypto.h>
68ebfedea0SLionel Sambuc # include <openssl/err.h>
69ebfedea0SLionel Sambuc # include <openssl/pem.h>
70ebfedea0SLionel Sambuc # include <openssl/pkcs12.h>
71ebfedea0SLionel Sambuc 
72ebfedea0SLionel Sambuc # define PROG pkcs12_main
73ebfedea0SLionel Sambuc 
74ebfedea0SLionel Sambuc const EVP_CIPHER *enc;
75ebfedea0SLionel Sambuc 
76ebfedea0SLionel Sambuc # define NOKEYS          0x1
77ebfedea0SLionel Sambuc # define NOCERTS         0x2
78ebfedea0SLionel Sambuc # define INFO            0x4
79ebfedea0SLionel Sambuc # define CLCERTS         0x8
80ebfedea0SLionel Sambuc # define CACERTS         0x10
81ebfedea0SLionel Sambuc 
82ebfedea0SLionel Sambuc int get_cert_chain(X509 *cert, X509_STORE *store, STACK_OF(X509) **chain);
83*0a6a1f1dSLionel Sambuc int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass, int passlen,
84*0a6a1f1dSLionel Sambuc                         int options, char *pempass);
85*0a6a1f1dSLionel Sambuc int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags,
86*0a6a1f1dSLionel Sambuc                           char *pass, int passlen, int options,
87*0a6a1f1dSLionel Sambuc                           char *pempass);
88*0a6a1f1dSLionel Sambuc int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bags, char *pass,
89ebfedea0SLionel Sambuc                          int passlen, int options, char *pempass);
90*0a6a1f1dSLionel Sambuc int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,
91*0a6a1f1dSLionel Sambuc                   const char *name);
92ebfedea0SLionel Sambuc void hex_prin(BIO *out, unsigned char *buf, int len);
93ebfedea0SLionel Sambuc int alg_print(BIO *x, X509_ALGOR *alg);
94ebfedea0SLionel Sambuc int cert_load(BIO *in, STACK_OF(X509) *sk);
95ebfedea0SLionel Sambuc static int set_pbe(BIO *err, int *ppbe, const char *str);
96ebfedea0SLionel Sambuc 
97ebfedea0SLionel Sambuc int MAIN(int, char **);
98ebfedea0SLionel Sambuc 
MAIN(int argc,char ** argv)99ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
100ebfedea0SLionel Sambuc {
101ebfedea0SLionel Sambuc     ENGINE *e = NULL;
102ebfedea0SLionel Sambuc     char *infile = NULL, *outfile = NULL, *keyname = NULL;
103ebfedea0SLionel Sambuc     char *certfile = NULL;
104ebfedea0SLionel Sambuc     BIO *in = NULL, *out = NULL;
105ebfedea0SLionel Sambuc     char **args;
106ebfedea0SLionel Sambuc     char *name = NULL;
107ebfedea0SLionel Sambuc     char *csp_name = NULL;
108ebfedea0SLionel Sambuc     int add_lmk = 0;
109ebfedea0SLionel Sambuc     PKCS12 *p12 = NULL;
110ebfedea0SLionel Sambuc     char pass[50], macpass[50];
111ebfedea0SLionel Sambuc     int export_cert = 0;
112ebfedea0SLionel Sambuc     int options = 0;
113ebfedea0SLionel Sambuc     int chain = 0;
114ebfedea0SLionel Sambuc     int badarg = 0;
115ebfedea0SLionel Sambuc     int iter = PKCS12_DEFAULT_ITER;
116ebfedea0SLionel Sambuc     int maciter = PKCS12_DEFAULT_ITER;
117ebfedea0SLionel Sambuc     int twopass = 0;
118ebfedea0SLionel Sambuc     int keytype = 0;
119*0a6a1f1dSLionel Sambuc     int cert_pbe;
120ebfedea0SLionel Sambuc     int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
121ebfedea0SLionel Sambuc     int ret = 1;
122ebfedea0SLionel Sambuc     int macver = 1;
123ebfedea0SLionel Sambuc     int noprompt = 0;
124ebfedea0SLionel Sambuc     STACK_OF(OPENSSL_STRING) *canames = NULL;
125ebfedea0SLionel Sambuc     char *cpass = NULL, *mpass = NULL;
126ebfedea0SLionel Sambuc     char *passargin = NULL, *passargout = NULL, *passarg = NULL;
127ebfedea0SLionel Sambuc     char *passin = NULL, *passout = NULL;
128ebfedea0SLionel Sambuc     char *inrand = NULL;
129ebfedea0SLionel Sambuc     char *macalg = NULL;
130ebfedea0SLionel Sambuc     char *CApath = NULL, *CAfile = NULL;
131ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
132ebfedea0SLionel Sambuc     char *engine = NULL;
133ebfedea0SLionel Sambuc # endif
134ebfedea0SLionel Sambuc 
135ebfedea0SLionel Sambuc     apps_startup();
136ebfedea0SLionel Sambuc 
137*0a6a1f1dSLionel Sambuc # ifdef OPENSSL_FIPS
138*0a6a1f1dSLionel Sambuc     if (FIPS_mode())
139*0a6a1f1dSLionel Sambuc         cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
140*0a6a1f1dSLionel Sambuc     else
141*0a6a1f1dSLionel Sambuc # endif
142*0a6a1f1dSLionel Sambuc         cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
143*0a6a1f1dSLionel Sambuc 
144ebfedea0SLionel Sambuc     enc = EVP_des_ede3_cbc();
145*0a6a1f1dSLionel Sambuc     if (bio_err == NULL)
146*0a6a1f1dSLionel Sambuc         bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
147ebfedea0SLionel Sambuc 
148ebfedea0SLionel Sambuc     if (!load_config(bio_err, NULL))
149ebfedea0SLionel Sambuc         goto end;
150ebfedea0SLionel Sambuc 
151ebfedea0SLionel Sambuc     args = argv + 1;
152ebfedea0SLionel Sambuc 
153ebfedea0SLionel Sambuc     while (*args) {
154ebfedea0SLionel Sambuc         if (*args[0] == '-') {
155*0a6a1f1dSLionel Sambuc             if (!strcmp(*args, "-nokeys"))
156*0a6a1f1dSLionel Sambuc                 options |= NOKEYS;
157*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-keyex"))
158*0a6a1f1dSLionel Sambuc                 keytype = KEY_EX;
159*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-keysig"))
160*0a6a1f1dSLionel Sambuc                 keytype = KEY_SIG;
161*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-nocerts"))
162*0a6a1f1dSLionel Sambuc                 options |= NOCERTS;
163*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-clcerts"))
164*0a6a1f1dSLionel Sambuc                 options |= CLCERTS;
165*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-cacerts"))
166*0a6a1f1dSLionel Sambuc                 options |= CACERTS;
167*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-noout"))
168*0a6a1f1dSLionel Sambuc                 options |= (NOKEYS | NOCERTS);
169*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-info"))
170*0a6a1f1dSLionel Sambuc                 options |= INFO;
171*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-chain"))
172*0a6a1f1dSLionel Sambuc                 chain = 1;
173*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-twopass"))
174*0a6a1f1dSLionel Sambuc                 twopass = 1;
175*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-nomacver"))
176*0a6a1f1dSLionel Sambuc                 macver = 0;
177ebfedea0SLionel Sambuc             else if (!strcmp(*args, "-descert"))
178ebfedea0SLionel Sambuc                 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
179*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-export"))
180*0a6a1f1dSLionel Sambuc                 export_cert = 1;
181*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-des"))
182*0a6a1f1dSLionel Sambuc                 enc = EVP_des_cbc();
183*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-des3"))
184*0a6a1f1dSLionel Sambuc                 enc = EVP_des_ede3_cbc();
185ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_IDEA
186*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-idea"))
187*0a6a1f1dSLionel Sambuc                 enc = EVP_idea_cbc();
188ebfedea0SLionel Sambuc # endif
189ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SEED
190*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-seed"))
191*0a6a1f1dSLionel Sambuc                 enc = EVP_seed_cbc();
192ebfedea0SLionel Sambuc # endif
193ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_AES
194*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-aes128"))
195*0a6a1f1dSLionel Sambuc                 enc = EVP_aes_128_cbc();
196*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-aes192"))
197*0a6a1f1dSLionel Sambuc                 enc = EVP_aes_192_cbc();
198*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-aes256"))
199*0a6a1f1dSLionel Sambuc                 enc = EVP_aes_256_cbc();
200ebfedea0SLionel Sambuc # endif
201ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_CAMELLIA
202*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-camellia128"))
203*0a6a1f1dSLionel Sambuc                 enc = EVP_camellia_128_cbc();
204*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-camellia192"))
205*0a6a1f1dSLionel Sambuc                 enc = EVP_camellia_192_cbc();
206*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-camellia256"))
207*0a6a1f1dSLionel Sambuc                 enc = EVP_camellia_256_cbc();
208ebfedea0SLionel Sambuc # endif
209*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-noiter"))
210*0a6a1f1dSLionel Sambuc                 iter = 1;
211ebfedea0SLionel Sambuc             else if (!strcmp(*args, "-maciter"))
212ebfedea0SLionel Sambuc                 maciter = PKCS12_DEFAULT_ITER;
213ebfedea0SLionel Sambuc             else if (!strcmp(*args, "-nomaciter"))
214ebfedea0SLionel Sambuc                 maciter = 1;
215ebfedea0SLionel Sambuc             else if (!strcmp(*args, "-nomac"))
216ebfedea0SLionel Sambuc                 maciter = -1;
217ebfedea0SLionel Sambuc             else if (!strcmp(*args, "-macalg"))
218ebfedea0SLionel Sambuc                 if (args[1]) {
219ebfedea0SLionel Sambuc                     args++;
220ebfedea0SLionel Sambuc                     macalg = *args;
221*0a6a1f1dSLionel Sambuc                 } else
222*0a6a1f1dSLionel Sambuc                     badarg = 1;
223*0a6a1f1dSLionel Sambuc             else if (!strcmp(*args, "-nodes"))
224*0a6a1f1dSLionel Sambuc                 enc = NULL;
225ebfedea0SLionel Sambuc             else if (!strcmp(*args, "-certpbe")) {
226ebfedea0SLionel Sambuc                 if (!set_pbe(bio_err, &cert_pbe, *++args))
227ebfedea0SLionel Sambuc                     badarg = 1;
228ebfedea0SLionel Sambuc             } else if (!strcmp(*args, "-keypbe")) {
229ebfedea0SLionel Sambuc                 if (!set_pbe(bio_err, &key_pbe, *++args))
230ebfedea0SLionel Sambuc                     badarg = 1;
231ebfedea0SLionel Sambuc             } else if (!strcmp(*args, "-rand")) {
232ebfedea0SLionel Sambuc                 if (args[1]) {
233ebfedea0SLionel Sambuc                     args++;
234ebfedea0SLionel Sambuc                     inrand = *args;
235*0a6a1f1dSLionel Sambuc                 } else
236*0a6a1f1dSLionel Sambuc                     badarg = 1;
237ebfedea0SLionel Sambuc             } else if (!strcmp(*args, "-inkey")) {
238ebfedea0SLionel Sambuc                 if (args[1]) {
239ebfedea0SLionel Sambuc                     args++;
240ebfedea0SLionel Sambuc                     keyname = *args;
241*0a6a1f1dSLionel Sambuc                 } else
242*0a6a1f1dSLionel Sambuc                     badarg = 1;
243ebfedea0SLionel Sambuc             } else if (!strcmp(*args, "-certfile")) {
244ebfedea0SLionel Sambuc                 if (args[1]) {
245ebfedea0SLionel Sambuc                     args++;
246ebfedea0SLionel Sambuc                     certfile = *args;
247*0a6a1f1dSLionel Sambuc                 } else
248*0a6a1f1dSLionel Sambuc                     badarg = 1;
249ebfedea0SLionel Sambuc             } else if (!strcmp(*args, "-name")) {
250ebfedea0SLionel Sambuc                 if (args[1]) {
251ebfedea0SLionel Sambuc                     args++;
252ebfedea0SLionel Sambuc                     name = *args;
253*0a6a1f1dSLionel Sambuc                 } else
254*0a6a1f1dSLionel Sambuc                     badarg = 1;
255ebfedea0SLionel Sambuc             } else if (!strcmp(*args, "-LMK"))
256ebfedea0SLionel Sambuc                 add_lmk = 1;
257ebfedea0SLionel Sambuc             else if (!strcmp(*args, "-CSP")) {
258ebfedea0SLionel Sambuc                 if (args[1]) {
259ebfedea0SLionel Sambuc                     args++;
260ebfedea0SLionel Sambuc                     csp_name = *args;
261*0a6a1f1dSLionel Sambuc                 } else
262*0a6a1f1dSLionel Sambuc                     badarg = 1;
263ebfedea0SLionel Sambuc             } else if (!strcmp(*args, "-caname")) {
264ebfedea0SLionel Sambuc                 if (args[1]) {
265ebfedea0SLionel Sambuc                     args++;
266*0a6a1f1dSLionel Sambuc                     if (!canames)
267*0a6a1f1dSLionel Sambuc                         canames = sk_OPENSSL_STRING_new_null();
268ebfedea0SLionel Sambuc                     sk_OPENSSL_STRING_push(canames, *args);
269*0a6a1f1dSLionel Sambuc                 } else
270*0a6a1f1dSLionel Sambuc                     badarg = 1;
271ebfedea0SLionel Sambuc             } else if (!strcmp(*args, "-in")) {
272ebfedea0SLionel Sambuc                 if (args[1]) {
273ebfedea0SLionel Sambuc                     args++;
274ebfedea0SLionel Sambuc                     infile = *args;
275*0a6a1f1dSLionel Sambuc                 } else
276*0a6a1f1dSLionel Sambuc                     badarg = 1;
277ebfedea0SLionel Sambuc             } else if (!strcmp(*args, "-out")) {
278ebfedea0SLionel Sambuc                 if (args[1]) {
279ebfedea0SLionel Sambuc                     args++;
280ebfedea0SLionel Sambuc                     outfile = *args;
281*0a6a1f1dSLionel Sambuc                 } else
282*0a6a1f1dSLionel Sambuc                     badarg = 1;
283ebfedea0SLionel Sambuc             } else if (!strcmp(*args, "-passin")) {
284ebfedea0SLionel Sambuc                 if (args[1]) {
285ebfedea0SLionel Sambuc                     args++;
286ebfedea0SLionel Sambuc                     passargin = *args;
287*0a6a1f1dSLionel Sambuc                 } else
288*0a6a1f1dSLionel Sambuc                     badarg = 1;
289ebfedea0SLionel Sambuc             } else if (!strcmp(*args, "-passout")) {
290ebfedea0SLionel Sambuc                 if (args[1]) {
291ebfedea0SLionel Sambuc                     args++;
292ebfedea0SLionel Sambuc                     passargout = *args;
293*0a6a1f1dSLionel Sambuc                 } else
294*0a6a1f1dSLionel Sambuc                     badarg = 1;
295ebfedea0SLionel Sambuc             } else if (!strcmp(*args, "-password")) {
296ebfedea0SLionel Sambuc                 if (args[1]) {
297ebfedea0SLionel Sambuc                     args++;
298ebfedea0SLionel Sambuc                     passarg = *args;
299ebfedea0SLionel Sambuc                     noprompt = 1;
300*0a6a1f1dSLionel Sambuc                 } else
301*0a6a1f1dSLionel Sambuc                     badarg = 1;
302ebfedea0SLionel Sambuc             } else if (!strcmp(*args, "-CApath")) {
303ebfedea0SLionel Sambuc                 if (args[1]) {
304ebfedea0SLionel Sambuc                     args++;
305ebfedea0SLionel Sambuc                     CApath = *args;
306*0a6a1f1dSLionel Sambuc                 } else
307*0a6a1f1dSLionel Sambuc                     badarg = 1;
308ebfedea0SLionel Sambuc             } else if (!strcmp(*args, "-CAfile")) {
309ebfedea0SLionel Sambuc                 if (args[1]) {
310ebfedea0SLionel Sambuc                     args++;
311ebfedea0SLionel Sambuc                     CAfile = *args;
312*0a6a1f1dSLionel Sambuc                 } else
313*0a6a1f1dSLionel Sambuc                     badarg = 1;
314ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
315ebfedea0SLionel Sambuc             } else if (!strcmp(*args, "-engine")) {
316ebfedea0SLionel Sambuc                 if (args[1]) {
317ebfedea0SLionel Sambuc                     args++;
318ebfedea0SLionel Sambuc                     engine = *args;
319*0a6a1f1dSLionel Sambuc                 } else
320*0a6a1f1dSLionel Sambuc                     badarg = 1;
321ebfedea0SLionel Sambuc # endif
322*0a6a1f1dSLionel Sambuc             } else
323*0a6a1f1dSLionel Sambuc                 badarg = 1;
324ebfedea0SLionel Sambuc 
325*0a6a1f1dSLionel Sambuc         } else
326*0a6a1f1dSLionel Sambuc             badarg = 1;
327ebfedea0SLionel Sambuc         args++;
328ebfedea0SLionel Sambuc     }
329ebfedea0SLionel Sambuc 
330ebfedea0SLionel Sambuc     if (badarg) {
331ebfedea0SLionel Sambuc         BIO_printf(bio_err, "Usage: pkcs12 [options]\n");
332ebfedea0SLionel Sambuc         BIO_printf(bio_err, "where options are\n");
333ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-export       output PKCS12 file\n");
334ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-chain        add certificate chain\n");
335ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-inkey file   private key if not infile\n");
336ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-certfile f   add all certs in f\n");
337ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-CApath arg   - PEM format directory of CA's\n");
338ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-CAfile arg   - PEM format file of CA's\n");
339ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-name \"name\"  use name as friendly name\n");
340*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
341*0a6a1f1dSLionel Sambuc                    "-caname \"nm\"  use nm as CA friendly name (can be used more than once).\n");
342ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-in  infile   input filename\n");
343ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-out outfile  output filename\n");
344*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
345*0a6a1f1dSLionel Sambuc                    "-noout        don't output anything, just verify.\n");
346ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-nomacver     don't verify MAC.\n");
347ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-nocerts      don't output certificates.\n");
348*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
349*0a6a1f1dSLionel Sambuc                    "-clcerts      only output client certificates.\n");
350ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-cacerts      only output CA certificates.\n");
351ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-nokeys       don't output private keys.\n");
352*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
353*0a6a1f1dSLionel Sambuc                    "-info         give info about PKCS#12 structure.\n");
354ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-des          encrypt private keys with DES\n");
355*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
356*0a6a1f1dSLionel Sambuc                    "-des3         encrypt private keys with triple DES (default)\n");
357ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_IDEA
358ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-idea         encrypt private keys with idea\n");
359ebfedea0SLionel Sambuc # endif
360ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SEED
361ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-seed         encrypt private keys with seed\n");
362ebfedea0SLionel Sambuc # endif
363ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_AES
364ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
365*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
366*0a6a1f1dSLionel Sambuc                    "              encrypt PEM output with cbc aes\n");
367ebfedea0SLionel Sambuc # endif
368ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_CAMELLIA
369ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
370*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
371*0a6a1f1dSLionel Sambuc                    "              encrypt PEM output with cbc camellia\n");
372ebfedea0SLionel Sambuc # endif
373ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-nodes        don't encrypt private keys\n");
374ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-noiter       don't use encryption iteration\n");
375ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-nomaciter    don't use MAC iteration\n");
376ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-maciter      use MAC iteration\n");
377ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-nomac        don't generate MAC\n");
378*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
379*0a6a1f1dSLionel Sambuc                    "-twopass      separate MAC, encryption passwords\n");
380*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
381*0a6a1f1dSLionel Sambuc                    "-descert      encrypt PKCS#12 certificates with triple DES (default RC2-40)\n");
382*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
383*0a6a1f1dSLionel Sambuc                    "-certpbe alg  specify certificate PBE algorithm (default RC2-40)\n");
384*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
385*0a6a1f1dSLionel Sambuc                    "-keypbe alg   specify private key PBE algorithm (default 3DES)\n");
386*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
387*0a6a1f1dSLionel Sambuc                    "-macalg alg   digest algorithm used in MAC (default SHA1)\n");
388ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-keyex        set MS key exchange type\n");
389ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-keysig       set MS key signature type\n");
390*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
391*0a6a1f1dSLionel Sambuc                    "-password p   set import/export password source\n");
392ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-passin p     input file pass phrase source\n");
393ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-passout p    output file pass phrase source\n");
394ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
395*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
396*0a6a1f1dSLionel Sambuc                    "-engine e     use engine e, possibly a hardware device.\n");
397ebfedea0SLionel Sambuc # endif
398*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
399*0a6a1f1dSLionel Sambuc                    LIST_SEPARATOR_CHAR);
400*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
401*0a6a1f1dSLionel Sambuc                    "              load the file (or the files in the directory) into\n");
402ebfedea0SLionel Sambuc         BIO_printf(bio_err, "              the random number generator\n");
403ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-CSP name     Microsoft CSP name\n");
404*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
405*0a6a1f1dSLionel Sambuc                    "-LMK          Add local machine keyset attribute to private key\n");
406ebfedea0SLionel Sambuc         goto end;
407ebfedea0SLionel Sambuc     }
408ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
409ebfedea0SLionel Sambuc     e = setup_engine(bio_err, engine, 0);
410ebfedea0SLionel Sambuc # endif
411ebfedea0SLionel Sambuc 
412ebfedea0SLionel Sambuc     if (passarg) {
413*0a6a1f1dSLionel Sambuc         if (export_cert)
414*0a6a1f1dSLionel Sambuc             passargout = passarg;
415*0a6a1f1dSLionel Sambuc         else
416*0a6a1f1dSLionel Sambuc             passargin = passarg;
417ebfedea0SLionel Sambuc     }
418ebfedea0SLionel Sambuc 
419ebfedea0SLionel Sambuc     if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
420ebfedea0SLionel Sambuc         BIO_printf(bio_err, "Error getting passwords\n");
421ebfedea0SLionel Sambuc         goto end;
422ebfedea0SLionel Sambuc     }
423ebfedea0SLionel Sambuc 
424ebfedea0SLionel Sambuc     if (!cpass) {
425*0a6a1f1dSLionel Sambuc         if (export_cert)
426*0a6a1f1dSLionel Sambuc             cpass = passout;
427*0a6a1f1dSLionel Sambuc         else
428*0a6a1f1dSLionel Sambuc             cpass = passin;
429ebfedea0SLionel Sambuc     }
430ebfedea0SLionel Sambuc 
431ebfedea0SLionel Sambuc     if (cpass) {
432ebfedea0SLionel Sambuc         mpass = cpass;
433ebfedea0SLionel Sambuc         noprompt = 1;
434ebfedea0SLionel Sambuc     } else {
435ebfedea0SLionel Sambuc         cpass = pass;
436ebfedea0SLionel Sambuc         mpass = macpass;
437ebfedea0SLionel Sambuc     }
438ebfedea0SLionel Sambuc 
439ebfedea0SLionel Sambuc     if (export_cert || inrand) {
440ebfedea0SLionel Sambuc         app_RAND_load_file(NULL, bio_err, (inrand != NULL));
441ebfedea0SLionel Sambuc         if (inrand != NULL)
442ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
443ebfedea0SLionel Sambuc                        app_RAND_load_files(inrand));
444ebfedea0SLionel Sambuc     }
445ebfedea0SLionel Sambuc     ERR_load_crypto_strings();
446ebfedea0SLionel Sambuc 
447ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
448ebfedea0SLionel Sambuc     CRYPTO_push_info("read files");
449ebfedea0SLionel Sambuc # endif
450ebfedea0SLionel Sambuc 
451*0a6a1f1dSLionel Sambuc     if (!infile)
452*0a6a1f1dSLionel Sambuc         in = BIO_new_fp(stdin, BIO_NOCLOSE);
453*0a6a1f1dSLionel Sambuc     else
454*0a6a1f1dSLionel Sambuc         in = BIO_new_file(infile, "rb");
455ebfedea0SLionel Sambuc     if (!in) {
456ebfedea0SLionel Sambuc         BIO_printf(bio_err, "Error opening input file %s\n",
457ebfedea0SLionel Sambuc                    infile ? infile : "<stdin>");
458ebfedea0SLionel Sambuc         perror(infile);
459ebfedea0SLionel Sambuc         goto end;
460ebfedea0SLionel Sambuc     }
461ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
462ebfedea0SLionel Sambuc     CRYPTO_pop_info();
463ebfedea0SLionel Sambuc     CRYPTO_push_info("write files");
464ebfedea0SLionel Sambuc # endif
465ebfedea0SLionel Sambuc 
466ebfedea0SLionel Sambuc     if (!outfile) {
467ebfedea0SLionel Sambuc         out = BIO_new_fp(stdout, BIO_NOCLOSE);
468ebfedea0SLionel Sambuc # ifdef OPENSSL_SYS_VMS
469ebfedea0SLionel Sambuc         {
470ebfedea0SLionel Sambuc             BIO *tmpbio = BIO_new(BIO_f_linebuffer());
471ebfedea0SLionel Sambuc             out = BIO_push(tmpbio, out);
472ebfedea0SLionel Sambuc         }
473ebfedea0SLionel Sambuc # endif
474*0a6a1f1dSLionel Sambuc     } else
475*0a6a1f1dSLionel Sambuc         out = BIO_new_file(outfile, "wb");
476ebfedea0SLionel Sambuc     if (!out) {
477ebfedea0SLionel Sambuc         BIO_printf(bio_err, "Error opening output file %s\n",
478ebfedea0SLionel Sambuc                    outfile ? outfile : "<stdout>");
479ebfedea0SLionel Sambuc         perror(outfile);
480ebfedea0SLionel Sambuc         goto end;
481ebfedea0SLionel Sambuc     }
482ebfedea0SLionel Sambuc     if (twopass) {
483ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
484ebfedea0SLionel Sambuc         CRYPTO_push_info("read MAC password");
485ebfedea0SLionel Sambuc # endif
486*0a6a1f1dSLionel Sambuc         if (EVP_read_pw_string
487*0a6a1f1dSLionel Sambuc             (macpass, sizeof macpass, "Enter MAC Password:", export_cert)) {
488ebfedea0SLionel Sambuc             BIO_printf(bio_err, "Can't read Password\n");
489ebfedea0SLionel Sambuc             goto end;
490ebfedea0SLionel Sambuc         }
491ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
492ebfedea0SLionel Sambuc         CRYPTO_pop_info();
493ebfedea0SLionel Sambuc # endif
494ebfedea0SLionel Sambuc     }
495ebfedea0SLionel Sambuc 
496ebfedea0SLionel Sambuc     if (export_cert) {
497ebfedea0SLionel Sambuc         EVP_PKEY *key = NULL;
498ebfedea0SLionel Sambuc         X509 *ucert = NULL, *x = NULL;
499ebfedea0SLionel Sambuc         STACK_OF(X509) *certs = NULL;
500ebfedea0SLionel Sambuc         const EVP_MD *macmd = NULL;
501ebfedea0SLionel Sambuc         unsigned char *catmp = NULL;
502ebfedea0SLionel Sambuc         int i;
503ebfedea0SLionel Sambuc 
504*0a6a1f1dSLionel Sambuc         if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
505ebfedea0SLionel Sambuc             BIO_printf(bio_err, "Nothing to do!\n");
506ebfedea0SLionel Sambuc             goto export_end;
507ebfedea0SLionel Sambuc         }
508ebfedea0SLionel Sambuc 
509ebfedea0SLionel Sambuc         if (options & NOCERTS)
510ebfedea0SLionel Sambuc             chain = 0;
511ebfedea0SLionel Sambuc 
512ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
513ebfedea0SLionel Sambuc         CRYPTO_push_info("process -export_cert");
514ebfedea0SLionel Sambuc         CRYPTO_push_info("reading private key");
515ebfedea0SLionel Sambuc # endif
516*0a6a1f1dSLionel Sambuc         if (!(options & NOKEYS)) {
517ebfedea0SLionel Sambuc             key = load_key(bio_err, keyname ? keyname : infile,
518ebfedea0SLionel Sambuc                            FORMAT_PEM, 1, passin, e, "private key");
519ebfedea0SLionel Sambuc             if (!key)
520ebfedea0SLionel Sambuc                 goto export_end;
521ebfedea0SLionel Sambuc         }
522ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
523ebfedea0SLionel Sambuc         CRYPTO_pop_info();
524ebfedea0SLionel Sambuc         CRYPTO_push_info("reading certs from input");
525ebfedea0SLionel Sambuc # endif
526ebfedea0SLionel Sambuc 
527ebfedea0SLionel Sambuc         /* Load in all certs in input file */
528*0a6a1f1dSLionel Sambuc         if (!(options & NOCERTS)) {
529ebfedea0SLionel Sambuc             certs = load_certs(bio_err, infile, FORMAT_PEM, NULL, e,
530ebfedea0SLionel Sambuc                                "certificates");
531ebfedea0SLionel Sambuc             if (!certs)
532ebfedea0SLionel Sambuc                 goto export_end;
533ebfedea0SLionel Sambuc 
534*0a6a1f1dSLionel Sambuc             if (key) {
535ebfedea0SLionel Sambuc                 /* Look for matching private key */
536*0a6a1f1dSLionel Sambuc                 for (i = 0; i < sk_X509_num(certs); i++) {
537ebfedea0SLionel Sambuc                     x = sk_X509_value(certs, i);
538*0a6a1f1dSLionel Sambuc                     if (X509_check_private_key(x, key)) {
539ebfedea0SLionel Sambuc                         ucert = x;
540ebfedea0SLionel Sambuc                         /* Zero keyid and alias */
541ebfedea0SLionel Sambuc                         X509_keyid_set1(ucert, NULL, 0);
542ebfedea0SLionel Sambuc                         X509_alias_set1(ucert, NULL, 0);
543ebfedea0SLionel Sambuc                         /* Remove from list */
544ebfedea0SLionel Sambuc                         (void)sk_X509_delete(certs, i);
545ebfedea0SLionel Sambuc                         break;
546ebfedea0SLionel Sambuc                     }
547ebfedea0SLionel Sambuc                 }
548*0a6a1f1dSLionel Sambuc                 if (!ucert) {
549*0a6a1f1dSLionel Sambuc                     BIO_printf(bio_err,
550*0a6a1f1dSLionel Sambuc                                "No certificate matches private key\n");
551ebfedea0SLionel Sambuc                     goto export_end;
552ebfedea0SLionel Sambuc                 }
553ebfedea0SLionel Sambuc             }
554ebfedea0SLionel Sambuc 
555ebfedea0SLionel Sambuc         }
556ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
557ebfedea0SLionel Sambuc         CRYPTO_pop_info();
558ebfedea0SLionel Sambuc         CRYPTO_push_info("reading certs from input 2");
559ebfedea0SLionel Sambuc # endif
560ebfedea0SLionel Sambuc 
561ebfedea0SLionel Sambuc         /* Add any more certificates asked for */
562*0a6a1f1dSLionel Sambuc         if (certfile) {
563ebfedea0SLionel Sambuc             STACK_OF(X509) *morecerts = NULL;
564ebfedea0SLionel Sambuc             if (!(morecerts = load_certs(bio_err, certfile, FORMAT_PEM,
565ebfedea0SLionel Sambuc                                          NULL, e,
566ebfedea0SLionel Sambuc                                          "certificates from certfile")))
567ebfedea0SLionel Sambuc                 goto export_end;
568ebfedea0SLionel Sambuc             while (sk_X509_num(morecerts) > 0)
569ebfedea0SLionel Sambuc                 sk_X509_push(certs, sk_X509_shift(morecerts));
570ebfedea0SLionel Sambuc             sk_X509_free(morecerts);
571ebfedea0SLionel Sambuc         }
572ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
573ebfedea0SLionel Sambuc         CRYPTO_pop_info();
574ebfedea0SLionel Sambuc         CRYPTO_push_info("reading certs from certfile");
575ebfedea0SLionel Sambuc # endif
576ebfedea0SLionel Sambuc 
577ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
578ebfedea0SLionel Sambuc         CRYPTO_pop_info();
579ebfedea0SLionel Sambuc         CRYPTO_push_info("building chain");
580ebfedea0SLionel Sambuc # endif
581ebfedea0SLionel Sambuc 
582ebfedea0SLionel Sambuc         /* If chaining get chain from user cert */
583ebfedea0SLionel Sambuc         if (chain) {
584ebfedea0SLionel Sambuc             int vret;
585ebfedea0SLionel Sambuc             STACK_OF(X509) *chain2;
586ebfedea0SLionel Sambuc             X509_STORE *store = X509_STORE_new();
587*0a6a1f1dSLionel Sambuc             if (!store) {
588ebfedea0SLionel Sambuc                 BIO_printf(bio_err, "Memory allocation error\n");
589ebfedea0SLionel Sambuc                 goto export_end;
590ebfedea0SLionel Sambuc             }
591ebfedea0SLionel Sambuc             if (!X509_STORE_load_locations(store, CAfile, CApath))
592ebfedea0SLionel Sambuc                 X509_STORE_set_default_paths(store);
593ebfedea0SLionel Sambuc 
594ebfedea0SLionel Sambuc             vret = get_cert_chain(ucert, store, &chain2);
595ebfedea0SLionel Sambuc             X509_STORE_free(store);
596ebfedea0SLionel Sambuc 
597ebfedea0SLionel Sambuc             if (!vret) {
598ebfedea0SLionel Sambuc                 /* Exclude verified certificate */
599ebfedea0SLionel Sambuc                 for (i = 1; i < sk_X509_num(chain2); i++)
600ebfedea0SLionel Sambuc                     sk_X509_push(certs, sk_X509_value(chain2, i));
601ebfedea0SLionel Sambuc                 /* Free first certificate */
602ebfedea0SLionel Sambuc                 X509_free(sk_X509_value(chain2, 0));
603ebfedea0SLionel Sambuc                 sk_X509_free(chain2);
604ebfedea0SLionel Sambuc             } else {
605ebfedea0SLionel Sambuc                 if (vret >= 0)
606ebfedea0SLionel Sambuc                     BIO_printf(bio_err, "Error %s getting chain.\n",
607ebfedea0SLionel Sambuc                                X509_verify_cert_error_string(vret));
608ebfedea0SLionel Sambuc                 else
609ebfedea0SLionel Sambuc                     ERR_print_errors(bio_err);
610ebfedea0SLionel Sambuc                 goto export_end;
611ebfedea0SLionel Sambuc             }
612ebfedea0SLionel Sambuc         }
613ebfedea0SLionel Sambuc 
614ebfedea0SLionel Sambuc         /* Add any CA names */
615ebfedea0SLionel Sambuc 
616*0a6a1f1dSLionel Sambuc         for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
617ebfedea0SLionel Sambuc             catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
618ebfedea0SLionel Sambuc             X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
619ebfedea0SLionel Sambuc         }
620ebfedea0SLionel Sambuc 
621ebfedea0SLionel Sambuc         if (csp_name && key)
622ebfedea0SLionel Sambuc             EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
623*0a6a1f1dSLionel Sambuc                                       MBSTRING_ASC, (unsigned char *)csp_name,
624*0a6a1f1dSLionel Sambuc                                       -1);
625ebfedea0SLionel Sambuc 
626ebfedea0SLionel Sambuc         if (add_lmk && key)
627ebfedea0SLionel Sambuc             EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
628ebfedea0SLionel Sambuc 
629ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
630ebfedea0SLionel Sambuc         CRYPTO_pop_info();
631ebfedea0SLionel Sambuc         CRYPTO_push_info("reading password");
632ebfedea0SLionel Sambuc # endif
633ebfedea0SLionel Sambuc 
634ebfedea0SLionel Sambuc         if (!noprompt &&
635*0a6a1f1dSLionel Sambuc             EVP_read_pw_string(pass, sizeof pass, "Enter Export Password:",
636*0a6a1f1dSLionel Sambuc                                1)) {
637ebfedea0SLionel Sambuc             BIO_printf(bio_err, "Can't read Password\n");
638ebfedea0SLionel Sambuc             goto export_end;
639ebfedea0SLionel Sambuc         }
640*0a6a1f1dSLionel Sambuc         if (!twopass)
641*0a6a1f1dSLionel Sambuc             BUF_strlcpy(macpass, pass, sizeof macpass);
642ebfedea0SLionel Sambuc 
643ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
644ebfedea0SLionel Sambuc         CRYPTO_pop_info();
645ebfedea0SLionel Sambuc         CRYPTO_push_info("creating PKCS#12 structure");
646ebfedea0SLionel Sambuc # endif
647ebfedea0SLionel Sambuc 
648ebfedea0SLionel Sambuc         p12 = PKCS12_create(cpass, name, key, ucert, certs,
649ebfedea0SLionel Sambuc                             key_pbe, cert_pbe, iter, -1, keytype);
650ebfedea0SLionel Sambuc 
651*0a6a1f1dSLionel Sambuc         if (!p12) {
652ebfedea0SLionel Sambuc             ERR_print_errors(bio_err);
653ebfedea0SLionel Sambuc             goto export_end;
654ebfedea0SLionel Sambuc         }
655ebfedea0SLionel Sambuc 
656*0a6a1f1dSLionel Sambuc         if (macalg) {
657ebfedea0SLionel Sambuc             macmd = EVP_get_digestbyname(macalg);
658*0a6a1f1dSLionel Sambuc             if (!macmd) {
659*0a6a1f1dSLionel Sambuc                 BIO_printf(bio_err, "Unknown digest algorithm %s\n", macalg);
660ebfedea0SLionel Sambuc             }
661ebfedea0SLionel Sambuc         }
662ebfedea0SLionel Sambuc 
663ebfedea0SLionel Sambuc         if (maciter != -1)
664ebfedea0SLionel Sambuc             PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
665ebfedea0SLionel Sambuc 
666ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
667ebfedea0SLionel Sambuc         CRYPTO_pop_info();
668ebfedea0SLionel Sambuc         CRYPTO_push_info("writing pkcs12");
669ebfedea0SLionel Sambuc # endif
670ebfedea0SLionel Sambuc 
671ebfedea0SLionel Sambuc         i2d_PKCS12_bio(out, p12);
672ebfedea0SLionel Sambuc 
673ebfedea0SLionel Sambuc         ret = 0;
674ebfedea0SLionel Sambuc 
675ebfedea0SLionel Sambuc  export_end:
676ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
677ebfedea0SLionel Sambuc         CRYPTO_pop_info();
678ebfedea0SLionel Sambuc         CRYPTO_pop_info();
679ebfedea0SLionel Sambuc         CRYPTO_push_info("process -export_cert: freeing");
680ebfedea0SLionel Sambuc # endif
681ebfedea0SLionel Sambuc 
682*0a6a1f1dSLionel Sambuc         if (key)
683*0a6a1f1dSLionel Sambuc             EVP_PKEY_free(key);
684*0a6a1f1dSLionel Sambuc         if (certs)
685*0a6a1f1dSLionel Sambuc             sk_X509_pop_free(certs, X509_free);
686*0a6a1f1dSLionel Sambuc         if (ucert)
687*0a6a1f1dSLionel Sambuc             X509_free(ucert);
688ebfedea0SLionel Sambuc 
689ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
690ebfedea0SLionel Sambuc         CRYPTO_pop_info();
691ebfedea0SLionel Sambuc # endif
692ebfedea0SLionel Sambuc         goto end;
693ebfedea0SLionel Sambuc 
694ebfedea0SLionel Sambuc     }
695ebfedea0SLionel Sambuc 
696ebfedea0SLionel Sambuc     if (!(p12 = d2i_PKCS12_bio(in, NULL))) {
697ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
698ebfedea0SLionel Sambuc         goto end;
699ebfedea0SLionel Sambuc     }
700ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
701ebfedea0SLionel Sambuc     CRYPTO_push_info("read import password");
702ebfedea0SLionel Sambuc # endif
703*0a6a1f1dSLionel Sambuc     if (!noprompt
704*0a6a1f1dSLionel Sambuc         && EVP_read_pw_string(pass, sizeof pass, "Enter Import Password:",
705*0a6a1f1dSLionel Sambuc                               0)) {
706ebfedea0SLionel Sambuc         BIO_printf(bio_err, "Can't read Password\n");
707ebfedea0SLionel Sambuc         goto end;
708ebfedea0SLionel Sambuc     }
709ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
710ebfedea0SLionel Sambuc     CRYPTO_pop_info();
711ebfedea0SLionel Sambuc # endif
712ebfedea0SLionel Sambuc 
713*0a6a1f1dSLionel Sambuc     if (!twopass)
714*0a6a1f1dSLionel Sambuc         BUF_strlcpy(macpass, pass, sizeof macpass);
715ebfedea0SLionel Sambuc 
716*0a6a1f1dSLionel Sambuc     if ((options & INFO) && p12->mac)
717*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err, "MAC Iteration %ld\n",
718*0a6a1f1dSLionel Sambuc                    p12->mac->iter ? ASN1_INTEGER_get(p12->mac->iter) : 1);
719ebfedea0SLionel Sambuc     if (macver) {
720ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
721ebfedea0SLionel Sambuc         CRYPTO_push_info("verify MAC");
722ebfedea0SLionel Sambuc # endif
723ebfedea0SLionel Sambuc         /* If we enter empty password try no password first */
724ebfedea0SLionel Sambuc         if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
725ebfedea0SLionel Sambuc             /* If mac and crypto pass the same set it to NULL too */
726*0a6a1f1dSLionel Sambuc             if (!twopass)
727*0a6a1f1dSLionel Sambuc                 cpass = NULL;
728ebfedea0SLionel Sambuc         } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
729ebfedea0SLionel Sambuc             BIO_printf(bio_err, "Mac verify error: invalid password?\n");
730ebfedea0SLionel Sambuc             ERR_print_errors(bio_err);
731ebfedea0SLionel Sambuc             goto end;
732ebfedea0SLionel Sambuc         }
733ebfedea0SLionel Sambuc         BIO_printf(bio_err, "MAC verified OK\n");
734ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
735ebfedea0SLionel Sambuc         CRYPTO_pop_info();
736ebfedea0SLionel Sambuc # endif
737ebfedea0SLionel Sambuc     }
738ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
739ebfedea0SLionel Sambuc     CRYPTO_push_info("output keys and certificates");
740ebfedea0SLionel Sambuc # endif
741ebfedea0SLionel Sambuc     if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout)) {
742ebfedea0SLionel Sambuc         BIO_printf(bio_err, "Error outputting keys and certificates\n");
743ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
744ebfedea0SLionel Sambuc         goto end;
745ebfedea0SLionel Sambuc     }
746ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
747ebfedea0SLionel Sambuc     CRYPTO_pop_info();
748ebfedea0SLionel Sambuc # endif
749ebfedea0SLionel Sambuc     ret = 0;
750ebfedea0SLionel Sambuc  end:
751*0a6a1f1dSLionel Sambuc     if (p12)
752*0a6a1f1dSLionel Sambuc         PKCS12_free(p12);
753*0a6a1f1dSLionel Sambuc     if (export_cert || inrand)
754*0a6a1f1dSLionel Sambuc         app_RAND_write_file(NULL, bio_err);
755ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
756ebfedea0SLionel Sambuc     CRYPTO_remove_all_info();
757ebfedea0SLionel Sambuc # endif
758ebfedea0SLionel Sambuc     BIO_free(in);
759ebfedea0SLionel Sambuc     BIO_free_all(out);
760*0a6a1f1dSLionel Sambuc     if (canames)
761*0a6a1f1dSLionel Sambuc         sk_OPENSSL_STRING_free(canames);
762*0a6a1f1dSLionel Sambuc     if (passin)
763*0a6a1f1dSLionel Sambuc         OPENSSL_free(passin);
764*0a6a1f1dSLionel Sambuc     if (passout)
765*0a6a1f1dSLionel Sambuc         OPENSSL_free(passout);
766ebfedea0SLionel Sambuc     apps_shutdown();
767ebfedea0SLionel Sambuc     OPENSSL_EXIT(ret);
768ebfedea0SLionel Sambuc }
769ebfedea0SLionel Sambuc 
dump_certs_keys_p12(BIO * out,PKCS12 * p12,char * pass,int passlen,int options,char * pempass)770ebfedea0SLionel Sambuc int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass,
771ebfedea0SLionel Sambuc                         int passlen, int options, char *pempass)
772ebfedea0SLionel Sambuc {
773ebfedea0SLionel Sambuc     STACK_OF(PKCS7) *asafes = NULL;
774ebfedea0SLionel Sambuc     STACK_OF(PKCS12_SAFEBAG) *bags;
775ebfedea0SLionel Sambuc     int i, bagnid;
776ebfedea0SLionel Sambuc     int ret = 0;
777ebfedea0SLionel Sambuc     PKCS7 *p7;
778ebfedea0SLionel Sambuc 
779*0a6a1f1dSLionel Sambuc     if (!(asafes = PKCS12_unpack_authsafes(p12)))
780*0a6a1f1dSLionel Sambuc         return 0;
781ebfedea0SLionel Sambuc     for (i = 0; i < sk_PKCS7_num(asafes); i++) {
782ebfedea0SLionel Sambuc         p7 = sk_PKCS7_value(asafes, i);
783ebfedea0SLionel Sambuc         bagnid = OBJ_obj2nid(p7->type);
784ebfedea0SLionel Sambuc         if (bagnid == NID_pkcs7_data) {
785ebfedea0SLionel Sambuc             bags = PKCS12_unpack_p7data(p7);
786*0a6a1f1dSLionel Sambuc             if (options & INFO)
787*0a6a1f1dSLionel Sambuc                 BIO_printf(bio_err, "PKCS7 Data\n");
788ebfedea0SLionel Sambuc         } else if (bagnid == NID_pkcs7_encrypted) {
789ebfedea0SLionel Sambuc             if (options & INFO) {
790ebfedea0SLionel Sambuc                 BIO_printf(bio_err, "PKCS7 Encrypted data: ");
791*0a6a1f1dSLionel Sambuc                 alg_print(bio_err, p7->d.encrypted->enc_data->algorithm);
792ebfedea0SLionel Sambuc             }
793ebfedea0SLionel Sambuc             bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
794*0a6a1f1dSLionel Sambuc         } else
795*0a6a1f1dSLionel Sambuc             continue;
796*0a6a1f1dSLionel Sambuc         if (!bags)
797*0a6a1f1dSLionel Sambuc             goto err;
798ebfedea0SLionel Sambuc         if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
799ebfedea0SLionel Sambuc                                    options, pempass)) {
800ebfedea0SLionel Sambuc             sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
801ebfedea0SLionel Sambuc             goto err;
802ebfedea0SLionel Sambuc         }
803ebfedea0SLionel Sambuc         sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
804ebfedea0SLionel Sambuc         bags = NULL;
805ebfedea0SLionel Sambuc     }
806ebfedea0SLionel Sambuc     ret = 1;
807ebfedea0SLionel Sambuc 
808ebfedea0SLionel Sambuc  err:
809ebfedea0SLionel Sambuc 
810ebfedea0SLionel Sambuc     if (asafes)
811ebfedea0SLionel Sambuc         sk_PKCS7_pop_free(asafes, PKCS7_free);
812ebfedea0SLionel Sambuc     return ret;
813ebfedea0SLionel Sambuc }
814ebfedea0SLionel Sambuc 
dump_certs_pkeys_bags(BIO * out,STACK_OF (PKCS12_SAFEBAG)* bags,char * pass,int passlen,int options,char * pempass)815ebfedea0SLionel Sambuc int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags,
816ebfedea0SLionel Sambuc                           char *pass, int passlen, int options, char *pempass)
817ebfedea0SLionel Sambuc {
818ebfedea0SLionel Sambuc     int i;
819ebfedea0SLionel Sambuc     for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
820ebfedea0SLionel Sambuc         if (!dump_certs_pkeys_bag(out,
821ebfedea0SLionel Sambuc                                   sk_PKCS12_SAFEBAG_value(bags, i),
822*0a6a1f1dSLionel Sambuc                                   pass, passlen, options, pempass))
823ebfedea0SLionel Sambuc             return 0;
824ebfedea0SLionel Sambuc     }
825ebfedea0SLionel Sambuc     return 1;
826ebfedea0SLionel Sambuc }
827ebfedea0SLionel Sambuc 
dump_certs_pkeys_bag(BIO * out,PKCS12_SAFEBAG * bag,char * pass,int passlen,int options,char * pempass)828ebfedea0SLionel Sambuc int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bag, char *pass,
829ebfedea0SLionel Sambuc                          int passlen, int options, char *pempass)
830ebfedea0SLionel Sambuc {
831ebfedea0SLionel Sambuc     EVP_PKEY *pkey;
832ebfedea0SLionel Sambuc     PKCS8_PRIV_KEY_INFO *p8;
833ebfedea0SLionel Sambuc     X509 *x509;
834ebfedea0SLionel Sambuc 
835*0a6a1f1dSLionel Sambuc     switch (M_PKCS12_bag_type(bag)) {
836ebfedea0SLionel Sambuc     case NID_keyBag:
837*0a6a1f1dSLionel Sambuc         if (options & INFO)
838*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "Key bag\n");
839*0a6a1f1dSLionel Sambuc         if (options & NOKEYS)
840*0a6a1f1dSLionel Sambuc             return 1;
841ebfedea0SLionel Sambuc         print_attribs(out, bag->attrib, "Bag Attributes");
842ebfedea0SLionel Sambuc         p8 = bag->value.keybag;
843*0a6a1f1dSLionel Sambuc         if (!(pkey = EVP_PKCS82PKEY(p8)))
844*0a6a1f1dSLionel Sambuc             return 0;
845ebfedea0SLionel Sambuc         print_attribs(out, p8->attributes, "Key Attributes");
846ebfedea0SLionel Sambuc         PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
847ebfedea0SLionel Sambuc         EVP_PKEY_free(pkey);
848ebfedea0SLionel Sambuc         break;
849ebfedea0SLionel Sambuc 
850ebfedea0SLionel Sambuc     case NID_pkcs8ShroudedKeyBag:
851ebfedea0SLionel Sambuc         if (options & INFO) {
852ebfedea0SLionel Sambuc             BIO_printf(bio_err, "Shrouded Keybag: ");
853ebfedea0SLionel Sambuc             alg_print(bio_err, bag->value.shkeybag->algor);
854ebfedea0SLionel Sambuc         }
855*0a6a1f1dSLionel Sambuc         if (options & NOKEYS)
856*0a6a1f1dSLionel Sambuc             return 1;
857ebfedea0SLionel Sambuc         print_attribs(out, bag->attrib, "Bag Attributes");
858ebfedea0SLionel Sambuc         if (!(p8 = PKCS12_decrypt_skey(bag, pass, passlen)))
859ebfedea0SLionel Sambuc             return 0;
860ebfedea0SLionel Sambuc         if (!(pkey = EVP_PKCS82PKEY(p8))) {
861ebfedea0SLionel Sambuc             PKCS8_PRIV_KEY_INFO_free(p8);
862ebfedea0SLionel Sambuc             return 0;
863ebfedea0SLionel Sambuc         }
864ebfedea0SLionel Sambuc         print_attribs(out, p8->attributes, "Key Attributes");
865ebfedea0SLionel Sambuc         PKCS8_PRIV_KEY_INFO_free(p8);
866ebfedea0SLionel Sambuc         PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
867ebfedea0SLionel Sambuc         EVP_PKEY_free(pkey);
868ebfedea0SLionel Sambuc         break;
869ebfedea0SLionel Sambuc 
870ebfedea0SLionel Sambuc     case NID_certBag:
871*0a6a1f1dSLionel Sambuc         if (options & INFO)
872*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "Certificate bag\n");
873*0a6a1f1dSLionel Sambuc         if (options & NOCERTS)
874*0a6a1f1dSLionel Sambuc             return 1;
875ebfedea0SLionel Sambuc         if (PKCS12_get_attr(bag, NID_localKeyID)) {
876*0a6a1f1dSLionel Sambuc             if (options & CACERTS)
877*0a6a1f1dSLionel Sambuc                 return 1;
878*0a6a1f1dSLionel Sambuc         } else if (options & CLCERTS)
879*0a6a1f1dSLionel Sambuc             return 1;
880ebfedea0SLionel Sambuc         print_attribs(out, bag->attrib, "Bag Attributes");
881ebfedea0SLionel Sambuc         if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate)
882ebfedea0SLionel Sambuc             return 1;
883*0a6a1f1dSLionel Sambuc         if (!(x509 = PKCS12_certbag2x509(bag)))
884*0a6a1f1dSLionel Sambuc             return 0;
885ebfedea0SLionel Sambuc         dump_cert_text(out, x509);
886ebfedea0SLionel Sambuc         PEM_write_bio_X509(out, x509);
887ebfedea0SLionel Sambuc         X509_free(x509);
888ebfedea0SLionel Sambuc         break;
889ebfedea0SLionel Sambuc 
890ebfedea0SLionel Sambuc     case NID_safeContentsBag:
891*0a6a1f1dSLionel Sambuc         if (options & INFO)
892*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "Safe Contents bag\n");
893ebfedea0SLionel Sambuc         print_attribs(out, bag->attrib, "Bag Attributes");
894ebfedea0SLionel Sambuc         return dump_certs_pkeys_bags(out, bag->value.safes, pass,
895ebfedea0SLionel Sambuc                                      passlen, options, pempass);
896ebfedea0SLionel Sambuc 
897ebfedea0SLionel Sambuc     default:
898ebfedea0SLionel Sambuc         BIO_printf(bio_err, "Warning unsupported bag type: ");
899ebfedea0SLionel Sambuc         i2a_ASN1_OBJECT(bio_err, bag->type);
900ebfedea0SLionel Sambuc         BIO_printf(bio_err, "\n");
901ebfedea0SLionel Sambuc         return 1;
902ebfedea0SLionel Sambuc         break;
903ebfedea0SLionel Sambuc     }
904ebfedea0SLionel Sambuc     return 1;
905ebfedea0SLionel Sambuc }
906ebfedea0SLionel Sambuc 
907ebfedea0SLionel Sambuc /* Given a single certificate return a verified chain or NULL if error */
908ebfedea0SLionel Sambuc 
909ebfedea0SLionel Sambuc /* Hope this is OK .... */
910ebfedea0SLionel Sambuc 
get_cert_chain(X509 * cert,X509_STORE * store,STACK_OF (X509)** chain)911ebfedea0SLionel Sambuc int get_cert_chain(X509 *cert, X509_STORE *store, STACK_OF(X509) **chain)
912ebfedea0SLionel Sambuc {
913ebfedea0SLionel Sambuc     X509_STORE_CTX store_ctx;
914ebfedea0SLionel Sambuc     STACK_OF(X509) *chn;
915ebfedea0SLionel Sambuc     int i = 0;
916ebfedea0SLionel Sambuc 
917*0a6a1f1dSLionel Sambuc     /*
918*0a6a1f1dSLionel Sambuc      * FIXME: Should really check the return status of X509_STORE_CTX_init
919*0a6a1f1dSLionel Sambuc      * for an error, but how that fits into the return value of this function
920*0a6a1f1dSLionel Sambuc      * is less obvious.
921*0a6a1f1dSLionel Sambuc      */
922ebfedea0SLionel Sambuc     X509_STORE_CTX_init(&store_ctx, store, cert, NULL);
923ebfedea0SLionel Sambuc     if (X509_verify_cert(&store_ctx) <= 0) {
924ebfedea0SLionel Sambuc         i = X509_STORE_CTX_get_error(&store_ctx);
925ebfedea0SLionel Sambuc         if (i == 0)
926*0a6a1f1dSLionel Sambuc             /*
927*0a6a1f1dSLionel Sambuc              * avoid returning 0 if X509_verify_cert() did not set an
928*0a6a1f1dSLionel Sambuc              * appropriate error value in the context
929*0a6a1f1dSLionel Sambuc              */
930ebfedea0SLionel Sambuc             i = -1;
931ebfedea0SLionel Sambuc         chn = NULL;
932ebfedea0SLionel Sambuc         goto err;
933ebfedea0SLionel Sambuc     } else
934ebfedea0SLionel Sambuc         chn = X509_STORE_CTX_get1_chain(&store_ctx);
935ebfedea0SLionel Sambuc  err:
936ebfedea0SLionel Sambuc     X509_STORE_CTX_cleanup(&store_ctx);
937ebfedea0SLionel Sambuc     *chain = chn;
938ebfedea0SLionel Sambuc 
939ebfedea0SLionel Sambuc     return i;
940ebfedea0SLionel Sambuc }
941ebfedea0SLionel Sambuc 
alg_print(BIO * x,X509_ALGOR * alg)942ebfedea0SLionel Sambuc int alg_print(BIO *x, X509_ALGOR *alg)
943ebfedea0SLionel Sambuc {
944ebfedea0SLionel Sambuc     PBEPARAM *pbe;
945ebfedea0SLionel Sambuc     const unsigned char *p;
946ebfedea0SLionel Sambuc     p = alg->parameter->value.sequence->data;
947ebfedea0SLionel Sambuc     pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length);
948ebfedea0SLionel Sambuc     if (!pbe)
949ebfedea0SLionel Sambuc         return 1;
950ebfedea0SLionel Sambuc     BIO_printf(bio_err, "%s, Iteration %ld\n",
951ebfedea0SLionel Sambuc                OBJ_nid2ln(OBJ_obj2nid(alg->algorithm)),
952ebfedea0SLionel Sambuc                ASN1_INTEGER_get(pbe->iter));
953ebfedea0SLionel Sambuc     PBEPARAM_free(pbe);
954ebfedea0SLionel Sambuc     return 1;
955ebfedea0SLionel Sambuc }
956ebfedea0SLionel Sambuc 
957ebfedea0SLionel Sambuc /* Load all certificates from a given file */
958ebfedea0SLionel Sambuc 
cert_load(BIO * in,STACK_OF (X509)* sk)959ebfedea0SLionel Sambuc int cert_load(BIO *in, STACK_OF(X509) *sk)
960ebfedea0SLionel Sambuc {
961ebfedea0SLionel Sambuc     int ret;
962ebfedea0SLionel Sambuc     X509 *cert;
963ebfedea0SLionel Sambuc     ret = 0;
964ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
965ebfedea0SLionel Sambuc     CRYPTO_push_info("cert_load(): reading one cert");
966ebfedea0SLionel Sambuc # endif
967ebfedea0SLionel Sambuc     while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
968ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
969ebfedea0SLionel Sambuc         CRYPTO_pop_info();
970ebfedea0SLionel Sambuc # endif
971ebfedea0SLionel Sambuc         ret = 1;
972ebfedea0SLionel Sambuc         sk_X509_push(sk, cert);
973ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
974ebfedea0SLionel Sambuc         CRYPTO_push_info("cert_load(): reading one cert");
975ebfedea0SLionel Sambuc # endif
976ebfedea0SLionel Sambuc     }
977ebfedea0SLionel Sambuc # ifdef CRYPTO_MDEBUG
978ebfedea0SLionel Sambuc     CRYPTO_pop_info();
979ebfedea0SLionel Sambuc # endif
980*0a6a1f1dSLionel Sambuc     if (ret)
981*0a6a1f1dSLionel Sambuc         ERR_clear_error();
982ebfedea0SLionel Sambuc     return ret;
983ebfedea0SLionel Sambuc }
984ebfedea0SLionel Sambuc 
985ebfedea0SLionel Sambuc /* Generalised attribute print: handle PKCS#8 and bag attributes */
986ebfedea0SLionel Sambuc 
print_attribs(BIO * out,STACK_OF (X509_ATTRIBUTE)* attrlst,const char * name)987*0a6a1f1dSLionel Sambuc int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,
988*0a6a1f1dSLionel Sambuc                   const char *name)
989ebfedea0SLionel Sambuc {
990ebfedea0SLionel Sambuc     X509_ATTRIBUTE *attr;
991ebfedea0SLionel Sambuc     ASN1_TYPE *av;
992ebfedea0SLionel Sambuc     char *value;
993ebfedea0SLionel Sambuc     int i, attr_nid;
994ebfedea0SLionel Sambuc     if (!attrlst) {
995ebfedea0SLionel Sambuc         BIO_printf(out, "%s: <No Attributes>\n", name);
996ebfedea0SLionel Sambuc         return 1;
997ebfedea0SLionel Sambuc     }
998ebfedea0SLionel Sambuc     if (!sk_X509_ATTRIBUTE_num(attrlst)) {
999ebfedea0SLionel Sambuc         BIO_printf(out, "%s: <Empty Attributes>\n", name);
1000ebfedea0SLionel Sambuc         return 1;
1001ebfedea0SLionel Sambuc     }
1002ebfedea0SLionel Sambuc     BIO_printf(out, "%s\n", name);
1003ebfedea0SLionel Sambuc     for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
1004ebfedea0SLionel Sambuc         attr = sk_X509_ATTRIBUTE_value(attrlst, i);
1005ebfedea0SLionel Sambuc         attr_nid = OBJ_obj2nid(attr->object);
1006ebfedea0SLionel Sambuc         BIO_printf(out, "    ");
1007ebfedea0SLionel Sambuc         if (attr_nid == NID_undef) {
1008ebfedea0SLionel Sambuc             i2a_ASN1_OBJECT(out, attr->object);
1009ebfedea0SLionel Sambuc             BIO_printf(out, ": ");
1010*0a6a1f1dSLionel Sambuc         } else
1011*0a6a1f1dSLionel Sambuc             BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
1012ebfedea0SLionel Sambuc 
1013ebfedea0SLionel Sambuc         if (sk_ASN1_TYPE_num(attr->value.set)) {
1014ebfedea0SLionel Sambuc             av = sk_ASN1_TYPE_value(attr->value.set, 0);
1015ebfedea0SLionel Sambuc             switch (av->type) {
1016ebfedea0SLionel Sambuc             case V_ASN1_BMPSTRING:
1017ebfedea0SLionel Sambuc                 value = OPENSSL_uni2asc(av->value.bmpstring->data,
1018ebfedea0SLionel Sambuc                                         av->value.bmpstring->length);
1019ebfedea0SLionel Sambuc                 BIO_printf(out, "%s\n", value);
1020ebfedea0SLionel Sambuc                 OPENSSL_free(value);
1021ebfedea0SLionel Sambuc                 break;
1022ebfedea0SLionel Sambuc 
1023ebfedea0SLionel Sambuc             case V_ASN1_OCTET_STRING:
1024ebfedea0SLionel Sambuc                 hex_prin(out, av->value.octet_string->data,
1025ebfedea0SLionel Sambuc                          av->value.octet_string->length);
1026ebfedea0SLionel Sambuc                 BIO_printf(out, "\n");
1027ebfedea0SLionel Sambuc                 break;
1028ebfedea0SLionel Sambuc 
1029ebfedea0SLionel Sambuc             case V_ASN1_BIT_STRING:
1030ebfedea0SLionel Sambuc                 hex_prin(out, av->value.bit_string->data,
1031ebfedea0SLionel Sambuc                          av->value.bit_string->length);
1032ebfedea0SLionel Sambuc                 BIO_printf(out, "\n");
1033ebfedea0SLionel Sambuc                 break;
1034ebfedea0SLionel Sambuc 
1035ebfedea0SLionel Sambuc             default:
1036ebfedea0SLionel Sambuc                 BIO_printf(out, "<Unsupported tag %d>\n", av->type);
1037ebfedea0SLionel Sambuc                 break;
1038ebfedea0SLionel Sambuc             }
1039*0a6a1f1dSLionel Sambuc         } else
1040*0a6a1f1dSLionel Sambuc             BIO_printf(out, "<No Values>\n");
1041ebfedea0SLionel Sambuc     }
1042ebfedea0SLionel Sambuc     return 1;
1043ebfedea0SLionel Sambuc }
1044ebfedea0SLionel Sambuc 
hex_prin(BIO * out,unsigned char * buf,int len)1045ebfedea0SLionel Sambuc void hex_prin(BIO *out, unsigned char *buf, int len)
1046ebfedea0SLionel Sambuc {
1047ebfedea0SLionel Sambuc     int i;
1048*0a6a1f1dSLionel Sambuc     for (i = 0; i < len; i++)
1049*0a6a1f1dSLionel Sambuc         BIO_printf(out, "%02X ", buf[i]);
1050ebfedea0SLionel Sambuc }
1051ebfedea0SLionel Sambuc 
set_pbe(BIO * err,int * ppbe,const char * str)1052ebfedea0SLionel Sambuc static int set_pbe(BIO *err, int *ppbe, const char *str)
1053ebfedea0SLionel Sambuc {
1054ebfedea0SLionel Sambuc     if (!str)
1055ebfedea0SLionel Sambuc         return 0;
1056*0a6a1f1dSLionel Sambuc     if (!strcmp(str, "NONE")) {
1057ebfedea0SLionel Sambuc         *ppbe = -1;
1058ebfedea0SLionel Sambuc         return 1;
1059ebfedea0SLionel Sambuc     }
1060ebfedea0SLionel Sambuc     *ppbe = OBJ_txt2nid(str);
1061*0a6a1f1dSLionel Sambuc     if (*ppbe == NID_undef) {
1062ebfedea0SLionel Sambuc         BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
1063ebfedea0SLionel Sambuc         return 0;
1064ebfedea0SLionel Sambuc     }
1065ebfedea0SLionel Sambuc     return 1;
1066ebfedea0SLionel Sambuc }
1067ebfedea0SLionel Sambuc 
1068ebfedea0SLionel Sambuc #endif
1069