xref: /onnv-gate/usr/src/common/net/wanboot/p12misc.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * ====================================================================
30Sstevel@tonic-gate  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
60Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
70Sstevel@tonic-gate  * are met:
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
100Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
140Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
150Sstevel@tonic-gate  *    distribution.
160Sstevel@tonic-gate  *
170Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
180Sstevel@tonic-gate  *    software must display the following acknowledgment:
190Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
200Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
210Sstevel@tonic-gate  *
220Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
230Sstevel@tonic-gate  *    endorse or promote products derived from this software without
240Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
250Sstevel@tonic-gate  *    licensing@OpenSSL.org.
260Sstevel@tonic-gate  *
270Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
280Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
290Sstevel@tonic-gate  *    permission of the OpenSSL Project.
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
320Sstevel@tonic-gate  *    acknowledgment:
330Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
340Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
370Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
380Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
390Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
400Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
410Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
420Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
430Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
440Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
450Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
460Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
470Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
480Sstevel@tonic-gate  * ====================================================================
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
510Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
520Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
57*2139Sjp161948  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
580Sstevel@tonic-gate  * Use is subject to license terms.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <stdio.h>
640Sstevel@tonic-gate #include <strings.h>
650Sstevel@tonic-gate #include <stdlib.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include <openssl/crypto.h>
680Sstevel@tonic-gate #include <openssl/err.h>
690Sstevel@tonic-gate #include <openssl/x509.h>
70*2139Sjp161948 #include <openssl/ssl.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include <openssl/pkcs12.h>
730Sstevel@tonic-gate #include <p12aux.h>
740Sstevel@tonic-gate #include <auxutil.h>
750Sstevel@tonic-gate #include <p12err.h>
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * sunw_cryto_init() does crypto-specific initialization.
790Sstevel@tonic-gate  *
800Sstevel@tonic-gate  * Arguments:
810Sstevel@tonic-gate  *   None.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * Returns:
840Sstevel@tonic-gate  *   None.
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate void
sunw_crypto_init(void)870Sstevel@tonic-gate sunw_crypto_init(void)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	OpenSSL_add_all_algorithms();
90*2139Sjp161948 	SSL_load_error_strings();
910Sstevel@tonic-gate 	ERR_load_SUNW_strings();
92*2139Sjp161948 	(void) SSL_library_init();
930Sstevel@tonic-gate }
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * sunw_split_certs() - Given a list of certs and a list of private keys,
970Sstevel@tonic-gate  *     moves certs which match one of the keys to a different stack.
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  * Arguments:
1000Sstevel@tonic-gate  *   allkeys  - Points to a stack of private keys to search.
1010Sstevel@tonic-gate  *   allcerts - Points to a stack of certs to be searched.
1020Sstevel@tonic-gate  *   keycerts - Points to address of a stack of certs with matching private
1030Sstevel@tonic-gate  *              keys.  They are moved from 'allcerts'.  This may not be NULL
1040Sstevel@tonic-gate  *              when called.  If *keycerts is NULL upon entry, a new stack will
1050Sstevel@tonic-gate  *              be allocated.  Otherwise, it must be a valid STACK_OF(509).
1060Sstevel@tonic-gate  *   nocerts  - Points to address of a stack for keys which have no matching
1070Sstevel@tonic-gate  *              certs.  Keys are moved from 'allkeys' here when they have no
1080Sstevel@tonic-gate  *              matching certs.  If this is NULL, matchless keys will be
1090Sstevel@tonic-gate  *              discarded.
1100Sstevel@tonic-gate  *
1110Sstevel@tonic-gate  *   Notes:  If an error occurs while moving certs, the cert being move may be
1120Sstevel@tonic-gate  *   lost.  'keycerts' may only contain part of the matching certs.  The number
1130Sstevel@tonic-gate  *   of certs successfully moved can be found by checking sk_X509_num(keycerts).
1140Sstevel@tonic-gate  *
1150Sstevel@tonic-gate  *   If there is a key which does not have a matching cert, it is moved to
1160Sstevel@tonic-gate  *   the list nocerts.
1170Sstevel@tonic-gate  *
1180Sstevel@tonic-gate  *   If all certs are removed from 'certs' and/or 'pkeys', it will be the
1190Sstevel@tonic-gate  *   caller's responsibility to free the empty stacks.
1200Sstevel@tonic-gate  *
1210Sstevel@tonic-gate  * Returns:
1220Sstevel@tonic-gate  *  <  0 - An error returned.  Call ERR_get_error() to get errors information.
1230Sstevel@tonic-gate  *         Where possible, memory has been freed.
1240Sstevel@tonic-gate  *  >= 0 - The number of certs moved from 'cert' to 'pkcerts'.
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate int
sunw_split_certs(STACK_OF (EVP_PKEY)* allkeys,STACK_OF (X509)* allcerts,STACK_OF (X509)** keycerts,STACK_OF (EVP_PKEY)** nocerts)1270Sstevel@tonic-gate sunw_split_certs(STACK_OF(EVP_PKEY) *allkeys, STACK_OF(X509) *allcerts,
1280Sstevel@tonic-gate     STACK_OF(X509) **keycerts, STACK_OF(EVP_PKEY) **nocerts)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	STACK_OF(X509) *matching;
1310Sstevel@tonic-gate 	STACK_OF(EVP_PKEY) *nomatch;
1320Sstevel@tonic-gate 	EVP_PKEY *tmpkey;
1330Sstevel@tonic-gate 	X509 *tmpcert;
1340Sstevel@tonic-gate 	int count = 0;
1350Sstevel@tonic-gate 	int found;
1360Sstevel@tonic-gate 	int res;
1370Sstevel@tonic-gate 	int i;
1380Sstevel@tonic-gate 	int k;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	*keycerts = NULL;
1410Sstevel@tonic-gate 	if (nocerts != NULL)
1420Sstevel@tonic-gate 		*nocerts = NULL;
1430Sstevel@tonic-gate 	nomatch = NULL;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if ((matching = sk_X509_new_null()) == NULL) {
1460Sstevel@tonic-gate 		SUNWerr(SUNW_F_SPLIT_CERTS, SUNW_R_MEMORY_FAILURE);
1470Sstevel@tonic-gate 		return (-1);
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 	*keycerts = matching;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	k = 0;
1520Sstevel@tonic-gate 	while (k < sk_EVP_PKEY_num(allkeys)) {
1530Sstevel@tonic-gate 		found = 0;
1540Sstevel@tonic-gate 		tmpkey = sk_EVP_PKEY_value(allkeys, k);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 		for (i = 0; i < sk_X509_num(allcerts); i++) {
1570Sstevel@tonic-gate 			tmpcert = sk_X509_value(allcerts, i);
1580Sstevel@tonic-gate 			res = X509_check_private_key(tmpcert, tmpkey);
1590Sstevel@tonic-gate 			if (res != 0) {
1600Sstevel@tonic-gate 				count++;
1610Sstevel@tonic-gate 				found = 1;
1620Sstevel@tonic-gate 				tmpcert = sk_X509_delete(allcerts, i);
1630Sstevel@tonic-gate 				if (sk_X509_push(matching, tmpcert) == 0) {
1640Sstevel@tonic-gate 					X509_free(tmpcert);
1650Sstevel@tonic-gate 					SUNWerr(SUNW_F_SPLIT_CERTS,
1660Sstevel@tonic-gate 					    SUNW_R_MEMORY_FAILURE);
1670Sstevel@tonic-gate 					return (-1);
1680Sstevel@tonic-gate 				}
1690Sstevel@tonic-gate 				break;
1700Sstevel@tonic-gate 			}
1710Sstevel@tonic-gate 		}
1720Sstevel@tonic-gate 		if (found != 0) {
1730Sstevel@tonic-gate 			/*
1740Sstevel@tonic-gate 			 * Found a match - keep the key & check out the next
1750Sstevel@tonic-gate 			 * one.
1760Sstevel@tonic-gate 			 */
1770Sstevel@tonic-gate 			k++;
1780Sstevel@tonic-gate 		} else {
1790Sstevel@tonic-gate 			/*
1800Sstevel@tonic-gate 			 * No cert matching this key.  Move the key if
1810Sstevel@tonic-gate 			 * possible or discard it.  Don't increment the
1820Sstevel@tonic-gate 			 * index.
1830Sstevel@tonic-gate 			 */
1840Sstevel@tonic-gate 			if (nocerts == NULL) {
1850Sstevel@tonic-gate 				tmpkey = sk_EVP_PKEY_delete(allkeys, k);
1860Sstevel@tonic-gate 				sunw_evp_pkey_free(tmpkey);
1870Sstevel@tonic-gate 			} else {
1880Sstevel@tonic-gate 				if (*nocerts == NULL) {
1890Sstevel@tonic-gate 					nomatch = sk_EVP_PKEY_new_null();
1900Sstevel@tonic-gate 					if (nomatch == NULL) {
1910Sstevel@tonic-gate 						SUNWerr(SUNW_F_SPLIT_CERTS,
1920Sstevel@tonic-gate 						    SUNW_R_MEMORY_FAILURE);
1930Sstevel@tonic-gate 						return (-1);
1940Sstevel@tonic-gate 					}
1950Sstevel@tonic-gate 					*nocerts = nomatch;
1960Sstevel@tonic-gate 				}
1970Sstevel@tonic-gate 				tmpkey = sk_EVP_PKEY_delete(allkeys, k);
1980Sstevel@tonic-gate 				if (sk_EVP_PKEY_push(nomatch, tmpkey) == 0) {
1990Sstevel@tonic-gate 					sunw_evp_pkey_free(tmpkey);
2000Sstevel@tonic-gate 					SUNWerr(SUNW_F_SPLIT_CERTS,
2010Sstevel@tonic-gate 					    SUNW_R_MEMORY_FAILURE);
2020Sstevel@tonic-gate 					return (-1);
2030Sstevel@tonic-gate 				}
2040Sstevel@tonic-gate 			}
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	return (count);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate  * sunw_evp_pkey_free() Given an EVP_PKEY structure, free any attributes
2130Sstevel@tonic-gate  *     that are attached.  Then free the EVP_PKEY itself.
2140Sstevel@tonic-gate  *
2150Sstevel@tonic-gate  *     This is a replacement for EVP_PKEY_free() for the sunw stuff.
2160Sstevel@tonic-gate  *     It should be used in places where EVP_PKEY_free would be used,
2170Sstevel@tonic-gate  *     including calls to sk_EVP_PKEY_pop_free().
2180Sstevel@tonic-gate  *
2190Sstevel@tonic-gate  * Arguments:
2200Sstevel@tonic-gate  *   pkey     - Entry which potentially has attributes to be freed.
2210Sstevel@tonic-gate  *
2220Sstevel@tonic-gate  * Returns:
2230Sstevel@tonic-gate  *   None.
2240Sstevel@tonic-gate  */
2250Sstevel@tonic-gate void
sunw_evp_pkey_free(EVP_PKEY * pkey)2260Sstevel@tonic-gate sunw_evp_pkey_free(EVP_PKEY *pkey)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	if (pkey != NULL) {
2290Sstevel@tonic-gate 		if (pkey->attributes != NULL) {
2300Sstevel@tonic-gate 			sk_X509_ATTRIBUTE_pop_free(pkey->attributes,
2310Sstevel@tonic-gate 			    X509_ATTRIBUTE_free);
2320Sstevel@tonic-gate 			pkey->attributes = NULL;
2330Sstevel@tonic-gate 		}
2340Sstevel@tonic-gate 		EVP_PKEY_free(pkey);
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate /*
2390Sstevel@tonic-gate  * sunw_set_localkeyid() sets the localkeyid in a cert, a private key or
2400Sstevel@tonic-gate  *     both.  Any existing localkeyid will be discarded.
2410Sstevel@tonic-gate  *
2420Sstevel@tonic-gate  * Arguments:
2430Sstevel@tonic-gate  *   keyid_str- A byte string with the localkeyid to set
2440Sstevel@tonic-gate  *   keyid_len- Length of the keyid byte string.
2450Sstevel@tonic-gate  *   pkey     - Points to a private key to set the keyidstr in.
2460Sstevel@tonic-gate  *   cert     - Points to a cert to set the keyidstr in.
2470Sstevel@tonic-gate  *
2480Sstevel@tonic-gate  * Note that setting a keyid into a cert which will not be written out as
2490Sstevel@tonic-gate  * a PKCS12 cert is pointless since it will be lost.
2500Sstevel@tonic-gate  *
2510Sstevel@tonic-gate  * Returns:
2520Sstevel@tonic-gate  *   0        - Success.
2530Sstevel@tonic-gate  *   < 0      - An error occurred.  It was probably an error in allocating
2540Sstevel@tonic-gate  *              memory.  The error will be set in the error stack.  Call
2550Sstevel@tonic-gate  *              ERR_get_error() to get specific information.
2560Sstevel@tonic-gate  */
2570Sstevel@tonic-gate int
sunw_set_localkeyid(const char * keyid_str,int keyid_len,EVP_PKEY * pkey,X509 * cert)2580Sstevel@tonic-gate sunw_set_localkeyid(const char *keyid_str, int keyid_len, EVP_PKEY *pkey,
2590Sstevel@tonic-gate     X509 *cert)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	X509_ATTRIBUTE *attr = NULL;
2620Sstevel@tonic-gate 	ASN1_STRING *str = NULL;
2630Sstevel@tonic-gate 	ASN1_TYPE *keyid = NULL;
2640Sstevel@tonic-gate 	int retval = -1;
2650Sstevel@tonic-gate 	int i;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	if (cert != NULL) {
2680Sstevel@tonic-gate 		if (X509_keyid_set1(cert, (uchar_t *)keyid_str, keyid_len)
2690Sstevel@tonic-gate 		    == 0) {
2700Sstevel@tonic-gate 			SUNWerr(SUNW_F_SET_LOCALKEYID, SUNW_R_SET_LKID_ERR);
2710Sstevel@tonic-gate 			goto cleanup;
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 	if (pkey != NULL) {
2750Sstevel@tonic-gate 		str = (ASN1_STRING *)M_ASN1_OCTET_STRING_new();
2760Sstevel@tonic-gate 		if (str == NULL ||
2770Sstevel@tonic-gate 		    M_ASN1_OCTET_STRING_set(str, keyid_str, keyid_len) == 0 ||
2780Sstevel@tonic-gate 		    (keyid = ASN1_TYPE_new()) == NULL) {
2790Sstevel@tonic-gate 			SUNWerr(SUNW_F_SET_LOCALKEYID, SUNW_R_MEMORY_FAILURE);
2800Sstevel@tonic-gate 			goto cleanup;
2810Sstevel@tonic-gate 		}
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 		ASN1_TYPE_set(keyid, V_ASN1_OCTET_STRING, str);
2840Sstevel@tonic-gate 		str = NULL;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 		attr = type2attrib(keyid, NID_localKeyID);
2870Sstevel@tonic-gate 		if (attr == NULL) {
2880Sstevel@tonic-gate 			/*
2890Sstevel@tonic-gate 			 * Error already on stack
2900Sstevel@tonic-gate 			 */
2910Sstevel@tonic-gate 			goto cleanup;
2920Sstevel@tonic-gate 		}
2930Sstevel@tonic-gate 		keyid = NULL;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 		if (pkey->attributes == NULL) {
2960Sstevel@tonic-gate 			pkey->attributes = sk_X509_ATTRIBUTE_new_null();
2970Sstevel@tonic-gate 			if (pkey->attributes == NULL) {
2980Sstevel@tonic-gate 				SUNWerr(SUNW_F_SET_LOCALKEYID,
2990Sstevel@tonic-gate 				    SUNW_R_MEMORY_FAILURE);
3000Sstevel@tonic-gate 				goto cleanup;
3010Sstevel@tonic-gate 			}
3020Sstevel@tonic-gate 		} else {
3030Sstevel@tonic-gate 			i = find_attr_by_nid(pkey->attributes, NID_localKeyID);
3040Sstevel@tonic-gate 			if (i >= 0)
3050Sstevel@tonic-gate 				sk_X509_ATTRIBUTE_delete(pkey->attributes, i);
3060Sstevel@tonic-gate 		}
3070Sstevel@tonic-gate 		if (sk_X509_ATTRIBUTE_push(pkey->attributes, attr) == 0) {
3080Sstevel@tonic-gate 			SUNWerr(SUNW_F_SET_LOCALKEYID, SUNW_R_MEMORY_FAILURE);
3090Sstevel@tonic-gate 			goto cleanup;
3100Sstevel@tonic-gate 		}
3110Sstevel@tonic-gate 		attr = NULL;
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 	retval = 0;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate cleanup:
3160Sstevel@tonic-gate 	if (str != NULL)
3170Sstevel@tonic-gate 		ASN1_STRING_free(str);
3180Sstevel@tonic-gate 	if (keyid != NULL)
3190Sstevel@tonic-gate 		ASN1_TYPE_free(keyid);
3200Sstevel@tonic-gate 	if (attr != NULL)
3210Sstevel@tonic-gate 		X509_ATTRIBUTE_free(attr);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	return (retval);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate  * sunw_get_pkey_localkeyid() gets the localkeyid from a private key.  It can
3280Sstevel@tonic-gate  *     optionally remove the value found.
3290Sstevel@tonic-gate  *
3300Sstevel@tonic-gate  * Arguments:
3310Sstevel@tonic-gate  *   dowhat   - What to do with the attributes (remove them or copy them).
3320Sstevel@tonic-gate  *   pkey     - Points to a private key to set the keyidstr in.
3330Sstevel@tonic-gate  *   keyid_str- Points to a location which will receive the pointer to
3340Sstevel@tonic-gate  *              a byte string containing the binary localkeyid.  Note that
3350Sstevel@tonic-gate  *              this is a copy, and the caller must free it.
3360Sstevel@tonic-gate  *   keyid_len- Length of keyid_str.
3370Sstevel@tonic-gate  *
3380Sstevel@tonic-gate  * Returns:
3390Sstevel@tonic-gate  *   >= 0     - The number of characters in the keyid returned.
3400Sstevel@tonic-gate  *   < 0      - An error occurred.  It was probably an error in allocating
3410Sstevel@tonic-gate  *              memory.  The error will be set in the error stack.  Call
3420Sstevel@tonic-gate  *              ERR_get_error() to get specific information.
3430Sstevel@tonic-gate  */
3440Sstevel@tonic-gate int
sunw_get_pkey_localkeyid(getdo_actions_t dowhat,EVP_PKEY * pkey,char ** keyid_str,int * keyid_len)3450Sstevel@tonic-gate sunw_get_pkey_localkeyid(getdo_actions_t dowhat, EVP_PKEY *pkey,
3460Sstevel@tonic-gate     char **keyid_str, int *keyid_len)
3470Sstevel@tonic-gate {
3480Sstevel@tonic-gate 	X509_ATTRIBUTE *attr = NULL;
3490Sstevel@tonic-gate 	ASN1_OCTET_STRING *str = NULL;
3500Sstevel@tonic-gate 	ASN1_TYPE *ty = NULL;
3510Sstevel@tonic-gate 	int len = 0;
3520Sstevel@tonic-gate 	int i;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	if (keyid_str != NULL)
3550Sstevel@tonic-gate 		*keyid_str = NULL;
3560Sstevel@tonic-gate 	if (keyid_len != NULL)
3570Sstevel@tonic-gate 		*keyid_len = 0;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if (pkey == NULL || pkey->attributes == NULL) {
3600Sstevel@tonic-gate 		return (0);
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	if ((i = find_attr_by_nid(pkey->attributes, NID_localKeyID)) < 0) {
3640Sstevel@tonic-gate 		return (0);
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 	attr = sk_X509_ATTRIBUTE_value(pkey->attributes, i);
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if ((ty = attrib2type(attr)) == NULL ||
3690Sstevel@tonic-gate 	    ty->type != V_ASN1_OCTET_STRING) {
3700Sstevel@tonic-gate 		return (0);
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	if (dowhat == GETDO_DEL) {
3740Sstevel@tonic-gate 		attr = sk_X509_ATTRIBUTE_delete(pkey->attributes, i);
3750Sstevel@tonic-gate 		if (attr != NULL)
3760Sstevel@tonic-gate 			X509_ATTRIBUTE_free(attr);
3770Sstevel@tonic-gate 		return (0);
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	str = ty->value.octet_string;
3810Sstevel@tonic-gate 	len = str->length;
3820Sstevel@tonic-gate 	if ((*keyid_str = malloc(len)) == NULL) {
3830Sstevel@tonic-gate 		SUNWerr(SUNW_F_GET_LOCALKEYID, SUNW_R_MEMORY_FAILURE);
3840Sstevel@tonic-gate 		return (-1);
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	(void) memcpy(*keyid_str, str->data, len);
3880Sstevel@tonic-gate 	*keyid_len = len;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	return (len);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate /*
3940Sstevel@tonic-gate  * sunw_get_pkey_fname() gets the friendlyName from a private key.  It can
3950Sstevel@tonic-gate  *     optionally remove the value found.
3960Sstevel@tonic-gate  *
3970Sstevel@tonic-gate  * Arguments:
3980Sstevel@tonic-gate  *   dowhat   - What to do with the attributes (remove them or copy them).
3990Sstevel@tonic-gate  *   pkey     - Points to a private key to get the frientlyname from
4000Sstevel@tonic-gate  *   fname    - Points to a location which will receive the pointer to a
4010Sstevel@tonic-gate  *              byte string with the ASCII friendlyname
4020Sstevel@tonic-gate  *
4030Sstevel@tonic-gate  * Returns:
4040Sstevel@tonic-gate  *   >= 0     - The number of characters in the frienlyname returned.
4050Sstevel@tonic-gate  *   < 0      - An error occurred.  It was probably an error in allocating
4060Sstevel@tonic-gate  *              memory.  The error will be set in the error stack.  Call
4070Sstevel@tonic-gate  *              ERR_get_error() to get specific information.
4080Sstevel@tonic-gate  */
4090Sstevel@tonic-gate int
sunw_get_pkey_fname(getdo_actions_t dowhat,EVP_PKEY * pkey,char ** fname)4100Sstevel@tonic-gate sunw_get_pkey_fname(getdo_actions_t dowhat, EVP_PKEY *pkey, char **fname)
4110Sstevel@tonic-gate {
4120Sstevel@tonic-gate 	X509_ATTRIBUTE *attr = NULL;
4130Sstevel@tonic-gate 	ASN1_BMPSTRING *str = NULL;
4140Sstevel@tonic-gate 	ASN1_TYPE *ty = NULL;
4150Sstevel@tonic-gate 	int len = 0;
4160Sstevel@tonic-gate 	int i;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	if (fname != NULL)
4190Sstevel@tonic-gate 		*fname = NULL;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (pkey == NULL || pkey->attributes == NULL) {
4220Sstevel@tonic-gate 		return (0);
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	if ((i = find_attr_by_nid(pkey->attributes, NID_friendlyName)) < 0) {
4260Sstevel@tonic-gate 		return (0);
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 	attr = sk_X509_ATTRIBUTE_value(pkey->attributes, i);
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	if ((ty = attrib2type(attr)) == NULL ||
4310Sstevel@tonic-gate 	    ty->type != V_ASN1_BMPSTRING) {
4320Sstevel@tonic-gate 		return (0);
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if (dowhat == GETDO_DEL) {
4360Sstevel@tonic-gate 		attr = sk_X509_ATTRIBUTE_delete(pkey->attributes, i);
4370Sstevel@tonic-gate 		if (attr != NULL)
4380Sstevel@tonic-gate 			X509_ATTRIBUTE_free(attr);
4390Sstevel@tonic-gate 		return (0);
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	str = ty->value.bmpstring;
4430Sstevel@tonic-gate 	*fname = uni2asc(str->data, str->length);
4440Sstevel@tonic-gate 	if (*fname == NULL) {
4450Sstevel@tonic-gate 		SUNWerr(SUNW_F_GET_PKEY_FNAME, SUNW_R_MEMORY_FAILURE);
4460Sstevel@tonic-gate 		return (-1);
4470Sstevel@tonic-gate 	}
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	len = strlen(*fname);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	return (len);
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate /*
4550Sstevel@tonic-gate  * sunw_find_localkeyid() searches stacks of certs and private keys,
4560Sstevel@tonic-gate  *     and returns  the first matching cert/private key found.
4570Sstevel@tonic-gate  *
4580Sstevel@tonic-gate  * Look for a keyid in a stack of certs.  if 'certs' is NULL and 'pkeys' is
4590Sstevel@tonic-gate  * not NULL, search the list of private keys.  Move the matching cert to
4600Sstevel@tonic-gate  * 'matching_cert' and its matching private key to 'matching_pkey'.  If no
4610Sstevel@tonic-gate  * cert or keys match, no match occurred.
4620Sstevel@tonic-gate  *
4630Sstevel@tonic-gate  * Arguments:
4640Sstevel@tonic-gate  *   keyid_str- A byte string with the localkeyid to match
4650Sstevel@tonic-gate  *   keyid_len- Length of the keyid byte string.
4660Sstevel@tonic-gate  *   pkeys    - Points to a stack of private keys which match the certs.
4670Sstevel@tonic-gate  *              This may be NULL, in which case no keys are returned.
4680Sstevel@tonic-gate  *   certs    - Points to a stack of certs to search.  If NULL, search the
4690Sstevel@tonic-gate  *              stack of keys instead.
4700Sstevel@tonic-gate  *   matching_pkey
4710Sstevel@tonic-gate  *            - Pointer to receive address of first matching pkey found.
4720Sstevel@tonic-gate  *              'matching_pkey' must not be NULL; '*matching_pkey' will be
4730Sstevel@tonic-gate  *              reset.
4740Sstevel@tonic-gate  *   matching_cert
4750Sstevel@tonic-gate  *            - Pointer to receive address of first matching cert found.
4760Sstevel@tonic-gate  *              'matching_cert' must not be NULL; '*matching_cert' will be
4770Sstevel@tonic-gate  *              reset.
4780Sstevel@tonic-gate  *
4790Sstevel@tonic-gate  * Returns:
4800Sstevel@tonic-gate  *  <  0 - An error returned.  Call ERR_get_error() to get errors information.
4810Sstevel@tonic-gate  *         Where possible, memory has been freed.
4820Sstevel@tonic-gate  *  >= 0 - Objects were found and returned.  Which objects are indicated by
4830Sstevel@tonic-gate  *         which bits are set (FOUND_PKEY and/or FOUND_CERT).
4840Sstevel@tonic-gate  */
4850Sstevel@tonic-gate int
sunw_find_localkeyid(char * keyid_str,int len,STACK_OF (EVP_PKEY)* pkeys,STACK_OF (X509)* certs,EVP_PKEY ** matching_pkey,X509 ** matching_cert)4860Sstevel@tonic-gate sunw_find_localkeyid(char *keyid_str, int len, STACK_OF(EVP_PKEY) *pkeys,
4870Sstevel@tonic-gate STACK_OF(X509) *certs, EVP_PKEY **matching_pkey, X509 **matching_cert)
4880Sstevel@tonic-gate {
4890Sstevel@tonic-gate 	ASN1_STRING *cmpstr = NULL;
4900Sstevel@tonic-gate 	EVP_PKEY *tmp_pkey = NULL;
4910Sstevel@tonic-gate 	X509 *tmp_cert = NULL;
4920Sstevel@tonic-gate 	int retval = 0;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	/* If NULL arguments, this is an error */
4950Sstevel@tonic-gate 	if (keyid_str == NULL ||
4960Sstevel@tonic-gate 	    (pkeys == NULL || certs == NULL) ||
4970Sstevel@tonic-gate 	    (pkeys != NULL && matching_pkey == NULL) ||
4980Sstevel@tonic-gate 	    (certs != NULL && matching_cert == NULL)) {
4990Sstevel@tonic-gate 		SUNWerr(SUNW_F_FIND_LOCALKEYID, SUNW_R_INVALID_ARG);
5000Sstevel@tonic-gate 		return (-1);
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	if (matching_pkey != NULL)
5040Sstevel@tonic-gate 		*matching_pkey = NULL;
5050Sstevel@tonic-gate 	if (matching_cert != NULL)
5060Sstevel@tonic-gate 		*matching_cert = NULL;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	cmpstr = (ASN1_STRING *)M_ASN1_OCTET_STRING_new();
5090Sstevel@tonic-gate 	if (cmpstr == NULL ||
5100Sstevel@tonic-gate 	    M_ASN1_OCTET_STRING_set(cmpstr, keyid_str, len) == 0) {
5110Sstevel@tonic-gate 		SUNWerr(SUNW_F_FIND_LOCALKEYID, SUNW_R_MEMORY_FAILURE);
5120Sstevel@tonic-gate 		return (-1);
5130Sstevel@tonic-gate 	}
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	retval = find_attr(NID_localKeyID, cmpstr, pkeys, &tmp_pkey, certs,
5160Sstevel@tonic-gate 	    &tmp_cert);
5170Sstevel@tonic-gate 	if (retval == 0) {
5180Sstevel@tonic-gate 		ASN1_STRING_free(cmpstr);
5190Sstevel@tonic-gate 		return (retval);
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	if (matching_pkey != NULL)
5230Sstevel@tonic-gate 		*matching_pkey = tmp_pkey;
5240Sstevel@tonic-gate 	if (matching_cert != NULL)
5250Sstevel@tonic-gate 		*matching_cert = tmp_cert;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	return (retval);
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate /*
5310Sstevel@tonic-gate  * sunw_find_fname() searches stacks of certs and private keys for one with
5320Sstevel@tonic-gate  *     a matching friendlyname and returns the first matching cert/private
5330Sstevel@tonic-gate  *     key found.
5340Sstevel@tonic-gate  *
5350Sstevel@tonic-gate  * Look for a friendlyname in a stack of certs.  if 'certs' is NULL and 'pkeys'
5360Sstevel@tonic-gate  * is not NULL, search the list of private keys.  Move the matching cert to
5370Sstevel@tonic-gate  * 'matching_cert' and its matching private key to 'matching_pkey'.  If no
5380Sstevel@tonic-gate  * cert or keys match, no match occurred.
5390Sstevel@tonic-gate  *
5400Sstevel@tonic-gate  * Arguments:
5410Sstevel@tonic-gate  *   fname    - Friendlyname to find (NULL-terminated ASCII string).
5420Sstevel@tonic-gate  *   pkeys    - Points to a stack of private keys which match the certs.
5430Sstevel@tonic-gate  *              This may be NULL, in which case no keys are returned.
5440Sstevel@tonic-gate  *   certs    - Points to a stack of certs to search.  If NULL, search the
5450Sstevel@tonic-gate  *              stack of keys instead.
5460Sstevel@tonic-gate  *   matching_pkey
5470Sstevel@tonic-gate  *            - Pointer to receive address of first matching pkey found.
5480Sstevel@tonic-gate  *   matching_cert
5490Sstevel@tonic-gate  *            - Pointer to receive address of first matching cert found.
5500Sstevel@tonic-gate  *
5510Sstevel@tonic-gate  * Returns:
5520Sstevel@tonic-gate  *  <  0 - An error returned.  Call ERR_get_error() to get errors information.
5530Sstevel@tonic-gate  *         Where possible, memory has been freed.
5540Sstevel@tonic-gate  *  >= 0 - Objects were found and returned.  Which objects are indicated by
5550Sstevel@tonic-gate  *         which bits are set (FOUND_PKEY and/or FOUND_CERT).
5560Sstevel@tonic-gate  */
5570Sstevel@tonic-gate int
sunw_find_fname(char * fname,STACK_OF (EVP_PKEY)* pkeys,STACK_OF (X509)* certs,EVP_PKEY ** matching_pkey,X509 ** matching_cert)5580Sstevel@tonic-gate sunw_find_fname(char *fname, STACK_OF(EVP_PKEY) *pkeys, STACK_OF(X509) *certs,
5590Sstevel@tonic-gate     EVP_PKEY **matching_pkey, X509 ** matching_cert)
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate 	ASN1_STRING *cmpstr = NULL;
5620Sstevel@tonic-gate 	EVP_PKEY *tmp_pkey = NULL;
5630Sstevel@tonic-gate 	X509 *tmp_cert = NULL;
5640Sstevel@tonic-gate 	int retval = 0;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	/* If NULL arguments, this is an error */
5670Sstevel@tonic-gate 	if (fname == NULL ||
5680Sstevel@tonic-gate 	    (pkeys == NULL || certs == NULL) ||
5690Sstevel@tonic-gate 	    (pkeys != NULL && matching_pkey == NULL) ||
5700Sstevel@tonic-gate 	    (certs != NULL && matching_cert == NULL)) {
5710Sstevel@tonic-gate 		SUNWerr(SUNW_F_FIND_FNAME, SUNW_R_INVALID_ARG);
5720Sstevel@tonic-gate 		return (-1);
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	if (matching_pkey != NULL)
5760Sstevel@tonic-gate 		*matching_pkey = NULL;
5770Sstevel@tonic-gate 	if (matching_cert != NULL)
5780Sstevel@tonic-gate 		*matching_cert = NULL;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	cmpstr = (ASN1_STRING *)asc2bmpstring(fname, strlen(fname));
5810Sstevel@tonic-gate 	if (cmpstr == NULL) {
5820Sstevel@tonic-gate 		/*
5830Sstevel@tonic-gate 		 * Error already on stack
5840Sstevel@tonic-gate 		 */
5850Sstevel@tonic-gate 		return (-1);
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	retval = find_attr(NID_friendlyName, cmpstr, pkeys, &tmp_pkey, certs,
5890Sstevel@tonic-gate 	    &tmp_cert);
5900Sstevel@tonic-gate 	if (retval == 0) {
5910Sstevel@tonic-gate 		ASN1_STRING_free(cmpstr);
5920Sstevel@tonic-gate 		return (retval);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	if (matching_pkey != NULL)
5960Sstevel@tonic-gate 		*matching_pkey = tmp_pkey;
5970Sstevel@tonic-gate 	if (matching_cert != NULL)
5980Sstevel@tonic-gate 		*matching_cert = tmp_cert;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	return (retval);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate /*
6040Sstevel@tonic-gate  * sunw_print_times() formats and prints cert times to the given file.
6050Sstevel@tonic-gate  *
6060Sstevel@tonic-gate  * The label is printed on one line. One or both dates are printed on
6070Sstevel@tonic-gate  * the following line or two, each with it's own indented label in the
6080Sstevel@tonic-gate  * format:
6090Sstevel@tonic-gate  *
6100Sstevel@tonic-gate  *    label
6110Sstevel@tonic-gate  *      'not before' date: whatever
6120Sstevel@tonic-gate  *      'not after' date:  whatever
6130Sstevel@tonic-gate  *
6140Sstevel@tonic-gate  * Arguments:
6150Sstevel@tonic-gate  *   fp       - file pointer for file to write to.
6160Sstevel@tonic-gate  *   dowhat   - what field(s) to print.
6170Sstevel@tonic-gate  *   label    - Label to use.  If NULL, no line will be printed.
6180Sstevel@tonic-gate  *   cert     - Points to a client or CA certs to check
6190Sstevel@tonic-gate  *
6200Sstevel@tonic-gate  * Returns:
6210Sstevel@tonic-gate  *  <  0 - An error occured.
6220Sstevel@tonic-gate  *  >= 0 - Number of lines written.
6230Sstevel@tonic-gate  */
6240Sstevel@tonic-gate int
sunw_print_times(FILE * fp,prnt_actions_t dowhat,char * label,X509 * cert)6250Sstevel@tonic-gate sunw_print_times(FILE *fp, prnt_actions_t dowhat, char *label, X509 *cert)
6260Sstevel@tonic-gate {
6270Sstevel@tonic-gate 	int lines = 0;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	if (label != NULL) {
6300Sstevel@tonic-gate 		(void) fprintf(fp, "%s\n", label);
6310Sstevel@tonic-gate 		lines++;
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	if (dowhat == PRNT_NOT_BEFORE || dowhat == PRNT_BOTH) {
6350Sstevel@tonic-gate 		(void) fprintf(fp, "'not before' date: ");
6360Sstevel@tonic-gate 		(void) print_time(fp, X509_get_notBefore(cert));
6370Sstevel@tonic-gate 		(void) fprintf(fp, "\n");
6380Sstevel@tonic-gate 		lines++;
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	if (dowhat == PRNT_NOT_AFTER || dowhat == PRNT_BOTH) {
6420Sstevel@tonic-gate 		(void) fprintf(fp, "'not after' date:  ");
6430Sstevel@tonic-gate 		(void) print_time(fp, X509_get_notAfter(cert));
6440Sstevel@tonic-gate 		(void) fprintf(fp, "\n");
6450Sstevel@tonic-gate 		lines++;
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 	return (lines);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate  * sunw_check_keys() compares the public key in the certificate and a
6520Sstevel@tonic-gate  *     private key to ensure that they match.
6530Sstevel@tonic-gate  *
6540Sstevel@tonic-gate  * Arguments:
6550Sstevel@tonic-gate  *   cert     - Points to a certificate.
6560Sstevel@tonic-gate  *   pkey     - Points to a private key.
6570Sstevel@tonic-gate  *
6580Sstevel@tonic-gate  * Returns:
6590Sstevel@tonic-gate  *  == 0 - These do not match.
6600Sstevel@tonic-gate  *  != 0 - The cert's public key and the private key match.
6610Sstevel@tonic-gate  */
6620Sstevel@tonic-gate int
sunw_check_keys(X509 * cert,EVP_PKEY * pkey)6630Sstevel@tonic-gate sunw_check_keys(X509 *cert, EVP_PKEY *pkey)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate 	int retval = 0;
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	if (pkey != NULL && cert != NULL)
6680Sstevel@tonic-gate 		retval = X509_check_private_key(cert, pkey);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	return (retval);
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate /*
6740Sstevel@tonic-gate  * sunw_issuer_attrs - Given a cert, return the issuer-specific attributes
6750Sstevel@tonic-gate  *     as one ASCII string.
6760Sstevel@tonic-gate  *
6770Sstevel@tonic-gate  * Arguments:
6780Sstevel@tonic-gate  *   cert     - Cert to process
6790Sstevel@tonic-gate  *   buf      - If non-NULL, buffer to receive string.  If NULL, one will
6800Sstevel@tonic-gate  *              be allocated and its value will be returned to the caller.
6810Sstevel@tonic-gate  *   len      - If 'buff' is non-null, the buffer's length.
6820Sstevel@tonic-gate  *
6830Sstevel@tonic-gate  * This returns an ASCII string with all issuer-related attributes in one
6840Sstevel@tonic-gate  * string separated by '/' characters.  Each attribute begins with its name
6850Sstevel@tonic-gate  * and an equal sign.  Two attributes (ATTR1 and Attr2) would have the
6860Sstevel@tonic-gate  * following form:
6870Sstevel@tonic-gate  *
6880Sstevel@tonic-gate  *         ATTR1=attr_value/ATTR2=attr2_value
6890Sstevel@tonic-gate  *
6900Sstevel@tonic-gate  * Returns:
6910Sstevel@tonic-gate  *   != NULL  - Pointer to the ASCII string containing the issuer-related
6920Sstevel@tonic-gate  *              attributes.  If the 'buf' argument was NULL, this is a
6930Sstevel@tonic-gate  *              dynamically-allocated buffer and the caller will have the
6940Sstevel@tonic-gate  *              responsibility for freeing it.
6950Sstevel@tonic-gate  *   NULL     - Memory needed to be allocated but could not be.  Errors
6960Sstevel@tonic-gate  *              are set on the error stack.
6970Sstevel@tonic-gate  */
6980Sstevel@tonic-gate char *
sunw_issuer_attrs(X509 * cert,char * buf,int len)6990Sstevel@tonic-gate sunw_issuer_attrs(X509 *cert, char *buf, int len)
7000Sstevel@tonic-gate {
7010Sstevel@tonic-gate 	return (X509_NAME_oneline(X509_get_issuer_name(cert), buf, len));
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate /*
7050Sstevel@tonic-gate  * sunw_subject_attrs - Given a cert, return the subject-specific attributes
7060Sstevel@tonic-gate  *     as one ASCII string.
7070Sstevel@tonic-gate  *
7080Sstevel@tonic-gate  * Arguments:
7090Sstevel@tonic-gate  *   cert     - Cert to process
7100Sstevel@tonic-gate  *   buf      - If non-NULL, buffer to receive string.  If NULL, one will
7110Sstevel@tonic-gate  *              be allocated and its value will be returned to the caller.
7120Sstevel@tonic-gate  *   len      - If 'buff' is non-null, the buffer's length.
7130Sstevel@tonic-gate  *
7140Sstevel@tonic-gate  * This returns an ASCII string with all subject-related attributes in one
7150Sstevel@tonic-gate  * string separated by '/' characters.  Each attribute begins with its name
7160Sstevel@tonic-gate  * and an equal sign.  Two attributes (ATTR1 and Attr2) would have the
7170Sstevel@tonic-gate  * following form:
7180Sstevel@tonic-gate  *
7190Sstevel@tonic-gate  *         ATTR1=attr_value/ATTR2=attr2_value
7200Sstevel@tonic-gate  *
7210Sstevel@tonic-gate  * Returns:
7220Sstevel@tonic-gate  *   != NULL  - Pointer to the ASCII string containing the subject-related
7230Sstevel@tonic-gate  *              attributes.  If the 'buf' argument was NULL, this is a
7240Sstevel@tonic-gate  *              dynamically-allocated buffer and the caller will have the
7250Sstevel@tonic-gate  *              responsibility for freeing it.
7260Sstevel@tonic-gate  *   NULL     - Memory needed to be allocated but could not be.  Errors
7270Sstevel@tonic-gate  *              are set on the error stack.
7280Sstevel@tonic-gate  */
7290Sstevel@tonic-gate char *
sunw_subject_attrs(X509 * cert,char * buf,int len)7300Sstevel@tonic-gate sunw_subject_attrs(X509 *cert, char *buf, int len)
7310Sstevel@tonic-gate {
7320Sstevel@tonic-gate 	return (X509_NAME_oneline(X509_get_subject_name(cert), buf, len));
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate /*
7360Sstevel@tonic-gate  * sunw_append_keys - Given two stacks of private keys, remove the keys from
7370Sstevel@tonic-gate  *      the second stack and append them to the first.  Both stacks must exist
7380Sstevel@tonic-gate  *      at time of call.
7390Sstevel@tonic-gate  *
7400Sstevel@tonic-gate  * Arguments:
7410Sstevel@tonic-gate  *   dst 	- the stack to receive the keys from 'src'
7420Sstevel@tonic-gate  *   src	- the stack whose keys are to be moved.
7430Sstevel@tonic-gate  *
7440Sstevel@tonic-gate  * Returns:
7450Sstevel@tonic-gate  *   -1  	- An error occurred.  The error status is set.
7460Sstevel@tonic-gate  *   >= 0       - The number of keys that were copied.
7470Sstevel@tonic-gate  */
7480Sstevel@tonic-gate int
sunw_append_keys(STACK_OF (EVP_PKEY)* dst,STACK_OF (EVP_PKEY)* src)7490Sstevel@tonic-gate sunw_append_keys(STACK_OF(EVP_PKEY) *dst, STACK_OF(EVP_PKEY) *src)
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate 	EVP_PKEY *tmpk;
7520Sstevel@tonic-gate 	int count = 0;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	while (sk_EVP_PKEY_num(src) > 0) {
7550Sstevel@tonic-gate 		tmpk = sk_EVP_PKEY_delete(src, 0);
7560Sstevel@tonic-gate 		if (sk_EVP_PKEY_push(dst, tmpk) == 0) {
7570Sstevel@tonic-gate 			sunw_evp_pkey_free(tmpk);
7580Sstevel@tonic-gate 			SUNWerr(SUNW_F_APPEND_KEYS, SUNW_R_MEMORY_FAILURE);
7590Sstevel@tonic-gate 			return (-1);
7600Sstevel@tonic-gate 		}
7610Sstevel@tonic-gate 		count++;
7620Sstevel@tonic-gate 	}
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	return (count);
7650Sstevel@tonic-gate }
766