xref: /freebsd/crypto/heimdal/lib/hx509/ks_mem.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1c19800e8SDoug Rabson /*
2*ae771770SStanislav Sedov  * Copyright (c) 2005 - 2006 Kungliga Tekniska Högskolan
3c19800e8SDoug Rabson  * (Royal Institute of Technology, Stockholm, Sweden).
4c19800e8SDoug Rabson  * All rights reserved.
5c19800e8SDoug Rabson  *
6c19800e8SDoug Rabson  * Redistribution and use in source and binary forms, with or without
7c19800e8SDoug Rabson  * modification, are permitted provided that the following conditions
8c19800e8SDoug Rabson  * are met:
9c19800e8SDoug Rabson  *
10c19800e8SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
11c19800e8SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
12c19800e8SDoug Rabson  *
13c19800e8SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
14c19800e8SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
15c19800e8SDoug Rabson  *    documentation and/or other materials provided with the distribution.
16c19800e8SDoug Rabson  *
17c19800e8SDoug Rabson  * 3. Neither the name of the Institute nor the names of its contributors
18c19800e8SDoug Rabson  *    may be used to endorse or promote products derived from this software
19c19800e8SDoug Rabson  *    without specific prior written permission.
20c19800e8SDoug Rabson  *
21c19800e8SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22c19800e8SDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23c19800e8SDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24c19800e8SDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25c19800e8SDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26c19800e8SDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27c19800e8SDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28c19800e8SDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29c19800e8SDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30c19800e8SDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31c19800e8SDoug Rabson  * SUCH DAMAGE.
32c19800e8SDoug Rabson  */
33c19800e8SDoug Rabson 
34c19800e8SDoug Rabson #include "hx_locl.h"
35c19800e8SDoug Rabson 
36c19800e8SDoug Rabson /*
37c19800e8SDoug Rabson  * Should use two hash/tree certificates intead of a array.  Criteria
38c19800e8SDoug Rabson  * should be subject and subjectKeyIdentifier since those two are
39c19800e8SDoug Rabson  * commonly seached on in CMS and path building.
40c19800e8SDoug Rabson  */
41c19800e8SDoug Rabson 
42c19800e8SDoug Rabson struct mem_data {
43c19800e8SDoug Rabson     char *name;
44c19800e8SDoug Rabson     struct {
45c19800e8SDoug Rabson 	unsigned long len;
46c19800e8SDoug Rabson 	hx509_cert *val;
47c19800e8SDoug Rabson     } certs;
48c19800e8SDoug Rabson     hx509_private_key *keys;
49c19800e8SDoug Rabson };
50c19800e8SDoug Rabson 
51c19800e8SDoug Rabson static int
mem_init(hx509_context context,hx509_certs certs,void ** data,int flags,const char * residue,hx509_lock lock)52c19800e8SDoug Rabson mem_init(hx509_context context,
53c19800e8SDoug Rabson 	 hx509_certs certs, void **data, int flags,
54c19800e8SDoug Rabson 	 const char *residue, hx509_lock lock)
55c19800e8SDoug Rabson {
56c19800e8SDoug Rabson     struct mem_data *mem;
57c19800e8SDoug Rabson     mem = calloc(1, sizeof(*mem));
58c19800e8SDoug Rabson     if (mem == NULL)
59c19800e8SDoug Rabson 	return ENOMEM;
60c19800e8SDoug Rabson     if (residue == NULL || residue[0] == '\0')
61c19800e8SDoug Rabson 	residue = "anonymous";
62c19800e8SDoug Rabson     mem->name = strdup(residue);
63c19800e8SDoug Rabson     if (mem->name == NULL) {
64c19800e8SDoug Rabson 	free(mem);
65c19800e8SDoug Rabson 	return ENOMEM;
66c19800e8SDoug Rabson     }
67c19800e8SDoug Rabson     *data = mem;
68c19800e8SDoug Rabson     return 0;
69c19800e8SDoug Rabson }
70c19800e8SDoug Rabson 
71c19800e8SDoug Rabson static int
mem_free(hx509_certs certs,void * data)72c19800e8SDoug Rabson mem_free(hx509_certs certs, void *data)
73c19800e8SDoug Rabson {
74c19800e8SDoug Rabson     struct mem_data *mem = data;
75c19800e8SDoug Rabson     unsigned long i;
76c19800e8SDoug Rabson 
77c19800e8SDoug Rabson     for (i = 0; i < mem->certs.len; i++)
78c19800e8SDoug Rabson 	hx509_cert_free(mem->certs.val[i]);
79c19800e8SDoug Rabson     free(mem->certs.val);
80c19800e8SDoug Rabson     for (i = 0; mem->keys && mem->keys[i]; i++)
81*ae771770SStanislav Sedov 	hx509_private_key_free(&mem->keys[i]);
82c19800e8SDoug Rabson     free(mem->keys);
83c19800e8SDoug Rabson     free(mem->name);
84c19800e8SDoug Rabson     free(mem);
85c19800e8SDoug Rabson 
86c19800e8SDoug Rabson     return 0;
87c19800e8SDoug Rabson }
88c19800e8SDoug Rabson 
89c19800e8SDoug Rabson static int
mem_add(hx509_context context,hx509_certs certs,void * data,hx509_cert c)90c19800e8SDoug Rabson mem_add(hx509_context context, hx509_certs certs, void *data, hx509_cert c)
91c19800e8SDoug Rabson {
92c19800e8SDoug Rabson     struct mem_data *mem = data;
93c19800e8SDoug Rabson     hx509_cert *val;
94c19800e8SDoug Rabson 
95c19800e8SDoug Rabson     val = realloc(mem->certs.val,
96c19800e8SDoug Rabson 		  (mem->certs.len + 1) * sizeof(mem->certs.val[0]));
97c19800e8SDoug Rabson     if (val == NULL)
98c19800e8SDoug Rabson 	return ENOMEM;
99c19800e8SDoug Rabson 
100c19800e8SDoug Rabson     mem->certs.val = val;
101c19800e8SDoug Rabson     mem->certs.val[mem->certs.len] = hx509_cert_ref(c);
102c19800e8SDoug Rabson     mem->certs.len++;
103c19800e8SDoug Rabson 
104c19800e8SDoug Rabson     return 0;
105c19800e8SDoug Rabson }
106c19800e8SDoug Rabson 
107c19800e8SDoug Rabson static int
mem_iter_start(hx509_context context,hx509_certs certs,void * data,void ** cursor)108c19800e8SDoug Rabson mem_iter_start(hx509_context context,
109c19800e8SDoug Rabson 	       hx509_certs certs,
110c19800e8SDoug Rabson 	       void *data,
111c19800e8SDoug Rabson 	       void **cursor)
112c19800e8SDoug Rabson {
113c19800e8SDoug Rabson     unsigned long *iter = malloc(sizeof(*iter));
114c19800e8SDoug Rabson 
115c19800e8SDoug Rabson     if (iter == NULL)
116c19800e8SDoug Rabson 	return ENOMEM;
117c19800e8SDoug Rabson 
118c19800e8SDoug Rabson     *iter = 0;
119c19800e8SDoug Rabson     *cursor = iter;
120c19800e8SDoug Rabson 
121c19800e8SDoug Rabson     return 0;
122c19800e8SDoug Rabson }
123c19800e8SDoug Rabson 
124c19800e8SDoug Rabson static int
mem_iter(hx509_context contexst,hx509_certs certs,void * data,void * cursor,hx509_cert * cert)125c19800e8SDoug Rabson mem_iter(hx509_context contexst,
126c19800e8SDoug Rabson 	 hx509_certs certs,
127c19800e8SDoug Rabson 	 void *data,
128c19800e8SDoug Rabson 	 void *cursor,
129c19800e8SDoug Rabson 	 hx509_cert *cert)
130c19800e8SDoug Rabson {
131c19800e8SDoug Rabson     unsigned long *iter = cursor;
132c19800e8SDoug Rabson     struct mem_data *mem = data;
133c19800e8SDoug Rabson 
134c19800e8SDoug Rabson     if (*iter >= mem->certs.len) {
135c19800e8SDoug Rabson 	*cert = NULL;
136c19800e8SDoug Rabson 	return 0;
137c19800e8SDoug Rabson     }
138c19800e8SDoug Rabson 
139c19800e8SDoug Rabson     *cert = hx509_cert_ref(mem->certs.val[*iter]);
140c19800e8SDoug Rabson     (*iter)++;
141c19800e8SDoug Rabson     return 0;
142c19800e8SDoug Rabson }
143c19800e8SDoug Rabson 
144c19800e8SDoug Rabson static int
mem_iter_end(hx509_context context,hx509_certs certs,void * data,void * cursor)145c19800e8SDoug Rabson mem_iter_end(hx509_context context,
146c19800e8SDoug Rabson 	     hx509_certs certs,
147c19800e8SDoug Rabson 	     void *data,
148c19800e8SDoug Rabson 	     void *cursor)
149c19800e8SDoug Rabson {
150c19800e8SDoug Rabson     free(cursor);
151c19800e8SDoug Rabson     return 0;
152c19800e8SDoug Rabson }
153c19800e8SDoug Rabson 
154c19800e8SDoug Rabson static int
mem_getkeys(hx509_context context,hx509_certs certs,void * data,hx509_private_key ** keys)155c19800e8SDoug Rabson mem_getkeys(hx509_context context,
156c19800e8SDoug Rabson 	     hx509_certs certs,
157c19800e8SDoug Rabson 	     void *data,
158c19800e8SDoug Rabson 	     hx509_private_key **keys)
159c19800e8SDoug Rabson {
160c19800e8SDoug Rabson     struct mem_data *mem = data;
161c19800e8SDoug Rabson     int i;
162c19800e8SDoug Rabson 
163c19800e8SDoug Rabson     for (i = 0; mem->keys && mem->keys[i]; i++)
164c19800e8SDoug Rabson 	;
165c19800e8SDoug Rabson     *keys = calloc(i + 1, sizeof(**keys));
166c19800e8SDoug Rabson     for (i = 0; mem->keys && mem->keys[i]; i++) {
167c19800e8SDoug Rabson 	(*keys)[i] = _hx509_private_key_ref(mem->keys[i]);
168c19800e8SDoug Rabson 	if ((*keys)[i] == NULL) {
169c19800e8SDoug Rabson 	    while (--i >= 0)
170*ae771770SStanislav Sedov 		hx509_private_key_free(&(*keys)[i]);
171c19800e8SDoug Rabson 	    hx509_set_error_string(context, 0, ENOMEM, "out of memory");
172c19800e8SDoug Rabson 	    return ENOMEM;
173c19800e8SDoug Rabson 	}
174c19800e8SDoug Rabson     }
175c19800e8SDoug Rabson     (*keys)[i] = NULL;
176c19800e8SDoug Rabson     return 0;
177c19800e8SDoug Rabson }
178c19800e8SDoug Rabson 
179c19800e8SDoug Rabson static int
mem_addkey(hx509_context context,hx509_certs certs,void * data,hx509_private_key key)180c19800e8SDoug Rabson mem_addkey(hx509_context context,
181c19800e8SDoug Rabson 	   hx509_certs certs,
182c19800e8SDoug Rabson 	   void *data,
183c19800e8SDoug Rabson 	   hx509_private_key key)
184c19800e8SDoug Rabson {
185c19800e8SDoug Rabson     struct mem_data *mem = data;
186c19800e8SDoug Rabson     void *ptr;
187c19800e8SDoug Rabson     int i;
188c19800e8SDoug Rabson 
189c19800e8SDoug Rabson     for (i = 0; mem->keys && mem->keys[i]; i++)
190c19800e8SDoug Rabson 	;
191c19800e8SDoug Rabson     ptr = realloc(mem->keys, (i + 2) * sizeof(*mem->keys));
192c19800e8SDoug Rabson     if (ptr == NULL) {
193c19800e8SDoug Rabson 	hx509_set_error_string(context, 0, ENOMEM, "out of memory");
194c19800e8SDoug Rabson 	return ENOMEM;
195c19800e8SDoug Rabson     }
196c19800e8SDoug Rabson     mem->keys = ptr;
197*ae771770SStanislav Sedov     mem->keys[i] = _hx509_private_key_ref(key);
198*ae771770SStanislav Sedov     mem->keys[i + 1] = NULL;
199c19800e8SDoug Rabson     return 0;
200c19800e8SDoug Rabson }
201c19800e8SDoug Rabson 
202c19800e8SDoug Rabson 
203c19800e8SDoug Rabson static struct hx509_keyset_ops keyset_mem = {
204c19800e8SDoug Rabson     "MEMORY",
205c19800e8SDoug Rabson     0,
206c19800e8SDoug Rabson     mem_init,
207c19800e8SDoug Rabson     NULL,
208c19800e8SDoug Rabson     mem_free,
209c19800e8SDoug Rabson     mem_add,
210c19800e8SDoug Rabson     NULL,
211c19800e8SDoug Rabson     mem_iter_start,
212c19800e8SDoug Rabson     mem_iter,
213c19800e8SDoug Rabson     mem_iter_end,
214c19800e8SDoug Rabson     NULL,
215c19800e8SDoug Rabson     mem_getkeys,
216c19800e8SDoug Rabson     mem_addkey
217c19800e8SDoug Rabson };
218c19800e8SDoug Rabson 
219c19800e8SDoug Rabson void
_hx509_ks_mem_register(hx509_context context)220c19800e8SDoug Rabson _hx509_ks_mem_register(hx509_context context)
221c19800e8SDoug Rabson {
222c19800e8SDoug Rabson     _hx509_ks_register(context, &keyset_mem);
223c19800e8SDoug Rabson }
224