credential.go 3.43 KB
Newer Older
1
2
3
package irmaclient

import (
4
5
	"errors"

6
	"github.com/privacybydesign/gabi"
7
	"github.com/privacybydesign/gabi/revocation"
8
	"github.com/privacybydesign/irmago"
9
10
11
12
13
14
)

// credential represents an IRMA credential, whose zeroth attribute
// is always the secret key and the first attribute the metadata attribute.
type credential struct {
	*gabi.Credential
15
16
	*irma.MetadataAttribute
	attrs *irma.AttributeList
17
18
}

19
20
func newCredential(gabicred *gabi.Credential, conf *irma.Configuration) (*credential, error) {
	meta := irma.MetadataFromInt(gabicred.Attributes[1], conf)
21
22
23
24
	cred := &credential{
		Credential:        gabicred,
		MetadataAttribute: meta,
	}
25
26
27
28
29
30

	if cred.CredentialType() == nil {
		// Unknown credtype, populate Pk field later
		return cred, nil
	}

31
	var err error
32
	cred.Pk, err = conf.PublicKey(meta.CredentialType().IssuerIdentifier(), cred.KeyCounter())
33
34
35
36
37
38
	if err != nil {
		return nil, err
	}
	return cred, nil
}

39
func (cred *credential) AttributeList() *irma.AttributeList {
40
	if cred.attrs == nil {
41
		cred.attrs = irma.NewAttributeListFromInts(cred.Credential.Attributes[1:], cred.MetadataAttribute.Conf)
42
43
44
	}
	return cred.attrs
}
45

46
// NonrevPrepare attempts to update the credential's nonrevocation witness from
47
// 1) the session request, and then 2) the revocation server if our witness is too far out of date.
48
49
// Returns whether or not the credential's nonrevocation state was updated. If so the caller should
// persist the updated credential to storage.
50
func (cred *credential) NonrevPrepare(conf *irma.Configuration, request irma.SessionRequest) (bool, error) {
51
	credtype := cred.CredentialType().Identifier()
52
53
	base := request.Base()
	if !base.RequestsRevocation(credtype) {
54
		return false, nil
55
56
	}

57
58
59
60
	if err := base.RevocationConsistent(); err != nil {
		return false, err
	}

61
62
	// first try to update witness by applying the revocation update messages attached to the session request
	keys := irma.RevocationKeys{Conf: conf}
63
	revupdates := base.RevocationUpdates[credtype]
64
	updated, err := cred.NonrevApplyUpdates(revupdates, keys)
65
	if err != nil {
66
		return updated, err
67
	}
68
	if cred.NonRevocationWitness.Accumulator.Index >= revupdates[len(revupdates)-1].EndIndex {
69
		return updated, nil
70
	}
71

72
73
	// nonrevocation witness is still out of date after applying the updates from the request:
	// we were too far behind. Update from revocation server.
74
	revupdates, err = irma.RevocationClient{Conf: conf}.FetchRevocationRecords(credtype, cred.NonRevocationWitness.Accumulator.Index+1)
75
	if err != nil {
76
		return updated, err
77
	}
78
	return cred.NonrevApplyUpdates(revupdates, keys)
79
80
}

81
// NonrevApplyUpdates updates the credential's nonrevocation witness using the specified messages,
82
// if they all verify and if their indices are ahead and adjacent to that of our witness.
83
func (cred *credential) NonrevApplyUpdates(messages []*irma.RevocationRecord, keys irma.RevocationKeys) (bool, error) {
84
	oldindex := cred.NonRevocationWitness.Accumulator.Index
85
86
87
88
89
90
91
92
93
94
95
96
97

	var err error
	var pk *revocation.PublicKey
	for _, record := range messages {
		if cred.CredentialType().IssuerIdentifier() != record.CredType.IssuerIdentifier() {
			return false, errors.New("cannot apply revocation record of other credential type")
		}
		if pk, err = keys.PublicKey(cred.CredentialType().IssuerIdentifier(), record.PublicKeyIndex); err != nil {
			return false, err
		}
		if err = cred.NonRevocationWitness.Update(pk, &record.Record); err != nil {
			return false, err
		}
98
	}
99

100
	return cred.NonRevocationWitness.Accumulator.Index != oldindex, nil
101
}