aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorlloyd <[email protected]>2012-01-19 14:41:31 +0000
committerlloyd <[email protected]>2012-01-19 14:41:31 +0000
commitb899ee14925310574da400c2af0f491f8cd2a103 (patch)
tree016b3cdf7ab29d8387eb23371d114b15ecc095b2 /doc
parentd42a036a7497ed05306778239e5a038e25726443 (diff)
Some cleanups, use cout instead of printf
Diffstat (limited to 'doc')
-rw-r--r--doc/examples/tls_client.cpp74
1 files changed, 53 insertions, 21 deletions
diff --git a/doc/examples/tls_client.cpp b/doc/examples/tls_client.cpp
index a9d6650c2..a23332f22 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>
@@ -22,6 +23,8 @@ using namespace std::tr1::placeholders;
class Client_TLS_Policy : public TLS_Policy
{
public:
+ Version_Code pref_version() const { return SSL_V3; }
+
bool check_cert(const std::vector<X509_Certificate>& certs) const
{
for(size_t i = 0; i != certs.size(); ++i)
@@ -71,12 +74,11 @@ int connect_to_host(const std::string& host, u16bit port)
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.major_version()
+ << "." << session.minor_version() << "\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,8 +102,6 @@ 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;
@@ -110,20 +110,20 @@ void process_data(const byte buf[], size_t buf_size, u16bit alert_info)
{
if(alert_info != NULL_ALERT)
{
- printf("Alert: %d\n", alert_info);
+ std::cout << "Alert: " << alert_info << "\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";
}
@@ -170,19 +170,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 +189,60 @@ 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);
}
+class Credentials_Manager_Simple : public Credentials_Manager
+ {
+ public:
+ Credentials_Manager_Simple(RandomNumberGenerator& rng) : rng(rng) {}
+
+ std::vector<X509_Certificate> cert_chain(
+ const std::string& cert_key_type,
+ const std::string& type,
+ const std::string& context)
+ {
+ X509_Certificate cert("user-rsa.crt");
+ Private_Key* key = PKCS8::load_key("user-rsa.key", rng);
+
+ certs_and_keys[cert] = key;
+
+ std::vector<X509_Certificate> certs;
+ certs.push_back(cert);
+ return certs;
+ }
+
+ Private_Key* private_key_for(const X509_Certificate& cert,
+ const std::string& type,
+ const std::string& context)
+ {
+ return certs_and_keys[cert];
+ }
+
+ private:
+ RandomNumberGenerator& rng;
+ std::map<X509_Certificate, Private_Key*> certs_and_keys;
+ };
+
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;
}
@@ -221,7 +253,7 @@ int main(int argc, char* argv[])
Client_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 +264,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;