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