-
Notifications
You must be signed in to change notification settings - Fork 0
/
keywrapper_secl.go
480 lines (405 loc) · 13.4 KB
/
keywrapper_secl.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package seclkeywrap
import (
"crypto/ecdsa"
"encoding/json"
"os/exec"
// Sym key enc libs
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
"sync"
"github.com/containers/ocicrypt/config"
"github.com/containers/ocicrypt/keywrap"
encutils "github.com/containers/ocicrypt/utils"
"github.com/pkg/errors"
jose "gopkg.in/square/go-jose.v2"
)
const (
// WrapTypeAssymetric uses assymmetric keys to perform wrapping with a JWE packet
WrapTypeAssymmetric string = "asym"
// WrapTypeSymmetric uses symmetric keys to perform wrapping with AES_GCM
WrapTypeSymmetric string = "sym"
)
var (
WrapMode string = WrapTypeSymmetric
WlLock sync.Mutex
)
type seclKeyWrapper struct{}
type annotationPacket struct {
KeyUrl string `json:"key_url"`
WrappedKey []byte `json:"wrapped_key"`
WrapType string `json:"wrap_type"`
}
type aesPacket struct {
Ciphertext []byte `json:"cipher_text"`
Nonce []byte `json:"nonce"`
}
func NewKeyWrapper() keywrap.KeyWrapper {
return &seclKeyWrapper{}
}
func (kw *seclKeyWrapper) WrapKeys(ec *config.EncryptConfig, optsData []byte) ([]byte, error) {
// If no KBS url provided, nothing to encrypt for
if len(ec.Parameters["secl-enabled"]) == 0 {
return nil, nil
}
if len(ec.Parameters["secl-asset-tag"]) != 1 {
return nil, errors.New("Current support encryption for 1 asset tag at a time")
}
assetTag := string(ec.Parameters["secl-asset-tag"][0])
// Check for required input parameters
var (
err error
keyUrl string = ""
wrappedKey []byte = []byte{}
)
// Cache keyUrl in ec.Parameters["kbs-keyurl-cache"] for same process memory encryption
if len(ec.Parameters["keyurl-cache"]) == 1 {
keyUrl = string(ec.Parameters["keyurl-cache"][0])
}
switch WrapMode {
case WrapTypeAssymmetric:
var pubKeyBytes []byte
pubKeyBytes, keyUrl, err = getPublicKeyFromBroker(keyUrl, assetTag)
if err != nil {
return nil, errors.Wrapf(err, "Unable to obtain public key from broker")
}
// Create wrapped key blob
wrappedKey, err = jweEncrypt(pubKeyBytes, optsData)
if err != nil {
return nil, errors.Wrap(err, "Failed to encrypt JWE packet")
}
case WrapTypeSymmetric:
var symKey []byte
symKey, keyUrl, err = getEncSymKeyFromBroker(keyUrl, assetTag)
if err != nil {
return nil, errors.Wrapf(err, "Unable to obtain sym key from broker")
}
// Create wrapped key blob
wrappedKey, err = aesEncrypt(symKey, optsData)
if err != nil {
return nil, errors.Wrap(err, "Failed to encrypt aes_gcm packet")
}
}
ec.Parameters["keyurl-cache"] = [][]byte{[]byte(keyUrl)}
// Create annotation packet
ap := annotationPacket{
KeyUrl: keyUrl,
WrappedKey: wrappedKey,
WrapType: WrapMode,
}
return json.Marshal(ap)
}
func (kw *seclKeyWrapper) UnwrapKey(dc *config.DecryptConfig, annotation []byte) ([]byte, error) {
// If no WLS url given, nothing to decrypt
if len(dc.Parameters["secl-enabled"]) == 0 {
return nil, nil
}
var ap annotationPacket
err := json.Unmarshal(annotation, &ap)
if err != nil {
return nil, errors.Wrap(err, "Unable to unmarshal annotation packet")
}
switch ap.WrapType {
case WrapTypeAssymmetric:
// Get private key from server and decrypt packet
privateKeyBytes, err := getPrivateKeyFromBroker(ap.KeyUrl)
if err != nil {
return nil, errors.Wrapf(err, "Unable to obtain key (url: %v)", ap.KeyUrl)
}
return jweDecrypt(privateKeyBytes, ap.WrappedKey)
case WrapTypeSymmetric:
// Get private key from server and decrypt packet
symKey, err := getDecSymKeyFromBroker(ap.KeyUrl)
if err != nil {
return nil, errors.Wrapf(err, "Unable to obtain key (url: %v)", ap.KeyUrl)
}
return aesDecrypt(symKey, ap.WrappedKey)
}
// Get private key from server and decrypt packet
privateKeyBytes, err := getPrivateKeyFromBroker(ap.KeyUrl)
if err != nil {
return nil, errors.Wrapf(err, "Unable to obtain key (url: %v)", ap.KeyUrl)
}
return jweDecrypt(privateKeyBytes, ap.WrappedKey)
}
func (kw *seclKeyWrapper) GetAnnotationID() string {
return "org.opencontainers.image.enc.keys.isecl"
}
// GetPrivateKeys (optional) gets the array of private keys. It is an optional
// as in some key services, a private key may not be exportable (i.e. HSM)
func (kw *seclKeyWrapper) GetPrivateKeys(dcparameters map[string][][]byte) [][]byte {
return nil
}
func (kw *seclKeyWrapper) NoPossibleKeys(dcparameters map[string][][]byte) bool {
return len(dcparameters["secl-enabled"]) == 0
}
// GetKeyIdsFromPacket (optional) gets a list of key IDs. This is optional as some encryption
// schemes may not have a notion of key IDs
func (kw *seclKeyWrapper) GetKeyIdsFromPacket(packet string) ([]uint64, error) {
return nil, nil
}
// GetRecipients (optional) gets a list of recipients. It is optional due to the validity of
// recipients in a particular encryptiong scheme
func (kw *seclKeyWrapper) GetRecipients(packet string) ([]string, error) {
return nil, nil
}
// getPrivateKeyFromBroker will obtain the Wrapped(privatekey) at keyUrl via the
// workload service at wlsUrl, authenticated with wlsCertificate.
//
// It will then communicate with the local TPM to unwrap the private key.
func getPrivateKeyFromBroker(keyUrl string) (privateKey []byte, err error) {
privateKey = []byte(`-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAnYarY9vO4oiCgMqIWNStjUdg+1x0NKKxVBLXhkUsY6JiTSUl
j8I3NThHIpML2A9T0GNSCXFpWob3ORxd0LlPrqSNhXl0PrJlJoT4f1ExV44Rjzww
IeqvK3d/KktCQlSbvo4111I4TRHMG1ywtz38NxE2ID/yyoH5rlUZtKY4pOBV+ktP
1V3hCfwPJJAyf/xuGgzpijUCjJYVtmsmGshxbo3JwGKTAXHD7CmCRXv3eqjHVqPV
qWjvfj4KuL0TkncjUmYL7LL/fk7Loxdlhs7QfbpN2n9Uj9epE6EFPPPWMbwcd/FE
TKOJGZCgslfARZisEmvG+5HVEuPKV7uG4Qmb1wIDAQABAoIBAHjyAvmCtM99XCWi
WxlJAY9tdGrJy3b2SwxwAwZWagR9ktgEY1iDF3xHH2bOW9OhwQpIl54kc21MHti8
jNNK1IEUWoxWeggBVGREx76JCkddDuJYpeQEmkXXU82XRuJCr+mYqoIN4Khbt8hy
XEP01Yc6McKFFtahAKD3OetXjDoZx8sWMG5hAVUssA/QTfxt5a6enZl0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romBroker will connect to a KBS at kbsUrl with certificate
// kbsCert. It will use uid for authentication with AAS.
//
// If keyUrl == "", it will generate a new key pair and return the public
// key and the associated keyUrl = kbsUrl/keyId
// Else, it will obtain the public key of the given keyUrl
func getPublicKeyFromBroker(keyUrl string, assetTag string) (publicKey []byte, retKeyUrl string, err error) {
publicKey = []byte(`-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnYarY9vO4oiCgMqIWNSt
jUdg+1x0NKKxVBLXhkUsY6JiTSUlj8I3NThHIpML2A9T0GNSCXFpWob3ORxd0LlP
rqSNhXl0PrJlJoT4f1ExV44RjzwwIeqvK3d/KktCQlSbvo4111I4TRHMG1ywtz38
NxE2ID/yyoH5rlUZtKY4pOBV+ktP1V3hCfwPJJAyf/xuGgzpijUCjJYVtmsmGshx
bo3JwGKTAXHD7CmCRXv3eqjHVqPVqWjvfj4KuL0TkncjUmYL7LL/fk7Loxdlhs7Q
fbpN2n9Uj9epE6EFPPPWMbwcd/FETKOJGZCgslfARZisEmvG+5HVEuPKV7uG4Qmb
1wIDAQAB
-----END PUBLIC KEY-----`)
return publicKey, "https://kbs.url/" + "some-key-id-xxx", nil
}
// KeyInfo if the return json struct from the key broker for a valid key
type KeyInfo struct {
KeyURL string `json:"key_url"`
Key []byte `json:"key"`
}
func getDecSymKeyFromBroker(keyUrl string) (symKey []byte, err error) {
//symKey = []byte("this_is_a_256_bit_AES_key_12345!")
//return symKey, nil
//run wlagent to fetch a new key
WlLock.Lock()
defer WlLock.Unlock()
cmdout, err := exec.Command("wlagent", "fetch-key-url", keyUrl).Output()
if err != nil {
return nil, errors.Wrap(err, "Unable to run wlagent")
}
var retKey KeyInfo
err = json.Unmarshal(cmdout[:], &retKey)
if err != nil {
return nil, errors.Wrap(err, "Unable to unmarshal Keyinfo")
}
return retKey.Key, nil
}
// getEncSymKeyFromBroker will connect to a KBS at kbsUrl with certificate
// kbsCert. It will use uid for authentication with AAS.
//
// If keyUrl == "", it will generate a new key and return the
// key and the associated keyUrl = kbsUrl/keyId
// Else, it will obtain the key of the given keyUrl
func getEncSymKeyFromBroker(keyUrl string, assetTag string) (symKey []byte, retKeyUrl string, err error) {
/*
symKey = []byte("this_is_a_256_bit_AES_key_12345!")
return symKey, kbsUrl + "/" + "some-key-id-xxx", nil
*/
var (
cmdout []byte
)
if assetTag == "" {
//run wpm to fetch a new key
cmdout, err = exec.Command("wpm", "fetch-key").Output()
if err != nil {
return nil, "", errors.Wrap(err, "Unable to run wpm")
}
} else {
//run wpm to fetch a new key with associated asset tag
cmdout, err = exec.Command("wpm", "fetch-key", "-t", assetTag).Output()
if err != nil {
return nil, "", errors.Wrap(err, "Unable to run wpm with specified asset tag")
}
}
var retKey KeyInfo
err = json.Unmarshal(cmdout[:], &retKey)
if err != nil {
return nil, "", errors.Wrap(err, "Unable to unmarshal Keyinfo")
}
return retKey.Key, retKey.KeyURL, nil
}
// JWE Helper Functions
func jweEncrypt(pubKey []byte, data []byte) ([]byte, error) {
var joseRecipients []jose.Recipient
err := jweAddPubKeys(&joseRecipients, [][]byte{pubKey})
if err != nil {
return nil, err
}
// no recipients is not an error...
if len(joseRecipients) == 0 {
return nil, nil
}
encrypter, err := jose.NewMultiEncrypter(jose.A256GCM, joseRecipients, nil)
if err != nil {
return nil, errors.Wrapf(err, "jose.NewMultiEncrypter failed")
}
jwe, err := encrypter.Encrypt(data)
if err != nil {
return nil, errors.Wrapf(err, "JWE Encrypt failed")
}
return []byte(jwe.FullSerialize()), nil
}
func jweAddPubKeys(joseRecipients *[]jose.Recipient, pubKeys [][]byte) error {
if len(pubKeys) == 0 {
return nil
}
for _, pubKey := range pubKeys {
key, err := encutils.ParsePublicKey(pubKey, "JWE")
if err != nil {
return err
}
alg := jose.RSA_OAEP
switch key.(type) {
case *ecdsa.PublicKey:
alg = jose.ECDH_ES_A256KW
}
*joseRecipients = append(*joseRecipients, jose.Recipient{
Algorithm: alg,
Key: key,
})
}
return nil
}
func jweDecrypt(privKey []byte, jweString []byte) ([]byte, error) {
jwe, err := jose.ParseEncrypted(string(jweString))
if err != nil {
return nil, errors.New("jose.ParseEncrypted failed")
}
key, err := encutils.ParsePrivateKey(privKey, nil, "JWE")
if err != nil {
return nil, err
}
_, _, plain, err := jwe.DecryptMulti(key)
if err != nil {
return nil, err
}
return plain, nil
}
// AES_GCM Helper Functions
func aesEncrypt(key []byte, plaintext []byte) ([]byte, error) {
if len(key) != 32 {
return nil, errors.New("Expected 256 bit key")
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
// Never use more than 2^32 random nonces with a given key because of the risk of a repeat.
nonce := make([]byte, 12)
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
ciphertext := aesgcm.Seal(nil, nonce, plaintext, nil)
aesp := aesPacket{
Ciphertext: ciphertext,
Nonce: nonce,
}
return json.Marshal(aesp)
}
func aesDecrypt(key []byte, data []byte) ([]byte, error) {
if len(key) != 32 {
return nil, errors.New("Expected 256 bit key")
}
var aesp aesPacket
err := json.Unmarshal(data, &aesp)
if err != nil {
return nil, errors.Wrap(err, "Unable to unmarshal aes packet")
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
plaintext, err := aesgcm.Open(nil, aesp.Nonce, aesp.Ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
}
// MAIN
/*
func main() {
// Initialize wrapper
kw := NewKeyWrapper()
key := []byte("this-is-wrapped-key-opts")
fmt.Printf("Wrapping sensitive content: %s\n\n", key)
// Encryption input
kbsUrl := "http://kbs.example.com"
kbsUid := "some-uid"
kbsCert := []byte("some-cert")
ec := &config.EncryptConfig{
Parameters: map[string][][]byte{
"kbs-url": [][]byte{[]byte(kbsUrl)},
"kbs-uid": [][]byte{[]byte(kbsUid)},
"kbs-cert": [][]byte{[]byte(kbsCert)},
},
}
annotation, err := kw.WrapKeys(ec, key)
if err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
}
fmt.Printf("Wrapped key gave annotation: \n\"%v\" : base64(%s)\n\n", kw.GetAnnotationID(), string(annotation))
// Decryption input
wlsUrl := "http://wls.example.com"
wlsCert := []byte("some-cert")
dc := &config.DecryptConfig{
Parameters: map[string][][]byte{
"wls-url": [][]byte{[]byte(wlsUrl)},
"wls-cert": [][]byte{wlsCert},
},
}
out, err := kw.UnwrapKey(dc, annotation)
if err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
}
fmt.Printf("Output from unwrapping: %s\n\n", out)
if string(out) != string(key) {
fmt.Println("BAD!!! Keys don't match!")
} else {
fmt.Println("Keys match!")
}
}
*/