Skip to content

Commit

Permalink
Merge #255
Browse files Browse the repository at this point in the history
255: Prepare for publishing `0.8.2` r=mzohreva a=Taowyoo

Back-port several PR's to fix problems that obstruct the publish

- #180 
- #206 
- #214 only commit [441d571](441d571)
- #229 


Co-authored-by: bors[bot] <26634292+bors[bot]@users.noreply.github.com>
Co-authored-by: Tobias Naumann <[email protected]>
  • Loading branch information
bors[bot] and DrTobe authored Apr 24, 2023
2 parents aa500d0 + 35c78e8 commit fa98271
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 208 deletions.
90 changes: 49 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions mbedtls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ byteorder = "1.0.0"
yasna = { version = "0.2", optional = true, features = ["num-bigint", "bit-vec"] }
num-bigint = { version = "0.2", optional = true }
bit-vec = { version = "0.5", optional = true }
block-modes = { version = "0.3", optional = true }
rc2 = { version = "0.3", optional = true }
cbc = { version = "0.1.2", optional = true }
rc2 = { version = "0.8.1", optional = true }
cfg-if = "1.0.0"

[target.x86_64-fortanix-unknown-sgx.dependencies]
Expand Down Expand Up @@ -67,7 +67,7 @@ time = ["mbedtls-sys-auto/time"]
padlock = ["mbedtls-sys-auto/padlock"]
dsa = ["std", "yasna", "num-bigint", "bit-vec"]
pkcs12 = ["std", "yasna"]
pkcs12_rc2 = ["pkcs12", "rc2", "block-modes"]
pkcs12_rc2 = ["pkcs12", "rc2", "cbc"]

[[example]]
name = "client"
Expand Down
4 changes: 3 additions & 1 deletion mbedtls/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ impl<T> Drop for Box<T> {
}
}

unsafe impl<T: Send> Send for Box<T> {}
unsafe impl<T: Sync> Sync for Box<T> {}

#[repr(transparent)]
pub struct List<T> {
pub(crate) inner: Option<Box<T>>
}

