handle.go 6.33 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
	request := session.request.(*irma.IssuanceRequest)
98

99
100
101
	discloseCount := len(commitments.Proofs) - len(request.Credentials)
	if discloseCount < 0 {
		return nil, session.fail(server.ErrorMalformedInput, "Received insufficient proofs")
Sietse Ringers's avatar
Sietse Ringers committed
102
	}
103

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

Sietse Ringers's avatar
Sietse Ringers committed
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()
120
		if session.conf.IrmaConfiguration.SchemeManagers[schemeid].Distributed() {
Sietse Ringers's avatar
Sietse Ringers committed
121
122
			proofP, err := session.getProofP(commitments, schemeid)
			if err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
123
				return nil, session.fail(server.ErrorKeyshareProofMissing, err.Error())
Sietse Ringers's avatar
Sietse Ringers committed
124
125
126
			}
			proof.MergeProofP(proofP, pubkey)
		}
127
128
	}

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

	// Compute CL signatures
	var sigs []*gabi.IssueSignatureMessage
	for i, cred := range request.Credentials {
		id := cred.CredentialTypeID.IssuerIdentifier()
150
151
		pk, _ := session.conf.IrmaConfiguration.PublicKey(id, cred.KeyCounter)
		sk, _ := session.conf.PrivateKey(id)
152
		issuer := gabi.NewIssuer(sk, pk, one)
153
154
155
156
		proof, ok := commitments.Proofs[i+discloseCount].(*gabi.ProofU)
		if !ok {
			return nil, session.fail(server.ErrorMalformedInput, "Received invalid issuance commitment")
		}
157
		attributes, err := cred.AttributeList(session.conf.IrmaConfiguration, 0x03)
Sietse Ringers's avatar
Sietse Ringers committed
158
		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
162
		}
		sig, err := issuer.IssueSignature(proof.U, attributes.Ints, commitments.Nonce2)
		if err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
163
			return nil, session.fail(server.ErrorIssuanceFailed, err.Error())
Sietse Ringers's avatar
Sietse Ringers committed
164
165
		}
		sigs = append(sigs, sig)
166
	}
Sietse Ringers's avatar
Sietse Ringers committed
167

Sietse Ringers's avatar
Sietse Ringers committed
168
	session.setStatus(server.StatusDone)
Sietse Ringers's avatar
Sietse Ringers committed
169
	return sigs, nil
170
}