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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
use libc::{c_int, c_uint, c_ulong};
use std::io;
use std::io::prelude::*;
use std::iter::repeat;
use std::mem;
use std::ptr;
use bio::MemBio;
use crypto::hash;
use crypto::hash::Type as HashType;
use ffi;
use ssl::error::{SslError, StreamError};
use crypto::rsa::RSA;

#[derive(Copy, Clone)]
pub enum Parts {
    Neither,
    Public,
    Both,
}

/// Represents a role an asymmetric key might be appropriate for.
#[derive(Copy, Clone)]
pub enum Role {
    Encrypt,
    Decrypt,
    Sign,
    Verify,
}

/// Type of encryption padding to use.
#[derive(Copy, Clone)]
pub enum EncryptionPadding {
    OAEP,
    PKCS1v15,
}

fn openssl_padding_code(padding: EncryptionPadding) -> c_int {
    match padding {
        EncryptionPadding::OAEP => 4,
        EncryptionPadding::PKCS1v15 => 1,
    }
}

fn openssl_hash_nid(hash: HashType) -> c_int {
    match hash {
        HashType::MD5 => 4,   // NID_md5,
        HashType::SHA1 => 64,  // NID_sha1
        HashType::SHA224 => 675, // NID_sha224
        HashType::SHA256 => 672, // NID_sha256
        HashType::SHA384 => 673, // NID_sha384
        HashType::SHA512 => 674, // NID_sha512
        HashType::RIPEMD160 => 117, // NID_ripemd160
    }
}

extern "C" {
    fn rust_EVP_PKEY_clone(pkey: *mut ffi::EVP_PKEY);
}

pub struct PKey {
    evp: *mut ffi::EVP_PKEY,
    parts: Parts,
}

unsafe impl Send for PKey {}
unsafe impl Sync for PKey {}

/// Represents a public key, optionally with a private key attached.
impl PKey {
    pub fn new() -> PKey {
        unsafe {
            ffi::init();

            PKey {
                evp: ffi::EVP_PKEY_new(),
                parts: Parts::Neither,
            }
        }
    }

    pub fn from_handle(handle: *mut ffi::EVP_PKEY, parts: Parts) -> PKey {
        ffi::init();
        assert!(!handle.is_null());

        PKey {
            evp: handle,
            parts: parts,
        }
    }

    /// Reads private key from PEM, takes ownership of handle
    pub fn private_key_from_pem<R>(reader: &mut R) -> Result<PKey, SslError>
        where R: Read
    {
        let mut mem_bio = try!(MemBio::new());
        try!(io::copy(reader, &mut mem_bio).map_err(StreamError));

        unsafe {
            let evp = try_ssl_null!(ffi::PEM_read_bio_PrivateKey(mem_bio.get_handle(),
                                                                 ptr::null_mut(),
                                                                 None,
                                                                 ptr::null_mut()));
            Ok(PKey {
                evp: evp as *mut ffi::EVP_PKEY,
                parts: Parts::Both,
            })
        }
    }

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

