-
Notifications
You must be signed in to change notification settings - Fork 171
/
local.go
241 lines (181 loc) · 6.29 KB
/
local.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package encryption
import (
"bytes"
"encoding/base64"
"fmt"
"github.com/tink-crypto/tink-go/aead"
"github.com/tink-crypto/tink-go/insecurecleartextkeyset"
"github.com/tink-crypto/tink-go/jwt"
"github.com/tink-crypto/tink-go/keyset"
"github.com/tink-crypto/tink-go/tink"
)
type localEncryptionService struct {
key *aead.KMSEnvelopeAEAD
privateEc256Handle *keyset.Handle
publicEc256Handle *keyset.Handle
}
// NewLocalEncryption creates a new local encryption service. keysetBytes is the raw keyset in
// base64-encoded JSON format. This can be generated by calling hatchet-admin keyset create-local.
func NewLocalEncryption(masterKey []byte, privateEc256 []byte, publicEc256 []byte) (*localEncryptionService, error) {
// get the master keyset handle
aes256GcmHandle, err := insecureHandleFromBytes(masterKey)
if err != nil {
return nil, err
}
a, err := aead.New(aes256GcmHandle)
if err != nil {
return nil, err
}
privateEc256Handle, err := handleFromBytes(privateEc256, a)
if err != nil {
return nil, err
}
publicEc256Handle, err := handleFromBytes(publicEc256, a)
if err != nil {
return nil, err
}
envelope := aead.NewKMSEnvelopeAEAD2(aead.AES128GCMKeyTemplate(), a)
if envelope == nil {
return nil, fmt.Errorf("failed to create envelope")
}
return &localEncryptionService{
key: envelope,
privateEc256Handle: privateEc256Handle,
publicEc256Handle: publicEc256Handle,
}, nil
}
func GenerateLocalKeys() (masterKey []byte, privateEc256 []byte, publicEc256 []byte, err error) {
masterKey, masterHandle, err := generateLocalMasterKey()
if err != nil {
return nil, nil, nil, err
}
a, err := aead.New(masterHandle)
if err != nil {
return nil, nil, nil, err
}
privateEc256, publicEc256, err = generateJWTKeysets(a)
if err != nil {
return nil, nil, nil, err
}
return masterKey, privateEc256, publicEc256, nil
}
func generateLocalMasterKey() ([]byte, *keyset.Handle, error) {
aeadTemplate := aead.AES256GCMKeyTemplate()
aes256GcmHandle, err := keyset.NewHandle(aeadTemplate)
if err != nil {
return nil, nil, fmt.Errorf("failed to create new keyset handle with AES256GCM template: %w", err)
}
bytes, err := insecureBytesFromHandle(aes256GcmHandle)
if err != nil {
return nil, nil, fmt.Errorf("failed to get bytes from handle: %w", err)
}
return bytes, aes256GcmHandle, nil
}
// generateJWTKeysets creates the keysets for JWT signing and verification encrypted with the
// masterKey. The masterKey can be from a remote KMS service or a local keyset.
func generateJWTKeysets(masterKey tink.AEAD) (privateEc256 []byte, publicEc256 []byte, err error) {
privateHandle, err := keyset.NewHandle(jwt.ES256Template())
if err != nil {
err = fmt.Errorf("failed to create new keyset handle with ES256 template: %w", err)
return
}
privateEc256, err = bytesFromHandle(privateHandle, masterKey)
if err != nil {
return
}
publicHandle, err := privateHandle.Public()
if err != nil {
err = fmt.Errorf("failed to get public keyset: %w", err)
return
}
publicEc256, err = bytesFromHandle(publicHandle, masterKey)
if err != nil {
return
}
return
}
// bytesFromHandle returns the encrypted keyset in base64-encoded JSON format, encrypted with the
// masterKey
func bytesFromHandle(kh *keyset.Handle, masterKey tink.AEAD) ([]byte, error) {
buf := new(bytes.Buffer)
writer := keyset.NewJSONWriter(buf)
err := kh.Write(writer, masterKey)
if err != nil {
return nil, fmt.Errorf("failed to write keyset: %w", err)
}
// base64-encode bytes
keysetBytes := make([]byte, base64.RawStdEncoding.EncodedLen(len(buf.Bytes())))
base64.RawStdEncoding.Encode(keysetBytes, buf.Bytes())
return keysetBytes, nil
}
// insecureBytesFromHandle returns the raw (unencrypted) keyset in base64-encoded JSON format.
func insecureBytesFromHandle(kh *keyset.Handle) ([]byte, error) {
buf := new(bytes.Buffer)
writer := keyset.NewJSONWriter(buf)
err := insecurecleartextkeyset.Write(kh, writer)
if err != nil {
return nil, fmt.Errorf("failed to write keyset: %w", err)
}
// base64-encode bytes
keysetBytes := make([]byte, base64.RawStdEncoding.EncodedLen(len(buf.Bytes())))
base64.RawStdEncoding.Encode(keysetBytes, buf.Bytes())
return keysetBytes, nil
}
func handleFromBytes(keysetBytes []byte, masterKey tink.AEAD) (*keyset.Handle, error) {
// base64-decode bytes
keysetJsonBytes := make([]byte, base64.RawStdEncoding.DecodedLen(len(keysetBytes)))
_, err := base64.RawStdEncoding.Decode(keysetJsonBytes, keysetBytes)
if err != nil {
return nil, fmt.Errorf("failed to decode keyset bytes: %w", err)
}
// read keyset
handle, err := keyset.Read(keyset.NewJSONReader(bytes.NewReader(keysetJsonBytes)), masterKey)
if err != nil {
return nil, fmt.Errorf("failed to read keyset: %w", err)
}
return handle, nil
}
func insecureHandleFromBytes(keysetBytes []byte) (*keyset.Handle, error) {
// base64-decode bytes
keysetJsonBytes := make([]byte, base64.RawStdEncoding.DecodedLen(len(keysetBytes)))
_, err := base64.RawStdEncoding.Decode(keysetJsonBytes, keysetBytes)
if err != nil {
return nil, fmt.Errorf("failed to decode keyset bytes: %w", err)
}
// read keyset
handle, err := insecurecleartextkeyset.Read(keyset.NewJSONReader(bytes.NewReader(keysetJsonBytes)))
if err != nil {
return nil, fmt.Errorf("failed to read keyset: %w", err)
}
return handle, nil
}
func (svc *localEncryptionService) Encrypt(plaintext []byte, dataId string) ([]byte, error) {
return encrypt(svc.key, plaintext, dataId)
}
func (svc *localEncryptionService) Decrypt(ciphertext []byte, dataId string) ([]byte, error) {
return decrypt(svc.key, ciphertext, dataId)
}
func (svc *localEncryptionService) EncryptString(data string, dataId string) (string, error) {
b, err := encrypt(svc.key, []byte(data), dataId)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(b), nil
}
func (svc *localEncryptionService) DecryptString(data string, dataId string) (string, error) {
plain, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return "", err
}
b, err := decrypt(svc.key, plain, dataId)
if err != nil {
return "", err
}
return string(b), nil
}
func (svc *localEncryptionService) GetPrivateJWTHandle() *keyset.Handle {
return svc.privateEc256Handle
}
func (svc *localEncryptionService) GetPublicJWTHandle() *keyset.Handle {
return svc.publicEc256Handle
}