xref: /titanic_50/usr/src/cmd/cmd-inet/usr.sbin/kssl/kssladm/ksslutil.c (revision 5b3e1433c6213363bcb6387e66fc84ee9ff21a5d)
171593db2Swyllys /*
271593db2Swyllys  * CDDL HEADER START
371593db2Swyllys  *
471593db2Swyllys  * The contents of this file are subject to the terms of the
571593db2Swyllys  * Common Development and Distribution License (the "License").
671593db2Swyllys  * You may not use this file except in compliance with the License.
771593db2Swyllys  *
871593db2Swyllys  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
971593db2Swyllys  * or http://www.opensolaris.org/os/licensing.
1071593db2Swyllys  * See the License for the specific language governing permissions
1171593db2Swyllys  * and limitations under the License.
1271593db2Swyllys  *
1371593db2Swyllys  * When distributing Covered Code, include this CDDL HEADER in each
1471593db2Swyllys  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1571593db2Swyllys  * If applicable, add the following below this CDDL HEADER, with the
1671593db2Swyllys  * fields enclosed by brackets "[]" replaced with your own identifying
1771593db2Swyllys  * information: Portions Copyright [yyyy] [name of copyright owner]
1871593db2Swyllys  *
1971593db2Swyllys  * CDDL HEADER END
2071593db2Swyllys  */
2171593db2Swyllys 
2271593db2Swyllys /*
2371593db2Swyllys  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
2471593db2Swyllys  * Use is subject to license terms.
2571593db2Swyllys  */
2671593db2Swyllys 
2771593db2Swyllys #pragma ident	"%Z%%M%	%I%	%E% SMI"
2871593db2Swyllys 
2971593db2Swyllys #include <stdio.h>
3071593db2Swyllys #include <assert.h>
3171593db2Swyllys #include <strings.h>
3271593db2Swyllys 
3371593db2Swyllys #include <kmfapi.h>
3471593db2Swyllys #include "kssladm.h"
3571593db2Swyllys 
3671593db2Swyllys /*
3771593db2Swyllys  * Extract the Certificate and raw key data from a PKCS#12 file.
3871593db2Swyllys  * The password needed for decrypting the PKCS#12 PDU is stored
3971593db2Swyllys  * in plaintext in the given "password_file" parameter.
4071593db2Swyllys  */
4171593db2Swyllys int
PKCS12_get_rsa_key_certs(KMF_HANDLE_T kmfh,const char * filename,const char * password_file,KMF_RAW_KEY_DATA ** rsa,KMF_X509_DER_CERT ** certs)42*5b3e1433Swyllys PKCS12_get_rsa_key_certs(KMF_HANDLE_T kmfh,
43*5b3e1433Swyllys     const char *filename, const char *password_file,
44*5b3e1433Swyllys     KMF_RAW_KEY_DATA **rsa, KMF_X509_DER_CERT **certs)
4571593db2Swyllys {
4671593db2Swyllys 	char password_buf[1024];
4771593db2Swyllys 	KMF_RETURN rv = KMF_OK;
4871593db2Swyllys 	KMF_CREDENTIAL pk12cred;
49*5b3e1433Swyllys 	KMF_X509_DER_CERT *tcerts;
5071593db2Swyllys 	KMF_RAW_KEY_DATA *keys;
5171593db2Swyllys 	int ncerts, nkeys;
5271593db2Swyllys 	char *err = NULL;
5371593db2Swyllys 
5471593db2Swyllys 	tcerts = NULL;
5571593db2Swyllys 	keys = NULL;
5671593db2Swyllys 	ncerts = 0;
5771593db2Swyllys 	nkeys = 0;
5871593db2Swyllys 
5971593db2Swyllys 	if (get_passphrase(password_file, password_buf,
6071593db2Swyllys 	    sizeof (password_buf)) <= 0) {
6171593db2Swyllys 		perror("Unable to read passphrase");
6271593db2Swyllys 		goto done;
6371593db2Swyllys 	}
6471593db2Swyllys 	pk12cred.cred = password_buf;
6571593db2Swyllys 	pk12cred.credlen = strlen(password_buf);
6671593db2Swyllys 
6730a5e8faSwyllys 	rv = kmf_import_objects(kmfh, (char *)filename, &pk12cred, &tcerts,
6830a5e8faSwyllys 	    &ncerts, &keys, &nkeys);
6971593db2Swyllys 	if (rv != KMF_OK) {
7071593db2Swyllys 		REPORT_KMF_ERROR(rv, "Error importing PKCS12 data", err);
7171593db2Swyllys 	}
7271593db2Swyllys 
7371593db2Swyllys done:
7471593db2Swyllys 	if (rv != KMF_OK) {
7571593db2Swyllys 		int i;
7671593db2Swyllys 		if (tcerts != NULL) {
7771593db2Swyllys 			for (i = 0; i < ncerts; i++)
78*5b3e1433Swyllys 				kmf_free_kmf_cert(kmfh, &tcerts[i]);
7971593db2Swyllys 			free(tcerts);
8071593db2Swyllys 		}
8171593db2Swyllys 		tcerts = NULL;
8271593db2Swyllys 		ncerts = 0;
8371593db2Swyllys 		if (keys != NULL) {
8471593db2Swyllys 			for (i = 0; i < nkeys; i++)
8530a5e8faSwyllys 				kmf_free_raw_key(&keys[i]);
8671593db2Swyllys 			free(keys);
8771593db2Swyllys 		}
8871593db2Swyllys 		keys = NULL;
8971593db2Swyllys 	}
9071593db2Swyllys 	*certs = tcerts;
9171593db2Swyllys 	*rsa = keys;
9271593db2Swyllys 
9371593db2Swyllys 	return (ncerts);
9471593db2Swyllys }
9571593db2Swyllys 
9671593db2Swyllys /*
9771593db2Swyllys  * Parse a PEM file which should contain RSA private keys and
9871593db2Swyllys  * their associated X.509v3 certificates.  More than 1 may
9971593db2Swyllys  * be present in the file.
10071593db2Swyllys  */
10171593db2Swyllys int
PEM_get_rsa_key_certs(KMF_HANDLE_T kmfh,const char * filename,char * password_file,KMF_RAW_KEY_DATA ** rsa,KMF_X509_DER_CERT ** certs)102*5b3e1433Swyllys PEM_get_rsa_key_certs(KMF_HANDLE_T kmfh,
103*5b3e1433Swyllys     const char *filename, char *password_file,
104*5b3e1433Swyllys     KMF_RAW_KEY_DATA **rsa, KMF_X509_DER_CERT **certs)
10571593db2Swyllys {
10671593db2Swyllys 	KMF_RETURN rv = KMF_OK;
10771593db2Swyllys 	KMF_CREDENTIAL creds;
108*5b3e1433Swyllys 	KMF_X509_DER_CERT *tcerts;
10971593db2Swyllys 	KMF_RAW_KEY_DATA *keys;
11071593db2Swyllys 	int ncerts, nkeys;
11171593db2Swyllys 	char *err = NULL;
11271593db2Swyllys 	char password_buf[1024];
11371593db2Swyllys 
11471593db2Swyllys 	tcerts = NULL;
11571593db2Swyllys 	keys = NULL;
11671593db2Swyllys 	ncerts = 0;
11771593db2Swyllys 	nkeys = 0;
11871593db2Swyllys 
11971593db2Swyllys 	if (get_passphrase(password_file, password_buf,
12071593db2Swyllys 	    sizeof (password_buf)) <= 0) {
12171593db2Swyllys 		perror("Unable to read passphrase");
12271593db2Swyllys 		goto done;
12371593db2Swyllys 	}
12471593db2Swyllys 	creds.cred = password_buf;
12571593db2Swyllys 	creds.credlen = strlen(password_buf);
12671593db2Swyllys 
12730a5e8faSwyllys 	rv = kmf_import_objects(kmfh, (char *)filename, &creds, &tcerts,
12830a5e8faSwyllys 	    &ncerts, &keys, &nkeys);
12971593db2Swyllys 	if (rv != KMF_OK) {
13071593db2Swyllys 		REPORT_KMF_ERROR(rv, "Error importing key data", err);
13171593db2Swyllys 	}
13271593db2Swyllys 
13371593db2Swyllys done:
13471593db2Swyllys 	if (rv != KMF_OK) {
13571593db2Swyllys 		int i;
13671593db2Swyllys 		if (tcerts != NULL) {
13771593db2Swyllys 			for (i = 0; i < ncerts; i++)
138*5b3e1433Swyllys 				kmf_free_kmf_cert(kmfh, &tcerts[i]);
13971593db2Swyllys 			free(tcerts);
14071593db2Swyllys 		}
14171593db2Swyllys 		tcerts = NULL;
14271593db2Swyllys 		ncerts = 0;
14371593db2Swyllys 		if (keys != NULL) {
14471593db2Swyllys 			for (i = 0; i < nkeys; i++)
14530a5e8faSwyllys 				kmf_free_raw_key(&keys[i]);
14671593db2Swyllys 			free(keys);
14771593db2Swyllys 		}
14871593db2Swyllys 		keys = NULL;
14971593db2Swyllys 	}
15071593db2Swyllys 	if (certs != NULL)
15171593db2Swyllys 		*certs = tcerts;
15271593db2Swyllys 	if (rsa != NULL)
15371593db2Swyllys 		*rsa = keys;
15471593db2Swyllys 
15571593db2Swyllys 	return (ncerts);
15671593db2Swyllys }
157