        unsafe {
            let evp = try_ssl_null!(ffi::PEM_read_bio_PUBKEY(mem_bio.get_handle(),
                                                             ptr::null_mut(),
                                                             None,
                                                             ptr::null_mut()));
            Ok(PKey {
                evp: evp as *mut ffi::EVP_PKEY,
                parts: Parts::Public,
            })
        }
    }

    /// Reads an RSA private key from PEM, takes ownership of handle
    pub fn private_rsa_key_from_pem<R>(reader: &mut R) -> Result<PKey, SslError>
    where R: Read
    {
        let rsa = try!(RSA::private_key_from_pem(reader));
        unsafe {
            let evp = try_ssl_null!(ffi::EVP_PKEY_new());
            try_ssl!(ffi::EVP_PKEY_set1_RSA(evp, rsa.as_ptr()));

            Ok(PKey {
                evp: evp,
                parts: Parts::Public,
            })
        }
    }

    /// Reads an RSA public key from PEM, takes ownership of handle
    pub fn public_rsa_key_from_pem<R>(reader: &mut R) -> Result<PKey, SslError>
    where R: Read
    {
        let rsa = try!(RSA::public_key_from_pem(reader));
        unsafe {
            let evp = try_ssl_null!(ffi::EVP_PKEY_new());
            try_ssl!(ffi::EVP_PKEY_set1_RSA(evp, rsa.as_ptr()));

            Ok(PKey {
                evp: evp,
                parts: Parts::Public,
            })
        }
    }

    fn _tostr(&self, f: unsafe extern "C" fn(*mut ffi::RSA, *const *mut u8) -> c_int) -> Vec<u8> {
        unsafe {
            let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
            let len = f(rsa, ptr::null());
            if len < 0 as c_int {
                return vec![];
            }
            let mut s = repeat(0u8).take(len as usize).collect::<Vec<_>>();

            let r = f(rsa, &s.as_mut_ptr());
            ffi::RSA_free(rsa);

            s.truncate(r as usize);
            s
        }
    }

    fn _fromstr(&mut self,
                s: &[u8],
                f: unsafe extern "C" fn(*const *mut ffi::RSA, *const *const u8, c_uint)
                                        -> *mut ffi::RSA)
                -> bool {
        unsafe {
            let rsa = ptr::null_mut();
            f(&rsa, &s.as_ptr(), s.len() as c_uint);
            if !rsa.is_null() {
                ffi::EVP_PKEY_set1_RSA(self.evp, rsa) == 1
            } else {
                false
            }
        }
    }

    pub fn gen(&mut self, keysz: usize) {
        unsafe {
            let rsa = ffi::RSA_generate_key(keysz as c_int,
                                            65537 as c_ulong,
                                            ptr::null(),
                                            ptr::null());

            // XXX: 6 == NID_rsaEncryption
            ffi::EVP_PKEY_assign(self.evp, 6 as c_int, mem::transmute(rsa));

            self.parts = Parts::Both;
        }
    }

    /// assign RSA key to this pkey
    pub fn set_rsa(&mut self, rsa: &RSA) {
        unsafe {
            // this needs to be a reference as the set1_RSA ups the reference count
            let rsa_ptr = rsa.as_ptr();
            if ffi::EVP_PKEY_set1_RSA(self.evp, rsa_ptr) == 1 {
                if rsa.has_e() && rsa.has_n() {
                    self.parts = Parts::Public;
                }
            }
        }
    }

    /// get a reference to the interal RSA key for direct access to the key components
    pub fn get_rsa(&self) -> RSA {
        unsafe {
            let evp_pkey: *mut ffi::EVP_PKEY = self.evp;
            // this is safe as the ffi increments a reference counter to the internal key
            RSA::from_raw(ffi::EVP_PKEY_get1_RSA(evp_pkey))
        }
    }

    /**
     * Returns a DER serialized form of the public key, suitable for load_pub().
     */
    pub fn save_pub(&self) -> Vec<u8> {
        self._tostr(ffi::i2d_RSA_PUBKEY)
    }

    /**
     * Loads a DER serialized form of the public key, as produced by save_pub().
     */
    pub fn load_pub(&mut self, s: &[u8]) {
        if self._fromstr(s, ffi::d2i_RSA_PUBKEY) {
            self.parts = Parts::Public;
        }
    }

    /**
     * Returns a serialized form of the public and private keys, suitable for
     * load_priv().
     */
    pub fn save_priv(&self) -> Vec<u8> {
        self._tostr(ffi::i2d_RSAPrivateKey)
    }
    /**
     * Loads a serialized form of the public and private keys, as produced by
     * save_priv().
     */
    pub fn load_priv(&mut self, s: &[u8]) {
        if self._fromstr(s, ffi::d2i_RSAPrivateKey) {
            self.parts = Parts::Both;
        }
    }

    /// Stores private key as a PEM
    // FIXME: also add password and encryption
    pub fn write_pem<W: Write>(&self,
                               writer: &mut W /* , password: Option<String> */)
                               -> Result<(), SslError> {
        let mut mem_bio = try!(MemBio::new());
        unsafe {
            try_ssl!(ffi::PEM_write_bio_PrivateKey(mem_bio.get_handle(),
                                                   self.evp,
                                                   ptr::null(),
                                                   ptr::null_mut(),
                                                   -1,
                                                   None,
                                                   ptr::null_mut()));

        }
        let mut buf = vec![];
        try!(mem_bio.read_to_end(&mut buf).map_err(StreamError));
        writer.write_all(&buf).map_err(StreamError)
    }

    /// Stores public key as a PEM
    pub fn write_pub_pem<W: Write>(&self,
                                   writer: &mut W /* , password: Option<String> */)
                                   -> Result<(), SslError> {
        let mut mem_bio = try!(MemBio::new());
        unsafe { try_ssl!(ffi::PEM_write_bio_PUBKEY(mem_bio.get_handle(), self.evp)) }
        let mut buf = vec![];
        try!(mem_bio.read_to_end(&mut buf).map_err(StreamError));
        writer.write_all(&buf).map_err(StreamError)
    }

    /**
     * Returns the size of the public key modulus.
     */
    pub fn size(&self) -> usize {
        unsafe {
            let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
            if rsa.is_null() {
                0
            } else {
                ffi::RSA_size(rsa) as usize
            }
        }
    }

    /**
     * Returns whether this pkey object can perform the specified role.
     */
    pub fn can(&self, r: Role) -> bool {
        match r {
            Role::Encrypt => {
                match self.parts {
                    Parts::Neither => false,
                    _ => true,
                }
            }
            Role::Verify => {
                match self.parts {
                    Parts::Neither => false,
                    _ => true,
                }
            }
            Role::Decrypt => {
                match self.parts {
                    Parts::Both => true,
                    _ => false,
                }
            }
            Role::Sign => {
                match self.parts {
                    Parts::Both => true,
                    _ => false,
                }
            }
        }
    }

    /**
     * Returns the maximum amount of data that can be encrypted by an encrypt()
     * call.
     */
    pub fn max_data(&self) -> usize {
        unsafe {
            let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
            if rsa.is_null() {
                return 0;
            }
            let len = ffi::RSA_size(rsa);

            // 41 comes from RSA_public_encrypt(3) for OAEP
            len as usize - 41
        }
    }

    pub fn private_encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> Vec<u8> {
        unsafe {
            let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
            if rsa.is_null() {
                panic!("Could not get RSA key for encryption");
            }
            let len = ffi::RSA_size(rsa);

            assert!(s.len() < self.max_data());

            let mut r = repeat(0u8).take(len as usize + 1).collect::<Vec<_>>();

            let rv = ffi::RSA_private_encrypt(s.len() as c_int,
                                              s.as_ptr(),
                                              r.as_mut_ptr(),
                                              rsa,
                                              openssl_padding_code(padding));

            if rv < 0 as c_int {
                // println!("{:?}", SslError::get());
                vec![]
            } else {
                r.truncate(rv as usize);
                r
            }
        }
    }

    pub fn public_encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> Vec<u8> {
        unsafe {
            let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
            if rsa.is_null() {
                panic!("Could not get RSA key for encryption");
            }
            let len = ffi::RSA_size(rsa);

            assert!(s.len() < self.max_data());

            let mut r = repeat(0u8).take(len as usize + 1).collect::<Vec<_>>();

            let rv = ffi::RSA_public_encrypt(s.len() as c_int,
                                             s.as_ptr(),
                                             r.as_mut_ptr(),
                                             rsa,
                                             openssl_padding_code(padding));

            if rv < 0 as c_int {
                vec![]
            } else {
                r.truncate(rv as usize);
                r
            }
        }
    }

    pub fn private_decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> Vec<u8> {
        unsafe {
            let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
            if rsa.is_null() {
                panic!("Could not get RSA key for decryption");
            }
            let len = ffi::RSA_size(rsa);

            assert_eq!(s.len() as c_int, ffi::RSA_size(rsa));

            let mut r = repeat(0u8).take(len as usize + 1).collect::<Vec<_>>();

            let rv = ffi::RSA_private_decrypt(s.len() as c_int,
                                              s.as_ptr(),
                                              r.as_mut_ptr(),
                                              rsa,
                                              openssl_padding_code(padding));

            if rv < 0 as c_int {
                vec![]
            } else {
                r.truncate(rv as usize);
                r
            }
        }
    }

    pub fn public_decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> Vec<u8> {
        unsafe {
            let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
            if rsa.is_null() {
                panic!("Could not get RSA key for decryption");
            }
            let len = ffi::RSA_size(rsa);

            assert_eq!(s.len() as c_int, ffi::RSA_size(rsa));

            let mut r = repeat(0u8).take(len as usize + 1).collect::<Vec<_>>();

            let rv = ffi::RSA_public_decrypt(s.len() as c_int,
                                             s.as_ptr(),
                                             r.as_mut_ptr(),
                                             rsa,
                                             openssl_padding_code(padding));

            if rv < 0 as c_int {
                vec![]
            } else {
                r.truncate(rv as usize);
                r
            }
        }
    }

    /**
     * Encrypts data with the public key, using OAEP padding, returning the encrypted data. The
     * supplied data must not be larger than max_data().
     */
    pub fn encrypt(&self, s: &[u8]) -> Vec<u8> {
        self.public_encrypt_with_padding(s, EncryptionPadding::OAEP)
    }

    /**
     * Encrypts data with the public key, using provided padding, returning the encrypted data. The
     * supplied data must not be larger than max_data().
     */
    pub fn encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> Vec<u8> {
        self.public_encrypt_with_padding(s, padding)
    }

    /**
     * Encrypts data with the public key, using OAEP padding, returning the encrypted data. The
     * supplied data must not be larger than max_data().
     */
    pub fn public_encrypt(&self, s: &[u8]) -> Vec<u8> {
        self.public_encrypt_with_padding(s, EncryptionPadding::OAEP)
    }

    /**
     * Decrypts data with the public key, using PKCS1v15 padding, returning the decrypted data.
     */
    pub fn public_decrypt(&self, s: &[u8]) -> Vec<u8> {
        self.public_decrypt_with_padding(s, EncryptionPadding::PKCS1v15)
    }

    /**
     * Decrypts data with the private key, expecting OAEP padding, returning the decrypted data.
     */
    pub fn decrypt(&self, s: &[u8]) -> Vec<u8> {
        self.private_decrypt_with_padding(s, EncryptionPadding::OAEP)
    }

    /**
     * Decrypts data with the private key, using provided padding, returning the encrypted data. The
     * supplied data must not be larger than max_data().
     */
    pub fn decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> Vec<u8> {
        self.private_decrypt_with_padding(s, padding)
    }

    /**
     * Decrypts data with the private key, expecting OAEP padding, returning the decrypted data.
     */
    pub fn private_decrypt(&self, s: &[u8]) -> Vec<u8> {
        self.private_decrypt_with_padding(s, EncryptionPadding::OAEP)
    }

    /**
     * Encrypts data with the private key, using PKCS1v15 padding, returning the encrypted data. The
     * supplied data must not be larger than max_data().
     */
    pub fn private_encrypt(&self, s: &[u8]) -> Vec<u8> {
        self.private_encrypt_with_padding(s, EncryptionPadding::PKCS1v15)
    }

    /**
     * Signs data, using OpenSSL's default scheme and adding sha256 ASN.1 information to the
     * signature.
     * The bytes to sign must be the result of a sha256 hashing;
     * returns the signature.
     */
    pub fn sign(&self, s: &[u8]) -> Vec<u8> {
        self.sign_with_hash(s, HashType::SHA256)
    }

    /**
     * Verifies a signature s (using OpenSSL's default scheme and sha256) on the SHA256 hash of a
     * message.
     * Returns true if the signature is valid, and false otherwise.
     */
    pub fn verify(&self, h: &[u8], s: &[u8]) -> bool {
        self.verify_with_hash(h, s, HashType::SHA256)
    }

    /**
     * Signs data, using OpenSSL's default scheme and add ASN.1 information for the given hash type to the
     * signature.
     * The bytes to sign must be the result of this type of hashing;
     * returns the signature.
     */
    pub fn sign_with_hash(&self, s: &[u8], hash: hash::Type) -> Vec<u8> {
        unsafe {
            let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
            if rsa.is_null() {
                panic!("Could not get RSA key for signing");
            }
            let len = ffi::RSA_size(rsa);
            let mut r = repeat(0u8).take(len as usize + 1).collect::<Vec<_>>();

            let mut len = 0;
            let rv = ffi::RSA_sign(openssl_hash_nid(hash),
                                   s.as_ptr(),
                                   s.len() as c_uint,
                                   r.as_mut_ptr(),
                                   &mut len,
                                   rsa);

            if rv < 0 as c_int {
                vec![]
            } else {
                r.truncate(len as usize);
                r
            }
        }
    }

    pub fn verify_with_hash(&self, h: &[u8], s: &[u8], hash: hash::Type) -> bool {
        unsafe {
            let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
            if rsa.is_null() {
                panic!("Could not get RSA key for verification");
            }

            let rv = ffi::RSA_verify(openssl_hash_nid(hash),
                                     h.as_ptr(),
                                     h.len() as c_uint,
                                     s.as_ptr(),
                                     s.len() as c_uint,
                                     rsa);

            rv == 1 as c_int
        }
    }

    pub unsafe fn get_handle(&self) -> *mut ffi::EVP_PKEY {
        return self.evp;
    }

    pub fn public_eq(&self, other: &PKey) -> bool {
        unsafe { ffi::EVP_PKEY_cmp(self.evp, other.evp) == 1 }
    }
}

