17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2002, 2003 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 279a686fbcSPaul Dagnelie /* 289a686fbcSPaul Dagnelie * Copyright (c) 2015 by Delphix. All rights reserved. 299a686fbcSPaul Dagnelie */ 309a686fbcSPaul Dagnelie 317c478bd9Sstevel@tonic-gate #ifndef _P12AUX_H 327c478bd9Sstevel@tonic-gate #define _P12AUX_H 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #include <openssl/pkcs12.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #ifdef __cplusplus 377c478bd9Sstevel@tonic-gate extern "C" { 387c478bd9Sstevel@tonic-gate #endif 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate /* 417c478bd9Sstevel@tonic-gate * I really hate to do this. It's pretty gross, but go ahead and use the 427c478bd9Sstevel@tonic-gate * macros and functions already defined to provide new EVP_PKEY-specific 437c478bd9Sstevel@tonic-gate * macros, for use within this file only. 447c478bd9Sstevel@tonic-gate * 457c478bd9Sstevel@tonic-gate * My apologies. 467c478bd9Sstevel@tonic-gate */ 47*c4567a61SPaul Dagnelie /* BEGIN CSTYLED */ 482a446632SRobert Mustacchi DECLARE_STACK_OF(EVP_PKEY) 49*c4567a61SPaul Dagnelie /* END CSTYLED */ 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate #define sk_EVP_PKEY_new_null() SKM_sk_new_null(EVP_PKEY) 527c478bd9Sstevel@tonic-gate #define sk_EVP_PKEY_free(st) SKM_sk_free(EVP_PKEY, (st)) 537c478bd9Sstevel@tonic-gate #define sk_EVP_PKEY_num(st) SKM_sk_num(EVP_PKEY, (st)) 547c478bd9Sstevel@tonic-gate #define sk_EVP_PKEY_value(st, i) SKM_sk_value(EVP_PKEY, (st), (i)) 557c478bd9Sstevel@tonic-gate #define sk_EVP_PKEY_push(st, val) SKM_sk_push(EVP_PKEY, (st), (val)) 567c478bd9Sstevel@tonic-gate #define sk_EVP_PKEY_find(st, val) SKM_sk_find(EVP_PKEY, (st), (val)) 577c478bd9Sstevel@tonic-gate #define sk_EVP_PKEY_delete(st, i) SKM_sk_delete(EVP_PKEY, (st), (i)) 587c478bd9Sstevel@tonic-gate #define sk_EVP_PKEY_delete_ptr(st, ptr) SKM_sk_delete_ptr(EVP_PKEY, (st), (ptr)) 597c478bd9Sstevel@tonic-gate #define sk_EVP_PKEY_insert(st, val, i) SKM_sk_insert(EVP_PKEY, (st), (val), (i)) 607c478bd9Sstevel@tonic-gate #define sk_EVP_PKEY_pop_free(st, free_func) SKM_sk_pop_free(EVP_PKEY, (st), \ 617c478bd9Sstevel@tonic-gate (free_func)) 627c478bd9Sstevel@tonic-gate #define sk_EVP_PKEY_pop(st) SKM_sk_pop(EVP_PKEY, (st)) 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate /* 657c478bd9Sstevel@tonic-gate * This type indicates what to do with an attribute being returned. 667c478bd9Sstevel@tonic-gate */ 677c478bd9Sstevel@tonic-gate typedef enum { 687c478bd9Sstevel@tonic-gate GETDO_COPY = 1, /* Simply return the value of the attribute */ 697c478bd9Sstevel@tonic-gate GETDO_DEL /* Delete the attribute at the same time. */ 707c478bd9Sstevel@tonic-gate } getdo_actions_t; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate /* 737c478bd9Sstevel@tonic-gate * The following is used to call the sunw_print_times function which is 747c478bd9Sstevel@tonic-gate * described at the bottom of the page. 757c478bd9Sstevel@tonic-gate */ 767c478bd9Sstevel@tonic-gate typedef enum { 777c478bd9Sstevel@tonic-gate PRNT_NOT_BEFORE = 1, /* Print 'not before' date */ 787c478bd9Sstevel@tonic-gate PRNT_NOT_AFTER, /* Print 'not after' date */ 797c478bd9Sstevel@tonic-gate PRNT_BOTH /* Prints both dates */ 807c478bd9Sstevel@tonic-gate } prnt_actions_t; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate /* 837c478bd9Sstevel@tonic-gate * For sunw_pkcs12_parse, the following are values for bits that indicate 847c478bd9Sstevel@tonic-gate * various types of searches/matching to do. Any of these values can be 857c478bd9Sstevel@tonic-gate * OR'd together. However, the order in which an attempt will be made 867c478bd9Sstevel@tonic-gate * to satisfy them is the order in which they are listed below. The 877c478bd9Sstevel@tonic-gate * exception is DO_NONE. It should not be OR'd with any other value. 887c478bd9Sstevel@tonic-gate */ 897c478bd9Sstevel@tonic-gate #define DO_NONE 0x00 /* Don't even try to match */ 907c478bd9Sstevel@tonic-gate #define DO_FIND_KEYID 0x01 /* 1st cert, key with matching localkeyid */ 917c478bd9Sstevel@tonic-gate #define DO_FIND_FN 0x02 /* 1st cert, key with matching friendlyname */ 927c478bd9Sstevel@tonic-gate #define DO_FIRST_PAIR 0x04 /* Return first matching cert/key pair found */ 937c478bd9Sstevel@tonic-gate #define DO_LAST_PAIR 0x08 /* Return last matching cert/key pair found */ 947c478bd9Sstevel@tonic-gate #define DO_UNMATCHING 0x10 /* Return first cert and/or key */ 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate /* Bits returned, which indicate what values were found. */ 977c478bd9Sstevel@tonic-gate #define FOUND_PKEY 0x01 /* Found one or more private key */ 987c478bd9Sstevel@tonic-gate #define FOUND_CERT 0x02 /* Found one or more client certificate */ 997c478bd9Sstevel@tonic-gate #define FOUND_CA_CERTS 0x04 /* Added at least one cert to the CA list */ 1007c478bd9Sstevel@tonic-gate #define FOUND_XPKEY 0x08 /* Found at least one private key which does */ 1017c478bd9Sstevel@tonic-gate /* not match a certificate in the certs list */ 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate /* 1047c478bd9Sstevel@tonic-gate * sunw_cryto_init() does crypto-specific initialization. 1057c478bd9Sstevel@tonic-gate * 1067c478bd9Sstevel@tonic-gate * Arguments: 1077c478bd9Sstevel@tonic-gate * None. 1087c478bd9Sstevel@tonic-gate * 1097c478bd9Sstevel@tonic-gate * Returns: 1107c478bd9Sstevel@tonic-gate * None. 1117c478bd9Sstevel@tonic-gate */ 1127c478bd9Sstevel@tonic-gate void sunw_crypto_init(void); 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate /* 1157c478bd9Sstevel@tonic-gate * sunw_PKCS12_parse() parses a pkcs#12 structure and returns component parts. 1167c478bd9Sstevel@tonic-gate * 1177c478bd9Sstevel@tonic-gate * Parse and decrypt a PKCS#12 structure returning user key, user cert and/or 1187c478bd9Sstevel@tonic-gate * other (CA) certs. Note either ca should be NULL, *ca should be NULL, 1197c478bd9Sstevel@tonic-gate * or it should point to a valid STACK_OF(X509) structure. pkey and cert can 1207c478bd9Sstevel@tonic-gate * be passed uninitialized. 1217c478bd9Sstevel@tonic-gate * 1227c478bd9Sstevel@tonic-gate * Arguments: 1237c478bd9Sstevel@tonic-gate * p12 - Structure with pkcs12 info to be parsed 1247c478bd9Sstevel@tonic-gate * pass - Pass phrase for the private key and entire pkcs12 wad (possibly 1257c478bd9Sstevel@tonic-gate * empty) or NULL if there is none. 1267c478bd9Sstevel@tonic-gate * matchty - Info about which certs/keys to return if many are in the file. 1277c478bd9Sstevel@tonic-gate * keyid_str- If private key localkeyids are to match a predetermined value, 1287c478bd9Sstevel@tonic-gate * the value to match. 1297c478bd9Sstevel@tonic-gate * keyid_len- Length of the keyid byte string. 1307c478bd9Sstevel@tonic-gate * name_str - If friendlynames are to match a predetermined value, the value 1317c478bd9Sstevel@tonic-gate * to match. 1327c478bd9Sstevel@tonic-gate * pkey - Points to location pointing to the private key returned. 1337c478bd9Sstevel@tonic-gate * cert - Points to locaiton which points to the client cert returned 1347c478bd9Sstevel@tonic-gate * ca - Points to location that points to a stack of 'certificate 1357c478bd9Sstevel@tonic-gate * authority' certs (possibly including trust anchors). 1367c478bd9Sstevel@tonic-gate * 1377c478bd9Sstevel@tonic-gate * Match based on the value of 'matchty' and the contents of 'keyid_str' 1387c478bd9Sstevel@tonic-gate * and/or 'name_str', as appropriate. Go through the lists of certs and 1397c478bd9Sstevel@tonic-gate * private keys which were taken from the pkcs12 structure, looking for 1407c478bd9Sstevel@tonic-gate * matches of the requested type. This function only searches the lists of 1417c478bd9Sstevel@tonic-gate * matching private keys and client certificates. Kinds of matches allowed, 1427c478bd9Sstevel@tonic-gate * and the order in which they will be checked, are: 1437c478bd9Sstevel@tonic-gate * 1447c478bd9Sstevel@tonic-gate * 1) Find the key and/or cert whose localkeyid attributes matches 'cmpstr' 1457c478bd9Sstevel@tonic-gate * 2) Find the key and/or cert whose friendlyname attributes matches 'cmpstr' 1467c478bd9Sstevel@tonic-gate * 3) Return the first matching key/cert pair found. 1477c478bd9Sstevel@tonic-gate * 4) Return the last matching key/cert pair found. 1487c478bd9Sstevel@tonic-gate * 5) Return whatever cert and/or key are available, even unmatching. 1497c478bd9Sstevel@tonic-gate * 1507c478bd9Sstevel@tonic-gate * Append the certs which do not have matching private keys and which were 1517c478bd9Sstevel@tonic-gate * not selected to the CA list. 1527c478bd9Sstevel@tonic-gate * 1537c478bd9Sstevel@tonic-gate * If none of the bits are set, no client certs or private keys will be 1547c478bd9Sstevel@tonic-gate * returned. CA (aka trust anchor) certs can be. 1557c478bd9Sstevel@tonic-gate * 1567c478bd9Sstevel@tonic-gate * Notes: If #3 is selected, then #4 will never occur. CA certs will be 1577c478bd9Sstevel@tonic-gate * selected after a cert/key pairs are isolated. 1587c478bd9Sstevel@tonic-gate * 1597c478bd9Sstevel@tonic-gate * Returns: 1607c478bd9Sstevel@tonic-gate * < 0 - An error returned. Call ERR_get_error() to get errors information. 1617c478bd9Sstevel@tonic-gate * Where possible, memory has been freed. 1627c478bd9Sstevel@tonic-gate * >= 0 - Objects were found and returned. Which objects are indicated by 1637c478bd9Sstevel@tonic-gate * which bits are set (FOUND_PKEY, FOUND_CERT, FOUND_CA_CERTS). 1647c478bd9Sstevel@tonic-gate */ 1657c478bd9Sstevel@tonic-gate int sunw_PKCS12_parse(PKCS12 *, const char *, int, char *, int, char *, 1667c478bd9Sstevel@tonic-gate EVP_PKEY **, X509 **, STACK_OF(X509) **); 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate /* 1707c478bd9Sstevel@tonic-gate * sunw_PKCS12_create() creates a pkcs#12 structure and given component parts. 1717c478bd9Sstevel@tonic-gate * 1727c478bd9Sstevel@tonic-gate * Given one or more of user private key, user cert and/or other (CA) certs, 1737c478bd9Sstevel@tonic-gate * return an encrypted PKCS12 structure containing them. 1747c478bd9Sstevel@tonic-gate * 1757c478bd9Sstevel@tonic-gate * Arguments: 1767c478bd9Sstevel@tonic-gate * pass - Pass phrase for the pkcs12 structure and private key (possibly 1777c478bd9Sstevel@tonic-gate * empty) or NULL if there is none. It will be used to encrypt 1787c478bd9Sstevel@tonic-gate * both the private key(s) and as the pass phrase for the whole 1797c478bd9Sstevel@tonic-gate * pkcs12 wad. 1807c478bd9Sstevel@tonic-gate * pkey - Points to stack of private keys. 1817c478bd9Sstevel@tonic-gate * cert - Points to stack of client (public ke) certs 1827c478bd9Sstevel@tonic-gate * ca - Points to stack of 'certificate authority' certs (or trust 1837c478bd9Sstevel@tonic-gate * anchors). 1847c478bd9Sstevel@tonic-gate * 1857c478bd9Sstevel@tonic-gate * Note that any of these may be NULL. 1867c478bd9Sstevel@tonic-gate * 1877c478bd9Sstevel@tonic-gate * Returns: 1887c478bd9Sstevel@tonic-gate * NULL - An error occurred. 1897c478bd9Sstevel@tonic-gate * != NULL - Address of PKCS12 structure. The user is responsible for 1907c478bd9Sstevel@tonic-gate * freeing the memory when done. 1917c478bd9Sstevel@tonic-gate */ 1927c478bd9Sstevel@tonic-gate PKCS12 *sunw_PKCS12_create(const char *, STACK_OF(EVP_PKEY) *, STACK_OF(X509) *, 1937c478bd9Sstevel@tonic-gate STACK_OF(X509) *); 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /* 1977c478bd9Sstevel@tonic-gate * sunw_split_certs() - Given a list of certs and a list of private keys, 1987c478bd9Sstevel@tonic-gate * moves certs which match one of the keys to a different stack. 1997c478bd9Sstevel@tonic-gate * 2007c478bd9Sstevel@tonic-gate * Arguments: 2017c478bd9Sstevel@tonic-gate * allkeys - Points to a stack of private keys to search. 2027c478bd9Sstevel@tonic-gate * allcerts - Points to a stack of certs to be searched. 2037c478bd9Sstevel@tonic-gate * keycerts - Points to address of a stack of certs with matching private 2047c478bd9Sstevel@tonic-gate * keys. They are moved from 'allcerts'. This may not be NULL 2057c478bd9Sstevel@tonic-gate * when called. If *keycerts is NULL upon entry, a new stack will 2067c478bd9Sstevel@tonic-gate * be allocated. Otherwise, it must be a valid STACK_OF(509). 2077c478bd9Sstevel@tonic-gate * nocerts - Points to address of a stack for keys which have no matching 2087c478bd9Sstevel@tonic-gate * certs. Keys are moved from 'allkeys' here when they have no 2097c478bd9Sstevel@tonic-gate * matching certs. If this is NULL, matchless keys will be 2107c478bd9Sstevel@tonic-gate * discarded. 2117c478bd9Sstevel@tonic-gate * 2127c478bd9Sstevel@tonic-gate * Notes: If an error occurs while moving certs, the cert being move may be 2137c478bd9Sstevel@tonic-gate * lost. 'keycerts' may only contain part of the matching certs. The number 2147c478bd9Sstevel@tonic-gate * of certs successfully moved can be found by checking sk_X509_num(keycerts). 2157c478bd9Sstevel@tonic-gate * 2167c478bd9Sstevel@tonic-gate * If there is a key which does not have a matching cert, it is moved to 2177c478bd9Sstevel@tonic-gate * the list nocerts. 2187c478bd9Sstevel@tonic-gate * 2197c478bd9Sstevel@tonic-gate * If all certs are removed from 'certs' and/or 'pkeys', it will be the 2207c478bd9Sstevel@tonic-gate * caller's responsibility to free the empty stacks. 2217c478bd9Sstevel@tonic-gate * 2227c478bd9Sstevel@tonic-gate * Returns: 2237c478bd9Sstevel@tonic-gate * < 0 - An error returned. Call ERR_get_error() to get errors information. 2247c478bd9Sstevel@tonic-gate * Where possible, memory has been freed. 2257c478bd9Sstevel@tonic-gate * >= 0 - The number of certs moved from 'cert' to 'pkcerts'. 2267c478bd9Sstevel@tonic-gate */ 2277c478bd9Sstevel@tonic-gate int sunw_split_certs(STACK_OF(EVP_PKEY) *, STACK_OF(X509) *, STACK_OF(X509) **, 2287c478bd9Sstevel@tonic-gate STACK_OF(EVP_PKEY) **); 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate /* 2317c478bd9Sstevel@tonic-gate * sunw_evp_pkey_free() Given an EVP_PKEY structure, free any attributes 2327c478bd9Sstevel@tonic-gate * that are attached. Then free the EVP_PKEY itself. 2337c478bd9Sstevel@tonic-gate * 2347c478bd9Sstevel@tonic-gate * This is the replacement for EVP_PKEY_free() for the sunw stuff. 2357c478bd9Sstevel@tonic-gate * It should be used in places where EVP_PKEY_free would be used, 2367c478bd9Sstevel@tonic-gate * including calls to sk_EVP_PKEY_pop_free(). 2377c478bd9Sstevel@tonic-gate * 2387c478bd9Sstevel@tonic-gate * Arguments: 2397c478bd9Sstevel@tonic-gate * pkey - Entry which potentially has attributes to be freed. 2407c478bd9Sstevel@tonic-gate * 2417c478bd9Sstevel@tonic-gate * Returns: 2427c478bd9Sstevel@tonic-gate * None. 2437c478bd9Sstevel@tonic-gate */ 2447c478bd9Sstevel@tonic-gate void sunw_evp_pkey_free(EVP_PKEY *); 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate /* 2477c478bd9Sstevel@tonic-gate * sunw_set_localkeyid() sets the localkeyid in a cert, a private key or 2487c478bd9Sstevel@tonic-gate * both. Any existing localkeyid will be discarded. 2497c478bd9Sstevel@tonic-gate * 2507c478bd9Sstevel@tonic-gate * Arguments: 2517c478bd9Sstevel@tonic-gate * keyid_str- A byte string with the localkeyid to set 2527c478bd9Sstevel@tonic-gate * keyid_len- Length of the keyid byte string. 2537c478bd9Sstevel@tonic-gate * pkey - Points to a private key to set the keyidstr in. 2547c478bd9Sstevel@tonic-gate * cert - Points to a cert to set the keyidstr in. 2557c478bd9Sstevel@tonic-gate * 2567c478bd9Sstevel@tonic-gate * Note that setting a keyid into a cert which will not be written out as 2577c478bd9Sstevel@tonic-gate * a PKCS12 cert is pointless since it will be lost. 2587c478bd9Sstevel@tonic-gate * 2597c478bd9Sstevel@tonic-gate * Returns: 2607c478bd9Sstevel@tonic-gate * 0 - Success. 2617c478bd9Sstevel@tonic-gate * < 0 - An error occurred. It was probably an error in allocating 2627c478bd9Sstevel@tonic-gate * memory. The error will be set in the error stack. Call 2637c478bd9Sstevel@tonic-gate * ERR_get_error() to get specific information. 2647c478bd9Sstevel@tonic-gate */ 2657c478bd9Sstevel@tonic-gate int sunw_set_localkeyid(const char *, int, EVP_PKEY *, X509 *); 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate /* 2697c478bd9Sstevel@tonic-gate * sunw_get_pkey_localkeyid() gets the localkeyid from a private key. It can 2707c478bd9Sstevel@tonic-gate * optionally remove the value found. 2717c478bd9Sstevel@tonic-gate * 2727c478bd9Sstevel@tonic-gate * Arguments: 2737c478bd9Sstevel@tonic-gate * dowhat - What to do with the attributes (remove them or copy them). 2747c478bd9Sstevel@tonic-gate * pkey - Points to a private key to set the keyidstr in. 2757c478bd9Sstevel@tonic-gate * keyid_str- Points to a location which will receive the pointer to 2767c478bd9Sstevel@tonic-gate * a byte string containing the binary localkeyid. Note that 2777c478bd9Sstevel@tonic-gate * this is a copy, and the caller must free it. 2787c478bd9Sstevel@tonic-gate * keyid_len- Length of keyid_str. 2797c478bd9Sstevel@tonic-gate * 2807c478bd9Sstevel@tonic-gate * Returns: 2817c478bd9Sstevel@tonic-gate * >= 0 - The number of characters in the keyid returned. 2827c478bd9Sstevel@tonic-gate * < 0 - An error occurred. It was probably an error in allocating 2837c478bd9Sstevel@tonic-gate * memory. The error will be set in the error stack. Call 2847c478bd9Sstevel@tonic-gate * ERR_get_error() to get specific information. 2857c478bd9Sstevel@tonic-gate */ 2867c478bd9Sstevel@tonic-gate int sunw_get_pkey_localkeyid(getdo_actions_t, EVP_PKEY *, char **, int *); 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate /* 2907c478bd9Sstevel@tonic-gate * sunw_get_pkey_fname() gets the friendlyName from a private key. It can 2917c478bd9Sstevel@tonic-gate * optionally remove the value found. 2927c478bd9Sstevel@tonic-gate * 2937c478bd9Sstevel@tonic-gate * Arguments: 2947c478bd9Sstevel@tonic-gate * dowhat - What to do with the attributes (remove them or just return 2957c478bd9Sstevel@tonic-gate * them). 2967c478bd9Sstevel@tonic-gate * pkey - Points to a private key to get the keyid from 2977c478bd9Sstevel@tonic-gate * fname - Points to a location which will receive the pointer to a 2987c478bd9Sstevel@tonic-gate * byte string with the ASCII friendlyname 2997c478bd9Sstevel@tonic-gate * 3007c478bd9Sstevel@tonic-gate * Returns: 3017c478bd9Sstevel@tonic-gate * >= 0 - The number of characters in the keyid returned. 3027c478bd9Sstevel@tonic-gate * < 0 - An error occurred. It was probably an error in allocating 3037c478bd9Sstevel@tonic-gate * memory. The error will be set in the error stack. Call 3047c478bd9Sstevel@tonic-gate * ERR_get_error() to get specific information. 3057c478bd9Sstevel@tonic-gate */ 3067c478bd9Sstevel@tonic-gate int sunw_get_pkey_fname(getdo_actions_t, EVP_PKEY *, char **); 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate /* 3107c478bd9Sstevel@tonic-gate * sunw_find_localkeyid() searches stacks of certs and private keys, and 3117c478bd9Sstevel@tonic-gate * returns the first matching cert/private key found. 3127c478bd9Sstevel@tonic-gate * 3137c478bd9Sstevel@tonic-gate * Look for a keyid in a stack of certs. if 'certs' is NULL and 'pkeys' is 3147c478bd9Sstevel@tonic-gate * not NULL, search the list of private keys. Move the matching cert to 3157c478bd9Sstevel@tonic-gate * 'matching_cert' and its matching private key to 'matching_pkey'. If no 3167c478bd9Sstevel@tonic-gate * cert or keys match, no match occurred. 3177c478bd9Sstevel@tonic-gate * 3187c478bd9Sstevel@tonic-gate * Arguments: 3197c478bd9Sstevel@tonic-gate * keyid_str- A byte string with the localkeyid to match 3207c478bd9Sstevel@tonic-gate * keyid_len- Length of the keyid byte string. 3217c478bd9Sstevel@tonic-gate * pkeys - Points to a stack of private keys which match the certs. 3227c478bd9Sstevel@tonic-gate * This may be NULL, in which case no keys are returned. 3237c478bd9Sstevel@tonic-gate * certs - Points to a stack of certs to search. If NULL, search the 3247c478bd9Sstevel@tonic-gate * stack of keys instead. 3257c478bd9Sstevel@tonic-gate * matching_pkey 3267c478bd9Sstevel@tonic-gate * - Pointer to receive address of first matching pkey found. 3277c478bd9Sstevel@tonic-gate * 'matching_pkey' must not be NULL; '*matching_pkey' will be 3287c478bd9Sstevel@tonic-gate * reset. 3297c478bd9Sstevel@tonic-gate * matching_cert 3307c478bd9Sstevel@tonic-gate * - Pointer to receive address of first matching cert found. 3317c478bd9Sstevel@tonic-gate * 'matching_cert' must not be NULL; '*matching_cert' will be 3327c478bd9Sstevel@tonic-gate * reset. 3337c478bd9Sstevel@tonic-gate * 3347c478bd9Sstevel@tonic-gate * Returns: 3357c478bd9Sstevel@tonic-gate * < 0 - An error returned. Call ERR_get_error() to get errors information. 3367c478bd9Sstevel@tonic-gate * Where possible, memory has been freed. 3377c478bd9Sstevel@tonic-gate * >= 0 - Objects were found and returned. Which objects are indicated by 3387c478bd9Sstevel@tonic-gate * which bits are set (FOUND_PKEY and/or FOUND_CERT). 3397c478bd9Sstevel@tonic-gate */ 3407c478bd9Sstevel@tonic-gate int sunw_find_localkeyid(char *, int, STACK_OF(EVP_PKEY) *, STACK_OF(X509) *, 3417c478bd9Sstevel@tonic-gate EVP_PKEY **, X509 **); 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate /* 3457c478bd9Sstevel@tonic-gate * sunw_find_fname() searches stacks of certs and private keys for one with 3467c478bd9Sstevel@tonic-gate * a matching friendlyname and returns the first matching cert/private 3477c478bd9Sstevel@tonic-gate * key found. 3487c478bd9Sstevel@tonic-gate * 3497c478bd9Sstevel@tonic-gate * Look for a friendlyname in a stack of certs. if 'certs' is NULL and 'pkeys' 3507c478bd9Sstevel@tonic-gate * is not NULL, search the list of private keys. Move the matching cert to 3517c478bd9Sstevel@tonic-gate * 'matching_cert' and its matching private key to 'matching_pkey'. If no 3527c478bd9Sstevel@tonic-gate * cert or keys match, no match occurred. 3537c478bd9Sstevel@tonic-gate * 3547c478bd9Sstevel@tonic-gate * Arguments: 3557c478bd9Sstevel@tonic-gate * fname - Friendlyname to find (NULL-terminated ASCII string). 3567c478bd9Sstevel@tonic-gate * pkeys - Points to a stack of private keys which match the certs. 3577c478bd9Sstevel@tonic-gate * This may be NULL, in which case no keys are returned. 3587c478bd9Sstevel@tonic-gate * certs - Points to a stack of certs to search. If NULL, search the 3597c478bd9Sstevel@tonic-gate * stack of keys instead. 3607c478bd9Sstevel@tonic-gate * matching_pkey 3617c478bd9Sstevel@tonic-gate * - Pointer to receive address of first matching pkey found. 3627c478bd9Sstevel@tonic-gate * matching_cert 3637c478bd9Sstevel@tonic-gate * - Pointer to receive address of first matching cert found. 3647c478bd9Sstevel@tonic-gate * 3657c478bd9Sstevel@tonic-gate * Returns: 3667c478bd9Sstevel@tonic-gate * < 0 - An error returned. Call ERR_get_error() to get errors information. 3677c478bd9Sstevel@tonic-gate * Where possible, memory has been freed. 3687c478bd9Sstevel@tonic-gate * >= 0 - Objects were found and returned. Which objects are indicated by 3697c478bd9Sstevel@tonic-gate * which bits are set (FOUND_PKEY and/or FOUND_CERT). 3707c478bd9Sstevel@tonic-gate */ 3717c478bd9Sstevel@tonic-gate int sunw_find_fname(char *, STACK_OF(EVP_PKEY) *, STACK_OF(X509) *, EVP_PKEY **, 3727c478bd9Sstevel@tonic-gate X509 **); 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate /* 3767c478bd9Sstevel@tonic-gate * sunw_print_times() formats and prints cert times to the given file. 3777c478bd9Sstevel@tonic-gate * 3787c478bd9Sstevel@tonic-gate * The label is printed on one line. One or both dates are printed on 3797c478bd9Sstevel@tonic-gate * the following line or two, each with it's own indented label in the 3807c478bd9Sstevel@tonic-gate * format: 3817c478bd9Sstevel@tonic-gate * 3827c478bd9Sstevel@tonic-gate * label 3837c478bd9Sstevel@tonic-gate * 'not before' date: whatever 3847c478bd9Sstevel@tonic-gate * 'not after' date: whatever 3857c478bd9Sstevel@tonic-gate * 3867c478bd9Sstevel@tonic-gate * Arguments: 3877c478bd9Sstevel@tonic-gate * fp - file pointer for file to write to. 3887c478bd9Sstevel@tonic-gate * dowhat - what field(s) to print. 3897c478bd9Sstevel@tonic-gate * label - Label to use. If NULL, no line will be printed. 3907c478bd9Sstevel@tonic-gate * cert - Points to a client or CA cert to check 3917c478bd9Sstevel@tonic-gate * 3927c478bd9Sstevel@tonic-gate * Returns: 3937c478bd9Sstevel@tonic-gate * < 0 - An error occured. 3947c478bd9Sstevel@tonic-gate * >= 0 - Number of lines written. 3957c478bd9Sstevel@tonic-gate */ 3967c478bd9Sstevel@tonic-gate int sunw_print_times(FILE *, prnt_actions_t, char *, X509 *); 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate 3997c478bd9Sstevel@tonic-gate /* 4007c478bd9Sstevel@tonic-gate * sunw_check_keys() compares the public key in the certificate and a 4017c478bd9Sstevel@tonic-gate * private key to ensure that they match. 4027c478bd9Sstevel@tonic-gate * 4037c478bd9Sstevel@tonic-gate * Arguments: 4047c478bd9Sstevel@tonic-gate * cert - Points to a certificate. 4057c478bd9Sstevel@tonic-gate * pkey - Points to a private key. 4067c478bd9Sstevel@tonic-gate * 4077c478bd9Sstevel@tonic-gate * Returns: 4087c478bd9Sstevel@tonic-gate * == 0 - These do not match. 4097c478bd9Sstevel@tonic-gate * != 0 - The cert's public key and the private key match. 4107c478bd9Sstevel@tonic-gate */ 4117c478bd9Sstevel@tonic-gate int sunw_check_keys(X509 *, EVP_PKEY *); 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate /* 4157c478bd9Sstevel@tonic-gate * sunw_issuer_attrs - Given a cert, return the issuer-specific attributes 4167c478bd9Sstevel@tonic-gate * as one ASCII string. 4177c478bd9Sstevel@tonic-gate * 4187c478bd9Sstevel@tonic-gate * Arguments: 4197c478bd9Sstevel@tonic-gate * cert - Cert to process 4207c478bd9Sstevel@tonic-gate * buf - If non-NULL, buffer to receive string. If NULL, one will 4217c478bd9Sstevel@tonic-gate * be allocated and its value will be returned to the caller. 4227c478bd9Sstevel@tonic-gate * len - If 'buff' is non-null, the buffer's length. 4237c478bd9Sstevel@tonic-gate * 4247c478bd9Sstevel@tonic-gate * This returns an ASCII string with all issuer-related attributes in one 4257c478bd9Sstevel@tonic-gate * string separated by '/' characters. Each attribute begins with its name 4267c478bd9Sstevel@tonic-gate * and an equal sign. Two attributes (ATTR1 and Attr2) would have the 4277c478bd9Sstevel@tonic-gate * following form: 4287c478bd9Sstevel@tonic-gate * 4297c478bd9Sstevel@tonic-gate * ATTR1=attr_value/ATTR2=attr2_value 4307c478bd9Sstevel@tonic-gate * 4317c478bd9Sstevel@tonic-gate * Returns: 4327c478bd9Sstevel@tonic-gate * != NULL - Pointer to the ASCII string containing the issuer-related 4337c478bd9Sstevel@tonic-gate * attributes. If the 'buf' argument was NULL, this is a 4347c478bd9Sstevel@tonic-gate * dynamically-allocated buffer and the caller will have the 4357c478bd9Sstevel@tonic-gate * responsibility for freeing it. 4367c478bd9Sstevel@tonic-gate * NULL - Memory needed to be allocated but could not be. Errors 4377c478bd9Sstevel@tonic-gate * are set on the error stack. 4387c478bd9Sstevel@tonic-gate */ 4397c478bd9Sstevel@tonic-gate char *sunw_issuer_attrs(X509 *cert, char *buf, int len); 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate /* 4437c478bd9Sstevel@tonic-gate * sunw_subject_attrs - Given a cert, return the subject-specific attributes 4447c478bd9Sstevel@tonic-gate * as one ASCII string. 4457c478bd9Sstevel@tonic-gate * 4467c478bd9Sstevel@tonic-gate * Arguments: 4477c478bd9Sstevel@tonic-gate * cert - Cert to process 4487c478bd9Sstevel@tonic-gate * buf - If non-NULL, buffer to receive string. If NULL, one will 4497c478bd9Sstevel@tonic-gate * be allocated and its value will be returned to the caller. 4507c478bd9Sstevel@tonic-gate * len - If 'buff' is non-null, the buffer's length. 4517c478bd9Sstevel@tonic-gate * 4527c478bd9Sstevel@tonic-gate * This returns an ASCII string with all subject-related attributes in one 4537c478bd9Sstevel@tonic-gate * string separated by '/' characters. Each attribute begins with its name 4547c478bd9Sstevel@tonic-gate * and an equal sign. Two attributes (ATTR1 and Attr2) would have the 4557c478bd9Sstevel@tonic-gate * following form: 4567c478bd9Sstevel@tonic-gate * 4577c478bd9Sstevel@tonic-gate * ATTR1=attr_value/ATTR2=attr2_value 4587c478bd9Sstevel@tonic-gate * 4597c478bd9Sstevel@tonic-gate * Returns: 4607c478bd9Sstevel@tonic-gate * != NULL - Pointer to the ASCII string containing the subject-related 4617c478bd9Sstevel@tonic-gate * attributes. If the 'buf' argument was NULL, this is a 4627c478bd9Sstevel@tonic-gate * dynamically-allocated buffer and the caller will have the 4637c478bd9Sstevel@tonic-gate * responsibility for freeing it. 4647c478bd9Sstevel@tonic-gate * NULL - Memory needed to be allocated but could not be. Errors 4657c478bd9Sstevel@tonic-gate * are set on the error stack. 4667c478bd9Sstevel@tonic-gate */ 4677c478bd9Sstevel@tonic-gate char *sunw_subject_attrs(X509 *cert, char *buf, int len); 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate /* 4707c478bd9Sstevel@tonic-gate * sunw_append_keys - Given two stacks of private keys, remove the keys from 4717c478bd9Sstevel@tonic-gate * the second stack and append them to the first. Both stacks must exist 4727c478bd9Sstevel@tonic-gate * at time of call. 4737c478bd9Sstevel@tonic-gate * 4747c478bd9Sstevel@tonic-gate * Arguments: 4757c478bd9Sstevel@tonic-gate * dst - the stack to receive the keys from 'src' 4767c478bd9Sstevel@tonic-gate * src - the stack whose keys are to be moved. 4777c478bd9Sstevel@tonic-gate * 4787c478bd9Sstevel@tonic-gate * Returns: 4797c478bd9Sstevel@tonic-gate * -1 - An error occurred. The error status is set. 4807c478bd9Sstevel@tonic-gate * >= 0 - The number of keys that were copied. 4817c478bd9Sstevel@tonic-gate */ 4827c478bd9Sstevel@tonic-gate int sunw_append_keys(STACK_OF(EVP_PKEY) *, STACK_OF(EVP_PKEY) *); 4837c478bd9Sstevel@tonic-gate 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate #ifdef __cplusplus 4867c478bd9Sstevel@tonic-gate } 4877c478bd9Sstevel@tonic-gate #endif 4887c478bd9Sstevel@tonic-gate 4897c478bd9Sstevel@tonic-gate #endif /* _P12AUX_H */ 490