Debian Codegraph @

lib/hx509/keyset.c

raw
1 /*
2 * Copyright (c) 2004 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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.
18 *
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.
22 *
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.
34 */
35
36 #include "hx_locl.h"
37
38 /**
39 * @page page_keyset Certificate store operations
40 *
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.
58 *
59 * See the library functions here: @ref hx509_keyset
60 */
61
62 struct hx509_certs_data {
63 unsigned int ref;
64 struct hx509_keyset_ops *ops;
65 void *ops_data;
66 };
67
68 static struct hx509_keyset_ops *
69 _hx509_ks_type(hx509_context context, const char *type)
70 {
71 int i;
72
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];
76
77 return NULL;
78 }
79
80 void
81 _hx509_ks_register(hx509_context context, struct hx509_keyset_ops *ops)
82 {
83 struct hx509_keyset_ops **val;
84
85 if (_hx509_ks_type(context, ops->name))
86 return;
87
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++;
95 }
96
97 /**
98 * Open or creates a new hx509 certificate store.
99 *
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().
109 *
110 * @return Returns an hx509 error code.
111 *
112 * @ingroup hx509_keyset
113 */
114
115 int
116 hx509_certs_init(hx509_context context,
117 const char *name, int flags,
118 hx509_lock lock, hx509_certs *certs)
119 {
120 struct hx509_keyset_ops *ops;
121 const char *residue;
122 hx509_certs c;
123 char *type;
124 int ret;
125
126 *certs = NULL;
127
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;
139 }
140 if (type == NULL) {
141 hx509_clear_error_string(context);
142 return ENOMEM;
143 }
144
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;
151 }
152 free(type);
153 c = calloc(1, sizeof(*c));
154 if (c == NULL) {
155 hx509_clear_error_string(context);
156 return ENOMEM;
157 }
158 c->ops = ops;
159 c->ref = 1;
160
161 ret = (*ops->init)(context, c, &c->ops_data, flags, residue, lock);
162 if (ret) {
163 free(c);
164 return ret;
165 }
166
167 *certs = c;
168 return 0;
169 }
170
171 /**
172 * Write the certificate store to stable storage.
173 *
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).
179 *
180 * @return Returns an hx509 error code. HX509_UNSUPPORTED_OPERATION if
181 * the certificate store doesn't support the store operation.
182 *
183 * @ingroup hx509_keyset
184 */
185
186 int
187 hx509_certs_store(hx509_context context,
188 hx509_certs certs,
189 int flags,
190 hx509_lock lock)
191 {
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;
198 }
199
200 return (*certs->ops->store)(context, certs, certs->ops_data, flags, lock);
201 }
202
203
204 hx509_certs
205 hx509_certs_ref(hx509_certs certs)
206 {
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;
215 }
216
217 /**
218 * Free a certificate store.
219 *
220 * @param certs certificate store to free.
221 *
222 * @ingroup hx509_keyset
223 */
224
225 void
226 hx509_certs_free(hx509_certs *certs)
227 {
228 if (*certs) {
229 if ((*certs)->ref == 0)
230 _hx509_abort("cert refcount == 0 on free");
231 if (--(*certs)->ref > 0)
232 return;
233
234 (*(*certs)->ops->free)(*certs, (*certs)->ops_data);
235 free(*certs);
236 *certs = NULL;
237 }
238 }
239
240 /**
241 * Start the integration
242 *
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().
247 *
248 * @return Returns an hx509 error code. HX509_UNSUPPORTED_OPERATION is
249 * returned if the certificate store doesn't support the iteration
250 * operation.
251 *
252 * @ingroup hx509_keyset
253 */
254
255 int
256 hx509_certs_start_seq(hx509_context context,
257 hx509_certs certs,
258 hx509_cursor *cursor)
259 {
260 int ret;
261
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;
267 }
268
269 ret = (*certs->ops->iter_start)(context, certs, certs->ops_data, cursor);
270 if (ret)
271 return ret;
272
273 return 0;
274 }
275
276 /**
277 * Get next ceritificate from the certificate keystore pointed out by
278 * cursor.
279 *
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().
285 *
286 * @return Returns an hx509 error code.
287 *
288 * @ingroup hx509_keyset
289 */
290
291 int
292 hx509_certs_next_cert(hx509_context context,
293 hx509_certs certs,
294 hx509_cursor cursor,
295 hx509_cert *cert)
296 {
297 *cert = NULL;
298 return (*certs->ops->iter)(context, certs, certs->ops_data, cursor, cert);
299 }
300
301 /**
302 * End the iteration over certificates.
303 *
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.
307 *
308 * @return Returns an hx509 error code.
309 *
310 * @ingroup hx509_keyset
311 */
312
313 int
314 hx509_certs_end_seq(hx509_context context,
315 hx509_certs certs,
316 hx509_cursor cursor)
317 {
318 (*certs->ops->iter_end)(context, certs, certs->ops_data, cursor);
319 return 0;
320 }
321
322 /**
323 * Iterate over all certificates in a keystore and call a function
324 * for each of them.
325 *
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.
332 *
333 * @return Returns an hx509 error code.
334 *
335 * @ingroup hx509_keyset
336 */
337
338 int
339 hx509_certs_iter_f(hx509_context context,
340 hx509_certs certs,
341 int (*func)(hx509_context, void *, hx509_cert),
342 void *ctx)
343 {
344 hx509_cursor cursor;
345 hx509_cert c;
346 int ret;
347
348 ret = hx509_certs_start_seq(context, certs, &cursor);
349 if (ret)
350 return ret;
351
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;
359 }
360 ret = (*func)(context, ctx, c);
361 hx509_cert_free(c);
362 if (ret)
363 break;
364 }
365
366 hx509_certs_end_seq(context, certs, cursor);
367
368 return ret;
369 }
370
371 #ifdef __BLOCKS__
372
373 static int
374 certs_iter(hx509_context context, void *ctx, hx509_cert cert)
375 {
376 int (^func)(hx509_cert) = ctx;
377 return func(cert);
378 }
379
380 /**
381 * Iterate over all certificates in a keystore and call a block
382 * for each of them.
383 *
384 * @param context a hx509 context.
385 * @param certs certificate store to iterate over.
386 * @param func block to call for each certificate. The function
387 * should return non-zero to abort the iteration, that value is passed
388 * back to the caller of hx509_certs_iter().
389 *
390 * @return Returns an hx509 error code.
391 *
392 * @ingroup hx509_keyset
393 */
394
395 int
396 hx509_certs_iter(hx509_context context,
397 hx509_certs certs,
398 int (^func)(hx509_cert))
399 {
400 return hx509_certs_iter_f(context, certs, certs_iter, func);
401 }
402 #endif
403
404
405 /**
406 * Function to use to hx509_certs_iter_f() as a function argument, the
407 * ctx variable to hx509_certs_iter_f() should be a FILE file descriptor.
408 *
409 * @param context a hx509 context.
410 * @param ctx used by hx509_certs_iter_f().
411 * @param c a certificate
412 *
413 * @return Returns an hx509 error code.
414 *
415 * @ingroup hx509_keyset
416 */
417
418 int
419 hx509_ci_print_names(hx509_context context, void *ctx, hx509_cert c)
420 {
421 Certificate *cert;
422 hx509_name n;
423 char *s, *i;
424
425 cert = _hx509_get_cert(c);
426
427 _hx509_name_from_Name(&cert->tbsCertificate.subject, &n);
428 hx509_name_to_string(n, &s);
429 hx509_name_free(&n);
430 _hx509_name_from_Name(&cert->tbsCertificate.issuer, &n);
431 hx509_name_to_string(n, &i);
432 hx509_name_free(&n);
433 fprintf(ctx, "subject: %s\nissuer: %s\n", s, i);
434 free(s);
435 free(i);
436 return 0;
437 }
438
439 /**
440 * Add a certificate to the certificiate store.
441 *
442 * The receiving keyset certs will either increase reference counter
443 * of the cert or make a deep copy, either way, the caller needs to
444 * free the cert itself.
445 *
446 * @param context a hx509 context.
447 * @param certs certificate store to add the certificate to.
448 * @param cert certificate to add.
449 *
450 * @return Returns an hx509 error code.
451 *
452 * @ingroup hx509_keyset
453 */
454
455 int
456 hx509_certs_add(hx509_context context, hx509_certs certs, hx509_cert cert)
457 {
458 if (certs->ops->add == NULL) {
459 hx509_set_error_string(context, 0, ENOENT,
460 "Keyset type %s doesn't support add operation",
461 certs->ops->name);
462 return ENOENT;
463 }
464
465 return (*certs->ops->add)(context, certs, certs->ops_data, cert);
466 }
467
468 /**
469 * Find a certificate matching the query.
470 *
471 * @param context a hx509 context.
472 * @param certs certificate store to search.
473 * @param q query allocated with @ref hx509_query functions.
474 * @param r return certificate (or NULL on error), should be freed
475 * with hx509_cert_free().
476 *
477 * @return Returns an hx509 error code.
478 *
479 * @ingroup hx509_keyset
480 */
481
482 int
483 hx509_certs_find(hx509_context context,
484 hx509_certs certs,
485 const hx509_query *q,
486 hx509_cert *r)
487 {
488 hx509_cursor cursor;
489 hx509_cert c;
490 int ret;
491
492 *r = NULL;
493
494 _hx509_query_statistic(context, 0, q);
495
496 if (certs->ops->query)
497 return (*certs->ops->query)(context, certs, certs->ops_data, q, r);
498
499 ret = hx509_certs_start_seq(context, certs, &cursor);
500 if (ret)
501 return ret;
502
503 c = NULL;
504 while (1) {
505 ret = hx509_certs_next_cert(context, certs, cursor, &c);
506 if (ret)
507 break;
508 if (c == NULL)
509 break;
510 if (_hx509_query_match_cert(context, q, c)) {
511 *r = c;
512 break;
513 }
514 hx509_cert_free(c);
515 }
516
517 hx509_certs_end_seq(context, certs, cursor);
518 if (ret)
519 return ret;
520 /**
521 * Return HX509_CERT_NOT_FOUND if no certificate in certs matched
522 * the query.
523 */
524 if (c == NULL) {
525 hx509_clear_error_string(context);
526 return HX509_CERT_NOT_FOUND;
527 }
528
529 return 0;
530 }
531
532 /**
533 * Filter certificate matching the query.
534 *
535 * @param context a hx509 context.
536 * @param certs certificate store to search.
537 * @param q query allocated with @ref hx509_query functions.
538 * @param result the filtered certificate store, caller must free with
539 * hx509_certs_free().
540 *
541 * @return Returns an hx509 error code.
542 *
543 * @ingroup hx509_keyset
544 */
545
546 int
547 hx509_certs_filter(hx509_context context,
548 hx509_certs certs,
549 const hx509_query *q,
550 hx509_certs *result)
551 {
552 hx509_cursor cursor;
553 hx509_cert c;
554 int ret, found = 0;
555
556 _hx509_query_statistic(context, 0, q);
557
558 ret = hx509_certs_init(context, "MEMORY:filter-certs", 0,
559 NULL, result);
560 if (ret)
561 return ret;
562
563 ret = hx509_certs_start_seq(context, certs, &cursor);
564 if (ret) {
565 hx509_certs_free(result);
566 return ret;
567 }
568
569 c = NULL;
570 while (1) {
571 ret = hx509_certs_next_cert(context, certs, cursor, &c);
572 if (ret)
573 break;
574 if (c == NULL)
575 break;
576 if (_hx509_query_match_cert(context, q, c)) {
577 hx509_certs_add(context, *result, c);
578 found = 1;
579 }
580 hx509_cert_free(c);
581 }
582
583 hx509_certs_end_seq(context, certs, cursor);
584 if (ret) {
585 hx509_certs_free(result);
586 return ret;
587 }
588
589 /**
590 * Return HX509_CERT_NOT_FOUND if no certificate in certs matched
591 * the query.
592 */
593 if (!found) {
594 hx509_certs_free(result);
595 hx509_clear_error_string(context);
596 return HX509_CERT_NOT_FOUND;
597 }
598
599 return 0;
600 }
601
602
603 static int
604 certs_merge_func(hx509_context context, void *ctx, hx509_cert c)
605 {
606 return hx509_certs_add(context, (hx509_certs)ctx, c);
607 }
608
609 /**
610 * Merge a certificate store into another. The from store is keep
611 * intact.
612 *
613 * @param context a hx509 context.
614 * @param to the store to merge into.
615 * @param from the store to copy the object from.
616 *
617 * @return Returns an hx509 error code.
618 *
619 * @ingroup hx509_keyset
620 */
621
622 int
623 hx509_certs_merge(hx509_context context, hx509_certs to, hx509_certs from)
624 {
625 if (from == NULL)
626 return 0;
627 return hx509_certs_iter_f(context, from, certs_merge_func, to);
628 }
629
630 /**
631 * Same a hx509_certs_merge() but use a lock and name to describe the
632 * from source.
633 *
634 * @param context a hx509 context.
635 * @param to the store to merge into.
636 * @param lock a lock that unlocks the certificates store, use NULL to
637 * select no password/certifictes/prompt lock (see @ref page_lock).
638 * @param name name of the source store
639 *
640 * @return Returns an hx509 error code.
641 *
642 * @ingroup hx509_keyset
643 */
644
645 int
646 hx509_certs_append(hx509_context context,
647 hx509_certs to,
648 hx509_lock lock,
649 const char *name)
650 {
651 hx509_certs s;
652 int ret;
653
654 ret = hx509_certs_init(context, name, 0, lock, &s);
655 if (ret)
656 return ret;
657 ret = hx509_certs_merge(context, to, s);
658 hx509_certs_free(&s);
659 return ret;
660 }
661
662 /**
663 * Get one random certificate from the certificate store.
664 *
665 * @param context a hx509 context.
666 * @param certs a certificate store to get the certificate from.
667 * @param c return certificate, should be freed with hx509_cert_free().
668 *
669 * @return Returns an hx509 error code.
670 *
671 * @ingroup hx509_keyset
672 */
673
674 int
675 hx509_get_one_cert(hx509_context context, hx509_certs certs, hx509_cert *c)
676 {
677 hx509_cursor cursor;
678 int ret;
679
680 *c = NULL;
681
682 ret = hx509_certs_start_seq(context, certs, &cursor);
683 if (ret)
684 return ret;
685
686 ret = hx509_certs_next_cert(context, certs, cursor, c);
687 if (ret)
688 return ret;
689
690 hx509_certs_end_seq(context, certs, cursor);
691 return 0;
692 }
693
694 static int
695 certs_info_stdio(void *ctx, const char *str)
696 {
697 FILE *f = ctx;
698 fprintf(f, "%s\n", str);
699 return 0;
700 }
701
702 /**
703 * Print some info about the certificate store.
704 *
705 * @param context a hx509 context.
706 * @param certs certificate store to print information about.
707 * @param func function that will get each line of the information, if
708 * NULL is used the data is printed on a FILE descriptor that should
709 * be passed in ctx, if ctx also is NULL, stdout is used.
710 * @param ctx parameter to func.
711 *
712 * @return Returns an hx509 error code.
713 *
714 * @ingroup hx509_keyset
715 */
716
717 int
718 hx509_certs_info(hx509_context context,
719 hx509_certs certs,
720 int (*func)(void *, const char *),
721 void *ctx)
722 {
723 if (func == NULL) {
724 func = certs_info_stdio;
725 if (ctx == NULL)
726 ctx = stdout;
727 }
728 if (certs->ops->printinfo == NULL) {
729 (*func)(ctx, "No info function for certs");
730 return 0;
731 }
732 return (*certs->ops->printinfo)(context, certs, certs->ops_data,
733 func, ctx);
734 }
735
736 void
737 _hx509_pi_printf(int (*func)(void *, const char *), void *ctx,
738 const char *fmt, ...)
739 {
740 va_list ap;
741 char *str;
742 int ret;
743
744 va_start(ap, fmt);
745 ret = vasprintf(&str, fmt, ap);
746 va_end(ap);
747 if (ret == -1 || str == NULL)
748 return;
749 (*func)(ctx, str);
750 free(str);
751 }
752
753 int
754 _hx509_certs_keys_get(hx509_context context,
755 hx509_certs certs,
756 hx509_private_key **keys)
757 {
758 if (certs->ops->getkeys == NULL) {
759 *keys = NULL;
760 return 0;
761 }
762 return (*certs->ops->getkeys)(context, certs, certs->ops_data, keys);
763 }
764
765 int
766 _hx509_certs_keys_add(hx509_context context,
767 hx509_certs certs,
768 hx509_private_key key)
769 {
770 if (certs->ops->addkey == NULL) {
771 hx509_set_error_string(context, 0, EINVAL,
772 "keystore if type %s doesn't support "
773 "key add operation",
774 certs->ops->name);
775 return EINVAL;
776 }
777 return (*certs->ops->addkey)(context, certs, certs->ops_data, key);
778 }
779
780
781 void
782 _hx509_certs_keys_free(hx509_context context,
783 hx509_private_key *keys)
784 {
785 int i;
786 for (i = 0; keys[i]; i++)
787 hx509_private_key_free(&keys[i]);
788 free(keys);
789 }