1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use ffi;
use std::fmt;
use ssl::error::{SslError, StreamError};
use std::ptr;
use std::io::{self, Read, Write};
use libc::c_int;

use bn::BigNum;
use bio::MemBio;
use crypto::HashTypeInternals;
use crypto::hash;

pub struct RSA(*mut ffi::RSA);

impl Drop for RSA {
    fn drop(&mut self) {
        unsafe {
            ffi::RSA_free(self.0);
        }
    }
}

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))
        }
    }

    pub fn from_private_components(n: BigNum,
                                   e: BigNum,
                                   d: BigNum,
                                   p: BigNum,
                                   q: BigNum,
                                   dp: BigNum,
                                   dq: BigNum,
                                   qi: BigNum)
                                   -> Result<RSA, SslError> {
        unsafe {
            let rsa = try_ssl_null!(ffi::RSA_new());
            (*rsa).n = n.into_raw();
            (*rsa).e = e.into_raw();
            (*rsa).d = d.into_raw();
            (*rsa).p = p.into_raw();
            (*rsa).q = q.into_raw();
            (*rsa).dmp1 = dp.into_raw();
            (*rsa).dmq1 = dq.into_raw();
            (*rsa).iqmp = qi.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
    {
        let mut mem_bio = try!(MemBio::new());
        try!(io::copy(reader, &mut mem_bio).map_err(StreamError));

        unsafe {
            let rsa = try_ssl_null!(ffi::PEM_read_bio_RSAPrivateKey(mem_bio.get_handle(),
                                                                    ptr::null_mut(),
                                                                    None,
                                                                    ptr::null_mut()));
            Ok(RSA(rsa))
        }
    }

    /// Writes an RSA private key as unencrypted PEM formatted data
    pub fn private_key_to_pem<W>(&self, writer: &mut W) -> Result<(), SslError>
        where W: Write
    {
        let mut mem_bio = try!(MemBio::new());

        let result = unsafe {
            ffi::PEM_write_bio_RSAPrivateKey(mem_bio.get_handle(),
                                             self.0,
                                             ptr::null(),
                                             ptr::null_mut(),
                                             0,
                                             None,
                                             ptr::null_mut())
        };

        if result == 1 {
            try!(io::copy(&mut mem_bio, writer).map_err(StreamError));

            Ok(())
        } else {
            Err(SslError::OpenSslErrors(vec![]))
        }
    }

    /// Reads an RSA public key from PEM formatted data.
    pub fn public_key_from_pem<R>(reader: &mut R) -> Result<RSA, SslError>
        where R: Read
    {
        let mut mem_bio = try!(MemBio::new());
        try!(io::copy(reader, &mut mem_bio).map_err(StreamError));

        unsafe {
            let rsa = try_ssl_null!(ffi::PEM_read_bio_RSA_PUBKEY(mem_bio.get_handle(),
                                                                 ptr::null_mut(),
                                                                 None,
                                                                 ptr::null_mut()));
            Ok(RSA(rsa))
        }
    }

    /// Writes an RSA public key as PEM formatted data
    pub fn public_key_to_pem<W>(&self, writer: &mut W) -> Result<(), SslError>
        where W: Write
    {
        let mut mem_bio = try!(MemBio::new());

        let result = unsafe { ffi::PEM_write_bio_RSA_PUBKEY(mem_bio.get_handle(), self.0) };

        if result == 1 {
            try!(io::copy(&mut mem_bio, writer).map_err(StreamError));

            Ok(())
        } else {
            Err(SslError::OpenSslErrors(vec![]))
        }
    }

    pub fn size(&self) -> Result<u32, SslError> {
        if self.has_n() {
            unsafe { Ok(ffi::RSA_size(self.0) as u32) }
        } else {
            Err(SslError::OpenSslErrors(vec![]))
        }
    }

    pub fn sign(&self, hash: hash::Type, message: &[u8]) -> Result<Vec<u8>, SslError> {
        let k_len = try!(self.size());
        let mut sig = vec![0;k_len as usize];
        let mut sig_len = k_len;

        unsafe {
            let result = ffi::RSA_sign(hash.as_nid() as c_int,
                                       message.as_ptr(),
                                       message.len() as u32,
                                       sig.as_mut_ptr(),
                                       &mut sig_len,
                                       self.0);
            assert!(sig_len == k_len);

            if result == 1 {
                Ok(sig)
            } else {
                Err(SslError::OpenSslErrors(vec![]))
            }
        }
    }

    pub fn verify(&self, hash: hash::Type, message: &[u8], sig: &[u8]) -> Result<bool, SslError> {
        unsafe {
            let result = ffi::RSA_verify(hash.as_nid() as c_int,
                                         message.as_ptr(),
                                         message.len() as u32,
                                         sig.as_ptr(),
                                         sig.len() as u32,
                                         self.0);

            Ok(result == 1)
        }
    }

    pub fn as_ptr(&self) -> *mut ffi::RSA {
        self.0
    }

    // The following getters are unsafe, since BigNum::new_from_ffi fails upon null pointers
    pub fn n(&self) -> Result<BigNum, SslError> {
        unsafe { BigNum::new_from_ffi((*self.0).n) }
    }

    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) }
    }

    pub fn e(&self) -> Result<BigNum, SslError> {
        unsafe { BigNum::new_from_ffi((*self.0).e) }
    }

    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) }
    }

    pub fn q(&self) -> Result<BigNum, SslError> {
        unsafe { BigNum::new_from_ffi((*self.0).q) }
    }
}

