Botan  1.10.9
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 
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 
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 }
std::string arg(size_t i) const
Definition: scan_name.cpp:153
virtual BlockCipher * clone() const =0
const BlockCipher * prototype_block_cipher(const std::string &algo_spec, const std::string &provider="")
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:152
size_t arg_count() const
Definition: scan_name.h:47
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
virtual HashFunction * clone() const =0
Algorithm_Factory & algorithm_factory() const
Definition: libstate.cpp:173
std::string algo_name() const
Definition: scan_name.h:37
const HashFunction * prototype_hash_function(const std::string &algo_spec, const std::string &provider="")
Library_State & global_state()
virtual void decode_params(DataSource &src)=0
std::string lookup(const OID &oid)
Definition: oids.cpp:31
std::string deref_alias(const std::string &alias) const
Definition: libstate.cpp:162
std::string as_string() const
Definition: asn1_oid.cpp:50
PBE * get_pbe(const std::string &algo_spec)
Definition: get_pbe.cpp:27
std::string as_string() const
Definition: scan_name.h:32
Definition: pbe.h:21