test.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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 SOCKET
  18. #define SNPRINTF _snprintf
  19. #elif defined(CYASSL_MDK_ARM)
  20. #include <string.h>
  21. #else
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #ifndef CYASSL_LEANPSK
  25. #include <unistd.h>
  26. #include <netdb.h>
  27. #include <netinet/in.h>
  28. #include <netinet/tcp.h>
  29. #include <arpa/inet.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/time.h>
  32. #include <sys/socket.h>
  33. #include <pthread.h>
  34. #include <fcntl.h>
  35. #ifdef TEST_IPV6
  36. #include <netdb.h>
  37. #endif
  38. #endif
  39. #define SOCKET_T int
  40. #ifndef SO_NOSIGPIPE
  41. #include <signal.h> /* ignore SIGPIPE */
  42. #endif
  43. #define SNPRINTF snprintf
  44. #endif /* USE_WINDOWS_API */
  45. #ifdef HAVE_CAVIUM
  46. #include "cavium_sysdep.h"
  47. #include "cavium_common.h"
  48. #include "cavium_ioctl.h"
  49. #endif
  50. #ifdef _MSC_VER
  51. /* disable conversion warning */
  52. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  53. #pragma warning(disable:4244 4996)
  54. #endif
  55. #if defined(__MACH__) || defined(USE_WINDOWS_API)
  56. #ifndef _SOCKLEN_T
  57. typedef int socklen_t;
  58. #endif
  59. #endif
  60. /* HPUX doesn't use socklent_t for third parameter to accept, unless
  61. _XOPEN_SOURCE_EXTENDED is defined */
  62. #if !defined(__hpux__) && !defined(CYASSL_MDK_ARM)
  63. typedef socklen_t* ACCEPT_THIRD_T;
  64. #else
  65. #if defined _XOPEN_SOURCE_EXTENDED
  66. typedef socklen_t* ACCEPT_THIRD_T;
  67. #else
  68. typedef int* ACCEPT_THIRD_T;
  69. #endif
  70. #endif
  71. #ifdef USE_WINDOWS_API
  72. #define CloseSocket(s) closesocket(s)
  73. #define StartTCP() { WSADATA wsd; WSAStartup(0x0002, &wsd); }
  74. #elif defined(CYASSL_MDK_ARM)
  75. #define CloseSocket(s) closesocket(s)
  76. #define StartTCP()
  77. #else
  78. #define CloseSocket(s) close(s)
  79. #define StartTCP()
  80. #endif
  81. #ifdef SINGLE_THREADED
  82. typedef unsigned int THREAD_RETURN;
  83. typedef void* THREAD_TYPE;
  84. #define CYASSL_THREAD
  85. #else
  86. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  87. typedef void* THREAD_RETURN;
  88. typedef pthread_t THREAD_TYPE;
  89. #define CYASSL_THREAD
  90. #define INFINITE -1
  91. #define WAIT_OBJECT_0 0L
  92. #elif defined(CYASSL_MDK_ARM)
  93. typedef unsigned int THREAD_RETURN;
  94. typedef int THREAD_TYPE;
  95. #define CYASSL_THREAD
  96. #else
  97. typedef unsigned int THREAD_RETURN;
  98. typedef intptr_t THREAD_TYPE;
  99. #define CYASSL_THREAD __stdcall
  100. #endif
  101. #endif
  102. #ifdef TEST_IPV6
  103. typedef struct sockaddr_in6 SOCKADDR_IN_T;
  104. #define AF_INET_V AF_INET6
  105. #else
  106. typedef struct sockaddr_in SOCKADDR_IN_T;
  107. #define AF_INET_V AF_INET
  108. #endif
  109. #define SERVER_DEFAULT_VERSION 3
  110. #define SERVER_DTLS_DEFAULT_VERSION (-2)
  111. #define SERVER_INVALID_VERSION (-99)
  112. #define CLIENT_DEFAULT_VERSION 3
  113. #define CLIENT_DTLS_DEFAULT_VERSION (-2)
  114. #define CLIENT_INVALID_VERSION (-99)
  115. /* all certs relative to CyaSSL home directory now */
  116. #define caCert "./certs/ca-cert.pem"
  117. #define eccCert "./certs/server-ecc.pem"
  118. #define eccKey "./certs/ecc-key.pem"
  119. #define svrCert "./certs/server-cert.pem"
  120. #define svrKey "./certs/server-key.pem"
  121. #define cliCert "./certs/client-cert.pem"
  122. #define cliKey "./certs/client-key.pem"
  123. #define ntruCert "./certs/ntru-cert.pem"
  124. #define ntruKey "./certs/ntru-key.raw"
  125. #define dhParam "./certs/dh2048.pem"
  126. #define cliEccKey "./certs/ecc-client-key.pem"
  127. #define cliEccCert "./certs/client-ecc-cert.pem"
  128. #define crlPemDir "./certs/crl"
  129. typedef struct tcp_ready {
  130. int ready; /* predicate */
  131. int port;
  132. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  133. pthread_mutex_t mutex;
  134. pthread_cond_t cond;
  135. #endif
  136. } tcp_ready;
  137. void InitTcpReady(tcp_ready*);
  138. void FreeTcpReady(tcp_ready*);
  139. typedef CYASSL_METHOD* (*method_provider)(void);
  140. typedef void (*ctx_callback)(CYASSL_CTX* ctx);
  141. typedef void (*ssl_callback)(CYASSL* ssl);
  142. typedef struct callback_functions {
  143. method_provider method;
  144. ctx_callback ctx_ready;
  145. ssl_callback ssl_ready;
  146. ssl_callback on_result;
  147. } callback_functions;
  148. typedef struct func_args {
  149. int argc;
  150. char** argv;
  151. int return_code;
  152. tcp_ready* signal;
  153. callback_functions *callbacks;
  154. } func_args;
  155. void wait_tcp_ready(func_args*);
  156. typedef THREAD_RETURN CYASSL_THREAD THREAD_FUNC(void*);
  157. void start_thread(THREAD_FUNC, func_args*, THREAD_TYPE*);
  158. void join_thread(THREAD_TYPE);
  159. /* yaSSL */
  160. #ifndef TEST_IPV6
  161. static const char* const yasslIP = "127.0.0.1";
  162. #else
  163. static const char* const yasslIP = "::1";
  164. #endif
  165. static const word16 yasslPort = 11111;
  166. static INLINE void err_sys(const char* msg)
  167. {
  168. printf("yassl error: %s\n", msg);
  169. #ifndef CYASSL_MDK_SHELL
  170. if (msg)
  171. exit(EXIT_FAILURE);
  172. #endif
  173. }
  174. #define MY_EX_USAGE 2
  175. extern int myoptind;
  176. extern char* myoptarg;
  177. static INLINE int mygetopt(int argc, char** argv, const char* optstring)
  178. {
  179. static char* next = NULL;
  180. char c;
  181. char* cp;
  182. if (myoptind == 0)
  183. next = NULL; /* we're starting new/over */
  184. if (next == NULL || *next == '\0') {
  185. if (myoptind == 0)
  186. myoptind++;
  187. if (myoptind >= argc || argv[myoptind][0] != '-' ||
  188. argv[myoptind][1] == '\0') {
  189. myoptarg = NULL;
  190. if (myoptind < argc)
  191. myoptarg = argv[myoptind];
  192. return -1;
  193. }
  194. if (strcmp(argv[myoptind], "--") == 0) {
  195. myoptind++;
  196. myoptarg = NULL;
  197. if (myoptind < argc)
  198. myoptarg = argv[myoptind];
  199. return -1;
  200. }
  201. next = argv[myoptind];
  202. next++; /* skip - */
  203. myoptind++;
  204. }
  205. c = *next++;
  206. /* The C++ strchr can return a different value */
  207. cp = (char*)strchr(optstring, c);
  208. if (cp == NULL || c == ':')
  209. return '?';
  210. cp++;
  211. if (*cp == ':') {
  212. if (*next != '\0') {
  213. myoptarg = next;
  214. next = NULL;
  215. }
  216. else if (myoptind < argc) {
  217. myoptarg = argv[myoptind];
  218. myoptind++;
  219. }
  220. else
  221. return '?';
  222. }
  223. return c;
  224. }
  225. #ifdef OPENSSL_EXTRA
  226. static INLINE int PasswordCallBack(char* passwd, int sz, int rw, void* userdata)
  227. {
  228. (void)rw;
  229. (void)userdata;
  230. strncpy(passwd, "yassl123", sz);
  231. return 8;
  232. }
  233. #endif
  234. #if defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)
  235. static INLINE void ShowX509(CYASSL_X509* x509, const char* hdr)
  236. {
  237. char* altName;
  238. char* issuer = CyaSSL_X509_NAME_oneline(
  239. CyaSSL_X509_get_issuer_name(x509), 0, 0);
  240. char* subject = CyaSSL_X509_NAME_oneline(
  241. CyaSSL_X509_get_subject_name(x509), 0, 0);
  242. byte serial[32];
  243. int ret;
  244. int sz = sizeof(serial);
  245. printf("%s\n issuer : %s\n subject: %s\n", hdr, issuer, subject);
  246. while ( (altName = CyaSSL_X509_get_next_altname(x509)) != NULL)
  247. printf(" altname = %s\n", altName);
  248. ret = CyaSSL_X509_get_serial_number(x509, serial, &sz);
  249. if (ret == SSL_SUCCESS) {
  250. int i;
  251. int strLen;
  252. char serialMsg[80];
  253. /* testsuite has multiple threads writing to stdout, get output
  254. message ready to write once */
  255. strLen = sprintf(serialMsg, " serial number");
  256. for (i = 0; i < sz; i++)
  257. sprintf(serialMsg + strLen + (i*3), ":%02x ", serial[i]);
  258. printf("%s\n", serialMsg);
  259. }
  260. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  261. XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL);
  262. }
  263. #endif /* KEEP_PEER_CERT || SESSION_CERTS */
  264. static INLINE void showPeer(CYASSL* ssl)
  265. {
  266. CYASSL_CIPHER* cipher;
  267. #ifdef KEEP_PEER_CERT
  268. CYASSL_X509* peer = CyaSSL_get_peer_certificate(ssl);
  269. if (peer)
  270. ShowX509(peer, "peer's cert info:");
  271. else
  272. printf("peer has no cert!\n");
  273. #endif
  274. printf("SSL version is %s\n", CyaSSL_get_version(ssl));
  275. cipher = CyaSSL_get_current_cipher(ssl);
  276. printf("SSL cipher suite is %s\n", CyaSSL_CIPHER_get_name(cipher));
  277. #if defined(SESSION_CERTS) && defined(SHOW_CERTS)
  278. {
  279. CYASSL_X509_CHAIN* chain = CyaSSL_get_peer_chain(ssl);
  280. int count = CyaSSL_get_chain_count(chain);
  281. int i;
  282. for (i = 0; i < count; i++) {
  283. int length;
  284. unsigned char buffer[3072];
  285. CYASSL_X509* chainX509;
  286. CyaSSL_get_chain_cert_pem(chain,i,buffer, sizeof(buffer), &length);
  287. buffer[length] = 0;
  288. printf("cert %d has length %d data = \n%s\n", i, length, buffer);
  289. chainX509 = CyaSSL_get_chain_X509(chain, i);
  290. if (chainX509)
  291. ShowX509(chainX509, "session cert info:");
  292. else
  293. printf("get_chain_X509 failed\n");
  294. CyaSSL_FreeX509(chainX509);
  295. }
  296. }
  297. #endif
  298. (void)ssl;
  299. }
  300. static INLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer,
  301. word16 port, int udp)
  302. {
  303. int useLookup = 0;
  304. (void)useLookup;
  305. (void)udp;
  306. memset(addr, 0, sizeof(SOCKADDR_IN_T));
  307. #ifndef TEST_IPV6
  308. /* peer could be in human readable form */
  309. if ( (peer != INADDR_ANY) && isalpha((int)peer[0])) {
  310. #ifdef CYASSL_MDK_ARM
  311. int err;
  312. struct hostent* entry = gethostbyname(peer, &err);
  313. #else
  314. struct hostent* entry = gethostbyname(peer);
  315. #endif
  316. if (entry) {
  317. memcpy(&addr->sin_addr.s_addr, entry->h_addr_list[0],
  318. entry->h_length);
  319. useLookup = 1;
  320. }
  321. else
  322. err_sys("no entry for host");
  323. }
  324. #endif
  325. #ifndef TEST_IPV6
  326. #if defined(CYASSL_MDK_ARM)
  327. addr->sin_family = PF_INET;
  328. #else
  329. addr->sin_family = AF_INET_V;
  330. #endif
  331. addr->sin_port = htons(port);
  332. if (peer == INADDR_ANY)
  333. addr->sin_addr.s_addr = INADDR_ANY;
  334. else {
  335. if (!useLookup)
  336. addr->sin_addr.s_addr = inet_addr(peer);
  337. }
  338. #else
  339. addr->sin6_family = AF_INET_V;
  340. addr->sin6_port = htons(port);
  341. if (peer == INADDR_ANY)
  342. addr->sin6_addr = in6addr_any;
  343. else {
  344. #ifdef HAVE_GETADDRINFO
  345. struct addrinfo hints;
  346. struct addrinfo* answer = NULL;
  347. int ret;
  348. char strPort[80];
  349. memset(&hints, 0, sizeof(hints));
  350. hints.ai_family = AF_INET_V;
  351. hints.ai_socktype = udp ? SOCK_DGRAM : SOCK_STREAM;
  352. hints.ai_protocol = udp ? IPPROTO_UDP : IPPROTO_TCP;
  353. SNPRINTF(strPort, sizeof(strPort), "%d", port);
  354. strPort[79] = '\0';
  355. ret = getaddrinfo(peer, strPort, &hints, &answer);
  356. if (ret < 0 || answer == NULL)
  357. err_sys("getaddrinfo failed");
  358. memcpy(addr, answer->ai_addr, answer->ai_addrlen);
  359. freeaddrinfo(answer);
  360. #else
  361. printf("no ipv6 getaddrinfo, loopback only tests/examples\n");
  362. addr->sin6_addr = in6addr_loopback;
  363. #endif
  364. }
  365. #endif
  366. }
  367. static INLINE void tcp_socket(SOCKET_T* sockfd, int udp)
  368. {
  369. if (udp)
  370. *sockfd = socket(AF_INET_V, SOCK_DGRAM, 0);
  371. else
  372. *sockfd = socket(AF_INET_V, SOCK_STREAM, 0);
  373. #ifdef USE_WINDOWS_API
  374. if (*sockfd == INVALID_SOCKET)
  375. err_sys("socket failed\n");
  376. #else
  377. if (*sockfd < 0)
  378. err_sys("socket failed\n");
  379. #endif
  380. #ifndef USE_WINDOWS_API
  381. #ifdef SO_NOSIGPIPE
  382. {
  383. int on = 1;
  384. socklen_t len = sizeof(on);
  385. int res = setsockopt(*sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, len);
  386. if (res < 0)
  387. err_sys("setsockopt SO_NOSIGPIPE failed\n");
  388. }
  389. #elif defined(CYASSL_MDK_ARM)
  390. /* nothing to define */
  391. #else /* no S_NOSIGPIPE */
  392. signal(SIGPIPE, SIG_IGN);
  393. #endif /* S_NOSIGPIPE */
  394. #if defined(TCP_NODELAY)
  395. if (!udp)
  396. {
  397. int on = 1;
  398. socklen_t len = sizeof(on);
  399. int res = setsockopt(*sockfd, IPPROTO_TCP, TCP_NODELAY, &on, len);
  400. if (res < 0)
  401. err_sys("setsockopt TCP_NODELAY failed\n");
  402. }
  403. #endif
  404. #endif /* USE_WINDOWS_API */
  405. }
  406. static INLINE void tcp_connect(SOCKET_T* sockfd, const char* ip, word16 port,
  407. int udp)
  408. {
  409. SOCKADDR_IN_T addr;
  410. build_addr(&addr, ip, port, udp);
  411. tcp_socket(sockfd, udp);
  412. if (!udp) {
  413. if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
  414. err_sys("tcp connect failed");
  415. }
  416. }
  417. static INLINE void udp_connect(SOCKET_T* sockfd, void* addr, int addrSz)
  418. {
  419. if (connect(*sockfd, (const struct sockaddr*)addr, addrSz) != 0)
  420. err_sys("tcp connect failed");
  421. }
  422. enum {
  423. TEST_SELECT_FAIL,
  424. TEST_TIMEOUT,
  425. TEST_RECV_READY,
  426. TEST_ERROR_READY
  427. };
  428. #if !defined(CYASSL_MDK_ARM)
  429. static INLINE int tcp_select(SOCKET_T socketfd, int to_sec)
  430. {
  431. fd_set recvfds, errfds;
  432. SOCKET_T nfds = socketfd + 1;
  433. struct timeval timeout = { (to_sec > 0) ? to_sec : 0, 0};
  434. int result;
  435. FD_ZERO(&recvfds);
  436. FD_SET(socketfd, &recvfds);
  437. FD_ZERO(&errfds);
  438. FD_SET(socketfd, &errfds);
  439. result = select(nfds, &recvfds, NULL, &errfds, &timeout);
  440. if (result == 0)
  441. return TEST_TIMEOUT;
  442. else if (result > 0) {
  443. if (FD_ISSET(socketfd, &recvfds))
  444. return TEST_RECV_READY;
  445. else if(FD_ISSET(socketfd, &errfds))
  446. return TEST_ERROR_READY;
  447. }
  448. return TEST_SELECT_FAIL;
  449. }
  450. #endif /* !CYASSL_MDK_ARM */
  451. static INLINE void tcp_listen(SOCKET_T* sockfd, int* port, int useAnyAddr,
  452. int udp)
  453. {
  454. SOCKADDR_IN_T addr;
  455. /* don't use INADDR_ANY by default, firewall may block, make user switch
  456. on */
  457. build_addr(&addr, (useAnyAddr ? INADDR_ANY : yasslIP), *port, udp);
  458. tcp_socket(sockfd, udp);
  459. #if !defined(USE_WINDOWS_API) && !defined(CYASSL_MDK_ARM)
  460. {
  461. int res, on = 1;
  462. socklen_t len = sizeof(on);
  463. res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
  464. if (res < 0)
  465. err_sys("setsockopt SO_REUSEADDR failed\n");
  466. }
  467. #endif
  468. if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
  469. err_sys("tcp bind failed");
  470. if (!udp) {
  471. if (listen(*sockfd, 5) != 0)
  472. err_sys("tcp listen failed");
  473. }
  474. #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API)
  475. if (*port == 0) {
  476. socklen_t len = sizeof(addr);
  477. if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) {
  478. #ifndef TEST_IPV6
  479. *port = ntohs(addr.sin_port);
  480. #else
  481. *port = ntohs(addr.sin6_port);
  482. #endif
  483. }
  484. }
  485. #endif
  486. }
  487. static INLINE int udp_read_connect(SOCKET_T sockfd)
  488. {
  489. SOCKADDR_IN_T cliaddr;
  490. byte b[1500];
  491. int n;
  492. socklen_t len = sizeof(cliaddr);
  493. n = (int)recvfrom(sockfd, (char*)b, sizeof(b), MSG_PEEK,
  494. (struct sockaddr*)&cliaddr, &len);
  495. if (n > 0) {
  496. if (connect(sockfd, (const struct sockaddr*)&cliaddr,
  497. sizeof(cliaddr)) != 0)
  498. err_sys("udp connect failed");
  499. }
  500. else
  501. err_sys("recvfrom failed");
  502. return sockfd;
  503. }
  504. static INLINE void udp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
  505. int useAnyAddr, int port, func_args* args)
  506. {
  507. SOCKADDR_IN_T addr;
  508. (void)args;
  509. build_addr(&addr, (useAnyAddr ? INADDR_ANY : yasslIP), port, 1);
  510. tcp_socket(sockfd, 1);
  511. #if !defined(USE_WINDOWS_API) && !defined(CYASSL_MDK_ARM)
  512. {
  513. int res, on = 1;
  514. socklen_t len = sizeof(on);
  515. res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
  516. if (res < 0)
  517. err_sys("setsockopt SO_REUSEADDR failed\n");
  518. }
  519. #endif
  520. if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
  521. err_sys("tcp bind failed");
  522. #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API)
  523. if (port == 0) {
  524. socklen_t len = sizeof(addr);
  525. if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) {
  526. #ifndef TEST_IPV6
  527. port = ntohs(addr.sin_port);
  528. #else
  529. port = ntohs(addr.sin6_port);
  530. #endif
  531. }
  532. }
  533. #endif
  534. #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__)
  535. /* signal ready to accept data */
  536. {
  537. tcp_ready* ready = args->signal;
  538. pthread_mutex_lock(&ready->mutex);
  539. ready->ready = 1;
  540. ready->port = port;
  541. pthread_cond_signal(&ready->cond);
  542. pthread_mutex_unlock(&ready->mutex);
  543. }
  544. #endif
  545. *clientfd = udp_read_connect(*sockfd);
  546. }
  547. static INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
  548. func_args* args, int port, int useAnyAddr,
  549. int udp)
  550. {
  551. SOCKADDR_IN_T client;
  552. socklen_t client_len = sizeof(client);
  553. if (udp) {
  554. udp_accept(sockfd, clientfd, useAnyAddr, port, args);
  555. return;
  556. }
  557. tcp_listen(sockfd, &port, useAnyAddr, udp);
  558. #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__)
  559. /* signal ready to tcp_accept */
  560. {
  561. tcp_ready* ready = args->signal;
  562. pthread_mutex_lock(&ready->mutex);
  563. ready->ready = 1;
  564. ready->port = port;
  565. pthread_cond_signal(&ready->cond);
  566. pthread_mutex_unlock(&ready->mutex);
  567. }
  568. #endif
  569. *clientfd = accept(*sockfd, (struct sockaddr*)&client,
  570. (ACCEPT_THIRD_T)&client_len);
  571. #ifdef USE_WINDOWS_API
  572. if (*clientfd == INVALID_SOCKET)
  573. err_sys("tcp accept failed");
  574. #else
  575. if (*clientfd == -1)
  576. err_sys("tcp accept failed");
  577. #endif
  578. }
  579. static INLINE void tcp_set_nonblocking(SOCKET_T* sockfd)
  580. {
  581. #ifdef USE_WINDOWS_API
  582. unsigned long blocking = 1;
  583. int ret = ioctlsocket(*sockfd, FIONBIO, &blocking);
  584. if (ret == SOCKET_ERROR)
  585. err_sys("ioctlsocket failed");
  586. #elif defined(CYASSL_MDK_ARM)
  587. /* non blocking not suppported, for now */
  588. #else
  589. int flags = fcntl(*sockfd, F_GETFL, 0);
  590. if (flags < 0)
  591. err_sys("fcntl get failed");
  592. flags = fcntl(*sockfd, F_SETFL, flags | O_NONBLOCK);
  593. if (flags < 0)
  594. err_sys("fcntl set failed");
  595. #endif
  596. }
  597. #ifndef NO_PSK
  598. static INLINE unsigned int my_psk_client_cb(CYASSL* ssl, const char* hint,
  599. char* identity, unsigned int id_max_len, unsigned char* key,
  600. unsigned int key_max_len)
  601. {
  602. (void)ssl;
  603. (void)hint;
  604. (void)key_max_len;
  605. /* identity is OpenSSL testing default for openssl s_client, keep same */
  606. strncpy(identity, "Client_identity", id_max_len);
  607. /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using
  608. unsigned binary */
  609. key[0] = 26;
  610. key[1] = 43;
  611. key[2] = 60;
  612. key[3] = 77;
  613. return 4; /* length of key in octets or 0 for error */
  614. }
  615. static INLINE unsigned int my_psk_server_cb(CYASSL* ssl, const char* identity,
  616. unsigned char* key, unsigned int key_max_len)
  617. {
  618. (void)ssl;
  619. (void)key_max_len;
  620. /* identity is OpenSSL testing default for openssl s_client, keep same */
  621. if (strncmp(identity, "Client_identity", 15) != 0)
  622. return 0;
  623. /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using
  624. unsigned binary */
  625. key[0] = 26;
  626. key[1] = 43;
  627. key[2] = 60;
  628. key[3] = 77;
  629. return 4; /* length of key in octets or 0 for error */
  630. }
  631. #endif /* NO_PSK */
  632. #ifdef USE_WINDOWS_API
  633. #define WIN32_LEAN_AND_MEAN
  634. #include <windows.h>
  635. static INLINE double current_time()
  636. {
  637. static int init = 0;
  638. static LARGE_INTEGER freq;
  639. LARGE_INTEGER count;
  640. if (!init) {
  641. QueryPerformanceFrequency(&freq);
  642. init = 1;
  643. }
  644. QueryPerformanceCounter(&count);
  645. return (double)count.QuadPart / freq.QuadPart;
  646. }
  647. #else
  648. #if !defined(CYASSL_MDK_ARM)
  649. #include <sys/time.h>
  650. static INLINE double current_time(void)
  651. {
  652. struct timeval tv;
  653. gettimeofday(&tv, 0);
  654. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  655. }
  656. #endif
  657. #endif /* USE_WINDOWS_API */
  658. #if defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  659. enum {
  660. CYASSL_CA = 1,
  661. CYASSL_CERT = 2,
  662. CYASSL_KEY = 3
  663. };
  664. static INLINE void load_buffer(CYASSL_CTX* ctx, const char* fname, int type)
  665. {
  666. /* test buffer load */
  667. long sz = 0;
  668. byte buff[10000];
  669. FILE* file = fopen(fname, "rb");
  670. if (!file)
  671. err_sys("can't open file for buffer load "
  672. "Please run from CyaSSL home directory if not");
  673. fseek(file, 0, SEEK_END);
  674. sz = ftell(file);
  675. rewind(file);
  676. fread(buff, sizeof(buff), 1, file);
  677. if (type == CYASSL_CA) {
  678. if (CyaSSL_CTX_load_verify_buffer(ctx, buff, sz, SSL_FILETYPE_PEM)
  679. != SSL_SUCCESS)
  680. err_sys("can't load buffer ca file");
  681. }
  682. else if (type == CYASSL_CERT) {
  683. if (CyaSSL_CTX_use_certificate_buffer(ctx, buff, sz,
  684. SSL_FILETYPE_PEM) != SSL_SUCCESS)
  685. err_sys("can't load buffer cert file");
  686. }
  687. else if (type == CYASSL_KEY) {
  688. if (CyaSSL_CTX_use_PrivateKey_buffer(ctx, buff, sz,
  689. SSL_FILETYPE_PEM) != SSL_SUCCESS)
  690. err_sys("can't load buffer key file");
  691. }
  692. }
  693. #endif /* NO_FILESYSTEM */
  694. #ifdef VERIFY_CALLBACK
  695. static INLINE int myVerify(int preverify, CYASSL_X509_STORE_CTX* store)
  696. {
  697. char buffer[80];
  698. #ifdef OPENSSL_EXTRA
  699. CYASSL_X509* peer;
  700. #endif
  701. printf("In verification callback, error = %d, %s\n", store->error,
  702. CyaSSL_ERR_error_string(store->error, buffer));
  703. #ifdef OPENSSL_EXTRA
  704. peer = store->current_cert;
  705. if (peer) {
  706. char* issuer = CyaSSL_X509_NAME_oneline(
  707. CyaSSL_X509_get_issuer_name(peer), 0, 0);
  708. char* subject = CyaSSL_X509_NAME_oneline(
  709. CyaSSL_X509_get_subject_name(peer), 0, 0);
  710. printf("peer's cert info:\n issuer : %s\n subject: %s\n", issuer,
  711. subject);
  712. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  713. XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL);
  714. }
  715. else
  716. printf("peer has no cert!\n");
  717. #endif
  718. printf("Subject's domain name is %s\n", store->domain);
  719. printf("Allowing to continue anyway (shouldn't do this, EVER!!!)\n");
  720. return 1;
  721. }
  722. #endif /* VERIFY_CALLBACK */
  723. #ifdef HAVE_CRL
  724. static INLINE void CRL_CallBack(const char* url)
  725. {
  726. printf("CRL callback url = %s\n", url);
  727. }
  728. #endif
  729. #ifndef NO_CERTS
  730. static INLINE void CaCb(unsigned char* der, int sz, int type)
  731. {
  732. (void)der;
  733. printf("Got CA cache add callback, derSz = %d, type = %d\n", sz, type);
  734. }
  735. static INLINE void SetDH(CYASSL* ssl)
  736. {
  737. /* dh1024 p */
  738. static unsigned char p[] =
  739. {
  740. 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
  741. 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
  742. 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
  743. 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
  744. 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
  745. 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
  746. 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
  747. 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
  748. 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
  749. 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
  750. 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
  751. };
  752. /* dh1024 g */
  753. static unsigned char g[] =
  754. {
  755. 0x02,
  756. };
  757. CyaSSL_SetTmpDH(ssl, p, sizeof(p), g, sizeof(g));
  758. }
  759. static INLINE void SetDHCtx(CYASSL_CTX* ctx)
  760. {
  761. /* dh1024 p */
  762. static unsigned char p[] =
  763. {
  764. 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
  765. 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
  766. 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
  767. 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
  768. 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
  769. 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
  770. 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
  771. 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
  772. 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
  773. 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
  774. 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
  775. };
  776. /* dh1024 g */
  777. static unsigned char g[] =
  778. {
  779. 0x02,
  780. };
  781. CyaSSL_CTX_SetTmpDH(ctx, p, sizeof(p), g, sizeof(g));
  782. }
  783. #endif /* !NO_CERTS */
  784. #ifdef HAVE_CAVIUM
  785. static INLINE int OpenNitroxDevice(int dma_mode,int dev_id)
  786. {
  787. Csp1CoreAssignment core_assign;
  788. Uint32 device;
  789. if (CspInitialize(CAVIUM_DIRECT,CAVIUM_DEV_ID))
  790. return -1;
  791. if (Csp1GetDevType(&device))
  792. return -1;
  793. if (device != NPX_DEVICE) {
  794. if (ioctl(gpkpdev_hdlr[CAVIUM_DEV_ID], IOCTL_CSP1_GET_CORE_ASSIGNMENT,
  795. (Uint32 *)&core_assign)!= 0)
  796. return -1;
  797. }
  798. CspShutdown(CAVIUM_DEV_ID);
  799. return CspInitialize(dma_mode, dev_id);
  800. }
  801. #endif /* HAVE_CAVIUM */
  802. #ifdef USE_WINDOWS_API
  803. /* do back x number of directories */
  804. static INLINE void ChangeDirBack(int x)
  805. {
  806. char path[MAX_PATH];
  807. if (x == 1)
  808. strncpy(path, "..\\", MAX_PATH);
  809. else if (x == 2)
  810. strncpy(path, "..\\..\\", MAX_PATH);
  811. else if (x == 3)
  812. strncpy(path, "..\\..\\..\\", MAX_PATH);
  813. else if (x == 4)
  814. strncpy(path, "..\\..\\..\\..\\", MAX_PATH);
  815. else
  816. strncpy(path, ".\\", MAX_PATH);
  817. SetCurrentDirectoryA(path);
  818. }
  819. /* does current dir contain str */
  820. static INLINE int CurrentDir(const char* str)
  821. {
  822. char path[MAX_PATH];
  823. char* baseName;
  824. GetCurrentDirectoryA(sizeof(path), path);
  825. baseName = strrchr(path, '\\');
  826. if (baseName)
  827. baseName++;
  828. else
  829. baseName = path;
  830. if (strstr(baseName, str))
  831. return 1;
  832. return 0;
  833. }
  834. #elif defined(CYASSL_MDK_ARM)
  835. /* KEIL-RL File System does not support relative directry */
  836. #else
  837. #ifndef MAX_PATH
  838. #define MAX_PATH 256
  839. #endif
  840. /* do back x number of directories */
  841. static INLINE void ChangeDirBack(int x)
  842. {
  843. char path[MAX_PATH];
  844. if (x == 1)
  845. strncpy(path, "../", MAX_PATH);
  846. else if (x == 2)
  847. strncpy(path, "../../", MAX_PATH);
  848. else if (x == 3)
  849. strncpy(path, "../../../", MAX_PATH);
  850. else if (x == 4)
  851. strncpy(path, "../../../../", MAX_PATH);
  852. else
  853. strncpy(path, "./", MAX_PATH);
  854. if (chdir(path) < 0)
  855. printf("chdir to %s failed\n", path);
  856. }
  857. /* does current dir contain str */
  858. static INLINE int CurrentDir(const char* str)
  859. {
  860. char path[MAX_PATH];
  861. char* baseName;
  862. if (getcwd(path, sizeof(path)) == NULL) {
  863. printf("no current dir?\n");
  864. return 0;
  865. }
  866. baseName = strrchr(path, '/');
  867. if (baseName)
  868. baseName++;
  869. else
  870. baseName = path;
  871. if (strstr(baseName, str))
  872. return 1;
  873. return 0;
  874. }
  875. #endif /* USE_WINDOWS_API */
  876. #ifdef USE_CYASSL_MEMORY
  877. typedef struct memoryStats {
  878. size_t totalAllocs; /* number of allocations */
  879. size_t totalBytes; /* total number of bytes allocated */
  880. size_t peakBytes; /* concurrent max bytes */
  881. size_t currentBytes; /* total current bytes in use */
  882. } memoryStats;
  883. typedef struct memHint {
  884. size_t thisSize; /* size of this memory */
  885. void* thisMemory; /* actual memory for user */
  886. } memHint;
  887. typedef struct memoryTrack {
  888. union {
  889. memHint hint;
  890. byte alignit[16]; /* make sure we have strong alignment */
  891. } u;
  892. } memoryTrack;
  893. #if defined(CYASSL_TRACK_MEMORY)
  894. #define DO_MEM_STATS
  895. static memoryStats ourMemStats;
  896. #endif
  897. static INLINE void* TrackMalloc(size_t sz)
  898. {
  899. memoryTrack* mt;
  900. if (sz == 0)
  901. return NULL;
  902. mt = (memoryTrack*)malloc(sizeof(memoryTrack) + sz);
  903. if (mt == NULL)
  904. return NULL;
  905. mt->u.hint.thisSize = sz;
  906. mt->u.hint.thisMemory = (byte*)mt + sizeof(memoryTrack);
  907. #ifdef DO_MEM_STATS
  908. ourMemStats.totalAllocs++;
  909. ourMemStats.totalBytes += sz;
  910. ourMemStats.currentBytes += sz;
  911. if (ourMemStats.currentBytes > ourMemStats.peakBytes)
  912. ourMemStats.peakBytes = ourMemStats.currentBytes;
  913. #endif
  914. return mt->u.hint.thisMemory;
  915. }
  916. static INLINE void TrackFree(void* ptr)
  917. {
  918. memoryTrack* mt;
  919. if (ptr == NULL)
  920. return;
  921. mt = (memoryTrack*)((byte*)ptr - sizeof(memoryTrack));
  922. #ifdef DO_MEM_STATS
  923. ourMemStats.currentBytes -= mt->u.hint.thisSize;
  924. #endif
  925. free(mt);
  926. }
  927. static INLINE void* TrackRealloc(void* ptr, size_t sz)
  928. {
  929. void* ret = TrackMalloc(sz);
  930. if (ptr) {
  931. /* if realloc is bigger, don't overread old ptr */
  932. memoryTrack* mt = (memoryTrack*)((byte*)ptr - sizeof(memoryTrack));
  933. if (mt->u.hint.thisSize < sz)
  934. sz = mt->u.hint.thisSize;
  935. }
  936. if (ret && ptr)
  937. memcpy(ret, ptr, sz);
  938. if (ret)
  939. TrackFree(ptr);
  940. return ret;
  941. }
  942. static INLINE void InitMemoryTracker(void)
  943. {
  944. if (CyaSSL_SetAllocators(TrackMalloc, TrackFree, TrackRealloc) != 0)
  945. err_sys("CyaSSL SetAllocators failed for track memory");
  946. #ifdef DO_MEM_STATS
  947. ourMemStats.totalAllocs = 0;
  948. ourMemStats.totalBytes = 0;
  949. ourMemStats.peakBytes = 0;
  950. ourMemStats.currentBytes = 0;
  951. #endif
  952. }
  953. static INLINE void ShowMemoryTracker(void)
  954. {
  955. #ifdef DO_MEM_STATS
  956. printf("total Allocs = %9lu\n",
  957. (unsigned long)ourMemStats.totalAllocs);
  958. printf("total Bytes = %9lu\n",
  959. (unsigned long)ourMemStats.totalBytes);
  960. printf("peak Bytes = %9lu\n",
  961. (unsigned long)ourMemStats.peakBytes);
  962. printf("current Bytes = %9lu\n",
  963. (unsigned long)ourMemStats.currentBytes);
  964. #endif
  965. }
  966. #endif /* USE_CYASSL_MEMORY */
  967. #ifdef HAVE_STACK_SIZE
  968. typedef THREAD_RETURN CYASSL_THREAD (*thread_func)(void* args);
  969. static INLINE void StackSizeCheck(func_args* args, thread_func tf)
  970. {
  971. int ret, i, used;
  972. unsigned char* myStack;
  973. int stackSize = 1024*128;
  974. pthread_attr_t myAttr;
  975. pthread_t threadId;
  976. #ifdef PTHREAD_STACK_MIN
  977. if (stackSize < PTHREAD_STACK_MIN)
  978. stackSize = PTHREAD_STACK_MIN;
  979. #endif
  980. ret = posix_memalign((void**)&myStack, sysconf(_SC_PAGESIZE), stackSize);
  981. if (ret != 0)
  982. err_sys("posix_memalign failed\n");
  983. memset(myStack, 0x01, stackSize);
  984. ret = pthread_attr_init(&myAttr);
  985. if (ret != 0)
  986. err_sys("attr_init failed");
  987. ret = pthread_attr_setstack(&myAttr, myStack, stackSize);
  988. if (ret != 0)
  989. err_sys("attr_setstackaddr failed");
  990. ret = pthread_create(&threadId, &myAttr, tf, args);
  991. if (ret != 0) {
  992. perror("pthread_create failed");
  993. exit(EXIT_FAILURE);
  994. }
  995. ret = pthread_join(threadId, NULL);
  996. if (ret != 0)
  997. err_sys("pthread_join failed");
  998. for (i = 0; i < stackSize; i++) {
  999. if (myStack[i] != 0x01) {
  1000. break;
  1001. }
  1002. }
  1003. used = stackSize - i;
  1004. printf("stack used = %d\n", used);
  1005. }
  1006. #endif /* HAVE_STACK_SIZE */
  1007. #ifdef STACK_TRAP
  1008. /* good settings
  1009. --enable-debug --disable-shared C_EXTRA_FLAGS="-DUSER_TIME -DTFM_TIMING_RESISTANT -DPOSITIVE_EXP_ONLY -DSTACK_TRAP"
  1010. */
  1011. #ifdef HAVE_STACK_SIZE
  1012. /* client only for now, setrlimit will fail if pthread_create() called */
  1013. /* STACK_SIZE does pthread_create() on client */
  1014. #error "can't use STACK_TRAP with STACK_SIZE, setrlimit will fail"
  1015. #endif /* HAVE_STACK_SIZE */
  1016. static INLINE void StackTrap(void)
  1017. {
  1018. struct rlimit rl;
  1019. if (getrlimit(RLIMIT_STACK, &rl) != 0)
  1020. err_sys("getrlimit failed");
  1021. printf("rlim_cur = %llu\n", rl.rlim_cur);
  1022. rl.rlim_cur = 1024*21; /* adjust trap size here */
  1023. if (setrlimit(RLIMIT_STACK, &rl) != 0) {
  1024. perror("setrlimit");
  1025. err_sys("setrlimit failed");
  1026. }
  1027. }
  1028. #else /* STACK_TRAP */
  1029. static INLINE void StackTrap(void)
  1030. {
  1031. }
  1032. #endif /* STACK_TRAP */
  1033. #if defined(__hpux__) || defined(__MINGW32__)
  1034. /* HP/UX doesn't have strsep, needed by test/suites.c */
  1035. static INLINE char* strsep(char **stringp, const char *delim)
  1036. {
  1037. char* start;
  1038. char* end;
  1039. start = *stringp;
  1040. if (start == NULL)
  1041. return NULL;
  1042. if ((end = strpbrk(start, delim))) {
  1043. *end++ = '\0';
  1044. *stringp = end;
  1045. } else {
  1046. *stringp = NULL;
  1047. }
  1048. return start;
  1049. }
  1050. #endif /* __hpux__ */
  1051. #endif /* CyaSSL_TEST_H */