21 #ifndef yaSSL_TEST_HPP
22 #define yaSSL_TEST_HPP
24 #include "runtime.hpp"
25 #include "openssl/ssl.h"
36 #ifdef TEST_IPV6 // don't require newer SDK for IPV4
40 #define SOCKET_T unsigned int
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <sys/ioctl.h>
48 #include <sys/types.h>
49 #include <sys/socket.h>
64 #pragma warning(disable:4244 4996)
68 #if !defined(_SOCKLEN_T) && (defined(_WIN32) || defined(__APPLE__))
69 typedef int socklen_t;
76 typedef int* ACCEPT_THIRD_T;
78 typedef socklen_t* ACCEPT_THIRD_T;
83 typedef sockaddr_in6 SOCKADDR_IN_T;
84 #define AF_INET_V AF_INET6
86 typedef sockaddr_in SOCKADDR_IN_T;
87 #define AF_INET_V AF_INET
92 #if !defined(_POSIX_THREADS) && defined(__hpux)
94 #define _POSIX_THREADS
98 #ifndef _POSIX_THREADS
99 typedef unsigned int THREAD_RETURN;
100 typedef HANDLE THREAD_TYPE;
101 #define YASSL_API __stdcall
103 typedef void* THREAD_RETURN;
104 typedef pthread_t THREAD_TYPE;
110 #ifdef _POSIX_THREADS
111 pthread_mutex_t mutex_;
112 pthread_cond_t cond_;
117 pthread_mutex_init(&mutex_, 0);
118 pthread_cond_init(&cond_, 0);
123 pthread_mutex_destroy(&mutex_);
124 pthread_cond_destroy(&cond_);
136 func_args(
int c = 0,
char** v = 0) : argc(c), argv(v) {}
138 void SetSignal(
tcp_ready* p) { signal_ = p; }
141 typedef THREAD_RETURN YASSL_API THREAD_FUNC(
void*);
143 void start_thread(THREAD_FUNC,
func_args*, THREAD_TYPE*);
144 void join_thread(THREAD_TYPE);
147 const char*
const yasslIP =
"127.0.0.1";
148 const unsigned short yasslPort = 11111;
152 const char*
const cert =
"../certs/client-cert.pem";
153 const char*
const key =
"../certs/client-key.pem";
155 const char*
const certSuite =
"../../certs/client-cert.pem";
156 const char*
const keySuite =
"../../certs/client-key.pem";
158 const char*
const certDebug =
"../../../certs/client-cert.pem";
159 const char*
const keyDebug =
"../../../certs/client-key.pem";
163 const char*
const svrCert =
"../certs/server-cert.pem";
164 const char*
const svrKey =
"../certs/server-key.pem";
166 const char*
const svrCert2 =
"../../certs/server-cert.pem";
167 const char*
const svrKey2 =
"../../certs/server-key.pem";
169 const char*
const svrCert3 =
"../../../certs/server-cert.pem";
170 const char*
const svrKey3 =
"../../../certs/server-key.pem";
174 const char*
const dsaCert =
"../certs/dsa-cert.pem";
175 const char*
const dsaKey =
"../certs/dsa512.der";
177 const char*
const dsaCert2 =
"../../certs/dsa-cert.pem";
178 const char*
const dsaKey2 =
"../../certs/dsa512.der";
180 const char*
const dsaCert3 =
"../../../certs/dsa-cert.pem";
181 const char*
const dsaKey3 =
"../../../certs/dsa512.der";
185 const char*
const caCert =
"../certs/ca-cert.pem";
186 const char*
const caCert2 =
"../../certs/ca-cert.pem";
187 const char*
const caCert3 =
"../../../certs/ca-cert.pem";
190 using namespace yaSSL;
193 inline void err_sys(
const char*
msg)
195 printf(
"yassl error: %s\n", msg);
201 static int PasswordCallBack(
char*,
int,
int,
void*);
205 static int PasswordCallBack(
char* passwd,
int sz,
int rw,
void* userdata)
207 strncpy(passwd,
"yassl123", sz);
212 inline void store_ca(
SSL_CTX* ctx)
215 if (SSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS)
216 if (SSL_CTX_load_verify_locations(ctx, caCert2, 0) != SSL_SUCCESS)
217 if (SSL_CTX_load_verify_locations(ctx, caCert3, 0) != SSL_SUCCESS)
218 err_sys(
"failed to use certificate: certs/cacert.pem");
221 if (SSL_CTX_load_verify_locations(ctx, cert, 0) != SSL_SUCCESS)
222 if (SSL_CTX_load_verify_locations(ctx, certSuite, 0) != SSL_SUCCESS)
223 if (SSL_CTX_load_verify_locations(ctx, certDebug,0) != SSL_SUCCESS)
224 err_sys(
"failed to use certificate: certs/client-cert.pem");
229 inline void set_certs(
SSL_CTX* ctx)
232 SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
235 if (SSL_CTX_use_certificate_file(ctx, cert, SSL_FILETYPE_PEM)
237 if (SSL_CTX_use_certificate_file(ctx, certSuite, SSL_FILETYPE_PEM)
239 if (SSL_CTX_use_certificate_file(ctx, certDebug, SSL_FILETYPE_PEM)
241 err_sys(
"failed to use certificate: certs/client-cert.pem");
244 if (SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM)
246 if (SSL_CTX_use_PrivateKey_file(ctx, keySuite, SSL_FILETYPE_PEM)
248 if (SSL_CTX_use_PrivateKey_file(ctx,keyDebug,SSL_FILETYPE_PEM)
250 err_sys(
"failed to use key file: certs/client-key.pem");
255 inline void set_serverCerts(
SSL_CTX* ctx)
258 SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
261 if (SSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)
263 if (SSL_CTX_use_certificate_file(ctx, svrCert2, SSL_FILETYPE_PEM)
265 if (SSL_CTX_use_certificate_file(ctx, svrCert3, SSL_FILETYPE_PEM)
267 err_sys(
"failed to use certificate: certs/server-cert.pem");
270 if (SSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)
272 if (SSL_CTX_use_PrivateKey_file(ctx, svrKey2, SSL_FILETYPE_PEM)
274 if (SSL_CTX_use_PrivateKey_file(ctx, svrKey3,SSL_FILETYPE_PEM)
276 err_sys(
"failed to use key file: certs/server-key.pem");
281 inline void set_dsaServerCerts(
SSL_CTX* ctx)
286 if (SSL_CTX_use_certificate_file(ctx, dsaCert, SSL_FILETYPE_PEM)
288 if (SSL_CTX_use_certificate_file(ctx, dsaCert2, SSL_FILETYPE_PEM)
290 if (SSL_CTX_use_certificate_file(ctx, dsaCert3, SSL_FILETYPE_PEM)
292 err_sys(
"failed to use certificate: certs/dsa-cert.pem");
295 if (SSL_CTX_use_PrivateKey_file(ctx, dsaKey, SSL_FILETYPE_ASN1)
297 if (SSL_CTX_use_PrivateKey_file(ctx, dsaKey2, SSL_FILETYPE_ASN1)
299 if (SSL_CTX_use_PrivateKey_file(ctx, dsaKey3,SSL_FILETYPE_ASN1)
301 err_sys(
"failed to use key file: certs/dsa512.der");
305 inline void set_args(
int& argc,
char**& argv,
func_args& args)
309 args.return_code = -1;
313 inline void tcp_set_nonblocking(SOCKET_T& sockfd)
317 unsigned long blocking = 1;
318 int ret = ioctlsocket(sockfd, FIONBIO, &blocking);
320 int flags = fcntl(sockfd, F_GETFL, 0);
321 int ret = fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
327 inline void tcp_socket(SOCKET_T& sockfd, SOCKADDR_IN_T& addr)
329 sockfd = socket(AF_INET_V, SOCK_STREAM, 0);
330 memset(&addr, 0,
sizeof(addr));
333 addr.sin6_family = AF_INET_V;
334 addr.sin6_port = htons(yasslPort);
335 addr.sin6_addr = in6addr_loopback;
351 addr.sin_family = AF_INET_V;
352 addr.sin_port = htons(yasslPort);
353 addr.sin_addr.s_addr = inet_addr(yasslIP);
359 inline void tcp_close(SOCKET_T& sockfd)
366 sockfd = (SOCKET_T) -1;
370 inline void tcp_connect(SOCKET_T& sockfd)
373 tcp_socket(sockfd, addr);
375 if (connect(sockfd, (
const sockaddr*)&addr,
sizeof(addr)) != 0) {
377 err_sys(
"tcp connect failed");
382 inline void tcp_listen(SOCKET_T& sockfd)
385 tcp_socket(sockfd, addr);
389 socklen_t len =
sizeof(on);
390 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
393 if (bind(sockfd, (
const sockaddr*)&addr,
sizeof(addr)) != 0) {
395 err_sys(
"tcp bind failed");
397 if (listen(sockfd, 3) != 0) {
399 err_sys(
"tcp listen failed");
405 inline void tcp_accept(SOCKET_T& sockfd, SOCKET_T& clientfd,
func_args& args)
409 SOCKADDR_IN_T client;
410 socklen_t client_len =
sizeof(client);
412 #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER)
415 pthread_mutex_lock(&ready.mutex_);
417 pthread_cond_signal(&ready.cond_);
418 pthread_mutex_unlock(&ready.mutex_);
421 clientfd = accept(sockfd, (sockaddr*)&client, (ACCEPT_THIRD_T)&client_len);
423 if (clientfd == (SOCKET_T) -1) {
425 err_sys(
"tcp accept failed");
429 tcp_set_nonblocking(clientfd);
434 inline void showPeer(
SSL* ssl)
436 X509* peer = SSL_get_peer_certificate(ssl);
438 char* issuer = X509_NAME_oneline(X509_get_issuer_name(peer), 0, 0);
439 char* subject = X509_NAME_oneline(X509_get_subject_name(peer), 0, 0);
441 printf(
"peer's cert info:\n issuer : %s\n subject: %s\n", issuer,
447 printf(
"peer has no cert!\n");
454 static unsigned char dh1024_p[] =
456 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
457 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
458 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
459 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
460 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
461 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
462 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
463 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
464 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
465 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
466 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
469 static unsigned char dh1024_g[] =
475 if ( (dh = DH_new()) ) {
476 dh->p = BN_bin2bn(dh1024_p,
sizeof(dh1024_p), 0);
477 dh->g = BN_bin2bn(dh1024_g,
sizeof(dh1024_g), 0);
479 if (!dh->p || !dh->g) {
483 SSL_CTX_set_tmp_dh(ctx, dh);
490 X509* err_cert = X509_STORE_CTX_get_current_cert(ctx);
491 int err = X509_STORE_CTX_get_error(ctx);
492 int depth = X509_STORE_CTX_get_error_depth(ctx);
495 if (err_cert && depth == 0 && err == TaoCrypt::SIG_OTHER_E)
502 #endif // yaSSL_TEST_HPP