9.3 KiB
libp2p AutoTLS
| Lifecycle Stage | Maturity | Status | Latest Revision |
|---|---|---|---|
| 1A | Working Draft | Active | r1, 2025-05-21 |
Authors: @gmelodie
Interest Group: TBD
Table of Contents
Overview
Most modern web browsers only establish TLS connections with peers that present certificates issued by a recognized Certificate Authority (CA). Self-signed certificates are generally not accepted. To obtain a CA-issued certificate, a requester must complete an ACME (Automatic Certificate Management Environment) challenge. This typically involves provisioning a DNS TXT record on a domain the requester controls.
However, most libp2p peers do not own or control domain names, making it impractical for them to complete DNS-based ACME challenges and, by extension, to obtain trusted TLS certificates. This limitation hinders direct communication between libp2p peers and standard web browsers.
AutoTLS addresses this problem by introducing an AutoTLS broker — a server that controls a domain and facilitates ACME challenges on behalf of libp2p peers. A peer can request the AutoTLS broker to fulfil an ACME DNS challenge on its behalf. Once the broker sets the appropriate DNS record, the requesting peer proceeds to notify the ACME server. The ACME server validates the challenge against the broker's domain, and if successful, issues a valid certificate.
This mechanism allows libp2p peers to obtain CA-issued certificates without needing to possess or manage their own domain names.
General Flow
The following is the general flow of a successful certificate request and subsequent issuance using AutoTLS. Here, "client" refers to the machine running a libp2p peer and requesting the challenge, while "broker" and "AutoTLS broker", which are used interchangeably, is the server that will fulfil the ACME challenge on behalf of the client.
- Client requests a challenge from the ACME server.
- Client sends the challenge to the broker.
- Broker tests client and sets DNS record (fulfilling challenge).
- Client waits until the broker fulfils the challenge.
- Client signals to ACME server that challenge is fulfilled.
- ACME server checks challenge in broker.
- Client finalizes certificate request (creates and sends CSR to ACME server).
- Client waits until certificate is ready for download.
- Client downloads certificate.
Requesting challenge from ACME server
- The client starts a libp2p peer with public IPv4 and support for
identifyprotocol. - The client encodes its
PeerIDas multibase base36 of the CIDv1 of the multihash with thelibp2p-key(0x72) multicodec:- Transform PeerID into a multihash
mh. - Encode
mhusing CIDv1 with thelibp2p-keymulticodec(0x72). - Encode the CID data (if
cidis the CID, thencid.data.buffershould be encoded) using multibase base36, which is the same as regular base36 without trimming leading zeroes and including a leadingkorK) to getb36peerid.
- Transform PeerID into a multihash
- The client generates a key
mykeyas specified in RFC7518. - The client registers an account on the ACME server (e.g. production or staging servers for Let's Encrypt).
- Send a GET request to the
/directoryendpoint of the ACME server, and extract thenewAccountvalue from the JSON response, which will be the registration URL we'll use. - Send JWT-signed POST request to registration URL with the following
payload:{"termsOfServiceAgreed": true}(acontactfield containing a list ofmailto:bob@example.orgcontact information strings can also be optionally specified in the payload). The POST body is signed using JWT withmykeyandnonce(nonceis a number returned by sending a GET request to the ACME server at the URL specified indirectory["newNonce"]). The JSON payload using an RSA-256 key before JWT-signing should look like:{ "header": { "alg": "RS256", "typ": "JWT", "nonce": "`nonce`", "url": "`url`", "jwk": { "kty": "RSA", "n": "`mykey.n`", "e": "`mykey.e`" } }, "claims": { "payload": { "termsOfServiceAgreed": true, "contact": [ "mailto:alice@example.com", "mailto:bob@example.com" ] } } }
json { "payload": "`claims.toBase64`", "protected": "`header.toBase64`", "signature": "`base64UrlEncode(signature)`" } - Send a GET request to the
- The client MUST save the
kidpresent in thelocationheader of the ACME server's response for in future requests to ACME server. - The client requests a certificate for the
*.{b36peerid}.libp2p.directdomain from the ACME server by issuing a POST request using the same JWT signature scheme (and a newnonce) but using thekidfield instead of thejwkfield and containing the following JSON payload:{ "type": "dns", "value": "*.{b36peerid}.libp2p.direct" } - From the ACME server response, the client MUST save the entry with
"type"of"dns-01"and derive theKey Authorizationfrom that.
Sending challenge to AutoTLS broker
- The client sends the
key authorizationto the AutoTLS broker (e.g.registration.libp2p.direct). This requires a PeerID Authentication between client and broker:- Client sends GET request to the AutoTLS broker's
/v1/_acme-challengeendpoint and extractschallenge-client,public-keyandopaquefrom thewww-authenticateresponse header. - Client generates 32-character-long random string to be sent as a
challengeServer. At the time of writing the PeerID Authentication specification does not contain recommendations about challenge length, but the officialgo-libp2pimplementation uses 32 characters. - Client generates
sig,headersandpayloadas follows, wherepeer-privkeyis the private key of the client's libp2p peer andmultiaddrsis a list of string representations of the libp2p peer's multiaddresses:
Note:sig = base64URL( peer-privkey.sign( bytes(varint + "challenge-client={challenge-client}") + bytes(varint + "hostname={hostname}") + bytes(varint + "server-public-key={public-key}") ) ) headers = { "Content-Type": "application/json", "User-Agent": "some-user-agent", "authorization": "libp2p-PeerID public-key=\"{clientPublicKeyB64}\", opaque=\"{opaque}\", challenge-server=\"{challengeServer}\", sig=\"{sig}\"" } payload = { "value": keyAuthorization, "addresses": multiaddrs }varintis a protobuf varint field that encodes the length of each of thekey=valuestring. Note: the AutoTLS broker MUST NOT dial multiaddresses containing private IPv4 addresses, thus the client SHOULD only include multiaddresses that contain public IPv4 addresses inmultiaddrs. 4. Client sends a POST request to/v1/_acme-challengeendpoint usingpayloadas body andheadersas headers. 6. Client SHOULD save thebearertoken from theauthentication-inforesponse header, and use it for following requests to the AutoTLS broker. - Client sends GET request to the AutoTLS broker's
- Client SHOULD query DNS records (
TXT _acme-challenge.{b36peerid}.libp2p.directandA dashed-public-ip-address.{b36peerid}.libp2p.direct) until they are set by the AutoTLS broker. - Client notifies the ACME server about challenge completion so that the ACME server can lookup the DNS resource records. The notification is done in the form of a POST request with an empty JSON payload (
{}) as body sent to theurlfield returned by the ACME server when it responded to client's initial challenge request.- Client sends an empty signed JSON payload (
{}) to the ACME server using thekidobtained from the initial ACME registration and gets the response from the server (completedResponse). - Client extracts
urlfield fromcompletedResponse's JSON body ting it, again withkidsigning. The extracted URL is namedcheckUrlin this document.
- Client sends an empty signed JSON payload (
- The client polls the ACME server by sending an empty bodied,
kidsigned GET request tocheckUrluntil it receives a response withstatus: validorstatus: invalidfield, meaning that the challenge checking was successful or not, respectively.
Signalling challenge completion to ACME server
- Download certificate from ACME server.