aboutsummaryrefslogtreecommitdiffstats
path: root/src/pbe/get_pbe.cpp
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-11-11 03:14:54 +0000
committerlloyd <[email protected]>2008-11-11 03:14:54 +0000
commit6d2db29350761e5573c6f7fafefb2b937993fb80 (patch)
treea644b1ac9eff87838e796fef0adb9a74b5ca964f /src/pbe/get_pbe.cpp
parent290f6c94ed43e4b5e30d0208e2a9e3a0a1eca2e7 (diff)
Switch to Algorithm_Factory in PBES2
Diffstat (limited to 'src/pbe/get_pbe.cpp')
-rw-r--r--src/pbe/get_pbe.cpp81
1 files changed, 47 insertions, 34 deletions
diff --git a/src/pbe/get_pbe.cpp b/src/pbe/get_pbe.cpp
index d5960f283..7e76b7943 100644
--- a/src/pbe/get_pbe.cpp
+++ b/src/pbe/get_pbe.cpp
@@ -19,21 +19,26 @@
namespace Botan {
-namespace {
-
-PBE* make_pbe_pkcs15(const std::string& cipher,
- const std::string& digest,
- Cipher_Dir direction)
+/*************************************************
+* Get an encryption PBE, set new parameters *
+*************************************************/
+PBE* get_pbe(const std::string& algo_spec)
{
+ SCAN_Name request(algo_spec);
+
+ const std::string pbe = request.algo_name();
+ const std::string digest = request.arg(0);
+ const std::string cipher = request.arg(1);
+
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);
+ throw Invalid_Argument("PBE: 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);
+ throw Invalid_Argument("PBE: Invalid cipher mode " + cipher);
Algorithm_Factory& af = global_state().algorithm_factory();
@@ -45,39 +50,23 @@ PBE* make_pbe_pkcs15(const std::string& cipher,
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 *
-*************************************************/
-PBE* get_pbe(const std::string& pbe_name)
- {
- SCAN_Name request(pbe_name);
-
if(request.arg_count() != 2)
- throw Invalid_Algorithm_Name(pbe_name);
-
- const std::string pbe = request.algo_name();
- const std::string digest = request.arg(0);
- const std::string cipher = request.arg(1);
+ throw Invalid_Algorithm_Name(algo_spec);
#if defined(BOTAN_HAS_PBE_PKCS_V15)
if(pbe == "PBE-PKCS5v15")
- return make_pbe_pkcs15(cipher, digest, ENCRYPTION);
+ return new PBE_PKCS5v15(block_cipher->clone(),
+ hash_function->clone(),
+ ENCRYPTION);
#endif
#if defined(BOTAN_HAS_PBE_PKCS_V20)
if(pbe == "PBE-PKCS5v20")
- return new PBE_PKCS5v20(digest, cipher);
+ return new PBE_PKCS5v20(block_cipher->clone(),
+ hash_function->clone());
#endif
- throw Algorithm_Not_Found(pbe_name);
+ throw Algorithm_Not_Found(algo_spec);
}
/*************************************************
@@ -87,23 +76,47 @@ PBE* get_pbe(const OID& pbe_oid, DataSource& params)
{
SCAN_Name request(OIDS::lookup(pbe_oid));
+ const std::string pbe = request.algo_name();
+
#if defined(BOTAN_HAS_PBE_PKCS_V15)
- if(request.algo_name() == "PBE-PKCS5v15")
+ if(pbe == "PBE-PKCS5v15")
{
if(request.arg_count() != 2)
- throw Invalid_Algorithm_Name(pbe_oid.as_string());
+ throw Invalid_Algorithm_Name(request.as_string());
const std::string digest = request.arg(0);
const std::string cipher = request.arg(1);
- PBE* pbe = make_pbe_pkcs15(cipher, digest, DECRYPTION);
+ std::vector<std::string> cipher_spec = split_on(cipher, '/');
+ if(cipher_spec.size() != 2)
+ throw Invalid_Argument("PBE: 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: Invalid cipher mode " + 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);
+
+ PBE* pbe = new PBE_PKCS5v15(block_cipher->clone(),
+ hash_function->clone(),
+ DECRYPTION);
pbe->decode_params(params);
return pbe;
}
#endif
#if defined(BOTAN_HAS_PBE_PKCS_V20)
- if(request.algo_name() == "PBE-PKCS5v20")
+ if(pbe == "PBE-PKCS5v20")
return new PBE_PKCS5v20(params);
#endif