Security
Public key cryptography and supported signature schemes over HTTP and JSON-LD.
Last updated
Was this helpful?
Public key cryptography and supported signature schemes over HTTP and JSON-LD.
Last updated
Was this helpful?
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:
The three parts of the Signature:
header can be broken down like so:
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:
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:
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:
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
.
Consider the following request:
Mastodon verifies the signature using the following algorithm:
Split Signature:
into its separate parameters.
Construct the signature string from the value of headers
.
Fetch the keyId
and resolve to an actor's publicKey
.
SHA256 hash the signature string and compare to the Base64-decoded signature
as decrypted by publicKey[publicKeyPem]
.
Use the Date: header to check that the signed request was made within the past 12 hours.
When accepting activities from a relay. Public activities can optionally be sent to a relay with LD Signatures, and any server subscribing to a relay does not have to manually refetch the activity from the origin. This prevents having potentially infinite servers attempt to load the status from your instance.
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:
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
.
To verify a signature, Mastodon uses the following algorithm:
Make sure that a signature
exists and is a hash.
Make sure that signature[type]
is RsaSignature2017
.
Fetch the signature[creator]
URI. Make sure the creator exists.
Strip type
, id
, and signatureValue
from the signature
, leaving only signature[creator]
and signature[created]
.
Base64-decode the signatureValue
and verify it against the public key in signature[creator]
.
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 for more information on actor keys. An example key looks like this:
See also:
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:
When running a sequence to send Delete activities to all known peers, the payload will use LD Signatures because HTTP Signatures will not be available. Receiving servers will process the signature by validating it against the locally cached actor key, since the HTTP server will no longer be hosting old actor information.