impl Drop for PKey {
    fn drop(&mut self) {
        unsafe {
            ffi::EVP_PKEY_free(self.evp);
        }
    }
}

impl Clone for PKey {
    fn clone(&self) -> Self {
        unsafe {
            rust_EVP_PKEY_clone(self.evp);
        }

        PKey::from_handle(self.evp, self.parts)
    }
}

#[cfg(test)]
mod tests {
    use std::path::Path;
    use std::fs::File;
    use crypto::hash::Type::{MD5, SHA1};
    use crypto::rsa::RSA;

    #[test]
    fn test_gen_pub() {
        let mut k0 = super::PKey::new();
        let mut k1 = super::PKey::new();
        k0.gen(512);
        k1.load_pub(&k0.save_pub());
        assert_eq!(k0.save_pub(), k1.save_pub());
        assert!(k0.public_eq(&k1));
        assert_eq!(k0.size(), k1.size());
        assert!(k0.can(super::Role::Encrypt));
        assert!(k0.can(super::Role::Decrypt));
        assert!(k0.can(super::Role::Verify));
        assert!(k0.can(super::Role::Sign));
        assert!(k1.can(super::Role::Encrypt));
        assert!(!k1.can(super::Role::Decrypt));
        assert!(k1.can(super::Role::Verify));
        assert!(!k1.can(super::Role::Sign));
    }

