Skip to content

Commit

Permalink
Upgrade to rustc 1.1.0-dev (435622028 2015-05-04) (built 2015-05-05)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcallister committed May 13, 2015
1 parent 7e1525f commit b3f0132
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 44 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ authors = ["[email protected]"]

log = "*"
rand = "*"
num = "*"
enum_primitive = "*"
2 changes: 0 additions & 2 deletions examples/google.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(net)]

extern crate suruga;

use std::io::prelude::*;
Expand Down
8 changes: 4 additions & 4 deletions src/cipher/chacha20_poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,18 @@ impl Aead for ChaCha20Poly1305 {
}

#[inline(always)]
fn new_encryptor(&self, key: Vec<u8>) -> Box<Encryptor + 'static> {
fn new_encryptor(&self, key: Vec<u8>) -> Box<Encryptor + Send + 'static> {
let encryptor = ChaCha20Poly1305Encryptor {
key: key,
};
Box::new(encryptor) as Box<Encryptor>
Box::new(encryptor) as Box<Encryptor + Send>
}

#[inline(always)]
fn new_decryptor(&self, key: Vec<u8>) -> Box<Decryptor + 'static> {
fn new_decryptor(&self, key: Vec<u8>) -> Box<Decryptor + Send + 'static> {
let decryptor = ChaCha20Poly1305Decryptor {
key: key,
};
Box::new(decryptor) as Box<Decryptor>
Box::new(decryptor) as Box<Decryptor + Send>
}
}
6 changes: 3 additions & 3 deletions src/cipher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub trait Aead {
fn key_size(&self) -> usize;
fn fixed_iv_len(&self) -> usize;
fn mac_len(&self) -> usize;
fn new_encryptor(&self, key: Vec<u8>) -> Box<Encryptor + 'static>;
fn new_decryptor(&self, key: Vec<u8>) -> Box<Decryptor + 'static>;
fn new_encryptor(&self, key: Vec<u8>) -> Box<Encryptor + Send + 'static>;
fn new_decryptor(&self, key: Vec<u8>) -> Box<Decryptor + Send + 'static>;
}

