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

import (
4
	"github.com/privacybydesign/gabi"
5
	"github.com/privacybydesign/gabi/revocation"
6
	"github.com/privacybydesign/irmago"
7
8
9
10
11
12
)

// 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
13
14
	*irma.MetadataAttribute
	attrs *irma.AttributeList
15
16
}

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

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

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

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

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

54
	revupdates := request.Base().RevocationUpdates[credtype]
55
	updated, err := cred.NonrevApplyUpdates(revupdates, conf.RevocationStorage)
56
	if err != nil {
57
		return updated, err
58
	}
59
60

	// TODO (in both branches): attach our newer updates to response
61
62
	if cred.NonRevocationWitness.Index >= revupdates[len(revupdates)-1].EndIndex {
		return updated, nil
63
	}
64
65
66

	// nonrevocation witness is still out of date after applying the updates from the request,
	// i.e. we were too far behind. Update from revocation server.
67
	revupdates, err = conf.RevocationStorage.GetUpdates(credtype, cred.NonRevocationWitness.Index+1)
68
	if err != nil {
69
		return updated, err
70
	}
71
	return cred.NonrevApplyUpdates(revupdates, conf.RevocationStorage)
72
73
}

74
// NonrevApplyUpdates updates the credential's nonrevocation witness using the specified messages,
75
// if they all verify and if their indices are ahead and adjacent to that of our witness.
76
func (cred *credential) NonrevApplyUpdates(messages []*revocation.Record, rs *irma.RevocationStorage) (bool, error) {
77
	oldindex := cred.NonRevocationWitness.Index
78
79
	if err := cred.NonRevocationWitness.Update(rs.Keystore(cred.CredentialType().IssuerIdentifier()), messages); err != nil {
		return false, err
80
	}
81

82
	return cred.NonRevocationWitness.Index != oldindex, cred.NonrevPrepareCache()
83
}