handle.go 6.21 KB
Newer Older
1
package servercore
2
3

import (
4
	"github.com/privacybydesign/gabi"
5
	"github.com/privacybydesign/irmago"
Sietse Ringers's avatar
Sietse Ringers committed
6
	"github.com/privacybydesign/irmago/server"
7
	"github.com/sirupsen/logrus"
8
9
)

Sietse Ringers's avatar
Sietse Ringers committed
10
11
12
13
14
// 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.

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

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

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

31
32
	var err error
	if session.version, err = chooseProtocolVersion(min, max); err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
33
		return nil, session.fail(server.ErrorProtocolVersion, "")
34
	}
35
	session.conf.Logger.WithFields(logrus.Fields{"session": session.token, "version": session.version.String()}).Debugf("Protocol version negotiated")
36
	session.request.Base().ProtocolVersion = session.version
37

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

42
43
44
45
func (session *session) handleGetStatus() (server.Status, *irma.RemoteError) {
	return session.status, nil
}

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

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

Sietse Ringers's avatar
Sietse Ringers committed
69
func (session *session) handlePostDisclosure(disclosure irma.Disclosure) (*irma.ProofStatus, *irma.RemoteError) {
Sietse Ringers's avatar
Sietse Ringers committed
70
71
	if session.status != server.StatusConnected {
		return nil, server.RemoteError(server.ErrorUnexpectedRequest, "Session not yet started or already finished")
72
	}
73
	session.markAlive()
74

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

Sietse Ringers's avatar
Sietse Ringers committed
91
func (session *session) handlePostCommitments(commitments *irma.IssueCommitmentMessage) ([]*gabi.IssueSignatureMessage, *irma.RemoteError) {
Sietse Ringers's avatar
Sietse Ringers committed
92
93
	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
94
95
	}
	session.markAlive()
96

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

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

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

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

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

Sietse Ringers's avatar
Sietse Ringers committed
164
	session.setStatus(server.StatusDone)
Sietse Ringers's avatar
Sietse Ringers committed
165
	return sigs, nil
166
}