forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
id.go
301 lines (271 loc) · 7.54 KB
/
id.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
293
294
295
296
297
298
299
300
301
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package tlf
import (
"encoding"
"encoding/hex"
"fmt"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/kbfs/kbfscrypto"
"github.com/pkg/errors"
)
const (
// idByteLen is the number of bytes in a top-level folder ID
idByteLen = 16
// idStringLen is the number of characters in the string
// representation of a top-level folder ID
idStringLen = 2 * idByteLen
// idSuffix is the last byte of a private top-level folder ID
idSuffix = 0x16
// pubIDSuffix is the last byte of a public top-level folder ID
pubIDSuffix = 0x17
// singleTeamIDSuffix is the last byte of a single-team top-level
// folder ID
singleTeamIDSuffix = 0x26
)
// Type is the type of TLF represented by a particular ID (e.g.,
// public, private, etc.)
type Type int
const (
// Unknown is a placeholder type for when TLF type information is not
// available. It is the zero value of the type Type.
Unknown Type = iota
// Private represents a private TLF between one or more individual users.
Private
// Public represents a public TLF for one or more individual users.
Public
// SingleTeam represents a private TLF for a single Keybase team.
SingleTeam
)
func (t Type) String() string {
switch t {
case Private:
return "private"
case Public:
return "public"
case SingleTeam:
return "singleTeam"
default:
return fmt.Sprintf("Unknown TLF type: %d", t)
}
}
// FolderType returns the keybase1.FolderType corresponding to the
// given TLF type.
func (t Type) FolderType() keybase1.FolderType {
switch t {
case Private:
return keybase1.FolderType_PRIVATE
case Public:
return keybase1.FolderType_PUBLIC
case SingleTeam:
return keybase1.FolderType_TEAM
default:
return keybase1.FolderType_UNKNOWN
}
}
// TypeFromFolderType returns the Type corresponding to the given
// keybase1.FolderType.
func TypeFromFolderType(ft keybase1.FolderType) Type {
switch ft {
case keybase1.FolderType_PRIVATE:
return Private
case keybase1.FolderType_PUBLIC:
return Public
case keybase1.FolderType_TEAM:
return SingleTeam
default:
return Unknown
}
}
// KeyingType represents a TLF keying mode. It normally have the same values
// as Type.
type KeyingType Type
const (
// UnknownKeying is a placeholder type for when TLF keying mode is unknown.
UnknownKeying = KeyingType(Unknown)
// PrivateKeying specifies the TLF keying mode used in classic private TLFs.
PrivateKeying = KeyingType(Private)
// PublicKeying specifies the TLF keying mode used in classic public TLFs.
PublicKeying = KeyingType(Public)
// TeamKeying specifies the TLF keying mode used for SingleTeam or
// implicit team backed TLFs.
TeamKeying = KeyingType(SingleTeam)
)
// ToKeyingType converts Type t into a KeyingType.
func (t Type) ToKeyingType() KeyingType {
return KeyingType(t)
}
// String implements the fmt.Stringer interface.
func (t KeyingType) String() string {
switch t {
case PrivateKeying:
return "private keying"
case PublicKeying:
return "public keying"
case TeamKeying:
return "team keying"
default:
return fmt.Sprintf("Unknown TLF keying type: %d", t)
}
}
// ID is a top-level folder ID
type ID struct {
id [idByteLen]byte
}
var _ encoding.BinaryMarshaler = ID{}
var _ encoding.BinaryUnmarshaler = (*ID)(nil)
var _ encoding.TextMarshaler = ID{}
var _ encoding.TextUnmarshaler = (*ID)(nil)
// NullID is an empty ID
var NullID = ID{}
// Bytes returns the bytes of the TLF ID.
func (id ID) Bytes() []byte {
return id.id[:]
}
// String implements the fmt.Stringer interface for ID.
func (id ID) String() string {
return hex.EncodeToString(id.id[:])
}
// MarshalBinary implements the encoding.BinaryMarshaler interface for ID.
func (id ID) MarshalBinary() (data []byte, err error) {
suffix := id.id[idByteLen-1]
if suffix != idSuffix && suffix != pubIDSuffix &&
suffix != singleTeamIDSuffix {
return nil, errors.WithStack(InvalidIDError{id.String()})
}
return id.id[:], nil
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface
// for ID.
func (id *ID) UnmarshalBinary(data []byte) error {
if len(data) != idByteLen {
return errors.WithStack(
InvalidIDError{hex.EncodeToString(data)})
}
suffix := data[idByteLen-1]
if suffix != idSuffix && suffix != pubIDSuffix &&
suffix != singleTeamIDSuffix {
return errors.WithStack(
InvalidIDError{hex.EncodeToString(data)})
}
copy(id.id[:], data)
return nil
}
// MarshalText implements the encoding.TextMarshaler interface for ID.
func (id ID) MarshalText() ([]byte, error) {
bytes, err := id.MarshalBinary()
if err != nil {
return nil, err
}
return []byte(hex.EncodeToString(bytes)), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface for
// ID.
func (id *ID) UnmarshalText(buf []byte) error {
s := string(buf)
bytes, err := hex.DecodeString(s)
if err != nil {
return errors.WithStack(InvalidIDError{s})
}
return id.UnmarshalBinary(bytes)
}
// SafeType returns the type of TLF represented by this ID. If the ID
// isn't valid, it returns tlf.Unknown along with an error.
func (id ID) SafeType() (Type, error) {
switch id.id[idByteLen-1] {
case idSuffix:
return Private, nil
case pubIDSuffix:
return Public, nil
case singleTeamIDSuffix:
return SingleTeam, nil
default:
return Unknown, fmt.Errorf("Unknown ID suffix %x", id.id[idByteLen-1])
}
}
// Type returns the type of TLF represented by this ID.
//
// Note that this function panics if the ID suffix is unknown, rather than
// returning tlf.Unknown.
func (id ID) Type() Type {
t, err := id.SafeType()
if err != nil {
panic(err)
}
return t
}
// ParseID parses a hex encoded ID. Returns NullID and an
// InvalidIDError on failure.
func ParseID(s string) (ID, error) {
var id ID
err := id.UnmarshalText([]byte(s))
if err != nil {
return ID{}, err
}
return id, nil
}
// MakeRandomID makes a random ID using a cryptographically secure
// RNG. Returns NullID on failure.
func MakeRandomID(t Type) (ID, error) {
var idBytes [idByteLen]byte
err := kbfscrypto.RandRead(idBytes[:])
if err != nil {
return NullID, err
}
switch t {
case Private:
idBytes[idByteLen-1] = idSuffix
case Public:
idBytes[idByteLen-1] = pubIDSuffix
case SingleTeam:
idBytes[idByteLen-1] = singleTeamIDSuffix
default:
panic(fmt.Sprintf("Unknown TLF type %d", t))
}
var id ID
err = id.UnmarshalBinary(idBytes[:])
if err != nil {
return NullID, err
}
return id, nil
}
// MakeIDFromTeam makes a deterministic TLF ID from a team ID and an epoch
// representing how many times a new TLF has been needed for this
// team. Returns NullID on failure.
func MakeIDFromTeam(t Type, tid keybase1.TeamID, epoch byte) (ID, error) {
idBytes := tid.ToBytes()
if len(idBytes) != idByteLen {
return NullID, errors.Errorf(
"The length of team ID %s doesn't match that of a TLF ID", tid)
}
idBytes[idByteLen-2] = epoch
switch t {
case Private:
if tid.IsPublic() {
return NullID, errors.Errorf(
"Cannot make a private TLF for a public team ID %s", tid)
}
idBytes[idByteLen-1] = idSuffix
case Public:
if !tid.IsPublic() {
return NullID, errors.Errorf(
"Cannot make a public TLF for a private team ID %s", tid)
}
idBytes[idByteLen-1] = pubIDSuffix
case SingleTeam:
if tid.IsPublic() {
return NullID, errors.Errorf(
"Cannot make a single-team TLF for a public team ID %s", tid)
}
idBytes[idByteLen-1] = singleTeamIDSuffix
default:
panic(fmt.Sprintf("Unknown TLF type %d", t))
}
var id ID
err := id.UnmarshalBinary(idBytes[:])
if err != nil {
return NullID, err
}
return id, nil
}