-
Notifications
You must be signed in to change notification settings - Fork 3
/
credentials.go
292 lines (249 loc) · 7.16 KB
/
credentials.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
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
package credentials
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"hash"
"io"
"strings"
"sync"
"time"
"github.com/Rocket-Rescue-Node/credentials/pb"
"github.com/Rocket-Rescue-Node/credentials/words"
"google.golang.org/protobuf/proto"
)
var hashAlgo = sha256.New
type OperatorType = pb.OperatorType
type AuthenticatedCredential pb.AuthenticatedCredential
type jsonAuthenticatedCredential struct {
NodeID string `json:"node_id"`
Timestamp int64 `json:"timestamp"`
OperatorType OperatorType `json:"operator_type"`
Mac string `json:"mac"`
}
func (ac *AuthenticatedCredential) Pb() *pb.AuthenticatedCredential {
return (*pb.AuthenticatedCredential)(ac)
}
func (ac *AuthenticatedCredential) MarshalJSON() ([]byte, error) {
var mac bytes.Buffer
nodeID := "0x" + hex.EncodeToString(ac.Credential.NodeId)
encoder := base64.NewEncoder(base64.URLEncoding, &mac)
_, err := encoder.Write(ac.Mac)
if err != nil {
return nil, err
}
encoder.Close()
return json.Marshal(&jsonAuthenticatedCredential{
NodeID: nodeID,
Timestamp: ac.Credential.Timestamp,
OperatorType: ac.Credential.OperatorType,
Mac: mac.String(),
})
}
func (ac *AuthenticatedCredential) UnmarshalJSON(data []byte) error {
var j jsonAuthenticatedCredential
ac.Pb().Reset()
if err := json.Unmarshal(data, &j); err != nil {
return err
}
ac.Credential = &pb.Credential{}
decoder := base64.NewDecoder(base64.URLEncoding, bytes.NewReader([]byte(j.Mac)))
decoded, err := io.ReadAll(decoder)
if err != nil {
return err
}
nodeID, err := hex.DecodeString(strings.TrimPrefix(j.NodeID, "0x"))
if err != nil {
return err
}
ac.Credential.NodeId = nodeID
ac.Credential.OperatorType = j.OperatorType
ac.Credential.Timestamp = j.Timestamp
ac.Mac = decoded
return nil
}
func (ac *AuthenticatedCredential) Base64URLEncodeUsername() string {
var out bytes.Buffer
encoder := base64.NewEncoder(base64.URLEncoding, &out)
_, err := encoder.Write(ac.Credential.NodeId)
if err != nil {
return ""
}
encoder.Close()
return out.String()
}
func (ac *AuthenticatedCredential) Base64URLEncodePassword() (string, error) {
var out bytes.Buffer
// Save the nodeId
nodeID := ac.Credential.NodeId
// Strip it to save space
ac.Credential.NodeId = nil
// Restore it when we're done
defer func() {
ac.Credential.NodeId = nodeID
}()
marshaled, err := proto.Marshal(ac.Pb())
if err != nil {
return "", err
}
// Encode the marshaled proto
encoder := base64.NewEncoder(base64.URLEncoding, &out)
_, err = encoder.Write(marshaled)
if err != nil {
return "", err
}
encoder.Close()
return out.String(), nil
}
func (ac *AuthenticatedCredential) Base64URLDecode(username string, password string) error {
decoder := base64.NewDecoder(base64.URLEncoding, bytes.NewReader([]byte(username)))
nodeID, err := io.ReadAll(decoder)
if err != nil {
return err
}
decoder = base64.NewDecoder(base64.URLEncoding, bytes.NewReader([]byte(password)))
decoded, err := io.ReadAll(decoder)
if err != nil {
return err
}
newCred := AuthenticatedCredential{}
err = proto.Unmarshal(decoded, newCred.Pb())
if err != nil {
return err
}
ac.Pb().Reset()
proto.Merge(ac.Pb(), newCred.Pb())
ac.Credential.NodeId = nodeID
return nil
}
type secret struct {
id *ID
hmac hash.Hash
}
type checker struct {
primary secret
extras []secret
}
// CredentialManager authenticates and verifies rescue node credentials
type CredentialManager struct {
// pool is a pool of checkers
id *ID
partnerIDs []*ID
p sync.Pool
}
func idFromKey(key []byte) *ID {
h := hashAlgo()
h.Write([]byte("rescue-credential-id"))
h.Write(key)
idBinary := h.Sum(nil)
return &ID{
bytes: *(*[32]byte)(idBinary),
words: words.Encode(idBinary),
}
}
// NewCredentialManager creates a new CredentialManager which can create and verify authenticated credentials
// Credentials are created with `key` but validated against `key` and all `extraSecrets`.
// Under the hood, the library uses sha256 as an hmac hash, so keys should be at least 32 bytes for full security.
func NewCredentialManager(key []byte, extraSecrets ...[]byte) *CredentialManager {
id := idFromKey(key)
out := &CredentialManager{
id: id,
p: sync.Pool{
New: func() any {
numExtras := len(extraSecrets)
out := new(checker)
out.primary = secret{
id: id,
hmac: hmac.New(hashAlgo, key),
}
if numExtras > 0 {
out.extras = make([]secret, numExtras)
for i, s := range extraSecrets {
out.extras[i] = secret{
id: idFromKey(s),
hmac: hmac.New(hashAlgo, s),
}
}
}
return out
},
},
}
out.partnerIDs = make([]*ID, 0)
for _, s := range extraSecrets {
out.partnerIDs = append(out.partnerIDs, idFromKey(s))
}
return out
}
func (c *CredentialManager) authenticateCredential(credential *AuthenticatedCredential) error {
// Serialize just the inner message so we can authenticate it and add it to the outer message
bytes, err := proto.Marshal(credential.Credential)
if err != nil {
return errors.Join(err, SerializationError)
}
v, ok := c.p.Get().(*checker)
if !ok {
return MemoryError
}
// defer stacks calls, so Put will always be called after the Reset() below
defer c.p.Put(v)
v.primary.hmac.Write(bytes)
credential.Mac = v.primary.hmac.Sum(nil)
defer v.primary.hmac.Reset()
return nil
}
// Create makes a new credential and authenticates it, returning a protoc struct that can be marshaled/unmarshaled
func (c *CredentialManager) Create(timestamp time.Time, nodeID []byte, OperatorType OperatorType) (*AuthenticatedCredential, error) {
if len(nodeID) != 20 {
return nil, fmt.Errorf("invalid nodeID length. Expected 20, got %d", len(nodeID))
}
message := AuthenticatedCredential{}
message.Credential = &pb.Credential{}
message.Credential.NodeId = nodeID
message.Credential.OperatorType = OperatorType
message.Credential.Timestamp = timestamp.Unix()
if err := c.authenticateCredential(&message); err != nil {
return nil, err
}
return &message, nil
}
// Verify checks that a AuthenticatedCredential has a valid mac
func (c *CredentialManager) Verify(authenticatedCredential *AuthenticatedCredential) (*ID, error) {
// Grab the byte representation of the inner message
bytes, err := proto.Marshal(authenticatedCredential.Credential)
if err != nil {
return nil, errors.Join(err, SerializationError)
}
v, ok := c.p.Get().(*checker)
if !ok {
return nil, MemoryError
}
// defer stacks calls, so Put will always be called after the Reset() calls below
defer c.p.Put(v)
secrets := append([]secret{v.primary}, v.extras...)
for _, s := range secrets {
s.hmac.Write(bytes)
mac := s.hmac.Sum(nil)
defer s.hmac.Reset()
if hmac.Equal(mac, authenticatedCredential.Mac) {
// A secret was able to auth this credential,
// return its ID
return s.id, nil
}
}
// MAC didn't match. Authenticity cannot be verified.
return nil, MismatchError
}
// ID returns the ID struct of the primary secret
func (c *CredentialManager) ID() *ID {
return c.id
}
// PartnerIDs returns a slice of ID structs of partner secrets
func (c *CredentialManager) PartnerIDs() []*ID {
return c.partnerIDs
}