Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

JSON Object Signing and Encryption (JOSE)

JSON Object Signing and Encryption (JOSE) is a set of standards that provides a method to secure the content of JSON objects. First of all, lets define the problem set that JOSE tries to solve. JSON objects are widely used in the web applications. However, JSON objects are subject to;

  1. Integrity: Ensuring that the content of the JSON object is not tampered.
  2. Confidentiality: Ensuring that the content of the JSON object is not read by unauthorized parties.
  3. Authentication: Ensuring that the content of the JSON object is sent by the expected party.

JOSE provides a set of standards to solve these problems. Lets explore them one by one.

JSON Web Algorithms (JWA)

JSON Web Algorithms (JWA) is a set of algorithms that can be used to secure the content of JSON objects. JWA organizes these algorithms into four different categories:

  1. Cryptographic Algorithms for Digital Signatures and MACs: These algorithms are used to ensure the integrity and the authentication of the content of the JSON objects.
  2. Cryptographic Algorithms for Key Management: These algorithms are used to manage the keys that are used to secure the content of the JSON objects.
  3. Cryptographic Algorithms for Content Encryption: These algorithms are used to ensure the confidentiality of the content of the JSON objects.
  4. Cryptographic Algorithms for Keys: These algorithms are used to manage the keys that are used to encrypt the content of the JSON objects.
"alg" ParamDigital Signature or MACImplementation Requirements
HS256HMAC using SHA-256Required
HS384HMAC using SHA-384Optional
HS512HMAC using SHA-512Optional
RS256RSASSA-PKCS1-v1_5 using SHA-256Recommended
RS384RSASSA-PKCS1-v1_5 using SHA-384Optional
RS512RSASSA-PKCS1-v1_5 using SHA-512Optional
ES256ECDSA using P-256 and SHA-256Recommended+
ES384ECDSA using P-384 and SHA-384Optional
ES512ECDSA using P-521 and SHA-512Optional
PS256RSASSA-PSS using SHA-256 and MGF1 with SHA-256Optional
PS384RSASSA-PSS using SHA-384 and MGF1 with SHA-384Optional
PS512RSASSA-PSS using SHA-512 and MGF1 with SHA-512Optional
noneNo digital signature or MAC performedOptional

JSON Web Signature (JWS)

JSON Web Signature (JWS) is a standardized format for digitally signing JSON-based content. By signing the content of a JSON object, the integrity and the authentication of the content can be ensured. JWS is defined in RFC 7515.

JWS simply starts with a JSON object that we want to sign and serializes it to a string representation. Then, the string representation is hashed with a cryptographic hash function. The hash is then encrypted with a private key. The result is the signature of the JSON object. The signature is then appended to the JSON object. The final result is a JWS object.

A JWS consists of Header.Payload.Signature structure. The Header part contains metadata about the signature. The Payload part contains the content of the JSON object. The Signature part contains the signature of the Header and Payload parts.

Lets try to calculate a JWS object of the following JSON object:

{
  "name": "John Doe",
  "age": 30,
  "email": "john.doe@example.com"
}

First, we need to decide which algorithm we want to use to sign the JSON object. There are several algorithms that can be used to sign a JSON object. Some of them are:

  • HMAC with SHA-256: This algorithm uses a symmetric key to sign the JSON object.
  • RSA with SHA-256: This algorithm uses an asymmetric key pair to sign the JSON object.
  • ECDSA with SHA-256: This algorithm uses an elliptic curve key pair to sign the JSON object.

JSON Web Key (JWK) and JSON Web Key Set (JWKS)

JSON Web Token (JWT)

JSON Web Encryption (JWE)