aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-07-09 20:47:52 +0000
committerlloyd <[email protected]>2010-07-09 20:47:52 +0000
commit08b232e7042555721890a0bb7b326af7f29936f3 (patch)
treebb9bc60da6e3c917a17c3a6520a3892977b239c5 /src
parent5463afbee47c10686c6829e7db809772dfba4e13 (diff)
Drop User_Interface; replace with a std::function callback
Diffstat (limited to 'src')
-rw-r--r--src/pubkey/pkcs8.cpp52
-rw-r--r--src/pubkey/pkcs8.h24
-rw-r--r--src/utils/info.txt2
-rw-r--r--src/utils/ui.cpp36
-rw-r--r--src/utils/ui.h37
5 files changed, 52 insertions, 99 deletions
diff --git a/src/pubkey/pkcs8.cpp b/src/pubkey/pkcs8.cpp
index 7d9c0d834..fd133ed85 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;
@@ -91,14 +93,12 @@ SecureVector<byte> PKCS8_decode(DataSource& source, const User_Interface& ui,
DataSource_Memory params(pbe_alg_id.parameters);
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());
@@ -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));
}
/*
diff --git a/src/pubkey/pkcs8.h b/src/pubkey/pkcs8.h
index 376429d5b..93f2f92c6 100644
--- a/src/pubkey/pkcs8.h
+++ b/src/pubkey/pkcs8.h
@@ -9,7 +9,7 @@
#define BOTAN_PKCS8_H__
#include <botan/x509_key.h>
-#include <botan/ui.h>
+#include <functional>
namespace Botan {
@@ -121,18 +121,19 @@ inline void encrypt_key(const Private_Key& key,
* Load a key from a data source.
* @param source the data source providing the encoded key
* @param rng the rng to use
-* @param ui the user interface to be used for passphrase dialog
+* @param get_passphrase a function that returns passphrases
* @return loaded private key object
*/
-BOTAN_DLL Private_Key* load_key(DataSource& source,
- RandomNumberGenerator& rng,
- const User_Interface& ui);
+BOTAN_DLL Private_Key* load_key(
+ DataSource& source,
+ RandomNumberGenerator& rng,
+ std::function<std::pair<bool, std::string> ()> get_passphrase);
/** Load a key from a data source.
* @param source the data source providing the encoded key
* @param rng the rng to use
* @param pass the passphrase to decrypt the key. Provide an empty
-* string if the key is not encoded.
+* string if the key is not encrypted
* @return loaded private key object
*/
BOTAN_DLL Private_Key* load_key(DataSource& source,
@@ -143,18 +144,19 @@ BOTAN_DLL Private_Key* load_key(DataSource& source,
* Load a key from a file.
* @param filename the path to the file containing the encoded key
* @param rng the rng to use
-* @param ui the user interface to be used for passphrase dialog
+* @param get_passphrase a function that returns passphrases
* @return loaded private key object
*/
-BOTAN_DLL Private_Key* load_key(const std::string& filename,
- RandomNumberGenerator& rng,
- const User_Interface& ui);
+BOTAN_DLL Private_Key* load_key(
+ const std::string& filename,
+ RandomNumberGenerator& rng,
+ std::function<std::pair<bool, std::string> ()> get_passphrase);
/** Load a key from a file.
* @param filename the path to the file containing the encoded key
* @param rng the rng to use
* @param pass the passphrase to decrypt the key. Provide an empty
-* string if the key is not encoded.
+* string if the key is not encrypted
* @return loaded private key object
*/
BOTAN_DLL Private_Key* load_key(const std::string& filename,
diff --git a/src/utils/info.txt b/src/utils/info.txt
index 2fb3e79a5..8cf130d49 100644
--- a/src/utils/info.txt
+++ b/src/utils/info.txt
@@ -8,7 +8,6 @@ cpuid.cpp
mlock.cpp
parsing.cpp
time.cpp
-ui.cpp
version.cpp
</source>
@@ -33,7 +32,6 @@ parsing.h
rotate.h
time.h
types.h
-ui.h
version.h
get_byte.h
</header:public>
diff --git a/src/utils/ui.cpp b/src/utils/ui.cpp
deleted file mode 100644
index e6c3430ff..000000000
--- a/src/utils/ui.cpp
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
-* User Interface
-* (C) 1999-2007 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-#include <botan/ui.h>
-
-namespace Botan {
-
-/*
-* Get a passphrase from the user
-*/
-std::string User_Interface::get_passphrase(const std::string&,
- const std::string&,
- UI_Result& action) const
- {
- action = OK;
-
- if(!first_try)
- action = CANCEL_ACTION;
-
- return preset_passphrase;
- }
-
-/*
-* User_Interface Constructor
-*/
-User_Interface::User_Interface(const std::string& preset) :
- preset_passphrase(preset)
- {
- first_try = true;
- }
-
-}
diff --git a/src/utils/ui.h b/src/utils/ui.h
deleted file mode 100644
index f69bb2c6d..000000000
--- a/src/utils/ui.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
-* User Interface
-* (C) 1999-2007 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-#ifndef BOTAN_UI_H__
-#define BOTAN_UI_H__
-
-#include <botan/build.h>
-#include <string>
-
-namespace Botan {
-
-/**
-* User Interface
-* Only really used for callbacks for PKCS #8 decryption
-*/
-class BOTAN_DLL User_Interface
- {
- public:
- enum UI_Result { OK, CANCEL_ACTION };
-
- virtual std::string get_passphrase(const std::string&,
- const std::string&,
- UI_Result&) const;
- User_Interface(const std::string& = "");
- virtual ~User_Interface() {}
- protected:
- std::string preset_passphrase;
- mutable bool first_try;
- };
-
-}
-
-#endif