Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

# IDE Temp Files
**/*.swp
**/compile_commands.json

# Android files
android/wolfcryptjni-ndk-gradle/.gradle
Expand Down
25 changes: 23 additions & 2 deletions mbedtls-compat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ kill %1
```

```
Handshake done, TLSv1.3, TLS1-3-AES-256-GCM-SHA384
tls_get() returned 0
```

Expand All @@ -78,15 +79,35 @@ bundle, whose path depends on the system:
www.wolfssl.com
```

```
Handshake done, TLSv1.3, TLS1-3-AES-128-GCM-SHA256
tls_get() returned 0
```

When the certificate check fails, the client prints the reasons from
`mbedtls_x509_crt_verify_info()`, as the Mbed TLS sample programs do. For
example, with a CA that did not sign the server's chain:

```
./client-tls-mbedtls -p 443 -A ../certs/ca-cert.pem www.wolfssl.com
```

```
! The certificate is not correctly signed by the trusted CA
tls_get() returned -9984
```

## Testing

`make check` builds `../tls/server-tls.c` as `check-server-tls12` and
`check-server-tls13` and runs the client against them for TLS 1.2 and 1.3,
blocking and non-blocking, with an IP address and a DNS name as the server
name and a host name to connect to, and for each failure it should report:
wrong server name or IP address, wrong CA, missing CA file and a CA file that
is not a certificate. `../tls/server-tls13` is not used because it requires a
client certificate, which this client does not send.
is not a certificate. Each handshake must also print the protocol line, and
each failed certificate check at least one reason. `../tls/server-tls13` is
not used because it requires a client certificate, which this client does not
send.

A wrong server name, IP address or CA fails with
`MBEDTLS_ERR_X509_CERT_VERIFY_FAILED` (`-9984`), a missing CA file with
Expand Down
12 changes: 11 additions & 1 deletion mbedtls-compat/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ has_line() {
printf '%s\n' "$out" | grep -qxF "$1"
}

# True if a line of the client output starts with $1
has_prefix() {
printf '%s\n' "$out" | grep -q "^$1"
}

# Retry while the server is not listening yet. A probe connection would end
# the server, since it exits when a handshake fails.
run_client() {
Expand All @@ -35,7 +40,12 @@ while IFS='|' read -r name server opts expect host; do
run_client
want=1
[ "$expect" -eq 0 ] && want=0
if ! has_line "tls_get() returned $expect" || [ $rc -ne $want ]; then
# The protocol after a handshake, the reasons after a failed verify
info=
[ $want -eq 0 ] && info="Handshake done, TLSv1"
[ "$expect" -eq -9984 ] && info=" ! "
if ! has_line "tls_get() returned $expect" || [ $rc -ne $want ] ||
{ [ -n "$info" ] && ! has_prefix "$info"; }; then
echo "FAIL: $name: $out"
FAILS=$((FAILS + 1))
# If the server failed to bind, the client reached another server
Expand Down
14 changes: 12 additions & 2 deletions mbedtls-compat/client-tls-mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@

/* Mbed TLS 4.x client built on wolfSSL through wolfPSA's Mbed TLS
* compatibility layer. tls_get() is the client from the Mbed TLS migration
* guide, plus retrying partial writes, poll() before non-blocking retries and
* an error when the server closes without a reply. */
* guide, plus retrying partial writes, poll() before non-blocking retries, an
* error when the server closes without a reply, and printing the protocol,
* cipher suite and certificate verify result. */

/* the usual suspects */
#include <stdlib.h>
Expand Down Expand Up @@ -68,6 +69,7 @@ int tls_get(int fd, const char* host, const char* caFile)
mbedtls_ssl_config conf;
mbedtls_x509_crt ca;
unsigned char buf[256];
char info[512];
const char req[] = "GET / HTTP/1.0\r\n\r\n";
size_t sent = 0;

Expand Down Expand Up @@ -102,6 +104,9 @@ int tls_get(int fd, const char* host, const char* caFile)
goto out;
}
}
printf("Handshake done, %s, %s\n", mbedtls_ssl_get_version(&ssl),
mbedtls_ssl_get_ciphersuite(&ssl));

while (sent < sizeof(req) - 1) {
ret = mbedtls_ssl_write(&ssl, (const unsigned char*)req + sent,
sizeof(req) - 1 - sent);
Expand All @@ -125,6 +130,11 @@ int tls_get(int fd, const char* host, const char* caFile)
mbedtls_ssl_close_notify(&ssl);

out:
if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
mbedtls_x509_crt_verify_info(info, sizeof(info), " ! ",
mbedtls_ssl_get_verify_result(&ssl));
printf("%s", info);
}
mbedtls_ssl_free(&ssl);
mbedtls_ssl_config_free(&conf);
mbedtls_x509_crt_free(&ca);
Expand Down
Loading