-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication.go
More file actions
423 lines (356 loc) · 11.7 KB
/
Copy pathauthentication.go
File metadata and controls
423 lines (356 loc) · 11.7 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package node
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha512"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"path"
"time"
)
const (
defaultAuthTimeout = 2 * time.Minute
defaultNonceSize = 32
defaultHashMethod = "sha512"
)
// Holds stored certificates, contacts the auth appliance, etc
type Authen struct {
CRAuthenticators map[string]CRAuthenticator
Authenticators map[string]Authenticator
AuthTimeout time.Duration
AuthMode string
}
func NewAuthen(node *node) Authen {
authen := Authen{
CRAuthenticators: make(map[string]CRAuthenticator),
AuthTimeout: defaultAuthTimeout,
AuthMode: os.Getenv("EXIS_AUTHENTICATION"),
}
authName := node.Config.AuthName
authen.CRAuthenticators["token"] = NewTokenAuthenticator(node, authName)
authen.CRAuthenticators["signature"] = NewSignatureAuthenticator(node, authName)
return authen
}
func DecodePublicKey(data []byte) (*rsa.PublicKey, error) {
// Decode the PEM public key
block, _ := pem.Decode(data)
if block == nil {
return nil, fmt.Errorf("Error decoding PEM file")
}
// Parse the public key.
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
// Type assertion: want an rsa.PublicKey.
pubkey, ok := pub.(*rsa.PublicKey)
if !ok {
return nil, fmt.Errorf("Error loading RSA public key")
}
return pubkey, nil
}
// Read a public key from a PEM file.
//
// PEM files are the ones that look like this:
// -----BEGIN PUBLIC KEY-----
// Base64 encoded data...
// -----END PUBLIC KEY-----
func ReadPublicKey(path string) (*rsa.PublicKey, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return DecodePublicKey(data)
}
// Try loading a domain's public key from file.
//
// We check for a filename with the same name as the domain in the
// directory set by the PUBKEYS environment variable.
func LoadPublicKey(domain string) (*rsa.PublicKey, error) {
dirname := os.Getenv("PUBKEYS")
if dirname == "" {
dirname = "."
}
path := path.Join(dirname, domain)
pubkey, err := ReadPublicKey(path)
return pubkey, err
}
// Move to authn
func (r *Authen) handleAuth(session *Session, hello *Hello) (*Welcome, error) {
msg, err := r.authenticate(session, hello)
if err != nil {
return nil, err
}
// we should never get anything besides WELCOME and CHALLENGE
if msg.MessageType() == WELCOME {
return msg.(*Welcome), nil
} else {
// Challenge response
challenge := msg.(*Challenge)
if err := session.Peer.Send(challenge); err != nil {
return nil, err
}
msg, err := GetMessageTimeout(session.Peer, r.AuthTimeout)
if err != nil {
return nil, err
}
//log.Printf("%s: %+v", msg.MessageType(), msg)
if authenticate, ok := msg.(*Authenticate); !ok {
return nil, fmt.Errorf("unexpected %s message received", msg.MessageType())
} else {
return r.checkResponse(session, challenge, authenticate)
}
}
}
// Authenticate either authenticates a client or returns a challenge message if
// challenge/response authentication is to be used.
func (r Authen) authenticate(session *Session, hello *Hello) (Message, error) {
// pprint the incoming details
// if b, err := json.MarshalIndent(details, "", " "); err != nil {
// fmt.Println("error:", err)
// } else {
// //log.Printf(string(b))
// }
authid, _ := hello.Details["authid"].(string)
if authid != "" {
session.authid = authid
}
if r.AuthMode == "off" {
session.authLevel = AUTH_LOW
return &Welcome{}, nil
}
// If client is a local peer, allow it without authentication.
if session.isLocal() {
session.authLevel = AUTH_HIGH
return &Welcome{}, nil
}
// Get list of supported authmethods from the Hello message.
authmethods := []string{}
_authmethods, ok := hello.Details["authmethods"].([]interface{})
if ok {
for _, method := range _authmethods {
if m, ok := method.(string); ok {
authmethods = append(authmethods, m)
}
}
}
// Soft mode allows client to declare an empty list of authmethods and
// proceed without authenticating or declare supported authmethods and
// proceed with authenticating.
//
// Default mode requires all clients to authenticate with a valid method.
// They still have to declare what methods they support in the Hello
// message.
if len(authmethods) == 0 {
if r.AuthMode == "soft" {
session.authLevel = AUTH_LOW
return &Welcome{}, nil
} else {
return nil, fmt.Errorf("could not authenticate with any method")
}
}
details := make(map[string]interface{})
details["authid"] = session.authid
for _, method := range authmethods {
if auth, ok := r.CRAuthenticators[method]; ok {
if challenge, err := auth.Challenge(details); err != nil {
return nil, err
} else {
return &Challenge{AuthMethod: method, Extra: challenge}, nil
}
}
if auth, ok := r.Authenticators[method]; ok {
if authDetails, err := auth.Authenticate(details); err != nil {
return nil, err
} else {
return &Welcome{Details: addAuthMethod(authDetails, method)}, nil
}
}
}
// TODO: check default auth (special '*' auth?)
return nil, fmt.Errorf("could not authenticate with any method")
}
// checkResponse determines whether the response to the challenge is sufficient to gain access to the Realm.
func (r Authen) checkResponse(session *Session, challenge *Challenge, authenticate *Authenticate) (*Welcome, error) {
if auth, ok := r.CRAuthenticators[challenge.AuthMethod]; !ok {
return nil, fmt.Errorf("authentication method has been removed")
} else {
// The agent is doing something funny here if he presents a token for pd.A
// but tries to set his pdid to pd.B. We will allow downward name changes.
if !subdomain(challenge.Extra["authid"].(string), string(session.pdid)) {
return nil, fmt.Errorf("Requested name not a permitted subdomain")
}
if details, err := auth.Authenticate(challenge.Extra, authenticate); err != nil {
return nil, err
} else {
out.Notice("Session [%s] authenticated by [%s]", session, challenge.AuthMethod)
session.authLevel = AUTH_HIGH
return &Welcome{Details: addAuthMethod(details, challenge.AuthMethod)}, nil
}
}
}
func addAuthMethod(details map[string]interface{}, method string) map[string]interface{} {
if details == nil {
details = make(map[string]interface{})
}
details["authmethod"] = method
return details
}
////////////////////////////////////////
// Misc and old
////////////////////////////////////////
// CRAuthenticator describes a type that can handle challenge/response authentication.
type CRAuthenticator interface {
// accept HELLO details and returns a challenge map (which will be sent in a CHALLENGE message)
Challenge(details map[string]interface{}) (map[string]interface{}, error)
// accept a challenge map (same as was generated in Challenge) and a signature string, and
// authenticates the signature string against the challenge. Returns a details map and error.
Authenticate(challenge map[string]interface{}, authenticate *Authenticate) (map[string]interface{}, error)
}
// Authenticator describes a type that can handle authentication based solely on the HELLO message.
//
// Use CRAuthenticator for more complex authentication schemes.
type Authenticator interface {
// Authenticate takes the HELLO details and returns a (WELCOME) details map if the
// authentication is successful, otherwise it returns an error
Authenticate(details map[string]interface{}) (map[string]interface{}, error)
}
//
// Token Authenticator
//
// 1. Through some means, the agent acquires a token.
// 2. During challenge-response, the agent presents his name, the issuing auth
// appliance, and the token.
// 3. We verify the validity token with the auth appliance.
//
type TokenAuthenticator struct {
node *node
agent *Client
authName string
}
func (ta *TokenAuthenticator) Challenge(details map[string]interface{}) (map[string]interface{}, error) {
return details, nil
}
func (ta *TokenAuthenticator) Authenticate(challenge map[string]interface{}, authenticate *Authenticate) (map[string]interface{}, error) {
authid := challenge["authid"].(string)
for _, auth := range ancestorDomains(authid, ta.authName) {
authEndpoint := auth + "/check_token_1"
active := ta.node.Dealer.hasRegistration(URI(authEndpoint))
if !active {
continue
}
out.Debug("Verifying token for %s with %s", authid, auth)
// Verify the token with auth.
args := []interface{}{authid, authenticate.Signature}
ret, err := ta.agent.Call(authEndpoint, args, nil)
if err != nil {
continue
}
permitted, ok := ret.Arguments[0].(bool)
if ok && permitted {
return nil, nil
}
}
return nil, fmt.Errorf("Unable to verify token with auth")
}
func NewTokenAuthenticator(node *node, authName string) *TokenAuthenticator {
authenticator := &TokenAuthenticator{
node: node,
agent: node.agent,
authName: authName,
}
return authenticator
}
//
// Signature Authenticator
//
// This is the more secure approach to authentication.
// 1. The agent holds a private key, and the knows the corresponding public key.
// 2. During challenge, we send a random string.
// 3. The agent signs the hash of the challenge string and sends it back.
// 4. The node verifies the signature against the public key.
//
// TODO: We are missing authentication of the node. The agent should
// send a challenge to the node, and the node should send back a signed hash.
//
type SignatureAuthenticator struct {
node *node
agent *Client
authName string
}
func (ta *SignatureAuthenticator) Challenge(details map[string]interface{}) (map[string]interface{}, error) {
data := make([]byte, defaultNonceSize)
_, err := rand.Read(data)
if err != nil {
return nil, fmt.Errorf("Error generating random nonce")
}
nonce := hex.EncodeToString(data)
details["challenge"] = nonce
// Tell the agent what hash method to use. This gives us a path to upgrade.
details["hash"] = defaultHashMethod
return details, nil
}
func (ta *SignatureAuthenticator) Authenticate(challenge map[string]interface{}, authenticate *Authenticate) (map[string]interface{}, error) {
authid := challenge["authid"].(string)
// This is the random nonce that was sent to the agent.
nonce := []byte(challenge["challenge"].(string))
// If we want to support different hash functions, here is where we need to
// do it.
if challenge["hash"] != "sha512" {
fmt.Printf("Warning: hash method %s not supported.\n", challenge["hash"])
return nil, fmt.Errorf("Node error: hash method not supported")
}
hashed := sha512.Sum512(nonce)
// Decode the base64 encoded signature from the agent.
signature, err := base64.StdEncoding.DecodeString(authenticate.Signature)
if err != nil {
return nil, fmt.Errorf("Error decoding signature")
}
pubkey, _ := LoadPublicKey(authid)
if pubkey == nil {
args := []interface{}{authid}
for _, auth := range ancestorDomains(authid, ta.authName) {
authEndpoint := auth + "/get_appliance_key"
active := ta.node.Dealer.hasRegistration(URI(authEndpoint))
if !active {
continue
}
out.Debug("Asking %s for public key of %s", auth, authid)
ret, err := ta.agent.Call(authEndpoint, args, nil)
if err != nil {
continue
}
pubkeyData, ok := ret.Arguments[0].(string)
if !ok {
continue
}
pubkey, err = DecodePublicKey([]byte(pubkeyData))
if err == nil {
// Found the public key.
break
}
}
if pubkey == nil {
return nil, fmt.Errorf("Error fetching public key")
}
}
err = rsa.VerifyPKCS1v15(pubkey, crypto.SHA512, hashed[:], signature)
if err != nil {
return nil, fmt.Errorf("Signature is not correct: %s", err)
}
return nil, nil
}
func NewSignatureAuthenticator(node *node, authName string) *SignatureAuthenticator {
authenticator := &SignatureAuthenticator{
node: node,
agent: node.agent,
authName: authName,
}
return authenticator
}