Skip to content

Commit f2002c7

Browse files
committed
fix: Better messages when no keyring succeeded to decrypt
If no keyrings attempt to decrypt any encrypted data keys, then the message can not be decrypted. The code attempted to enforce this, by retrieving the unencrypted data key in node. There were two issues here 1. The check ensure the validity of the materials, itself threw an error. 1. Had this check succeeded, the error message `'Unencrypted data key is invalid.’` is not incredibly more helpful than 'unencryptedDataKey has not been set' The error message has been updated, and the tests have been updated to verify _this_ error message. On a related note awslabs/aws-encryption-sdk-specification#97 starts to explore some additional possibilities. The fullness of this issue is not only in failure, but success can also have similar issues.
1 parent 0a1ef65 commit f2002c7

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ export class WebCryptoDefaultCryptographicMaterialsManager
8686
* and that the unencrypted data key is non-NULL.
8787
* See: cryptographic_materials.ts, `getUnencryptedDataKey`
8888
*/
89-
needs(material.hasValidKey(), 'Unencrypted data key is invalid.')
89+
needs(
90+
material.hasValidKey(),
91+
'No keyring generated an unencrypted data key.' +
92+
'\nYou may not have access to any wrapping keys.'
93+
)
9094

9195
/* Postcondition: The WebCryptoEncryptionMaterial must contain at least 1 EncryptedDataKey. */
9296
needs(
@@ -113,7 +117,11 @@ export class WebCryptoDefaultCryptographicMaterialsManager
113117
* that the data key matches the algorithm suite specification
114118
* and that the unencrypted data key is non-NULL.
115119
*/
116-
needs(material.hasValidKey(), 'Unencrypted data key is invalid.')
120+
needs(
121+
material.hasValidKey(),
122+
'No keyring attempted to decrypted any of the encrypted data keys.' +
123+
'\nYou may not have access to any wrapping keys.'
124+
)
117125

118126
return material
119127
}

modules/material-management-browser/test/browser_cryptographic_materials_manager.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ describe('WebCryptoDefaultCryptographicMaterialsManager', () => {
361361

362362
await expect(
363363
cmm.getEncryptionMaterials({ encryptionContext })
364-
).to.rejectedWith(Error)
364+
).to.rejectedWith(Error, 'No keyring generated an unencrypted data key.')
365365
})
366366

367367
it('Postcondition: The WebCryptoEncryptionMaterial must contain at least 1 EncryptedDataKey.', async () => {
@@ -485,6 +485,9 @@ describe('WebCryptoDefaultCryptographicMaterialsManager', () => {
485485
encryptionContext,
486486
encryptedDataKeys: [edk],
487487
})
488-
).to.rejectedWith(Error)
488+
).to.rejectedWith(
489+
Error,
490+
'No keyring attempted to decrypted any of the encrypted data keys.'
491+
)
489492
})
490493
})

modules/material-management-node/src/node_cryptographic_materials_manager.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ export class NodeDefaultCryptographicMaterialsManager
7878
* and that the unencrypted data key is non-NULL.
7979
* See: cryptographic_materials.ts, `getUnencryptedDataKey`
8080
*/
81-
needs(material.getUnencryptedDataKey(), 'Unencrypted data key is invalid.')
81+
needs(
82+
material.hasValidKey(),
83+
'No keyring generated an unencrypted data key.' +
84+
'\nYou may not have access to any wrapping keys.'
85+
)
8286

8387
/* Postcondition: The NodeEncryptionMaterial must contain at least 1 EncryptedDataKey. */
8488
needs(
@@ -105,7 +109,11 @@ export class NodeDefaultCryptographicMaterialsManager
105109
* that the data key matches the algorithm suite specification
106110
* and that the unencrypted data key is non-NULL.
107111
*/
108-
needs(material.getUnencryptedDataKey(), 'Unencrypted data key is invalid.')
112+
needs(
113+
material.hasValidKey(),
114+
'No keyring attempted to decrypted any of the encrypted data keys.' +
115+
'\nYou may not have access to any wrapping keys.'
116+
)
109117

110118
return material
111119
}

modules/material-management-node/test/node_cryptographic_materials_manager.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ describe('NodeDefaultCryptographicMaterialsManager', () => {
228228

229229
await expect(
230230
cmm.getEncryptionMaterials({ suite, encryptionContext: {} })
231-
).to.rejectedWith(Error)
231+
).to.rejectedWith(Error, 'No keyring generated an unencrypted data key.')
232232
})
233233

234234
it('Postcondition: The NodeEncryptionMaterial must contain at least 1 EncryptedDataKey.', async () => {
@@ -287,7 +287,10 @@ describe('NodeDefaultCryptographicMaterialsManager', () => {
287287

288288
await expect(
289289
cmm.decryptMaterials({ suite, encryptedDataKeys, encryptionContext: {} })
290-
).to.rejectedWith(Error)
290+
).to.rejectedWith(
291+
Error,
292+
'No keyring attempted to decrypted any of the encrypted data keys.'
293+
)
291294
})
292295

293296
it('Return decryption material', async () => {

0 commit comments

Comments
 (0)