Skip to content

Commit 65c5c2a

Browse files
committed
feat: Change to AwsEsdkJsCryptoKey
Regarding aws#237, the shape of the CryptoKey when dealing with a fallback may slightly differ. By changing all references to AwsEsdkJsCryptoKey I can control any type mismatch.
1 parent c901318 commit 65c5c2a

File tree

6 files changed

+22
-17
lines changed

6 files changed

+22
-17
lines changed

modules/material-management-browser/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ export {
2222
AlgorithmSuiteIdentifier, EncryptionContext, EncryptedDataKey, KeyringWebCrypto,
2323
KeyringTrace, KeyringTraceFlag, needs, MixedBackendCryptoKey, MultiKeyringWebCrypto,
2424
immutableBaseClass, immutableClass, frozenClass, readOnlyProperty, keyUsageForMaterial,
25-
isValidCryptoKey, isCryptoKey, WebCryptoMaterialsManager, unwrapDataKey
25+
isValidCryptoKey, isCryptoKey, WebCryptoMaterialsManager, unwrapDataKey, AwsEsdkJsCryptoKey
2626
} from '@aws-crypto/material-management'

modules/material-management-browser/src/material_helpers.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
keyUsageForMaterial,
2424
subtleFunctionForMaterial,
2525
unwrapDataKey,
26+
AwsEsdkJsCryptoKey, // eslint-disable-line no-unused-vars
2627
WebCryptoMaterial // eslint-disable-line no-unused-vars
2728
} from '@aws-crypto/material-management'
2829

