Skip to content

Commit d2fded6

Browse files
authored
Try #226:
2 parents 7f1d694 + 8197e3c commit d2fded6

20 files changed

+1636
-69
lines changed

Cargo.lock

Lines changed: 512 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
[workspace]
22
members = ["mbedtls", "mbedtls-sys"]
33
resolver = "2"
4+
5+
[patch.crates-io]
6+
mio = { git = "https://github.com/mzohreva/mio", branch = "mz/sgx-port-0.7.6" }
7+
tokio = { git = "https://github.com/mzohreva/tokio", branch = "mz/sgx-port-0.3.4" }
8+

ct.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ if [ "$TRAVIS_RUST_VERSION" == "stable" ] || [ "$TRAVIS_RUST_VERSION" == "beta"
3333
cargo test --features pkcs12 --target $TARGET
3434
cargo test --features pkcs12_rc2 --target $TARGET
3535
cargo test --features dsa --target $TARGET
36+
cargo test --test hyper13 --features=std,async-rt --target $TARGET
37+
cargo test --test async_session --features=async-rt --target $TARGET
3638

3739
# If zlib is installed, test the zlib feature
3840
if [ -n "$ZLIB_INSTALLED" ]; then

mbedtls-sys/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ quote = "1.0.9"
4242
# * strstr/strlen/strncpy/strncmp/strcmp/snprintf
4343
# * memmove/memcpy/memcmp/memset
4444
# * rand/printf (used only for self tests. optionally use custom_printf)
45-
default = ["std", "debug", "threading", "zlib", "time", "aesni", "padlock", "legacy_protocols"]
46-
std = ["debug"] # deprecated automatic enabling of debug, can be removed on major version bump
47-
debug = []
45+
default = ["std", "threading", "zlib", "time", "aesni", "padlock", "legacy_protocols"]
46+
std = [] # deprecated automatic enabling of debug, can be removed on major version bump
4847
custom_printf = []
4948
custom_has_support = []
5049
aes_alt = []

mbedtls/Cargo.toml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ bit-vec = { version = "0.5", optional = true }
2929
block-modes = { version = "0.3", optional = true }
3030
rc2 = { version = "0.3", optional = true }
3131
cfg-if = "1.0.0"
32+
tokio = { version = "0.3.4", optional = true }
3233

3334
[target.x86_64-fortanix-unknown-sgx.dependencies]
3435
rs-libc = "0.2.0"
3536
chrono = "0.4"
3637

3738
[dependencies.mbedtls-sys-auto]
38-
version = "2.25.0"
39+
version = "2.28.0"
3940
default-features = false
4041
features = ["custom_printf", "trusted_cert_callback", "threading"]
4142
path = "../mbedtls-sys"
@@ -47,6 +48,11 @@ serde_cbor = "0.6"
4748
hex = "0.3"
4849
matches = "0.1.8"
4950
hyper = { version = "0.10.16", default-features = false }
51+
hyper13 = { package = "hyper", version = "0.13", default-features = false, features = ["stream"] }
52+
tokio-02 = { package = "tokio", version = "0.2", default-features = false }
53+
async-stream = "0.3.0"
54+
futures = "0.3"
55+
tracing = "0.1"
5056

5157
[build-dependencies]
5258
cc = "1.0"
@@ -55,7 +61,7 @@ cc = "1.0"
5561
# Features are documented in the README
5662
default = ["std", "aesni", "time", "padlock"]
5763
std = ["byteorder/std", "mbedtls-sys-auto/std", "serde/std", "yasna"]
58-
debug = ["mbedtls-sys-auto/debug"]
64+
debug = []
5965
no_std_deps = ["spin", "serde/alloc"]
6066
force_aesni_support = ["mbedtls-sys-auto/custom_has_support", "mbedtls-sys-auto/aes_alt", "aesni"]
6167
mpi_force_c_code = ["mbedtls-sys-auto/mpi_force_c_code"]
@@ -68,6 +74,9 @@ dsa = ["std", "yasna", "num-bigint", "bit-vec"]
6874
pkcs12 = ["std", "yasna"]
6975
pkcs12_rc2 = ["pkcs12", "rc2", "block-modes"]
7076
legacy_protocols = ["mbedtls-sys-auto/legacy_protocols"]
77+
async = ["std", "tokio","tokio/net","tokio/io-util", "tokio/macros"]
78+
async-rt = ["async", "tokio/rt", "tokio/sync", "tokio/rt-multi-thread"]
79+
migration_mode=[]
7180

7281
[[example]]
7382
name = "client"
@@ -100,3 +109,12 @@ required-features = ["std"]
100109
[[test]]
101110
name = "hyper"
102111
required-features = ["std"]
112+
113+
[[test]]
114+
name = "hyper13"
115+
required-features = ["std", "async-rt"]
116+
117+
[[test]]
118+
name = "async_session"
119+
path = "tests/async_session.rs"
120+
required-features = ["async-rt"]

mbedtls/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ mod private;
5353

5454
// needs to be pub for global visiblity
5555
#[doc(hidden)]
56-
#[cfg(sys_threading_component = "custom")]
56+
57+
#[cfg(all(sys_threading_component = "custom", not(feature = "migration_mode")))]
5758
pub mod threading;
5859

60+
#[cfg(not(feature = "migration_mode"))]
5961
cfg_if::cfg_if! {
6062
if #[cfg(any(feature = "force_aesni_support", target_env = "sgx"))] {
6163
// needs to be pub for global visiblity
@@ -105,6 +107,7 @@ mod alloc_prelude {
105107
pub(crate) use rust_alloc::borrow::Cow;
106108
}
107109

110+
#[cfg(not(feature = "migration_mode"))]
108111
cfg_if::cfg_if! {
109112
if #[cfg(sys_time_component = "custom")] {
110113
use mbedtls_sys::types::{time_t, tm};
@@ -154,7 +157,7 @@ cfg_if::cfg_if! {
154157
///
155158
/// The caller must ensure no other MbedTLS code is running when calling this
156159
/// function.
157-
#[cfg(feature = "debug")]
160+
#[cfg(all(feature = "debug", not(feature = "migration_mode")))]
158161
pub unsafe fn set_global_debug_threshold(threshold: i32) {
159162
mbedtls_sys::debug_set_threshold(threshold);
160163
}

mbedtls/src/pk/dsa/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,13 @@ fn sample_secret_value<F: Random>(upper_bound: &Mpi, rng: &mut F) -> Result<Mpi>
217217
Ok(c)
218218
}
219219

220-
fn encode_dsa_signature(r: &Mpi, s: &Mpi) -> Result<Vec<u8>> {
221-
let r = BigUint::from_bytes_be(&r.to_binary()?);
222-
let s = BigUint::from_bytes_be(&s.to_binary()?);
220+
pub fn encode_dsa_signature(r: &Mpi, s: &Mpi) -> Result<Vec<u8>> {
221+
serialize_signature(&r.to_binary()?, &s.to_binary()?)
222+
}
223+
224+
pub fn serialize_signature(r: &[u8], s: &[u8]) -> Result<Vec<u8>> {
225+
let r = BigUint::from_bytes_be(r);
226+
let s = BigUint::from_bytes_be(s);
223227

224228
Ok(yasna::construct_der(|w| {
225229
w.write_sequence(|w| {
@@ -229,6 +233,18 @@ fn encode_dsa_signature(r: &Mpi, s: &Mpi) -> Result<Vec<u8>> {
229233
}))
230234
}
231235

236+
pub fn deserialize_signature(signature: &Vec<u8>) -> Result<(Vec<u8>, Vec<u8>)> {
237+
let (r,s) = yasna::parse_der(signature, |r| {
238+
r.read_sequence(|rdr| {
239+
let r = rdr.next().read_biguint()?;
240+
let s = rdr.next().read_biguint()?;
241+
Ok((r,s))
242+
})
243+
}).map_err(|_| Error::X509InvalidSignature)?;
244+
245+
Ok((r.to_bytes_be(), s.to_bytes_be()))
246+
}
247+
232248
impl DsaPrivateKey {
233249
pub fn from_components(params: DsaParams, x: Mpi) -> Result<Self> {
234250
if x <= Mpi::new(1)? || x >= params.q {

mbedtls/src/pk/mod.rs

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -201,34 +201,7 @@ define!(
201201
//
202202
// - Only used when creating/freeing - which is safe by design - eckey_alloc_wrap / eckey_free_wrap
203203
//
204-
// 3. ECDSA: mbedtls_ecdsa_info at ../../../mbedtls-sys/vendor/crypto/library/pk_wrap.c:729
205-
// This does not use internal locks but avoids interior mutability.
206-
//
207-
// - Const access / copies context to stack based variables:
208-
// ecdsa_verify_wrap: ../../../mbedtls-sys/vendor/crypto/library/pk_wrap.c:544
209-
// This copies the public key on the stack - in buf[] and copies the group id and nbits.
210-
// That is done via: mbedtls_pk_write_pubkey( &p, buf, &key ) where key.pk_ctx = ctx;
211-
// And the key is a const parameter to mbedtls_pk_write_pubkey - ../../../mbedtls-sys/vendor/crypto/library/pkwrite.c:158
212-
//
213-
// - Const access with additional notes due to call stacks involved.
214-
//
215-
// ecdsa_sign_wrap: ../../../mbedtls-sys/vendor/crypto/library/pk_wrap.c:657
216-
// mbedtls_ecdsa_write_signature ../../../mbedtls-sys/vendor/crypto/library/ecdsa.c:688
217-
// mbedtls_ecdsa_write_signature_restartable ../../../mbedtls-sys/vendor/crypto/library/ecdsa.c:640
218-
// MBEDTLS_ECDSA_DETERMINISTIC is not defined.
219-
// MBEDTLS_ECDSA_SIGN_ALT is not defined.
220-
// Passes grp to: ecdsa_sign_restartable: ../../../mbedtls-sys/vendor/crypto/library/ecdsa.c:253
221-
// Const access to group - reads parameters, passed as const to mbedtls_ecp_gen_privkey,
222-
// mbedtls_ecp_mul_restartable: ../../../mbedtls-sys/vendor/crypto/library/ecp.c:2351
223-
// MBEDTLS_ECP_INTERNAL_ALT is not defined. (otherwise it might not be safe depending on ecp_init/ecp_free) ../../../mbedtls-sys/build/config.rs:131
224-
// Passes as const to: mbedtls_ecp_check_privkey / mbedtls_ecp_check_pubkey / mbedtls_ecp_get_type( grp
225-
//
226-
// - Ignored due to not defined: ecdsa_verify_rs_wrap, ecdsa_sign_rs_wrap, ecdsa_rs_alloc, ecdsa_rs_free
227-
// (Undefined - MBEDTLS_ECP_RESTARTABLE - ../../../mbedtls-sys/build/config.rs:173)
228-
//
229-
// - Only const access to context: eckey_check_pair
230-
//
231-
// - Only used when creating/freeing - which is safe by design: ecdsa_alloc_wrap, ecdsa_free_wrap
204+
// 3. ECDSA - code uses mbedtls_pk wrappers. In this case code goes through ECKEY logic above. (mbedtls_pk_parse_key intentionally never calls mbedtls_pk_info_from_type with MBEDTLS_PK_ECDSA)
232205
//
233206
unsafe impl Sync for Pk {}
234207

@@ -826,7 +799,7 @@ impl Pk {
826799
///
827800
/// On success, returns the actual number of bytes written to `sig`.
828801
pub fn sign<F: Random>(
829-
&mut self,
802+
&self,
830803
md: MdType,
831804
hash: &[u8],
832805
sig: &mut [u8],
@@ -853,7 +826,7 @@ impl Pk {
853826
let mut ret = 0usize;
854827
unsafe {
855828
pk_sign(
856-
&mut self.inner,
829+
&self.inner as *const _ as *mut _,
857830
md.into(),
858831
hash.as_ptr(),
859832
hash.len(),
@@ -922,15 +895,14 @@ impl Pk {
922895
}
923896
}
924897

925-
pub fn verify(&mut self, md: MdType, hash: &[u8], sig: &[u8]) -> Result<()> {
926-
// If hash or sig are allowed with size 0 (&[]) then mbedtls will attempt to auto-detect size and cause an invalid write.
898+
pub fn verify(&self, md: MdType, hash: &[u8], sig: &[u8]) -> Result<()> {
927899
if hash.len() == 0 || sig.len() == 0 {
928900
return Err(Error::PkBadInputData)
929901
}
930902

931903
unsafe {
932904
pk_verify(
933-
&mut self.inner,
905+
&self.inner as *const _ as *mut _,
934906
md.into(),
935907
hash.as_ptr(),
936908
hash.len(),

mbedtls/src/rust_printf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <stdio.h>
1010
#include <stdarg.h>
1111

12-
extern void mbedtls_log(const char* msg);
12+
extern void mbedtls8_log(const char* msg);
1313

1414
extern int mbedtls_printf(const char *fmt, ...) {
1515
va_list ap;
@@ -31,7 +31,7 @@ extern int mbedtls_printf(const char *fmt, ...) {
3131
if (n<0)
3232
return -1;
3333

34-
mbedtls_log(p);
34+
mbedtls8_log(p);
3535

3636
return n;
3737
}

mbedtls/src/self_test.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ cfg_if::cfg_if! {
2525
// needs to be pub for global visiblity
2626
#[doc(hidden)]
2727
#[no_mangle]
28-
pub unsafe extern "C" fn mbedtls_log(msg: *const std::os::raw::c_char) {
28+
pub unsafe extern "C" fn mbedtls8_log(msg: *const std::os::raw::c_char) {
2929
print!("{}", std::ffi::CStr::from_ptr(msg).to_string_lossy());
3030
}
3131
} else {
@@ -35,11 +35,13 @@ cfg_if::cfg_if! {
3535
// needs to be pub for global visiblity
3636
#[doc(hidden)]
3737
#[no_mangle]
38-
pub unsafe extern "C" fn mbedtls_log(msg: *const c_char) {
38+
pub unsafe extern "C" fn mbedtls8_log(msg: *const c_char) {
3939
log_f.expect("Called self-test log without enabling self-test")(msg)
4040
}
4141
}
4242
}
43+
44+
#[cfg(not(feature = "migration_mode"))]
4345
cfg_if::cfg_if! {
4446
if #[cfg(any(not(feature = "std"), target_env = "sgx"))] {
4547
#[allow(non_upper_case_globals)]
@@ -66,6 +68,7 @@ cfg_if::cfg_if! {
6668
/// The caller needs to ensure this function is not called while any other
6769
/// function in this module is called.
6870
#[allow(unused)]
71+
#[cfg(not(feature = "migration_mode"))]
6972
pub unsafe fn enable(rand: fn() -> c_int, log: Option<unsafe fn(*const c_char)>) {
7073
#[cfg(any(not(feature = "std"), target_env = "sgx"))] {
7174
rand_f = Some(rand);
@@ -79,6 +82,7 @@ pub unsafe fn enable(rand: fn() -> c_int, log: Option<unsafe fn(*const c_char)>)
7982
///
8083
/// The caller needs to ensure this function is not called while any other
8184
/// function in this module is called.
85+
#[cfg(not(feature = "migration_mode"))]
8286
pub unsafe fn disable() {
8387
#[cfg(any(not(feature = "std"), target_env = "sgx"))] {
8488
rand_f = None;

0 commit comments

Comments
 (0)