Path: blob/main/crypto/heimdal/lib/hx509/collector.c
34879 views
/*1* Copyright (c) 2004 - 2007 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "hx_locl.h"3435struct private_key {36AlgorithmIdentifier alg;37hx509_private_key private_key;38heim_octet_string localKeyId;39};4041struct hx509_collector {42hx509_lock lock;43hx509_certs unenvelop_certs;44hx509_certs certs;45struct {46struct private_key **data;47size_t len;48} val;49};505152int53_hx509_collector_alloc(hx509_context context, hx509_lock lock, struct hx509_collector **collector)54{55struct hx509_collector *c;56int ret;5758*collector = NULL;5960c = calloc(1, sizeof(*c));61if (c == NULL) {62hx509_set_error_string(context, 0, ENOMEM, "out of memory");63return ENOMEM;64}65c->lock = lock;6667ret = hx509_certs_init(context, "MEMORY:collector-unenvelop-cert",680,NULL, &c->unenvelop_certs);69if (ret) {70free(c);71return ret;72}73c->val.data = NULL;74c->val.len = 0;75ret = hx509_certs_init(context, "MEMORY:collector-tmp-store",760, NULL, &c->certs);77if (ret) {78hx509_certs_free(&c->unenvelop_certs);79free(c);80return ret;81}8283*collector = c;84return 0;85}8687hx509_lock88_hx509_collector_get_lock(struct hx509_collector *c)89{90return c->lock;91}929394int95_hx509_collector_certs_add(hx509_context context,96struct hx509_collector *c,97hx509_cert cert)98{99return hx509_certs_add(context, c->certs, cert);100}101102static void103free_private_key(struct private_key *key)104{105free_AlgorithmIdentifier(&key->alg);106if (key->private_key)107hx509_private_key_free(&key->private_key);108der_free_octet_string(&key->localKeyId);109free(key);110}111112int113_hx509_collector_private_key_add(hx509_context context,114struct hx509_collector *c,115const AlgorithmIdentifier *alg,116hx509_private_key private_key,117const heim_octet_string *key_data,118const heim_octet_string *localKeyId)119{120struct private_key *key;121void *d;122int ret;123124key = calloc(1, sizeof(*key));125if (key == NULL)126return ENOMEM;127128d = realloc(c->val.data, (c->val.len + 1) * sizeof(c->val.data[0]));129if (d == NULL) {130free(key);131hx509_set_error_string(context, 0, ENOMEM, "Out of memory");132return ENOMEM;133}134c->val.data = d;135136ret = copy_AlgorithmIdentifier(alg, &key->alg);137if (ret) {138hx509_set_error_string(context, 0, ret, "Failed to copy "139"AlgorithmIdentifier");140goto out;141}142if (private_key) {143key->private_key = private_key;144} else {145ret = hx509_parse_private_key(context, alg,146key_data->data, key_data->length,147HX509_KEY_FORMAT_DER,148&key->private_key);149if (ret)150goto out;151}152if (localKeyId) {153ret = der_copy_octet_string(localKeyId, &key->localKeyId);154if (ret) {155hx509_set_error_string(context, 0, ret,156"Failed to copy localKeyId");157goto out;158}159} else160memset(&key->localKeyId, 0, sizeof(key->localKeyId));161162c->val.data[c->val.len] = key;163c->val.len++;164165out:166if (ret)167free_private_key(key);168169return ret;170}171172static int173match_localkeyid(hx509_context context,174struct private_key *value,175hx509_certs certs)176{177hx509_cert cert;178hx509_query q;179int ret;180181if (value->localKeyId.length == 0) {182hx509_set_error_string(context, 0, HX509_LOCAL_ATTRIBUTE_MISSING,183"No local key attribute on private key");184return HX509_LOCAL_ATTRIBUTE_MISSING;185}186187_hx509_query_clear(&q);188q.match |= HX509_QUERY_MATCH_LOCAL_KEY_ID;189190q.local_key_id = &value->localKeyId;191192ret = hx509_certs_find(context, certs, &q, &cert);193if (ret == 0) {194195if (value->private_key)196_hx509_cert_assign_key(cert, value->private_key);197hx509_cert_free(cert);198}199return ret;200}201202static int203match_keys(hx509_context context, struct private_key *value, hx509_certs certs)204{205hx509_cursor cursor;206hx509_cert c;207int ret, found = HX509_CERT_NOT_FOUND;208209if (value->private_key == NULL) {210hx509_set_error_string(context, 0, HX509_PRIVATE_KEY_MISSING,211"No private key to compare with");212return HX509_PRIVATE_KEY_MISSING;213}214215ret = hx509_certs_start_seq(context, certs, &cursor);216if (ret)217return ret;218219c = NULL;220while (1) {221ret = hx509_certs_next_cert(context, certs, cursor, &c);222if (ret)223break;224if (c == NULL)225break;226if (_hx509_cert_private_key(c)) {227hx509_cert_free(c);228continue;229}230231ret = _hx509_match_keys(c, value->private_key);232if (ret) {233_hx509_cert_assign_key(c, value->private_key);234hx509_cert_free(c);235found = 0;236break;237}238hx509_cert_free(c);239}240241hx509_certs_end_seq(context, certs, cursor);242243if (found)244hx509_clear_error_string(context);245246return found;247}248249int250_hx509_collector_collect_certs(hx509_context context,251struct hx509_collector *c,252hx509_certs *ret_certs)253{254hx509_certs certs;255int ret;256size_t i;257258*ret_certs = NULL;259260ret = hx509_certs_init(context, "MEMORY:collector-store", 0, NULL, &certs);261if (ret)262return ret;263264ret = hx509_certs_merge(context, certs, c->certs);265if (ret) {266hx509_certs_free(&certs);267return ret;268}269270for (i = 0; i < c->val.len; i++) {271ret = match_localkeyid(context, c->val.data[i], certs);272if (ret == 0)273continue;274ret = match_keys(context, c->val.data[i], certs);275if (ret == 0)276continue;277}278279*ret_certs = certs;280281return 0;282}283284int285_hx509_collector_collect_private_keys(hx509_context context,286struct hx509_collector *c,287hx509_private_key **keys)288{289size_t i, nkeys;290291*keys = NULL;292293for (i = 0, nkeys = 0; i < c->val.len; i++)294if (c->val.data[i]->private_key)295nkeys++;296297*keys = calloc(nkeys + 1, sizeof(**keys));298if (*keys == NULL) {299hx509_set_error_string(context, 0, ENOMEM, "malloc - out of memory");300return ENOMEM;301}302303for (i = 0, nkeys = 0; i < c->val.len; i++) {304if (c->val.data[i]->private_key) {305(*keys)[nkeys++] = c->val.data[i]->private_key;306c->val.data[i]->private_key = NULL;307}308}309(*keys)[nkeys] = NULL;310311return 0;312}313314315void316_hx509_collector_free(struct hx509_collector *c)317{318size_t i;319320if (c->unenvelop_certs)321hx509_certs_free(&c->unenvelop_certs);322if (c->certs)323hx509_certs_free(&c->certs);324for (i = 0; i < c->val.len; i++)325free_private_key(c->val.data[i]);326if (c->val.data)327free(c->val.data);328free(c);329}330331332