All pages
Powered by GitBook
1 of 4

Loading...

Loading...

Loading...

Loading...

Encrypted tokens and

Unprotected Headers

As well as the signed tokens, the encrypted tokens also have unprotected header. But with one difference: there are two unprotected headers:

  • Shared unprotected header applicable to all recipients.

  • Per-recipient unprotected header.

With the example below, we will create an encrypted token for two recipient and some unprotected header parameters:

$jwe = $jweBuilder
    ->create()
    ->withPayload('...')
    ->withSharedProtectedHeader(['enc' => 'A256GCM', 'alg' => 'A256KW'])
    ->withSharedHeader(['author' => 'John Doe'])
    ->addRecipient($recipient_public_key_1, ['message' => 'Hello World!'])
    ->addRecipient($recipient_public_key_2, ['description' => 'Nice song for you'])
    ->build();

The variable $jwe will be a valid JWE object built for two recipients. The unprotected header parameter author is applicable to the whole token while message and description are available only for the first and second recipient respectively.

Note: when an unprotected header is set, the Compact Serialization mode is not available.

Multiple Recipients

When you need to encrypt the same payload for several audiences, you may want to do it at once. The JWE Builder supports multiple recipients.

With the example below, we will create an encrypted token for three different recipients using three different key encryption algorithms.

Important notes:

  • The content encryption algorithm MUST be the same for all recipients.

  • The Key Management Modes of the key encryption algorithms MUST be compatible (see table below).

Note: when an unprotected header is set, the Compact Serialization mode is not available.

Key Management Modes

Each Key Encryption Algorithm has its own Key Management Mode.

Key Encryption Algorithms And Associated Key Management Mode.

Compatibility table between Key Management Modes:

$jweBuilder
    ->create()
    ->withPayload('...')
    ->withSharedProtectedHeader(['enc' => 'A128GCM'])
    ->addRecipient($recipient_key_1, ['alg' => 'RSA1_5'])
    ->addRecipient($recipient_key_2, ['alg' => 'RSA-OAEP-256'])
    ->build();

A192KW

X

A256KW

X

ECDH-ES

X

ECDH-ES+A128KW

X

ECDH-ES+A192KW

X

ECDH-ES+A256KW

X

PBES2-HS256+A128KW

X

PBES2-HS384+A192KW

X

PBES2-HS512+A256KW

X

RSA1_5

X

RSA-OAEP

X

RSA-OAEP-256

X

A128GCMKW

X

A192GCMKW

X

A256GCMKW

X

NO

YES

NO

Direct Key Agreement

NO

NO

NO

NO

NO

Key Agreement with Key Wrapping

YES

YES

NO

NO

NO

Direct Encryption

NO

NO

NO

NO

NO

Algorithm Key Management Mode

Key Encryption

Key Wrapping

Direct Key Agreement

Key Agreement with Key Wrapping

Direct Encryption

dir

X

A128KW

Key Management Mode

Key Encryption

Key Wrapping

Direct Key Agreement

Key Agreement with Key Wrapping

Direct Encryption

Key Encryption

YES

YES

NO

YES

NO

Key Wrapping

YES

X

YES

Additional Authentication Data (AAD)

The Additional Authenticated Data (AAD) is an input to an Authenticated Encryption operation. The AAD is integrity protected but not encrypted.

Its value can be any string you want that is needed by your application. With the example below, we will add a dummy AAD:

$jwe = $jweBuilder
    ->create()
    ->withPayload('...')
    ->withSharedProtectedHeader([
        'enc' => 'A256CBC-HS512',
        'alg' => 'RSA-OAEP-256',
        //'zip' => 'DEF',
    ])
    ->addRecipient($recipient_key)
    ->withAAD('A,B,C,D')
    ->build();

Note: when the AAD is set, the Compact Serialization mode is not available.