1 /* $NetBSD: collector.c,v 1.1.1.2 2014/04/24 12:45:41 pettai Exp $ */
4 * Copyright (c) 2004 - 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 AlgorithmIdentifier alg
;
40 hx509_private_key private_key
;
41 heim_octet_string localKeyId
;
44 struct hx509_collector
{
46 hx509_certs unenvelop_certs
;
49 struct private_key
**data
;
56 _hx509_collector_alloc(hx509_context context
, hx509_lock lock
, struct hx509_collector
**collector
)
58 struct hx509_collector
*c
;
63 c
= calloc(1, sizeof(*c
));
65 hx509_set_error_string(context
, 0, ENOMEM
, "out of memory");
70 ret
= hx509_certs_init(context
, "MEMORY:collector-unenvelop-cert",
71 0,NULL
, &c
->unenvelop_certs
);
78 ret
= hx509_certs_init(context
, "MEMORY:collector-tmp-store",
81 hx509_certs_free(&c
->unenvelop_certs
);
91 _hx509_collector_get_lock(struct hx509_collector
*c
)
98 _hx509_collector_certs_add(hx509_context context
,
99 struct hx509_collector
*c
,
102 return hx509_certs_add(context
, c
->certs
, cert
);
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
);
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
;
127 key
= calloc(1, sizeof(*key
));
131 d
= realloc(c
->val
.data
, (c
->val
.len
+ 1) * sizeof(c
->val
.data
[0]));
134 hx509_set_error_string(context
, 0, ENOMEM
, "Out of memory");
139 ret
= copy_AlgorithmIdentifier(alg
, &key
->alg
);
141 hx509_set_error_string(context
, 0, ret
, "Failed to copy "
142 "AlgorithmIdentifier");
146 key
->private_key
= private_key
;
148 ret
= hx509_parse_private_key(context
, alg
,
149 key_data
->data
, key_data
->length
,
150 HX509_KEY_FORMAT_DER
,
156 ret
= der_copy_octet_string(localKeyId
, &key
->localKeyId
);
158 hx509_set_error_string(context
, 0, ret
,
159 "Failed to copy localKeyId");
163 memset(&key
->localKeyId
, 0, sizeof(key
->localKeyId
));
165 c
->val
.data
[c
->val
.len
] = key
;
170 free_private_key(key
);
176 match_localkeyid(hx509_context context
,
177 struct private_key
*value
,
184 if (value
->localKeyId
.length
== 0) {
185 hx509_set_error_string(context
, 0, HX509_LOCAL_ATTRIBUTE_MISSING
,
186 "No local key attribute on private key");
187 return HX509_LOCAL_ATTRIBUTE_MISSING
;
190 _hx509_query_clear(&q
);
191 q
.match
|= HX509_QUERY_MATCH_LOCAL_KEY_ID
;
193 q
.local_key_id
= &value
->localKeyId
;
195 ret
= hx509_certs_find(context
, certs
, &q
, &cert
);
198 if (value
->private_key
)
199 _hx509_cert_assign_key(cert
, value
->private_key
);
200 hx509_cert_free(cert
);
206 match_keys(hx509_context context
, struct private_key
*value
, hx509_certs certs
)
210 int ret
, found
= HX509_CERT_NOT_FOUND
;
212 if (value
->private_key
== NULL
) {
213 hx509_set_error_string(context
, 0, HX509_PRIVATE_KEY_MISSING
,
214 "No private key to compare with");
215 return HX509_PRIVATE_KEY_MISSING
;
218 ret
= hx509_certs_start_seq(context
, certs
, &cursor
);
224 ret
= hx509_certs_next_cert(context
, certs
, cursor
, &c
);
229 if (_hx509_cert_private_key(c
)) {
234 ret
= _hx509_match_keys(c
, value
->private_key
);
236 _hx509_cert_assign_key(c
, value
->private_key
);
244 hx509_certs_end_seq(context
, certs
, cursor
);
247 hx509_clear_error_string(context
);
253 _hx509_collector_collect_certs(hx509_context context
,
254 struct hx509_collector
*c
,
255 hx509_certs
*ret_certs
)
263 ret
= hx509_certs_init(context
, "MEMORY:collector-store", 0, NULL
, &certs
);
267 ret
= hx509_certs_merge(context
, certs
, c
->certs
);
269 hx509_certs_free(&certs
);
273 for (i
= 0; i
< c
->val
.len
; i
++) {
274 ret
= match_localkeyid(context
, c
->val
.data
[i
], certs
);
277 ret
= match_keys(context
, c
->val
.data
[i
], certs
);
288 _hx509_collector_collect_private_keys(hx509_context context
,
289 struct hx509_collector
*c
,
290 hx509_private_key
**keys
)
296 for (i
= 0, nkeys
= 0; i
< c
->val
.len
; i
++)
297 if (c
->val
.data
[i
]->private_key
)
300 *keys
= calloc(nkeys
+ 1, sizeof(**keys
));
302 hx509_set_error_string(context
, 0, ENOMEM
, "malloc - out of memory");
306 for (i
= 0, nkeys
= 0; i
< c
->val
.len
; i
++) {
307 if (c
->val
.data
[i
]->private_key
) {
308 (*keys
)[nkeys
++] = c
->val
.data
[i
]->private_key
;
309 c
->val
.data
[i
]->private_key
= NULL
;
312 (*keys
)[nkeys
] = NULL
;
319 _hx509_collector_free(struct hx509_collector
*c
)
323 if (c
->unenvelop_certs
)
324 hx509_certs_free(&c
->unenvelop_certs
);
326 hx509_certs_free(&c
->certs
);
327 for (i
= 0; i
< c
->val
.len
; i
++)
328 free_private_key(c
->val
.data
[i
]);