forked from cossacklabs/acra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataEncryptor.go
99 lines (92 loc) · 3.81 KB
/
dataEncryptor.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
package acrablock
import (
"context"
"github.com/cossacklabs/acra/acrastruct"
"github.com/cossacklabs/acra/decryptor/base"
"github.com/cossacklabs/acra/encryptor"
"github.com/cossacklabs/acra/encryptor/config"
"github.com/cossacklabs/acra/keystore"
)
// standaloneEncryptorFilterFunction return true if operation should be applied only if setting configured for
// encryption without any other operations like tokenization/masking
func standaloneEncryptorFilterFunction(setting config.ColumnEncryptionSetting) bool {
return setting.GetCryptoEnvelope() != config.CryptoEnvelopeTypeAcraBlock || !setting.OnlyEncryption()
}
// DataEncryptor that uses AcraBlocks for encryption
type DataEncryptor struct {
keyStore keystore.DataEncryptorKeyStore
zoneMode bool
needSkipEncryptionFunc encryptor.CheckFunction
}
// NewDataEncryptor return new DataEncryptor that uses AcraBlock to encrypt data which may be used by other encryptors
func NewDataEncryptor(zonemode bool, keyStore keystore.DataEncryptorKeyStore) (*DataEncryptor, error) {
return &DataEncryptor{zoneMode: zonemode, keyStore: keyStore, needSkipEncryptionFunc: encryptor.EmptyCheckFunction}, nil
}
// NewStandaloneDataEncryptor return new DataEncryptor that uses AcraBlock to encrypt data as separate OnColumn processor
// and checks passed setting that it configured only for transparent AcraBlock encryption
func NewStandaloneDataEncryptor(zonemode bool, keyStore keystore.DataEncryptorKeyStore) (*DataEncryptor, error) {
return &DataEncryptor{zoneMode: zonemode, keyStore: keyStore, needSkipEncryptionFunc: standaloneEncryptorFilterFunction}, nil
}
// EncryptWithZoneID data using AcraBlock and ZoneID
func (d *DataEncryptor) EncryptWithZoneID(zoneID, data []byte, setting config.ColumnEncryptionSetting) ([]byte, error) {
if d.needSkipEncryptionFunc(setting) {
return data, nil
}
// skip already encrypted AcraBlock
if _, _, err := ExtractAcraBlockFromData(data); err == nil {
return data, nil
}
if setting.ShouldReEncryptAcraStructToAcraBlock() {
// decrypt AcraStruct to encrypt it with AcraBlock
if err := acrastruct.ValidateAcraStructLength(data); err == nil {
dataContext := base.NewDataProcessorContext(d.keyStore)
accessContext := base.NewAccessContext(base.WithZoneMode(d.zoneMode))
accessContext.SetZoneID(zoneID)
dataContext.Context = base.SetAccessContextToContext(context.Background(), accessContext)
decrypted, err := base.DecryptProcessor{}.Process(data, dataContext)
if err != nil {
return data, err
}
data = decrypted
}
}
keys, err := d.keyStore.GetZoneIDSymmetricKeys(zoneID)
if err != nil {
return data, err
}
if len(keys) == 0 {
return data, keystore.ErrKeysNotFound
}
return CreateAcraBlock(data, keys[0], zoneID)
}
// EncryptWithClientID encrypt data using AcraBlock
func (d *DataEncryptor) EncryptWithClientID(clientID, data []byte, setting config.ColumnEncryptionSetting) ([]byte, error) {
if d.needSkipEncryptionFunc(setting) {
return data, nil
}
// skip already encrypted AcraBlock
if _, _, err := ExtractAcraBlockFromData(data); err == nil {
return data, nil
}
if setting.ShouldReEncryptAcraStructToAcraBlock() {
// decrypt AcraStruct to encrypt it with AcraBlock
if err := acrastruct.ValidateAcraStructLength(data); err == nil {
dataContext := base.NewDataProcessorContext(d.keyStore)
accessContext := base.NewAccessContext(base.WithClientID(clientID))
dataContext.Context = base.SetAccessContextToContext(context.Background(), accessContext)
decrypted, err := base.DecryptProcessor{}.Process(data, dataContext)
if err != nil {
return data, err
}
data = decrypted
}
}
keys, err := d.keyStore.GetClientIDSymmetricKeys(clientID)
if err != nil {
return data, err
}
if len(keys) == 0 {
return data, keystore.ErrKeysNotFound
}
return CreateAcraBlock(data, keys[0], nil)
}