No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / hx509 / collector.c
blob180c3c64300f523aeb08ab274df9420f5eddd12e
1 /*
2 * Copyright (c) 2004 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "hx_locl.h"
35 __RCSID("$Heimdal: collector.c 20778 2007-06-01 22:04:13Z lha $"
36 "$NetBSD$");
38 struct private_key {
39 AlgorithmIdentifier alg;
40 hx509_private_key private_key;
41 heim_octet_string localKeyId;
44 struct hx509_collector {
45 hx509_lock lock;
46 hx509_certs unenvelop_certs;
47 hx509_certs certs;
48 struct {
49 struct private_key **data;
50 size_t len;
51 } val;
55 int
56 _hx509_collector_alloc(hx509_context context, hx509_lock lock, struct hx509_collector **collector)
58 struct hx509_collector *c;
59 int ret;
61 *collector = NULL;
63 c = calloc(1, sizeof(*c));
64 if (c == NULL) {
65 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
66 return ENOMEM;
68 c->lock = lock;
70 ret = hx509_certs_init(context, "MEMORY:collector-unenvelop-cert",
71 0,NULL, &c->unenvelop_certs);
72 if (ret) {
73 free(c);
74 return ret;
76 c->val.data = NULL;
77 c->val.len = 0;
78 ret = hx509_certs_init(context, "MEMORY:collector-tmp-store",
79 0, NULL, &c->certs);
80 if (ret) {
81 hx509_certs_free(&c->unenvelop_certs);
82 free(c);
83 return ret;
86 *collector = c;
87 return 0;
90 hx509_lock
91 _hx509_collector_get_lock(struct hx509_collector *c)
93 return c->lock;
97 int
98 _hx509_collector_certs_add(hx509_context context,
99 struct hx509_collector *c,
100 hx509_cert cert)
102 return hx509_certs_add(context, c->certs, cert);
105 static void
106 free_private_key(struct private_key *key)
108 free_AlgorithmIdentifier(&key->alg);
109 if (key->private_key)
110 _hx509_private_key_free(&key->private_key);
111 der_free_octet_string(&key->localKeyId);
112 free(key);
116 _hx509_collector_private_key_add(hx509_context context,
117 struct hx509_collector *c,
118 const AlgorithmIdentifier *alg,
119 hx509_private_key private_key,
120 const heim_octet_string *key_data,
121 const heim_octet_string *localKeyId)
123 struct private_key *key;
124 void *d;
125 int ret;
127 key = calloc(1, sizeof(*key));
128 if (key == NULL)
129 return ENOMEM;
131 d = realloc(c->val.data, (c->val.len + 1) * sizeof(c->val.data[0]));
132 if (d == NULL) {
133 free(key);
134 hx509_set_error_string(context, 0, ENOMEM, "Out of memory");
135 return ENOMEM;
137 c->val.data = d;
139 ret = copy_AlgorithmIdentifier(alg, &key->alg);
140 if (ret) {
141 hx509_set_error_string(context, 0, ret, "Failed to copy "
142 "AlgorithmIdentifier");
143 goto out;
145 if (private_key) {
146 key->private_key = private_key;
147 } else {
148 ret = _hx509_parse_private_key(context, &alg->algorithm,
149 key_data->data, key_data->length,
150 &key->private_key);
151 if (ret)
152 goto out;
154 if (localKeyId) {
155 ret = der_copy_octet_string(localKeyId, &key->localKeyId);
156 if (ret) {
157 hx509_set_error_string(context, 0, ret,
158 "Failed to copy localKeyId");
159 goto out;
161 } else
162 memset(&key->localKeyId, 0, sizeof(key->localKeyId));
164 c->val.data[c->val.len] = key;
165 c->val.len++;
167 out:
168 if (ret)
169 free_private_key(key);
171 return ret;
174 static int
175 match_localkeyid(hx509_context context,
176 struct private_key *value,
177 hx509_certs certs)
179 hx509_cert cert;
180 hx509_query q;
181 int ret;
183 if (value->localKeyId.length == 0) {
184 hx509_set_error_string(context, 0, HX509_LOCAL_ATTRIBUTE_MISSING,
185 "No local key attribute on private key");
186 return HX509_LOCAL_ATTRIBUTE_MISSING;
189 _hx509_query_clear(&q);
190 q.match |= HX509_QUERY_MATCH_LOCAL_KEY_ID;
192 q.local_key_id = &value->localKeyId;
194 ret = hx509_certs_find(context, certs, &q, &cert);
195 if (ret == 0) {
197 if (value->private_key)
198 _hx509_cert_assign_key(cert, value->private_key);
199 hx509_cert_free(cert);
201 return ret;
204 static int
205 match_keys(hx509_context context, struct private_key *value, hx509_certs certs)
207 hx509_cursor cursor;
208 hx509_cert c;
209 int ret, found = HX509_CERT_NOT_FOUND;
211 if (value->private_key == NULL) {
212 hx509_set_error_string(context, 0, HX509_PRIVATE_KEY_MISSING,
213 "No private key to compare with");
214 return HX509_PRIVATE_KEY_MISSING;
217 ret = hx509_certs_start_seq(context, certs, &cursor);
218 if (ret)
219 return ret;
221 c = NULL;
222 while (1) {
223 ret = hx509_certs_next_cert(context, certs, cursor, &c);
224 if (ret)
225 break;
226 if (c == NULL)
227 break;
228 if (_hx509_cert_private_key(c)) {
229 hx509_cert_free(c);
230 continue;
233 ret = _hx509_match_keys(c, value->private_key);
234 if (ret) {
235 _hx509_cert_assign_key(c, value->private_key);
236 hx509_cert_free(c);
237 found = 0;
238 break;
240 hx509_cert_free(c);
243 hx509_certs_end_seq(context, certs, cursor);
245 if (found)
246 hx509_clear_error_string(context);
248 return found;
252 _hx509_collector_collect_certs(hx509_context context,
253 struct hx509_collector *c,
254 hx509_certs *ret_certs)
256 hx509_certs certs;
257 int ret, i;
259 *ret_certs = NULL;
261 ret = hx509_certs_init(context, "MEMORY:collector-store", 0, NULL, &certs);
262 if (ret)
263 return ret;
265 ret = hx509_certs_merge(context, certs, c->certs);
266 if (ret) {
267 hx509_certs_free(&certs);
268 return ret;
271 for (i = 0; i < c->val.len; i++) {
272 ret = match_localkeyid(context, c->val.data[i], certs);
273 if (ret == 0)
274 continue;
275 ret = match_keys(context, c->val.data[i], certs);
276 if (ret == 0)
277 continue;
280 *ret_certs = certs;
282 return 0;
286 _hx509_collector_collect_private_keys(hx509_context context,
287 struct hx509_collector *c,
288 hx509_private_key **keys)
290 int i, nkeys;
292 *keys = NULL;
294 for (i = 0, nkeys = 0; i < c->val.len; i++)
295 if (c->val.data[i]->private_key)
296 nkeys++;
298 *keys = calloc(nkeys + 1, sizeof(**keys));
299 if (*keys == NULL) {
300 hx509_set_error_string(context, 0, ENOMEM, "malloc - out of memory");
301 return ENOMEM;
304 for (i = 0, nkeys = 0; i < c->val.len; i++) {
305 if (c->val.data[i]->private_key) {
306 (*keys)[nkeys++] = c->val.data[i]->private_key;
307 c->val.data[i]->private_key = NULL;
310 (*keys)[nkeys++] = NULL;
312 return 0;
316 void
317 _hx509_collector_free(struct hx509_collector *c)
319 int i;
321 if (c->unenvelop_certs)
322 hx509_certs_free(&c->unenvelop_certs);
323 if (c->certs)
324 hx509_certs_free(&c->certs);
325 for (i = 0; i < c->val.len; i++)
326 free_private_key(c->val.data[i]);
327 if (c->val.data)
328 free(c->val.data);
329 free(c);