aboutsummaryrefslogtreecommitdiffstats
path: root/doc/examples/credentials.h
blob: 802e3233c84f160fea5324fc0655d9992833722e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

#ifndef EXAMPLE_CREDENTIALS_MANAGER_H__
#define EXAMPLE_CREDENTIALS_MANAGER_H__

#include <botan/credentials_manager.h>

class Credentials_Manager_Simple : public Botan::Credentials_Manager
   {
   public:
      Credentials_Manager_Simple(Botan::RandomNumberGenerator& rng) : rng(rng) {}

      std::vector<Botan::X509_Certificate> cert_chain(
         const std::string& cert_key_type,
         const std::string& type,
         const std::string& context)
         {
         std::vector<Botan::X509_Certificate> certs;

         if(type == "tls-server")
            {
            const std::string hostname = (context == "" ? "localhost" : context);

            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(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);
            }

         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