1
|
|
|
package kms |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/base64" |
6
|
|
|
) |
7
|
|
|
|
8
|
|
|
// XDeleteKey deletes the key using 'ScheduleKeyDeletion'. |
9
|
|
|
func (svc *KMS) XDeleteKey(ctx context.Context, key string, day ...int64) error { |
10
|
|
|
metaData, err := svc.DescribeKey(ctx, DescribeKeyRequest{ |
11
|
|
|
KeyID: key, |
12
|
|
|
}) |
13
|
|
|
if err != nil { |
14
|
|
|
return err |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
const defaultWindowDay = 30 |
18
|
|
|
d := int64(defaultWindowDay) |
19
|
|
|
if len(day) != 0 { |
20
|
|
|
d = day[0] |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
_, err = svc.ScheduleKeyDeletion(ctx, ScheduleKeyDeletionRequest{ |
24
|
|
|
KeyID: metaData.KeyMetadata.KeyID, |
25
|
|
|
PendingWindowInDays: d, |
26
|
|
|
}) |
27
|
|
|
return err |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// XEncryptBytes encrypts the 'plainData' using the 'key' and returns 'encryptedData'. |
31
|
|
|
func (svc *KMS) XEncryptBytes(ctx context.Context, key string, plainData []byte) (encryptedData []byte, err error) { |
32
|
|
|
result, err := svc.Encrypt(ctx, EncryptRequest{ |
33
|
|
|
KeyID: key, |
34
|
|
|
Plaintext: plainData, |
35
|
|
|
}) |
36
|
|
|
if err != nil { |
37
|
|
|
return nil, err |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return result.CiphertextBlob, nil |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// XEncryptString encrypts the 'plainText' using the 'key' and returns encrypted 'base64Text'. |
44
|
|
|
func (svc *KMS) XEncryptString(ctx context.Context, key string, plainText string) (base64Text string, err error) { |
45
|
|
|
encryptedData, err := svc.XEncryptBytes(ctx, key, []byte(plainText)) |
46
|
|
|
if err != nil { |
47
|
|
|
return "", err |
48
|
|
|
} |
49
|
|
|
return base64.StdEncoding.EncodeToString(encryptedData), nil |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// XDecryptBytes decrypts the 'encryptedData'. |
53
|
|
|
func (svc *KMS) XDecryptBytes(ctx context.Context, encryptedData []byte) (plainData []byte, err error) { |
54
|
|
|
result, err := svc.Decrypt(ctx, DecryptRequest{ |
55
|
|
|
CiphertextBlob: encryptedData, |
56
|
|
|
}) |
57
|
|
|
if err != nil { |
58
|
|
|
return nil, err |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return result.Plaintext, nil |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// XDecryptString decrypts the 'base64Text'. |
65
|
|
|
func (svc *KMS) XDecryptString(ctx context.Context, base64Text string) (plainText string, err error) { |
66
|
|
|
byt, err := base64.StdEncoding.DecodeString(base64Text) |
67
|
|
|
if err != nil { |
68
|
|
|
return "", err |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
plainData, err := svc.XDecryptBytes(ctx, byt) |
72
|
|
|
if err != nil { |
73
|
|
|
return "", err |
74
|
|
|
} |
75
|
|
|
return string(plainData), nil |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// XReEncryptBytes re-encrypts the 'encryptedData' using 'destinationKey'. |
79
|
|
|
func (svc *KMS) XReEncryptBytes(ctx context.Context, destinationKey string, encryptedData []byte) (resultEncryptedData []byte, err error) { |
80
|
|
|
result, err := svc.ReEncrypt(ctx, ReEncryptRequest{ |
81
|
|
|
DestinationKeyID: destinationKey, |
82
|
|
|
CiphertextBlob: encryptedData, |
83
|
|
|
}) |
84
|
|
|
if err != nil { |
85
|
|
|
return nil, err |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return result.CiphertextBlob, nil |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// XReEncryptString re-encrypts the 'base64Text' using 'destinationKey'. |
92
|
|
|
func (svc *KMS) XReEncryptString(ctx context.Context, destinationKey, base64Text string) (resultBase64Text string, err error) { |
93
|
|
|
byt, err := base64.StdEncoding.DecodeString(base64Text) |
94
|
|
|
if err != nil { |
95
|
|
|
return "", err |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
encryptedData, err := svc.XReEncryptBytes(ctx, destinationKey, byt) |
99
|
|
|
if err != nil { |
100
|
|
|
return "", err |
101
|
|
|
} |
102
|
|
|
return base64.StdEncoding.EncodeToString(encryptedData), nil |
103
|
|
|
} |
104
|
|
|
|