|
| 1 | +// This file is dual licensed under the terms of the Apache License, Version |
| 2 | +// 2.0, and the BSD License. See the LICENSE file in the root of this repository |
| 3 | +// for complete details. |
| 4 | + |
| 5 | +use crate::{KeySerializationError, KeySerializationResult}; |
| 6 | + |
| 7 | +pub enum EncryptionAlgorithm { |
| 8 | + PBESHA1And3KeyTripleDESCBC, |
| 9 | + PBESv2SHA256AndAES256CBC, |
| 10 | +} |
| 11 | + |
| 12 | +impl EncryptionAlgorithm { |
| 13 | + pub fn salt_length(&self) -> usize { |
| 14 | + match self { |
| 15 | + EncryptionAlgorithm::PBESHA1And3KeyTripleDESCBC => 8, |
| 16 | + EncryptionAlgorithm::PBESv2SHA256AndAES256CBC => 16, |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + pub fn algorithm_identifier<'a>( |
| 21 | + &self, |
| 22 | + cipher_kdf_iter: u64, |
| 23 | + salt: &'a [u8], |
| 24 | + iv: &'a [u8], |
| 25 | + ) -> cryptography_x509::common::AlgorithmIdentifier<'a> { |
| 26 | + match self { |
| 27 | + EncryptionAlgorithm::PBESHA1And3KeyTripleDESCBC => { |
| 28 | + cryptography_x509::common::AlgorithmIdentifier { |
| 29 | + oid: asn1::DefinedByMarker::marker(), |
| 30 | + params: cryptography_x509::common::AlgorithmParameters::PbeWithShaAnd3KeyTripleDesCbc(cryptography_x509::common::Pkcs12PbeParams{ |
| 31 | + salt, |
| 32 | + iterations: cipher_kdf_iter, |
| 33 | + }), |
| 34 | + } |
| 35 | + } |
| 36 | + EncryptionAlgorithm::PBESv2SHA256AndAES256CBC => { |
| 37 | + let kdf_algorithm_identifier = cryptography_x509::common::AlgorithmIdentifier { |
| 38 | + oid: asn1::DefinedByMarker::marker(), |
| 39 | + params: cryptography_x509::common::AlgorithmParameters::Pbkdf2( |
| 40 | + cryptography_x509::common::PBKDF2Params { |
| 41 | + salt, |
| 42 | + iteration_count: cipher_kdf_iter, |
| 43 | + key_length: None, |
| 44 | + prf: Box::new(cryptography_x509::common::AlgorithmIdentifier { |
| 45 | + oid: asn1::DefinedByMarker::marker(), |
| 46 | + params: |
| 47 | + cryptography_x509::common::AlgorithmParameters::HmacWithSha256( |
| 48 | + Some(()), |
| 49 | + ), |
| 50 | + }), |
| 51 | + }, |
| 52 | + ), |
| 53 | + }; |
| 54 | + let encryption_algorithm_identifier = |
| 55 | + cryptography_x509::common::AlgorithmIdentifier { |
| 56 | + oid: asn1::DefinedByMarker::marker(), |
| 57 | + params: cryptography_x509::common::AlgorithmParameters::Aes256Cbc( |
| 58 | + iv[..16].try_into().unwrap(), |
| 59 | + ), |
| 60 | + }; |
| 61 | + |
| 62 | + cryptography_x509::common::AlgorithmIdentifier { |
| 63 | + oid: asn1::DefinedByMarker::marker(), |
| 64 | + params: cryptography_x509::common::AlgorithmParameters::Pbes2( |
| 65 | + cryptography_x509::common::PBES2Params { |
| 66 | + key_derivation_func: Box::new(kdf_algorithm_identifier), |
| 67 | + encryption_scheme: Box::new(encryption_algorithm_identifier), |
| 68 | + }, |
| 69 | + ), |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + pub fn encrypt( |
| 76 | + &self, |
| 77 | + password: &[u8], |
| 78 | + cipher_kdf_iter: u64, |
| 79 | + salt: &[u8], |
| 80 | + iv: &[u8], |
| 81 | + data: &[u8], |
| 82 | + ) -> KeySerializationResult<Vec<u8>> { |
| 83 | + match self { |
| 84 | + EncryptionAlgorithm::PBESHA1And3KeyTripleDESCBC => { |
| 85 | + let password = std::str::from_utf8(password) |
| 86 | + .map_err(|_| KeySerializationError::PasswordMustBeUtf8)?; |
| 87 | + |
| 88 | + let key = cryptography_crypto::pkcs12::kdf( |
| 89 | + password, |
| 90 | + salt, |
| 91 | + cryptography_crypto::pkcs12::KDF_ENCRYPTION_KEY_ID, |
| 92 | + cipher_kdf_iter, |
| 93 | + 24, |
| 94 | + openssl::hash::MessageDigest::sha1(), |
| 95 | + )?; |
| 96 | + let iv = cryptography_crypto::pkcs12::kdf( |
| 97 | + password, |
| 98 | + salt, |
| 99 | + cryptography_crypto::pkcs12::KDF_IV_ID, |
| 100 | + cipher_kdf_iter, |
| 101 | + 8, |
| 102 | + openssl::hash::MessageDigest::sha1(), |
| 103 | + )?; |
| 104 | + |
| 105 | + Ok(openssl::symm::encrypt( |
| 106 | + openssl::symm::Cipher::des_ede3_cbc(), |
| 107 | + &key, |
| 108 | + Some(&iv), |
| 109 | + data, |
| 110 | + )?) |
| 111 | + } |
| 112 | + EncryptionAlgorithm::PBESv2SHA256AndAES256CBC => { |
| 113 | + let sha256 = openssl::hash::MessageDigest::sha256(); |
| 114 | + |
| 115 | + let mut key = [0; 32]; |
| 116 | + openssl::pkcs5::pbkdf2_hmac( |
| 117 | + password, |
| 118 | + salt, |
| 119 | + cipher_kdf_iter.try_into().unwrap(), |
| 120 | + sha256, |
| 121 | + &mut key, |
| 122 | + )?; |
| 123 | + |
| 124 | + Ok(openssl::symm::encrypt( |
| 125 | + openssl::symm::Cipher::aes_256_cbc(), |
| 126 | + &key, |
| 127 | + Some(iv), |
| 128 | + data, |
| 129 | + )?) |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments