2 * Copyright (c) 2004 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
35 RCSID("$Id: collector.c,v 1.16 2007/01/09 10:52:04 lha Exp $");
38 AlgorithmIdentifier alg;
39 hx509_private_key private_key;
40 heim_octet_string localKeyId;
43 struct hx509_collector {
45 hx509_certs unenvelop_certs;
48 struct private_key **data;
54 struct hx509_collector *
55 _hx509_collector_alloc(hx509_context context, hx509_lock lock)
57 struct hx509_collector *c;
60 c = calloc(1, sizeof(*c));
65 ret = hx509_certs_init(context, "MEMORY:collector-unenvelop-cert",
66 0,NULL, &c->unenvelop_certs);
73 ret = hx509_certs_init(context, "MEMORY:collector-tmp-store",
76 hx509_certs_free(&c->unenvelop_certs);
85 _hx509_collector_get_lock(struct hx509_collector *c)
92 _hx509_collector_certs_add(hx509_context context,
93 struct hx509_collector *c,
96 return hx509_certs_add(context, c->certs, cert);
100 free_private_key(struct private_key *key)
102 free_AlgorithmIdentifier(&key->alg);
103 if (key->private_key)
104 _hx509_private_key_free(&key->private_key);
105 der_free_octet_string(&key->localKeyId);
110 _hx509_collector_private_key_add(hx509_context context,
111 struct hx509_collector *c,
112 const AlgorithmIdentifier *alg,
113 hx509_private_key private_key,
114 const heim_octet_string *key_data,
115 const heim_octet_string *localKeyId)
117 struct private_key *key;
121 key = calloc(1, sizeof(*key));
125 d = realloc(c->val.data, (c->val.len + 1) * sizeof(c->val.data[0]));
128 hx509_set_error_string(context, 0, ENOMEM, "Out of memory");
133 ret = copy_AlgorithmIdentifier(alg, &key->alg);
135 hx509_set_error_string(context, 0, ret, "Failed to copy "
136 "AlgorithmIdentifier");
140 key->private_key = private_key;
142 ret = _hx509_parse_private_key(context, &alg->algorithm,
143 key_data->data, key_data->length,
149 ret = der_copy_octet_string(localKeyId, &key->localKeyId);
151 hx509_set_error_string(context, 0, ret,
152 "Failed to copy localKeyId");
156 memset(&key->localKeyId, 0, sizeof(key->localKeyId));
158 c->val.data[c->val.len] = key;
163 free_private_key(key);
169 match_localkeyid(hx509_context context,
170 struct private_key *value,
177 if (value->localKeyId.length == 0) {
178 hx509_set_error_string(context, 0, HX509_LOCAL_ATTRIBUTE_MISSING,
179 "No local key attribute on private key");
180 return HX509_LOCAL_ATTRIBUTE_MISSING;
183 _hx509_query_clear(&q);
184 q.match |= HX509_QUERY_MATCH_LOCAL_KEY_ID;
186 q.local_key_id = &value->localKeyId;
188 ret = hx509_certs_find(context, certs, &q, &cert);
191 if (value->private_key)
192 _hx509_cert_assign_key(cert, value->private_key);
193 hx509_cert_free(cert);
199 match_keys(hx509_context context, struct private_key *value, hx509_certs certs)
203 int ret, found = HX509_CERT_NOT_FOUND;
205 if (value->private_key == NULL) {
206 hx509_set_error_string(context, 0, HX509_PRIVATE_KEY_MISSING,
207 "No private key to compare with");
208 return HX509_PRIVATE_KEY_MISSING;
211 ret = hx509_certs_start_seq(context, certs, &cursor);
217 ret = hx509_certs_next_cert(context, certs, cursor, &c);
222 if (_hx509_cert_private_key(c)) {
227 ret = _hx509_match_keys(c, value->private_key);
229 _hx509_cert_assign_key(c, value->private_key);
237 hx509_certs_end_seq(context, certs, cursor);
240 hx509_clear_error_string(context);
246 _hx509_collector_collect_certs(hx509_context context,
247 struct hx509_collector *c,
248 hx509_certs *ret_certs)
255 ret = hx509_certs_init(context, "MEMORY:collector-store", 0, NULL, &certs);
259 ret = hx509_certs_merge(context, certs, c->certs);
261 hx509_certs_free(&certs);
265 for (i = 0; i < c->val.len; i++) {
266 ret = match_localkeyid(context, c->val.data[i], certs);
269 ret = match_keys(context, c->val.data[i], certs);
280 _hx509_collector_collect_private_keys(hx509_context context,
281 struct hx509_collector *c,
282 hx509_private_key **keys)
288 for (i = 0, nkeys = 0; i < c->val.len; i++)
289 if (c->val.data[i]->private_key)
292 *keys = calloc(nkeys + 1, sizeof(**keys));
294 hx509_set_error_string(context, 0, ENOMEM, "malloc - out of memory");
298 for (i = 0, nkeys = 0; i < c->val.len; i++) {
299 if (c->val.data[i]->private_key) {
300 (*keys)[nkeys++] = c->val.data[i]->private_key;
301 c->val.data[i]->private_key = NULL;
304 (*keys)[nkeys++] = NULL;
311 _hx509_collector_free(struct hx509_collector *c)
315 if (c->unenvelop_certs)
316 hx509_certs_free(&c->unenvelop_certs);
318 hx509_certs_free(&c->certs);
319 for (i = 0; i < c->val.len; i++)
320 free_private_key(c->val.data[i]);