1/* $NetBSD$ */ 2 3/* 4 * Copyright (c) 2004 - 2006 Kungliga Tekniska H��gskolan 5 * (Royal Institute of Technology, Stockholm, Sweden). 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * 3. Neither the name of the Institute nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36/* Id */ 37 38#include <config.h> 39 40#include <stdio.h> 41#include <stdlib.h> 42#include <ctype.h> 43#include <errno.h> 44#ifdef HAVE_STRINGS_H 45#include <strings.h> 46#endif 47#include <assert.h> 48#include <stdarg.h> 49#include <err.h> 50#include <limits.h> 51 52#include <krb5/roken.h> 53 54#include <krb5/getarg.h> 55#include <krb5/base64.h> 56#include <krb5/hex.h> 57#include <krb5/com_err.h> 58#include <krb5/parse_units.h> 59#include <krb5/parse_bytes.h> 60 61#include <krb5/krb5-types.h> 62 63#include <krb5/rfc2459_asn1.h> 64#include <krb5/cms_asn1.h> 65#include <krb5/pkcs8_asn1.h> 66#include <krb5/pkcs9_asn1.h> 67#include <krb5/pkcs12_asn1.h> 68#include <krb5/ocsp_asn1.h> 69#include <krb5/pkcs10_asn1.h> 70#include <krb5/asn1_err.h> 71#include <krb5/pkinit_asn1.h> 72 73#include <krb5/der.h> 74 75#define HC_DEPRECATED_CRYPTO 76#include "crypto-headers.h" 77 78struct hx509_keyset_ops; 79struct hx509_collector; 80struct hx509_generate_private_context; 81typedef struct hx509_path hx509_path; 82 83#include <krb5/hx509.h> 84 85typedef void (*_hx509_cert_release_func)(struct hx509_cert_data *, void *); 86 87 88#include "sel.h" 89 90#include <hx509-private.h> 91#include <krb5/hx509_err.h> 92 93struct hx509_peer_info { 94 hx509_cert cert; 95 AlgorithmIdentifier *val; 96 size_t len; 97}; 98 99#define HX509_CERTS_FIND_SERIALNUMBER 1 100#define HX509_CERTS_FIND_ISSUER 2 101#define HX509_CERTS_FIND_SUBJECT 4 102#define HX509_CERTS_FIND_ISSUER_KEY_ID 8 103#define HX509_CERTS_FIND_SUBJECT_KEY_ID 16 104 105struct hx509_name_data { 106 Name der_name; 107}; 108 109struct hx509_path { 110 size_t len; 111 hx509_cert *val; 112}; 113 114struct hx509_query_data { 115 int match; 116#define HX509_QUERY_FIND_ISSUER_CERT 0x000001 117#define HX509_QUERY_MATCH_SERIALNUMBER 0x000002 118#define HX509_QUERY_MATCH_ISSUER_NAME 0x000004 119#define HX509_QUERY_MATCH_SUBJECT_NAME 0x000008 120#define HX509_QUERY_MATCH_SUBJECT_KEY_ID 0x000010 121#define HX509_QUERY_MATCH_ISSUER_ID 0x000020 122#define HX509_QUERY_PRIVATE_KEY 0x000040 123#define HX509_QUERY_KU_ENCIPHERMENT 0x000080 124#define HX509_QUERY_KU_DIGITALSIGNATURE 0x000100 125#define HX509_QUERY_KU_KEYCERTSIGN 0x000200 126#define HX509_QUERY_KU_CRLSIGN 0x000400 127#define HX509_QUERY_KU_NONREPUDIATION 0x000800 128#define HX509_QUERY_KU_KEYAGREEMENT 0x001000 129#define HX509_QUERY_KU_DATAENCIPHERMENT 0x002000 130#define HX509_QUERY_ANCHOR 0x004000 131#define HX509_QUERY_MATCH_CERTIFICATE 0x008000 132#define HX509_QUERY_MATCH_LOCAL_KEY_ID 0x010000 133#define HX509_QUERY_NO_MATCH_PATH 0x020000 134#define HX509_QUERY_MATCH_FRIENDLY_NAME 0x040000 135#define HX509_QUERY_MATCH_FUNCTION 0x080000 136#define HX509_QUERY_MATCH_KEY_HASH_SHA1 0x100000 137#define HX509_QUERY_MATCH_TIME 0x200000 138#define HX509_QUERY_MATCH_EKU 0x400000 139#define HX509_QUERY_MATCH_EXPR 0x800000 140#define HX509_QUERY_MASK 0xffffff 141 Certificate *subject; 142 Certificate *certificate; 143 heim_integer *serial; 144 heim_octet_string *subject_id; 145 heim_octet_string *local_key_id; 146 Name *issuer_name; 147 Name *subject_name; 148 hx509_path *path; 149 char *friendlyname; 150 int (*cmp_func)(hx509_context, hx509_cert, void *); 151 void *cmp_func_ctx; 152 heim_octet_string *keyhash_sha1; 153 time_t timenow; 154 heim_oid *eku; 155 struct hx_expr *expr; 156}; 157 158struct hx509_keyset_ops { 159 const char *name; 160 int flags; 161 int (*init)(hx509_context, hx509_certs, void **, 162 int, const char *, hx509_lock); 163 int (*store)(hx509_context, hx509_certs, void *, int, hx509_lock); 164 int (*free)(hx509_certs, void *); 165 int (*add)(hx509_context, hx509_certs, void *, hx509_cert); 166 int (*query)(hx509_context, hx509_certs, void *, 167 const hx509_query *, hx509_cert *); 168 int (*iter_start)(hx509_context, hx509_certs, void *, void **); 169 int (*iter)(hx509_context, hx509_certs, void *, void *, hx509_cert *); 170 int (*iter_end)(hx509_context, hx509_certs, void *, void *); 171 int (*printinfo)(hx509_context, hx509_certs, 172 void *, int (*)(void *, const char *), void *); 173 int (*getkeys)(hx509_context, hx509_certs, void *, hx509_private_key **); 174 int (*addkey)(hx509_context, hx509_certs, void *, hx509_private_key); 175}; 176 177struct _hx509_password { 178 size_t len; 179 char **val; 180}; 181 182extern hx509_lock _hx509_empty_lock; 183 184struct hx509_context_data { 185 struct hx509_keyset_ops **ks_ops; 186 int ks_num_ops; 187 int flags; 188#define HX509_CTX_VERIFY_MISSING_OK 1 189 int ocsp_time_diff; 190#define HX509_DEFAULT_OCSP_TIME_DIFF (5*60) 191 hx509_error error; 192 struct et_list *et_list; 193 char *querystat; 194 hx509_certs default_trust_anchors; 195}; 196 197/* _hx509_calculate_path flag field */ 198#define HX509_CALCULATE_PATH_NO_ANCHOR 1 199 200/* environment */ 201struct hx509_env_data { 202 enum { env_string, env_list } type; 203 char *name; 204 struct hx509_env_data *next; 205 union { 206 char *string; 207 struct hx509_env_data *list; 208 } u; 209}; 210 211 212extern const AlgorithmIdentifier * _hx509_crypto_default_sig_alg; 213extern const AlgorithmIdentifier * _hx509_crypto_default_digest_alg; 214extern const AlgorithmIdentifier * _hx509_crypto_default_secret_alg; 215 216/* 217 * Configurable options 218 */ 219 220#ifdef __APPLE__ 221#define HX509_DEFAULT_ANCHORS "KEYCHAIN:system-anchors" 222#endif 223