aboutsummaryrefslogtreecommitdiffstats
path: root/doc/examples/tls_client.cpp
blob: 1d62bbc20c03a29348cc0f8aff397249fb6df9fc (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <botan/botan.h>
#include <botan/tls_client.h>
#include "socket.h"

using namespace Botan;

#include <stdio.h>
#include <string>
#include <iostream>
#include <memory>

class Client_TLS_Policy : public TLS_Policy
   {
   public:
      bool check_cert(const std::vector<X509_Certificate>& certs) const
         {
         for(size_t i = 0; i != certs.size(); ++i)
            {
            std::cout << certs[i].to_string();
            }

         std::cout << "Warning: not checking cert signatures\n";

         return true;
         }
   };

class HTTPS_Client
   {
   public:
      HTTPS_Client(const std::string& host, u16bit port, RandomNumberGenerator& r) :
         rng(r),
         socket(host, port),
         client(std::tr1::bind(&HTTPS_Client::socket_write, std::tr1::ref(*this), _1, _2),
                std::tr1::bind(&HTTPS_Client::proc_data, std::tr1::ref(*this), _1, _2, _3),
                sessions,
                policy,
                rng,
                host,
                host + "_username")
         {
         SecureVector<byte> socket_buf(1024);
         size_t desired = 0;

         quit_reading = false;

         while(!client.is_active() || desired)
            {
            const size_t socket_got = socket.read(&socket_buf[0], socket_buf.size());
            //printf("Got %d bytes from socket\n", socket_got);
            desired = client.received_data(&socket_buf[0], socket_got);
            socket_buf.resize(desired || 1);
            //printf("Going around for another read?\n");

            if(quit_reading)
               break;
            }
         }

      void socket_write(const byte buf[], size_t buf_size)
         {
         std::cout << "socket_write " << buf_size << "\n";
         socket.write(buf, buf_size);
         }

      void proc_data(const byte data[], size_t data_len, u16bit alert_info)
         {
         printf("Block of data %d bytes alert %d\n", (int)data_len, alert_info);
         for(size_t i = 0; i != data_len; ++i)
            printf("%c", data[i]);

         if(alert_info != 255)
            quit_reading = true;
         }

      void write(const std::string& s)
         {
         client.queue_for_sending((const byte*)s.c_str(), s.length());
         }

      void read_response()
         {
         while(!quit_reading)
            {
            SecureVector<byte> buf(4096);

            size_t got = socket.read(&buf[0], buf.size(), true);

            if(got == 0)
               break;

            client.received_data(&buf[0], got);
            }
         }

   private:
      bool quit_reading;
      RandomNumberGenerator& rng;
      Socket socket;
      Client_TLS_Policy policy;
      TLS_Session_Manager_In_Memory sessions;

      TLS_Client client;
   };

int main(int argc, char* argv[])
   {
   if(argc != 2 && argc != 3)
      {
      printf("Usage: %s host [port]\n", argv[0]);
      return 1;
      }

   try
      {
      LibraryInitializer botan_init;

      std::string host = argv[1];
      u32bit port = argc == 3 ? Botan::to_u32bit(argv[2]) : 443;

      //SocketInitializer socket_init;

      AutoSeeded_RNG rng;

      printf("Connecting to %s:%d...\n", host.c_str(), port);

      HTTPS_Client https(host, port, rng);

      std::string http_command = "GET / HTTP/1.0\r\n\r\n";

      printf("Sending request\n");
      https.write(http_command);

      https.read_response();

   }
   catch(std::exception& e)
      {
      printf("%s\n", e.what());
      return 1;
      }
   return 0;
   }