    #[test]
    fn test_gen_priv() {
        let mut k0 = super::PKey::new();
        let mut k1 = super::PKey::new();
        k0.gen(512);
        k1.load_priv(&k0.save_priv());
        assert_eq!(k0.save_priv(), k1.save_priv());
        assert!(k0.public_eq(&k1));
        assert_eq!(k0.size(), k1.size());
        assert!(k0.can(super::Role::Encrypt));
        assert!(k0.can(super::Role::Decrypt));
        assert!(k0.can(super::Role::Verify));
        assert!(k0.can(super::Role::Sign));
        assert!(k1.can(super::Role::Encrypt));
        assert!(k1.can(super::Role::Decrypt));
        assert!(k1.can(super::Role::Verify));
        assert!(k1.can(super::Role::Sign));
    }

    #[test]
    fn test_private_key_from_pem() {
        let key_path = Path::new("test/key.pem");
        let mut file = File::open(&key_path)
                           .ok()
                           .expect("Failed to open `test/key.pem`");

        super::PKey::private_key_from_pem(&mut file).unwrap();
    }

    #[test]
    fn test_public_key_from_pem() {
        let key_path = Path::new("test/key.pem.pub");
        let mut file = File::open(&key_path)
                           .ok()
                           .expect("Failed to open `test/key.pem.pub`");

        super::PKey::public_key_from_pem(&mut file).unwrap();
    }