@@ -155,7 +156,7 @@ export function getSubtleFunction<T extends WebCryptoMaterial<T>> (
155156
const { encryption: cipherName, ivLength, tagLength } = suite
156157

157158
return (info: Uint8Array) => {
158-
const derivedKeyPromise: Promise<CryptoKey|MixedBackendCryptoKey> = isCryptoKey(cryptoKey)
159+
const derivedKeyPromise: Promise<AwsEsdkJsCryptoKey|MixedBackendCryptoKey> = isCryptoKey(cryptoKey)
159160
? WebCryptoKdf(getNonZeroByteBackend(backend), material, cryptoKey, [subtleFunction], info)
160161
: Promise.all([
161162
WebCryptoKdf(getNonZeroByteBackend(backend), material, cryptoKey.nonZeroByteCryptoKey, [subtleFunction], info),
@@ -190,7 +191,7 @@ export function getSubtleFunction<T extends WebCryptoMaterial<T>> (
190191
export async function WebCryptoKdf<T extends WebCryptoMaterial<T>> (
191192
subtle: SubtleCrypto,
192193
material: T,
193-
cryptoKey: CryptoKey,
194+
cryptoKey: AwsEsdkJsCryptoKey,
194195
keyUsages: SubtleFunction[],
195196
info: Uint8Array
196197
): Promise<CryptoKey> {
@@ -244,7 +245,7 @@ export async function _importCryptoKey<T extends WebCryptoMaterial<T>> (
244245
subtle: SubtleCrypto,
245246
material: T,
246247
keyUsages: KeyUsage[] = [keyUsageForMaterial(material)]
247-
) {
248+
): Promise<AwsEsdkJsCryptoKey> {
248249
const { suite } = material
249250
const extractable = false
250251
const udk = unwrapDataKey(material.getUnencryptedDataKey())

modules/raw-aes-keyring-browser/src/raw_aes_keyring_browser.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import {
2727
_importCryptoKey,
2828
unwrapDataKey,
2929
importForWebCryptoEncryptionMaterial,
30-
importForWebCryptoDecryptionMaterial
30+
importForWebCryptoDecryptionMaterial,
31+
AwsEsdkJsCryptoKey // eslint-disable-line no-unused-vars
3132
} from '@aws-crypto/material-management-browser'
3233
import {
3334
serializeFactory,
@@ -55,7 +56,7 @@ const decryptFlags = KeyringTraceFlag.WRAPPING_KEY_DECRYPTED_DATA_KEY | KeyringT
5556
export type RawAesKeyringWebCryptoInput = {
5657
keyNamespace: string
5758
keyName: string
58-
masterKey: CryptoKey,
59+
masterKey: AwsEsdkJsCryptoKey,
5960
wrappingSuite: WrappingSuiteIdentifier
6061
}
6162

@@ -125,7 +126,7 @@ export class RawAesKeyringWebCrypto extends KeyringWebCrypto {
125126
*/
126127
_onDecrypt = _onDecrypt<WebCryptoAlgorithmSuite, RawAesKeyringWebCrypto>()
127128

128-
static async importCryptoKey (masterKey: Uint8Array, wrappingSuite: WrappingSuiteIdentifier) {
129+
static async importCryptoKey (masterKey: Uint8Array, wrappingSuite: WrappingSuiteIdentifier): Promise<AwsEsdkJsCryptoKey> {
129130
needs(masterKey instanceof Uint8Array, 'Unsupported master key type.')
130131
const material = new WebCryptoRawAesMaterial(wrappingSuite)
131132
/* Precondition: masterKey must correspond to the algorithm suite specification.

modules/raw-rsa-keyring-browser/src/get_import_options.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import {
2525
import {
2626
MixedBackendCryptoKey, // eslint-disable-line no-unused-vars
2727
needs,
28-
isCryptoKey
28+
isCryptoKey,
29+
AwsEsdkJsCryptoKey // eslint-disable-line no-unused-vars
2930
} from '@aws-crypto/material-management-browser'
3031

3132
type WebCryptoRsaName = keyof typeof JsonWebKeyRsaAlg
@@ -80,7 +81,7 @@ export function getImportOptions (keyInfo: RsaImportableKey) {
8081
throw new Error('Unsupported RsaImportableKey')
8182
}
8283

83-
export function getWrappingAlgorithm (publicKey?: CryptoKey, privateKey?: CryptoKey|MixedBackendCryptoKey) {
84+
export function getWrappingAlgorithm (publicKey?: AwsEsdkJsCryptoKey, privateKey?: AwsEsdkJsCryptoKey|MixedBackendCryptoKey) {
8485
const privateKeys = flattenMixedCryptoKey(privateKey)
8586
if (publicKey && privateKeys.length) {
8687
return verify(...[publicKey, ...privateKeys].map(extract))
@@ -92,7 +93,7 @@ export function getWrappingAlgorithm (publicKey?: CryptoKey, privateKey?: Crypto
9293
throw new Error('No Key provided.')
9394
}
9495

95-
export function extract (key: CryptoKey): RsaWrappingKeyAlgorithm {
96+
export function extract (key: AwsEsdkJsCryptoKey): RsaWrappingKeyAlgorithm {
9697
const { algorithm } = key
9798
// @ts-ignore
9899
const { name, hash } = algorithm
@@ -122,7 +123,7 @@ export function verify (...args: RsaWrappingKeyAlgorithm[]) {
122123
}
123124
}
124125

125-
export function flattenMixedCryptoKey (key?: CryptoKey|MixedBackendCryptoKey): CryptoKey[] {
126+
export function flattenMixedCryptoKey (key?: AwsEsdkJsCryptoKey|MixedBackendCryptoKey): AwsEsdkJsCryptoKey[] {
126127
/* Check for early return (Postcondition): empty inputs should return an empty array. */
127128
if (!key) return []
128129
if (isCryptoKey(key)) return [key]

modules/raw-rsa-keyring-browser/src/raw_rsa_keyring_web_crypto.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import {
2828
importForWebCryptoEncryptionMaterial,
2929
unwrapDataKey,
3030
MixedBackendCryptoKey, // eslint-disable-line no-unused-vars
31-
WebCryptoAlgorithmSuite // eslint-disable-line no-unused-vars
31+
WebCryptoAlgorithmSuite, // eslint-disable-line no-unused-vars
32+
AwsEsdkJsCryptoKey // eslint-disable-line no-unused-vars
3233
} from '@aws-crypto/material-management-browser'
3334

3435
import {
@@ -168,14 +169,14 @@ export class RawRsaKeyringWebCrypto extends KeyringWebCrypto {
168169
*/
169170
_onDecrypt = _onDecrypt<WebCryptoAlgorithmSuite, RawRsaKeyringWebCrypto>()
170171

171-
static async importPublicKey (publicKey: RsaImportableKey): Promise<CryptoKey> {
172+
static async importPublicKey (publicKey: RsaImportableKey): Promise<AwsEsdkJsCryptoKey> {
172173
const { wrappingAlgorithm, format, key } = getImportOptions(publicKey)
173174
const backend = await getWebCryptoBackend()
174175
const subtle = getNonZeroByteBackend(backend)
175176
return subtle.importKey(format, key, wrappingAlgorithm, false, ['wrapKey'])
176177
}
177178

178-
static async importPrivateKey (privateKey: RsaImportableKey): Promise<CryptoKey|MixedBackendCryptoKey> {
179+
static async importPrivateKey (privateKey: RsaImportableKey): Promise<AwsEsdkJsCryptoKey|MixedBackendCryptoKey> {
179180
const { wrappingAlgorithm, format, key } = getImportOptions(privateKey)
180181
const backend = await getWebCryptoBackend()
181182

modules/raw-rsa-keyring-browser/src/types.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
*/
1515

1616
import {
17-
MixedBackendCryptoKey // eslint-disable-line no-unused-vars
17+
MixedBackendCryptoKey, // eslint-disable-line no-unused-vars
18+
AwsEsdkJsCryptoKey // eslint-disable-line no-unused-vars
1819
} from '@aws-crypto/material-management-browser'
1920

2021
export enum RsaPadding {
@@ -69,6 +70,6 @@ export type RsaImportableKey = RsaJsonWebKey | BinaryKey
6970
export type RawRsaKeyringWebCryptoInput = {
7071
keyNamespace: string
7172
keyName: string
72-
privateKey?: CryptoKey|MixedBackendCryptoKey
73-
publicKey?: CryptoKey
73+
privateKey?: AwsEsdkJsCryptoKey|MixedBackendCryptoKey
74+
publicKey?: AwsEsdkJsCryptoKey
7475
}

0 commit comments

Comments
 (0)