|
| 1 | +# Mbed TLS Compatibility Example |
| 2 | + |
| 3 | +`client-tls-mbedtls.c` is an application written for the Mbed TLS 4.x API, |
| 4 | +built on wolfSSL through the Mbed TLS compatibility layer in |
| 5 | +[wolfPSA](https://github.com/wolfSSL/wolfpsa). Its `tls_get()` function is the |
| 6 | +client from the "Porting a TLS Client" chapter of the Mbed TLS to wolfSSL |
| 7 | +migration guide, unchanged. |
| 8 | + |
| 9 | +wolfPSA provides the PSA Crypto API on wolfCrypt, as TF-PSA-Crypto does for |
| 10 | +Mbed TLS. Built with `MBEDTLS_COMPAT=1`, it also provides the Mbed TLS TLS, |
| 11 | +X.509 and network API on wolfSSL, so the client links `libwolfpsa.a` in place |
| 12 | +of `libmbedtls`, `libmbedx509` and `libtfpsacrypto`. The calls mapped so far |
| 13 | +and the known differences from Mbed TLS are in wolfPSA's README, section |
| 14 | +"Mbed TLS compatibility". |
| 15 | + |
| 16 | +## Building |
| 17 | + |
| 18 | +Build wolfPSA with the compatibility layer, next to `wolfssl-examples`. It |
| 19 | +compiles wolfCrypt and the wolfSSL TLS sources from `WOLFSSL_PATH` into |
| 20 | +`libwolfpsa.a` with its own settings, so the client needs no wolfSSL install: |
| 21 | + |
| 22 | +``` |
| 23 | +cd wolfpsa |
| 24 | +make MBEDTLS_COMPAT=1 WOLFSSL_PATH=../wolfssl |
| 25 | +``` |
| 26 | + |
| 27 | +Then build the example (set `WOLFPSA_DIR` if wolfPSA is elsewhere): |
| 28 | + |
| 29 | +``` |
| 30 | +make |
| 31 | +``` |
| 32 | + |
| 33 | +The Makefile puts wolfPSA's headers on the include path, so `<mbedtls/ssl.h>` |
| 34 | +resolves to wolfPSA even if Mbed TLS is installed too. |
| 35 | + |
| 36 | +## Running |
| 37 | + |
| 38 | +The servers are wolfSSL programs, built from `../tls/server-tls.c` against an |
| 39 | +installed wolfSSL, as for the other examples: |
| 40 | + |
| 41 | +``` |
| 42 | +cd wolfssl |
| 43 | +./configure |
| 44 | +make |
| 45 | +sudo make install |
| 46 | +sudo ldconfig |
| 47 | +``` |
| 48 | + |
| 49 | +Start a TLS 1.3 server, then run the client: |
| 50 | + |
| 51 | +``` |
| 52 | +make check-server-tls13 |
| 53 | +./check-server-tls13 & |
| 54 | +./client-tls-mbedtls 127.0.0.1 |
| 55 | +``` |
| 56 | + |
| 57 | +``` |
| 58 | +tls_get() returned 0 |
| 59 | +``` |
| 60 | + |
| 61 | +The client checks the server certificate against `../certs/ca-cert.pem` and |
| 62 | +the name `example.com`. Options: |
| 63 | + |
| 64 | +* `-A <file>` CA certificate file, PEM or DER |
| 65 | +* `-S <name>` server name to send and check |
| 66 | +* `-N` use a non-blocking socket |
| 67 | + |
| 68 | +## Testing |
| 69 | + |
| 70 | +`make check` builds `../tls/server-tls.c` as `check-server-tls12` and |
| 71 | +`check-server-tls13` and runs the client against them for TLS 1.2 and 1.3, |
| 72 | +blocking and non-blocking, and for each failure it should report: wrong server |
| 73 | +name, wrong CA, missing CA file and a CA file that is not a certificate. |
| 74 | +`../tls/server-tls13` is not used because it requires a client certificate, |
| 75 | +which this client does not send. |
| 76 | + |
| 77 | +A wrong server name or CA fails with `MBEDTLS_ERR_X509_CERT_VERIFY_FAILED` |
| 78 | +(`-9984`), and a CA file that is not a certificate with |
| 79 | +`MBEDTLS_ERR_X509_INVALID_FORMAT` (`-8576`). The compatibility layer returns |
| 80 | +only Mbed TLS codes: a wolfSSL error with no Mbed TLS match returns a general |
| 81 | +code for the call, such as `MBEDTLS_ERR_SSL_INTERNAL_ERROR`. |
| 82 | + |
| 83 | +`check.sh` runs the client and servers from `BIN_DIR`, by default the current |
| 84 | +directory. |
| 85 | + |
| 86 | +An IP address passed to `-S` fails with `MBEDTLS_ERR_SSL_BAD_INPUT_DATA` |
| 87 | +(`-135`), because the compatibility layer does not map IP address checks yet. |
0 commit comments