1 1.1 elric /* $NetBSD: collector.c,v 1.2 2017/01/28 21:31:48 christos Exp $ */ 2 1.1 elric 3 1.1 elric /* 4 1.1 elric * Copyright (c) 2004 - 2007 Kungliga Tekniska Hgskolan 5 1.1 elric * (Royal Institute of Technology, Stockholm, Sweden). 6 1.1 elric * All rights reserved. 7 1.1 elric * 8 1.1 elric * Redistribution and use in source and binary forms, with or without 9 1.1 elric * modification, are permitted provided that the following conditions 10 1.1 elric * are met: 11 1.1 elric * 12 1.1 elric * 1. Redistributions of source code must retain the above copyright 13 1.1 elric * notice, this list of conditions and the following disclaimer. 14 1.1 elric * 15 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 elric * notice, this list of conditions and the following disclaimer in the 17 1.1 elric * documentation and/or other materials provided with the distribution. 18 1.1 elric * 19 1.1 elric * 3. Neither the name of the Institute nor the names of its contributors 20 1.1 elric * may be used to endorse or promote products derived from this software 21 1.1 elric * without specific prior written permission. 22 1.1 elric * 23 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24 1.1 elric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 1.1 elric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 1.1 elric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 27 1.1 elric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 1.1 elric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 1.1 elric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 1.1 elric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 1.1 elric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 1.1 elric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 1.1 elric * SUCH DAMAGE. 34 1.1 elric */ 35 1.1 elric 36 1.1 elric #include "hx_locl.h" 37 1.1 elric 38 1.1 elric struct private_key { 39 1.1 elric AlgorithmIdentifier alg; 40 1.1 elric hx509_private_key private_key; 41 1.1 elric heim_octet_string localKeyId; 42 1.1 elric }; 43 1.1 elric 44 1.1 elric struct hx509_collector { 45 1.1 elric hx509_lock lock; 46 1.1 elric hx509_certs unenvelop_certs; 47 1.1 elric hx509_certs certs; 48 1.1 elric struct { 49 1.1 elric struct private_key **data; 50 1.1 elric size_t len; 51 1.1 elric } val; 52 1.1 elric }; 53 1.1 elric 54 1.1 elric 55 1.1 elric int 56 1.1 elric _hx509_collector_alloc(hx509_context context, hx509_lock lock, struct hx509_collector **collector) 57 1.1 elric { 58 1.1 elric struct hx509_collector *c; 59 1.1 elric int ret; 60 1.1 elric 61 1.1 elric *collector = NULL; 62 1.1 elric 63 1.1 elric c = calloc(1, sizeof(*c)); 64 1.1 elric if (c == NULL) { 65 1.1 elric hx509_set_error_string(context, 0, ENOMEM, "out of memory"); 66 1.1 elric return ENOMEM; 67 1.1 elric } 68 1.1 elric c->lock = lock; 69 1.1 elric 70 1.1 elric ret = hx509_certs_init(context, "MEMORY:collector-unenvelop-cert", 71 1.1 elric 0,NULL, &c->unenvelop_certs); 72 1.1 elric if (ret) { 73 1.1 elric free(c); 74 1.1 elric return ret; 75 1.1 elric } 76 1.1 elric c->val.data = NULL; 77 1.1 elric c->val.len = 0; 78 1.1 elric ret = hx509_certs_init(context, "MEMORY:collector-tmp-store", 79 1.1 elric 0, NULL, &c->certs); 80 1.1 elric if (ret) { 81 1.1 elric hx509_certs_free(&c->unenvelop_certs); 82 1.1 elric free(c); 83 1.1 elric return ret; 84 1.1 elric } 85 1.1 elric 86 1.1 elric *collector = c; 87 1.1 elric return 0; 88 1.1 elric } 89 1.1 elric 90 1.1 elric hx509_lock 91 1.1 elric _hx509_collector_get_lock(struct hx509_collector *c) 92 1.1 elric { 93 1.1 elric return c->lock; 94 1.1 elric } 95 1.1 elric 96 1.1 elric 97 1.1 elric int 98 1.1 elric _hx509_collector_certs_add(hx509_context context, 99 1.1 elric struct hx509_collector *c, 100 1.1 elric hx509_cert cert) 101 1.1 elric { 102 1.1 elric return hx509_certs_add(context, c->certs, cert); 103 1.1 elric } 104 1.1 elric 105 1.1 elric static void 106 1.1 elric free_private_key(struct private_key *key) 107 1.1 elric { 108 1.1 elric free_AlgorithmIdentifier(&key->alg); 109 1.1 elric if (key->private_key) 110 1.1 elric hx509_private_key_free(&key->private_key); 111 1.1 elric der_free_octet_string(&key->localKeyId); 112 1.1 elric free(key); 113 1.1 elric } 114 1.1 elric 115 1.1 elric int 116 1.1 elric _hx509_collector_private_key_add(hx509_context context, 117 1.1 elric struct hx509_collector *c, 118 1.1 elric const AlgorithmIdentifier *alg, 119 1.1 elric hx509_private_key private_key, 120 1.1 elric const heim_octet_string *key_data, 121 1.1 elric const heim_octet_string *localKeyId) 122 1.1 elric { 123 1.1 elric struct private_key *key; 124 1.1 elric void *d; 125 1.1 elric int ret; 126 1.1 elric 127 1.1 elric key = calloc(1, sizeof(*key)); 128 1.1 elric if (key == NULL) 129 1.1 elric return ENOMEM; 130 1.1 elric 131 1.1 elric d = realloc(c->val.data, (c->val.len + 1) * sizeof(c->val.data[0])); 132 1.1 elric if (d == NULL) { 133 1.1 elric free(key); 134 1.1 elric hx509_set_error_string(context, 0, ENOMEM, "Out of memory"); 135 1.1 elric return ENOMEM; 136 1.1 elric } 137 1.1 elric c->val.data = d; 138 1.2 christos 139 1.1 elric ret = copy_AlgorithmIdentifier(alg, &key->alg); 140 1.1 elric if (ret) { 141 1.1 elric hx509_set_error_string(context, 0, ret, "Failed to copy " 142 1.1 elric "AlgorithmIdentifier"); 143 1.1 elric goto out; 144 1.1 elric } 145 1.1 elric if (private_key) { 146 1.1 elric key->private_key = private_key; 147 1.1 elric } else { 148 1.1 elric ret = hx509_parse_private_key(context, alg, 149 1.1 elric key_data->data, key_data->length, 150 1.1 elric HX509_KEY_FORMAT_DER, 151 1.1 elric &key->private_key); 152 1.1 elric if (ret) 153 1.1 elric goto out; 154 1.1 elric } 155 1.1 elric if (localKeyId) { 156 1.1 elric ret = der_copy_octet_string(localKeyId, &key->localKeyId); 157 1.1 elric if (ret) { 158 1.1 elric hx509_set_error_string(context, 0, ret, 159 1.1 elric "Failed to copy localKeyId"); 160 1.1 elric goto out; 161 1.1 elric } 162 1.1 elric } else 163 1.1 elric memset(&key->localKeyId, 0, sizeof(key->localKeyId)); 164 1.1 elric 165 1.1 elric c->val.data[c->val.len] = key; 166 1.1 elric c->val.len++; 167 1.1 elric 168 1.1 elric out: 169 1.1 elric if (ret) 170 1.1 elric free_private_key(key); 171 1.1 elric 172 1.1 elric return ret; 173 1.1 elric } 174 1.1 elric 175 1.1 elric static int 176 1.1 elric match_localkeyid(hx509_context context, 177 1.1 elric struct private_key *value, 178 1.1 elric hx509_certs certs) 179 1.1 elric { 180 1.1 elric hx509_cert cert; 181 1.1 elric hx509_query q; 182 1.1 elric int ret; 183 1.1 elric 184 1.1 elric if (value->localKeyId.length == 0) { 185 1.1 elric hx509_set_error_string(context, 0, HX509_LOCAL_ATTRIBUTE_MISSING, 186 1.1 elric "No local key attribute on private key"); 187 1.1 elric return HX509_LOCAL_ATTRIBUTE_MISSING; 188 1.1 elric } 189 1.1 elric 190 1.1 elric _hx509_query_clear(&q); 191 1.1 elric q.match |= HX509_QUERY_MATCH_LOCAL_KEY_ID; 192 1.1 elric 193 1.1 elric q.local_key_id = &value->localKeyId; 194 1.1 elric 195 1.1 elric ret = hx509_certs_find(context, certs, &q, &cert); 196 1.1 elric if (ret == 0) { 197 1.2 christos 198 1.1 elric if (value->private_key) 199 1.1 elric _hx509_cert_assign_key(cert, value->private_key); 200 1.1 elric hx509_cert_free(cert); 201 1.1 elric } 202 1.1 elric return ret; 203 1.1 elric } 204 1.1 elric 205 1.1 elric static int 206 1.1 elric match_keys(hx509_context context, struct private_key *value, hx509_certs certs) 207 1.1 elric { 208 1.1 elric hx509_cursor cursor; 209 1.1 elric hx509_cert c; 210 1.1 elric int ret, found = HX509_CERT_NOT_FOUND; 211 1.1 elric 212 1.1 elric if (value->private_key == NULL) { 213 1.1 elric hx509_set_error_string(context, 0, HX509_PRIVATE_KEY_MISSING, 214 1.1 elric "No private key to compare with"); 215 1.1 elric return HX509_PRIVATE_KEY_MISSING; 216 1.1 elric } 217 1.1 elric 218 1.1 elric ret = hx509_certs_start_seq(context, certs, &cursor); 219 1.1 elric if (ret) 220 1.1 elric return ret; 221 1.1 elric 222 1.1 elric c = NULL; 223 1.1 elric while (1) { 224 1.1 elric ret = hx509_certs_next_cert(context, certs, cursor, &c); 225 1.1 elric if (ret) 226 1.1 elric break; 227 1.1 elric if (c == NULL) 228 1.1 elric break; 229 1.1 elric if (_hx509_cert_private_key(c)) { 230 1.1 elric hx509_cert_free(c); 231 1.1 elric continue; 232 1.1 elric } 233 1.1 elric 234 1.1 elric ret = _hx509_match_keys(c, value->private_key); 235 1.1 elric if (ret) { 236 1.1 elric _hx509_cert_assign_key(c, value->private_key); 237 1.1 elric hx509_cert_free(c); 238 1.1 elric found = 0; 239 1.1 elric break; 240 1.1 elric } 241 1.1 elric hx509_cert_free(c); 242 1.1 elric } 243 1.1 elric 244 1.1 elric hx509_certs_end_seq(context, certs, cursor); 245 1.1 elric 246 1.1 elric if (found) 247 1.1 elric hx509_clear_error_string(context); 248 1.1 elric 249 1.1 elric return found; 250 1.1 elric } 251 1.1 elric 252 1.1 elric int 253 1.1 elric _hx509_collector_collect_certs(hx509_context context, 254 1.1 elric struct hx509_collector *c, 255 1.1 elric hx509_certs *ret_certs) 256 1.1 elric { 257 1.1 elric hx509_certs certs; 258 1.2 christos int ret; 259 1.2 christos size_t i; 260 1.1 elric 261 1.1 elric *ret_certs = NULL; 262 1.1 elric 263 1.1 elric ret = hx509_certs_init(context, "MEMORY:collector-store", 0, NULL, &certs); 264 1.1 elric if (ret) 265 1.1 elric return ret; 266 1.1 elric 267 1.1 elric ret = hx509_certs_merge(context, certs, c->certs); 268 1.1 elric if (ret) { 269 1.1 elric hx509_certs_free(&certs); 270 1.1 elric return ret; 271 1.1 elric } 272 1.1 elric 273 1.1 elric for (i = 0; i < c->val.len; i++) { 274 1.1 elric ret = match_localkeyid(context, c->val.data[i], certs); 275 1.1 elric if (ret == 0) 276 1.1 elric continue; 277 1.1 elric ret = match_keys(context, c->val.data[i], certs); 278 1.1 elric if (ret == 0) 279 1.1 elric continue; 280 1.1 elric } 281 1.1 elric 282 1.1 elric *ret_certs = certs; 283 1.1 elric 284 1.1 elric return 0; 285 1.1 elric } 286 1.1 elric 287 1.1 elric int 288 1.1 elric _hx509_collector_collect_private_keys(hx509_context context, 289 1.1 elric struct hx509_collector *c, 290 1.1 elric hx509_private_key **keys) 291 1.1 elric { 292 1.2 christos size_t i, nkeys; 293 1.1 elric 294 1.1 elric *keys = NULL; 295 1.1 elric 296 1.1 elric for (i = 0, nkeys = 0; i < c->val.len; i++) 297 1.1 elric if (c->val.data[i]->private_key) 298 1.1 elric nkeys++; 299 1.1 elric 300 1.1 elric *keys = calloc(nkeys + 1, sizeof(**keys)); 301 1.1 elric if (*keys == NULL) { 302 1.1 elric hx509_set_error_string(context, 0, ENOMEM, "malloc - out of memory"); 303 1.1 elric return ENOMEM; 304 1.1 elric } 305 1.1 elric 306 1.1 elric for (i = 0, nkeys = 0; i < c->val.len; i++) { 307 1.1 elric if (c->val.data[i]->private_key) { 308 1.1 elric (*keys)[nkeys++] = c->val.data[i]->private_key; 309 1.1 elric c->val.data[i]->private_key = NULL; 310 1.1 elric } 311 1.1 elric } 312 1.1 elric (*keys)[nkeys] = NULL; 313 1.1 elric 314 1.1 elric return 0; 315 1.1 elric } 316 1.1 elric 317 1.1 elric 318 1.1 elric void 319 1.1 elric _hx509_collector_free(struct hx509_collector *c) 320 1.1 elric { 321 1.2 christos size_t i; 322 1.1 elric 323 1.1 elric if (c->unenvelop_certs) 324 1.1 elric hx509_certs_free(&c->unenvelop_certs); 325 1.1 elric if (c->certs) 326 1.1 elric hx509_certs_free(&c->certs); 327 1.1 elric for (i = 0; i < c->val.len; i++) 328 1.1 elric free_private_key(c->val.data[i]); 329 1.1 elric if (c->val.data) 330 1.1 elric free(c->val.data); 331 1.1 elric free(c); 332 1.1 elric } 333