Botan  1.11.10
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_V20)
15  #include <botan/pbes2.h>
16  #include <botan/hmac.h>
17 #endif
18 
19 namespace Botan {
20 
21 /*
22 * Get an encryption PBE, set new parameters
23 */
24 PBE* get_pbe(const std::string& algo_spec,
25  const std::string& passphrase,
26  std::chrono::milliseconds msec,
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 = SCAN_Name::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_V20)
59  if(pbe == "PBE-PKCS5v20")
60  return new PBE_PKCS5v20(block_cipher->clone(),
61  new HMAC(hash_function->clone()),
62  passphrase,
63  msec,
64  rng);
65 #endif
66 
67  throw Algorithm_Not_Found(algo_spec);
68  }
69 
70 /*
71 * Get a decryption PBE, decode parameters
72 */
73 PBE* get_pbe(const OID& pbe_oid,
74  const std::vector<byte>& params,
75  const std::string& passphrase)
76  {
77  SCAN_Name request(OIDS::lookup(pbe_oid));
78 
79  const std::string pbe = request.algo_name();
80 
81 #if defined(BOTAN_HAS_PBE_PKCS_V20)
82  if(pbe == "PBE-PKCS5v20")
83  return new PBE_PKCS5v20(params, passphrase);
84 #endif
85 
86  throw Algorithm_Not_Found(pbe_oid.as_string());
87  }
88 
89 }
std::string arg(size_t i) const
Definition: scan_name.cpp:155
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:108
size_t arg_count() const
Definition: scan_name.h:49
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
virtual HashFunction * clone() const =0
Algorithm_Factory & algorithm_factory() const
Definition: libstate.cpp:46
std::string algo_name() const
Definition: scan_name.h:39
const HashFunction * prototype_hash_function(const std::string &algo_spec, const std::string &provider="")
Library_State & global_state()
std::string lookup(const OID &oid)
Definition: oids.cpp:111
PBE * get_pbe(const std::string &algo_spec, const std::string &passphrase, std::chrono::milliseconds msec, RandomNumberGenerator &rng)
Definition: get_pbe.cpp:24
Definition: buf_comp.h:15
std::string passphrase
Definition: pkcs8.cpp:245
std::string as_string() const
Definition: asn1_oid.cpp:50
static std::string deref_alias(const std::string &alias)
Definition: scan_name.cpp:184
Definition: pbe.h:21