-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt.go
179 lines (131 loc) · 2.99 KB
/
crypt.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
package crypt
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
"golang.org/x/crypto/nacl/secretbox"
)
const (
KeyLength = 32
NonceLength = 24
)
type Crypt struct {
AwsRegion string
AwsAccess string
AwsSecret string
AwsToken string
}
type Instance struct {
Region string `json::"region"`
}
type Role struct {
Access string `json:"AccessKeyId"`
Secret string `json:"SecretAccessKey"`
Token string `json:"Token"`
}
type Envelope struct {
Ciphertext []byte `json:"c"`
EncryptedKey []byte `json:"k"`
Nonce []byte `json:"n"`
}
func New(region, access, secret string) *Crypt {
return &Crypt{
AwsRegion: region,
AwsAccess: access,
AwsSecret: secret,
}
}
func NewIam(role string) (*Crypt, error) {
res, err := http.Get("http://169.254.169.254/latest/dynamic/instance-identity/document")
if err != nil {
return nil, err
}
defer res.Body.Close()
id, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var instance Instance
err = json.Unmarshal(id, &instance)
if err != nil {
return nil, err
}
res, err = http.Get(fmt.Sprintf("http://169.254.169.254/latest/meta-data/iam/security-credentials/%s", role))
if err != nil {
return nil, err
}
defer res.Body.Close()
rd, err := ioutil.ReadAll(res.Body)
var r Role
err = json.Unmarshal(rd, &r)
if err != nil {
return nil, err
}
crypt := &Crypt{
AwsRegion: instance.Region,
AwsAccess: r.Access,
AwsSecret: r.Secret,
AwsToken: r.Token,
}
return crypt, nil
}
func (c *Crypt) Encrypt(keyArn string, dec []byte) ([]byte, error) {
req := &kms.GenerateDataKeyInput{
KeyId: aws.String(keyArn),
NumberOfBytes: aws.Int64(KeyLength),
}
res, err := KMS(c).GenerateDataKey(req)
if err != nil {
return nil, err
}
var key [KeyLength]byte
copy(key[:], res.Plaintext[0:KeyLength])
rand, err := c.generateNonce()
if err != nil {
return nil, err
}
var nonce [NonceLength]byte
copy(nonce[:], rand[0:NonceLength])
var enc []byte
enc = secretbox.Seal(enc, dec, &nonce, &key)
e := &Envelope{
Ciphertext: enc,
EncryptedKey: res.CiphertextBlob,
Nonce: nonce[:],
}
return json.Marshal(e)
}
func (c *Crypt) Decrypt(keyArn string, data []byte) ([]byte, error) {
var e *Envelope
err := json.Unmarshal(data, &e)
if err != nil {
return nil, err
}
req := &kms.DecryptInput{
CiphertextBlob: e.EncryptedKey,
}
res, err := KMS(c).Decrypt(req)
if err != nil {
return nil, err
}
var key [KeyLength]byte
copy(key[:], res.Plaintext[0:KeyLength])
var nonce [NonceLength]byte
copy(nonce[:], e.Nonce[0:NonceLength])
var dec []byte
dec, ok := secretbox.Open(dec, e.Ciphertext, &nonce, &key)
if !ok {
return nil, fmt.Errorf("failed decryption")
}
return dec, nil
}
func (c *Crypt) generateNonce() ([]byte, error) {
res, err := KMS(c).GenerateRandom(&kms.GenerateRandomInput{NumberOfBytes: aws.Int64(NonceLength)})
if err != nil {
return nil, err
}
return res.Plaintext, nil
}