Skip to content

Commit 0f4dd7e

Browse files
authored
fix: material-management should not export DOM types (#147)
resolves #137 Cryptographic materials need to understand CryptoKeys to insure that a given algorithm suite is satisfied. However, by exporting the DOM types in function calls, all dependent modules must import DOM types. This pollution is not needed, especially for node packages. Pull the type structure into the module and export that interface. Update raw_aes_materials to use new types.
1 parent 36d6cba commit 0f4dd7e

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

modules/material-management/src/cryptographic_material.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
import { MixedBackendCryptoKey, SupportedAlgorithmSuites } from './types' // eslint-disable-line no-unused-vars
16+
import { MixedBackendCryptoKey, SupportedAlgorithmSuites, AwsEsdkJsCryptoKey, AwsEsdkJsKeyUsage } from './types' // eslint-disable-line no-unused-vars
1717
import { EncryptedDataKey } from './encrypted_data_key'
1818
import { SignatureKey, VerificationKey } from './signature_key'
1919
import { frozenClass, readOnlyProperty } from './immutable_class'
@@ -97,10 +97,10 @@ export interface DecryptionMaterial<T extends CryptographicMaterial<T>> extends
9797
}
9898

9999
export interface WebCryptoMaterial<T extends CryptographicMaterial<T>> extends CryptographicMaterial<T> {
100-
setCryptoKey: (dataKey: CryptoKey|MixedBackendCryptoKey, trace: KeyringTrace) => T
101-
getCryptoKey: () => CryptoKey|MixedBackendCryptoKey
100+
setCryptoKey: (dataKey: AwsEsdkJsCryptoKey|MixedBackendCryptoKey, trace: KeyringTrace) => T
101+
getCryptoKey: () => AwsEsdkJsCryptoKey|MixedBackendCryptoKey
102102
hasCryptoKey: boolean
103-
validUsages: ReadonlyArray<KeyUsage>
103+
validUsages: ReadonlyArray<AwsEsdkJsKeyUsage>
104104
}
105105

