|
183 | 183 | #define MBEDTLS_ERR_SSL_BAD_CONFIG -0x5E80
|
184 | 184 | /** Cache entry not found */
|
185 | 185 | #define MBEDTLS_ERR_SSL_CACHE_ENTRY_NOT_FOUND -0x5E00
|
| 186 | +/** Attempt to verify a certificate without an expected hostname. |
| 187 | + * This is usually insecure. |
| 188 | + * |
| 189 | + * In TLS clients, when a client authenticates a server through its |
| 190 | + * certificate, the client normally checks three things: |
| 191 | + * - the certificate chain must be valid; |
| 192 | + * - the chain must start from a trusted CA; |
| 193 | + * - the certificate must cover the server name that is expected by the client. |
| 194 | + * |
| 195 | + * Omitting any of these checks is generally insecure, and can allow a |
| 196 | + * malicious server to impersonate a legitimate server. |
| 197 | + * |
| 198 | + * The third check may be safely skipped in some unusual scenarios, |
| 199 | + * such as networks where eavesdropping is a risk but not active attacks, |
| 200 | + * or a private PKI where the client equally trusts all servers that are |
| 201 | + * accredited by the root CA. |
| 202 | + * |
| 203 | + * You should call mbedtls_ssl_set_hostname() with the expected server name |
| 204 | + * before starting a TLS handshake on a client (unless the client is |
| 205 | + * set up to only use PSK-based authentication, which does not rely on the |
| 206 | + * host name). If you have determined that server name verification is not |
| 207 | + * required for security in your scenario, call mbedtls_ssl_set_hostname() |
| 208 | + * with \p NULL as the server name. |
| 209 | + * |
| 210 | + * This error is raised if all of the following conditions are met: |
| 211 | + * |
| 212 | + * - A TLS client is configured with the authentication mode |
| 213 | + * #MBEDTLS_SSL_VERIFY_REQUIRED (default). |
| 214 | + * - Certificate authentication is enabled. |
| 215 | + * - The client does not call mbedtls_ssl_set_hostname(). |
| 216 | + * - The configuration option |
| 217 | + * #MBEDTLS_SSL_CLI_ALLOW_WEAK_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME |
| 218 | + * is not enabled. |
| 219 | + */ |
| 220 | +#define MBEDTLS_ERR_SSL_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME -0x5D80 |
186 | 221 |
|
187 | 222 | /*
|
188 | 223 | * Various constants
|
@@ -1403,8 +1438,36 @@ struct mbedtls_ssl_context {
|
1403 | 1438 | * User settings
|
1404 | 1439 | */
|
1405 | 1440 | #if defined(MBEDTLS_X509_CRT_PARSE_C)
|
1406 |
| - char *hostname; /*!< expected peer CN for verification |
1407 |
| - (and SNI if available) */ |
| 1441 | + /** Expected peer CN for verification. |
| 1442 | + * |
| 1443 | + * Also used on clients for SNI. |
| 1444 | + * |
| 1445 | + * The value of this field can be: |
| 1446 | + * - \p NULL in a newly initialized or reset context. |
| 1447 | + * - A heap-allocated copy of the last value passed to |
| 1448 | + * mbedtls_ssl_set_hostname(), if the last call had a non-null |
| 1449 | + * \p hostname argument. |
| 1450 | + * - A special value to indicate that mbedtls_ssl_set_hostname() |
| 1451 | + * was called with \p NULL (as opposed to never having been called). |
| 1452 | + * |
| 1453 | + * If you need to obtain the value passed to |
| 1454 | + * mbedtls_ssl_set_hostname() even if it may have been called with |
| 1455 | + * \p NULL, call mbedtls_ssl_get_hostname_pointer(). |
| 1456 | + * |
| 1457 | + * If this field contains the value \p NULL and the configuration option |
| 1458 | + * #MBEDTLS_SSL_CLI_ALLOW_WEAK_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME |
| 1459 | + * is unset, on a TLS client, attempting to verify a server certificate |
| 1460 | + * results in the error |
| 1461 | + * #MBEDTLS_ERR_SSL_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME. |
| 1462 | + * |
| 1463 | + * If this field contains the special value described above, or if |
| 1464 | + * the value is \p NULL and the configuration option |
| 1465 | + * #MBEDTLS_SSL_CLI_ALLOW_WEAK_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME |
| 1466 | + * is set, then the peer name verification is skipped, which may be |
| 1467 | + * insecure, especially on a client. Furthermore, on a client, the |
| 1468 | + * server_name extension is not sent. |
| 1469 | + */ |
| 1470 | + char *hostname; |
1408 | 1471 | #endif /* MBEDTLS_X509_CRT_PARSE_C */
|
1409 | 1472 |
|
1410 | 1473 | #if defined(MBEDTLS_SSL_ALPN)
|
@@ -1535,6 +1598,14 @@ void mbedtls_ssl_init(mbedtls_ssl_context *ssl);
|
1535 | 1598 | * Calling mbedtls_ssl_setup again is not supported, even
|
1536 | 1599 | * if no session is active.
|
1537 | 1600 | *
|
| 1601 | + * \warning After setting up a client context, if certificate-based |
| 1602 | + * authentication is enabled, you should call |
| 1603 | + * mbedtls_ssl_set_hostname() to specifiy the expected |
| 1604 | + * name of the server. Without this, in most scenarios, |
| 1605 | + * the TLS connection is insecure. See |
| 1606 | + * #MBEDTLS_ERR_SSL_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME |
| 1607 | + * for more information. |
| 1608 | + * |
1538 | 1609 | * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
1539 | 1610 | * subsystem must have been initialized by calling
|
1540 | 1611 | * psa_crypto_init() before calling this function.
|
@@ -3107,16 +3178,29 @@ void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
|
3107 | 3178 | #if defined(MBEDTLS_X509_CRT_PARSE_C)
|
3108 | 3179 | /**
|
3109 | 3180 | * \brief Set or reset the hostname to check against the received
|
3110 |
| - * server certificate. It sets the ServerName TLS extension, |
3111 |
| - * too, if that extension is enabled. (client-side only) |
| 3181 | + * peer certificate. On a client, this also sets the |
| 3182 | + * ServerName TLS extension, if that extension is enabled. |
| 3183 | + * On a TLS 1.3 client, this also sets the server name in |
| 3184 | + * the session resumption ticket, if that feature is enabled. |
3112 | 3185 | *
|
3113 | 3186 | * \param ssl SSL context
|
3114 |
| - * \param hostname the server hostname, may be NULL to clear hostname |
3115 |
| -
|
3116 |
| - * \note Maximum hostname length MBEDTLS_SSL_MAX_HOST_NAME_LEN. |
3117 |
| - * |
3118 |
| - * \return 0 if successful, MBEDTLS_ERR_SSL_ALLOC_FAILED on |
3119 |
| - * allocation failure, MBEDTLS_ERR_SSL_BAD_INPUT_DATA on |
| 3187 | + * \param hostname The server hostname. This may be \c NULL to clear |
| 3188 | + * the hostname. |
| 3189 | + * |
| 3190 | + * \note Maximum hostname length #MBEDTLS_SSL_MAX_HOST_NAME_LEN. |
| 3191 | + * |
| 3192 | + * \note If the hostname is \c NULL on a client, then the server |
| 3193 | + * is not authenticated: it only needs to have a valid |
| 3194 | + * certificate, not a certificate matching its name. |
| 3195 | + * Therefore you should always call this function on a client, |
| 3196 | + * unless the connection is set up to only allow |
| 3197 | + * pre-shared keys, or in scenarios where server |
| 3198 | + * impersonation is not a concern. See the documentation of |
| 3199 | + * #MBEDTLS_ERR_SSL_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME |
| 3200 | + * for more details. |
| 3201 | + * |
| 3202 | + * \return 0 if successful, #MBEDTLS_ERR_SSL_ALLOC_FAILED on |
| 3203 | + * allocation failure, #MBEDTLS_ERR_SSL_BAD_INPUT_DATA on |
3120 | 3204 | * too long input hostname.
|
3121 | 3205 | *
|
3122 | 3206 | * Hostname set to the one provided on success (cleared
|
|
0 commit comments