| 1 | <?php |
| 2 | namespace Aws\Crypto; |
| 3 | |
| 4 | use Aws\HasDataTrait; |
| 5 | use \ArrayAccess; |
| 6 | use \IteratorAggregate; |
| 7 | use \InvalidArgumentException; |
| 8 | use \JsonSerializable; |
| 9 | |
| 10 | /** |
| 11 | * Stores encryption metadata for reading and writing. |
| 12 | * |
| 13 | * @internal |
| 14 | */ |
| 15 | class MetadataEnvelope implements ArrayAccess, IteratorAggregate, JsonSerializable |
| 16 | { |
| 17 | use HasDataTrait; |
| 18 | |
| 19 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 20 | //# The "x-amz-" prefix denotes that the metadata is owned by an Amazon product |
| 21 | //# and MUST be prepended to all S3EC metadata mapkeys. |
| 22 | |
| 23 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 24 | //# - The mapkey "x-amz-key-v2" MUST be present for V2 format objects. |
| 25 | const CONTENT_KEY_V2_HEADER = 'x-amz-key-v2'; |
| 26 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 27 | //= type=implication |
| 28 | //# - This mapkey ("x-amz-3") SHOULD be represented by a constant named "ENCRYPTED_DATA_KEY_V3" or similar in the implementation code. |
| 29 | const ENCRYPTED_DATA_KEY_V3 = 'x-amz-3'; |
| 30 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 31 | //# - The mapkey "x-amz-iv" MUST be present for V1 format objects. |
| 32 | const IV_HEADER = 'x-amz-iv'; |
| 33 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 34 | //# - The mapkey "x-amz-matdesc" MUST be present for V1 format objects. |
| 35 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 36 | //# - The mapkey "x-amz-matdesc" MUST be present for V2 format objects. |
| 37 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 38 | //# - The mapkey "x-amz-iv" MUST be present for V2 format objects. |
| 39 | const MATERIALS_DESCRIPTION_HEADER = 'x-amz-matdesc'; |
| 40 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 41 | //= type=implication |
| 42 | //# - This mapkey ("x-amz-m") SHOULD be represented by a constant named "MAT_DESC_V3" or similar in the implementation code. |
| 43 | const MAT_DESC_V3 = 'x-amz-m'; |
| 44 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 45 | //# - The mapkey "x-amz-wrap-alg" MUST be present for V2 format objects. |
| 46 | const KEY_WRAP_ALGORITHM_HEADER = 'x-amz-wrap-alg'; |
| 47 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 48 | //= type=implication |
| 49 | //# - This mapkey ("x-amz-w") SHOULD be represented by a constant named "ENCRYPTED_DATA_KEY_ALGORITHM_V3" or similar in the implementation code. |
| 50 | const ENCRYPTED_DATA_KEY_ALGORITHM_V3 = 'x-amz-w'; |
| 51 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 52 | //# - The mapkey "x-amz-cek-alg" MUST be present for V2 format objects. |
| 53 | const CONTENT_CRYPTO_SCHEME_HEADER = 'x-amz-cek-alg'; |
| 54 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 55 | //= type=implication |
| 56 | //# - This mapkey ("x-amz-c") SHOULD be represented by a constant named "CONTENT_CIPHER_V3" or similar in the implementation code. |
| 57 | const CONTENT_CIPHER_V3 = 'x-amz-c'; |
| 58 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 59 | //# - The mapkey "x-amz-tag-len" MUST be present for V2 format objects. |
| 60 | const CRYPTO_TAG_LENGTH_HEADER = 'x-amz-tag-len'; |
| 61 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 62 | //# - The mapkey "x-amz-unencrypted-content-length" SHOULD be present for V1 format objects. |
| 63 | const UNENCRYPTED_CONTENT_LENGTH_HEADER = 'x-amz-unencrypted-content-length'; |
| 64 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 65 | //= type=implication |
| 66 | //# - This mapkey ("x-amz-t") SHOULD be represented by a constant named "ENCRYPTION_CONTEXT_V3" or similar in the implementation code. |
| 67 | const ENCRYPTION_CONTEXT_V3 = 'x-amz-t'; |
| 68 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 69 | //= type=implication |
| 70 | //# - This mapkey ("x-amz-d") SHOULD be represented by a constant named "KEY_COMMITMENT_V3" or similar in the implementation code. |
| 71 | const KEY_COMMITMENT_V3 = 'x-amz-d'; |
| 72 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 73 | //= type=implication |
| 74 | //# - This mapkey ("x-amz-i") SHOULD be represented by a constant named "MESSAGE_ID_V3" or similar in the implementation code. |
| 75 | const MESSAGE_ID_V3 = 'x-amz-i'; |
| 76 | |
| 77 | private static $constants = []; |
| 78 | |
| 79 | public static function getConstantValues() |
| 80 | { |
| 81 | if (empty(self::$constants)) { |
| 82 | $reflection = new \ReflectionClass(static::class); |
| 83 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 84 | //# The "x-amz-meta-" prefix is automatically added by the S3 server and MUST NOT be included in implementation code. |
| 85 | foreach (array_values($reflection->getConstants()) as $constant) { |
| 86 | self::$constants[$constant] = true; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return array_keys(self::$constants); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @return void |
| 95 | */ |
| 96 | #[\ReturnTypeWillChange] |
| 97 | public function offsetSet($name, $value) |
| 98 | { |
| 99 | $constants = self::getConstantValues(); |
| 100 | //= ../specification/s3-encryption/data-format/content-metadata.md#determining-s3ec-object-status |
| 101 | //# In general, if there is any deviation from the above format, with the exception of additional unrelated mapkeys, then the S3EC SHOULD throw an exception. |
| 102 | if (is_null($name) || !in_array($name, $constants)) { |
| 103 | throw new InvalidArgumentException('MetadataEnvelope fields must' |
| 104 | . ' must match a predefined offset; use the header constants.'); |
| 105 | } |
| 106 | |
| 107 | $this->data[$name] = $value; |
| 108 | } |
| 109 | |
| 110 | #[\ReturnTypeWillChange] |
| 111 | public function jsonSerialize() |
| 112 | { |
| 113 | return $this->data; |
| 114 | } |
| 115 | |
| 116 | public static function isV2Envelope(MetadataEnvelope $envelope): bool |
| 117 | { |
| 118 | if (!isset($envelope[MetadataEnvelope::CONTENT_KEY_V2_HEADER]) |
| 119 | || !isset($envelope[MetadataEnvelope::MATERIALS_DESCRIPTION_HEADER]) |
| 120 | || !isset($envelope[MetadataEnvelope::IV_HEADER]) |
| 121 | || !isset($envelope[MetadataEnvelope::KEY_WRAP_ALGORITHM_HEADER]) |
| 122 | || !isset($envelope[MetadataEnvelope::CONTENT_CRYPTO_SCHEME_HEADER]) |
| 123 | || !isset($envelope[MetadataEnvelope::CRYPTO_TAG_LENGTH_HEADER]) |
| 124 | ) { |
| 125 | return false; |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | public static function isV1Envelope(MetadataEnvelope $envelope): bool |
| 131 | { |
| 132 | if (!isset($envelope[MetadataEnvelope::CONTENT_KEY_V2_HEADER]) |
| 133 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 134 | //# - The mapkey "x-amz-matdesc" MUST be present for V1 format objects. |
| 135 | || !isset($envelope[MetadataEnvelope::MATERIALS_DESCRIPTION_HEADER]) |
| 136 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 137 | //# - The mapkey "x-amz-iv" MUST be present for V1 format objects. |
| 138 | || !isset($envelope[MetadataEnvelope::IV_HEADER]) |
| 139 | || !isset($envelope[MetadataEnvelope::KEY_WRAP_ALGORITHM_HEADER]) |
| 140 | || !isset($envelope[MetadataEnvelope::CONTENT_CRYPTO_SCHEME_HEADER]) |
| 141 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 142 | //# - The mapkey "x-amz-unencrypted-content-length" SHOULD be present for V1 format objects. |
| 143 | || !isset($envelope[MetadataEnvelope::UNENCRYPTED_CONTENT_LENGTH_HEADER]) |
| 144 | ) { |
| 145 | return false; |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | public static function isV3Envelope(MetadataEnvelope $envelope): bool |
| 151 | { |
| 152 | |
| 153 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 154 | //= type=implication |
| 155 | //# - The mapkey "x-amz-3" MUST be present for V3 format objects. |
| 156 | if (!isset($envelope[MetadataEnvelope::ENCRYPTED_DATA_KEY_V3]) |
| 157 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 158 | //= type=implication |
| 159 | //# - The mapkey "x-amz-c" MUST be present for V3 format objects. |
| 160 | || !isset($envelope[MetadataEnvelope::CONTENT_CIPHER_V3]) |
| 161 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 162 | //= type=implication |
| 163 | //# - The mapkey "x-amz-d" MUST be present for V3 format objects. |
| 164 | || !isset($envelope[MetadataEnvelope::KEY_COMMITMENT_V3]) |
| 165 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 166 | //= type=implication |
| 167 | //# - The mapkey "x-amz-i" MUST be present for V3 format objects. |
| 168 | || !isset($envelope[MetadataEnvelope::MESSAGE_ID_V3]) |
| 169 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 170 | //= type=implication |
| 171 | //# - The mapkey "x-amz-w" MUST be present for V3 format objects. |
| 172 | || !isset($envelope[MetadataEnvelope::ENCRYPTED_DATA_KEY_ALGORITHM_V3]) |
| 173 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 174 | //= type=implication |
| 175 | //# - The mapkey "x-amz-t" SHOULD be present for V3 format objects that use KMS Encryption Context. |
| 176 | || !isset($envelope[MetadataEnvelope::ENCRYPTION_CONTEXT_V3]) |
| 177 | ) { |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | public static function getV2Fields(): array |
| 185 | { |
| 186 | return [ |
| 187 | MetadataEnvelope::CONTENT_KEY_V2_HEADER, |
| 188 | MetadataEnvelope::MATERIALS_DESCRIPTION_HEADER, |
| 189 | MetadataEnvelope::IV_HEADER, |
| 190 | MetadataEnvelope::KEY_WRAP_ALGORITHM_HEADER, |
| 191 | MetadataEnvelope::CONTENT_CRYPTO_SCHEME_HEADER, |
| 192 | MetadataEnvelope::CRYPTO_TAG_LENGTH_HEADER |
| 193 | ]; |
| 194 | } |
| 195 | |
| 196 | public static function getV3Fields(): array |
| 197 | { |
| 198 | return [ |
| 199 | MetadataEnvelope::ENCRYPTED_DATA_KEY_V3, |
| 200 | MetadataEnvelope::CONTENT_CIPHER_V3, |
| 201 | MetadataEnvelope::KEY_COMMITMENT_V3, |
| 202 | MetadataEnvelope::MESSAGE_ID_V3, |
| 203 | MetadataEnvelope::ENCRYPTED_DATA_KEY_ALGORITHM_V3, |
| 204 | MetadataEnvelope::ENCRYPTION_CONTEXT_V3 |
| 205 | ]; |
| 206 | } |
| 207 | } |