ec172f46f4449282e3ae91a0df7ee5cb38a47897
[sfrench/samba-autobuild/.git] / source4 / heimdal / lib / hx509 / collector.c
1 /*
2  * Copyright (c) 2004 - 2007 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 #include "hx_locl.h"
35 RCSID("$Id: collector.c,v 1.16 2007/01/09 10:52:04 lha Exp $");
36
37 struct private_key {
38     AlgorithmIdentifier alg;
39     hx509_private_key private_key;
40     heim_octet_string localKeyId;
41 };
42
43 struct hx509_collector {
44     hx509_lock lock;
45     hx509_certs unenvelop_certs;
46     hx509_certs certs;
47     struct {
48         struct private_key **data;
49         size_t len;
50     } val;
51 };
52
53
54 struct hx509_collector *
55 _hx509_collector_alloc(hx509_context context, hx509_lock lock)
56 {
57     struct hx509_collector *c;
58     int ret;
59
60     c = calloc(1, sizeof(*c));
61     if (c == NULL)
62         return NULL;
63     c->lock = lock;
64
65     ret = hx509_certs_init(context, "MEMORY:collector-unenvelop-cert",
66                            0,NULL, &c->unenvelop_certs);
67     if (ret) {
68         free(c);
69         return NULL;
70     }
71     c->val.data = NULL;
72     c->val.len = 0;
73     ret = hx509_certs_init(context, "MEMORY:collector-tmp-store",
74                            0, NULL, &c->certs);
75     if (ret) {
76         hx509_certs_free(&c->unenvelop_certs);
77         free(c);
78         return NULL;
79     }
80
81     return c;
82 }
83
84 hx509_lock
85 _hx509_collector_get_lock(struct hx509_collector *c)
86 {
87     return c->lock;
88 }
89
90
91 int
92 _hx509_collector_certs_add(hx509_context context,
93                            struct hx509_collector *c,
94                            hx509_cert cert)
95 {
96     return hx509_certs_add(context, c->certs, cert);
97 }
98
99 static void
100 free_private_key(struct private_key *key)
101 {
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);
106     free(key);
107 }
108
109 int
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)
116 {
117     struct private_key *key;
118     void *d;
119     int ret;
120
121     key = calloc(1, sizeof(*key));
122     if (key == NULL)
123         return ENOMEM;
124
125     d = realloc(c->val.data, (c->val.len + 1) * sizeof(c->val.data[0]));
126     if (d == NULL) {
127         free(key);
128         hx509_set_error_string(context, 0, ENOMEM, "Out of memory");
129         return ENOMEM;
130     }
131     c->val.data = d;
132         
133     ret = copy_AlgorithmIdentifier(alg, &key->alg);
134     if (ret) {
135         hx509_set_error_string(context, 0, ret, "Failed to copy "
136                                "AlgorithmIdentifier");
137         goto out;
138     }
139     if (private_key) {
140         key->private_key = private_key;
141     } else {
142         ret = _hx509_parse_private_key(context, &alg->algorithm,
143                                        key_data->data, key_data->length,
144                                        &key->private_key);
145         if (ret)
146             goto out;
147     }
148     if (localKeyId) {
149         ret = der_copy_octet_string(localKeyId, &key->localKeyId);
150         if (ret) {
151             hx509_set_error_string(context, 0, ret, 
152                                    "Failed to copy localKeyId");
153             goto out;
154         }
155     } else
156         memset(&key->localKeyId, 0, sizeof(key->localKeyId));
157
158     c->val.data[c->val.len] = key;
159     c->val.len++;
160
161 out:
162     if (ret)
163         free_private_key(key);
164
165     return ret;
166 }
167
168 static int
169 match_localkeyid(hx509_context context,
170                  struct private_key *value,
171                  hx509_certs certs)
172 {
173     hx509_cert cert;
174     hx509_query q;
175     int ret;
176
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;
181     }
182
183     _hx509_query_clear(&q);
184     q.match |= HX509_QUERY_MATCH_LOCAL_KEY_ID;
185     
186     q.local_key_id = &value->localKeyId;
187     
188     ret = hx509_certs_find(context, certs, &q, &cert);
189     if (ret == 0) {
190         
191         if (value->private_key)
192             _hx509_cert_assign_key(cert, value->private_key);
193         hx509_cert_free(cert);
194     }
195     return ret;
196 }
197
198 static int
199 match_keys(hx509_context context, struct private_key *value, hx509_certs certs)
200 {
201     hx509_cursor cursor;
202     hx509_cert c;
203     int ret, found = HX509_CERT_NOT_FOUND;
204
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;
209     }
210
211     ret = hx509_certs_start_seq(context, certs, &cursor);
212     if (ret)
213         return ret;
214
215     c = NULL;
216     while (1) {
217         ret = hx509_certs_next_cert(context, certs, cursor, &c);
218         if (ret)
219             break;
220         if (c == NULL)
221             break;
222         if (_hx509_cert_private_key(c)) {
223             hx509_cert_free(c);
224             continue;
225         }
226
227         ret = _hx509_match_keys(c, value->private_key);
228         if (ret) {
229             _hx509_cert_assign_key(c, value->private_key);
230             hx509_cert_free(c);
231             found = 0;
232             break;
233         }
234         hx509_cert_free(c);
235     }
236
237     hx509_certs_end_seq(context, certs, cursor);
238
239     if (found)
240         hx509_clear_error_string(context);
241
242     return found;
243 }
244
245 int
246 _hx509_collector_collect_certs(hx509_context context, 
247                                struct hx509_collector *c,
248                                hx509_certs *ret_certs)
249 {
250     hx509_certs certs;
251     int ret, i;
252
253     *ret_certs = NULL;
254
255     ret = hx509_certs_init(context, "MEMORY:collector-store", 0, NULL, &certs);
256     if (ret)
257         return ret;
258
259     ret = hx509_certs_merge(context, certs, c->certs);
260     if (ret) {
261         hx509_certs_free(&certs);
262         return ret;
263     }
264
265     for (i = 0; i < c->val.len; i++) {
266         ret = match_localkeyid(context, c->val.data[i], certs);
267         if (ret == 0)
268             continue;
269         ret = match_keys(context, c->val.data[i], certs);
270         if (ret == 0)
271             continue;
272     }
273
274     *ret_certs = certs;
275
276     return 0;
277 }
278
279 int
280 _hx509_collector_collect_private_keys(hx509_context context, 
281                                       struct hx509_collector *c,
282                                       hx509_private_key **keys)
283 {
284     int i, nkeys;
285
286     *keys = NULL;
287
288     for (i = 0, nkeys = 0; i < c->val.len; i++)
289         if (c->val.data[i]->private_key)
290             nkeys++;
291
292     *keys = calloc(nkeys + 1, sizeof(**keys));
293     if (*keys == NULL) {
294         hx509_set_error_string(context, 0, ENOMEM, "malloc - out of memory");
295         return ENOMEM;
296     }
297
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;
302         }
303     }
304     (*keys)[nkeys++] = NULL;
305
306     return 0;
307 }
308
309
310 void
311 _hx509_collector_free(struct hx509_collector *c)
312 {
313     int i;
314
315     if (c->unenvelop_certs)
316         hx509_certs_free(&c->unenvelop_certs);
317     if (c->certs)
318         hx509_certs_free(&c->certs);
319     for (i = 0; i < c->val.len; i++)
320         free_private_key(c->val.data[i]);
321     if (c->val.data)
322         free(c->val.data);
323     free(c);
324 }