1 /* $NetBSD: keyset.c,v 1.1.1.1 2011/04/13 18:15:11 elric Exp $ */
4 * Copyright (c) 2004 - 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
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
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
41 * @page page_keyset Certificate store operations
43 * Type of certificates store:
45 * In memory based format. Doesnt support storing.
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.
52 * Same as FILE, defaulting to PEM encoded certificates.
54 * Same as FILE, defaulting to DER encoded certificates.
59 * Apple Mac OS X KeyChain backed keychain object.
61 * See the library functions here: @ref hx509_keyset
64 struct hx509_certs_data
{
66 struct hx509_keyset_ops
*ops
;
70 static struct hx509_keyset_ops
*
71 _hx509_ks_type(hx509_context context
, const char *type
)
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
];
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
))
90 val
= realloc(context
->ks_ops
,
91 (context
->ks_num_ops
+ 1) * sizeof(context
->ks_ops
[0]));
94 val
[context
->ks_num_ops
] = ops
;
95 context
->ks_ops
= val
;
96 context
->ks_num_ops
++;
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
;
128 residue
= strchr(name
, ':');
130 type
= malloc(residue
- name
+ 1);
132 strlcpy(type
, name
, residue
- name
+ 1);
134 if (residue
[0] == '\0')
137 type
= strdup("MEMORY");
141 hx509_clear_error_string(context
);
145 ops
= _hx509_ks_type(context
, type
);
147 hx509_set_error_string(context
, 0, ENOENT
,
148 "Keyset type %s is not supported", type
);
153 c
= calloc(1, sizeof(*c
));
155 hx509_clear_error_string(context
);
161 ret
= (*ops
->init
)(context
, c
, &c
->ops_data
, flags
, residue
, lock
);
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
,
192 if (certs
->ops
->store
== NULL
) {
193 hx509_set_error_string(context
, 0, HX509_UNSUPPORTED_OPERATION
,
194 "keystore if type %s doesn't support "
197 return HX509_UNSUPPORTED_OPERATION
;
200 return (*certs
->ops
->store
)(context
, certs
, certs
->ops_data
, flags
, lock
);
205 hx509_certs_ref(hx509_certs certs
)
210 _hx509_abort("certs refcount == 0 on ref");
211 if (certs
->ref
== UINT_MAX
)
212 _hx509_abort("certs refcount == UINT_MAX on ref");
218 * Free a certificate store.
220 * @param certs certificate store to free.
222 * @ingroup hx509_keyset
226 hx509_certs_free(hx509_certs
*certs
)
229 if ((*certs
)->ref
== 0)
230 _hx509_abort("cert refcount == 0 on free");
231 if (--(*certs
)->ref
> 0)
234 (*(*certs
)->ops
->free
)(*certs
, (*certs
)->ops_data
);
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
252 * @ingroup hx509_keyset
256 hx509_certs_start_seq(hx509_context context
,
258 hx509_cursor
*cursor
)
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",
266 return HX509_UNSUPPORTED_OPERATION
;
269 ret
= (*certs
->ops
->iter_start
)(context
, certs
, certs
->ops_data
, cursor
);
277 * Get next ceritificate from the certificate keystore pointed out by
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
,
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
,
318 (*certs
->ops
->iter_end
)(context
, certs
, certs
->ops_data
, cursor
);
323 * Iterate over all certificates in a keystore and call an function
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
,
341 int (*func
)(hx509_context
, void *, hx509_cert
),
348 ret
= hx509_certs_start_seq(context
, certs
, &cursor
);
353 ret
= hx509_certs_next_cert(context
, certs
, cursor
, &c
);
360 ret
= (*func
)(context
, ctx
, c
);
366 hx509_certs_end_seq(context
, certs
, cursor
);
372 * Iterate over all certificates in a keystore and call an function
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
389 certs_iter(hx509_context context
, void *ctx
, hx509_cert cert
)
391 int (^func
)(hx509_cert
) = ctx
;
396 * Iterate over all certificates in a keystore and call an block
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
,
413 int (^func
)(hx509_cert
))
415 return hx509_certs_iter_f(context
, certs
, certs_iter
, func
);
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
)
440 cert
= _hx509_get_cert(c
);
442 _hx509_name_from_Name(&cert
->tbsCertificate
.subject
, &n
);
443 hx509_name_to_string(n
, &s
);
445 _hx509_name_from_Name(&cert
->tbsCertificate
.issuer
, &n
);
446 hx509_name_to_string(n
, &i
);
448 fprintf(ctx
, "subject: %s\nissuer: %s\n", s
, i
);
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",
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
,
500 const hx509_query
*q
,
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
);
520 ret
= hx509_certs_next_cert(context
, certs
, cursor
, &c
);
525 if (_hx509_query_match_cert(context
, q
, c
)) {
532 hx509_certs_end_seq(context
, certs
, cursor
);
536 * Return HX509_CERT_NOT_FOUND if no certificate in certs matched
540 hx509_clear_error_string(context
);
541 return HX509_CERT_NOT_FOUND
;
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
,
564 const hx509_query
*q
,
571 _hx509_query_statistic(context
, 0, q
);
573 ret
= hx509_certs_init(context
, "MEMORY:filter-certs", 0,
578 ret
= hx509_certs_start_seq(context
, certs
, &cursor
);
580 hx509_certs_free(result
);
586 ret
= hx509_certs_next_cert(context
, certs
, cursor
, &c
);
591 if (_hx509_query_match_cert(context
, q
, c
)) {
592 hx509_certs_add(context
, *result
, c
);
598 hx509_certs_end_seq(context
, certs
, cursor
);
600 hx509_certs_free(result
);
605 * Return HX509_CERT_NOT_FOUND if no certificate in certs matched
609 hx509_certs_free(result
);
610 hx509_clear_error_string(context
);
611 return HX509_CERT_NOT_FOUND
;
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
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
)
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
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
,
669 ret
= hx509_certs_init(context
, name
, 0, lock
, &s
);
672 ret
= hx509_certs_merge(context
, to
, s
);
673 hx509_certs_free(&s
);
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
)
697 ret
= hx509_certs_start_seq(context
, certs
, &cursor
);
701 ret
= hx509_certs_next_cert(context
, certs
, cursor
, c
);
705 hx509_certs_end_seq(context
, certs
, cursor
);
710 certs_info_stdio(void *ctx
, const char *str
)
713 fprintf(f
, "%s\n", str
);
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
,
735 int (*func
)(void *, const char *),
739 func
= certs_info_stdio
;
743 if (certs
->ops
->printinfo
== NULL
) {
744 (*func
)(ctx
, "No info function for certs");
747 return (*certs
->ops
->printinfo
)(context
, certs
, certs
->ops_data
,
752 _hx509_pi_printf(int (*func
)(void *, const char *), void *ctx
,
753 const char *fmt
, ...)
759 vasprintf(&str
, fmt
, ap
);
768 _hx509_certs_keys_get(hx509_context context
,
770 hx509_private_key
**keys
)
772 if (certs
->ops
->getkeys
== NULL
) {
776 return (*certs
->ops
->getkeys
)(context
, certs
, certs
->ops_data
, keys
);
780 _hx509_certs_keys_add(hx509_context context
,
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 "
791 return (*certs
->ops
->addkey
)(context
, certs
, certs
->ops_data
, key
);
796 _hx509_certs_keys_free(hx509_context context
,
797 hx509_private_key
*keys
)
800 for (i
= 0; keys
[i
]; i
++)
801 hx509_private_key_free(&keys
[i
]);