xref: /titanic_51/usr/src/common/net/wanboot/p12access.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * This file includes interfaces to be used together with SSL to get PKCS#12
24*7c478bd9Sstevel@tonic-gate  * certs and pass them to SSL.  They replace similar functions for PEM,
25*7c478bd9Sstevel@tonic-gate  * already provided for within SSL.
26*7c478bd9Sstevel@tonic-gate  *
27*7c478bd9Sstevel@tonic-gate  * The interfaces included here are:
28*7c478bd9Sstevel@tonic-gate  *   sunw_p12_use_certfile - gets the user's cert from a pkcs12 file & pass
29*7c478bd9Sstevel@tonic-gate  *                it to SSL.
30*7c478bd9Sstevel@tonic-gate  *   sunw_p12_use_keyfile - gets the RSA private key from a pkcs12 file and
31*7c478bd9Sstevel@tonic-gate  *                pass it to SSL
32*7c478bd9Sstevel@tonic-gate  *   sunw_p12_use_trustfile - read the pkcs12 trust anchor (aka certificate
33*7c478bd9Sstevel@tonic-gate  *                authority certs) file into memory and hand them off to SSL.
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * These functions use the sunw_PKCS12_parse to read the certs.
36*7c478bd9Sstevel@tonic-gate  *
37*7c478bd9Sstevel@tonic-gate  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
38*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #include <stdio.h>
44*7c478bd9Sstevel@tonic-gate #include <strings.h>
45*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
47*7c478bd9Sstevel@tonic-gate #include <unistd.h>
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate #include <openssl/crypto.h>
50*7c478bd9Sstevel@tonic-gate #include <openssl/err.h>
51*7c478bd9Sstevel@tonic-gate #include <openssl/x509.h>
52*7c478bd9Sstevel@tonic-gate #include <openssl/ssl.h>
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate #include <openssl/pkcs12.h>
55*7c478bd9Sstevel@tonic-gate #include <p12access.h>
56*7c478bd9Sstevel@tonic-gate #include <p12err.h>
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate static PKCS12 *p12_read_file(char *);
59*7c478bd9Sstevel@tonic-gate static int p12_doparse(PKCS12 *, char *, int, EVP_PKEY **,
60*7c478bd9Sstevel@tonic-gate     X509 **, STACK_OF(X509) **);
61*7c478bd9Sstevel@tonic-gate static int checkfile(char *);
62*7c478bd9Sstevel@tonic-gate static int check_password(PKCS12 *, char *);
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /*
65*7c478bd9Sstevel@tonic-gate  * sunw_use_x509cert - pass an x509 client certificate to ssl
66*7c478bd9Sstevel@tonic-gate  *
67*7c478bd9Sstevel@tonic-gate  * Arguments:
68*7c478bd9Sstevel@tonic-gate  *   ctx        - SSL's context structure
69*7c478bd9Sstevel@tonic-gate  *   cert	- Certificate to pass in x509 format
70*7c478bd9Sstevel@tonic-gate  *
71*7c478bd9Sstevel@tonic-gate  * Returns:
72*7c478bd9Sstevel@tonic-gate  *   <=0 	- Error occurred.  Check the error stack for specifics.
73*7c478bd9Sstevel@tonic-gate  *   >0         - Success.  Cert was successfully added.
74*7c478bd9Sstevel@tonic-gate  */
75*7c478bd9Sstevel@tonic-gate static int
76*7c478bd9Sstevel@tonic-gate sunw_use_x509cert(SSL_CTX *ctx, X509 *cert)
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate 	ERR_clear_error();
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	if (ctx == NULL || cert == NULL) {
81*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_USE_X509CERT, SUNW_R_INVALID_ARG);
82*7c478bd9Sstevel@tonic-gate 		return (-1);
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	if (SSL_CTX_use_certificate(ctx, cert) != 1) {
86*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_USE_X509CERT, SUNW_R_CERT_ERR);
87*7c478bd9Sstevel@tonic-gate 		return (-1);
88*7c478bd9Sstevel@tonic-gate 	}
89*7c478bd9Sstevel@tonic-gate 	return (1);
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate /*
93*7c478bd9Sstevel@tonic-gate  * sunw_use_pkey - pass an EVP_PKEY private key to ssl
94*7c478bd9Sstevel@tonic-gate  *
95*7c478bd9Sstevel@tonic-gate  * Arguments:
96*7c478bd9Sstevel@tonic-gate  *   ctx        - SSL's context structure
97*7c478bd9Sstevel@tonic-gate  *   pkey	- EVP_PKEY formatted private key
98*7c478bd9Sstevel@tonic-gate  *
99*7c478bd9Sstevel@tonic-gate  * Returns:
100*7c478bd9Sstevel@tonic-gate  *   <=0 	- Error occurred.  Check the error stack for specifics.
101*7c478bd9Sstevel@tonic-gate  *   >0         - Success.
102*7c478bd9Sstevel@tonic-gate  */
103*7c478bd9Sstevel@tonic-gate static int
104*7c478bd9Sstevel@tonic-gate sunw_use_pkey(SSL_CTX *ctx, EVP_PKEY *pkey)
105*7c478bd9Sstevel@tonic-gate {
106*7c478bd9Sstevel@tonic-gate 	ERR_clear_error();
107*7c478bd9Sstevel@tonic-gate 	if (ctx == NULL || pkey == NULL) {
108*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_USE_PKEY, SUNW_R_INVALID_ARG);
109*7c478bd9Sstevel@tonic-gate 		return (-1);
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	if (SSL_CTX_use_PrivateKey(ctx, pkey) != 1) {
113*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_USE_PKEY, SUNW_R_PKEY_ERR);
114*7c478bd9Sstevel@tonic-gate 		return (-1);
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	return (1);
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate /*
121*7c478bd9Sstevel@tonic-gate  * sunw_use_tastore - take a stack of X509 certs and add them to the
122*7c478bd9Sstevel@tonic-gate  *              SSL store of trust anchors (aka CA certs).
123*7c478bd9Sstevel@tonic-gate  *
124*7c478bd9Sstevel@tonic-gate  * This function takes the certs in the stack and passes them into
125*7c478bd9Sstevel@tonic-gate  * SSL for addition to the cache of TA certs.
126*7c478bd9Sstevel@tonic-gate  *
127*7c478bd9Sstevel@tonic-gate  * Arguments:
128*7c478bd9Sstevel@tonic-gate  *   ctx        - SSL's context structure
129*7c478bd9Sstevel@tonic-gate  *   ta_certs   - Stack of certs to add to the list of SSL trust anchors.
130*7c478bd9Sstevel@tonic-gate  *
131*7c478bd9Sstevel@tonic-gate  * Returns:
132*7c478bd9Sstevel@tonic-gate  *   <=0 	- Error occurred.  Check the error stack for specifics.
133*7c478bd9Sstevel@tonic-gate  *   >0         - Success.  Certs were successfully added.
134*7c478bd9Sstevel@tonic-gate  */
135*7c478bd9Sstevel@tonic-gate static int
136*7c478bd9Sstevel@tonic-gate sunw_use_tastore(SSL_CTX *ctx, STACK_OF(X509) *ta_certs)
137*7c478bd9Sstevel@tonic-gate {
138*7c478bd9Sstevel@tonic-gate 	X509 *tmp;
139*7c478bd9Sstevel@tonic-gate 	int ret = -1;
140*7c478bd9Sstevel@tonic-gate 	int i;
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	ERR_clear_error();
143*7c478bd9Sstevel@tonic-gate 	if (ctx == NULL || ctx->cert_store == NULL || ta_certs == NULL) {
144*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_USE_TASTORE, SUNW_R_INVALID_ARG);
145*7c478bd9Sstevel@tonic-gate 		return (-1);
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	if (sk_X509_num(ta_certs) == 0) {
149*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_USE_TASTORE, SUNW_R_NO_TRUST_ANCHOR);
150*7c478bd9Sstevel@tonic-gate 		return (-1);
151*7c478bd9Sstevel@tonic-gate 	}
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sk_X509_num(ta_certs); i++) {
154*7c478bd9Sstevel@tonic-gate 		tmp = sk_X509_value(ta_certs, i);
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 		ret = X509_STORE_add_cert(ctx->cert_store, tmp);
157*7c478bd9Sstevel@tonic-gate 		if (ret == 0) {
158*7c478bd9Sstevel@tonic-gate 			if (ERR_GET_REASON(ERR_peek_error()) ==
159*7c478bd9Sstevel@tonic-gate 					X509_R_CERT_ALREADY_IN_HASH_TABLE) {
160*7c478bd9Sstevel@tonic-gate 				ERR_clear_error();
161*7c478bd9Sstevel@tonic-gate 				continue;
162*7c478bd9Sstevel@tonic-gate 			}
163*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_USE_TASTORE, SUNW_R_ADD_TRUST_ERR);
164*7c478bd9Sstevel@tonic-gate 			return (-1);
165*7c478bd9Sstevel@tonic-gate 		} else if (ret < 0) {
166*7c478bd9Sstevel@tonic-gate 			break;
167*7c478bd9Sstevel@tonic-gate 		}
168*7c478bd9Sstevel@tonic-gate 	}
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	if (ret < 0) {
171*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_USE_TASTORE, SUNW_R_ADD_TRUST_ERR);
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	return (ret);
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate /*
178*7c478bd9Sstevel@tonic-gate  * sunw_p12_use_certfile - read a client certificate from a pkcs12 file and
179*7c478bd9Sstevel@tonic-gate  *              pass it in to SSL.
180*7c478bd9Sstevel@tonic-gate  *
181*7c478bd9Sstevel@tonic-gate  * Read in the certificate in pkcs12-formated file.  Use the provided
182*7c478bd9Sstevel@tonic-gate  * passphrase to decrypt it. Pass the cert to SSL.
183*7c478bd9Sstevel@tonic-gate  *
184*7c478bd9Sstevel@tonic-gate  * Arguments:
185*7c478bd9Sstevel@tonic-gate  *   ctx        - SSL's context structure
186*7c478bd9Sstevel@tonic-gate  *   filename	- Name of file with the client certificate.
187*7c478bd9Sstevel@tonic-gate  *   passwd     - Passphrase for pkcs12 data.
188*7c478bd9Sstevel@tonic-gate  *
189*7c478bd9Sstevel@tonic-gate  * Returns:
190*7c478bd9Sstevel@tonic-gate  *   <=0 	- Error occurred.  Check the error stack for specifics.
191*7c478bd9Sstevel@tonic-gate  *   >0         - Success.  Cert was successfully added.
192*7c478bd9Sstevel@tonic-gate  */
193*7c478bd9Sstevel@tonic-gate int
194*7c478bd9Sstevel@tonic-gate sunw_p12_use_certfile(SSL_CTX *ctx, char *filename, char *passwd)
195*7c478bd9Sstevel@tonic-gate {
196*7c478bd9Sstevel@tonic-gate 	PKCS12 *p12 = NULL;
197*7c478bd9Sstevel@tonic-gate 	X509 *cert = NULL;
198*7c478bd9Sstevel@tonic-gate 	int ret = -1;
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	ERR_clear_error();
201*7c478bd9Sstevel@tonic-gate 	if (ctx == NULL || filename == NULL) {
202*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_USE_CERTFILE, SUNW_R_INVALID_ARG);
203*7c478bd9Sstevel@tonic-gate 		return (-1);
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	p12 = p12_read_file(filename);
207*7c478bd9Sstevel@tonic-gate 	if (p12 != NULL) {
208*7c478bd9Sstevel@tonic-gate 		ret = p12_doparse(p12, passwd, DO_UNMATCHING, NULL,
209*7c478bd9Sstevel@tonic-gate 		    &cert, NULL);
210*7c478bd9Sstevel@tonic-gate 		if (ret > 0 && cert != NULL) {
211*7c478bd9Sstevel@tonic-gate 			if (sunw_use_x509cert(ctx, cert) == -1) {
212*7c478bd9Sstevel@tonic-gate 				/*
213*7c478bd9Sstevel@tonic-gate 				 * Error already on stack
214*7c478bd9Sstevel@tonic-gate 				 */
215*7c478bd9Sstevel@tonic-gate 				ret = -1;
216*7c478bd9Sstevel@tonic-gate 			}
217*7c478bd9Sstevel@tonic-gate 		}
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	if (p12 != NULL)
221*7c478bd9Sstevel@tonic-gate 		PKCS12_free(p12);
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	if (ret == -1 && cert != NULL) {
224*7c478bd9Sstevel@tonic-gate 		X509_free(cert);
225*7c478bd9Sstevel@tonic-gate 		cert = NULL;
226*7c478bd9Sstevel@tonic-gate 	}
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	return (ret);
229*7c478bd9Sstevel@tonic-gate }
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate /*
232*7c478bd9Sstevel@tonic-gate  * sunw_p12_use_keyfile - read a RSA private key from a pkcs12 file and pass
233*7c478bd9Sstevel@tonic-gate  *              it in to SSL.
234*7c478bd9Sstevel@tonic-gate  *
235*7c478bd9Sstevel@tonic-gate  * Read in the RSA private key in pkcs12 format. Use the provided
236*7c478bd9Sstevel@tonic-gate  * passphrase to decrypt it. Pass the cert to SSL.
237*7c478bd9Sstevel@tonic-gate  *
238*7c478bd9Sstevel@tonic-gate  * Arguments:
239*7c478bd9Sstevel@tonic-gate  *   ctx        - SSL's context structure
240*7c478bd9Sstevel@tonic-gate  *   filename	- Name of file with private key.
241*7c478bd9Sstevel@tonic-gate  *   passwd     - Passphrase for pkcs12 data.
242*7c478bd9Sstevel@tonic-gate  *
243*7c478bd9Sstevel@tonic-gate  * Returns:
244*7c478bd9Sstevel@tonic-gate  *   <=0 	- Error occurred.  Check the error stack for specifics.
245*7c478bd9Sstevel@tonic-gate  *   >0         - Success.  Key was successfully added.
246*7c478bd9Sstevel@tonic-gate  */
247*7c478bd9Sstevel@tonic-gate int
248*7c478bd9Sstevel@tonic-gate sunw_p12_use_keyfile(SSL_CTX *ctx, char *filename, char *passwd)
249*7c478bd9Sstevel@tonic-gate {
250*7c478bd9Sstevel@tonic-gate 	EVP_PKEY *pkey = NULL;
251*7c478bd9Sstevel@tonic-gate 	PKCS12 *p12 = NULL;
252*7c478bd9Sstevel@tonic-gate 	int ret = -1;
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	ERR_clear_error();
255*7c478bd9Sstevel@tonic-gate 	if (ctx == NULL || filename == NULL) {
256*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_USE_KEYFILE, SUNW_R_INVALID_ARG);
257*7c478bd9Sstevel@tonic-gate 		return (-1);
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	p12 = p12_read_file(filename);
261*7c478bd9Sstevel@tonic-gate 	if (p12 != NULL) {
262*7c478bd9Sstevel@tonic-gate 		ret = p12_doparse(p12, passwd, DO_UNMATCHING, &pkey, NULL,
263*7c478bd9Sstevel@tonic-gate 		    NULL);
264*7c478bd9Sstevel@tonic-gate 		if (ret > 0 && pkey != NULL) {
265*7c478bd9Sstevel@tonic-gate 			if (sunw_use_pkey(ctx, pkey) != 1) {
266*7c478bd9Sstevel@tonic-gate 				/*
267*7c478bd9Sstevel@tonic-gate 				 * Error already on stack
268*7c478bd9Sstevel@tonic-gate 				 */
269*7c478bd9Sstevel@tonic-gate 				ret = -1;
270*7c478bd9Sstevel@tonic-gate 			}
271*7c478bd9Sstevel@tonic-gate 		} else {
272*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_USE_KEYFILE, SUNW_R_BAD_PKEY);
273*7c478bd9Sstevel@tonic-gate 		}
274*7c478bd9Sstevel@tonic-gate 	} else {
275*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_USE_KEYFILE, SUNW_R_PKEY_READ_ERR);
276*7c478bd9Sstevel@tonic-gate 	}
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 	if (p12 != NULL)
279*7c478bd9Sstevel@tonic-gate 		PKCS12_free(p12);
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	if (ret == -1 && pkey != NULL) {
282*7c478bd9Sstevel@tonic-gate 		sunw_evp_pkey_free(pkey);
283*7c478bd9Sstevel@tonic-gate 		pkey = NULL;
284*7c478bd9Sstevel@tonic-gate 	}
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 	return (ret);
287*7c478bd9Sstevel@tonic-gate }
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate /*
290*7c478bd9Sstevel@tonic-gate  * sunw_p12_use_trustfile - read a list of trustanchors from a pkcs12 file and
291*7c478bd9Sstevel@tonic-gate  *              pass the stack in to SSL.
292*7c478bd9Sstevel@tonic-gate  *
293*7c478bd9Sstevel@tonic-gate  * Read in the trust anchors from pkcs12-formated file. Use the provided
294*7c478bd9Sstevel@tonic-gate  * passphrase to decrypt it. Pass the cert to SSL.
295*7c478bd9Sstevel@tonic-gate  *
296*7c478bd9Sstevel@tonic-gate  * Arguments:
297*7c478bd9Sstevel@tonic-gate  *   ctx        - SSL's context structure
298*7c478bd9Sstevel@tonic-gate  *   filename	- Name of file with the certificates.
299*7c478bd9Sstevel@tonic-gate  *   passwd     - Passphrase for pkcs12 data.
300*7c478bd9Sstevel@tonic-gate  *
301*7c478bd9Sstevel@tonic-gate  * Returns:
302*7c478bd9Sstevel@tonic-gate  *   <=0 	- Error occurred.  Check the error stack for specifics.
303*7c478bd9Sstevel@tonic-gate  *   >0         - Success.  Trust anchors were successfully added.
304*7c478bd9Sstevel@tonic-gate  */
305*7c478bd9Sstevel@tonic-gate int
306*7c478bd9Sstevel@tonic-gate sunw_p12_use_trustfile(SSL_CTX *ctx, char *filename, char *passwd)
307*7c478bd9Sstevel@tonic-gate {
308*7c478bd9Sstevel@tonic-gate 	PKCS12 *p12 = NULL;
309*7c478bd9Sstevel@tonic-gate 	STACK_OF(X509) *ta_sk = NULL;
310*7c478bd9Sstevel@tonic-gate 	int ret = -1;
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 	ERR_clear_error();
313*7c478bd9Sstevel@tonic-gate 	if (ctx == NULL || filename == NULL) {
314*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_USE_TRUSTFILE, SUNW_R_INVALID_ARG);
315*7c478bd9Sstevel@tonic-gate 		return (-1);
316*7c478bd9Sstevel@tonic-gate 	}
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	p12 = p12_read_file(filename);
319*7c478bd9Sstevel@tonic-gate 	if (p12 != NULL) {
320*7c478bd9Sstevel@tonic-gate 		ret = p12_doparse(p12, passwd, DO_NONE, NULL, NULL,
321*7c478bd9Sstevel@tonic-gate 		    &ta_sk);
322*7c478bd9Sstevel@tonic-gate 		if (ret > 0 && ta_sk != NULL)
323*7c478bd9Sstevel@tonic-gate 			ret = sunw_use_tastore(ctx, ta_sk);
324*7c478bd9Sstevel@tonic-gate 		else {
325*7c478bd9Sstevel@tonic-gate 			SUNWerr(SUNW_F_USE_TRUSTFILE, SUNW_R_BAD_TRUST);
326*7c478bd9Sstevel@tonic-gate 			ret = -1;
327*7c478bd9Sstevel@tonic-gate 		}
328*7c478bd9Sstevel@tonic-gate 	} else {
329*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_USE_TRUSTFILE, SUNW_R_READ_TRUST_ERR);
330*7c478bd9Sstevel@tonic-gate 	}
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 	if (p12 != NULL)
333*7c478bd9Sstevel@tonic-gate 		PKCS12_free(p12);
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	if (ta_sk != NULL)
336*7c478bd9Sstevel@tonic-gate 		sk_X509_pop_free(ta_sk, X509_free);
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	return (ret);
339*7c478bd9Sstevel@tonic-gate }
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate /*
342*7c478bd9Sstevel@tonic-gate  * p12_read_file - read a pkcs12 file and get its contents.  Return the
343*7c478bd9Sstevel@tonic-gate  *                 pkcs12 structures.
344*7c478bd9Sstevel@tonic-gate  *
345*7c478bd9Sstevel@tonic-gate  * Arguments:
346*7c478bd9Sstevel@tonic-gate  *   filename	- Name of file with the client certificate.
347*7c478bd9Sstevel@tonic-gate  *
348*7c478bd9Sstevel@tonic-gate  *
349*7c478bd9Sstevel@tonic-gate  * Returns:
350*7c478bd9Sstevel@tonic-gate  *   NULL 	- Error occurred.  Check the error stack for specifics.
351*7c478bd9Sstevel@tonic-gate  *   != NULL	- Success.  The return value is the address of a pkcs12
352*7c478bd9Sstevel@tonic-gate  *                structure.
353*7c478bd9Sstevel@tonic-gate  */
354*7c478bd9Sstevel@tonic-gate static PKCS12 *
355*7c478bd9Sstevel@tonic-gate p12_read_file(char *filename)
356*7c478bd9Sstevel@tonic-gate {
357*7c478bd9Sstevel@tonic-gate 	PKCS12 *p12 = NULL;
358*7c478bd9Sstevel@tonic-gate 	FILE *fp = NULL;
359*7c478bd9Sstevel@tonic-gate 	int ret = 0;
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 	ERR_clear_error();
362*7c478bd9Sstevel@tonic-gate 	if (checkfile(filename) == -1) {
363*7c478bd9Sstevel@tonic-gate 		/*
364*7c478bd9Sstevel@tonic-gate 		 * Error already on stack
365*7c478bd9Sstevel@tonic-gate 		 */
366*7c478bd9Sstevel@tonic-gate 		return (NULL);
367*7c478bd9Sstevel@tonic-gate 	}
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 	if ((fp = fopen(filename, "r")) == 0) {
370*7c478bd9Sstevel@tonic-gate 		SYSerr(SYS_F_FOPEN, errno);
371*7c478bd9Sstevel@tonic-gate 		return (NULL);
372*7c478bd9Sstevel@tonic-gate 	}
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	p12 = d2i_PKCS12_fp(fp, NULL);
375*7c478bd9Sstevel@tonic-gate 	if (p12 == NULL) {
376*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_READ_FILE, SUNW_R_READ_ERR);
377*7c478bd9Sstevel@tonic-gate 		ret = -1;
378*7c478bd9Sstevel@tonic-gate 	}
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate 	if (fp != NULL)
381*7c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 	if (ret == -1 && p12 != NULL) {
384*7c478bd9Sstevel@tonic-gate 		PKCS12_free(p12);
385*7c478bd9Sstevel@tonic-gate 		p12 = NULL;
386*7c478bd9Sstevel@tonic-gate 	}
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	return (p12);
389*7c478bd9Sstevel@tonic-gate }
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate /*
392*7c478bd9Sstevel@tonic-gate  * p12_doparse - Given a pkcs12 structure, check the passphrase and then
393*7c478bd9Sstevel@tonic-gate  *               parse it.
394*7c478bd9Sstevel@tonic-gate  *
395*7c478bd9Sstevel@tonic-gate  * Arguments:
396*7c478bd9Sstevel@tonic-gate  *   p12	- Structure with pkcs12 data which has been read in
397*7c478bd9Sstevel@tonic-gate  *   passwd     - Passphrase for pkcs12 data & key.
398*7c478bd9Sstevel@tonic-gate  *   matchty    - How to decide which matching entry to take... See the
399*7c478bd9Sstevel@tonic-gate  *                DO_* definitions for valid values.
400*7c478bd9Sstevel@tonic-gate  *   pkey       - Points at pointer to private key structure.
401*7c478bd9Sstevel@tonic-gate  *   cert       - Points at pointer to client certificate structure
402*7c478bd9Sstevel@tonic-gate  *   ca         - Points at pointer to list of CA certs
403*7c478bd9Sstevel@tonic-gate  *
404*7c478bd9Sstevel@tonic-gate  * Returns:
405*7c478bd9Sstevel@tonic-gate  *   <=0 	- Error occurred.  Check the error stack for specifics.
406*7c478bd9Sstevel@tonic-gate  *   >0         - Success.  Bits set reflect the kind of information
407*7c478bd9Sstevel@tonic-gate  *                returned.  (See the FOUND_* definitions.)
408*7c478bd9Sstevel@tonic-gate  */
409*7c478bd9Sstevel@tonic-gate static int
410*7c478bd9Sstevel@tonic-gate p12_doparse(PKCS12 *p12, char *passwd, int matchty,
411*7c478bd9Sstevel@tonic-gate     EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca)
412*7c478bd9Sstevel@tonic-gate {
413*7c478bd9Sstevel@tonic-gate 	int ret = 0;
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	ERR_clear_error();
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 	/*
418*7c478bd9Sstevel@tonic-gate 	 * Check passphrase (including null one).
419*7c478bd9Sstevel@tonic-gate 	 */
420*7c478bd9Sstevel@tonic-gate 	if (check_password(p12, passwd) == 0)  {
421*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_DOPARSE, SUNW_R_MAC_VERIFY_FAILURE);
422*7c478bd9Sstevel@tonic-gate 		return (-1);
423*7c478bd9Sstevel@tonic-gate 	}
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate 	ret = sunw_PKCS12_parse(p12, passwd, matchty, NULL, 0, NULL,
426*7c478bd9Sstevel@tonic-gate 	    pkey, cert, ca);
427*7c478bd9Sstevel@tonic-gate 	if (ret <= 0) {
428*7c478bd9Sstevel@tonic-gate 		/*
429*7c478bd9Sstevel@tonic-gate 		 * Error already on stack
430*7c478bd9Sstevel@tonic-gate 		 */
431*7c478bd9Sstevel@tonic-gate 		return (-1);
432*7c478bd9Sstevel@tonic-gate 	}
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 	return (ret);
435*7c478bd9Sstevel@tonic-gate }
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate /*
438*7c478bd9Sstevel@tonic-gate  * checkfile - given a file name, verify that the file exists and is
439*7c478bd9Sstevel@tonic-gate  *             readable.
440*7c478bd9Sstevel@tonic-gate  */
441*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
442*7c478bd9Sstevel@tonic-gate static int
443*7c478bd9Sstevel@tonic-gate checkfile(char *filename)
444*7c478bd9Sstevel@tonic-gate {
445*7c478bd9Sstevel@tonic-gate #ifndef _BOOT
446*7c478bd9Sstevel@tonic-gate 	struct stat sbuf;
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 	if (access(filename, R_OK) == -1 || stat(filename, &sbuf) == -1) {
449*7c478bd9Sstevel@tonic-gate 		SYSerr(SYS_F_FOPEN, errno);
450*7c478bd9Sstevel@tonic-gate 		return (-1);
451*7c478bd9Sstevel@tonic-gate 	}
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate 	if (!S_ISREG(sbuf.st_mode)) {
454*7c478bd9Sstevel@tonic-gate 		SUNWerr(SUNW_F_CHECKFILE, SUNW_R_BAD_FILETYPE);
455*7c478bd9Sstevel@tonic-gate 		return (-1);
456*7c478bd9Sstevel@tonic-gate 	}
457*7c478bd9Sstevel@tonic-gate #endif
458*7c478bd9Sstevel@tonic-gate 	return (0);
459*7c478bd9Sstevel@tonic-gate }
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate /*
462*7c478bd9Sstevel@tonic-gate  * check_password - do various password checks to see if the current password
463*7c478bd9Sstevel@tonic-gate  *                  will work or we need to prompt for a new one.
464*7c478bd9Sstevel@tonic-gate  *
465*7c478bd9Sstevel@tonic-gate  * Arguments:
466*7c478bd9Sstevel@tonic-gate  *   pass   - password to check
467*7c478bd9Sstevel@tonic-gate  *
468*7c478bd9Sstevel@tonic-gate  * Returns:
469*7c478bd9Sstevel@tonic-gate  *   1      - Password is OK.
470*7c478bd9Sstevel@tonic-gate  *   0      - Password not valid.  Error stack was set - use ERR_get_error() to
471*7c478bd9Sstevel@tonic-gate  *            to get the error.
472*7c478bd9Sstevel@tonic-gate  */
473*7c478bd9Sstevel@tonic-gate static int
474*7c478bd9Sstevel@tonic-gate check_password(PKCS12 *p12, char *pass)
475*7c478bd9Sstevel@tonic-gate {
476*7c478bd9Sstevel@tonic-gate 	int ret = 1;
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 	/*
479*7c478bd9Sstevel@tonic-gate 	 * If password is zero length or NULL then try verifying both cases
480*7c478bd9Sstevel@tonic-gate 	 * to determine which password is correct. The reason for this is that
481*7c478bd9Sstevel@tonic-gate 	 * under PKCS#12 password based encryption no password and a zero
482*7c478bd9Sstevel@tonic-gate 	 * length password are two different things.  Otherwise, calling
483*7c478bd9Sstevel@tonic-gate 	 * PKCS12_verify_mac() with a length of -1 means that the length
484*7c478bd9Sstevel@tonic-gate 	 * can be determined via strlen().
485*7c478bd9Sstevel@tonic-gate 	 */
486*7c478bd9Sstevel@tonic-gate 	/* Check the mac */
487*7c478bd9Sstevel@tonic-gate 	if (pass == NULL || *pass == '\0') {
488*7c478bd9Sstevel@tonic-gate 		if (PKCS12_verify_mac(p12, NULL, 0) == 0 &&
489*7c478bd9Sstevel@tonic-gate 		    PKCS12_verify_mac(p12, "", 0) == 0)
490*7c478bd9Sstevel@tonic-gate 			ret = 0;
491*7c478bd9Sstevel@tonic-gate 	} else if (PKCS12_verify_mac(p12, pass, -1) == 0) {
492*7c478bd9Sstevel@tonic-gate 		ret = 0;
493*7c478bd9Sstevel@tonic-gate 	}
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate 	return (ret);
496*7c478bd9Sstevel@tonic-gate }
497