nodemcu-firmware/docs/en/modules/tls.md

8.9 KiB

TLS Module

Since Origin / Contributor Maintainer Source
2016-12-15 PhoeniX PhoeniX tls.c

SSL/TLS support

!!! attention The TLS module depends on the net module, it is a required dependency.

NodeMCU includes the open-source version of mbed TLS library. With the NodeMCU default configuration it supports TLS 1.0 / 1.1 / 1.2 and the most common cipher suites, including DH/ECDH. ECDSA-based cipher suites are disabled by default.

!!! tip The complete configuration is stored in user_mbedtls.h. This is the file to edit if you build your own firmware and want to change mbed TLS behavior.

For a list of features have a look at the mbed TLS features page.

This module handles certificate verification when SSL/TLS is in use.

tls.createConnection()

Creates TLS connection.

Syntax

tls.createConnection()

Parameters

none

Returns

tls.socket sub module

Example

tls.createConnection()

tls.socket Module

tls.socket:close()

Closes socket.

Syntax

close()

Parameters

none

Returns

nil

See also

tls.createConnection()

tls.socket:connect()

Connect to a remote server.

Syntax

connect(port, ip|domain)

Parameters

  • port port number
  • ip IP address or domain name string

Returns

nil

See also

tls.socket:on()

tls.socket:dns()

Provides DNS resolution for a hostname.

Syntax

dns(domain, function(tls.socket, ip))

Parameters

  • domain domain name
  • function(tls.socket, ip) callback function. The first parameter is the socket, the second parameter is the IP address as a string.

Returns

nil

Example

sk = tls.createConnection()
sk:dns("google.com", function(conn, ip) print(ip) end)
sk = nil

tls.socket:getpeer()

Retrieve port and ip of peer.

Syntax

getpeer()

Parameters

none

Returns

  • ip of peer
  • port of peer

tls.socket:hold()

Throttle data reception by placing a request to block the TCP receive function. This request is not effective immediately, Espressif recommends to call it while reserving 5*1460 bytes of memory.

Syntax

hold()

Parameters

none

Returns

nil

See also

tls.socket:unhold()

tls.socket:on()

Register callback functions for specific events.

Syntax

on(event, function())

Parameters

  • event string, which can be "connection", "reconnection", "disconnection", "receive" or "sent"
  • function(tls.socket[, string]) callback function. The first parameter is the socket. If event is "receive", the second parameter is the received data as string. If event is "reconnection", the second parameter is the reason of connection error (string).

Returns

nil

Example

srv = tls.createConnection()
srv:on("receive", function(sck, c) print(c) end)
srv:on("connection", function(sck, c)
  -- Wait for connection before sending.
  sck:send("GET / HTTP/1.1\r\nHost: google.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
end)
srv:connect(443,"google.com")

!!! note The receive event is fired for every network frame! See details at net.socket:on().

See also

tls.socket:send()

Sends data to remote peer.

Syntax

send(string)

Parameters

  • string data in string which will be sent to server

Returns

nil

Note

Multiple consecutive send() calls aren't guaranteed to work (and often don't) as network requests are treated as separate tasks by the SDK. Instead, subscribe to the "sent" event on the socket and send additional data (or close) in that callback. See #730 for details.

See also

tls.socket:on()

tls.socket:unhold()

Unblock TCP receiving data by revocation of a preceding hold().

Syntax

unhold()

Parameters

none

Returns

nil

See also

tls.socket:hold()

tls.cert Module

tls.cert.verify()

Controls the vertificate verification process when the Nodemcu makes a secure connection.

Syntax

tls.cert.verify(enable)

tls.cert.verify(pemdata)

Parameters

  • enable A boolean which indicates whether verification should be enabled or not. The default at boot is false.
  • pemdata A string containing the CA certificate to use for verification.

Returns

true if it worked.

Can throw a number of errors if invalid data is supplied.

Example

Make a secure https connection and verify that the certificate chain is valid.

tls.cert.verify(true)
http.get("https://example.com/info", nil, function (code, resp) print(code, resp) end)

Load a certificate into the flash chip and make a request. This is the startssl root certificate. They provide free certificates.

tls.cert.verify([[
-----BEGIN CERTIFICATE-----
MIIHyTCCBbGgAwIBAgIBATANBgkqhkiG9w0BAQUFADB9MQswCQYDVQQGEwJJTDEW
MBQGA1UEChMNU3RhcnRDb20gTHRkLjErMCkGA1UECxMiU2VjdXJlIERpZ2l0YWwg
Q2VydGlmaWNhdGUgU2lnbmluZzEpMCcGA1UEAxMgU3RhcnRDb20gQ2VydGlmaWNh
dGlvbiBBdXRob3JpdHkwHhcNMDYwOTE3MTk0NjM2WhcNMzYwOTE3MTk0NjM2WjB9
MQswCQYDVQQGEwJJTDEWMBQGA1UEChMNU3RhcnRDb20gTHRkLjErMCkGA1UECxMi
U2VjdXJlIERpZ2l0YWwgQ2VydGlmaWNhdGUg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fAii6w0TiYiBKGHLHVKt+V9E9e4DGTANtLJL4YSjCMJwRuC
O3NJo2pXh5Tl1njFmUNj403gdy3hZZlyaQQaRwnmDwFWJPsfvw55qVguucQJAX6V
um0ABj6y6koQOdjQK/W/7HW/lwLFCRsI3FU34oH7N4RDYiDK51ZLZer+bMEkkySh
NOsF/5oirpt9P/FlUQqmMGqz9IgcgA38corog14=
-----END CERTIFICATE-----
]])

http.get("https://pskreporter.info/gen404", nil, function (code, resp) print(code, resp) end)

Notes

The certificate needed for verification is stored in the flash chip. The tls.cert.verify call with true enables verification against the value stored in the flash.

The certificate can be loaded into the flash chip in two ways -- one at firmware build time, and the other at initial boot of the firmware. In order to load the certificate at build time, just place a file containing the CA certificate (in PEM format) at server-ca.crt in the root of the nodemcu-firmware build tree. The build scripts will incorporate this into the resulting firmware image.

The alternative approach is easier for development, and that is to supply the PEM data as a string value to tls.cert.verify. This will store the certificate into the flash chip and turn on verification for that certificate. Subsequent boots of the nodemcu can then use tls.cert.verify(true) and use the stored certificate.