-
Notifications
You must be signed in to change notification settings - Fork 2
/
identity.go
228 lines (192 loc) · 6.47 KB
/
identity.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
package identity
import (
"bytes"
"crypto/ed25519"
"encoding/base64"
"errors"
"github.com/TankerHQ/identity-go/v3/internal/app"
"github.com/TankerHQ/identity-go/v3/internal/crypto"
"github.com/iancoleman/orderedmap"
"golang.org/x/crypto/blake2b"
)
type publicIdentity struct {
TrustchainID []byte `json:"trustchain_id"`
Target string `json:"target"`
Value string `json:"value"`
}
type identity struct {
publicIdentity
DelegationSignature []byte `json:"delegation_signature"`
EphemeralPublicSignatureKey []byte `json:"ephemeral_public_signature_key"`
EphemeralPrivateSignatureKey []byte `json:"ephemeral_private_signature_key"`
UserSecret []byte `json:"user_secret"`
}
type publicProvisionalIdentity struct {
publicIdentity
PublicSignatureKey []byte `json:"public_signature_key"`
PublicEncryptionKey []byte `json:"public_encryption_key"`
}
type provisionalIdentity struct {
publicProvisionalIdentity
PrivateSignatureKey []byte `json:"private_signature_key"`
PrivateEncryptionKey []byte `json:"private_encryption_key"`
}
// Create returns a new identity crafted from config and userID
func Create(config Config, userID string) (*string, error) {
conf, err := config.fromBase64()
if err != nil {
return nil, err
}
identity, err := generateIdentity(*conf, userID)
if err != nil {
return nil, err
}
return Encode(identity)
}
// CreateProvisional returns a new provisional identity crafted from
// config, target and value
func CreateProvisional(config Config, target string, value string) (*string, error) {
conf, err := config.fromBase64()
if err != nil {
return nil, err
}
if target != "email" && target != "phone_number" {
return nil, errors.New("unsupported provisional identity target")
}
provisional, err := generateProvisionalIdentity(*conf, target, value)
if err != nil {
return nil, err
}
return Encode(provisional)
}
// GetPublicIdentity returns the public identity associated with the
// provided identity
func GetPublicIdentity(b64Identity string) (*string, error) {
type anyPublicIdentity struct {
publicIdentity
PublicSignatureKey []byte `json:"public_signature_key,omitempty"`
PublicEncryptionKey []byte `json:"public_encryption_key,omitempty"`
}
publicIdentity := new(anyPublicIdentity)
if err := Decode(b64Identity, publicIdentity); err != nil {
return nil, err
}
hashTarget := true
switch publicIdentity.Target {
case "user":
hashTarget = false
case "email":
publicIdentity.Value = hashProvisionalIdentityEmail(publicIdentity.Value)
case "phone_number":
privateIdentity := struct {
PrivateSignatureKey *string `json:"private_signature_key"`
}{}
// in practice this case should never happen since we are decoding into a
// more permissive type, and we already decoded above so there should be
// no problem with b64Identity itself
if err := Decode(b64Identity, &privateIdentity); err != nil {
return nil, err
}
privateSignatureKey := privateIdentity.PrivateSignatureKey
if privateSignatureKey == nil {
return nil, errors.New("invalid tanker identity")
}
publicIdentity.Value = hashProvisionalIdentityValue(publicIdentity.Value, *privateSignatureKey)
default:
return nil, errors.New("unsupported identity target")
}
if hashTarget {
publicIdentity.Target = "hashed_" + publicIdentity.Target
}
return Encode(publicIdentity)
}
// UpgradeIdentity upgrades the provided identity if needed and returns
// the result of the upgrade
func UpgradeIdentity(b64Identity string) (*string, error) {
identity := orderedmap.New()
if err := Decode(b64Identity, &identity); err != nil {
return nil, err
}
_, isPrivate := identity.Get("private_encryption_key")
target, found := identity.Get("target")
if !found {
return nil, errors.New("invalid provisional identity (missing target field)")
}
if target == "email" && !isPrivate {
identity.Set("target", "hashed_email")
value, valueFound := identity.Get("value")
if !valueFound {
return nil, errors.New("unsupported identity without value")
}
hashedEmail := blake2b.Sum256([]byte(value.(string)))
identity.Set("value", base64.StdEncoding.EncodeToString(hashedEmail[:]))
}
return Encode(identity)
}
func checkKeysIntegrity(config config) error {
if !bytes.Equal(app.GetAppId(config.AppSecret), config.AppID) {
return errors.New("app secret and app ID mismatch")
}
return nil
}
func generateIdentity(config config, userIDString string) (*identity, error) {
if err := checkKeysIntegrity(config); err != nil {
return nil, err
}
userID := hashUserID(config.AppID, userIDString)
epubSignKey, eprivSignKey, err := ed25519.GenerateKey(nil)
if err != nil {
return nil, err
}
payload := append(epubSignKey, userID...)
delegationSignature := ed25519.Sign(config.AppSecret, payload)
identity := identity{
publicIdentity: publicIdentity{
TrustchainID: config.AppID,
Target: "user",
Value: base64.StdEncoding.EncodeToString(userID),
},
DelegationSignature: delegationSignature,
EphemeralPrivateSignatureKey: eprivSignKey,
EphemeralPublicSignatureKey: epubSignKey,
UserSecret: newUserSecret(userID),
}
return &identity, nil
}
func generateProvisionalIdentity(config config, target string, value string) (*provisionalIdentity, error) {
if err := checkKeysIntegrity(config); err != nil {
return nil, err
}
publicSignatureKey, privateSignatureKey, err := ed25519.GenerateKey(nil)
if err != nil {
return nil, err
}
publicEncryptionKey, privateEncryptionKey, err := crypto.NewKeyPair()
if err != nil {
return nil, err
}
provisionalIdentity := provisionalIdentity{
publicProvisionalIdentity: publicProvisionalIdentity{
publicIdentity: publicIdentity{
TrustchainID: config.AppID,
Target: target,
Value: value,
},
PublicEncryptionKey: publicEncryptionKey,
PublicSignatureKey: publicSignatureKey,
},
PrivateSignatureKey: privateSignatureKey,
PrivateEncryptionKey: privateEncryptionKey,
}
return &provisionalIdentity, nil
}
func hashProvisionalIdentityEmail(email string) (hash string) {
hashedValue := blake2b.Sum256([]byte(email))
return base64.StdEncoding.EncodeToString(hashedValue[:])
}
func hashProvisionalIdentityValue(value string, privateSignatureKeyB64 string) (hash string) {
privateSignatureKey, _ := base64.StdEncoding.DecodeString(privateSignatureKeyB64)
hashSalt := blake2b.Sum256(privateSignatureKey)
hashedValue := blake2b.Sum256(append(hashSalt[:], value...))
return base64.StdEncoding.EncodeToString(hashedValue[:])
}