PageRenderTime 68ms CodeModel.GetById 17ms RepoModel.GetById 3ms app.codeStats 0ms

/cyassl/test.h

https://github.com/andersmalm/cyassl
C Header | 924 lines | 727 code | 171 blank | 26 comment | 103 complexity | 0d2326d4bf015790c04479459a23cb24 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* test.h */
  2. #ifndef CyaSSL_TEST_H
  3. #define CyaSSL_TEST_H
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <assert.h>
  7. #include <ctype.h>
  8. #include <cyassl/ssl.h>
  9. #include <cyassl/ctaocrypt/types.h>
  10. #ifdef USE_WINDOWS_API
  11. #include <winsock2.h>
  12. #include <process.h>
  13. #ifdef TEST_IPV6 /* don't require newer SDK for IPV4 */
  14. #include <ws2tcpip.h>
  15. #include <wspiapi.h>
  16. #endif
  17. #define SOCKET_T unsigned int
  18. #else
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <netdb.h>
  22. #include <netinet/in.h>
  23. #include <netinet/tcp.h>
  24. #include <arpa/inet.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/time.h>
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <pthread.h>
  30. #include <fcntl.h>
  31. #ifdef TEST_IPV6
  32. #include <netdb.h>
  33. #endif
  34. #define SOCKET_T int
  35. #ifndef SO_NOSIGPIPE
  36. #include <signal.h> /* ignore SIGPIPE */
  37. #endif
  38. #endif /* USE_WINDOWS_API */
  39. #ifdef HAVE_CAVIUM
  40. #include "cavium_sysdep.h"
  41. #include "cavium_common.h"
  42. #include "cavium_ioctl.h"
  43. #endif
  44. #ifdef _MSC_VER
  45. /* disable conversion warning */
  46. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  47. #pragma warning(disable:4244 4996)
  48. #endif
  49. #if defined(__MACH__) || defined(USE_WINDOWS_API)
  50. #ifndef _SOCKLEN_T
  51. typedef int socklen_t;
  52. #endif
  53. #endif
  54. /* HPUX doesn't use socklent_t for third parameter to accept */
  55. #if !defined(__hpux__)
  56. typedef socklen_t* ACCEPT_THIRD_T;
  57. #else
  58. typedef int* ACCEPT_THIRD_T;
  59. #endif
  60. #ifdef USE_WINDOWS_API
  61. #define CloseSocket(s) closesocket(s)
  62. #define StartTCP() { WSADATA wsd; WSAStartup(0x0002, &wsd); }
  63. #else
  64. #define CloseSocket(s) close(s)
  65. #define StartTCP()
  66. #endif
  67. #ifdef SINGLE_THREADED
  68. typedef unsigned int THREAD_RETURN;
  69. typedef void* THREAD_TYPE;
  70. #define CYASSL_THREAD
  71. #else
  72. #ifdef _POSIX_THREADS
  73. typedef void* THREAD_RETURN;
  74. typedef pthread_t THREAD_TYPE;
  75. #define CYASSL_THREAD
  76. #define INFINITE -1
  77. #define WAIT_OBJECT_0 0L
  78. #else
  79. typedef unsigned int THREAD_RETURN;
  80. typedef HANDLE THREAD_TYPE;
  81. #define CYASSL_THREAD __stdcall
  82. #endif
  83. #endif
  84. #ifdef TEST_IPV6
  85. typedef struct sockaddr_in6 SOCKADDR_IN_T;
  86. #define AF_INET_V AF_INET6
  87. #else
  88. typedef struct sockaddr_in SOCKADDR_IN_T;
  89. #define AF_INET_V AF_INET
  90. #endif
  91. #define SERVER_DEFAULT_VERSION 3
  92. #define CLIENT_DEFAULT_VERSION 3
  93. /* all certs relative to CyaSSL home directory now */
  94. #define caCert "./certs/ca-cert.pem"
  95. #define eccCert "./certs/server-ecc.pem"
  96. #define eccKey "./certs/ecc-key.pem"
  97. #define svrCert "./certs/server-cert.pem"
  98. #define svrKey "./certs/server-key.pem"
  99. #define cliCert "./certs/client-cert.pem"
  100. #define cliKey "./certs/client-key.pem"
  101. #define ntruCert "./certs/ntru-cert.pem"
  102. #define ntruKey "./certs/ntru-key.raw"
  103. #define dhParam "./certs/dh2048.pem"
  104. #define cliEccKey "./certs/ecc-client-key.pem"
  105. #define cliEccCert "./certs/client-ecc-cert.pem"
  106. #define crlPemDir "./certs/crl"
  107. typedef struct tcp_ready {
  108. int ready; /* predicate */
  109. #ifdef _POSIX_THREADS
  110. pthread_mutex_t mutex;
  111. pthread_cond_t cond;
  112. #endif
  113. } tcp_ready;
  114. void InitTcpReady(tcp_ready*);
  115. void FreeTcpReady(tcp_ready*);
  116. typedef struct func_args {
  117. int argc;
  118. char** argv;
  119. int return_code;
  120. tcp_ready* signal;
  121. } func_args;
  122. void wait_tcp_ready(func_args*);
  123. typedef THREAD_RETURN CYASSL_THREAD THREAD_FUNC(void*);
  124. void start_thread(THREAD_FUNC, func_args*, THREAD_TYPE*);
  125. void join_thread(THREAD_TYPE);
  126. /* yaSSL */
  127. static const char* const yasslIP = "127.0.0.1";
  128. static const word16 yasslPort = 11111;
  129. static INLINE void err_sys(const char* msg)
  130. {
  131. printf("yassl error: %s\n", msg);
  132. if (msg)
  133. exit(EXIT_FAILURE);
  134. }
  135. #define MY_EX_USAGE 2
  136. extern int myoptind;
  137. extern char* myoptarg;
  138. static INLINE int mygetopt(int argc, char** argv, const char* optstring)
  139. {
  140. static char* next = NULL;
  141. char c;
  142. char* cp;
  143. if (myoptind == 0)
  144. next = NULL; /* we're starting new/over */
  145. if (next == NULL || *next == '\0') {
  146. if (myoptind == 0)
  147. myoptind++;
  148. if (myoptind >= argc || argv[myoptind][0] != '-' ||
  149. argv[myoptind][1] == '\0') {
  150. myoptarg = NULL;
  151. if (myoptind < argc)
  152. myoptarg = argv[myoptind];
  153. return -1;
  154. }
  155. if (strcmp(argv[myoptind], "--") == 0) {
  156. myoptind++;
  157. myoptarg = NULL;
  158. if (myoptind < argc)
  159. myoptarg = argv[myoptind];
  160. return -1;
  161. }
  162. next = argv[myoptind];
  163. next++; /* skip - */
  164. myoptind++;
  165. }
  166. c = *next++;
  167. /* The C++ strchr can return a different value */
  168. cp = (char*)strchr(optstring, c);
  169. if (cp == NULL || c == ':')
  170. return '?';
  171. cp++;
  172. if (*cp == ':') {
  173. if (*next != '\0') {
  174. myoptarg = next;
  175. next = NULL;
  176. }
  177. else if (myoptind < argc) {
  178. myoptarg = argv[myoptind];
  179. myoptind++;
  180. }
  181. else
  182. return '?';
  183. }
  184. return c;
  185. }
  186. #ifdef OPENSSL_EXTRA
  187. static INLINE int PasswordCallBack(char* passwd, int sz, int rw, void* userdata)
  188. {
  189. (void)rw;
  190. (void)userdata;
  191. strncpy(passwd, "yassl123", sz);
  192. return 8;
  193. }
  194. #endif
  195. static INLINE void showPeer(CYASSL* ssl)
  196. {
  197. #ifdef OPENSSL_EXTRA
  198. CYASSL_CIPHER* cipher;
  199. CYASSL_X509* peer = CyaSSL_get_peer_certificate(ssl);
  200. if (peer) {
  201. char* altName;
  202. char* issuer = CyaSSL_X509_NAME_oneline(
  203. CyaSSL_X509_get_issuer_name(peer), 0, 0);
  204. char* subject = CyaSSL_X509_NAME_oneline(
  205. CyaSSL_X509_get_subject_name(peer), 0, 0);
  206. byte serial[32];
  207. int ret;
  208. int sz = sizeof(serial);
  209. printf("peer's cert info:\n issuer : %s\n subject: %s\n", issuer,
  210. subject);
  211. while ( (altName = CyaSSL_X509_get_next_altname(peer)) )
  212. printf(" altname = %s\n", altName);
  213. ret = CyaSSL_X509_get_serial_number(peer, serial, &sz);
  214. if (ret == 0) {
  215. int i;
  216. int strLen;
  217. char serialMsg[80];
  218. /* testsuite has multiple threads writing to stdout, get output
  219. message ready to write once */
  220. strLen = sprintf(serialMsg, " serial number");
  221. for (i = 0; i < sz; i++)
  222. sprintf(serialMsg + strLen + (i*3), ":%02x ", serial[i]);
  223. printf("%s\n", serialMsg);
  224. }
  225. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  226. XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL);
  227. }
  228. else
  229. printf("peer has no cert!\n");
  230. printf("SSL version is %s\n", CyaSSL_get_version(ssl));
  231. cipher = CyaSSL_get_current_cipher(ssl);
  232. printf("SSL cipher suite is %s\n", CyaSSL_CIPHER_get_name(cipher));
  233. #endif
  234. #if defined(SESSION_CERTS) && defined(SHOW_CERTS)
  235. {
  236. CYASSL_X509_CHAIN* chain = CyaSSL_get_peer_chain(ssl);
  237. int count = CyaSSL_get_chain_count(chain);
  238. int i;
  239. for (i = 0; i < count; i++) {
  240. int length;
  241. unsigned char buffer[3072];
  242. CyaSSL_get_chain_cert_pem(chain,i,buffer, sizeof(buffer), &length);
  243. buffer[length] = 0;
  244. printf("cert %d has length %d data = \n%s\n", i, length, buffer);
  245. }
  246. }
  247. #endif
  248. (void)ssl;
  249. }
  250. static INLINE void build_addr(SOCKADDR_IN_T* addr,
  251. const char* peer, word16 port)
  252. {
  253. #ifndef TEST_IPV6
  254. const char* host = peer;
  255. /* peer could be in human readable form */
  256. if (peer != INADDR_ANY && isalpha(peer[0])) {
  257. struct hostent* entry = gethostbyname(peer);
  258. if (entry) {
  259. struct sockaddr_in tmp;
  260. memset(&tmp, 0, sizeof(struct sockaddr_in));
  261. memcpy(&tmp.sin_addr.s_addr, entry->h_addr_list[0],
  262. entry->h_length);
  263. host = inet_ntoa(tmp.sin_addr);
  264. }
  265. else
  266. err_sys("no entry for host");
  267. }
  268. #endif
  269. memset(addr, 0, sizeof(SOCKADDR_IN_T));
  270. #ifndef TEST_IPV6
  271. addr->sin_family = AF_INET_V;
  272. addr->sin_port = htons(port);
  273. if (host == INADDR_ANY)
  274. addr->sin_addr.s_addr = INADDR_ANY;
  275. else
  276. addr->sin_addr.s_addr = inet_addr(host);
  277. #else
  278. (void)peer;
  279. addr->sin6_family = AF_INET_V;
  280. addr->sin6_port = htons(port);
  281. addr->sin6_addr = in6addr_loopback;
  282. #endif
  283. }
  284. static INLINE void tcp_socket(SOCKET_T* sockfd, int udp)
  285. {
  286. if (udp)
  287. *sockfd = socket(AF_INET_V, SOCK_DGRAM, 0);
  288. else
  289. *sockfd = socket(AF_INET_V, SOCK_STREAM, 0);
  290. #ifndef USE_WINDOWS_API
  291. #ifdef SO_NOSIGPIPE
  292. {
  293. int on = 1;
  294. socklen_t len = sizeof(on);
  295. int res = setsockopt(*sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, len);
  296. if (res < 0)
  297. err_sys("setsockopt SO_NOSIGPIPE failed\n");
  298. }
  299. #else /* no S_NOSIGPIPE */
  300. signal(SIGPIPE, SIG_IGN);
  301. #endif /* S_NOSIGPIPE */
  302. #if defined(TCP_NODELAY)
  303. if (!udp)
  304. {
  305. int on = 1;
  306. socklen_t len = sizeof(on);
  307. int res = setsockopt(*sockfd, IPPROTO_TCP, TCP_NODELAY, &on, len);
  308. if (res < 0)
  309. err_sys("setsockopt TCP_NODELAY failed\n");
  310. }
  311. #endif
  312. #endif /* USE_WINDOWS_API */
  313. }
  314. static INLINE void tcp_connect(SOCKET_T* sockfd, const char* ip, word16 port,
  315. int udp)
  316. {
  317. SOCKADDR_IN_T addr;
  318. build_addr(&addr, ip, port);
  319. tcp_socket(sockfd, udp);
  320. if (!udp) {
  321. if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
  322. err_sys("tcp connect failed");
  323. }
  324. }
  325. static INLINE void udp_connect(SOCKET_T* sockfd, void* addr, int addrSz)
  326. {
  327. if (connect(*sockfd, (const struct sockaddr*)addr, addrSz) != 0)
  328. err_sys("tcp connect failed");
  329. }
  330. enum {
  331. TEST_SELECT_FAIL,
  332. TEST_TIMEOUT,
  333. TEST_RECV_READY,
  334. TEST_ERROR_READY
  335. };
  336. static INLINE int tcp_select(SOCKET_T socketfd, unsigned int to_sec)
  337. {
  338. fd_set recvfds, errfds;
  339. SOCKET_T nfds = socketfd + 1;
  340. struct timeval timeout = {to_sec, 0};
  341. int result;
  342. FD_ZERO(&recvfds);
  343. FD_SET(socketfd, &recvfds);
  344. FD_ZERO(&errfds);
  345. FD_SET(socketfd, &errfds);
  346. result = select(nfds, &recvfds, NULL, &errfds, &timeout);
  347. if (result == 0)
  348. return TEST_TIMEOUT;
  349. else if (result > 0) {
  350. if (FD_ISSET(socketfd, &recvfds))
  351. return TEST_RECV_READY;
  352. else if(FD_ISSET(socketfd, &errfds))
  353. return TEST_ERROR_READY;
  354. }
  355. return TEST_SELECT_FAIL;
  356. }
  357. static INLINE void tcp_listen(SOCKET_T* sockfd, int port, int useAnyAddr,
  358. int udp)
  359. {
  360. SOCKADDR_IN_T addr;
  361. /* don't use INADDR_ANY by default, firewall may block, make user switch
  362. on */
  363. build_addr(&addr, (useAnyAddr ? INADDR_ANY : yasslIP), port);
  364. tcp_socket(sockfd, udp);
  365. #ifndef USE_WINDOWS_API
  366. {
  367. int on = 1;
  368. socklen_t len = sizeof(on);
  369. setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
  370. }
  371. #endif
  372. if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
  373. err_sys("tcp bind failed");
  374. if (!udp) {
  375. if (listen(*sockfd, 5) != 0)
  376. err_sys("tcp listen failed");
  377. }
  378. }
  379. static INLINE int udp_read_connect(SOCKET_T sockfd)
  380. {
  381. SOCKADDR_IN_T cliaddr;
  382. byte b[1500];
  383. int n;
  384. socklen_t len = sizeof(cliaddr);
  385. n = (int)recvfrom(sockfd, (char*)b, sizeof(b), MSG_PEEK,
  386. (struct sockaddr*)&cliaddr, &len);
  387. if (n > 0) {
  388. if (connect(sockfd, (const struct sockaddr*)&cliaddr,
  389. sizeof(cliaddr)) != 0)
  390. err_sys("udp connect failed");
  391. }
  392. else
  393. err_sys("recvfrom failed");
  394. return sockfd;
  395. }
  396. static INLINE void udp_accept(SOCKET_T* sockfd, int* clientfd, int useAnyAddr,
  397. func_args* args)
  398. {
  399. SOCKADDR_IN_T addr;
  400. (void)args;
  401. build_addr(&addr, (useAnyAddr ? INADDR_ANY : yasslIP), yasslPort);
  402. tcp_socket(sockfd, 1);
  403. #ifndef USE_WINDOWS_API
  404. {
  405. int on = 1;
  406. socklen_t len = sizeof(on);
  407. setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
  408. }
  409. #endif
  410. if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
  411. err_sys("tcp bind failed");
  412. #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER)
  413. /* signal ready to accept data */
  414. {
  415. tcp_ready* ready = args->signal;
  416. pthread_mutex_lock(&ready->mutex);
  417. ready->ready = 1;
  418. pthread_cond_signal(&ready->cond);
  419. pthread_mutex_unlock(&ready->mutex);
  420. }
  421. #endif
  422. *clientfd = udp_read_connect(*sockfd);
  423. }
  424. static INLINE void tcp_accept(SOCKET_T* sockfd, int* clientfd, func_args* args,
  425. int port, int useAnyAddr, int udp)
  426. {
  427. SOCKADDR_IN_T client;
  428. socklen_t client_len = sizeof(client);
  429. if (udp) {
  430. udp_accept(sockfd, clientfd, useAnyAddr, args);
  431. return;
  432. }
  433. tcp_listen(sockfd, port, useAnyAddr, udp);
  434. #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER)
  435. /* signal ready to tcp_accept */
  436. {
  437. tcp_ready* ready = args->signal;
  438. pthread_mutex_lock(&ready->mutex);
  439. ready->ready = 1;
  440. pthread_cond_signal(&ready->cond);
  441. pthread_mutex_unlock(&ready->mutex);
  442. }
  443. #endif
  444. *clientfd = accept(*sockfd, (struct sockaddr*)&client,
  445. (ACCEPT_THIRD_T)&client_len);
  446. if (*clientfd == -1)
  447. err_sys("tcp accept failed");
  448. }
  449. static INLINE void tcp_set_nonblocking(SOCKET_T* sockfd)
  450. {
  451. #ifdef USE_WINDOWS_API
  452. unsigned long blocking = 1;
  453. int ret = ioctlsocket(*sockfd, FIONBIO, &blocking);
  454. #else
  455. int flags = fcntl(*sockfd, F_GETFL, 0);
  456. fcntl(*sockfd, F_SETFL, flags | O_NONBLOCK);
  457. #endif
  458. }
  459. #ifndef NO_PSK
  460. static INLINE unsigned int my_psk_client_cb(CYASSL* ssl, const char* hint,
  461. char* identity, unsigned int id_max_len, unsigned char* key,
  462. unsigned int key_max_len)
  463. {
  464. (void)ssl;
  465. (void)hint;
  466. (void)key_max_len;
  467. /* identity is OpenSSL testing default for openssl s_client, keep same */
  468. strncpy(identity, "Client_identity", id_max_len);
  469. /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using
  470. unsigned binary */
  471. key[0] = 26;
  472. key[1] = 43;
  473. key[2] = 60;
  474. key[3] = 77;
  475. return 4; /* length of key in octets or 0 for error */
  476. }
  477. static INLINE unsigned int my_psk_server_cb(CYASSL* ssl, const char* identity,
  478. unsigned char* key, unsigned int key_max_len)
  479. {
  480. (void)ssl;
  481. (void)key_max_len;
  482. /* identity is OpenSSL testing default for openssl s_client, keep same */
  483. if (strncmp(identity, "Client_identity", 15) != 0)
  484. return 0;
  485. /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using
  486. unsigned binary */
  487. key[0] = 26;
  488. key[1] = 43;
  489. key[2] = 60;
  490. key[3] = 77;
  491. return 4; /* length of key in octets or 0 for error */
  492. }
  493. #endif /* NO_PSK */
  494. #ifdef USE_WINDOWS_API
  495. #define WIN32_LEAN_AND_MEAN
  496. #include <windows.h>
  497. static INLINE double current_time()
  498. {
  499. static int init = 0;
  500. static LARGE_INTEGER freq;
  501. LARGE_INTEGER count;
  502. if (!init) {
  503. QueryPerformanceFrequency(&freq);
  504. init = 1;
  505. }
  506. QueryPerformanceCounter(&count);
  507. return (double)count.QuadPart / freq.QuadPart;
  508. }
  509. #else
  510. #include <sys/time.h>
  511. static INLINE double current_time(void)
  512. {
  513. struct timeval tv;
  514. gettimeofday(&tv, 0);
  515. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  516. }
  517. #endif /* USE_WINDOWS_API */
  518. #if defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  519. enum {
  520. CYASSL_CA = 1,
  521. CYASSL_CERT = 2,
  522. CYASSL_KEY = 3
  523. };
  524. static INLINE void load_buffer(CYASSL_CTX* ctx, const char* fname, int type)
  525. {
  526. /* test buffer load */
  527. long sz = 0;
  528. byte buff[10000];
  529. FILE* file = fopen(fname, "rb");
  530. if (!file)
  531. err_sys("can't open file for buffer load "
  532. "Please run from CyaSSL home directory if not");
  533. fseek(file, 0, SEEK_END);
  534. sz = ftell(file);
  535. rewind(file);
  536. fread(buff, sizeof(buff), 1, file);
  537. if (type == CYASSL_CA) {
  538. if (CyaSSL_CTX_load_verify_buffer(ctx, buff, sz, SSL_FILETYPE_PEM)
  539. != SSL_SUCCESS)
  540. err_sys("can't load buffer ca file");
  541. }
  542. else if (type == CYASSL_CERT) {
  543. if (CyaSSL_CTX_use_certificate_buffer(ctx, buff, sz,
  544. SSL_FILETYPE_PEM) != SSL_SUCCESS)
  545. err_sys("can't load buffer cert file");
  546. }
  547. else if (type == CYASSL_KEY) {
  548. if (CyaSSL_CTX_use_PrivateKey_buffer(ctx, buff, sz,
  549. SSL_FILETYPE_PEM) != SSL_SUCCESS)
  550. err_sys("can't load buffer key file");
  551. }
  552. }
  553. #endif /* NO_FILESYSTEM */
  554. #ifdef VERIFY_CALLBACK
  555. static INLINE int myVerify(int preverify, CYASSL_X509_STORE_CTX* store)
  556. {
  557. char buffer[80];
  558. #ifdef OPENSSL_EXTRA
  559. CYASSL_X509* peer;
  560. #endif
  561. printf("In verification callback, error = %d, %s\n", store->error,
  562. CyaSSL_ERR_error_string(store->error, buffer));
  563. #ifdef OPENSSL_EXTRA
  564. peer = store->current_cert;
  565. if (peer) {
  566. char* issuer = CyaSSL_X509_NAME_oneline(
  567. CyaSSL_X509_get_issuer_name(peer), 0, 0);
  568. char* subject = CyaSSL_X509_NAME_oneline(
  569. CyaSSL_X509_get_subject_name(peer), 0, 0);
  570. printf("peer's cert info:\n issuer : %s\n subject: %s\n", issuer,
  571. subject);
  572. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  573. XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL);
  574. }
  575. else
  576. printf("peer has no cert!\n");
  577. #endif
  578. printf("Subject's domain name is %s\n", store->domain);
  579. printf("Allowing to continue anyway (shouldn't do this, EVER!!!)\n");
  580. return 1;
  581. }
  582. #endif /* VERIFY_CALLBACK */
  583. #ifdef HAVE_CRL
  584. static INLINE void CRL_CallBack(const char* url)
  585. {
  586. printf("CRL callback url = %s\n", url);
  587. }
  588. #endif
  589. #ifndef NO_CERTS
  590. static INLINE void CaCb(unsigned char* der, int sz, int type)
  591. {
  592. (void)der;
  593. printf("Got CA cache add callback, derSz = %d, type = %d\n", sz, type);
  594. }
  595. static INLINE void SetDH(CYASSL* ssl)
  596. {
  597. /* dh1024 p */
  598. static unsigned char p[] =
  599. {
  600. 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
  601. 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
  602. 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
  603. 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
  604. 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
  605. 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
  606. 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
  607. 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
  608. 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
  609. 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
  610. 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
  611. };
  612. /* dh1024 g */
  613. static unsigned char g[] =
  614. {
  615. 0x02,
  616. };
  617. CyaSSL_SetTmpDH(ssl, p, sizeof(p), g, sizeof(g));
  618. }
  619. static INLINE void SetDHCtx(CYASSL_CTX* ctx)
  620. {
  621. /* dh1024 p */
  622. static unsigned char p[] =
  623. {
  624. 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
  625. 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
  626. 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
  627. 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
  628. 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
  629. 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
  630. 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
  631. 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
  632. 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
  633. 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
  634. 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
  635. };
  636. /* dh1024 g */
  637. static unsigned char g[] =
  638. {
  639. 0x02,
  640. };
  641. CyaSSL_CTX_SetTmpDH(ctx, p, sizeof(p), g, sizeof(g));
  642. }
  643. #endif /* !NO_CERTS */
  644. #ifdef HAVE_CAVIUM
  645. static INLINE int OpenNitroxDevice(int dma_mode,int dev_id)
  646. {
  647. Csp1CoreAssignment core_assign;
  648. Uint32 device;
  649. if (CspInitialize(CAVIUM_DIRECT,CAVIUM_DEV_ID))
  650. return -1;
  651. if (Csp1GetDevType(&device))
  652. return -1;
  653. if (device != NPX_DEVICE) {
  654. if (ioctl(gpkpdev_hdlr[CAVIUM_DEV_ID], IOCTL_CSP1_GET_CORE_ASSIGNMENT,
  655. (Uint32 *)&core_assign)!= 0)
  656. return -1;
  657. }
  658. CspShutdown(CAVIUM_DEV_ID);
  659. return CspInitialize(dma_mode, dev_id);
  660. }
  661. #endif /* HAVE_CAVIUM */
  662. #ifdef USE_WINDOWS_API
  663. /* do back x number of directories */
  664. static INLINE void ChangeDirBack(int x)
  665. {
  666. char path[MAX_PATH];
  667. if (x == 1)
  668. strncpy(path, "..\\", MAX_PATH);
  669. else if (x == 2)
  670. strncpy(path, "..\\..\\", MAX_PATH);
  671. else if (x == 3)
  672. strncpy(path, "..\\..\\..\\", MAX_PATH);
  673. else if (x == 4)
  674. strncpy(path, "..\\..\\..\\..\\", MAX_PATH);
  675. else
  676. strncpy(path, ".\\", MAX_PATH);
  677. SetCurrentDirectoryA(path);
  678. }
  679. /* does current dir contain str */
  680. static INLINE int CurrentDir(const char* str)
  681. {
  682. char path[MAX_PATH];
  683. char* baseName;
  684. GetCurrentDirectoryA(sizeof(path), path);
  685. baseName = strrchr(path, '\\');
  686. if (baseName)
  687. baseName++;
  688. else
  689. baseName = path;
  690. if (strstr(baseName, str))
  691. return 1;
  692. return 0;
  693. }
  694. #else
  695. #ifndef MAX_PATH
  696. #define MAX_PATH 256
  697. #endif
  698. /* do back x number of directories */
  699. static INLINE void ChangeDirBack(int x)
  700. {
  701. char path[MAX_PATH];
  702. if (x == 1)
  703. strncpy(path, "../", MAX_PATH);
  704. else if (x == 2)
  705. strncpy(path, "../../", MAX_PATH);
  706. else if (x == 3)
  707. strncpy(path, "../../../", MAX_PATH);
  708. else if (x == 4)
  709. strncpy(path, "../../../../", MAX_PATH);
  710. else
  711. strncpy(path, "./", MAX_PATH);
  712. if (chdir(path) < 0)
  713. printf("chdir to %s failed\n", path);
  714. }
  715. /* does current dir contain str */
  716. static INLINE int CurrentDir(const char* str)
  717. {
  718. char path[MAX_PATH];
  719. char* baseName;
  720. if (getcwd(path, sizeof(path)) == NULL) {
  721. printf("no current dir?\n");
  722. return 0;
  723. }
  724. baseName = strrchr(path, '/');
  725. if (baseName)
  726. baseName++;
  727. else
  728. baseName = path;
  729. if (strstr(baseName, str))
  730. return 1;
  731. return 0;
  732. }
  733. #endif /* USE_WINDOWS_API */
  734. #endif /* CyaSSL_TEST_H */