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