helpers.go 5.08 KB
Newer Older
1
package core
Sietse Ringers's avatar
Sietse Ringers committed
2
3

import (
4
5
	"encoding/json"
	"reflect"
Sietse Ringers's avatar
Sietse Ringers committed
6
7
8
9
	"time"

	"github.com/dgrijalva/jwt-go"
	"github.com/go-errors/errors"
10
	"github.com/privacybydesign/gabi"
Sietse Ringers's avatar
Sietse Ringers committed
11
	"github.com/privacybydesign/irmago"
Sietse Ringers's avatar
Sietse Ringers committed
12
	"github.com/privacybydesign/irmago/server"
Sietse Ringers's avatar
Sietse Ringers committed
13
14
15
16
17
)

// Session helpers

func (session *session) finished() bool {
Sietse Ringers's avatar
Sietse Ringers committed
18
19
20
	return session.status == server.StatusDone ||
		session.status == server.StatusCancelled ||
		session.status == server.StatusTimeout
Sietse Ringers's avatar
Sietse Ringers committed
21
22
23
24
}

func (session *session) markAlive() {
	session.lastActive = time.Now()
25
	conf.Logger.Debugf("session %s marked active at %s", session.token, session.lastActive.String())
Sietse Ringers's avatar
Sietse Ringers committed
26
27
}

Sietse Ringers's avatar
Sietse Ringers committed
28
func (session *session) setStatus(status server.Status) {
29
	conf.Logger.Debugf("Status of session %s updated to %s", session.token, status)
Sietse Ringers's avatar
Sietse Ringers committed
30
	session.status = status
Sietse Ringers's avatar
Sietse Ringers committed
31
	session.result.Status = status
Sietse Ringers's avatar
Sietse Ringers committed
32
33
}

Sietse Ringers's avatar
Sietse Ringers committed
34
35
36
37
func (session *session) fail(err server.Error, message string) *irma.RemoteError {
	rerr := server.RemoteError(err, message)
	session.setStatus(server.StatusCancelled)
	session.result = &server.SessionResult{Err: rerr, Token: session.token, Status: server.StatusCancelled}
Sietse Ringers's avatar
Sietse Ringers committed
38
39
40
41
42
43
44
45
46
	return rerr
}

// Issuance helpers

func validateIssuanceRequest(request *irma.IssuanceRequest) error {
	for _, cred := range request.Credentials {
		// Check that we have the appropriate private key
		iss := cred.CredentialTypeID.IssuerIdentifier()
47
48
49
50
51
		privatekey, err := privatekey(iss)
		if err != nil {
			return err
		}
		if privatekey == nil {
52
			return errors.Errorf("missing private key of issuer %s", iss.String())
Sietse Ringers's avatar
Sietse Ringers committed
53
54
55
56
57
58
		}
		pubkey, err := conf.IrmaConfiguration.PublicKey(iss, int(privatekey.Counter))
		if err != nil {
			return err
		}
		if pubkey == nil {
59
			return errors.Errorf("missing public key of issuer %s", iss.String())
Sietse Ringers's avatar
Sietse Ringers committed
60
61
62
63
64
65
66
67
68
		}
		cred.KeyCounter = int(privatekey.Counter)

		// Check that the credential is consistent with irma_configuration
		if err := cred.Validate(conf.IrmaConfiguration); err != nil {
			return err
		}

		// Ensure the credential has an expiry date
69
		defaultValidity := irma.Timestamp(time.Now().AddDate(0, 6, 0))
Sietse Ringers's avatar
Sietse Ringers committed
70
71
72
73
74
75
76
77
78
79
80
		if cred.Validity == nil {
			cred.Validity = &defaultValidity
		}
		if cred.Validity.Before(irma.Timestamp(time.Now())) {
			return errors.New("cannot issue expired credentials")
		}
	}

	return nil
}

