summaryrefslogtreecommitdiff
path: root/daemon/kmd/session/auth.go
blob: 29aa5acc201c1186a65808698a3f08ba1af2a776 (plain)
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
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package session

import (
	"bytes"
	"crypto/rand"
	"crypto/subtle"
	"fmt"
	"regexp"
	"time"

	"github.com/algorand/go-algorand/daemon/kmd/wallet"
)

const (
	wHandleIDBytes       = 8
	wHandleSecretBytes   = 32
	handleCleanupSeconds = 60
)

var wHandleTokenSplitChar = []byte(".")
var wHandleIDRegex = regexp.MustCompile(`^[0-9a-f]{16}$`)
var wHandleSecretRegex = regexp.MustCompile(`^[0-9a-f]{64}$`)

func validateHandleID(handleID []byte) error {
	if wHandleIDRegex.Find(handleID) == nil {
		return fmt.Errorf("invalid wallet handle id")
	}
	return nil
}

func validateHandleSecret(handleSecret []byte) error {
	if wHandleSecretRegex.Find(handleSecret) == nil {
		return fmt.Errorf("invalid wallet handle secret")
	}
	return nil
}

func splitHandle(walletHandle []byte) ([]byte, []byte, error) {
	handleID, handleSecret, found := bytes.Cut(walletHandle, wHandleTokenSplitChar)

	if !found {
		return nil, nil, fmt.Errorf("wrong number of token parts")
	}

	err := validateHandleID(handleID)
	if err != nil {
		return nil, nil, err
	}

	err = validateHandleSecret(handleSecret)
	if err != nil {
		return nil, nil, err
	}

	return handleID, handleSecret, nil
}

func checkHandleNotExpired(expires time.Time) error {
	if time.Now().After(expires) {
		return fmt.Errorf("handle expired")
	}
	return nil
}

func generateHandleIDAndSecret() ([]byte, []byte, error) {
	handleID := make([]byte, wHandleIDBytes)
	_, err := rand.Read(handleID)
	if err != nil {
		return nil, nil, err
	}

	handleSecret := make([]byte, wHandleSecretBytes)
	_, err = rand.Read(handleSecret)
	if err != nil {
		return nil, nil, err
	}

	hexID := []byte(fmt.Sprintf("%x", handleID))
	hexSecret := []byte(fmt.Sprintf("%x", handleSecret))
	return hexID, hexSecret, nil
}

// cleanUpExpiredHandlesLocked periodically calls deleteExpiredHandles until
// sm.ctx is canceled.
func (sm *Manager) cleanUpExpiredHandles() {
	ticker := time.NewTicker(handleCleanupSeconds * time.Second)
	for {
		select {
		case <-ticker.C:
			sm.deleteExpiredHandles()
		case <-sm.ctx.Done():
			ticker.Stop()
			return
		}
	}
}

// deleteExpiredHandles is a helper for cleanUpExpiredHandles. It periodically
// iterates over the walletHandles map and deletes ones that have expired
func (sm *Manager) deleteExpiredHandles() {
	sm.mux.Lock()
	defer sm.mux.Unlock()
	for handleID, handle := range sm.walletHandles {
		if checkHandleNotExpired(handle.expires) != nil {
			delete(sm.walletHandles, handleID)
		}
	}
}

// InitWalletHandle attempts to init the wallet using the passed password,
// generates a wallet handle token, and adds the session to the memory store
func (sm *Manager) InitWalletHandle(w wallet.Wallet, pw []byte) ([]byte, error) {
	// Attempt to initialize the wallet with the password
	err := w.Init(pw)
	if err != nil {
		return nil, err
	}

	// Generate wallet handle credentials
	handleID, handleSecret, err := generateHandleIDAndSecret()
	if err != nil {
		return nil, err
	}

	// Build the walletHandle
	handle := walletHandle{
		secret:  handleSecret,
		expires: time.Now().Add(sm.sessionLifetime),
		wallet:  w,
	}

	// Insert the handle into the walletHandles map
	sm.mux.Lock()
	defer sm.mux.Unlock()
	sm.walletHandles[string(handleID)] = handle
	handleToken := []byte(fmt.Sprintf("%s%s%s", handleID, wHandleTokenSplitChar, handleSecret))

	return handleToken, nil
}

// getHandleFromTokenLocked is a helper that looks up the wallet handle from
// the token and checks that the token secret is correct. If it is, it returns
// the handle ID and the handle itself
func (sm *Manager) getHandleFromTokenLocked(walletHandleToken []byte) (id []byte, wh walletHandle, err error) {
	// Ensure the token is a valid format
	handleID, handleSecret, err := splitHandle(walletHandleToken)
	if err != nil {
		return
	}

	// Fetch the handle if it exists
	handle, ok := sm.walletHandles[string(handleID)]
	if !ok {
		err = fmt.Errorf("handle does not exist")
		return
	}

	// Check that the token is correct in constant time
	if subtle.ConstantTimeCompare(handleSecret, handle.secret) != 1 {
		err = fmt.Errorf("invalid token")
		return
	}

	return handleID, handle, err
}

// ReleaseWalletHandle deletes the wallet handle if it exists
func (sm *Manager) ReleaseWalletHandle(walletHandleToken []byte) error {
	sm.mux.Lock()
	defer sm.mux.Unlock()

	// Fetch the handle + check that the token is correct
	handleID, _, err := sm.getHandleFromTokenLocked(walletHandleToken)
	if err != nil {
		return err
	}

	// Delete the handle
	delete(sm.walletHandles, string(handleID))
	return nil
}

// authMaybeRenewWalletHandleToken parses an untrusted walletHandle []byte and
// returns the Wallet it corresponds to + seconds until expiration if and only
// if the walletHandle was valid. If `renew` is true, it also renews the token
func (sm *Manager) authMaybeRenewWalletHandleToken(walletHandleToken []byte, renew bool) (wallet.Wallet, int64, error) {
	sm.mux.Lock()
	defer sm.mux.Unlock()

	// Fetch the handle + check that the token is correct
	handleID, handle, err := sm.getHandleFromTokenLocked(walletHandleToken)
	if err != nil {
		return nil, 0, err
	}

	// Check that the handle has not expired
	err = checkHandleNotExpired(handle.expires)
	if err != nil {
		// It's expired, so delete it
		delete(sm.walletHandles, string(handleID))
		return nil, 0, err
	}

	// Maybe renew the handle
	if renew {
		handle.expires = time.Now().Add(sm.sessionLifetime)
		sm.walletHandles[string(handleID)] = handle
	}

	// Compute how many seconds are left until the handle expires
	expiresSeconds := int64(handle.expires.Sub(time.Now()).Seconds())

	// Return the wallet and seconds remaining to expiration
	return handle.wallet, expiresSeconds, nil
}

// RenewWalletHandleToken parses an untrusted walletHandle []byte and renews it
// if the secret is correct and if it hasn't already expired
func (sm *Manager) RenewWalletHandleToken(walletHandleToken []byte) (wallet.Wallet, int64, error) {
	return sm.authMaybeRenewWalletHandleToken(walletHandleToken, true)
}

// AuthWithWalletHandleToken parses an untrusted walletHandle []byte and
// returns the Wallet it corresponds to + seconds until expiration if and only
// if the walletHandle was valid.
func (sm *Manager) AuthWithWalletHandleToken(walletHandleToken []byte) (wallet.Wallet, int64, error) {
	return sm.authMaybeRenewWalletHandleToken(walletHandleToken, false)
}