|
| 1 | +WiFiClientSecure |
| 2 | +================ |
| 3 | + |
| 4 | +The WiFiClientSecure class implements support for secure connections using TLS (SSL). |
| 5 | +It inherits from WiFiClient and thus implements a superset of that class' interface. |
| 6 | +There are three ways to establish a secure connection using the WiFiClientSecure class: |
| 7 | +using a root certificate authority (CA) cert, using a root CA cert plus a client cert and key, |
| 8 | +and using a pre-shared key (PSK). |
| 9 | + |
| 10 | +Using a root certificate authority cert |
| 11 | +--------------------------------------- |
| 12 | +This method authenticates the server and negotiates an encrypted connection. |
| 13 | +It is the same functionality as implemented in your web browser when you connect to HTTPS sites. |
| 14 | + |
| 15 | +If you are accessing your own server: |
| 16 | +- Generate a root certificate for your own certificate authority |
| 17 | +- Generate a cert & private key using your root certificate ("self-signed cert") for your server |
| 18 | +If you are accessing a public server: |
| 19 | +- Obtain the cert of the public CA that signed that server's cert |
| 20 | +Then: |
| 21 | +- In WiFiClientSecure use setCACert (or the appropriate connect method) to set the root cert of your |
| 22 | + CA or of the public CA |
| 23 | +- When WiFiClientSecure connects to the target server it uses the CA cert to verify the certificate |
| 24 | + presented by the server, and then negotiates encryption for the connection |
| 25 | + |
| 26 | +Please see the WiFiClientSecure example. |
| 27 | + |
| 28 | +Using a root CA cert and client cert/keys |
| 29 | +----------------------------------------- |
| 30 | +This method authenticates the server and additionally also authenticates |
| 31 | +the client to the server, then negotiates an encrypted connection. |
| 32 | + |
| 33 | +- Follow steps above |
| 34 | +- Using your root CA generate cert/key for your client |
| 35 | +- Register the keys with the server you will be accessing so the server can authenticate your client |
| 36 | +- In WiFiClientSecure use setCACert (or the appropriate connect method) to set the root cert of your |
| 37 | + CA or of the public CA, this is used to authenticate the server |
| 38 | +- In WiFiClientSecure use setCertificate, and setPrivateKey (or the appropriate connect method) to |
| 39 | + set your client's cert & key, this will be used to authenticate your client to the server |
| 40 | +- When WiFiClientSecure connects to the target server it uses the CA cert to verify the certificate |
| 41 | + presented by the server, it will use the cert/key to authenticate your client to the server, and |
| 42 | + it will then negotiate encryption for the connection |
| 43 | + |
| 44 | +Using Pre-Shared Keys (PSK) |
| 45 | +--------------------------- |
| 46 | + |
| 47 | +TLS supports authentication and encryption using a pre-shared key (i.e. a key that both client and |
| 48 | +server know) as an alternative to the public key cryptography commonly used on the web for HTTPS. |
| 49 | +PSK is starting to be used for MQTT, e.g. in mosquitto, to simplify the set-up and avoid having to |
| 50 | +go through the whole CA, cert, and private key process. |
| 51 | + |
| 52 | +A pre-shared key is a binary string of up to 32 bytes and is commonly represented in hex form. In |
| 53 | +addition to the key, clients can also present an id and typically the server allows a different key |
| 54 | +to be associated with each client id. In effect this is very similar to username and password pairs, |
| 55 | +except that unlike a password the key is not directly transmitted to the server, thus a connection to a |
| 56 | +malicious server does not divulge the password. Plus the server is also authenticated to the client. |
| 57 | + |
| 58 | +To use PSK: |
| 59 | +- Generate a random hex string (generating an MD5 or SHA for some file is one way to do this) |
| 60 | +- Come up with a string id for your client and configure your server to accept the id/key pair |
| 61 | +- In WiFiClientSecure use setPreSharedKey (or the appropriate connect method) to |
| 62 | + set the id/key combo |
| 63 | +- When WiFiClientSecure connects to the target server it uses the id/key combo to authenticate the |
| 64 | + server (it must prove that it has the key too), authenticate the client and then negotiate |
| 65 | + encryption for the connection |
| 66 | + |
| 67 | +Please see the WiFiClientPSK example. |
0 commit comments