forked from privacybydesign/irmago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identifiers.go
430 lines (368 loc) · 12.9 KB
/
identifiers.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
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
424
425
426
427
428
429
430
package irma
import (
"database/sql/driver" // only imported to refer to the driver.Value type
"fmt"
"strconv"
"strings"
"github.com/fxamacker/cbor"
"github.com/go-errors/errors"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
type metaObjectIdentifier string
func (oi *metaObjectIdentifier) UnmarshalCBOR(data []byte) error {
return cbor.Unmarshal(data, (*string)(oi))
}
func (oi metaObjectIdentifier) MarshalCBOR() (data []byte, err error) {
return cbor.Marshal(string(oi), cbor.EncOptions{})
}
// RequestorSchemeIdentifier identifies a requestor scheme. Equal to its ID. For example "pbdf-requestors"
type RequestorSchemeIdentifier struct {
metaObjectIdentifier
}
type RequestorIdentifier struct {
metaObjectIdentifier
}
type IssueWizardIdentifier struct {
metaObjectIdentifier
}
// SchemeManagerIdentifier identifies a scheme manager. Equal to its ID. For example "irma-demo".
type SchemeManagerIdentifier struct {
metaObjectIdentifier
}
// IssuerIdentifier identifies an issuer. For example "irma-demo.RU".
type IssuerIdentifier struct {
metaObjectIdentifier
}
// PublicKeyIdentifier identifies a single key from an issuer. For example: "irma-demo.RU-1"
type PublicKeyIdentifier struct {
Issuer IssuerIdentifier `json:"issuer"`
Counter uint `json:"counter"`
}
// CredentialTypeIdentifier identifies a credentialtype. For example "irma-demo.RU.studentCard".
type CredentialTypeIdentifier struct {
metaObjectIdentifier
}
// AttributeTypeIdentifier identifies an attribute. For example "irma-demo.RU.studentCard.studentID".
type AttributeTypeIdentifier struct {
metaObjectIdentifier
}
// CredentialIdentifier identifies a credential instance.
type CredentialIdentifier struct {
Type CredentialTypeIdentifier
Hash string
}
// AttributeIdentifier identifies an attribute instance.
type AttributeIdentifier struct {
Type AttributeTypeIdentifier
CredentialHash string
}
// IrmaIdentifierSet contains a set (ensured by using map[...]struct{}) of all scheme managers,
// all issuers, all credential types, all public keys and all attribute types that are involved in an IRMA session.
type IrmaIdentifierSet struct {
SchemeManagers map[SchemeManagerIdentifier]struct{}
Issuers map[IssuerIdentifier]struct{}
CredentialTypes map[CredentialTypeIdentifier]struct{}
PublicKeys map[IssuerIdentifier][]uint
AttributeTypes map[AttributeTypeIdentifier]struct{}
RequestorSchemes map[RequestorSchemeIdentifier]struct{}
}
func newIrmaIdentifierSet() *IrmaIdentifierSet {
return &IrmaIdentifierSet{
SchemeManagers: map[SchemeManagerIdentifier]struct{}{},
Issuers: map[IssuerIdentifier]struct{}{},
CredentialTypes: map[CredentialTypeIdentifier]struct{}{},
PublicKeys: map[IssuerIdentifier][]uint{},
AttributeTypes: map[AttributeTypeIdentifier]struct{}{},
RequestorSchemes: map[RequestorSchemeIdentifier]struct{}{},
}
}
// Parent returns the parent object of this identifier.
func (oi metaObjectIdentifier) Parent() string {
str := string(oi)
if i := strings.LastIndex(str, "."); i != -1 {
return str[:i]
} else {
return str
}
}
// Name returns the last part of this identifier.
func (oi metaObjectIdentifier) Name() string {
str := string(oi)
return str[strings.LastIndex(str, ".")+1:]
}
// String returns this identifier as a string.
func (oi metaObjectIdentifier) String() string {
return string(oi)
}
func (oi metaObjectIdentifier) Empty() bool {
return len(oi) == 0
}
func (oi metaObjectIdentifier) Root() string {
str := string(oi)
if i := strings.Index(str, "."); i != -1 {
return str[:i]
} else {
return str
}
}
func (oi metaObjectIdentifier) PartsCount() int {
return strings.Count(string(oi), ".")
}
// NewRequestorSchemeIdentifier converts the specified identifier to a RequestorSchemeIdentifier.
func NewRequestorSchemeIdentifier(id string) RequestorSchemeIdentifier {
return RequestorSchemeIdentifier{metaObjectIdentifier(id)}
}
// NewRequestorIdentifier converts the specified identifier to a NewRequestorIdentifier.
func NewRequestorIdentifier(id string) RequestorIdentifier {
return RequestorIdentifier{metaObjectIdentifier(id)}
}
// NewIssueWizardIdentifier converts the specified identifier to a NewIssueWizardIdentifier.
func NewIssueWizardIdentifier(id string) IssueWizardIdentifier {
return IssueWizardIdentifier{metaObjectIdentifier(id)}
}
// NewSchemeManagerIdentifier converts the specified identifier to a SchemeManagerIdentifier.
func NewSchemeManagerIdentifier(id string) SchemeManagerIdentifier {
return SchemeManagerIdentifier{metaObjectIdentifier(id)}
}
// NewIssuerIdentifier converts the specified identifier to a IssuerIdentifier.
func NewIssuerIdentifier(id string) IssuerIdentifier {
return IssuerIdentifier{metaObjectIdentifier(id)}
}
// NewCredentialTypeIdentifier converts the specified identifier to a CredentialTypeIdentifier.
func NewCredentialTypeIdentifier(id string) CredentialTypeIdentifier {
return CredentialTypeIdentifier{metaObjectIdentifier(id)}
}
// NewAttributeTypeIdentifier converts the specified identifier to a AttributeTypeIdentifier.
func NewAttributeTypeIdentifier(id string) AttributeTypeIdentifier {
return AttributeTypeIdentifier{metaObjectIdentifier(id)}
}
// RequestorIdentifier returns the requestor identifier of the issue wizard.
func (id IssueWizardIdentifier) RequestorIdentifier() RequestorIdentifier {
return NewRequestorIdentifier(id.Parent())
}
// RequestorSchemeIdentifier returns the requestor scheme identifier of the requestor.
func (id RequestorIdentifier) RequestorSchemeIdentifier() RequestorSchemeIdentifier {
return NewRequestorSchemeIdentifier(id.Parent())
}
// SchemeManagerIdentifier returns the scheme manager identifer of the issuer.
func (id IssuerIdentifier) SchemeManagerIdentifier() SchemeManagerIdentifier {
return NewSchemeManagerIdentifier(id.Parent())
}
// IssuerIdentifier returns the IssuerIdentifier of the credential identifier.
func (id CredentialTypeIdentifier) IssuerIdentifier() IssuerIdentifier {
return NewIssuerIdentifier(id.Parent())
}
func (id CredentialTypeIdentifier) SchemeManagerIdentifier() SchemeManagerIdentifier {
return NewSchemeManagerIdentifier(id.Root())
}
// CredentialTypeIdentifier returns the CredentialTypeIdentifier of the attribute identifier.
func (id AttributeTypeIdentifier) CredentialTypeIdentifier() CredentialTypeIdentifier {
if id.IsCredential() {
return NewCredentialTypeIdentifier(id.String())
}
return NewCredentialTypeIdentifier(id.Parent())
}
// IsCredential returns true if this attribute refers to its containing credential
// (i.e., it consists of only 3 parts).
func (id AttributeTypeIdentifier) IsCredential() bool {
return strings.Count(id.String(), ".") == 2
}
// CredentialIdentifier returns the credential identifier of this attribute.
func (ai *AttributeIdentifier) CredentialIdentifier() CredentialIdentifier {
return CredentialIdentifier{Type: ai.Type.CredentialTypeIdentifier(), Hash: ai.CredentialHash}
}
// MarshalText implements encoding.TextMarshaler.
func (id SchemeManagerIdentifier) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (id *SchemeManagerIdentifier) UnmarshalText(text []byte) error {
*id = NewSchemeManagerIdentifier(string(text))
return nil
}
// MarshalText implements encoding.TextMarshaler.
func (id RequestorSchemeIdentifier) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (id *RequestorSchemeIdentifier) UnmarshalText(text []byte) error {
*id = NewRequestorSchemeIdentifier(string(text))
return nil
}
// MarshalText implements encoding.TextMarshaler.
func (id RequestorIdentifier) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (id *RequestorIdentifier) UnmarshalText(text []byte) error {
*id = NewRequestorIdentifier(string(text))
return nil
}
// MarshalText implements encoding.TextMarshaler.
func (id IssueWizardIdentifier) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (id *IssueWizardIdentifier) UnmarshalText(text []byte) error {
*id = NewIssueWizardIdentifier(string(text))
return nil
}
// MarshalText implements encoding.TextMarshaler.
func (id IssuerIdentifier) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (id *IssuerIdentifier) UnmarshalText(text []byte) error {
*id = NewIssuerIdentifier(string(text))
return nil
}
func (pki *PublicKeyIdentifier) UnmarshalText(text []byte) error {
str := string(text)
index := strings.LastIndex(str, "-")
if index == -1 {
return errors.New("Invalid PublicKeyIdentifier")
}
counter, err := strconv.Atoi(str[index+1:])
if err != nil {
return err
}
*pki = PublicKeyIdentifier{Issuer: NewIssuerIdentifier(str[:index]), Counter: uint(counter)}
return nil
}
func (pki *PublicKeyIdentifier) MarshalText() (text []byte, err error) {
return []byte(fmt.Sprintf("%s-%d", pki.Issuer, pki.Counter)), nil
}
// MarshalText implements encoding.TextMarshaler.
func (id CredentialTypeIdentifier) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (id *CredentialTypeIdentifier) UnmarshalText(text []byte) error {
*id = NewCredentialTypeIdentifier(string(text))
return nil
}
// MarshalText implements encoding.TextMarshaler.
func (id AttributeTypeIdentifier) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (id *AttributeTypeIdentifier) UnmarshalText(text []byte) error {
*id = NewAttributeTypeIdentifier(string(text))
return nil
}
func (set *IrmaIdentifierSet) join(other *IrmaIdentifierSet) {
for scheme := range other.SchemeManagers {
set.SchemeManagers[scheme] = struct{}{}
}
for issuer := range other.Issuers {
set.Issuers[issuer] = struct{}{}
}
for ct := range other.CredentialTypes {
set.CredentialTypes[ct] = struct{}{}
}
for at := range other.AttributeTypes {
set.AttributeTypes[at] = struct{}{}
}
for issuer := range other.PublicKeys {
if len(set.PublicKeys[issuer]) == 0 {
set.PublicKeys[issuer] = make([]uint, 0, len(other.PublicKeys[issuer]))
}
set.PublicKeys[issuer] = append(set.PublicKeys[issuer], other.PublicKeys[issuer]...)
}
for scheme := range other.RequestorSchemes {
set.RequestorSchemes[scheme] = struct{}{}
}
}
func (set *IrmaIdentifierSet) Distributed(conf *Configuration) bool {
for id := range set.SchemeManagers {
if conf.SchemeManagers[id].Distributed() {
return true
}
}
return false
}
func (set *IrmaIdentifierSet) allSchemes() map[SchemeManagerIdentifier]struct{} {
schemes := make(map[SchemeManagerIdentifier]struct{})
for s := range set.SchemeManagers {
schemes[s] = struct{}{}
}
for i := range set.Issuers {
schemes[i.SchemeManagerIdentifier()] = struct{}{}
}
for i := range set.PublicKeys {
if len(set.PublicKeys[i]) > 0 {
schemes[i.SchemeManagerIdentifier()] = struct{}{}
}
}
for c := range set.CredentialTypes {
schemes[c.IssuerIdentifier().SchemeManagerIdentifier()] = struct{}{}
}
for a := range set.AttributeTypes {
schemes[a.CredentialTypeIdentifier().IssuerIdentifier().SchemeManagerIdentifier()] = struct{}{}
}
return schemes
}
func (set *IrmaIdentifierSet) String() string {
var builder strings.Builder
for s := range set.SchemeManagers {
builder.WriteString(s.String() + ", ")
}
for s := range set.RequestorSchemes {
builder.WriteString(s.String() + ", ")
}
for i := range set.Issuers {
builder.WriteString(i.String() + ", ")
}
for i, keys := range set.PublicKeys {
for _, k := range keys {
builder.WriteString(fmt.Sprintf("%s-%d", i.String(), k))
}
}
for c := range set.CredentialTypes {
builder.WriteString(c.String() + ", ")
}
for a := range set.AttributeTypes {
builder.WriteString(a.String() + ", ")
}
s := builder.String()
if len(s) > 0 { // strip trailing comma
s = s[:len(s)-2]
}
return s
}
func (set *IrmaIdentifierSet) Empty() bool {
return len(set.SchemeManagers) == 0 &&
len(set.Issuers) == 0 &&
len(set.CredentialTypes) == 0 &&
len(set.PublicKeys) == 0 &&
len(set.AttributeTypes) == 0 &&
len(set.RequestorSchemes) == 0
}
// Value implements sql/driver Scanner interface.
func (oi metaObjectIdentifier) Value() (driver.Value, error) {
return oi.String(), nil
}
// Scan implements sql/driver Scanner interface.
func (oi *metaObjectIdentifier) Scan(src interface{}) error {
switch s := src.(type) {
case string:
*oi = metaObjectIdentifier(s)
return nil
case []byte:
*oi = metaObjectIdentifier(s)
return nil
}
return errors.New("cannot convert source: not a string or []byte")
}
// GormDBDataType implements the gorm.io/gorm/migrator GormDataTypeInterface interface.
func (metaObjectIdentifier) GormDBDataType(db *gorm.DB, _ *schema.Field) string {
switch db.Dialector.Name() {
case "postgres":
return "text"
case "mysql":
return "varchar(255)"
default:
return ""
}
}