-
Notifications
You must be signed in to change notification settings - Fork 13
/
keystore.go
230 lines (190 loc) · 5.44 KB
/
keystore.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
package mixin
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/ed25519"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"io"
"sync"
"time"
"github.com/golang-jwt/jwt"
"golang.org/x/crypto/curve25519"
)
type Keystore struct {
ClientID string `json:"client_id"`
SessionID string `json:"session_id"`
PrivateKey string `json:"private_key"`
PinToken string `json:"pin_token"`
Scope string `json:"scope"`
// AppID is equivalent to the ClientID
AppID string `json:"app_id"`
// ServerPublicKey is equivalent to the PinToken in hex format
ServerPublicKey string `json:"server_public_key"`
// SessionPrivateKey is equivalent to the PrivateKey in hex format
SessionPrivateKey string `json:"session_private_key"`
// ExtraClaims is used to store extra claims in the jwt token
ExtraClaims map[string]interface{} `json:"extra_claims"`
}
func (k *Keystore) init() error {
if k.ClientID == "" && k.AppID != "" {
k.ClientID = k.AppID
}
if k.PrivateKey == "" && k.SessionPrivateKey != "" {
seed, err := hex.DecodeString(k.SessionPrivateKey)
if err != nil {
return err
}
if len(seed) != ed25519.SeedSize {
return errors.New("invalid session private key")
}
key := ed25519.NewKeyFromSeed(seed)
k.PrivateKey = ed25519Encoding.EncodeToString(key)
}
if k.PinToken == "" && k.ServerPublicKey != "" {
b, err := hex.DecodeString(k.ServerPublicKey)
if err != nil {
return err
}
if len(b) != ed25519.PublicKeySize {
return errors.New("invalid server public key")
}
pub, err := publicKeyToCurve25519(b)
if err != nil {
return err
}
k.PinToken = ed25519Encoding.EncodeToString(pub)
}
return nil
}
type KeystoreAuth struct {
*Keystore
signMethod jwt.SigningMethod
signKey interface{}
pinCipher cipher.Block
// seq is increasing number
iter uint64
mux sync.Mutex
}
// AuthFromKeystore produces a signer using both ed25519 & RSA keystore.
func AuthFromKeystore(store *Keystore) (*KeystoreAuth, error) {
if err := store.init(); err != nil {
return nil, err
}
auth := &KeystoreAuth{Keystore: store}
var decodePinToken func([]byte) ([]byte, error)
if b, err := ed25519Encoding.DecodeString(store.PrivateKey); err == nil && len(b) == ed25519.PrivateKeySize {
auth.signMethod = jwt.SigningMethodEdDSA
auth.signKey = ed25519.PrivateKey(b)
decodePinToken = func(token []byte) ([]byte, error) {
var curve [32]byte
privateKeyToCurve25519(&curve, b)
return curve25519.X25519(curve[:], token)
}
} else if key, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(store.PrivateKey)); err == nil {
auth.signMethod = jwt.SigningMethodRS512
auth.signKey = key
decodePinToken = func(token []byte) ([]byte, error) {
return rsa.DecryptOAEP(sha256.New(), rand.Reader, key, token, []byte(store.SessionID))
}
} else {
return nil, fmt.Errorf("parse private key failed")
}
if store.PinToken != "" {
token, err := ed25519Encoding.DecodeString(store.PinToken)
if err != nil {
return nil, fmt.Errorf("decode pin token failed: %w", err)
}
keyBytes, err := decodePinToken(token)
if err != nil {
return nil, err
}
pinCipher, err := aes.NewCipher(keyBytes)
if err != nil {
return nil, err
}
auth.pinCipher = pinCipher
}
return auth, nil
}
// AuthEd25519FromKeystore produces a signer using an ed25519 keystore.
// Deprecated: use AuthFromKeystore instead.
func AuthEd25519FromKeystore(store *Keystore) (*KeystoreAuth, error) {
return AuthFromKeystore(store)
}
func (k *KeystoreAuth) SignTokenAt(signature, requestID string, at time.Time, exp time.Duration) string {
jwtMap := jwt.MapClaims{
"uid": k.ClientID,
"sid": k.SessionID,
"iat": at.Unix(),
"exp": at.Add(exp).Unix(),
"jti": requestID,
"sig": signature,
"scp": ScopeFull,
}
if k.Scope != "" {
jwtMap["scp"] = k.Scope
}
for k, v := range k.ExtraClaims {
if _, ok := jwtMap[k]; !ok {
jwtMap[k] = v
}
}
token, err := jwt.NewWithClaims(k.signMethod, jwtMap).SignedString(k.signKey)
if err != nil {
panic(err)
}
return token
}
func (k *KeystoreAuth) SignToken(signature, requestID string, exp time.Duration) string {
return k.SignTokenAt(signature, requestID, time.Now(), exp)
}
func (k *KeystoreAuth) sequence() uint64 {
k.mux.Lock()
defer k.mux.Unlock()
if iter := uint64(time.Now().UnixNano()); iter > k.iter {
k.iter = iter
} else {
k.iter += 1
}
return k.iter
}
func (k *KeystoreAuth) EncryptPin(pin string) string {
if k.pinCipher == nil {
panic(errors.New("keystore: pin_token required"))
}
pinByte := []byte(pin)
if len(pin) > 6 {
bts, err := hex.DecodeString(pin)
if err != nil {
panic(err)
}
pinByte = bts
} else if err := ValidatePinPattern(pin); err != nil {
panic(err)
}
timeBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(timeBytes, uint64(time.Now().Unix()))
pinByte = append(pinByte, timeBytes...)
iteratorBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(iteratorBytes, k.sequence())
pinByte = append(pinByte, iteratorBytes...)
padding := aes.BlockSize - len(pinByte)%aes.BlockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
pinByte = append(pinByte, padText...)
cipherText := make([]byte, aes.BlockSize+len(pinByte))
iv := cipherText[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
mode := cipher.NewCBCEncrypter(k.pinCipher, iv)
mode.CryptBlocks(cipherText[aes.BlockSize:], pinByte)
return base64.RawURLEncoding.EncodeToString(cipherText)
}