Changeset 464 for azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfssl/test.h
- Timestamp:
- Jun 22, 2021, 9:00:19 PM (3 years ago)
- Location:
- azure_iot_hub_f767zi/trunk/wolfssl-4.7.0
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfssl/test.h
r457 r464 4 4 #define wolfSSL_TEST_H 5 5 6 #include <stdio.h> 7 #include <stdlib.h> 6 #ifdef FUSION_RTOS 7 #include <fclstdio.h> 8 #include <fclstdlib.h> 9 #else 10 #include <stdio.h> 11 #include <stdlib.h> 12 #endif 8 13 #include <assert.h> 9 14 #include <ctype.h> … … 56 61 #define SOCKET_T SOCKET 57 62 #define SNPRINTF _snprintf 63 #define XSLEEP_MS(t) Sleep(t) 58 64 #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) 59 65 #include <string.h> … … 70 76 } 71 77 #if defined(HAVE_KEIL_RTX) 72 #define sleep(t) os_dly_wait(t/1000+1);78 #define XSLEEP_MS(t) os_dly_wait(t) 73 79 #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2) 74 #define sleep(t) osDelay(t/1000+1);80 #define XSLEEP_MS(t) osDelay(t) 75 81 #endif 76 82 #elif defined(WOLFSSL_TIRTOS) … … 89 95 }; 90 96 #define SOCKET_T int 97 #define XSLEEP_MS(t) Task_sleep(t/1000) 91 98 #elif defined(WOLFSSL_VXWORKS) 92 99 #include <hostLib.h> … … 144 151 #endif 145 152 #endif 153 #ifdef FREESCALE_MQX 154 typedef int socklen_t ; 155 #endif 146 156 #define SOCKET_T int 147 157 #ifndef SO_NOSIGPIPE … … 149 159 #endif 150 160 #define SNPRINTF snprintf 161 162 #define XSELECT_WAIT(x,y) do { \ 163 struct timeval tv = {(x),(y)}; \ 164 select(0, NULL, NULL, NULL, &tv); \ 165 } while (0) 166 #define XSLEEP_US(u) XSELECT_WAIT(0,u) 167 #define XSLEEP_MS(m) XSELECT_WAIT(0,(m)*1000) 151 168 #endif /* USE_WINDOWS_API */ 169 170 #ifndef XSLEEP_MS 171 #define XSLEEP_MS(t) sleep(t/1000) 172 #endif 152 173 153 174 #ifdef WOLFSSL_ASYNC_CRYPT … … 231 252 #define INFINITE -1 232 253 #define WAIT_OBJECT_0 0L 233 #elif defined(WOLFSSL_MDK_ARM)|| defined(WOLFSSL_KEIL_TCP_NET) 254 #elif defined(WOLFSSL_MDK_ARM)|| defined(WOLFSSL_KEIL_TCP_NET) || defined(FREESCALE_MQX) 234 255 typedef unsigned int THREAD_RETURN; 235 256 typedef int THREAD_TYPE; … … 287 308 #define DEFAULT_MIN_RSAKEY_BITS 2048 288 309 #else 310 #ifndef DEFAULT_MIN_RSAKEY_BITS 289 311 #define DEFAULT_MIN_RSAKEY_BITS 1024 312 #endif 290 313 #endif 291 314 #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH) 292 315 #define DEFAULT_MIN_ECCKEY_BITS 256 293 316 #else 317 #ifndef DEFAULT_MIN_ECCKEY_BITS 294 318 #define DEFAULT_MIN_ECCKEY_BITS 224 319 #endif 295 320 #endif 296 321 … … 410 435 ssl_callback on_result; 411 436 WOLFSSL_CTX* ctx; 437 unsigned char isSharedCtx:1; 412 438 } callback_functions; 413 439 … … 472 498 err_sys(const char* msg) 473 499 { 474 printf("wolfSSL error: %s\n", msg);475 476 500 #if !defined(__GNUC__) 477 501 /* scan-build (which pretends to be gnuc) can get confused and think the … … 484 508 #endif 485 509 { 510 printf("wolfSSL error: %s\n", msg); 511 512 XEXIT_T(EXIT_FAILURE); 513 } 514 } 515 516 static WC_INLINE 517 #if defined(WOLFSSL_FORCE_MALLOC_FAIL_TEST) || defined(WOLFSSL_ZEPHYR) 518 THREAD_RETURN 519 #else 520 WC_NORETURN void 521 #endif 522 err_sys_with_errno(const char* msg) 523 { 524 #if !defined(__GNUC__) 525 /* scan-build (which pretends to be gnuc) can get confused and think the 526 * msg pointer can be null even when hardcoded and then it won't exit, 527 * making null pointer checks above the err_sys() call useless. 528 * We could just always exit() but some compilers will complain about no 529 * possible return, with gcc we know the attribute to handle that with 530 * WC_NORETURN. */ 531 if (msg) 532 #endif 533 { 534 #if defined(HAVE_STRING_H) && defined(HAVE_ERRNO_H) 535 printf("wolfSSL error: %s: %s\n", msg, strerror(errno)); 536 #else 537 printf("wolfSSL error: %s\n", msg); 538 #endif 539 486 540 XEXIT_T(EXIT_FAILURE); 487 541 } … … 492 546 extern char* myoptarg; 493 547 548 /** 549 * 550 * @param argc Number of argv strings 551 * @param argv Array of string arguments 552 * @param optstring String containing the supported alphanumeric arguments. 553 * A ':' following a character means that it requires a 554 * value in myoptarg to be set. A ';' means that the 555 * myoptarg is optional. myoptarg is set to "" if not 556 * present. 557 * @return Option letter in argument 558 */ 494 559 static WC_INLINE int mygetopt(int argc, char** argv, const char* optstring) 495 560 { … … 541 606 cp = (char*)strchr(optstring, c); 542 607 543 if (cp == NULL || c == ':' )608 if (cp == NULL || c == ':' || c == ';') 544 609 return '?'; 545 610 … … 557 622 else 558 623 return '?'; 624 } 625 else if (*cp == ';') { 626 myoptarg = (char*)""; 627 if (*next != '\0') { 628 myoptarg = next; 629 next = NULL; 630 } 631 else if (myoptind < argc) { 632 /* Check if next argument is not a parameter argument */ 633 if (argv[myoptind] && argv[myoptind][0] != '-') { 634 myoptarg = argv[myoptind]; 635 myoptind++; 636 } 637 } 559 638 } 560 639 … … 935 1014 936 1015 if(WOLFSSL_SOCKET_IS_INVALID(*sockfd)) { 937 err_sys ("socket failed\n");1016 err_sys_with_errno("socket failed\n"); 938 1017 } 939 1018 … … 945 1024 int res = setsockopt(*sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, len); 946 1025 if (res < 0) 947 err_sys ("setsockopt SO_NOSIGPIPE failed\n");1026 err_sys_with_errno("setsockopt SO_NOSIGPIPE failed\n"); 948 1027 } 949 1028 #elif defined(WOLFSSL_MDK_ARM) || defined (WOLFSSL_TIRTOS) ||\ … … 961 1040 int res = setsockopt(*sockfd, IPPROTO_TCP, TCP_NODELAY, &on, len); 962 1041 if (res < 0) 963 err_sys ("setsockopt TCP_NODELAY failed\n");1042 err_sys_with_errno("setsockopt TCP_NODELAY failed\n"); 964 1043 } 965 1044 #endif … … 979 1058 if (!udp) { 980 1059 if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0) 981 err_sys ("tcp connect failed");1060 err_sys_with_errno("tcp connect failed"); 982 1061 } 983 1062 } … … 987 1066 { 988 1067 if (connect(*sockfd, (const struct sockaddr*)addr, addrSz) != 0) 989 err_sys ("tcp connect failed");1068 err_sys_with_errno("tcp connect failed"); 990 1069 } 991 1070 … … 1085 1164 res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len); 1086 1165 if (res < 0) 1087 err_sys("setsockopt SO_REUSEADDR failed\n"); 1088 } 1166 err_sys_with_errno("setsockopt SO_REUSEADDR failed\n"); 1167 } 1168 #ifdef SO_REUSEPORT 1169 { 1170 int res, on = 1; 1171 socklen_t len = sizeof(on); 1172 res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEPORT, &on, len); 1173 if (res < 0) 1174 err_sys_with_errno("setsockopt SO_REUSEPORT failed\n"); 1175 } 1176 #endif 1089 1177 #endif 1090 1178 1091 1179 if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0) 1092 err_sys ("tcp bind failed");1180 err_sys_with_errno("tcp bind failed"); 1093 1181 if (!udp) { 1094 1182 #ifdef WOLFSSL_KEIL_TCP_NET … … 1098 1186 #endif 1099 1187 if (listen(*sockfd, SOCK_LISTEN_MAX_QUEUE) != 0) 1100 err_sys ("tcp listen failed");1188 err_sys_with_errno("tcp listen failed"); 1101 1189 } 1102 1190 #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_TIRTOS) \ … … 1155 1243 res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len); 1156 1244 if (res < 0) 1157 err_sys("setsockopt SO_REUSEADDR failed\n"); 1158 } 1245 err_sys_with_errno("setsockopt SO_REUSEADDR failed\n"); 1246 } 1247 #ifdef SO_REUSEPORT 1248 { 1249 int res, on = 1; 1250 socklen_t len = sizeof(on); 1251 res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEPORT, &on, len); 1252 if (res < 0) 1253 err_sys_with_errno("setsockopt SO_REUSEPORT failed\n"); 1254 } 1255 #endif 1159 1256 #endif 1160 1257 1161 1258 if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0) 1162 err_sys ("tcp bind failed");1259 err_sys_with_errno("tcp bind failed"); 1163 1260 1164 1261 #if (defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API)) && !defined(WOLFSSL_TIRTOS) … … 1201 1298 int udp, int sctp, int ready_file, int do_listen) 1202 1299 { 1203 SOCKADDR_IN_T client ;1204 socklen_t client_len = sizeof(client );1300 SOCKADDR_IN_T client_addr; 1301 socklen_t client_len = sizeof(client_addr); 1205 1302 tcp_ready* ready = NULL; 1206 1303 … … 1259 1356 } 1260 1357 1261 *clientfd = accept(*sockfd, (struct sockaddr*)&client ,1358 *clientfd = accept(*sockfd, (struct sockaddr*)&client_addr, 1262 1359 (ACCEPT_THIRD_T)&client_len); 1263 1360 if(WOLFSSL_SOCKET_IS_INVALID(*clientfd)) { 1264 err_sys ("tcp accept failed");1361 err_sys_with_errno("tcp accept failed"); 1265 1362 } 1266 1363 } … … 1273 1370 int ret = ioctlsocket(*sockfd, FIONBIO, &blocking); 1274 1371 if (ret == SOCKET_ERROR) 1275 err_sys ("ioctlsocket failed");1372 err_sys_with_errno("ioctlsocket failed"); 1276 1373 #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) \ 1277 1374 || defined (WOLFSSL_TIRTOS)|| defined(WOLFSSL_VXWORKS) \ … … 1281 1378 int flags = fcntl(*sockfd, F_GETFL, 0); 1282 1379 if (flags < 0) 1283 err_sys ("fcntl get failed");1380 err_sys_with_errno("fcntl get failed"); 1284 1381 flags = fcntl(*sockfd, F_SETFL, flags | O_NONBLOCK); 1285 1382 if (flags < 0) 1286 err_sys ("fcntl set failed");1383 err_sys_with_errno("fcntl set failed"); 1287 1384 #endif 1288 1385 } … … 1303 1400 1304 1401 /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */ 1305 strncpy(identity, kIdentityStr, id_max_len);1402 XSTRNCPY(identity, kIdentityStr, id_max_len); 1306 1403 1307 1404 if (wolfSSL_GetVersion(ssl) < WOLFSSL_TLSV1_3) { … … 1337 1434 1338 1435 /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */ 1339 if ( strncmp(identity, kIdentityStr, strlen(kIdentityStr)) != 0)1436 if (XSTRNCMP(identity, kIdentityStr, XSTRLEN(kIdentityStr)) != 0) 1340 1437 return 0; 1341 1438 … … 1371 1468 int i; 1372 1469 int b = 0x01; 1470 const char* userCipher = (const char*)wolfSSL_get_psk_callback_ctx(ssl); 1373 1471 1374 1472 (void)ssl; … … 1377 1475 1378 1476 /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */ 1379 strncpy(identity, kIdentityStr, id_max_len);1477 XSTRNCPY(identity, kIdentityStr, id_max_len); 1380 1478 1381 1479 for (i = 0; i < 32; i++, b += 0x22) { … … 1385 1483 } 1386 1484 1387 *ciphersuite = "TLS13-AES128-GCM-SHA256";1485 *ciphersuite = userCipher ? userCipher : "TLS13-AES128-GCM-SHA256"; 1388 1486 1389 1487 return 32; /* length of key in octets or 0 for error */ … … 1397 1495 int i; 1398 1496 int b = 0x01; 1497 const char* userCipher = (const char*)wolfSSL_get_psk_callback_ctx(ssl); 1399 1498 1400 1499 (void)ssl; … … 1402 1501 1403 1502 /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */ 1404 if ( strncmp(identity, kIdentityStr, strlen(kIdentityStr)) != 0)1503 if (XSTRNCMP(identity, kIdentityStr, XSTRLEN(kIdentityStr)) != 0) 1405 1504 return 0; 1406 1505 … … 1411 1510 } 1412 1511 1413 *ciphersuite = "TLS13-AES128-GCM-SHA256";1512 *ciphersuite = userCipher ? userCipher : "TLS13-AES128-GCM-SHA256"; 1414 1513 1415 1514 return 32; /* length of key in octets or 0 for error */ 1416 1515 } 1417 1516 1418 #endif /* NO_PSK */1517 #endif /* !NO_PSK */ 1419 1518 1420 1519 … … 1499 1598 static WC_INLINE void OCSPRespFreeCb(void* ioCtx, unsigned char* response) 1500 1599 { 1501 (void)ioCtx; 1502 (void)response; 1600 return EmbedOcspRespFree(ioCtx, response); 1503 1601 } 1504 1602 #endif … … 1513 1611 int ret; 1514 1612 long int fileSz; 1515 XFILE file;1613 XFILE lFile; 1516 1614 1517 1615 if (fname == NULL || buf == NULL || bufLen == NULL) … … 1523 1621 1524 1622 /* open file (read-only binary) */ 1525 file = XFOPEN(fname, "rb");1526 if (! file) {1623 lFile = XFOPEN(fname, "rb"); 1624 if (!lFile) { 1527 1625 printf("Error loading %s\n", fname); 1528 1626 return BAD_PATH_ERROR; 1529 1627 } 1530 1628 1531 fseek( file, 0, SEEK_END);1532 fileSz = (int)ftell( file);1533 rewind( file);1629 fseek(lFile, 0, SEEK_END); 1630 fileSz = (int)ftell(lFile); 1631 rewind(lFile); 1534 1632 if (fileSz > 0) { 1535 1633 *bufLen = (size_t)fileSz; … … 1540 1638 } 1541 1639 else { 1542 size_t readLen = fread(*buf, *bufLen, 1, file);1640 size_t readLen = fread(*buf, *bufLen, 1, lFile); 1543 1641 1544 1642 /* check response code */ … … 1549 1647 ret = BUFFER_E; 1550 1648 } 1551 fclose( file);1649 fclose(lFile); 1552 1650 1553 1651 return ret; … … 1676 1774 #endif /* !NO_CERTS */ 1677 1775 1678 static int myVerifyFail = 0; 1776 enum { 1777 VERIFY_OVERRIDE_ERROR, 1778 VERIFY_FORCE_FAIL, 1779 VERIFY_USE_PREVERFIY, 1780 VERIFY_OVERRIDE_DATE_ERR, 1781 }; 1782 static THREAD_LS_T int myVerifyAction = VERIFY_OVERRIDE_ERROR; 1679 1783 1680 1784 /* The verify callback is called for every certificate only when … … 1728 1832 XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL); 1729 1833 #if defined(SHOW_CERTS) && !defined(NO_FILESYSTEM) 1730 /* avoid printing duplicate certs */1834 /* avoid printing duplicate certs */ 1731 1835 if (store->depth == 1) { 1732 1836 /* retrieve x509 certs and display them on stdout */ … … 1763 1867 1764 1868 /* Testing forced fail case by return zero */ 1765 if (myVerify Fail) {1869 if (myVerifyAction == VERIFY_FORCE_FAIL) { 1766 1870 return 0; /* test failure case */ 1767 1871 } 1768 1872 1873 if (myVerifyAction == VERIFY_OVERRIDE_DATE_ERR && 1874 (store->error == ASN_BEFORE_DATE_E || store->error == ASN_AFTER_DATE_E)) { 1875 printf("Overriding cert date error as example for bad clock testing\n"); 1876 return 1; 1877 } 1878 1769 1879 /* If error indicate we are overriding it for testing purposes */ 1770 if (store->error != 0 ) {1880 if (store->error != 0 && myVerifyAction == VERIFY_OVERRIDE_ERROR) { 1771 1881 printf("\tAllowing failed certificate check, testing only " 1772 1882 "(shouldn't do this in production)\n"); … … 1774 1884 1775 1885 /* A non-zero return code indicates failure override */ 1776 return 1; 1777 } 1778 1779 1780 static WC_INLINE int myDateCb(int preverify, WOLFSSL_X509_STORE_CTX* store) 1781 { 1782 char buffer[WOLFSSL_MAX_ERROR_SZ]; 1783 (void)preverify; 1784 1785 printf("In verification callback, error = %d, %s\n", store->error, 1786 wolfSSL_ERR_error_string(store->error, buffer)); 1787 printf("Subject's domain name is %s\n", store->domain); 1788 1789 if (store->error == ASN_BEFORE_DATE_E || store->error == ASN_AFTER_DATE_E) { 1790 printf("Overriding cert date error as example for bad clock testing\n"); 1791 return 1; 1792 } 1793 printf("Cert error is not date error, not overriding\n"); 1794 1795 return 0; 1886 return (myVerifyAction == VERIFY_OVERRIDE_ERROR) ? 1 : preverify; 1796 1887 } 1797 1888 … … 1917 2008 #if !defined(NO_FILESYSTEM) || defined(FORCE_BUFFER_TEST) 1918 2009 int depth, res; 1919 XFILE file;2010 XFILE keyFile; 1920 2011 for(depth = 0; depth <= MAX_WOLF_ROOT_DEPTH; depth++) { 1921 file = XFOPEN(ntruKeyFile, "rb");1922 if ( file != NULL) {1923 fclose( file);2012 keyFile = XFOPEN(ntruKeyFile, "rb"); 2013 if (keyFile != NULL) { 2014 fclose(keyFile); 1924 2015 return depth; 1925 2016 } … … 1948 2039 #define STACK_CHECK_VAL 0x01 1949 2040 2041 struct stack_size_debug_context { 2042 unsigned char *myStack; 2043 size_t stackSize; 2044 #ifdef HAVE_STACK_SIZE_VERBOSE 2045 size_t *stackSizeHWM_ptr; 2046 thread_func fn; 2047 void *args; 2048 #endif 2049 }; 2050 2051 #ifdef HAVE_STACK_SIZE_VERBOSE 2052 2053 /* per-subtest stack high water mark tracking. 2054 * 2055 * enable with 2056 * 2057 * ./configure --enable-stacksize=verbose [...] 2058 */ 2059 2060 static THREAD_RETURN debug_stack_size_verbose_shim(struct stack_size_debug_context *shim_args) { 2061 StackSizeCheck_myStack = shim_args->myStack; 2062 StackSizeCheck_stackSize = shim_args->stackSize; 2063 StackSizeCheck_stackSizeHWM_ptr = shim_args->stackSizeHWM_ptr; 2064 return shim_args->fn(shim_args->args); 2065 } 2066 2067 static WC_INLINE int StackSizeSetOffset(const char *funcname, void *p) 2068 { 2069 if (StackSizeCheck_myStack == NULL) 2070 return -BAD_FUNC_ARG; 2071 2072 StackSizeCheck_stackOffsetPointer = p; 2073 2074 printf("setting stack relative offset reference mark in %s to +%lu\n", 2075 funcname, (unsigned long)((char*)(StackSizeCheck_myStack + 2076 StackSizeCheck_stackSize) - (char *)p)); 2077 2078 return 0; 2079 } 2080 2081 static WC_INLINE ssize_t StackSizeHWM(void) 2082 { 2083 size_t i; 2084 ssize_t used; 2085 2086 if (StackSizeCheck_myStack == NULL) 2087 return -BAD_FUNC_ARG; 2088 2089 for (i = 0; i < StackSizeCheck_stackSize; i++) { 2090 if (StackSizeCheck_myStack[i] != STACK_CHECK_VAL) { 2091 break; 2092 } 2093 } 2094 2095 used = StackSizeCheck_stackSize - i; 2096 if ((ssize_t)*StackSizeCheck_stackSizeHWM_ptr < used) 2097 *StackSizeCheck_stackSizeHWM_ptr = used; 2098 2099 return used; 2100 } 2101 2102 static WC_INLINE ssize_t StackSizeHWM_OffsetCorrected(void) 2103 { 2104 ssize_t used = StackSizeHWM(); 2105 if (used < 0) 2106 return used; 2107 if (StackSizeCheck_stackOffsetPointer) 2108 used -= (ssize_t)(((char *)StackSizeCheck_myStack + StackSizeCheck_stackSize) - (char *)StackSizeCheck_stackOffsetPointer); 2109 return used; 2110 } 2111 2112 static 2113 #ifdef __GNUC__ 2114 __attribute__((unused)) __attribute__((noinline)) 2115 #endif 2116 int StackSizeHWMReset(void) 2117 { 2118 volatile ssize_t i; 2119 2120 if (StackSizeCheck_myStack == NULL) 2121 return -BAD_FUNC_ARG; 2122 2123 for (i = (ssize_t)((char *)&i - (char *)StackSizeCheck_myStack) - (ssize_t)sizeof i - 1; i >= 0; --i) 2124 { 2125 StackSizeCheck_myStack[i] = STACK_CHECK_VAL; 2126 } 2127 2128 return 0; 2129 } 2130 2131 #define STACK_SIZE_CHECKPOINT(...) ({ \ 2132 ssize_t HWM = StackSizeHWM_OffsetCorrected(); \ 2133 __VA_ARGS__; \ 2134 printf(" relative stack peak usage = %ld bytes\n", HWM); \ 2135 StackSizeHWMReset(); \ 2136 }) 2137 2138 #define STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max, ...) ({ \ 2139 ssize_t HWM = StackSizeHWM_OffsetCorrected(); \ 2140 int _ret; \ 2141 __VA_ARGS__; \ 2142 printf(" relative stack peak usage = %ld bytes\n", HWM); \ 2143 _ret = StackSizeHWMReset(); \ 2144 if ((max >= 0) && (HWM > (ssize_t)(max))) { \ 2145 printf(" relative stack usage at %s L%d exceeds designated max %ld bytes.\n", __FILE__, __LINE__, (ssize_t)(max)); \ 2146 _ret = -1; \ 2147 } \ 2148 _ret; \ 2149 }) 2150 2151 2152 #ifdef __GNUC__ 2153 #define STACK_SIZE_INIT() (void)StackSizeSetOffset(__FUNCTION__, __builtin_frame_address(0)) 2154 #endif 2155 2156 #endif /* HAVE_STACK_SIZE_VERBOSE */ 2157 1950 2158 static WC_INLINE int StackSizeCheck(func_args* args, thread_func tf) 1951 2159 { 1952 int ret, i, used; 2160 size_t i; 2161 int ret; 1953 2162 void* status; 1954 2163 unsigned char* myStack = NULL; 1955 int stackSize = 1024*152;2164 size_t stackSize = 1024*1024; 1956 2165 pthread_attr_t myAttr; 1957 2166 pthread_t threadId; 2167 #ifdef HAVE_STACK_SIZE_VERBOSE 2168 struct stack_size_debug_context shim_args; 2169 #endif 1958 2170 1959 2171 #ifdef PTHREAD_STACK_MIN … … 1964 2176 ret = posix_memalign((void**)&myStack, sysconf(_SC_PAGESIZE), stackSize); 1965 2177 if (ret != 0 || myStack == NULL) 1966 err_sys ("posix_memalign failed\n");2178 err_sys_with_errno("posix_memalign failed\n"); 1967 2179 1968 2180 XMEMSET(myStack, STACK_CHECK_VAL, stackSize); … … 1976 2188 err_sys("attr_setstackaddr failed"); 1977 2189 2190 #ifdef HAVE_STACK_SIZE_VERBOSE 2191 StackSizeCheck_stackSizeHWM = 0; 2192 shim_args.myStack = myStack; 2193 shim_args.stackSize = stackSize; 2194 shim_args.stackSizeHWM_ptr = &StackSizeCheck_stackSizeHWM; 2195 shim_args.fn = tf; 2196 shim_args.args = args; 2197 ret = pthread_create(&threadId, &myAttr, (thread_func)debug_stack_size_verbose_shim, (void *)&shim_args); 2198 #else 1978 2199 ret = pthread_create(&threadId, &myAttr, tf, args); 2200 #endif 1979 2201 if (ret != 0) { 1980 2202 perror("pthread_create failed"); … … 1993 2215 1994 2216 free(myStack); 1995 1996 used = stackSize - i; 1997 printf("stack used = %d\n", used); 2217 #ifdef HAVE_STACK_SIZE_VERBOSE 2218 printf("stack used = %lu\n", StackSizeCheck_stackSizeHWM > (stackSize - i) 2219 ? (unsigned long)StackSizeCheck_stackSizeHWM 2220 : (unsigned long)(stackSize - i)); 2221 #else 2222 { 2223 size_t used = stackSize - i; 2224 printf("stack used = %lu\n", (unsigned long)used); 2225 } 2226 #endif 1998 2227 1999 2228 return (int)((size_t)status); 2000 2229 } 2001 2230 2231 static WC_INLINE int StackSizeCheck_launch(func_args* args, thread_func tf, pthread_t *threadId, void **stack_context) 2232 { 2233 int ret; 2234 unsigned char* myStack = NULL; 2235 size_t stackSize = 1024*1024; 2236 pthread_attr_t myAttr; 2237 2238 #ifdef PTHREAD_STACK_MIN 2239 if (stackSize < PTHREAD_STACK_MIN) 2240 stackSize = PTHREAD_STACK_MIN; 2241 #endif 2242 2243 struct stack_size_debug_context *shim_args = (struct stack_size_debug_context *)malloc(sizeof *shim_args); 2244 if (! shim_args) { 2245 perror("malloc"); 2246 exit(EXIT_FAILURE); 2247 } 2248 2249 ret = posix_memalign((void**)&myStack, sysconf(_SC_PAGESIZE), stackSize); 2250 if (ret != 0 || myStack == NULL) 2251 err_sys_with_errno("posix_memalign failed\n"); 2252 2253 XMEMSET(myStack, STACK_CHECK_VAL, stackSize); 2254 2255 ret = pthread_attr_init(&myAttr); 2256 if (ret != 0) 2257 err_sys("attr_init failed"); 2258 2259 ret = pthread_attr_setstack(&myAttr, myStack, stackSize); 2260 if (ret != 0) 2261 err_sys("attr_setstackaddr failed"); 2262 2263 shim_args->myStack = myStack; 2264 shim_args->stackSize = stackSize; 2265 #ifdef HAVE_STACK_SIZE_VERBOSE 2266 shim_args->stackSizeHWM_ptr = &StackSizeCheck_stackSizeHWM; 2267 shim_args->fn = tf; 2268 shim_args->args = args; 2269 ret = pthread_create(threadId, &myAttr, (thread_func)debug_stack_size_verbose_shim, (void *)shim_args); 2270 #else 2271 ret = pthread_create(threadId, &myAttr, tf, args); 2272 #endif 2273 if (ret != 0) { 2274 fprintf(stderr,"pthread_create failed: %s",strerror(ret)); 2275 exit(EXIT_FAILURE); 2276 } 2277 2278 *stack_context = (void *)shim_args; 2279 2280 return 0; 2281 } 2282 2283 static WC_INLINE int StackSizeCheck_reap(pthread_t threadId, void *stack_context) 2284 { 2285 struct stack_size_debug_context *shim_args = (struct stack_size_debug_context *)stack_context; 2286 size_t i; 2287 void *status; 2288 int ret = pthread_join(threadId, &status); 2289 if (ret != 0) 2290 err_sys("pthread_join failed"); 2291 2292 for (i = 0; i < shim_args->stackSize; i++) { 2293 if (shim_args->myStack[i] != STACK_CHECK_VAL) { 2294 break; 2295 } 2296 } 2297 2298 free(shim_args->myStack); 2299 #ifdef HAVE_STACK_SIZE_VERBOSE 2300 printf("stack used = %lu\n", 2301 *shim_args->stackSizeHWM_ptr > (shim_args->stackSize - i) 2302 ? (unsigned long)*shim_args->stackSizeHWM_ptr 2303 : (unsigned long)(shim_args->stackSize - i)); 2304 #else 2305 { 2306 size_t used = shim_args->stackSize - i; 2307 printf("stack used = %lu\n", (unsigned long)used); 2308 } 2309 #endif 2310 free(shim_args); 2311 2312 return (int)((size_t)status); 2313 } 2314 2002 2315 2003 2316 #endif /* HAVE_STACK_SIZE */ 2004 2317 2318 #ifndef STACK_SIZE_CHECKPOINT 2319 #define STACK_SIZE_CHECKPOINT(...) (__VA_ARGS__) 2320 #endif 2321 #ifndef STACK_SIZE_INIT 2322 #define STACK_SIZE_INIT() 2323 #endif 2005 2324 2006 2325 #ifdef STACK_TRAP … … 2021 2340 struct rlimit rl; 2022 2341 if (getrlimit(RLIMIT_STACK, &rl) != 0) 2023 err_sys ("getrlimit failed");2342 err_sys_with_errno("getrlimit failed"); 2024 2343 printf("rlim_cur = %llu\n", rl.rlim_cur); 2025 2344 rl.rlim_cur = 1024*21; /* adjust trap size here */ 2026 if (setrlimit(RLIMIT_STACK, &rl) != 0) { 2027 perror("setrlimit"); 2028 err_sys("setrlimit failed"); 2029 } 2345 if (setrlimit(RLIMIT_STACK, &rl) != 0) 2346 err_sys_with_errno("setrlimit failed"); 2030 2347 } 2031 2348 … … 2390 2707 encCtx = (AtomicEncCtx*)malloc(sizeof(AtomicEncCtx)); 2391 2708 if (encCtx == NULL) 2392 err_sys ("AtomicEncCtx malloc failed");2709 err_sys_with_errno("AtomicEncCtx malloc failed"); 2393 2710 XMEMSET(encCtx, 0, sizeof(AtomicEncCtx)); 2394 2711 … … 2396 2713 if (decCtx == NULL) { 2397 2714 free(encCtx); 2398 err_sys ("AtomicDecCtx malloc failed");2715 err_sys_with_errno("AtomicDecCtx malloc failed"); 2399 2716 } 2400 2717 XMEMSET(decCtx, 0, sizeof(AtomicDecCtx)); … … 2665 2982 } 2666 2983 2984 #if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_FIPS) && \ 2985 !defined(HAVE_SELFTEST) 2986 if (ret == 0) { 2987 ret = wc_ecc_set_rng(privKey, wolfSSL_GetRNG(ssl)); 2988 } 2989 #endif 2990 2667 2991 /* generate shared secret and return it */ 2668 2992 if (ret == 0) { … … 2874 3198 ret = wc_Ed448PrivateKeyDecode(keyBuf, &idx, &myKey, keySz); 2875 3199 if (ret == 0) 2876 ret = wc_ed448_sign_msg(in, inSz, out, outSz, &myKey );3200 ret = wc_ed448_sign_msg(in, inSz, out, outSz, &myKey, NULL, 0); 2877 3201 wc_ed448_free(&myKey); 2878 3202 } … … 2906 3230 ret = wc_ed448_import_public(key, keySz, &myKey); 2907 3231 if (ret == 0) { 2908 ret = wc_ed448_verify_msg(sig, sigSz, msg, msgSz, result, &myKey); 3232 ret = wc_ed448_verify_msg(sig, sigSz, msg, msgSz, result, &myKey, 3233 NULL, 0); 2909 3234 } 2910 3235 wc_ed448_free(&myKey); … … 3499 3824 #endif /* HAVE_PK_CALLBACKS */ 3500 3825 3501 3502 3826 static WC_INLINE int SimulateWantWriteIOSendCb(WOLFSSL *ssl, char *buf, int sz, void *ctx) 3827 { 3828 static int wantWriteFlag = 1; 3829 3830 int sent; 3831 int sd = *(int*)ctx; 3832 3833 (void)ssl; 3834 3835 if (!wantWriteFlag) 3836 { 3837 wantWriteFlag = 1; 3838 3839 sent = wolfIO_Send(sd, buf, sz, 0); 3840 if (sent < 0) { 3841 int err = errno; 3842 3843 if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) { 3844 return WOLFSSL_CBIO_ERR_WANT_WRITE; 3845 } 3846 else if (err == SOCKET_ECONNRESET) { 3847 return WOLFSSL_CBIO_ERR_CONN_RST; 3848 } 3849 else if (err == SOCKET_EINTR) { 3850 return WOLFSSL_CBIO_ERR_ISR; 3851 } 3852 else if (err == SOCKET_EPIPE) { 3853 return WOLFSSL_CBIO_ERR_CONN_CLOSE; 3854 } 3855 else { 3856 return WOLFSSL_CBIO_ERR_GENERAL; 3857 } 3858 } 3859 3860 return sent; 3861 } 3862 else 3863 { 3864 wantWriteFlag = 0; 3865 return WOLFSSL_CBIO_ERR_WANT_WRITE; 3866 } 3867 } 3503 3868 3504 3869 #if defined(__hpux__) || defined(__MINGW32__) || defined (WOLFSSL_TIRTOS) \ … … 3567 3932 3568 3933 3569 #if defined(HAVE_SESSION_TICKET) && defined(HAVE_CHACHA) && \ 3570 defined(HAVE_POLY1305) 3571 3934 #if defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \ 3935 ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || \ 3936 defined(HAVE_AESGCM)) 3937 3938 #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) 3572 3939 #include <wolfssl/wolfcrypt/chacha20_poly1305.h> 3940 #define WOLFSSL_TICKET_KEY_SZ CHACHA20_POLY1305_AEAD_KEYSIZE 3941 #elif defined(HAVE_AESGCM) 3942 #include <wolfssl/wolfcrypt/aes.h> 3943 #include <wolfssl/wolfcrypt/wc_encrypt.h> /* AES IV sizes in FIPS mode */ 3944 #define WOLFSSL_TICKET_KEY_SZ AES_256_KEY_SIZE 3945 #endif 3573 3946 3574 3947 typedef struct key_ctx { 3575 byte name[WOLFSSL_TICKET_NAME_SZ]; 3576 byte key[ CHACHA20_POLY1305_AEAD_KEYSIZE];/* cipher key */3948 byte name[WOLFSSL_TICKET_NAME_SZ]; /* name for this context */ 3949 byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */ 3577 3950 } key_ctx; 3578 3951 … … 3606 3979 void* userCtx) 3607 3980 { 3608 (void)ssl;3609 (void)userCtx;3610 3611 3981 int ret; 3612 3982 word16 sLen = XHTONS(inLen); … … 3614 3984 int aadSz = WOLFSSL_TICKET_NAME_SZ + WOLFSSL_TICKET_IV_SZ + 2; 3615 3985 byte* tmp = aad; 3616 3986 #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) 3987 /* chahca20/poly1305 */ 3988 #elif defined(HAVE_AESGCM) 3989 Aes aes; 3990 #endif 3991 3992 (void)ssl; 3993 (void)userCtx; 3994 3995 /* encrypt */ 3617 3996 if (enc) { 3618 3997 XMEMCPY(key_name, myKey_ctx.name, WOLFSSL_TICKET_NAME_SZ); … … 3626 4005 XMEMCPY(tmp, iv, WOLFSSL_TICKET_IV_SZ); 3627 4006 tmp += WOLFSSL_TICKET_IV_SZ; 3628 XMEMCPY(tmp, &sLen, 2); 3629 4007 XMEMCPY(tmp, &sLen, sizeof(sLen)); 4008 4009 #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) 3630 4010 ret = wc_ChaCha20Poly1305_Encrypt(myKey_ctx.key, iv, 3631 4011 aad, aadSz, … … 3633 4013 ticket, 3634 4014 mac); 4015 #elif defined(HAVE_AESGCM) 4016 ret = wc_AesInit(&aes, NULL, INVALID_DEVID); 4017 if (ret != 0) return WOLFSSL_TICKET_RET_REJECT; 4018 4019 ret = wc_AesGcmSetKey(&aes, myKey_ctx.key, sizeof(myKey_ctx.key)); 4020 if (ret == 0) { 4021 ret = wc_AesGcmEncrypt(&aes, ticket, ticket, inLen, 4022 iv, GCM_NONCE_MID_SZ, mac, AES_BLOCK_SIZE, 4023 aad, aadSz); 4024 } 4025 wc_AesFree(&aes); 4026 #endif 4027 3635 4028 if (ret != 0) return WOLFSSL_TICKET_RET_REJECT; 3636 4029 *outLen = inLen; /* no padding in this mode */ 3637 } else {3638 3639 4030 } 4031 /* decrypt */ 4032 else { 3640 4033 /* see if we know this key */ 3641 4034 if (XMEMCMP(key_name, myKey_ctx.name, WOLFSSL_TICKET_NAME_SZ) != 0){ 3642 printf("client presented unknown ticket key name ");4035 printf("client presented unknown ticket key name %s\n", key_name); 3643 4036 return WOLFSSL_TICKET_RET_FATAL; 3644 4037 } … … 3649 4042 XMEMCPY(tmp, iv, WOLFSSL_TICKET_IV_SZ); 3650 4043 tmp += WOLFSSL_TICKET_IV_SZ; 3651 XMEMCPY(tmp, &sLen, 2); 3652 4044 XMEMCPY(tmp, &sLen, sizeof(sLen)); 4045 4046 #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) 3653 4047 ret = wc_ChaCha20Poly1305_Decrypt(myKey_ctx.key, iv, 3654 4048 aad, aadSz, … … 3656 4050 mac, 3657 4051 ticket); 4052 #elif defined(HAVE_AESGCM) 4053 ret = wc_AesInit(&aes, NULL, INVALID_DEVID); 4054 if (ret != 0) return WOLFSSL_TICKET_RET_REJECT; 4055 4056 ret = wc_AesGcmSetKey(&aes, myKey_ctx.key, sizeof(myKey_ctx.key)); 4057 if (ret == 0) { 4058 ret = wc_AesGcmDecrypt(&aes, ticket, ticket, inLen, 4059 iv, GCM_NONCE_MID_SZ, mac, AES_BLOCK_SIZE, 4060 aad, aadSz); 4061 } 4062 wc_AesFree(&aes); 4063 #endif 4064 3658 4065 if (ret != 0) return WOLFSSL_TICKET_RET_REJECT; 3659 4066 *outLen = inLen; /* no padding in this mode */ … … 3663 4070 } 3664 4071 3665 #endif /* HAVE_SESSION_TICKET && CHACHA20 && POLY1305 */ 4072 #endif /* HAVE_SESSION_TICKET && ((HAVE_CHACHA && HAVE_POLY1305) || HAVE_AESGCM) */ 4073 3666 4074 3667 4075 static WC_INLINE word16 GetRandomPort(void)
Note:
See TracChangeset
for help on using the changeset viewer.