diff options
Diffstat (limited to 'doc/examples/tls_client.cpp')
-rw-r--r-- | doc/examples/tls_client.cpp | 74 |
1 files changed, 29 insertions, 45 deletions
diff --git a/doc/examples/tls_client.cpp b/doc/examples/tls_client.cpp index a9d6650c2..1cca002af 100644 --- a/doc/examples/tls_client.cpp +++ b/doc/examples/tls_client.cpp @@ -1,5 +1,6 @@ #include <botan/botan.h> #include <botan/tls_client.h> +#include <botan/pkcs8.h> #include <botan/hex.h> #include <stdio.h> #include <string> @@ -15,26 +16,12 @@ #include <errno.h> #include <fcntl.h> +#include "credentials.h" + using namespace Botan; using namespace std::tr1::placeholders; -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()); @@ -69,14 +56,13 @@ int connect_to_host(const std::string& host, u16bit port) return fd; } -bool handshake_complete(const TLS_Session& session) +bool handshake_complete(const TLS::Session& session) { - printf("Handshake complete, protocol=%04X ciphersuite=%04X compression=%d\n", - session.version(), session.ciphersuite(), - session.compression_method()); + std::cout << "Handshake complete!\n"; + std::cout << "Protocol version " << session.version().to_string() << "\n"; + std::cout << "Ciphersuite " << std::hex << session.ciphersuite().to_string() << "\n"; + std::cout << "Session ID " << hex_encode(session.session_id()) << "\n"; - printf("Session id = %s\n", hex_encode(session.session_id()).c_str()); - printf("Master secret = %s\n", hex_encode(session.master_secret()).c_str()); return true; } @@ -100,43 +86,41 @@ void socket_write(int sockfd, const byte buf[], size_t length) offset += sent; length -= sent; } - - //printf("socket write %d\n", offset); } bool got_alert = false; -void process_data(const byte buf[], size_t buf_size, u16bit alert_info) +void process_data(const byte buf[], size_t buf_size, TLS::Alert alert) { - if(alert_info != NULL_ALERT) + if(alert.is_valid()) { - printf("Alert: %d\n", alert_info); + std::cout << "Alert: " << alert.type_string() << "\n"; got_alert = true; } for(size_t i = 0; i != buf_size; ++i) { - printf("%c", buf[i]); + std::cout << buf[i]; } } std::string protocol_chooser(const std::vector<std::string>& protocols) { for(size_t i = 0; i != protocols.size(); ++i) - printf("Protocol %d - %s\n", i, protocols[i].c_str()); + std::cout << "Protocol " << i << " = " << protocols[i] << "\n"; return "http/1.1"; } void doit(RandomNumberGenerator& rng, - TLS_Policy& policy, - TLS_Session_Manager& session_manager, + TLS::Policy& policy, + TLS::Session_Manager& session_manager, Credentials_Manager& creds, const std::string& host, u16bit port) { int sockfd = connect_to_host(host, port); - TLS_Client client(std::tr1::bind(socket_write, sockfd, _1, _2), + TLS::Client client(std::tr1::bind(socket_write, sockfd, _1, _2), process_data, handshake_complete, session_manager, @@ -170,19 +154,17 @@ void doit(RandomNumberGenerator& rng, if(got == 0) { - printf("EOF on socket\n"); + std::cout << "EOF on socket\n"; break; } else if(got == -1) { - printf("Socket error %d (%s)\n", errno, strerror(errno)); + std::cout << "Socket error: " << errno << " " << strerror(errno) << "\n"; continue; } - //printf("socket read %d\n", got); - const size_t needed = client.received_data(buf, got); - printf("socket - got %d bytes, need %d\n", got, needed); + //std::cout << "Socket - got " << got << " bytes, need " << needed << "\n"; } else if(FD_ISSET(STDIN_FILENO, &readfds)) { @@ -191,26 +173,28 @@ void doit(RandomNumberGenerator& rng, if(got == 0) { - printf("EOF on stdin\n"); + std::cout << "EOF on stdin\n"; client.close(); break; } else if(got == -1) { - printf("Error reading stdin %d (%s)\n", errno, strerror(errno)); + std::cout << "Stdin error: " << errno << " " << strerror(errno) << "\n"; continue; } - client.queue_for_sending(buf, got); + client.send(buf, got); } } + + ::close(sockfd); } int main(int argc, char* argv[]) { if(argc != 2 && argc != 3) { - printf("Usage: %s host [port]\n", argv[0]); + std::cout << "Usage " << argv[0] << " host [port]\n"; return 1; } @@ -218,10 +202,10 @@ int main(int argc, char* argv[]) { LibraryInitializer botan_init; AutoSeeded_RNG rng; - Client_TLS_Policy policy; - TLS_Session_Manager_In_Memory session_manager; + TLS::Policy policy; + TLS::Session_Manager_In_Memory session_manager; - Credentials_Manager creds; + Credentials_Manager_Simple creds(rng); std::string host = argv[1]; u32bit port = argc == 3 ? Botan::to_u32bit(argv[2]) : 443; @@ -232,7 +216,7 @@ int main(int argc, char* argv[]) } catch(std::exception& e) { - printf("%s\n", e.what()); + std::cout << "Exception: " << e.what() << "\n"; return 1; } return 0; |