106106
export class NodeEncryptionMaterial implements
@@ -178,15 +178,15 @@ export class WebCryptoEncryptionMaterial implements
178178
addEncryptedDataKey!: (edk: EncryptedDataKey, flags: KeyringTraceFlag) => WebCryptoEncryptionMaterial
179179
setSignatureKey!: (key: SignatureKey) => WebCryptoEncryptionMaterial
180180
signatureKey?: SignatureKey
181-
setCryptoKey!: (dataKey: CryptoKey|MixedBackendCryptoKey, trace: KeyringTrace) => WebCryptoEncryptionMaterial
182-
getCryptoKey!: () => CryptoKey|MixedBackendCryptoKey
181+
setCryptoKey!: (dataKey: AwsEsdkJsCryptoKey|MixedBackendCryptoKey, trace: KeyringTrace) => WebCryptoEncryptionMaterial
182+
getCryptoKey!: () => AwsEsdkJsCryptoKey|MixedBackendCryptoKey
183183
hasCryptoKey!: boolean
184-
validUsages: ReadonlyArray<KeyUsage>
184+
validUsages: ReadonlyArray<AwsEsdkJsKeyUsage>
185185
constructor (suite: WebCryptoAlgorithmSuite) {
186186
/* Precondition: WebCryptoEncryptionMaterial suite must be WebCryptoAlgorithmSuite. */
187187
needs(suite instanceof WebCryptoAlgorithmSuite, 'Suite must be a WebCryptoAlgorithmSuite')
188188
this.suite = suite
189-
this.validUsages = Object.freeze(<KeyUsage[]>['deriveKey', 'encrypt'])
189+
this.validUsages = Object.freeze(<AwsEsdkJsKeyUsage[]>['deriveKey', 'encrypt'])
190190
// EncryptionMaterial have generated a data key on setUnencryptedDataKey
191191
const setFlag = KeyringTraceFlag.WRAPPING_KEY_GENERATED_DATA_KEY
192192
decorateCryptographicMaterial<WebCryptoEncryptionMaterial>(this, setFlag)
@@ -214,15 +214,15 @@ export class WebCryptoDecryptionMaterial implements
214214
keyringTrace: KeyringTrace[] = []
215215
setVerificationKey!: (key: VerificationKey) => WebCryptoDecryptionMaterial
216216
verificationKey?: VerificationKey
217-
setCryptoKey!: (dataKey: CryptoKey|MixedBackendCryptoKey, trace: KeyringTrace) => WebCryptoDecryptionMaterial
218-
getCryptoKey!: () => CryptoKey|MixedBackendCryptoKey
217+
setCryptoKey!: (dataKey: AwsEsdkJsCryptoKey|MixedBackendCryptoKey, trace: KeyringTrace) => WebCryptoDecryptionMaterial
218+
getCryptoKey!: () => AwsEsdkJsCryptoKey|MixedBackendCryptoKey
219219
hasCryptoKey!: boolean
220-
validUsages: ReadonlyArray<KeyUsage>
220+
validUsages: ReadonlyArray<AwsEsdkJsKeyUsage>
221221
constructor (suite: WebCryptoAlgorithmSuite) {
222222
/* Precondition: WebCryptoDecryptionMaterial suite must be WebCryptoAlgorithmSuite. */
223223
needs(suite instanceof WebCryptoAlgorithmSuite, 'Suite must be a WebCryptoAlgorithmSuite')
224224
this.suite = suite
225-
this.validUsages = Object.freeze(<KeyUsage[]>['deriveKey', 'decrypt'])
225+
this.validUsages = Object.freeze(<AwsEsdkJsKeyUsage[]>['deriveKey', 'decrypt'])
226226
// DecryptionMaterial have decrypted a data key on setUnencryptedDataKey
227227
const setFlag = KeyringTraceFlag.WRAPPING_KEY_DECRYPTED_DATA_KEY
228228
decorateCryptographicMaterial<WebCryptoDecryptionMaterial>(this, setFlag)
@@ -460,9 +460,9 @@ export function decorateDecryptionMaterial<T extends DecryptionMaterial<T>> (mat
460460
}
461461

462462
export function decorateWebCryptoMaterial<T extends WebCryptoMaterial<T>> (material: T, setFlags: KeyringTraceFlag) {
463-
let cryptoKey: Readonly<CryptoKey|MixedBackendCryptoKey>|undefined
463+
let cryptoKey: Readonly<AwsEsdkJsCryptoKey|MixedBackendCryptoKey>|undefined
464464

465-
const setCryptoKey = (dataKey: CryptoKey|MixedBackendCryptoKey, trace: KeyringTrace) => {
465+
const setCryptoKey = (dataKey: AwsEsdkJsCryptoKey|MixedBackendCryptoKey, trace: KeyringTrace) => {
466466
/* Precondition: cryptoKey must not be set. Modifying the cryptoKey is denied */
467467
needs(!cryptoKey, 'cryptoKey is already set.')
468468
/* Precondition: dataKey must be a supported type. */
@@ -504,7 +504,7 @@ export function decorateWebCryptoMaterial<T extends WebCryptoMaterial<T>> (mater
504504
needs(cryptoKey, 'Crypto key is not set.')
505505
// In the case of MixedBackendCryptoKey the object
506506
// has already been frozen above so it is safe to return
507-
return <Readonly<CryptoKey|MixedBackendCryptoKey>>cryptoKey
507+
return <Readonly<AwsEsdkJsCryptoKey|MixedBackendCryptoKey>>cryptoKey
508508
}
509509
readOnlyProperty(material, 'getCryptoKey', getCryptoKey)
510510

@@ -516,7 +516,7 @@ export function decorateWebCryptoMaterial<T extends WebCryptoMaterial<T>> (mater
516516
return material
517517
}
518518

519-
export function isCryptoKey (dataKey: any): dataKey is CryptoKey {
519+
export function isCryptoKey (dataKey: any): dataKey is AwsEsdkJsCryptoKey {
520520
return dataKey &&
521521
'algorithm' in dataKey &&
522522
'type' in dataKey &&
@@ -526,7 +526,7 @@ export function isCryptoKey (dataKey: any): dataKey is CryptoKey {
526526
}
527527

528528
export function isValidCryptoKey<T extends WebCryptoMaterial<T>> (
529-
dataKey: CryptoKey|MixedBackendCryptoKey,
529+
dataKey: AwsEsdkJsCryptoKey|MixedBackendCryptoKey,
530530
material: T
531531
) : boolean {
532532
if (!isCryptoKey(dataKey)) {
@@ -565,7 +565,7 @@ function isMixedBackendCryptoKey (dataKey: any): dataKey is MixedBackendCryptoKe
565565
return isCryptoKey(zeroByteCryptoKey) && isCryptoKey(nonZeroByteCryptoKey)
566566
}
567567

568-
export function keyUsageForMaterial<T extends WebCryptoMaterial<T>> (material: T): KeyUsage {
568+
export function keyUsageForMaterial<T extends WebCryptoMaterial<T>> (material: T): AwsEsdkJsKeyUsage {
569569
const { suite } = material
570570
if (suite.kdf) return 'deriveKey'
571571
return subtleFunctionForMaterial(material)

modules/material-management/src/signature_key.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { encodeNamedCurves } from './ecc_encode'
1818
import { decodeNamedCurves } from './ecc_decode'
1919
import { frozenClass, readOnlyBinaryProperty, readOnlyProperty } from './immutable_class'
2020
import { publicKeyPem, privateKeyPem } from './pem_helpers'
21+
import { AwsEsdkJsCryptoKey } from './types' // eslint-disable-line no-unused-vars
2122

2223
/*
2324
* This public interface to the SignatureKey object is provided for
@@ -27,10 +28,10 @@ import { publicKeyPem, privateKeyPem } from './pem_helpers'
2728
*/
2829

2930
export class SignatureKey {
30-
public readonly privateKey!: string|CryptoKey
31+
public readonly privateKey!: string|AwsEsdkJsCryptoKey
3132
public readonly compressPoint!: Uint8Array
3233
public readonly signatureCurve!: NodeECDHCurve|WebCryptoECDHCurve
33-
constructor (privateKey: Uint8Array|CryptoKey, compressPoint: Uint8Array, suite: AlgorithmSuite) {
34+
constructor (privateKey: Uint8Array|AwsEsdkJsCryptoKey, compressPoint: Uint8Array, suite: AlgorithmSuite) {
3435
const { signatureCurve: namedCurve } = suite
3536
/* Precondition: Do not create a SignatureKey for an algorithm suite that does not have an EC named curve. */
3637
if (!namedCurve) throw new Error('Unsupported Algorithm')
@@ -63,9 +64,9 @@ export class SignatureKey {
6364
frozenClass(SignatureKey)
6465

6566
export class VerificationKey {
66-
public readonly publicKey!: string|CryptoKey
67+
public readonly publicKey!: string|AwsEsdkJsCryptoKey
6768
public readonly signatureCurve!: NodeECDHCurve|WebCryptoECDHCurve
68-
constructor (publicKey: Uint8Array|CryptoKey, suite: AlgorithmSuite) {
69+
constructor (publicKey: Uint8Array|AwsEsdkJsCryptoKey, suite: AlgorithmSuite) {
6970
const { signatureCurve: namedCurve } = suite
7071
/* Precondition: Do not create a VerificationKey for an algorithm suite that does not have an EC named curve. */
7172
if (!namedCurve) throw new Error('Unsupported Algorithm')

modules/material-management/src/types.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,23 @@ import {
2323

2424
export type EncryptionContext = {[index: string]: string}
2525

26+
/* need to copy some things from DOM */
27+
export interface AwsEsdkJsKeyAlgorithm {
28+
name: string
29+
}
30+
export type AwsEsdkJsKeyType = 'public' | 'private' | 'secret'
31+
export type AwsEsdkJsKeyUsage = 'encrypt' | 'decrypt' | 'sign' | 'verify' | 'deriveKey' | 'deriveBits' | 'wrapKey' | 'unwrapKey'
32+
33+
export interface AwsEsdkJsCryptoKey {
34+
readonly algorithm: AwsEsdkJsKeyAlgorithm
35+
readonly extractable: boolean
36+
readonly type: AwsEsdkJsKeyType
37+
readonly usages: AwsEsdkJsKeyUsage[]
38+
}
39+
2640
export type MixedBackendCryptoKey = {
27-
nonZeroByteCryptoKey: CryptoKey
28-
zeroByteCryptoKey: CryptoKey
41+
nonZeroByteCryptoKey: AwsEsdkJsCryptoKey
42+
zeroByteCryptoKey: AwsEsdkJsCryptoKey
2943
}
3044

3145
export interface EncryptionRequest<S extends NodeAlgorithmSuite|WebCryptoAlgorithmSuite> {

modules/raw-keyring/src/raw_aes_material.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import {
2929
frozenClass,
3030
NodeAlgorithmSuite,
3131
WebCryptoAlgorithmSuite,
32+
AwsEsdkJsCryptoKey, // eslint-disable-line no-unused-vars
33+
AwsEsdkJsKeyUsage, // eslint-disable-line no-unused-vars
3234
KeyringTrace, // eslint-disable-line no-unused-vars
3335
KeyringTraceFlag,
3436
needs
@@ -77,15 +79,15 @@ export class WebCryptoRawAesMaterial implements
7779
hasUnencryptedDataKey!: boolean
7880
unencryptedDataKeyLength!: number
7981
keyringTrace: KeyringTrace[] = []
80-
setCryptoKey!: (dataKey: CryptoKey|MixedBackendCryptoKey, trace: KeyringTrace) => WebCryptoRawAesMaterial
81-
getCryptoKey!: () => CryptoKey|MixedBackendCryptoKey
82+
setCryptoKey!: (dataKey: AwsEsdkJsCryptoKey|MixedBackendCryptoKey, trace: KeyringTrace) => WebCryptoRawAesMaterial
83+
getCryptoKey!: () => AwsEsdkJsCryptoKey|MixedBackendCryptoKey
8284
hasCryptoKey!: boolean
83-
validUsages: ReadonlyArray<KeyUsage>
85+
validUsages: ReadonlyArray<AwsEsdkJsKeyUsage>
8486
constructor (suiteId: WrappingSuiteIdentifier) {
8587
/* Precondition: WebCryptoAlgorithmSuite suiteId must be RawAesWrappingSuiteIdentifier. */
8688
needs(RawAesWrappingSuiteIdentifier[suiteId], 'suiteId not supported.')
8789
this.suite = new WebCryptoAlgorithmSuite(suiteId)
88-
this.validUsages = Object.freeze([<KeyUsage>'decrypt', <KeyUsage>'encrypt'])
90+
this.validUsages = Object.freeze([<AwsEsdkJsKeyUsage>'decrypt', <AwsEsdkJsKeyUsage>'encrypt'])
8991
/* WebCryptoRawAesMaterial need to set a flag, this is an abuse of TraceFlags
9092
* because the material is not generated.
9193
* but CryptographicMaterial force a flag to be set.

0 commit comments

Comments
 (0)