impl fmt::Debug for RSA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "RSA")
    }
}

#[cfg(test)]
mod test {
    use std::fs::File;
    use std::io::Write;
    use super::*;
    use crypto::hash::*;

    fn signing_input_rs256() -> Vec<u8> {
        vec![101, 121, 74, 104, 98, 71, 99, 105, 79, 105, 74, 83, 85, 122, 73, 49, 78, 105, 74, 57,
             46, 101, 121, 74, 112, 99, 51, 77, 105, 79, 105, 74, 113, 98, 50, 85, 105, 76, 65, 48,
             75, 73, 67, 74, 108, 101, 72, 65, 105, 79, 106, 69, 122, 77, 68, 65, 52, 77, 84, 107,
             122, 79, 68, 65, 115, 68, 81, 111, 103, 73, 109, 104, 48, 100, 72, 65, 54, 76, 121,
             57, 108, 101, 71, 70, 116, 99, 71, 120, 108, 76, 109, 78, 118, 98, 83, 57, 112, 99,
             49, 57, 121, 98, 50, 57, 48, 73, 106, 112, 48, 99, 110, 86, 108, 102, 81]
    }

    fn signature_rs256() -> Vec<u8> {
        vec![112, 46, 33, 137, 67, 232, 143, 209, 30, 181, 216, 45, 191, 120, 69, 243, 65, 6, 174,
             27, 129, 255, 247, 115, 17, 22, 173, 209, 113, 125, 131, 101, 109, 66, 10, 253, 60,
             150, 238, 221, 115, 162, 102, 62, 81, 102, 104, 123, 0, 11, 135, 34, 110, 1, 135, 237,
             16, 115, 249, 69, 229, 130, 173, 252, 239, 22, 216, 90, 121, 142, 232, 198, 109, 219,
             61, 184, 151, 91, 23, 208, 148, 2, 190, 237, 213, 217, 217, 112, 7, 16, 141, 178, 129,
             96, 213, 248, 4, 12, 167, 68, 87, 98, 184, 31, 190, 127, 249, 217, 46, 10, 231, 111,
             36, 242, 91, 51, 187, 230, 244, 74, 230, 30, 177, 4, 10, 203, 32, 4, 77, 62, 249, 18,
             142, 212, 1, 48, 121, 91, 212, 189, 59, 65, 238, 202, 208, 102, 171, 101, 25, 129,
             253, 228, 141, 247, 127, 55, 45, 195, 139, 159, 175, 221, 59, 239, 177, 139, 93, 163,
             204, 60, 46, 176, 47, 158, 58, 65, 214, 18, 202, 173, 21, 145, 18, 115, 160, 95, 35,
             185, 232, 56, 250, 175, 132, 157, 105, 132, 41, 239, 90, 30, 136, 121, 130, 54, 195,
             212, 14, 96, 69, 34, 165, 68, 200, 242, 122, 122, 45, 184, 6, 99, 209, 108, 247, 202,
             234, 86, 222, 64, 92, 178, 33, 90, 69, 178, 194, 85, 102, 181, 90, 193, 167, 72, 160,
             112, 223, 200, 163, 42, 70, 149, 67, 208, 25, 238, 251, 71]
    }

    #[test]
    pub fn test_sign() {
        let mut buffer = File::open("test/rsa.pem").unwrap();
        let private_key = RSA::private_key_from_pem(&mut buffer).unwrap();

        let mut sha = Hasher::new(Type::SHA256);
        sha.write_all(&signing_input_rs256()).unwrap();
        let digest = sha.finish();

        let result = private_key.sign(Type::SHA256, &digest).unwrap();

        assert_eq!(result, signature_rs256());
    }

    #[test]
    pub fn test_verify() {
        let mut buffer = File::open("test/rsa.pem.pub").unwrap();
        let public_key = RSA::public_key_from_pem(&mut buffer).unwrap();

        let mut sha = Hasher::new(Type::SHA256);
        sha.write_all(&signing_input_rs256()).unwrap();
        let digest = sha.finish();

        let result = public_key.verify(Type::SHA256, &digest, &signature_rs256()).unwrap();

        assert!(result);
    }
}