s3-lsa: Fix static list of luids in our privileges implementation.
[Samba/ekacnet.git] / source4 / heimdal / lib / hx509 / keyset.c
blob465ca1b4d3e5a0e22c3ef81dcc0b9a761de81111
1 /*
2 * Copyright (c) 2004 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include "hx_locl.h"
38 /**
39 * @page page_keyset Certificate store operations
41 * Type of certificates store:
42 * - MEMORY
43 * In memory based format. Doesnt support storing.
44 * - FILE
45 * FILE supports raw DER certicates and PEM certicates. When PEM is
46 * used the file can contain may certificates and match private
47 * keys. Support storing the certificates. DER format only supports
48 * on certificate and no private key.
49 * - PEM-FILE
50 * Same as FILE, defaulting to PEM encoded certificates.
51 * - PEM-FILE
52 * Same as FILE, defaulting to DER encoded certificates.
53 * - PKCS11
54 * - PKCS12
55 * - DIR
56 * - KEYCHAIN
57 * Apple Mac OS X KeyChain backed keychain object.
59 * See the library functions here: @ref hx509_keyset
62 struct hx509_certs_data {
63 unsigned int ref;
64 struct hx509_keyset_ops *ops;
65 void *ops_data;
68 static struct hx509_keyset_ops *
69 _hx509_ks_type(hx509_context context, const char *type)
71 int i;
73 for (i = 0; i < context->ks_num_ops; i++)
74 if (strcasecmp(type, context->ks_ops[i]->name) == 0)
75 return context->ks_ops[i];
77 return NULL;
80 void
81 _hx509_ks_register(hx509_context context, struct hx509_keyset_ops *ops)
83 struct hx509_keyset_ops **val;
85 if (_hx509_ks_type(context, ops->name))
86 return;
88 val = realloc(context->ks_ops,
89 (context->ks_num_ops + 1) * sizeof(context->ks_ops[0]));
90 if (val == NULL)
91 return;
92 val[context->ks_num_ops] = ops;
93 context->ks_ops = val;
94 context->ks_num_ops++;
97 /**
98 * Open or creates a new hx509 certificate store.
100 * @param context A hx509 context
101 * @param name name of the store, format is TYPE:type-specific-string,
102 * if NULL is used the MEMORY store is used.
103 * @param flags list of flags:
104 * - HX509_CERTS_CREATE create a new keystore of the specific TYPE.
105 * - HX509_CERTS_UNPROTECT_ALL fails if any private key failed to be extracted.
106 * @param lock a lock that unlocks the certificates store, use NULL to
107 * select no password/certifictes/prompt lock (see @ref page_lock).
108 * @param certs return pointer, free with hx509_certs_free().
110 * @ingroup hx509_keyset
114 hx509_certs_init(hx509_context context,
115 const char *name, int flags,
116 hx509_lock lock, hx509_certs *certs)
118 struct hx509_keyset_ops *ops;
119 const char *residue;
120 hx509_certs c;
121 char *type;
122 int ret;
124 *certs = NULL;
126 residue = strchr(name, ':');
127 if (residue) {
128 type = malloc(residue - name + 1);
129 if (type)
130 strlcpy(type, name, residue - name + 1);
131 residue++;
132 if (residue[0] == '\0')
133 residue = NULL;
134 } else {
135 type = strdup("MEMORY");
136 residue = name;
138 if (type == NULL) {
139 hx509_clear_error_string(context);
140 return ENOMEM;
143 ops = _hx509_ks_type(context, type);
144 if (ops == NULL) {
145 hx509_set_error_string(context, 0, ENOENT,
146 "Keyset type %s is not supported", type);
147 free(type);
148 return ENOENT;
150 free(type);
151 c = calloc(1, sizeof(*c));
152 if (c == NULL) {
153 hx509_clear_error_string(context);
154 return ENOMEM;
156 c->ops = ops;
157 c->ref = 1;
159 ret = (*ops->init)(context, c, &c->ops_data, flags, residue, lock);
160 if (ret) {
161 free(c);
162 return ret;
165 *certs = c;
166 return 0;
170 * Write the certificate store to stable storage.
172 * @param context A hx509 context.
173 * @param certs a certificate store to store.
174 * @param flags currently unused, use 0.
175 * @param lock a lock that unlocks the certificates store, use NULL to
176 * select no password/certifictes/prompt lock (see @ref page_lock).
178 * @return Returns an hx509 error code. HX509_UNSUPPORTED_OPERATION if
179 * the certificate store doesn't support the store operation.
181 * @ingroup hx509_keyset
185 hx509_certs_store(hx509_context context,
186 hx509_certs certs,
187 int flags,
188 hx509_lock lock)
190 if (certs->ops->store == NULL) {
191 hx509_set_error_string(context, 0, HX509_UNSUPPORTED_OPERATION,
192 "keystore if type %s doesn't support "
193 "store operation",
194 certs->ops->name);
195 return HX509_UNSUPPORTED_OPERATION;
198 return (*certs->ops->store)(context, certs, certs->ops_data, flags, lock);
202 hx509_certs
203 hx509_certs_ref(hx509_certs certs)
205 if (certs == NULL)
206 return NULL;
207 if (certs->ref == 0)
208 _hx509_abort("certs refcount == 0 on ref");
209 if (certs->ref == UINT_MAX)
210 _hx509_abort("certs refcount == UINT_MAX on ref");
211 certs->ref++;
212 return certs;
216 * Free a certificate store.
218 * @param certs certificate store to free.
220 * @ingroup hx509_keyset
223 void
224 hx509_certs_free(hx509_certs *certs)
226 if (*certs) {
227 if ((*certs)->ref == 0)
228 _hx509_abort("cert refcount == 0 on free");
229 if (--(*certs)->ref > 0)
230 return;
232 (*(*certs)->ops->free)(*certs, (*certs)->ops_data);
233 free(*certs);
234 *certs = NULL;
239 * Start the integration
241 * @param context a hx509 context.
242 * @param certs certificate store to iterate over
243 * @param cursor cursor that will keep track of progress, free with
244 * hx509_certs_end_seq().
246 * @return Returns an hx509 error code. HX509_UNSUPPORTED_OPERATION is
247 * returned if the certificate store doesn't support the iteration
248 * operation.
250 * @ingroup hx509_keyset
254 hx509_certs_start_seq(hx509_context context,
255 hx509_certs certs,
256 hx509_cursor *cursor)
258 int ret;
260 if (certs->ops->iter_start == NULL) {
261 hx509_set_error_string(context, 0, HX509_UNSUPPORTED_OPERATION,
262 "Keyset type %s doesn't support iteration",
263 certs->ops->name);
264 return HX509_UNSUPPORTED_OPERATION;
267 ret = (*certs->ops->iter_start)(context, certs, certs->ops_data, cursor);
268 if (ret)
269 return ret;
271 return 0;
275 * Get next ceritificate from the certificate keystore pointed out by
276 * cursor.
278 * @param context a hx509 context.
279 * @param certs certificate store to iterate over.
280 * @param cursor cursor that keeps track of progress.
281 * @param cert return certificate next in store, NULL if the store
282 * contains no more certificates. Free with hx509_cert_free().
284 * @return Returns an hx509 error code.
286 * @ingroup hx509_keyset
290 hx509_certs_next_cert(hx509_context context,
291 hx509_certs certs,
292 hx509_cursor cursor,
293 hx509_cert *cert)
295 *cert = NULL;
296 return (*certs->ops->iter)(context, certs, certs->ops_data, cursor, cert);
300 * End the iteration over certificates.
302 * @param context a hx509 context.
303 * @param certs certificate store to iterate over.
304 * @param cursor cursor that will keep track of progress, freed.
306 * @return Returns an hx509 error code.
308 * @ingroup hx509_keyset
312 hx509_certs_end_seq(hx509_context context,
313 hx509_certs certs,
314 hx509_cursor cursor)
316 (*certs->ops->iter_end)(context, certs, certs->ops_data, cursor);
317 return 0;
321 * Iterate over all certificates in a keystore and call an function
322 * for each fo them.
324 * @param context a hx509 context.
325 * @param certs certificate store to iterate over.
326 * @param func function to call for each certificate. The function
327 * should return non-zero to abort the iteration, that value is passed
328 * back to the caller of hx509_certs_iter_f().
329 * @param ctx context variable that will passed to the function.
331 * @return Returns an hx509 error code.
333 * @ingroup hx509_keyset
337 hx509_certs_iter_f(hx509_context context,
338 hx509_certs certs,
339 int (*func)(hx509_context, void *, hx509_cert),
340 void *ctx)
342 hx509_cursor cursor;
343 hx509_cert c;
344 int ret;
346 ret = hx509_certs_start_seq(context, certs, &cursor);
347 if (ret)
348 return ret;
350 while (1) {
351 ret = hx509_certs_next_cert(context, certs, cursor, &c);
352 if (ret)
353 break;
354 if (c == NULL) {
355 ret = 0;
356 break;
358 ret = (*func)(context, ctx, c);
359 hx509_cert_free(c);
360 if (ret)
361 break;
364 hx509_certs_end_seq(context, certs, cursor);
366 return ret;
370 * Iterate over all certificates in a keystore and call an function
371 * for each fo them.
373 * @param context a hx509 context.
374 * @param certs certificate store to iterate over.
375 * @param func function to call for each certificate. The function
376 * should return non-zero to abort the iteration, that value is passed
377 * back to the caller of hx509_certs_iter().
379 * @return Returns an hx509 error code.
381 * @ingroup hx509_keyset
384 #ifdef __BLOCKS__
386 static int
387 certs_iter(hx509_context context, void *ctx, hx509_cert cert)
389 int (^func)(hx509_cert) = ctx;
390 return func(cert);
394 hx509_certs_iter(hx509_context context,
395 hx509_certs certs,
396 int (^func)(hx509_cert))
398 return hx509_certs_iter_f(context, certs, certs_iter, func);
400 #endif
404 * Function to use to hx509_certs_iter_f() as a function argument, the
405 * ctx variable to hx509_certs_iter_f() should be a FILE file descriptor.
407 * @param context a hx509 context.
408 * @param ctx used by hx509_certs_iter_f().
409 * @param c a certificate
411 * @return Returns an hx509 error code.
413 * @ingroup hx509_keyset
417 hx509_ci_print_names(hx509_context context, void *ctx, hx509_cert c)
419 Certificate *cert;
420 hx509_name n;
421 char *s, *i;
423 cert = _hx509_get_cert(c);
425 _hx509_name_from_Name(&cert->tbsCertificate.subject, &n);
426 hx509_name_to_string(n, &s);
427 hx509_name_free(&n);
428 _hx509_name_from_Name(&cert->tbsCertificate.issuer, &n);
429 hx509_name_to_string(n, &i);
430 hx509_name_free(&n);
431 fprintf(ctx, "subject: %s\nissuer: %s\n", s, i);
432 free(s);
433 free(i);
434 return 0;
438 * Add a certificate to the certificiate store.
440 * The receiving keyset certs will either increase reference counter
441 * of the cert or make a deep copy, either way, the caller needs to
442 * free the cert itself.
444 * @param context a hx509 context.
445 * @param certs certificate store to add the certificate to.
446 * @param cert certificate to add.
448 * @return Returns an hx509 error code.
450 * @ingroup hx509_keyset
454 hx509_certs_add(hx509_context context, hx509_certs certs, hx509_cert cert)
456 if (certs->ops->add == NULL) {
457 hx509_set_error_string(context, 0, ENOENT,
458 "Keyset type %s doesn't support add operation",
459 certs->ops->name);
460 return ENOENT;
463 return (*certs->ops->add)(context, certs, certs->ops_data, cert);
467 * Find a certificate matching the query.
469 * @param context a hx509 context.
470 * @param certs certificate store to search.
471 * @param q query allocated with @ref hx509_query functions.
472 * @param r return certificate (or NULL on error), should be freed
473 * with hx509_cert_free().
475 * @return Returns an hx509 error code.
477 * @ingroup hx509_keyset
481 hx509_certs_find(hx509_context context,
482 hx509_certs certs,
483 const hx509_query *q,
484 hx509_cert *r)
486 hx509_cursor cursor;
487 hx509_cert c;
488 int ret;
490 *r = NULL;
492 _hx509_query_statistic(context, 0, q);
494 if (certs->ops->query)
495 return (*certs->ops->query)(context, certs, certs->ops_data, q, r);
497 ret = hx509_certs_start_seq(context, certs, &cursor);
498 if (ret)
499 return ret;
501 c = NULL;
502 while (1) {
503 ret = hx509_certs_next_cert(context, certs, cursor, &c);
504 if (ret)
505 break;
506 if (c == NULL)
507 break;
508 if (_hx509_query_match_cert(context, q, c)) {
509 *r = c;
510 break;
512 hx509_cert_free(c);
515 hx509_certs_end_seq(context, certs, cursor);
516 if (ret)
517 return ret;
519 * Return HX509_CERT_NOT_FOUND if no certificate in certs matched
520 * the query.
522 if (c == NULL) {
523 hx509_clear_error_string(context);
524 return HX509_CERT_NOT_FOUND;
527 return 0;
531 * Filter certificate matching the query.
533 * @param context a hx509 context.
534 * @param certs certificate store to search.
535 * @param q query allocated with @ref hx509_query functions.
536 * @param result the filtered certificate store, caller must free with
537 * hx509_certs_free().
539 * @return Returns an hx509 error code.
541 * @ingroup hx509_keyset
545 hx509_certs_filter(hx509_context context,
546 hx509_certs certs,
547 const hx509_query *q,
548 hx509_certs *result)
550 hx509_cursor cursor;
551 hx509_cert c;
552 int ret, found = 0;
554 _hx509_query_statistic(context, 0, q);
556 ret = hx509_certs_init(context, "MEMORY:filter-certs", 0,
557 NULL, result);
558 if (ret)
559 return ret;
561 ret = hx509_certs_start_seq(context, certs, &cursor);
562 if (ret) {
563 hx509_certs_free(result);
564 return ret;
567 c = NULL;
568 while (1) {
569 ret = hx509_certs_next_cert(context, certs, cursor, &c);
570 if (ret)
571 break;
572 if (c == NULL)
573 break;
574 if (_hx509_query_match_cert(context, q, c)) {
575 hx509_certs_add(context, *result, c);
576 found = 1;
578 hx509_cert_free(c);
581 hx509_certs_end_seq(context, certs, cursor);
582 if (ret) {
583 hx509_certs_free(result);
584 return ret;
588 * Return HX509_CERT_NOT_FOUND if no certificate in certs matched
589 * the query.
591 if (!found) {
592 hx509_certs_free(result);
593 hx509_clear_error_string(context);
594 return HX509_CERT_NOT_FOUND;
597 return 0;
601 static int
602 certs_merge_func(hx509_context context, void *ctx, hx509_cert c)
604 return hx509_certs_add(context, (hx509_certs)ctx, c);
608 * Merge a certificate store into another. The from store is keep
609 * intact.
611 * @param context a hx509 context.
612 * @param to the store to merge into.
613 * @param from the store to copy the object from.
615 * @return Returns an hx509 error code.
617 * @ingroup hx509_keyset
621 hx509_certs_merge(hx509_context context, hx509_certs to, hx509_certs from)
623 if (from == NULL)
624 return 0;
625 return hx509_certs_iter_f(context, from, certs_merge_func, to);
629 * Same a hx509_certs_merge() but use a lock and name to describe the
630 * from source.
632 * @param context a hx509 context.
633 * @param to the store to merge into.
634 * @param lock a lock that unlocks the certificates store, use NULL to
635 * select no password/certifictes/prompt lock (see @ref page_lock).
636 * @param name name of the source store
638 * @return Returns an hx509 error code.
640 * @ingroup hx509_keyset
644 hx509_certs_append(hx509_context context,
645 hx509_certs to,
646 hx509_lock lock,
647 const char *name)
649 hx509_certs s;
650 int ret;
652 ret = hx509_certs_init(context, name, 0, lock, &s);
653 if (ret)
654 return ret;
655 ret = hx509_certs_merge(context, to, s);
656 hx509_certs_free(&s);
657 return ret;
661 * Get one random certificate from the certificate store.
663 * @param context a hx509 context.
664 * @param certs a certificate store to get the certificate from.
665 * @param c return certificate, should be freed with hx509_cert_free().
667 * @return Returns an hx509 error code.
669 * @ingroup hx509_keyset
673 hx509_get_one_cert(hx509_context context, hx509_certs certs, hx509_cert *c)
675 hx509_cursor cursor;
676 int ret;
678 *c = NULL;
680 ret = hx509_certs_start_seq(context, certs, &cursor);
681 if (ret)
682 return ret;
684 ret = hx509_certs_next_cert(context, certs, cursor, c);
685 if (ret)
686 return ret;
688 hx509_certs_end_seq(context, certs, cursor);
689 return 0;
692 static int
693 certs_info_stdio(void *ctx, const char *str)
695 FILE *f = ctx;
696 fprintf(f, "%s\n", str);
697 return 0;
701 * Print some info about the certificate store.
703 * @param context a hx509 context.
704 * @param certs certificate store to print information about.
705 * @param func function that will get each line of the information, if
706 * NULL is used the data is printed on a FILE descriptor that should
707 * be passed in ctx, if ctx also is NULL, stdout is used.
708 * @param ctx parameter to func.
710 * @return Returns an hx509 error code.
712 * @ingroup hx509_keyset
716 hx509_certs_info(hx509_context context,
717 hx509_certs certs,
718 int (*func)(void *, const char *),
719 void *ctx)
721 if (func == NULL) {
722 func = certs_info_stdio;
723 if (ctx == NULL)
724 ctx = stdout;
726 if (certs->ops->printinfo == NULL) {
727 (*func)(ctx, "No info function for certs");
728 return 0;
730 return (*certs->ops->printinfo)(context, certs, certs->ops_data,
731 func, ctx);
734 void
735 _hx509_pi_printf(int (*func)(void *, const char *), void *ctx,
736 const char *fmt, ...)
738 va_list ap;
739 char *str;
741 va_start(ap, fmt);
742 vasprintf(&str, fmt, ap);
743 va_end(ap);
744 if (str == NULL)
745 return;
746 (*func)(ctx, str);
747 free(str);
751 _hx509_certs_keys_get(hx509_context context,
752 hx509_certs certs,
753 hx509_private_key **keys)
755 if (certs->ops->getkeys == NULL) {
756 *keys = NULL;
757 return 0;
759 return (*certs->ops->getkeys)(context, certs, certs->ops_data, keys);
763 _hx509_certs_keys_add(hx509_context context,
764 hx509_certs certs,
765 hx509_private_key key)
767 if (certs->ops->addkey == NULL) {
768 hx509_set_error_string(context, 0, EINVAL,
769 "keystore if type %s doesn't support "
770 "key add operation",
771 certs->ops->name);
772 return EINVAL;
774 return (*certs->ops->addkey)(context, certs, certs->ops_data, key);
778 void
779 _hx509_certs_keys_free(hx509_context context,
780 hx509_private_key *keys)
782 int i;
783 for (i = 0; keys[i]; i++)
784 _hx509_private_key_free(&keys[i]);
785 free(keys);