forked from sfackler/rust-openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sfackler#259 from jedisct1/dh
Add support for DHE for forward secrecy
- Loading branch information
Showing
8 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ aes_xts = [] | |
aes_ctr = [] | ||
npn = [] | ||
alpn = [] | ||
rfc5114 = [] | ||
|
||
[dependencies] | ||
libc = "0.1" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use ffi; | ||
use ssl::error::SslError; | ||
use bn::BigNum; | ||
use std::mem; | ||
use std::ptr; | ||
|
||
pub struct DH(*mut ffi::DH); | ||
|
||
impl DH { | ||
pub fn from_params(p: BigNum, g: BigNum, q: BigNum) -> Result<DH, SslError> { | ||
let dh = unsafe { ffi::DH_new_from_params(p.raw(), g.raw(), q.raw()) }; | ||
if dh == ptr::null_mut() { | ||
return Err(SslError::get()); | ||
} | ||
mem::forget(p); | ||
mem::forget(g); | ||
mem::forget(q); | ||
Ok(DH(dh)) | ||
} | ||
|
||
#[cfg(feature = "rfc5114")] | ||
pub fn get_1024_160() -> Result<DH, SslError> { | ||
let dh = unsafe { ffi::DH_get_1024_160() }; | ||
if dh == ptr::null_mut() { | ||
return Err(SslError::get()); | ||
} | ||
Ok(DH(dh)) | ||
} | ||
|
||
#[cfg(feature = "rfc5114")] | ||
pub fn get_2048_224() -> Result<DH, SslError> { | ||
let dh = unsafe { ffi::DH_get_2048_224() }; | ||
if dh == ptr::null_mut() { | ||
return Err(SslError::get()); | ||
} | ||
Ok(DH(dh)) | ||
} | ||
|
||
#[cfg(feature = "rfc5114")] | ||
pub fn get_2048_256() -> Result<DH, SslError> { | ||
let dh = unsafe { ffi::DH_get_2048_256() }; | ||
if dh == ptr::null_mut() { | ||
return Err(SslError::get()); | ||
} | ||
Ok(DH(dh)) | ||
} | ||
|
||
pub unsafe fn raw(&self) -> *mut ffi::DH { | ||
let DH(n) = *self; | ||
n | ||
} | ||
|
||
pub unsafe fn raw_ptr(&self) -> *const *mut ffi::DH { | ||
let DH(ref n) = *self; | ||
n | ||
} | ||
} | ||
|
||
impl Drop for DH { | ||
fn drop(&mut self) { | ||
unsafe { | ||
if !self.raw().is_null() { | ||
ffi::DH_free(self.raw()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::DH; | ||
use bn::BigNum; | ||
use ssl::SslContext; | ||
use ssl::SslMethod::Sslv23; | ||
|
||
#[test] | ||
#[cfg(feature = "rfc5114")] | ||
fn test_dh_rfc5114() { | ||
let ctx = SslContext::new(Sslv23).unwrap(); | ||
let dh1 = DH::get_1024_160().unwrap(); | ||
ctx.set_tmp_dh(dh1).unwrap(); | ||
let dh2 = DH::get_2048_224().unwrap(); | ||
ctx.set_tmp_dh(dh2).unwrap(); | ||
let dh3 = DH::get_2048_256().unwrap(); | ||
ctx.set_tmp_dh(dh3).unwrap(); | ||
} | ||
|
||
#[test] | ||
fn test_dh() { | ||
let ctx = SslContext::new(Sslv23).unwrap(); | ||
let p = BigNum::from_hex_str("87A8E61DB4B6663CFFBBD19C651959998CEEF608660DD0F25D2CEED4435E3B00E00DF8F1D61957D4FAF7DF4561B2AA3016C3D91134096FAA3BF4296D830E9A7C209E0C6497517ABD5A8A9D306BCF67ED91F9E6725B4758C022E0B1EF4275BF7B6C5BFC11D45F9088B941F54EB1E59BB8BC39A0BF12307F5C4FDB70C581B23F76B63ACAE1CAA6B7902D52526735488A0EF13C6D9A51BFA4AB3AD8347796524D8EF6A167B5A41825D967E144E5140564251CCACB83E6B486F6B3CA3F7971506026C0B857F689962856DED4010ABD0BE621C3A3960A54E710C375F26375D7014103A4B54330C198AF126116D2276E11715F693877FAD7EF09CADB094AE91E1A1597").unwrap(); | ||
let g = BigNum::from_hex_str("3FB32C9B73134D0B2E77506660EDBD484CA7B18F21EF205407F4793A1A0BA12510DBC15077BE463FFF4FED4AAC0BB555BE3A6C1B0C6B47B1BC3773BF7E8C6F62901228F8C28CBB18A55AE31341000A650196F931C77A57F2DDF463E5E9EC144B777DE62AAAB8A8628AC376D282D6ED3864E67982428EBC831D14348F6F2F9193B5045AF2767164E1DFC967C1FB3F2E55A4BD1BFFE83B9C80D052B985D182EA0ADB2A3B7313D3FE14C8484B1E052588B9B7D2BBD2DF016199ECD06E1557CD0915B3353BBB64E0EC377FD028370DF92B52C7891428CDC67EB6184B523D1DB246C32F63078490F00EF8D647D148D47954515E2327CFEF98C582664B4C0F6CC41659").unwrap(); | ||
let q = BigNum::from_hex_str("8CF83642A709A097B447997640129DA299B1A47D1EB3750BA308B0FE64F5FBD3").unwrap(); | ||
let dh = DH::from_params(p, g, q).unwrap(); | ||
ctx.set_tmp_dh(dh).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ pub mod asn1; | |
pub mod bn; | ||
pub mod bio; | ||
pub mod crypto; | ||
pub mod dh; | ||
pub mod ssl; | ||
pub mod x509; | ||
pub mod nid; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters