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
22namespace Botan {
23
24/*
25* Get an encryption PBE, set new parameters
26*/
27PBE* 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(),
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*/
77PBE* 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}
const HashFunction * prototype_hash_function(const std::string &algo_spec, const std::string &provider="")
const BlockCipher * prototype_block_cipher(const std::string &algo_spec, const std::string &provider="")
virtual BlockCipher * clone() const =0
virtual HashFunction * clone() const =0
Algorithm_Factory & algorithm_factory() const
Definition libstate.cpp:173
std::string deref_alias(const std::string &alias) const
Definition libstate.cpp:162
std::string as_string() const
Definition asn1_oid.cpp:50
PBE_PKCS5v15(BlockCipher *cipher, HashFunction *hash, Cipher_Dir direction)
Definition pbes1.cpp:161
PBE_PKCS5v20(DataSource &input)
Definition pbes2.cpp:228
virtual void decode_params(DataSource &src)=0
std::string arg(size_t i) const
size_t arg_count() const
Definition scan_name.h:47
std::string algo_name() const
Definition scan_name.h:37
std::string as_string() const
Definition scan_name.h:32
std::string lookup(const OID &oid)
Definition oids.cpp:31
std::invalid_argument Invalid_Argument
Definition exceptn.h:20
std::vector< std::string > split_on(const std::string &str, char delim)
Definition parsing.cpp:152
Library_State & global_state()
@ DECRYPTION
Definition sym_algo.h:87
@ ENCRYPTION
Definition sym_algo.h:87
PBE * get_pbe(const std::string &algo_spec)
Definition get_pbe.cpp:27
Algorithm_Not_Found(const std::string &name)
Definition exceptn.h:111
Invalid_Algorithm_Name(const std::string &name)
Definition exceptn.h:121