-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
128: MbedTLS Reference counted instead of lifetimes r=jethrogb a=AdrianCX Moving from referene counting allows simpler move to native-tls / hyper. Arc Changes: - Each Config/Context/... will hold Arcs towards items it holds pointers to. - This forces objects to live as long as needed, once no longer used they get destroyed by reference counting. This allows passing the objects to multiple threads without worrying about lifetime. I've also added notes why classes are Sync where used. Let me know if I missed any classes. Usage example of an intermediate mbed-hyper integration is at: - https://github.com/fortanix/rust-mbedtls/tree/acruceru/wip-mbed-hyper-v2/mbedtls-hyper/examples/integrations There I added a crate to wrap hyper - similar to native-tls. (that will be moved to native-tls layer soon) That crate can be considered an integration test that I will raise a separate PR for. Edit: Changes after initial review: - Added forward_mbedtls_calloc / forward_mbedtls_free functions so we can pass certificates to and from mbedtls without allocator mismatches/corruptions. - Switched to MbedtlsList<Certificate> and Certificate. A MbedtlsBox is pending for this PR as well. - Fixed most comments. Still pending: - Update define! macros - Add MbedtlsBox<Certificate> Fixes #1 Partial progress on #3 Fixes #4 Fixes #8 Partially addresses #9 Co-authored-by: Adrian Cruceru <[email protected]> Co-authored-by: Jethro Beekman <[email protected]>
- Loading branch information
Showing
37 changed files
with
2,770 additions
and
1,000 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
status = [ | ||
"continuous-integration/travis-ci/push", | ||
] | ||
timeout_sec = 36000 # ten hours |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* Copyright (c) Fortanix, Inc. | ||
* | ||
* Licensed under the GNU General Public License, version 2 <LICENSE-GPL or | ||
* https://www.gnu.org/licenses/gpl-2.0.html> or the Apache License, Version | ||
* 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>, at your | ||
* option. This file may not be copied, modified, or distributed except | ||
* according to those terms. */ | ||
|
||
use core::fmt; | ||
use core::ops::{Deref, DerefMut}; | ||
use core::ptr::NonNull; | ||
use core::ptr::drop_in_place; | ||
use core::mem::ManuallyDrop; | ||
|
||
use mbedtls_sys::types::raw_types::c_void; | ||
|
||
extern "C" { | ||
pub(crate) fn forward_mbedtls_free(n: *mut mbedtls_sys::types::raw_types::c_void); | ||
} | ||
|
||
#[repr(transparent)] | ||
pub struct Box<T> { | ||
pub(crate) inner: NonNull<T> | ||
} | ||
|
||
impl<T> Box<T> { | ||
pub(crate) fn into_raw(self) -> *mut T { | ||
let v = ManuallyDrop::new(self); | ||
v.inner.as_ptr() | ||
} | ||
} | ||
|
||
impl<T> Deref for Box<T> { | ||
type Target = T; | ||
fn deref(&self) -> &T { | ||
unsafe { self.inner.as_ref() } | ||
} | ||
} | ||
|
||
impl<T> DerefMut for Box<T> { | ||
fn deref_mut(&mut self) -> &mut T { | ||
unsafe { self.inner.as_mut() } | ||
} | ||
} | ||
|
||
impl<T: fmt::Debug> fmt::Debug for Box<T> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
fmt::Debug::fmt(&**self, f) | ||
} | ||
} | ||
|
||
impl<T> Drop for Box<T> { | ||
fn drop(&mut self) { | ||
unsafe { | ||
drop_in_place(self.inner.as_ptr()); | ||
forward_mbedtls_free(self.inner.as_ptr() as *mut c_void) | ||
} | ||
} | ||
} | ||
|
||
#[repr(transparent)] | ||
pub struct List<T> { | ||
pub(crate) inner: Option<Box<T>> | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* Copyright (c) Fortanix, Inc. | ||
* | ||
* Licensed under the GNU General Public License, version 2 <LICENSE-GPL or | ||
* https://www.gnu.org/licenses/gpl-2.0.html> or the Apache License, Version | ||
* 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>, at your | ||
* option. This file may not be copied, modified, or distributed except | ||
* according to those terms. */ | ||
|
||
// Follow same pattern for config and alloc/free as everywhere in mbedtls | ||
#if !defined(MBEDTLS_CONFIG_FILE) | ||
#include "mbedtls/config.h" | ||
#else | ||
#include MBEDTLS_CONFIG_FILE | ||
#endif | ||
|
||
#if defined(MBEDTLS_PLATFORM_C) | ||
#include "mbedtls/platform.h" | ||
#else | ||
#include <stdlib.h> | ||
#define mbedtls_calloc calloc | ||
#define mbedtls_free free | ||
#endif | ||
|
||
extern void *forward_mbedtls_calloc( size_t n, size_t size ) { | ||
return mbedtls_calloc(n, size); | ||
} | ||
|
||
extern void forward_mbedtls_free( void *ptr ) { | ||
mbedtls_free(ptr); | ||
} | ||
|
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
Oops, something went wrong.