35 #if defined(ENABLE_CRYPTO_OPENSSL)
45 #include <openssl/bn.h>
46 #include <openssl/err.h>
47 #include <openssl/x509v3.h>
58 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
63 X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
68 if (!preverify_ok && !
session->opt->verify_hash_no_ca)
76 subject =
"(Failed to retrieve certificate subject)";
80 if (X509_STORE_CTX_get_error(ctx) == X509_V_ERR_UNABLE_TO_GET_CRL)
83 X509_STORE_CTX_get_error_depth(ctx),
84 X509_verify_cert_error_string(X509_STORE_CTX_get_error(ctx)),
92 X509_STORE_CTX_get_error_depth(ctx),
93 X509_verify_cert_error_string(X509_STORE_CTX_get_error(ctx)),
94 subject, serial ? serial :
"<not available>");
115 #ifdef ENABLE_X509ALTUSERNAME
117 x509_username_field_ext_supported(
const char *fieldname)
119 int nid = OBJ_txt2nid(fieldname);
120 return nid == NID_subject_alt_name || nid == NID_issuer_alt_name;
125 extract_x509_extension(X509 *cert,
char *fieldname,
char *out,
int size)
130 if (!x509_username_field_ext_supported(fieldname))
133 "ERROR: --x509-username-field 'ext:%s' not supported", fieldname);
137 int nid = OBJ_txt2nid(fieldname);
138 GENERAL_NAMES *extensions = X509_get_ext_d2i(cert, nid, NULL, NULL);
148 numalts = sk_GENERAL_NAME_num(extensions);
151 for (i = 0; i<numalts; i++)
154 const GENERAL_NAME *name = sk_GENERAL_NAME_value(extensions, i );
159 if (ASN1_STRING_to_UTF8((
unsigned char **)&buf, name->d.ia5) < 0)
163 if (strlen(buf) != name->d.ia5->length)
178 __func__, name->type);
182 GENERAL_NAMES_free(extensions);
206 X509_NAME_ENTRY *x509ne = NULL;
207 ASN1_STRING *asn1 = NULL;
208 unsigned char *buf = NULL;
209 ASN1_OBJECT *field_name_obj = OBJ_txt2obj(field_name, 0);
211 if (field_name_obj == NULL)
222 tmp = X509_NAME_get_index_by_OBJ(x509, field_name_obj, lastpos);
225 ASN1_OBJECT_free(field_name_obj);
233 x509ne = X509_NAME_get_entry(x509, lastpos);
239 asn1 = X509_NAME_ENTRY_get_data(x509ne);
244 if (ASN1_STRING_to_UTF8(&buf, asn1) < 0)
260 char *x509_username_field, X509 *peer_cert)
262 #ifdef ENABLE_X509ALTUSERNAME
263 if (strncmp(
"ext:", x509_username_field, 4) == 0)
265 if (!extract_x509_extension(peer_cert, x509_username_field+4, common_name, cn_len))
270 else if (strcmp(LN_serialNumber, x509_username_field) == 0)
272 ASN1_INTEGER *asn1_i = X509_get_serialNumber(peer_cert);
277 if (!serial || cn_len <= strlen(serial)+2)
282 snprintf(common_name, cn_len,
"0x%s", serial);
288 x509_username_field, common_name, cn_len))
299 ASN1_INTEGER *asn1_i;
301 char *openssl_serial, *serial;
303 asn1_i = X509_get_serialNumber(cert);
304 bignum = ASN1_INTEGER_to_BN(asn1_i, NULL);
305 openssl_serial = BN_bn2dec(bignum);
310 OPENSSL_free(openssl_serial);
318 const ASN1_INTEGER *asn1_i = X509_get_serialNumber(cert);
320 return format_hex_ex(asn1_i->data, asn1_i->length, 0, 1,
":", gc);
326 BIO *out = BIO_new_file(filename,
"w");
332 if (!PEM_write_bio_X509(out, cert))
349 const EVP_MD *sha1 = EVP_sha1();
351 X509_digest(cert, EVP_sha1(),
BPTR(&
hash), NULL);
359 const EVP_MD *sha256 = EVP_sha256();
361 X509_digest(cert, EVP_sha256(),
BPTR(&
hash), NULL);
369 BIO *subject_bio = NULL;
370 BUF_MEM *subject_mem;
371 char *subject = NULL;
373 subject_bio = BIO_new(BIO_s_mem());
374 if (subject_bio == NULL)
379 X509_NAME_print_ex(subject_bio, X509_get_subject_name(cert),
380 0, XN_FLAG_SEP_CPLUS_SPC | XN_FLAG_FN_SN
381 |ASN1_STRFLGS_UTF8_CONVERT | ASN1_STRFLGS_ESC_CTRL);
383 if (BIO_eof(subject_bio))
388 BIO_get_mem_ptr(subject_bio, &subject_mem);
390 subject =
gc_malloc(subject_mem->length + 1,
false, gc);
392 memcpy(subject, subject_mem->data, subject_mem->length);
393 subject[subject_mem->length] =
'\0';
396 BIO_free(subject_bio);
434 if (xt->
nid != NID_undef)
441 msg(msglevel,
"x509_track: no such attribute '%s'",
name);
450 size_t name_expand_size;
454 name_expand_size = 64 + strlen(
name);
455 name_expand = (
char *) malloc(name_expand_size);
457 snprintf(name_expand, name_expand_size,
"X509_%d_%s", depth,
name);
466 X509_NAME *x509_name = X509_get_subject_name(x509);
467 const char nullc =
'\0';
481 if (xt->
nid == NID_sha1)
498 int i = X509_NAME_get_index_by_NID(x509_name, xt->
nid, -1);
501 X509_NAME_ENTRY *ent = X509_NAME_get_entry(x509_name, i);
504 ASN1_STRING *val = X509_NAME_ENTRY_get_data(ent);
505 unsigned char *buf = NULL;
506 if (ASN1_STRING_to_UTF8(&buf, val) >= 0)
515 i = X509_get_ext_by_NID(x509, xt->
nid, -1);
518 X509_EXTENSION *ext = X509_get_ext(x509, i);
521 BIO *bio = BIO_new(BIO_s_mem());
524 if (X509V3_EXT_print(bio, ext, 0, 0))
526 if (BIO_write(bio, &nullc, 1) == 1)
529 BIO_get_mem_data(bio, &str);
558 X509_NAME_ENTRY *ent;
560 unsigned char *buf = NULL;
562 size_t name_expand_size;
563 X509_NAME *x509 = X509_get_subject_name(peer_cert);
565 n = X509_NAME_entry_count(x509);
566 for (i = 0; i < n; ++i)
568 ent = X509_NAME_get_entry(x509, i);
573 fn = X509_NAME_ENTRY_get_object(ent);
578 val = X509_NAME_ENTRY_get_data(ent);
583 fn_nid = OBJ_obj2nid(fn);
584 if (fn_nid == NID_undef)
588 objbuf = OBJ_nid2sn(fn_nid);
593 if (ASN1_STRING_to_UTF8(&buf, val) < 0)
597 name_expand_size = 64 + strlen(objbuf);
598 name_expand = (
char *) malloc(name_expand_size);
600 snprintf(name_expand, name_expand_size,
"X509_%d_%s", cert_depth,
623 result_t result = X509_check_purpose(peer_cert, X509_PURPOSE_SSL_CLIENT, 0) ?
634 ns = X509_get_ext_d2i(peer_cert, NID_netscape_cert_type, NULL, NULL);
635 result = (ns && ns->length > 0 && (ns->data[0] & NS_SSL_CLIENT)) ?
SUCCESS :
FAILURE;
638 msg(
M_WARN,
"X509: Certificate is a client certificate yet it's purpose "
639 "cannot be verified (check may fail in the future)");
641 ASN1_BIT_STRING_free(ns);
651 result_t result = X509_check_purpose(peer_cert, X509_PURPOSE_SSL_SERVER, 0) ?
662 ns = X509_get_ext_d2i(peer_cert, NID_netscape_cert_type, NULL, NULL);
663 result = (ns && ns->length > 0 && (ns->data[0] & NS_SSL_SERVER)) ?
SUCCESS :
FAILURE;
666 msg(
M_WARN,
"X509: Certificate is a server certificate yet it's purpose "
667 "cannot be verified (check may fail in the future)");
669 ASN1_BIT_STRING_free(ns);
681 ASN1_BIT_STRING *ku = X509_get_ext_d2i(x509, NID_key_usage, NULL, NULL);
692 ASN1_BIT_STRING_free(ku);
697 for (
size_t i = 0; i < 8; i++)
699 if (ASN1_BIT_STRING_get_bit(ku, i))
708 if ((nku & 0xff) == 0)
715 for (
size_t i = 0; fFound !=
SUCCESS && i < expected_len; i++)
717 if (expected_ku[i] != 0 && (nku & expected_ku[i]) == expected_ku[i])
726 "ERROR: Certificate has key usage %04x, expected one of:", nku);
727 for (
size_t i = 0; i < expected_len && expected_ku[i]; i++)
733 ASN1_BIT_STRING_free(ku);
741 EXTENDED_KEY_USAGE *eku = NULL;
744 if ((eku = (EXTENDED_KEY_USAGE *) X509_get_ext_d2i(x509, NID_ext_key_usage,
745 NULL, NULL)) == NULL)
747 msg(
D_HANDSHAKE,
"Certificate does not have extended key usage extension");
754 for (i = 0;
SUCCESS != fFound && i < sk_ASN1_OBJECT_num(eku); i++)
756 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(eku, i);
759 if (
SUCCESS != fFound && OBJ_obj2txt(szOid,
sizeof(szOid), oid, 0) != -1)
762 szOid, expected_oid);
763 if (!strcmp(expected_oid, szOid))
768 if (
SUCCESS != fFound && OBJ_obj2txt(szOid,
sizeof(szOid), oid, 1) != -1)
771 szOid, expected_oid);
772 if (!strcmp(expected_oid, szOid))
782 sk_ASN1_OBJECT_pop_free(eku, ASN1_OBJECT_free);
796 X509_STORE *store = SSL_CTX_get_cert_store(opt->
ssl_ctx.
ctx);
802 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
803 for (
int i = 0; i < sk_X509_OBJECT_num(objs); i++)
805 X509_OBJECT *obj = sk_X509_OBJECT_value(objs, i);
807 if (X509_OBJECT_get_type(obj) == X509_LU_CRL)