Skip to content

Commit 11409c3

Browse files
committed
address feedback
1 parent 1842480 commit 11409c3

File tree

5 files changed

+30
-28
lines changed

5 files changed

+30
-28
lines changed

modules/kms-keyring-node/src/kms_hkeyring_node.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,10 @@ export class KmsHierarchicalKeyRingNode
260260
readOnlyProperty(this, '_cmc', cache)
261261

262262
if (utf8Sorting === undefined) {
263-
utf8Sorting = true
263+
readOnlyProperty(this, '_utf8Sorting', false)
264+
} else {
265+
readOnlyProperty(this, '_utf8Sorting', utf8Sorting)
264266
}
265-
readOnlyProperty(this, '_utf8Sorting', utf8Sorting)
266267

267268
Object.freeze(this)
268269
/* Postcondition: The HKR object must be frozen */

modules/kms-keyring-node/test/kms_hkeyring_node.constructor.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ describe('KmsHierarchicalKeyRingNode: constructor', () => {
287287
keyStore,
288288
cacheLimitTtl,
289289
})._utf8Sorting
290-
).to.equal(true)
290+
).to.equal(false)
291291
})
292292

293293
it('utf8Sorting value is set correctly', () => {

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ import {
1818
importForWebCryptoDecryptionMaterial,
1919
AwsEsdkJsCryptoKey,
2020
} from '@aws-crypto/material-management-browser'
21-
import {
22-
serializeFactory,
23-
concatBuffers,
24-
SerializeOptions,
25-
} from '@aws-crypto/serialize'
21+
import { serializeFactory, concatBuffers } from '@aws-crypto/serialize'
2622
import {
2723
_onEncrypt,
2824
_onDecrypt,
@@ -56,14 +52,15 @@ export type RawAesKeyringWebCryptoInput = {
5652
keyName: string
5753
masterKey: AwsEsdkJsCryptoKey
5854
wrappingSuite: WrappingSuiteIdentifier
59-
utf8Sorting?: boolean | true
55+
utf8Sorting?: boolean
6056
}
6157

6258
export class RawAesKeyringWebCrypto extends KeyringWebCrypto {
6359
public declare keyNamespace: string
6460
public declare keyName: string
6561
declare _wrapKey: WrapKey<WebCryptoAlgorithmSuite>
6662
declare _unwrapKey: UnwrapKey<WebCryptoAlgorithmSuite>
63+
public declare _utf8Sorting: boolean
6764

6865
constructor(input: RawAesKeyringWebCryptoInput) {
6966
super()
@@ -83,12 +80,15 @@ export class RawAesKeyringWebCrypto extends KeyringWebCrypto {
8380
flags: KeyringTraceFlag.WRAPPING_KEY_GENERATED_DATA_KEY,
8481
})
8582

86-
const maybeUtf8Sorting = utf8Sorting ?? true
87-
const serializeOptions: SerializeOptions = { utf8Sorting: maybeUtf8Sorting }
88-
const { serializeEncryptionContext } = serializeFactory(
89-
fromUtf8,
90-
serializeOptions
91-
)
83+
if (utf8Sorting === undefined) {
84+
readOnlyProperty(this, '_utf8Sorting', false)
85+
} else {
86+
readOnlyProperty(this, '_utf8Sorting', utf8Sorting)
87+
}
88+
// default will be false
89+
const { serializeEncryptionContext } = serializeFactory(fromUtf8, {
90+
utf8Sorting: this._utf8Sorting,
91+
})
9292
const _wrapKey = async (material: WebCryptoEncryptionMaterial) => {
9393
/* The AAD section is uInt16BE(length) + AAD
9494
* see: https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/message-format.html#header-aad

modules/raw-aes-keyring-node/src/raw_aes_keyring_node.ts

+5-13
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ import {
1414
NodeAlgorithmSuite,
1515
} from '@aws-crypto/material-management-node'
1616
import { randomBytes, createCipheriv, createDecipheriv } from 'crypto'
17-
import {
18-
serializeFactory,
19-
concatBuffers,
20-
SerializeOptions,
21-
} from '@aws-crypto/serialize'
17+
import { serializeFactory, concatBuffers } from '@aws-crypto/serialize'
2218
import {
2319
_onEncrypt,
2420
_onDecrypt,
@@ -78,18 +74,14 @@ export class RawAesKeyringNode extends KeyringNode {
7874
})
7975

8076
if (utf8Sorting === undefined) {
81-
readOnlyProperty(this, '_utf8Sorting', true)
77+
readOnlyProperty(this, '_utf8Sorting', false)
8278
} else {
8379
readOnlyProperty(this, '_utf8Sorting', utf8Sorting)
8480
}
85-
// default will be true
86-
const serializeOptions: SerializeOptions = {
81+
// default will be false
82+
const { serializeEncryptionContext } = serializeFactory(fromUtf8, {
8783
utf8Sorting: this._utf8Sorting,
88-
}
89-
const { serializeEncryptionContext } = serializeFactory(
90-
fromUtf8,
91-
serializeOptions
92-
)
84+
})
9385
const _wrapKey = async (material: NodeEncryptionMaterial) => {
9486
/* The AAD section is uInt16BE(length) + AAD
9587
* see: https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/message-format.html#header-aad

modules/serialize/src/serialize_factory.ts

+9
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ export function serializeFactory(
9797
encryptionContext: EncryptionContext
9898
): Uint8Array[] {
9999
// use closure value from the serializeFactory
100+
// If the encryption context contains high order
101+
// utf8 code points the "old" implementation would sort these values
102+
// based on their values, see the false branch of this function.
103+
// This led to different sorting if using these high order utf8 code points,
104+
// which led to decryption failures from other ESDK language implementations
105+
// that correctly sorted the encryption context by sorting based on the utf8
106+
// values as opposed to the string value.
107+
// See, https://github.com/aws/aws-encryption-sdk-javascript/issues/428
108+
// for mote details
100109
const { utf8Sorting } = sorting
101110
if (utf8Sorting) {
102111
return Object.entries(encryptionContext)

0 commit comments

Comments
 (0)