loop-polarssl.cc revision a98d478e
1a98d478eSShuo Chen#include <polarssl/ctr_drbg.h> 2a98d478eSShuo Chen#include <polarssl/error.h> 3a98d478eSShuo Chen#include <polarssl/entropy.h> 4a98d478eSShuo Chen#include <polarssl/ssl.h> 5a98d478eSShuo Chen 6a98d478eSShuo Chen#include <polarssl/certs.h> 7a98d478eSShuo Chen 8a98d478eSShuo Chen#include <muduo/base/Thread.h> 9a98d478eSShuo Chen 10a98d478eSShuo Chen#include <boost/bind.hpp> 11a98d478eSShuo Chen 12a98d478eSShuo Chen#include <stdio.h> 13a98d478eSShuo Chen#include <sys/socket.h> 14a98d478eSShuo Chen 15a98d478eSShuo Chenbool useRSA = false; 16a98d478eSShuo Chenconst int N = 500; 17a98d478eSShuo Chen 18a98d478eSShuo Chen// FIXME: net_recv with buffer 19a98d478eSShuo Chen 20a98d478eSShuo Chenvoid clientThread(entropy_context* entropy, int* clientFd) 21a98d478eSShuo Chen{ 22a98d478eSShuo Chen ctr_drbg_context ctr_drbg; 23a98d478eSShuo Chen ctr_drbg_init(&ctr_drbg, entropy_func, entropy, NULL, 0); 24a98d478eSShuo Chen 25a98d478eSShuo Chen ssl_context ssl; 26a98d478eSShuo Chen bzero(&ssl, sizeof ssl); 27a98d478eSShuo Chen ssl_init(&ssl); 28a98d478eSShuo Chen ssl_set_rng(&ssl, ctr_drbg_random, &ctr_drbg); 29a98d478eSShuo Chen ssl_set_bio(&ssl, &net_recv, clientFd, &net_send, clientFd); 30a98d478eSShuo Chen ssl_set_endpoint(&ssl, SSL_IS_CLIENT); 31a98d478eSShuo Chen ssl_set_authmode(&ssl, SSL_VERIFY_NONE); 32a98d478eSShuo Chen 33a98d478eSShuo Chen for (int i = 0; i < N; ++i) 34a98d478eSShuo Chen { 35a98d478eSShuo Chen ssl_session_reset( &ssl ); 36a98d478eSShuo Chen int ret = 0; 37a98d478eSShuo Chen while ( (ret = ssl_handshake(&ssl)) != 0) 38a98d478eSShuo Chen { 39a98d478eSShuo Chen if (ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE) 40a98d478eSShuo Chen { 41a98d478eSShuo Chen printf("client handshake failed %d\n", ret); 42a98d478eSShuo Chen break; 43a98d478eSShuo Chen } 44a98d478eSShuo Chen } 45a98d478eSShuo Chen if (i == 0) 46a98d478eSShuo Chen printf("client done %s %s\n", ssl_get_version(&ssl), ssl_get_ciphersuite(&ssl)); 47a98d478eSShuo Chen } 48a98d478eSShuo Chen 49a98d478eSShuo Chen ssl_free(&ssl); 50a98d478eSShuo Chen} 51a98d478eSShuo Chen 52a98d478eSShuo Chenvoid serverThread(entropy_context* entropy, int* serverFd) 53a98d478eSShuo Chen{ 54a98d478eSShuo Chen const char* srv_cert = test_srv_crt_ec; 55a98d478eSShuo Chen const char* srv_key = test_srv_key_ec; 56a98d478eSShuo Chen if (useRSA) 57a98d478eSShuo Chen { 58a98d478eSShuo Chen srv_cert = test_srv_crt; 59a98d478eSShuo Chen srv_key = test_srv_key; 60a98d478eSShuo Chen } 61a98d478eSShuo Chen x509_crt cert; 62a98d478eSShuo Chen x509_crt_init(&cert); 63a98d478eSShuo Chen x509_crt_parse(&cert, reinterpret_cast<const unsigned char*>(srv_cert), strlen(srv_cert)); 64a98d478eSShuo Chen x509_crt_parse(&cert, reinterpret_cast<const unsigned char*>(test_ca_list), strlen(test_ca_list)); 65a98d478eSShuo Chen 66a98d478eSShuo Chen pk_context pkey; 67a98d478eSShuo Chen pk_init(&pkey); 68a98d478eSShuo Chen pk_parse_key(&pkey, reinterpret_cast<const unsigned char*>(srv_key), strlen(srv_key), NULL, 0); 69a98d478eSShuo Chen 70a98d478eSShuo Chen ctr_drbg_context ctr_drbg; 71a98d478eSShuo Chen ctr_drbg_init(&ctr_drbg, entropy_func, entropy, NULL, 0); 72a98d478eSShuo Chen 73a98d478eSShuo Chen ssl_context ssl_server; 74a98d478eSShuo Chen bzero(&ssl_server, sizeof ssl_server); 75a98d478eSShuo Chen ssl_init(&ssl_server); 76a98d478eSShuo Chen ssl_set_rng(&ssl_server, ctr_drbg_random, &ctr_drbg); 77a98d478eSShuo Chen ssl_set_bio(&ssl_server, &net_recv, serverFd, &net_send, serverFd); 78a98d478eSShuo Chen ssl_set_endpoint(&ssl_server, SSL_IS_SERVER); 79a98d478eSShuo Chen ssl_set_authmode(&ssl_server, SSL_VERIFY_NONE); 80a98d478eSShuo Chen ssl_set_ca_chain(&ssl_server, cert.next, NULL, NULL); 81a98d478eSShuo Chen ssl_set_own_cert(&ssl_server, &cert, &pkey); 82a98d478eSShuo Chen // ssl_set_dbg(&ssl_server, my_debug, (void*)"server"); 83a98d478eSShuo Chen ecp_group_id curves[] = { POLARSSL_ECP_DP_SECP256R1, POLARSSL_ECP_DP_NONE }; 84a98d478eSShuo Chen ssl_set_curves(&ssl_server, curves); 85a98d478eSShuo Chen 86a98d478eSShuo Chen for (int i = 0; i < N; ++i) 87a98d478eSShuo Chen { 88a98d478eSShuo Chen ssl_session_reset(&ssl_server); 89a98d478eSShuo Chen int ret = 0; 90a98d478eSShuo Chen while ( (ret = ssl_handshake(&ssl_server)) != 0) 91a98d478eSShuo Chen { 92a98d478eSShuo Chen if (ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE) 93a98d478eSShuo Chen { 94a98d478eSShuo Chen printf("server handshake failed %d\n", ret); 95a98d478eSShuo Chen break; 96a98d478eSShuo Chen } 97a98d478eSShuo Chen } 98a98d478eSShuo Chen if (i == 0) 99a98d478eSShuo Chen printf("server done %s %s\n", ssl_get_version(&ssl_server), ssl_get_ciphersuite(&ssl_server)); 100a98d478eSShuo Chen } 101a98d478eSShuo Chen 102a98d478eSShuo Chen ssl_free(&ssl_server); 103a98d478eSShuo Chen pk_free(&pkey); 104a98d478eSShuo Chen x509_crt_free(&cert); 105a98d478eSShuo Chen} 106a98d478eSShuo Chen 107a98d478eSShuo Chenint main(int argc, char* argv[]) 108a98d478eSShuo Chen{ 109a98d478eSShuo Chen unsigned char buf[16384] = { 0 }; 110a98d478eSShuo Chen entropy_context entropy; 111a98d478eSShuo Chen entropy_init(&entropy); 112a98d478eSShuo Chen 113a98d478eSShuo Chen if (argc > 1) 114a98d478eSShuo Chen useRSA = true; 115a98d478eSShuo Chen 116a98d478eSShuo Chen int fds[2]; 117a98d478eSShuo Chen if (::socketpair(AF_UNIX, SOCK_STREAM, 0, fds)) 118a98d478eSShuo Chen abort(); 119a98d478eSShuo Chen 120a98d478eSShuo Chen muduo::Thread client(boost::bind(&clientThread, &entropy, &fds[0]), "ssl client"); 121a98d478eSShuo Chen muduo::Thread server(boost::bind(&serverThread, &entropy, &fds[1]), "ssl server"); 122a98d478eSShuo Chen client.start(); 123a98d478eSShuo Chen server.start(); 124a98d478eSShuo Chen 125a98d478eSShuo Chen client.join(); 126a98d478eSShuo Chen server.join(); 127a98d478eSShuo Chen entropy_free(&entropy); 128a98d478eSShuo Chen} 129