| 1 | <?php |
| 2 | |
| 3 | namespace Aws\Crypto; |
| 4 | |
| 5 | use Aws\Crypto\Cipher\CipherMethod; |
| 6 | use GuzzleHttp\Psr7\AppendStream; |
| 7 | use GuzzleHttp\Psr7\Stream; |
| 8 | |
| 9 | /** |
| 10 | * @internal |
| 11 | */ |
| 12 | abstract class AbstractCryptoClientV3 |
| 13 | { |
| 14 | const SUPPORTED_SECURITY_PROFILES = ['V3', 'V3_AND_LEGACY']; |
| 15 | |
| 16 | const LEGACY_SECURITY_PROFILES = ['V3_AND_LEGACY']; |
| 17 | |
| 18 | const KEY_COMMITMENT_POLICIES = [ |
| 19 | 'FORBID_ENCRYPT_ALLOW_DECRYPT', |
| 20 | 'REQUIRE_ENCRYPT_ALLOW_DECRYPT', |
| 21 | 'REQUIRE_ENCRYPT_REQUIRE_DECRYPT' |
| 22 | ]; |
| 23 | |
| 24 | public static array $supportedCiphers = ['gcm']; |
| 25 | |
| 26 | public static array $supportedKeyWraps = [ |
| 27 | KmsMaterialsProviderV3::WRAP_ALGORITHM_NAME |
| 28 | ]; |
| 29 | |
| 30 | /** |
| 31 | * Returns if the passed policy name is supported for encryption by the SDK. |
| 32 | * |
| 33 | * @param string $policy The name of a key commitment policy to verify is registered. |
| 34 | * |
| 35 | * @return bool If the key commitment policy passed is in our supported list. |
| 36 | */ |
| 37 | public static function isSupportedKeyCommitmentPolicy(string $policy): bool |
| 38 | { |
| 39 | return in_array($policy, AbstractCryptoClientV3::KEY_COMMITMENT_POLICIES, strict: true); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Returns if the passed cipher name is supported for encryption by the SDK. |
| 44 | * |
| 45 | * @param string $cipherName The name of a cipher to verify is registered. |
| 46 | * |
| 47 | * @return bool If the cipher passed is in our supported list. |
| 48 | */ |
| 49 | public static function isSupportedCipher(string $cipherName): bool |
| 50 | { |
| 51 | return in_array($cipherName, self::$supportedCiphers, true); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns an identifier recognizable by `openssl_*` functions, such as |
| 56 | * `aes-256-gcm` |
| 57 | * |
| 58 | * @param string $cipherName Name of the cipher being used for encrypting |
| 59 | * or decrypting. |
| 60 | * @param int $keySize Size of the encryption key, in bits, that will be |
| 61 | * used. |
| 62 | * |
| 63 | * @return string |
| 64 | */ |
| 65 | abstract protected function getCipherOpenSslName( |
| 66 | $cipherName, |
| 67 | $keySize |
| 68 | ); |
| 69 | |
| 70 | /** |
| 71 | * Constructs a CipherMethod for the given name, initialized with the other |
| 72 | * data passed for use in encrypting or decrypting. |
| 73 | * |
| 74 | * @param string $cipherName Name of the cipher to generate for encrypting. |
| 75 | * @param string $iv Base Initialization Vector for the cipher. |
| 76 | * @param int $keySize Size of the encryption key, in bits, that will be |
| 77 | * used. |
| 78 | * |
| 79 | * @return CipherMethod |
| 80 | * |
| 81 | * @internal |
| 82 | */ |
| 83 | abstract protected function buildCipherMethod( |
| 84 | $cipherName, |
| 85 | $iv, |
| 86 | $keySize |
| 87 | ); |
| 88 | |
| 89 | /** |
| 90 | * Performs a reverse lookup to get the openssl_* cipher name from the |
| 91 | * AESName passed in from the MetadataEnvelope. |
| 92 | * |
| 93 | * @param string $aesName |
| 94 | * |
| 95 | * @return string |
| 96 | * |
| 97 | * @internal |
| 98 | */ |
| 99 | abstract protected function getCipherFromAesName($aesName); |
| 100 | |
| 101 | /** |
| 102 | * Dependency to provide an interface for building an encryption stream for |
| 103 | * data given cipher details, metadata, and materials to do so. |
| 104 | * |
| 105 | * @param Stream $plaintext Plain-text data to be encrypted using the |
| 106 | * materials, algorithm, and data provided. |
| 107 | * @param AlgorithmSuite $algorithmSuite AlgorithmSuite for use in encryption. |
| 108 | * @param array $options Options for use in encryption, including cipher |
| 109 | * options, and encryption context. |
| 110 | * @param MaterialsProviderV3 $provider A provider to supply and encrypt |
| 111 | * materials used in encryption. |
| 112 | * @param MetadataEnvelope $envelope A storage envelope for encryption |
| 113 | * metadata to be added to. |
| 114 | * |
| 115 | * @return AppendStream |
| 116 | * |
| 117 | * @internal |
| 118 | */ |
| 119 | abstract public function encrypt( |
| 120 | Stream $plaintext, |
| 121 | AlgorithmSuite $algorithmSuite, |
| 122 | array $options, |
| 123 | MaterialsProviderV3 $provider, |
| 124 | MetadataEnvelope $envelope |
| 125 | ): AppendStream; |
| 126 | |
| 127 | /** |
| 128 | * Dependency to provide an interface for building a decryption stream for |
| 129 | * cipher text given metadata and materials to do so. |
| 130 | * |
| 131 | * @param string $cipherText Plain-text data to be decrypted using the |
| 132 | * materials, algorithm, and data provided. |
| 133 | * @param MaterialsProviderInterface $provider A provider to supply and encrypt |
| 134 | * materials used in encryption. |
| 135 | * @param MetadataEnvelope $envelope A storage envelope for encryption |
| 136 | * metadata to be read from. |
| 137 | * @param string $commitmentPolicy Commitment Policy to use for decrypting objects. |
| 138 | * @param array $options Options used for decryption. |
| 139 | * |
| 140 | * @return AesStreamInterface |
| 141 | * |
| 142 | * @internal |
| 143 | */ |
| 144 | abstract public function decrypt( |
| 145 | string $cipherText, |
| 146 | MaterialsProviderInterfaceV3 $provider, |
| 147 | MetadataEnvelope $envelope, |
| 148 | string $commitmentPolicy, |
| 149 | array $options = [] |
| 150 | ): AesStreamInterface; |
| 151 | } |