37 #if defined(ENABLE_CRYPTO_MBEDTLS)
51 #include <mbedtls/debug.h>
52 #include <mbedtls/error.h>
53 #include <mbedtls/version.h>
55 #if MBEDTLS_VERSION_NUMBER >= 0x02040000
56 #include <mbedtls/net_sockets.h>
58 #include <mbedtls/net.h>
61 #include <mbedtls/oid.h>
62 #include <mbedtls/pem.h>
64 static const mbedtls_x509_crt_profile openvpn_x509_crt_profile_legacy =
67 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 )
68 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 )
69 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 )
70 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 )
71 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 )
72 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
78 static const mbedtls_x509_crt_profile openvpn_x509_crt_profile_preferred =
81 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 )
82 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 )
83 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 )
84 |MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
90 #define openvpn_x509_crt_profile_suiteb mbedtls_x509_crt_profile_suiteb;
113 ctx->
endpoint = MBEDTLS_SSL_IS_SERVER;
126 ctx->
endpoint = MBEDTLS_SSL_IS_CLIENT;
138 mbedtls_x509_crt_free(ctx->
ca_chain);
144 mbedtls_dhm_free(ctx->
dhm_ctx);
147 mbedtls_x509_crl_free(ctx->
crl);
150 #if defined(ENABLE_PKCS11)
154 if (ctx->pkcs11_cert)
156 pkcs11h_certificate_freeCertificate(ctx->pkcs11_cert);
177 #ifdef HAVE_EXPORT_KEYING_MATERIAL
179 #if HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB
189 mbedtls_ssl_export_keys_cb(
void *p_expkey,
const unsigned char *ms,
190 const unsigned char *kb,
size_t maclen,
191 size_t keylen,
size_t ivlen,
192 const unsigned char client_random[32],
193 const unsigned char server_random[32],
194 mbedtls_tls_prf_types tls_prf_type)
201 ==
sizeof(cache->master_secret),
"master size mismatch");
203 memcpy(cache->client_server_random, client_random, 32);
204 memcpy(cache->client_server_random + 32, server_random, 32);
205 memcpy(cache->master_secret, ms,
sizeof(cache->master_secret));
206 cache->tls_prf_type = tls_prf_type;
210 #elif HAVE_MBEDTLS_SSL_SET_EXPORT_KEYS_CB
223 mbedtls_ssl_export_keys_cb(
void *p_expkey,
224 mbedtls_ssl_key_export_type type,
225 const unsigned char *secret,
227 const unsigned char client_random[32],
228 const unsigned char server_random[32],
229 mbedtls_tls_prf_types tls_prf_type)
233 if (type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET)
244 if (secret_len !=
sizeof(cache->master_secret))
247 "ERROR: Incorrect TLS 1.2 master secret length: Got %zu, expected %zu",
248 secret_len,
sizeof(cache->master_secret));
251 memcpy(cache->client_server_random, client_random, 32);
252 memcpy(cache->client_server_random + 32, server_random, 32);
253 memcpy(cache->master_secret, secret,
sizeof(cache->master_secret));
254 cache->tls_prf_type = tls_prf_type;
260 const char *label,
size_t label_size,
261 void *ekm,
size_t ekm_size)
263 ASSERT(strlen(label) == label_size);
269 if (cache->tls_prf_type == MBEDTLS_SSL_TLS_PRF_NONE)
274 int ret = mbedtls_ssl_tls_prf(cache->tls_prf_type, cache->master_secret,
275 sizeof(cache->master_secret),
276 label, cache->client_server_random,
277 sizeof(cache->client_server_random),
293 const char *label,
size_t label_size,
294 void *ekm,
size_t ekm_size)
308 tls_translate_cipher_name(
const char *cipher_name)
318 if (0 != strcmp(cipher_name, pair->
iana_name))
336 msg(
M_WARN,
"mbed TLS does not support setting tls-ciphersuites. "
337 "Ignoring TLS 1.3 cipher list: %s", ciphers);
343 char *tmp_ciphers, *tmp_ciphers_orig, *token;
360 tmp_ciphers_orig = tmp_ciphers =
string_alloc(ciphers, NULL);
362 token = strtok(tmp_ciphers, ":");
366 tls_translate_cipher_name(token));
371 token = strtok(NULL,
":");
373 free(tmp_ciphers_orig);
379 if (!profile || 0 == strcmp(profile,
"legacy")
380 || 0 == strcmp(profile,
"insecure"))
384 else if (0 == strcmp(profile,
"preferred"))
388 else if (0 == strcmp(profile,
"suiteb"))
394 msg(
M_FATAL,
"ERROR: Invalid cert profile: %s", profile);
413 while ((token =
strsep(&tmp_groups, ":")))
415 const mbedtls_ecp_curve_info *ci =
416 mbedtls_ecp_curve_info_from_name(token);
419 msg(
M_WARN,
"Warning unknown curve/group specified: %s", token);
446 if (mbedtls_x509_time_is_future(&ctx->
crt_chain->valid_from))
448 msg(
M_WARN,
"WARNING: Your certificate is not yet valid!");
451 if (mbedtls_x509_time_is_past(&ctx->
crt_chain->valid_to))
453 msg(
M_WARN,
"WARNING: Your certificate has expired!");
464 (
const unsigned char *) dh_file,
465 strlen(dh_file) + 1)))
467 msg(
M_FATAL,
"Cannot read inline DH parameters");
474 msg(
M_FATAL,
"Cannot read DH parameters from file %s", dh_file);
486 if (NULL != curve_name)
488 msg(
M_WARN,
"WARNING: mbed TLS builds do not support specifying an "
489 "ECDH curve with --ecdh-curve, using default curves. Use "
490 "--tls-groups to specify curves.");
496 bool pkcs12_file_inline,
bool load_ca_file)
498 msg(
M_FATAL,
"PKCS #12 files not yet supported for mbed TLS.");
502 #ifdef ENABLE_CRYPTOAPI
506 msg(
M_FATAL,
"Windows CryptoAPI not yet supported for mbed TLS.");
524 (
const unsigned char *)cert_file,
525 strlen(cert_file) + 1)))
527 msg(
M_FATAL,
"Cannot load inline certificate file");
534 msg(
M_FATAL,
"Cannot load certificate file %s", cert_file);
541 bool priv_key_inline)
554 (
const unsigned char *) priv_key_file,
555 strlen(priv_key_file) + 1, NULL, 0,
556 mbedtls_ctr_drbg_random,
559 if (MBEDTLS_ERR_PK_PASSWORD_REQUIRED ==
status)
564 (
const unsigned char *) priv_key_file,
565 strlen(priv_key_file) + 1,
568 mbedtls_ctr_drbg_random,
577 mbedtls_ctr_drbg_random,
579 if (MBEDTLS_ERR_PK_PASSWORD_REQUIRED ==
status)
585 mbedtls_ctr_drbg_random,
591 #ifdef ENABLE_MANAGEMENT
597 msg(
M_WARN,
"Cannot load private key file %s",
604 mbedtls_ctr_drbg_random,
607 msg(
M_WARN,
"Private key does not match the certificate");
633 external_pkcs1_sign(
void *ctx_voidptr,
634 int (*f_rng)(
void *,
unsigned char *,
size_t),
void *p_rng,
635 #
if MBEDTLS_VERSION_NUMBER < 0x03020100
638 mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *
hash,
643 uint8_t *to_sign = NULL;
644 size_t asn_len = 0, oid_size = 0;
645 const char *oid = NULL;
649 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
652 #if MBEDTLS_VERSION_NUMBER < 0x03020100
653 if (MBEDTLS_RSA_PRIVATE != mode)
655 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
665 if (md_alg != MBEDTLS_MD_NONE)
667 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
670 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
673 if (!
mbed_ok(mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size )))
675 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
678 hashlen = mbedtls_md_get_size( md_info );
679 asn_len = 10 + oid_size;
682 if ((SIZE_MAX - hashlen) < asn_len
685 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
689 uint8_t *p = to_sign;
690 if (md_alg != MBEDTLS_MD_NONE)
701 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
702 *p++ = (
unsigned char) ( 0x08 + oid_size + hashlen );
703 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
704 *p++ = (
unsigned char) ( 0x04 + oid_size );
705 *p++ = MBEDTLS_ASN1_OID;
706 *p++ = oid_size & 0xFF;
707 memcpy( p, oid, oid_size );
709 *p++ = MBEDTLS_ASN1_NULL;
711 *p++ = MBEDTLS_ASN1_OCTET_STRING;
715 ASSERT(asn_len == p - to_sign);
719 memcpy(p,
hash, hashlen);
722 if (!ctx->
sign(ctx->
sign_ctx, to_sign, asn_len + hashlen, sig,
725 rv = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
737 external_key_len(
void *vctx)
752 msg(
M_WARN,
"ERROR: external key requires a certificate.");
756 if (mbedtls_pk_get_type(&ctx->
crt_chain->pk) != MBEDTLS_PK_RSA)
758 msg(
M_WARN,
"ERROR: external key with mbed TLS requires a "
759 "certificate with an RSA key.");
769 NULL, external_pkcs1_sign, external_key_len)))
777 #ifdef ENABLE_MANAGEMENT
780 management_sign_func(
void *
sign_ctx,
const void *src,
size_t src_len,
781 void *dst,
size_t dst_len)
784 char *src_b64 = NULL;
785 char *dst_b64 = NULL;
797 "RSA_PKCS1_PADDING")))
825 bool ca_inline,
const char *ca_path,
bool tls_server)
829 msg(
M_FATAL,
"ERROR: mbed TLS cannot handle the capath directive");
832 if (ca_file && ca_inline)
835 (
const unsigned char *) ca_file,
836 strlen(ca_file) + 1)))
838 msg(
M_FATAL,
"Cannot load inline CA certificates");
846 msg(
M_FATAL,
"Cannot load CA certificate file %s", ca_file);
853 bool extra_certs_inline)
862 if (extra_certs_inline)
865 (
const unsigned char *) extra_certs_file,
866 strlen(extra_certs_file) + 1)))
868 msg(
M_FATAL,
"Cannot load inline extra-certs file");
873 if (!
mbed_ok(mbedtls_x509_crt_parse_file(ctx->
crt_chain, extra_certs_file)))
875 msg(
M_FATAL,
"Cannot load extra-certs file: %s", extra_certs_file);
907 buf_free_entry(cur_block);
913 endless_buf_read(
endless_buffer *in,
unsigned char *out,
size_t out_len )
919 return MBEDTLS_ERR_SSL_WANT_READ;
922 while (in->
first_block != NULL && read_len < out_len)
925 if (block_len <= out_len - read_len)
928 memcpy(out + read_len, cur_entry->data + in->
data_start,
931 read_len += block_len;
941 buf_free_entry(cur_entry);
956 endless_buf_write(
endless_buffer *out,
const unsigned char *in,
size_t len )
959 if (NULL == new_block)
961 return MBEDTLS_ERR_NET_SEND_FAILED;
964 new_block->data = malloc(len);
965 if (NULL == new_block->data)
968 return MBEDTLS_ERR_NET_SEND_FAILED;
971 new_block->length = len;
972 new_block->next_block = NULL;
974 memcpy(new_block->data, in, len);
992 ssl_bio_read(
void *ctx,
unsigned char *out,
size_t out_len)
995 return endless_buf_read(&my_ctx->
in, out, out_len);
999 ssl_bio_write(
void *ctx,
const unsigned char *in,
size_t in_len)
1002 return endless_buf_write(&my_ctx->
out, in, in_len);
1006 my_debug(
void *ctx,
int level,
const char *file,
int line,
1010 msg(my_loglevel,
"mbed TLS msg (%s:%d): %s", file, line, str);
1019 static char old_sha256_hash[32] = {0};
1020 unsigned char sha256_hash[32] = {0};
1025 mbedtls_x509_crt *cert = ctx->
crt_chain;
1027 if (!
md_full(
"SHA256", cert->tbs.p, cert->tbs.len, sha256_hash))
1029 msg(
M_WARN,
"WARNING: failed to personalise random");
1032 if (0 != memcmp(old_sha256_hash, sha256_hash,
sizeof(sha256_hash)))
1036 msg(
M_WARN,
"WARNING: failed to personalise random, could not update CTR_DRBG");
1038 memcpy(old_sha256_hash, sha256_hash,
sizeof(old_sha256_hash));
1046 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1049 #error "mbedtls is compiled without support for TLS 1.2."
1061 tls_version_to_ssl_version(
int tls_ver)
1065 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1070 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1076 msg(
M_FATAL,
"%s: invalid or unsupported TLS version %d", __func__, tls_ver);
1087 if (ctx->
crl == NULL)
1091 mbedtls_x509_crl_free(ctx->
crl);
1095 if (!
mbed_ok(mbedtls_x509_crl_parse(ctx->
crl,
1096 (
const unsigned char *)crl_file,
1097 strlen(crl_file) + 1)))
1099 msg(
M_WARN,
"CRL: cannot parse inline CRL");
1105 if (!
mbed_ok(mbedtls_x509_crl_parse_file(ctx->
crl, crl_file)))
1107 msg(
M_WARN,
"CRL: cannot read CRL from file %s", crl_file);
1114 mbedtls_x509_crl_free(ctx->
crl);
1130 MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
1131 #ifdef MBEDTLS_DEBUG_C
1138 mbedtls_debug_set_threshold(3);
1142 mbedtls_debug_set_threshold(2);
1145 mbedtls_ssl_conf_dbg(ks_ssl->
ssl_config, my_debug, NULL);
1146 mbedtls_ssl_conf_rng(ks_ssl->
ssl_config, mbedtls_ctr_drbg_random,
1164 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1165 mbedtls_ssl_conf_renegotiation(ks_ssl->
ssl_config, MBEDTLS_SSL_RENEGOTIATION_DISABLED);
1172 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
1173 mbedtls_ssl_conf_cbc_record_splitting(ks_ssl->
ssl_config,
1174 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED);
1190 mbedtls_ssl_conf_authmode(ks_ssl->
ssl_config, MBEDTLS_SSL_VERIFY_OPTIONAL);
1194 mbedtls_ssl_conf_authmode(ks_ssl->
ssl_config, MBEDTLS_SSL_VERIFY_REQUIRED);
1203 const int configured_tls_version_min =
1212 version = tls_version_to_ssl_version(configured_tls_version_min);
1220 const int configured_tls_version_max =
1228 version = tls_version_to_ssl_version(configured_tls_version_max);
1239 #if HAVE_MBEDTLS_SSL_CONF_EXPORT_KEYS_EXT_CB
1241 mbedtls_ssl_conf_export_keys_ext_cb(ks_ssl->
ssl_config,
1242 mbedtls_ssl_export_keys_cb,
session);
1247 mbedtls_ssl_init(ks_ssl->
ctx);
1254 #if HAVE_MBEDTLS_SSL_SET_EXPORT_KEYS_CB
1256 mbedtls_ssl_set_export_keys_cb(ks_ssl->
ctx, mbedtls_ssl_export_keys_cb,
session);
1261 mbedtls_ssl_set_bio(ks_ssl->
ctx, ks_ssl->
bio_ctx, ssl_bio_write,
1262 ssl_bio_read, NULL);
1269 mbedtls_ssl_send_alert_message(ks_ssl->
ctx, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1270 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY);
1282 mbedtls_ssl_free(ks_ssl->
ctx);
1335 retval = mbedtls_ssl_write(ks->
ctx, data, len);
1340 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1345 "TLS ERROR: write tls_write_plaintext_const error");
1352 "TLS ERROR: write tls_write_plaintext_const incomplete %d/%d",
1391 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1436 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1441 "TLS ERROR: write tls_write_ciphertext error");
1445 if (retval != buf->
len)
1447 msg(
D_TLS_ERRORS,
"TLS ERROR: write tls_write_ciphertext incomplete %d/%d",
1483 retval = mbedtls_ssl_read(ks->
ctx,
BPTR(buf), len);
1488 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1523 const mbedtls_x509_crt *cert;
1528 snprintf(s1,
sizeof(s1),
"%s %s, cipher %s",
1530 mbedtls_ssl_get_version(ks_ssl->
ctx),
1531 mbedtls_ssl_get_ciphersuite(ks_ssl->
ctx));
1533 cert = mbedtls_ssl_get_peer_cert(ks_ssl->
ctx);
1536 snprintf(s2,
sizeof(s2),
", %u bit key",
1537 (
unsigned int) mbedtls_pk_get_bitlen(&cert->pk));
1545 const char *tls_cert_profile,
1554 const int *ciphers = mbedtls_ssl_list_ciphersuites();
1560 if (tls_ctx.allowed_ciphers)
1562 ciphers = tls_ctx.allowed_ciphers;
1565 while (*ciphers != 0)
1567 printf(
"%s\n", mbedtls_ssl_get_ciphersuite_name(*ciphers));
1576 const mbedtls_ecp_curve_info *pcurve = mbedtls_ecp_curve_list();
1580 msg(
M_FATAL,
"Cannot retrieve curve list from mbed TLS");
1584 printf(
"Available Elliptic curves, listed in order of preference:\n\n");
1585 while (MBEDTLS_ECP_DP_NONE != pcurve->grp_id)
1587 printf(
"%s\n", pcurve->name);
1595 static char mbedtls_version[30];
1596 unsigned int pv = mbedtls_version_get_number();
1597 snprintf(mbedtls_version,
sizeof(mbedtls_version),
"mbed TLS %d.%d.%d",
1598 (pv>>24)&0xff, (pv>>16)&0xff, (pv>>8)&0xff );
1599 return mbedtls_version;