Skip to content

Commit

Permalink
Merge pull request #261 from chrysn-pull-requests/expose-c_i
Browse files Browse the repository at this point in the history
Expose C_I to responders
  • Loading branch information
geonnave authored May 3, 2024
2 parents e4a1e94 + 57a1586 commit c0d74ac
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/coap/src/bin/coapserver-coaphandler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl coap_handler::Handler for EdhocHandler {
let message_1 =
&EdhocMessageBuffer::new_from_slice(&request.payload()[1..]).map_err(too_small)?;

let (responder, ead_1) =
let (responder, _c_i, ead_1) =
EdhocResponder::new(lakers_crypto::default_crypto(), &R, cred_r)
.process_message_1(message_1)
.map_err(render_error)?;
Expand Down
2 changes: 1 addition & 1 deletion examples/coap/src/bin/coapserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn main() {
.expect("wrong length");
let result = responder.process_message_1(&message_1);

if let Ok((responder, ead_1)) = result {
if let Ok((responder, _c_i, ead_1)) = result {
let c_r =
generate_connection_identifier_cbor(&mut lakers_crypto::default_crypto());
let ead_2 = if let Some(ead_1) = ead_1 {
Expand Down
2 changes: 1 addition & 1 deletion examples/lakers-no_std/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn main() -> ! {

let (initiator, message_1) = initiator.prepare_message_1(None, &None).unwrap();

let (responder, _ead_1) = responder.process_message_1(&message_1).unwrap();
let (responder, _c_i, _ead_1) = responder.process_message_1(&message_1).unwrap();
let (responder, message_2) = responder
.prepare_message_2(CredentialTransfer::ByReference, None, &None)
.unwrap();
Expand Down
12 changes: 9 additions & 3 deletions lakers-python/src/responder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@ impl PyEdhocResponder {
}
}

fn process_message_1(&mut self, message_1: Vec<u8>) -> PyResult<Option<EADItem>> {
fn process_message_1<'a>(
&mut self,
py: Python<'a>,
message_1: Vec<u8>,
) -> PyResult<(&'a PyBytes, Option<EADItem>)> {
let message_1 = EdhocMessageBuffer::new_from_slice(message_1.as_slice())?;
let (state, ead_1) = r_process_message_1(&self.start, &mut default_crypto(), &message_1)?;
let (state, c_i, ead_1) =
r_process_message_1(&self.start, &mut default_crypto(), &message_1)?;
self.processing_m1 = state;
let c_i = PyBytes::new(py, c_i.as_slice());

Ok(ead_1)
Ok((c_i, ead_1))
}

fn prepare_message_2<'a>(
Expand Down
2 changes: 1 addition & 1 deletion lakers-python/test/test_ead_authz.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_handshake_with_authz():
device.set_h_message_1(initiator.get_h_message_1())

# responder
ead_1 = responder.process_message_1(message_1)
_c_i, ead_1 = responder.process_message_1(message_1)
loc_w, voucher_request = authenticator.process_ead_1(ead_1, message_1)
voucher_response = enrollment_server.handle_voucher_request(voucher_request)
ead_2 = authenticator.prepare_ead_2(voucher_response)
Expand Down
2 changes: 1 addition & 1 deletion lakers-python/test/test_lakers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_handshake():
message_1 = initiator.prepare_message_1(c_i=None, ead_1=None)

# responder
ead_1 = responder.process_message_1(message_1)
_c_i, ead_1 = responder.process_message_1(message_1)
assert ead_1 == None
message_2 = responder.prepare_message_2(lakers.CredentialTransfer.ByReference, None, ead_1)
assert type(message_2) == bytes
Expand Down
3 changes: 2 additions & 1 deletion lib/src/edhoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn r_process_message_1(
state: &ResponderStart,
crypto: &mut impl CryptoTrait,
message_1: &BufferMessage1,
) -> Result<(ProcessingM1, Option<EADItem>), EDHOCError> {
) -> Result<(ProcessingM1, ConnId, Option<EADItem>), EDHOCError> {
// Step 1: decode message_1
// g_x will be saved to the state
if let Ok((method, suites_i, suites_i_len, g_x, c_i, ead_1)) = parse_message_1(message_1) {
Expand All @@ -78,6 +78,7 @@ pub fn r_process_message_1(
g_x,
h_message_1,
},
c_i,
ead_1,
))
} else {
Expand Down
16 changes: 12 additions & 4 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,15 @@ impl<'a, Crypto: CryptoTrait> EdhocResponder<'a, Crypto> {
pub fn process_message_1(
mut self,
message_1: &BufferMessage1,
) -> Result<(EdhocResponderProcessedM1<'a, Crypto>, Option<EADItem>), EDHOCError> {
let (state, ead_1) = r_process_message_1(&self.state, &mut self.crypto, message_1)?;
) -> Result<
(
EdhocResponderProcessedM1<'a, Crypto>,
ConnId,
Option<EADItem>,
),
EDHOCError,
> {
let (state, c_i, ead_1) = r_process_message_1(&self.state, &mut self.crypto, message_1)?;

Ok((
EdhocResponderProcessedM1 {
Expand All @@ -117,6 +124,7 @@ impl<'a, Crypto: CryptoTrait> EdhocResponder<'a, Crypto> {
cred_r: self.cred_r,
crypto: self.crypto,
},
c_i,
ead_1,
))
}
Expand Down Expand Up @@ -567,7 +575,7 @@ mod test {
// ---- end initiator handling

// ---- begin responder handling
let (responder, _ead_1) = responder.process_message_1(&message_1).unwrap();
let (responder, _c_i, _ead_1) = responder.process_message_1(&message_1).unwrap();
// if ead_1: process ead_1
// if needed: prepare ead_2
let (responder, message_2) = responder
Expand Down Expand Up @@ -681,7 +689,7 @@ mod test_authz {
let (initiator, message_1) = initiator.prepare_message_1(None, &Some(ead_1)).unwrap();
device.set_h_message_1(initiator.state.h_message_1.clone());

let (responder, ead_1) = responder.process_message_1(&message_1).unwrap();
let (responder, _c_i, ead_1) = responder.process_message_1(&message_1).unwrap();
let ead_2 = if let Some(ead_1) = ead_1 {
let (authenticator, _loc_w, voucher_request) =
authenticator.process_ead_1(&ead_1, &message_1).unwrap();
Expand Down

0 comments on commit c0d74ac

Please sign in to comment.