xref: /titanic_44/usr/src/common/net/wanboot/p12auxpars.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * ====================================================================
3*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
6*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
7*7c478bd9Sstevel@tonic-gate  * are met:
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
10*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
11*7c478bd9Sstevel@tonic-gate  *
12*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
13*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
14*7c478bd9Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
15*7c478bd9Sstevel@tonic-gate  *    distribution.
16*7c478bd9Sstevel@tonic-gate  *
17*7c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
18*7c478bd9Sstevel@tonic-gate  *    software must display the following acknowledgment:
19*7c478bd9Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
20*7c478bd9Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23*7c478bd9Sstevel@tonic-gate  *    endorse or promote products derived from this software without
24*7c478bd9Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
25*7c478bd9Sstevel@tonic-gate  *    licensing@OpenSSL.org.
26*7c478bd9Sstevel@tonic-gate  *
27*7c478bd9Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
28*7c478bd9Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
29*7c478bd9Sstevel@tonic-gate  *    permission of the OpenSSL Project.
30*7c478bd9Sstevel@tonic-gate  *
31*7c478bd9Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
32*7c478bd9Sstevel@tonic-gate  *    acknowledgment:
33*7c478bd9Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
34*7c478bd9Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37*7c478bd9Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39*7c478bd9Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40*7c478bd9Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41*7c478bd9Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42*7c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43*7c478bd9Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44*7c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45*7c478bd9Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46*7c478bd9Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47*7c478bd9Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
48*7c478bd9Sstevel@tonic-gate  * ====================================================================
49*7c478bd9Sstevel@tonic-gate  *
50*7c478bd9Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
51*7c478bd9Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
52*7c478bd9Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
53*7c478bd9Sstevel@tonic-gate  *
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate /*
57*7c478bd9Sstevel@tonic-gate  * Copyright 2002, 2003 Sun Microsystems, Inc.  All rights reserved.
58*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
59*7c478bd9Sstevel@tonic-gate  */
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate #include <stdio.h>
64*7c478bd9Sstevel@tonic-gate #include <strings.h>
65*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate #include <openssl/crypto.h>
68*7c478bd9Sstevel@tonic-gate #include <openssl/err.h>
69*7c478bd9Sstevel@tonic-gate #include <openssl/x509.h>
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate #include <openssl/pkcs12.h>
72*7c478bd9Sstevel@tonic-gate #include <p12aux.h>
73*7c478bd9Sstevel@tonic-gate #include <auxutil.h>
74*7c478bd9Sstevel@tonic-gate #include <p12err.h>
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate /*
77*7c478bd9Sstevel@tonic-gate  * Briefly, a note on the APIs provided by this module.
78*7c478bd9Sstevel@tonic-gate  *
79*7c478bd9Sstevel@tonic-gate  * The sunw_PKCS_parse, parse_pkcs12 and sunw_PKCS12_contents APIs
80*7c478bd9Sstevel@tonic-gate  * replace OpenSSL funcionality provided by PKCS12_parse and its
81*7c478bd9Sstevel@tonic-gate  * supporting routines.
82*7c478bd9Sstevel@tonic-gate  *
83*7c478bd9Sstevel@tonic-gate  * The APIs provided here provide more functionality:
84*7c478bd9Sstevel@tonic-gate  *
85*7c478bd9Sstevel@tonic-gate  * - sunw_PKCS12_parse provides:
86*7c478bd9Sstevel@tonic-gate  *
87*7c478bd9Sstevel@tonic-gate  *   earlier MAC processing than PKCS12_parse
88*7c478bd9Sstevel@tonic-gate  *
89*7c478bd9Sstevel@tonic-gate  *   treats the handling of the difference between CA certs and certs
90*7c478bd9Sstevel@tonic-gate  *   with matching private keys differently that PKCS12_parse does.  In
91*7c478bd9Sstevel@tonic-gate  *   PKCS12_parse, any cert which is not the one selected is assumed to be
92*7c478bd9Sstevel@tonic-gate  *   a CA cert.  In parse_pkcs12, certs which have matching private keys are
93*7c478bd9Sstevel@tonic-gate  *   not returned as part of the CA certs.
94*7c478bd9Sstevel@tonic-gate  *
95*7c478bd9Sstevel@tonic-gate  *   the matching of private keys and certs is done at this level, rather than
96*7c478bd9Sstevel@tonic-gate  *   at the lower levels which were used in the openssl implementation.  This
97*7c478bd9Sstevel@tonic-gate  *   is part of the changes introduced so that the parsing functions can
98*7c478bd9Sstevel@tonic-gate  *   return just a cert, just a private key, the stack of CA certs or any
99*7c478bd9Sstevel@tonic-gate  *   combination.
100*7c478bd9Sstevel@tonic-gate  *
101*7c478bd9Sstevel@tonic-gate  *   added DO_FIRST_PAIR, DO_LAST_PAIR and DO_UNMATCHING matchty support.
102*7c478bd9Sstevel@tonic-gate  *
103*7c478bd9Sstevel@tonic-gate  *   do a much better job of cleaning up.  Specifically, free the added
104*7c478bd9Sstevel@tonic-gate  *   attributes on the private key which was done by calling
105*7c478bd9Sstevel@tonic-gate  *   sunw_evp_pkey_free().
106*7c478bd9Sstevel@tonic-gate  *
107*7c478bd9Sstevel@tonic-gate  *   in sunw_PKCS12_contents, handle allocation of the stacks of certificates
108*7c478bd9Sstevel@tonic-gate  *   and private keys so that a) the original stacks are not changed unless
109*7c478bd9Sstevel@tonic-gate  *   the parsing was successful; b) it will either extend stacks passed in,
110*7c478bd9Sstevel@tonic-gate  *   or allocate new ones if none were supplied.
111*7c478bd9Sstevel@tonic-gate  *
112*7c478bd9Sstevel@tonic-gate  * - for parse_outer vs. parse_pk12() (from the openssl source base):
113*7c478bd9Sstevel@tonic-gate  *
114*7c478bd9Sstevel@tonic-gate  *   this calls lower levels with stacks of private keys and certs, rather
115*7c478bd9Sstevel@tonic-gate  *   that a cert, a private key and a stack for CA certs.
116*7c478bd9Sstevel@tonic-gate  *
117*7c478bd9Sstevel@tonic-gate  * - In the case of parse_all_bags vs. parse_bags, there is no real difference,
118*7c478bd9Sstevel@tonic-gate  *   other than use of stacks of private keys and certificates (as opposed
119*7c478bd9Sstevel@tonic-gate  *   to one cert, one private key and a stack of CA certificates).
120*7c478bd9Sstevel@tonic-gate  *
121*7c478bd9Sstevel@tonic-gate  * - Finally, for parse_one_bag vs. parse_bag:
122*7c478bd9Sstevel@tonic-gate  *
123*7c478bd9Sstevel@tonic-gate  *   got rid of the bugs the openssl matching of keys and certificates.
124*7c478bd9Sstevel@tonic-gate  *
125*7c478bd9Sstevel@tonic-gate  *   got rid of the requirement that there is one private key and a matching
126*7c478bd9Sstevel@tonic-gate  *   cert somewhere in the input.  This was done by moving the matching
127*7c478bd9Sstevel@tonic-gate  *   code to a higher level.
128*7c478bd9Sstevel@tonic-gate  *
129*7c478bd9Sstevel@tonic-gate  *   put any localKeyID and/or friendlyName attributes found in the structures
130*7c478bd9Sstevel@tonic-gate  *   returned, so that they can be used at higher levels for searching, etc.
131*7c478bd9Sstevel@tonic-gate  *
132*7c478bd9Sstevel@tonic-gate  *   added some error returns (like an error when there is an unsupported
133*7c478bd9Sstevel@tonic-gate  *   bag type, an unsupported certificate type or an unsupported key type)
134*7c478bd9Sstevel@tonic-gate  *
135*7c478bd9Sstevel@tonic-gate  *   Added cleanup before returning.
136*7c478bd9Sstevel@tonic-gate  */
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate static int parse_pkcs12(PKCS12 *, const char *, int, char *, int, char *,
139*7c478bd9Sstevel@tonic-gate     EVP_PKEY **, X509 **, STACK_OF(X509) **);
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate static int parse_outer(PKCS12 *, const char *, STACK_OF(EVP_PKEY) *,
142*7c478bd9Sstevel@tonic-gate     STACK_OF(X509) *);
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate static int parse_all_bags(STACK_OF(PKCS12_SAFEBAG) *, const char *,
145*7c478bd9Sstevel@tonic-gate     STACK_OF(EVP_PKEY) *, STACK_OF(X509) *);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate static int parse_one_bag(PKCS12_SAFEBAG *, const char *, STACK_OF(EVP_PKEY) *,
148*7c478bd9Sstevel@tonic-gate     STACK_OF(X509) *);
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate static int sunw_PKCS12_contents(PKCS12 *p12, const char *pass,
151*7c478bd9Sstevel@tonic-gate     STACK_OF(EVP_PKEY) **pkey, STACK_OF(X509) **certs);
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate /*
154*7c478bd9Sstevel@tonic-gate  * sunw_PKCS12_parse - Parse a PKCS12 structure and break it into its parts.
155*7c478bd9Sstevel@tonic-gate  *
156*7c478bd9Sstevel@tonic-gate  * Parse and decrypt a PKCS#12 structure returning user key, user cert and/or
157*7c478bd9Sstevel@tonic-gate  * other (CA) certs. Note either ca should be NULL, *ca should be NULL,
158*7c478bd9Sstevel@tonic-gate  * or it should point to a valid STACK_OF(X509) structure. pkey and cert can
159*7c478bd9Sstevel@tonic-gate  * be passed uninitialized.
160*7c478bd9Sstevel@tonic-gate  *
161*7c478bd9Sstevel@tonic-gate  * Arguments:
162*7c478bd9Sstevel@tonic-gate  *   p12      - Structure with pkcs12 info to be parsed
163*7c478bd9Sstevel@tonic-gate  *   pass     - Pass phrase for the private key (possibly empty) or NULL if
164*7c478bd9Sstevel@tonic-gate  *              there is none.
165*7c478bd9Sstevel@tonic-gate  *   matchty  - Info about which certs/keys to return if many are in the file.
166*7c478bd9Sstevel@tonic-gate  *   keyid    - If private key localkeyids friendlynames are to match a
167*7c478bd9Sstevel@tonic-gate  *              predetermined value, the value to match. This value should
168*7c478bd9Sstevel@tonic-gate  *		be an octet string.
169*7c478bd9Sstevel@tonic-gate  *   keyid_len- Length of the keyid byte string.
170*7c478bd9Sstevel@tonic-gate  *   name_str - If friendlynames are to match a predetermined value, the value
171*7c478bd9Sstevel@tonic-gate  *		 to match. This value should be a NULL terminated string.
172*7c478bd9Sstevel@tonic-gate  *   pkey     - Points to location pointing to the private key returned.
173*7c478bd9Sstevel@tonic-gate  *   cert     - Points to locaiton which points to the client cert returned
174*7c478bd9Sstevel@tonic-gate  *   ca       - Points to location that points to a stack of 'certificate
175*7c478bd9Sstevel@tonic-gate  *               authority' certs/trust anchors.
176*7c478bd9Sstevel@tonic-gate  *
177*7c478bd9Sstevel@tonic-gate  * Match based on the value of 'matchty' and the contents of 'keyid'
178*7c478bd9Sstevel@tonic-gate  * and/or 'name_str', as appropriate.  Go through the lists of certs and
179*7c478bd9Sstevel@tonic-gate  * private keys which were taken from the pkcs12 structure, looking for
180*7c478bd9Sstevel@tonic-gate  * matches of the requested type.  This function only searches the lists of
181*7c478bd9Sstevel@tonic-gate  * matching private keys and client certificates.  Kinds of matches allowed,
182*7c478bd9Sstevel@tonic-gate  * and the order in which they will be checked, are:
183*7c478bd9Sstevel@tonic-gate  *
184*7c478bd9Sstevel@tonic-gate  *   1) Find the key and/or cert whose localkeyid attributes matches
185*7c478bd9Sstevel@tonic-gate  *      'keyid'.
186*7c478bd9Sstevel@tonic-gate  *   2) Find the key and/or cert whose friendlyname attributes matches
187*7c478bd9Sstevel@tonic-gate  *	'name_str'
188*7c478bd9Sstevel@tonic-gate  *   3) Return the first matching key/cert pair found.
189*7c478bd9Sstevel@tonic-gate  *   4) Return the last matching key/cert pair found.
190*7c478bd9Sstevel@tonic-gate  *   5) Return whatever cert and/or key are available, even unmatching.
191*7c478bd9Sstevel@tonic-gate  *
192*7c478bd9Sstevel@tonic-gate  *   Append to the CA list, the certs which do not have matching private
193*7c478bd9Sstevel@tonic-gate  *   keys and which were not selected.
194*7c478bd9Sstevel@tonic-gate  *
195*7c478bd9Sstevel@tonic-gate  * If none of the bits are set, no client certs or private keys will be
196*7c478bd9Sstevel@tonic-gate  * returned.  CA (aka trust anchor) certs can be.
197*7c478bd9Sstevel@tonic-gate  *
198*7c478bd9Sstevel@tonic-gate  * Notes: If #3 is selected, then #4 will never occur.  CA certs will be
199*7c478bd9Sstevel@tonic-gate  * selected after a cert/key pairs are isolated.
200*7c478bd9Sstevel@tonic-gate  *
201*7c478bd9Sstevel@tonic-gate  * Returns:
202*7c478bd9Sstevel@tonic-gate  *  <  0 - An error returned.  Call ERR_get_error() to get errors information.
203*7c478bd9Sstevel@tonic-gate  *         Where possible, memory has been freed.
204*7c478bd9Sstevel@tonic-gate  *  >= 0 - Objects were found and returned.  Which objects are indicated by
205*7c478bd9Sstevel@tonic-gate  *         which bits are set (FOUND_PKEY, FOUND_CERT, FOUND_CA_CERTS).
206*7c478bd9Sstevel@tonic-gate  */
207*7c478bd9Sstevel@tonic-gate int
sunw_PKCS12_parse(PKCS12 * p12,const char * pass,int matchty,char * keyid,int keyid_len,char * name_str,EVP_PKEY ** pkey,X509 ** cert,STACK_OF (X509)** ca)208*7c478bd9Sstevel@tonic-gate sunw_PKCS12_parse(PKCS12 *p12, const char *pass, int matchty, char *keyid,
209*7c478bd9Sstevel@tonic-gate     int keyid_len, char *name_str, EVP_PKEY **pkey, X509 **cert,
210*7c478bd9Sstevel@tonic-gate     STACK_OF(X509) **ca)
211*7c478bd9Sstevel@tonic-gate {
212*7c478bd9Sstevel@tonic-gate 	boolean_t ca_supplied;
213*7c478bd9Sstevel@tonic-gate 	int retval = -1;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	/* If NULL PKCS12 structure, this is an error */
216*7c478bd9Sstevel@tonic-gate 	if (p12 == NULL) {
217*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_PKCS12_PARSE, SUNW_R_INVALID_ARG);
218*7c478bd9Sstevel@tonic-gate 		return (-1);
219*7c478bd9Sstevel@tonic-gate 	}
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	/* Set up arguments....  These will be allocated if needed */
222*7c478bd9Sstevel@tonic-gate 	if (pkey)
223*7c478bd9Sstevel@tonic-gate 		*pkey = NULL;
224*7c478bd9Sstevel@tonic-gate 	if (cert)
225*7c478bd9Sstevel@tonic-gate 		*cert = NULL;
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	/*
228*7c478bd9Sstevel@tonic-gate 	 * If there is already a ca list, use it.  Otherwise, allocate one
229*7c478bd9Sstevel@tonic-gate 	 * and free is later if an error occurs or whatever.)
230*7c478bd9Sstevel@tonic-gate 	 */
231*7c478bd9Sstevel@tonic-gate 	ca_supplied = (ca != NULL && *ca != NULL);
232*7c478bd9Sstevel@tonic-gate 	if (ca != NULL && *ca == NULL) {
233*7c478bd9Sstevel@tonic-gate 		if ((*ca = sk_X509_new_null()) == NULL) {
234*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_PKCS12_PARSE, SUNW_R_MEMORY_FAILURE);
235*7c478bd9Sstevel@tonic-gate 			return (-1);
236*7c478bd9Sstevel@tonic-gate 		}
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	/*
240*7c478bd9Sstevel@tonic-gate 	 * If password is zero length or NULL then try verifying both cases
241*7c478bd9Sstevel@tonic-gate 	 * to determine which password is correct. The reason for this is that
242*7c478bd9Sstevel@tonic-gate 	 * under PKCS#12 password based encryption no password and a zero
243*7c478bd9Sstevel@tonic-gate 	 * length password are two different things. If the password has a
244*7c478bd9Sstevel@tonic-gate 	 * non-zero length and is not NULL then call PKCS12_verify_mac() with
245*7c478bd9Sstevel@tonic-gate 	 * a length of '-1' and let it use strlen() to figure out the length
246*7c478bd9Sstevel@tonic-gate 	 * of the password.
247*7c478bd9Sstevel@tonic-gate 	 */
248*7c478bd9Sstevel@tonic-gate 	/* Check the mac */
249*7c478bd9Sstevel@tonic-gate 	if (pass == NULL || *pass == '\0') {
250*7c478bd9Sstevel@tonic-gate 		if (PKCS12_verify_mac(p12, NULL, 0))
251*7c478bd9Sstevel@tonic-gate 			pass = NULL;
252*7c478bd9Sstevel@tonic-gate 		else if (PKCS12_verify_mac(p12, "", 0))
253*7c478bd9Sstevel@tonic-gate 			pass = "";
254*7c478bd9Sstevel@tonic-gate 		else {
255*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_PKCS12_PARSE,
256*7c478bd9Sstevel@tonic-gate 			    SUNW_R_MAC_VERIFY_FAILURE);
257*7c478bd9Sstevel@tonic-gate 			goto err;
258*7c478bd9Sstevel@tonic-gate 		}
259*7c478bd9Sstevel@tonic-gate 	} else if (PKCS12_verify_mac(p12, pass, -1) == 0) {
260*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_PKCS12_PARSE, SUNW_R_MAC_VERIFY_FAILURE);
261*7c478bd9Sstevel@tonic-gate 		goto err;
262*7c478bd9Sstevel@tonic-gate 	}
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 	retval = parse_pkcs12(p12, pass, matchty, keyid, keyid_len,
265*7c478bd9Sstevel@tonic-gate 	    name_str, pkey, cert, ca);
266*7c478bd9Sstevel@tonic-gate 	if (retval < 0) {
267*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_PKCS12_PARSE, SUNW_R_PKCS12_PARSE_ERR);
268*7c478bd9Sstevel@tonic-gate 		goto err;
269*7c478bd9Sstevel@tonic-gate 	}
270*7c478bd9Sstevel@tonic-gate 	return (retval);
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate err:
273*7c478bd9Sstevel@tonic-gate 	if (pkey && *pkey) {
274*7c478bd9Sstevel@tonic-gate 		sunw_evp_pkey_free(*pkey);
275*7c478bd9Sstevel@tonic-gate 	}
276*7c478bd9Sstevel@tonic-gate 	if (cert && *cert)
277*7c478bd9Sstevel@tonic-gate 		X509_free(*cert);
278*7c478bd9Sstevel@tonic-gate 	if (ca_supplied == B_FALSE && ca != NULL)
279*7c478bd9Sstevel@tonic-gate 		sk_X509_pop_free(*ca, X509_free);
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	return (-1);
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate }
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate /*
286*7c478bd9Sstevel@tonic-gate  * parse_pkcs12 - Oversee parsing of the pkcs12 structure.  Get it
287*7c478bd9Sstevel@tonic-gate  *         parsed.  After that either return what's found directly, or
288*7c478bd9Sstevel@tonic-gate  *         do any required matching.
289*7c478bd9Sstevel@tonic-gate  *
290*7c478bd9Sstevel@tonic-gate  * Arguments:
291*7c478bd9Sstevel@tonic-gate  *   p12      - Structure with pkcs12 info to be parsed
292*7c478bd9Sstevel@tonic-gate  *   pass     - Pass phrase for the private key (possibly empty) or NULL if
293*7c478bd9Sstevel@tonic-gate  *              there is none.
294*7c478bd9Sstevel@tonic-gate  *   matchty  - Info about which certs/keys to return if many are in the file.
295*7c478bd9Sstevel@tonic-gate  *   keyid    - If private key localkeyids friendlynames are to match a
296*7c478bd9Sstevel@tonic-gate  *              predetermined value, the value to match. This value should
297*7c478bd9Sstevel@tonic-gate  *		be an octet string.
298*7c478bd9Sstevel@tonic-gate  *   keyid_len- Length of the keyid byte string.
299*7c478bd9Sstevel@tonic-gate  *   name_str - If friendlynames are to match a predetermined value, the value
300*7c478bd9Sstevel@tonic-gate  *		 to match. This value should be a NULL terminated string.
301*7c478bd9Sstevel@tonic-gate  *   pkey     - Points to location pointing to the private key returned.
302*7c478bd9Sstevel@tonic-gate  *   cert     - Points to locaiton which points to the client cert returned
303*7c478bd9Sstevel@tonic-gate  *   ca       - Points to location that points to a stack of 'certificate
304*7c478bd9Sstevel@tonic-gate  *              authority' certs/trust anchors.
305*7c478bd9Sstevel@tonic-gate  *
306*7c478bd9Sstevel@tonic-gate  *   Note about error codes:  This function is an internal function, and the
307*7c478bd9Sstevel@tonic-gate  *   place where it is called sets error codes.  Therefore only set an error
308*7c478bd9Sstevel@tonic-gate  *   code if it is something that is unique or if the function which detected
309*7c478bd9Sstevel@tonic-gate  *   the error doesn't set one.
310*7c478bd9Sstevel@tonic-gate  *
311*7c478bd9Sstevel@tonic-gate  * Returns:
312*7c478bd9Sstevel@tonic-gate  *   == -1 - An error occurred.  Call ERR_get_error() to get error information.
313*7c478bd9Sstevel@tonic-gate  *           Where possible, memory has been freed.
314*7c478bd9Sstevel@tonic-gate  *   == 0  - No matching returns were found.
315*7c478bd9Sstevel@tonic-gate  *    > 0  - This is the aithmetic 'or' of the FOUND_* bits that indicate which
316*7c478bd9Sstevel@tonic-gate  *           of the requested entries were found.
317*7c478bd9Sstevel@tonic-gate  */
318*7c478bd9Sstevel@tonic-gate static int
parse_pkcs12(PKCS12 * p12,const char * pass,int matchty,char * keyid,int kstr_len,char * name_str,EVP_PKEY ** pkey,X509 ** cert,STACK_OF (X509)** ca)319*7c478bd9Sstevel@tonic-gate parse_pkcs12(PKCS12 *p12, const char *pass, int matchty, char *keyid,
320*7c478bd9Sstevel@tonic-gate     int kstr_len, char *name_str, EVP_PKEY **pkey, X509 **cert,
321*7c478bd9Sstevel@tonic-gate     STACK_OF(X509) **ca)
322*7c478bd9Sstevel@tonic-gate {
323*7c478bd9Sstevel@tonic-gate 	STACK_OF(EVP_PKEY) *work_kl = NULL;	/* Head for private key list */
324*7c478bd9Sstevel@tonic-gate 	STACK_OF(EVP_PKEY) *nocerts = NULL;	/* Head for alt. key list */
325*7c478bd9Sstevel@tonic-gate 	STACK_OF(X509) *work_ca = NULL;		/* Head for cert list */
326*7c478bd9Sstevel@tonic-gate 	STACK_OF(X509) *work_cl = NULL;
327*7c478bd9Sstevel@tonic-gate 	int retval = 0;
328*7c478bd9Sstevel@tonic-gate 	int n;
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate 	retval = sunw_PKCS12_contents(p12, pass, &work_kl, &work_ca);
331*7c478bd9Sstevel@tonic-gate 	if (retval < 0) {
332*7c478bd9Sstevel@tonic-gate 		goto cleanup;
333*7c478bd9Sstevel@tonic-gate 	} else if (retval == 0) {
334*7c478bd9Sstevel@tonic-gate 		/*
335*7c478bd9Sstevel@tonic-gate 		 * Not really an error here - its just that nothing was found.
336*7c478bd9Sstevel@tonic-gate 		 */
337*7c478bd9Sstevel@tonic-gate 		goto cleanup;
338*7c478bd9Sstevel@tonic-gate 	}
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	if (sk_EVP_PKEY_num(work_kl) > 0) {
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 		if (sunw_split_certs(work_kl, work_ca, &work_cl, &nocerts)
343*7c478bd9Sstevel@tonic-gate 		    < 0) {
344*7c478bd9Sstevel@tonic-gate 			goto cleanup;
345*7c478bd9Sstevel@tonic-gate 		}
346*7c478bd9Sstevel@tonic-gate 	}
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	/*
349*7c478bd9Sstevel@tonic-gate 	 * Go through the lists of certs and private keys which were
350*7c478bd9Sstevel@tonic-gate 	 * returned, looking for matches of the appropriate type.  Do these
351*7c478bd9Sstevel@tonic-gate 	 * in the order described above.
352*7c478bd9Sstevel@tonic-gate 	 */
353*7c478bd9Sstevel@tonic-gate 	if ((matchty & DO_FIND_KEYID) != 0) {
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 		if (keyid == NULL) {
356*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_PKCS12_PARSE, SUNW_R_INVALID_ARG);
357*7c478bd9Sstevel@tonic-gate 			retval = -1;
358*7c478bd9Sstevel@tonic-gate 			goto cleanup;
359*7c478bd9Sstevel@tonic-gate 		}
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 		/* See if string matches localkeyid's */
362*7c478bd9Sstevel@tonic-gate 		retval = sunw_find_localkeyid(keyid, kstr_len,
363*7c478bd9Sstevel@tonic-gate 		    work_kl, work_cl, pkey, cert);
364*7c478bd9Sstevel@tonic-gate 		if (retval != 0) {
365*7c478bd9Sstevel@tonic-gate 			if (retval == -1)
366*7c478bd9Sstevel@tonic-gate 				goto cleanup;
367*7c478bd9Sstevel@tonic-gate 			else
368*7c478bd9Sstevel@tonic-gate 				goto last_part;
369*7c478bd9Sstevel@tonic-gate 		}
370*7c478bd9Sstevel@tonic-gate 	}
371*7c478bd9Sstevel@tonic-gate 	if ((matchty & DO_FIND_FN) != 0) {
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 		if (name_str == NULL) {
374*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_PKCS12_PARSE, SUNW_R_INVALID_ARG);
375*7c478bd9Sstevel@tonic-gate 			retval = -1;
376*7c478bd9Sstevel@tonic-gate 			goto cleanup;
377*7c478bd9Sstevel@tonic-gate 		}
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 		/* See if string matches friendly names */
380*7c478bd9Sstevel@tonic-gate 		retval = sunw_find_fname(name_str, work_kl, work_cl,
381*7c478bd9Sstevel@tonic-gate 		    pkey, cert);
382*7c478bd9Sstevel@tonic-gate 		if (retval != 0) {
383*7c478bd9Sstevel@tonic-gate 			if (retval == -1)
384*7c478bd9Sstevel@tonic-gate 				goto cleanup;
385*7c478bd9Sstevel@tonic-gate 			else
386*7c478bd9Sstevel@tonic-gate 				goto last_part;
387*7c478bd9Sstevel@tonic-gate 		}
388*7c478bd9Sstevel@tonic-gate 	}
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate 	if (matchty & DO_FIRST_PAIR) {
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 		/* Find the first cert and private key and return them */
393*7c478bd9Sstevel@tonic-gate 		retval = get_key_cert(0, work_kl, pkey, work_cl, cert);
394*7c478bd9Sstevel@tonic-gate 		if (retval != 0) {
395*7c478bd9Sstevel@tonic-gate 			if (retval == -1)
396*7c478bd9Sstevel@tonic-gate 				goto cleanup;
397*7c478bd9Sstevel@tonic-gate 			else
398*7c478bd9Sstevel@tonic-gate 				goto last_part;
399*7c478bd9Sstevel@tonic-gate 		}
400*7c478bd9Sstevel@tonic-gate 	}
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate 	if (matchty & DO_LAST_PAIR) {
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 		/*
405*7c478bd9Sstevel@tonic-gate 		 * Find the last matching cert and private key and return
406*7c478bd9Sstevel@tonic-gate 		 * them.  Since keys which don't have matching client certs
407*7c478bd9Sstevel@tonic-gate 		 * are at the end of the list of keys, use the number of
408*7c478bd9Sstevel@tonic-gate 		 * client certs to compute the position of the last private
409*7c478bd9Sstevel@tonic-gate 		 * key which matches a client cert.
410*7c478bd9Sstevel@tonic-gate 		 */
411*7c478bd9Sstevel@tonic-gate 		n = sk_X509_num(work_cl) - 1;
412*7c478bd9Sstevel@tonic-gate 		retval = get_key_cert(n, work_kl, pkey, work_cl, cert);
413*7c478bd9Sstevel@tonic-gate 		if (retval != 0) {
414*7c478bd9Sstevel@tonic-gate 			if (retval == -1)
415*7c478bd9Sstevel@tonic-gate 				goto cleanup;
416*7c478bd9Sstevel@tonic-gate 			else
417*7c478bd9Sstevel@tonic-gate 				goto last_part;
418*7c478bd9Sstevel@tonic-gate 		}
419*7c478bd9Sstevel@tonic-gate 	}
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 	if (matchty & DO_UNMATCHING) {
422*7c478bd9Sstevel@tonic-gate 		STACK_OF(EVP_PKEY) *tmpk;
423*7c478bd9Sstevel@tonic-gate 		STACK_OF(X509) *tmpc;
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate 		/* Find the first cert and private key and return them */
426*7c478bd9Sstevel@tonic-gate 		tmpc = work_cl;
427*7c478bd9Sstevel@tonic-gate 		if (work_cl == NULL || sk_X509_num(work_cl) == 0)
428*7c478bd9Sstevel@tonic-gate 			tmpc = work_ca;
429*7c478bd9Sstevel@tonic-gate 		tmpk = work_kl;
430*7c478bd9Sstevel@tonic-gate 		if (work_kl == NULL || sk_EVP_PKEY_num(work_kl) == 0)
431*7c478bd9Sstevel@tonic-gate 			tmpk = nocerts;
432*7c478bd9Sstevel@tonic-gate 		retval = get_key_cert(0, tmpk, pkey, tmpc, cert);
433*7c478bd9Sstevel@tonic-gate 		if (retval != 0) {
434*7c478bd9Sstevel@tonic-gate 			if (retval == -1)
435*7c478bd9Sstevel@tonic-gate 				goto cleanup;
436*7c478bd9Sstevel@tonic-gate 			else
437*7c478bd9Sstevel@tonic-gate 				goto last_part;
438*7c478bd9Sstevel@tonic-gate 		}
439*7c478bd9Sstevel@tonic-gate 	}
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate last_part:
442*7c478bd9Sstevel@tonic-gate 	/* If no errors, terminate normally */
443*7c478bd9Sstevel@tonic-gate 	if (retval != -1)
444*7c478bd9Sstevel@tonic-gate 		retval |= set_results(NULL, NULL, NULL, NULL, ca, &work_ca,
445*7c478bd9Sstevel@tonic-gate 		    NULL, NULL);
446*7c478bd9Sstevel@tonic-gate 	if (retval >= 0) {
447*7c478bd9Sstevel@tonic-gate 		goto clean_part;
448*7c478bd9Sstevel@tonic-gate 	}
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 	/* Fallthrough is intentional in error cases. */
451*7c478bd9Sstevel@tonic-gate cleanup:
452*7c478bd9Sstevel@tonic-gate 	if (pkey != NULL && *pkey != NULL) {
453*7c478bd9Sstevel@tonic-gate 		sunw_evp_pkey_free(*pkey);
454*7c478bd9Sstevel@tonic-gate 		*pkey = NULL;
455*7c478bd9Sstevel@tonic-gate 	}
456*7c478bd9Sstevel@tonic-gate 	if (cert != NULL && *cert != NULL) {
457*7c478bd9Sstevel@tonic-gate 		X509_free(*cert);
458*7c478bd9Sstevel@tonic-gate 		*cert = NULL;
459*7c478bd9Sstevel@tonic-gate 	}
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate clean_part:
462*7c478bd9Sstevel@tonic-gate 
463*7c478bd9Sstevel@tonic-gate 	if (work_kl != NULL) {
464*7c478bd9Sstevel@tonic-gate 		sk_EVP_PKEY_pop_free(work_kl, sunw_evp_pkey_free);
465*7c478bd9Sstevel@tonic-gate 	}
466*7c478bd9Sstevel@tonic-gate 	if (work_ca != NULL)
467*7c478bd9Sstevel@tonic-gate 		sk_X509_pop_free(work_ca, X509_free);
468*7c478bd9Sstevel@tonic-gate 	if (work_cl != NULL)
469*7c478bd9Sstevel@tonic-gate 		sk_X509_pop_free(work_cl, X509_free);
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 	return (retval);
472*7c478bd9Sstevel@tonic-gate }
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate /*
475*7c478bd9Sstevel@tonic-gate  * sunw_PKCS12_contents() parses a pkcs#12 structure and returns component
476*7c478bd9Sstevel@tonic-gate  *     parts found, without evaluation.
477*7c478bd9Sstevel@tonic-gate  *
478*7c478bd9Sstevel@tonic-gate  * Parse and decrypt a PKCS#12 structure returning any user keys and/or
479*7c478bd9Sstevel@tonic-gate  * various certs. Note these should either be NULL, *whatever should
480*7c478bd9Sstevel@tonic-gate  * be NULL, or it should point to a valid STACK_OF(X509) structure.
481*7c478bd9Sstevel@tonic-gate  *
482*7c478bd9Sstevel@tonic-gate  * Arguments:
483*7c478bd9Sstevel@tonic-gate  *   p12      - Structure with pkcs12 info to be parsed
484*7c478bd9Sstevel@tonic-gate  *   pass     - Pass phrase for the private key and entire pkcs12 wad (possibly
485*7c478bd9Sstevel@tonic-gate  *              empty) or NULL if there is none.
486*7c478bd9Sstevel@tonic-gate  *   pkeys    - Points to address of a stack of private keys to return.
487*7c478bd9Sstevel@tonic-gate  *   certs    - Points to address of a stack of client certs return.
488*7c478bd9Sstevel@tonic-gate  *
489*7c478bd9Sstevel@tonic-gate  *   Note:  The certs and keys being returned are in random order.
490*7c478bd9Sstevel@tonic-gate  *
491*7c478bd9Sstevel@tonic-gate  * Returns:
492*7c478bd9Sstevel@tonic-gate  *  <  0 - An error returned.  Call ERR_get_error() to get errors information.
493*7c478bd9Sstevel@tonic-gate  *         Where possible, memory has been freed.
494*7c478bd9Sstevel@tonic-gate  *  >= 0 - Objects were found and returned.  Which objects are indicated by
495*7c478bd9Sstevel@tonic-gate  *         which bits are set (FOUND_PKEY or FOUND_CERT)
496*7c478bd9Sstevel@tonic-gate  */
497*7c478bd9Sstevel@tonic-gate static int
sunw_PKCS12_contents(PKCS12 * p12,const char * pass,STACK_OF (EVP_PKEY)** pkey,STACK_OF (X509)** certs)498*7c478bd9Sstevel@tonic-gate sunw_PKCS12_contents(PKCS12 *p12, const char *pass, STACK_OF(EVP_PKEY) **pkey,
499*7c478bd9Sstevel@tonic-gate     STACK_OF(X509) **certs)
500*7c478bd9Sstevel@tonic-gate {
501*7c478bd9Sstevel@tonic-gate 	STACK_OF(EVP_PKEY) *work_kl = NULL;
502*7c478bd9Sstevel@tonic-gate 	STACK_OF(X509) *work_ca = NULL;
503*7c478bd9Sstevel@tonic-gate 	int retval = -1;
504*7c478bd9Sstevel@tonic-gate 
505*7c478bd9Sstevel@tonic-gate 	/*
506*7c478bd9Sstevel@tonic-gate 	 * Allocate the working stacks for private key and for the
507*7c478bd9Sstevel@tonic-gate 	 * ca certs.
508*7c478bd9Sstevel@tonic-gate 	 */
509*7c478bd9Sstevel@tonic-gate 	if ((work_kl = sk_EVP_PKEY_new_null()) == NULL) {
510*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_PKCS12_CONTENTS, SUNW_R_MEMORY_FAILURE);
511*7c478bd9Sstevel@tonic-gate 		goto cleanup;
512*7c478bd9Sstevel@tonic-gate 	}
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate 	if ((work_ca = sk_X509_new_null()) == NULL) {
515*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_PKCS12_CONTENTS, SUNW_R_MEMORY_FAILURE);
516*7c478bd9Sstevel@tonic-gate 		goto cleanup;
517*7c478bd9Sstevel@tonic-gate 	}
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate 	if (parse_outer(p12, pass, work_kl, work_ca) == 0) {
520*7c478bd9Sstevel@tonic-gate 		/*
521*7c478bd9Sstevel@tonic-gate 		 * Error already on stack
522*7c478bd9Sstevel@tonic-gate 		 */
523*7c478bd9Sstevel@tonic-gate 		goto cleanup;
524*7c478bd9Sstevel@tonic-gate 	}
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate 	/* on error, set_results() returns an error on the stack */
527*7c478bd9Sstevel@tonic-gate 	retval = set_results(pkey, &work_kl, certs, &work_ca, NULL,
528*7c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL);
529*7c478bd9Sstevel@tonic-gate 
530*7c478bd9Sstevel@tonic-gate cleanup:
531*7c478bd9Sstevel@tonic-gate 	if (work_kl != NULL) {
532*7c478bd9Sstevel@tonic-gate 		sk_EVP_PKEY_pop_free(work_kl, sunw_evp_pkey_free);
533*7c478bd9Sstevel@tonic-gate 	}
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate 	return (retval);
536*7c478bd9Sstevel@tonic-gate }
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate /*
539*7c478bd9Sstevel@tonic-gate  * parse_outer - Unpack the outer PKCS#12 structure and go through the
540*7c478bd9Sstevel@tonic-gate  *         individual bags.  Return stacks of certs, private keys found and
541*7c478bd9Sstevel@tonic-gate  *         CA certs found.
542*7c478bd9Sstevel@tonic-gate  *
543*7c478bd9Sstevel@tonic-gate  *   Note about error codes:  This function is an internal function, and the
544*7c478bd9Sstevel@tonic-gate  *   place where it is called sets error codes.
545*7c478bd9Sstevel@tonic-gate  *
546*7c478bd9Sstevel@tonic-gate  * Returns:
547*7c478bd9Sstevel@tonic-gate  *    0 - An error returned.  Call ERR_get_error() to get errors information.
548*7c478bd9Sstevel@tonic-gate  *        Where possible, memory has been freed.
549*7c478bd9Sstevel@tonic-gate  *    1 - PKCS12 data object was parsed and lists of certs and private keys
550*7c478bd9Sstevel@tonic-gate  *        were returned.
551*7c478bd9Sstevel@tonic-gate  */
552*7c478bd9Sstevel@tonic-gate static int
parse_outer(PKCS12 * p12,const char * pass,STACK_OF (EVP_PKEY)* kl,STACK_OF (X509)* cl)553*7c478bd9Sstevel@tonic-gate parse_outer(PKCS12 *p12, const char *pass, STACK_OF(EVP_PKEY) *kl,
554*7c478bd9Sstevel@tonic-gate     STACK_OF(X509) *cl)
555*7c478bd9Sstevel@tonic-gate {
556*7c478bd9Sstevel@tonic-gate 	STACK_OF(PKCS12_SAFEBAG) *bags;
557*7c478bd9Sstevel@tonic-gate 	STACK_OF(PKCS7) *asafes;
558*7c478bd9Sstevel@tonic-gate 	int i, bagnid;
559*7c478bd9Sstevel@tonic-gate 	PKCS7 *p7;
560*7c478bd9Sstevel@tonic-gate 
561*7c478bd9Sstevel@tonic-gate 	if ((asafes = M_PKCS12_unpack_authsafes(p12)) == NULL)
562*7c478bd9Sstevel@tonic-gate 		return (0);
563*7c478bd9Sstevel@tonic-gate 
564*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sk_PKCS7_num(asafes); i++) {
565*7c478bd9Sstevel@tonic-gate 		p7 = sk_PKCS7_value(asafes, i);
566*7c478bd9Sstevel@tonic-gate 		bagnid = OBJ_obj2nid(p7->type);
567*7c478bd9Sstevel@tonic-gate 		if (bagnid == NID_pkcs7_data) {
568*7c478bd9Sstevel@tonic-gate 			bags = M_PKCS12_unpack_p7data(p7);
569*7c478bd9Sstevel@tonic-gate 		} else if (bagnid == NID_pkcs7_encrypted) {
570*7c478bd9Sstevel@tonic-gate 			/*
571*7c478bd9Sstevel@tonic-gate 			 * A length of '-1' means strlen() can be used
572*7c478bd9Sstevel@tonic-gate 			 * to determine the password length.
573*7c478bd9Sstevel@tonic-gate 			 */
574*7c478bd9Sstevel@tonic-gate 			bags = M_PKCS12_unpack_p7encdata(p7, pass, -1);
575*7c478bd9Sstevel@tonic-gate 		} else {
576*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_PARSE_OUTER, SUNW_R_BAD_BAGTYPE);
577*7c478bd9Sstevel@tonic-gate 			return (0);
578*7c478bd9Sstevel@tonic-gate 		}
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 		if (bags == NULL) {
581*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_PARSE_OUTER, SUNW_R_PARSE_BAG_ERR);
582*7c478bd9Sstevel@tonic-gate 			sk_PKCS7_pop_free(asafes, PKCS7_free);
583*7c478bd9Sstevel@tonic-gate 			return (0);
584*7c478bd9Sstevel@tonic-gate 		}
585*7c478bd9Sstevel@tonic-gate 		if (parse_all_bags(bags, pass, kl, cl) == 0) {
586*7c478bd9Sstevel@tonic-gate 			sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
587*7c478bd9Sstevel@tonic-gate 			sk_PKCS7_pop_free(asafes, PKCS7_free);
588*7c478bd9Sstevel@tonic-gate 			return (0);
589*7c478bd9Sstevel@tonic-gate 		}
590*7c478bd9Sstevel@tonic-gate 	}
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate 	return (1);
593*7c478bd9Sstevel@tonic-gate }
594*7c478bd9Sstevel@tonic-gate 
595*7c478bd9Sstevel@tonic-gate /*
596*7c478bd9Sstevel@tonic-gate  * parse_all_bags - go through the stack of bags, parsing each.
597*7c478bd9Sstevel@tonic-gate  *
598*7c478bd9Sstevel@tonic-gate  *   Note about error codes:  This function is an internal function, and the
599*7c478bd9Sstevel@tonic-gate  *   place where it is called sets error codes.
600*7c478bd9Sstevel@tonic-gate  *
601*7c478bd9Sstevel@tonic-gate  * Returns:
602*7c478bd9Sstevel@tonic-gate  *    0 - An error returned.  Call ERR_get_error() to get errors information.
603*7c478bd9Sstevel@tonic-gate  *        Where possible, memory has been freed.
604*7c478bd9Sstevel@tonic-gate  *    1 - Stack of safebags was parsed and lists of certs and private keys
605*7c478bd9Sstevel@tonic-gate  *        were returned.
606*7c478bd9Sstevel@tonic-gate  */
607*7c478bd9Sstevel@tonic-gate static int
parse_all_bags(STACK_OF (PKCS12_SAFEBAG)* bags,const char * pass,STACK_OF (EVP_PKEY)* kl,STACK_OF (X509)* cl)608*7c478bd9Sstevel@tonic-gate parse_all_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
609*7c478bd9Sstevel@tonic-gate     STACK_OF(EVP_PKEY) *kl, STACK_OF(X509) *cl)
610*7c478bd9Sstevel@tonic-gate {
611*7c478bd9Sstevel@tonic-gate 	int i;
612*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
613*7c478bd9Sstevel@tonic-gate 		if (parse_one_bag(sk_PKCS12_SAFEBAG_value(bags, i),
614*7c478bd9Sstevel@tonic-gate 		    pass, kl, cl) == 0)
615*7c478bd9Sstevel@tonic-gate 			return (0);
616*7c478bd9Sstevel@tonic-gate 	}
617*7c478bd9Sstevel@tonic-gate 	return (1);
618*7c478bd9Sstevel@tonic-gate }
619*7c478bd9Sstevel@tonic-gate 
620*7c478bd9Sstevel@tonic-gate /*
621*7c478bd9Sstevel@tonic-gate  * parse_one_bag - Parse an individual bag
622*7c478bd9Sstevel@tonic-gate  *
623*7c478bd9Sstevel@tonic-gate  *   i = parse_one_bag(bag, pass, kl, cl);
624*7c478bd9Sstevel@tonic-gate  *
625*7c478bd9Sstevel@tonic-gate  * Arguments:
626*7c478bd9Sstevel@tonic-gate  *   bag	- pkcs12 safebag to parse.
627*7c478bd9Sstevel@tonic-gate  *   pass 	- password for use in decryption of shrouded keybag
628*7c478bd9Sstevel@tonic-gate  *   kl         - Stack of private keys found so far.  New private keys will
629*7c478bd9Sstevel@tonic-gate  *                be added here if found.
630*7c478bd9Sstevel@tonic-gate  *   cl         - Stack of certs found so far.  New certificates will be
631*7c478bd9Sstevel@tonic-gate  *                added here if found.
632*7c478bd9Sstevel@tonic-gate  *
633*7c478bd9Sstevel@tonic-gate  * Returns:
634*7c478bd9Sstevel@tonic-gate  *    0 - An error returned.  Call ERR_get_error() to get errors information.
635*7c478bd9Sstevel@tonic-gate  *        Where possible, memory has been freed.
636*7c478bd9Sstevel@tonic-gate  *    1 - one safebag was parsed. If it contained a cert or private key, it
637*7c478bd9Sstevel@tonic-gate  *        was added to the stack of certs or private keys found, respectively.
638*7c478bd9Sstevel@tonic-gate  *        localKeyId or friendlyName attributes are returned with the
639*7c478bd9Sstevel@tonic-gate  *        private key or certificate.
640*7c478bd9Sstevel@tonic-gate  */
641*7c478bd9Sstevel@tonic-gate static int
parse_one_bag(PKCS12_SAFEBAG * bag,const char * pass,STACK_OF (EVP_PKEY)* kl,STACK_OF (X509)* cl)642*7c478bd9Sstevel@tonic-gate parse_one_bag(PKCS12_SAFEBAG *bag, const char *pass, STACK_OF(EVP_PKEY) *kl,
643*7c478bd9Sstevel@tonic-gate     STACK_OF(X509) *cl)
644*7c478bd9Sstevel@tonic-gate {
645*7c478bd9Sstevel@tonic-gate 	X509_ATTRIBUTE *attr = NULL;
646*7c478bd9Sstevel@tonic-gate 	ASN1_TYPE *keyid = NULL;
647*7c478bd9Sstevel@tonic-gate 	ASN1_TYPE *fname = NULL;
648*7c478bd9Sstevel@tonic-gate 	PKCS8_PRIV_KEY_INFO *p8;
649*7c478bd9Sstevel@tonic-gate 	EVP_PKEY *pkey = NULL;
650*7c478bd9Sstevel@tonic-gate 	X509 *x509 = NULL;
651*7c478bd9Sstevel@tonic-gate 	uchar_t *data = NULL;
652*7c478bd9Sstevel@tonic-gate 	char *str = NULL;
653*7c478bd9Sstevel@tonic-gate 	int retval = 1;
654*7c478bd9Sstevel@tonic-gate 
655*7c478bd9Sstevel@tonic-gate 	keyid = PKCS12_get_attr(bag, NID_localKeyID);
656*7c478bd9Sstevel@tonic-gate 	fname = PKCS12_get_attr(bag, NID_friendlyName);
657*7c478bd9Sstevel@tonic-gate 
658*7c478bd9Sstevel@tonic-gate 	switch (M_PKCS12_bag_type(bag)) {
659*7c478bd9Sstevel@tonic-gate 	case NID_keyBag:
660*7c478bd9Sstevel@tonic-gate 		if ((pkey = EVP_PKCS82PKEY(bag->value.keybag)) == NULL) {
661*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_PARSE_ONE_BAG, SUNW_R_PARSE_BAG_ERR);
662*7c478bd9Sstevel@tonic-gate 			retval = 0;
663*7c478bd9Sstevel@tonic-gate 			break;
664*7c478bd9Sstevel@tonic-gate 		}
665*7c478bd9Sstevel@tonic-gate 		break;
666*7c478bd9Sstevel@tonic-gate 
667*7c478bd9Sstevel@tonic-gate 	case NID_pkcs8ShroudedKeyBag:
668*7c478bd9Sstevel@tonic-gate 		/*
669*7c478bd9Sstevel@tonic-gate 		 * A length of '-1' means strlen() can be used
670*7c478bd9Sstevel@tonic-gate 		 * to determine the password length.
671*7c478bd9Sstevel@tonic-gate 		 */
672*7c478bd9Sstevel@tonic-gate 		if ((p8 = M_PKCS12_decrypt_skey(bag, pass, -1)) == NULL) {
673*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_PARSE_ONE_BAG, SUNW_R_PARSE_BAG_ERR);
674*7c478bd9Sstevel@tonic-gate 			retval = 0;
675*7c478bd9Sstevel@tonic-gate 			break;
676*7c478bd9Sstevel@tonic-gate 		}
677*7c478bd9Sstevel@tonic-gate 		pkey = EVP_PKCS82PKEY(p8);
678*7c478bd9Sstevel@tonic-gate 		PKCS8_PRIV_KEY_INFO_free(p8);
679*7c478bd9Sstevel@tonic-gate 		if (pkey == NULL) {
680*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_PARSE_ONE_BAG, SUNW_R_PARSE_BAG_ERR);
681*7c478bd9Sstevel@tonic-gate 			retval = 0;
682*7c478bd9Sstevel@tonic-gate 		}
683*7c478bd9Sstevel@tonic-gate 		break;
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate 	case NID_certBag:
686*7c478bd9Sstevel@tonic-gate 		if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate) {
687*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_PARSE_ONE_BAG, SUNW_R_BAD_CERTTYPE);
688*7c478bd9Sstevel@tonic-gate 			break;
689*7c478bd9Sstevel@tonic-gate 		}
690*7c478bd9Sstevel@tonic-gate 		if ((x509 = M_PKCS12_certbag2x509(bag)) == NULL) {
691*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_PARSE_ONE_BAG,
692*7c478bd9Sstevel@tonic-gate 			    SUNW_R_PARSE_CERT_ERR);
693*7c478bd9Sstevel@tonic-gate 			retval = 0;
694*7c478bd9Sstevel@tonic-gate 			break;
695*7c478bd9Sstevel@tonic-gate 		}
696*7c478bd9Sstevel@tonic-gate 
697*7c478bd9Sstevel@tonic-gate 		if (keyid != NULL) {
698*7c478bd9Sstevel@tonic-gate 			if (keyid->type != V_ASN1_OCTET_STRING) {
699*7c478bd9Sstevel@tonic-gate 				SUNWerr(SUNW_F_PARSE_ONE_BAG,
700*7c478bd9Sstevel@tonic-gate 				    SUNW_R_BAD_LKID);
701*7c478bd9Sstevel@tonic-gate 				retval = 0;
702*7c478bd9Sstevel@tonic-gate 				break;
703*7c478bd9Sstevel@tonic-gate 			}
704*7c478bd9Sstevel@tonic-gate 			if (X509_keyid_set1(x509,
705*7c478bd9Sstevel@tonic-gate 			    keyid->value.octet_string->data,
706*7c478bd9Sstevel@tonic-gate 			    keyid->value.octet_string->length) == 0) {
707*7c478bd9Sstevel@tonic-gate 				SUNWerr(SUNW_F_PARSE_ONE_BAG,
708*7c478bd9Sstevel@tonic-gate 				    SUNW_R_SET_LKID_ERR);
709*7c478bd9Sstevel@tonic-gate 				retval = 0;
710*7c478bd9Sstevel@tonic-gate 				break;
711*7c478bd9Sstevel@tonic-gate 			}
712*7c478bd9Sstevel@tonic-gate 		}
713*7c478bd9Sstevel@tonic-gate 
714*7c478bd9Sstevel@tonic-gate 		if (fname != NULL) {
715*7c478bd9Sstevel@tonic-gate 			ASN1_STRING *tmpstr = NULL;
716*7c478bd9Sstevel@tonic-gate 			int len;
717*7c478bd9Sstevel@tonic-gate 
718*7c478bd9Sstevel@tonic-gate 			if (fname->type != V_ASN1_BMPSTRING) {
719*7c478bd9Sstevel@tonic-gate 				SUNWerr(SUNW_F_PARSE_ONE_BAG,
720*7c478bd9Sstevel@tonic-gate 				    SUNW_R_BAD_FNAME);
721*7c478bd9Sstevel@tonic-gate 				retval = 0;
722*7c478bd9Sstevel@tonic-gate 				break;
723*7c478bd9Sstevel@tonic-gate 			}
724*7c478bd9Sstevel@tonic-gate 
725*7c478bd9Sstevel@tonic-gate 			tmpstr = fname->value.asn1_string;
726*7c478bd9Sstevel@tonic-gate 			len = ASN1_STRING_to_UTF8(&data, tmpstr);
727*7c478bd9Sstevel@tonic-gate 			if (len < 0) {
728*7c478bd9Sstevel@tonic-gate 				SUNWerr(SUNW_F_PARSE_ONE_BAG,
729*7c478bd9Sstevel@tonic-gate 				    SUNW_R_SET_FNAME_ERR);
730*7c478bd9Sstevel@tonic-gate 				retval = 0;
731*7c478bd9Sstevel@tonic-gate 				break;
732*7c478bd9Sstevel@tonic-gate 			}
733*7c478bd9Sstevel@tonic-gate 
734*7c478bd9Sstevel@tonic-gate 			if (X509_alias_set1(x509, data, len) == 0) {
735*7c478bd9Sstevel@tonic-gate 				SUNWerr(SUNW_F_PARSE_ONE_BAG,
736*7c478bd9Sstevel@tonic-gate 				    SUNW_R_SET_FNAME_ERR);
737*7c478bd9Sstevel@tonic-gate 				retval = 0;
738*7c478bd9Sstevel@tonic-gate 				break;
739*7c478bd9Sstevel@tonic-gate 			}
740*7c478bd9Sstevel@tonic-gate 		}
741*7c478bd9Sstevel@tonic-gate 
742*7c478bd9Sstevel@tonic-gate 		if (sk_X509_push(cl, x509) == 0) {
743*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_PARSE_ONE_BAG, SUNW_R_MEMORY_FAILURE);
744*7c478bd9Sstevel@tonic-gate 			retval = 0;
745*7c478bd9Sstevel@tonic-gate 			break;
746*7c478bd9Sstevel@tonic-gate 		}
747*7c478bd9Sstevel@tonic-gate 		x509 = NULL;
748*7c478bd9Sstevel@tonic-gate 		break;
749*7c478bd9Sstevel@tonic-gate 
750*7c478bd9Sstevel@tonic-gate 	case NID_safeContentsBag:
751*7c478bd9Sstevel@tonic-gate 		if (keyid != NULL)
752*7c478bd9Sstevel@tonic-gate 			ASN1_TYPE_free(keyid);
753*7c478bd9Sstevel@tonic-gate 		if (fname != NULL)
754*7c478bd9Sstevel@tonic-gate 			ASN1_TYPE_free(fname);
755*7c478bd9Sstevel@tonic-gate 		if (parse_all_bags(bag->value.safes, pass, kl, cl) == 0) {
756*7c478bd9Sstevel@tonic-gate 			/*
757*7c478bd9Sstevel@tonic-gate 			 * Error already on stack
758*7c478bd9Sstevel@tonic-gate 			 */
759*7c478bd9Sstevel@tonic-gate 			return (0);
760*7c478bd9Sstevel@tonic-gate 		}
761*7c478bd9Sstevel@tonic-gate 		return (1);
762*7c478bd9Sstevel@tonic-gate 
763*7c478bd9Sstevel@tonic-gate 	default:
764*7c478bd9Sstevel@tonic-gate 		if (keyid != NULL)
765*7c478bd9Sstevel@tonic-gate 			ASN1_TYPE_free(keyid);
766*7c478bd9Sstevel@tonic-gate 		if (fname != NULL)
767*7c478bd9Sstevel@tonic-gate 			ASN1_TYPE_free(fname);
768*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_PARSE_ONE_BAG, SUNW_R_BAD_BAGTYPE);
769*7c478bd9Sstevel@tonic-gate 		return (0);
770*7c478bd9Sstevel@tonic-gate 	}
771*7c478bd9Sstevel@tonic-gate 
772*7c478bd9Sstevel@tonic-gate 
773*7c478bd9Sstevel@tonic-gate 	if (pkey != NULL) {
774*7c478bd9Sstevel@tonic-gate 		if (retval != 0 && (keyid != NULL || fname != NULL) &&
775*7c478bd9Sstevel@tonic-gate 		    pkey->attributes == NULL) {
776*7c478bd9Sstevel@tonic-gate 			pkey->attributes = sk_X509_ATTRIBUTE_new_null();
777*7c478bd9Sstevel@tonic-gate 			if (pkey->attributes == NULL) {
778*7c478bd9Sstevel@tonic-gate 				SUNWerr(SUNW_F_PARSE_ONE_BAG,
779*7c478bd9Sstevel@tonic-gate 				    SUNW_R_MEMORY_FAILURE);
780*7c478bd9Sstevel@tonic-gate 				retval = 0;
781*7c478bd9Sstevel@tonic-gate 			}
782*7c478bd9Sstevel@tonic-gate 		}
783*7c478bd9Sstevel@tonic-gate 
784*7c478bd9Sstevel@tonic-gate 		if (retval != 0 && keyid != NULL) {
785*7c478bd9Sstevel@tonic-gate 			attr = type2attrib(keyid, NID_localKeyID);
786*7c478bd9Sstevel@tonic-gate 			if (attr == NULL)
787*7c478bd9Sstevel@tonic-gate 				/*
788*7c478bd9Sstevel@tonic-gate 				 * Error already on stack
789*7c478bd9Sstevel@tonic-gate 				 */
790*7c478bd9Sstevel@tonic-gate 				retval = 0;
791*7c478bd9Sstevel@tonic-gate 			else {
792*7c478bd9Sstevel@tonic-gate 				keyid = NULL;
793*7c478bd9Sstevel@tonic-gate 				if (sk_X509_ATTRIBUTE_push(pkey->attributes,
794*7c478bd9Sstevel@tonic-gate 				    attr) == 0) {
795*7c478bd9Sstevel@tonic-gate 					SUNWerr(SUNW_F_PARSE_ONE_BAG,
796*7c478bd9Sstevel@tonic-gate 					    SUNW_R_MEMORY_FAILURE);
797*7c478bd9Sstevel@tonic-gate 					retval = 0;
798*7c478bd9Sstevel@tonic-gate 				} else {
799*7c478bd9Sstevel@tonic-gate 					attr = NULL;
800*7c478bd9Sstevel@tonic-gate 				}
801*7c478bd9Sstevel@tonic-gate 			}
802*7c478bd9Sstevel@tonic-gate 		}
803*7c478bd9Sstevel@tonic-gate 
804*7c478bd9Sstevel@tonic-gate 		if (retval != 0 && fname != NULL) {
805*7c478bd9Sstevel@tonic-gate 			attr = type2attrib(fname, NID_friendlyName);
806*7c478bd9Sstevel@tonic-gate 			if (attr == NULL) {
807*7c478bd9Sstevel@tonic-gate 				/*
808*7c478bd9Sstevel@tonic-gate 				 * Error already on stack
809*7c478bd9Sstevel@tonic-gate 				 */
810*7c478bd9Sstevel@tonic-gate 				retval = 0;
811*7c478bd9Sstevel@tonic-gate 			} else {
812*7c478bd9Sstevel@tonic-gate 				fname = NULL;
813*7c478bd9Sstevel@tonic-gate 				if (sk_X509_ATTRIBUTE_push(pkey->attributes,
814*7c478bd9Sstevel@tonic-gate 				    attr) == 0) {
815*7c478bd9Sstevel@tonic-gate 					SUNWerr(SUNW_F_PARSE_ONE_BAG,
816*7c478bd9Sstevel@tonic-gate 					    SUNW_R_MEMORY_FAILURE);
817*7c478bd9Sstevel@tonic-gate 					retval = 0;
818*7c478bd9Sstevel@tonic-gate 				} else {
819*7c478bd9Sstevel@tonic-gate 					attr = NULL;
820*7c478bd9Sstevel@tonic-gate 				}
821*7c478bd9Sstevel@tonic-gate 			}
822*7c478bd9Sstevel@tonic-gate 		}
823*7c478bd9Sstevel@tonic-gate 
824*7c478bd9Sstevel@tonic-gate 		/* Save the private key */
825*7c478bd9Sstevel@tonic-gate 		if (retval != 0) {
826*7c478bd9Sstevel@tonic-gate 			if (sk_EVP_PKEY_push(kl, pkey) == 0) {
827*7c478bd9Sstevel@tonic-gate 				SUNWerr(SUNW_F_PARSE_ONE_BAG,
828*7c478bd9Sstevel@tonic-gate 				    SUNW_R_MEMORY_FAILURE);
829*7c478bd9Sstevel@tonic-gate 				retval = 0;
830*7c478bd9Sstevel@tonic-gate 			} else {
831*7c478bd9Sstevel@tonic-gate 				pkey = NULL;
832*7c478bd9Sstevel@tonic-gate 			}
833*7c478bd9Sstevel@tonic-gate 		}
834*7c478bd9Sstevel@tonic-gate 	}
835*7c478bd9Sstevel@tonic-gate 
836*7c478bd9Sstevel@tonic-gate 	if (pkey != NULL) {
837*7c478bd9Sstevel@tonic-gate 		sunw_evp_pkey_free(pkey);
838*7c478bd9Sstevel@tonic-gate 	}
839*7c478bd9Sstevel@tonic-gate 
840*7c478bd9Sstevel@tonic-gate 	if (x509 != NULL)
841*7c478bd9Sstevel@tonic-gate 		X509_free(x509);
842*7c478bd9Sstevel@tonic-gate 
843*7c478bd9Sstevel@tonic-gate 	if (keyid != NULL)
844*7c478bd9Sstevel@tonic-gate 		ASN1_TYPE_free(keyid);
845*7c478bd9Sstevel@tonic-gate 
846*7c478bd9Sstevel@tonic-gate 	if (fname != NULL)
847*7c478bd9Sstevel@tonic-gate 		ASN1_TYPE_free(fname);
848*7c478bd9Sstevel@tonic-gate 
849*7c478bd9Sstevel@tonic-gate 	if (attr != NULL)
850*7c478bd9Sstevel@tonic-gate 		X509_ATTRIBUTE_free(attr);
851*7c478bd9Sstevel@tonic-gate 
852*7c478bd9Sstevel@tonic-gate 	if (data != NULL)
853*7c478bd9Sstevel@tonic-gate 		OPENSSL_free(data);
854*7c478bd9Sstevel@tonic-gate 
855*7c478bd9Sstevel@tonic-gate 	if (str != NULL)
856*7c478bd9Sstevel@tonic-gate 		OPENSSL_free(str);
857*7c478bd9Sstevel@tonic-gate 
858*7c478bd9Sstevel@tonic-gate 	return (retval);
859*7c478bd9Sstevel@tonic-gate }
860