diff options
author | lloyd <[email protected]> | 2010-08-13 15:13:21 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-08-13 15:13:21 +0000 |
commit | 3da3b04e1ba1577b3d0e1b4b9b3f4b24381fc433 (patch) | |
tree | 8a4b3a3b8e830ffcbd41f41859db4ab4fd972fb2 /src/pubkey/pkcs8.cpp | |
parent | 2cae6ec17984ad016f973d5925860278e575b3f4 (diff) | |
parent | 789cee0888bbede838a2a3cf6221677c9f172872 (diff) |
propagate from branch 'net.randombit.botan' (head 0a3348f52bf558bc2282e1066c2913a72a1aeda5)
to branch 'net.randombit.botan.c++0x' (head 552c20ae8874f12da779fc25ea368e36e71cbfe8)
Diffstat (limited to 'src/pubkey/pkcs8.cpp')
-rw-r--r-- | src/pubkey/pkcs8.cpp | 56 |
1 files changed, 41 insertions, 15 deletions
diff --git a/src/pubkey/pkcs8.cpp b/src/pubkey/pkcs8.cpp index 5eed776be..cc2ed8941 100644 --- a/src/pubkey/pkcs8.cpp +++ b/src/pubkey/pkcs8.cpp @@ -41,8 +41,10 @@ SecureVector<byte> PKCS8_extract(DataSource& source, /* * PEM decode and/or decrypt a private key */ -SecureVector<byte> PKCS8_decode(DataSource& source, const User_Interface& ui, - AlgorithmIdentifier& pk_alg_id) +SecureVector<byte> PKCS8_decode( + DataSource& source, + std::function<std::pair<bool,std::string> ()> get_passphrase, + AlgorithmIdentifier& pk_alg_id) { AlgorithmIdentifier pbe_alg_id; SecureVector<byte> key_data, key; @@ -89,16 +91,14 @@ SecureVector<byte> PKCS8_decode(DataSource& source, const User_Interface& ui, if(is_encrypted) { DataSource_Memory params(pbe_alg_id.parameters); - std::auto_ptr<PBE> pbe(get_pbe(pbe_alg_id.oid, params)); + std::unique_ptr<PBE> pbe(get_pbe(pbe_alg_id.oid, params)); - User_Interface::UI_Result result = User_Interface::OK; - const std::string passphrase = - ui.get_passphrase("PKCS #8 private key", source.id(), result); + std::pair<bool, std::string> pass = get_passphrase(); - if(result == User_Interface::CANCEL_ACTION) + if(pass.first == false) break; - pbe->set_key(passphrase); + pbe->set_key(pass.second); Pipe decryptor(pbe.release()); decryptor.process_msg(key_data, key_data.size()); @@ -162,7 +162,7 @@ SecureVector<byte> BER_encode(const Private_Key& key, { const std::string DEFAULT_PBE = "PBE-PKCS5v20(SHA-1,AES-256/CBC)"; - std::auto_ptr<PBE> pbe(get_pbe(((pbe_algo != "") ? pbe_algo : DEFAULT_PBE))); + std::unique_ptr<PBE> pbe(get_pbe(((pbe_algo != "") ? pbe_algo : DEFAULT_PBE))); pbe->new_params(rng); pbe->set_key(pass); @@ -200,10 +200,10 @@ std::string PEM_encode(const Private_Key& key, */ Private_Key* load_key(DataSource& source, RandomNumberGenerator& rng, - const User_Interface& ui) + std::function<std::pair<bool, std::string> ()> get_pass) { AlgorithmIdentifier alg_id; - SecureVector<byte> pkcs8_key = PKCS8_decode(source, ui, alg_id); + SecureVector<byte> pkcs8_key = PKCS8_decode(source, get_pass, alg_id); const std::string alg_name = OIDS::lookup(alg_id.oid); if(alg_name == "" || alg_name == alg_id.oid.as_string()) @@ -218,12 +218,38 @@ Private_Key* load_key(DataSource& source, */ Private_Key* load_key(const std::string& fsname, RandomNumberGenerator& rng, - const User_Interface& ui) + std::function<std::pair<bool, std::string> ()> get_pass) { DataSource_Stream source(fsname, true); - return PKCS8::load_key(source, rng, ui); + return PKCS8::load_key(source, rng, get_pass); } +namespace { + +class Single_Shot_Passphrase + { + public: + Single_Shot_Passphrase(const std::string& pass) : + passphrase(pass), first(true) {} + + std::pair<bool, std::string> operator()() + { + if(first) + { + first = false; + return std::make_pair(true, passphrase); + } + else + return std::make_pair(false, ""); + } + + private: + std::string passphrase; + bool first; + }; + +} + /* * Extract a private key and return it */ @@ -231,7 +257,7 @@ Private_Key* load_key(DataSource& source, RandomNumberGenerator& rng, const std::string& pass) { - return PKCS8::load_key(source, rng, User_Interface(pass)); + return PKCS8::load_key(source, rng, Single_Shot_Passphrase(pass)); } /* @@ -241,7 +267,7 @@ Private_Key* load_key(const std::string& fsname, RandomNumberGenerator& rng, const std::string& pass) { - return PKCS8::load_key(fsname, rng, User_Interface(pass)); + return PKCS8::load_key(fsname, rng, Single_Shot_Passphrase(pass)); } /* |