📄 keyset.c
字号:
{ hx509_cursor cursor; hx509_cert c; int ret; ret = hx509_certs_start_seq(context, certs, &cursor); if (ret) return ret; while (1) { ret = hx509_certs_next_cert(context, certs, cursor, &c); if (ret) break; if (c == NULL) { ret = 0; break; } ret = (*func)(context, ctx, c); hx509_cert_free(c); if (ret) break; } hx509_certs_end_seq(context, certs, cursor); return ret;}/** * Function to use to hx509_certs_iter() as a function argument, the * ctx variable to hx509_certs_iter() should be a FILE file descriptor. * * @param context a hx509 context. * @param ctx used by hx509_certs_iter(). * @param c a certificate * * @return Returns an hx509 error code. * * @ingroup hx509_keyset */inthx509_ci_print_names(hx509_context context, void *ctx, hx509_cert c){ Certificate *cert; hx509_name n; char *s, *i; cert = _hx509_get_cert(c); _hx509_name_from_Name(&cert->tbsCertificate.subject, &n); hx509_name_to_string(n, &s); hx509_name_free(&n); _hx509_name_from_Name(&cert->tbsCertificate.issuer, &n); hx509_name_to_string(n, &i); hx509_name_free(&n); fprintf(ctx, "subject: %s\nissuer: %s\n", s, i); free(s); free(i); return 0;}/** * Add a certificate to the certificiate store. * * The receiving keyset certs will either increase reference counter * of the cert or make a deep copy, either way, the caller needs to * free the cert itself. * * @param context a hx509 context. * @param certs certificate store to add the certificate to. * @param cert certificate to add. * * @return Returns an hx509 error code. * * @ingroup hx509_keyset */inthx509_certs_add(hx509_context context, hx509_certs certs, hx509_cert cert){ if (certs->ops->add == NULL) { hx509_set_error_string(context, 0, ENOENT, "Keyset type %s doesn't support add operation", certs->ops->name); return ENOENT; } return (*certs->ops->add)(context, certs, certs->ops_data, cert);}/** * Find a certificate matching the query. * * @param context a hx509 context. * @param certs certificate store to search. * @param q query allocated with @ref hx509_query functions. * @param r return certificate (or NULL on error), should be freed * with hx509_cert_free(). * * @return Returns an hx509 error code. * * @ingroup hx509_keyset */inthx509_certs_find(hx509_context context, hx509_certs certs, const hx509_query *q, hx509_cert *r){ hx509_cursor cursor; hx509_cert c; int ret; *r = NULL; _hx509_query_statistic(context, 0, q); if (certs->ops->query) return (*certs->ops->query)(context, certs, certs->ops_data, q, r); ret = hx509_certs_start_seq(context, certs, &cursor); if (ret) return ret; c = NULL; while (1) { ret = hx509_certs_next_cert(context, certs, cursor, &c); if (ret) break; if (c == NULL) break; if (_hx509_query_match_cert(context, q, c)) { *r = c; break; } hx509_cert_free(c); } hx509_certs_end_seq(context, certs, cursor); if (ret) return ret; if (c == NULL) { hx509_clear_error_string(context); return HX509_CERT_NOT_FOUND; } return 0;}static intcerts_merge_func(hx509_context context, void *ctx, hx509_cert c){ return hx509_certs_add(context, (hx509_certs)ctx, c);}/** * Merge a certificate store into another. The from store is keep * intact. * * @param context a hx509 context. * @param to the store to merge into. * @param from the store to copy the object from. * * @return Returns an hx509 error code. * * @ingroup hx509_keyset */inthx509_certs_merge(hx509_context context, hx509_certs to, hx509_certs from){ if (from == NULL) return 0; return hx509_certs_iter(context, from, certs_merge_func, to);}/** * Same a hx509_certs_merge() but use a lock and name to describe the * from source. * * @param context a hx509 context. * @param to the store to merge into. * @param lock a lock that unlocks the certificates store, use NULL to * select no password/certifictes/prompt lock (see @ref page_lock). * @param name name of the source store * * @return Returns an hx509 error code. * * @ingroup hx509_keyset */inthx509_certs_append(hx509_context context, hx509_certs to, hx509_lock lock, const char *name){ hx509_certs s; int ret; ret = hx509_certs_init(context, name, 0, lock, &s); if (ret) return ret; ret = hx509_certs_merge(context, to, s); hx509_certs_free(&s); return ret;}/** * Get one random certificate from the certificate store. * * @param context a hx509 context. * @param certs a certificate store to get the certificate from. * @param c return certificate, should be freed with hx509_cert_free(). * * @return Returns an hx509 error code. * * @ingroup hx509_keyset */inthx509_get_one_cert(hx509_context context, hx509_certs certs, hx509_cert *c){ hx509_cursor cursor; int ret; *c = NULL; ret = hx509_certs_start_seq(context, certs, &cursor); if (ret) return ret; ret = hx509_certs_next_cert(context, certs, cursor, c); if (ret) return ret; hx509_certs_end_seq(context, certs, cursor); return 0;}static intcerts_info_stdio(void *ctx, const char *str){ FILE *f = ctx; fprintf(f, "%s\n", str); return 0;}/** * Print some info about the certificate store. * * @param context a hx509 context. * @param certs certificate store to print information about. * @param func function that will get each line of the information, if * NULL is used the data is printed on a FILE descriptor that should * be passed in ctx, if ctx also is NULL, stdout is used. * @param ctx parameter to func. * * @return Returns an hx509 error code. * * @ingroup hx509_keyset */inthx509_certs_info(hx509_context context, hx509_certs certs, int (*func)(void *, const char *), void *ctx){ if (func == NULL) { func = certs_info_stdio; if (ctx == NULL) ctx = stdout; } if (certs->ops->printinfo == NULL) { (*func)(ctx, "No info function for certs"); return 0; } return (*certs->ops->printinfo)(context, certs, certs->ops_data, func, ctx);}void_hx509_pi_printf(int (*func)(void *, const char *), void *ctx, const char *fmt, ...){ va_list ap; char *str; va_start(ap, fmt); vasprintf(&str, fmt, ap); va_end(ap); if (str == NULL) return; (*func)(ctx, str); free(str);}int_hx509_certs_keys_get(hx509_context context, hx509_certs certs, hx509_private_key **keys){ if (certs->ops->getkeys == NULL) { *keys = NULL; return 0; } return (*certs->ops->getkeys)(context, certs, certs->ops_data, keys);}int_hx509_certs_keys_add(hx509_context context, hx509_certs certs, hx509_private_key key){ if (certs->ops->addkey == NULL) { hx509_set_error_string(context, 0, EINVAL, "keystore if type %s doesn't support " "key add operation", certs->ops->name); return EINVAL; } return (*certs->ops->addkey)(context, certs, certs->ops_data, key);}void_hx509_certs_keys_free(hx509_context context, hx509_private_key *keys){ int i; for (i = 0; keys[i]; i++) _hx509_private_key_free(&keys[i]); free(keys);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -