36 #if defined(ENABLE_CRYPTO_MBEDTLS)
41 #include <mbedtls/asn1.h>
42 #include <mbedtls/error.h>
43 #include <mbedtls/bignum.h>
44 #include <mbedtls/oid.h>
45 #include <mbedtls/sha1.h>
47 #define MAX_SUBJECT_LENGTH 256
50 verify_callback(
void *session_obj, mbedtls_x509_crt *cert,
int cert_depth,
65 if (
session->opt->verify_hash_no_ca)
75 uint32_t flags_ignore = MBEDTLS_X509_BADCERT_NOT_TRUSTED
76 | MBEDTLS_X509_BADCERT_EXPIRED
77 | MBEDTLS_X509_BADCERT_FUTURE;
78 *flags = *flags & ~flags_ignore;
85 char errstr[512] = { 0 };
89 ret = mbedtls_x509_crt_verify_info(errstr,
sizeof(errstr)-1,
"", *flags);
90 if (ret <= 0 && !snprintf(errstr,
sizeof(errstr),
91 "Could not retrieve error string, flags=%" PRIx32, *flags))
103 cert_depth, subject, serial ? serial :
"<not available>", errstr);
108 "subject string from certificate): %s", cert_depth, errstr);
115 *flags |= MBEDTLS_X509_BADCERT_OTHER;
126 #ifdef ENABLE_X509ALTUSERNAME
127 #warning "X509 alt user name not yet supported for mbed TLS"
132 char *x509_username_field, mbedtls_x509_crt *cert)
134 mbedtls_x509_name *name;
138 name = &cert->subject;
143 if (0 == memcmp(name->oid.p, MBEDTLS_OID_AT_CN,
144 MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_CN)))
159 if (cn_len > name->val.len)
161 memcpy( cn, name->val.p, name->val.len );
162 cn[name->val.len] =
'\0';
166 memcpy( cn, name->val.p, cn_len);
178 mbedtls_mpi serial_mpi = { 0 };
181 mbedtls_mpi_init(&serial_mpi);
182 if (!
mbed_ok(mbedtls_mpi_read_binary(&serial_mpi, cert->serial.p,
185 msg(
M_WARN,
"Failed to retrieve serial from certificate.");
190 mbedtls_mpi_write_string(&serial_mpi, 10, NULL, 0, &buflen);
194 if (!
mbed_ok(mbedtls_mpi_write_string(&serial_mpi, 10, buf, buflen, &buflen)))
196 msg(
M_WARN,
"Failed to write serial to string.");
202 mbedtls_mpi_free(&serial_mpi);
210 size_t len = cert->serial.len * 3 + 1;
214 if (mbedtls_x509_serial_gets(buf,
len-1, &cert->serial) < 0)
258 x509_get_fingerprint(const mbedtls_md_info_t *md_info, mbedtls_x509_crt *cert,
261 const size_t md_size = mbedtls_md_get_size(md_info);
263 mbedtls_md(md_info, cert->raw.p, cert->raw.len,
BPTR(&fingerprint));
271 return x509_get_fingerprint(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
278 return x509_get_fingerprint(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
285 char tmp_subject[MAX_SUBJECT_LENGTH] = {0};
286 char *subject = NULL;
290 ret = mbedtls_x509_dn_gets( tmp_subject, MAX_SUBJECT_LENGTH-1, &cert->subject );
304 size_t name_expand_size;
307 msg(
D_X509_ATTR,
"X509 ATTRIBUTE name='%s' value='%s' depth=%d", name, value, depth);
308 name_expand_size = 64 + strlen(name);
309 name_expand = (
char *) malloc(name_expand_size);
311 snprintf(name_expand, name_expand_size,
"X509_%d_%s", depth, name);
317 asn1_buf_to_c_string(
const mbedtls_asn1_buf *orig,
struct gc_arena *
gc)
322 if (!(orig->tag == MBEDTLS_ASN1_UTF8_STRING
323 || orig->tag == MBEDTLS_ASN1_PRINTABLE_STRING
324 || orig->tag == MBEDTLS_ASN1_IA5_STRING))
330 for (i = 0; i < orig->len; ++i)
332 if (orig->p[i] ==
'\0')
338 memcpy(val, orig->p, orig->len);
339 val[orig->len] =
'\0';
345 const mbedtls_x509_crt *cert,
int depth,
struct gc_arena *
gc)
347 const mbedtls_x509_name *xn;
348 for (xn = &cert->subject; xn != NULL; xn = xn->next)
350 const char *xn_short_name = NULL;
351 if (0 == mbedtls_oid_get_attr_short_name(&xn->oid, &xn_short_name)
352 && 0 == strcmp(xt->
name, xn_short_name))
354 char *val_str = asn1_buf_to_c_string(&xn->val,
gc);
377 const int depth, mbedtls_x509_crt *cert)
384 if (0 == strcmp(xt->
name,
"SHA1") || 0 == strcmp(xt->
name,
"SHA256"))
390 if (0 == strcmp(xt->
name,
"SHA1"))
405 do_setenv_name(
es, xt, cert, depth, &
gc);
423 const mbedtls_x509_name *name;
426 name = &cert->subject;
430 char name_expand[64+8];
431 const char *shortname;
433 if (0 == mbedtls_oid_get_attr_short_name(&name->oid, &shortname) )
435 snprintf(name_expand,
sizeof(name_expand),
"X509_%d_%s",
436 cert_depth, shortname);
440 snprintf(name_expand,
sizeof(name_expand),
"X509_%d_\?\?",
444 for (i = 0; i < name->val.len; i++)
446 if (i >= (
int)
sizeof( s ) - 1)
452 if (c < 32 || c == 127 || ( c > 128 && c < 160 ) )
494 "ERROR: Certificate does not have key usage extension");
505 for (
size_t i = 0;
SUCCESS != fFound && i<expected_len; i++)
507 if (expected_ku[i] != 0
508 && 0 == mbedtls_x509_crt_check_key_usage(cert, expected_ku[i]))
516 msg(
D_TLS_ERRORS,
"ERROR: Certificate has invalid key usage, expected one of:");
517 for (
size_t i = 0; i < expected_len && expected_ku[i]; i++)
533 msg(
D_HANDSHAKE,
"Certificate does not have extended key usage extension");
537 mbedtls_x509_sequence *oid_seq = &(cert->ext_key_usage);
540 while (oid_seq != NULL)
542 mbedtls_x509_buf *oid = &oid_seq->buf;
543 char oid_num_str[1024];
546 if (0 == mbedtls_oid_get_extended_key_usage( oid, &oid_str ))
549 oid_str, expected_oid);
550 if (!strcmp(expected_oid, oid_str))
557 if (0 < mbedtls_oid_get_numeric_string( oid_num_str,
558 sizeof(oid_num_str), oid))
561 oid_num_str, expected_oid);
562 if (!strcmp(expected_oid, oid_num_str))
568 oid_seq = oid_seq->next;