xref: /titanic_50/usr/src/common/net/wanboot/p12misc.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  * sunw_cryto_init() does crypto-specific initialization.
78*7c478bd9Sstevel@tonic-gate  *
79*7c478bd9Sstevel@tonic-gate  * Arguments:
80*7c478bd9Sstevel@tonic-gate  *   None.
81*7c478bd9Sstevel@tonic-gate  *
82*7c478bd9Sstevel@tonic-gate  * Returns:
83*7c478bd9Sstevel@tonic-gate  *   None.
84*7c478bd9Sstevel@tonic-gate  */
85*7c478bd9Sstevel@tonic-gate void
86*7c478bd9Sstevel@tonic-gate sunw_crypto_init(void)
87*7c478bd9Sstevel@tonic-gate {
88*7c478bd9Sstevel@tonic-gate 	OpenSSL_add_all_algorithms();
89*7c478bd9Sstevel@tonic-gate 	ERR_load_crypto_strings();
90*7c478bd9Sstevel@tonic-gate 	ERR_load_SUNW_strings();
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate /*
94*7c478bd9Sstevel@tonic-gate  * sunw_split_certs() - Given a list of certs and a list of private keys,
95*7c478bd9Sstevel@tonic-gate  *     moves certs which match one of the keys to a different stack.
96*7c478bd9Sstevel@tonic-gate  *
97*7c478bd9Sstevel@tonic-gate  * Arguments:
98*7c478bd9Sstevel@tonic-gate  *   allkeys  - Points to a stack of private keys to search.
99*7c478bd9Sstevel@tonic-gate  *   allcerts - Points to a stack of certs to be searched.
100*7c478bd9Sstevel@tonic-gate  *   keycerts - Points to address of a stack of certs with matching private
101*7c478bd9Sstevel@tonic-gate  *              keys.  They are moved from 'allcerts'.  This may not be NULL
102*7c478bd9Sstevel@tonic-gate  *              when called.  If *keycerts is NULL upon entry, a new stack will
103*7c478bd9Sstevel@tonic-gate  *              be allocated.  Otherwise, it must be a valid STACK_OF(509).
104*7c478bd9Sstevel@tonic-gate  *   nocerts  - Points to address of a stack for keys which have no matching
105*7c478bd9Sstevel@tonic-gate  *              certs.  Keys are moved from 'allkeys' here when they have no
106*7c478bd9Sstevel@tonic-gate  *              matching certs.  If this is NULL, matchless keys will be
107*7c478bd9Sstevel@tonic-gate  *              discarded.
108*7c478bd9Sstevel@tonic-gate  *
109*7c478bd9Sstevel@tonic-gate  *   Notes:  If an error occurs while moving certs, the cert being move may be
110*7c478bd9Sstevel@tonic-gate  *   lost.  'keycerts' may only contain part of the matching certs.  The number
111*7c478bd9Sstevel@tonic-gate  *   of certs successfully moved can be found by checking sk_X509_num(keycerts).
112*7c478bd9Sstevel@tonic-gate  *
113*7c478bd9Sstevel@tonic-gate  *   If there is a key which does not have a matching cert, it is moved to
114*7c478bd9Sstevel@tonic-gate  *   the list nocerts.
115*7c478bd9Sstevel@tonic-gate  *
116*7c478bd9Sstevel@tonic-gate  *   If all certs are removed from 'certs' and/or 'pkeys', it will be the
117*7c478bd9Sstevel@tonic-gate  *   caller's responsibility to free the empty stacks.
118*7c478bd9Sstevel@tonic-gate  *
119*7c478bd9Sstevel@tonic-gate  * Returns:
120*7c478bd9Sstevel@tonic-gate  *  <  0 - An error returned.  Call ERR_get_error() to get errors information.
121*7c478bd9Sstevel@tonic-gate  *         Where possible, memory has been freed.
122*7c478bd9Sstevel@tonic-gate  *  >= 0 - The number of certs moved from 'cert' to 'pkcerts'.
123*7c478bd9Sstevel@tonic-gate  */
124*7c478bd9Sstevel@tonic-gate int
125*7c478bd9Sstevel@tonic-gate sunw_split_certs(STACK_OF(EVP_PKEY) *allkeys, STACK_OF(X509) *allcerts,
126*7c478bd9Sstevel@tonic-gate     STACK_OF(X509) **keycerts, STACK_OF(EVP_PKEY) **nocerts)
127*7c478bd9Sstevel@tonic-gate {
128*7c478bd9Sstevel@tonic-gate 	STACK_OF(X509) *matching;
129*7c478bd9Sstevel@tonic-gate 	STACK_OF(EVP_PKEY) *nomatch;
130*7c478bd9Sstevel@tonic-gate 	EVP_PKEY *tmpkey;
131*7c478bd9Sstevel@tonic-gate 	X509 *tmpcert;
132*7c478bd9Sstevel@tonic-gate 	int count = 0;
133*7c478bd9Sstevel@tonic-gate 	int found;
134*7c478bd9Sstevel@tonic-gate 	int res;
135*7c478bd9Sstevel@tonic-gate 	int i;
136*7c478bd9Sstevel@tonic-gate 	int k;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	*keycerts = NULL;
139*7c478bd9Sstevel@tonic-gate 	if (nocerts != NULL)
140*7c478bd9Sstevel@tonic-gate 		*nocerts = NULL;
141*7c478bd9Sstevel@tonic-gate 	nomatch = NULL;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	if ((matching = sk_X509_new_null()) == NULL) {
144*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_SPLIT_CERTS, SUNW_R_MEMORY_FAILURE);
145*7c478bd9Sstevel@tonic-gate 		return (-1);
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 	*keycerts = matching;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	k = 0;
150*7c478bd9Sstevel@tonic-gate 	while (k < sk_EVP_PKEY_num(allkeys)) {
151*7c478bd9Sstevel@tonic-gate 		found = 0;
152*7c478bd9Sstevel@tonic-gate 		tmpkey = sk_EVP_PKEY_value(allkeys, k);
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < sk_X509_num(allcerts); i++) {
155*7c478bd9Sstevel@tonic-gate 			tmpcert = sk_X509_value(allcerts, i);
156*7c478bd9Sstevel@tonic-gate 			res = X509_check_private_key(tmpcert, tmpkey);
157*7c478bd9Sstevel@tonic-gate 			if (res != 0) {
158*7c478bd9Sstevel@tonic-gate 				count++;
159*7c478bd9Sstevel@tonic-gate 				found = 1;
160*7c478bd9Sstevel@tonic-gate 				tmpcert = sk_X509_delete(allcerts, i);
161*7c478bd9Sstevel@tonic-gate 				if (sk_X509_push(matching, tmpcert) == 0) {
162*7c478bd9Sstevel@tonic-gate 					X509_free(tmpcert);
163*7c478bd9Sstevel@tonic-gate 					SUNWerr(SUNW_F_SPLIT_CERTS,
164*7c478bd9Sstevel@tonic-gate 					    SUNW_R_MEMORY_FAILURE);
165*7c478bd9Sstevel@tonic-gate 					return (-1);
166*7c478bd9Sstevel@tonic-gate 				}
167*7c478bd9Sstevel@tonic-gate 				break;
168*7c478bd9Sstevel@tonic-gate 			}
169*7c478bd9Sstevel@tonic-gate 		}
170*7c478bd9Sstevel@tonic-gate 		if (found != 0) {
171*7c478bd9Sstevel@tonic-gate 			/*
172*7c478bd9Sstevel@tonic-gate 			 * Found a match - keep the key & check out the next
173*7c478bd9Sstevel@tonic-gate 			 * one.
174*7c478bd9Sstevel@tonic-gate 			 */
175*7c478bd9Sstevel@tonic-gate 			k++;
176*7c478bd9Sstevel@tonic-gate 		} else {
177*7c478bd9Sstevel@tonic-gate 			/*
178*7c478bd9Sstevel@tonic-gate 			 * No cert matching this key.  Move the key if
179*7c478bd9Sstevel@tonic-gate 			 * possible or discard it.  Don't increment the
180*7c478bd9Sstevel@tonic-gate 			 * index.
181*7c478bd9Sstevel@tonic-gate 			 */
182*7c478bd9Sstevel@tonic-gate 			if (nocerts == NULL) {
183*7c478bd9Sstevel@tonic-gate 				tmpkey = sk_EVP_PKEY_delete(allkeys, k);
184*7c478bd9Sstevel@tonic-gate 				sunw_evp_pkey_free(tmpkey);
185*7c478bd9Sstevel@tonic-gate 			} else {
186*7c478bd9Sstevel@tonic-gate 				if (*nocerts == NULL) {
187*7c478bd9Sstevel@tonic-gate 					nomatch = sk_EVP_PKEY_new_null();
188*7c478bd9Sstevel@tonic-gate 					if (nomatch == NULL) {
189*7c478bd9Sstevel@tonic-gate 						SUNWerr(SUNW_F_SPLIT_CERTS,
190*7c478bd9Sstevel@tonic-gate 						    SUNW_R_MEMORY_FAILURE);
191*7c478bd9Sstevel@tonic-gate 						return (-1);
192*7c478bd9Sstevel@tonic-gate 					}
193*7c478bd9Sstevel@tonic-gate 					*nocerts = nomatch;
194*7c478bd9Sstevel@tonic-gate 				}
195*7c478bd9Sstevel@tonic-gate 				tmpkey = sk_EVP_PKEY_delete(allkeys, k);
196*7c478bd9Sstevel@tonic-gate 				if (sk_EVP_PKEY_push(nomatch, tmpkey) == 0) {
197*7c478bd9Sstevel@tonic-gate 					sunw_evp_pkey_free(tmpkey);
198*7c478bd9Sstevel@tonic-gate 					SUNWerr(SUNW_F_SPLIT_CERTS,
199*7c478bd9Sstevel@tonic-gate 					    SUNW_R_MEMORY_FAILURE);
200*7c478bd9Sstevel@tonic-gate 					return (-1);
201*7c478bd9Sstevel@tonic-gate 				}
202*7c478bd9Sstevel@tonic-gate 			}
203*7c478bd9Sstevel@tonic-gate 		}
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	return (count);
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate /*
210*7c478bd9Sstevel@tonic-gate  * sunw_evp_pkey_free() Given an EVP_PKEY structure, free any attributes
211*7c478bd9Sstevel@tonic-gate  *     that are attached.  Then free the EVP_PKEY itself.
212*7c478bd9Sstevel@tonic-gate  *
213*7c478bd9Sstevel@tonic-gate  *     This is a replacement for EVP_PKEY_free() for the sunw stuff.
214*7c478bd9Sstevel@tonic-gate  *     It should be used in places where EVP_PKEY_free would be used,
215*7c478bd9Sstevel@tonic-gate  *     including calls to sk_EVP_PKEY_pop_free().
216*7c478bd9Sstevel@tonic-gate  *
217*7c478bd9Sstevel@tonic-gate  * Arguments:
218*7c478bd9Sstevel@tonic-gate  *   pkey     - Entry which potentially has attributes to be freed.
219*7c478bd9Sstevel@tonic-gate  *
220*7c478bd9Sstevel@tonic-gate  * Returns:
221*7c478bd9Sstevel@tonic-gate  *   None.
222*7c478bd9Sstevel@tonic-gate  */
223*7c478bd9Sstevel@tonic-gate void
224*7c478bd9Sstevel@tonic-gate sunw_evp_pkey_free(EVP_PKEY *pkey)
225*7c478bd9Sstevel@tonic-gate {
226*7c478bd9Sstevel@tonic-gate 	if (pkey != NULL) {
227*7c478bd9Sstevel@tonic-gate 		if (pkey->attributes != NULL) {
228*7c478bd9Sstevel@tonic-gate 			sk_X509_ATTRIBUTE_pop_free(pkey->attributes,
229*7c478bd9Sstevel@tonic-gate 			    X509_ATTRIBUTE_free);
230*7c478bd9Sstevel@tonic-gate 			pkey->attributes = NULL;
231*7c478bd9Sstevel@tonic-gate 		}
232*7c478bd9Sstevel@tonic-gate 		EVP_PKEY_free(pkey);
233*7c478bd9Sstevel@tonic-gate 	}
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate /*
237*7c478bd9Sstevel@tonic-gate  * sunw_set_localkeyid() sets the localkeyid in a cert, a private key or
238*7c478bd9Sstevel@tonic-gate  *     both.  Any existing localkeyid will be discarded.
239*7c478bd9Sstevel@tonic-gate  *
240*7c478bd9Sstevel@tonic-gate  * Arguments:
241*7c478bd9Sstevel@tonic-gate  *   keyid_str- A byte string with the localkeyid to set
242*7c478bd9Sstevel@tonic-gate  *   keyid_len- Length of the keyid byte string.
243*7c478bd9Sstevel@tonic-gate  *   pkey     - Points to a private key to set the keyidstr in.
244*7c478bd9Sstevel@tonic-gate  *   cert     - Points to a cert to set the keyidstr in.
245*7c478bd9Sstevel@tonic-gate  *
246*7c478bd9Sstevel@tonic-gate  * Note that setting a keyid into a cert which will not be written out as
247*7c478bd9Sstevel@tonic-gate  * a PKCS12 cert is pointless since it will be lost.
248*7c478bd9Sstevel@tonic-gate  *
249*7c478bd9Sstevel@tonic-gate  * Returns:
250*7c478bd9Sstevel@tonic-gate  *   0        - Success.
251*7c478bd9Sstevel@tonic-gate  *   < 0      - An error occurred.  It was probably an error in allocating
252*7c478bd9Sstevel@tonic-gate  *              memory.  The error will be set in the error stack.  Call
253*7c478bd9Sstevel@tonic-gate  *              ERR_get_error() to get specific information.
254*7c478bd9Sstevel@tonic-gate  */
255*7c478bd9Sstevel@tonic-gate int
256*7c478bd9Sstevel@tonic-gate sunw_set_localkeyid(const char *keyid_str, int keyid_len, EVP_PKEY *pkey,
257*7c478bd9Sstevel@tonic-gate     X509 *cert)
258*7c478bd9Sstevel@tonic-gate {
259*7c478bd9Sstevel@tonic-gate 	X509_ATTRIBUTE *attr = NULL;
260*7c478bd9Sstevel@tonic-gate 	ASN1_STRING *str = NULL;
261*7c478bd9Sstevel@tonic-gate 	ASN1_TYPE *keyid = NULL;
262*7c478bd9Sstevel@tonic-gate 	int retval = -1;
263*7c478bd9Sstevel@tonic-gate 	int i;
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	if (cert != NULL) {
266*7c478bd9Sstevel@tonic-gate 		if (X509_keyid_set1(cert, (uchar_t *)keyid_str, keyid_len)
267*7c478bd9Sstevel@tonic-gate 		    == 0) {
268*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_SET_LOCALKEYID, SUNW_R_SET_LKID_ERR);
269*7c478bd9Sstevel@tonic-gate 			goto cleanup;
270*7c478bd9Sstevel@tonic-gate 		}
271*7c478bd9Sstevel@tonic-gate 	}
272*7c478bd9Sstevel@tonic-gate 	if (pkey != NULL) {
273*7c478bd9Sstevel@tonic-gate 		str = (ASN1_STRING *)M_ASN1_OCTET_STRING_new();
274*7c478bd9Sstevel@tonic-gate 		if (str == NULL ||
275*7c478bd9Sstevel@tonic-gate 		    M_ASN1_OCTET_STRING_set(str, keyid_str, keyid_len) == 0 ||
276*7c478bd9Sstevel@tonic-gate 		    (keyid = ASN1_TYPE_new()) == NULL) {
277*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_SET_LOCALKEYID, SUNW_R_MEMORY_FAILURE);
278*7c478bd9Sstevel@tonic-gate 			goto cleanup;
279*7c478bd9Sstevel@tonic-gate 		}
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 		ASN1_TYPE_set(keyid, V_ASN1_OCTET_STRING, str);
282*7c478bd9Sstevel@tonic-gate 		str = NULL;
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 		attr = type2attrib(keyid, NID_localKeyID);
285*7c478bd9Sstevel@tonic-gate 		if (attr == NULL) {
286*7c478bd9Sstevel@tonic-gate 			/*
287*7c478bd9Sstevel@tonic-gate 			 * Error already on stack
288*7c478bd9Sstevel@tonic-gate 			 */
289*7c478bd9Sstevel@tonic-gate 			goto cleanup;
290*7c478bd9Sstevel@tonic-gate 		}
291*7c478bd9Sstevel@tonic-gate 		keyid = NULL;
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 		if (pkey->attributes == NULL) {
294*7c478bd9Sstevel@tonic-gate 			pkey->attributes = sk_X509_ATTRIBUTE_new_null();
295*7c478bd9Sstevel@tonic-gate 			if (pkey->attributes == NULL) {
296*7c478bd9Sstevel@tonic-gate 				SUNWerr(SUNW_F_SET_LOCALKEYID,
297*7c478bd9Sstevel@tonic-gate 				    SUNW_R_MEMORY_FAILURE);
298*7c478bd9Sstevel@tonic-gate 				goto cleanup;
299*7c478bd9Sstevel@tonic-gate 			}
300*7c478bd9Sstevel@tonic-gate 		} else {
301*7c478bd9Sstevel@tonic-gate 			i = find_attr_by_nid(pkey->attributes, NID_localKeyID);
302*7c478bd9Sstevel@tonic-gate 			if (i >= 0)
303*7c478bd9Sstevel@tonic-gate 				sk_X509_ATTRIBUTE_delete(pkey->attributes, i);
304*7c478bd9Sstevel@tonic-gate 		}
305*7c478bd9Sstevel@tonic-gate 		if (sk_X509_ATTRIBUTE_push(pkey->attributes, attr) == 0) {
306*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_SET_LOCALKEYID, SUNW_R_MEMORY_FAILURE);
307*7c478bd9Sstevel@tonic-gate 			goto cleanup;
308*7c478bd9Sstevel@tonic-gate 		}
309*7c478bd9Sstevel@tonic-gate 		attr = NULL;
310*7c478bd9Sstevel@tonic-gate 	}
311*7c478bd9Sstevel@tonic-gate 	retval = 0;
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate cleanup:
314*7c478bd9Sstevel@tonic-gate 	if (str != NULL)
315*7c478bd9Sstevel@tonic-gate 		ASN1_STRING_free(str);
316*7c478bd9Sstevel@tonic-gate 	if (keyid != NULL)
317*7c478bd9Sstevel@tonic-gate 		ASN1_TYPE_free(keyid);
318*7c478bd9Sstevel@tonic-gate 	if (attr != NULL)
319*7c478bd9Sstevel@tonic-gate 		X509_ATTRIBUTE_free(attr);
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 	return (retval);
322*7c478bd9Sstevel@tonic-gate }
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate /*
325*7c478bd9Sstevel@tonic-gate  * sunw_get_pkey_localkeyid() gets the localkeyid from a private key.  It can
326*7c478bd9Sstevel@tonic-gate  *     optionally remove the value found.
327*7c478bd9Sstevel@tonic-gate  *
328*7c478bd9Sstevel@tonic-gate  * Arguments:
329*7c478bd9Sstevel@tonic-gate  *   dowhat   - What to do with the attributes (remove them or copy them).
330*7c478bd9Sstevel@tonic-gate  *   pkey     - Points to a private key to set the keyidstr in.
331*7c478bd9Sstevel@tonic-gate  *   keyid_str- Points to a location which will receive the pointer to
332*7c478bd9Sstevel@tonic-gate  *              a byte string containing the binary localkeyid.  Note that
333*7c478bd9Sstevel@tonic-gate  *              this is a copy, and the caller must free it.
334*7c478bd9Sstevel@tonic-gate  *   keyid_len- Length of keyid_str.
335*7c478bd9Sstevel@tonic-gate  *
336*7c478bd9Sstevel@tonic-gate  * Returns:
337*7c478bd9Sstevel@tonic-gate  *   >= 0     - The number of characters in the keyid returned.
338*7c478bd9Sstevel@tonic-gate  *   < 0      - An error occurred.  It was probably an error in allocating
339*7c478bd9Sstevel@tonic-gate  *              memory.  The error will be set in the error stack.  Call
340*7c478bd9Sstevel@tonic-gate  *              ERR_get_error() to get specific information.
341*7c478bd9Sstevel@tonic-gate  */
342*7c478bd9Sstevel@tonic-gate int
343*7c478bd9Sstevel@tonic-gate sunw_get_pkey_localkeyid(getdo_actions_t dowhat, EVP_PKEY *pkey,
344*7c478bd9Sstevel@tonic-gate     char **keyid_str, int *keyid_len)
345*7c478bd9Sstevel@tonic-gate {
346*7c478bd9Sstevel@tonic-gate 	X509_ATTRIBUTE *attr = NULL;
347*7c478bd9Sstevel@tonic-gate 	ASN1_OCTET_STRING *str = NULL;
348*7c478bd9Sstevel@tonic-gate 	ASN1_TYPE *ty = NULL;
349*7c478bd9Sstevel@tonic-gate 	int len = 0;
350*7c478bd9Sstevel@tonic-gate 	int i;
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate 	if (keyid_str != NULL)
353*7c478bd9Sstevel@tonic-gate 		*keyid_str = NULL;
354*7c478bd9Sstevel@tonic-gate 	if (keyid_len != NULL)
355*7c478bd9Sstevel@tonic-gate 		*keyid_len = 0;
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 	if (pkey == NULL || pkey->attributes == NULL) {
358*7c478bd9Sstevel@tonic-gate 		return (0);
359*7c478bd9Sstevel@tonic-gate 	}
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 	if ((i = find_attr_by_nid(pkey->attributes, NID_localKeyID)) < 0) {
362*7c478bd9Sstevel@tonic-gate 		return (0);
363*7c478bd9Sstevel@tonic-gate 	}
364*7c478bd9Sstevel@tonic-gate 	attr = sk_X509_ATTRIBUTE_value(pkey->attributes, i);
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 	if ((ty = attrib2type(attr)) == NULL ||
367*7c478bd9Sstevel@tonic-gate 	    ty->type != V_ASN1_OCTET_STRING) {
368*7c478bd9Sstevel@tonic-gate 		return (0);
369*7c478bd9Sstevel@tonic-gate 	}
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate 	if (dowhat == GETDO_DEL) {
372*7c478bd9Sstevel@tonic-gate 		attr = sk_X509_ATTRIBUTE_delete(pkey->attributes, i);
373*7c478bd9Sstevel@tonic-gate 		if (attr != NULL)
374*7c478bd9Sstevel@tonic-gate 			X509_ATTRIBUTE_free(attr);
375*7c478bd9Sstevel@tonic-gate 		return (0);
376*7c478bd9Sstevel@tonic-gate 	}
377*7c478bd9Sstevel@tonic-gate 
378*7c478bd9Sstevel@tonic-gate 	str = ty->value.octet_string;
379*7c478bd9Sstevel@tonic-gate 	len = str->length;
380*7c478bd9Sstevel@tonic-gate 	if ((*keyid_str = malloc(len)) == NULL) {
381*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_GET_LOCALKEYID, SUNW_R_MEMORY_FAILURE);
382*7c478bd9Sstevel@tonic-gate 		return (-1);
383*7c478bd9Sstevel@tonic-gate 	}
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 	(void) memcpy(*keyid_str, str->data, len);
386*7c478bd9Sstevel@tonic-gate 	*keyid_len = len;
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	return (len);
389*7c478bd9Sstevel@tonic-gate }
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate /*
392*7c478bd9Sstevel@tonic-gate  * sunw_get_pkey_fname() gets the friendlyName from a private key.  It can
393*7c478bd9Sstevel@tonic-gate  *     optionally remove the value found.
394*7c478bd9Sstevel@tonic-gate  *
395*7c478bd9Sstevel@tonic-gate  * Arguments:
396*7c478bd9Sstevel@tonic-gate  *   dowhat   - What to do with the attributes (remove them or copy them).
397*7c478bd9Sstevel@tonic-gate  *   pkey     - Points to a private key to get the frientlyname from
398*7c478bd9Sstevel@tonic-gate  *   fname    - Points to a location which will receive the pointer to a
399*7c478bd9Sstevel@tonic-gate  *              byte string with the ASCII friendlyname
400*7c478bd9Sstevel@tonic-gate  *
401*7c478bd9Sstevel@tonic-gate  * Returns:
402*7c478bd9Sstevel@tonic-gate  *   >= 0     - The number of characters in the frienlyname returned.
403*7c478bd9Sstevel@tonic-gate  *   < 0      - An error occurred.  It was probably an error in allocating
404*7c478bd9Sstevel@tonic-gate  *              memory.  The error will be set in the error stack.  Call
405*7c478bd9Sstevel@tonic-gate  *              ERR_get_error() to get specific information.
406*7c478bd9Sstevel@tonic-gate  */
407*7c478bd9Sstevel@tonic-gate int
408*7c478bd9Sstevel@tonic-gate sunw_get_pkey_fname(getdo_actions_t dowhat, EVP_PKEY *pkey, char **fname)
409*7c478bd9Sstevel@tonic-gate {
410*7c478bd9Sstevel@tonic-gate 	X509_ATTRIBUTE *attr = NULL;
411*7c478bd9Sstevel@tonic-gate 	ASN1_BMPSTRING *str = NULL;
412*7c478bd9Sstevel@tonic-gate 	ASN1_TYPE *ty = NULL;
413*7c478bd9Sstevel@tonic-gate 	int len = 0;
414*7c478bd9Sstevel@tonic-gate 	int i;
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 	if (fname != NULL)
417*7c478bd9Sstevel@tonic-gate 		*fname = NULL;
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 	if (pkey == NULL || pkey->attributes == NULL) {
420*7c478bd9Sstevel@tonic-gate 		return (0);
421*7c478bd9Sstevel@tonic-gate 	}
422*7c478bd9Sstevel@tonic-gate 
423*7c478bd9Sstevel@tonic-gate 	if ((i = find_attr_by_nid(pkey->attributes, NID_friendlyName)) < 0) {
424*7c478bd9Sstevel@tonic-gate 		return (0);
425*7c478bd9Sstevel@tonic-gate 	}
426*7c478bd9Sstevel@tonic-gate 	attr = sk_X509_ATTRIBUTE_value(pkey->attributes, i);
427*7c478bd9Sstevel@tonic-gate 
428*7c478bd9Sstevel@tonic-gate 	if ((ty = attrib2type(attr)) == NULL ||
429*7c478bd9Sstevel@tonic-gate 	    ty->type != V_ASN1_BMPSTRING) {
430*7c478bd9Sstevel@tonic-gate 		return (0);
431*7c478bd9Sstevel@tonic-gate 	}
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 	if (dowhat == GETDO_DEL) {
434*7c478bd9Sstevel@tonic-gate 		attr = sk_X509_ATTRIBUTE_delete(pkey->attributes, i);
435*7c478bd9Sstevel@tonic-gate 		if (attr != NULL)
436*7c478bd9Sstevel@tonic-gate 			X509_ATTRIBUTE_free(attr);
437*7c478bd9Sstevel@tonic-gate 		return (0);
438*7c478bd9Sstevel@tonic-gate 	}
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate 	str = ty->value.bmpstring;
441*7c478bd9Sstevel@tonic-gate 	*fname = uni2asc(str->data, str->length);
442*7c478bd9Sstevel@tonic-gate 	if (*fname == NULL) {
443*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_GET_PKEY_FNAME, SUNW_R_MEMORY_FAILURE);
444*7c478bd9Sstevel@tonic-gate 		return (-1);
445*7c478bd9Sstevel@tonic-gate 	}
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 	len = strlen(*fname);
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate 	return (len);
450*7c478bd9Sstevel@tonic-gate }
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate /*
453*7c478bd9Sstevel@tonic-gate  * sunw_find_localkeyid() searches stacks of certs and private keys,
454*7c478bd9Sstevel@tonic-gate  *     and returns  the first matching cert/private key found.
455*7c478bd9Sstevel@tonic-gate  *
456*7c478bd9Sstevel@tonic-gate  * Look for a keyid in a stack of certs.  if 'certs' is NULL and 'pkeys' is
457*7c478bd9Sstevel@tonic-gate  * not NULL, search the list of private keys.  Move the matching cert to
458*7c478bd9Sstevel@tonic-gate  * 'matching_cert' and its matching private key to 'matching_pkey'.  If no
459*7c478bd9Sstevel@tonic-gate  * cert or keys match, no match occurred.
460*7c478bd9Sstevel@tonic-gate  *
461*7c478bd9Sstevel@tonic-gate  * Arguments:
462*7c478bd9Sstevel@tonic-gate  *   keyid_str- A byte string with the localkeyid to match
463*7c478bd9Sstevel@tonic-gate  *   keyid_len- Length of the keyid byte string.
464*7c478bd9Sstevel@tonic-gate  *   pkeys    - Points to a stack of private keys which match the certs.
465*7c478bd9Sstevel@tonic-gate  *              This may be NULL, in which case no keys are returned.
466*7c478bd9Sstevel@tonic-gate  *   certs    - Points to a stack of certs to search.  If NULL, search the
467*7c478bd9Sstevel@tonic-gate  *              stack of keys instead.
468*7c478bd9Sstevel@tonic-gate  *   matching_pkey
469*7c478bd9Sstevel@tonic-gate  *            - Pointer to receive address of first matching pkey found.
470*7c478bd9Sstevel@tonic-gate  *              'matching_pkey' must not be NULL; '*matching_pkey' will be
471*7c478bd9Sstevel@tonic-gate  *              reset.
472*7c478bd9Sstevel@tonic-gate  *   matching_cert
473*7c478bd9Sstevel@tonic-gate  *            - Pointer to receive address of first matching cert found.
474*7c478bd9Sstevel@tonic-gate  *              'matching_cert' must not be NULL; '*matching_cert' will be
475*7c478bd9Sstevel@tonic-gate  *              reset.
476*7c478bd9Sstevel@tonic-gate  *
477*7c478bd9Sstevel@tonic-gate  * Returns:
478*7c478bd9Sstevel@tonic-gate  *  <  0 - An error returned.  Call ERR_get_error() to get errors information.
479*7c478bd9Sstevel@tonic-gate  *         Where possible, memory has been freed.
480*7c478bd9Sstevel@tonic-gate  *  >= 0 - Objects were found and returned.  Which objects are indicated by
481*7c478bd9Sstevel@tonic-gate  *         which bits are set (FOUND_PKEY and/or FOUND_CERT).
482*7c478bd9Sstevel@tonic-gate  */
483*7c478bd9Sstevel@tonic-gate int
484*7c478bd9Sstevel@tonic-gate sunw_find_localkeyid(char *keyid_str, int len, STACK_OF(EVP_PKEY) *pkeys,
485*7c478bd9Sstevel@tonic-gate STACK_OF(X509) *certs, EVP_PKEY **matching_pkey, X509 **matching_cert)
486*7c478bd9Sstevel@tonic-gate {
487*7c478bd9Sstevel@tonic-gate 	ASN1_STRING *cmpstr = NULL;
488*7c478bd9Sstevel@tonic-gate 	EVP_PKEY *tmp_pkey = NULL;
489*7c478bd9Sstevel@tonic-gate 	X509 *tmp_cert = NULL;
490*7c478bd9Sstevel@tonic-gate 	int retval = 0;
491*7c478bd9Sstevel@tonic-gate 
492*7c478bd9Sstevel@tonic-gate 	/* If NULL arguments, this is an error */
493*7c478bd9Sstevel@tonic-gate 	if (keyid_str == NULL ||
494*7c478bd9Sstevel@tonic-gate 	    (pkeys == NULL || certs == NULL) ||
495*7c478bd9Sstevel@tonic-gate 	    (pkeys != NULL && matching_pkey == NULL) ||
496*7c478bd9Sstevel@tonic-gate 	    (certs != NULL && matching_cert == NULL)) {
497*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_FIND_LOCALKEYID, SUNW_R_INVALID_ARG);
498*7c478bd9Sstevel@tonic-gate 		return (-1);
499*7c478bd9Sstevel@tonic-gate 	}
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate 	if (matching_pkey != NULL)
502*7c478bd9Sstevel@tonic-gate 		*matching_pkey = NULL;
503*7c478bd9Sstevel@tonic-gate 	if (matching_cert != NULL)
504*7c478bd9Sstevel@tonic-gate 		*matching_cert = NULL;
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate 	cmpstr = (ASN1_STRING *)M_ASN1_OCTET_STRING_new();
507*7c478bd9Sstevel@tonic-gate 	if (cmpstr == NULL ||
508*7c478bd9Sstevel@tonic-gate 	    M_ASN1_OCTET_STRING_set(cmpstr, keyid_str, len) == 0) {
509*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_FIND_LOCALKEYID, SUNW_R_MEMORY_FAILURE);
510*7c478bd9Sstevel@tonic-gate 		return (-1);
511*7c478bd9Sstevel@tonic-gate 	}
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 	retval = find_attr(NID_localKeyID, cmpstr, pkeys, &tmp_pkey, certs,
514*7c478bd9Sstevel@tonic-gate 	    &tmp_cert);
515*7c478bd9Sstevel@tonic-gate 	if (retval == 0) {
516*7c478bd9Sstevel@tonic-gate 		ASN1_STRING_free(cmpstr);
517*7c478bd9Sstevel@tonic-gate 		return (retval);
518*7c478bd9Sstevel@tonic-gate 	}
519*7c478bd9Sstevel@tonic-gate 
520*7c478bd9Sstevel@tonic-gate 	if (matching_pkey != NULL)
521*7c478bd9Sstevel@tonic-gate 		*matching_pkey = tmp_pkey;
522*7c478bd9Sstevel@tonic-gate 	if (matching_cert != NULL)
523*7c478bd9Sstevel@tonic-gate 		*matching_cert = tmp_cert;
524*7c478bd9Sstevel@tonic-gate 
525*7c478bd9Sstevel@tonic-gate 	return (retval);
526*7c478bd9Sstevel@tonic-gate }
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate /*
529*7c478bd9Sstevel@tonic-gate  * sunw_find_fname() searches stacks of certs and private keys for one with
530*7c478bd9Sstevel@tonic-gate  *     a matching friendlyname and returns the first matching cert/private
531*7c478bd9Sstevel@tonic-gate  *     key found.
532*7c478bd9Sstevel@tonic-gate  *
533*7c478bd9Sstevel@tonic-gate  * Look for a friendlyname in a stack of certs.  if 'certs' is NULL and 'pkeys'
534*7c478bd9Sstevel@tonic-gate  * is not NULL, search the list of private keys.  Move the matching cert to
535*7c478bd9Sstevel@tonic-gate  * 'matching_cert' and its matching private key to 'matching_pkey'.  If no
536*7c478bd9Sstevel@tonic-gate  * cert or keys match, no match occurred.
537*7c478bd9Sstevel@tonic-gate  *
538*7c478bd9Sstevel@tonic-gate  * Arguments:
539*7c478bd9Sstevel@tonic-gate  *   fname    - Friendlyname to find (NULL-terminated ASCII string).
540*7c478bd9Sstevel@tonic-gate  *   pkeys    - Points to a stack of private keys which match the certs.
541*7c478bd9Sstevel@tonic-gate  *              This may be NULL, in which case no keys are returned.
542*7c478bd9Sstevel@tonic-gate  *   certs    - Points to a stack of certs to search.  If NULL, search the
543*7c478bd9Sstevel@tonic-gate  *              stack of keys instead.
544*7c478bd9Sstevel@tonic-gate  *   matching_pkey
545*7c478bd9Sstevel@tonic-gate  *            - Pointer to receive address of first matching pkey found.
546*7c478bd9Sstevel@tonic-gate  *   matching_cert
547*7c478bd9Sstevel@tonic-gate  *            - Pointer to receive address of first matching cert found.
548*7c478bd9Sstevel@tonic-gate  *
549*7c478bd9Sstevel@tonic-gate  * Returns:
550*7c478bd9Sstevel@tonic-gate  *  <  0 - An error returned.  Call ERR_get_error() to get errors information.
551*7c478bd9Sstevel@tonic-gate  *         Where possible, memory has been freed.
552*7c478bd9Sstevel@tonic-gate  *  >= 0 - Objects were found and returned.  Which objects are indicated by
553*7c478bd9Sstevel@tonic-gate  *         which bits are set (FOUND_PKEY and/or FOUND_CERT).
554*7c478bd9Sstevel@tonic-gate  */
555*7c478bd9Sstevel@tonic-gate int
556*7c478bd9Sstevel@tonic-gate sunw_find_fname(char *fname, STACK_OF(EVP_PKEY) *pkeys, STACK_OF(X509) *certs,
557*7c478bd9Sstevel@tonic-gate     EVP_PKEY **matching_pkey, X509 ** matching_cert)
558*7c478bd9Sstevel@tonic-gate {
559*7c478bd9Sstevel@tonic-gate 	ASN1_STRING *cmpstr = NULL;
560*7c478bd9Sstevel@tonic-gate 	EVP_PKEY *tmp_pkey = NULL;
561*7c478bd9Sstevel@tonic-gate 	X509 *tmp_cert = NULL;
562*7c478bd9Sstevel@tonic-gate 	int retval = 0;
563*7c478bd9Sstevel@tonic-gate 
564*7c478bd9Sstevel@tonic-gate 	/* If NULL arguments, this is an error */
565*7c478bd9Sstevel@tonic-gate 	if (fname == NULL ||
566*7c478bd9Sstevel@tonic-gate 	    (pkeys == NULL || certs == NULL) ||
567*7c478bd9Sstevel@tonic-gate 	    (pkeys != NULL && matching_pkey == NULL) ||
568*7c478bd9Sstevel@tonic-gate 	    (certs != NULL && matching_cert == NULL)) {
569*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_FIND_FNAME, SUNW_R_INVALID_ARG);
570*7c478bd9Sstevel@tonic-gate 		return (-1);
571*7c478bd9Sstevel@tonic-gate 	}
572*7c478bd9Sstevel@tonic-gate 
573*7c478bd9Sstevel@tonic-gate 	if (matching_pkey != NULL)
574*7c478bd9Sstevel@tonic-gate 		*matching_pkey = NULL;
575*7c478bd9Sstevel@tonic-gate 	if (matching_cert != NULL)
576*7c478bd9Sstevel@tonic-gate 		*matching_cert = NULL;
577*7c478bd9Sstevel@tonic-gate 
578*7c478bd9Sstevel@tonic-gate 	cmpstr = (ASN1_STRING *)asc2bmpstring(fname, strlen(fname));
579*7c478bd9Sstevel@tonic-gate 	if (cmpstr == NULL) {
580*7c478bd9Sstevel@tonic-gate 		/*
581*7c478bd9Sstevel@tonic-gate 		 * Error already on stack
582*7c478bd9Sstevel@tonic-gate 		 */
583*7c478bd9Sstevel@tonic-gate 		return (-1);
584*7c478bd9Sstevel@tonic-gate 	}
585*7c478bd9Sstevel@tonic-gate 
586*7c478bd9Sstevel@tonic-gate 	retval = find_attr(NID_friendlyName, cmpstr, pkeys, &tmp_pkey, certs,
587*7c478bd9Sstevel@tonic-gate 	    &tmp_cert);
588*7c478bd9Sstevel@tonic-gate 	if (retval == 0) {
589*7c478bd9Sstevel@tonic-gate 		ASN1_STRING_free(cmpstr);
590*7c478bd9Sstevel@tonic-gate 		return (retval);
591*7c478bd9Sstevel@tonic-gate 	}
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate 	if (matching_pkey != NULL)
594*7c478bd9Sstevel@tonic-gate 		*matching_pkey = tmp_pkey;
595*7c478bd9Sstevel@tonic-gate 	if (matching_cert != NULL)
596*7c478bd9Sstevel@tonic-gate 		*matching_cert = tmp_cert;
597*7c478bd9Sstevel@tonic-gate 
598*7c478bd9Sstevel@tonic-gate 	return (retval);
599*7c478bd9Sstevel@tonic-gate }
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate /*
602*7c478bd9Sstevel@tonic-gate  * sunw_print_times() formats and prints cert times to the given file.
603*7c478bd9Sstevel@tonic-gate  *
604*7c478bd9Sstevel@tonic-gate  * The label is printed on one line. One or both dates are printed on
605*7c478bd9Sstevel@tonic-gate  * the following line or two, each with it's own indented label in the
606*7c478bd9Sstevel@tonic-gate  * format:
607*7c478bd9Sstevel@tonic-gate  *
608*7c478bd9Sstevel@tonic-gate  *    label
609*7c478bd9Sstevel@tonic-gate  *      'not before' date: whatever
610*7c478bd9Sstevel@tonic-gate  *      'not after' date:  whatever
611*7c478bd9Sstevel@tonic-gate  *
612*7c478bd9Sstevel@tonic-gate  * Arguments:
613*7c478bd9Sstevel@tonic-gate  *   fp       - file pointer for file to write to.
614*7c478bd9Sstevel@tonic-gate  *   dowhat   - what field(s) to print.
615*7c478bd9Sstevel@tonic-gate  *   label    - Label to use.  If NULL, no line will be printed.
616*7c478bd9Sstevel@tonic-gate  *   cert     - Points to a client or CA certs to check
617*7c478bd9Sstevel@tonic-gate  *
618*7c478bd9Sstevel@tonic-gate  * Returns:
619*7c478bd9Sstevel@tonic-gate  *  <  0 - An error occured.
620*7c478bd9Sstevel@tonic-gate  *  >= 0 - Number of lines written.
621*7c478bd9Sstevel@tonic-gate  */
622*7c478bd9Sstevel@tonic-gate int
623*7c478bd9Sstevel@tonic-gate sunw_print_times(FILE *fp, prnt_actions_t dowhat, char *label, X509 *cert)
624*7c478bd9Sstevel@tonic-gate {
625*7c478bd9Sstevel@tonic-gate 	int lines = 0;
626*7c478bd9Sstevel@tonic-gate 
627*7c478bd9Sstevel@tonic-gate 	if (label != NULL) {
628*7c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%s\n", label);
629*7c478bd9Sstevel@tonic-gate 		lines++;
630*7c478bd9Sstevel@tonic-gate 	}
631*7c478bd9Sstevel@tonic-gate 
632*7c478bd9Sstevel@tonic-gate 	if (dowhat == PRNT_NOT_BEFORE || dowhat == PRNT_BOTH) {
633*7c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "'not before' date: ");
634*7c478bd9Sstevel@tonic-gate 		(void) print_time(fp, X509_get_notBefore(cert));
635*7c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\n");
636*7c478bd9Sstevel@tonic-gate 		lines++;
637*7c478bd9Sstevel@tonic-gate 	}
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 	if (dowhat == PRNT_NOT_AFTER || dowhat == PRNT_BOTH) {
640*7c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "'not after' date:  ");
641*7c478bd9Sstevel@tonic-gate 		(void) print_time(fp, X509_get_notAfter(cert));
642*7c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\n");
643*7c478bd9Sstevel@tonic-gate 		lines++;
644*7c478bd9Sstevel@tonic-gate 	}
645*7c478bd9Sstevel@tonic-gate 	return (lines);
646*7c478bd9Sstevel@tonic-gate }
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate /*
649*7c478bd9Sstevel@tonic-gate  * sunw_check_keys() compares the public key in the certificate and a
650*7c478bd9Sstevel@tonic-gate  *     private key to ensure that they match.
651*7c478bd9Sstevel@tonic-gate  *
652*7c478bd9Sstevel@tonic-gate  * Arguments:
653*7c478bd9Sstevel@tonic-gate  *   cert     - Points to a certificate.
654*7c478bd9Sstevel@tonic-gate  *   pkey     - Points to a private key.
655*7c478bd9Sstevel@tonic-gate  *
656*7c478bd9Sstevel@tonic-gate  * Returns:
657*7c478bd9Sstevel@tonic-gate  *  == 0 - These do not match.
658*7c478bd9Sstevel@tonic-gate  *  != 0 - The cert's public key and the private key match.
659*7c478bd9Sstevel@tonic-gate  */
660*7c478bd9Sstevel@tonic-gate int
661*7c478bd9Sstevel@tonic-gate sunw_check_keys(X509 *cert, EVP_PKEY *pkey)
662*7c478bd9Sstevel@tonic-gate {
663*7c478bd9Sstevel@tonic-gate 	int retval = 0;
664*7c478bd9Sstevel@tonic-gate 
665*7c478bd9Sstevel@tonic-gate 	if (pkey != NULL && cert != NULL)
666*7c478bd9Sstevel@tonic-gate 		retval = X509_check_private_key(cert, pkey);
667*7c478bd9Sstevel@tonic-gate 
668*7c478bd9Sstevel@tonic-gate 	return (retval);
669*7c478bd9Sstevel@tonic-gate }
670*7c478bd9Sstevel@tonic-gate 
671*7c478bd9Sstevel@tonic-gate /*
672*7c478bd9Sstevel@tonic-gate  * sunw_issuer_attrs - Given a cert, return the issuer-specific attributes
673*7c478bd9Sstevel@tonic-gate  *     as one ASCII string.
674*7c478bd9Sstevel@tonic-gate  *
675*7c478bd9Sstevel@tonic-gate  * Arguments:
676*7c478bd9Sstevel@tonic-gate  *   cert     - Cert to process
677*7c478bd9Sstevel@tonic-gate  *   buf      - If non-NULL, buffer to receive string.  If NULL, one will
678*7c478bd9Sstevel@tonic-gate  *              be allocated and its value will be returned to the caller.
679*7c478bd9Sstevel@tonic-gate  *   len      - If 'buff' is non-null, the buffer's length.
680*7c478bd9Sstevel@tonic-gate  *
681*7c478bd9Sstevel@tonic-gate  * This returns an ASCII string with all issuer-related attributes in one
682*7c478bd9Sstevel@tonic-gate  * string separated by '/' characters.  Each attribute begins with its name
683*7c478bd9Sstevel@tonic-gate  * and an equal sign.  Two attributes (ATTR1 and Attr2) would have the
684*7c478bd9Sstevel@tonic-gate  * following form:
685*7c478bd9Sstevel@tonic-gate  *
686*7c478bd9Sstevel@tonic-gate  *         ATTR1=attr_value/ATTR2=attr2_value
687*7c478bd9Sstevel@tonic-gate  *
688*7c478bd9Sstevel@tonic-gate  * Returns:
689*7c478bd9Sstevel@tonic-gate  *   != NULL  - Pointer to the ASCII string containing the issuer-related
690*7c478bd9Sstevel@tonic-gate  *              attributes.  If the 'buf' argument was NULL, this is a
691*7c478bd9Sstevel@tonic-gate  *              dynamically-allocated buffer and the caller will have the
692*7c478bd9Sstevel@tonic-gate  *              responsibility for freeing it.
693*7c478bd9Sstevel@tonic-gate  *   NULL     - Memory needed to be allocated but could not be.  Errors
694*7c478bd9Sstevel@tonic-gate  *              are set on the error stack.
695*7c478bd9Sstevel@tonic-gate  */
696*7c478bd9Sstevel@tonic-gate char *
697*7c478bd9Sstevel@tonic-gate sunw_issuer_attrs(X509 *cert, char *buf, int len)
698*7c478bd9Sstevel@tonic-gate {
699*7c478bd9Sstevel@tonic-gate 	return (X509_NAME_oneline(X509_get_issuer_name(cert), buf, len));
700*7c478bd9Sstevel@tonic-gate }
701*7c478bd9Sstevel@tonic-gate 
702*7c478bd9Sstevel@tonic-gate /*
703*7c478bd9Sstevel@tonic-gate  * sunw_subject_attrs - Given a cert, return the subject-specific attributes
704*7c478bd9Sstevel@tonic-gate  *     as one ASCII string.
705*7c478bd9Sstevel@tonic-gate  *
706*7c478bd9Sstevel@tonic-gate  * Arguments:
707*7c478bd9Sstevel@tonic-gate  *   cert     - Cert to process
708*7c478bd9Sstevel@tonic-gate  *   buf      - If non-NULL, buffer to receive string.  If NULL, one will
709*7c478bd9Sstevel@tonic-gate  *              be allocated and its value will be returned to the caller.
710*7c478bd9Sstevel@tonic-gate  *   len      - If 'buff' is non-null, the buffer's length.
711*7c478bd9Sstevel@tonic-gate  *
712*7c478bd9Sstevel@tonic-gate  * This returns an ASCII string with all subject-related attributes in one
713*7c478bd9Sstevel@tonic-gate  * string separated by '/' characters.  Each attribute begins with its name
714*7c478bd9Sstevel@tonic-gate  * and an equal sign.  Two attributes (ATTR1 and Attr2) would have the
715*7c478bd9Sstevel@tonic-gate  * following form:
716*7c478bd9Sstevel@tonic-gate  *
717*7c478bd9Sstevel@tonic-gate  *         ATTR1=attr_value/ATTR2=attr2_value
718*7c478bd9Sstevel@tonic-gate  *
719*7c478bd9Sstevel@tonic-gate  * Returns:
720*7c478bd9Sstevel@tonic-gate  *   != NULL  - Pointer to the ASCII string containing the subject-related
721*7c478bd9Sstevel@tonic-gate  *              attributes.  If the 'buf' argument was NULL, this is a
722*7c478bd9Sstevel@tonic-gate  *              dynamically-allocated buffer and the caller will have the
723*7c478bd9Sstevel@tonic-gate  *              responsibility for freeing it.
724*7c478bd9Sstevel@tonic-gate  *   NULL     - Memory needed to be allocated but could not be.  Errors
725*7c478bd9Sstevel@tonic-gate  *              are set on the error stack.
726*7c478bd9Sstevel@tonic-gate  */
727*7c478bd9Sstevel@tonic-gate char *
728*7c478bd9Sstevel@tonic-gate sunw_subject_attrs(X509 *cert, char *buf, int len)
729*7c478bd9Sstevel@tonic-gate {
730*7c478bd9Sstevel@tonic-gate 	return (X509_NAME_oneline(X509_get_subject_name(cert), buf, len));
731*7c478bd9Sstevel@tonic-gate }
732*7c478bd9Sstevel@tonic-gate 
733*7c478bd9Sstevel@tonic-gate /*
734*7c478bd9Sstevel@tonic-gate  * sunw_append_keys - Given two stacks of private keys, remove the keys from
735*7c478bd9Sstevel@tonic-gate  *      the second stack and append them to the first.  Both stacks must exist
736*7c478bd9Sstevel@tonic-gate  *      at time of call.
737*7c478bd9Sstevel@tonic-gate  *
738*7c478bd9Sstevel@tonic-gate  * Arguments:
739*7c478bd9Sstevel@tonic-gate  *   dst 	- the stack to receive the keys from 'src'
740*7c478bd9Sstevel@tonic-gate  *   src	- the stack whose keys are to be moved.
741*7c478bd9Sstevel@tonic-gate  *
742*7c478bd9Sstevel@tonic-gate  * Returns:
743*7c478bd9Sstevel@tonic-gate  *   -1  	- An error occurred.  The error status is set.
744*7c478bd9Sstevel@tonic-gate  *   >= 0       - The number of keys that were copied.
745*7c478bd9Sstevel@tonic-gate  */
746*7c478bd9Sstevel@tonic-gate int
747*7c478bd9Sstevel@tonic-gate sunw_append_keys(STACK_OF(EVP_PKEY) *dst, STACK_OF(EVP_PKEY) *src)
748*7c478bd9Sstevel@tonic-gate {
749*7c478bd9Sstevel@tonic-gate 	EVP_PKEY *tmpk;
750*7c478bd9Sstevel@tonic-gate 	int count = 0;
751*7c478bd9Sstevel@tonic-gate 
752*7c478bd9Sstevel@tonic-gate 	while (sk_EVP_PKEY_num(src) > 0) {
753*7c478bd9Sstevel@tonic-gate 		tmpk = sk_EVP_PKEY_delete(src, 0);
754*7c478bd9Sstevel@tonic-gate 		if (sk_EVP_PKEY_push(dst, tmpk) == 0) {
755*7c478bd9Sstevel@tonic-gate 			sunw_evp_pkey_free(tmpk);
756*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_APPEND_KEYS, SUNW_R_MEMORY_FAILURE);
757*7c478bd9Sstevel@tonic-gate 			return (-1);
758*7c478bd9Sstevel@tonic-gate 		}
759*7c478bd9Sstevel@tonic-gate 		count++;
760*7c478bd9Sstevel@tonic-gate 	}
761*7c478bd9Sstevel@tonic-gate 
762*7c478bd9Sstevel@tonic-gate 	return (count);
763*7c478bd9Sstevel@tonic-gate }
764