]> git.proxmox.com Git - rustc.git/blob - src/vendor/openssl/src/dh.rs
New upstream version 1.19.0+dfsg1
[rustc.git] / src / vendor / openssl / src / dh.rs
1 use error::ErrorStack;
2 use ffi;
3 use foreign_types::ForeignTypeRef;
4 use std::mem;
5 use std::ptr;
6
7 use {cvt, cvt_p, init};
8 use bn::BigNum;
9
10 foreign_type! {
11 type CType = ffi::DH;
12 fn drop = ffi::DH_free;
13
14 pub struct Dh;
15
16 pub struct DhRef;
17 }
18
19 impl DhRef {
20 to_pem!(ffi::PEM_write_bio_DHparams);
21 to_der!(ffi::i2d_DHparams);
22 }
23
24 impl Dh {
25 pub fn from_params(p: BigNum, g: BigNum, q: BigNum) -> Result<Dh, ErrorStack> {
26 unsafe {
27 init();
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));
31 Ok(dh)
32 }
33 }
34
35 from_pem!(Dh, ffi::PEM_read_bio_DHparams);
36 from_der!(Dh, ffi::d2i_DHparams);
37
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) }
42 }
43
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) }
48 }
49
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) }
54 }
55 }
56
57 #[cfg(ossl110)]
58 mod compat {
59 pub use ffi::DH_set0_pqg;
60 }
61
62 #[cfg(ossl10x)]
63 #[allow(bad_style)]
64 mod compat {
65 use ffi;
66 use libc::c_int;
67
68 pub unsafe fn DH_set0_pqg(dh: *mut ffi::DH,
69 p: *mut ffi::BIGNUM,
70 q: *mut ffi::BIGNUM,
71 g: *mut ffi::BIGNUM)
72 -> c_int {
73 (*dh).p = p;
74 (*dh).q = q;
75 (*dh).g = g;
76 1
77 }
78 }
79
80 #[cfg(test)]
81 mod tests {
82 use dh::Dh;
83 use bn::BigNum;
84 use ssl::{SslMethod, SslContext};
85
86 #[test]
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();
96 }
97
98 #[test]
99 fn test_dh() {
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")
110 .unwrap();
111 let g = BigNum::from_hex_str("3FB32C9B73134D0B2E77506660EDBD484CA7B18F21EF205407F4793A1A0\
112 BA12510DBC15077BE463FFF4FED4AAC0BB555BE3A6C1B0C6B47B1BC3773\
113 BF7E8C6F62901228F8C28CBB18A55AE31341000A650196F931C77A57F2D\
114 DF463E5E9EC144B777DE62AAAB8A8628AC376D282D6ED3864E67982428E\
115 BC831D14348F6F2F9193B5045AF2767164E1DFC967C1FB3F2E55A4BD1BF\
116 FE83B9C80D052B985D182EA0ADB2A3B7313D3FE14C8484B1E052588B9B7\
117 D2BBD2DF016199ECD06E1557CD0915B3353BBB64E0EC377FD028370DF92\
118 B52C7891428CDC67EB6184B523D1DB246C32F63078490F00EF8D647D148\
119 D47954515E2327CFEF98C582664B4C0F6CC41659")
120 .unwrap();
121 let q = BigNum::from_hex_str("8CF83642A709A097B447997640129DA299B1A47D1EB3750BA308B0FE64F\
122 5FBD3")
123 .unwrap();
124 let dh = Dh::from_params(p, g, q).unwrap();
125 ctx.set_tmp_dh(&dh).unwrap();
126 }
127
128 #[test]
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();
134 }
135
136 #[test]
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();
142 }
143 }