    #[test]
    fn test_private_rsa_key_from_pem() {
        let key_path = Path::new("test/key.pem");
        let mut file = File::open(&key_path)
                            .ok()
                            .expect("Failed to open `test/key.pem`");

        super::PKey::private_rsa_key_from_pem(&mut file).unwrap();
    }

    #[test]
    fn test_public_rsa_key_from_pem() {
        let key_path = Path::new("test/key.pem.pub");
        let mut file = File::open(&key_path)
                            .ok()
                            .expect("Failed to open `test/key.pem.pub`");

        super::PKey::public_rsa_key_from_pem(&mut file).unwrap();
    }

    #[test]
    fn test_private_encrypt() {
        let mut k0 = super::PKey::new();
        let mut k1 = super::PKey::new();
        let msg = vec![0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
        k0.gen(512);
        k1.load_pub(&k0.save_pub());
        let emsg = k0.private_encrypt(&msg);
        let dmsg = k1.public_decrypt(&emsg);
        assert!(msg == dmsg);
    }

    #[test]
    fn test_public_encrypt() {
        let mut k0 = super::PKey::new();
        let mut k1 = super::PKey::new();
        let msg = vec![0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
        k0.gen(512);
        k1.load_pub(&k0.save_pub());
        let emsg = k1.public_encrypt(&msg);
        let dmsg = k0.private_decrypt(&emsg);
        assert!(msg == dmsg);
    }

    #[test]
    fn test_public_encrypt_pkcs() {
        let mut k0 = super::PKey::new();
        let mut k1 = super::PKey::new();
        let msg = vec![0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
        k0.gen(512);
        k1.load_pub(&k0.save_pub());
        let emsg = k1.public_encrypt_with_padding(&msg, super::EncryptionPadding::PKCS1v15);
        let dmsg = k0.private_decrypt_with_padding(&emsg, super::EncryptionPadding::PKCS1v15);
        assert!(msg == dmsg);
    }

    #[test]
    fn test_sign() {
        let mut k0 = super::PKey::new();
        let mut k1 = super::PKey::new();
        let msg = vec![0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
        k0.gen(512);
        k1.load_pub(&k0.save_pub());
        let sig = k0.sign(&msg);
        let rv = k1.verify(&msg, &sig);
        assert!(rv == true);
    }

    #[test]
    fn test_sign_hashes() {
        let mut k0 = super::PKey::new();
        let mut k1 = super::PKey::new();
        let msg = vec![0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
        k0.gen(512);
        k1.load_pub(&k0.save_pub());

        let sig = k0.sign_with_hash(&msg, MD5);

        assert!(k1.verify_with_hash(&msg, &sig, MD5));
        assert!(!k1.verify_with_hash(&msg, &sig, SHA1));
    }

    #[test]
    fn test_eq() {
        let mut k0 = super::PKey::new();
        let mut p0 = super::PKey::new();
        let mut k1 = super::PKey::new();
        let mut p1 = super::PKey::new();
        k0.gen(512);
        k1.gen(512);
        p0.load_pub(&k0.save_pub());
        p1.load_pub(&k1.save_pub());

        assert!(k0.public_eq(&k0));
        assert!(k1.public_eq(&k1));
        assert!(p0.public_eq(&p0));
        assert!(p1.public_eq(&p1));
        assert!(k0.public_eq(&p0));
        assert!(k1.public_eq(&p1));

        assert!(!k0.public_eq(&k1));
        assert!(!p0.public_eq(&p1));
        assert!(!k0.public_eq(&p1));
        assert!(!p0.public_eq(&k1));
    }

    #[test]
    fn test_pem() {
        let key_path = Path::new("test/key.pem");
        let mut file = File::open(&key_path)
                           .ok()
                           .expect("Failed to open `test/key.pem`");

        let key = super::PKey::private_key_from_pem(&mut file).unwrap();

        let mut priv_key = Vec::new();
        let mut pub_key = Vec::new();

        key.write_pem(&mut priv_key).unwrap();
        key.write_pub_pem(&mut pub_key).unwrap();

        // As a super-simple verification, just check that the buffers contain
        // the `PRIVATE KEY` or `PUBLIC KEY` strings.
        assert!(priv_key.windows(11).any(|s| s == b"PRIVATE KEY"));
        assert!(pub_key.windows(10).any(|s| s == b"PUBLIC KEY"));
    }

    #[test]
    fn test_public_key_from_raw() {
        let mut k0 = super::PKey::new();
        let mut k1 = super::PKey::new();
        let msg = vec![0xdeu8, 0xadu8, 0xd0u8, 0x0du8];

        k0.gen(512);
        let sig = k0.sign(&msg);

        let r0 = k0.get_rsa();
        let r1 = RSA::from_public_components(r0.n().expect("n"), r0.e().expect("e")).expect("r1");
        k1.set_rsa(&r1);

        assert!(k1.can(super::Role::Encrypt));
        assert!(!k1.can(super::Role::Decrypt));
        assert!(k1.can(super::Role::Verify));
        assert!(!k1.can(super::Role::Sign));

        let rv = k1.verify(&msg, &sig);
        assert!(rv == true);
    }

    #[test]
    #[should_panic(expected = "Could not get RSA key for encryption")]
    fn test_nokey_encrypt() {
        let mut pkey = super::PKey::new();
        pkey.load_pub(&[]);
        pkey.encrypt(&[]);
    }

    #[test]
    #[should_panic(expected = "Could not get RSA key for decryption")]
    fn test_nokey_decrypt() {
        let mut pkey = super::PKey::new();
        pkey.load_priv(&[]);
        pkey.decrypt(&[]);
    }

    #[test]
    #[should_panic(expected = "Could not get RSA key for signing")]
    fn test_nokey_sign() {
        let mut pkey = super::PKey::new();
        pkey.load_priv(&[]);
        pkey.sign(&[]);
    }

    #[test]
    #[should_panic(expected = "Could not get RSA key for verification")]
    fn test_nokey_verify() {
        let mut pkey = super::PKey::new();
        pkey.load_pub(&[]);
        pkey.verify(&[], &[]);
    }
}