|
| 1 | +# Cryptography |
| 2 | + |
| 3 | +The cryptography extension can encrypt and decrypt sensitive data, e.g. |
| 4 | +personal data of customers. For each subject (e.g. a person) a separate cipher |
| 5 | +key is created and used to encrypt the marked fields. If the key is deleted, |
| 6 | +the data becomes unreadable. This pattern is known as crypto-shredding and |
| 7 | +makes "forgetting" a person possible even in immutable storage. |
| 8 | +:::experimental |
| 9 | +The cryptography extension is experimental and may change in a minor release. |
| 10 | +::: |
| 11 | + |
| 12 | +## Setup |
| 13 | + |
| 14 | +Register the `CryptographyExtension` on the builder and pass it a |
| 15 | +`Cryptographer`. The `BaseCryptographer` with the openssl cipher is the default |
| 16 | +choice; it needs a [cipher key store](#cipher-key-store) to keep the keys. |
| 17 | + |
| 18 | +```php |
| 19 | +use Patchlevel\Hydrator\CoreExtension; |
| 20 | +use Patchlevel\Hydrator\Extension\Cryptography\BaseCryptographer; |
| 21 | +use Patchlevel\Hydrator\Extension\Cryptography\CryptographyExtension; |
| 22 | +use Patchlevel\Hydrator\Extension\Cryptography\Store\InMemoryCipherKeyStore; |
| 23 | +use Patchlevel\Hydrator\StackHydratorBuilder; |
| 24 | + |
| 25 | +$cipherKeyStore = new InMemoryCipherKeyStore(); |
| 26 | + |
| 27 | +$hydrator = (new StackHydratorBuilder()) |
| 28 | + ->useExtension(new CoreExtension()) |
| 29 | + ->useExtension(new CryptographyExtension(BaseCryptographer::createWithOpenssl($cipherKeyStore))) |
| 30 | + ->build(); |
| 31 | +``` |
| 32 | + |
| 33 | +## DataSubjectId |
| 34 | + |
| 35 | +First you need to define which field identifies the subject the data belongs |
| 36 | +to. The cipher key is created and looked up per subject id. |
| 37 | + |
| 38 | +```php |
| 39 | +use Patchlevel\Hydrator\Extension\Cryptography\Attribute\DataSubjectId; |
| 40 | +use Patchlevel\Hydrator\Extension\Cryptography\Attribute\SensitiveData; |
| 41 | + |
| 42 | +final class EmailChanged |
| 43 | +{ |
| 44 | + public function __construct( |
| 45 | + #[DataSubjectId] |
| 46 | + public readonly string $profileId, |
| 47 | + #[SensitiveData] |
| 48 | + public readonly string|null $email, |
| 49 | + ) { |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | +:::warning |
| 54 | +The `DataSubjectId` must be a string, you can use a [normalizer](normalizer.md) |
| 55 | +to convert a value object to a string. The subject id itself cannot be sensitive |
| 56 | +data. |
| 57 | +::: |
| 58 | + |
| 59 | +You can also use multiple subject ids in one class by naming them and |
| 60 | +referencing the name from the sensitive fields. The default name is `default`. |
| 61 | + |
| 62 | +```php |
| 63 | +use Patchlevel\Hydrator\Extension\Cryptography\Attribute\DataSubjectId; |
| 64 | +use Patchlevel\Hydrator\Extension\Cryptography\Attribute\SensitiveData; |
| 65 | + |
| 66 | +final class ProfilesMerged |
| 67 | +{ |
| 68 | + public function __construct( |
| 69 | + #[DataSubjectId(name: 'source')] |
| 70 | + public readonly string $sourceProfileId, |
| 71 | + #[SensitiveData(subjectIdName: 'source')] |
| 72 | + public readonly string|null $sourceEmail, |
| 73 | + #[DataSubjectId(name: 'target')] |
| 74 | + public readonly string $targetProfileId, |
| 75 | + #[SensitiveData(subjectIdName: 'target')] |
| 76 | + public readonly string|null $targetEmail, |
| 77 | + ) { |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## Fallback values |
| 83 | + |
| 84 | +If the data could not be decrypted, because the key has been removed, a |
| 85 | +fallback value is inserted. The default fallback is `null`. You can change this |
| 86 | +with the `fallback` parameter: |
| 87 | + |
| 88 | +```php |
| 89 | +use Patchlevel\Hydrator\Extension\Cryptography\Attribute\DataSubjectId; |
| 90 | +use Patchlevel\Hydrator\Extension\Cryptography\Attribute\SensitiveData; |
| 91 | + |
| 92 | +final class ProfileCreated |
| 93 | +{ |
| 94 | + public function __construct( |
| 95 | + #[DataSubjectId] |
| 96 | + public readonly string $profileId, |
| 97 | + #[SensitiveData(fallback: 'unknown')] |
| 98 | + public readonly string $name, |
| 99 | + ) { |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +You can also use a callable as a fallback. It receives the subject id: |
| 105 | + |
| 106 | +```php |
| 107 | +use Patchlevel\Hydrator\Extension\Cryptography\Attribute\DataSubjectId; |
| 108 | +use Patchlevel\Hydrator\Extension\Cryptography\Attribute\SensitiveData; |
| 109 | + |
| 110 | +final class ProfileCreated |
| 111 | +{ |
| 112 | + public function __construct( |
| 113 | + #[DataSubjectId] |
| 114 | + public readonly string $profileId, |
| 115 | + #[SensitiveData(fallback: 'deleted profile')] |
| 116 | + public readonly string $name, |
| 117 | + #[SensitiveData(fallbackCallable: [self::class, 'anonymizedEmail'])] |
| 118 | + public readonly string $email, |
| 119 | + ) { |
| 120 | + } |
| 121 | + |
| 122 | + public static function anonymizedEmail(string $subjectId): string |
| 123 | + { |
| 124 | + return sprintf('%s@anonymized.example', $subjectId); |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | +:::note |
| 129 | +`fallback` and `fallbackCallable` are mutually exclusive, setting both throws |
| 130 | +an exception. |
| 131 | +::: |
| 132 | + |
| 133 | +## Cipher Key Store |
| 134 | + |
| 135 | +The cipher keys must be stored somewhere. For testing purposes there is an |
| 136 | +in-memory implementation: |
| 137 | + |
| 138 | +```php |
| 139 | +use Patchlevel\Hydrator\Extension\Cryptography\Store\InMemoryCipherKeyStore; |
| 140 | + |
| 141 | +$cipherKeyStore = new InMemoryCipherKeyStore(); |
| 142 | +``` |
| 143 | + |
| 144 | +For production you have to implement the `CipherKeyStore` interface yourself, |
| 145 | +backed by a database or a key management service, because only you know where |
| 146 | +the keys should live: |
| 147 | + |
| 148 | +```php |
| 149 | +namespace Patchlevel\Hydrator\Extension\Cryptography\Store; |
| 150 | + |
| 151 | +use Patchlevel\Hydrator\Extension\Cryptography\Cipher\CipherKey; |
| 152 | + |
| 153 | +interface CipherKeyStore |
| 154 | +{ |
| 155 | + /** @throws CipherKeyNotExists */ |
| 156 | + public function currentKeyFor(string $subjectId): CipherKey; |
| 157 | + |
| 158 | + /** @throws CipherKeyNotExists */ |
| 159 | + public function get(string $id): CipherKey; |
| 160 | + |
| 161 | + public function store(CipherKey $key): void; |
| 162 | + |
| 163 | + public function remove(string $id): void; |
| 164 | + |
| 165 | + public function removeWithSubjectId(string $subjectId): void; |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +To avoid hitting your key storage for every operation, you can wrap the store |
| 170 | +in one of the cache decorators: |
| 171 | + |
| 172 | +```php |
| 173 | +use Patchlevel\Hydrator\Extension\Cryptography\Store\Psr6CacheStoreDecorator; |
| 174 | +use Patchlevel\Hydrator\Extension\Cryptography\Store\Psr16CacheStoreDecorator; |
| 175 | + |
| 176 | +$cipherKeyStore = new Psr6CacheStoreDecorator($myDatabaseStore, $psr6CachePool); |
| 177 | +// or |
| 178 | +$cipherKeyStore = new Psr16CacheStoreDecorator($myDatabaseStore, $psr16Cache); |
| 179 | +``` |
| 180 | + |
| 181 | +## Remove personal data |
| 182 | + |
| 183 | +To remove personal data, you only need to remove the keys for the subject from |
| 184 | +the store. All encrypted fields of that subject then resolve to their |
| 185 | +[fallback values](#fallback-values). |
| 186 | + |
| 187 | +```php |
| 188 | +$cipherKeyStore->removeWithSubjectId('profile-1'); |
| 189 | +``` |
| 190 | +:::danger |
| 191 | +Removing a cipher key is irreversible. The encrypted data can never be decrypted |
| 192 | +again, that is the point of crypto-shredding, but make sure it is what you want. |
| 193 | +::: |
| 194 | + |
| 195 | +:::tip |
| 196 | +Cryptography is very expensive in terms of performance. You can combine it with |
| 197 | +[lazy objects](lazy.md) so the data is only decrypted when the object is |
| 198 | +actually accessed. |
| 199 | +::: |
| 200 | + |
| 201 | +## Learn more |
| 202 | + |
| 203 | +* [How to hydrate objects lazily](lazy.md) |
| 204 | +* [How to reshape outdated stored data](upcasting.md) |
| 205 | +* [How extensions work](extensions.md) |
| 206 | +* [How to use the hydrator](hydrator.md) |
0 commit comments