aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Dawes <[email protected]>2016-05-05 23:41:55 +0100
committerChris Dawes <[email protected]>2016-05-05 23:41:55 +0100
commitf82a1c4f75208c414a24deb2a6d90e0fc3981637 (patch)
treef926484132542ba9d068b8a961a6f0b4bea01840
parentadd missing NIDs and use Nid as input to signing (diff)
downloadrust-openssl-f82a1c4f75208c414a24deb2a6d90e0fc3981637.tar.xz
rust-openssl-f82a1c4f75208c414a24deb2a6d90e0fc3981637.zip
add rsa signature tests
-rw-r--r--openssl-sys/src/lib.rs6
-rw-r--r--openssl/src/crypto/rsa.rs109
-rw-r--r--openssl/test/rsa.pem27
-rw-r--r--openssl/test/rsa.pem.pub9
4 files changed, 150 insertions, 1 deletions
diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs
index 3d7c59c3..0a762944 100644
--- a/openssl-sys/src/lib.rs
+++ b/openssl-sys/src/lib.rs
@@ -627,6 +627,12 @@ extern "C" {
callback: Option<PasswordCallback>,
user_data: *mut c_void) -> c_int;
pub fn PEM_write_bio_PUBKEY(bp: *mut BIO, x: *mut EVP_PKEY) -> c_int;
+ pub fn PEM_write_bio_RSAPrivateKey(bp: *mut BIO, rsa: *mut RSA, cipher: *const EVP_CIPHER,
+ kstr: *mut c_char, klen: c_int,
+ callback: Option<PasswordCallback>,
+ user_data: *mut c_void) -> c_int;
+ pub fn PEM_write_bio_RSAPublicKey(bp: *mut BIO, rsa: *mut RSA) -> c_int;
+ pub fn PEM_write_bio_RSA_PUBKEY(bp: *mut BIO, rsa: *mut RSA) -> c_int;
pub fn PEM_write_bio_X509(bio: *mut BIO, x509: *mut X509) -> c_int;
pub fn PEM_write_bio_X509_REQ(bio: *mut BIO, x509: *mut X509_REQ) -> c_int;
diff --git a/openssl/src/crypto/rsa.rs b/openssl/src/crypto/rsa.rs
index f9d1dece..021d450b 100644
--- a/openssl/src/crypto/rsa.rs
+++ b/openssl/src/crypto/rsa.rs
@@ -2,7 +2,7 @@ use ffi;
use std::fmt;
use ssl::error::{SslError, StreamError};
use std::ptr;
-use std::io::{self, Read};
+use std::io::{self, Read, Write};
use bn::BigNum;
use bio::MemBio;
@@ -65,6 +65,25 @@ impl RSA {
Ok(RSA(rsa))
}
}
+
+ /// Writes an RSA private key as unencrypted PEM formatted data
+ pub fn private_key_to_pem<W>(&self, writer: &mut W) -> Result<(), SslError>
+ where W: Write
+ {
+ let mut mem_bio = try!(MemBio::new());
+
+ let result = unsafe {
+ ffi::PEM_write_bio_RSAPrivateKey(mem_bio.get_handle(), self.0, ptr::null(), ptr::null_mut(), 0, None, ptr::null_mut())
+ };
+
+ if result == 1 {
+ try!(io::copy(&mut mem_bio, writer).map_err(StreamError));
+
+ Ok(())
+ } else {
+ Err(SslError::OpenSslErrors(vec![]))
+ }
+ }
/// Reads an RSA public key from PEM formatted data.
pub fn public_key_from_pem<R>(reader: &mut R) -> Result<RSA, SslError>
@@ -82,6 +101,25 @@ impl RSA {
}
}
+ /// Writes an RSA public key as PEM formatted data
+ pub fn public_key_to_pem<W>(&self, writer: &mut W) -> Result<(), SslError>
+ where W: Write
+ {
+ let mut mem_bio = try!(MemBio::new());
+
+ let result = unsafe {
+ ffi::PEM_write_bio_RSA_PUBKEY(mem_bio.get_handle(), self.0)
+ };
+
+ if result == 1 {
+ try!(io::copy(&mut mem_bio, writer).map_err(StreamError));
+
+ Ok(())
+ } else {
+ Err(SslError::OpenSslErrors(vec![]))
+ }
+ }
+
pub fn size(&self) -> Result<u32, SslError> {
if self.has_n() {
unsafe {
@@ -170,3 +208,72 @@ impl fmt::Debug for RSA {
write!(f, "RSA")
}
}
+
+#[cfg(test)]
+mod test {
+ use nid;
+ use std::fs::File;
+ use std::io::Write;
+ use super::*;
+ use crypto::hash::*;
+
+ fn signing_input_rs256() -> Vec<u8> {
+ vec![101, 121, 74, 104, 98, 71, 99, 105, 79, 105, 74, 83, 85, 122, 73,
+ 49, 78, 105, 74, 57, 46, 101, 121, 74, 112, 99, 51, 77, 105, 79, 105,
+ 74, 113, 98, 50, 85, 105, 76, 65, 48, 75, 73, 67, 74, 108, 101, 72,
+ 65, 105, 79, 106, 69, 122, 77, 68, 65, 52, 77, 84, 107, 122, 79, 68,
+ 65, 115, 68, 81, 111, 103, 73, 109, 104, 48, 100, 72, 65, 54, 76,
+ 121, 57, 108, 101, 71, 70, 116, 99, 71, 120, 108, 76, 109, 78, 118,
+ 98, 83, 57, 112, 99, 49, 57, 121, 98, 50, 57, 48, 73, 106, 112, 48,
+ 99, 110, 86, 108, 102, 81]
+ }
+
+ fn signature_rs256() -> Vec<u8> {
+ vec![112, 46, 33, 137, 67, 232, 143, 209, 30, 181, 216, 45, 191, 120, 69,
+ 243, 65, 6, 174, 27, 129, 255, 247, 115, 17, 22, 173, 209, 113, 125,
+ 131, 101, 109, 66, 10, 253, 60, 150, 238, 221, 115, 162, 102, 62, 81,
+ 102, 104, 123, 0, 11, 135, 34, 110, 1, 135, 237, 16, 115, 249, 69,
+ 229, 130, 173, 252, 239, 22, 216, 90, 121, 142, 232, 198, 109, 219,
+ 61, 184, 151, 91, 23, 208, 148, 2, 190, 237, 213, 217, 217, 112, 7,
+ 16, 141, 178, 129, 96, 213, 248, 4, 12, 167, 68, 87, 98, 184, 31,
+ 190, 127, 249, 217, 46, 10, 231, 111, 36, 242, 91, 51, 187, 230, 244,
+ 74, 230, 30, 177, 4, 10, 203, 32, 4, 77, 62, 249, 18, 142, 212, 1,
+ 48, 121, 91, 212, 189, 59, 65, 238, 202, 208, 102, 171, 101, 25, 129,
+ 253, 228, 141, 247, 127, 55, 45, 195, 139, 159, 175, 221, 59, 239,
+ 177, 139, 93, 163, 204, 60, 46, 176, 47, 158, 58, 65, 214, 18, 202,
+ 173, 21, 145, 18, 115, 160, 95, 35, 185, 232, 56, 250, 175, 132, 157,
+ 105, 132, 41, 239, 90, 30, 136, 121, 130, 54, 195, 212, 14, 96, 69,
+ 34, 165, 68, 200, 242, 122, 122, 45, 184, 6, 99, 209, 108, 247, 202,
+ 234, 86, 222, 64, 92, 178, 33, 90, 69, 178, 194, 85, 102, 181, 90,
+ 193, 167, 72, 160, 112, 223, 200, 163, 42, 70, 149, 67, 208, 25, 238,
+ 251, 71]
+ }
+
+ #[test]
+ pub fn test_sign() {
+ let mut buffer = File::open("test/rsa.pem").unwrap();
+ let private_key = RSA::private_key_from_pem(&mut buffer).unwrap();
+
+ let mut sha = Hasher::new(Type::SHA256);
+ sha.write_all(&signing_input_rs256()).unwrap();
+ let digest = sha.finish();
+
+ let result = private_key.sign(nid::Nid::SHA256, &digest).unwrap();
+
+ assert_eq!(result, signature_rs256());
+ }
+
+ #[test]
+ pub fn test_verify() {
+ let mut buffer = File::open("test/rsa.pem.pub").unwrap();
+ let public_key = RSA::public_key_from_pem(&mut buffer).unwrap();
+
+ let mut sha = Hasher::new(Type::SHA256);
+ sha.write_all(&signing_input_rs256()).unwrap();
+ let digest = sha.finish();
+
+ let result = public_key.verify(nid::Nid::SHA256, &digest, &signature_rs256()).unwrap();
+
+ assert!(result);
+ }
+} \ No newline at end of file
diff --git a/openssl/test/rsa.pem b/openssl/test/rsa.pem
new file mode 100644
index 00000000..d8185fed
--- /dev/null
+++ b/openssl/test/rsa.pem
@@ -0,0 +1,27 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIEowIBAAKCAQEAofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd/wWJcyQoTbji9k0
+l8W26mPddxHmfHQp+Vaw+4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL+yRT+SFd2lZS+pC
+gNMsD1W/YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb/7OMg0LOL+bSf63kpaSHSX
+ndS5z5rexMdbBYUsLA9e+KXBdQOS+UTo7WTBEMa2R2CapHg665xsmtdVMTBQY4uD
+Zlxvb3qCo5ZwKh9kG4LT6/I5IhlJH7aGhyxXFvUK+DWNmoudF8NAco9/h9iaGNj8
+q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQIDAQ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q93S
+0iJMdQKBgCGHewxzoa1r8ZMD0LETNrToK423K377UCYqXfg5XMclbrjPbEC3YI1Z
+NqMtuhdBJqUnBi6tjKMF+34Xf0CUN8ncuXGO2CAYvO8PdyCixHX52ybaDjy1FtCE
+6yUXjoKNXKvUm7MWGsAYH6f4IegOetN5NvmUMFStCSkh7ixZLkN1
+-----END RSA PRIVATE KEY-----
diff --git a/openssl/test/rsa.pem.pub b/openssl/test/rsa.pem.pub
new file mode 100644
index 00000000..093f2ba1
--- /dev/null
+++ b/openssl/test/rsa.pem.pub
@@ -0,0 +1,9 @@
+-----BEGIN PUBLIC KEY-----
+MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAofgWCuLjybRlzo0tZWJj
+NiuSfb4p4fAkd/wWJcyQoTbji9k0l8W26mPddxHmfHQp+Vaw+4qPCJrcS2mJPMEz
+P1Pt0Bm4d4QlL+yRT+SFd2lZS+pCgNMsD1W/YpRPEwOWvG6b32690r2jZ47soMZo
+9wGzjb/7OMg0LOL+bSf63kpaSHSXndS5z5rexMdbBYUsLA9e+KXBdQOS+UTo7WTB
+EMa2R2CapHg665xsmtdVMTBQY4uDZlxvb3qCo5ZwKh9kG4LT6/I5IhlJH7aGhyxX
+FvUK+DWNmoudF8NAco9/h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXp
+oQIDAQAB
+-----END PUBLIC KEY-----