/
wasm.go
288 lines (237 loc) · 8.27 KB
/
wasm.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
package e2ee
import (
"encoding/base64"
"syscall/js"
"errors"
"fmt"
)
type remoteSecretKeyMaterial struct {
keyID uint32
secretKeyMaterial []byte
}
// js にわたすための変換前の処理
type startSessionResult struct {
selfConnectionID string
selfKeyID uint32
selfSecretKeyMaterial []byte
remoteSecretKeyMaterials map[string]remoteSecretKeyMaterial
messages [][]byte
}
type stopSessionResult struct {
selfConnectionID string
selfKeyID uint32
selfSecretKeyMaterial []byte
messages [][]byte
}
type receiveMessageResult struct {
remoteSecretKeyMaterials map[string]remoteSecretKeyMaterial
messages [][]byte
}
const (
connectionIDLength = 26
)
// RegisterCallbacks ...
func RegisterCallbacks(version string) {
js.Global().Set("E2EE", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
e := newE2EE(version)
this.Set("version", js.FuncOf(e.wasmVersion))
this.Set("init", js.FuncOf(e.wasmInitE2EE))
this.Set("start", js.FuncOf(e.wasmStartE2EE))
this.Set("startSession", js.FuncOf(e.wasmStartSession))
this.Set("stopSession", js.FuncOf(e.wasmStopSession))
this.Set("receiveMessage", js.FuncOf(e.wasmReceiveMessage))
this.Set("addPreKeyBundle", js.FuncOf(e.wasmAddPreKeyBundle))
this.Set("selfFingerprint", js.FuncOf(e.wasmSelfFingerprint))
this.Set("remoteFingerprints", js.FuncOf(e.wasmRemoteFingerprints))
return js.Undefined()
}))
js.Global().Get("E2EE").Set("version", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return version
}))
}
func (e *e2ee) wasmVersion(this js.Value, args []js.Value) interface{} {
return e.getVersion()
}
func (e *e2ee) wasmInitE2EE(this js.Value, args []js.Value) interface{} {
if err := e.init(); err != nil {
// TODO(v): エラーメッセージを考える
return toJsReturnValue(nil, jsError(errors.New("InitError")))
}
return map[string]interface{}{
"preKeyBundle": e.selfPreKeyBundle.toJsValue(),
}
}
func (e *e2ee) wasmStartE2EE(this js.Value, args []js.Value) interface{} {
// TODO(v): バリデーション
selfConnectionID := args[0].String()
if len(selfConnectionID) != connectionIDLength {
return toJsReturnValue(nil, jsError(errors.New("UnexpectedSelfConnectionIDError")))
}
secretKeyMaterial := e.start(selfConnectionID)
result := map[string]interface{}{
"selfKeyId": e.keyID,
"selfSecretKeyMaterial": bytesToUint8Array(secretKeyMaterial),
}
return toJsReturnValue(result, nil)
}
func (p preKeyBundle) toJsValue() map[string]interface{} {
base64edIdentityKey := base64.StdEncoding.EncodeToString(p.identityKey)
base64edSignedPreKey := base64.StdEncoding.EncodeToString(p.signedPreKey[:])
base64edPreKeySignature := base64.StdEncoding.EncodeToString(p.preKeySignature)
return map[string]interface{}{
"identityKey": base64edIdentityKey,
"signedPreKey": base64edSignedPreKey,
"preKeySignature": base64edPreKeySignature,
}
}
func (e *e2ee) wasmStartSession(this js.Value, args []js.Value) interface{} {
// 相手の connectionID を追加
remoteConnectionID := args[0].String()
if len(remoteConnectionID) != connectionIDLength {
return toJsReturnValue(nil, jsError(errors.New("UnexpectedRemoteConnectionIDError")))
}
base64edIdentityKey := args[1].String()
identityKey, err := base64.StdEncoding.DecodeString(base64edIdentityKey)
if err != nil {
return toJsReturnValue(nil, jsError(err))
}
base64edSignedPreKey := args[2].String()
signedPreKey, err := base64.StdEncoding.DecodeString(base64edSignedPreKey)
if err != nil {
return toJsReturnValue(nil, jsError(err))
}
base64edPreKeySignature := args[3].String()
preKeySignature, err := base64.StdEncoding.DecodeString(base64edPreKeySignature)
if err != nil {
return toJsReturnValue(nil, jsError(err))
}
result, err := e.startSession(remoteConnectionID, identityKey, signedPreKey, preKeySignature)
if err != nil {
return toJsReturnValue(nil, jsError(err))
}
return toJsReturnValue(result.toJsValue(), nil)
}
func (e *e2ee) wasmStopSession(this js.Value, args []js.Value) interface{} {
remoteConnectionID := args[0].String()
if len(remoteConnectionID) != connectionIDLength {
return toJsReturnValue(nil, jsError(errors.New("UnexpectedRemoteConnectionIDError")))
}
result, err := e.stopSession(remoteConnectionID)
if err != nil {
return toJsReturnValue(nil, jsError(err))
}
return toJsReturnValue(result.toJsValue(), nil)
}
func (e *e2ee) wasmReceiveMessage(this js.Value, args []js.Value) interface{} {
data := make([]byte, args[0].Get("length").Int())
_ = js.CopyBytesToGo(data, args[0])
result, err := e.receiveMessage(data)
if err != nil {
return toJsReturnValue(nil, jsError(err))
}
return toJsReturnValue(result.toJsValue(), nil)
}
func (e *e2ee) wasmAddPreKeyBundle(this js.Value, args []js.Value) interface{} {
remoteConnectionID := args[0].String()
if len(remoteConnectionID) != connectionIDLength {
return jsError(errors.New("UnexpectedRemoteConnectionIDError"))
}
base64edIdentityKey := args[1].String()
identityKey, err := base64.StdEncoding.DecodeString(base64edIdentityKey)
if err != nil {
return jsError(err)
}
base64edSignedPreKey := args[2].String()
signedPreKey, err := base64.StdEncoding.DecodeString(base64edSignedPreKey)
if err != nil {
return jsError(err)
}
base64edPreKeySignature := args[3].String()
preKeySignature, err := base64.StdEncoding.DecodeString(base64edPreKeySignature)
if err != nil {
return jsError(err)
}
if err := e.addPreKeyBundle(remoteConnectionID, identityKey, signedPreKey, preKeySignature); err != nil {
return jsError(err)
}
return nil
}
func (e *e2ee) wasmSelfFingerprint(this js.Value, args []js.Value) interface{} {
return e.selfFingerprint()
}
func (e *e2ee) wasmRemoteFingerprints(this js.Value, args []js.Value) interface{} {
return e.remoteFingerprints()
}
func (r startSessionResult) toJsValue() map[string]interface{} {
secretKeyMaterials := make(map[string]interface{})
for connectionID, v := range r.remoteSecretKeyMaterials {
secretKeyMaterials[connectionID] = map[string]interface{}{
"keyId": v.keyID,
"secretKeyMaterial": bytesToUint8Array(v.secretKeyMaterial),
}
}
var messages []interface{}
for _, s := range r.messages {
messages = append(messages, bytesToUint8Array(s))
}
return map[string]interface{}{
"selfConnectionId": r.selfConnectionID,
"selfKeyId": r.selfKeyID,
"selfSecretKeyMaterial": bytesToUint8Array(r.selfSecretKeyMaterial),
"remoteSecretKeyMaterials": secretKeyMaterials,
"messages": messages,
}
}
func (r stopSessionResult) toJsValue() map[string]interface{} {
var messages []interface{}
for _, s := range r.messages {
messages = append(messages, bytesToUint8Array(s))
}
return map[string]interface{}{
"selfConnectionId": r.selfConnectionID,
"selfKeyId": r.selfKeyID,
"selfSecretKeyMaterial": bytesToUint8Array(r.selfSecretKeyMaterial),
"messages": messages,
}
}
func (r receiveMessageResult) toJsValue() map[string]interface{} {
secretKeyMaterials := make(map[string]interface{})
for connectionID, v := range r.remoteSecretKeyMaterials {
secretKeyMaterials[connectionID] = map[string]interface{}{
"keyId": v.keyID,
"secretKeyMaterial": bytesToUint8Array(v.secretKeyMaterial),
}
}
var messages []interface{}
for _, s := range r.messages {
messages = append(messages, bytesToUint8Array(s))
}
return map[string]interface{}{
"remoteSecretKeyMaterials": secretKeyMaterials,
"messages": messages,
}
}
func bytesToUint8Array(data []byte) js.Value {
d := js.Global().Get("Uint8Array").New(len(data))
_ = js.CopyBytesToJS(d, data)
return d
}
// エラーも js に返すため [jsValue, error] へ
func toJsReturnValue(jsValue, err interface{}) []interface{} {
if jsValue == nil {
jsValue = js.Undefined()
}
if err == nil {
err = js.Undefined()
}
return []interface{}{jsValue, err}
}
// error を js の Error オブジェクトへ
func jsError(err error) js.Value {
return js.Global().Get("Error").New(err.Error())
}
// console にエラーを表示させる
// TODO: デバッグ用のため、不要な場合は削除する
func jsConsole(level, format string, args ...interface{}) {
js.Global().Get("console").Call(level, fmt.Sprintf(format, args...))
}