|
| 1 | +/* |
| 2 | + * Copyright 2019 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 | +/* This is a simple example of using a KMS Keyring |
| 17 | + * to encrypt and decrypt using the AWS Encryption SDK for Javascript in a browser. |
| 18 | + */ |
| 19 | + |
| 20 | +import { encrypt } from '@aws-crypto/encrypt-browser' |
| 21 | +import { decrypt } from '@aws-crypto/decrypt-browser' |
| 22 | +import { |
| 23 | + KmsKeyringBrowser, |
| 24 | + KMS, |
| 25 | + getClient |
| 26 | +} from '@aws-crypto/kms-keyring-browser' |
| 27 | +import { toBase64 } from '@aws-sdk/util-base64-browser' |
| 28 | + |
| 29 | +/* This is injected by webpack. |
| 30 | + * The webpack.DefinePlugin will replace the values when bundling. |
| 31 | + * The credential values are pulled from @aws-sdk/credential-provider-node |
| 32 | + * Use any method you like to get credentials into the browser. |
| 33 | + * See kms.webpack.config |
| 34 | + */ |
| 35 | +declare const AWS_CREDENTIALS: {accessKeyId: string, secretAccessKey:string } |
| 36 | + |
| 37 | +;(async function kmsSimpleExample () { |
| 38 | + /* A KMS CMK to generate the data key is required. |
| 39 | + * Access to KMS generateDataKey is required for the generatorKeyId. |
| 40 | + */ |
| 41 | + const generatorKeyId = 'arn:aws:kms:us-west-2:658956600833:alias/EncryptDecrypt' |
| 42 | + |
| 43 | + /* Adding Alternate KMS keys that can decrypt. |
| 44 | + * Access to KMS encrypt is required for every CMK in keyIds. |
| 45 | + * Often this used to have a local CMK in multiple regions. |
| 46 | + * In this example, I am using the same CMK. |
| 47 | + * This is *only* to demonstrate how the CMK ARN's are configured. |
| 48 | + */ |
| 49 | + const keyIds = ['arn:aws:kms:us-west-2:658956600833:key/b3537ef1-d8dc-4780-9f5a-55776cbb2f7f'] |
| 50 | + |
| 51 | + /* Need a client provider that will inject correct credentials. |
| 52 | + * The credentials here are injected by webpack from your environment bundle is created |
| 53 | + * The credential values are pulled using @aws-sdk/credential-provider-node. |
| 54 | + * See kms.webpack.config |
| 55 | + * You should inject your credential into the browser in a secure manner, |
| 56 | + * that works with your application. |
| 57 | + */ |
| 58 | + const { accessKeyId, secretAccessKey } = AWS_CREDENTIALS |
| 59 | + |
| 60 | + /* getClient takes a KMS client constructor |
| 61 | + * and optional configuration values. |
| 62 | + * The credentials can be injected here, |
| 63 | + * because browser do not have a standard credential discover process the way Node.js does. |
| 64 | + */ |
| 65 | + const clientProvider = getClient(KMS, { |
| 66 | + credentials: { |
| 67 | + accessKeyId, |
| 68 | + secretAccessKey |
| 69 | + } |
| 70 | + }) |
| 71 | + |
| 72 | + /* The KMS Keyring must be configured with the desired CMK's */ |
| 73 | + const keyring = new KmsKeyringBrowser({ clientProvider, generatorKeyId, keyIds }) |
| 74 | + |
| 75 | + /* Encryption Context is a *very* powerful tool for controlling and managing access. |
| 76 | + * It is ***not*** secret! |
| 77 | + * Remember encrypted data is opaque, encryption context will help your run time checking. |
| 78 | + * Just because you have decrypted a JSON file, and it successfully parsed, |
| 79 | + * does not mean it is the intended JSON file. |
| 80 | + */ |
| 81 | + const context = { |
| 82 | + stage: 'demo', |
| 83 | + purpose: 'simple demonstration app', |
| 84 | + origin: 'us-west-2' |
| 85 | + } |
| 86 | + |
| 87 | + /* I need something to encrypt. */ |
| 88 | + const plainText = new Uint8Array([1, 2, 3, 4, 5]) |
| 89 | + |
| 90 | + /* Encrypt the data. */ |
| 91 | + const { cipherMessage } = await encrypt(keyring, plainText, { encryptionContext: context }) |
| 92 | + |
| 93 | + /* Log the plain text. */ |
| 94 | + console.log('plainText:', plainText) |
| 95 | + document.write('</br>plainText:' + plainText + '</br>') |
| 96 | + |
| 97 | + /* In case you want to check compatibility, I log the cipher text. */ |
| 98 | + const cipherMessageBase64 = toBase64(cipherMessage) |
| 99 | + console.log(cipherMessageBase64) |
| 100 | + document.write(cipherMessageBase64) |
| 101 | + |
| 102 | + const { clearMessage, messageHeader } = await decrypt(keyring, cipherMessage) |
| 103 | + |
| 104 | + /* Grab the encryption context so I can verify it. */ |
| 105 | + const { encryptionContext } = messageHeader |
| 106 | + |
| 107 | + /* Verify the encryption context. |
| 108 | + * Depending on the Algorithm Suite, the `encryptionContext` _may_ contain additional values. |
| 109 | + * In Signing Algorithm Suites the public verification key is serialized into the `encryptionContext`. |
| 110 | + * So it is best to make sure that all the values that you expect exist as opposed to the reverse. |
| 111 | + */ |
| 112 | + Object |
| 113 | + .entries(context) |
| 114 | + .forEach(([key, value]) => { |
| 115 | + if (encryptionContext[key] !== value) throw new Error('Encryption Context does not match expected values') |
| 116 | + }) |
| 117 | + |
| 118 | + /* Log the clear message. */ |
| 119 | + document.write('</br>Decrypted:' + clearMessage) |
| 120 | + console.log(clearMessage) |
| 121 | +})() |
0 commit comments