2 * Copyright (c) 2004 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID("$Id: keyset.c 22466 2008-01-16 14:26:35Z lha $");
38 * @page page_keyset Certificate store operations
40 * Type of certificates store:
42 * In memory based format. Doesnt support storing.
44 * FILE supports raw DER certicates and PEM certicates. When PEM is
45 * used the file can contain may certificates and match private
46 * keys. Support storing the certificates. DER format only supports
47 * on certificate and no private key.
49 * Same as FILE, defaulting to PEM encoded certificates.
51 * Same as FILE, defaulting to DER encoded certificates.
56 * Apple Mac OS X KeyChain backed keychain object.
58 * See the library functions here: @ref hx509_keyset
61 struct hx509_certs_data {
63 struct hx509_keyset_ops *ops;
67 static struct hx509_keyset_ops *
68 _hx509_ks_type(hx509_context context, const char *type)
72 for (i = 0; i < context->ks_num_ops; i++)
73 if (strcasecmp(type, context->ks_ops[i]->name) == 0)
74 return context->ks_ops[i];
80 _hx509_ks_register(hx509_context context, struct hx509_keyset_ops *ops)
82 struct hx509_keyset_ops **val;
84 if (_hx509_ks_type(context, ops->name))
87 val = realloc(context->ks_ops,
88 (context->ks_num_ops + 1) * sizeof(context->ks_ops[0]));
91 val[context->ks_num_ops] = ops;
92 context->ks_ops = val;
93 context->ks_num_ops++;
97 * Open or creates a new hx509 certificate store.
99 * @param context A hx509 context
100 * @param name name of the store, format is TYPE:type-specific-string,
101 * if NULL is used the MEMORY store is used.
102 * @param flags list of flags:
103 * - HX509_CERTS_CREATE create a new keystore of the specific TYPE.
104 * - HX509_CERTS_UNPROTECT_ALL fails if any private key failed to be extracted.
105 * @param lock a lock that unlocks the certificates store, use NULL to
106 * select no password/certifictes/prompt lock (see @ref page_lock).
107 * @param certs return pointer, free with hx509_certs_free().
109 * @ingroup hx509_keyset
113 hx509_certs_init(hx509_context context,
114 const char *name, int flags,
115 hx509_lock lock, hx509_certs *certs)
117 struct hx509_keyset_ops *ops;
125 residue = strchr(name, ':');
127 type = malloc(residue - name + 1);
129 strlcpy(type, name, residue - name + 1);
131 if (residue[0] == '\0')
134 type = strdup("MEMORY");
138 hx509_clear_error_string(context);
142 ops = _hx509_ks_type(context, type);
144 hx509_set_error_string(context, 0, ENOENT,
145 "Keyset type %s is not supported", type);
150 c = calloc(1, sizeof(*c));
152 hx509_clear_error_string(context);
158 ret = (*ops->init)(context, c, &c->ops_data, flags, residue, lock);
169 * Write the certificate store to stable storage.
171 * @param context A hx509 context.
172 * @param certs a certificate store to store.
173 * @param flags currently unused, use 0.
174 * @param lock a lock that unlocks the certificates store, use NULL to
175 * select no password/certifictes/prompt lock (see @ref page_lock).
177 * @return Returns an hx509 error code. HX509_UNSUPPORTED_OPERATION if
178 * the certificate store doesn't support the store operation.
180 * @ingroup hx509_keyset
184 hx509_certs_store(hx509_context context,
189 if (certs->ops->store == NULL) {
190 hx509_set_error_string(context, 0, HX509_UNSUPPORTED_OPERATION,
191 "keystore if type %s doesn't support "
194 return HX509_UNSUPPORTED_OPERATION;
197 return (*certs->ops->store)(context, certs, certs->ops_data, flags, lock);
202 _hx509_certs_ref(hx509_certs certs)
207 _hx509_abort("certs refcount <= 0");
210 _hx509_abort("certs refcount == 0");
215 * Free a certificate store.
217 * @param certs certificate store to free.
219 * @ingroup hx509_keyset
223 hx509_certs_free(hx509_certs *certs)
226 if ((*certs)->ref <= 0)
227 _hx509_abort("refcount <= 0");
228 if (--(*certs)->ref > 0)
231 (*(*certs)->ops->free)(*certs, (*certs)->ops_data);
238 * Start the integration
240 * @param context a hx509 context.
241 * @param certs certificate store to iterate over
242 * @param cursor cursor that will keep track of progress, free with
243 * hx509_certs_end_seq().
245 * @return Returns an hx509 error code. HX509_UNSUPPORTED_OPERATION is
246 * returned if the certificate store doesn't support the iteration
249 * @ingroup hx509_keyset
253 hx509_certs_start_seq(hx509_context context,
255 hx509_cursor *cursor)
259 if (certs->ops->iter_start == NULL) {
260 hx509_set_error_string(context, 0, HX509_UNSUPPORTED_OPERATION,
261 "Keyset type %s doesn't support iteration",
263 return HX509_UNSUPPORTED_OPERATION;
266 ret = (*certs->ops->iter_start)(context, certs, certs->ops_data, cursor);
274 * Get next ceritificate from the certificate keystore pointed out by
277 * @param context a hx509 context.
278 * @param certs certificate store to iterate over.
279 * @param cursor cursor that keeps track of progress.
280 * @param cert return certificate next in store, NULL if the store
281 * contains no more certificates. Free with hx509_cert_free().
283 * @return Returns an hx509 error code.
285 * @ingroup hx509_keyset
289 hx509_certs_next_cert(hx509_context context,
295 return (*certs->ops->iter)(context, certs, certs->ops_data, cursor, cert);
299 * End the iteration over certificates.
301 * @param context a hx509 context.
302 * @param certs certificate store to iterate over.
303 * @param cursor cursor that will keep track of progress, freed.
305 * @return Returns an hx509 error code.
307 * @ingroup hx509_keyset
311 hx509_certs_end_seq(hx509_context context,
315 (*certs->ops->iter_end)(context, certs, certs->ops_data, cursor);
320 * Iterate over all certificates in a keystore and call an function
323 * @param context a hx509 context.
324 * @param certs certificate store to iterate over.
325 * @param func function to call for each certificate. The function
326 * should return non-zero to abort the iteration, that value is passed
327 * back to te caller of hx509_certs_iter().
328 * @param ctx context variable that will passed to the function.
330 * @return Returns an hx509 error code.
332 * @ingroup hx509_keyset
336 hx509_certs_iter(hx509_context context,
338 int (*func)(hx509_context, void *, hx509_cert),
345 ret = hx509_certs_start_seq(context, certs, &cursor);
350 ret = hx509_certs_next_cert(context, certs, cursor, &c);
357 ret = (*func)(context, ctx, c);
363 hx509_certs_end_seq(context, certs, cursor);
370 * Function to use to hx509_certs_iter() as a function argument, the
371 * ctx variable to hx509_certs_iter() should be a FILE file descriptor.
373 * @param context a hx509 context.
374 * @param ctx used by hx509_certs_iter().
375 * @param c a certificate
377 * @return Returns an hx509 error code.
379 * @ingroup hx509_keyset
383 hx509_ci_print_names(hx509_context context, void *ctx, hx509_cert c)
389 cert = _hx509_get_cert(c);
391 _hx509_name_from_Name(&cert->tbsCertificate.subject, &n);
392 hx509_name_to_string(n, &s);
394 _hx509_name_from_Name(&cert->tbsCertificate.issuer, &n);
395 hx509_name_to_string(n, &i);
397 fprintf(ctx, "subject: %s\nissuer: %s\n", s, i);
404 * Add a certificate to the certificiate store.
406 * The receiving keyset certs will either increase reference counter
407 * of the cert or make a deep copy, either way, the caller needs to
408 * free the cert itself.
410 * @param context a hx509 context.
411 * @param certs certificate store to add the certificate to.
412 * @param cert certificate to add.
414 * @return Returns an hx509 error code.
416 * @ingroup hx509_keyset
420 hx509_certs_add(hx509_context context, hx509_certs certs, hx509_cert cert)
422 if (certs->ops->add == NULL) {
423 hx509_set_error_string(context, 0, ENOENT,
424 "Keyset type %s doesn't support add operation",
429 return (*certs->ops->add)(context, certs, certs->ops_data, cert);
433 * Find a certificate matching the query.
435 * @param context a hx509 context.
436 * @param certs certificate store to search.
437 * @param q query allocated with @ref hx509_query functions.
438 * @param r return certificate (or NULL on error), should be freed
439 * with hx509_cert_free().
441 * @return Returns an hx509 error code.
443 * @ingroup hx509_keyset
447 hx509_certs_find(hx509_context context,
449 const hx509_query *q,
458 _hx509_query_statistic(context, 0, q);
460 if (certs->ops->query)
461 return (*certs->ops->query)(context, certs, certs->ops_data, q, r);
463 ret = hx509_certs_start_seq(context, certs, &cursor);
469 ret = hx509_certs_next_cert(context, certs, cursor, &c);
474 if (_hx509_query_match_cert(context, q, c)) {
481 hx509_certs_end_seq(context, certs, cursor);
485 hx509_clear_error_string(context);
486 return HX509_CERT_NOT_FOUND;
493 certs_merge_func(hx509_context context, void *ctx, hx509_cert c)
495 return hx509_certs_add(context, (hx509_certs)ctx, c);
499 * Merge a certificate store into another. The from store is keep
502 * @param context a hx509 context.
503 * @param to the store to merge into.
504 * @param from the store to copy the object from.
506 * @return Returns an hx509 error code.
508 * @ingroup hx509_keyset
512 hx509_certs_merge(hx509_context context, hx509_certs to, hx509_certs from)
516 return hx509_certs_iter(context, from, certs_merge_func, to);
520 * Same a hx509_certs_merge() but use a lock and name to describe the
523 * @param context a hx509 context.
524 * @param to the store to merge into.
525 * @param lock a lock that unlocks the certificates store, use NULL to
526 * select no password/certifictes/prompt lock (see @ref page_lock).
527 * @param name name of the source store
529 * @return Returns an hx509 error code.
531 * @ingroup hx509_keyset
535 hx509_certs_append(hx509_context context,
543 ret = hx509_certs_init(context, name, 0, lock, &s);
546 ret = hx509_certs_merge(context, to, s);
547 hx509_certs_free(&s);
552 * Get one random certificate from the certificate store.
554 * @param context a hx509 context.
555 * @param certs a certificate store to get the certificate from.
556 * @param c return certificate, should be freed with hx509_cert_free().
558 * @return Returns an hx509 error code.
560 * @ingroup hx509_keyset
564 hx509_get_one_cert(hx509_context context, hx509_certs certs, hx509_cert *c)
571 ret = hx509_certs_start_seq(context, certs, &cursor);
575 ret = hx509_certs_next_cert(context, certs, cursor, c);
579 hx509_certs_end_seq(context, certs, cursor);
584 certs_info_stdio(void *ctx, const char *str)
587 fprintf(f, "%s\n", str);
592 * Print some info about the certificate store.
594 * @param context a hx509 context.
595 * @param certs certificate store to print information about.
596 * @param func function that will get each line of the information, if
597 * NULL is used the data is printed on a FILE descriptor that should
598 * be passed in ctx, if ctx also is NULL, stdout is used.
599 * @param ctx parameter to func.
601 * @return Returns an hx509 error code.
603 * @ingroup hx509_keyset
607 hx509_certs_info(hx509_context context,
609 int (*func)(void *, const char *),
613 func = certs_info_stdio;
617 if (certs->ops->printinfo == NULL) {
618 (*func)(ctx, "No info function for certs");
621 return (*certs->ops->printinfo)(context, certs, certs->ops_data,
626 _hx509_pi_printf(int (*func)(void *, const char *), void *ctx,
627 const char *fmt, ...)
633 vasprintf(&str, fmt, ap);
642 _hx509_certs_keys_get(hx509_context context,
644 hx509_private_key **keys)
646 if (certs->ops->getkeys == NULL) {
650 return (*certs->ops->getkeys)(context, certs, certs->ops_data, keys);
654 _hx509_certs_keys_add(hx509_context context,
656 hx509_private_key key)
658 if (certs->ops->addkey == NULL) {
659 hx509_set_error_string(context, 0, EINVAL,
660 "keystore if type %s doesn't support "
665 return (*certs->ops->addkey)(context, certs, certs->ops_data, key);
670 _hx509_certs_keys_free(hx509_context context,
671 hx509_private_key *keys)
674 for (i = 0; keys[i]; i++)
675 _hx509_private_key_free(&keys[i]);