-
Notifications
You must be signed in to change notification settings - Fork 18
/
rest.go
148 lines (117 loc) · 4.23 KB
/
rest.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
package o3
import (
"crypto/rand"
"encoding/base64"
"errors"
"github.com/o3ma/o3rest/apiclient_pkg"
"github.com/o3ma/o3rest/models_pkg"
"golang.org/x/crypto/nacl/box"
)
// ThreemaRest provides convenient wrappers for task that require the use of Threemas REST API
type ThreemaRest struct {
client apiclient_pkg.APICLIENT_IMPL
}
// CreateIdentity generates a new NaCl Keypair, registers it with the Three servers and returns the assigned ID
func (tr ThreemaRest) CreateIdentity() (ThreemaID, error) {
// Get Keypair and nonce ready
publicKey, privateKey, err := box.GenerateKey(rand.Reader)
if err != nil {
return ThreemaID{}, err
}
pubkey := base64.StdEncoding.EncodeToString(publicKey[:])
// The nonce is hardcoded in threema
nonce := [24]byte{0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x49, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e}
// Request token and tokenRespKeyPub
idCreateParams := models_pkg.CreateRequest{
PublicKey: &pubkey}
challenge, err := tr.client.IdentityCreate(&idCreateParams)
if err != nil {
return ThreemaID{}, err
}
// Decode token and tokenRespPub
tokenRespKeyPub, err := base64.StdEncoding.DecodeString(*challenge.TokenRespKeyPub)
if err != nil {
return ThreemaID{}, err
}
var tokenPubKey [32]byte
copy(tokenPubKey[:], tokenRespKeyPub[:32])
token, err := base64.StdEncoding.DecodeString(*challenge.Token)
if err != nil {
return ThreemaID{}, err
}
// Compute the Response
tokenResponse := base64.StdEncoding.EncodeToString(box.Seal(nil, []byte(token), &nonce, &tokenPubKey, privateKey))
response := models_pkg.CreateStage2Request{
PublicKey: &pubkey,
Response: &tokenResponse,
Token: challenge.Token}
finalResult, err := tr.client.IdentityCreateStage2(&response)
if err != nil {
return ThreemaID{}, err
}
if !*finalResult.Success {
panic("Server responded with error!")
}
newID := ThreemaID{
ID: NewIDString(*finalResult.Identity),
Nick: NewPubNick(*finalResult.Identity),
LSK: *privateKey}
if !tr.setFeatureLevel(newID, 1) {
return ThreemaID{}, errors.New("failed to set feature level")
}
return newID, nil
}
// setFeatureLevel lets the threema servers know which features are supported by the client/id
// 0 = {Text, Pictures, Videos}, 1 = {0, Audio, Groups}, 2 = {1, Ballots}, 3 = {2, Files}
func (tr ThreemaRest) setFeatureLevel(thid ThreemaID, featurelevel int) (succes bool) {
featureLevelFloat := float64(featurelevel)
// The nonce is hardcoded in threema
nonce := [24]byte{0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x49, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e}
nonceB64 := base64.StdEncoding.EncodeToString(nonce[:])
idString := thid.String()
request := models_pkg.SetFeaturelevelRequest{
Identity: &idString,
FeatureLevel: &featureLevelFloat}
challenge, err := tr.client.IdentitySetFeaturelevel(&request)
// Decode token and tokenRespPub
tokenRespKeyPub, err := base64.StdEncoding.DecodeString(*challenge.TokenRespKeyPub)
if err != nil {
return false
}
var tokenPubKey [32]byte
copy(tokenPubKey[:], tokenRespKeyPub[:32])
token, err := base64.StdEncoding.DecodeString(*challenge.Token)
if err != nil {
return false
}
// Compute the Response
tokenResponse := base64.StdEncoding.EncodeToString(box.Seal(nil, []byte(token), &nonce, &tokenPubKey, &thid.LSK))
response := models_pkg.SetFeaturelevelStage2Request{
Response: &tokenResponse,
Identity: &idString,
Nonce: &nonceB64,
Token: challenge.Token,
FeatureLevel: &featureLevelFloat,
}
result, err := tr.client.IdentitySetFeaturelevelStage2(&response)
if err != nil {
return false
}
return *result.Success
}
// GetContactByID returns a ThreemaContact containing the public key as queried from the Threema servers
func (tr ThreemaRest) GetContactByID(thIDString IDString) (ThreemaContact, error) {
response, err := tr.client.IdentityById(thIDString.String())
if err != nil {
return ThreemaContact{}, err
}
pubKeyDecoded, err := base64.StdEncoding.DecodeString(*response.PublicKey)
if err != nil {
return ThreemaContact{}, err
}
var pubKey [32]byte
copy(pubKey[:], (pubKeyDecoded[:32]))
return ThreemaContact{
ID: [8]byte(thIDString),
LPK: pubKey}, nil
}