pub trait Encryptor {
Expand All @@ -41,7 +41,7 @@ macro_rules! cipher_suite {
$id:ident = $kex:ident, $cipher:ident, $mac:ident, $v1:expr, $v2:expr;
)+) => (
#[allow(non_camel_case_types)]
#[derive(Copy, PartialEq, Debug)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum CipherSuite {
$(
$id,
Expand Down
9 changes: 6 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rand::{Rng, OsRng};

use tls_result::TlsResult;
use tls_result::TlsErrorKind::{UnexpectedMessage, InternalError, DecryptError, IllegalParameter};
use util::crypto_compare;
use util::{SurugaError, crypto_compare};
use cipher::{self, Aead};
use cipher::prf::Prf;
use crypto::sha2::sha256;
Expand Down Expand Up @@ -255,7 +255,10 @@ impl<R: Read, W: Write> Write for TlsClient<R, W> {
Err(err) => {
let err = self.tls.send_tls_alert(err);
// FIXME more verbose io error
Err(io::Error::new(io::ErrorKind::Other, "TLS write error", Some(err.desc)))
Err(io::Error::new(io::ErrorKind::Other, SurugaError {
desc: "TLS write error",
cause: Some(Box::new(err)),
}))
}
}
}
Expand All @@ -280,7 +283,7 @@ impl<R: Read, W: Write> Read for TlsClient<R, W> {

let selflen = self.buf.len();
let necessary = cmp::min(remaining, selflen);
copy_memory(&mut buf[pos .. pos + necessary], &self.buf[.. necessary]);
copy_memory(&self.buf[.. necessary], &mut buf[pos .. pos + necessary]);
pos += necessary;

self.buf = self.buf[necessary..].to_vec();
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/wrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// NOTE: since there is no const fn (yet), you can't use `w32(100)` function
// for consts and statics. (workaround: use `Wrapping(100)`.)

pub use std::num::wrapping::{Wrapping, WrappingOps};
pub use std::num::Wrapping;

pub trait ToWrapping {
fn to_w64(self) -> w64;
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

#![allow(missing_copy_implementations)]

#![feature(io, core, collections, net)]
#![feature(core, collections)]

#[macro_use]
extern crate log;
extern crate rand;
extern crate num;

#[macro_use]
extern crate enum_primitive;

pub use tls::Tls;
pub use client::TlsClient;
Expand Down
28 changes: 15 additions & 13 deletions src/record.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::io::prelude::*;
use std::num::FromPrimitive;
use num::traits::FromPrimitive;

use tls_result::TlsResult;
use tls_result::TlsErrorKind::{UnexpectedMessage, RecordOverflow, BadRecordMac, AlertReceived};
Expand All @@ -15,14 +15,16 @@ use self::ContentType::{ChangeCipherSpecTy, AlertTy, HandshakeTy, ApplicationDat
use self::Message::{HandshakeMessage, ChangeCipherSpecMessage, AlertMessage,
ApplicationDataMessage};

#[repr(u8)]
#[derive(Copy, PartialEq, FromPrimitive, Debug)]
pub enum ContentType {
ChangeCipherSpecTy = 20,
AlertTy = 21,
HandshakeTy = 22,
ApplicationDataTy = 23,
// HeartBeat = 24, RFC 6520 extension :-)
enum_from_primitive! {
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum ContentType {
ChangeCipherSpecTy = 20,
AlertTy = 21,
HandshakeTy = 22,
ApplicationDataTy = 23,
// HeartBeat = 24, RFC 6520 extension :-)
}
}

/// maximum length of Record (excluding content_type, version, length fields)
Expand Down Expand Up @@ -84,7 +86,7 @@ impl EncryptedRecord {
pub struct RecordWriter<W: Write> {
writer: W,
// if encryptor is None, handshake is not done yet.
encryptor: Option<Box<Encryptor + 'static>>,
encryptor: Option<Box<Encryptor + Send + 'static>>,
write_count: u64,
}

Expand All @@ -97,7 +99,7 @@ impl<W: Write> RecordWriter<W> {
}
}

pub fn set_encryptor(&mut self, encryptor: Box<Encryptor + 'static>) {
pub fn set_encryptor(&mut self, encryptor: Box<Encryptor + Send + 'static>) {
self.encryptor = Some(encryptor);
self.write_count = 0;
}
Expand Down Expand Up @@ -191,7 +193,7 @@ pub enum Message {
pub struct RecordReader<R: ReadExt> {
reader: R,
// if decryptor is none, handshake is not done yet.
decryptor: Option<Box<Decryptor + 'static>>,
decryptor: Option<Box<Decryptor + Send + 'static>>,
read_count: u64,
handshake_buffer: HandshakeBuffer,
}
Expand All @@ -206,7 +208,7 @@ impl<R: ReadExt> RecordReader<R> {
}
}

pub fn set_decryptor(&mut self, decryptor: Box<Decryptor + 'static>) {
pub fn set_decryptor(&mut self, decryptor: Box<Decryptor + Send + 'static>) {
self.decryptor = Some(decryptor);
self.read_count = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ impl Decryptor for NullDecryptor {
fn null_tls<R: Read, W: Write>(reader: R, writer: W) -> Tls<R, W> {
let mut tls = Tls::new(reader, writer, OsRng::new().unwrap());

let null_encryptor = Box::new(NullEncryptor) as Box<Encryptor>;
let null_encryptor = Box::new(NullEncryptor) as Box<Encryptor + Send>;
tls.writer.set_encryptor(null_encryptor);
let null_decryptor = Box::new(NullDecryptor) as Box<Decryptor>;
let null_decryptor = Box::new(NullDecryptor) as Box<Decryptor + Send>;
tls.reader.set_decryptor(null_decryptor);

tls
Expand Down
18 changes: 10 additions & 8 deletions src/tls_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ macro_rules! tls_enum {
),+
}
) => (
#[allow(non_camel_case_types)]
#[derive(Copy, PartialEq, FromPrimitive)]
$(#[$a])*
pub enum $name {
$(
$item = $n,
)+
enum_from_primitive! {
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, PartialEq)]
$(#[$a])*
pub enum $name {
$(
$item = $n,
)+
}
}

impl TlsItem for $name {
Expand All @@ -106,7 +108,7 @@ macro_rules! tls_enum {

fn tls_read<R: ReadExt>(reader: &mut R) -> ::tls_result::TlsResult<$name> {
let num = stry_read_num!($repr_ty, reader) as u64;
let n: Option<$name> = ::std::num::FromPrimitive::from_u64(num);
let n: Option<$name> = ::num::traits::FromPrimitive::from_u64(num);
match n {
Some(n) => Ok(n),
None => tls_err!(::tls_result::TlsErrorKind::DecodeError,
Expand Down
8 changes: 4 additions & 4 deletions src/tls_result.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::error::{Error, FromError};
use std::error::Error;
use std::io;
use std::fmt;

#[derive(Copy, PartialEq, Debug)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum TlsErrorKind {
// corresponds to alert messages

Expand Down Expand Up @@ -54,8 +54,8 @@ impl Error for TlsError {
}
}

impl FromError<io::Error> for TlsError {
fn from_error(err: io::Error) -> TlsError {
impl From<io::Error> for TlsError {
fn from(err: io::Error) -> TlsError {
TlsError {
kind: TlsErrorKind::IoFailure,
desc: format!("io error: {}", err),
Expand Down
30 changes: 27 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
use std::mem;
use std::num::Int;
use std::{mem, fmt};
use std::error::Error;
use std::io::{self, Read, Write};

#[derive(Debug)]
pub struct SurugaError {
pub desc: &'static str,
pub cause: Option<Box<Error + Send + Sync + 'static>>,
}

impl fmt::Display for SurugaError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
<Self as fmt::Debug>::fmt(self, fmt)
}
}

impl Error for SurugaError {
fn description(&self) -> &str {
self.desc
}

// FIXME: implement fn cause(&self) -> Option<&Error>
// This runs into difficulties with differing trait bounds.
}

/// constant-time compare function.
/// `a` and `b` may be SECRET, but the length is known.
/// precondition: `a.len() == b.len()`
Expand Down Expand Up @@ -62,7 +83,10 @@ pub trait ReadExt: Read {
while pos < len {
let num_bytes = try!(self.read(&mut buf[pos..]));
if num_bytes == 0 {
return Err(io::Error::new(io::ErrorKind::Other, "EOF during `fill_exact`", None));
return Err(io::Error::new(io::ErrorKind::Other, SurugaError {
desc: "EOF during `fill_exact`",
cause: None
}));
}
pos += num_bytes;
}
Expand Down

0 comments on commit b3f0132

Please sign in to comment.