/
cryptosign.go
174 lines (145 loc) · 3.96 KB
/
cryptosign.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
package auth
import (
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"time"
"github.com/gammazero/nexus/v3/wamp"
"golang.org/x/crypto/nacl/sign"
)
type CryptoSignAuthenticator struct {
keyStore KeyStore
timeout time.Duration
}
func NewCryptoSignAuthenticator(keyStore KeyStore, timeout time.Duration) *CryptoSignAuthenticator {
if timeout == 0 {
timeout = defaultCRAuthTimeout
}
return &CryptoSignAuthenticator{
keyStore: keyStore,
timeout: timeout,
}
}
func (cr *CryptoSignAuthenticator) AuthMethod() string { return "cryptosign" }
func (cr *CryptoSignAuthenticator) Authenticate(sid wamp.ID, details wamp.Dict, client wamp.Peer) (*wamp.Welcome, error) { //nolint:lll
authid, _ := wamp.AsString(details["authid"])
if authid == "" {
return nil, errors.New("missing authid")
}
authrole, err := cr.keyStore.AuthRole(authid)
if err != nil {
return nil, err
}
ks, ok := cr.keyStore.(BypassKeyStore)
if ok {
if ks.AlreadyAuth(authid, details) {
// Create welcome details containing auth info.
welcome := &wamp.Welcome{
Details: wamp.Dict{
"authid": authid,
"authrole": authrole,
"authmethod": cr.AuthMethod(),
"authprovider": cr.keyStore.Provider(),
},
}
if err = ks.OnWelcome(authid, welcome, details); err != nil {
return nil, err
}
return welcome, nil
}
}
// Get the key and authrole needed for signing the challenge string.
key, err := cr.keyStore.AuthKey(authid, cr.AuthMethod())
if err != nil {
return nil, errors.New("failed to retrieve key")
}
//channelBinding := cr.extractChannelBinding(details)
challenge, err := cr.computeChallenge(nil)
if err != nil {
return nil, err
}
extra := wamp.Dict{"challenge": hex.EncodeToString(challenge)}
chalMsg := &wamp.Challenge{
AuthMethod: cr.AuthMethod(),
Extra: extra,
}
// Challenge response needed. Send CHALLENGE message to client.
select {
case client.Send() <- chalMsg:
default:
return nil, errors.New("cannot send challenge to client: blocked")
}
// Read AUTHENTICATE response from client.
msg, err := wamp.RecvTimeout(client, cr.timeout)
if err != nil {
return nil, err
}
authRsp, ok := msg.(*wamp.Authenticate)
if !ok {
return nil, fmt.Errorf("unexpected %v message received from client %v",
msg.MessageType(), client)
}
verify, err := cr.verifySignature(authRsp.Signature, key)
if err != nil {
return nil, err
}
if !verify {
return nil, errors.New("invalid signature")
}
// Create welcome message containing auth info.
welcome := &wamp.Welcome{
Details: wamp.Dict{
"authid": authid,
"authrole": authrole,
"authmethod": cr.AuthMethod(),
"authprovider": cr.keyStore.Provider(),
},
}
return welcome, nil
}
func (cr *CryptoSignAuthenticator) verifySignature(signature string, publicKey []byte) (bool, error) {
signatureBytes, err := hex.DecodeString(signature)
if err != nil {
fmt.Println(err)
return false, err
}
if len(signatureBytes) != 96 {
return false, fmt.Errorf("signed message has invalid length (was %v, but should have been 96", len(signatureBytes))
}
signedOut := make([]byte, 32)
var pubkey [32]byte
copy(pubkey[:], publicKey)
_, verify := sign.Open(signedOut, signatureBytes, &pubkey)
return verify, nil
}
func (cr *CryptoSignAuthenticator) extractChannelBinding(details wamp.Dict) []byte {
if details != nil {
authextra, ok := wamp.AsDict(details["authextra"])
if !ok {
return nil
}
_, ok = wamp.AsDict(authextra["channel_binding"])
if !ok {
return nil
}
// TODO: extract and convert channel binding
}
return nil
}
func (cr *CryptoSignAuthenticator) computeChallenge(channelBinding []byte) ([]byte, error) {
challenge := make([]byte, 32)
_, err := rand.Read(challenge)
if err != nil {
return nil, err
}
signedMessage := make([]byte, 32)
if channelBinding != nil {
for index, v := range challenge {
signedMessage[index] = v ^ channelBinding[index]
}
} else {
copy(signedMessage, challenge)
}
return signedMessage, nil
}