diff options
author | lloyd <[email protected]> | 2010-02-14 05:39:37 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-02-14 05:39:37 +0000 |
commit | 1e596a25e32c3106b3d6e2aceb64a270a8b30713 (patch) | |
tree | 6f9bc30f3583f81420b69200d5ff1c82b4a00917 /doc/examples | |
parent | 12e07d37e9622cfb24b2102090550a0260c6665c (diff) | |
parent | 3b980d2fcee997ba262cf7e8d8542eb51a56be3e (diff) |
propagate from branch 'net.randombit.botan' (head 5bfc3e699003b86615c584f8ae40bd6e761f96c0)
to branch 'net.randombit.botan.ssl' (head 6865128cf0c5f6ad1987e22cc1d521fd2e38fd21)
Diffstat (limited to 'doc/examples')
-rw-r--r-- | doc/examples/tls_client.cpp | 55 | ||||
-rw-r--r-- | doc/examples/tls_server.cpp | 71 |
2 files changed, 126 insertions, 0 deletions
diff --git a/doc/examples/tls_client.cpp b/doc/examples/tls_client.cpp new file mode 100644 index 000000000..20fde6354 --- /dev/null +++ b/doc/examples/tls_client.cpp @@ -0,0 +1,55 @@ +/* +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/init.h> +#include <botan/tls_client.h> +#include <botan/unx_sock.h> + +using namespace Botan; + +#include <stdio.h> +#include <string> +#include <iostream> +#include <memory> + +int main() + { + try + { + LibraryInitializer init; + + Unix_Socket sock("randombit.net", 443); + + std::auto_ptr<Botan::RandomNumberGenerator> rng( + Botan::RandomNumberGenerator::make_rng()); + + TLS_Client tls(*rng, sock); + + printf("Connection open\n"); + + while(true) + { + if(tls.is_closed()) + break; + + std::string str; + std::getline(std::cin, str); + str += "\n"; + tls.write((const byte*)str.c_str(), str.length()); + + byte buf[4096] = { 0 }; + tls.read(buf, sizeof(buf)); + printf("%s", buf); + fflush(0); + } + } + catch(std::exception& e) + { + printf("%s\n", e.what()); + return 1; + } + return 0; + } diff --git a/doc/examples/tls_server.cpp b/doc/examples/tls_server.cpp new file mode 100644 index 000000000..d5cb77a84 --- /dev/null +++ b/doc/examples/tls_server.cpp @@ -0,0 +1,71 @@ +/* +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/init.h> +#include <botan/tls_server.h> +#include <botan/unx_sock.h> + +#include <botan/rsa.h> +#include <botan/dsa.h> +#include <botan/x509self.h> + +using namespace Botan; + +#include <stdio.h> +#include <string> +#include <iostream> +#include <memory> + +int main() + { + try + { + LibraryInitializer init; + + std::auto_ptr<RandomNumberGenerator> rng( + RandomNumberGenerator::make_rng()); + + RSA_PrivateKey key(*rng, 512); + //DSA_PrivateKey key(get_dl_group("DSA-1024")); + + X509_Cert_Options options( + "www.randombit.net/US/Syn Ack Labs/Mathematical Munitions Dept"); + + X509_Certificate cert = + X509::create_self_signed_cert(options, key, "SHA-1", *rng); + + Unix_Server_Socket listener(4433); + + printf("Now listening...\n"); + + while(true) + { + try { + Socket* sock = listener.accept(); + + printf("Got new connection\n"); + + TLS_Server tls(*rng, *sock, cert, key); + + char msg[] = "Foo\nBar\nBaz\nQuux\n"; + tls.write((const byte*)msg, strlen(msg)); + + char buf[10] = { 0 }; + u32bit got = tls.read((byte*)buf, 9); + printf("%d: '%s'\n", got, buf); + + tls.close(); + } + catch(std::exception& e) { printf("%s\n", e.what()); } + } + } + catch(std::exception& e) + { + printf("%s\n", e.what()); + return 1; + } + return 0; + } |