Remove building with NOCRYPTO option
[minix.git] / crypto / external / bsd / heimdal / dist / lib / hx509 / keyset.c
blob83835b90217ba944af449c49a708329b3cf602bd
1 /* $NetBSD: keyset.c,v 1.1.1.1 2011/04/13 18:15:11 elric Exp $ */
3 /*
4 * Copyright (c) 2004 - 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
8 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
38 #include "hx_locl.h"
40 /**
41 * @page page_keyset Certificate store operations
43 * Type of certificates store:
44 * - MEMORY
45 * In memory based format. Doesnt support storing.
46 * - FILE
47 * FILE supports raw DER certicates and PEM certicates. When PEM is
48 * used the file can contain may certificates and match private
49 * keys. Support storing the certificates. DER format only supports
50 * on certificate and no private key.
51 * - PEM-FILE
52 * Same as FILE, defaulting to PEM encoded certificates.
53 * - PEM-FILE
54 * Same as FILE, defaulting to DER encoded certificates.
55 * - PKCS11
56 * - PKCS12
57 * - DIR
58 * - KEYCHAIN
59 * Apple Mac OS X KeyChain backed keychain object.
61 * See the library functions here: @ref hx509_keyset
64 struct hx509_certs_data {
65 unsigned int ref;
66 struct hx509_keyset_ops *ops;
67 void *ops_data;
70 static struct hx509_keyset_ops *
71 _hx509_ks_type(hx509_context context, const char *type)
73 int i;
75 for (i = 0; i < context->ks_num_ops; i++)
76 if (strcasecmp(type, context->ks_ops[i]->name) == 0)
77 return context->ks_ops[i];
79 return NULL;
82 void
83 _hx509_ks_register(hx509_context context, struct hx509_keyset_ops *ops)
85 struct hx509_keyset_ops **val;
87 if (_hx509_ks_type(context, ops->name))
88 return;
90 val = realloc(context->ks_ops,
91 (context->ks_num_ops + 1) * sizeof(context->ks_ops[0]));
92 if (val == NULL)
93 return;
94 val[context->ks_num_ops] = ops;
95 context->ks_ops = val;
96 context->ks_num_ops++;
99 /**
100 * Open or creates a new hx509 certificate store.
102 * @param context A hx509 context
103 * @param name name of the store, format is TYPE:type-specific-string,
104 * if NULL is used the MEMORY store is used.
105 * @param flags list of flags:
106 * - HX509_CERTS_CREATE create a new keystore of the specific TYPE.
107 * - HX509_CERTS_UNPROTECT_ALL fails if any private key failed to be extracted.
108 * @param lock a lock that unlocks the certificates store, use NULL to
109 * select no password/certifictes/prompt lock (see @ref page_lock).
110 * @param certs return pointer, free with hx509_certs_free().
112 * @ingroup hx509_keyset
116 hx509_certs_init(hx509_context context,
117 const char *name, int flags,
118 hx509_lock lock, hx509_certs *certs)
120 struct hx509_keyset_ops *ops;
121 const char *residue;
122 hx509_certs c;
123 char *type;
124 int ret;
126 *certs = NULL;
128 residue = strchr(name, ':');
129 if (residue) {
130 type = malloc(residue - name + 1);
131 if (type)
132 strlcpy(type, name, residue - name + 1);
133 residue++;
134 if (residue[0] == '\0')
135 residue = NULL;
136 } else {
137 type = strdup("MEMORY");
138 residue = name;
140 if (type == NULL) {
141 hx509_clear_error_string(context);
142 return ENOMEM;
145 ops = _hx509_ks_type(context, type);
146 if (ops == NULL) {
147 hx509_set_error_string(context, 0, ENOENT,
148 "Keyset type %s is not supported", type);
149 free(type);
150 return ENOENT;
152 free(type);
153 c = calloc(1, sizeof(*c));
154 if (c == NULL) {
155 hx509_clear_error_string(context);
156 return ENOMEM;
158 c->ops = ops;
159 c->ref = 1;
161 ret = (*ops->init)(context, c, &c->ops_data, flags, residue, lock);
162 if (ret) {
163 free(c);
164 return ret;
167 *certs = c;
168 return 0;
172 * Write the certificate store to stable storage.
174 * @param context A hx509 context.
175 * @param certs a certificate store to store.
176 * @param flags currently unused, use 0.
177 * @param lock a lock that unlocks the certificates store, use NULL to
178 * select no password/certifictes/prompt lock (see @ref page_lock).
180 * @return Returns an hx509 error code. HX509_UNSUPPORTED_OPERATION if
181 * the certificate store doesn't support the store operation.
183 * @ingroup hx509_keyset
187 hx509_certs_store(hx509_context context,
188 hx509_certs certs,
189 int flags,
190 hx509_lock lock)
192 if (certs->ops->store == NULL) {
193 hx509_set_error_string(context, 0, HX509_UNSUPPORTED_OPERATION,
194 "keystore if type %s doesn't support "
195 "store operation",
196 certs->ops->name);
197 return HX509_UNSUPPORTED_OPERATION;
200 return (*certs->ops->store)(context, certs, certs->ops_data, flags, lock);
204 hx509_certs
205 hx509_certs_ref(hx509_certs certs)
207 if (certs == NULL)
208 return NULL;
209 if (certs->ref == 0)
210 _hx509_abort("certs refcount == 0 on ref");
211 if (certs->ref == UINT_MAX)
212 _hx509_abort("certs refcount == UINT_MAX on ref");
213 certs->ref++;
214 return certs;
218 * Free a certificate store.
220 * @param certs certificate store to free.
222 * @ingroup hx509_keyset
225 void
226 hx509_certs_free(hx509_certs *certs)
228 if (*certs) {
229 if ((*certs)->ref == 0)
230 _hx509_abort("cert refcount == 0 on free");
231 if (--(*certs)->ref > 0)
232 return;
234 (*(*certs)->ops->free)(*certs, (*certs)->ops_data);
235 free(*certs);
236 *certs = NULL;
241 * Start the integration
243 * @param context a hx509 context.
244 * @param certs certificate store to iterate over
245 * @param cursor cursor that will keep track of progress, free with
246 * hx509_certs_end_seq().
248 * @return Returns an hx509 error code. HX509_UNSUPPORTED_OPERATION is
249 * returned if the certificate store doesn't support the iteration
250 * operation.
252 * @ingroup hx509_keyset
256 hx509_certs_start_seq(hx509_context context,
257 hx509_certs certs,
258 hx509_cursor *cursor)
260 int ret;
262 if (certs->ops->iter_start == NULL) {
263 hx509_set_error_string(context, 0, HX509_UNSUPPORTED_OPERATION,
264 "Keyset type %s doesn't support iteration",
265 certs->ops->name);
266 return HX509_UNSUPPORTED_OPERATION;
269 ret = (*certs->ops->iter_start)(context, certs, certs->ops_data, cursor);
270 if (ret)
271 return ret;
273 return 0;
277 * Get next ceritificate from the certificate keystore pointed out by
278 * cursor.
280 * @param context a hx509 context.
281 * @param certs certificate store to iterate over.
282 * @param cursor cursor that keeps track of progress.
283 * @param cert return certificate next in store, NULL if the store
284 * contains no more certificates. Free with hx509_cert_free().
286 * @return Returns an hx509 error code.
288 * @ingroup hx509_keyset
292 hx509_certs_next_cert(hx509_context context,
293 hx509_certs certs,
294 hx509_cursor cursor,
295 hx509_cert *cert)
297 *cert = NULL;
298 return (*certs->ops->iter)(context, certs, certs->ops_data, cursor, cert);
302 * End the iteration over certificates.
304 * @param context a hx509 context.
305 * @param certs certificate store to iterate over.
306 * @param cursor cursor that will keep track of progress, freed.
308 * @return Returns an hx509 error code.
310 * @ingroup hx509_keyset
314 hx509_certs_end_seq(hx509_context context,
315 hx509_certs certs,
316 hx509_cursor cursor)
318 (*certs->ops->iter_end)(context, certs, certs->ops_data, cursor);
319 return 0;
323 * Iterate over all certificates in a keystore and call an function
324 * for each fo them.
326 * @param context a hx509 context.
327 * @param certs certificate store to iterate over.
328 * @param func function to call for each certificate. The function
329 * should return non-zero to abort the iteration, that value is passed
330 * back to the caller of hx509_certs_iter_f().
331 * @param ctx context variable that will passed to the function.
333 * @return Returns an hx509 error code.
335 * @ingroup hx509_keyset
339 hx509_certs_iter_f(hx509_context context,
340 hx509_certs certs,
341 int (*func)(hx509_context, void *, hx509_cert),
342 void *ctx)
344 hx509_cursor cursor;
345 hx509_cert c;
346 int ret;
348 ret = hx509_certs_start_seq(context, certs, &cursor);
349 if (ret)
350 return ret;
352 while (1) {
353 ret = hx509_certs_next_cert(context, certs, cursor, &c);
354 if (ret)
355 break;
356 if (c == NULL) {
357 ret = 0;
358 break;
360 ret = (*func)(context, ctx, c);
361 hx509_cert_free(c);
362 if (ret)
363 break;
366 hx509_certs_end_seq(context, certs, cursor);
368 return ret;
372 * Iterate over all certificates in a keystore and call an function
373 * for each fo them.
375 * @param context a hx509 context.
376 * @param certs certificate store to iterate over.
377 * @param func function to call for each certificate. The function
378 * should return non-zero to abort the iteration, that value is passed
379 * back to the caller of hx509_certs_iter().
381 * @return Returns an hx509 error code.
383 * @ingroup hx509_keyset
386 #ifdef __BLOCKS__
388 static int
389 certs_iter(hx509_context context, void *ctx, hx509_cert cert)
391 int (^func)(hx509_cert) = ctx;
392 return func(cert);
396 * Iterate over all certificates in a keystore and call an block
397 * for each fo them.
399 * @param context a hx509 context.
400 * @param certs certificate store to iterate over.
401 * @param func block to call for each certificate. The function
402 * should return non-zero to abort the iteration, that value is passed
403 * back to the caller of hx509_certs_iter().
405 * @return Returns an hx509 error code.
407 * @ingroup hx509_keyset
411 hx509_certs_iter(hx509_context context,
412 hx509_certs certs,
413 int (^func)(hx509_cert))
415 return hx509_certs_iter_f(context, certs, certs_iter, func);
417 #endif
421 * Function to use to hx509_certs_iter_f() as a function argument, the
422 * ctx variable to hx509_certs_iter_f() should be a FILE file descriptor.
424 * @param context a hx509 context.
425 * @param ctx used by hx509_certs_iter_f().
426 * @param c a certificate
428 * @return Returns an hx509 error code.
430 * @ingroup hx509_keyset
434 hx509_ci_print_names(hx509_context context, void *ctx, hx509_cert c)
436 Certificate *cert;
437 hx509_name n;
438 char *s, *i;
440 cert = _hx509_get_cert(c);
442 _hx509_name_from_Name(&cert->tbsCertificate.subject, &n);
443 hx509_name_to_string(n, &s);
444 hx509_name_free(&n);
445 _hx509_name_from_Name(&cert->tbsCertificate.issuer, &n);
446 hx509_name_to_string(n, &i);
447 hx509_name_free(&n);
448 fprintf(ctx, "subject: %s\nissuer: %s\n", s, i);
449 free(s);
450 free(i);
451 return 0;
455 * Add a certificate to the certificiate store.
457 * The receiving keyset certs will either increase reference counter
458 * of the cert or make a deep copy, either way, the caller needs to
459 * free the cert itself.
461 * @param context a hx509 context.
462 * @param certs certificate store to add the certificate to.
463 * @param cert certificate to add.
465 * @return Returns an hx509 error code.
467 * @ingroup hx509_keyset
471 hx509_certs_add(hx509_context context, hx509_certs certs, hx509_cert cert)
473 if (certs->ops->add == NULL) {
474 hx509_set_error_string(context, 0, ENOENT,
475 "Keyset type %s doesn't support add operation",
476 certs->ops->name);
477 return ENOENT;
480 return (*certs->ops->add)(context, certs, certs->ops_data, cert);
484 * Find a certificate matching the query.
486 * @param context a hx509 context.
487 * @param certs certificate store to search.
488 * @param q query allocated with @ref hx509_query functions.
489 * @param r return certificate (or NULL on error), should be freed
490 * with hx509_cert_free().
492 * @return Returns an hx509 error code.
494 * @ingroup hx509_keyset
498 hx509_certs_find(hx509_context context,
499 hx509_certs certs,
500 const hx509_query *q,
501 hx509_cert *r)
503 hx509_cursor cursor;
504 hx509_cert c;
505 int ret;
507 *r = NULL;
509 _hx509_query_statistic(context, 0, q);
511 if (certs->ops->query)
512 return (*certs->ops->query)(context, certs, certs->ops_data, q, r);
514 ret = hx509_certs_start_seq(context, certs, &cursor);
515 if (ret)
516 return ret;
518 c = NULL;
519 while (1) {
520 ret = hx509_certs_next_cert(context, certs, cursor, &c);
521 if (ret)
522 break;
523 if (c == NULL)
524 break;
525 if (_hx509_query_match_cert(context, q, c)) {
526 *r = c;
527 break;
529 hx509_cert_free(c);
532 hx509_certs_end_seq(context, certs, cursor);
533 if (ret)
534 return ret;
536 * Return HX509_CERT_NOT_FOUND if no certificate in certs matched
537 * the query.
539 if (c == NULL) {
540 hx509_clear_error_string(context);
541 return HX509_CERT_NOT_FOUND;
544 return 0;
548 * Filter certificate matching the query.
550 * @param context a hx509 context.
551 * @param certs certificate store to search.
552 * @param q query allocated with @ref hx509_query functions.
553 * @param result the filtered certificate store, caller must free with
554 * hx509_certs_free().
556 * @return Returns an hx509 error code.
558 * @ingroup hx509_keyset
562 hx509_certs_filter(hx509_context context,
563 hx509_certs certs,
564 const hx509_query *q,
565 hx509_certs *result)
567 hx509_cursor cursor;
568 hx509_cert c;
569 int ret, found = 0;
571 _hx509_query_statistic(context, 0, q);
573 ret = hx509_certs_init(context, "MEMORY:filter-certs", 0,
574 NULL, result);
575 if (ret)
576 return ret;
578 ret = hx509_certs_start_seq(context, certs, &cursor);
579 if (ret) {
580 hx509_certs_free(result);
581 return ret;
584 c = NULL;
585 while (1) {
586 ret = hx509_certs_next_cert(context, certs, cursor, &c);
587 if (ret)
588 break;
589 if (c == NULL)
590 break;
591 if (_hx509_query_match_cert(context, q, c)) {
592 hx509_certs_add(context, *result, c);
593 found = 1;
595 hx509_cert_free(c);
598 hx509_certs_end_seq(context, certs, cursor);
599 if (ret) {
600 hx509_certs_free(result);
601 return ret;
605 * Return HX509_CERT_NOT_FOUND if no certificate in certs matched
606 * the query.
608 if (!found) {
609 hx509_certs_free(result);
610 hx509_clear_error_string(context);
611 return HX509_CERT_NOT_FOUND;
614 return 0;
618 static int
619 certs_merge_func(hx509_context context, void *ctx, hx509_cert c)
621 return hx509_certs_add(context, (hx509_certs)ctx, c);
625 * Merge a certificate store into another. The from store is keep
626 * intact.
628 * @param context a hx509 context.
629 * @param to the store to merge into.
630 * @param from the store to copy the object from.
632 * @return Returns an hx509 error code.
634 * @ingroup hx509_keyset
638 hx509_certs_merge(hx509_context context, hx509_certs to, hx509_certs from)
640 if (from == NULL)
641 return 0;
642 return hx509_certs_iter_f(context, from, certs_merge_func, to);
646 * Same a hx509_certs_merge() but use a lock and name to describe the
647 * from source.
649 * @param context a hx509 context.
650 * @param to the store to merge into.
651 * @param lock a lock that unlocks the certificates store, use NULL to
652 * select no password/certifictes/prompt lock (see @ref page_lock).
653 * @param name name of the source store
655 * @return Returns an hx509 error code.
657 * @ingroup hx509_keyset
661 hx509_certs_append(hx509_context context,
662 hx509_certs to,
663 hx509_lock lock,
664 const char *name)
666 hx509_certs s;
667 int ret;
669 ret = hx509_certs_init(context, name, 0, lock, &s);
670 if (ret)
671 return ret;
672 ret = hx509_certs_merge(context, to, s);
673 hx509_certs_free(&s);
674 return ret;
678 * Get one random certificate from the certificate store.
680 * @param context a hx509 context.
681 * @param certs a certificate store to get the certificate from.
682 * @param c return certificate, should be freed with hx509_cert_free().
684 * @return Returns an hx509 error code.
686 * @ingroup hx509_keyset
690 hx509_get_one_cert(hx509_context context, hx509_certs certs, hx509_cert *c)
692 hx509_cursor cursor;
693 int ret;
695 *c = NULL;
697 ret = hx509_certs_start_seq(context, certs, &cursor);
698 if (ret)
699 return ret;
701 ret = hx509_certs_next_cert(context, certs, cursor, c);
702 if (ret)
703 return ret;
705 hx509_certs_end_seq(context, certs, cursor);
706 return 0;
709 static int
710 certs_info_stdio(void *ctx, const char *str)
712 FILE *f = ctx;
713 fprintf(f, "%s\n", str);
714 return 0;
718 * Print some info about the certificate store.
720 * @param context a hx509 context.
721 * @param certs certificate store to print information about.
722 * @param func function that will get each line of the information, if
723 * NULL is used the data is printed on a FILE descriptor that should
724 * be passed in ctx, if ctx also is NULL, stdout is used.
725 * @param ctx parameter to func.
727 * @return Returns an hx509 error code.
729 * @ingroup hx509_keyset
733 hx509_certs_info(hx509_context context,
734 hx509_certs certs,
735 int (*func)(void *, const char *),
736 void *ctx)
738 if (func == NULL) {
739 func = certs_info_stdio;
740 if (ctx == NULL)
741 ctx = stdout;
743 if (certs->ops->printinfo == NULL) {
744 (*func)(ctx, "No info function for certs");
745 return 0;
747 return (*certs->ops->printinfo)(context, certs, certs->ops_data,
748 func, ctx);
751 void
752 _hx509_pi_printf(int (*func)(void *, const char *), void *ctx,
753 const char *fmt, ...)
755 va_list ap;
756 char *str;
758 va_start(ap, fmt);
759 vasprintf(&str, fmt, ap);
760 va_end(ap);
761 if (str == NULL)
762 return;
763 (*func)(ctx, str);
764 free(str);
768 _hx509_certs_keys_get(hx509_context context,
769 hx509_certs certs,
770 hx509_private_key **keys)
772 if (certs->ops->getkeys == NULL) {
773 *keys = NULL;
774 return 0;
776 return (*certs->ops->getkeys)(context, certs, certs->ops_data, keys);
780 _hx509_certs_keys_add(hx509_context context,
781 hx509_certs certs,
782 hx509_private_key key)
784 if (certs->ops->addkey == NULL) {
785 hx509_set_error_string(context, 0, EINVAL,
786 "keystore if type %s doesn't support "
787 "key add operation",
788 certs->ops->name);
789 return EINVAL;
791 return (*certs->ops->addkey)(context, certs, certs->ops_data, key);
795 void
796 _hx509_certs_keys_free(hx509_context context,
797 hx509_private_key *keys)
799 int i;
800 for (i = 0; keys[i]; i++)
801 hx509_private_key_free(&keys[i]);
802 free(keys);