📄 keyset.c
字号:
/* * Copyright (c) 2004 - 2007 Kungliga Tekniska H鰃skolan * (Royal Institute of Technology, Stockholm, Sweden). * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * 3. Neither the name of the Institute nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#include "hx_locl.h"RCSID("$Id: keyset.c 22466 2008-01-16 14:26:35Z lha $");/** * @page page_keyset Certificate store operations * * Type of certificates store: * - MEMORY * In memory based format. Doesnt support storing. * - FILE * FILE supports raw DER certicates and PEM certicates. When PEM is * used the file can contain may certificates and match private * keys. Support storing the certificates. DER format only supports * on certificate and no private key. * - PEM-FILE * Same as FILE, defaulting to PEM encoded certificates. * - PEM-FILE * Same as FILE, defaulting to DER encoded certificates. * - PKCS11 * - PKCS12 * - DIR * - KEYCHAIN * Apple Mac OS X KeyChain backed keychain object. * * See the library functions here: @ref hx509_keyset */struct hx509_certs_data { int ref; struct hx509_keyset_ops *ops; void *ops_data;};static struct hx509_keyset_ops *_hx509_ks_type(hx509_context context, const char *type){ int i; for (i = 0; i < context->ks_num_ops; i++) if (strcasecmp(type, context->ks_ops[i]->name) == 0) return context->ks_ops[i]; return NULL;}void_hx509_ks_register(hx509_context context, struct hx509_keyset_ops *ops){ struct hx509_keyset_ops **val; if (_hx509_ks_type(context, ops->name)) return; val = realloc(context->ks_ops, (context->ks_num_ops + 1) * sizeof(context->ks_ops[0])); if (val == NULL) return; val[context->ks_num_ops] = ops; context->ks_ops = val; context->ks_num_ops++;}/** * Open or creates a new hx509 certificate store. * * @param context A hx509 context * @param name name of the store, format is TYPE:type-specific-string, * if NULL is used the MEMORY store is used. * @param flags list of flags: * - HX509_CERTS_CREATE create a new keystore of the specific TYPE. * - HX509_CERTS_UNPROTECT_ALL fails if any private key failed to be extracted. * @param lock a lock that unlocks the certificates store, use NULL to * select no password/certifictes/prompt lock (see @ref page_lock). * @param certs return pointer, free with hx509_certs_free(). * * @ingroup hx509_keyset */inthx509_certs_init(hx509_context context, const char *name, int flags, hx509_lock lock, hx509_certs *certs){ struct hx509_keyset_ops *ops; const char *residue; hx509_certs c; char *type; int ret; *certs = NULL; residue = strchr(name, ':'); if (residue) { type = malloc(residue - name + 1); if (type) strlcpy(type, name, residue - name + 1); residue++; if (residue[0] == '\0') residue = NULL; } else { type = strdup("MEMORY"); residue = name; } if (type == NULL) { hx509_clear_error_string(context); return ENOMEM; } ops = _hx509_ks_type(context, type); if (ops == NULL) { hx509_set_error_string(context, 0, ENOENT, "Keyset type %s is not supported", type); free(type); return ENOENT; } free(type); c = calloc(1, sizeof(*c)); if (c == NULL) { hx509_clear_error_string(context); return ENOMEM; } c->ops = ops; c->ref = 1; ret = (*ops->init)(context, c, &c->ops_data, flags, residue, lock); if (ret) { free(c); return ret; } *certs = c; return 0;}/** * Write the certificate store to stable storage. * * @param context A hx509 context. * @param certs a certificate store to store. * @param flags currently unused, use 0. * @param lock a lock that unlocks the certificates store, use NULL to * select no password/certifictes/prompt lock (see @ref page_lock). * * @return Returns an hx509 error code. HX509_UNSUPPORTED_OPERATION if * the certificate store doesn't support the store operation. * * @ingroup hx509_keyset */inthx509_certs_store(hx509_context context, hx509_certs certs, int flags, hx509_lock lock){ if (certs->ops->store == NULL) { hx509_set_error_string(context, 0, HX509_UNSUPPORTED_OPERATION, "keystore if type %s doesn't support " "store operation", certs->ops->name); return HX509_UNSUPPORTED_OPERATION; } return (*certs->ops->store)(context, certs, certs->ops_data, flags, lock);}hx509_certs_hx509_certs_ref(hx509_certs certs){ if (certs == NULL) return NULL; if (certs->ref <= 0) _hx509_abort("certs refcount <= 0"); certs->ref++; if (certs->ref == 0) _hx509_abort("certs refcount == 0"); return certs;}/** * Free a certificate store. * * @param certs certificate store to free. * * @ingroup hx509_keyset */voidhx509_certs_free(hx509_certs *certs){ if (*certs) { if ((*certs)->ref <= 0) _hx509_abort("refcount <= 0"); if (--(*certs)->ref > 0) return; (*(*certs)->ops->free)(*certs, (*certs)->ops_data); free(*certs); *certs = NULL; }}/** * Start the integration * * @param context a hx509 context. * @param certs certificate store to iterate over * @param cursor cursor that will keep track of progress, free with * hx509_certs_end_seq(). * * @return Returns an hx509 error code. HX509_UNSUPPORTED_OPERATION is * returned if the certificate store doesn't support the iteration * operation. * * @ingroup hx509_keyset */inthx509_certs_start_seq(hx509_context context, hx509_certs certs, hx509_cursor *cursor){ int ret; if (certs->ops->iter_start == NULL) { hx509_set_error_string(context, 0, HX509_UNSUPPORTED_OPERATION, "Keyset type %s doesn't support iteration", certs->ops->name); return HX509_UNSUPPORTED_OPERATION; } ret = (*certs->ops->iter_start)(context, certs, certs->ops_data, cursor); if (ret) return ret; return 0;}/** * Get next ceritificate from the certificate keystore pointed out by * cursor. * * @param context a hx509 context. * @param certs certificate store to iterate over. * @param cursor cursor that keeps track of progress. * @param cert return certificate next in store, NULL if the store * contains no more certificates. Free with hx509_cert_free(). * * @return Returns an hx509 error code. * * @ingroup hx509_keyset */inthx509_certs_next_cert(hx509_context context, hx509_certs certs, hx509_cursor cursor, hx509_cert *cert){ *cert = NULL; return (*certs->ops->iter)(context, certs, certs->ops_data, cursor, cert);}/** * End the iteration over certificates. * * @param context a hx509 context. * @param certs certificate store to iterate over. * @param cursor cursor that will keep track of progress, freed. * * @return Returns an hx509 error code. * * @ingroup hx509_keyset */inthx509_certs_end_seq(hx509_context context, hx509_certs certs, hx509_cursor cursor){ (*certs->ops->iter_end)(context, certs, certs->ops_data, cursor); return 0;}/** * Iterate over all certificates in a keystore and call an function * for each fo them. * * @param context a hx509 context. * @param certs certificate store to iterate over. * @param func function to call for each certificate. The function * should return non-zero to abort the iteration, that value is passed * back to te caller of hx509_certs_iter(). * @param ctx context variable that will passed to the function. * * @return Returns an hx509 error code. * * @ingroup hx509_keyset */inthx509_certs_iter(hx509_context context, hx509_certs certs, int (*func)(hx509_context, void *, hx509_cert), void *ctx)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -