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: hx_locl.h 21083 2007-06-13 02:11:19Z lha $ */
35
36#ifdef HAVE_CONFIG_H
37#include <config.h>
38#endif
39
40#include <stdio.h>
41#include <stdlib.h>
42#include <ctype.h>
43#include <errno.h>
44#include <strings.h>
45#include <assert.h>
46#include <stdarg.h>
47#include <err.h>
48#include <getarg.h>
49#include <base64.h>
50#include <hex.h>
51#include <roken.h>
52#include <com_err.h>
53#include <parse_units.h>
54#include <parse_bytes.h>
55
56#include <krb5-types.h>
57
58#include <rfc2459_asn1.h>
59#include <cms_asn1.h>
60#include <pkcs8_asn1.h>
61#include <pkcs9_asn1.h>
62#include <pkcs12_asn1.h>
63#include <ocsp_asn1.h>
64#include <pkcs10_asn1.h>
65#include <asn1_err.h>
66#include <pkinit_asn1.h>
67
68#include <der.h>
69
70#include "crypto-headers.h"
71
72struct hx509_keyset_ops;
73struct hx509_collector;
74struct hx509_generate_private_context;
75typedef struct hx509_path hx509_path;
76
77#include <hx509.h>
78
79typedef void (*_hx509_cert_release_func)(struct hx509_cert_data *, void *);
80
81typedef struct hx509_private_key_ops hx509_private_key_ops;
82
83#include <hx509-private.h>
84#include <hx509_err.h>
85
86struct hx509_peer_info {
87    hx509_cert cert;
88    AlgorithmIdentifier *val;
89    size_t len;
90};
91
92#define HX509_CERTS_FIND_SERIALNUMBER		1
93#define HX509_CERTS_FIND_ISSUER			2
94#define HX509_CERTS_FIND_SUBJECT		4
95#define HX509_CERTS_FIND_ISSUER_KEY_ID		8
96#define HX509_CERTS_FIND_SUBJECT_KEY_ID		16
97
98struct hx509_name_data {
99    Name der_name;
100};
101
102struct hx509_path {
103    size_t len;
104    hx509_cert *val;
105};
106
107struct hx509_query_data {
108    int match;
109#define HX509_QUERY_FIND_ISSUER_CERT		0x000001
110#define HX509_QUERY_MATCH_SERIALNUMBER		0x000002
111#define HX509_QUERY_MATCH_ISSUER_NAME		0x000004
112#define HX509_QUERY_MATCH_SUBJECT_NAME		0x000008
113#define HX509_QUERY_MATCH_SUBJECT_KEY_ID	0x000010
114#define HX509_QUERY_MATCH_ISSUER_ID		0x000020
115#define HX509_QUERY_PRIVATE_KEY			0x000040
116#define HX509_QUERY_KU_ENCIPHERMENT		0x000080
117#define HX509_QUERY_KU_DIGITALSIGNATURE		0x000100
118#define HX509_QUERY_KU_KEYCERTSIGN		0x000200
119#define HX509_QUERY_KU_CRLSIGN			0x000400
120#define HX509_QUERY_KU_NONREPUDIATION		0x000800
121#define HX509_QUERY_KU_KEYAGREEMENT		0x001000
122#define HX509_QUERY_KU_DATAENCIPHERMENT		0x002000
123#define HX509_QUERY_ANCHOR			0x004000
124#define HX509_QUERY_MATCH_CERTIFICATE		0x008000
125#define HX509_QUERY_MATCH_LOCAL_KEY_ID		0x010000
126#define HX509_QUERY_NO_MATCH_PATH		0x020000
127#define HX509_QUERY_MATCH_FRIENDLY_NAME		0x040000
128#define HX509_QUERY_MATCH_FUNCTION		0x080000
129#define HX509_QUERY_MATCH_KEY_HASH_SHA1		0x100000
130#define HX509_QUERY_MATCH_TIME			0x200000
131#define HX509_QUERY_MASK			0x3fffff
132    Certificate *subject;
133    Certificate *certificate;
134    heim_integer *serial;
135    heim_octet_string *subject_id;
136    heim_octet_string *local_key_id;
137    Name *issuer_name;
138    Name *subject_name;
139    hx509_path *path;
140    char *friendlyname;
141    int (*cmp_func)(void *, hx509_cert);
142    void *cmp_func_ctx;
143    heim_octet_string *keyhash_sha1;
144    time_t timenow;
145};
146
147struct hx509_keyset_ops {
148    const char *name;
149    int flags;
150    int (*init)(hx509_context, hx509_certs, void **,
151		int, const char *, hx509_lock);
152    int (*store)(hx509_context, hx509_certs, void *, int, hx509_lock);
153    int (*free)(hx509_certs, void *);
154    int (*add)(hx509_context, hx509_certs, void *, hx509_cert);
155    int (*query)(hx509_context, hx509_certs, void *,
156		 const hx509_query *, hx509_cert *);
157    int (*iter_start)(hx509_context, hx509_certs, void *, void **);
158    int (*iter)(hx509_context, hx509_certs, void *, void *, hx509_cert *);
159    int (*iter_end)(hx509_context, hx509_certs, void *, void *);
160    int (*printinfo)(hx509_context, hx509_certs,
161		     void *, int (*)(void *, const char *), void *);
162    int (*getkeys)(hx509_context, hx509_certs, void *, hx509_private_key **);
163    int (*addkey)(hx509_context, hx509_certs, void *, hx509_private_key);
164};
165
166struct _hx509_password {
167    size_t len;
168    char **val;
169};
170
171extern hx509_lock _hx509_empty_lock;
172
173struct hx509_context_data {
174    struct hx509_keyset_ops **ks_ops;
175    int ks_num_ops;
176    int flags;
177#define HX509_CTX_VERIFY_MISSING_OK	1
178    int ocsp_time_diff;
179#define HX509_DEFAULT_OCSP_TIME_DIFF	(5*60)
180    hx509_error error;
181    struct et_list *et_list;
182    char *querystat;
183    hx509_certs default_trust_anchors;
184};
185
186/* _hx509_calculate_path flag field */
187#define HX509_CALCULATE_PATH_NO_ANCHOR 1
188
189extern const AlgorithmIdentifier * _hx509_crypto_default_sig_alg;
190extern const AlgorithmIdentifier * _hx509_crypto_default_digest_alg;
191extern const AlgorithmIdentifier * _hx509_crypto_default_secret_alg;
192
193/*
194 * Configurable options
195 */
196
197#ifdef __APPLE__
198#define HX509_DEFAULT_ANCHORS "KEYCHAIN:system-anchors"
199#endif
200