This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
kms.go
209 lines (169 loc) · 4.92 KB
/
kms.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"bytes"
"crypto/rand"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"io"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
"golang.org/x/crypto/nacl/secretbox"
)
type kmsPayload struct {
EncryptedDataKey []byte
Nonce *[24]byte
Message []byte
}
// KmsClient wraps the KMS client to allow mocking
type KmsClient interface {
GenerateDataKey(keyID string) (*[32]byte, []byte, error)
Decrypt([]byte) (*[32]byte, error)
}
// KmsClientImpl implements the real KMS client
type KmsClientImpl struct {
Impl *kms.KMS
}
// KmsFunction is a lambda that operates on the KMS service
type KmsFunction func(*kms.KMS) error
// CallWithRetry executes a function with retry for MissingRegion errors
func (k *KmsClientImpl) CallWithRetry(f KmsFunction) error {
// Lazy initialize the session
if k.Impl == nil {
// Force enable Shared Config to support $AWS_DEFAULT_REGION
sess, err := session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
})
if err != nil {
return err
}
k.Impl = kms.New(sess)
}
// Invoke the function
err := f(k.Impl)
// Attempt to use EC2 meta-data service in case of MissingRegion error
if err == aws.ErrMissingRegion {
body, err := httpGet("http://169.254.169.254/2016-06-30/dynamic/instance-identity/document")
if err == nil {
var doc ec2metadata.EC2InstanceIdentityDocument
err := json.Unmarshal(body, &doc)
if err == nil {
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String(doc.Region)},
SharedConfigState: session.SharedConfigEnable,
})
if err == nil {
k.Impl = kms.New(sess)
// Retry the function with the new session
return f(k.Impl)
}
}
}
}
return err
}
// GenerateDataKey returns a new symmetric key and its encrypted form
func (k *KmsClientImpl) GenerateDataKey(keyID string) (*[32]byte, []byte, error) {
var response *kms.GenerateDataKeyOutput
bytes := int64(32)
err := k.CallWithRetry(func(impl *kms.KMS) error {
var ferr error
response, ferr = impl.GenerateDataKey(&kms.GenerateDataKeyInput{
KeyId: &keyID,
NumberOfBytes: &bytes,
})
return ferr
})
if err != nil {
return nil, nil, err
}
dataKey, err := asKey(response.Plaintext)
if err != nil {
return nil, nil, err
}
return dataKey, response.CiphertextBlob, nil
}
// Decrypt a symmetric key
func (k *KmsClientImpl) Decrypt(data []byte) (*[32]byte, error) {
var response *kms.DecryptOutput
err := k.CallWithRetry(func(impl *kms.KMS) error {
var ferr error
response, ferr = impl.Decrypt(&kms.DecryptInput{
CiphertextBlob: data,
})
return ferr
})
if err != nil {
return nil, err
}
return asKey(response.Plaintext)
}
func newKmsClient() *KmsClientImpl {
return &KmsClientImpl{}
}
// KmsEncryptionStrategy implements envelope encryption using Amazon AWS KMS
type KmsEncryptionStrategy struct {
Client KmsClient
KeyID string
}
// Encrypt a plaintext message and returns a transport envelope
func (k *KmsEncryptionStrategy) Encrypt(plaintext []byte) (string, error) {
// Generate symmetric data key
dataKey, encryptedDataKey, err := k.Client.GenerateDataKey(k.KeyID)
if err != nil {
return "", err
}
// Initialize payload
payload := &kmsPayload{
EncryptedDataKey: encryptedDataKey,
Nonce: &[24]byte{},
}
// Generate nonce
_, err = io.ReadFull(rand.Reader, payload.Nonce[:])
if err != nil {
return "", errors.New("Failed generate random nonce")
}
// Encrypt message
payload.Message = secretbox.Seal(payload.Message, plaintext, payload.Nonce, dataKey)
buffer := &bytes.Buffer{}
if err := gob.NewEncoder(buffer).Encode(payload); err != nil {
return "", err
}
return fmt.Sprintf("ENC[KMS,%s]", encode(buffer.Bytes())), nil
}
func newKmsEncryptionStrategy(client KmsClient, keyID string) *KmsEncryptionStrategy {
return &KmsEncryptionStrategy{Client: client, KeyID: keyID}
}
// KmsDecryptionStrategy implements envelope decryption using Amazon AWS KMS
type KmsDecryptionStrategy struct {
Client KmsClient
}
// Decrypt a transport envelope
func (k *KmsDecryptionStrategy) Decrypt(envelope string) ([]byte, error) {
// Extract payload
encrypted, err := decode(envelope[8 : len(envelope)-1])
if err != nil {
return nil, err
}
// Decode payload struct
var payload kmsPayload
gob.NewDecoder(bytes.NewReader(encrypted)).Decode(&payload)
// Decrypt key
dataKey, err := k.Client.Decrypt(payload.EncryptedDataKey)
if err != nil {
return nil, err
}
// Decrypt message
var plaintext []byte
plaintext, ok := secretbox.Open(plaintext, payload.Message, payload.Nonce, dataKey)
if !ok {
return nil, fmt.Errorf("Failed to open secretbox")
}
return plaintext, nil
}
func newKmsDecryptionStrategy(client KmsClient) *KmsDecryptionStrategy {
return &KmsDecryptionStrategy{Client: client}
}