36 #if defined(ENABLE_CRYPTO_MBEDTLS)
50 #include <mbedtls/debug.h>
51 #include <mbedtls/error.h>
52 #include <mbedtls/version.h>
54 #if MBEDTLS_VERSION_NUMBER >= 0x02040000
55 #include <mbedtls/net_sockets.h>
57 #include <mbedtls/net.h>
60 #include <mbedtls/oid.h>
61 #include <mbedtls/pem.h>
63 static const mbedtls_x509_crt_profile openvpn_x509_crt_profile_legacy =
66 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 )
67 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 )
68 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 )
69 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 )
70 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 )
71 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
77 static const mbedtls_x509_crt_profile openvpn_x509_crt_profile_preferred =
80 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 )
81 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 )
82 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 )
83 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
89 #define openvpn_x509_crt_profile_suiteb mbedtls_x509_crt_profile_suiteb;
112 ctx->
endpoint = MBEDTLS_SSL_IS_SERVER;
125 ctx->
endpoint = MBEDTLS_SSL_IS_CLIENT;
137 mbedtls_x509_crt_free(ctx->
ca_chain);
143 mbedtls_dhm_free(ctx->
dhm_ctx);
146 mbedtls_x509_crl_free(ctx->
crl);
149 #if defined(ENABLE_PKCS11)
153 if (ctx->pkcs11_cert)
155 pkcs11h_certificate_freeCertificate(ctx->pkcs11_cert);
176 #ifdef HAVE_EXPORT_KEYING_MATERIAL
178 #if HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB
188 mbedtls_ssl_export_keys_cb(
void *p_expkey,
const unsigned char *ms,
189 const unsigned char *kb,
size_t maclen,
190 size_t keylen,
size_t ivlen,
191 const unsigned char client_random[32],
192 const unsigned char server_random[32],
193 mbedtls_tls_prf_types tls_prf_type)
200 ==
sizeof(cache->master_secret),
"master size mismatch");
202 memcpy(cache->client_server_random, client_random, 32);
203 memcpy(cache->client_server_random + 32, server_random, 32);
204 memcpy(cache->master_secret, ms,
sizeof(cache->master_secret));
205 cache->tls_prf_type = tls_prf_type;
209 #elif HAVE_MBEDTLS_SSL_SET_EXPORT_KEYS_CB
222 mbedtls_ssl_export_keys_cb(
void *p_expkey,
223 mbedtls_ssl_key_export_type type,
224 const unsigned char *secret,
226 const unsigned char client_random[32],
227 const unsigned char server_random[32],
228 mbedtls_tls_prf_types tls_prf_type)
232 if (type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET)
243 if (secret_len !=
sizeof(cache->master_secret))
246 "ERROR: Incorrect TLS 1.2 master secret length: Got %zu, expected %zu",
247 secret_len,
sizeof(cache->master_secret));
250 memcpy(cache->client_server_random, client_random, 32);
251 memcpy(cache->client_server_random + 32, server_random, 32);
252 memcpy(cache->master_secret, secret,
sizeof(cache->master_secret));
253 cache->tls_prf_type = tls_prf_type;
259 const char *label,
size_t label_size,
260 void *ekm,
size_t ekm_size)
262 ASSERT(strlen(label) == label_size);
268 if (cache->tls_prf_type == MBEDTLS_SSL_TLS_PRF_NONE)
273 int ret = mbedtls_ssl_tls_prf(cache->tls_prf_type, cache->master_secret,
274 sizeof(cache->master_secret),
275 label, cache->client_server_random,
276 sizeof(cache->client_server_random),
292 const char *label,
size_t label_size,
293 void *ekm,
size_t ekm_size)
307 tls_translate_cipher_name(
const char *cipher_name)
317 if (0 != strcmp(cipher_name, pair->
iana_name))
335 msg(
M_WARN,
"mbed TLS does not support setting tls-ciphersuites. "
336 "Ignoring TLS 1.3 cipher list: %s", ciphers);
342 char *tmp_ciphers, *tmp_ciphers_orig, *token;
359 tmp_ciphers_orig = tmp_ciphers =
string_alloc(ciphers, NULL);
361 token = strtok(tmp_ciphers, ":");
365 tls_translate_cipher_name(token));
370 token = strtok(NULL,
":");
372 free(tmp_ciphers_orig);
378 if (!profile || 0 == strcmp(profile,
"legacy")
379 || 0 == strcmp(profile,
"insecure"))
383 else if (0 == strcmp(profile,
"preferred"))
387 else if (0 == strcmp(profile,
"suiteb"))
393 msg(
M_FATAL,
"ERROR: Invalid cert profile: %s", profile);
412 while ((token =
strsep(&tmp_groups, ":")))
414 const mbedtls_ecp_curve_info *ci =
415 mbedtls_ecp_curve_info_from_name(token);
418 msg(
M_WARN,
"Warning unknown curve/group specified: %s", token);
445 if (mbedtls_x509_time_is_future(&ctx->
crt_chain->valid_from))
447 msg(
M_WARN,
"WARNING: Your certificate is not yet valid!");
450 if (mbedtls_x509_time_is_past(&ctx->
crt_chain->valid_to))
452 msg(
M_WARN,
"WARNING: Your certificate has expired!");
463 (
const unsigned char *) dh_file,
464 strlen(dh_file) + 1)))
466 msg(
M_FATAL,
"Cannot read inline DH parameters");
473 msg(
M_FATAL,
"Cannot read DH parameters from file %s", dh_file);
485 if (NULL != curve_name)
487 msg(
M_WARN,
"WARNING: mbed TLS builds do not support specifying an "
488 "ECDH curve with --ecdh-curve, using default curves. Use "
489 "--tls-groups to specify curves.");
495 bool pkcs12_file_inline,
bool load_ca_file)
497 msg(
M_FATAL,
"PKCS #12 files not yet supported for mbed TLS.");
501 #ifdef ENABLE_CRYPTOAPI
505 msg(
M_FATAL,
"Windows CryptoAPI not yet supported for mbed TLS.");
523 (
const unsigned char *)cert_file,
524 strlen(cert_file) + 1)))
526 msg(
M_FATAL,
"Cannot load inline certificate file");
533 msg(
M_FATAL,
"Cannot load certificate file %s", cert_file);
540 bool priv_key_inline)
553 (
const unsigned char *) priv_key_file,
554 strlen(priv_key_file) + 1, NULL, 0,
555 mbedtls_ctr_drbg_random,
558 if (MBEDTLS_ERR_PK_PASSWORD_REQUIRED ==
status)
563 (
const unsigned char *) priv_key_file,
564 strlen(priv_key_file) + 1,
567 mbedtls_ctr_drbg_random,
576 mbedtls_ctr_drbg_random,
578 if (MBEDTLS_ERR_PK_PASSWORD_REQUIRED ==
status)
584 mbedtls_ctr_drbg_random,
590 #ifdef ENABLE_MANAGEMENT
596 msg(
M_WARN,
"Cannot load private key file %s",
603 mbedtls_ctr_drbg_random,
606 msg(
M_WARN,
"Private key does not match the certificate");
632 external_pkcs1_sign(
void *ctx_voidptr,
633 int (*f_rng)(
void *,
unsigned char *,
size_t),
void *p_rng,
634 #
if MBEDTLS_VERSION_NUMBER < 0x03020100
637 mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *
hash,
642 uint8_t *to_sign = NULL;
643 size_t asn_len = 0, oid_size = 0;
644 const char *oid = NULL;
648 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
651 #if MBEDTLS_VERSION_NUMBER < 0x03020100
652 if (MBEDTLS_RSA_PRIVATE != mode)
654 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
664 if (md_alg != MBEDTLS_MD_NONE)
666 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
669 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
672 if (!
mbed_ok(mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size )))
674 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
677 hashlen = mbedtls_md_get_size( md_info );
678 asn_len = 10 + oid_size;
681 if ((SIZE_MAX - hashlen) < asn_len
684 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
688 uint8_t *p = to_sign;
689 if (md_alg != MBEDTLS_MD_NONE)
700 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
701 *p++ = (
unsigned char) ( 0x08 + oid_size + hashlen );
702 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
703 *p++ = (
unsigned char) ( 0x04 + oid_size );
704 *p++ = MBEDTLS_ASN1_OID;
705 *p++ = oid_size & 0xFF;
706 memcpy( p, oid, oid_size );
708 *p++ = MBEDTLS_ASN1_NULL;
710 *p++ = MBEDTLS_ASN1_OCTET_STRING;
714 ASSERT(asn_len == p - to_sign);
718 memcpy(p,
hash, hashlen);
721 if (!ctx->
sign(ctx->
sign_ctx, to_sign, asn_len + hashlen, sig,
724 rv = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
736 external_key_len(
void *vctx)
751 msg(
M_WARN,
"ERROR: external key requires a certificate.");
755 if (mbedtls_pk_get_type(&ctx->
crt_chain->pk) != MBEDTLS_PK_RSA)
757 msg(
M_WARN,
"ERROR: external key with mbed TLS requires a "
758 "certificate with an RSA key.");
768 NULL, external_pkcs1_sign, external_key_len)))
776 #ifdef ENABLE_MANAGEMENT
779 management_sign_func(
void *
sign_ctx,
const void *src,
size_t src_len,
780 void *dst,
size_t dst_len)
783 char *src_b64 = NULL;
784 char *dst_b64 = NULL;
796 "RSA_PKCS1_PADDING")))
824 bool ca_inline,
const char *ca_path,
bool tls_server)
828 msg(
M_FATAL,
"ERROR: mbed TLS cannot handle the capath directive");
831 if (ca_file && ca_inline)
834 (
const unsigned char *) ca_file,
835 strlen(ca_file) + 1)))
837 msg(
M_FATAL,
"Cannot load inline CA certificates");
845 msg(
M_FATAL,
"Cannot load CA certificate file %s", ca_file);
852 bool extra_certs_inline)
861 if (extra_certs_inline)
864 (
const unsigned char *) extra_certs_file,
865 strlen(extra_certs_file) + 1)))
867 msg(
M_FATAL,
"Cannot load inline extra-certs file");
872 if (!
mbed_ok(mbedtls_x509_crt_parse_file(ctx->
crt_chain, extra_certs_file)))
874 msg(
M_FATAL,
"Cannot load extra-certs file: %s", extra_certs_file);
906 buf_free_entry(cur_block);
912 endless_buf_read(
endless_buffer *in,
unsigned char *out,
size_t out_len )
918 return MBEDTLS_ERR_SSL_WANT_READ;
921 while (in->
first_block != NULL && read_len < out_len)
924 if (block_len <= out_len - read_len)
927 memcpy(out + read_len, cur_entry->data + in->
data_start,
930 read_len += block_len;
940 buf_free_entry(cur_entry);
955 endless_buf_write(
endless_buffer *out,
const unsigned char *in,
size_t len )
958 if (NULL == new_block)
960 return MBEDTLS_ERR_NET_SEND_FAILED;
963 new_block->data = malloc(len);
964 if (NULL == new_block->data)
967 return MBEDTLS_ERR_NET_SEND_FAILED;
970 new_block->length = len;
971 new_block->next_block = NULL;
973 memcpy(new_block->data, in, len);
991 ssl_bio_read(
void *ctx,
unsigned char *out,
size_t out_len)
994 return endless_buf_read(&my_ctx->
in, out, out_len);
998 ssl_bio_write(
void *ctx,
const unsigned char *in,
size_t in_len)
1001 return endless_buf_write(&my_ctx->
out, in, in_len);
1005 my_debug(
void *ctx,
int level,
const char *file,
int line,
1009 msg(my_loglevel,
"mbed TLS msg (%s:%d): %s", file, line, str);
1018 static char old_sha256_hash[32] = {0};
1019 unsigned char sha256_hash[32] = {0};
1024 mbedtls_x509_crt *cert = ctx->
crt_chain;
1026 if (!
md_full(
"SHA256", cert->tbs.p, cert->tbs.len, sha256_hash))
1028 msg(
M_WARN,
"WARNING: failed to personalise random");
1031 if (0 != memcmp(old_sha256_hash, sha256_hash,
sizeof(sha256_hash)))
1035 msg(
M_WARN,
"WARNING: failed to personalise random, could not update CTR_DRBG");
1037 memcpy(old_sha256_hash, sha256_hash,
sizeof(old_sha256_hash));
1045 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1048 #error "mbedtls is compiled without support for TLS 1.2."
1060 tls_version_to_ssl_version(
int tls_ver)
1064 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1069 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1075 msg(
M_FATAL,
"%s: invalid or unsupported TLS version %d", __func__, tls_ver);
1086 if (ctx->
crl == NULL)
1090 mbedtls_x509_crl_free(ctx->
crl);
1094 if (!
mbed_ok(mbedtls_x509_crl_parse(ctx->
crl,
1095 (
const unsigned char *)crl_file,
1096 strlen(crl_file) + 1)))
1098 msg(
M_WARN,
"CRL: cannot parse inline CRL");
1104 if (!
mbed_ok(mbedtls_x509_crl_parse_file(ctx->
crl, crl_file)))
1106 msg(
M_WARN,
"CRL: cannot read CRL from file %s", crl_file);
1113 mbedtls_x509_crl_free(ctx->
crl);
1129 MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
1130 #ifdef MBEDTLS_DEBUG_C
1137 mbedtls_debug_set_threshold(3);
1141 mbedtls_debug_set_threshold(2);
1144 mbedtls_ssl_conf_dbg(ks_ssl->
ssl_config, my_debug, NULL);
1145 mbedtls_ssl_conf_rng(ks_ssl->
ssl_config, mbedtls_ctr_drbg_random,
1163 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1164 mbedtls_ssl_conf_renegotiation(ks_ssl->
ssl_config, MBEDTLS_SSL_RENEGOTIATION_DISABLED);
1171 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
1172 mbedtls_ssl_conf_cbc_record_splitting(ks_ssl->
ssl_config,
1173 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED);
1189 mbedtls_ssl_conf_authmode(ks_ssl->
ssl_config, MBEDTLS_SSL_VERIFY_OPTIONAL);
1193 mbedtls_ssl_conf_authmode(ks_ssl->
ssl_config, MBEDTLS_SSL_VERIFY_REQUIRED);
1202 const int configured_tls_version_min =
1211 version = tls_version_to_ssl_version(configured_tls_version_min);
1219 const int configured_tls_version_max =
1227 version = tls_version_to_ssl_version(configured_tls_version_max);
1238 #if HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB
1240 mbedtls_ssl_conf_export_keys_ext_cb(ks_ssl->
ssl_config,
1241 mbedtls_ssl_export_keys_cb,
session);
1246 mbedtls_ssl_init(ks_ssl->
ctx);
1249 #if HAVE_MBEDTLS_SSL_SET_EXPORT_KEYS_CB
1251 mbedtls_ssl_set_export_keys_cb(ks_ssl->
ctx, mbedtls_ssl_export_keys_cb,
session);
1256 mbedtls_ssl_set_bio(ks_ssl->
ctx, ks_ssl->
bio_ctx, ssl_bio_write,
1257 ssl_bio_read, NULL);
1264 mbedtls_ssl_send_alert_message(ks_ssl->
ctx, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1265 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY);
1277 mbedtls_ssl_free(ks_ssl->
ctx);
1330 retval = mbedtls_ssl_write(ks->
ctx, data, len);
1335 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1340 "TLS ERROR: write tls_write_plaintext_const error");
1347 "TLS ERROR: write tls_write_plaintext_const incomplete %d/%d",
1386 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1431 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1436 "TLS ERROR: write tls_write_ciphertext error");
1440 if (retval != buf->
len)
1442 msg(
D_TLS_ERRORS,
"TLS ERROR: write tls_write_ciphertext incomplete %d/%d",
1478 retval = mbedtls_ssl_read(ks->
ctx,
BPTR(buf), len);
1483 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1518 const mbedtls_x509_crt *cert;
1523 snprintf(s1,
sizeof(s1),
"%s %s, cipher %s",
1525 mbedtls_ssl_get_version(ks_ssl->
ctx),
1526 mbedtls_ssl_get_ciphersuite(ks_ssl->
ctx));
1528 cert = mbedtls_ssl_get_peer_cert(ks_ssl->
ctx);
1531 snprintf(s2,
sizeof(s2),
", %u bit key",
1532 (
unsigned int) mbedtls_pk_get_bitlen(&cert->pk));
1540 const char *tls_cert_profile,
1549 const int *ciphers = mbedtls_ssl_list_ciphersuites();
1555 if (tls_ctx.allowed_ciphers)
1557 ciphers = tls_ctx.allowed_ciphers;
1560 while (*ciphers != 0)
1562 printf(
"%s\n", mbedtls_ssl_get_ciphersuite_name(*ciphers));
1571 const mbedtls_ecp_curve_info *pcurve = mbedtls_ecp_curve_list();
1575 msg(
M_FATAL,
"Cannot retrieve curve list from mbed TLS");
1579 printf(
"Available Elliptic curves, listed in order of preference:\n\n");
1580 while (MBEDTLS_ECP_DP_NONE != pcurve->grp_id)
1582 printf(
"%s\n", pcurve->name);
1590 const char *cipher_name;
1591 const int *ciphers = mbedtls_ssl_list_ciphersuites();
1594 msg(
M_FATAL,
"Cannot retrieve list of supported SSL ciphers.");
1597 cipher_name = mbedtls_ssl_get_ciphersuite_name(*ciphers);
1604 static char mbedtls_version[30];
1605 unsigned int pv = mbedtls_version_get_number();
1606 snprintf(mbedtls_version,
sizeof(mbedtls_version),
"mbed TLS %d.%d.%d",
1607 (pv>>24)&0xff, (pv>>16)&0xff, (pv>>8)&0xff );
1608 return mbedtls_version;