1178825Sdfr/*
2233294Sstas * Copyright (c) 2004 - 2006 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
5178825Sdfr *
6233294Sstas * Redistribution and use in source and binary forms, with or without
7233294Sstas * modification, are permitted provided that the following conditions
8233294Sstas * are met:
9178825Sdfr *
10233294Sstas * 1. Redistributions of source code must retain the above copyright
11233294Sstas *    notice, this list of conditions and the following disclaimer.
12178825Sdfr *
13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
14233294Sstas *    notice, this list of conditions and the following disclaimer in the
15233294Sstas *    documentation and/or other materials provided with the distribution.
16178825Sdfr *
17233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
18233294Sstas *    may be used to endorse or promote products derived from this software
19233294Sstas *    without specific prior written permission.
20178825Sdfr *
21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31233294Sstas * SUCH DAMAGE.
32178825Sdfr */
33178825Sdfr
34233294Sstas/* $Id$ */
35178825Sdfr
36178825Sdfr#include <config.h>
37178825Sdfr
38178825Sdfr#include <stdio.h>
39178825Sdfr#include <stdlib.h>
40178825Sdfr#include <ctype.h>
41178825Sdfr#include <errno.h>
42233294Sstas#ifdef HAVE_STRINGS_H
43178825Sdfr#include <strings.h>
44233294Sstas#endif
45178825Sdfr#include <assert.h>
46178825Sdfr#include <stdarg.h>
47178825Sdfr#include <err.h>
48233294Sstas#include <limits.h>
49233294Sstas
50233294Sstas#include <roken.h>
51233294Sstas
52178825Sdfr#include <getarg.h>
53178825Sdfr#include <base64.h>
54178825Sdfr#include <hex.h>
55178825Sdfr#include <com_err.h>
56178825Sdfr#include <parse_units.h>
57178825Sdfr#include <parse_bytes.h>
58178825Sdfr
59178825Sdfr#include <krb5-types.h>
60178825Sdfr
61178825Sdfr#include <rfc2459_asn1.h>
62178825Sdfr#include <cms_asn1.h>
63178825Sdfr#include <pkcs8_asn1.h>
64178825Sdfr#include <pkcs9_asn1.h>
65178825Sdfr#include <pkcs12_asn1.h>
66178825Sdfr#include <ocsp_asn1.h>
67178825Sdfr#include <pkcs10_asn1.h>
68178825Sdfr#include <asn1_err.h>
69178825Sdfr#include <pkinit_asn1.h>
70178825Sdfr
71178825Sdfr#include <der.h>
72178825Sdfr
73233294Sstas#define HC_DEPRECATED_CRYPTO
74178825Sdfr#include "crypto-headers.h"
75178825Sdfr
76178825Sdfrstruct hx509_keyset_ops;
77178825Sdfrstruct hx509_collector;
78178825Sdfrstruct hx509_generate_private_context;
79178825Sdfrtypedef struct hx509_path hx509_path;
80178825Sdfr
81178825Sdfr#include <hx509.h>
82178825Sdfr
83178825Sdfrtypedef void (*_hx509_cert_release_func)(struct hx509_cert_data *, void *);
84178825Sdfr
85178825Sdfr
86233294Sstas#include "sel.h"
87233294Sstas
88178825Sdfr#include <hx509-private.h>
89178825Sdfr#include <hx509_err.h>
90178825Sdfr
91178825Sdfrstruct hx509_peer_info {
92178825Sdfr    hx509_cert cert;
93178825Sdfr    AlgorithmIdentifier *val;
94178825Sdfr    size_t len;
95178825Sdfr};
96178825Sdfr
97178825Sdfr#define HX509_CERTS_FIND_SERIALNUMBER		1
98178825Sdfr#define HX509_CERTS_FIND_ISSUER			2
99178825Sdfr#define HX509_CERTS_FIND_SUBJECT		4
100178825Sdfr#define HX509_CERTS_FIND_ISSUER_KEY_ID		8
101178825Sdfr#define HX509_CERTS_FIND_SUBJECT_KEY_ID		16
102178825Sdfr
103178825Sdfrstruct hx509_name_data {
104178825Sdfr    Name der_name;
105178825Sdfr};
106178825Sdfr
107178825Sdfrstruct hx509_path {
108178825Sdfr    size_t len;
109178825Sdfr    hx509_cert *val;
110178825Sdfr};
111178825Sdfr
112178825Sdfrstruct hx509_query_data {
113178825Sdfr    int match;
114178825Sdfr#define HX509_QUERY_FIND_ISSUER_CERT		0x000001
115178825Sdfr#define HX509_QUERY_MATCH_SERIALNUMBER		0x000002
116178825Sdfr#define HX509_QUERY_MATCH_ISSUER_NAME		0x000004
117178825Sdfr#define HX509_QUERY_MATCH_SUBJECT_NAME		0x000008
118178825Sdfr#define HX509_QUERY_MATCH_SUBJECT_KEY_ID	0x000010
119178825Sdfr#define HX509_QUERY_MATCH_ISSUER_ID		0x000020
120178825Sdfr#define HX509_QUERY_PRIVATE_KEY			0x000040
121178825Sdfr#define HX509_QUERY_KU_ENCIPHERMENT		0x000080
122178825Sdfr#define HX509_QUERY_KU_DIGITALSIGNATURE		0x000100
123178825Sdfr#define HX509_QUERY_KU_KEYCERTSIGN		0x000200
124178825Sdfr#define HX509_QUERY_KU_CRLSIGN			0x000400
125178825Sdfr#define HX509_QUERY_KU_NONREPUDIATION		0x000800
126178825Sdfr#define HX509_QUERY_KU_KEYAGREEMENT		0x001000
127178825Sdfr#define HX509_QUERY_KU_DATAENCIPHERMENT		0x002000
128178825Sdfr#define HX509_QUERY_ANCHOR			0x004000
129178825Sdfr#define HX509_QUERY_MATCH_CERTIFICATE		0x008000
130178825Sdfr#define HX509_QUERY_MATCH_LOCAL_KEY_ID		0x010000
131178825Sdfr#define HX509_QUERY_NO_MATCH_PATH		0x020000
132178825Sdfr#define HX509_QUERY_MATCH_FRIENDLY_NAME		0x040000
133178825Sdfr#define HX509_QUERY_MATCH_FUNCTION		0x080000
134178825Sdfr#define HX509_QUERY_MATCH_KEY_HASH_SHA1		0x100000
135178825Sdfr#define HX509_QUERY_MATCH_TIME			0x200000
136233294Sstas#define HX509_QUERY_MATCH_EKU			0x400000
137233294Sstas#define HX509_QUERY_MATCH_EXPR			0x800000
138233294Sstas#define HX509_QUERY_MASK			0xffffff
139178825Sdfr    Certificate *subject;
140178825Sdfr    Certificate *certificate;
141178825Sdfr    heim_integer *serial;
142178825Sdfr    heim_octet_string *subject_id;
143178825Sdfr    heim_octet_string *local_key_id;
144178825Sdfr    Name *issuer_name;
145178825Sdfr    Name *subject_name;
146178825Sdfr    hx509_path *path;
147178825Sdfr    char *friendlyname;
148233294Sstas    int (*cmp_func)(hx509_context, hx509_cert, void *);
149178825Sdfr    void *cmp_func_ctx;
150178825Sdfr    heim_octet_string *keyhash_sha1;
151178825Sdfr    time_t timenow;
152233294Sstas    heim_oid *eku;
153233294Sstas    struct hx_expr *expr;
154178825Sdfr};
155178825Sdfr
156178825Sdfrstruct hx509_keyset_ops {
157178825Sdfr    const char *name;
158178825Sdfr    int flags;
159233294Sstas    int (*init)(hx509_context, hx509_certs, void **,
160178825Sdfr		int, const char *, hx509_lock);
161178825Sdfr    int (*store)(hx509_context, hx509_certs, void *, int, hx509_lock);
162178825Sdfr    int (*free)(hx509_certs, void *);
163178825Sdfr    int (*add)(hx509_context, hx509_certs, void *, hx509_cert);
164233294Sstas    int (*query)(hx509_context, hx509_certs, void *,
165178825Sdfr		 const hx509_query *, hx509_cert *);
166178825Sdfr    int (*iter_start)(hx509_context, hx509_certs, void *, void **);
167178825Sdfr    int (*iter)(hx509_context, hx509_certs, void *, void *, hx509_cert *);
168178825Sdfr    int (*iter_end)(hx509_context, hx509_certs, void *, void *);
169233294Sstas    int (*printinfo)(hx509_context, hx509_certs,
170178825Sdfr		     void *, int (*)(void *, const char *), void *);
171178825Sdfr    int (*getkeys)(hx509_context, hx509_certs, void *, hx509_private_key **);
172178825Sdfr    int (*addkey)(hx509_context, hx509_certs, void *, hx509_private_key);
173178825Sdfr};
174178825Sdfr
175178825Sdfrstruct _hx509_password {
176178825Sdfr    size_t len;
177178825Sdfr    char **val;
178178825Sdfr};
179178825Sdfr
180178825Sdfrextern hx509_lock _hx509_empty_lock;
181178825Sdfr
182178825Sdfrstruct hx509_context_data {
183178825Sdfr    struct hx509_keyset_ops **ks_ops;
184178825Sdfr    int ks_num_ops;
185178825Sdfr    int flags;
186178825Sdfr#define HX509_CTX_VERIFY_MISSING_OK	1
187178825Sdfr    int ocsp_time_diff;
188178825Sdfr#define HX509_DEFAULT_OCSP_TIME_DIFF	(5*60)
189178825Sdfr    hx509_error error;
190178825Sdfr    struct et_list *et_list;
191178825Sdfr    char *querystat;
192178825Sdfr    hx509_certs default_trust_anchors;
193178825Sdfr};
194178825Sdfr
195178825Sdfr/* _hx509_calculate_path flag field */
196178825Sdfr#define HX509_CALCULATE_PATH_NO_ANCHOR 1
197178825Sdfr
198233294Sstas/* environment */
199233294Sstasstruct hx509_env_data {
200233294Sstas    enum { env_string, env_list } type;
201233294Sstas    char *name;
202233294Sstas    struct hx509_env_data *next;
203233294Sstas    union {
204233294Sstas	char *string;
205233294Sstas	struct hx509_env_data *list;
206233294Sstas    } u;
207233294Sstas};
208233294Sstas
209233294Sstas
210178825Sdfrextern const AlgorithmIdentifier * _hx509_crypto_default_sig_alg;
211178825Sdfrextern const AlgorithmIdentifier * _hx509_crypto_default_digest_alg;
212178825Sdfrextern const AlgorithmIdentifier * _hx509_crypto_default_secret_alg;
213178825Sdfr
214178825Sdfr/*
215178825Sdfr * Configurable options
216178825Sdfr */
217178825Sdfr
218178825Sdfr#ifdef __APPLE__
219178825Sdfr#define HX509_DEFAULT_ANCHORS "KEYCHAIN:system-anchors"
220178825Sdfr#endif
221