/
rotator.go
136 lines (122 loc) · 4.54 KB
/
rotator.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
/*
Copyright 2018, Cossack Labs Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"encoding/hex"
"encoding/json"
acrastruct2 "github.com/cossacklabs/acra/acrastruct"
"github.com/cossacklabs/acra/crypto"
"github.com/cossacklabs/acra/decryptor/base"
"github.com/cossacklabs/acra/keystore"
"github.com/cossacklabs/acra/utils"
"github.com/cossacklabs/themis/gothemis/keys"
log "github.com/sirupsen/logrus"
)
// RotateStorageKeyStore enables storage key rotation. It is used by acra-rotate tool.
type RotateStorageKeyStore interface {
keystore.StorageKeyCreation
keystore.DataEncryptorKeyStore
}
type keyRotator struct {
keystore RotateStorageKeyStore
newKeypairs map[string]*keys.Keypair
}
func newRotator(store RotateStorageKeyStore) (*keyRotator, error) {
return &keyRotator{keystore: store, newKeypairs: make(map[string]*keys.Keypair)}, nil
}
func (rotator *keyRotator) getRotatedPublicKey(keyID []byte) (*keys.PublicKey, error) {
keypair, ok := rotator.newKeypairs[string(keyID)]
if ok {
return keypair.Public, nil
}
newKeypair, err := keys.New(keys.TypeEC)
if err != nil {
return nil, err
}
rotator.newKeypairs[string(keyID)] = newKeypair
return newKeypair.Public, nil
}
func (rotator *keyRotator) rotateAcrastruct(id, acrastruct []byte) ([]byte, error) {
return rotator.rotateAcrastructWithClientID(id, acrastruct)
}
func (rotator *keyRotator) rotateAcrastructWithClientID(clientID, data []byte) ([]byte, error) {
logger := log.WithFields(log.Fields{"KeyID": string(clientID)})
logger.Infof("Rotate AcraStruct")
// rotate
handler, err := crypto.GetHandlerByEnvelopeID(crypto.AcraStructEnvelopeID)
if err != nil {
log.WithError(err).Errorln("Can't load handler by envelope ID")
return nil, err
}
accessContext := base.NewAccessContext(base.WithClientID(clientID))
dataContext := &base.DataProcessorContext{Keystore: rotator.keystore,
Context: base.SetAccessContextToContext(context.Background(), accessContext)}
acrastruct, envelopeID, err := crypto.DeserializeEncryptedData(data)
if err != nil {
logger.WithError(err).Errorln("Can't deserialize container")
return nil, err
}
if envelopeID != crypto.AcraStructEnvelopeID {
logger.WithField("envelope_id", envelopeID).WithError(err).Errorln("Incorrect envelope ID in container, not AcraStruct")
return nil, err
}
decrypted, err := handler.Decrypt(acrastruct, dataContext)
if err != nil {
logger.WithField("acrastruct", hex.EncodeToString(acrastruct)).WithError(err).Errorln("Can't decrypt AcraStruct")
return nil, err
}
defer utils.ZeroizeBytes(decrypted)
publicKey, err := rotator.getRotatedPublicKey(clientID)
if err != nil {
logger.WithField("acrastruct", hex.EncodeToString(acrastruct)).WithError(err).Errorln("Can't load public key")
return nil, err
}
rotated, err := acrastruct2.CreateAcrastruct(decrypted, publicKey, nil)
if err != nil {
logger.WithField("acrastruct", hex.EncodeToString(acrastruct)).WithError(err).Errorln("Can't rotate data")
return nil, err
}
rotated, err = crypto.SerializeEncryptedData(rotated, crypto.AcraStructEnvelopeID)
if err != nil {
logger.WithField("acrastruct", hex.EncodeToString(acrastruct)).WithError(err).Errorln("Can't serialize data")
return nil, err
}
return rotated, nil
}
func (rotator *keyRotator) saveRotatedKey(id []byte, keypair *keys.Keypair) error {
return rotator.keystore.SaveDataEncryptionKeys(id, keypair)
}
func (rotator *keyRotator) saveRotatedKeys() error {
for id, keypair := range rotator.newKeypairs {
if err := rotator.saveRotatedKey([]byte(id), keypair); err != nil {
log.WithField("key_id", id).
WithError(err).Errorln("Can't save rotated keypair")
return err
}
}
return nil
}
func (rotator *keyRotator) clearKeys() {
for _, keypair := range rotator.newKeypairs {
utils.ZeroizePrivateKey(keypair.Private)
}
}
func (rotator *keyRotator) marshal() ([]byte, error) {
const PublicKey = "new_public_key"
output := make(map[string]map[string][]byte)
for id, keypair := range rotator.newKeypairs {
output[id] = map[string][]byte{PublicKey: keypair.Public.Value}
}
return json.Marshal(output)
}