@@ -28,36 +28,35 @@ func NewAwsKmsCryptographicMaterialsProvider(keyID string, encryptionContext map
28
28
}, nil
29
29
}
30
30
31
- // GenerateDataKey generates a new data key using AWS KMS and wraps the Tink keyset.
32
- func (p * AwsKmsCryptographicMaterialsProvider ) GenerateDataKey () (* delegatedkeys.TinkDelegatedKey , []byte , error ) {
31
+ // EncryptionMaterials retrieves and stores encryption materials for the given encryption context.
32
+ func (p * AwsKmsCryptographicMaterialsProvider ) EncryptionMaterials (ctx context.Context , materialName string ) (materials.CryptographicMaterials , error ) {
33
+ // Generate a new Tink keyset and wrap it
33
34
delegatedKey , wrappedKeyset , err := delegatedkeys .GenerateDataKey (p .KeyID )
34
35
if err != nil {
35
- return nil , nil , fmt .Errorf ("failed to generate data key: %v" , err )
36
+ return nil , fmt .Errorf ("failed to generate and wrap data key: %v" , err )
36
37
}
37
38
38
- return delegatedKey , wrappedKeyset , nil
39
- }
40
-
41
- // DecryptDataKey unwraps the Tink keyset using AWS KMS.
42
- func (p * AwsKmsCryptographicMaterialsProvider ) DecryptDataKey (encryptedKeyset []byte ) (* delegatedkeys.TinkDelegatedKey , error ) {
43
- return delegatedkeys .UnwrapKeyset (encryptedKeyset , p .KeyID )
44
- }
45
-
46
- // EncryptionMaterials retrieves and stores encryption materials for the given encryption context.
47
- func (p * AwsKmsCryptographicMaterialsProvider ) EncryptionMaterials (ctx context.Context , materialName string ) (materials.CryptographicMaterials , error ) {
48
- // Generate a new Tink keyset and wrap it
49
- delegatedKey , wrappedKeyset , err := p .GenerateDataKey ()
39
+ // Assume GenerateSigningKey is modified to return public key as well
40
+ delegatedSigningKey , _ , publicKeyBytes , err := delegatedkeys .GenerateSigningKey (p .KeyID )
50
41
if err != nil {
51
42
return nil , fmt .Errorf ("failed to generate and wrap data key: %v" , err )
52
43
}
53
44
45
+ // Sign the wrappedKeyset
46
+ signature , err := delegatedSigningKey .Sign (wrappedKeyset )
47
+ if err != nil {
48
+ return nil , fmt .Errorf ("failed to sign wrappedKeyset: %v" , err )
49
+ }
50
+
54
51
// Prepare the material description with encryption context and wrapped keyset
55
52
materialDescription := make (map [string ]string )
56
53
for key , value := range p .EncryptionContext {
57
54
materialDescription [key ] = value
58
55
}
59
56
materialDescription ["ContentEncryptionAlgorithm" ] = delegatedKey .Algorithm ()
60
57
materialDescription ["WrappedKeyset" ] = base64 .StdEncoding .EncodeToString (wrappedKeyset )
58
+ materialDescription ["Signature" ] = base64 .StdEncoding .EncodeToString (signature )
59
+ materialDescription ["PublicKey" ] = base64 .StdEncoding .EncodeToString (publicKeyBytes )
61
60
62
61
// Create encryption materials with the material description and the encryption key
63
62
encryptionMaterials := materials .NewEncryptionMaterials (materialDescription , delegatedKey , nil )
@@ -81,7 +80,25 @@ func (p *AwsKmsCryptographicMaterialsProvider) DecryptionMaterials(ctx context.C
81
80
return nil , fmt .Errorf ("failed to decode encrypted keyset: %v" , err )
82
81
}
83
82
84
- delegatedKey , err := p .DecryptDataKey (encryptedKeyset )
83
+ publicKeyBase64 := materialDescMap ["PublicKey" ]
84
+ publicKeyBytes , err := base64 .StdEncoding .DecodeString (publicKeyBase64 )
85
+ if err != nil {
86
+ return nil , fmt .Errorf ("failed to decode public key: %v" , err )
87
+ }
88
+
89
+ signatureBase64 := materialDescMap ["Signature" ]
90
+ signatureBytes , err := base64 .StdEncoding .DecodeString (signatureBase64 )
91
+ if err != nil {
92
+ return nil , fmt .Errorf ("failed to decode signature: %v" , err )
93
+ }
94
+
95
+ // Verify the wrapped keyset's signature
96
+ valid , err := delegatedkeys .VerifySignature (publicKeyBytes , signatureBytes , encryptedKeyset )
97
+ if err != nil || ! valid {
98
+ return nil , fmt .Errorf ("failed to verify the wrapped keyset's signature: %v" , err )
99
+ }
100
+
101
+ delegatedKey , err := delegatedkeys .UnwrapKeyset (encryptedKeyset , p .KeyID )
85
102
if err != nil {
86
103
return nil , fmt .Errorf ("failed to decrypt and unwrap data key: %v" , err )
87
104
}
0 commit comments