2N/A/*
2N/A * ====================================================================
2N/A * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions
2N/A * are met:
2N/A *
2N/A * 1. Redistributions of source code must retain the above copyright
2N/A * notice, this list of conditions and the following disclaimer.
2N/A *
2N/A * 2. Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in
2N/A * the documentation and/or other materials provided with the
2N/A * distribution.
2N/A *
2N/A * 3. All advertising materials mentioning features or use of this
2N/A * software must display the following acknowledgment:
2N/A * "This product includes software developed by the OpenSSL Project
2N/A * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2N/A *
2N/A * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2N/A * endorse or promote products derived from this software without
2N/A * prior written permission. For written permission, please contact
2N/A * licensing@OpenSSL.org.
2N/A *
2N/A * 5. Products derived from this software may not be called "OpenSSL"
2N/A * nor may "OpenSSL" appear in their names without prior written
2N/A * permission of the OpenSSL Project.
2N/A *
2N/A * 6. Redistributions of any form whatsoever must retain the following
2N/A * acknowledgment:
2N/A * "This product includes software developed by the OpenSSL Project
2N/A * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
2N/A * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
2N/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2N/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2N/A * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2N/A * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2N/A * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2N/A * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
2N/A * OF THE POSSIBILITY OF SUCH DAMAGE.
2N/A * ====================================================================
2N/A *
2N/A * This product includes cryptographic software written by Eric Young
2N/A * (eay@cryptsoft.com). This product includes software written by Tim
2N/A * Hudson (tjh@cryptsoft.com).
2N/A *
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A
2N/A#include <strings.h>
2N/A#include <stdlib.h>
2N/A#include <assert.h>
2N/A
2N/A#include <openssl/crypto.h>
2N/A#include <openssl/err.h>
2N/A#include <openssl/x509.h>
2N/A#include <openssl/pem.h>
2N/A
2N/A#include <openssl/pkcs12.h>
2N/A#include "p12lib.h"
2N/A
2N/A/*
2N/A * OpenSSL provides a framework for pushing error codes onto a stack.
2N/A * When an error occurs, the consumer may use the framework to
2N/A * pop the errors off the stack and provide a trace of where the
2N/A * errors occurred.
2N/A *
2N/A * Our PKCS12 code plugs into this framework by calling
2N/A * ERR_load_SUNW_strings(). To push an error (which by the way, consists
2N/A * of a function code and an error code) onto the stack our PKCS12 code
2N/A * calls SUNWerr().
2N/A *
2N/A * Consumers of our PKCS12 code can then call the OpenSSL error routines
2N/A * when an error occurs and retrieve the stack of errors.
2N/A */
2N/A
2N/A#ifndef OPENSSL_NO_ERR
2N/A
2N/A/* Function codes and their matching strings */
2N/Astatic ERR_STRING_DATA SUNW_str_functs[] = {
2N/A { ERR_PACK(0, SUNW_F_USE_X509CERT, 0), "sunw_use_x509cert" },
2N/A { ERR_PACK(0, SUNW_F_USE_PKEY, 0), "sunw_use_pkey" },
2N/A { ERR_PACK(0, SUNW_F_USE_TASTORE, 0), "sunw_use_tastore" },
2N/A { ERR_PACK(0, SUNW_F_USE_CERTFILE, 0), "sunw_p12_use_certfile" },
2N/A { ERR_PACK(0, SUNW_F_USE_KEYFILE, 0), "sunw_p12_use_keyfile" },
2N/A { ERR_PACK(0, SUNW_F_USE_TRUSTFILE, 0), "sunw_p12_use_trustfile" },
2N/A { ERR_PACK(0, SUNW_F_READ_FILE, 0), "p12_read_file" },
2N/A { ERR_PACK(0, SUNW_F_DOPARSE, 0), "p12_doparse" },
2N/A { ERR_PACK(0, SUNW_F_PKCS12_PARSE, 0), "sunw_PKCS12_parse" },
2N/A { ERR_PACK(0, SUNW_F_PKCS12_CONTENTS, 0), "sunw_PKCS12_contents" },
2N/A { ERR_PACK(0, SUNW_F_PARSE_ONE_BAG, 0), "parse_one_bag" },
2N/A { ERR_PACK(0, SUNW_F_PKCS12_CREATE, 0), "sunw_PKCS12_create" },
2N/A { ERR_PACK(0, SUNW_F_SPLIT_CERTS, 0), "sunw_split_certs" },
2N/A { ERR_PACK(0, SUNW_F_FIND_LOCALKEYID, 0), "sunw_find_localkeyid" },
2N/A { ERR_PACK(0, SUNW_F_SET_LOCALKEYID, 0), "sunw_set_localkeyid" },
2N/A { ERR_PACK(0, SUNW_F_GET_LOCALKEYID, 0), "sunw_get_localkeyid" },
2N/A { ERR_PACK(0, SUNW_F_SET_FNAME, 0), "sunw_set_fname" },
2N/A { ERR_PACK(0, SUNW_F_GET_PKEY_FNAME, 0), "sunw_get_pkey_fname" },
2N/A { ERR_PACK(0, SUNW_F_APPEND_KEYS, 0), "sunw_append_keys" },
2N/A { ERR_PACK(0, SUNW_F_PEM_CONTENTS, 0), "sunw_PEM_contents" },
2N/A { ERR_PACK(0, SUNW_F_PEM_INFO, 0), "pem_info" },
2N/A { ERR_PACK(0, SUNW_F_ASC2BMPSTRING, 0), "asc2bmpstring" },
2N/A { ERR_PACK(0, SUNW_F_UTF82ASCSTR, 0), "utf82ascstr" },
2N/A { ERR_PACK(0, SUNW_F_FINDATTR, 0), "findattr" },
2N/A { ERR_PACK(0, SUNW_F_TYPE2ATTRIB, 0), "type2attrib" },
2N/A { ERR_PACK(0, SUNW_F_MOVE_CERTS, 0), "move_certs" },
2N/A { ERR_PACK(0, SUNW_F_FIND_FNAME, 0), "sunw_find_fname" },
2N/A { ERR_PACK(0, SUNW_F_PARSE_OUTER, 0), "parse_outer" },
2N/A { ERR_PACK(0, SUNW_F_CHECKFILE, 0), "checkfile" },
2N/A { 0, NULL }
2N/A};
2N/A
2N/A/* Error codes and their matching strings */
2N/Astatic ERR_STRING_DATA SUNW_str_reasons[] = {
2N/A { SUNW_R_INVALID_ARG, "invalid argument" },
2N/A { SUNW_R_MEMORY_FAILURE, "memory failure" },
2N/A { SUNW_R_MAC_VERIFY_FAILURE, "mac verify failure" },
2N/A { SUNW_R_MAC_CREATE_FAILURE, "mac create failure" },
2N/A { SUNW_R_BAD_FILETYPE, "bad file type" },
2N/A { SUNW_R_BAD_PKEY, "bad or missing private key" },
2N/A { SUNW_R_BAD_PKEYTYPE, "unsupported key type" },
2N/A { SUNW_R_PKEY_READ_ERR, "unable to read private key" },
2N/A { SUNW_R_NO_TRUST_ANCHOR, "no trust anchors found" },
2N/A { SUNW_R_READ_TRUST_ERR, "unable to read trust anchor" },
2N/A { SUNW_R_ADD_TRUST_ERR, "unable to add trust anchor" },
2N/A { SUNW_R_PKCS12_PARSE_ERR, "PKCS12 parse error" },
2N/A { SUNW_R_PKCS12_CREATE_ERR, "PKCS12 create error" },
2N/A { SUNW_R_BAD_CERTTYPE, "unsupported certificate type" },
2N/A { SUNW_R_PARSE_CERT_ERR, "error parsing PKCS12 certificate" },
2N/A { SUNW_R_PARSE_BAG_ERR, "error parsing PKCS12 bag" },
2N/A { SUNW_R_MAKE_BAG_ERR, "error making PKCS12 bag" },
2N/A { SUNW_R_BAD_LKID, "bad localKeyID format" },
2N/A { SUNW_R_SET_LKID_ERR, "error setting localKeyID" },
2N/A { SUNW_R_BAD_FNAME, "bad friendlyName format" },
2N/A { SUNW_R_SET_FNAME_ERR, "error setting friendlyName" },
2N/A { SUNW_R_BAD_TRUST, "bad or missing trust anchor" },
2N/A { SUNW_R_BAD_BAGTYPE, "unsupported bag type" },
2N/A { SUNW_R_CERT_ERR, "certificate error" },
2N/A { SUNW_R_PKEY_ERR, "private key error" },
2N/A { SUNW_R_READ_ERR, "error reading file" },
2N/A { SUNW_R_ADD_ATTR_ERR, "error adding attribute" },
2N/A { SUNW_R_STR_CONVERT_ERR, "error converting string" },
2N/A { SUNW_R_PKCS12_EMPTY_ERR, "empty PKCS12 structure" },
2N/A { SUNW_R_PASSWORD_ERR, "bad password" },
2N/A { 0, NULL }
2N/A};
2N/A
2N/A/*
2N/A * The library name that our module will be known as. This name
2N/A * may be retrieved via OpenSSLs error APIs.
2N/A */
2N/Astatic ERR_STRING_DATA SUNW_lib_name[] = {
2N/A { 0, SUNW_LIB_NAME },
2N/A { 0, NULL }
2N/A};
2N/A#endif
2N/A
2N/A/*
2N/A * The value of this variable (initialized by a call to
2N/A * ERR_load_SUNW_strings()) is what identifies our errors
2N/A * to OpenSSL as being ours.
2N/A */
2N/Astatic int SUNW_lib_error_code = 0;
2N/A
2N/A/* local routines */
2N/Astatic int parse_pkcs12(PKCS12 *, const char *, int, char *, int, char *,
2N/A EVP_PKEY **, X509 **, STACK_OF(X509) **);
2N/Astatic int pem_info(FILE *, pem_password_cb, void *,
2N/A STACK_OF(EVP_PKEY) **, STACK_OF(X509) **);
2N/A
2N/Astatic int parse_outer(PKCS12 *, const char *, STACK_OF(EVP_PKEY) *,
2N/A STACK_OF(X509) *);
2N/A
2N/Astatic int parse_all_bags(STACK_OF(PKCS12_SAFEBAG) *, const char *,
2N/A STACK_OF(EVP_PKEY) *, STACK_OF(X509) *);
2N/A
2N/Astatic int parse_one_bag(PKCS12_SAFEBAG *, const char *,
2N/A STACK_OF(EVP_PKEY) *, STACK_OF(X509) *);
2N/A
2N/Astatic X509_ATTRIBUTE *type2attrib(ASN1_TYPE *, int);
2N/Astatic ASN1_TYPE *attrib2type(X509_ATTRIBUTE *);
2N/Astatic uchar_t *utf82ascstr(ASN1_UTF8STRING *);
2N/Astatic ASN1_BMPSTRING *asc2bmpstring(const char *, int);
2N/Astatic int find_attr_by_nid(STACK_OF(X509_ATTRIBUTE) *, int);
2N/Astatic int find_attr(int, ASN1_STRING *, STACK_OF(EVP_PKEY) *,
2N/A EVP_PKEY **, STACK_OF(X509) *, X509 **);
2N/A
2N/Astatic chk_errs_t check_time(chk_actions_t, X509 *);
2N/Astatic int get_key_cert(int, STACK_OF(EVP_PKEY) *, EVP_PKEY **,
2N/A STACK_OF(X509) *, X509 **cert);
2N/Astatic int move_certs(STACK_OF(X509) *, STACK_OF(X509) *);
2N/Astatic int sunw_append_keys(STACK_OF(EVP_PKEY) *,
2N/A STACK_OF(EVP_PKEY) *);
2N/Astatic int set_results(STACK_OF(EVP_PKEY) **,
2N/A STACK_OF(EVP_PKEY) **, STACK_OF(X509) **, STACK_OF(X509) **,
2N/A STACK_OF(X509) **, STACK_OF(X509) **,
2N/A STACK_OF(EVP_PKEY) **, STACK_OF(EVP_PKEY) **);
2N/A
2N/A/*
2N/A * ----------------------------------------------------------------------------
2N/A * Public routines
2N/A * ----------------------------------------------------------------------------
2N/A */
2N/A
2N/A/*
2N/A * sunw_PKCS12_parse - Parse a PKCS12 structure and break it into its parts.
2N/A *
2N/A * Parse and decrypt a PKCS#12 structure returning user key, user cert and/or
2N/A * other (CA) certs. Note either ca should be NULL, *ca should be NULL,
2N/A * or it should point to a valid STACK_OF(X509) structure. pkey and cert can
2N/A * be passed uninitialized.
2N/A *
2N/A * Arguments:
2N/A * p12 - Structure with pkcs12 info to be parsed
2N/A * pass - Pass phrase for the private key (possibly empty) or NULL if
2N/A * there is none.
2N/A * matchty - Info about which certs/keys to return if many are in the file.
2N/A * keyid - If private key localkeyids friendlynames are to match a
2N/A * predetermined value, the value to match. This value should
2N/A * be an octet string.
2N/A * keyid_len- Length of the keyid byte string.
2N/A * name_str - If friendlynames are to match a predetermined value, the value
2N/A * to match. This value should be a NULL terminated string.
2N/A * pkey - Points to location pointing to the private key returned.
2N/A * cert - Points to locaiton which points to the client cert returned
2N/A * ca - Points to location that points to a stack of 'certificate
2N/A * authority' certs/trust anchors.
2N/A *
2N/A * Match based on the value of 'matchty' and the contents of 'keyid'
2N/A * and/or 'name_str', as appropriate. Go through the lists of certs and
2N/A * private keys which were taken from the pkcs12 structure, looking for
2N/A * matches of the requested type. This function only searches the lists of
2N/A * matching private keys and client certificates. Kinds of matches allowed,
2N/A * and the order in which they will be checked, are:
2N/A *
2N/A * 1) Find the key and/or cert whose localkeyid attributes matches
2N/A * 'keyid'.
2N/A * 2) Find the key and/or cert whose friendlyname attributes matches
2N/A * 'name_str'
2N/A * 3) Return the first matching key/cert pair found.
2N/A * 4) Return the last matching key/cert pair found.
2N/A * 5) Return whatever cert and/or key are available, even unmatching.
2N/A *
2N/A * Append to the CA list, the certs which do not have matching private
2N/A * keys and which were not selected.
2N/A *
2N/A * If none of the bits are set, no client certs or private keys will be
2N/A * returned. CA (aka trust anchor) certs can be.
2N/A *
2N/A * Notes: If #3 is selected, then #4 will never occur. CA certs will be
2N/A * selected after a cert/key pairs are isolated.
2N/A *
2N/A * Returns:
2N/A * < 0 - An error returned. Call ERR_get_error() to get errors information.
2N/A * Where possible, memory has been freed.
2N/A * >= 0 - Objects were found and returned. Which objects are indicated by
2N/A * which bits are set (FOUND_PKEY, FOUND_CERT, FOUND_CA_CERTS).
2N/A */
2N/Aint
2N/Asunw_PKCS12_parse(PKCS12 *p12, const char *pass, int matchty, char *keyid,
2N/A int keyid_len, char *name_str, EVP_PKEY **pkey, X509 **cert,
2N/A STACK_OF(X509) **ca)
2N/A{
2N/A boolean_t ca_supplied;
2N/A int retval = -1;
2N/A
2N/A /* If NULL PKCS12 structure, this is an error */
2N/A if (p12 == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_PARSE, SUNW_R_INVALID_ARG);
2N/A return (-1);
2N/A }
2N/A
2N/A /* Set up arguments.... These will be allocated if needed */
2N/A if (pkey)
2N/A *pkey = NULL;
2N/A if (cert)
2N/A *cert = NULL;
2N/A
2N/A /*
2N/A * If there is already a ca list, use it. Otherwise, allocate one
2N/A * and free is later if an error occurs or whatever.)
2N/A */
2N/A ca_supplied = (ca != NULL && *ca != NULL);
2N/A if (ca != NULL && *ca == NULL) {
2N/A if ((*ca = sk_X509_new_null()) == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_PARSE, SUNW_R_MEMORY_FAILURE);
2N/A return (-1);
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * If password is zero length or NULL then try verifying both cases
2N/A * to determine which password is correct. The reason for this is that
2N/A * under PKCS#12 password based encryption no password and a zero
2N/A * length password are two different things. If the password has a
2N/A * non-zero length and is not NULL then call PKCS12_verify_mac() with
2N/A * a length of '-1' and let it use strlen() to figure out the length
2N/A * of the password.
2N/A */
2N/A /* Check the mac */
2N/A if (pass == NULL || *pass == '\0') {
2N/A if (PKCS12_verify_mac(p12, NULL, 0))
2N/A pass = NULL;
2N/A else if (PKCS12_verify_mac(p12, "", 0))
2N/A pass = "";
2N/A else {
2N/A SUNWerr(SUNW_F_PKCS12_PARSE,
2N/A SUNW_R_MAC_VERIFY_FAILURE);
2N/A goto err;
2N/A }
2N/A } else if (PKCS12_verify_mac(p12, pass, -1) == 0) {
2N/A SUNWerr(SUNW_F_PKCS12_PARSE, SUNW_R_MAC_VERIFY_FAILURE);
2N/A goto err;
2N/A }
2N/A
2N/A retval = parse_pkcs12(p12, pass, matchty, keyid, keyid_len,
2N/A name_str, pkey, cert, ca);
2N/A if (retval < 0) {
2N/A SUNWerr(SUNW_F_PKCS12_PARSE, SUNW_R_PKCS12_PARSE_ERR);
2N/A goto err;
2N/A }
2N/A return (retval);
2N/A
2N/Aerr:
2N/A if (pkey && *pkey) {
2N/A sunw_evp_pkey_free(*pkey);
2N/A }
2N/A if (cert && *cert)
2N/A X509_free(*cert);
2N/A if (ca_supplied == B_FALSE && ca != NULL)
2N/A sk_X509_pop_free(*ca, X509_free);
2N/A
2N/A return (-1);
2N/A
2N/A}
2N/A
2N/A
2N/A/*
2N/A * sunw_PEM_contents() parses a PEM file and returns component parts found
2N/A *
2N/A * Parse and decrypt a PEM file, returning any user keys and certs.
2N/A *
2N/A * There are some limits to this function. It will ignore the following:
2N/A * - certificates identified by "TRUSTED CERTIFICATE"
2N/A * - CERTIFICATE REQUEST and NEW CERTIFICATE REQUEST records.
2N/A * - X509 CRL
2N/A * - DH PARAMETERS
2N/A * - DSA PARAMETERS
2N/A * - Any PUBLIC KEY
2N/A * - PKCS7
2N/A * - PRIVATE KEY or ENCRYPTED PRIVATE KEY (PKCS 8)
2N/A *
2N/A * Arguments:
2N/A * fp - File pointer for file containing PEM data.
2N/A * pass - Pass phrase for the private key or NULL if there is none.
2N/A * pkeys - Points to address of a stack of private keys to return.
2N/A * certs - Points to address of a stack of client certs to return.
2N/A *
2N/A * The pointers to stacks should either be NULL or their contents should
2N/A * either be NULL or should point to a valid STACK_OF(X509) structure.
2N/A * If the stacks contain information, corresponding information from the
2N/A * file will be appended to the original contents.
2N/A *
2N/A * Note: Client certs and and their matching private keys will be in any
2N/A * order.
2N/A *
2N/A * Certs which have no matching private key are assumed to be ca certs.
2N/A *
2N/A * Returns:
2N/A * < 0 - An error returned. Call ERR_get_error() to get errors information.
2N/A * Where possible, memory has been freed.
2N/A * >= 0 - Objects were found and returned. Which objects are indicated by
2N/A * which bits are set (FOUND_PKEY, FOUND_CERT)
2N/A */
2N/Aint sunw_PEM_contents(FILE *fp, pem_password_cb *cb, void *userdata,
2N/A STACK_OF(EVP_PKEY) **pkey, STACK_OF(X509) **certs)
2N/A{
2N/A STACK_OF(EVP_PKEY) *work_kl = NULL;
2N/A STACK_OF(X509) *work_ca = NULL;
2N/A int retval = -1;
2N/A
2N/A /*
2N/A * Allocate the working stacks for private key and for the
2N/A * ca certs.
2N/A */
2N/A if ((work_kl = sk_EVP_PKEY_new_null()) == NULL) {
2N/A SUNWerr(SUNW_F_PEM_CONTENTS, SUNW_R_MEMORY_FAILURE);
2N/A goto cleanup;
2N/A }
2N/A
2N/A if ((work_ca = sk_X509_new_null()) == NULL) {
2N/A SUNWerr(SUNW_F_PEM_CONTENTS, SUNW_R_MEMORY_FAILURE);
2N/A goto cleanup;
2N/A }
2N/A
2N/A /* Error strings are set within the following. */
2N/A if (pem_info(fp, cb, userdata, &work_kl, &work_ca) <= 0) {
2N/A goto cleanup;
2N/A }
2N/A
2N/A /* on error, set_results() returns an error on the stack */
2N/A retval = set_results(pkey, &work_kl, certs, &work_ca, NULL, NULL, NULL,
2N/A NULL);
2N/Acleanup:
2N/A if (work_kl != NULL) {
2N/A sk_EVP_PKEY_pop_free(work_kl, sunw_evp_pkey_free);
2N/A }
2N/A if (work_ca != NULL)
2N/A sk_X509_pop_free(work_ca, X509_free);
2N/A
2N/A return (retval);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * sunw_PKCS12_contents() parses a pkcs#12 structure and returns component
2N/A * parts found, without evaluation.
2N/A *
2N/A * Parse and decrypt a PKCS#12 structure returning any user keys and/or
2N/A * various certs. Note these should either be NULL, *whatever should
2N/A * be NULL, or it should point to a valid STACK_OF(X509) structure.
2N/A *
2N/A * Arguments:
2N/A * p12 - Structure with pkcs12 info to be parsed
2N/A * pass - Pass phrase for the private key and entire pkcs12 wad (possibly
2N/A * empty) or NULL if there is none.
2N/A * pkeys - Points to address of a stack of private keys to return.
2N/A * certs - Points to address of a stack of client certs return.
2N/A *
2N/A * Note: The certs and keys being returned are in random order.
2N/A *
2N/A * Returns:
2N/A * < 0 - An error returned. Call ERR_get_error() to get errors information.
2N/A * Where possible, memory has been freed.
2N/A * >= 0 - Objects were found and returned. Which objects are indicated by
2N/A * which bits are set (FOUND_PKEY or FOUND_CERT)
2N/A */
2N/Aint
2N/Asunw_PKCS12_contents(PKCS12 *p12, const char *pass, STACK_OF(EVP_PKEY) **pkey,
2N/A STACK_OF(X509) **certs)
2N/A{
2N/A STACK_OF(EVP_PKEY) *work_kl = NULL;
2N/A STACK_OF(X509) *work_ca = NULL;
2N/A int retval = -1;
2N/A
2N/A /*
2N/A * Allocate the working stacks for private key and for the
2N/A * ca certs.
2N/A */
2N/A if ((work_kl = sk_EVP_PKEY_new_null()) == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_CONTENTS, SUNW_R_MEMORY_FAILURE);
2N/A goto cleanup;
2N/A }
2N/A
2N/A if ((work_ca = sk_X509_new_null()) == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_CONTENTS, SUNW_R_MEMORY_FAILURE);
2N/A goto cleanup;
2N/A }
2N/A
2N/A if (parse_outer(p12, pass, work_kl, work_ca) == 0) {
2N/A /*
2N/A * Error already on stack
2N/A */
2N/A goto cleanup;
2N/A }
2N/A
2N/A /* on error, set_results() returns an error on the stack */
2N/A retval = set_results(pkey, &work_kl, certs, &work_ca, NULL,
2N/A NULL, NULL, NULL);
2N/A
2N/Acleanup:
2N/A if (work_kl != NULL) {
2N/A sk_EVP_PKEY_pop_free(work_kl, sunw_evp_pkey_free);
2N/A }
2N/A
2N/A return (retval);
2N/A}
2N/A
2N/A
2N/A
2N/A/*
2N/A * sunw_split_certs() - Given a list of certs and a list of private keys,
2N/A * moves certs which match one of the keys to a different stack.
2N/A *
2N/A * Arguments:
2N/A * allkeys - Points to a stack of private keys to search.
2N/A * allcerts - Points to a stack of certs to be searched.
2N/A * keycerts - Points to address of a stack of certs with matching private
2N/A * keys. They are moved from 'allcerts'. This may not be NULL
2N/A * when called. If *keycerts is NULL upon entry, a new stack will
2N/A * be allocated. Otherwise, it must be a valid STACK_OF(509).
2N/A * nocerts - Points to address of a stack for keys which have no matching
2N/A * certs. Keys are moved from 'allkeys' here when they have no
2N/A * matching certs. If this is NULL, matchless keys will be
2N/A * discarded.
2N/A *
2N/A * Notes: If an error occurs while moving certs, the cert being move may be
2N/A * lost. 'keycerts' may only contain part of the matching certs. The number
2N/A * of certs successfully moved can be found by checking sk_X509_num(keycerts).
2N/A *
2N/A * If there is a key which does not have a matching cert, it is moved to
2N/A * the list nocerts.
2N/A *
2N/A * If all certs are removed from 'certs' and/or 'pkeys', it will be the
2N/A * caller's responsibility to free the empty stacks.
2N/A *
2N/A * Returns:
2N/A * < 0 - An error returned. Call ERR_get_error() to get errors information.
2N/A * Where possible, memory has been freed.
2N/A * >= 0 - The number of certs moved from 'cert' to 'pkcerts'.
2N/A */
2N/Aint
2N/Asunw_split_certs(STACK_OF(EVP_PKEY) *allkeys, STACK_OF(X509) *allcerts,
2N/A STACK_OF(X509) **keycerts, STACK_OF(EVP_PKEY) **nocerts)
2N/A{
2N/A STACK_OF(X509) *matching;
2N/A STACK_OF(EVP_PKEY) *nomatch;
2N/A EVP_PKEY *tmpkey;
2N/A X509 *tmpcert;
2N/A int count = 0;
2N/A int found;
2N/A int res;
2N/A int i;
2N/A int k;
2N/A
2N/A *keycerts = NULL;
2N/A if (nocerts != NULL)
2N/A *nocerts = NULL;
2N/A nomatch = NULL;
2N/A
2N/A if ((matching = sk_X509_new_null()) == NULL) {
2N/A SUNWerr(SUNW_F_SPLIT_CERTS, SUNW_R_MEMORY_FAILURE);
2N/A return (-1);
2N/A }
2N/A *keycerts = matching;
2N/A
2N/A k = 0;
2N/A while (k < sk_EVP_PKEY_num(allkeys)) {
2N/A found = 0;
2N/A tmpkey = sk_EVP_PKEY_value(allkeys, k);
2N/A
2N/A for (i = 0; i < sk_X509_num(allcerts); i++) {
2N/A tmpcert = sk_X509_value(allcerts, i);
2N/A res = X509_check_private_key(tmpcert, tmpkey);
2N/A if (res != 0) {
2N/A count++;
2N/A found = 1;
2N/A tmpcert = sk_X509_delete(allcerts, i);
2N/A if (sk_X509_push(matching, tmpcert) == 0) {
2N/A X509_free(tmpcert);
2N/A SUNWerr(SUNW_F_SPLIT_CERTS,
2N/A SUNW_R_MEMORY_FAILURE);
2N/A return (-1);
2N/A }
2N/A break;
2N/A }
2N/A }
2N/A if (found != 0) {
2N/A /*
2N/A * Found a match - keep the key & check out the next
2N/A * one.
2N/A */
2N/A k++;
2N/A } else {
2N/A /*
2N/A * No cert matching this key. Move the key if
2N/A * possible or discard it. Don't increment the
2N/A * index.
2N/A */
2N/A if (nocerts == NULL) {
2N/A tmpkey = sk_EVP_PKEY_delete(allkeys, k);
2N/A sunw_evp_pkey_free(tmpkey);
2N/A } else {
2N/A if (*nocerts == NULL) {
2N/A nomatch = sk_EVP_PKEY_new_null();
2N/A if (nomatch == NULL) {
2N/A SUNWerr(SUNW_F_SPLIT_CERTS,
2N/A SUNW_R_MEMORY_FAILURE);
2N/A return (-1);
2N/A }
2N/A *nocerts = nomatch;
2N/A }
2N/A tmpkey = sk_EVP_PKEY_delete(allkeys, k);
2N/A if (sk_EVP_PKEY_push(nomatch, tmpkey) == 0) {
2N/A sunw_evp_pkey_free(tmpkey);
2N/A SUNWerr(SUNW_F_SPLIT_CERTS,
2N/A SUNW_R_MEMORY_FAILURE);
2N/A return (-1);
2N/A }
2N/A }
2N/A }
2N/A }
2N/A
2N/A return (count);
2N/A}
2N/A
2N/A/*
2N/A * sunw_PKCS12_create() creates a pkcs#12 structure and given component parts.
2N/A *
2N/A * Given one or more of user private key, user cert and/or other (CA) certs,
2N/A * return an encrypted PKCS12 structure containing them.
2N/A *
2N/A * Arguments:
2N/A * pass - Pass phrase for the pkcs12 structure and private key (possibly
2N/A * empty) or NULL if there is none. It will be used to encrypt
2N/A * both the private key(s) and as the pass phrase for the whole
2N/A * pkcs12 wad.
2N/A * pkeys - Points to stack of private keys.
2N/A * certs - Points to stack of client (public ke) certs
2N/A * cacerts - Points to stack of 'certificate authority' certs (or trust
2N/A * anchors).
2N/A *
2N/A * Note that any of these may be NULL.
2N/A *
2N/A * Returns:
2N/A * NULL - An error occurred.
2N/A * != NULL - Address of PKCS12 structure. The user is responsible for
2N/A * freeing the memory when done.
2N/A */
2N/APKCS12 *
2N/Asunw_PKCS12_create(const char *pass, STACK_OF(EVP_PKEY) *pkeys,
2N/A STACK_OF(X509) *certs, STACK_OF(X509) *cacerts)
2N/A{
2N/A int nid_cert = NID_pbe_WithSHA1And40BitRC2_CBC;
2N/A int nid_key = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
2N/A STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
2N/A STACK_OF(PKCS7) *safes = NULL;
2N/A PKCS12_SAFEBAG *bag = NULL;
2N/A PKCS8_PRIV_KEY_INFO *p8 = NULL;
2N/A EVP_PKEY *pkey = NULL;
2N/A PKCS12 *ret_p12 = NULL;
2N/A PKCS12 *p12 = NULL;
2N/A PKCS7 *authsafe = NULL;
2N/A X509 *cert = NULL;
2N/A uchar_t *str = NULL;
2N/A int certs_there = 0;
2N/A int keys_there = 0;
2N/A int len;
2N/A int i;
2N/A
2N/A if ((safes = sk_PKCS7_new_null()) == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE, SUNW_R_MEMORY_FAILURE);
2N/A return (NULL);
2N/A }
2N/A
2N/A if ((bags = sk_PKCS12_SAFEBAG_new_null()) == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE, SUNW_R_MEMORY_FAILURE);
2N/A goto err_ret;
2N/A }
2N/A
2N/A if (certs != NULL && sk_X509_num(certs) > 0) {
2N/A
2N/A for (i = 0; i < sk_X509_num(certs); i++) {
2N/A cert = sk_X509_value(certs, i);
2N/A
2N/A /* Add user certificate */
2N/A if ((bag = M_PKCS12_x5092certbag(cert)) == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE, SUNW_R_CERT_ERR);
2N/A goto err_ret;
2N/A }
2N/A if (cert->aux != NULL && cert->aux->alias != NULL &&
2N/A cert->aux->alias->type == V_ASN1_UTF8STRING) {
2N/A str = utf82ascstr(cert->aux->alias);
2N/A if (str == NULL) {
2N/A /*
2N/A * Error already on stack
2N/A */
2N/A goto err_ret;
2N/A }
2N/A if (PKCS12_add_friendlyname_asc(bag,
2N/A (char const *) str,
2N/A strlen((char const *) str)) == 0) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE,
2N/A SUNW_R_ADD_ATTR_ERR);
2N/A goto err_ret;
2N/A }
2N/A }
2N/A if (cert->aux != NULL && cert->aux->keyid != NULL &&
2N/A cert->aux->keyid->type == V_ASN1_OCTET_STRING) {
2N/A str = cert->aux->keyid->data;
2N/A len = cert->aux->keyid->length;
2N/A
2N/A if (str != NULL &&
2N/A PKCS12_add_localkeyid(bag, str, len) == 0) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE,
2N/A SUNW_R_ADD_ATTR_ERR);
2N/A goto err_ret;
2N/A }
2N/A }
2N/A if (sk_PKCS12_SAFEBAG_push(bags, bag) == 0) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE,
2N/A SUNW_R_MEMORY_FAILURE);
2N/A goto err_ret;
2N/A }
2N/A certs_there++;
2N/A bag = NULL;
2N/A }
2N/A }
2N/A
2N/A if (cacerts != NULL && sk_X509_num(cacerts) > 0) {
2N/A
2N/A /* Put all certs in structure */
2N/A for (i = 0; i < sk_X509_num(cacerts); i++) {
2N/A cert = sk_X509_value(cacerts, i);
2N/A if ((bag = M_PKCS12_x5092certbag(cert)) == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE, SUNW_R_CERT_ERR);
2N/A goto err_ret;
2N/A }
2N/A
2N/A if (cert->aux != NULL && cert->aux->alias != NULL &&
2N/A cert->aux->alias->type == V_ASN1_UTF8STRING) {
2N/A str = utf82ascstr(cert->aux->alias);
2N/A if (str == NULL) {
2N/A /*
2N/A * Error already on stack
2N/A */
2N/A goto err_ret;
2N/A }
2N/A if (PKCS12_add_friendlyname_asc(
2N/A bag, (char const *) str,
2N/A strlen((char const *) str)) == 0) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE,
2N/A SUNW_R_ADD_ATTR_ERR);
2N/A goto err_ret;
2N/A }
2N/A }
2N/A if (cert->aux != NULL && cert->aux->keyid != NULL &&
2N/A cert->aux->keyid->type == V_ASN1_OCTET_STRING) {
2N/A str = cert->aux->keyid->data;
2N/A len = cert->aux->keyid->length;
2N/A
2N/A if (str != NULL &&
2N/A PKCS12_add_localkeyid(bag, str, len) == 0) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE,
2N/A SUNW_R_ADD_ATTR_ERR);
2N/A goto err_ret;
2N/A }
2N/A }
2N/A if (sk_PKCS12_SAFEBAG_push(bags, bag) == 0) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE,
2N/A SUNW_R_MEMORY_FAILURE);
2N/A goto err_ret;
2N/A }
2N/A certs_there++;
2N/A bag = NULL;
2N/A }
2N/A }
2N/A
2N/A if (certs != NULL || cacerts != NULL && certs_there) {
2N/A /* Turn certbags into encrypted authsafe */
2N/A authsafe = PKCS12_pack_p7encdata(nid_cert, pass, -1,
2N/A NULL, 0, PKCS12_DEFAULT_ITER, bags);
2N/A if (authsafe == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE, SUNW_R_CERT_ERR);
2N/A goto err_ret;
2N/A }
2N/A sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
2N/A bags = NULL;
2N/A
2N/A if (sk_PKCS7_push(safes, authsafe) == 0) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE, SUNW_R_MEMORY_FAILURE);
2N/A goto err_ret;
2N/A }
2N/A authsafe = NULL;
2N/A }
2N/A
2N/A if (pkeys != NULL && sk_EVP_PKEY_num(pkeys) > 0) {
2N/A
2N/A if (bags == NULL &&
2N/A (bags = sk_PKCS12_SAFEBAG_new_null()) == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE, SUNW_R_MEMORY_FAILURE);
2N/A goto err_ret;
2N/A }
2N/A
2N/A for (i = 0; i < sk_EVP_PKEY_num(pkeys); i++) {
2N/A
2N/A pkey = sk_EVP_PKEY_value(pkeys, i);
2N/A
2N/A /* Make a shrouded key bag */
2N/A if ((p8 = EVP_PKEY2PKCS8(pkey)) == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE, SUNW_R_PKEY_ERR);
2N/A goto err_ret;
2N/A }
2N/A
2N/A bag = PKCS12_MAKE_SHKEYBAG(nid_key, pass, -1, NULL, 0,
2N/A PKCS12_DEFAULT_ITER, p8);
2N/A if (bag == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE,
2N/A SUNW_R_MAKE_BAG_ERR);
2N/A goto err_ret;
2N/A }
2N/A PKCS8_PRIV_KEY_INFO_free(p8);
2N/A p8 = NULL;
2N/A
2N/A len = sunw_get_pkey_fname(GETDO_COPY, pkey,
2N/A (char **)&str);
2N/A if (str != NULL) {
2N/A if (PKCS12_add_friendlyname_asc(bag,
2N/A (const char *)str, len) == 0) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE,
2N/A SUNW_R_ADD_ATTR_ERR);
2N/A goto err_ret;
2N/A }
2N/A }
2N/A str = NULL;
2N/A
2N/A len = sunw_get_pkey_localkeyid(GETDO_COPY, pkey,
2N/A (char **)&str, &len);
2N/A if (str != NULL) {
2N/A if (PKCS12_add_localkeyid(bag, str, len) == 0) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE,
2N/A SUNW_R_ADD_ATTR_ERR);
2N/A goto err_ret;
2N/A }
2N/A }
2N/A str = NULL;
2N/A
2N/A if (sk_PKCS12_SAFEBAG_push(bags, bag) == 0) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE,
2N/A SUNW_R_MEMORY_FAILURE);
2N/A goto err_ret;
2N/A }
2N/A keys_there++;
2N/A bag = NULL;
2N/A }
2N/A
2N/A if (keys_there) {
2N/A /* Turn into unencrypted authsafe */
2N/A authsafe = PKCS12_pack_p7data(bags);
2N/A if (authsafe == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE,
2N/A SUNW_R_PKCS12_CREATE_ERR);
2N/A goto err_ret;
2N/A }
2N/A sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
2N/A bags = NULL;
2N/A
2N/A if (sk_PKCS7_push(safes, authsafe) == 0) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE,
2N/A SUNW_R_MEMORY_FAILURE);
2N/A }
2N/A authsafe = NULL;
2N/A }
2N/A }
2N/A
2N/A if (certs_there == 0 && keys_there == 0) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE, SUNW_R_PKCS12_EMPTY_ERR);
2N/A goto err_ret;
2N/A }
2N/A
2N/A if ((p12 = PKCS12_init(NID_pkcs7_data)) == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE, SUNW_R_PKCS12_CREATE_ERR);
2N/A goto err_ret;
2N/A }
2N/A
2N/A /*
2N/A * Note that safes is copied by the following. Therefore, it needs
2N/A * to be freed whether or not the following succeeds.
2N/A */
2N/A if (M_PKCS12_pack_authsafes(p12, safes) == 0) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE, SUNW_R_PKCS12_CREATE_ERR);
2N/A goto err_ret;
2N/A }
2N/A if (PKCS12_set_mac(p12, pass, -1, NULL, 0, 2048, NULL) == 0) {
2N/A SUNWerr(SUNW_F_PKCS12_CREATE, SUNW_R_MAC_CREATE_FAILURE);
2N/A goto err_ret;
2N/A }
2N/A
2N/A ret_p12 = p12;
2N/A p12 = NULL;
2N/A
2N/A /* Fallthrough is intentional */
2N/A
2N/Aerr_ret:
2N/A
2N/A if (str != NULL)
2N/A free(str);
2N/A
2N/A if (p8 != NULL)
2N/A PKCS8_PRIV_KEY_INFO_free(p8);
2N/A
2N/A if (bag != NULL)
2N/A PKCS12_SAFEBAG_free(bag);
2N/A if (bags != NULL)
2N/A sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
2N/A if (authsafe != NULL)
2N/A PKCS7_free(authsafe);
2N/A if (safes != NULL)
2N/A sk_PKCS7_pop_free(safes, PKCS7_free);
2N/A if (p12 != NULL)
2N/A PKCS12_free(p12);
2N/A
2N/A return (ret_p12);
2N/A}
2N/A
2N/A/*
2N/A * sunw_evp_pkey_free() Given an EVP_PKEY structure, free any attributes
2N/A * that are attached. Then free the EVP_PKEY itself.
2N/A *
2N/A * This is a replacement for EVP_PKEY_free() for the sunw stuff.
2N/A * It should be used in places where EVP_PKEY_free would be used,
2N/A * including calls to sk_EVP_PKEY_pop_free().
2N/A *
2N/A * Arguments:
2N/A * pkey - Entry which potentially has attributes to be freed.
2N/A *
2N/A * Returns:
2N/A * None.
2N/A */
2N/Avoid
2N/Asunw_evp_pkey_free(EVP_PKEY *pkey)
2N/A{
2N/A if (pkey != NULL) {
2N/A if (pkey->attributes != NULL) {
2N/A sk_X509_ATTRIBUTE_pop_free(pkey->attributes,
2N/A X509_ATTRIBUTE_free);
2N/A pkey->attributes = NULL;
2N/A }
2N/A EVP_PKEY_free(pkey);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * sunw_set_localkeyid() sets the localkeyid in a cert, a private key or
2N/A * both. Any existing localkeyid will be discarded.
2N/A *
2N/A * Arguments:
2N/A * keyid_str- A byte string with the localkeyid to set
2N/A * keyid_len- Length of the keyid byte string.
2N/A * pkey - Points to a private key to set the keyidstr in.
2N/A * cert - Points to a cert to set the keyidstr in.
2N/A *
2N/A * Note that setting a keyid into a cert which will not be written out as
2N/A * a PKCS12 cert is pointless since it will be lost.
2N/A *
2N/A * Returns:
2N/A * 0 - Success.
2N/A * < 0 - An error occurred. It was probably an error in allocating
2N/A * memory. The error will be set in the error stack. Call
2N/A * ERR_get_error() to get specific information.
2N/A */
2N/Aint
2N/Asunw_set_localkeyid(const char *keyid_str, int keyid_len, EVP_PKEY *pkey,
2N/A X509 *cert)
2N/A{
2N/A X509_ATTRIBUTE *attr = NULL;
2N/A ASN1_STRING *str = NULL;
2N/A ASN1_TYPE *keyid = NULL;
2N/A int retval = -1;
2N/A int i;
2N/A
2N/A if (cert != NULL) {
2N/A if (X509_keyid_set1(cert, (uchar_t *)keyid_str, keyid_len)
2N/A == 0) {
2N/A SUNWerr(SUNW_F_SET_LOCALKEYID, SUNW_R_SET_LKID_ERR);
2N/A goto cleanup;
2N/A }
2N/A }
2N/A if (pkey != NULL) {
2N/A str = (ASN1_STRING *)M_ASN1_OCTET_STRING_new();
2N/A if (str == NULL ||
2N/A M_ASN1_OCTET_STRING_set(str, keyid_str, keyid_len) == 0 ||
2N/A (keyid = ASN1_TYPE_new()) == NULL) {
2N/A SUNWerr(SUNW_F_SET_LOCALKEYID, SUNW_R_MEMORY_FAILURE);
2N/A goto cleanup;
2N/A }
2N/A
2N/A ASN1_TYPE_set(keyid, V_ASN1_OCTET_STRING, str);
2N/A str = NULL;
2N/A
2N/A attr = type2attrib(keyid, NID_localKeyID);
2N/A if (attr == NULL) {
2N/A /*
2N/A * Error already on stack
2N/A */
2N/A goto cleanup;
2N/A }
2N/A keyid = NULL;
2N/A
2N/A if (pkey->attributes == NULL) {
2N/A pkey->attributes = sk_X509_ATTRIBUTE_new_null();
2N/A if (pkey->attributes == NULL) {
2N/A SUNWerr(SUNW_F_SET_LOCALKEYID,
2N/A SUNW_R_MEMORY_FAILURE);
2N/A goto cleanup;
2N/A }
2N/A } else {
2N/A i = find_attr_by_nid(pkey->attributes, NID_localKeyID);
2N/A if (i >= 0)
2N/A sk_X509_ATTRIBUTE_delete(pkey->attributes, i);
2N/A }
2N/A if (sk_X509_ATTRIBUTE_push(pkey->attributes, attr) == 0) {
2N/A SUNWerr(SUNW_F_SET_LOCALKEYID, SUNW_R_MEMORY_FAILURE);
2N/A goto cleanup;
2N/A }
2N/A attr = NULL;
2N/A }
2N/A retval = 0;
2N/A
2N/Acleanup:
2N/A if (str != NULL)
2N/A ASN1_STRING_free(str);
2N/A if (keyid != NULL)
2N/A ASN1_TYPE_free(keyid);
2N/A if (attr != NULL)
2N/A X509_ATTRIBUTE_free(attr);
2N/A
2N/A return (retval);
2N/A}
2N/A
2N/A/*
2N/A * sunw_get_pkey_localkeyid() gets the localkeyid from a private key. It can
2N/A * optionally remove the value found.
2N/A *
2N/A * Arguments:
2N/A * dowhat - What to do with the attributes (remove them or copy them).
2N/A * pkey - Points to a private key to set the keyidstr in.
2N/A * keyid_str- Points to a location which will receive the pointer to
2N/A * a byte string containing the binary localkeyid. Note that
2N/A * this is a copy, and the caller must free it.
2N/A * keyid_len- Length of keyid_str.
2N/A *
2N/A * Returns:
2N/A * >= 0 - The number of characters in the keyid returned.
2N/A * < 0 - An error occurred. It was probably an error in allocating
2N/A * memory. The error will be set in the error stack. Call
2N/A * ERR_get_error() to get specific information.
2N/A */
2N/Aint
2N/Asunw_get_pkey_localkeyid(getdo_actions_t dowhat, EVP_PKEY *pkey,
2N/A char **keyid_str, int *keyid_len)
2N/A{
2N/A X509_ATTRIBUTE *attr = NULL;
2N/A ASN1_OCTET_STRING *str = NULL;
2N/A ASN1_TYPE *ty = NULL;
2N/A int len = 0;
2N/A int i;
2N/A
2N/A if (keyid_str != NULL)
2N/A *keyid_str = NULL;
2N/A if (keyid_len != NULL)
2N/A *keyid_len = 0;
2N/A
2N/A if (pkey == NULL || pkey->attributes == NULL) {
2N/A return (0);
2N/A }
2N/A
2N/A if ((i = find_attr_by_nid(pkey->attributes, NID_localKeyID)) < 0) {
2N/A return (0);
2N/A }
2N/A attr = sk_X509_ATTRIBUTE_value(pkey->attributes, i);
2N/A
2N/A if ((ty = attrib2type(attr)) == NULL ||
2N/A ty->type != V_ASN1_OCTET_STRING) {
2N/A return (0);
2N/A }
2N/A
2N/A if (dowhat == GETDO_DEL) {
2N/A attr = sk_X509_ATTRIBUTE_delete(pkey->attributes, i);
2N/A if (attr != NULL)
2N/A X509_ATTRIBUTE_free(attr);
2N/A return (0);
2N/A }
2N/A
2N/A str = ty->value.octet_string;
2N/A len = str->length;
2N/A if ((*keyid_str = malloc(len)) == NULL) {
2N/A SUNWerr(SUNW_F_GET_LOCALKEYID, SUNW_R_MEMORY_FAILURE);
2N/A return (-1);
2N/A }
2N/A
2N/A (void) memcpy(*keyid_str, str->data, len);
2N/A *keyid_len = len;
2N/A
2N/A return (len);
2N/A}
2N/A
2N/A/*
2N/A * sunw_get_pkey_fname() gets the friendlyName from a private key. It can
2N/A * optionally remove the value found.
2N/A *
2N/A * Arguments:
2N/A * dowhat - What to do with the attributes (remove them or copy them).
2N/A * pkey - Points to a private key to get the frientlyname from
2N/A * fname - Points to a location which will receive the pointer to a
2N/A * byte string with the ASCII friendlyname
2N/A *
2N/A * Returns:
2N/A * >= 0 - The number of characters in the frienlyname returned.
2N/A * < 0 - An error occurred. It was probably an error in allocating
2N/A * memory. The error will be set in the error stack. Call
2N/A * ERR_get_error() to get specific information.
2N/A */
2N/Aint
2N/Asunw_get_pkey_fname(getdo_actions_t dowhat, EVP_PKEY *pkey, char **fname)
2N/A{
2N/A X509_ATTRIBUTE *attr = NULL;
2N/A ASN1_BMPSTRING *str = NULL;
2N/A ASN1_TYPE *ty = NULL;
2N/A int len = 0;
2N/A int i;
2N/A
2N/A if (fname != NULL)
2N/A *fname = NULL;
2N/A
2N/A if (pkey == NULL || pkey->attributes == NULL) {
2N/A return (0);
2N/A }
2N/A
2N/A if ((i = find_attr_by_nid(pkey->attributes, NID_friendlyName)) < 0) {
2N/A return (0);
2N/A }
2N/A attr = sk_X509_ATTRIBUTE_value(pkey->attributes, i);
2N/A
2N/A if ((ty = attrib2type(attr)) == NULL ||
2N/A ty->type != V_ASN1_BMPSTRING) {
2N/A return (0);
2N/A }
2N/A
2N/A if (dowhat == GETDO_DEL) {
2N/A attr = sk_X509_ATTRIBUTE_delete(pkey->attributes, i);
2N/A if (attr != NULL)
2N/A X509_ATTRIBUTE_free(attr);
2N/A return (0);
2N/A }
2N/A
2N/A str = ty->value.bmpstring;
2N/A#if OPENSSL_VERSION_NUMBER < 0x10000000L
2N/A *fname = uni2asc(str->data, str->length);
2N/A#else
2N/A *fname = OPENSSL_uni2asc(str->data, str->length);
2N/A#endif /* OPENSSL_VERSION_NUMBER < 0x10000000L */
2N/A if (*fname == NULL) {
2N/A SUNWerr(SUNW_F_GET_PKEY_FNAME, SUNW_R_MEMORY_FAILURE);
2N/A return (-1);
2N/A }
2N/A
2N/A len = strlen(*fname);
2N/A
2N/A return (len);
2N/A}
2N/A
2N/A/*
2N/A * sunw_find_localkeyid() searches stacks of certs and private keys,
2N/A * and returns the first matching cert/private key found.
2N/A *
2N/A * Look for a keyid in a stack of certs. if 'certs' is NULL and 'pkeys' is
2N/A * not NULL, search the list of private keys. Move the matching cert to
2N/A * 'matching_cert' and its matching private key to 'matching_pkey'. If no
2N/A * cert or keys match, no match occurred.
2N/A *
2N/A * Arguments:
2N/A * keyid_str- A byte string with the localkeyid to match
2N/A * keyid_len- Length of the keyid byte string.
2N/A * pkeys - Points to a stack of private keys which match the certs.
2N/A * This may be NULL, in which case no keys are returned.
2N/A * certs - Points to a stack of certs to search. If NULL, search the
2N/A * stack of keys instead.
2N/A * matching_pkey
2N/A * - Pointer to receive address of first matching pkey found.
2N/A * 'matching_pkey' must not be NULL; '*matching_pkey' will be
2N/A * reset.
2N/A * matching_cert
2N/A * - Pointer to receive address of first matching cert found.
2N/A * 'matching_cert' must not be NULL; '*matching_cert' will be
2N/A * reset.
2N/A *
2N/A * Returns:
2N/A * < 0 - An error returned. Call ERR_get_error() to get errors information.
2N/A * Where possible, memory has been freed.
2N/A * >= 0 - Objects were found and returned. Which objects are indicated by
2N/A * which bits are set (FOUND_PKEY and/or FOUND_CERT).
2N/A */
2N/Aint
2N/Asunw_find_localkeyid(char *keyid_str, int len, STACK_OF(EVP_PKEY) *pkeys,
2N/ASTACK_OF(X509) *certs, EVP_PKEY **matching_pkey, X509 **matching_cert)
2N/A{
2N/A ASN1_STRING *cmpstr = NULL;
2N/A EVP_PKEY *tmp_pkey = NULL;
2N/A X509 *tmp_cert = NULL;
2N/A int retval = 0;
2N/A
2N/A /* If NULL arguments, this is an error */
2N/A if (keyid_str == NULL ||
2N/A (pkeys == NULL || certs == NULL) ||
2N/A (pkeys != NULL && matching_pkey == NULL) ||
2N/A (certs != NULL && matching_cert == NULL)) {
2N/A SUNWerr(SUNW_F_FIND_LOCALKEYID, SUNW_R_INVALID_ARG);
2N/A return (-1);
2N/A }
2N/A
2N/A if (matching_pkey != NULL)
2N/A *matching_pkey = NULL;
2N/A if (matching_cert != NULL)
2N/A *matching_cert = NULL;
2N/A
2N/A cmpstr = (ASN1_STRING *)M_ASN1_OCTET_STRING_new();
2N/A if (cmpstr == NULL ||
2N/A M_ASN1_OCTET_STRING_set(cmpstr, keyid_str, len) == 0) {
2N/A SUNWerr(SUNW_F_FIND_LOCALKEYID, SUNW_R_MEMORY_FAILURE);
2N/A return (-1);
2N/A }
2N/A
2N/A retval = find_attr(NID_localKeyID, cmpstr, pkeys, &tmp_pkey, certs,
2N/A &tmp_cert);
2N/A if (retval == 0) {
2N/A ASN1_STRING_free(cmpstr);
2N/A return (retval);
2N/A }
2N/A
2N/A if (matching_pkey != NULL)
2N/A *matching_pkey = tmp_pkey;
2N/A if (matching_cert != NULL)
2N/A *matching_cert = tmp_cert;
2N/A
2N/A return (retval);
2N/A}
2N/A
2N/A/*
2N/A * sunw_find_fname() searches stacks of certs and private keys for one with
2N/A * a matching friendlyname and returns the first matching cert/private
2N/A * key found.
2N/A *
2N/A * Look for a friendlyname in a stack of certs. if 'certs' is NULL and 'pkeys'
2N/A * is not NULL, search the list of private keys. Move the matching cert to
2N/A * 'matching_cert' and its matching private key to 'matching_pkey'. If no
2N/A * cert or keys match, no match occurred.
2N/A *
2N/A * Arguments:
2N/A * fname - Friendlyname to find (NULL-terminated ASCII string).
2N/A * pkeys - Points to a stack of private keys which match the certs.
2N/A * This may be NULL, in which case no keys are returned.
2N/A * certs - Points to a stack of certs to search. If NULL, search the
2N/A * stack of keys instead.
2N/A * matching_pkey
2N/A * - Pointer to receive address of first matching pkey found.
2N/A * matching_cert
2N/A * - Pointer to receive address of first matching cert found.
2N/A *
2N/A * Returns:
2N/A * < 0 - An error returned. Call ERR_get_error() to get errors information.
2N/A * Where possible, memory has been freed.
2N/A * >= 0 - Objects were found and returned. Which objects are indicated by
2N/A * which bits are set (FOUND_PKEY and/or FOUND_CERT).
2N/A */
2N/Aint
2N/Asunw_find_fname(char *fname, STACK_OF(EVP_PKEY) *pkeys, STACK_OF(X509) *certs,
2N/A EVP_PKEY **matching_pkey, X509 ** matching_cert)
2N/A{
2N/A ASN1_STRING *cmpstr = NULL;
2N/A EVP_PKEY *tmp_pkey = NULL;
2N/A X509 *tmp_cert = NULL;
2N/A int retval = 0;
2N/A
2N/A /* If NULL arguments, this is an error */
2N/A if (fname == NULL ||
2N/A (pkeys == NULL && certs == NULL) ||
2N/A (pkeys != NULL && matching_pkey == NULL) ||
2N/A (certs != NULL && matching_cert == NULL)) {
2N/A SUNWerr(SUNW_F_FIND_FNAME, SUNW_R_INVALID_ARG);
2N/A return (-1);
2N/A }
2N/A
2N/A if (matching_pkey != NULL)
2N/A *matching_pkey = NULL;
2N/A if (matching_cert != NULL)
2N/A *matching_cert = NULL;
2N/A
2N/A cmpstr = (ASN1_STRING *)asc2bmpstring(fname, strlen(fname));
2N/A if (cmpstr == NULL) {
2N/A /*
2N/A * Error already on stack
2N/A */
2N/A return (-1);
2N/A }
2N/A
2N/A retval = find_attr(NID_friendlyName, cmpstr, pkeys, &tmp_pkey, certs,
2N/A &tmp_cert);
2N/A if (retval == 0) {
2N/A ASN1_STRING_free(cmpstr);
2N/A return (retval);
2N/A }
2N/A
2N/A if (matching_pkey != NULL)
2N/A *matching_pkey = tmp_pkey;
2N/A if (matching_cert != NULL)
2N/A *matching_cert = tmp_cert;
2N/A
2N/A return (retval);
2N/A}
2N/A
2N/A/*
2N/A * sunw_get_cert_fname() gets the fiendlyname from a cert. It can
2N/A * optionally remove the value found.
2N/A *
2N/A * Arguments:
2N/A * dowhat - What to do with the attributes (remove them or copy them).
2N/A * cert - Points to a cert to get the friendlyName from.
2N/A * fname - Points to a location which will receive the pointer to a
2N/A * byte string with the ASCII friendlyname
2N/A *
2N/A * Returns:
2N/A * >= 0 - The number of characters in the friendlyname returned.
2N/A * < 0 - An error occurred. It was probably an error in allocating
2N/A * memory. The error will be set in the error stack. Call
2N/A * ERR_get_error() to get specific information.
2N/A */
2N/Aint
2N/Asunw_get_cert_fname(getdo_actions_t dowhat, X509 *cert, char **fname)
2N/A{
2N/A int len;
2N/A
2N/A if (fname != NULL)
2N/A *fname = NULL;
2N/A
2N/A if (cert == NULL || cert->aux == NULL || cert->aux->alias == NULL) {
2N/A return (0);
2N/A }
2N/A
2N/A if (dowhat == GETDO_DEL) {
2N/A /* Delete the entry */
2N/A ASN1_UTF8STRING_free(cert->aux->alias);
2N/A cert->aux->alias = NULL;
2N/A return (0);
2N/A }
2N/A
2N/A *((uchar_t **)fname) = utf82ascstr(cert->aux->alias);
2N/A if (*fname == NULL) {
2N/A /*
2N/A * Error already on stack
2N/A */
2N/A return (-1);
2N/A }
2N/A
2N/A len = strlen(*fname);
2N/A
2N/A return (len);
2N/A}
2N/A
2N/A/*
2N/A * sunw_set_fname() sets the friendlyName in a cert, a private key or
2N/A * both. Any existing friendlyname will be discarded.
2N/A *
2N/A * Arguments:
2N/A * ascname - An ASCII string with the friendlyName to set
2N/A * pkey - Points to a private key to set the fname in.
2N/A * cert - Points to a cert to set the fname in.
2N/A *
2N/A * Note that setting a friendlyName into a cert which will not be written out
2N/A * as a PKCS12 cert is pointless since it will be lost.
2N/A *
2N/A * Returns:
2N/A * 0 - Success.
2N/A * <0 - An error occurred. It was probably an error in allocating
2N/A * memory. The error will be set in the error stack. Call
2N/A * ERR_get_error() to get specific information.
2N/A */
2N/Aint
2N/Asunw_set_fname(const char *ascname, EVP_PKEY *pkey, X509 *cert)
2N/A{
2N/A X509_ATTRIBUTE *attr = NULL;
2N/A ASN1_BMPSTRING *str = NULL;
2N/A ASN1_TYPE *fname = NULL;
2N/A unsigned char *data = NULL;
2N/A int retval = -1;
2N/A int len;
2N/A int i;
2N/A
2N/A str = asc2bmpstring(ascname, strlen(ascname));
2N/A if (str == NULL) {
2N/A /*
2N/A * Error already on stack
2N/A */
2N/A return (-1);
2N/A }
2N/A
2N/A if (cert != NULL) {
2N/A if (cert->aux != NULL && cert->aux->alias != NULL) {
2N/A ASN1_UTF8STRING_free(cert->aux->alias);
2N/A }
2N/A
2N/A len = ASN1_STRING_to_UTF8(&data, str);
2N/A i = -23;
2N/A if (len <= 0 || (i = X509_alias_set1(cert, data, len)) == 0) {
2N/A SUNWerr(SUNW_F_SET_FNAME, SUNW_R_SET_FNAME_ERR);
2N/A goto cleanup;
2N/A }
2N/A }
2N/A if (pkey != NULL) {
2N/A if ((fname = ASN1_TYPE_new()) == NULL) {
2N/A SUNWerr(SUNW_F_SET_FNAME, SUNW_R_MEMORY_FAILURE);
2N/A goto cleanup;
2N/A }
2N/A
2N/A ASN1_TYPE_set(fname, V_ASN1_BMPSTRING, str);
2N/A str = NULL;
2N/A
2N/A attr = type2attrib(fname, NID_friendlyName);
2N/A if (attr == NULL) {
2N/A /*
2N/A * Error already on stack
2N/A */
2N/A goto cleanup;
2N/A }
2N/A fname = NULL;
2N/A
2N/A if (pkey->attributes == NULL) {
2N/A pkey->attributes = sk_X509_ATTRIBUTE_new_null();
2N/A if (pkey->attributes == NULL) {
2N/A SUNWerr(SUNW_F_SET_FNAME,
2N/A SUNW_R_MEMORY_FAILURE);
2N/A goto cleanup;
2N/A }
2N/A } else if ((i = find_attr_by_nid(pkey->attributes,
2N/A NID_friendlyName)) >= 0) {
2N/A (void) sk_X509_ATTRIBUTE_delete(pkey->attributes, i);
2N/A }
2N/A
2N/A if (sk_X509_ATTRIBUTE_push(pkey->attributes, attr) == 0) {
2N/A SUNWerr(SUNW_F_SET_FNAME, SUNW_R_MEMORY_FAILURE);
2N/A goto cleanup;
2N/A }
2N/A
2N/A attr = NULL;
2N/A }
2N/A retval = 0;
2N/A
2N/Acleanup:
2N/A if (data != NULL)
2N/A OPENSSL_free(data);
2N/A if (str != NULL)
2N/A ASN1_BMPSTRING_free(str);
2N/A if (fname != NULL)
2N/A ASN1_TYPE_free(fname);
2N/A if (attr != NULL)
2N/A X509_ATTRIBUTE_free(attr);
2N/A
2N/A return (retval);
2N/A}
2N/A
2N/A/*
2N/A * sunw_check_keys() compares the public key in the certificate and a
2N/A * private key to ensure that they match.
2N/A *
2N/A * Arguments:
2N/A * cert - Points to a certificate.
2N/A * pkey - Points to a private key.
2N/A *
2N/A * Returns:
2N/A * == 0 - These do not match.
2N/A * != 0 - The cert's public key and the private key match.
2N/A */
2N/Aint
2N/Asunw_check_keys(X509 *cert, EVP_PKEY *pkey)
2N/A{
2N/A int retval = 0;
2N/A
2N/A if (pkey != NULL && cert != NULL)
2N/A retval = X509_check_private_key(cert, pkey);
2N/A
2N/A return (retval);
2N/A}
2N/A
2N/A/*
2N/A * sunw_check_cert_times() compares the time fields in a certificate
2N/A *
2N/A * Compare the 'not before' and the 'not after' times in the cert
2N/A * to the current time. Return the results of the comparison (bad time formats,
2N/A * cert not yet in force, cert expired or in range)
2N/A *
2N/A * Arguments:
2N/A * dowhat - what field(s) to check.
2N/A * cert - Points to a cert to check
2N/A *
2N/A * Returns:
2N/A * Results of the comparison.
2N/A */
2N/Achk_errs_t
2N/Asunw_check_cert_times(chk_actions_t chkwhat, X509 *cert)
2N/A{
2N/A return (check_time(chkwhat, cert));
2N/A}
2N/A
2N/A/*
2N/A * ----------------------------------------------------------------------------
2N/A * Local routines
2N/A * ----------------------------------------------------------------------------
2N/A */
2N/A
2N/A
2N/A/*
2N/A * parse_pkcs12 - Oversee parsing of the pkcs12 structure. Get it
2N/A * parsed. After that either return what's found directly, or
2N/A * do any required matching.
2N/A *
2N/A * Arguments:
2N/A * p12 - Structure with pkcs12 info to be parsed
2N/A * pass - Pass phrase for the private key (possibly empty) or NULL if
2N/A * there is none.
2N/A * matchty - Info about which certs/keys to return if many are in the file.
2N/A * keyid - If private key localkeyids friendlynames are to match a
2N/A * predetermined value, the value to match. This value should
2N/A * be an octet string.
2N/A * keyid_len- Length of the keyid byte string.
2N/A * name_str - If friendlynames are to match a predetermined value, the value
2N/A * to match. This value should be a NULL terminated string.
2N/A * pkey - Points to location pointing to the private key returned.
2N/A * cert - Points to locaiton which points to the client cert returned
2N/A * ca - Points to location that points to a stack of 'certificate
2N/A * authority' certs/trust anchors.
2N/A *
2N/A * Note about error codes: This function is an internal function, and the
2N/A * place where it is called sets error codes. Therefore only set an error
2N/A * code if it is something that is unique or if the function which detected
2N/A * the error doesn't set one.
2N/A *
2N/A * Returns:
2N/A * == -1 - An error occurred. Call ERR_get_error() to get error information.
2N/A * Where possible, memory has been freed.
2N/A * == 0 - No matching returns were found.
2N/A * > 0 - This is the aithmetic 'or' of the FOUND_* bits that indicate which
2N/A * of the requested entries were found.
2N/A */
2N/Astatic int
2N/Aparse_pkcs12(PKCS12 *p12, const char *pass, int matchty, char *keyid,
2N/A int kstr_len, char *name_str, EVP_PKEY **pkey, X509 **cert,
2N/A STACK_OF(X509) **ca)
2N/A{
2N/A STACK_OF(EVP_PKEY) *work_kl = NULL; /* Head for private key list */
2N/A STACK_OF(EVP_PKEY) *nocerts = NULL; /* Head for alt. key list */
2N/A STACK_OF(X509) *work_ca = NULL; /* Head for cert list */
2N/A STACK_OF(X509) *work_cl = NULL;
2N/A int retval = 0;
2N/A int n;
2N/A
2N/A retval = sunw_PKCS12_contents(p12, pass, &work_kl, &work_ca);
2N/A if (retval < 0) {
2N/A goto cleanup;
2N/A } else if (retval == 0) {
2N/A /*
2N/A * Not really an error here - its just that nothing was found.
2N/A */
2N/A goto cleanup;
2N/A }
2N/A
2N/A if (sk_EVP_PKEY_num(work_kl) > 0) {
2N/A
2N/A if (sunw_split_certs(work_kl, work_ca, &work_cl, &nocerts)
2N/A < 0) {
2N/A goto cleanup;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Go through the lists of certs and private keys which were
2N/A * returned, looking for matches of the appropriate type. Do these
2N/A * in the order described above.
2N/A */
2N/A if ((matchty & DO_FIND_KEYID) != 0) {
2N/A
2N/A if (keyid == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_PARSE, SUNW_R_INVALID_ARG);
2N/A retval = -1;
2N/A goto cleanup;
2N/A }
2N/A
2N/A /* See if string matches localkeyid's */
2N/A retval = sunw_find_localkeyid(keyid, kstr_len,
2N/A work_kl, work_cl, pkey, cert);
2N/A if (retval != 0) {
2N/A if (retval == -1)
2N/A goto cleanup;
2N/A else
2N/A goto last_part;
2N/A }
2N/A }
2N/A if ((matchty & DO_FIND_FN) != 0) {
2N/A
2N/A if (name_str == NULL) {
2N/A SUNWerr(SUNW_F_PKCS12_PARSE, SUNW_R_INVALID_ARG);
2N/A retval = -1;
2N/A goto cleanup;
2N/A }
2N/A
2N/A /* See if string matches friendly names */
2N/A retval = sunw_find_fname(name_str, work_kl, work_cl,
2N/A pkey, cert);
2N/A if (retval != 0) {
2N/A if (retval == -1)
2N/A goto cleanup;
2N/A else
2N/A goto last_part;
2N/A }
2N/A }
2N/A
2N/A if (matchty & DO_FIRST_PAIR) {
2N/A
2N/A /* Find the first cert and private key and return them */
2N/A retval = get_key_cert(0, work_kl, pkey, work_cl, cert);
2N/A if (retval != 0) {
2N/A if (retval == -1)
2N/A goto cleanup;
2N/A else
2N/A goto last_part;
2N/A }
2N/A }
2N/A
2N/A if (matchty & DO_LAST_PAIR) {
2N/A
2N/A /*
2N/A * Find the last matching cert and private key and return
2N/A * them. Since keys which don't have matching client certs
2N/A * are at the end of the list of keys, use the number of
2N/A * client certs to compute the position of the last private
2N/A * key which matches a client cert.
2N/A */
2N/A n = sk_X509_num(work_cl) - 1;
2N/A retval = get_key_cert(n, work_kl, pkey, work_cl, cert);
2N/A if (retval != 0) {
2N/A if (retval == -1)
2N/A goto cleanup;
2N/A else
2N/A goto last_part;
2N/A }
2N/A }
2N/A
2N/A if (matchty & DO_UNMATCHING) {
2N/A STACK_OF(EVP_PKEY) *tmpk;
2N/A STACK_OF(X509) *tmpc;
2N/A
2N/A /* Find the first cert and private key and return them */
2N/A tmpc = work_cl;
2N/A if (work_cl == NULL || sk_X509_num(work_cl) == 0)
2N/A tmpc = work_ca;
2N/A tmpk = work_kl;
2N/A if (work_kl == NULL || sk_EVP_PKEY_num(work_kl) == 0)
2N/A tmpk = nocerts;
2N/A retval = get_key_cert(0, tmpk, pkey, tmpc, cert);
2N/A if (retval != 0) {
2N/A if (retval == -1)
2N/A goto cleanup;
2N/A else
2N/A goto last_part;
2N/A }
2N/A }
2N/A
2N/Alast_part:
2N/A /* If no errors, terminate normally */
2N/A if (retval != -1)
2N/A retval |= set_results(NULL, NULL, NULL, NULL, ca, &work_ca,
2N/A NULL, NULL);
2N/A if (retval >= 0) {
2N/A goto clean_part;
2N/A }
2N/A
2N/A /* Fallthrough is intentional in error cases. */
2N/Acleanup:
2N/A if (pkey != NULL && *pkey != NULL) {
2N/A sunw_evp_pkey_free(*pkey);
2N/A *pkey = NULL;
2N/A }
2N/A if (cert != NULL && *cert != NULL) {
2N/A X509_free(*cert);
2N/A *cert = NULL;
2N/A }
2N/A
2N/Aclean_part:
2N/A
2N/A if (work_kl != NULL) {
2N/A sk_EVP_PKEY_pop_free(work_kl, sunw_evp_pkey_free);
2N/A }
2N/A if (work_ca != NULL)
2N/A sk_X509_pop_free(work_ca, X509_free);
2N/A if (work_cl != NULL)
2N/A sk_X509_pop_free(work_cl, X509_free);
2N/A
2N/A return (retval);
2N/A}
2N/A
2N/A/*
2N/A * parse_outer - Unpack the outer PKCS#12 structure and go through the
2N/A * individual bags. Return stacks of certs, private keys found and
2N/A * CA certs found.
2N/A *
2N/A * Note about error codes: This function is an internal function, and the
2N/A * place where it is called sets error codes.
2N/A *
2N/A * Returns:
2N/A * 0 - An error returned. Call ERR_get_error() to get errors information.
2N/A * Where possible, memory has been freed.
2N/A * 1 - PKCS12 data object was parsed and lists of certs and private keys
2N/A * were returned.
2N/A */
2N/Astatic int
2N/Aparse_outer(PKCS12 *p12, const char *pass, STACK_OF(EVP_PKEY) *kl,
2N/A STACK_OF(X509) *cl)
2N/A{
2N/A STACK_OF(PKCS12_SAFEBAG) *bags;
2N/A STACK_OF(PKCS7) *asafes;
2N/A int i, bagnid;
2N/A PKCS7 *p7;
2N/A
2N/A if ((asafes = M_PKCS12_unpack_authsafes(p12)) == NULL)
2N/A return (0);
2N/A
2N/A for (i = 0; i < sk_PKCS7_num(asafes); i++) {
2N/A p7 = sk_PKCS7_value(asafes, i);
2N/A bagnid = OBJ_obj2nid(p7->type);
2N/A if (bagnid == NID_pkcs7_data) {
2N/A bags = M_PKCS12_unpack_p7data(p7);
2N/A } else if (bagnid == NID_pkcs7_encrypted) {
2N/A /*
2N/A * A length of '-1' means strlen() can be used
2N/A * to determine the password length.
2N/A */
2N/A bags = M_PKCS12_unpack_p7encdata(p7, pass, -1);
2N/A } else {
2N/A SUNWerr(SUNW_F_PARSE_OUTER, SUNW_R_BAD_BAGTYPE);
2N/A return (0);
2N/A }
2N/A
2N/A if (bags == NULL) {
2N/A SUNWerr(SUNW_F_PARSE_OUTER, SUNW_R_PARSE_BAG_ERR);
2N/A sk_PKCS7_pop_free(asafes, PKCS7_free);
2N/A return (0);
2N/A }
2N/A if (parse_all_bags(bags, pass, kl, cl) == 0) {
2N/A sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
2N/A sk_PKCS7_pop_free(asafes, PKCS7_free);
2N/A return (0);
2N/A }
2N/A }
2N/A
2N/A return (1);
2N/A}
2N/A
2N/A/*
2N/A * parse_all_bags - go through the stack of bags, parsing each.
2N/A *
2N/A * Note about error codes: This function is an internal function, and the
2N/A * place where it is called sets error codes.
2N/A *
2N/A * Returns:
2N/A * 0 - An error returned. Call ERR_get_error() to get errors information.
2N/A * Where possible, memory has been freed.
2N/A * 1 - Stack of safebags was parsed and lists of certs and private keys
2N/A * were returned.
2N/A */
2N/Astatic int
2N/Aparse_all_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
2N/A STACK_OF(EVP_PKEY) *kl, STACK_OF(X509) *cl)
2N/A{
2N/A int i;
2N/A for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
2N/A if (parse_one_bag(sk_PKCS12_SAFEBAG_value(bags, i),
2N/A pass, kl, cl) == 0)
2N/A return (0);
2N/A }
2N/A return (1);
2N/A}
2N/A
2N/A/*
2N/A * parse_one_bag - Parse an individual bag
2N/A *
2N/A * i = parse_one_bag(bag, pass, kl, cl);
2N/A *
2N/A * Arguments:
2N/A * bag - pkcs12 safebag to parse.
2N/A * pass - password for use in decryption of shrouded keybag
2N/A * kl - Stack of private keys found so far. New private keys will
2N/A * be added here if found.
2N/A * cl - Stack of certs found so far. New certificates will be
2N/A * added here if found.
2N/A *
2N/A * Returns:
2N/A * 0 - An error returned. Call ERR_get_error() to get errors information.
2N/A * Where possible, memory has been freed.
2N/A * 1 - one safebag was parsed. If it contained a cert or private key, it
2N/A * was added to the stack of certs or private keys found, respectively.
2N/A * localKeyId or friendlyName attributes are returned with the
2N/A * private key or certificate.
2N/A */
2N/Astatic int
2N/Aparse_one_bag(PKCS12_SAFEBAG *bag, const char *pass, STACK_OF(EVP_PKEY) *kl,
2N/A STACK_OF(X509) *cl)
2N/A{
2N/A X509_ATTRIBUTE *attr = NULL;
2N/A ASN1_TYPE *keyid = NULL;
2N/A ASN1_TYPE *fname = NULL;
2N/A PKCS8_PRIV_KEY_INFO *p8;
2N/A EVP_PKEY *pkey = NULL;
2N/A X509 *x509 = NULL;
2N/A uchar_t *data = NULL;
2N/A char *str = NULL;
2N/A int retval = 1;
2N/A
2N/A keyid = PKCS12_get_attr(bag, NID_localKeyID);
2N/A fname = PKCS12_get_attr(bag, NID_friendlyName);
2N/A
2N/A switch (M_PKCS12_bag_type(bag)) {
2N/A case NID_keyBag:
2N/A if ((pkey = EVP_PKCS82PKEY(bag->value.keybag)) == NULL) {
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG, SUNW_R_PARSE_BAG_ERR);
2N/A retval = 0;
2N/A break;
2N/A }
2N/A break;
2N/A
2N/A case NID_pkcs8ShroudedKeyBag:
2N/A /*
2N/A * A length of '-1' means strlen() can be used
2N/A * to determine the password length.
2N/A */
2N/A if ((p8 = M_PKCS12_decrypt_skey(bag, pass, -1)) == NULL) {
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG, SUNW_R_PARSE_BAG_ERR);
2N/A retval = 0;
2N/A break;
2N/A }
2N/A pkey = EVP_PKCS82PKEY(p8);
2N/A PKCS8_PRIV_KEY_INFO_free(p8);
2N/A if (pkey == NULL) {
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG, SUNW_R_PARSE_BAG_ERR);
2N/A retval = 0;
2N/A }
2N/A break;
2N/A
2N/A case NID_certBag:
2N/A if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate) {
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG, SUNW_R_BAD_CERTTYPE);
2N/A break;
2N/A }
2N/A if ((x509 = M_PKCS12_certbag2x509(bag)) == NULL) {
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG,
2N/A SUNW_R_PARSE_CERT_ERR);
2N/A retval = 0;
2N/A break;
2N/A }
2N/A
2N/A if (keyid != NULL) {
2N/A if (keyid->type != V_ASN1_OCTET_STRING) {
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG,
2N/A SUNW_R_BAD_LKID);
2N/A retval = 0;
2N/A break;
2N/A }
2N/A if (X509_keyid_set1(x509,
2N/A keyid->value.octet_string->data,
2N/A keyid->value.octet_string->length) == 0) {
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG,
2N/A SUNW_R_SET_LKID_ERR);
2N/A retval = 0;
2N/A break;
2N/A }
2N/A }
2N/A
2N/A if (fname != NULL) {
2N/A ASN1_STRING *tmpstr = NULL;
2N/A int len;
2N/A
2N/A if (fname->type != V_ASN1_BMPSTRING) {
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG,
2N/A SUNW_R_BAD_FNAME);
2N/A retval = 0;
2N/A break;
2N/A }
2N/A
2N/A tmpstr = fname->value.asn1_string;
2N/A len = ASN1_STRING_to_UTF8(&data, tmpstr);
2N/A if (len < 0) {
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG,
2N/A SUNW_R_SET_FNAME_ERR);
2N/A retval = 0;
2N/A break;
2N/A }
2N/A
2N/A if (X509_alias_set1(x509, data, len) == 0) {
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG,
2N/A SUNW_R_SET_FNAME_ERR);
2N/A retval = 0;
2N/A break;
2N/A }
2N/A }
2N/A
2N/A if (sk_X509_push(cl, x509) == 0) {
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG, SUNW_R_MEMORY_FAILURE);
2N/A retval = 0;
2N/A break;
2N/A }
2N/A x509 = NULL;
2N/A break;
2N/A
2N/A case NID_safeContentsBag:
2N/A if (keyid != NULL)
2N/A ASN1_TYPE_free(keyid);
2N/A if (fname != NULL)
2N/A ASN1_TYPE_free(fname);
2N/A if (parse_all_bags(bag->value.safes, pass, kl, cl) == 0) {
2N/A /*
2N/A * Error already on stack
2N/A */
2N/A return (0);
2N/A }
2N/A return (1);
2N/A
2N/A default:
2N/A if (keyid != NULL)
2N/A ASN1_TYPE_free(keyid);
2N/A if (fname != NULL)
2N/A ASN1_TYPE_free(fname);
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG, SUNW_R_BAD_BAGTYPE);
2N/A return (0);
2N/A }
2N/A
2N/A
2N/A if (pkey != NULL) {
2N/A if (retval != 0 && (keyid != NULL || fname != NULL) &&
2N/A pkey->attributes == NULL) {
2N/A pkey->attributes = sk_X509_ATTRIBUTE_new_null();
2N/A if (pkey->attributes == NULL) {
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG,
2N/A SUNW_R_MEMORY_FAILURE);
2N/A retval = 0;
2N/A }
2N/A }
2N/A
2N/A if (retval != 0 && keyid != NULL) {
2N/A attr = type2attrib(keyid, NID_localKeyID);
2N/A if (attr == NULL)
2N/A /*
2N/A * Error already on stack
2N/A */
2N/A retval = 0;
2N/A else {
2N/A keyid = NULL;
2N/A if (sk_X509_ATTRIBUTE_push(pkey->attributes,
2N/A attr) == 0) {
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG,
2N/A SUNW_R_MEMORY_FAILURE);
2N/A retval = 0;
2N/A } else {
2N/A attr = NULL;
2N/A }
2N/A }
2N/A }
2N/A
2N/A if (retval != 0 && fname != NULL) {
2N/A attr = type2attrib(fname, NID_friendlyName);
2N/A if (attr == NULL) {
2N/A /*
2N/A * Error already on stack
2N/A */
2N/A retval = 0;
2N/A } else {
2N/A fname = NULL;
2N/A if (sk_X509_ATTRIBUTE_push(pkey->attributes,
2N/A attr) == 0) {
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG,
2N/A SUNW_R_MEMORY_FAILURE);
2N/A retval = 0;
2N/A } else {
2N/A attr = NULL;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /* Save the private key */
2N/A if (retval != 0) {
2N/A if (sk_EVP_PKEY_push(kl, pkey) == 0) {
2N/A SUNWerr(SUNW_F_PARSE_ONE_BAG,
2N/A SUNW_R_MEMORY_FAILURE);
2N/A retval = 0;
2N/A } else {
2N/A pkey = NULL;
2N/A }
2N/A }
2N/A }
2N/A
2N/A if (pkey != NULL) {
2N/A sunw_evp_pkey_free(pkey);
2N/A }
2N/A
2N/A if (x509 != NULL)
2N/A X509_free(x509);
2N/A
2N/A if (keyid != NULL)
2N/A ASN1_TYPE_free(keyid);
2N/A
2N/A if (fname != NULL)
2N/A ASN1_TYPE_free(fname);
2N/A
2N/A if (attr != NULL)
2N/A X509_ATTRIBUTE_free(attr);
2N/A
2N/A if (data != NULL)
2N/A OPENSSL_free(data);
2N/A
2N/A if (str != NULL)
2N/A OPENSSL_free(str);
2N/A
2N/A return (retval);
2N/A}
2N/A
2N/A/*
2N/A * This function uses the only function that reads PEM files, regardless of
2N/A * the kinds of information included (private keys, public keys, cert requests,
2N/A * certs). Other interfaces that read files require that the application
2N/A * specifically know what kinds of things to read next, and call different
2N/A * interfaces for the different kinds of entities.
2N/A *
2N/A * There is only one aspect of this function that's a bit problematic.
2N/A * If it finds an encrypted private key, it does not decrypt it. It returns
2N/A * the encrypted data and other information needed to decrypt it. The caller
2N/A * must do the decryption. This function does the decoding.
2N/A */
2N/Astatic int
2N/Apem_info(FILE *fp, pem_password_cb cb, void *userdata,
2N/A STACK_OF(EVP_PKEY) **pkeys, STACK_OF(X509) **certs)
2N/A{
2N/A STACK_OF(X509_INFO) *info;
2N/A STACK_OF(EVP_PKEY) *work_kl;
2N/A STACK_OF(X509) *work_cl;
2N/A X509_INFO *x;
2N/A int retval = 0;
2N/A int i;
2N/A
2N/A info = PEM_X509_INFO_read(fp, NULL, cb, userdata);
2N/A if (info == NULL) {
2N/A SUNWerr(SUNW_F_PEM_INFO, SUNW_R_READ_ERR);
2N/A return (-1);
2N/A }
2N/A
2N/A /*
2N/A * Allocate the working stacks for private key(s) and for the cert(s).
2N/A */
2N/A if ((work_kl = sk_EVP_PKEY_new_null()) == NULL) {
2N/A SUNWerr(SUNW_F_PEM_INFO, SUNW_R_MEMORY_FAILURE);
2N/A retval = -1;
2N/A goto cleanup;
2N/A }
2N/A
2N/A if ((work_cl = sk_X509_new_null()) == NULL) {
2N/A SUNWerr(SUNW_F_PEM_INFO, SUNW_R_MEMORY_FAILURE);
2N/A retval = -1;
2N/A goto cleanup;
2N/A }
2N/A
2N/A /*
2N/A * Go through the entries in the info structure.
2N/A */
2N/A for (i = 0; i < sk_X509_INFO_num(info); i++) {
2N/A x = sk_X509_INFO_value(info, i);
2N/A if (x->x509) {
2N/A if (sk_X509_push(work_cl, x->x509) == 0) {
2N/A retval = -1;
2N/A break;
2N/A }
2N/A x->x509 = NULL;
2N/A }
2N/A if (x->x_pkey != NULL && x->x_pkey->dec_pkey != NULL &&
2N/A (x->x_pkey->dec_pkey->type == EVP_PKEY_RSA ||
2N/A x->x_pkey->dec_pkey->type == EVP_PKEY_DSA)) {
2N/A const uchar_t *p;
2N/A
2N/A /*
2N/A * If the key was encrypted, PEM_X509_INFO_read does
2N/A * not decrypt it. If that is the case, the 'enc_pkey'
2N/A * field is set to point to the unencrypted key data.
2N/A * Go through the additional steps to decode it before
2N/A * going on.
2N/A */
2N/A if (x->x_pkey->enc_pkey != NULL) {
2N/A
2N/A if (PEM_do_header(&x->enc_cipher,
2N/A (uchar_t *)x->enc_data,
2N/A (long *)&x->enc_len,
2N/A cb, userdata) == 0) {
2N/A if (ERR_GET_REASON(ERR_peek_error()) ==
2N/A PEM_R_BAD_PASSWORD_READ) {
2N/A SUNWerr(SUNW_F_PEM_INFO,
2N/A SUNW_R_PASSWORD_ERR);
2N/A } else {
2N/A SUNWerr(SUNW_F_PEM_INFO,
2N/A SUNW_R_PKEY_READ_ERR);
2N/A }
2N/A retval = -1;
2N/A break;
2N/A }
2N/A if (x->x_pkey->dec_pkey->type == EVP_PKEY_RSA) {
2N/A RSA **pp;
2N/A
2N/A pp = &(x->x_pkey->dec_pkey->pkey.rsa);
2N/A p = (uchar_t *)x->enc_data;
2N/A if (d2i_RSAPrivateKey(pp, &p,
2N/A x->enc_len) == NULL) {
2N/A SUNWerr(SUNW_F_PEM_INFO,
2N/A SUNW_R_PKEY_READ_ERR);
2N/A retval = -1;
2N/A break;
2N/A }
2N/A } else {
2N/A DSA **pp;
2N/A
2N/A pp = &(x->x_pkey->dec_pkey->pkey.dsa);
2N/A p = (uchar_t *)x->enc_data;
2N/A if (d2i_DSAPrivateKey(pp, &p,
2N/A x->enc_len) == NULL) {
2N/A SUNWerr(SUNW_F_PEM_INFO,
2N/A SUNW_R_PKEY_READ_ERR);
2N/A retval = -1;
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /* Save the key. */
2N/A retval = sk_EVP_PKEY_push(work_kl, x->x_pkey->dec_pkey);
2N/A if (retval == 0) {
2N/A retval = -1;
2N/A break;
2N/A }
2N/A x->x_pkey->dec_pkey = NULL;
2N/A } else if (x->x_pkey != NULL) {
2N/A SUNWerr(SUNW_F_PEM_INFO, SUNW_R_BAD_PKEYTYPE);
2N/A retval = -1;
2N/A break;
2N/A }
2N/A }
2N/A if (retval == -1)
2N/A goto cleanup;
2N/A
2N/A /* If error occurs, then error already on stack */
2N/A retval = set_results(pkeys, &work_kl, certs, &work_cl, NULL, NULL,
2N/A NULL, NULL);
2N/A
2N/Acleanup:
2N/A if (work_kl != NULL) {
2N/A sk_EVP_PKEY_pop_free(work_kl, sunw_evp_pkey_free);
2N/A }
2N/A if (work_cl != NULL)
2N/A sk_X509_pop_free(work_cl, X509_free);
2N/A
2N/A sk_X509_INFO_pop_free(info, X509_INFO_free);
2N/A
2N/A return (retval);
2N/A}
2N/A
2N/A/*
2N/A * sunw_append_keys - Given two stacks of private keys, remove the keys from
2N/A * the second stack and append them to the first. Both stacks must exist
2N/A * at time of call.
2N/A *
2N/A * Arguments:
2N/A * dst - the stack to receive the keys from 'src'
2N/A * src - the stack whose keys are to be moved.
2N/A *
2N/A * Returns:
2N/A * -1 - An error occurred. The error status is set.
2N/A * >= 0 - The number of keys that were copied.
2N/A */
2N/Astatic int
2N/Asunw_append_keys(STACK_OF(EVP_PKEY) *dst, STACK_OF(EVP_PKEY) *src)
2N/A{
2N/A EVP_PKEY *tmpk;
2N/A int count = 0;
2N/A
2N/A while (sk_EVP_PKEY_num(src) > 0) {
2N/A tmpk = sk_EVP_PKEY_delete(src, 0);
2N/A if (sk_EVP_PKEY_push(dst, tmpk) == 0) {
2N/A sunw_evp_pkey_free(tmpk);
2N/A SUNWerr(SUNW_F_APPEND_KEYS, SUNW_R_MEMORY_FAILURE);
2N/A return (-1);
2N/A }
2N/A count ++;
2N/A }
2N/A
2N/A return (count);
2N/A}
2N/A
2N/A/*
2N/A * move_certs - Given two stacks of certs, remove the certs from
2N/A * the second stack and append them to the first.
2N/A *
2N/A * Arguments:
2N/A * dst - the stack to receive the certs from 'src'
2N/A * src - the stack whose certs are to be moved.
2N/A *
2N/A * Returns:
2N/A * -1 - An error occurred. The error status is set.
2N/A * >= 0 - The number of certs that were copied.
2N/A */
2N/Astatic int
2N/Amove_certs(STACK_OF(X509) *dst, STACK_OF(X509) *src)
2N/A{
2N/A X509 *tmpc;
2N/A int count = 0;
2N/A
2N/A while (sk_X509_num(src) > 0) {
2N/A tmpc = sk_X509_delete(src, 0);
2N/A if (sk_X509_push(dst, tmpc) == 0) {
2N/A X509_free(tmpc);
2N/A SUNWerr(SUNW_F_MOVE_CERTS, SUNW_R_MEMORY_FAILURE);
2N/A return (-1);
2N/A }
2N/A count++;
2N/A }
2N/A
2N/A return (count);
2N/A}
2N/A
2N/A/*
2N/A * get_key_cert - Get a cert and its matching key from the stacks of certs
2N/A * and keys. They are removed from the stacks.
2N/A *
2N/A * Arguments:
2N/A * n - Offset of the entries to return.
2N/A * kl - Points to a stack of private keys that matches the list of
2N/A * certs below.
2N/A * pkey - Points at location where the address of the matching private
2N/A * key will be stored.
2N/A * cl - Points to a stack of client certs with matching private keys.
2N/A * cert - Points to locaiton where the address of the matching client cert
2N/A * will be returned
2N/A *
2N/A * The assumption is that the stacks of keys and certs contain key/cert pairs,
2N/A * with entries in the same order and hence at the same offset. Provided
2N/A * the key and cert selected match, each will be removed from its stack and
2N/A * returned.
2N/A *
2N/A * A stack of certs can be passed in without a stack of private keys, and vise
2N/A * versa. In that case, the indicated key/cert will be returned.
2N/A *
2N/A * Returns:
2N/A * 0 - No matches were found.
2N/A * > 0 - Bits set based on FOUND_* definitions, indicating what is returned.
2N/A * This can be FOUND_PKEY, FOUND_CERT or (FOUND_PKEY | FOUND_CERT).
2N/A */
2N/Astatic int
2N/Aget_key_cert(int n, STACK_OF(EVP_PKEY) *kl, EVP_PKEY **pkey, STACK_OF(X509) *cl,
2N/A X509 **cert)
2N/A{
2N/A int retval = 0;
2N/A int nk;
2N/A int nc;
2N/A
2N/A nk = (kl != NULL) ? sk_EVP_PKEY_num(kl) : 0;
2N/A nc = (cl != NULL) ? sk_X509_num(cl) : 0;
2N/A
2N/A if (pkey != NULL && *pkey == NULL) {
2N/A if (nk > 0 && n >= 0 || n < nk) {
2N/A *pkey = sk_EVP_PKEY_delete(kl, n);
2N/A if (*pkey != NULL)
2N/A retval |= FOUND_PKEY;
2N/A }
2N/A }
2N/A
2N/A if (cert != NULL && *cert == NULL) {
2N/A if (nc > 0 && n >= 0 && n < nc) {
2N/A *cert = sk_X509_delete(cl, n);
2N/A if (*cert != NULL)
2N/A retval |= FOUND_CERT;
2N/A }
2N/A }
2N/A
2N/A return (retval);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * asc2bmpstring - Convert a regular C ASCII string to an ASn1_STRING in
2N/A * ASN1_BMPSTRING format.
2N/A *
2N/A * Arguments:
2N/A * str - String to be convered.
2N/A * len - Length of the string.
2N/A *
2N/A * Returns:
2N/A * == NULL - An error occurred. Error information (accessible by
2N/A * ERR_get_error()) is set.
2N/A * != NULL - Points to an ASN1_BMPSTRING structure with the converted
2N/A * string as a value.
2N/A */
2N/Astatic ASN1_BMPSTRING *
2N/Aasc2bmpstring(const char *str, int len)
2N/A{
2N/A ASN1_BMPSTRING *bmp = NULL;
2N/A uchar_t *uni = NULL;
2N/A int unilen;
2N/A
2N/A /* Convert the character to the bmp format. */
2N/A#if OPENSSL_VERSION_NUMBER < 0x10000000L
2N/A if (asc2uni(str, len, &uni, &unilen) == 0) {
2N/A#else
2N/A if (OPENSSL_asc2uni(str, len, &uni, &unilen) == 0) {
2N/A#endif /* OPENSSL_VERSION_NUMBER < 0x10000000L */
2N/A SUNWerr(SUNW_F_ASC2BMPSTRING, SUNW_R_MEMORY_FAILURE);
2N/A return (NULL);
2N/A }
2N/A
2N/A /*
2N/A * Adjust for possible pair of NULL bytes at the end because
2N/A * asc2uni() returns a doubly null terminated string.
2N/A */
2N/A if (uni[unilen - 1] == '\0' && uni[unilen - 2] == '\0')
2N/A unilen -= 2;
2N/A
2N/A /* Construct comparison string with correct format */
2N/A bmp = M_ASN1_BMPSTRING_new();
2N/A if (bmp == NULL) {
2N/A SUNWerr(SUNW_F_ASC2BMPSTRING, SUNW_R_MEMORY_FAILURE);
2N/A OPENSSL_free(uni);
2N/A return (NULL);
2N/A }
2N/A
2N/A bmp->data = uni;
2N/A bmp->length = unilen;
2N/A
2N/A return (bmp);
2N/A}
2N/A
2N/A/*
2N/A * utf82ascstr - Convert a UTF8STRING string to a regular C ASCII string.
2N/A * This goes through an intermediate step with a ASN1_STRING type of
2N/A * IA5STRING (International Alphabet 5, which is the same as ASCII).
2N/A *
2N/A * Arguments:
2N/A * str - UTF8STRING to be converted.
2N/A *
2N/A * Returns:
2N/A * == NULL - An error occurred. Error information (accessible by
2N/A * ERR_get_error()) is set.
2N/A * != NULL - Points to a NULL-termianted ASCII string. The caller must
2N/A * free it.
2N/A */
2N/Astatic uchar_t *
2N/Autf82ascstr(ASN1_UTF8STRING *ustr)
2N/A{
2N/A ASN1_STRING tmpstr;
2N/A ASN1_STRING *astr = &tmpstr;
2N/A uchar_t *retstr = NULL;
2N/A int mbflag;
2N/A int ret;
2N/A
2N/A if (ustr == NULL || ustr->type != V_ASN1_UTF8STRING) {
2N/A SUNWerr(SUNW_F_UTF82ASCSTR, SUNW_R_INVALID_ARG);
2N/A return (NULL);
2N/A }
2N/A
2N/A mbflag = MBSTRING_ASC;
2N/A tmpstr.data = NULL;
2N/A tmpstr.length = 0;
2N/A
2N/A ret = ASN1_mbstring_copy(&astr, ustr->data, ustr->length, mbflag,
2N/A B_ASN1_IA5STRING);
2N/A if (ret < 0) {
2N/A SUNWerr(SUNW_F_UTF82ASCSTR, SUNW_R_STR_CONVERT_ERR);
2N/A return (NULL);
2N/A }
2N/A
2N/A retstr = OPENSSL_malloc(astr->length + 1);
2N/A if (retstr == NULL) {
2N/A SUNWerr(SUNW_F_UTF82ASCSTR, SUNW_R_MEMORY_FAILURE);
2N/A return (NULL);
2N/A }
2N/A
2N/A (void) memcpy(retstr, astr->data, astr->length);
2N/A retstr[astr->length] = '\0';
2N/A OPENSSL_free(astr->data);
2N/A
2N/A return (retstr);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * type2attrib - Given a ASN1_TYPE, return a X509_ATTRIBUTE of the type
2N/A * specified by the given NID.
2N/A *
2N/A * Arguments:
2N/A * ty - Type structure to be made into an attribute
2N/A * nid - NID of the attribute
2N/A *
2N/A * Returns:
2N/A * NULL An error occurred.
2N/A * != NULL An X509_ATTRIBUTE structure.
2N/A */
2N/AX509_ATTRIBUTE *
2N/Atype2attrib(ASN1_TYPE *ty, int nid)
2N/A{
2N/A X509_ATTRIBUTE *a;
2N/A
2N/A if ((a = X509_ATTRIBUTE_new()) == NULL ||
2N/A (a->value.set = sk_ASN1_TYPE_new_null()) == NULL ||
2N/A sk_ASN1_TYPE_push(a->value.set, ty) == 0) {
2N/A if (a != NULL)
2N/A X509_ATTRIBUTE_free(a);
2N/A SUNWerr(SUNW_F_TYPE2ATTRIB, SUNW_R_MEMORY_FAILURE);
2N/A return (NULL);
2N/A }
2N/A a->single = 0;
2N/A a->object = OBJ_nid2obj(nid);
2N/A
2N/A return (a);
2N/A}
2N/A
2N/A/*
2N/A * attrib2type - Given a X509_ATTRIBUTE, return pointer to the ASN1_TYPE
2N/A * component
2N/A *
2N/A * Arguments:
2N/A * attr - Attribute structure containing a type.
2N/A *
2N/A * Returns:
2N/A * NULL An error occurred.
2N/A * != NULL An ASN1_TYPE structure.
2N/A */
2N/Astatic ASN1_TYPE *
2N/Aattrib2type(X509_ATTRIBUTE *attr)
2N/A{
2N/A ASN1_TYPE *ty = NULL;
2N/A
2N/A if (attr == NULL || attr->single == 1)
2N/A return (NULL);
2N/A
2N/A if (sk_ASN1_TYPE_num(attr->value.set) > 0)
2N/A ty = sk_ASN1_TYPE_value(attr->value.set, 0);
2N/A
2N/A return (ty);
2N/A}
2N/A
2N/A/*
2N/A * find_attr_by_nid - Given a ASN1_TYPE, return the offset of a X509_ATTRIBUTE
2N/A * of the type specified by the given NID.
2N/A *
2N/A * Arguments:
2N/A * attrs - Stack of attributes to search
2N/A * nid - NID of the attribute being searched for
2N/A *
2N/A * Returns:
2N/A * -1 None found
2N/A * != -1 Offset of the matching attribute.
2N/A */
2N/Astatic int
2N/Afind_attr_by_nid(STACK_OF(X509_ATTRIBUTE) *attrs, int nid)
2N/A{
2N/A X509_ATTRIBUTE *a;
2N/A int i;
2N/A
2N/A if (attrs == NULL)
2N/A return (-1);
2N/A
2N/A for (i = 0; i < sk_X509_ATTRIBUTE_num(attrs); i++) {
2N/A a = sk_X509_ATTRIBUTE_value(attrs, i);
2N/A if (OBJ_obj2nid(a->object) == nid)
2N/A return (i);
2N/A }
2N/A return (-1);
2N/A}
2N/A
2N/A/*
2N/A * Called by our PKCS12 code to read our function and error codes
2N/A * into memory so that the OpenSSL framework can retrieve them.
2N/A */
2N/Avoid
2N/AERR_load_SUNW_strings(void)
2N/A{
2N/A assert(SUNW_lib_error_code == 0);
2N/A#ifndef OPENSSL_NO_ERR
2N/A /*
2N/A * Have OpenSSL provide us with a unique ID.
2N/A */
2N/A SUNW_lib_error_code = ERR_get_next_error_library();
2N/A
2N/A ERR_load_strings(SUNW_lib_error_code, SUNW_str_functs);
2N/A ERR_load_strings(SUNW_lib_error_code, SUNW_str_reasons);
2N/A
2N/A SUNW_lib_name->error = ERR_PACK(SUNW_lib_error_code, 0, 0);
2N/A ERR_load_strings(0, SUNW_lib_name);
2N/A#endif
2N/A}
2N/A
2N/A/*
2N/A * The SUNWerr macro resolves to this routine. So when we need
2N/A * to push an error, this routine does it for us. Notice that
2N/A * the SUNWerr macro provides a filename and line #.
2N/A */
2N/Avoid
2N/AERR_SUNW_error(int function, int reason, char *file, int line)
2N/A{
2N/A assert(SUNW_lib_error_code != 0);
2N/A#ifndef OPENSSL_NO_ERR
2N/A ERR_PUT_error(SUNW_lib_error_code, function, reason, file, line);
2N/A#endif
2N/A}
2N/A
2N/A/*
2N/A * check_time - Given an indication of the which time(s) to check, check
2N/A * that time or those times against the current time and return the
2N/A * relationship.
2N/A *
2N/A * Arguments:
2N/A * chkwhat - What kind of check to do.
2N/A * cert - The cert to check.
2N/A *
2N/A * Returns:
2N/A * CHKERR_* values.
2N/A */
2N/Astatic chk_errs_t
2N/Acheck_time(chk_actions_t chkwhat, X509 *cert)
2N/A{
2N/A int i;
2N/A
2N/A if (chkwhat == CHK_NOT_BEFORE || chkwhat == CHK_BOTH) {
2N/A i = X509_cmp_time(X509_get_notBefore(cert), NULL);
2N/A if (i == 0)
2N/A return (CHKERR_TIME_BEFORE_BAD);
2N/A if (i > 0)
2N/A return (CHKERR_TIME_IS_BEFORE);
2N/A
2N/A /* The current time is after the 'not before' time */
2N/A }
2N/A
2N/A if (chkwhat == CHK_NOT_AFTER || chkwhat == CHK_BOTH) {
2N/A i = X509_cmp_time(X509_get_notAfter(cert), NULL);
2N/A if (i == 0)
2N/A return (CHKERR_TIME_AFTER_BAD);
2N/A if (i < 0)
2N/A return (CHKERR_TIME_HAS_EXPIRED);
2N/A }
2N/A
2N/A return (CHKERR_TIME_OK);
2N/A}
2N/A
2N/A/*
2N/A * find_attr - Look for a given attribute of the type associated with the NID.
2N/A *
2N/A * Arguments:
2N/A * nid - NID for the attribute to be found (either NID_friendlyName or
2N/A * NID_locakKeyId)
2N/A * str - ASN1_STRING-type structure containing the value to be found,
2N/A * FriendlyName expects a ASN1_BMPSTRING and localKeyID uses a
2N/A * ASN1_STRING.
2N/A * kl - Points to a stack of private keys.
2N/A * pkey - Points at a location where the address of the matching private
2N/A * key will be stored.
2N/A * cl - Points to a stack of client certs with matching private keys.
2N/A * cert - Points to locaiton where the address of the matching client cert
2N/A * will be returned
2N/A *
2N/A * This function is designed to process lists of certs and private keys.
2N/A * This is made complex because these the attributes are stored differently
2N/A * for certs and for keys. For certs, only a few attributes are retained.
2N/A * FriendlyName is stored in the aux structure, under the name 'alias'.
2N/A * LocalKeyId is also stored in the aux structure, under the name 'keyid'.
2N/A * A pkey structure has a stack of attributes.
2N/A *
2N/A * The basic approach is:
2N/A * - If there there is no stack of certs but a stack of private keys exists,
2N/A * search the stack of keys for a match. Alternately, if there is a stack
2N/A * of certs and no private keys, search the certs.
2N/A *
2N/A * - If there are both certs and keys, assume that the matching certs and
2N/A * keys are in their respective stacks, with matching entries in the same
2N/A * order. Search for the name or keyid in the stack of certs. If it is
2N/A * not found, then this function returns 0 (nothing found).
2N/A *
2N/A * - Once a cert is found, verify that the key actually matches by
2N/A * comparing the private key with the public key (in the cert).
2N/A * If they don't match, return an error.
2N/A *
2N/A * A pointer to cert and/or pkey which matches the name or keyid is stored
2N/A * in the return arguments.
2N/A *
2N/A * Returns:
2N/A * 0 - No matches were found.
2N/A * > 0 - Bits set based on FOUND_* definitions, indicating what was found.
2N/A * This can be FOUND_PKEY, FOUND_CERT or (FOUND_PKEY | FOUND_CERT).
2N/A */
2N/Astatic int
2N/Afind_attr(int nid, ASN1_STRING *str, STACK_OF(EVP_PKEY) *kl, EVP_PKEY **pkey,
2N/A STACK_OF(X509) *cl, X509 **cert)
2N/A{
2N/A ASN1_UTF8STRING *ustr = NULL;
2N/A ASN1_STRING *s;
2N/A ASN1_TYPE *t;
2N/A EVP_PKEY *p;
2N/A uchar_t *fname = NULL;
2N/A X509 *x;
2N/A int found = 0;
2N/A int chkcerts;
2N/A int len;
2N/A int res;
2N/A int c = -1;
2N/A int k = -1;
2N/A
2N/A chkcerts = (cert != NULL || pkey != NULL) && cl != NULL;
2N/A if (chkcerts && nid == NID_friendlyName &&
2N/A str->type == V_ASN1_BMPSTRING) {
2N/A ustr = ASN1_UTF8STRING_new();
2N/A if (ustr == NULL) {
2N/A SUNWerr(SUNW_F_FINDATTR, SUNW_R_MEMORY_FAILURE);
2N/A return (0);
2N/A }
2N/A len = ASN1_STRING_to_UTF8(&fname, str);
2N/A if (fname == NULL) {
2N/A ASN1_UTF8STRING_free(ustr);
2N/A SUNWerr(SUNW_F_FINDATTR, SUNW_R_STR_CONVERT_ERR);
2N/A return (0);
2N/A }
2N/A
2N/A if (ASN1_STRING_set(ustr, fname, len) == 0) {
2N/A ASN1_UTF8STRING_free(ustr);
2N/A OPENSSL_free(fname);
2N/A SUNWerr(SUNW_F_FINDATTR, SUNW_R_MEMORY_FAILURE);
2N/A return (0);
2N/A }
2N/A }
2N/A
2N/A if (chkcerts) {
2N/A for (c = 0; c < sk_X509_num(cl); c++) {
2N/A res = -1;
2N/A x = sk_X509_value(cl, c);
2N/A if (nid == NID_friendlyName && ustr != NULL) {
2N/A if (x->aux == NULL || x->aux->alias == NULL)
2N/A continue;
2N/A s = x->aux->alias;
2N/A if (s != NULL && s->type == ustr->type &&
2N/A s->data != NULL) {
2N/A res = ASN1_STRING_cmp(s, ustr);
2N/A }
2N/A } else {
2N/A if (x->aux == NULL || x->aux->keyid == NULL)
2N/A continue;
2N/A s = x->aux->keyid;
2N/A if (s != NULL && s->type == str->type &&
2N/A s->data != NULL) {
2N/A res = ASN1_STRING_cmp(s, str);
2N/A }
2N/A }
2N/A if (res == 0) {
2N/A if (cert != NULL)
2N/A *cert = sk_X509_delete(cl, c);
2N/A found = FOUND_CERT;
2N/A break;
2N/A }
2N/A }
2N/A if (ustr != NULL) {
2N/A ASN1_UTF8STRING_free(ustr);
2N/A OPENSSL_free(fname);
2N/A }
2N/A }
2N/A
2N/A if (pkey != NULL && kl != NULL) {
2N/A /*
2N/A * Looking for pkey to match a cert? If so, assume that
2N/A * lists of certs and their matching pkeys are in the same
2N/A * order. Call X509_check_private_key() to verify this
2N/A * assumption.
2N/A */
2N/A if (found != 0 && cert != NULL) {
2N/A k = c;
2N/A p = sk_EVP_PKEY_value(kl, k);
2N/A if (X509_check_private_key(x, p) != 0) {
2N/A if (pkey != NULL)
2N/A *pkey = sk_EVP_PKEY_delete(kl, k);
2N/A found |= FOUND_PKEY;
2N/A }
2N/A } else if (cert == NULL) {
2N/A for (k = 0; k < sk_EVP_PKEY_num(kl); k++) {
2N/A p = sk_EVP_PKEY_value(kl, k);
2N/A if (p == NULL || p->attributes == NULL)
2N/A continue;
2N/A
2N/A t = PKCS12_get_attr_gen(p->attributes, nid);
2N/A if (t != NULL || ASN1_STRING_cmp(str,
2N/A t->value.asn1_string) == 0)
2N/A continue;
2N/A
2N/A found |= FOUND_PKEY;
2N/A if (pkey != NULL)
2N/A *pkey = sk_EVP_PKEY_delete(kl, k);
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A
2N/A return (found);
2N/A}
2N/A
2N/A/*
2N/A * set_results - Given two pointers to stacks of private keys, certs or CA
2N/A * CA certs, either copy the second stack to the first, or append the
2N/A * contents of the second to the first.
2N/A *
2N/A * Arguments:
2N/A * pkeys - Points to stack of pkeys
2N/A * work_kl - Points to working stack of pkeys
2N/A * certs - Points to stack of certs
2N/A * work_cl - Points to working stack of certs
2N/A * cacerts - Points to stack of CA certs
2N/A * work_ca - Points to working stack of CA certs
2N/A * xtrakeys - Points to stack of unmatcned pkeys
2N/A * work_xl - Points to working stack of unmatcned pkeys
2N/A *
2N/A * The arguments are in pairs. The first of each pair points to a stack
2N/A * of keys or certs. The second of the pair points at a 'working stack'
2N/A * of the same type of entities. Actions taken are as follows:
2N/A *
2N/A * - If either the first or second argument is NULL, or if there are no
2N/A * members in the second stack, there is nothing to do.
2N/A * - If the first argument points to a pointer which is NULL, then there
2N/A * is no existing stack for the first argument. Copy the stack pointer
2N/A * from the second argument to the first argument and NULL out the stack
2N/A * pointer for the second.
2N/A * - Otherwise, go through the elements of the second stack, removing each
2N/A * and adding it to the first stack.
2N/A *
2N/A * Returns:
2N/A * == -1 - An error occurred. Call ERR_get_error() to get error information.
2N/A * == 0 - No matching returns were found.
2N/A * > 0 - This is the arithmetic 'or' of the FOUND_* bits that indicate which
2N/A * of the requested entries were manipulated.
2N/A */
2N/Astatic int
2N/Aset_results(STACK_OF(EVP_PKEY) **pkeys, STACK_OF(EVP_PKEY) **work_kl,
2N/A STACK_OF(X509) **certs, STACK_OF(X509) **work_cl,
2N/A STACK_OF(X509) **cacerts, STACK_OF(X509) **work_ca,
2N/A STACK_OF(EVP_PKEY) **xtrakeys, STACK_OF(EVP_PKEY) **work_xl)
2N/A{
2N/A int retval = 0;
2N/A
2N/A if (pkeys != NULL && work_kl != NULL && *work_kl != NULL &&
2N/A sk_EVP_PKEY_num(*work_kl) > 0) {
2N/A if (*pkeys == NULL) {
2N/A *pkeys = *work_kl;
2N/A *work_kl = NULL;
2N/A } else {
2N/A if (sunw_append_keys(*pkeys, *work_kl) < 0) {
2N/A return (-1);
2N/A }
2N/A }
2N/A retval |= FOUND_PKEY;
2N/A }
2N/A if (certs != NULL && work_cl != NULL && *work_cl != NULL &&
2N/A sk_X509_num(*work_cl) > 0) {
2N/A if (*certs == NULL) {
2N/A *certs = *work_cl;
2N/A *work_cl = NULL;
2N/A } else {
2N/A if (move_certs(*certs, *work_cl) < 0) {
2N/A return (-1);
2N/A }
2N/A }
2N/A retval |= FOUND_CERT;
2N/A }
2N/A
2N/A if (cacerts != NULL && work_ca != NULL && *work_ca != NULL &&
2N/A sk_X509_num(*work_ca) > 0) {
2N/A if (*cacerts == NULL) {
2N/A *cacerts = *work_ca;
2N/A *work_ca = NULL;
2N/A } else {
2N/A if (move_certs(*cacerts, *work_ca) < 0) {
2N/A return (-1);
2N/A }
2N/A }
2N/A retval |= FOUND_CA_CERTS;
2N/A }
2N/A
2N/A if (xtrakeys != NULL && work_xl != NULL && *work_xl != NULL &&
2N/A sk_EVP_PKEY_num(*work_xl) > 0) {
2N/A if (*xtrakeys == NULL) {
2N/A *xtrakeys = *work_xl;
2N/A *work_xl = NULL;
2N/A } else {
2N/A if (sunw_append_keys(*xtrakeys, *work_xl) < 0) {
2N/A return (-1);
2N/A }
2N/A }
2N/A retval |= FOUND_XPKEY;
2N/A }
2N/A
2N/A return (retval);
2N/A}