forked from privacybydesign/irmago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attributes.go
359 lines (312 loc) · 10.3 KB
/
attributes.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
package irma
import (
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"errors"
"time"
"github.com/privacybydesign/gabi"
"github.com/privacybydesign/gabi/big"
)
const (
// ExpiryFactor is the precision for the expiry attribute. Value is one week.
ExpiryFactor = 60 * 60 * 24 * 7
metadataLength = 1 + 3 + 2 + 2 + 16
)
var (
versionField = metadataField{1, 0}
signingDateField = metadataField{3, 1}
validityField = metadataField{2, 4}
keyCounterField = metadataField{2, 6}
credentialID = metadataField{16, 8}
)
// metadataField contains the length and offset of a field within a metadata attribute.
type metadataField struct {
length int
offset int
}
// metadataAttribute represents a metadata attribute. Contains the credential type, signing date, validity, and the public key counter.
type MetadataAttribute struct {
Int *big.Int
pk *gabi.PublicKey
Conf *Configuration
}
// AttributeList contains attributes, excluding the secret key,
// providing convenient access to the metadata attribute.
type AttributeList struct {
*MetadataAttribute `json:"-"`
Ints []*big.Int
Revoked bool `json:",omitempty"`
strings []TranslatedString
attrMap map[AttributeTypeIdentifier]TranslatedString
info *CredentialInfo
h string
}
// NewAttributeListFromInts initializes a new AttributeList from a list of bigints.
func NewAttributeListFromInts(ints []*big.Int, conf *Configuration) *AttributeList {
al := &AttributeList{
Ints: ints,
MetadataAttribute: MetadataFromInt(ints[0], conf),
}
return al
}
func (al *AttributeList) Info() *CredentialInfo {
if al.info == nil {
al.info = NewCredentialInfo(al.Ints, al.Conf)
}
al.info.Revoked = al.Revoked
return al.info
}
// EqualsExceptMetadata checks whether two AttributeLists have the same attribute values.
// The attribute containing the metadata information is skipped in this check.
func (al *AttributeList) EqualsExceptMetadata(ol *AttributeList) bool {
if len(al.Ints) != len(ol.Ints) {
return false
}
// Check whether value of all attributes, except for metadata attribute, is equal
for i := 1; i < len(al.Ints); i++ {
if al.Ints[i].Cmp(ol.Ints[i]) != 0 {
return false
}
}
return true
}
func (al *AttributeList) Hash() string {
if al.h == "" {
bytes := []byte{}
for _, i := range al.Ints {
bytes = append(bytes, i.Bytes()...)
}
shasum := sha256.Sum256(bytes)
al.h = hex.EncodeToString(shasum[:])
}
return al.h
}
func (al *AttributeList) Map(conf *Configuration) map[AttributeTypeIdentifier]TranslatedString {
if al.attrMap == nil {
al.attrMap = make(map[AttributeTypeIdentifier]TranslatedString)
ctid := al.CredentialType().Identifier()
attrTypes := conf.CredentialTypes[ctid].AttributeTypes
for i, val := range al.Strings() {
if attrTypes[i].RevocationAttribute {
continue
}
al.attrMap[attrTypes[i].GetAttributeTypeIdentifier()] = val
}
}
return al.attrMap
}
// Strings converts the current instance to human-readable strings.
func (al *AttributeList) Strings() []TranslatedString {
if al.strings == nil {
al.strings = make([]TranslatedString, len(al.Ints)-1)
for i := range al.Ints[1:] { // skip metadata
val := al.decode(i)
if val == nil {
continue
}
al.strings[i] = NewTranslatedString(val)
}
}
return al.strings
}
// NewTranslatedString returns a TranslatedString containing the specified string for each supported language,
// or nil when attr is nil.
func NewTranslatedString(attr *string) TranslatedString {
if attr == nil {
return nil
}
return map[string]string{
"": *attr, // raw value
"en": *attr,
"nl": *attr,
}
}
func (al *AttributeList) decode(i int) *string {
attr := al.Ints[i+1]
metadataVersion := al.MetadataAttribute.Version()
return decodeAttribute(attr, metadataVersion)
}
// Decode attribute value into string according to metadataVersion
func decodeAttribute(attr *big.Int, metadataVersion byte) *string {
bi := new(big.Int).Set(attr)
if metadataVersion >= 3 {
if bi.Bit(0) == 0 { // attribute does not exist
return nil
}
bi.Rsh(bi, 1)
}
str := string(bi.Bytes())
return &str
}
// UntranslatedAttribute decodes the bigint corresponding to the specified attribute.
func (al *AttributeList) UntranslatedAttribute(identifier AttributeTypeIdentifier) *string {
if al.CredentialType().Identifier() != identifier.CredentialTypeIdentifier() {
return nil
}
for i, desc := range al.CredentialType().AttributeTypes {
if desc.ID == string(identifier.Name()) {
return al.decode(i)
}
}
return nil
}
// Attribute returns the content of the specified attribute, or nil if not present in this attribute list.
func (al *AttributeList) Attribute(identifier AttributeTypeIdentifier) TranslatedString {
if al.CredentialType().Identifier() != identifier.CredentialTypeIdentifier() {
return nil
}
for i, val := range al.Strings() {
if al.CredentialType().AttributeTypes[i].ID == string(identifier.Name()) {
return val
}
}
return nil
}
// MetadataFromInt wraps the given Int
func MetadataFromInt(i *big.Int, conf *Configuration) *MetadataAttribute {
return &MetadataAttribute{Int: i, Conf: conf}
}
// NewMetadataAttribute constructs a new instance containing the default values:
// provided version as versionField
// now as signing date
// 0 as keycounter
// ValidityDefault (half a year) as default validity.
func NewMetadataAttribute(version byte) *MetadataAttribute {
val := MetadataAttribute{new(big.Int), nil, nil}
val.setField(versionField, []byte{version})
val.setSigningDate()
val.setKeyCounter(0)
val.setDefaultValidityDuration()
return &val
}
// Bytes returns this metadata attribute as a byte slice.
func (attr *MetadataAttribute) Bytes() []byte {
bytes := attr.Int.Bytes()
if len(bytes) < metadataLength {
bytes = append(bytes, make([]byte, metadataLength-len(bytes))...)
}
return bytes
}
// PublicKey extracts identifier of the Idemix public key with which this instance was signed,
// and returns this public key.
func (attr *MetadataAttribute) PublicKey() (*gabi.PublicKey, error) {
if attr.pk == nil {
var err error
attr.pk, err = attr.Conf.PublicKey(attr.CredentialType().IssuerIdentifier(), attr.KeyCounter())
if err != nil {
return nil, err
}
}
return attr.pk, nil
}
// Version returns the metadata version of this instance
func (attr *MetadataAttribute) Version() byte {
return attr.field(versionField)[0]
}
// SigningDate returns the time at which this instance was signed
func (attr *MetadataAttribute) SigningDate() time.Time {
bytes := attr.field(signingDateField)
bytes = bytes[1:] // The signing date field is one byte too long
timestamp := int64(binary.BigEndian.Uint16(bytes)) * ExpiryFactor
return time.Unix(timestamp, 0)
}
func (attr *MetadataAttribute) setSigningDate() {
attr.setField(signingDateField, shortToByte(uint(time.Now().Unix()/ExpiryFactor)))
}
// KeyCounter return the public key counter of the metadata attribute
func (attr *MetadataAttribute) KeyCounter() uint {
return uint(binary.BigEndian.Uint16(attr.field(keyCounterField)))
}
func (attr *MetadataAttribute) setKeyCounter(i uint) {
attr.setField(keyCounterField, shortToByte(i))
}
// ValidityDuration returns the amount of epochs during which this instance is valid
func (attr *MetadataAttribute) ValidityDuration() int {
return int(binary.BigEndian.Uint16(attr.field(validityField)))
}
func (attr *MetadataAttribute) setValidityDuration(weeks uint) {
attr.setField(validityField, shortToByte(weeks))
}
func (attr *MetadataAttribute) setDefaultValidityDuration() {
// setExpiryDate only errors if setting the expiry date before the signing date,
// which never happens here
_ = attr.setExpiryDate(nil)
}
func (attr *MetadataAttribute) setExpiryDate(timestamp *Timestamp) error {
var expiry int64
if timestamp == nil {
expiry = time.Now().AddDate(0, 6, 0).Unix()
} else {
expiry = time.Time(*timestamp).Unix()
}
signing := attr.SigningDate().Unix()
if expiry-signing < 0 {
return errors.New("cannot set expired date")
}
attr.setValidityDuration(uint((expiry - signing) / ExpiryFactor))
return nil
}
// CredentialType returns the credential type of the current instance
// using the Configuration.
func (attr *MetadataAttribute) CredentialType() *CredentialType {
return attr.Conf.hashToCredentialType(attr.field(credentialID))
}
func (attr *MetadataAttribute) setCredentialTypeIdentifier(id string) {
bytes := sha256.Sum256([]byte(id))
attr.setField(credentialID, bytes[:16])
}
func (attr *MetadataAttribute) CredentialTypeHash() []byte {
return attr.field(credentialID)
}
// Expiry returns the expiry date of this instance
func (attr *MetadataAttribute) Expiry() time.Time {
expiry := attr.SigningDate().Unix() + int64(attr.ValidityDuration()*ExpiryFactor)
return time.Unix(expiry, 0)
}
// IsValidOn returns whether this instance is still valid at the given time
func (attr *MetadataAttribute) IsValidOn(t time.Time) bool {
return attr.Expiry().After(t)
}
// IsValid returns whether this instance is valid.
func (attr *MetadataAttribute) IsValid() bool {
return attr.IsValidOn(time.Now())
}
// FloorToEpochBoundary returns the greatest time not greater than the argument
// that falls on the boundary of an epoch for attribute validity or expiry,
// of which the value is defined by ExpiryFactor (one week).
func FloorToEpochBoundary(t time.Time) time.Time {
return time.Unix((t.Unix()/ExpiryFactor)*ExpiryFactor, 0)
}
func (attr *MetadataAttribute) field(field metadataField) []byte {
return attr.Bytes()[field.offset : field.offset+field.length]
}
func (attr *MetadataAttribute) setField(field metadataField, value []byte) {
if len(value) > field.length {
panic("Specified metadata field too large")
}
bytes := attr.Bytes()
// Push the value to the right within the field. Graphical representation:
// --xxxXXX----
// "-" indicates a byte of another field
// "X" is a byte of the value and "x" of our field
// In this example, our field has offset 2, length 6,
// but the specified value is only 3 bytes long.
startindex := field.length - len(value)
for i := 0; i < field.length; i++ {
if i < startindex {
bytes[i+field.offset] = 0
} else {
bytes[i+field.offset] = value[i-startindex]
}
}
attr.Int.SetBytes(bytes)
}
func shortToByte(x uint) []byte {
bytes := make([]byte, 2)
if x > 1<<16 {
panic("overflow uint16")
}
binary.BigEndian.PutUint16(bytes, uint16(x))
return bytes
}