Botan 1.10.17

get_pbe.cpp
Go to the documentation of this file.
1 /*
2 * PBE Retrieval
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/get_pbe.h>
9 #include <botan/oids.h>
10 #include <botan/scan_name.h>
11 #include <botan/parsing.h>
12 #include <botan/libstate.h>
13
14 #if defined(BOTAN_HAS_PBE_PKCS_V15)
15 #include <botan/pbes1.h>
16 #endif
17
18 #if defined(BOTAN_HAS_PBE_PKCS_V20)
19 #include <botan/pbes2.h>
20 #endif
21
22 namespace Botan {
23
24 /*
25 * Get an encryption PBE, set new parameters
26 */
27 PBE* get_pbe(const std::string& algo_spec)
28 {
29 SCAN_Name request(algo_spec);
30
31 const std::string pbe = request.algo_name();
32 std::string digest_name = request.arg(0);
33 const std::string cipher = request.arg(1);
34
35 std::vector<std::string> cipher_spec = split_on(cipher, '/');
36 if(cipher_spec.size() != 2)
37 throw Invalid_Argument("PBE: Invalid cipher spec " + cipher);
38
39 const std::string cipher_algo = global_state().deref_alias(cipher_spec[0]);
40 const std::string cipher_mode = cipher_spec[1];
41
42 if(cipher_mode != "CBC")
43 throw Invalid_Argument("PBE: Invalid cipher mode " + cipher);
44
45 Algorithm_Factory& af = global_state().algorithm_factory();
46
47 const BlockCipher* block_cipher = af.prototype_block_cipher(cipher_algo);
48 if(!block_cipher)
49 throw Algorithm_Not_Found(cipher_algo);
50
51 const HashFunction* hash_function = af.prototype_hash_function(digest_name);
52 if(!hash_function)
53 throw Algorithm_Not_Found(digest_name);
54
55 if(request.arg_count() != 2)
56 throw Invalid_Algorithm_Name(algo_spec);
57
58 #if defined(BOTAN_HAS_PBE_PKCS_V15)
59 if(pbe == "PBE-PKCS5v15")
60 return new PBE_PKCS5v15(block_cipher->clone(),
61 hash_function->clone(),
62 ENCRYPTION);
63 #endif
64
65 #if defined(BOTAN_HAS_PBE_PKCS_V20)
66 if(pbe == "PBE-PKCS5v20")
67 return new PBE_PKCS5v20(block_cipher->clone(),
68 hash_function->clone());
69 #endif
70
71 throw Algorithm_Not_Found(algo_spec);
72 }
73
74 /*
75 * Get a decryption PBE, decode parameters
76 */
77 PBE* get_pbe(const OID& pbe_oid, DataSource& params)
78 {
79 SCAN_Name request(OIDS::lookup(pbe_oid));
80
81 const std::string pbe = request.algo_name();
82
83 #if defined(BOTAN_HAS_PBE_PKCS_V15)
84 if(pbe == "PBE-PKCS5v15")
85 {
86 if(request.arg_count() != 2)
87 throw Invalid_Algorithm_Name(request.as_string());
88
89 std::string digest_name = request.arg(0);
90 const std::string cipher = request.arg(1);
91
92 std::vector<std::string> cipher_spec = split_on(cipher, '/');
93 if(cipher_spec.size() != 2)
94 throw Invalid_Argument("PBE: Invalid cipher spec " + cipher);
95
96 const std::string cipher_algo = global_state().deref_alias(cipher_spec[0]);
97 const std::string cipher_mode = cipher_spec[1];
98
99 if(cipher_mode != "CBC")
100 throw Invalid_Argument("PBE: Invalid cipher mode " + cipher);
101
102 Algorithm_Factory& af = global_state().algorithm_factory();
103
104 const BlockCipher* block_cipher = af.prototype_block_cipher(cipher_algo);
105 if(!block_cipher)
106 throw Algorithm_Not_Found(cipher_algo);
107
108 const HashFunction* hash_function =
109 af.prototype_hash_function(digest_name);
110
111 if(!hash_function)
112 throw Algorithm_Not_Found(digest_name);
113
114 PBE* pbe = new PBE_PKCS5v15(block_cipher->clone(),
115 hash_function->clone(),
116 DECRYPTION);
117 pbe->decode_params(params);
118 return pbe;
119 }
120 #endif
121
122 #if defined(BOTAN_HAS_PBE_PKCS_V20)
123 if(pbe == "PBE-PKCS5v20")
124 return new PBE_PKCS5v20(params);
125 #endif
126
127 throw Algorithm_Not_Found(pbe_oid.as_string());
128 }
129
130 }
Botan::SCAN_Name::as_string
std::string as_string() const
Definition: scan_name.h:32
Botan::Algorithm_Factory
Definition: algo_factory.h:34
Botan::SCAN_Name::arg_count
size_t arg_count() const
Definition: scan_name.h:47
Botan::Library_State::algorithm_factory
Algorithm_Factory & algorithm_factory() const
Definition: libstate.cpp:173
Botan::ENCRYPTION
Definition: sym_algo.h:87
Botan::OID::as_string
std::string as_string() const
Definition: asn1_oid.cpp:50
Botan::BlockCipher::clone
virtual BlockCipher * clone() const =0
Botan::Algorithm_Factory::prototype_block_cipher
const BlockCipher * prototype_block_cipher(const std::string &algo_spec, const std::string &provider="")
Definition: algo_factory.cpp:193
Botan::split_on
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:152
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::DataSource
Definition: data_src.h:20
Botan::HashFunction::clone
virtual HashFunction * clone() const =0
Botan::DECRYPTION
Definition: sym_algo.h:87
Botan::PBE_PKCS5v20
Definition: pbes2.h:21
Botan::Algorithm_Factory::prototype_hash_function
const HashFunction * prototype_hash_function(const std::string &algo_spec, const std::string &provider="")
Definition: algo_factory.cpp:215
Botan::Global_State_Management::global_state
Library_State & global_state()
Definition: global_state.cpp:32
Botan::PBE::decode_params
virtual void decode_params(DataSource &src)=0
Botan::OIDS::lookup
std::string lookup(const OID &oid)
Definition: oids.cpp:31
Botan::SCAN_Name::arg
std::string arg(size_t i) const
Definition: scan_name.cpp:153
Botan
Definition: algo_base.h:14
Botan::SCAN_Name
Definition: scan_name.h:21
Botan::HashFunction
Definition: hash.h:20
Botan::PBE_PKCS5v15
Definition: pbes1.h:21
Botan::Library_State::deref_alias
std::string deref_alias(const std::string &alias) const
Definition: libstate.cpp:162
Botan::Invalid_Algorithm_Name
Definition: exceptn.h:119
Botan::get_pbe
PBE * get_pbe(const std::string &algo_spec)
Definition: get_pbe.cpp:27
Botan::OID
Definition: asn1_oid.h:20
Botan::Algorithm_Not_Found
Definition: exceptn.h:109
Botan::PBE
Definition: pbe.h:21
Botan::SCAN_Name::algo_name
std::string algo_name() const
Definition: scan_name.h:37
Botan::BlockCipher
Definition: block_cipher.h:18