1178825Sdfr/*
2178825Sdfr * Copyright (c) 2004 - 2006 Kungliga Tekniska H�gskolan
3178825Sdfr * (Royal Institute of Technology, Stockholm, Sweden).
4178825Sdfr * All rights reserved.
5178825Sdfr *
6178825Sdfr * Redistribution and use in source and binary forms, with or without
7178825Sdfr * modification, are permitted provided that the following conditions
8178825Sdfr * are met:
9178825Sdfr *
10178825Sdfr * 1. Redistributions of source code must retain the above copyright
11178825Sdfr *    notice, this list of conditions and the following disclaimer.
12178825Sdfr *
13178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright
14178825Sdfr *    notice, this list of conditions and the following disclaimer in the
15178825Sdfr *    documentation and/or other materials provided with the distribution.
16178825Sdfr *
17178825Sdfr * 3. Neither the name of the Institute nor the names of its contributors
18178825Sdfr *    may be used to endorse or promote products derived from this software
19178825Sdfr *    without specific prior written permission.
20178825Sdfr *
21178825Sdfr * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22178825Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23178825Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24178825Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25178825Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26178825Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27178825Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28178825Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29178825Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30178825Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31178825Sdfr * SUCH DAMAGE.
32178825Sdfr */
33178825Sdfr
34178825Sdfr/* $Id: hx_locl.h 21083 2007-06-13 02:11:19Z lha $ */
35178825Sdfr
36178825Sdfr#ifdef HAVE_CONFIG_H
37178825Sdfr#include <config.h>
38178825Sdfr#endif
39178825Sdfr
40178825Sdfr#include <stdio.h>
41178825Sdfr#include <stdlib.h>
42178825Sdfr#include <ctype.h>
43178825Sdfr#include <errno.h>
44178825Sdfr#include <strings.h>
45178825Sdfr#include <assert.h>
46178825Sdfr#include <stdarg.h>
47178825Sdfr#include <err.h>
48178825Sdfr#include <getarg.h>
49178825Sdfr#include <base64.h>
50178825Sdfr#include <hex.h>
51178825Sdfr#include <roken.h>
52178825Sdfr#include <com_err.h>
53178825Sdfr#include <parse_units.h>
54178825Sdfr#include <parse_bytes.h>
55178825Sdfr
56178825Sdfr#include <krb5-types.h>
57178825Sdfr
58178825Sdfr#include <rfc2459_asn1.h>
59178825Sdfr#include <cms_asn1.h>
60178825Sdfr#include <pkcs8_asn1.h>
61178825Sdfr#include <pkcs9_asn1.h>
62178825Sdfr#include <pkcs12_asn1.h>
63178825Sdfr#include <ocsp_asn1.h>
64178825Sdfr#include <pkcs10_asn1.h>
65178825Sdfr#include <asn1_err.h>
66178825Sdfr#include <pkinit_asn1.h>
67178825Sdfr
68178825Sdfr#include <der.h>
69178825Sdfr
70178825Sdfr#include "crypto-headers.h"
71178825Sdfr
72178825Sdfrstruct hx509_keyset_ops;
73178825Sdfrstruct hx509_collector;
74178825Sdfrstruct hx509_generate_private_context;
75178825Sdfrtypedef struct hx509_path hx509_path;
76178825Sdfr
77178825Sdfr#include <hx509.h>
78178825Sdfr
79178825Sdfrtypedef void (*_hx509_cert_release_func)(struct hx509_cert_data *, void *);
80178825Sdfr
81178825Sdfrtypedef struct hx509_private_key_ops hx509_private_key_ops;
82178825Sdfr
83178825Sdfr#include <hx509-private.h>
84178825Sdfr#include <hx509_err.h>
85178825Sdfr
86178825Sdfrstruct hx509_peer_info {
87178825Sdfr    hx509_cert cert;
88178825Sdfr    AlgorithmIdentifier *val;
89178825Sdfr    size_t len;
90178825Sdfr};
91178825Sdfr
92178825Sdfr#define HX509_CERTS_FIND_SERIALNUMBER		1
93178825Sdfr#define HX509_CERTS_FIND_ISSUER			2
94178825Sdfr#define HX509_CERTS_FIND_SUBJECT		4
95178825Sdfr#define HX509_CERTS_FIND_ISSUER_KEY_ID		8
96178825Sdfr#define HX509_CERTS_FIND_SUBJECT_KEY_ID		16
97178825Sdfr
98178825Sdfrstruct hx509_name_data {
99178825Sdfr    Name der_name;
100178825Sdfr};
101178825Sdfr
102178825Sdfrstruct hx509_path {
103178825Sdfr    size_t len;
104178825Sdfr    hx509_cert *val;
105178825Sdfr};
106178825Sdfr
107178825Sdfrstruct hx509_query_data {
108178825Sdfr    int match;
109178825Sdfr#define HX509_QUERY_FIND_ISSUER_CERT		0x000001
110178825Sdfr#define HX509_QUERY_MATCH_SERIALNUMBER		0x000002
111178825Sdfr#define HX509_QUERY_MATCH_ISSUER_NAME		0x000004
112178825Sdfr#define HX509_QUERY_MATCH_SUBJECT_NAME		0x000008
113178825Sdfr#define HX509_QUERY_MATCH_SUBJECT_KEY_ID	0x000010
114178825Sdfr#define HX509_QUERY_MATCH_ISSUER_ID		0x000020
115178825Sdfr#define HX509_QUERY_PRIVATE_KEY			0x000040
116178825Sdfr#define HX509_QUERY_KU_ENCIPHERMENT		0x000080
117178825Sdfr#define HX509_QUERY_KU_DIGITALSIGNATURE		0x000100
118178825Sdfr#define HX509_QUERY_KU_KEYCERTSIGN		0x000200
119178825Sdfr#define HX509_QUERY_KU_CRLSIGN			0x000400
120178825Sdfr#define HX509_QUERY_KU_NONREPUDIATION		0x000800
121178825Sdfr#define HX509_QUERY_KU_KEYAGREEMENT		0x001000
122178825Sdfr#define HX509_QUERY_KU_DATAENCIPHERMENT		0x002000
123178825Sdfr#define HX509_QUERY_ANCHOR			0x004000
124178825Sdfr#define HX509_QUERY_MATCH_CERTIFICATE		0x008000
125178825Sdfr#define HX509_QUERY_MATCH_LOCAL_KEY_ID		0x010000
126178825Sdfr#define HX509_QUERY_NO_MATCH_PATH		0x020000
127178825Sdfr#define HX509_QUERY_MATCH_FRIENDLY_NAME		0x040000
128178825Sdfr#define HX509_QUERY_MATCH_FUNCTION		0x080000
129178825Sdfr#define HX509_QUERY_MATCH_KEY_HASH_SHA1		0x100000
130178825Sdfr#define HX509_QUERY_MATCH_TIME			0x200000
131178825Sdfr#define HX509_QUERY_MASK			0x3fffff
132178825Sdfr    Certificate *subject;
133178825Sdfr    Certificate *certificate;
134178825Sdfr    heim_integer *serial;
135178825Sdfr    heim_octet_string *subject_id;
136178825Sdfr    heim_octet_string *local_key_id;
137178825Sdfr    Name *issuer_name;
138178825Sdfr    Name *subject_name;
139178825Sdfr    hx509_path *path;
140178825Sdfr    char *friendlyname;
141178825Sdfr    int (*cmp_func)(void *, hx509_cert);
142178825Sdfr    void *cmp_func_ctx;
143178825Sdfr    heim_octet_string *keyhash_sha1;
144178825Sdfr    time_t timenow;
145178825Sdfr};
146178825Sdfr
147178825Sdfrstruct hx509_keyset_ops {
148178825Sdfr    const char *name;
149178825Sdfr    int flags;
150178825Sdfr    int (*init)(hx509_context, hx509_certs, void **,
151178825Sdfr		int, const char *, hx509_lock);
152178825Sdfr    int (*store)(hx509_context, hx509_certs, void *, int, hx509_lock);
153178825Sdfr    int (*free)(hx509_certs, void *);
154178825Sdfr    int (*add)(hx509_context, hx509_certs, void *, hx509_cert);
155178825Sdfr    int (*query)(hx509_context, hx509_certs, void *,
156178825Sdfr		 const hx509_query *, hx509_cert *);
157178825Sdfr    int (*iter_start)(hx509_context, hx509_certs, void *, void **);
158178825Sdfr    int (*iter)(hx509_context, hx509_certs, void *, void *, hx509_cert *);
159178825Sdfr    int (*iter_end)(hx509_context, hx509_certs, void *, void *);
160178825Sdfr    int (*printinfo)(hx509_context, hx509_certs,
161178825Sdfr		     void *, int (*)(void *, const char *), void *);
162178825Sdfr    int (*getkeys)(hx509_context, hx509_certs, void *, hx509_private_key **);
163178825Sdfr    int (*addkey)(hx509_context, hx509_certs, void *, hx509_private_key);
164178825Sdfr};
165178825Sdfr
166178825Sdfrstruct _hx509_password {
167178825Sdfr    size_t len;
168178825Sdfr    char **val;
169178825Sdfr};
170178825Sdfr
171178825Sdfrextern hx509_lock _hx509_empty_lock;
172178825Sdfr
173178825Sdfrstruct hx509_context_data {
174178825Sdfr    struct hx509_keyset_ops **ks_ops;
175178825Sdfr    int ks_num_ops;
176178825Sdfr    int flags;
177178825Sdfr#define HX509_CTX_VERIFY_MISSING_OK	1
178178825Sdfr    int ocsp_time_diff;
179178825Sdfr#define HX509_DEFAULT_OCSP_TIME_DIFF	(5*60)
180178825Sdfr    hx509_error error;
181178825Sdfr    struct et_list *et_list;
182178825Sdfr    char *querystat;
183178825Sdfr    hx509_certs default_trust_anchors;
184178825Sdfr};
185178825Sdfr
186178825Sdfr/* _hx509_calculate_path flag field */
187178825Sdfr#define HX509_CALCULATE_PATH_NO_ANCHOR 1
188178825Sdfr
189178825Sdfrextern const AlgorithmIdentifier * _hx509_crypto_default_sig_alg;
190178825Sdfrextern const AlgorithmIdentifier * _hx509_crypto_default_digest_alg;
191178825Sdfrextern const AlgorithmIdentifier * _hx509_crypto_default_secret_alg;
192178825Sdfr
193178825Sdfr/*
194178825Sdfr * Configurable options
195178825Sdfr */
196178825Sdfr
197178825Sdfr#ifdef __APPLE__
198178825Sdfr#define HX509_DEFAULT_ANCHORS "KEYCHAIN:system-anchors"
199178825Sdfr#endif
200