Repository navigation
Expand file tree
/
Copy pathStoredDataKey.php
More file actions
85 lines (78 loc) · 4.22 KB
/
Copy pathStoredDataKey.php
File metadata and controls
85 lines (78 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\KeyManagement;
/**
* A data key as a store persists it: wrapped, never in plaintext.
*
* This is the shape the rewrapping path works on. {@see RewrappableDataKeyStoreInterface} lists
* stored keys so that each one can be unwrapped with the client that produced it and re-wrapped
* with another, which is how a master key or a whole KMS provider is rotated without reading or
* rewriting a single encrypted payload: only these rows change.
*
* `$client` is the name of the configured KMS client that wrapped the key, and the master key
* itself is named by `$wrapped->keyId`. Both are needed because a migration runs with the old and
* the new provider configured at the same time, so each row must say who can unwrap it.
*
* `$binding` ties the key to its reference and scope, {@see bindingFor()}.
* Its HMAC key is derived from the data key and from a secret of the store.
* A database writer doesn't know that secret.
* A store checks it once the key is open.
*
* @author Florent Morselli <florent.morselli@spomky-labs.com>
*
* @experimental
*/
final class StoredDataKey
{
/**
* Tells this derivation from any other use of the same data key, and leaves room for a second.
*/
private const string BINDING_INFO = 'symfony/key-management/data-key-binding/v1';
/**
* Tells this context from anything else the same master key is asked to authenticate.
*/
private const string WRAPPING_LABEL = 'symfony/key-management/data-key-wrapping/v1';
public function __construct(
public readonly string $reference,
public readonly string $scope,
public readonly Ciphertext $wrapped,
public readonly string $client,
public readonly string $binding,
) {
}
/**
* Proof that a data key was minted for `$reference` and `$scope` by a store that knows `$secret`.
*
* The wrapping of a data key is bound to nothing but the master key.
* Without this proof, a row whose scope was edited would hand the key of one tenant, column or purpose to another one, and the row of a retired key copied under a fresh UUIDv7 would read as the current key of its scope.
* The length prefix keeps two different pairs from hashing the same input.
*
* The HMAC key is derived from the data key and the secret, instead of passing authenticated data to the KMS, which some backends cannot enforce.
* The secret stops a database writer from inserting a data key they know.
* They could wrap such a key under a public key, or under a master key of their own that the application is allowed to use.
* A rewrap keeps the data key, so the binding can be checked and computed again under the current secret of the store.
*/
public static function bindingFor(string $reference, string $scope, #[\SensitiveParameter] string $plaintext, #[\SensitiveParameter] string $secret): string
{
return hash_hmac('sha256', pack('n', \strlen($reference)).$reference.$scope, hash_hkdf('sha256', $plaintext, 32, self::BINDING_INFO, $secret), true);
}
/**
* The context a store binds the wrapping of a data key to, where its backend can authenticate one.
*
* Passed as the `$aad` of {@see DataKeyGeneratorInterface::generateDataKey()} and of {@see DataKeyGeneratorInterface::unwrapDataKey()}, it has the KMS refuse a key presented under another reference or scope, which {@see bindingFor()} only refuses once the key is open.
* A key created for derivation requires one, HashiCorp Vault Transit `derived=true` for instance, and a backend refusing authenticated data cannot carry one at all.
* Only the KMS checks the context, so a client that ignores authenticated data instead of refusing it gets no protection from the context.
* {@see bindingFor()} is the check that applies whatever the backend.
*/
public static function wrappingContextFor(string $reference, string $scope): string
{
return self::WRAPPING_LABEL.pack('n', \strlen($reference)).$reference.$scope;
}
}