66 changes: 66 additions & 0 deletions mbedtls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,69 @@ cfg_if::cfg_if! {
pub unsafe fn set_global_debug_threshold(threshold: i32) {
mbedtls_sys::debug_set_threshold(threshold);
}

#[cfg(test)]
mod tests {
#[allow(dead_code)]
/// Utilities for testing whether types implement certain traits.
///
/// For each trait `Trait` that you want to be able to test, you should
/// implement:
/// ```ignore
/// impl<T: “Trait”> Testable<dyn “Trait”> for T {}
/// ```
///
/// Then, to test whether a type `Type` implements `Trait`, call:
/// ```ignore
/// TestTrait::<dyn “Trait”, “Type”>::new().impls_trait()
/// ```
/// This returns a `bool` indicating whether the trait is implemented.
// This relies on auto-deref to distinguish between types that do and don't
// implement the trait.
mod testtrait {
use core::marker::PhantomData;

pub struct NonImplTrait<T> {
inner: PhantomData<T>
}

pub struct TestTrait<TraitObj: ?Sized, Type> {
non_impl: NonImplTrait<Type>,
phantom: PhantomData<*const TraitObj>,
}

pub trait Testable<T: ?Sized> {}

impl<TraitObj: ?Sized, Type> TestTrait<TraitObj, Type> {
pub fn new() -> Self {
TestTrait { non_impl: NonImplTrait { inner: PhantomData }, phantom: PhantomData }
}
}

impl<TraitObj: ?Sized, Type: Testable<TraitObj>> TestTrait<TraitObj, Type> {
pub fn impls_trait(&self) -> bool {
true
}
}

impl<T> NonImplTrait<T> {
pub fn impls_trait(&self) -> bool {
false
}
}

impl<TraitObj: ?Sized, Type> core::ops::Deref for TestTrait<TraitObj, Type> {
type Target = NonImplTrait<Type>;

fn deref(&self) -> &NonImplTrait<Type> {
&self.non_impl
}
}
}

pub use testtrait::{TestTrait, Testable};

impl<T: Send> Testable<dyn Send> for T {}
impl<T: Sync> Testable<dyn Sync> for T {}
impl<T: Send + Sync> Testable<dyn Send + Sync> for T {}
}
2 changes: 1 addition & 1 deletion mbedtls/src/pk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ extern "C" fn alloc_custom_pk_ctx() -> *mut c_void {
}

unsafe extern "C" fn free_custom_pk_ctx(p: *mut c_void) {
Box::from_raw(p as *mut CustomPkContext);
let _ = Box::from_raw(p as *mut CustomPkContext);
}

extern "C" fn custom_pk_can_do(_t: u32) -> i32 {
Expand Down
8 changes: 4 additions & 4 deletions mbedtls/src/pkcs12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,17 +601,17 @@ fn decrypt_3des(ciphertext: &[u8], key: &[u8], iv: &[u8]) -> Pkcs12Result<Vec<u8

#[cfg(feature = "pkcs12_rc2")]
fn decrypt_rc2(ciphertext: &[u8], key: &[u8], iv: &[u8]) -> Pkcs12Result<Vec<u8>> {
use block_modes::BlockMode;
use rc2::cipher::{block_padding::Pkcs7, BlockDecryptMut, KeyIvInit};

let cipher =
block_modes::Cbc::<rc2::Rc2, block_modes::block_padding::Pkcs7>::new_var(&key, &iv)
cbc::Decryptor::<rc2::Rc2>::new_from_slices(key, iv)
.map_err(|e| Pkcs12Error::Custom(format!("{:?}", e)))?;

let mut pt = ciphertext.to_vec();
let pt = cipher
.decrypt(&mut pt)
.decrypt_padded_mut::<Pkcs7>(&mut pt)
.map_err(|e| Pkcs12Error::Custom(format!("{:?}", e)))?;
return Ok(pt.to_owned());
Ok(pt.to_owned())
}

#[cfg(not(feature = "pkcs12_rc2"))]
Expand Down
60 changes: 1 addition & 59 deletions mbedtls/src/wrapper_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,69 +301,11 @@ macro_rules! getter {

#[cfg(test)]
mod tests {
#[allow(dead_code)]
/// Utilities for testing whether types implement certain traits.
///
/// For each trait `Trait` that you want to be able to test, you should
/// implement:
/// ```ignore
/// impl<T: “Trait”> Testable<dyn “Trait”> for T {}
/// ```
///
/// Then, to test whether a type `Type` implements `Trait`, call:
/// ```ignore
/// TestTrait::<dyn “Trait”, “Type”>::new().impls_trait()
/// ```
/// This returns a `bool` indicating whether the trait is implemented.
// This relies on auto-deref to distinguish between types that do and don't
// implement the trait.
mod testtrait {
use core::marker::PhantomData;

pub struct NonImplTrait<T> {
inner: PhantomData<T>
}

pub struct TestTrait<TraitObj: ?Sized, Type> {
non_impl: NonImplTrait<Type>,
phantom: PhantomData<*const TraitObj>,
}

pub trait Testable<T: ?Sized> {}

impl<TraitObj: ?Sized, Type> TestTrait<TraitObj, Type> {
pub fn new() -> Self {
TestTrait { non_impl: NonImplTrait { inner: PhantomData }, phantom: PhantomData }
}
}

impl<TraitObj: ?Sized, Type: Testable<TraitObj>> TestTrait<TraitObj, Type> {
pub fn impls_trait(&self) -> bool {
true
}
}

impl<T> NonImplTrait<T> {
pub fn impls_trait(&self) -> bool {
false
}
}

impl<TraitObj: ?Sized, Type> core::ops::Deref for TestTrait<TraitObj, Type> {
type Target = NonImplTrait<Type>;

fn deref(&self) -> &NonImplTrait<Type> {
&self.non_impl
}
}
}

use testtrait::{TestTrait, Testable};
use crate::tests::{TestTrait, Testable};

callback!(RustTest: Fn() -> ());
callback!(NativeTestMut,NativeTest() -> ());

impl<T: Send + Sync> Testable<dyn Send + Sync> for T {}
impl<T: RustTest> Testable<dyn RustTest> for T {}
impl<T: NativeTest> Testable<dyn NativeTest> for T {}
impl<T: NativeTestMut> Testable<dyn NativeTestMut> for T {}
Expand Down
Loading

0 comments on commit fa98271

Please sign in to comment.