-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Forward transport errors from Secure Session callbacks #375
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ab6cbda
Add TransportError to themis::Error
ilammy 96429a0
Forward transport errors from Secure Session callbacks
ilammy 9a2b889
Give a name to maximum error value constant
ilammy d1a6720
Leave a note about PartialEq implementation
ilammy 5118683
Forward errors during channel negotiation
ilammy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ pub struct SecureSession { | |
struct SecureSessionContext { | ||
callbacks: secure_session_user_callbacks_t, | ||
transport: Box<dyn SecureSessionTransport>, | ||
last_error: Option<TransportError>, | ||
} | ||
|
||
/// Transport delegate for Secure Session. | ||
|
@@ -265,6 +266,7 @@ impl SecureSession { | |
user_data: std::ptr::null_mut(), | ||
}, | ||
transport: Box::new(transport), | ||
last_error: None, | ||
}); | ||
context.callbacks.user_data = context_as_user_data(&context); | ||
|
||
|
@@ -567,6 +569,7 @@ impl SecureSession { | |
// Furthermore, Themis expects the send callback to send the whole message so it is kinda | ||
// pointless to return the amount of bytes send. The receive callback returns accurate number | ||
// of bytes, but I do not really like the Rust interface this implies. It could be made better. | ||
const THEMIX_MAX_ERROR: isize = 21; | ||
|
||
/// Sends a message to the remote peer. | ||
/// | ||
|
@@ -582,7 +585,14 @@ impl SecureSession { | |
unsafe { | ||
let length = | ||
secure_session_send(self.session, message_ptr as *const c_void, message_len); | ||
if length <= 21 { | ||
if length == TRANSPORT_FAILURE { | ||
let error = self.context.last_error.take().expect("missing error"); | ||
return Err(Error::from_transport_error(error)); | ||
} | ||
if length == TRANSPORT_OVERFLOW { | ||
return Err(Error::with_kind(ErrorKind::BufferTooSmall)); | ||
} | ||
if length <= Self::THEMIX_MAX_ERROR { | ||
return Err(Error::from_session_status(length as themis_status_t)); | ||
} | ||
} | ||
|
@@ -610,7 +620,14 @@ impl SecureSession { | |
message.as_mut_ptr() as *mut c_void, | ||
message.capacity(), | ||
); | ||
if length <= 21 { | ||
if length == TRANSPORT_FAILURE { | ||
let error = self.context.last_error.take().expect("missing error"); | ||
return Err(Error::from_transport_error(error)); | ||
} | ||
if length == TRANSPORT_OVERFLOW { | ||
return Err(Error::with_kind(ErrorKind::BufferTooSmall)); | ||
} | ||
if length <= Self::THEMIX_MAX_ERROR { | ||
return Err(Error::from_session_status(length as themis_status_t)); | ||
} | ||
debug_assert!(length as usize <= message.capacity()); | ||
|
@@ -636,6 +653,13 @@ impl SecureSession { | |
pub fn negotiate_transport(&mut self) -> Result<()> { | ||
unsafe { | ||
let result = secure_session_receive(self.session, ptr::null_mut(), 0); | ||
if result == TRANSPORT_FAILURE { | ||
let error = self.context.last_error.take().expect("missing error"); | ||
return Err(Error::from_transport_error(error)); | ||
} | ||
if result == TRANSPORT_OVERFLOW { | ||
return Err(Error::with_kind(ErrorKind::BufferTooSmall)); | ||
} | ||
let error = Error::from_session_status(result as themis_status_t); | ||
if error.kind() != ErrorKind::Success { | ||
return Err(error); | ||
|
@@ -659,19 +683,24 @@ unsafe fn user_data_as_context<'a>(ptr: *mut c_void) -> &'a mut SecureSessionCon | |
&mut *(ptr as *mut SecureSessionContext) | ||
} | ||
|
||
const TRANSPORT_FAILURE: isize = -1; | ||
const TRANSPORT_OVERFLOW: isize = -2; | ||
|
||
unsafe extern "C" fn send_data( | ||
data_ptr: *const u8, | ||
data_len: usize, | ||
user_data: *mut c_void, | ||
) -> isize { | ||
let data = byte_slice_from_ptr(data_ptr, data_len); | ||
let transport = &mut user_data_as_context(user_data).transport; | ||
let context = user_data_as_context(user_data); | ||
|
||
transport | ||
.send_data(data) | ||
.ok() | ||
.and_then(as_isize) | ||
.unwrap_or(-1) | ||
match context.transport.send_data(data) { | ||
Ok(sent_bytes) => as_isize(sent_bytes).unwrap_or(TRANSPORT_OVERFLOW), | ||
Err(error) => { | ||
context.last_error = Some(error); | ||
TRANSPORT_FAILURE | ||
} | ||
} | ||
} | ||
|
||
unsafe extern "C" fn receive_data( | ||
|
@@ -680,13 +709,15 @@ unsafe extern "C" fn receive_data( | |
user_data: *mut c_void, | ||
) -> isize { | ||
let data = byte_slice_from_ptr_mut(data_ptr, data_len); | ||
let transport = &mut user_data_as_context(user_data).transport; | ||
let context = user_data_as_context(user_data); | ||
|
||
transport | ||
.receive_data(data) | ||
.ok() | ||
.and_then(as_isize) | ||
.unwrap_or(-1) | ||
match context.transport.receive_data(data) { | ||
Ok(received_bytes) => as_isize(received_bytes).unwrap_or(TRANSPORT_OVERFLOW), | ||
Err(error) => { | ||
context.last_error = Some(error); | ||
TRANSPORT_FAILURE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks better with constants usage |
||
} | ||
} | ||
} | ||
|
||
unsafe extern "C" fn state_changed(event: c_int, user_data: *mut c_void) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will be good to add a comment to ErrorKind definition that we should update
error_kinds_equal
if some new values will be addedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah... That's unfortunate but the catch-all branch in this match makes it possible to add a new variant to
ErrorKind
without adding a corresponding line to the comparison implementation.I guess there's no other way around, I'll add a comment and let's hope it will be noticed.