-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdsc.go
321 lines (287 loc) · 7.51 KB
/
mdsc.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package dshards
import (
"encoding/base64"
"errors"
"fmt"
"strconv"
"strings"
)
// Cap represents any capability. Can be type-asserted to VerifyCap, ReadCap,
// or ReadWriteCap.
type Cap interface {
String() string
KeyDataURN() (URN, error)
}
// VerifyCap is the basic verifiable capability of a mutable datashard.
type VerifyCap interface {
Cap
}
// ReadCap is the capability that can verify, read the location of, and decrypt
// the contents of the mutable datashard.
type ReadCap interface {
VerifyCap
VerifyCap() VerifyCap
}
// ReadWriteCap is the capability that can verify, read the location of, decrypt
// the contents of, and write new revisions of a mutable datashard.
type ReadWriteCap interface {
ReadCap
ReadCap() ReadCap
}
type accessLevel string
const (
verifyAL accessLevel = "v"
readAL accessLevel = "r"
writeAL accessLevel = "w"
)
func toAccessLevel(s string) (a accessLevel, err error) {
switch accessLevel(s) {
case verifyAL:
a = verifyAL
case readAL:
a = readAL
case writeAL:
a = writeAL
default:
err = fmt.Errorf("unknown access level: %q", s)
}
return
}
func toVersion(s []string) (n int, hash []byte, err error) {
if len(s) > 2 {
err = errors.New("malformed mdsc: too many versions")
return
} else if len(s) == 0 {
return noVersionProvided, nil, nil
}
var i int64
i, err = strconv.ParseInt(s[0], 10, 64)
if err != nil {
return
}
n = int(i)
if n < 0 {
err = fmt.Errorf("malformed mdsc: negative version %d", n)
return
}
if len(s) > 1 {
hash, err = base64.RawURLEncoding.DecodeString(s[1])
}
return
}
var _ VerifyCap = new(verifyMDSC)
// verifyMDSC only contains enough information to verify a mutable datashard's
// existence.
type verifyMDSC struct {
a accessLevel
s Suite
keyDataHash []byte
keyDataSymmKey SymmetricKey
// TODO: Why have nVersion, and not prevHashVersion?
nVersion int
hashVersion []byte
}
var _ ReadCap = new(readMDSC)
// readMDSC contains enough information to locate (and presumably fetch) a
// mutable datashard, as well as decrypt its contents.
type readMDSC struct {
verifyMDSC
readKey SymmetricKey
}
var _ ReadWriteCap = new(mdsc)
// mdsc is a mutable Datashard. It contains all private key information for all
// capabilities.
type mdsc struct {
verifyMDSC
writeKey SymmetricKey
}
const (
noVersionProvided = -1
mdscPrefix = "mdsc"
mdscDelim = idscDelim
// TODO Why not have versioningDelim = idscDelim?
versioningDelim = "/"
)
// ParseMDSC parses a string mutable datashard identifier into its capability.
func ParseMDSC(s string) (c Cap, err error) {
ss := strings.Split(s, protocolDelim)
if len(ss) != 2 {
err = errors.New("malformed mdsc")
return
} else if ss[0] != mdscPrefix {
err = errors.New("malformed mdsc: not prefixed with 'mdsc'")
return
}
ps := strings.Split(ss[1], mdscDelim)
if len(ps) < 4 || len(ps) > 5 {
err = errors.New("malformed mdsc")
return
}
m := verifyMDSC{nVersion: noVersionProvided}
m.a, err = toAccessLevel(ps[0])
if err != nil {
return
}
m.s, err = toSuite(ps[1])
if err != nil {
return
}
m.keyDataHash, err = base64.RawURLEncoding.DecodeString(ps[2])
if err != nil {
return
}
// Detect & split the versioning at the end
if len(ps) == 4 {
vs := strings.Split(ps[3], versioningDelim)
m.keyDataSymmKey, err = base64.RawURLEncoding.DecodeString(vs[0])
if err != nil {
return
}
m.nVersion, m.hashVersion, err = toVersion(vs[1:])
if err != nil {
return
}
c = &m
}
if len(ps) == 5 {
m.keyDataSymmKey, err = base64.RawURLEncoding.DecodeString(ps[3])
if err != nil {
return
}
vs := strings.Split(ps[4], versioningDelim)
if m.a == readAL {
r := readMDSC{verifyMDSC: m}
r.readKey, err = base64.RawURLEncoding.DecodeString(vs[0])
if err != nil {
return
}
r.nVersion, r.hashVersion, err = toVersion(vs[1:])
if err != nil {
return
}
c = &r
} else if m.a == writeAL {
w := mdsc{verifyMDSC: m}
w.writeKey, err = base64.RawURLEncoding.DecodeString(vs[0])
if err != nil {
return
}
w.nVersion, w.hashVersion, err = toVersion(vs[1:])
if err != nil {
return
}
c = &w
} else {
err = errors.New("malformed mdsc: provided read|write key for non-read non-write mdsc")
return
}
}
return
}
func (m verifyMDSC) baseString() string {
var buf strings.Builder
buf.WriteString(mdscPrefix)
buf.WriteString(protocolDelim)
buf.WriteString(string(m.a))
buf.WriteString(mdscDelim)
buf.WriteString(string(m.s))
buf.WriteString(mdscDelim)
dst := make([]byte, base64.RawURLEncoding.EncodedLen(len(m.keyDataHash)))
base64.RawURLEncoding.Encode(dst, m.keyDataHash)
buf.Write(dst)
buf.WriteString(mdscDelim)
dst = make([]byte, base64.RawURLEncoding.EncodedLen(len(m.keyDataSymmKey)))
base64.RawURLEncoding.Encode(dst, m.keyDataSymmKey)
buf.Write(dst)
return buf.String()
}
func (m verifyMDSC) versionSuffixString() string {
var buf strings.Builder
if m.nVersion != noVersionProvided {
buf.WriteString(versioningDelim)
buf.WriteString(strconv.FormatInt(int64(m.nVersion), 10))
// TODO: Trailing slash?
buf.WriteString(versioningDelim)
if len(m.hashVersion) > 0 {
dst := make([]byte, base64.RawURLEncoding.EncodedLen(len(m.hashVersion)))
base64.RawURLEncoding.Encode(dst, m.hashVersion)
buf.Write(dst)
}
}
return buf.String()
}
func (m verifyMDSC) KeyDataURN() (URN, error) {
return newURNForSuite(m.s, m.keyDataHash)
}
func (m verifyMDSC) String() string {
var buf strings.Builder
buf.WriteString(m.baseString())
buf.WriteString(m.versionSuffixString())
return buf.String()
}
func (m readMDSC) String() string {
var buf strings.Builder
buf.WriteString(m.verifyMDSC.baseString())
buf.WriteString(mdscDelim)
dst := make([]byte, base64.RawURLEncoding.EncodedLen(len(m.readKey)))
base64.RawURLEncoding.Encode(dst, m.readKey)
buf.Write(dst)
buf.WriteString(m.versionSuffixString())
return buf.String()
}
func (m mdsc) String() string {
var buf strings.Builder
buf.WriteString(m.verifyMDSC.baseString())
buf.WriteString(mdscDelim)
dst := make([]byte, base64.RawURLEncoding.EncodedLen(len(m.writeKey)))
base64.RawURLEncoding.Encode(dst, m.writeKey)
buf.Write(dst)
buf.WriteString(m.versionSuffixString())
return buf.String()
}
func (m mdsc) ReadCap() ReadCap {
r := &readMDSC{
verifyMDSC: verifyMDSC{
a: readAL,
s: m.s,
keyDataHash: make([]byte, len(m.keyDataHash)),
keyDataSymmKey: make([]byte, len(m.keyDataSymmKey)),
nVersion: m.nVersion,
hashVersion: make([]byte, len(m.hashVersion)),
},
readKey: nil,
}
copy(r.keyDataHash, m.keyDataHash)
copy(r.keyDataSymmKey, m.keyDataSymmKey)
copy(r.hashVersion, m.hashVersion)
r.readKey = toReadKey(m.writeKey)
return r
}
func (m mdsc) VerifyCap() VerifyCap {
v := &verifyMDSC{
a: verifyAL,
s: m.s,
keyDataHash: make([]byte, len(m.keyDataHash)),
keyDataSymmKey: make([]byte, len(m.keyDataSymmKey)),
nVersion: m.nVersion,
hashVersion: make([]byte, len(m.hashVersion)),
}
copy(v.keyDataHash, m.keyDataHash)
copy(v.keyDataSymmKey, m.keyDataSymmKey)
copy(v.hashVersion, m.hashVersion)
return v
}
func (r readMDSC) VerifyCap() VerifyCap {
v := &verifyMDSC{
a: verifyAL,
s: r.s,
keyDataHash: make([]byte, len(r.keyDataHash)),
keyDataSymmKey: make([]byte, len(r.keyDataSymmKey)),
nVersion: r.nVersion,
hashVersion: make([]byte, len(r.hashVersion)),
}
copy(v.keyDataHash, r.keyDataHash)
copy(v.keyDataSymmKey, r.keyDataSymmKey)
copy(v.hashVersion, r.hashVersion)
return v
}