handle.go 5.91 KB
Newer Older
1
2
3
4
5
package backend

import (
	"github.com/mhe/gabi"
	"github.com/privacybydesign/irmago"
Sietse Ringers's avatar
Sietse Ringers committed
6
	"github.com/privacybydesign/irmago/server"
7
8
)

Sietse Ringers's avatar
Sietse Ringers committed
9
10
11
12
13
// This file contains the handler functions for the protocol messages, receiving and returning normally
// Go-typed messages here (JSON (un)marshalling is handled by the router).
// Maintaining the session state is done here, as well as checking whether the session is in the
// appropriate status before handling the request.

Sietse Ringers's avatar
Sietse Ringers committed
14
var conf *server.Configuration
15

16
func (session *session) handleDelete() {
17
	if session.finished() {
18
		return
19
	}
20
	session.markAlive()
Sietse Ringers's avatar
Sietse Ringers committed
21

Sietse Ringers's avatar
Sietse Ringers committed
22
23
	session.result = &server.SessionResult{Token: session.token, Status: server.StatusCancelled}
	session.setStatus(server.StatusCancelled)
24
25
}

26
func (session *session) handleGetRequest(min, max *irma.ProtocolVersion) (irma.SessionRequest, *irma.RemoteError) {
Sietse Ringers's avatar
Sietse Ringers committed
27
28
	if session.status != server.StatusInitialized {
		return nil, server.RemoteError(server.ErrorUnexpectedRequest, "Session already started")
29
30
31
	}
	session.markAlive()

32
33
	var err error
	if session.version, err = chooseProtocolVersion(min, max); err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
34
		return nil, session.fail(server.ErrorProtocolVersion, "")
35
36
	}
	session.request.SetVersion(session.version)
37

Sietse Ringers's avatar
Sietse Ringers committed
38
	session.setStatus(server.StatusConnected)
39
	return session.request, nil
40
41
}

42
func (session *session) handlePostSignature(signature *irma.SignedMessage) (*irma.ProofStatus, *irma.RemoteError) {
Sietse Ringers's avatar
Sietse Ringers committed
43
44
	if session.status != server.StatusConnected {
		return nil, server.RemoteError(server.ErrorUnexpectedRequest, "Session not yet started or already finished")
45
	}
46
	session.markAlive()
47

48
49
	var err error
	var rerr *irma.RemoteError
50
	session.result.Signature = signature
51
	session.result.Disclosed, session.result.ProofStatus, err = signature.Verify(
52
		conf.IrmaConfiguration, session.request.(*irma.SignatureRequest))
53
	if err == nil {
Sietse Ringers's avatar
Sietse Ringers committed
54
		session.setStatus(server.StatusDone)
55
	} else {
Sietse Ringers's avatar
Sietse Ringers committed
56
		session.setStatus(server.StatusCancelled)
57
		if err == irma.ErrorMissingPublicKey {
Sietse Ringers's avatar
Sietse Ringers committed
58
			rerr = session.fail(server.ErrorUnknownPublicKey, err.Error())
59
		} else {
Sietse Ringers's avatar
Sietse Ringers committed
60
			rerr = session.fail(server.ErrorUnknown, err.Error())
61
62
63
		}
	}
	return &session.result.ProofStatus, rerr
64
}
65

66
func (session *session) handlePostProofs(proofs gabi.ProofList) (*irma.ProofStatus, *irma.RemoteError) {
Sietse Ringers's avatar
Sietse Ringers committed
67
68
	if session.status != server.StatusConnected {
		return nil, server.RemoteError(server.ErrorUnexpectedRequest, "Session not yet started or already finished")
69
	}
70
	session.markAlive()
71

72
73
74
	var err error
	var rerr *irma.RemoteError
	session.result.Disclosed, session.result.ProofStatus, err = irma.ProofList(proofs).Verify(
75
		conf.IrmaConfiguration, session.request.(*irma.DisclosureRequest))
76
	if err == nil {
Sietse Ringers's avatar
Sietse Ringers committed
77
		session.setStatus(server.StatusDone)
78
	} else {
Sietse Ringers's avatar
Sietse Ringers committed
79
		session.setStatus(server.StatusCancelled)
80
		if err == irma.ErrorMissingPublicKey {
Sietse Ringers's avatar
Sietse Ringers committed
81
			rerr = session.fail(server.ErrorUnknownPublicKey, err.Error())
82
		} else {
Sietse Ringers's avatar
Sietse Ringers committed
83
			rerr = session.fail(server.ErrorUnknown, err.Error())
84
85
86
		}
	}
	return &session.result.ProofStatus, rerr
87
88
}

