Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ctz committed Jul 17, 2017
1 parent dbf16df commit 84814bb
Show file tree
Hide file tree
Showing 21 changed files with 318 additions and 64 deletions.
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,8 @@ pub enum Error {
/// The SCT contained an invalid signature.
InvalidSignature,

/// The SCT referenced a Log that has an invalid public key encoding.
InvalidKey,

/// The SCT was signed in the future. Clock skew?
SCTTimestampInFuture,
TimestampInFuture,

/// The SCT had a version that this library does not handle.
UnsupportedSCTVersion,
Expand Down Expand Up @@ -141,6 +138,11 @@ fn write_u24(v: u32, out: &mut Vec<u8>) {
out.push(v as u8);
}

fn write_u16(v: u16, out: &mut Vec<u8>) {
out.push((v >> 8) as u8);
out.push(v as u8);
}

#[derive(Debug)]
struct SCT<'a> {
log_id: &'a [u8],
Expand All @@ -157,7 +159,6 @@ const RSA_PKCS1_SHA384: u16 = 0x0501;
const SCT_V1: u8 = 0u8;
const SCT_TIMESTAMP: u8 = 0u8;
const SCT_X509_ENTRY: [u8; 2] = [0, 0];
const SCT_NO_EXTENSION: [u8; 2] = [0, 0];

impl<'a> SCT<'a> {
fn verify(&self, key: &[u8], cert: &[u8]) -> Result<(), Error> {
Expand All @@ -176,7 +177,8 @@ impl<'a> SCT<'a> {
data.extend_from_slice(&SCT_X509_ENTRY);
write_u24(cert.len() as u32, &mut data);
data.extend_from_slice(cert);
data.extend_from_slice(&SCT_NO_EXTENSION);
write_u16(self.exts.len() as u16, &mut data);
data.extend_from_slice(self.exts);

let sig = untrusted::Input::from(self.sig);
let data = untrusted::Input::from(&data);
Expand Down Expand Up @@ -249,7 +251,7 @@ pub fn verify_sct(cert: &[u8],
sct.verify(log.key, cert)?;

if sct.timestamp > at_time {
return Err(Error::SCTTimestampInFuture);
return Err(Error::TimestampInFuture);
}

Ok(i)
Expand Down
Binary file modified src/testdata/ecdsa_p256-basic-sct.bin
Binary file not shown.
Binary file added src/testdata/ecdsa_p256-future-sct.bin
Binary file not shown.
Binary file added src/testdata/ecdsa_p256-junk-sct.bin
Binary file not shown.
Binary file added src/testdata/ecdsa_p256-short-sct.bin
Binary file not shown.
Binary file added src/testdata/ecdsa_p256-version-sct.bin
Binary file not shown.
Binary file added src/testdata/ecdsa_p256-wrongcert-sct.bin
Binary file not shown.
Binary file added src/testdata/ecdsa_p256-wrongext-sct.bin
Binary file not shown.
Binary file added src/testdata/ecdsa_p256-wrongid-sct.bin
Binary file not shown.
Binary file added src/testdata/ecdsa_p256-wrongtime-sct.bin
Binary file not shown.
Binary file modified src/testdata/ecdsa_p384-basic-sct.bin
Binary file not shown.
Binary file added src/testdata/ecdsa_p384-wrongcert-sct.bin
Binary file not shown.
Binary file added src/testdata/ecdsa_p384-wrongtime-sct.bin
Binary file not shown.
Binary file added src/testdata/rsa2048-wrongcert-sct.bin
Binary file not shown.
Binary file added src/testdata/rsa2048-wrongtime-sct.bin
Binary file not shown.
Binary file added src/testdata/rsa3072-wrongcert-sct.bin
Binary file not shown.
Binary file added src/testdata/rsa3072-wrongtime-sct.bin
Binary file not shown.
Binary file added src/testdata/rsa4096-wrongcert-sct.bin
Binary file not shown.
Binary file added src/testdata/rsa4096-wrongtime-sct.bin
Binary file not shown.
258 changes: 218 additions & 40 deletions src/tests_generated.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
use super::{Log, verify_sct};
use super::{Log, Error, verify_sct};

static TEST_LOG_ECDSA_P256: Log = Log {
description: "fake test ecdsa_p256 log",
url: "",
operated_by: "random python script",
max_merge_delay: 0,
key: include_bytes!("testdata/ecdsa-prime256v1-pub.raw"),
id: [0x71, 0xdc, 0x5e, 0xdb, 0xf0, 0x13, 0xd3, 0x88, 0x8a, 0x14, 0x6f, 0x49, 0x3d, 0xbe, 0x33, 0x94, 0xbb, 0x5a, 0xdb, 0x65, 0xb2, 0x6a, 0x96, 0xe2, 0x38, 0x35, 0x4e, 0xd4, 0x8f, 0xeb, 0xb2, 0x4f],
};

static TEST_LOG_ECDSA_P384: Log = Log {
description: "fake test ecdsa_p384 log",
url: "",
operated_by: "random python script",
max_merge_delay: 0,
key: include_bytes!("testdata/ecdsa-secp384r1-pub.raw"),
id: [0x29, 0xbb, 0xef, 0x00, 0xba, 0xd9, 0x3d, 0x5d, 0x4c, 0x03, 0xc7, 0x29, 0xe9, 0x4d, 0xb6, 0xac, 0x00, 0xe0, 0xfd, 0x28, 0xf6, 0x46, 0x56, 0x37, 0x24, 0xac, 0x58, 0xdc, 0x66, 0xb1, 0x99, 0xe9],
};

static TEST_LOG_RSA2048: Log = Log {
description: "fake test rsa2048 log",
Expand Down Expand Up @@ -27,76 +45,236 @@ static TEST_LOG_RSA4096: Log = Log {
id: [0xfb, 0x56, 0x27, 0x12, 0xec, 0xa0, 0xf0, 0xdc, 0x7f, 0x06, 0xda, 0x76, 0xab, 0xba, 0x5d, 0x88, 0x28, 0x2b, 0x62, 0xc5, 0x71, 0xf6, 0x0d, 0x69, 0x41, 0x94, 0x85, 0x16, 0xc8, 0x22, 0xf3, 0x29],
};

static TEST_LOG_ECDSA_P256: Log = Log {
description: "fake test ecdsa_p256 log",
url: "",
operated_by: "random python script",
max_merge_delay: 0,
key: include_bytes!("testdata/ecdsa-prime256v1-pub.raw"),
id: [0x71, 0xdc, 0x5e, 0xdb, 0xf0, 0x13, 0xd3, 0x88, 0x8a, 0x14, 0x6f, 0x49, 0x3d, 0xbe, 0x33, 0x94, 0xbb, 0x5a, 0xdb, 0x65, 0xb2, 0x6a, 0x96, 0xe2, 0x38, 0x35, 0x4e, 0xd4, 0x8f, 0xeb, 0xb2, 0x4f],
};
#[test]
pub fn ecdsa_p256_basic() {
let sct = include_bytes!("testdata/ecdsa_p256-basic-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_ECDSA_P256];
let now = 1235;

static TEST_LOG_ECDSA_P384: Log = Log {
description: "fake test ecdsa_p384 log",
url: "",
operated_by: "random python script",
max_merge_delay: 0,
key: include_bytes!("testdata/ecdsa-secp384r1-pub.raw"),
id: [0x29, 0xbb, 0xef, 0x00, 0xba, 0xd9, 0x3d, 0x5d, 0x4c, 0x03, 0xc7, 0x29, 0xe9, 0x4d, 0xb6, 0xac, 0x00, 0xe0, 0xfd, 0x28, 0xf6, 0x46, 0x56, 0x37, 0x24, 0xac, 0x58, 0xdc, 0x66, 0xb1, 0x99, 0xe9],
};
assert_eq!(Ok(0),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn ecdsa_p256_wrongtime() {
let sct = include_bytes!("testdata/ecdsa_p256-wrongtime-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_ECDSA_P256];
let now = 1235;

assert_eq!(Err(Error::InvalidSignature),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn ecdsa_p256_wrongcert() {
let sct = include_bytes!("testdata/ecdsa_p256-wrongcert-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_ECDSA_P256];
let now = 1235;

assert_eq!(Err(Error::InvalidSignature),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn ecdsa_p384_basic() {
let sct = include_bytes!("testdata/ecdsa_p384-basic-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_ECDSA_P384];
let now = 1235;

assert_eq!(Ok(0),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn test_basic_rsa2048() {
pub fn ecdsa_p384_wrongtime() {
let sct = include_bytes!("testdata/ecdsa_p384-wrongtime-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_ECDSA_P384];
let now = 1235;

assert_eq!(Err(Error::InvalidSignature),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn ecdsa_p384_wrongcert() {
let sct = include_bytes!("testdata/ecdsa_p384-wrongcert-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_ECDSA_P384];
let now = 1235;

assert_eq!(Err(Error::InvalidSignature),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn rsa2048_basic() {
let sct = include_bytes!("testdata/rsa2048-basic-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_RSA2048];
let now = 1235;
assert_eq!(0,
verify_sct(cert, sct, now, &logs).unwrap());

assert_eq!(Ok(0),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn test_basic_rsa3072() {
pub fn rsa2048_wrongtime() {
let sct = include_bytes!("testdata/rsa2048-wrongtime-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_RSA2048];
let now = 1235;

assert_eq!(Err(Error::InvalidSignature),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn rsa2048_wrongcert() {
let sct = include_bytes!("testdata/rsa2048-wrongcert-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_RSA2048];
let now = 1235;

assert_eq!(Err(Error::InvalidSignature),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn rsa3072_basic() {
let sct = include_bytes!("testdata/rsa3072-basic-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_RSA3072];
let now = 1235;

assert_eq!(0,
verify_sct(cert, sct, now, &logs).unwrap());

assert_eq!(Ok(0),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn rsa3072_wrongtime() {
let sct = include_bytes!("testdata/rsa3072-wrongtime-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_RSA3072];
let now = 1235;

assert_eq!(Err(Error::InvalidSignature),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn rsa3072_wrongcert() {
let sct = include_bytes!("testdata/rsa3072-wrongcert-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_RSA3072];
let now = 1235;

assert_eq!(Err(Error::InvalidSignature),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn test_basic_rsa4096() {
pub fn rsa4096_basic() {
let sct = include_bytes!("testdata/rsa4096-basic-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_RSA4096];
let now = 1235;
assert_eq!(0,
verify_sct(cert, sct, now, &logs).unwrap());

assert_eq!(Ok(0),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn test_basic_ecdsa_p256() {
let sct = include_bytes!("testdata/ecdsa_p256-basic-sct.bin");
pub fn rsa4096_wrongtime() {
let sct = include_bytes!("testdata/rsa4096-wrongtime-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_RSA4096];
let now = 1235;

assert_eq!(Err(Error::InvalidSignature),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn rsa4096_wrongcert() {
let sct = include_bytes!("testdata/rsa4096-wrongcert-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_RSA4096];
let now = 1235;

assert_eq!(Err(Error::InvalidSignature),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn ecdsa_p256_junk() {
let sct = include_bytes!("testdata/ecdsa_p256-junk-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_ECDSA_P256];
let now = 1235;
assert_eq!(0,
verify_sct(cert, sct, now, &logs).unwrap());

assert_eq!(Err(Error::MalformedSCT),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn test_basic_ecdsa_p384() {
let sct = include_bytes!("testdata/ecdsa_p384-basic-sct.bin");
pub fn ecdsa_p256_wrongid() {
let sct = include_bytes!("testdata/ecdsa_p256-wrongid-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_ECDSA_P384];
let logs = [&TEST_LOG_ECDSA_P256];
let now = 1235;

assert_eq!(Err(Error::UnknownLog),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn ecdsa_p256_version() {
let sct = include_bytes!("testdata/ecdsa_p256-version-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_ECDSA_P256];
let now = 1235;

assert_eq!(Err(Error::UnsupportedSCTVersion),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn ecdsa_p256_future() {
let sct = include_bytes!("testdata/ecdsa_p256-future-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_ECDSA_P256];
let now = 1233;

assert_eq!(Err(Error::TimestampInFuture),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn ecdsa_p256_wrongext() {
let sct = include_bytes!("testdata/ecdsa_p256-wrongext-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_ECDSA_P256];
let now = 1235;

assert_eq!(0,
verify_sct(cert, sct, now, &logs).unwrap());

assert_eq!(Err(Error::InvalidSignature),
verify_sct(cert, sct, now, &logs));
}

#[test]
pub fn ecdsa_p256_short() {
let sct = include_bytes!("testdata/ecdsa_p256-short-sct.bin");
let cert = b"cert";
let logs = [&TEST_LOG_ECDSA_P256];
let now = 1234;

for l in 0..118 {
assert_eq!(Err(Error::MalformedSCT),
verify_sct(cert, &sct[..l], now, &logs));
}
}

Loading

0 comments on commit 84814bb

Please sign in to comment.