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
37 * @page page_keyset Certificate store operations
39 * Type of certificates store:
41 * In memory based format. Doesnt support storing.
43 * FILE supports raw DER certicates and PEM certicates. When PEM is
44 * used the file can contain may certificates and match private
45 * keys. Support storing the certificates. DER format only supports
46 * on certificate and no private key.
48 * Same as FILE, defaulting to PEM encoded certificates.
50 * Same as FILE, defaulting to DER encoded certificates.
55 * Apple Mac OS X KeyChain backed keychain object.
57 * See the library functions here: @ref hx509_keyset
60 struct hx509_certs_data
{
62 struct hx509_keyset_ops
*ops
;
66 static struct hx509_keyset_ops
*
67 _hx509_ks_type(hx509_context context
, const char *type
)
71 for (i
= 0; i
< context
->ks_num_ops
; i
++)
72 if (strcasecmp(type
, context
->ks_ops
[i
]->name
) == 0)
73 return context
->ks_ops
[i
];
79 _hx509_ks_register(hx509_context context
, struct hx509_keyset_ops
*ops
)
81 struct hx509_keyset_ops
**val
;
83 if (_hx509_ks_type(context
, ops
->name
))
86 val
= realloc(context
->ks_ops
,
87 (context
->ks_num_ops
+ 1) * sizeof(context
->ks_ops
[0]));
90 val
[context
->ks_num_ops
] = ops
;
91 context
->ks_ops
= val
;
92 context
->ks_num_ops
++;
96 * Open or creates a new hx509 certificate store.
98 * @param context A hx509 context
99 * @param name name of the store, format is TYPE:type-specific-string,
100 * if NULL is used the MEMORY store is used.
101 * @param flags list of flags:
102 * - HX509_CERTS_CREATE create a new keystore of the specific TYPE.
103 * - HX509_CERTS_UNPROTECT_ALL fails if any private key failed to be extracted.
104 * @param lock a lock that unlocks the certificates store, use NULL to
105 * select no password/certifictes/prompt lock (see @ref page_lock).
106 * @param certs return pointer, free with hx509_certs_free().
108 * @ingroup hx509_keyset
112 hx509_certs_init(hx509_context context
,
113 const char *name
, int flags
,
114 hx509_lock lock
, hx509_certs
*certs
)
116 struct hx509_keyset_ops
*ops
;
124 residue
= strchr(name
, ':');
126 type
= malloc(residue
- name
+ 1);
128 strlcpy(type
, name
, residue
- name
+ 1);
130 if (residue
[0] == '\0')
133 type
= strdup("MEMORY");
137 hx509_clear_error_string(context
);
141 ops
= _hx509_ks_type(context
, type
);
143 hx509_set_error_string(context
, 0, ENOENT
,
144 "Keyset type %s is not supported", type
);
149 c
= calloc(1, sizeof(*c
));
151 hx509_clear_error_string(context
);
157 ret
= (*ops
->init
)(context
, c
, &c
->ops_data
, flags
, residue
, lock
);
168 * Write the certificate store to stable storage.
170 * @param context A hx509 context.
171 * @param certs a certificate store to store.
172 * @param flags currently unused, use 0.
173 * @param lock a lock that unlocks the certificates store, use NULL to
174 * select no password/certifictes/prompt lock (see @ref page_lock).
176 * @return Returns an hx509 error code. HX509_UNSUPPORTED_OPERATION if
177 * the certificate store doesn't support the store operation.
179 * @ingroup hx509_keyset
183 hx509_certs_store(hx509_context context
,
188 if (certs
->ops
->store
== NULL
) {
189 hx509_set_error_string(context
, 0, HX509_UNSUPPORTED_OPERATION
,
190 "keystore if type %s doesn't support "
193 return HX509_UNSUPPORTED_OPERATION
;
196 return (*certs
->ops
->store
)(context
, certs
, certs
->ops_data
, flags
, lock
);
201 hx509_certs_ref(hx509_certs certs
)
206 _hx509_abort("certs refcount == 0 on ref");
207 if (certs
->ref
== UINT_MAX
)
208 _hx509_abort("certs refcount == UINT_MAX on ref");
214 * Free a certificate store.
216 * @param certs certificate store to free.
218 * @ingroup hx509_keyset
222 hx509_certs_free(hx509_certs
*certs
)
225 if ((*certs
)->ref
== 0)
226 _hx509_abort("cert refcount == 0 on free");
227 if (--(*certs
)->ref
> 0)
230 (*(*certs
)->ops
->free
)(*certs
, (*certs
)->ops_data
);
237 * Start the integration
239 * @param context a hx509 context.
240 * @param certs certificate store to iterate over
241 * @param cursor cursor that will keep track of progress, free with
242 * hx509_certs_end_seq().
244 * @return Returns an hx509 error code. HX509_UNSUPPORTED_OPERATION is
245 * returned if the certificate store doesn't support the iteration
248 * @ingroup hx509_keyset
252 hx509_certs_start_seq(hx509_context context
,
254 hx509_cursor
*cursor
)
258 if (certs
->ops
->iter_start
== NULL
) {
259 hx509_set_error_string(context
, 0, HX509_UNSUPPORTED_OPERATION
,
260 "Keyset type %s doesn't support iteration",
262 return HX509_UNSUPPORTED_OPERATION
;
265 ret
= (*certs
->ops
->iter_start
)(context
, certs
, certs
->ops_data
, cursor
);
273 * Get next ceritificate from the certificate keystore pointed out by
276 * @param context a hx509 context.
277 * @param certs certificate store to iterate over.
278 * @param cursor cursor that keeps track of progress.
279 * @param cert return certificate next in store, NULL if the store
280 * contains no more certificates. Free with hx509_cert_free().
282 * @return Returns an hx509 error code.
284 * @ingroup hx509_keyset
288 hx509_certs_next_cert(hx509_context context
,
294 return (*certs
->ops
->iter
)(context
, certs
, certs
->ops_data
, cursor
, cert
);
298 * End the iteration over certificates.
300 * @param context a hx509 context.
301 * @param certs certificate store to iterate over.
302 * @param cursor cursor that will keep track of progress, freed.
304 * @return Returns an hx509 error code.
306 * @ingroup hx509_keyset
310 hx509_certs_end_seq(hx509_context context
,
314 (*certs
->ops
->iter_end
)(context
, certs
, certs
->ops_data
, cursor
);
319 * Iterate over all certificates in a keystore and call an function
322 * @param context a hx509 context.
323 * @param certs certificate store to iterate over.
324 * @param func function to call for each certificate. The function
325 * should return non-zero to abort the iteration, that value is passed
326 * back to te caller of hx509_certs_iter().
327 * @param ctx context variable that will passed to the function.
329 * @return Returns an hx509 error code.
331 * @ingroup hx509_keyset
335 hx509_certs_iter(hx509_context context
,
337 int (*func
)(hx509_context
, void *, hx509_cert
),
344 ret
= hx509_certs_start_seq(context
, certs
, &cursor
);
349 ret
= hx509_certs_next_cert(context
, certs
, cursor
, &c
);
356 ret
= (*func
)(context
, ctx
, c
);
362 hx509_certs_end_seq(context
, certs
, cursor
);
369 * Function to use to hx509_certs_iter() as a function argument, the
370 * ctx variable to hx509_certs_iter() should be a FILE file descriptor.
372 * @param context a hx509 context.
373 * @param ctx used by hx509_certs_iter().
374 * @param c a certificate
376 * @return Returns an hx509 error code.
378 * @ingroup hx509_keyset
382 hx509_ci_print_names(hx509_context context
, void *ctx
, hx509_cert c
)
388 cert
= _hx509_get_cert(c
);
390 _hx509_name_from_Name(&cert
->tbsCertificate
.subject
, &n
);
391 hx509_name_to_string(n
, &s
);
393 _hx509_name_from_Name(&cert
->tbsCertificate
.issuer
, &n
);
394 hx509_name_to_string(n
, &i
);
396 fprintf(ctx
, "subject: %s\nissuer: %s\n", s
, i
);
403 * Add a certificate to the certificiate store.
405 * The receiving keyset certs will either increase reference counter
406 * of the cert or make a deep copy, either way, the caller needs to
407 * free the cert itself.
409 * @param context a hx509 context.
410 * @param certs certificate store to add the certificate to.
411 * @param cert certificate to add.
413 * @return Returns an hx509 error code.
415 * @ingroup hx509_keyset
419 hx509_certs_add(hx509_context context
, hx509_certs certs
, hx509_cert cert
)
421 if (certs
->ops
->add
== NULL
) {
422 hx509_set_error_string(context
, 0, ENOENT
,
423 "Keyset type %s doesn't support add operation",
428 return (*certs
->ops
->add
)(context
, certs
, certs
->ops_data
, cert
);
432 * Find a certificate matching the query.
434 * @param context a hx509 context.
435 * @param certs certificate store to search.
436 * @param q query allocated with @ref hx509_query functions.
437 * @param r return certificate (or NULL on error), should be freed
438 * with hx509_cert_free().
440 * @return Returns an hx509 error code.
442 * @ingroup hx509_keyset
446 hx509_certs_find(hx509_context context
,
448 const hx509_query
*q
,
457 _hx509_query_statistic(context
, 0, q
);
459 if (certs
->ops
->query
)
460 return (*certs
->ops
->query
)(context
, certs
, certs
->ops_data
, q
, r
);
462 ret
= hx509_certs_start_seq(context
, certs
, &cursor
);
468 ret
= hx509_certs_next_cert(context
, certs
, cursor
, &c
);
473 if (_hx509_query_match_cert(context
, q
, c
)) {
480 hx509_certs_end_seq(context
, certs
, cursor
);
484 * Return HX509_CERT_NOT_FOUND if no certificate in certs matched
488 hx509_clear_error_string(context
);
489 return HX509_CERT_NOT_FOUND
;
496 * Filter certificate matching the query.
498 * @param context a hx509 context.
499 * @param certs certificate store to search.
500 * @param q query allocated with @ref hx509_query functions.
501 * @param result the filtered certificate store, caller must free with
502 * hx509_certs_free().
504 * @return Returns an hx509 error code.
506 * @ingroup hx509_keyset
510 hx509_certs_filter(hx509_context context
,
512 const hx509_query
*q
,
519 _hx509_query_statistic(context
, 0, q
);
521 ret
= hx509_certs_init(context
, "MEMORY:filter-certs", 0,
526 ret
= hx509_certs_start_seq(context
, certs
, &cursor
);
528 hx509_certs_free(result
);
534 ret
= hx509_certs_next_cert(context
, certs
, cursor
, &c
);
539 if (_hx509_query_match_cert(context
, q
, c
)) {
540 hx509_certs_add(context
, *result
, c
);
546 hx509_certs_end_seq(context
, certs
, cursor
);
548 hx509_certs_free(result
);
553 * Return HX509_CERT_NOT_FOUND if no certificate in certs matched
557 hx509_certs_free(result
);
558 hx509_clear_error_string(context
);
559 return HX509_CERT_NOT_FOUND
;
567 certs_merge_func(hx509_context context
, void *ctx
, hx509_cert c
)
569 return hx509_certs_add(context
, (hx509_certs
)ctx
, c
);
573 * Merge a certificate store into another. The from store is keep
576 * @param context a hx509 context.
577 * @param to the store to merge into.
578 * @param from the store to copy the object from.
580 * @return Returns an hx509 error code.
582 * @ingroup hx509_keyset
586 hx509_certs_merge(hx509_context context
, hx509_certs to
, hx509_certs from
)
590 return hx509_certs_iter(context
, from
, certs_merge_func
, to
);
594 * Same a hx509_certs_merge() but use a lock and name to describe the
597 * @param context a hx509 context.
598 * @param to the store to merge into.
599 * @param lock a lock that unlocks the certificates store, use NULL to
600 * select no password/certifictes/prompt lock (see @ref page_lock).
601 * @param name name of the source store
603 * @return Returns an hx509 error code.
605 * @ingroup hx509_keyset
609 hx509_certs_append(hx509_context context
,
617 ret
= hx509_certs_init(context
, name
, 0, lock
, &s
);
620 ret
= hx509_certs_merge(context
, to
, s
);
621 hx509_certs_free(&s
);
626 * Get one random certificate from the certificate store.
628 * @param context a hx509 context.
629 * @param certs a certificate store to get the certificate from.
630 * @param c return certificate, should be freed with hx509_cert_free().
632 * @return Returns an hx509 error code.
634 * @ingroup hx509_keyset
638 hx509_get_one_cert(hx509_context context
, hx509_certs certs
, hx509_cert
*c
)
645 ret
= hx509_certs_start_seq(context
, certs
, &cursor
);
649 ret
= hx509_certs_next_cert(context
, certs
, cursor
, c
);
653 hx509_certs_end_seq(context
, certs
, cursor
);
658 certs_info_stdio(void *ctx
, const char *str
)
661 fprintf(f
, "%s\n", str
);
666 * Print some info about the certificate store.
668 * @param context a hx509 context.
669 * @param certs certificate store to print information about.
670 * @param func function that will get each line of the information, if
671 * NULL is used the data is printed on a FILE descriptor that should
672 * be passed in ctx, if ctx also is NULL, stdout is used.
673 * @param ctx parameter to func.
675 * @return Returns an hx509 error code.
677 * @ingroup hx509_keyset
681 hx509_certs_info(hx509_context context
,
683 int (*func
)(void *, const char *),
687 func
= certs_info_stdio
;
691 if (certs
->ops
->printinfo
== NULL
) {
692 (*func
)(ctx
, "No info function for certs");
695 return (*certs
->ops
->printinfo
)(context
, certs
, certs
->ops_data
,
700 _hx509_pi_printf(int (*func
)(void *, const char *), void *ctx
,
701 const char *fmt
, ...)
707 vasprintf(&str
, fmt
, ap
);
716 _hx509_certs_keys_get(hx509_context context
,
718 hx509_private_key
**keys
)
720 if (certs
->ops
->getkeys
== NULL
) {
724 return (*certs
->ops
->getkeys
)(context
, certs
, certs
->ops_data
, keys
);
728 _hx509_certs_keys_add(hx509_context context
,
730 hx509_private_key key
)
732 if (certs
->ops
->addkey
== NULL
) {
733 hx509_set_error_string(context
, 0, EINVAL
,
734 "keystore if type %s doesn't support "
739 return (*certs
->ops
->addkey
)(context
, certs
, certs
->ops_data
, key
);
744 _hx509_certs_keys_free(hx509_context context
,
745 hx509_private_key
*keys
)
748 for (i
= 0; keys
[i
]; i
++)
749 _hx509_private_key_free(&keys
[i
]);