diff options
author | lloyd <[email protected]> | 2008-11-11 02:51:40 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-11-11 02:51:40 +0000 |
commit | 290f6c94ed43e4b5e30d0208e2a9e3a0a1eca2e7 (patch) | |
tree | 4239af13767f5b6dfdd2c69061bc6c8042f7b614 /src/pbe/get_pbe.cpp | |
parent | 686f1b30a4a7be70697644a071d50973a547f58e (diff) |
Remove global state dependency from pbes1
Diffstat (limited to 'src/pbe/get_pbe.cpp')
-rw-r--r-- | src/pbe/get_pbe.cpp | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/src/pbe/get_pbe.cpp b/src/pbe/get_pbe.cpp index 103e64a0c..d5960f283 100644 --- a/src/pbe/get_pbe.cpp +++ b/src/pbe/get_pbe.cpp @@ -6,6 +6,8 @@ #include <botan/get_pbe.h> #include <botan/oids.h> #include <botan/scan_name.h> +#include <botan/parsing.h> +#include <botan/libstate.h> #if defined(BOTAN_HAS_PBE_PKCS_V15) #include <botan/pbes1.h> @@ -17,6 +19,40 @@ namespace Botan { +namespace { + +PBE* make_pbe_pkcs15(const std::string& cipher, + const std::string& digest, + Cipher_Dir direction) + { + std::vector<std::string> cipher_spec = split_on(cipher, '/'); + if(cipher_spec.size() != 2) + throw Invalid_Argument("PBE-PKCS5 v1.5: Invalid cipher spec " + cipher); + + const std::string cipher_algo = global_state().deref_alias(cipher_spec[0]); + const std::string cipher_mode = cipher_spec[1]; + + if(cipher_mode != "CBC") + throw Invalid_Argument("PBE-PKCS5 v1.5: Invalid cipher " + cipher); + + Algorithm_Factory& af = global_state().algorithm_factory(); + + const BlockCipher* block_cipher = af.make_block_cipher(cipher_algo); + if(!block_cipher) + throw Algorithm_Not_Found(cipher_algo); + + const HashFunction* hash_function = af.make_hash_function(digest); + if(!hash_function) + throw Algorithm_Not_Found(digest); + + return new PBE_PKCS5v15(block_cipher->clone(), + hash_function->clone(), + direction); + + } + +} + /************************************************* * Get an encryption PBE, set new parameters * *************************************************/ @@ -33,7 +69,7 @@ PBE* get_pbe(const std::string& pbe_name) #if defined(BOTAN_HAS_PBE_PKCS_V15) if(pbe == "PBE-PKCS5v15") - return new PBE_PKCS5v15(digest, cipher, ENCRYPTION); + return make_pbe_pkcs15(cipher, digest, ENCRYPTION); #endif #if defined(BOTAN_HAS_PBE_PKCS_V20) @@ -60,7 +96,7 @@ PBE* get_pbe(const OID& pbe_oid, DataSource& params) const std::string digest = request.arg(0); const std::string cipher = request.arg(1); - PBE* pbe = new PBE_PKCS5v15(digest, cipher, DECRYPTION); + PBE* pbe = make_pbe_pkcs15(cipher, digest, DECRYPTION); pbe->decode_params(params); return pbe; } |