This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathprofile.go
322 lines (294 loc) · 9.5 KB
/
profile.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
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
package profiles
import (
"bytes"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/rand"
zkgroup "github.com/nanu-c/zkgroup"
uuidUtil "github.com/satori/go.uuid"
"github.com/signal-golang/textsecure/config"
"github.com/signal-golang/textsecure/contacts"
"github.com/signal-golang/textsecure/crypto"
"github.com/signal-golang/textsecure/transport"
log "github.com/sirupsen/logrus"
)
const (
PROFILE_PATH = "/v1/profile/%s"
PROFILE_CREDENTIAL_PATH = "/v1/profile/%s/%s/%s"
NAME_PADDED_LENGTH = 53
)
// Profile ...
type ProfileSettings struct {
Version string `json:"version"`
Name []byte `json:"name"`
About []byte `json:"about"`
AboutEmoji []byte `json:"aboutEmoji"`
paymentAddress []byte `json:"paymentAddress"`
Avatar bool `json:"avatar"`
Commitment []byte `json:"commitment"`
BadgeIds []string `json:"badgeIds"`
}
// GenerateProfileKey generates a new ProfileKey
func GenerateProfileKey() []byte {
profileKey := make([]byte, 32)
rand.Read(profileKey)
return profileKey
}
func uuidToByte(id string) []byte {
s, _ := uuidUtil.FromString(id)
return s.Bytes()
}
// UpdateProfile ...
func UpdateProfile(profileKey []byte, uuid, name string) error {
log.Debugln("[textsecure] UpdateProfile", uuid, name)
uuidByte := uuidToByte(uuid)
profile := ProfileSettings{}
version, err := zkgroup.ProfileKeyGetProfileKeyVersion(profileKey, uuidByte)
if err != nil {
return err
}
commitment, err := zkgroup.ProfileKeyGetCommitment(profileKey, uuidByte)
if err != nil {
return err
}
nameCiphertext, err := encryptName(profileKey, []byte(name), NAME_PADDED_LENGTH)
if err != nil {
return err
}
profile.Version = string(version[:])
profile.Commitment = commitment
profile.Name = nameCiphertext
profile.Avatar = false
err = writeOwnProfile(profile)
if err != nil {
return err
}
myProfile, err := GetProfile(uuid, profileKey)
if err != nil {
return err
}
config.ConfigFile.AccountCapabilities = myProfile.Capabilities
return nil
}
// WriteProfile ...
func writeOwnProfile(profile ProfileSettings) error {
body, err := json.Marshal(profile)
if err != nil {
return err
}
transport.Transport.PutJSON(fmt.Sprintf(PROFILE_PATH, ""), body)
return nil
}
func encryptName(key, input []byte, paddedLength int) ([]byte, error) {
inputLength := len(input)
if inputLength > paddedLength {
return nil, errors.New("Input too long")
}
padded := append(input, make([]byte, paddedLength-inputLength)...)
nonce := make([]byte, 12)
rand.Read(nonce)
ciphertext, err := crypto.AesgcmEncrypt(key, nonce, padded)
if err != nil {
return nil, err
}
return append(nonce, ciphertext...), nil
}
func decryptName(key, nonceAndCiphertext []byte) ([]byte, error) {
if len(nonceAndCiphertext) < 12+16+1 {
return nil, errors.New("nonceAndCipher too short")
}
nonce := nonceAndCiphertext[:12]
ciphertext := nonceAndCiphertext[12:]
padded, err := crypto.AesgcmDecrypt(key, nonce, ciphertext, []byte{})
if err != nil {
return nil, err
}
paddedLength := len(padded)
plaintextLength := 0
for i := paddedLength - 1; i >= 0; i-- {
if padded[i] != byte(0) {
plaintextLength = i + 1
break
}
}
return padded[:plaintextLength], nil
}
func getCommitment(profileKey, uuid []byte) ([]byte, error) {
return zkgroup.ProfileKeyGetCommitment(profileKey, uuid)
}
// Profile describes the profile type
type Profile struct {
IdentityKey string `json:"identityKey"`
UnidentifiedAccess string `json:"unidentifiedAccess"`
UnrestrictedUnidentifiedAccess bool `json:"unrestrictedUnidentifiedAccess"`
Capabilities config.AccountCapabilities `json:"capabilities"`
Badges []string `json:"badges"`
UUID string `json:"uuid"`
Name string `json:"name"`
About string `json:"about"`
AboutEmoji string `json:"aboutEmoji"`
Avatar string `json:"avatar"`
PaymentAddress string `json:"paymentAddress"`
Credential []byte `json:"credential"`
}
func GetProfile(UUID string, profileKey []byte) (*Profile, error) {
resp, err := transport.Transport.Get(fmt.Sprintf(PROFILE_PATH, UUID))
profile := &Profile{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&profile)
if err != nil {
log.Debugln("[textsecure] GetProfile", err)
return nil, err
} else {
err = decryptProfile(profileKey, profile)
if err != nil {
log.Debugln("[textsecure] ", err)
return nil, err
}
}
return profile, nil
}
func GetProfileAndCredential(UUID string, profileKey []byte) (*Profile, error) {
log.Infoln("[textsecure] GetProfileAndCredential")
uuid, err := uuidUtil.FromString(UUID)
if err != nil {
log.Debugln("[textsecure] GetProfileAndCredential", err)
return nil, err
}
zkGroupServerPublicParams, _ := base64.StdEncoding.DecodeString(config.ZKGROUP_SERVER_PUBLIC_PARAMS)
requestContext, err := zkgroup.CreateProfileKeyCredentialRequestContext(zkGroupServerPublicParams, uuid.Bytes(), profileKey)
if err != nil {
log.Debugln("[textsecure] createProfileCredentialRequest", err)
return nil, err
}
credentialsRequest, err := requestContext.ProfileKeyCredentialRequestContextGetRequest()
if err != nil {
log.Debugln("[textsecure] GetProfileAndCredential", err)
return nil, err
}
credentialsRequestHex := hex.EncodeToString(credentialsRequest)
version, err := zkgroup.ProfileKeyGetProfileKeyVersion(profileKey, uuid.Bytes())
if err != nil {
log.Debugln("[textsecure] GetProfileAndCredential", err)
return nil, err
}
resp, err := transport.Transport.Get(fmt.Sprintf(PROFILE_CREDENTIAL_PATH, UUID, version, credentialsRequestHex))
if err != nil {
log.Debugln("[textsecure] GetProfileAndCredential", err)
return nil, err
}
profile := &Profile{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&profile)
if err != nil {
log.Debugln("[textsecure] GetProfileAndCredential", err)
return nil, err
} else {
err = decryptProfile(profileKey, profile)
if err != nil {
log.Debugln("[textsecure] GetProfileAndCredential", err)
return nil, err
}
}
response := zkgroup.ProfileKeyCredentialResponse(profile.Credential)
if err != nil {
log.Debugln("[textsecure] GetProfileAndCredential", err)
return nil, err
}
serverPublicParams, err := zkgroup.NewServerPublicParams(zkGroupServerPublicParams)
if err != nil {
log.Debugln("[textsecure] GetProfileAndCredential", err)
return nil, err
}
credential, err := serverPublicParams.ReceiveProfileKeyCredential(requestContext, response)
if err != nil {
log.Debugln("[textsecure] GetProfileAndCredential", err)
return nil, err
}
log.Debugln("[textsecure] GetProfileAndCredential", len(credential))
profile.Credential = credential
return profile, err
}
func decryptProfile(profileKey []byte, profile *Profile) error {
log.Println("[textsecure] decryptProfile")
name, err := decryptString(profileKey, profile.Name)
if err != nil {
log.Debugln("[textsecure] decryptProfile name", err)
return err
}
profile.Name = name
if profile.About != "" {
about, err := decryptString(profileKey, profile.About)
if err != nil {
log.Debugln("[textsecure] decryptProfile about", err)
return err
}
profile.About = about
}
if profile.AboutEmoji != "" {
emoji, err := decryptString(profileKey, profile.AboutEmoji)
if err != nil {
log.Debugln("[textsecure] decryptProfile aboutEmoji", err)
return err
}
profile.AboutEmoji = emoji
}
identityKey, err := base64.StdEncoding.DecodeString(profile.IdentityKey)
if err != nil {
log.Debugln("[textsecure] decryptProfile identitykey", err)
return err
}
profile.IdentityKey = string(identityKey)
return nil
}
func decryptString(profileKey []byte, data string) (string, error) {
bData, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return "", err
}
dData, err := decryptName(profileKey, bData)
if err != nil {
return "", err
}
return string(dData), nil
}
func createProfileCredentialRequest(uuid, profileKey []byte) (zkgroup.ProfileKeyCredentialRequest, error) {
zkGroupServerPublicParams, _ := base64.StdEncoding.DecodeString(config.ZKGROUP_SERVER_PUBLIC_PARAMS)
requestContext, err := zkgroup.CreateProfileKeyCredentialRequestContext(zkGroupServerPublicParams, uuid, profileKey)
if err != nil {
log.Debugln("[textsecure] createProfileCredentialRequest", err)
return nil, err
}
return requestContext.ProfileKeyCredentialRequestContextGetRequest()
}
// GetProfileE164 get a profile by a phone number
func GetProfileE164(tel string) (contacts.Contact, error) {
resp, err := transport.Transport.Get(fmt.Sprintf(PROFILE_PATH, tel))
if err != nil {
log.Errorln("[textsecure] GetProfileE164 ", err)
}
profile := &Profile{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&profile)
if err != nil {
log.Errorln("[textsecure] GetProfileE164 ", err)
}
avatar, _ := GetAvatar(profile.Avatar)
buf := new(bytes.Buffer)
buf.ReadFrom(avatar)
c := contacts.Contacts[profile.UUID]
avatarDecrypted, err := decryptAvatar(buf.Bytes(), []byte(profile.IdentityKey))
if err != nil {
log.Errorln("[textsecure] GetProfileE164 ", err)
}
c.Name = profile.Name
c.UUID = profile.UUID
c.HasAvatar = true
c.AvatarImg = avatarDecrypted
contacts.Contacts[c.UUID] = c
contacts.WriteContactsToPath()
return c, nil
}