loop-libressl.cc revision 42b85340
142b85340SShuo Chen#include <assert.h> 242b85340SShuo Chen#include <stdio.h> 342b85340SShuo Chen#include <sys/types.h> 442b85340SShuo Chen#include <sys/socket.h> 542b85340SShuo Chen 642b85340SShuo Chen#include <tls.h> 742b85340SShuo Chen 842b85340SShuo Chenstruct tls* client() 942b85340SShuo Chen{ 1042b85340SShuo Chen struct tls_config* cfg = tls_config_new(); 1142b85340SShuo Chen assert(cfg != NULL); 1242b85340SShuo Chen 1342b85340SShuo Chen tls_config_set_ca_file(cfg, "ca.pem"); 1442b85340SShuo Chen // tls_config_insecure_noverifycert(cfg); 1542b85340SShuo Chen // tls_config_insecure_noverifyname(cfg); 1642b85340SShuo Chen 1742b85340SShuo Chen struct tls* ctx = tls_client(); 1842b85340SShuo Chen assert(ctx != NULL); 1942b85340SShuo Chen 2042b85340SShuo Chen int ret = tls_configure(ctx, cfg); 2142b85340SShuo Chen assert(ret == 0); 2242b85340SShuo Chen 2342b85340SShuo Chen return ctx; 2442b85340SShuo Chen} 2542b85340SShuo Chen 2642b85340SShuo Chenint main(int argc, char* argv[]) 2742b85340SShuo Chen{ 2842b85340SShuo Chen int ret = tls_init(); 2942b85340SShuo Chen assert(ret == 0); 3042b85340SShuo Chen 3142b85340SShuo Chen struct tls_config* cfg = tls_config_new(); 3242b85340SShuo Chen assert(cfg != NULL); 3342b85340SShuo Chen 3442b85340SShuo Chen ret = tls_config_set_cert_file(cfg, "cert.crt"); 3542b85340SShuo Chen assert(ret == 0); 3642b85340SShuo Chen 3742b85340SShuo Chen ret = tls_config_set_key_file(cfg, "keyfile.key"); 3842b85340SShuo Chen assert(ret == 0); 3942b85340SShuo Chen 4042b85340SShuo Chen tls_config_verify_client_optional(cfg); 4142b85340SShuo Chen struct tls* ctx = tls_server(); 4242b85340SShuo Chen assert(ctx != NULL); 4342b85340SShuo Chen 4442b85340SShuo Chen ret = tls_configure(ctx, cfg); 4542b85340SShuo Chen // printf("%s\n", tls_error(ctx)); 4642b85340SShuo Chen assert(ret == 0); 4742b85340SShuo Chen 4842b85340SShuo Chen int fds[2]; 4942b85340SShuo Chen socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, fds); 5042b85340SShuo Chen 5142b85340SShuo Chen struct tls* sctx = NULL; 5242b85340SShuo Chen ret = tls_accept_socket(ctx, &sctx, fds[0]); 5342b85340SShuo Chen assert(ret == 0); 5442b85340SShuo Chen 5542b85340SShuo Chen struct tls* cctx = client(); 5642b85340SShuo Chen ret = tls_connect_socket(cctx, fds[1], "Test Server Cert"); 5742b85340SShuo Chen assert(ret == 0); 5842b85340SShuo Chen 5942b85340SShuo Chen do { 6042b85340SShuo Chen printf("ctx "); 6142b85340SShuo Chen ret = tls_handshake(sctx); 6242b85340SShuo Chen printf("%d\n", ret); 6342b85340SShuo Chen ret = tls_handshake(cctx); 6442b85340SShuo Chen printf("%d\n", ret); 6542b85340SShuo Chen } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT); 6642b85340SShuo Chen 6742b85340SShuo Chen printf("%s\n", tls_error(cctx)); 6842b85340SShuo Chen // printf("%s\n", tls_error(sctx)); 6942b85340SShuo Chen} 70