📄 print.c
字号:
ret = hx509_general_name_unparse(name, &s); if (ret == 0 && s != NULL) { validate_print(ctx, HX509_VALIDATE_F_VERBOSE, " %s\n", s); free(s); } } break; case choice_DistributionPointName_nameRelativeToCRLIssuer: validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "Unknown nameRelativeToCRLIssuer"); break; default: validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Unknown DistributionPointName"); break; } free_DistributionPointName(&dpname); } } free_CRLDistributionPoints(&dp); status->haveCRLDP = 1; return 0;}struct { const char *name; const heim_oid *(*oid)(void); int (*func)(hx509_validate_ctx, heim_any *);} check_altname[] = { { "pk-init", oid_id_pkinit_san, check_pkinit_san }, { "jabber", oid_id_pkix_on_xmppAddr, check_utf8_string_san }, { "dns-srv", oid_id_pkix_on_dnsSRV, check_altnull }, { "card-id", oid_id_uspkicommon_card_id, check_altnull }, { "Microsoft NT-PRINCIPAL-NAME", oid_id_pkinit_ms_san, check_utf8_string_san }};static intcheck_altName(hx509_validate_ctx ctx, struct cert_status *status, const char *name, enum critical_flag cf, const Extension *e){ GeneralNames gn; size_t size; int ret, i; check_Null(ctx, status, cf, e); if (e->extnValue.length == 0) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "%sAltName empty, not allowed", name); return 1; } ret = decode_GeneralNames(e->extnValue.data, e->extnValue.length, &gn, &size); if (ret) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "\tret = %d while decoding %s GeneralNames\n", ret, name); return 1; } if (gn.len == 0) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "%sAltName generalName empty, not allowed\n", name); return 1; } for (i = 0; i < gn.len; i++) { switch (gn.val[i].element) { case choice_GeneralName_otherName: { unsigned j; validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "%sAltName otherName ", name); for (j = 0; j < sizeof(check_altname)/sizeof(check_altname[0]); j++) { if (der_heim_oid_cmp((*check_altname[j].oid)(), &gn.val[i].u.otherName.type_id) != 0) continue; validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "%s: ", check_altname[j].name); (*check_altname[j].func)(ctx, &gn.val[i].u.otherName.value); break; } if (j == sizeof(check_altname)/sizeof(check_altname[0])) { hx509_oid_print(&gn.val[i].u.otherName.type_id, validate_vprint, ctx); validate_print(ctx, HX509_VALIDATE_F_VERBOSE, " unknown"); } validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "\n"); break; } default: { char *s; ret = hx509_general_name_unparse(&gn.val[i], &s); if (ret) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "ret = %d unparsing GeneralName\n", ret); return 1; } validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "%s\n", s); free(s); break; } } } free_GeneralNames(&gn); return 0;}static intcheck_subjectAltName(hx509_validate_ctx ctx, struct cert_status *status, enum critical_flag cf, const Extension *e){ status->haveSAN = 1; return check_altName(ctx, status, "subject", cf, e);}static intcheck_issuerAltName(hx509_validate_ctx ctx, struct cert_status *status, enum critical_flag cf, const Extension *e){ status->haveIAN = 1; return check_altName(ctx, status, "issuer", cf, e);}static intcheck_basicConstraints(hx509_validate_ctx ctx, struct cert_status *status, enum critical_flag cf, const Extension *e){ BasicConstraints b; size_t size; int ret; check_Null(ctx, status, cf, e); ret = decode_BasicConstraints(e->extnValue.data, e->extnValue.length, &b, &size); if (ret) { printf("\tret = %d while decoding BasicConstraints\n", ret); return 0; } if (size != e->extnValue.length) printf("\tlength of der data isn't same as extension\n"); validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "\tis %sa CA\n", b.cA && *b.cA ? "" : "NOT "); if (b.pathLenConstraint) validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "\tpathLenConstraint: %d\n", *b.pathLenConstraint); if (b.cA) { if (*b.cA) { if (!e->critical) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Is a CA and not BasicConstraints CRITICAL\n"); status->isca = 1; } else validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "cA is FALSE, not allowed to be\n"); } free_BasicConstraints(&b); return 0;}static intcheck_proxyCertInfo(hx509_validate_ctx ctx, struct cert_status *status, enum critical_flag cf, const Extension *e){ check_Null(ctx, status, cf, e); status->isproxy = 1; return 0;}static intcheck_authorityInfoAccess(hx509_validate_ctx ctx, struct cert_status *status, enum critical_flag cf, const Extension *e){ AuthorityInfoAccessSyntax aia; size_t size; int ret, i; check_Null(ctx, status, cf, e); ret = decode_AuthorityInfoAccessSyntax(e->extnValue.data, e->extnValue.length, &aia, &size); if (ret) { printf("\tret = %d while decoding AuthorityInfoAccessSyntax\n", ret); return 0; } for (i = 0; i < aia.len; i++) { char *str; validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "\ttype: "); hx509_oid_print(&aia.val[i].accessMethod, validate_vprint, ctx); hx509_general_name_unparse(&aia.val[i].accessLocation, &str); validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "\n\tdirname: %s\n", str); free(str); } free_AuthorityInfoAccessSyntax(&aia); return 0;}/* * */struct { const char *name; const heim_oid *(*oid)(void); int (*func)(hx509_validate_ctx ctx, struct cert_status *status, enum critical_flag cf, const Extension *); enum critical_flag cf;} check_extension[] = {#define ext(name, checkname) #name, &oid_id_x509_ce_##name, check_##checkname { ext(subjectDirectoryAttributes, Null), M_N_C }, { ext(subjectKeyIdentifier, subjectKeyIdentifier), M_N_C }, { ext(keyUsage, Null), S_C }, { ext(subjectAltName, subjectAltName), M_N_C }, { ext(issuerAltName, issuerAltName), S_N_C }, { ext(basicConstraints, basicConstraints), D_C }, { ext(cRLNumber, Null), M_N_C }, { ext(cRLReason, Null), M_N_C }, { ext(holdInstructionCode, Null), M_N_C }, { ext(invalidityDate, Null), M_N_C }, { ext(deltaCRLIndicator, Null), M_C }, { ext(issuingDistributionPoint, Null), M_C }, { ext(certificateIssuer, Null), M_C }, { ext(nameConstraints, Null), M_C }, { ext(cRLDistributionPoints, CRLDistributionPoints), S_N_C }, { ext(certificatePolicies, Null) }, { ext(policyMappings, Null), M_N_C }, { ext(authorityKeyIdentifier, authorityKeyIdentifier), M_N_C }, { ext(policyConstraints, Null), D_C }, { ext(extKeyUsage, extKeyUsage), D_C }, { ext(freshestCRL, Null), M_N_C }, { ext(inhibitAnyPolicy, Null), M_C },#undef ext#define ext(name, checkname) #name, &oid_id_pkix_pe_##name, check_##checkname { ext(proxyCertInfo, proxyCertInfo), M_C }, { ext(authorityInfoAccess, authorityInfoAccess), M_C },#undef ext { "US Fed PKI - PIV Interim", oid_id_uspkicommon_piv_interim, check_Null, D_C }, { "Netscape cert comment", oid_id_netscape_cert_comment, check_Null, D_C }, { NULL }};/** * Allocate a hx509 validation/printing context. * * @param context A hx509 context. * @param ctx a new allocated hx509 validation context, free with * hx509_validate_ctx_free(). * @return An hx509 error code, see hx509_get_error_string(). * * @ingroup hx509_print */inthx509_validate_ctx_init(hx509_context context, hx509_validate_ctx *ctx){ *ctx = malloc(sizeof(**ctx)); if (*ctx == NULL) return ENOMEM; memset(*ctx, 0, sizeof(**ctx)); return 0;}/** * Set the printing functions for the validation context. * * @param ctx a hx509 valication context. * @param func the printing function to usea. * @param c the context variable to the printing function. * * @return An hx509 error code, see hx509_get_error_string(). * * @ingroup hx509_print */voidhx509_validate_ctx_set_print(hx509_validate_ctx ctx, hx509_vprint_func func, void *c){ ctx->vprint_func = func; ctx->ctx = c;}/** * Add flags to control the behaivor of the hx509_validate_cert() * function. * * @param ctx A hx509 validation context. * @param flags flags to add to the validation context. * * @return An hx509 error code, see hx509_get_error_string(). * * @ingroup hx509_print */voidhx509_validate_ctx_add_flags(hx509_validate_ctx ctx, int flags){ ctx->flags |= flags;}/** * Free an hx509 validate context. * * @param ctx the hx509 validate context to free. * * @ingroup hx509_print */voidhx509_validate_ctx_free(hx509_validate_ctx ctx){ free(ctx);}/** * Validate/Print the status of the certificate. * * @param context A hx509 context. * @param ctx A hx509 validation context. * @param cert the cerificate to validate/print. * @return An hx509 error code, see hx509_get_error_string(). * * @ingroup hx509_print */inthx509_validate_cert(hx509_context context, hx509_validate_ctx ctx, hx509_cert cert){ Certificate *c = _hx509_get_cert(cert); TBSCertificate *t = &c->tbsCertificate; hx509_name issuer, subject; char *str; struct cert_status status; int ret; memset(&status, 0, sizeof(status)); if (_hx509_cert_get_version(c) != 3) validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "Not version 3 certificate\n"); if ((t->version == NULL || *t->version < 2) && t->extensions) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Not version 3 certificate with extensions\n"); if (_hx509_cert_get_version(c) >= 3 && t->extensions == NULL) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Version 3 certificate without extensions\n"); ret = hx509_cert_get_subject(cert, &subject); if (ret) abort(); hx509_name_to_string(subject, &str); validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "subject name: %s\n", str); free(str); ret = hx509_cert_get_issuer(cert, &issuer); if (ret) abort(); hx509_name_to_string(issuer, &str); validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "issuer name: %s\n", str); free(str); if (hx509_name_cmp(subject, issuer) == 0) { status.selfsigned = 1; validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "\tis a self-signed certificate\n"); } validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "Validity:\n"); Time2string(&t->validity.notBefore, &str); validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "\tnotBefore %s\n", str); free(str); Time2string(&t->validity.notAfter, &str); validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "\tnotAfter %s\n", str); free(str); if (t->extensions) { int i, j; if (t->extensions->len == 0) { validate_print(ctx, HX509_VALIDATE_F_VALIDATE|HX509_VALIDATE_F_VERBOSE, "The empty extensions list is not " "allowed by PKIX\n"); } for (i = 0; i < t->extensions->len; i++) { for (j = 0; check_extension[j].name; j++) if (der_heim_oid_cmp((*check_extension[j].oid)(), &t->extensions->val[i].extnID) == 0) break; if (check_extension[j].name == NULL) { int flags = HX509_VALIDATE_F_VERBOSE; if (t->extensions->val[i].critical) flags |= HX509_VALIDATE_F_VALIDATE; validate_print(ctx, flags, "don't know what "); if (t->extensions->val[i].critical) validate_print(ctx, flags, "and is CRITICAL "); if (ctx->flags & flags) hx509_oid_print(&t->extensions->val[i].extnID, validate_vprint, ctx); validate_print(ctx, flags, " is\n"); continue; } validate_print(ctx, HX509_VALIDATE_F_VALIDATE|HX509_VALIDATE_F_VERBOSE, "checking extention: %s\n", check_extension[j].name); (*check_extension[j].func)(ctx, &status, check_extension[j].cf, &t->extensions->val[i]); } } else validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "no extentions\n"); if (status.isca) { if (!status.haveSKI) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "CA certificate have no SubjectKeyIdentifier\n"); } else { if (!status.haveAKI) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Is not CA and doesn't have " "AuthorityKeyIdentifier\n"); } if (!status.haveSKI) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Doesn't have SubjectKeyIdentifier\n"); if (status.isproxy && status.isca) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Proxy and CA at the same time!\n"); if (status.isproxy) { if (status.haveSAN) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Proxy and have SAN\n"); if (status.haveIAN) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Proxy and have IAN\n"); } if (hx509_name_is_null_p(subject) && !status.haveSAN) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "NULL subject DN and doesn't have a SAN\n"); if (!status.selfsigned && !status.haveCRLDP) validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Not a CA nor PROXY and doesn't have" "CRL Dist Point\n"); if (status.selfsigned) { ret = _hx509_verify_signature_bitstring(context, c, &c->signatureAlgorithm, &c->tbsCertificate._save, &c->signatureValue); if (ret == 0) validate_print(ctx, HX509_VALIDATE_F_VERBOSE, "Self-signed certificate was self-signed\n"); else validate_print(ctx, HX509_VALIDATE_F_VALIDATE, "Self-signed certificate NOT really self-signed!\n"); } hx509_name_free(&subject); hx509_name_free(&issuer); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -