|
| 1 | +/* |
| 2 | + * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use |
| 5 | + * this file except in compliance with the License. A copy of the License is |
| 6 | + * located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0/ |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed on an |
| 11 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | + * implied. See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +import { |
| 17 | + KmsKeyring, |
| 18 | + KmsKeyringInput, // eslint-disable-line no-unused-vars |
| 19 | + KMSConstructible, // eslint-disable-line no-unused-vars |
| 20 | + KmsClientSupplier, // eslint-disable-line no-unused-vars |
| 21 | + getClient, |
| 22 | + limitRegions, |
| 23 | + excludeRegions, |
| 24 | + cacheClients |
| 25 | +} from '@aws-crypto/kms-keyring' |
| 26 | +import { |
| 27 | + WebCryptoAlgorithmSuite, // eslint-disable-line no-unused-vars |
| 28 | + WebCryptoEncryptionMaterial, // eslint-disable-line no-unused-vars |
| 29 | + WebCryptoDecryptionMaterial, // eslint-disable-line no-unused-vars |
| 30 | + EncryptionContext, // eslint-disable-line no-unused-vars |
| 31 | + EncryptedDataKey, // eslint-disable-line no-unused-vars |
| 32 | + immutableClass, |
| 33 | + importCryptoKey |
| 34 | +} from '@aws-crypto/material-management-browser' |
| 35 | +import { getWebCryptoBackend } from '@aws-crypto/web-crypto-backend' |
| 36 | +import { KMS, KMSConfiguration } from '@aws-sdk/client-kms-browser' // eslint-disable-line no-unused-vars |
| 37 | + |
| 38 | +const getKmsClient = getClient(KMS) |
| 39 | +const cacheKmsClients = cacheClients(getKmsClient) |
| 40 | + |
| 41 | +export type KmsKeyringWebCryptoInput = Partial<KmsKeyringInput<KMS>> |
| 42 | +export type KMSWebCryptoConstructible = KMSConstructible<KMS, KMSConfiguration> |
| 43 | +export type KmsWebCryptoClientSupplier = KmsClientSupplier<KMS> |
| 44 | + |
| 45 | +export class KmsKeyringNode extends KmsKeyring<WebCryptoAlgorithmSuite, KMS> { |
| 46 | + constructor ({ |
| 47 | + clientProvider = cacheKmsClients, |
| 48 | + kmsKeys, |
| 49 | + generatorKmsKey, |
| 50 | + grantTokens |
| 51 | + }: KmsKeyringWebCryptoInput) { |
| 52 | + super({ clientProvider, kmsKeys, generatorKmsKey, grantTokens }) |
| 53 | + } |
| 54 | + |
| 55 | + async _onEncrypt (material: WebCryptoEncryptionMaterial, context?: EncryptionContext) { |
| 56 | + const _material = await super._onEncrypt(material, context) |
| 57 | + |
| 58 | + /* Check for early return (Postcondition): If a cryptoKey has already been imported, return. */ |
| 59 | + if (_material.hasUnencryptedDataKey && _material.hasCryptoKey) { |
| 60 | + return _material |
| 61 | + } |
| 62 | + |
| 63 | + const backend = await getWebCryptoBackend() |
| 64 | + const cryptoKey = await importCryptoKey(backend, _material) |
| 65 | + // The trace is only set when the material does not already have |
| 66 | + // an hasUnencryptedDataKey. This is an implementation detail :( |
| 67 | + const [trace] = _material.keyringTrace |
| 68 | + |
| 69 | + return _material.setCryptoKey(cryptoKey, trace) |
| 70 | + } |
| 71 | + |
| 72 | + async _onDecrypt (material: WebCryptoDecryptionMaterial, encryptedDataKeys: EncryptedDataKey[], context?: EncryptionContext) { |
| 73 | + const _material = await super._onDecrypt(material, encryptedDataKeys, context) |
| 74 | + |
| 75 | + /* Check for early return (Postcondition): If a cryptoKey has already been imported, return. */ |
| 76 | + if (_material.hasUnencryptedDataKey && _material.hasCryptoKey) { |
| 77 | + return _material |
| 78 | + } |
| 79 | + |
| 80 | + const backend = await getWebCryptoBackend() |
| 81 | + const cryptoKey = await importCryptoKey(backend, _material) |
| 82 | + // Now that a cryptoKey has been imported, the unencrypted data key can be zeroed. |
| 83 | + _material.zeroUnencryptedDataKey() |
| 84 | + // The trace is only set when the material does not already have |
| 85 | + // an hasUnencryptedDataKey. This is an implementation detail :( |
| 86 | + const [trace] = _material.keyringTrace |
| 87 | + |
| 88 | + return _material.setCryptoKey(cryptoKey, trace) |
| 89 | + } |
| 90 | +} |
| 91 | +immutableClass(KmsKeyringNode) |
| 92 | + |
| 93 | +export { getClient, cacheKmsClients, limitRegions, excludeRegions, cacheClients } |
0 commit comments