handle.go 6.14 KB
Newer Older
1
package core
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.

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

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

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

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

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

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

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

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

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

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

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

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

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

Sietse Ringers's avatar
Sietse Ringers committed
105
106
107
108
	// 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
109
		return nil, session.fail(server.ErrorInvalidProofs, err.Error())
Sietse Ringers's avatar
Sietse Ringers committed
110
111
112
113
114
115
	}
	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)
	}
116

Sietse Ringers's avatar
Sietse Ringers committed
117
118
119
120
121
122
123
	// 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
124
				return nil, session.fail(server.ErrorKeyshareProofMissing, err.Error())
Sietse Ringers's avatar
Sietse Ringers committed
125
126
127
			}
			proof.MergeProofP(proofP, pubkey)
		}
128
129
	}

Sietse Ringers's avatar
Sietse Ringers committed
130
	// Verify all proofs and check disclosed attributes, if any, against request
Sietse Ringers's avatar
Sietse Ringers committed
131
	session.result.Disclosed, session.result.ProofStatus, err = commitments.Disclosure().VerifyAgainstDisjunctions(
Sietse Ringers's avatar
Sietse Ringers committed
132
		conf.IrmaConfiguration, request.Disclose, request.Context, request.Nonce, pubkeys, false)
133
134
	if err != nil {
		if err == irma.ErrorMissingPublicKey {
Sietse Ringers's avatar
Sietse Ringers committed
135
			return nil, session.fail(server.ErrorUnknownPublicKey, "")
136
		} else {
Sietse Ringers's avatar
Sietse Ringers committed
137
			return nil, session.fail(server.ErrorUnknown, "")
138
139
140
		}
	}
	if session.result.ProofStatus == irma.ProofStatusExpired {
Sietse Ringers's avatar
Sietse Ringers committed
141
		return nil, session.fail(server.ErrorAttributesExpired, "")
142
	}
Sietse Ringers's avatar
Sietse Ringers committed
143
	if session.result.ProofStatus != irma.ProofStatusValid {
Sietse Ringers's avatar
Sietse Ringers committed
144
		return nil, session.fail(server.ErrorInvalidProofs, "")
145
	}
Sietse Ringers's avatar
Sietse Ringers committed
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)
152
		sk, _ := conf.PrivateKey(id)
153
		issuer := gabi.NewIssuer(sk, pk, one)
Sietse Ringers's avatar
Sietse Ringers committed
154
155
156
		proof := commitments.Proofs[i+discloseCount].(*gabi.ProofU)
		attributes, err := cred.AttributeList(conf.IrmaConfiguration, 0x03)
		if err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
157
			return nil, session.fail(server.ErrorIssuanceFailed, err.Error())
Sietse Ringers's avatar
Sietse Ringers committed
158
159
160
		}
		sig, err := issuer.IssueSignature(proof.U, attributes.Ints, commitments.Nonce2)
		if err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
161
			return nil, session.fail(server.ErrorIssuanceFailed, err.Error())
Sietse Ringers's avatar
Sietse Ringers committed
162
163
		}
		sigs = append(sigs, sig)
164
	}
Sietse Ringers's avatar
Sietse Ringers committed
165

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