-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlibasherah.go
More file actions
374 lines (314 loc) · 11.7 KB
/
libasherah.go
File metadata and controls
374 lines (314 loc) · 11.7 KB
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
package main
import (
"C"
)
import (
"encoding/json"
"errors"
"fmt"
"os"
"sync/atomic"
"github.com/godaddy/cobhan-go"
"unsafe"
"github.com/godaddy/asherah-cobhan/internal/asherah"
"github.com/godaddy/asherah-cobhan/internal/log"
"github.com/godaddy/asherah/go/appencryption"
)
var EstimatedIntermediateKeyOverhead = 0
var nullDataCheck atomic.Bool
func main() {
}
//export Shutdown
func Shutdown() {
log.DebugLog("Asherah shutdown")
asherah.Shutdown()
}
type Env map[string]string
/*
Work-around to load environment variables needed by Asherah dependencies,
because sometimes Go os.Getenv() doesn't see variables set by C.setenv().
References:
https://github.com/golang/go/wiki/cgo#environmental-variables
https://github.com/golang/go/issues/27693
*/
//export SetEnv
func SetEnv(envJson unsafe.Pointer) (result int32) {
defer func() {
if r := recover(); r != nil {
log.ErrorLogf("SetEnv: Panic: %v", r)
result = ERR_PANIC
}
}()
cobhan.AllowTempFileBuffers(false)
env := Env{}
result = cobhan.BufferToJsonStruct(envJson, &env)
if result != cobhan.ERR_NONE {
log.ErrorLogf("Failed to deserialize environment JSON string %v", cobhan.CobhanErrorToString(result))
return result
}
for k, v := range env {
if err := os.Setenv(k, v); err != nil {
log.ErrorLogf("Failed to set environment variable %v: %v", k, err)
return ERR_BAD_CONFIG
}
}
return cobhan.ERR_NONE
}
//export SetupJson
func SetupJson(configJson unsafe.Pointer) (result int32) {
defer func() {
if r := recover(); r != nil {
log.ErrorLogf("SetupJson: Panic: %v", r)
panic(r)
}
}()
cobhan.AllowTempFileBuffers(false)
options := &asherah.Options{}
result = cobhan.BufferToJsonStruct(configJson, options)
if result != cobhan.ERR_NONE {
log.ErrorLogf("Failed to deserialize configuration string %v", cobhan.CobhanErrorToString(result))
configString, stringResult := cobhan.BufferToString(configJson)
if stringResult != cobhan.ERR_NONE {
log.ErrorLogf("Could not convert configJson to string: %v", cobhan.CobhanErrorToString(stringResult))
return result
}
log.ErrorLogf("Could not deserialize: %v", configString)
return result
}
log.EnableVerboseLog(options.Verbose)
log.DebugLog("Successfully deserialized config JSON")
EstimatedIntermediateKeyOverhead = len(options.ProductID) + len(options.ServiceName)
cobhan.CopyBuffers(options.DisableZeroCopy)
nullDataCheck.Store(options.NullDataCheck)
err := asherah.Setup(options)
if err == asherah.ErrAsherahAlreadyInitialized {
log.ErrorLog("Setup failed: asherah is already initialized")
log.ErrorLogf("Setup: asherah.Setup returned %v", err)
return ERR_ALREADY_INITIALIZED
}
if err != nil {
log.ErrorLog("Setup failed due to bad config?")
log.ErrorLogf("Setup: asherah.Setup returned %v", err)
return ERR_BAD_CONFIG
}
log.DebugLog("Successfully configured asherah")
return cobhan.ERR_NONE
}
//export EstimateBuffer
func EstimateBuffer(dataLen int32, partitionLen int32) int32 {
estimatedDataLen := ((int(dataLen) + EstimatedEncryptionOverhead + 2) / 3) * 4
result := int32(cobhan.BUFFER_HEADER_SIZE + EstimatedEnvelopeOverhead + EstimatedIntermediateKeyOverhead + int(partitionLen) + estimatedDataLen)
return result
}
func EstimateBufferInt(dataLen int, partitionLen int) int {
return int(EstimateBuffer(int32(dataLen), int32(partitionLen)))
}
//export Decrypt
func Decrypt(partitionIdPtr unsafe.Pointer, encryptedDataPtr unsafe.Pointer, encryptedKeyPtr unsafe.Pointer,
created int64, parentKeyIdPtr unsafe.Pointer, parentKeyCreated int64, outputDecryptedDataPtr unsafe.Pointer) (result int32) {
defer func() {
if r := recover(); r != nil {
log.ErrorLogf("Decrypt: Panic: %v", r)
result = ERR_PANIC
}
}()
var encryptedData []byte
encryptedData, result = cobhan.BufferToBytes(encryptedDataPtr)
if result != cobhan.ERR_NONE {
log.ErrorLogf("Decrypt failed: Failed to convert encryptedDataPtr cobhan buffer to bytes %v", cobhan.CobhanErrorToString(result))
return result
}
var encryptedKey []byte
encryptedKey, result = cobhan.BufferToBytes(encryptedKeyPtr)
if result != cobhan.ERR_NONE {
log.ErrorLogf("Decrypt failed: Failed to convert encryptedKeyPtr cobhan buffer to bytes %v", cobhan.CobhanErrorToString(result))
return result
}
var parentKeyId string
parentKeyId, result = cobhan.BufferToString(parentKeyIdPtr)
if result != cobhan.ERR_NONE {
log.ErrorLogf("Decrypt failed: Failed to convert parentKeyIdPtr cobhan buffer to string %v", cobhan.CobhanErrorToString(result))
return result
}
drr := appencryption.DataRowRecord{
Data: encryptedData,
Key: &appencryption.EnvelopeKeyRecord{
EncryptedKey: encryptedKey,
Created: created,
ParentKeyMeta: &appencryption.KeyMeta{
ID: parentKeyId,
Created: parentKeyCreated,
},
},
}
var data []byte
var err error
data, result, err = decryptData(partitionIdPtr, &drr)
if result != cobhan.ERR_NONE {
log.ErrorLogf("Failed to decrypt data %v", cobhan.CobhanErrorToString(result))
log.ErrorLogf("Decrypt: decryptData returned %v", err)
return result
}
return cobhan.BytesToBuffer(data, outputDecryptedDataPtr)
}
//export Encrypt
func Encrypt(partitionIdPtr unsafe.Pointer, dataPtr unsafe.Pointer, outputEncryptedDataPtr unsafe.Pointer,
outputEncryptedKeyPtr unsafe.Pointer, outputCreatedPtr unsafe.Pointer, outputParentKeyIdPtr unsafe.Pointer,
outputParentKeyCreatedPtr unsafe.Pointer) (result int32) {
defer func() {
if r := recover(); r != nil {
log.ErrorLogf("Encrypt: Panic: %v", r)
result = ERR_PANIC
}
}()
inputAlreadyNull := false
if nullDataCheck.Load() && cobhan.IsBufferAllNulls(dataPtr) {
log.ErrorLogf("Encrypt: input data buffer is all null before encryption (len=%d)", cobhan.BufferLength(dataPtr))
inputAlreadyNull = true
}
var drr *appencryption.DataRowRecord
var err error
drr, result, err = encryptData(partitionIdPtr, dataPtr)
if result != cobhan.ERR_NONE {
log.ErrorLogf("Failed to encrypt data %v", cobhan.CobhanErrorToString(result))
log.ErrorLogf("Encrypt failed: encryptData returned %v", err)
return result
}
if !inputAlreadyNull && nullDataCheck.Load() && cobhan.IsBufferAllNulls(dataPtr) {
log.ErrorLogf("Encrypt: input data buffer was nulled during encryption (len=%d)", cobhan.BufferLength(dataPtr))
}
result = cobhan.BytesToBuffer(drr.Data, outputEncryptedDataPtr)
if result != cobhan.ERR_NONE {
log.ErrorLogf("Encrypted data length: %v", len(drr.Data))
log.ErrorLogf("Encrypt failed: BytesToBuffer returned %v for outputEncryptedDataPtr", cobhan.CobhanErrorToString(result))
return result
}
result = cobhan.BytesToBuffer(drr.Key.EncryptedKey, outputEncryptedKeyPtr)
if result != cobhan.ERR_NONE {
log.ErrorLogf("Encrypt failed: BytesToBuffer returned %v for outputEncryptedKeyPtr", cobhan.CobhanErrorToString(result))
return result
}
result = cobhan.Int64ToBuffer(drr.Key.Created, outputCreatedPtr)
if result != cobhan.ERR_NONE {
log.ErrorLogf("Encrypt failed: Int64ToBuffer returned %v for outputCreatedPtr", cobhan.CobhanErrorToString(result))
return result
}
result = cobhan.StringToBuffer(drr.Key.ParentKeyMeta.ID, outputParentKeyIdPtr)
if result != cobhan.ERR_NONE {
log.ErrorLogf("Encrypt failed: BytesToBuffer returned %v for outputParentKeyIdPtr", cobhan.CobhanErrorToString(result))
return result
}
result = cobhan.Int64ToBuffer(drr.Key.ParentKeyMeta.Created, outputParentKeyCreatedPtr)
if result != cobhan.ERR_NONE {
log.ErrorLogf("Encrypt failed: BytesToBuffer returned %v for outputParentKeyCreatedPtr", cobhan.CobhanErrorToString(result))
return result
}
return cobhan.ERR_NONE
}
//export EncryptToJson
func EncryptToJson(partitionIdPtr unsafe.Pointer, dataPtr unsafe.Pointer, jsonPtr unsafe.Pointer) (result int32) {
defer func() {
if r := recover(); r != nil {
log.ErrorLogf("EncryptToJson: Panic: %v", r)
result = ERR_PANIC
}
}()
inputAlreadyNull := false
if nullDataCheck.Load() && cobhan.IsBufferAllNulls(dataPtr) {
log.ErrorLogf("EncryptToJson: input data buffer is all null before encryption (len=%d)", cobhan.BufferLength(dataPtr))
inputAlreadyNull = true
}
var drr *appencryption.DataRowRecord
var err error
drr, result, err = encryptData(partitionIdPtr, dataPtr)
if result != cobhan.ERR_NONE {
log.ErrorLogf("Failed to encrypt data %v", cobhan.CobhanErrorToString(result))
log.ErrorLogf("EncryptToJson failed: encryptData returned %v", err)
return result
}
if !inputAlreadyNull && nullDataCheck.Load() && cobhan.IsBufferAllNulls(dataPtr) {
log.ErrorLogf("EncryptToJson: input data buffer was nulled during encryption (len=%d)", cobhan.BufferLength(dataPtr))
}
result = cobhan.JsonToBuffer(drr, jsonPtr)
if result != cobhan.ERR_NONE {
if result == cobhan.ERR_BUFFER_TOO_SMALL {
outputBytes, err := json.Marshal(drr)
if err == nil {
log.ErrorLogf("EncryptToJson failed: JsonToBuffer: Output buffer needed %v bytes", len(outputBytes))
return result
}
}
log.ErrorLogf("EncryptToJson failed: JsonToBuffer returned %v for jsonPtr", cobhan.CobhanErrorToString(result))
return result
}
return cobhan.ERR_NONE
}
//export DecryptFromJson
func DecryptFromJson(partitionIdPtr unsafe.Pointer, jsonPtr unsafe.Pointer, dataPtr unsafe.Pointer) (result int32) {
defer func() {
if r := recover(); r != nil {
log.ErrorLogf("DecryptFromJson: Panic: %v", r)
result = ERR_PANIC
}
}()
var drr appencryption.DataRowRecord
result = cobhan.BufferToJsonStruct(jsonPtr, &drr)
if result != cobhan.ERR_NONE {
log.ErrorLogf("DecryptFromJson failed: Failed to convert cobhan buffer to JSON structs %v", cobhan.CobhanErrorToString(result))
return result
}
var data []byte
var err error
data, result, err = decryptData(partitionIdPtr, &drr)
if result != cobhan.ERR_NONE {
log.ErrorLogf("Failed to decrypt data %v", cobhan.CobhanErrorToString(result))
log.ErrorLogf("DecryptFromJson failed: decryptData returned %v", err)
return result
}
result = cobhan.BytesToBuffer(data, dataPtr)
if result != cobhan.ERR_NONE {
if result == cobhan.ERR_BUFFER_TOO_SMALL {
log.ErrorLogf("DecryptFromJson: BytesToBuffer: Output buffer needed %v bytes", len(data))
return result
}
log.ErrorLogf("DecryptFromJson failed: BytesToBuffer returned %v for dataPtr", cobhan.CobhanErrorToString(result))
return result
}
return cobhan.ERR_NONE
}
func encryptData(partitionIdPtr unsafe.Pointer, dataPtr unsafe.Pointer) (*appencryption.DataRowRecord, int32, error) {
partitionId, result := cobhan.BufferToString(partitionIdPtr)
if result != cobhan.ERR_NONE {
errorMessage := fmt.Sprintf("encryptData failed: Failed to convert cobhan buffer to string %v", cobhan.CobhanErrorToString(result))
return nil, result, errors.New(errorMessage)
}
data, result := cobhan.BufferToBytes(dataPtr)
if result != cobhan.ERR_NONE {
errorMessage := fmt.Sprintf("encryptData failed: Failed to convert cobhan buffer to bytes %v", cobhan.CobhanErrorToString(result))
return nil, result, errors.New(errorMessage)
}
drr, err := asherah.Encrypt(partitionId, data)
if err != nil {
if err == asherah.ErrAsherahNotInitialized {
return nil, ERR_NOT_INITIALIZED, err
}
return nil, ERR_ENCRYPT_FAILED, err
}
return drr, cobhan.ERR_NONE, nil
}
func decryptData(partitionIdPtr unsafe.Pointer, drr *appencryption.DataRowRecord) ([]byte, int32, error) {
partitionId, result := cobhan.BufferToString(partitionIdPtr)
if result != cobhan.ERR_NONE {
errorMessage := fmt.Sprintf("decryptData failed: Failed to convert cobhan buffer to string %v", cobhan.CobhanErrorToString(result))
log.ErrorLog(errorMessage)
return nil, result, errors.New(errorMessage)
}
data, err := asherah.Decrypt(partitionId, drr)
if err != nil {
if err == asherah.ErrAsherahNotInitialized {
return nil, ERR_NOT_INITIALIZED, err
}
return nil, ERR_DECRYPT_FAILED, err
}
return data, cobhan.ERR_NONE, nil
}