Skip to content

Commit c82226a

Browse files
committed
fix: Better messages when no keyring succeeded to decrypt
resolves: aws#152, aws#31 linked: awslabs/aws-encryption-sdk-specification#105 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 cc79bc1 commit c82226a

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ export class WebCryptoDefaultCryptographicMaterialsManager
8888
* and that the unencrypted data key is non-NULL.
8989
* See: cryptographic_materials.ts, `getUnencryptedDataKey`
9090
*/
91-
needs(material.hasValidKey(), 'Unencrypted data key is invalid.')
91+
needs(
92+
material.hasValidKey(),
93+
'No keyring generated an unencrypted data key.' +
94+
'\nYou may not have access to any wrapping keys.'
95+
)
9296

9397
/* Postcondition: The WebCryptoEncryptionMaterial must contain at least 1 EncryptedDataKey. */
9498
needs(
@@ -115,7 +119,11 @@ export class WebCryptoDefaultCryptographicMaterialsManager
115119
* that the data key matches the algorithm suite specification
116120
* and that the unencrypted data key is non-NULL.
117121
*/
118-
needs(material.hasValidKey(), 'Unencrypted data key is invalid.')
122+
needs(
123+
material.hasValidKey(),
124+
'No keyring attempted to decrypted any of the encrypted data keys.' +
125+
'\nYou may not have access to any wrapping keys.'
126+
)
119127

120128
return material
121129
}

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ describe('WebCryptoDefaultCryptographicMaterialsManager', () => {
332332

333333
await expect(
334334
cmm.getEncryptionMaterials({ encryptionContext })
335-
).to.rejectedWith(Error)
335+
).to.rejectedWith(Error, 'No keyring generated an unencrypted data key.')
336336
})
337337

338338
it('Postcondition: The WebCryptoEncryptionMaterial must contain at least 1 EncryptedDataKey.', async () => {
@@ -444,6 +444,9 @@ describe('WebCryptoDefaultCryptographicMaterialsManager', () => {
444444
encryptionContext,
445445
encryptedDataKeys: [edk],
446446
})
447-
).to.rejectedWith(Error)
447+
).to.rejectedWith(
448+
Error,
449+
'No keyring attempted to decrypted any of the encrypted data keys.'
450+
)
448451
})
449452
})

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

+10-2
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

+5-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ describe('NodeDefaultCryptographicMaterialsManager', () => {
227227

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

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

283283
await expect(
284284
cmm.decryptMaterials({ suite, encryptedDataKeys, encryptionContext: {} })
285-
).to.rejectedWith(Error)
285+
).to.rejectedWith(
286+
Error,
287+
'No keyring attempted to decrypted any of the encrypted data keys.'
288+
)
286289
})
287290

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

0 commit comments

Comments
 (0)