Sietse Ringers's avatar
Sietse Ringers committed
89
func (session *session) handlePostCommitments(commitments *gabi.IssueCommitmentMessage) ([]*gabi.IssueSignatureMessage, *irma.RemoteError) {
Sietse Ringers's avatar
Sietse Ringers committed
90
91
	if session.status != server.StatusConnected {
		return nil, server.RemoteError(server.ErrorUnexpectedRequest, "Session not yet started or already finished")
Sietse Ringers's avatar
Sietse Ringers committed
92
93
	}
	session.markAlive()
94

Sietse Ringers's avatar
Sietse Ringers committed
95
96
97
	request := session.request.(*irma.IssuanceRequest)
	discloseCount := len(request.Disclose)
	if len(commitments.Proofs) != len(request.Credentials)+discloseCount {
Sietse Ringers's avatar
Sietse Ringers committed
98
		return nil, session.fail(server.ErrorAttributesMissing, "")
Sietse Ringers's avatar
Sietse Ringers committed
99
	}
100

Sietse Ringers's avatar
Sietse Ringers committed
101
102
103
104
	// Compute list of public keys against which to verify the received proofs
	disclosureproofs := irma.ProofList(commitments.Proofs[:discloseCount])
	pubkeys, err := disclosureproofs.ExtractPublicKeys(conf.IrmaConfiguration)
	if err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
105
		return nil, session.fail(server.ErrorInvalidProofs, err.Error())
Sietse Ringers's avatar
Sietse Ringers committed
106
107
108
109
110
111
	}
	for _, cred := range request.Credentials {
		iss := cred.CredentialTypeID.IssuerIdentifier()
		pubkey, _ := conf.IrmaConfiguration.PublicKey(iss, cred.KeyCounter) // No error, already checked earlier
		pubkeys = append(pubkeys, pubkey)
	}
112

Sietse Ringers's avatar
Sietse Ringers committed
113
114
115
116
117
118
119
	// Verify and merge keyshare server proofs, if any
	for i, proof := range commitments.Proofs {
		pubkey := pubkeys[i]
		schemeid := irma.NewIssuerIdentifier(pubkey.Issuer).SchemeManagerIdentifier()
		if conf.IrmaConfiguration.SchemeManagers[schemeid].Distributed() {
			proofP, err := session.getProofP(commitments, schemeid)
			if err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
120
				return nil, session.fail(server.ErrorKeyshareProofMissing, err.Error())
Sietse Ringers's avatar
Sietse Ringers committed
121
122
123
			}
			proof.MergeProofP(proofP, pubkey)
		}
124
125
	}

Sietse Ringers's avatar
Sietse Ringers committed
126
	// Verify all proofs and check disclosed attributes, if any, against request
127
	session.result.Disclosed, session.result.ProofStatus, err = irma.ProofList(commitments.Proofs).VerifyAgainstDisjunctions(
Sietse Ringers's avatar
Sietse Ringers committed
128
		conf.IrmaConfiguration, request.Disclose, request.Context, request.Nonce, pubkeys, false)
129
130
	if err != nil {
		if err == irma.ErrorMissingPublicKey {
Sietse Ringers's avatar
Sietse Ringers committed
131
			return nil, session.fail(server.ErrorUnknownPublicKey, "")
132
		} else {
Sietse Ringers's avatar
Sietse Ringers committed
133
			return nil, session.fail(server.ErrorUnknown, "")
134
135
136
		}
	}
	if session.result.ProofStatus == irma.ProofStatusExpired {
Sietse Ringers's avatar
Sietse Ringers committed
137
		return nil, session.fail(server.ErrorAttributesExpired, "")
138
	}
Sietse Ringers's avatar
Sietse Ringers committed
139
	if session.result.ProofStatus != irma.ProofStatusValid {
Sietse Ringers's avatar
Sietse Ringers committed
140
		return nil, session.fail(server.ErrorInvalidProofs, "")
141
	}
Sietse Ringers's avatar
Sietse Ringers committed
142
143
144
145
146
147
148
149
150
151

	// Compute CL signatures
	var sigs []*gabi.IssueSignatureMessage
	for i, cred := range request.Credentials {
		id := cred.CredentialTypeID.IssuerIdentifier()
		pk, _ := conf.IrmaConfiguration.PublicKey(id, cred.KeyCounter)
		issuer := gabi.NewIssuer(conf.PrivateKeys[id], pk, one)
		proof := commitments.Proofs[i+discloseCount].(*gabi.ProofU)
		attributes, err := cred.AttributeList(conf.IrmaConfiguration, 0x03)
		if err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
152
			return nil, session.fail(server.ErrorIssuanceFailed, err.Error())
Sietse Ringers's avatar
Sietse Ringers committed
153
154
155
		}
		sig, err := issuer.IssueSignature(proof.U, attributes.Ints, commitments.Nonce2)
		if err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
156
			return nil, session.fail(server.ErrorIssuanceFailed, err.Error())
Sietse Ringers's avatar
Sietse Ringers committed
157
158
		}
		sigs = append(sigs, sig)
159
	}
Sietse Ringers's avatar
Sietse Ringers committed
160

Sietse Ringers's avatar
Sietse Ringers committed
161
	session.setStatus(server.StatusDone)
Sietse Ringers's avatar
Sietse Ringers committed
162
	return sigs, nil
163
}