boring_imp/
dh.rs

1use crate::error::ErrorStack;
2use crate::ffi;
3use foreign_types::{ForeignType, ForeignTypeRef};
4use std::mem;
5use std::ptr;
6
7use crate::bn::BigNum;
8use crate::pkey::{HasParams, Params};
9use crate::{cvt, cvt_p};
10
11generic_foreign_type_and_impl_send_sync! {
12    type CType = ffi::DH;
13    fn drop = ffi::DH_free;
14
15    pub struct Dh<T>;
16
17    pub struct DhRef<T>;
18}
19
20impl<T> DhRef<T>
21where
22    T: HasParams,
23{
24    to_pem! {
25        /// Serializes the parameters into a PEM-encoded PKCS#3 DHparameter structure.
26        ///
27        /// The output will have a header of `-----BEGIN DH PARAMETERS-----`.
28        ///
29        /// This corresponds to [`PEM_write_bio_DHparams`].
30        ///
31        /// [`PEM_write_bio_DHparams`]: https://www.openssl.org/docs/manmaster/man3/PEM_write_bio_DHparams.html
32        params_to_pem,
33        ffi::PEM_write_bio_DHparams
34    }
35
36    to_der! {
37        /// Serializes the parameters into a DER-encoded PKCS#3 DHparameter structure.
38        ///
39        /// This corresponds to [`i2d_DHparams`].
40        ///
41        /// [`i2d_DHparams`]: https://www.openssl.org/docs/man1.1.0/crypto/i2d_DHparams.html
42        params_to_der,
43        ffi::i2d_DHparams
44    }
45}
46
47impl Dh<Params> {
48    pub fn from_params(p: BigNum, g: BigNum, q: BigNum) -> Result<Dh<Params>, ErrorStack> {
49        unsafe {
50            let dh = Dh::from_ptr(cvt_p(ffi::DH_new())?);
51            cvt(DH_set0_pqg(dh.0, p.as_ptr(), q.as_ptr(), g.as_ptr()))?;
52            mem::forget((p, g, q));
53            Ok(dh)
54        }
55    }
56
57    from_pem! {
58        /// Deserializes a PEM-encoded PKCS#3 DHpararameters structure.
59        ///
60        /// The input should have a header of `-----BEGIN DH PARAMETERS-----`.
61        ///
62        /// This corresponds to [`PEM_read_bio_DHparams`].
63        ///
64        /// [`PEM_read_bio_DHparams`]: https://www.openssl.org/docs/man1.0.2/crypto/PEM_read_bio_DHparams.html
65        params_from_pem,
66        Dh<Params>,
67        ffi::PEM_read_bio_DHparams
68    }
69
70    from_der! {
71        /// Deserializes a DER-encoded PKCS#3 DHparameters structure.
72        ///
73        /// This corresponds to [`d2i_DHparams`].
74        ///
75        /// [`d2i_DHparams`]: https://www.openssl.org/docs/man1.1.0/crypto/d2i_DHparams.html
76        params_from_der,
77        Dh<Params>,
78        ffi::d2i_DHparams,
79        ::libc::c_long
80    }
81}
82
83use crate::ffi::DH_set0_pqg;
84
85#[cfg(test)]
86mod tests {
87    use crate::bn::BigNum;
88    use crate::dh::Dh;
89    use crate::ssl::{SslContext, SslMethod};
90
91    #[test]
92    fn test_dh() {
93        let mut ctx = SslContext::builder(SslMethod::tls()).unwrap();
94        let p = BigNum::from_hex_str(
95            "87A8E61DB4B6663CFFBBD19C651959998CEEF608660DD0F25D2CEED4435E3B00E00DF8F1D61957D4FAF7DF\
96             4561B2AA3016C3D91134096FAA3BF4296D830E9A7C209E0C6497517ABD5A8A9D306BCF67ED91F9E6725B47\
97             58C022E0B1EF4275BF7B6C5BFC11D45F9088B941F54EB1E59BB8BC39A0BF12307F5C4FDB70C581B23F76B6\
98             3ACAE1CAA6B7902D52526735488A0EF13C6D9A51BFA4AB3AD8347796524D8EF6A167B5A41825D967E144E5\
99             140564251CCACB83E6B486F6B3CA3F7971506026C0B857F689962856DED4010ABD0BE621C3A3960A54E710\
100             C375F26375D7014103A4B54330C198AF126116D2276E11715F693877FAD7EF09CADB094AE91E1A1597",
101        ).unwrap();
102        let g = BigNum::from_hex_str(
103            "3FB32C9B73134D0B2E77506660EDBD484CA7B18F21EF205407F4793A1A0BA12510DBC15077BE463FFF4FED\
104             4AAC0BB555BE3A6C1B0C6B47B1BC3773BF7E8C6F62901228F8C28CBB18A55AE31341000A650196F931C77A\
105             57F2DDF463E5E9EC144B777DE62AAAB8A8628AC376D282D6ED3864E67982428EBC831D14348F6F2F9193B5\
106             045AF2767164E1DFC967C1FB3F2E55A4BD1BFFE83B9C80D052B985D182EA0ADB2A3B7313D3FE14C8484B1E\
107             052588B9B7D2BBD2DF016199ECD06E1557CD0915B3353BBB64E0EC377FD028370DF92B52C7891428CDC67E\
108             B6184B523D1DB246C32F63078490F00EF8D647D148D47954515E2327CFEF98C582664B4C0F6CC41659",
109        ).unwrap();
110        let q = BigNum::from_hex_str(
111            "8CF83642A709A097B447997640129DA299B1A47D1EB3750BA308B0FE64F5FBD3",
112        )
113        .unwrap();
114        let dh = Dh::from_params(p, g, q).unwrap();
115        ctx.set_tmp_dh(&dh).unwrap();
116    }
117
118    #[test]
119    fn test_dh_from_pem() {
120        let mut ctx = SslContext::builder(SslMethod::tls()).unwrap();
121        let params = include_bytes!("../test/dhparams.pem");
122        let dh = Dh::params_from_pem(params).unwrap();
123        ctx.set_tmp_dh(&dh).unwrap();
124    }
125
126    #[test]
127    fn test_dh_from_der() {
128        let params = include_bytes!("../test/dhparams.pem");
129        let dh = Dh::params_from_pem(params).unwrap();
130        let der = dh.params_to_der().unwrap();
131        Dh::params_from_der(&der).unwrap();
132    }
133}