Skip to content

Commit ce358f4

Browse files
bors[bot]Max Kmaxknv
authored
Merge #271
271: support for combined errors from mbedtls r=jethrogb a=mkaynov Error enum updated with variants: HighLevel(HiError), - for mbedtls error including HighLevel code only LowLevel(LoError), - for mbedtls error including LowLevel code only HighAndLowLevel(HiError, LoError) - for mbedtls combined error This closes #7 Co-authored-by: Max K <[email protected]> Co-authored-by: Max K <[email protected]>
2 parents a3aee0d + 8cbd54b commit ce358f4

27 files changed

+527
-356
lines changed

ct.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export CC_x86_64_fortanix_unknown_sgx=clang-11
2020
export CC_aarch64_unknown_linux_musl=/tmp/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc
2121
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=/tmp/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc
2222
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUNNER=qemu-aarch64
23+
# to be removed after migration to rustc 1.70.., issue #277
24+
export RUSTFLAGS="-A ambiguous_glob_reexports"
2325

2426
if [ "$TRAVIS_RUST_VERSION" == "stable" ] || [ "$TRAVIS_RUST_VERSION" == "beta" ] || [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then
2527
# Install the rust toolchain

mbedtls/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mbedtls"
3-
version = "0.9.1"
3+
version = "0.10.0"
44
authors = ["Jethro Beekman <[email protected]>"]
55
build = "build.rs"
66
edition = "2018"
@@ -75,7 +75,7 @@ dsa = ["std", "yasna", "num-bigint", "bit-vec"]
7575
pkcs12 = ["std", "yasna"]
7676
pkcs12_rc2 = ["pkcs12", "rc2", "cbc"]
7777
legacy_protocols = ["mbedtls-sys-auto/legacy_protocols"]
78-
async = ["std", "tokio","tokio/net","tokio/io-util", "tokio/macros"]
78+
async = ["std", "tokio", "tokio/net", "tokio/io-util", "tokio/macros"]
7979
async-rt = ["async", "tokio/rt", "tokio/sync", "tokio/rt-multi-thread"]
8080

8181
[[example]]

mbedtls/src/bignum/mod.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
* option. This file may not be copied, modified, or distributed except
77
* according to those terms. */
88

9-
use crate::error::{Error, IntoResult, Result};
9+
10+
#[cfg(feature = "std")]
11+
use crate::error::Error;
12+
use crate::error::{IntoResult, Result, codes};
1013
use mbedtls_sys::*;
1114

1215
#[cfg(not(feature = "std"))]
@@ -142,7 +145,7 @@ impl Mpi {
142145
pub fn as_u32(&self) -> Result<u32> {
143146
if self.bit_length()? > 32 {
144147
// Not exactly correct but close enough
145-
return Err(Error::MpiBufferTooSmall);
148+
return Err(codes::MpiBufferTooSmall.into());
146149
}
147150

148151
Ok(self.get_limb(0) as u32)
@@ -165,7 +168,7 @@ impl Mpi {
165168
unsafe { mpi_write_string(&self.inner, radix, ::core::ptr::null_mut(), 0, &mut olen) };
166169

167170
if r != ERR_MPI_BUFFER_TOO_SMALL {
168-
return Err(Error::from_mbedtls_code(r));
171+
return Err(r.into());
169172
}
170173

171174
let mut buf = vec![0u8; olen];
@@ -258,20 +261,20 @@ impl Mpi {
258261
let zero = Mpi::new(0)?;
259262

260263
if self < &zero || self >= p {
261-
return Err(Error::MpiBadInputData);
264+
return Err(codes::MpiBadInputData.into());
262265
}
263266
if self == &zero {
264267
return Ok(zero);
265268
}
266269

267270
// This ignores p=2 (for which this algorithm is valid), as not cryptographically interesting.
268271
if p.get_bit(0) == false || p <= &zero {
269-
return Err(Error::MpiBadInputData);
272+
return Err(codes::MpiBadInputData.into());
270273
}
271274

272275
if self.jacobi(p)? != 1 {
273276
// a is not a quadratic residue mod p
274-
return Err(Error::MpiBadInputData);
277+
return Err(codes::MpiBadInputData.into());
275278
}
276279

277280
if (p % 4)?.as_u32()? == 3 {
@@ -318,7 +321,7 @@ impl Mpi {
318321
bo = bo.mod_exp(&two, p)?;
319322
m += 1;
320323
if m >= r {
321-
return Err(Error::MpiBadInputData);
324+
return Err(codes::MpiBadInputData.into());
322325
}
323326
}
324327

@@ -351,7 +354,7 @@ impl Mpi {
351354
let one = Mpi::new(1)?;
352355

353356
if self < &zero || n < &zero || n.get_bit(0) == false {
354-
return Err(Error::MpiBadInputData);
357+
return Err(codes::MpiBadInputData.into());
355358
}
356359

357360
let mut x = self.modulo(n)?;

mbedtls/src/cipher/raw/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
use mbedtls_sys::*;
1010

11-
use crate::error::{Error, IntoResult, Result};
11+
use crate::error::{IntoResult, Result, codes};
1212

1313
mod serde;
1414

@@ -234,7 +234,7 @@ impl Cipher {
234234
};
235235

236236
if outdata.len() < reqd_size {
237-
return Err(Error::CipherFullBlockExpected);
237+
return Err(codes::CipherFullBlockExpected.into());
238238
}
239239

240240
let mut olen = 0;
@@ -254,7 +254,7 @@ impl Cipher {
254254
pub fn finish(&mut self, outdata: &mut [u8]) -> Result<usize> {
255255
// Check that minimum required space is available in outdata buffer
256256
if outdata.len() < self.block_size() {
257-
return Err(Error::CipherFullBlockExpected);
257+
return Err(codes::CipherFullBlockExpected.into());
258258
}
259259

260260
let mut olen = 0;
@@ -325,7 +325,7 @@ impl Cipher {
325325
if cipher_and_tag.len()
326326
.checked_sub(tag_len)
327327
.map_or(true, |cipher_len| cipher_len < plain.len()) {
328-
return Err(Error::CipherBadInputData);
328+
return Err(codes::CipherBadInputData.into());
329329
}
330330

331331
let iv = self.inner.iv;
@@ -363,7 +363,7 @@ impl Cipher {
363363
cipher_and_tag.len()
364364
.checked_sub(tag_len)
365365
.map_or(true, |cipher_len| plain.len() < cipher_len) {
366-
return Err(Error::CipherBadInputData);
366+
return Err(codes::CipherBadInputData.into());
367367
}
368368

369369
let iv = self.inner.iv;
@@ -474,7 +474,7 @@ impl Cipher {
474474
pub fn cmac(&mut self, key: &[u8], data: &[u8], outdata: &mut [u8]) -> Result<()> {
475475
// Check that outdata buffer has enough space
476476
if outdata.len() < self.block_size() {
477-
return Err(Error::CipherFullBlockExpected);
477+
return Err(codes::CipherFullBlockExpected.into());
478478
}
479479
self.reset()?;
480480
unsafe {

mbedtls/src/ecp/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* according to those terms. */
88

99
use core::convert::TryFrom;
10-
use crate::error::{Error, IntoResult, Result};
10+
use crate::error::{Error, IntoResult, Result, codes};
1111
use mbedtls_sys::*;
1212

1313
#[cfg(not(feature = "std"))]
@@ -114,7 +114,7 @@ impl EcGroup {
114114
|| &order <= &zero
115115
|| (&a == &zero && &b == &zero)
116116
{
117-
return Err(Error::EcpBadInputData);
117+
return Err(codes::EcpBadInputData.into());
118118
}
119119

120120
// Compute `order - 2`, needed below.
@@ -135,7 +135,7 @@ impl EcGroup {
135135
Test that the provided generator satisfies the curve equation
136136
*/
137137
if unsafe { ecp_check_pubkey(&ret.inner, &ret.inner.G) } != 0 {
138-
return Err(Error::EcpBadInputData);
138+
return Err(codes::EcpBadInputData.into());
139139
}
140140

141141
/*
@@ -162,7 +162,7 @@ impl EcGroup {
162162
let is_zero = unsafe { ecp_is_zero(&g_m.inner as *const ecp_point as *mut ecp_point) };
163163

164164
if is_zero != 1 {
165-
return Err(Error::EcpBadInputData);
165+
return Err(codes::EcpBadInputData.into());
166166
}
167167

168168
Ok(ret)
@@ -200,7 +200,7 @@ impl EcGroup {
200200
EcGroupId::Curve25519 => Ok(8),
201201
EcGroupId::Curve448 => Ok(4),
202202
// Requires a point-counting algorithm such as SEA.
203-
EcGroupId::None => Err(Error::EcpFeatureUnavailable),
203+
EcGroupId::None => Err(codes::EcpFeatureUnavailable.into()),
204204
_ => Ok(1),
205205
}
206206
}
@@ -213,7 +213,7 @@ impl EcGroup {
213213
match unsafe { ecp_check_pubkey(&self.inner, &point.inner) } {
214214
0 => Ok(true),
215215
ERR_ECP_INVALID_KEY => Ok(false),
216-
err => Err(Error::from_mbedtls_code(err)),
216+
err => Err(err.into()),
217217
}
218218
}
219219
}
@@ -256,7 +256,7 @@ impl EcPoint {
256256
}
257257

258258
pub fn from_binary(group: &EcGroup, bin: &[u8]) -> Result<EcPoint> {
259-
let prefix = *bin.get(0).ok_or(Error::EcpBadInputData)?;
259+
let prefix = *bin.get(0).ok_or(Error::from(codes::EcpBadInputData))?;
260260

261261
if prefix == 0x02 || prefix == 0x03 {
262262
// Compressed point, which mbedtls does not understand
@@ -267,7 +267,7 @@ impl EcPoint {
267267
let b = group.b()?;
268268

269269
if bin.len() != (p.byte_length()? + 1) {
270-
return Err(Error::EcpBadInputData);
270+
return Err(codes::EcpBadInputData.into());
271271
}
272272

273273
let x = Mpi::from_binary(&bin[1..]).unwrap();
@@ -319,7 +319,7 @@ impl EcPoint {
319319
match unsafe { ecp_is_zero(&self.inner as *const ecp_point as *mut ecp_point) } {
320320
0 => Ok(false),
321321
1 => Ok(true),
322-
_ => Err(Error::EcpInvalidKey),
322+
_ => Err(codes::EcpInvalidKey.into()),
323323
}
324324
}
325325

@@ -355,11 +355,11 @@ impl EcPoint {
355355
let mut ret = Self::init();
356356

357357
if group.contains_point(&pt1)? == false {
358-
return Err(Error::EcpInvalidKey);
358+
return Err(codes::EcpInvalidKey.into());
359359
}
360360

361361
if group.contains_point(&pt2)? == false {
362-
return Err(Error::EcpInvalidKey);
362+
return Err(codes::EcpInvalidKey.into());
363363
}
364364

365365
unsafe {
@@ -383,7 +383,7 @@ impl EcPoint {
383383
match r {
384384
0 => Ok(true),
385385
ERR_ECP_BAD_INPUT_DATA => Ok(false),
386-
x => Err(Error::from_mbedtls_code(x)),
386+
x => Err(x.into()),
387387
}
388388
}
389389

0 commit comments

Comments
 (0)