📄 print.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: print.c 22538 2008-01-27 13:05:47Z lha $");/** * @page page_print Hx509 printing functions * * See the library functions here: @ref hx509_print */struct hx509_validate_ctx_data { int flags; hx509_vprint_func vprint_func; void *ctx;};struct cert_status { unsigned int selfsigned:1; unsigned int isca:1; unsigned int isproxy:1; unsigned int haveSAN:1; unsigned int haveIAN:1; unsigned int haveSKI:1; unsigned int haveAKI:1; unsigned int haveCRLDP:1;};/* * */static intTime2string(const Time *T, char **str){ time_t t; char *s; struct tm *tm; *str = NULL; t = _hx509_Time2time_t(T); tm = gmtime (&t); s = malloc(30); if (s == NULL) return ENOMEM; strftime(s, 30, "%Y-%m-%d %H:%M:%S", tm); *str = s; return 0;}/** * Helper function to print on stdout for: * - hx509_oid_print(), * - hx509_bitstring_print(), * - hx509_validate_ctx_set_print(). * * @param ctx the context to the print function. If the ctx is NULL, * stdout is used. * @param fmt the printing format. * @param va the argumet list. * * @ingroup hx509_print */voidhx509_print_stdout(void *ctx, const char *fmt, va_list va){ FILE *f = ctx; if (f == NULL) f = stdout; vfprintf(f, fmt, va);}static voidprint_func(hx509_vprint_func func, void *ctx, const char *fmt, ...){ va_list va; va_start(va, fmt); (*func)(ctx, fmt, va); va_end(va);}/** * Print a oid to a string. * * @param oid oid to print * @param str allocated string, free with hx509_xfree(). * * @return An hx509 error code, see hx509_get_error_string(). * * @ingroup hx509_print */inthx509_oid_sprint(const heim_oid *oid, char **str){ return der_print_heim_oid(oid, '.', str);}/** * Print a oid using a hx509_vprint_func function. To print to stdout * use hx509_print_stdout(). * * @param oid oid to print * @param func hx509_vprint_func to print with. * @param ctx context variable to hx509_vprint_func function. * * @ingroup hx509_print */voidhx509_oid_print(const heim_oid *oid, hx509_vprint_func func, void *ctx){ char *str; hx509_oid_sprint(oid, &str); print_func(func, ctx, "%s", str); free(str);}/** * Print a bitstring using a hx509_vprint_func function. To print to * stdout use hx509_print_stdout(). * * @param b bit string to print. * @param func hx509_vprint_func to print with. * @param ctx context variable to hx509_vprint_func function. * * @ingroup hx509_print */voidhx509_bitstring_print(const heim_bit_string *b, hx509_vprint_func func, void *ctx){ int i; print_func(func, ctx, "\tlength: %d\n\t", b->length); for (i = 0; i < (b->length + 7) / 8; i++) print_func(func, ctx, "%02x%s%s", ((unsigned char *)b->data)[i], i < (b->length - 7) / 8 && (i == 0 || (i % 16) != 15) ? ":" : "", i != 0 && (i % 16) == 15 ? (i <= ((b->length + 7) / 8 - 2) ? "\n\t" : "\n"):"");}/** * Print certificate usage for a certificate to a string. * * @param context A hx509 context. * @param c a certificate print the keyusage for. * @param s the return string with the keysage printed in to, free * with hx509_xfree(). * * @return An hx509 error code, see hx509_get_error_string(). * * @ingroup hx509_print */inthx509_cert_keyusage_print(hx509_context context, hx509_cert c, char **s){ KeyUsage ku; char buf[256]; int ret; *s = NULL; ret = _hx509_cert_get_keyusage(context, c, &ku); if (ret) return ret; unparse_flags(KeyUsage2int(ku), asn1_KeyUsage_units(), buf, sizeof(buf)); *s = strdup(buf); if (*s == NULL) { hx509_set_error_string(context, 0, ENOMEM, "out of memory"); return ENOMEM; } return 0;}/* * */static voidvalidate_vprint(void *c, const char *fmt, va_list va){ hx509_validate_ctx ctx = c; if (ctx->vprint_func == NULL) return; (ctx->vprint_func)(ctx->ctx, fmt, va);}static voidvalidate_print(hx509_validate_ctx ctx, int flags, const char *fmt, ...){ va_list va; if ((ctx->flags & flags) == 0) return; va_start(va, fmt); validate_vprint(ctx, fmt, va); va_end(va);}/* * Dont Care, SHOULD critical, SHOULD NOT critical, MUST critical, * MUST NOT critical */enum critical_flag { D_C = 0, S_C, S_N_C, M_C, M_N_C };static intcheck_Null(hx509_validate_ctx ctx, struct cert_status *status, enum critical_flag cf, const Extension *e){ switch(cf) { case D_C: break; case S_C: if (!e->critical) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "\tCritical not set on SHOULD\n"); break; case S_N_C: if (e->critical) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "\tCritical set on SHOULD NOT\n"); break; case M_C: if (!e->critical) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "\tCritical not set on MUST\n"); break; case M_N_C: if (e->critical) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "\tCritical set on MUST NOT\n"); break; default: _hx509_abort("internal check_Null state error"); } return 0;}static intcheck_subjectKeyIdentifier(hx509_validate_ctx ctx, struct cert_status *status, enum critical_flag cf, const Extension *e){ SubjectKeyIdentifier si; size_t size; int ret; status->haveSKI = 1; check_Null(ctx, status, cf, e); ret = decode_SubjectKeyIdentifier(e->extnValue.data, e->extnValue.length, &si, &size); if (ret) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Decoding SubjectKeyIdentifier failed: %d", ret); return 1; } if (size != e->extnValue.length) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Decoding SKI ahve extra bits on the end"); return 1; } if (si.length == 0) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "SKI is too short (0 bytes)"); if (si.length > 20) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "SKI is too long"); { char *id; hex_encode(si.data, si.length, &id); if (id) { validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "\tsubject key id: %s\n", id); free(id); } } free_SubjectKeyIdentifier(&si); return 0;}static intcheck_authorityKeyIdentifier(hx509_validate_ctx ctx, struct cert_status *status, enum critical_flag cf, const Extension *e){ AuthorityKeyIdentifier ai; size_t size; int ret; status->haveAKI = 1; check_Null(ctx, status, cf, e); ret = decode_AuthorityKeyIdentifier(e->extnValue.data, e->extnValue.length, &ai, &size); if (ret) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Decoding AuthorityKeyIdentifier failed: %d", ret); return 1; } if (size != e->extnValue.length) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Decoding SKI ahve extra bits on the end"); return 1; } if (ai.keyIdentifier) { char *id; hex_encode(ai.keyIdentifier->data, ai.keyIdentifier->length, &id); if (id) { validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "\tauthority key id: %s\n", id); free(id); } } return 0;}static intcheck_extKeyUsage(hx509_validate_ctx ctx, struct cert_status *status, enum critical_flag cf, const Extension *e){ ExtKeyUsage eku; size_t size, i; int ret; check_Null(ctx, status, cf, e); ret = decode_ExtKeyUsage(e->extnValue.data, e->extnValue.length, &eku, &size); if (ret) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Decoding ExtKeyUsage failed: %d", ret); return 1; } if (size != e->extnValue.length) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Padding data in EKU"); free_ExtKeyUsage(&eku); return 1; } if (eku.len == 0) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "ExtKeyUsage length is 0"); return 1; } for (i = 0; i < eku.len; i++) { char *str; ret = der_print_heim_oid (&eku.val[i], '.', &str); if (ret) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "\tEKU: failed to print oid %d", i); free_ExtKeyUsage(&eku); return 1; } validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "\teku-%d: %s\n", i, str);; free(str); } free_ExtKeyUsage(&eku); return 0;}static intcheck_pkinit_san(hx509_validate_ctx ctx, heim_any *a){ KRB5PrincipalName kn; unsigned i; size_t size; int ret; ret = decode_KRB5PrincipalName(a->data, a->length, &kn, &size); if (ret) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Decoding kerberos name in SAN failed: %d", ret); return 1; } if (size != a->length) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Decoding kerberos name have extra bits on the end"); return 1; } /* print kerberos principal, add code to quote / within components */ for (i = 0; i < kn.principalName.name_string.len; i++) { validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "%s", kn.principalName.name_string.val[i]); if (i + 1 < kn.principalName.name_string.len) validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "/"); } validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "@"); validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "%s", kn.realm); free_KRB5PrincipalName(&kn); return 0;}static intcheck_utf8_string_san(hx509_validate_ctx ctx, heim_any *a){ PKIXXmppAddr jid; size_t size; int ret; ret = decode_PKIXXmppAddr(a->data, a->length, &jid, &size); if (ret) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Decoding JID in SAN failed: %d", ret); return 1; } validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "%s", jid); free_PKIXXmppAddr(&jid); return 0;}static intcheck_altnull(hx509_validate_ctx ctx, heim_any *a){ return 0;}static intcheck_CRLDistributionPoints(hx509_validate_ctx ctx, struct cert_status *status, enum critical_flag cf, const Extension *e){ CRLDistributionPoints dp; size_t size; int ret, i; check_Null(ctx, status, cf, e); ret = decode_CRLDistributionPoints(e->extnValue.data, e->extnValue.length, &dp, &size); if (ret) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Decoding CRL Distribution Points failed: %d\n", ret); return 1; } validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "CRL Distribution Points:\n"); for (i = 0 ; i < dp.len; i++) { if (dp.val[i].distributionPoint) { DistributionPointName dpname; heim_any *data = dp.val[i].distributionPoint; int j; ret = decode_DistributionPointName(data->data, data->length, &dpname, NULL); if (ret) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Failed to parse CRL Distribution Point Name: %d\n", ret); continue; } switch (dpname.element) { case choice_DistributionPointName_fullName: validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "Fullname:\n"); for (j = 0 ; j < dpname.u.fullName.len; j++) { char *s; GeneralName *name = &dpname.u.fullName.val[j];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -