aboutsummaryrefslogtreecommitdiff
path: root/openssl/src
diff options
context:
space:
mode:
Diffstat (limited to 'openssl/src')
-rw-r--r--openssl/src/bn/mod.rs7
-rw-r--r--openssl/src/crypto/pkey.rs45
-rw-r--r--openssl/src/crypto/rsa.rs28
-rw-r--r--openssl/src/lib.rs2
-rw-r--r--openssl/src/ssl/mod.rs93
5 files changed, 171 insertions, 4 deletions
diff --git a/openssl/src/bn/mod.rs b/openssl/src/bn/mod.rs
index 00a0a0ca..d548e9ef 100644
--- a/openssl/src/bn/mod.rs
+++ b/openssl/src/bn/mod.rs
@@ -1,7 +1,7 @@
use libc::{c_int, c_ulong, c_void};
use std::ffi::{CStr, CString};
use std::cmp::Ordering;
-use std::{fmt, ptr};
+use std::{fmt, ptr, mem};
use ffi;
use ssl::error::SslError;
@@ -473,6 +473,11 @@ impl BigNum {
n
}
+ pub fn into_raw(self) -> *mut ffi::BIGNUM {
+ let mut me = self;
+ mem::replace(&mut me.0, ptr::null_mut())
+ }
+
pub fn to_vec(&self) -> Vec<u8> {
let size = self.num_bytes() as usize;
let mut v = Vec::with_capacity(size);
diff --git a/openssl/src/crypto/pkey.rs b/openssl/src/crypto/pkey.rs
index e556730d..cafd50ad 100644
--- a/openssl/src/crypto/pkey.rs
+++ b/openssl/src/crypto/pkey.rs
@@ -205,6 +205,28 @@ impl PKey {
}
}
+ /// assign RSA key to this pkey
+ pub fn set_rsa(&mut self, rsa: &RSA) {
+ unsafe {
+ // this needs to be a reference as the set1_RSA ups the reference count
+ let rsa_ptr = rsa.as_ptr();
+ if ffi::EVP_PKEY_set1_RSA(self.evp, rsa_ptr) == 1 {
+ if rsa.has_e() && rsa.has_n() {
+ self.parts = Parts::Public;
+ }
+ }
+ }
+ }
+
+ /// get a reference to the interal RSA key for direct access to the key components
+ pub fn get_rsa(&self) -> RSA {
+ unsafe {
+ let evp_pkey: *mut ffi::EVP_PKEY = self.evp;
+ // this is safe as the ffi increments a reference counter to the internal key
+ RSA::from_raw(ffi::EVP_PKEY_get1_RSA(evp_pkey))
+ }
+ }
+
/**
* Returns a DER serialized form of the public key, suitable for load_pub().
*/
@@ -604,6 +626,7 @@ mod tests {
use std::path::Path;
use std::fs::File;
use crypto::hash::Type::{MD5, SHA1};
+ use crypto::rsa::RSA;
#[test]
fn test_gen_pub() {
@@ -791,6 +814,28 @@ mod tests {
}
#[test]
+ fn test_public_key_from_raw() {
+ let mut k0 = super::PKey::new();
+ let mut k1 = super::PKey::new();
+ let msg = vec![0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
+
+ k0.gen(512);
+ let sig = k0.sign(&msg);
+
+ let r0 = k0.get_rsa();
+ let r1 = RSA::from_public_components(r0.n().expect("n"), r0.e().expect("e")).expect("r1");
+ k1.set_rsa(&r1);
+
+ assert!(k1.can(super::Role::Encrypt));
+ assert!(!k1.can(super::Role::Decrypt));
+ assert!(k1.can(super::Role::Verify));
+ assert!(!k1.can(super::Role::Sign));
+
+ let rv = k1.verify(&msg, &sig);
+ assert!(rv == true);
+ }
+
+ #[test]
#[should_panic(expected = "Could not get RSA key for encryption")]
fn test_nokey_encrypt() {
let mut pkey = super::PKey::new();
diff --git a/openssl/src/crypto/rsa.rs b/openssl/src/crypto/rsa.rs
index ee0d9ec4..6fcb5b07 100644
--- a/openssl/src/crypto/rsa.rs
+++ b/openssl/src/crypto/rsa.rs
@@ -18,6 +18,22 @@ impl Drop for RSA {
}
impl RSA {
+ /// only useful for associating the key material directly with the key, it's safer to use
+ /// the supplied load and save methods for DER formatted keys.
+ pub fn from_public_components(n: BigNum, e: BigNum) -> Result<RSA, SslError> {
+ unsafe {
+ let rsa = try_ssl_null!(ffi::RSA_new());
+ (*rsa).n = n.into_raw();
+ (*rsa).e = e.into_raw();
+ Ok(RSA(rsa))
+ }
+ }
+
+ /// the caller should assert that the rsa pointer is valid.
+ pub unsafe fn from_raw(rsa: *mut ffi::RSA) -> RSA {
+ RSA(rsa)
+ }
+
/// Reads an RSA private key from PEM formatted data.
pub fn private_key_from_pem<R>(reader: &mut R) -> Result<RSA, SslError>
where R: Read
@@ -61,6 +77,12 @@ impl RSA {
}
}
+ pub fn has_n(&self) -> bool {
+ unsafe {
+ !(*self.0).n.is_null()
+ }
+ }
+
pub fn d(&self) -> Result<BigNum, SslError> {
unsafe {
BigNum::new_from_ffi((*self.0).d)
@@ -73,6 +95,12 @@ impl RSA {
}
}
+ pub fn has_e(&self) -> bool {
+ unsafe {
+ !(*self.0).e.is_null()
+ }
+ }
+
pub fn p(&self) -> Result<BigNum, SslError> {
unsafe {
BigNum::new_from_ffi((*self.0).p)
diff --git a/openssl/src/lib.rs b/openssl/src/lib.rs
index f1b6d13a..e89e6590 100644
--- a/openssl/src/lib.rs
+++ b/openssl/src/lib.rs
@@ -1,4 +1,4 @@
-#![doc(html_root_url="https://sfackler.github.io/rust-openssl/doc/v0.7.6")]
+#![doc(html_root_url="https://sfackler.github.io/rust-openssl/doc/v0.7.7")]
#![cfg_attr(feature = "nightly", feature(const_fn, recover, panic_propagate))]
#[macro_use]
diff --git a/openssl/src/ssl/mod.rs b/openssl/src/ssl/mod.rs
index 8e6d061d..574a324b 100644
--- a/openssl/src/ssl/mod.rs
+++ b/openssl/src/ssl/mod.rs
@@ -569,7 +569,8 @@ impl SslContext {
pub fn set_servername_callback(&mut self, callback: Option<ServerNameCallback>) {
unsafe {
ffi::SSL_CTX_set_ex_data(self.ctx, SNI_IDX, mem::transmute(callback));
- let f: extern "C" fn() = mem::transmute(raw_sni);
+ let f: extern "C" fn(_, _, _) -> _ = raw_sni;
+ let f: extern "C" fn() = mem::transmute(f);
ffi_extras::SSL_CTX_set_tlsext_servername_callback(self.ctx, Some(f));
}
}
@@ -586,7 +587,8 @@ impl SslContext {
ffi::SSL_CTX_set_ex_data(self.ctx, SNI_IDX, mem::transmute(Some(callback)));
ffi_extras::SSL_CTX_set_tlsext_servername_arg(self.ctx, mem::transmute(data));
- let f: extern "C" fn() = mem::transmute(raw_sni_with_data::<T>);
+ let f: extern "C" fn(_, _, _) -> _ = raw_sni_with_data::<T>;
+ let f: extern "C" fn() = mem::transmute(f);
ffi_extras::SSL_CTX_set_tlsext_servername_callback(self.ctx, Some(f));
}
}
@@ -769,6 +771,71 @@ impl SslContext {
}
}
+
+pub struct CipherBits {
+ /// The number of secret bits used for the cipher.
+ pub secret: i32,
+ /// The number of bits processed by the chosen algorithm, if not None.
+ pub algorithm: Option<i32>,
+}
+
+
+pub struct SslCipher<'a> {
+ cipher: *const ffi::SSL_CIPHER,
+ ph: PhantomData<&'a ()>,
+}
+
+impl <'a> SslCipher<'a> {
+ /// Returns the name of cipher.
+ pub fn name(&self) -> &'static str {
+ let name = unsafe {
+ let ptr = ffi::SSL_CIPHER_get_name(self.cipher);
+ CStr::from_ptr(ptr as *const _)
+ };
+
+ str::from_utf8(name.to_bytes()).unwrap()
+ }
+
+ /// Returns the SSL/TLS protocol version that first defined the cipher.
+ pub fn version(&self) -> &'static str {
+ let version = unsafe {
+ let ptr = ffi::SSL_CIPHER_get_version(self.cipher);
+ CStr::from_ptr(ptr as *const _)
+ };
+
+ str::from_utf8(version.to_bytes()).unwrap()
+ }
+
+ /// Returns the number of bits used for the cipher.
+ pub fn bits(&self) -> CipherBits {
+ unsafe {
+ let algo_bits : *mut c_int = ptr::null_mut();
+ let secret_bits = ffi::SSL_CIPHER_get_bits(self.cipher, algo_bits);
+ if !algo_bits.is_null() {
+ CipherBits { secret: secret_bits, algorithm: Some(*algo_bits) }
+ } else {
+ CipherBits { secret: secret_bits, algorithm: None }
+ }
+ }
+ }
+
+ /// Returns a textual description of the cipher used
+ pub fn description(&self) -> Option<String> {
+ unsafe {
+ // SSL_CIPHER_description requires a buffer of at least 128 bytes.
+ let mut buf = [0i8; 128];
+ let desc_ptr = ffi::SSL_CIPHER_description(self.cipher, &mut buf[0], 128);
+
+ if !desc_ptr.is_null() {
+ String::from_utf8(CStr::from_ptr(desc_ptr).to_bytes().to_vec()).ok()
+ } else {
+ None
+ }
+ }
+ }
+}
+
+
pub struct Ssl {
ssl: *mut ffi::SSL,
}
@@ -836,6 +903,18 @@ impl Ssl {
}
}
+ pub fn get_current_cipher<'a>(&'a self) -> Option<SslCipher<'a>> {
+ unsafe {
+ let ptr = ffi::SSL_get_current_cipher(self.ssl);
+
+ if ptr.is_null() {
+ None
+ } else {
+ Some(SslCipher{ cipher: ptr, ph: PhantomData })
+ }
+ }
+ }
+
pub fn state_string(&self) -> &'static str {
let state = unsafe {
let ptr = ffi::SSL_state_string(self.ssl);
@@ -881,6 +960,16 @@ impl Ssl {
}
}
+ /// Returns the name of the protocol used for the connection, e.g. "TLSv1.2", "SSLv3", etc.
+ pub fn version(&self) -> &'static str {
+ let version = unsafe {
+ let ptr = ffi::SSL_get_version(self.ssl);
+ CStr::from_ptr(ptr as *const _)
+ };
+
+ str::from_utf8(version.to_bytes()).unwrap()
+ }
+
/// Returns the protocol selected by performing Next Protocol Negotiation, if any.
///
/// The protocol's name is returned is an opaque sequence of bytes. It is up to the client