-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathresponder.rs
191 lines (175 loc) · 6.49 KB
/
responder.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) 2018-2022 The MobileCoin Foundation
//! Responder-specific transition functions
use crate::{
error::Error,
event::{AuthResponseOutput, ClientAuthRequestInput, NodeAuthRequestInput},
mealy::Transition,
state::{Ready, Start},
};
use alloc::vec::Vec;
use mc_attest_core::{ReportDataMask, VerificationReport};
use mc_crypto_keys::{Kex, ReprBytes};
use mc_crypto_noise::{
HandshakeIX, HandshakeNX, HandshakePattern, HandshakeState, HandshakeStatus, NoiseCipher,
NoiseDigest, ProtocolName,
};
use prost::Message;
use rand_core::{CryptoRng, RngCore};
/// A trait containing default implementations, used to tack repeatable chunks
/// of code onto the "Start" state for use below.
trait ResponderTransitionMixin {
fn handle_request<Handshake, KexAlgo, Cipher, DigestAlgo>(
&self,
data: &[u8],
local_identity: KexAlgo::Private,
) -> Result<(HandshakeState<KexAlgo, Cipher, DigestAlgo>, Vec<u8>), Error>
where
Handshake: HandshakePattern,
KexAlgo: Kex,
Cipher: NoiseCipher,
DigestAlgo: NoiseDigest,
ProtocolName<Handshake, KexAlgo, Cipher, DigestAlgo>: AsRef<str>;
fn handle_response<KexAlgo, Cipher, DigestAlgo>(
csprng: &mut (impl CryptoRng + RngCore),
handshake_state: HandshakeState<KexAlgo, Cipher, DigestAlgo>,
ias_report: VerificationReport,
) -> Result<(Ready<Cipher>, AuthResponseOutput), Error>
where
KexAlgo: Kex,
Cipher: NoiseCipher,
DigestAlgo: NoiseDigest;
}
impl ResponderTransitionMixin for Start {
fn handle_request<Handshake, KexAlgo, Cipher, DigestAlgo>(
&self,
data: &[u8],
local_identity: KexAlgo::Private,
) -> Result<(HandshakeState<KexAlgo, Cipher, DigestAlgo>, Vec<u8>), Error>
where
Handshake: HandshakePattern,
KexAlgo: Kex,
Cipher: NoiseCipher,
DigestAlgo: NoiseDigest,
ProtocolName<Handshake, KexAlgo, Cipher, DigestAlgo>: AsRef<str>,
{
let handshake_state = HandshakeState::new(
false,
ProtocolName::<Handshake, KexAlgo, Cipher, DigestAlgo>::default(),
self.responder_id.as_ref(),
Some(local_identity),
None,
None,
None,
)
.map_err(Error::HandshakeInit)?;
// Read the inbound message
let output = handshake_state
.read_message(data)
.map_err(Error::HandshakeWrite)?;
match output.status {
HandshakeStatus::InProgress(new_state) => Ok((new_state, output.payload)),
HandshakeStatus::Complete(_v) => Err(Error::EarlyHandshakeComplete),
}
}
fn handle_response<KexAlgo, Cipher, DigestAlgo>(
csprng: &mut (impl CryptoRng + RngCore),
handshake_state: HandshakeState<KexAlgo, Cipher, DigestAlgo>,
ias_report: VerificationReport,
) -> Result<(Ready<Cipher>, AuthResponseOutput), Error>
where
KexAlgo: Kex,
Cipher: NoiseCipher,
DigestAlgo: NoiseDigest,
{
// Encrypt the local report for output
let mut report_bytes = Vec::with_capacity(ias_report.encoded_len());
ias_report
.encode(&mut report_bytes)
.expect("Invariant failure, encoded_len insufficient to encode IAS report");
let output = handshake_state
.write_message(csprng, &report_bytes)
.map_err(Error::HandshakeWrite)?;
match output.status {
HandshakeStatus::InProgress(_state) => Err(Error::HandshakeNotComplete),
HandshakeStatus::Complete(result) => Ok((
Ready {
writer: result.responder_cipher,
reader: result.initiator_cipher,
binding: result.channel_binding,
},
AuthResponseOutput::from(output.payload),
)),
}
}
}
/// Start + NodeAuthRequestInput => Ready + AuthResponseOutput
///
/// This defines the responder's action when an AuthRequestInput for an IX
/// exchange is provided.
impl<KexAlgo, Cipher, DigestAlgo>
Transition<Ready<Cipher>, NodeAuthRequestInput<KexAlgo, Cipher, DigestAlgo>, AuthResponseOutput>
for Start
where
KexAlgo: Kex,
Cipher: NoiseCipher,
DigestAlgo: NoiseDigest,
ProtocolName<HandshakeIX, KexAlgo, Cipher, DigestAlgo>: AsRef<str>,
{
type Error = Error;
fn try_next<R: CryptoRng + RngCore>(
self,
csprng: &mut R,
input: NodeAuthRequestInput<KexAlgo, Cipher, DigestAlgo>,
) -> Result<(Ready<Cipher>, AuthResponseOutput), Error> {
// Read the request and return the payload and state
let (handshake_state, payload) = self
.handle_request::<HandshakeIX, KexAlgo, Cipher, DigestAlgo>(
&input.data.data,
input.local_identity,
)?;
let mut verifier = input.verifier;
// Parse the received IAS report
let remote_report = VerificationReport::decode(payload.as_slice())
.map_err(|_| Error::ReportDeserialization)?;
// Verify using given verifier, and ensure the first 32B of the report data are
// the identity pubkey.
verifier
.report_data(
&handshake_state
.remote_identity()
.ok_or(Error::MissingRemoteIdentity)?
.map_bytes(|bytes| {
ReportDataMask::try_from(bytes).map_err(|_| Error::BadRemoteIdentity)
})?,
)
.verify(&remote_report)?;
Self::handle_response(csprng, handshake_state, input.ias_report)
}
}
/// Start + ClientAuthRequestInput => Ready + AuthResponseOutput
impl<KexAlgo, Cipher, DigestAlgo>
Transition<
Ready<Cipher>,
ClientAuthRequestInput<KexAlgo, Cipher, DigestAlgo>,
AuthResponseOutput,
> for Start
where
KexAlgo: Kex,
Cipher: NoiseCipher,
DigestAlgo: NoiseDigest,
ProtocolName<HandshakeNX, KexAlgo, Cipher, DigestAlgo>: AsRef<str>,
{
type Error = Error;
fn try_next<R: CryptoRng + RngCore>(
self,
csprng: &mut R,
input: ClientAuthRequestInput<KexAlgo, Cipher, DigestAlgo>,
) -> Result<(Ready<Cipher>, AuthResponseOutput), Error> {
let (handshake_state, _payload) = self
.handle_request::<HandshakeNX, KexAlgo, Cipher, DigestAlgo>(
&input.data.data,
input.local_identity,
)?;
Self::handle_response(csprng, handshake_state, input.ias_report)
}
}