Path: blob/main/crypto/heimdal/lib/hx509/ks_mem.c
107992 views
/*1* Copyright (c) 2005 - 2006 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"3435/*36* Should use two hash/tree certificates intead of a array. Criteria37* should be subject and subjectKeyIdentifier since those two are38* commonly seached on in CMS and path building.39*/4041struct mem_data {42char *name;43struct {44unsigned long len;45hx509_cert *val;46} certs;47hx509_private_key *keys;48};4950static int51mem_init(hx509_context context,52hx509_certs certs, void **data, int flags,53const char *residue, hx509_lock lock)54{55struct mem_data *mem;56mem = calloc(1, sizeof(*mem));57if (mem == NULL)58return ENOMEM;59if (residue == NULL || residue[0] == '\0')60residue = "anonymous";61mem->name = strdup(residue);62if (mem->name == NULL) {63free(mem);64return ENOMEM;65}66*data = mem;67return 0;68}6970static int71mem_free(hx509_certs certs, void *data)72{73struct mem_data *mem = data;74unsigned long i;7576for (i = 0; i < mem->certs.len; i++)77hx509_cert_free(mem->certs.val[i]);78free(mem->certs.val);79for (i = 0; mem->keys && mem->keys[i]; i++)80hx509_private_key_free(&mem->keys[i]);81free(mem->keys);82free(mem->name);83free(mem);8485return 0;86}8788static int89mem_add(hx509_context context, hx509_certs certs, void *data, hx509_cert c)90{91struct mem_data *mem = data;92hx509_cert *val;9394val = realloc(mem->certs.val,95(mem->certs.len + 1) * sizeof(mem->certs.val[0]));96if (val == NULL)97return ENOMEM;9899mem->certs.val = val;100mem->certs.val[mem->certs.len] = hx509_cert_ref(c);101mem->certs.len++;102103return 0;104}105106static int107mem_iter_start(hx509_context context,108hx509_certs certs,109void *data,110void **cursor)111{112unsigned long *iter = malloc(sizeof(*iter));113114if (iter == NULL)115return ENOMEM;116117*iter = 0;118*cursor = iter;119120return 0;121}122123static int124mem_iter(hx509_context contexst,125hx509_certs certs,126void *data,127void *cursor,128hx509_cert *cert)129{130unsigned long *iter = cursor;131struct mem_data *mem = data;132133if (*iter >= mem->certs.len) {134*cert = NULL;135return 0;136}137138*cert = hx509_cert_ref(mem->certs.val[*iter]);139(*iter)++;140return 0;141}142143static int144mem_iter_end(hx509_context context,145hx509_certs certs,146void *data,147void *cursor)148{149free(cursor);150return 0;151}152153static int154mem_getkeys(hx509_context context,155hx509_certs certs,156void *data,157hx509_private_key **keys)158{159struct mem_data *mem = data;160int i;161162for (i = 0; mem->keys && mem->keys[i]; i++)163;164*keys = calloc(i + 1, sizeof(**keys));165for (i = 0; mem->keys && mem->keys[i]; i++) {166(*keys)[i] = _hx509_private_key_ref(mem->keys[i]);167if ((*keys)[i] == NULL) {168while (--i >= 0)169hx509_private_key_free(&(*keys)[i]);170hx509_set_error_string(context, 0, ENOMEM, "out of memory");171return ENOMEM;172}173}174(*keys)[i] = NULL;175return 0;176}177178static int179mem_addkey(hx509_context context,180hx509_certs certs,181void *data,182hx509_private_key key)183{184struct mem_data *mem = data;185void *ptr;186int i;187188for (i = 0; mem->keys && mem->keys[i]; i++)189;190ptr = realloc(mem->keys, (i + 2) * sizeof(*mem->keys));191if (ptr == NULL) {192hx509_set_error_string(context, 0, ENOMEM, "out of memory");193return ENOMEM;194}195mem->keys = ptr;196mem->keys[i] = _hx509_private_key_ref(key);197mem->keys[i + 1] = NULL;198return 0;199}200201202static struct hx509_keyset_ops keyset_mem = {203"MEMORY",2040,205mem_init,206NULL,207mem_free,208mem_add,209NULL,210mem_iter_start,211mem_iter,212mem_iter_end,213NULL,214mem_getkeys,215mem_addkey216};217218void219_hx509_ks_mem_register(hx509_context context)220{221_hx509_ks_register(context, &keyset_mem);222}223224225