aboutsummaryrefslogtreecommitdiffstats
path: root/doc/examples/tls_client.cpp
blob: be72a65e746e0a4ceda6bb6e53e55df449b4485b (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <botan/botan.h>
#include <botan/tls_client.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <memory>

#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

using namespace Botan;

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

int connect_to_host(const std::string& host, u16bit port)
   {
   hostent* host_addr = ::gethostbyname(host.c_str());

   if(host_addr == 0)
      throw std::runtime_error("gethostbyname failed for " + host);

   if(host_addr->h_addrtype != AF_INET) // FIXME
      throw std::runtime_error(host + " has IPv6 address");

   int fd = ::socket(PF_INET, SOCK_STREAM, 0);
   if(fd == -1)
      throw std::runtime_error("Unable to acquire socket");

   sockaddr_in socket_info;
   ::memset(&socket_info, 0, sizeof(socket_info));
   socket_info.sin_family = AF_INET;
   socket_info.sin_port = htons(port);

   ::memcpy(&socket_info.sin_addr,
            host_addr->h_addr,
            host_addr->h_length);

   socket_info.sin_addr = *(struct in_addr*)host_addr->h_addr; // FIXME

   if(::connect(fd, (sockaddr*)&socket_info, sizeof(struct sockaddr)) != 0)
      {
      ::close(fd);
      throw std::runtime_error("connect failed");
      }

   return fd;
   }

void handshake_complete(const TLS_Session_Params& session)
   {
   printf("Handshake complete, protocol=%04X ciphersuite=%04X compression=%d\n",
          session.version(), session.ciphersuite(),
          session.compression_method());

   printf("Session id = %s\n", hex_encode(session.session_id()).c_str());
   printf("Master secret = %s\n", hex_encode(session.master_secret()).c_str());
   }

void socket_write(int sockfd, const byte buf[], size_t length)
   {
   size_t offset = 0;

   while(length)
      {
      ssize_t sent = ::send(sockfd, (const char*)buf + offset,
                            length, MSG_NOSIGNAL);

      if(sent == -1)
         {
         if(errno == EINTR)
            sent = 0;
         else
            throw std::runtime_error("Socket::write: Socket write failed");
         }

      offset += sent;
      length -= sent;
      }

   //printf("socket write %d\n", offset);
   }

void process_data(const byte buf[], size_t buf_size, u16bit alert_info)
   {
   if(alert_info != NULL_ALERT)
      {
      printf("Alert: %d\n", alert_info);
      }

   for(size_t i = 0; i != buf_size; ++i)
      printf("%c", buf[i]);
   }

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;
      AutoSeeded_RNG rng;
      Client_TLS_Policy policy;
      TLS_Session_Manager_In_Memory session_manager;

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

      int sockfd = connect_to_host(host, port);

      TLS_Client client(std::tr1::bind(socket_write, sockfd, _1, _2),
                        process_data,
                        handshake_complete,
                        session_manager,
                        policy,
                        rng,
                        host);

      fd_set readfds;

      bool version_reported = false;

      while(true)
         {
         FD_ZERO(&readfds);
         FD_SET(sockfd, &readfds);
         FD_SET(STDIN_FILENO, &readfds);

         ::select(sockfd + 1, &readfds, NULL, NULL, NULL);

         if(client.is_closed())
            break;

         if(client.is_active() && !version_reported)
            printf("Negotiated version %04X\n", client.protocol_version());

         if(FD_ISSET(sockfd, &readfds))
            {
            byte buf[1024] = { 0 };
            ssize_t got = read(sockfd, buf, sizeof(buf));

            if(got == 0)
               {
               printf("EOF on socket\n");
               break;
               }
            else if(got == -1)
               {
               printf("Socket error %d (%s)\n", errno, strerror(errno));
               continue;
               }

            //printf("socket read %d\n", got);

            client.received_data(buf, got);
            }
         else if(FD_ISSET(STDIN_FILENO, &readfds))
            {
            byte buf[1024] = { 0 };
            ssize_t got = read(STDIN_FILENO, buf, sizeof(buf));

            if(got == 0)
               {
               printf("EOF on stdin\n");
               client.close();
               break;
               }
            else if(got == -1)
               {
               printf("Error reading stdin %d (%s)\n", errno, strerror(errno));
               continue;
               }

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