Ignore:
Timestamp:
Jun 22, 2021, 9:00:19 PM (3 years ago)
Author:
coas-nagasima
Message:

WolfSSLとAzure IoT SDKを更新

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  
    44#define wolfSSL_TEST_H
    55
    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
    813#include <assert.h>
    914#include <ctype.h>
     
    5661    #define SOCKET_T SOCKET
    5762    #define SNPRINTF _snprintf
     63    #define XSLEEP_MS(t) Sleep(t)
    5864#elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
    5965    #include <string.h>
     
    7076    }
    7177    #if defined(HAVE_KEIL_RTX)
    72         #define sleep(t) os_dly_wait(t/1000+1);
     78        #define XSLEEP_MS(t)  os_dly_wait(t)
    7379    #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2)
    74         #define sleep(t) osDelay(t/1000+1);
     80        #define XSLEEP_MS(t)  osDelay(t)
    7581    #endif
    7682#elif defined(WOLFSSL_TIRTOS)
     
    8995    };
    9096    #define SOCKET_T int
     97    #define XSLEEP_MS(t) Task_sleep(t/1000)
    9198#elif defined(WOLFSSL_VXWORKS)
    9299    #include <hostLib.h>
     
    144151    #endif
    145152#endif
     153    #ifdef FREESCALE_MQX
     154        typedef int socklen_t ;
     155    #endif
    146156    #define SOCKET_T int
    147157    #ifndef SO_NOSIGPIPE
     
    149159    #endif
    150160    #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)
    151168#endif /* USE_WINDOWS_API */
     169
     170#ifndef XSLEEP_MS
     171    #define XSLEEP_MS(t) sleep(t/1000)
     172#endif
    152173
    153174#ifdef WOLFSSL_ASYNC_CRYPT
     
    231252        #define INFINITE -1
    232253        #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)
    234255        typedef unsigned int  THREAD_RETURN;
    235256        typedef int           THREAD_TYPE;
     
    287308    #define DEFAULT_MIN_RSAKEY_BITS 2048
    288309#else
     310    #ifndef DEFAULT_MIN_RSAKEY_BITS
    289311    #define DEFAULT_MIN_RSAKEY_BITS 1024
     312    #endif
    290313#endif
    291314#if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH)
    292315    #define DEFAULT_MIN_ECCKEY_BITS 256
    293316#else
     317    #ifndef DEFAULT_MIN_ECCKEY_BITS
    294318    #define DEFAULT_MIN_ECCKEY_BITS 224
     319    #endif
    295320#endif
    296321
     
    410435    ssl_callback on_result;
    411436    WOLFSSL_CTX* ctx;
     437    unsigned char isSharedCtx:1;
    412438} callback_functions;
    413439
     
    472498err_sys(const char* msg)
    473499{
    474     printf("wolfSSL error: %s\n", msg);
    475 
    476500#if !defined(__GNUC__)
    477501    /* scan-build (which pretends to be gnuc) can get confused and think the
     
    484508#endif
    485509    {
     510        printf("wolfSSL error: %s\n", msg);
     511
     512        XEXIT_T(EXIT_FAILURE);
     513    }
     514}
     515
     516static WC_INLINE
     517#if defined(WOLFSSL_FORCE_MALLOC_FAIL_TEST) || defined(WOLFSSL_ZEPHYR)
     518THREAD_RETURN
     519#else
     520WC_NORETURN void
     521#endif
     522err_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
    486540        XEXIT_T(EXIT_FAILURE);
    487541    }
     
    492546extern char* myoptarg;
    493547
     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 */
    494559static WC_INLINE int mygetopt(int argc, char** argv, const char* optstring)
    495560{
     
    541606    cp = (char*)strchr(optstring, c);
    542607
    543     if (cp == NULL || c == ':')
     608    if (cp == NULL || c == ':' || c == ';')
    544609        return '?';
    545610
     
    557622        else
    558623            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        }
    559638    }
    560639
     
    9351014
    9361015    if(WOLFSSL_SOCKET_IS_INVALID(*sockfd)) {
    937         err_sys("socket failed\n");
     1016        err_sys_with_errno("socket failed\n");
    9381017    }
    9391018
     
    9451024        int       res = setsockopt(*sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, len);
    9461025        if (res < 0)
    947             err_sys("setsockopt SO_NOSIGPIPE failed\n");
     1026            err_sys_with_errno("setsockopt SO_NOSIGPIPE failed\n");
    9481027    }
    9491028#elif defined(WOLFSSL_MDK_ARM) || defined (WOLFSSL_TIRTOS) ||\
     
    9611040        int       res = setsockopt(*sockfd, IPPROTO_TCP, TCP_NODELAY, &on, len);
    9621041        if (res < 0)
    963             err_sys("setsockopt TCP_NODELAY failed\n");
     1042            err_sys_with_errno("setsockopt TCP_NODELAY failed\n");
    9641043    }
    9651044#endif
     
    9791058    if (!udp) {
    9801059        if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
    981             err_sys("tcp connect failed");
     1060            err_sys_with_errno("tcp connect failed");
    9821061    }
    9831062}
     
    9871066{
    9881067    if (connect(*sockfd, (const struct sockaddr*)addr, addrSz) != 0)
    989         err_sys("tcp connect failed");
     1068        err_sys_with_errno("tcp connect failed");
    9901069}
    9911070
     
    10851164        res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
    10861165        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
    10891177#endif
    10901178
    10911179    if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
    1092         err_sys("tcp bind failed");
     1180        err_sys_with_errno("tcp bind failed");
    10931181    if (!udp) {
    10941182        #ifdef WOLFSSL_KEIL_TCP_NET
     
    10981186        #endif
    10991187        if (listen(*sockfd, SOCK_LISTEN_MAX_QUEUE) != 0)
    1100                 err_sys("tcp listen failed");
     1188                err_sys_with_errno("tcp listen failed");
    11011189    }
    11021190    #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_TIRTOS) \
     
    11551243        res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
    11561244        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
    11591256#endif
    11601257
    11611258    if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
    1162         err_sys("tcp bind failed");
     1259        err_sys_with_errno("tcp bind failed");
    11631260
    11641261    #if (defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API)) && !defined(WOLFSSL_TIRTOS)
     
    12011298                              int udp, int sctp, int ready_file, int do_listen)
    12021299{
    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);
    12051302    tcp_ready* ready = NULL;
    12061303
     
    12591356    }
    12601357
    1261     *clientfd = accept(*sockfd, (struct sockaddr*)&client,
     1358    *clientfd = accept(*sockfd, (struct sockaddr*)&client_addr,
    12621359                      (ACCEPT_THIRD_T)&client_len);
    12631360    if(WOLFSSL_SOCKET_IS_INVALID(*clientfd)) {
    1264         err_sys("tcp accept failed");
     1361        err_sys_with_errno("tcp accept failed");
    12651362    }
    12661363}
     
    12731370        int ret = ioctlsocket(*sockfd, FIONBIO, &blocking);
    12741371        if (ret == SOCKET_ERROR)
    1275             err_sys("ioctlsocket failed");
     1372            err_sys_with_errno("ioctlsocket failed");
    12761373    #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) \
    12771374        || defined (WOLFSSL_TIRTOS)|| defined(WOLFSSL_VXWORKS) \
     
    12811378        int flags = fcntl(*sockfd, F_GETFL, 0);
    12821379        if (flags < 0)
    1283             err_sys("fcntl get failed");
     1380            err_sys_with_errno("fcntl get failed");
    12841381        flags = fcntl(*sockfd, F_SETFL, flags | O_NONBLOCK);
    12851382        if (flags < 0)
    1286             err_sys("fcntl set failed");
     1383            err_sys_with_errno("fcntl set failed");
    12871384    #endif
    12881385}
     
    13031400
    13041401    /* 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);
    13061403
    13071404    if (wolfSSL_GetVersion(ssl) < WOLFSSL_TLSV1_3) {
     
    13371434
    13381435    /* 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)
    13401437        return 0;
    13411438
     
    13711468    int i;
    13721469    int b = 0x01;
     1470    const char* userCipher = (const char*)wolfSSL_get_psk_callback_ctx(ssl);
    13731471
    13741472    (void)ssl;
     
    13771475
    13781476    /* 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);
    13801478
    13811479    for (i = 0; i < 32; i++, b += 0x22) {
     
    13851483    }
    13861484
    1387     *ciphersuite = "TLS13-AES128-GCM-SHA256";
     1485    *ciphersuite = userCipher ? userCipher : "TLS13-AES128-GCM-SHA256";
    13881486
    13891487    return 32;   /* length of key in octets or 0 for error */
     
    13971495    int i;
    13981496    int b = 0x01;
     1497    const char* userCipher = (const char*)wolfSSL_get_psk_callback_ctx(ssl);
    13991498
    14001499    (void)ssl;
     
    14021501
    14031502    /* 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)
    14051504        return 0;
    14061505
     
    14111510    }
    14121511
    1413     *ciphersuite = "TLS13-AES128-GCM-SHA256";
     1512    *ciphersuite = userCipher ? userCipher : "TLS13-AES128-GCM-SHA256";
    14141513
    14151514    return 32;   /* length of key in octets or 0 for error */
    14161515}
    14171516
    1418 #endif /* NO_PSK */
     1517#endif /* !NO_PSK */
    14191518
    14201519
     
    14991598static WC_INLINE void OCSPRespFreeCb(void* ioCtx, unsigned char* response)
    15001599{
    1501     (void)ioCtx;
    1502     (void)response;
     1600    return EmbedOcspRespFree(ioCtx, response);
    15031601}
    15041602#endif
     
    15131611        int ret;
    15141612        long int fileSz;
    1515         XFILE file;
     1613        XFILE lFile;
    15161614
    15171615        if (fname == NULL || buf == NULL || bufLen == NULL)
     
    15231621
    15241622        /* open file (read-only binary) */
    1525         file = XFOPEN(fname, "rb");
    1526         if (!file) {
     1623        lFile = XFOPEN(fname, "rb");
     1624        if (!lFile) {
    15271625            printf("Error loading %s\n", fname);
    15281626            return BAD_PATH_ERROR;
    15291627        }
    15301628
    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);
    15341632        if (fileSz  > 0) {
    15351633            *bufLen = (size_t)fileSz;
     
    15401638            }
    15411639            else {
    1542                 size_t readLen = fread(*buf, *bufLen, 1, file);
     1640                size_t readLen = fread(*buf, *bufLen, 1, lFile);
    15431641
    15441642                /* check response code */
     
    15491647            ret = BUFFER_E;
    15501648        }
    1551         fclose(file);
     1649        fclose(lFile);
    15521650
    15531651        return ret;
     
    16761774#endif /* !NO_CERTS */
    16771775
    1678 static int myVerifyFail = 0;
     1776enum {
     1777    VERIFY_OVERRIDE_ERROR,
     1778    VERIFY_FORCE_FAIL,
     1779    VERIFY_USE_PREVERFIY,
     1780    VERIFY_OVERRIDE_DATE_ERR,
     1781};
     1782static THREAD_LS_T int myVerifyAction = VERIFY_OVERRIDE_ERROR;
    16791783
    16801784/* The verify callback is called for every certificate only when
     
    17281832        XFREE(issuer,  0, DYNAMIC_TYPE_OPENSSL);
    17291833#if defined(SHOW_CERTS) && !defined(NO_FILESYSTEM)
    1730 /* avoid printing duplicate certs */
     1834        /* avoid printing duplicate certs */
    17311835        if (store->depth == 1) {
    17321836            /* retrieve x509 certs and display them on stdout */
     
    17631867
    17641868    /* Testing forced fail case by return zero */
    1765     if (myVerifyFail) {
     1869    if (myVerifyAction == VERIFY_FORCE_FAIL) {
    17661870        return 0; /* test failure case */
    17671871    }
    17681872
     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
    17691879    /* If error indicate we are overriding it for testing purposes */
    1770     if (store->error != 0) {
     1880    if (store->error != 0 && myVerifyAction == VERIFY_OVERRIDE_ERROR) {
    17711881        printf("\tAllowing failed certificate check, testing only "
    17721882            "(shouldn't do this in production)\n");
     
    17741884
    17751885    /* 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;
    17961887}
    17971888
     
    19172008        #if !defined(NO_FILESYSTEM) || defined(FORCE_BUFFER_TEST)
    19182009            int depth, res;
    1919             XFILE file;
     2010            XFILE keyFile;
    19202011            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);
    19242015                    return depth;
    19252016                }
     
    19482039#define STACK_CHECK_VAL 0x01
    19492040
     2041struct 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
     2060static 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
     2067static 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
     2081static 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
     2102static 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
     2112static
     2113#ifdef __GNUC__
     2114__attribute__((unused)) __attribute__((noinline))
     2115#endif
     2116int 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
    19502158static WC_INLINE int StackSizeCheck(func_args* args, thread_func tf)
    19512159{
    1952     int            ret, i, used;
     2160    size_t         i;
     2161    int            ret;
    19532162    void*          status;
    19542163    unsigned char* myStack = NULL;
    1955     int            stackSize = 1024*152;
     2164    size_t         stackSize = 1024*1024;
    19562165    pthread_attr_t myAttr;
    19572166    pthread_t      threadId;
     2167#ifdef HAVE_STACK_SIZE_VERBOSE
     2168    struct stack_size_debug_context shim_args;
     2169#endif
    19582170
    19592171#ifdef PTHREAD_STACK_MIN
     
    19642176    ret = posix_memalign((void**)&myStack, sysconf(_SC_PAGESIZE), stackSize);
    19652177    if (ret != 0 || myStack == NULL)
    1966         err_sys("posix_memalign failed\n");
     2178        err_sys_with_errno("posix_memalign failed\n");
    19672179
    19682180    XMEMSET(myStack, STACK_CHECK_VAL, stackSize);
     
    19762188        err_sys("attr_setstackaddr failed");
    19772189
     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
    19782199    ret = pthread_create(&threadId, &myAttr, tf, args);
     2200#endif
    19792201    if (ret != 0) {
    19802202        perror("pthread_create failed");
     
    19932215
    19942216    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
    19982227
    19992228    return (int)((size_t)status);
    20002229}
    20012230
     2231static 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
     2283static 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
    20022315
    20032316#endif /* HAVE_STACK_SIZE */
    20042317
     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
    20052324
    20062325#ifdef STACK_TRAP
     
    20212340    struct rlimit  rl;
    20222341    if (getrlimit(RLIMIT_STACK, &rl) != 0)
    2023         err_sys("getrlimit failed");
     2342        err_sys_with_errno("getrlimit failed");
    20242343    printf("rlim_cur = %llu\n", rl.rlim_cur);
    20252344    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");
    20302347}
    20312348
     
    23902707    encCtx = (AtomicEncCtx*)malloc(sizeof(AtomicEncCtx));
    23912708    if (encCtx == NULL)
    2392         err_sys("AtomicEncCtx malloc failed");
     2709        err_sys_with_errno("AtomicEncCtx malloc failed");
    23932710    XMEMSET(encCtx, 0, sizeof(AtomicEncCtx));
    23942711
     
    23962713    if (decCtx == NULL) {
    23972714        free(encCtx);
    2398         err_sys("AtomicDecCtx malloc failed");
     2715        err_sys_with_errno("AtomicDecCtx malloc failed");
    23992716    }
    24002717    XMEMSET(decCtx, 0, sizeof(AtomicDecCtx));
     
    26652982    }
    26662983
     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
    26672991    /* generate shared secret and return it */
    26682992    if (ret == 0) {
     
    28743198        ret = wc_Ed448PrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
    28753199        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);
    28773201        wc_ed448_free(&myKey);
    28783202    }
     
    29063230        ret = wc_ed448_import_public(key, keySz, &myKey);
    29073231        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);
    29093234        }
    29103235        wc_ed448_free(&myKey);
     
    34993824#endif /* HAVE_PK_CALLBACKS */
    35003825
    3501 
    3502 
     3826static 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}
    35033868
    35043869#if defined(__hpux__) || defined(__MINGW32__) || defined (WOLFSSL_TIRTOS) \
     
    35673932
    35683933
    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)
    35723939    #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
    35733946
    35743947    typedef struct key_ctx {
    3575         byte name[WOLFSSL_TICKET_NAME_SZ];        /* name for this context */
    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 */
    35773950    } key_ctx;
    35783951
     
    36063979                             void* userCtx)
    36073980    {
    3608         (void)ssl;
    3609         (void)userCtx;
    3610 
    36113981        int ret;
    36123982        word16 sLen = XHTONS(inLen);
     
    36143984        int  aadSz = WOLFSSL_TICKET_NAME_SZ + WOLFSSL_TICKET_IV_SZ + 2;
    36153985        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 */
    36173996        if (enc) {
    36183997            XMEMCPY(key_name, myKey_ctx.name, WOLFSSL_TICKET_NAME_SZ);
     
    36264005            XMEMCPY(tmp, iv, WOLFSSL_TICKET_IV_SZ);
    36274006            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)
    36304010            ret = wc_ChaCha20Poly1305_Encrypt(myKey_ctx.key, iv,
    36314011                                              aad, aadSz,
     
    36334013                                              ticket,
    36344014                                              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           
    36354028            if (ret != 0) return WOLFSSL_TICKET_RET_REJECT;
    36364029            *outLen = inLen;  /* no padding in this mode */
    3637         } else {
    3638             /* decrypt */
    3639 
     4030        }
     4031        /* decrypt */
     4032        else {
    36404033            /* see if we know this key */
    36414034            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);
    36434036                return WOLFSSL_TICKET_RET_FATAL;
    36444037            }
     
    36494042            XMEMCPY(tmp, iv, WOLFSSL_TICKET_IV_SZ);
    36504043            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)
    36534047            ret = wc_ChaCha20Poly1305_Decrypt(myKey_ctx.key, iv,
    36544048                                              aad, aadSz,
     
    36564050                                              mac,
    36574051                                              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
    36584065            if (ret != 0) return WOLFSSL_TICKET_RET_REJECT;
    36594066            *outLen = inLen;  /* no padding in this mode */
     
    36634070    }
    36644071
    3665 #endif  /* HAVE_SESSION_TICKET && CHACHA20 && POLY1305 */
     4072#endif /* HAVE_SESSION_TICKET && ((HAVE_CHACHA && HAVE_POLY1305) || HAVE_AESGCM) */
     4073
    36664074
    36674075static WC_INLINE word16 GetRandomPort(void)
Note: See TracChangeset for help on using the changeset viewer.