1
1
import * as iam from '@aws-cdk/aws-iam' ;
2
2
import * as cxschema from '@aws-cdk/cloud-assembly-schema' ;
3
- import { FeatureFlags , IResource , Lazy , RemovalPolicy , Resource , ResourceProps , Stack , Duration , Token , ContextProvider , Arn , ArnFormat } from '@aws-cdk/core' ;
3
+ import {
4
+ Arn ,
5
+ ArnFormat ,
6
+ ContextProvider ,
7
+ Duration ,
8
+ FeatureFlags ,
9
+ IResource ,
10
+ Lazy ,
11
+ RemovalPolicy ,
12
+ Resource ,
13
+ ResourceProps ,
14
+ Stack ,
15
+ Token ,
16
+ } from '@aws-cdk/core' ;
4
17
import * as cxapi from '@aws-cdk/cx-api' ;
5
18
import { Construct } from 'constructs' ;
6
19
import { Alias } from './alias' ;
@@ -60,6 +73,16 @@ export interface IKey extends IResource {
60
73
* Grant encryption and decryption permissions using this key to the given principal
61
74
*/
62
75
grantEncryptDecrypt ( grantee : iam . IGrantable ) : iam . Grant ;
76
+
77
+ /**
78
+ * Grant permissions to generating MACs to the given principal
79
+ */
80
+ grantGenerateMac ( grantee : iam . IGrantable ) : iam . Grant
81
+
82
+ /**
83
+ * Grant permissions to verifying MACs to the given principal
84
+ */
85
+ grantVerifyMac ( grantee : iam . IGrantable ) : iam . Grant
63
86
}
64
87
65
88
abstract class KeyBase extends Resource implements IKey {
@@ -193,6 +216,20 @@ abstract class KeyBase extends Resource implements IKey {
193
216
return this . grant ( grantee , ...[ ...perms . DECRYPT_ACTIONS , ...perms . ENCRYPT_ACTIONS ] ) ;
194
217
}
195
218
219
+ /**
220
+ * Grant permissions to generating MACs to the given principal
221
+ */
222
+ public grantGenerateMac ( grantee : iam . IGrantable ) : iam . Grant {
223
+ return this . grant ( grantee , ...perms . GENERATE_HMAC_ACTIONS ) ;
224
+ }
225
+
226
+ /**
227
+ * Grant permissions to verifying MACs to the given principal
228
+ */
229
+ public grantVerifyMac ( grantee : iam . IGrantable ) : iam . Grant {
230
+ return this . grant ( grantee , ...perms . VERIFY_HMAC_ACTIONS ) ;
231
+ }
232
+
196
233
/**
197
234
* Checks whether the grantee belongs to a stack that will be deployed
198
235
* after the stack containing this key.
@@ -300,6 +337,41 @@ export enum KeySpec {
300
337
* Valid usage: SIGN_VERIFY
301
338
*/
302
339
ECC_SECG_P256K1 = 'ECC_SECG_P256K1' ,
340
+
341
+ /**
342
+ * Hash-Based Message Authentication Code as defined in RFC 2104 using the message digest function SHA224.
343
+ *
344
+ * Valid usage: GENERATE_VERIFY_MAC
345
+ */
346
+ HMAC_224 = 'HMAC_224' ,
347
+
348
+ /**
349
+ * Hash-Based Message Authentication Code as defined in RFC 2104 using the message digest function SHA256.
350
+ *
351
+ * Valid usage: GENERATE_VERIFY_MAC
352
+ */
353
+ HMAC_256 = 'HMAC_256' ,
354
+
355
+ /**
356
+ * Hash-Based Message Authentication Code as defined in RFC 2104 using the message digest function SHA384.
357
+ *
358
+ * Valid usage: GENERATE_VERIFY_MAC
359
+ */
360
+ HMAC_384 = 'HMAC_384' ,
361
+
362
+ /**
363
+ * Hash-Based Message Authentication Code as defined in RFC 2104 using the message digest function SHA512.
364
+ *
365
+ * Valid usage: GENERATE_VERIFY_MAC
366
+ */
367
+ HMAC_512 = 'HMAC_512' ,
368
+
369
+ /**
370
+ * Elliptic curve key spec available only in China Regions.
371
+ *
372
+ * Valid usage: ENCRYPT_DECRYPT and SIGN_VERIFY
373
+ */
374
+ SM2 = 'SM2' ,
303
375
}
304
376
305
377
/**
@@ -315,6 +387,11 @@ export enum KeyUsage {
315
387
* Signing and verification
316
388
*/
317
389
SIGN_VERIFY = 'SIGN_VERIFY' ,
390
+
391
+ /**
392
+ * Generating and verifying MACs
393
+ */
394
+ GENERATE_VERIFY_MAC = 'GENERATE_VERIFY_MAC' ,
318
395
}
319
396
320
397
/**
@@ -595,9 +672,28 @@ export class Key extends KeyBase {
595
672
KeySpec . ECC_NIST_P384 ,
596
673
KeySpec . ECC_NIST_P521 ,
597
674
KeySpec . ECC_SECG_P256K1 ,
675
+ KeySpec . HMAC_224 ,
676
+ KeySpec . HMAC_256 ,
677
+ KeySpec . HMAC_384 ,
678
+ KeySpec . HMAC_512 ,
598
679
] ,
599
680
[ KeyUsage . SIGN_VERIFY ] : [
600
681
KeySpec . SYMMETRIC_DEFAULT ,
682
+ KeySpec . HMAC_224 ,
683
+ KeySpec . HMAC_256 ,
684
+ KeySpec . HMAC_384 ,
685
+ KeySpec . HMAC_512 ,
686
+ ] ,
687
+ [ KeyUsage . GENERATE_VERIFY_MAC ] : [
688
+ KeySpec . RSA_2048 ,
689
+ KeySpec . RSA_3072 ,
690
+ KeySpec . RSA_4096 ,
691
+ KeySpec . ECC_NIST_P256 ,
692
+ KeySpec . ECC_NIST_P384 ,
693
+ KeySpec . ECC_NIST_P521 ,
694
+ KeySpec . ECC_SECG_P256K1 ,
695
+ KeySpec . SYMMETRIC_DEFAULT ,
696
+ KeySpec . SM2 ,
601
697
] ,
602
698
} ;
603
699
const keySpec = props . keySpec ?? KeySpec . SYMMETRIC_DEFAULT ;
@@ -606,6 +702,10 @@ export class Key extends KeyBase {
606
702
throw new Error ( `key spec '${ keySpec } ' is not valid with usage '${ keyUsage } '` ) ;
607
703
}
608
704
705
+ if ( keySpec . startsWith ( 'HMAC' ) && props . enableKeyRotation ) {
706
+ throw new Error ( 'key rotation cannot be enabled on HMAC keys' ) ;
707
+ }
708
+
609
709
if ( keySpec !== KeySpec . SYMMETRIC_DEFAULT && props . enableKeyRotation ) {
610
710
throw new Error ( 'key rotation cannot be enabled on asymmetric keys' ) ;
611
711
}
0 commit comments