aboutsummaryrefslogtreecommitdiffstats
path: root/doc/examples/credentials.h
diff options
context:
space:
mode:
authorlloyd <[email protected]>2012-01-28 06:51:44 +0000
committerlloyd <[email protected]>2012-01-28 06:51:44 +0000
commitada0998533c7b6b8eb782c494f8efdf5b6f7f712 (patch)
tree11ce3427ea06995b73fd248cb9417dd3aa837b53 /doc/examples/credentials.h
parentfd6a59f73b4b6d65966b61e8e7a8cda050a4ba43 (diff)
parentaa00e2879f90562bb06146726a602685d6051b6f (diff)
propagate from branch 'net.randombit.botan' (head 3f6b267bc00d2da1b5d36ca2215c3e1b6a40e796)
to branch 'net.randombit.botan.tls-state-machine' (head 7df407e6678bd51328c348fd2a665f20fb22d62d)
Diffstat (limited to 'doc/examples/credentials.h')
-rw-r--r--doc/examples/credentials.h95
1 files changed, 95 insertions, 0 deletions
diff --git a/doc/examples/credentials.h b/doc/examples/credentials.h
new file mode 100644
index 000000000..160fec772
--- /dev/null
+++ b/doc/examples/credentials.h
@@ -0,0 +1,95 @@
+
+#ifndef EXAMPLE_CREDENTIALS_MANAGER_H__
+#define EXAMPLE_CREDENTIALS_MANAGER_H__
+
+#include <botan/credentials_manager.h>
+#include <iostream>
+
+bool value_exists(const std::vector<std::string>& vec,
+ const std::string& val)
+ {
+ for(size_t i = 0; i != vec.size(); ++i)
+ if(vec[i] == val)
+ return true;
+ return false;
+ }
+
+class Credentials_Manager_Simple : public Botan::Credentials_Manager
+ {
+ public:
+ Credentials_Manager_Simple(Botan::RandomNumberGenerator& rng) : rng(rng) {}
+
+ std::string psk_identity(const std::string&, const std::string&,
+ const std::string& identity_hint)
+ {
+ return "Client_identity";
+ }
+
+ Botan::SymmetricKey psk(const std::string&, const std::string&,
+ const std::string& identity)
+ {
+ if(identity == "Client_identity")
+ return Botan::SymmetricKey("AABBCC");
+ throw Botan::Internal_Error("No PSK set for " + identity);
+ }
+
+ std::vector<Botan::X509_Certificate> cert_chain(
+ const std::vector<std::string>& cert_key_types,
+ const std::string& type,
+ const std::string& context)
+ {
+ std::vector<Botan::X509_Certificate> certs;
+
+ try
+ {
+ if(type == "tls-server")
+ {
+ const std::string hostname = (context == "" ? "localhost" : context);
+
+ if(value_exists(cert_key_types, "RSA"))
+ {
+ Botan::X509_Certificate cert(hostname + ".crt");
+ Botan::Private_Key* key = Botan::PKCS8::load_key(hostname + ".key", rng);
+
+ certs_and_keys[cert] = key;
+ certs.push_back(cert);
+ }
+ else if(value_exists(cert_key_types, "DSA"))
+ {
+ Botan::X509_Certificate cert(hostname + ".dsa.crt");
+ Botan::Private_Key* key = Botan::PKCS8::load_key(hostname + ".dsa.key", rng);
+
+ certs_and_keys[cert] = key;
+ certs.push_back(cert);
+ }
+ }
+ else if(type == "tls-client")
+ {
+ Botan::X509_Certificate cert("user-rsa.crt");
+ Botan::Private_Key* key = Botan::PKCS8::load_key("user-rsa.key", rng);
+
+ certs_and_keys[cert] = key;
+ certs.push_back(cert);
+ }
+ }
+ catch(std::exception& e)
+ {
+ std::cout << e.what() << "\n";
+ }
+
+ return certs;
+ }
+
+ Botan::Private_Key* private_key_for(const Botan::X509_Certificate& cert,
+ const std::string& type,
+ const std::string& context)
+ {
+ return certs_and_keys[cert];
+ }
+
+ private:
+ Botan::RandomNumberGenerator& rng;
+ std::map<Botan::X509_Certificate, Botan::Private_Key*> certs_and_keys;
+ };
+
+#endif