-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption_session.go
210 lines (183 loc) · 7.69 KB
/
encryption_session.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
package main
/*
#include "./seald_sdk.h"
*/
import "C"
import (
"github.com/seald/go-seald-sdk/common_models"
"github.com/seald/go-seald-sdk/sdk"
"github.com/ztrue/tracerr"
"sync"
"unsafe"
)
// Encryption Session
func encryptionSessionToGo(es *C.SealdEncryptionSession) *sdk.EncryptionSession {
return (*sdk.EncryptionSession)(unsafe.Pointer(es))
}
var sealdEncryptionSessionRefMap = sync.Map{}
func goEncryptionSessionToC(es *sdk.EncryptionSession) *C.SealdEncryptionSession {
sealdEncryptionSessionRefMap.Store(uintptr(unsafe.Pointer(es)), es)
return (*C.SealdEncryptionSession)(unsafe.Pointer(es))
}
//export SealdEncryptionSession_Free
func SealdEncryptionSession_Free(es *C.SealdEncryptionSession) {
sealdEncryptionSessionRefMap.Delete(uintptr(unsafe.Pointer(es)))
}
//export SealdEncryptionSession_Id
func SealdEncryptionSession_Id(es *C.SealdEncryptionSession) *C.char {
return C.CString(encryptionSessionToGo(es).Id)
}
//export SealdEncryptionSession_RetrievalDetails
func SealdEncryptionSession_RetrievalDetails(es *C.SealdEncryptionSession) *C.SealdEncryptionSessionRetrievalDetails {
return retrievalDetailsFromGo(encryptionSessionToGo(es).RetrievalDetails)
}
//export SealdEncryptionSession_AddRecipients
func SealdEncryptionSession_AddRecipients(es *C.SealdEncryptionSession, recipients *C.SealdRecipientsWithRightsArray, result **C.SealdActionStatusArray, err_ **C.SealdError) C.int {
resp, err := encryptionSessionToGo(es).AddRecipients(recipientsWithRightsArrayToGo(recipients).getSlice())
if err != nil {
*err_ = sealdErrorFromGo(tracerr.Wrap(err))
return C.int(-1)
}
*result = actionStatusArrayFromAddKey(resp)
return C.int(0)
}
//export SealdEncryptionSession_AddProxySession
func SealdEncryptionSession_AddProxySession(es *C.SealdEncryptionSession, proxySessionId *C.char, readRight C.int, forwardRight C.int, revokeRight C.int, err_ **C.SealdError) C.int {
err := encryptionSessionToGo(es).AddProxySession(
C.GoString(proxySessionId),
&sdk.RecipientRights{
Read: readRight == 1,
Forward: forwardRight == 1,
Revoke: revokeRight == 1,
},
)
if err != nil {
*err_ = sealdErrorFromGo(tracerr.Wrap(err))
return C.int(-1)
}
return C.int(0)
}
//export SealdEncryptionSession_RevokeRecipients
func SealdEncryptionSession_RevokeRecipients(es *C.SealdEncryptionSession, recipientsIds *C.SealdStringArray, proxySessionsIds *C.SealdStringArray, result **C.SealdRevokeResult, err_ **C.SealdError) C.int {
resp, err := encryptionSessionToGo(es).RevokeRecipients(stringArrayToGo(recipientsIds).getSlice(), stringArrayToGo(proxySessionsIds).getSlice())
if err != nil {
*err_ = sealdErrorFromGo(tracerr.Wrap(err))
return C.int(-1)
}
*result = revokeResultFromGo(resp.UserIds, resp.ProxyMkIds)
return C.int(0)
}
//export SealdEncryptionSession_RevokeAll
func SealdEncryptionSession_RevokeAll(es *C.SealdEncryptionSession, result **C.SealdRevokeResult, err_ **C.SealdError) C.int {
resp, err := encryptionSessionToGo(es).RevokeAll()
if err != nil {
*err_ = sealdErrorFromGo(tracerr.Wrap(err))
return C.int(-1)
}
*result = revokeResultFromGo(resp.RevokeAll.UserIds, resp.RevokeAll.ProxyMkIds)
return C.int(0)
}
//export SealdEncryptionSession_RevokeOthers
func SealdEncryptionSession_RevokeOthers(es *C.SealdEncryptionSession, result **C.SealdRevokeResult, err_ **C.SealdError) C.int {
resp, err := encryptionSessionToGo(es).RevokeOthers()
if err != nil {
*err_ = sealdErrorFromGo(tracerr.Wrap(err))
return C.int(-1)
}
*result = revokeResultFromGo(resp.RevokeAll.UserIds, resp.RevokeAll.ProxyMkIds)
return C.int(0)
}
//export SealdEncryptionSession_EncryptMessage
func SealdEncryptionSession_EncryptMessage(es *C.SealdEncryptionSession, clearMessage *C.char, result **C.char, err_ **C.SealdError) C.int {
res, err := encryptionSessionToGo(es).EncryptMessage(C.GoString(clearMessage))
if err != nil {
*err_ = sealdErrorFromGo(tracerr.Wrap(err))
return C.int(-1)
}
*result = C.CString(res)
return C.int(0)
}
//export SealdEncryptionSession_DecryptMessage
func SealdEncryptionSession_DecryptMessage(es *C.SealdEncryptionSession, encryptedMessage *C.char, result **C.char, err_ **C.SealdError) C.int {
res, err := encryptionSessionToGo(es).DecryptMessage(C.GoString(encryptedMessage))
if err != nil {
*err_ = sealdErrorFromGo(tracerr.Wrap(err))
return C.int(-1)
}
*result = C.CString(res)
return C.int(0)
}
//export SealdEncryptionSession_EncryptFile
func SealdEncryptionSession_EncryptFile(es *C.SealdEncryptionSession, clearFile *C.uchar, clearFileLen C.int, filename *C.char, result **C.uchar, resultLen *C.int, err_ **C.SealdError) C.int {
clearFileSlice := C.GoBytes(unsafe.Pointer(clearFile), clearFileLen)
res, err := encryptionSessionToGo(es).EncryptFile(clearFileSlice, C.GoString(filename))
if err != nil {
*err_ = sealdErrorFromGo(tracerr.Wrap(err))
return C.int(-1)
}
*result = (*C.uchar)(C.CBytes(res))
*resultLen = C.int(len(res))
return C.int(0)
}
//export SealdEncryptionSession_DecryptFile
func SealdEncryptionSession_DecryptFile(es *C.SealdEncryptionSession, encryptedFile *C.uchar, encryptedFileLen C.int, result **C.SealdClearFile, err_ **C.SealdError) C.int {
encryptedFileSlice := C.GoBytes(unsafe.Pointer(encryptedFile), encryptedFileLen)
res, err := encryptionSessionToGo(es).DecryptFile(encryptedFileSlice)
if err != nil {
*err_ = sealdErrorFromGo(tracerr.Wrap(err))
return C.int(-1)
}
*result = clearFileFromCommon(res)
return C.int(0)
}
//export SealdEncryptionSession_EncryptFileFromPath
func SealdEncryptionSession_EncryptFileFromPath(es *C.SealdEncryptionSession, clearFilePath *C.char, result **C.char, err_ **C.SealdError) C.int {
res, err := encryptionSessionToGo(es).EncryptFileFromPath(C.GoString(clearFilePath))
if err != nil {
*err_ = sealdErrorFromGo(tracerr.Wrap(err))
return C.int(-1)
}
*result = C.CString(res)
return C.int(0)
}
//export SealdEncryptionSession_DecryptFileFromPath
func SealdEncryptionSession_DecryptFileFromPath(es *C.SealdEncryptionSession, encryptedFilePath *C.char, result **C.char, err_ **C.SealdError) C.int {
res, err := encryptionSessionToGo(es).DecryptFileFromPath(C.GoString(encryptedFilePath))
if err != nil {
*err_ = sealdErrorFromGo(tracerr.Wrap(err))
return C.int(-1)
}
*result = C.CString(res)
return C.int(0)
}
//export SealdEncryptionSession_AddTmrAccess
func SealdEncryptionSession_AddTmrAccess(es *C.SealdEncryptionSession, authFactorType *C.char, authFactorValue *C.char, overEncryptionKey *C.uchar, overEncryptionKeyLen C.int, readRight C.int, forwardRight C.int, revokeRight C.int, result **C.char, err_ **C.SealdError) C.int {
overEncryptionKeyBytes := C.GoBytes(unsafe.Pointer(overEncryptionKey), overEncryptionKeyLen)
recipientRights := &sdk.RecipientRights{
Read: readRight == 1,
Forward: forwardRight == 1,
Revoke: revokeRight == 1,
}
authFactor := &common_models.AuthFactor{
Type: C.GoString(authFactorType),
Value: C.GoString(authFactorValue),
}
recipient := &sdk.TmrRecipientWithRights{AuthFactor: authFactor, Rights: recipientRights, OverEncryptionKey: overEncryptionKeyBytes}
tmrAccessId, err := encryptionSessionToGo(es).AddTmrAccess(recipient)
if err != nil {
*err_ = sealdErrorFromGo(tracerr.Wrap(err))
return C.int(-1)
}
*result = C.CString(tmrAccessId)
return C.int(0)
}
//export SealdEncryptionSession_AddMultipleTmrAccesses
func SealdEncryptionSession_AddMultipleTmrAccesses(es *C.SealdEncryptionSession, recipients *C.SealdTmrRecipientsWithRightsArray, result **C.SealdActionStatusArray, err_ **C.SealdError) C.int {
resp, err := encryptionSessionToGo(es).AddMultipleTmrAccesses(tmrRecipientsWithRightsArrayToGo(recipients).getSlice())
if err != nil {
*err_ = sealdErrorFromGo(tracerr.Wrap(err))
return C.int(-1)
}
*result = actionStatusArrayFromAddTmrAccess(resp)
return C.int(0)
}