35 #if defined(ENABLE_CRYPTO_MBEDTLS)
40 #include <mbedtls/asn1.h>
41 #include <mbedtls/error.h>
42 #include <mbedtls/bignum.h>
43 #include <mbedtls/oid.h>
44 #include <mbedtls/sha1.h>
46 #define MAX_SUBJECT_LENGTH 256
49 verify_callback(
void *session_obj, mbedtls_x509_crt *cert,
int cert_depth,
64 if (
session->opt->verify_hash_no_ca)
74 uint32_t flags_ignore = MBEDTLS_X509_BADCERT_NOT_TRUSTED
75 | MBEDTLS_X509_BADCERT_EXPIRED
76 | MBEDTLS_X509_BADCERT_FUTURE;
77 *flags = *flags & ~flags_ignore;
84 char errstr[512] = { 0 };
88 ret = mbedtls_x509_crt_verify_info(errstr,
sizeof(errstr)-1,
"", *flags);
89 if (ret <= 0 && !snprintf(errstr,
sizeof(errstr),
90 "Could not retrieve error string, flags=%" PRIx32, *flags))
102 cert_depth, subject, serial ? serial :
"<not available>", errstr);
107 "subject string from certificate): %s", cert_depth, errstr);
114 *flags |= MBEDTLS_X509_BADCERT_OTHER;
125 #ifdef ENABLE_X509ALTUSERNAME
126 #warning "X509 alt user name not yet supported for mbed TLS"
131 char *x509_username_field, mbedtls_x509_crt *cert)
133 mbedtls_x509_name *name;
137 name = &cert->subject;
142 if (0 == memcmp(name->oid.p, MBEDTLS_OID_AT_CN,
143 MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_CN)))
158 if (cn_len > name->val.len)
160 memcpy( cn, name->val.p, name->val.len );
161 cn[name->val.len] =
'\0';
165 memcpy( cn, name->val.p, cn_len);
177 mbedtls_mpi serial_mpi = { 0 };
180 mbedtls_mpi_init(&serial_mpi);
181 if (!
mbed_ok(mbedtls_mpi_read_binary(&serial_mpi, cert->serial.p,
184 msg(
M_WARN,
"Failed to retrieve serial from certificate.");
189 mbedtls_mpi_write_string(&serial_mpi, 10, NULL, 0, &buflen);
193 if (!
mbed_ok(mbedtls_mpi_write_string(&serial_mpi, 10, buf, buflen, &buflen)))
195 msg(
M_WARN,
"Failed to write serial to string.");
201 mbedtls_mpi_free(&serial_mpi);
209 size_t len = cert->serial.len * 3 + 1;
213 if (mbedtls_x509_serial_gets(buf,
len-1, &cert->serial) < 0)
257 x509_get_fingerprint(const mbedtls_md_info_t *md_info, mbedtls_x509_crt *cert,
260 const size_t md_size = mbedtls_md_get_size(md_info);
262 mbedtls_md(md_info, cert->raw.p, cert->raw.len,
BPTR(&fingerprint));
270 return x509_get_fingerprint(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
277 return x509_get_fingerprint(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
284 char tmp_subject[MAX_SUBJECT_LENGTH] = {0};
285 char *subject = NULL;
289 ret = mbedtls_x509_dn_gets( tmp_subject, MAX_SUBJECT_LENGTH-1, &cert->subject );
303 size_t name_expand_size;
306 msg(
D_X509_ATTR,
"X509 ATTRIBUTE name='%s' value='%s' depth=%d", name, value, depth);
307 name_expand_size = 64 + strlen(name);
308 name_expand = (
char *) malloc(name_expand_size);
310 snprintf(name_expand, name_expand_size,
"X509_%d_%s", depth, name);
316 asn1_buf_to_c_string(
const mbedtls_asn1_buf *orig,
struct gc_arena *gc)
321 if (!(orig->tag == MBEDTLS_ASN1_UTF8_STRING
322 || orig->tag == MBEDTLS_ASN1_PRINTABLE_STRING
323 || orig->tag == MBEDTLS_ASN1_IA5_STRING))
326 return string_alloc(
"ERROR: unsupported ASN.1 string type", gc);
329 for (i = 0; i < orig->len; ++i)
331 if (orig->p[i] ==
'\0')
337 memcpy(val, orig->p, orig->len);
338 val[orig->len] =
'\0';
344 const mbedtls_x509_crt *cert,
int depth,
struct gc_arena *gc)
346 const mbedtls_x509_name *xn;
347 for (xn = &cert->subject; xn != NULL; xn = xn->next)
349 const char *xn_short_name = NULL;
350 if (0 == mbedtls_oid_get_attr_short_name(&xn->oid, &xn_short_name)
351 && 0 == strcmp(xt->
name, xn_short_name))
353 char *val_str = asn1_buf_to_c_string(&xn->val, gc);
376 const int depth, mbedtls_x509_crt *cert)
383 if (0 == strcmp(xt->
name,
"SHA1") || 0 == strcmp(xt->
name,
"SHA256"))
389 if (0 == strcmp(xt->
name,
"SHA1"))
404 do_setenv_name(
es, xt, cert, depth, &gc);
422 const mbedtls_x509_name *name;
425 name = &cert->subject;
429 char name_expand[64+8];
430 const char *shortname;
432 if (0 == mbedtls_oid_get_attr_short_name(&name->oid, &shortname) )
434 snprintf(name_expand,
sizeof(name_expand),
"X509_%d_%s",
435 cert_depth, shortname);
439 snprintf(name_expand,
sizeof(name_expand),
"X509_%d_\?\?",
443 for (i = 0; i < name->val.len; i++)
445 if (i >= (
int)
sizeof( s ) - 1)
451 if (c < 32 || c == 127 || ( c > 128 && c < 160 ) )
493 "ERROR: Certificate does not have key usage extension");
504 for (
size_t i = 0;
SUCCESS != fFound && i<expected_len; i++)
506 if (expected_ku[i] != 0
507 && 0 == mbedtls_x509_crt_check_key_usage(cert, expected_ku[i]))
515 msg(
D_TLS_ERRORS,
"ERROR: Certificate has invalid key usage, expected one of:");
516 for (
size_t i = 0; i < expected_len && expected_ku[i]; i++)
532 msg(
D_HANDSHAKE,
"Certificate does not have extended key usage extension");
536 mbedtls_x509_sequence *oid_seq = &(cert->ext_key_usage);
539 while (oid_seq != NULL)
541 mbedtls_x509_buf *oid = &oid_seq->buf;
542 char oid_num_str[1024];
545 if (0 == mbedtls_oid_get_extended_key_usage( oid, &oid_str ))
548 oid_str, expected_oid);
549 if (!strcmp(expected_oid, oid_str))
556 if (0 < mbedtls_oid_get_numeric_string( oid_num_str,
557 sizeof(oid_num_str), oid))
560 oid_num_str, expected_oid);
561 if (!strcmp(expected_oid, oid_num_str))
567 oid_seq = oid_seq->next;