title: Security description: Public key cryptography and supported signature schemes over HTTP and JSON-LD. menu: docs:
weight: 30
parent: spec
{{< caption-link url="https://github.com/tootsuite/mastodon/blob/master/app/lib/request.rb" caption="app/lib/request.rb" >}}
HTTP Signatures is a specification for signing HTTP messages by using a `Signature:` header with your HTTP request. Mastodon requires the use of HTTP Signatures in order to validate that any activity received was authored by the actor generating it. When secure mode is enabled, all GET requests require HTTP signatures as well.
For any HTTP request incoming to Mastodon, the following header should be attached:
Signature: keyId="https://my-example.com/actor#main-key",headers="(request-target) host date",signature="Y2FiYW...IxNGRiZDk4ZA=="
The three parts of the Signature:
header can be broken down like so:
Signature:
keyId="https://my-example.com/actor#main-key",
headers="(request-target) host date",
signature="Y2FiYW...IxNGRiZDk4ZA=="
The keyId
should correspond to the actor and the key being used to generate the signature
, whose value is equal to all parameters in headers
concatenated together and signed by the key, then Base64-encoded. See ActivityPub > Public key for more information on actor keys. An example key looks like this:
"publicKey": {
"id": "https://my-example.com/actor#main-key",
"owner": "https://my-example.com/actor",
"publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvXc4vkECU2/CeuSo1wtn\nFoim94Ne1jBMYxTZ9wm2YTdJq1oiZKif06I2fOqDzY/4q/S9uccrE9Bkajv1dnkO\nVm31QjWlhVpSKynVxEWjVBO5Ienue8gND0xvHIuXf87o61poqjEoepvsQFElA5ym\novljWGSA/jpj7ozygUZhCXtaS2W5AD5tnBQUpcO0lhItYPYTjnmzcc4y2NbJV8hz\n2s2G8qKv8fyimE23gY1XrPJg+cRF+g4PqFXujjlJ7MihD9oqtLGxbu7o1cifTn3x\nBfIdPythWu5b4cujNsB3m3awJjVmx+MHQ9SugkSIYXV0Ina77cTNS0M2PYiH1PFR\nTwIDAQAB\n-----END PUBLIC KEY-----\n"
},
See also: https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
To create an HTTP signature, you will have to define which headers are being hashed and signed. For example, consider the following request being sent out:
GET /users/username/inbox HTTP/1.1
Host: mastodon.example
Date: 18 Dec 2019 10:08:46 GMT
Accept: application/activity+json
The signature string is constructed using the values of the HTTP headers defined in headers
, joined by newlines. Typically, you will want to include the request target, as well as the host and the date. Mastodon assumes Date:
header if none are provided. For the above request, to generate a Signature:
with headers="(request-target) host date"
we would generate the following string:
(request-target): get /users/username/inbox
host: mastodon.example
date: 18 Dec 2019 10:08:46 GMT
Note that we don't care about the Accept:
header because we won't be specifying it in headers
.
The signature string is then hashed with SHA256 and signed with the actor's public key. The resulting value is attached as signature
within the Signature: header. The final request looks like this:
GET /users/username/inbox HTTP/1.1
Host: mastodon.example
Date: 18 Dec 2019 10:08:46 GMT
Accept: application/activity+json
Signature: keyId="https://my-example.com/actor#main-key",headers="(request-target) host date",signature="Y2FiYW...IxNGRiZDk4ZA=="
This request is functionally equivalent to saying that https://my-example.com/actor
is requesting https://mastodon.example/users/username/inbox
and is proving that they sent this request by signing (request-target)
, Host:
, and Date:
with their public key linked at keyId
, resulting in the provided signature
.
{{< caption-link url="https://github.com/tootsuite/mastodon/blob/master/app/controllers/concerns/signature_verification.rb" caption="app/controllers/concerns/signature_verification.rb" >}}
Consider the following request:
GET /users/username/inbox HTTP/1.1
Host: mastodon.example
Date: 18 Dec 2019 10:08:46 GMT
Accept: application/activity+json
Signature: keyId="https://my-example.com/actor#main-key",headers="(request-target) host date",signature="Y2FiYW...IxNGRiZDk4ZA=="
Mastodon verifies the signature using the following algorithm:
Signature:
into its separate parameters.headers
.keyId
and resolve to an actor's publicKey
.signature
as decrypted by publicKey[publicKeyPem]
.{{< caption-link url="https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/linked_data_signature.rb" caption="app/lib/activitypub/linked_data_signature.rb" >}}
Linked Data Signatures 1.0 is a specification for attaching cryptographic signatures to JSON-LD documents. LD Signatures are not used widely within Mastodon, but they are used in the following situations:
To create a signature, Mastodon uses the keypair attached to an actor at https://mastodon.example/users/username#main-key
. It then creates an SHA256 hash of the document, signs it with the keypair, and Base64-strict-encodes the resulting output to derive a signatureValue
. The following hash is merged into the JSON-LD document:
"signature": {
"type": "RsaSignature2017",
"creator": "https://mastodon.example/users/username#main-key",
"created": "2019-12-08T03:48:33.901Z",
"signatureValue": "s69F3mfddd99dGjmvjdjjs81e12jn121Gkm1"
}
{{< hint style="warning" >}}
Mastodon's current implementation of LD Signatures is somewhat outdated due to a change in the JSON-LD @context between the drafting stage and finalization stage of the specification. Mastodon expects a type
of RsaSignature2017
while the current specification instead defines RsaSignature2018
via the namespace https://w3id.org/security/v2
.
{{< /hint >}}
To verify a signature, Mastodon uses the following algorithm:
signature
exists and is a hash.signature[type]
is RsaSignature2017
.signature[creator]
URI. Make sure the creator exists.type
, id
, and signatureValue
from the signature
, leaving only signature[creator]
and signature[created]
.signatureValue
and verify it against the public key in signature[creator]
.