3 use foreign_types
::ForeignTypeRef
;
7 use {cvt, cvt_p, init}
;
12 fn drop
= ffi
::DH_free
;
20 to_pem
!(ffi
::PEM_write_bio_DHparams
);
21 to_der
!(ffi
::i2d_DHparams
);
25 pub fn from_params(p
: BigNum
, g
: BigNum
, q
: BigNum
) -> Result
<Dh
, ErrorStack
> {
28 let dh
= Dh(try
!(cvt_p(ffi
::DH_new())));
29 try
!(cvt(compat
::DH_set0_pqg(dh
.0, p
.as_ptr(), q
.as_ptr(), g
.as_ptr())));
30 mem
::forget((p
, g
, q
));
35 from_pem
!(Dh
, ffi
::PEM_read_bio_DHparams
);
36 from_der
!(Dh
, ffi
::d2i_DHparams
);
38 /// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0.
39 #[cfg(any(all(feature = "v102", ossl102), all(feature = "v110", ossl110)))]
40 pub fn get_1024_160() -> Result
<Dh
, ErrorStack
> {
41 unsafe { cvt_p(ffi::DH_get_1024_160()).map(Dh) }
44 /// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0.
45 #[cfg(any(all(feature = "v102", ossl102), all(feature = "v110", ossl110)))]
46 pub fn get_2048_224() -> Result
<Dh
, ErrorStack
> {
47 unsafe { cvt_p(ffi::DH_get_2048_224()).map(Dh) }
50 /// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0.
51 #[cfg(any(all(feature = "v102", ossl102), all(feature = "v110", ossl110)))]
52 pub fn get_2048_256() -> Result
<Dh
, ErrorStack
> {
53 unsafe { cvt_p(ffi::DH_get_2048_256()).map(Dh) }
59 pub use ffi
::DH_set0_pqg
;
68 pub unsafe fn DH_set0_pqg(dh
: *mut ffi
::DH
,
84 use ssl
::{SslMethod, SslContext}
;
87 #[cfg(any(all(feature = "v102", ossl102), all(feature = "v110", ossl110)))]
88 fn test_dh_rfc5114() {
89 let mut ctx
= SslContext
::builder(SslMethod
::tls()).unwrap();
90 let dh1
= Dh
::get_1024_160().unwrap();
91 ctx
.set_tmp_dh(&dh1
).unwrap();
92 let dh2
= Dh
::get_2048_224().unwrap();
93 ctx
.set_tmp_dh(&dh2
).unwrap();
94 let dh3
= Dh
::get_2048_256().unwrap();
95 ctx
.set_tmp_dh(&dh3
).unwrap();
100 let mut ctx
= SslContext
::builder(SslMethod
::tls()).unwrap();
101 let p
= BigNum
::from_hex_str("87A8E61DB4B6663CFFBBD19C651959998CEEF608660DD0F25D2CEED4435\
102 E3B00E00DF8F1D61957D4FAF7DF4561B2AA3016C3D91134096FAA3BF429\
103 6D830E9A7C209E0C6497517ABD5A8A9D306BCF67ED91F9E6725B4758C02\
104 2E0B1EF4275BF7B6C5BFC11D45F9088B941F54EB1E59BB8BC39A0BF1230\
105 7F5C4FDB70C581B23F76B63ACAE1CAA6B7902D52526735488A0EF13C6D9\
106 A51BFA4AB3AD8347796524D8EF6A167B5A41825D967E144E5140564251C\
107 CACB83E6B486F6B3CA3F7971506026C0B857F689962856DED4010ABD0BE\
108 621C3A3960A54E710C375F26375D7014103A4B54330C198AF126116D227\
109 6E11715F693877FAD7EF09CADB094AE91E1A1597")
111 let g
= BigNum
::from_hex_str("3FB32C9B73134D0B2E77506660EDBD484CA7B18F21EF205407F4793A1A0\
112 BA12510DBC15077BE463FFF4FED4AAC0BB555BE3A6C1B0C6B47B1BC3773\
113 BF7E8C6F62901228F8C28CBB18A55AE31341000A650196F931C77A57F2D\
114 DF463E5E9EC144B777DE62AAAB8A8628AC376D282D6ED3864E67982428E\
115 BC831D14348F6F2F9193B5045AF2767164E1DFC967C1FB3F2E55A4BD1BF\
116 FE83B9C80D052B985D182EA0ADB2A3B7313D3FE14C8484B1E052588B9B7\
117 D2BBD2DF016199ECD06E1557CD0915B3353BBB64E0EC377FD028370DF92\
118 B52C7891428CDC67EB6184B523D1DB246C32F63078490F00EF8D647D148\
119 D47954515E2327CFEF98C582664B4C0F6CC41659")
121 let q
= BigNum
::from_hex_str("8CF83642A709A097B447997640129DA299B1A47D1EB3750BA308B0FE64F\
124 let dh
= Dh
::from_params(p
, g
, q
).unwrap();
125 ctx
.set_tmp_dh(&dh
).unwrap();
129 fn test_dh_from_pem() {
130 let mut ctx
= SslContext
::builder(SslMethod
::tls()).unwrap();
131 let params
= include_bytes
!("../test/dhparams.pem");
132 let dh
= Dh
::from_pem(params
).unwrap();
133 ctx
.set_tmp_dh(&dh
).unwrap();
137 fn test_dh_from_der() {
138 let params
= include_bytes
!("../test/dhparams.pem");
139 let dh
= Dh
::from_pem(params
).unwrap();
140 let der
= dh
.to_der().unwrap();
141 Dh
::from_der(&der
).unwrap();