81
82
83
84
85
86
87
88
89
90
func privatekey(id irma.IssuerIdentifier) (sk *gabi.PrivateKey, err error) {
	sk = conf.IssuerPrivateKeys[id]
	if sk == nil {
		if sk, err = conf.IrmaConfiguration.PrivateKey(id); err != nil {
			return nil, err
		}
	}
	return sk, nil
}

Sietse Ringers's avatar
Sietse Ringers committed
91
func (session *session) getProofP(commitments *irma.IssueCommitmentMessage, scheme irma.SchemeManagerIdentifier) (*gabi.ProofP, error) {
Sietse Ringers's avatar
Sietse Ringers committed
92
93
94
95
96
97
98
99
100
	if session.kssProofs == nil {
		session.kssProofs = make(map[irma.SchemeManagerIdentifier]*gabi.ProofP)
	}

	if _, contains := session.kssProofs[scheme]; !contains {
		str, contains := commitments.ProofPjwts[scheme.Name()]
		if !contains {
			return nil, errors.Errorf("no keyshare proof included for scheme %s", scheme.Name())
		}
101
		conf.Logger.Trace("Parsing keyshare ProofP JWT: ", str)
Sietse Ringers's avatar
Sietse Ringers committed
102
103
104
105
		claims := &struct {
			jwt.StandardClaims
			ProofP *gabi.ProofP
		}{}
106
		token, err := jwt.ParseWithClaims(str, claims, conf.IrmaConfiguration.KeyshareServerKeyFunc(scheme))
Sietse Ringers's avatar
Sietse Ringers committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
		if err != nil {
			return nil, err
		}
		if !token.Valid {
			return nil, errors.Errorf("invalid keyshare proof included for scheme %s", scheme.Name())
		}
		session.kssProofs[scheme] = claims.ProofP
	}

	return session.kssProofs[scheme], nil
}

// Other

func chooseProtocolVersion(min, max *irma.ProtocolVersion) (*irma.ProtocolVersion, error) {
122
	if min.AboveVersion(maxProtocolVersion) || max.BelowVersion(minProtocolVersion) || max.BelowVersion(min) {
123
		return nil, server.LogWarning(errors.Errorf("Protocol version negotiation failed, min=%s max=%s", min.String(), max.String()))
Sietse Ringers's avatar
Sietse Ringers committed
124
125
126
127
128
129
130
	}
	if max.AboveVersion(maxProtocolVersion) {
		return maxProtocolVersion, nil
	} else {
		return max, nil
	}
}
131
132

// logPurgedRequest logs the request excluding any attribute values.
133
func logPurgedRequest(request irma.RequestorRequest) {
134
135
136
137
	// We want to log as much as possible of the request, but no attribute values.
	// We cannot just remove them from the request parameter as that would break the calling code.
	// So we create a deep copy of the request from which we can then safely remove whatever we want to.
	// Ugly hack alert: the easiest way to do this seems to be to convert it to JSON and then back.
138
	// As we do not know the precise type of request, we use reflection to create a new instance
139
140
141
142
143
144
	// of the same type as request, into which we then unmarshal our copy.
	cpy := reflect.New(reflect.TypeOf(request).Elem()).Interface()
	bts, _ := json.Marshal(request)
	_ = json.Unmarshal(bts, cpy)

	// Remove required attribute values from any attributes to be disclosed
145
	attrs := cpy.(irma.RequestorRequest).SessionRequest().ToDisclose()
146
147
148
149
	for _, disjunction := range attrs {
		disjunction.Values = nil
	}
	// Remove attribute values from attributes to be issued
150
151
	if isreq, ok := cpy.(*irma.IdentityProviderRequest); ok {
		for _, cred := range isreq.Request.Credentials {
152
153
154
155
156
157
			cred.Attributes = nil
		}
	}
	// Convert back to JSON to log
	conf.Logger.Info("Session request (purged of attribute values): ", server.ToJson(cpy))
}