wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Tue May 02 08:44:47 2017 +0000
Revision:
7:481bce714567
wolfSSL3.10.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 7:481bce714567 1 /* test.h */
wolfSSL 7:481bce714567 2
wolfSSL 7:481bce714567 3 #ifndef wolfSSL_TEST_H
wolfSSL 7:481bce714567 4 #define wolfSSL_TEST_H
wolfSSL 7:481bce714567 5
wolfSSL 7:481bce714567 6 #include <stdio.h>
wolfSSL 7:481bce714567 7 #include <stdlib.h>
wolfSSL 7:481bce714567 8 #include <assert.h>
wolfSSL 7:481bce714567 9 #include <ctype.h>
wolfSSL 7:481bce714567 10 #include <wolfssl/wolfcrypt/types.h>
wolfSSL 7:481bce714567 11 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 7:481bce714567 12 #include <wolfssl/wolfcrypt/random.h>
wolfSSL 7:481bce714567 13 #include <wolfssl/wolfcrypt/mem_track.h>
wolfSSL 7:481bce714567 14
wolfSSL 7:481bce714567 15 #ifdef ATOMIC_USER
wolfSSL 7:481bce714567 16 #include <wolfssl/wolfcrypt/aes.h>
wolfSSL 7:481bce714567 17 #include <wolfssl/wolfcrypt/arc4.h>
wolfSSL 7:481bce714567 18 #include <wolfssl/wolfcrypt/hmac.h>
wolfSSL 7:481bce714567 19 #endif
wolfSSL 7:481bce714567 20 #ifdef HAVE_PK_CALLBACKS
wolfSSL 7:481bce714567 21 #include <wolfssl/wolfcrypt/asn.h>
wolfSSL 7:481bce714567 22 #ifdef HAVE_ECC
wolfSSL 7:481bce714567 23 #include <wolfssl/wolfcrypt/ecc.h>
wolfSSL 7:481bce714567 24 #endif /* HAVE_ECC */
wolfSSL 7:481bce714567 25 #endif /*HAVE_PK_CALLBACKS */
wolfSSL 7:481bce714567 26
wolfSSL 7:481bce714567 27 #ifdef USE_WINDOWS_API
wolfSSL 7:481bce714567 28 #include <winsock2.h>
wolfSSL 7:481bce714567 29 #include <process.h>
wolfSSL 7:481bce714567 30 #ifdef TEST_IPV6 /* don't require newer SDK for IPV4 */
wolfSSL 7:481bce714567 31 #include <ws2tcpip.h>
wolfSSL 7:481bce714567 32 #include <wspiapi.h>
wolfSSL 7:481bce714567 33 #endif
wolfSSL 7:481bce714567 34 #define SOCKET_T SOCKET
wolfSSL 7:481bce714567 35 #define SNPRINTF _snprintf
wolfSSL 7:481bce714567 36 #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 7:481bce714567 37 #include <string.h>
wolfSSL 7:481bce714567 38 #include "rl_net.h"
wolfSSL 7:481bce714567 39 #define SOCKET_T int
wolfSSL 7:481bce714567 40 typedef int socklen_t ;
wolfSSL 7:481bce714567 41 static unsigned long inet_addr(const char *cp)
wolfSSL 7:481bce714567 42 {
wolfSSL 7:481bce714567 43 unsigned int a[4] ; unsigned long ret ;
wolfSSL 7:481bce714567 44 sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) ;
wolfSSL 7:481bce714567 45 ret = ((a[3]<<24) + (a[2]<<16) + (a[1]<<8) + a[0]) ;
wolfSSL 7:481bce714567 46 return(ret) ;
wolfSSL 7:481bce714567 47 }
wolfSSL 7:481bce714567 48 #if defined(HAVE_KEIL_RTX)
wolfSSL 7:481bce714567 49 #define sleep(t) os_dly_wait(t/1000+1) ;
wolfSSL 7:481bce714567 50 #elif defined (WOLFSSL_CMSIS_RTOS)
wolfSSL 7:481bce714567 51 #define sleep(t) osDelay(t/1000+1) ;
wolfSSL 7:481bce714567 52 #endif
wolfSSL 7:481bce714567 53
wolfSSL 7:481bce714567 54 static int wolfssl_tcp_select(int sd, int timeout)
wolfSSL 7:481bce714567 55 { return 0 ; }
wolfSSL 7:481bce714567 56 #define tcp_select(sd,t) wolfssl_tcp_select(sd, t) /* avoid conflicting Keil TCP tcp_select */
wolfSSL 7:481bce714567 57 #elif defined(WOLFSSL_TIRTOS)
wolfSSL 7:481bce714567 58 #include <string.h>
wolfSSL 7:481bce714567 59 #include <netdb.h>
wolfSSL 7:481bce714567 60 #include <sys/types.h>
wolfSSL 7:481bce714567 61 #include <arpa/inet.h>
wolfSSL 7:481bce714567 62 #include <sys/socket.h>
wolfSSL 7:481bce714567 63 #include <ti/sysbios/knl/Task.h>
wolfSSL 7:481bce714567 64 struct hostent {
wolfSSL 7:481bce714567 65 char *h_name; /* official name of host */
wolfSSL 7:481bce714567 66 char **h_aliases; /* alias list */
wolfSSL 7:481bce714567 67 int h_addrtype; /* host address type */
wolfSSL 7:481bce714567 68 int h_length; /* length of address */
wolfSSL 7:481bce714567 69 char **h_addr_list; /* list of addresses from name server */
wolfSSL 7:481bce714567 70 };
wolfSSL 7:481bce714567 71 #define SOCKET_T int
wolfSSL 7:481bce714567 72 #elif defined(WOLFSSL_VXWORKS)
wolfSSL 7:481bce714567 73 #include <hostLib.h>
wolfSSL 7:481bce714567 74 #include <sockLib.h>
wolfSSL 7:481bce714567 75 #include <arpa/inet.h>
wolfSSL 7:481bce714567 76 #include <string.h>
wolfSSL 7:481bce714567 77 #include <selectLib.h>
wolfSSL 7:481bce714567 78 #include <sys/types.h>
wolfSSL 7:481bce714567 79 #include <netinet/in.h>
wolfSSL 7:481bce714567 80 #include <fcntl.h>
wolfSSL 7:481bce714567 81 #include <sys/time.h>
wolfSSL 7:481bce714567 82 #include <netdb.h>
wolfSSL 7:481bce714567 83 #include <pthread.h>
wolfSSL 7:481bce714567 84 #define SOCKET_T int
wolfSSL 7:481bce714567 85 #else
wolfSSL 7:481bce714567 86 #include <string.h>
wolfSSL 7:481bce714567 87 #include <sys/types.h>
wolfSSL 7:481bce714567 88 #ifndef WOLFSSL_LEANPSK
wolfSSL 7:481bce714567 89 #include <unistd.h>
wolfSSL 7:481bce714567 90 #include <netdb.h>
wolfSSL 7:481bce714567 91 #include <netinet/in.h>
wolfSSL 7:481bce714567 92 #include <netinet/tcp.h>
wolfSSL 7:481bce714567 93 #include <arpa/inet.h>
wolfSSL 7:481bce714567 94 #include <sys/ioctl.h>
wolfSSL 7:481bce714567 95 #include <sys/time.h>
wolfSSL 7:481bce714567 96 #include <sys/socket.h>
wolfSSL 7:481bce714567 97 #include <pthread.h>
wolfSSL 7:481bce714567 98 #include <fcntl.h>
wolfSSL 7:481bce714567 99 #ifdef TEST_IPV6
wolfSSL 7:481bce714567 100 #include <netdb.h>
wolfSSL 7:481bce714567 101 #endif
wolfSSL 7:481bce714567 102 #endif
wolfSSL 7:481bce714567 103 #define SOCKET_T int
wolfSSL 7:481bce714567 104 #ifndef SO_NOSIGPIPE
wolfSSL 7:481bce714567 105 #include <signal.h> /* ignore SIGPIPE */
wolfSSL 7:481bce714567 106 #endif
wolfSSL 7:481bce714567 107 #define SNPRINTF snprintf
wolfSSL 7:481bce714567 108 #endif /* USE_WINDOWS_API */
wolfSSL 7:481bce714567 109
wolfSSL 7:481bce714567 110 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 7:481bce714567 111 #include <wolfssl/wolfcrypt/async.h>
wolfSSL 7:481bce714567 112 #endif
wolfSSL 7:481bce714567 113 #ifdef HAVE_CAVIUM
wolfSSL 7:481bce714567 114 #include <wolfssl/wolfcrypt/port/cavium/cavium_nitrox.h>
wolfSSL 7:481bce714567 115 #endif
wolfSSL 7:481bce714567 116
wolfSSL 7:481bce714567 117 #ifdef _MSC_VER
wolfSSL 7:481bce714567 118 /* disable conversion warning */
wolfSSL 7:481bce714567 119 /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
wolfSSL 7:481bce714567 120 #pragma warning(disable:4244 4996)
wolfSSL 7:481bce714567 121 #endif
wolfSSL 7:481bce714567 122
wolfSSL 7:481bce714567 123 /* Buffer for benchmark tests */
wolfSSL 7:481bce714567 124 #ifndef TEST_BUFFER_SIZE
wolfSSL 7:481bce714567 125 #define TEST_BUFFER_SIZE 16384
wolfSSL 7:481bce714567 126 #endif
wolfSSL 7:481bce714567 127
wolfSSL 7:481bce714567 128 #ifndef WOLFSSL_HAVE_MIN
wolfSSL 7:481bce714567 129 #define WOLFSSL_HAVE_MIN
wolfSSL 7:481bce714567 130 static INLINE word32 min(word32 a, word32 b)
wolfSSL 7:481bce714567 131 {
wolfSSL 7:481bce714567 132 return a > b ? b : a;
wolfSSL 7:481bce714567 133 }
wolfSSL 7:481bce714567 134 #endif /* WOLFSSL_HAVE_MIN */
wolfSSL 7:481bce714567 135
wolfSSL 7:481bce714567 136 /* Socket Handling */
wolfSSL 7:481bce714567 137 #ifndef WOLFSSL_SOCKET_INVALID
wolfSSL 7:481bce714567 138 #ifdef USE_WINDOWS_API
wolfSSL 7:481bce714567 139 #define WOLFSSL_SOCKET_INVALID ((SOCKET_T)INVALID_SOCKET)
wolfSSL 7:481bce714567 140 #elif defined(WOLFSSL_TIRTOS)
wolfSSL 7:481bce714567 141 #define WOLFSSL_SOCKET_INVALID ((SOCKET_T)-1)
wolfSSL 7:481bce714567 142 #else
wolfSSL 7:481bce714567 143 #define WOLFSSL_SOCKET_INVALID (SOCKET_T)(0)
wolfSSL 7:481bce714567 144 #endif
wolfSSL 7:481bce714567 145 #endif /* WOLFSSL_SOCKET_INVALID */
wolfSSL 7:481bce714567 146
wolfSSL 7:481bce714567 147 #ifndef WOLFSSL_SOCKET_IS_INVALID
wolfSSL 7:481bce714567 148 #if defined(USE_WINDOWS_API) || defined(WOLFSSL_TIRTOS)
wolfSSL 7:481bce714567 149 #define WOLFSSL_SOCKET_IS_INVALID(s) ((SOCKET_T)(s) == WOLFSSL_SOCKET_INVALID)
wolfSSL 7:481bce714567 150 #else
wolfSSL 7:481bce714567 151 #define WOLFSSL_SOCKET_IS_INVALID(s) ((SOCKET_T)(s) < WOLFSSL_SOCKET_INVALID)
wolfSSL 7:481bce714567 152 #endif
wolfSSL 7:481bce714567 153 #endif /* WOLFSSL_SOCKET_IS_INVALID */
wolfSSL 7:481bce714567 154
wolfSSL 7:481bce714567 155 #if defined(__MACH__) || defined(USE_WINDOWS_API)
wolfSSL 7:481bce714567 156 #ifndef _SOCKLEN_T
wolfSSL 7:481bce714567 157 typedef int socklen_t;
wolfSSL 7:481bce714567 158 #endif
wolfSSL 7:481bce714567 159 #endif
wolfSSL 7:481bce714567 160
wolfSSL 7:481bce714567 161
wolfSSL 7:481bce714567 162 /* HPUX doesn't use socklent_t for third parameter to accept, unless
wolfSSL 7:481bce714567 163 _XOPEN_SOURCE_EXTENDED is defined */
wolfSSL 7:481bce714567 164 #if !defined(__hpux__) && !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_IAR_ARM)\
wolfSSL 7:481bce714567 165 && !defined(WOLFSSL_ROWLEY_ARM) && !defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 7:481bce714567 166 typedef socklen_t* ACCEPT_THIRD_T;
wolfSSL 7:481bce714567 167 #else
wolfSSL 7:481bce714567 168 #if defined _XOPEN_SOURCE_EXTENDED
wolfSSL 7:481bce714567 169 typedef socklen_t* ACCEPT_THIRD_T;
wolfSSL 7:481bce714567 170 #else
wolfSSL 7:481bce714567 171 typedef int* ACCEPT_THIRD_T;
wolfSSL 7:481bce714567 172 #endif
wolfSSL 7:481bce714567 173 #endif
wolfSSL 7:481bce714567 174
wolfSSL 7:481bce714567 175
wolfSSL 7:481bce714567 176 #ifdef USE_WINDOWS_API
wolfSSL 7:481bce714567 177 #define CloseSocket(s) closesocket(s)
wolfSSL 7:481bce714567 178 #define StartTCP() { WSADATA wsd; WSAStartup(0x0002, &wsd); }
wolfSSL 7:481bce714567 179 #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 7:481bce714567 180 #define CloseSocket(s) closesocket(s)
wolfSSL 7:481bce714567 181 #define StartTCP()
wolfSSL 7:481bce714567 182 #else
wolfSSL 7:481bce714567 183 #define CloseSocket(s) close(s)
wolfSSL 7:481bce714567 184 #define StartTCP()
wolfSSL 7:481bce714567 185 #endif
wolfSSL 7:481bce714567 186
wolfSSL 7:481bce714567 187
wolfSSL 7:481bce714567 188 #ifdef SINGLE_THREADED
wolfSSL 7:481bce714567 189 typedef unsigned int THREAD_RETURN;
wolfSSL 7:481bce714567 190 typedef void* THREAD_TYPE;
wolfSSL 7:481bce714567 191 #define WOLFSSL_THREAD
wolfSSL 7:481bce714567 192 #else
wolfSSL 7:481bce714567 193 #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
wolfSSL 7:481bce714567 194 typedef void* THREAD_RETURN;
wolfSSL 7:481bce714567 195 typedef pthread_t THREAD_TYPE;
wolfSSL 7:481bce714567 196 #define WOLFSSL_THREAD
wolfSSL 7:481bce714567 197 #define INFINITE -1
wolfSSL 7:481bce714567 198 #define WAIT_OBJECT_0 0L
wolfSSL 7:481bce714567 199 #elif defined(WOLFSSL_MDK_ARM)|| defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 7:481bce714567 200 typedef unsigned int THREAD_RETURN;
wolfSSL 7:481bce714567 201 typedef int THREAD_TYPE;
wolfSSL 7:481bce714567 202 #define WOLFSSL_THREAD
wolfSSL 7:481bce714567 203 #elif defined(WOLFSSL_TIRTOS)
wolfSSL 7:481bce714567 204 typedef void THREAD_RETURN;
wolfSSL 7:481bce714567 205 typedef Task_Handle THREAD_TYPE;
wolfSSL 7:481bce714567 206 #define WOLFSSL_THREAD
wolfSSL 7:481bce714567 207 #else
wolfSSL 7:481bce714567 208 typedef unsigned int THREAD_RETURN;
wolfSSL 7:481bce714567 209 typedef intptr_t THREAD_TYPE;
wolfSSL 7:481bce714567 210 #define WOLFSSL_THREAD __stdcall
wolfSSL 7:481bce714567 211 #endif
wolfSSL 7:481bce714567 212 #endif
wolfSSL 7:481bce714567 213
wolfSSL 7:481bce714567 214
wolfSSL 7:481bce714567 215 #ifdef TEST_IPV6
wolfSSL 7:481bce714567 216 typedef struct sockaddr_in6 SOCKADDR_IN_T;
wolfSSL 7:481bce714567 217 #define AF_INET_V AF_INET6
wolfSSL 7:481bce714567 218 #else
wolfSSL 7:481bce714567 219 typedef struct sockaddr_in SOCKADDR_IN_T;
wolfSSL 7:481bce714567 220 #define AF_INET_V AF_INET
wolfSSL 7:481bce714567 221 #endif
wolfSSL 7:481bce714567 222
wolfSSL 7:481bce714567 223
wolfSSL 7:481bce714567 224 #define SERVER_DEFAULT_VERSION 3
wolfSSL 7:481bce714567 225 #define SERVER_DTLS_DEFAULT_VERSION (-2)
wolfSSL 7:481bce714567 226 #define SERVER_INVALID_VERSION (-99)
wolfSSL 7:481bce714567 227 #define CLIENT_DEFAULT_VERSION 3
wolfSSL 7:481bce714567 228 #define CLIENT_DTLS_DEFAULT_VERSION (-2)
wolfSSL 7:481bce714567 229 #define CLIENT_INVALID_VERSION (-99)
wolfSSL 7:481bce714567 230 #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH)
wolfSSL 7:481bce714567 231 #define DEFAULT_MIN_DHKEY_BITS 2048
wolfSSL 7:481bce714567 232 #else
wolfSSL 7:481bce714567 233 #define DEFAULT_MIN_DHKEY_BITS 1024
wolfSSL 7:481bce714567 234 #endif
wolfSSL 7:481bce714567 235 #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH)
wolfSSL 7:481bce714567 236 #define DEFAULT_MIN_RSAKEY_BITS 2048
wolfSSL 7:481bce714567 237 #else
wolfSSL 7:481bce714567 238 #define DEFAULT_MIN_RSAKEY_BITS 1024
wolfSSL 7:481bce714567 239 #endif
wolfSSL 7:481bce714567 240 #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH)
wolfSSL 7:481bce714567 241 #define DEFAULT_MIN_ECCKEY_BITS 256
wolfSSL 7:481bce714567 242 #else
wolfSSL 7:481bce714567 243 #define DEFAULT_MIN_ECCKEY_BITS 224
wolfSSL 7:481bce714567 244 #endif
wolfSSL 7:481bce714567 245
wolfSSL 7:481bce714567 246 /* all certs relative to wolfSSL home directory now */
wolfSSL 7:481bce714567 247 #if defined(WOLFSSL_NO_CURRDIR) || defined(WOLFSSL_MDK_SHELL)
wolfSSL 7:481bce714567 248 #define caCert "certs/ca-cert.pem"
wolfSSL 7:481bce714567 249 #define eccCert "certs/server-ecc.pem"
wolfSSL 7:481bce714567 250 #define eccKey "certs/ecc-key.pem"
wolfSSL 7:481bce714567 251 #define svrCert "certs/server-cert.pem"
wolfSSL 7:481bce714567 252 #define svrKey "certs/server-key.pem"
wolfSSL 7:481bce714567 253 #define cliCert "certs/client-cert.pem"
wolfSSL 7:481bce714567 254 #define cliKey "certs/client-key.pem"
wolfSSL 7:481bce714567 255 #define ntruCert "certs/ntru-cert.pem"
wolfSSL 7:481bce714567 256 #define ntruKey "certs/ntru-key.raw"
wolfSSL 7:481bce714567 257 #define dhParam "certs/dh2048.pem"
wolfSSL 7:481bce714567 258 #define cliEccKey "certs/ecc-client-key.pem"
wolfSSL 7:481bce714567 259 #define cliEccCert "certs/client-ecc-cert.pem"
wolfSSL 7:481bce714567 260 #define crlPemDir "certs/crl"
wolfSSL 7:481bce714567 261 #ifdef HAVE_WNR
wolfSSL 7:481bce714567 262 /* Whitewood netRandom default config file */
wolfSSL 7:481bce714567 263 #define wnrConfig "wnr-example.conf"
wolfSSL 7:481bce714567 264 #endif
wolfSSL 7:481bce714567 265 #else
wolfSSL 7:481bce714567 266 #define caCert "./certs/ca-cert.pem"
wolfSSL 7:481bce714567 267 #define eccCert "./certs/server-ecc.pem"
wolfSSL 7:481bce714567 268 #define eccKey "./certs/ecc-key.pem"
wolfSSL 7:481bce714567 269 #define svrCert "./certs/server-cert.pem"
wolfSSL 7:481bce714567 270 #define svrKey "./certs/server-key.pem"
wolfSSL 7:481bce714567 271 #define cliCert "./certs/client-cert.pem"
wolfSSL 7:481bce714567 272 #define cliKey "./certs/client-key.pem"
wolfSSL 7:481bce714567 273 #define ntruCert "./certs/ntru-cert.pem"
wolfSSL 7:481bce714567 274 #define ntruKey "./certs/ntru-key.raw"
wolfSSL 7:481bce714567 275 #define dhParam "./certs/dh2048.pem"
wolfSSL 7:481bce714567 276 #define cliEccKey "./certs/ecc-client-key.pem"
wolfSSL 7:481bce714567 277 #define cliEccCert "./certs/client-ecc-cert.pem"
wolfSSL 7:481bce714567 278 #define crlPemDir "./certs/crl"
wolfSSL 7:481bce714567 279 #ifdef HAVE_WNR
wolfSSL 7:481bce714567 280 /* Whitewood netRandom default config file */
wolfSSL 7:481bce714567 281 #define wnrConfig "./wnr-example.conf"
wolfSSL 7:481bce714567 282 #endif
wolfSSL 7:481bce714567 283 #endif
wolfSSL 7:481bce714567 284
wolfSSL 7:481bce714567 285 typedef struct tcp_ready {
wolfSSL 7:481bce714567 286 word16 ready; /* predicate */
wolfSSL 7:481bce714567 287 word16 port;
wolfSSL 7:481bce714567 288 char* srfName; /* server ready file name */
wolfSSL 7:481bce714567 289 #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
wolfSSL 7:481bce714567 290 pthread_mutex_t mutex;
wolfSSL 7:481bce714567 291 pthread_cond_t cond;
wolfSSL 7:481bce714567 292 #endif
wolfSSL 7:481bce714567 293 } tcp_ready;
wolfSSL 7:481bce714567 294
wolfSSL 7:481bce714567 295
wolfSSL 7:481bce714567 296 static INLINE void InitTcpReady(tcp_ready* ready)
wolfSSL 7:481bce714567 297 {
wolfSSL 7:481bce714567 298 ready->ready = 0;
wolfSSL 7:481bce714567 299 ready->port = 0;
wolfSSL 7:481bce714567 300 ready->srfName = NULL;
wolfSSL 7:481bce714567 301 #ifdef SINGLE_THREADED
wolfSSL 7:481bce714567 302 #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
wolfSSL 7:481bce714567 303 pthread_mutex_init(&ready->mutex, 0);
wolfSSL 7:481bce714567 304 pthread_cond_init(&ready->cond, 0);
wolfSSL 7:481bce714567 305 #endif
wolfSSL 7:481bce714567 306 }
wolfSSL 7:481bce714567 307
wolfSSL 7:481bce714567 308
wolfSSL 7:481bce714567 309 static INLINE void FreeTcpReady(tcp_ready* ready)
wolfSSL 7:481bce714567 310 {
wolfSSL 7:481bce714567 311 #ifdef SINGLE_THREADED
wolfSSL 7:481bce714567 312 (void)ready;
wolfSSL 7:481bce714567 313 #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
wolfSSL 7:481bce714567 314 pthread_mutex_destroy(&ready->mutex);
wolfSSL 7:481bce714567 315 pthread_cond_destroy(&ready->cond);
wolfSSL 7:481bce714567 316 #else
wolfSSL 7:481bce714567 317 (void)ready;
wolfSSL 7:481bce714567 318 #endif
wolfSSL 7:481bce714567 319 }
wolfSSL 7:481bce714567 320
wolfSSL 7:481bce714567 321 typedef WOLFSSL_METHOD* (*method_provider)(void);
wolfSSL 7:481bce714567 322 typedef void (*ctx_callback)(WOLFSSL_CTX* ctx);
wolfSSL 7:481bce714567 323 typedef void (*ssl_callback)(WOLFSSL* ssl);
wolfSSL 7:481bce714567 324
wolfSSL 7:481bce714567 325 typedef struct callback_functions {
wolfSSL 7:481bce714567 326 method_provider method;
wolfSSL 7:481bce714567 327 ctx_callback ctx_ready;
wolfSSL 7:481bce714567 328 ssl_callback ssl_ready;
wolfSSL 7:481bce714567 329 ssl_callback on_result;
wolfSSL 7:481bce714567 330 } callback_functions;
wolfSSL 7:481bce714567 331
wolfSSL 7:481bce714567 332 typedef struct func_args {
wolfSSL 7:481bce714567 333 int argc;
wolfSSL 7:481bce714567 334 char** argv;
wolfSSL 7:481bce714567 335 int return_code;
wolfSSL 7:481bce714567 336 tcp_ready* signal;
wolfSSL 7:481bce714567 337 callback_functions *callbacks;
wolfSSL 7:481bce714567 338 } func_args;
wolfSSL 7:481bce714567 339
wolfSSL 7:481bce714567 340
wolfSSL 7:481bce714567 341
wolfSSL 7:481bce714567 342
wolfSSL 7:481bce714567 343 void wait_tcp_ready(func_args*);
wolfSSL 7:481bce714567 344
wolfSSL 7:481bce714567 345 typedef THREAD_RETURN WOLFSSL_THREAD THREAD_FUNC(void*);
wolfSSL 7:481bce714567 346
wolfSSL 7:481bce714567 347 void start_thread(THREAD_FUNC, func_args*, THREAD_TYPE*);
wolfSSL 7:481bce714567 348 void join_thread(THREAD_TYPE);
wolfSSL 7:481bce714567 349
wolfSSL 7:481bce714567 350 /* wolfSSL */
wolfSSL 7:481bce714567 351 #ifndef TEST_IPV6
wolfSSL 7:481bce714567 352 static const char* const wolfSSLIP = "127.0.0.1";
wolfSSL 7:481bce714567 353 #else
wolfSSL 7:481bce714567 354 static const char* const wolfSSLIP = "::1";
wolfSSL 7:481bce714567 355 #endif
wolfSSL 7:481bce714567 356 static const word16 wolfSSLPort = 11111;
wolfSSL 7:481bce714567 357
wolfSSL 7:481bce714567 358
wolfSSL 7:481bce714567 359 #if defined(__GNUC__)
wolfSSL 7:481bce714567 360 #define WC_NORETURN __attribute__((noreturn))
wolfSSL 7:481bce714567 361 #else
wolfSSL 7:481bce714567 362 #define WC_NORETURN
wolfSSL 7:481bce714567 363 #endif
wolfSSL 7:481bce714567 364
wolfSSL 7:481bce714567 365 static INLINE WC_NORETURN void err_sys(const char* msg)
wolfSSL 7:481bce714567 366 {
wolfSSL 7:481bce714567 367 printf("wolfSSL error: %s\n", msg);
wolfSSL 7:481bce714567 368
wolfSSL 7:481bce714567 369 #if !defined(__GNUC__)
wolfSSL 7:481bce714567 370 /* scan-build (which pretends to be gnuc) can get confused and think the
wolfSSL 7:481bce714567 371 * msg pointer can be null even when hardcoded and then it won't exit,
wolfSSL 7:481bce714567 372 * making null pointer checks above the err_sys() call useless.
wolfSSL 7:481bce714567 373 * We could just always exit() but some compilers will complain about no
wolfSSL 7:481bce714567 374 * possible return, with gcc we know the attribute to handle that with
wolfSSL 7:481bce714567 375 * WC_NORETURN. */
wolfSSL 7:481bce714567 376 if (msg)
wolfSSL 7:481bce714567 377 #endif
wolfSSL 7:481bce714567 378 {
wolfSSL 7:481bce714567 379 exit(EXIT_FAILURE);
wolfSSL 7:481bce714567 380 }
wolfSSL 7:481bce714567 381 }
wolfSSL 7:481bce714567 382
wolfSSL 7:481bce714567 383
wolfSSL 7:481bce714567 384 #define MY_EX_USAGE 2
wolfSSL 7:481bce714567 385
wolfSSL 7:481bce714567 386 extern int myoptind;
wolfSSL 7:481bce714567 387 extern char* myoptarg;
wolfSSL 7:481bce714567 388
wolfSSL 7:481bce714567 389 static INLINE int mygetopt(int argc, char** argv, const char* optstring)
wolfSSL 7:481bce714567 390 {
wolfSSL 7:481bce714567 391 static char* next = NULL;
wolfSSL 7:481bce714567 392
wolfSSL 7:481bce714567 393 char c;
wolfSSL 7:481bce714567 394 char* cp;
wolfSSL 7:481bce714567 395
wolfSSL 7:481bce714567 396 if (myoptind == 0)
wolfSSL 7:481bce714567 397 next = NULL; /* we're starting new/over */
wolfSSL 7:481bce714567 398
wolfSSL 7:481bce714567 399 if (next == NULL || *next == '\0') {
wolfSSL 7:481bce714567 400 if (myoptind == 0)
wolfSSL 7:481bce714567 401 myoptind++;
wolfSSL 7:481bce714567 402
wolfSSL 7:481bce714567 403 if (myoptind >= argc || argv[myoptind][0] != '-' ||
wolfSSL 7:481bce714567 404 argv[myoptind][1] == '\0') {
wolfSSL 7:481bce714567 405 myoptarg = NULL;
wolfSSL 7:481bce714567 406 if (myoptind < argc)
wolfSSL 7:481bce714567 407 myoptarg = argv[myoptind];
wolfSSL 7:481bce714567 408
wolfSSL 7:481bce714567 409 return -1;
wolfSSL 7:481bce714567 410 }
wolfSSL 7:481bce714567 411
wolfSSL 7:481bce714567 412 if (strcmp(argv[myoptind], "--") == 0) {
wolfSSL 7:481bce714567 413 myoptind++;
wolfSSL 7:481bce714567 414 myoptarg = NULL;
wolfSSL 7:481bce714567 415
wolfSSL 7:481bce714567 416 if (myoptind < argc)
wolfSSL 7:481bce714567 417 myoptarg = argv[myoptind];
wolfSSL 7:481bce714567 418
wolfSSL 7:481bce714567 419 return -1;
wolfSSL 7:481bce714567 420 }
wolfSSL 7:481bce714567 421
wolfSSL 7:481bce714567 422 next = argv[myoptind];
wolfSSL 7:481bce714567 423 next++; /* skip - */
wolfSSL 7:481bce714567 424 myoptind++;
wolfSSL 7:481bce714567 425 }
wolfSSL 7:481bce714567 426
wolfSSL 7:481bce714567 427 c = *next++;
wolfSSL 7:481bce714567 428 /* The C++ strchr can return a different value */
wolfSSL 7:481bce714567 429 cp = (char*)strchr(optstring, c);
wolfSSL 7:481bce714567 430
wolfSSL 7:481bce714567 431 if (cp == NULL || c == ':')
wolfSSL 7:481bce714567 432 return '?';
wolfSSL 7:481bce714567 433
wolfSSL 7:481bce714567 434 cp++;
wolfSSL 7:481bce714567 435
wolfSSL 7:481bce714567 436 if (*cp == ':') {
wolfSSL 7:481bce714567 437 if (*next != '\0') {
wolfSSL 7:481bce714567 438 myoptarg = next;
wolfSSL 7:481bce714567 439 next = NULL;
wolfSSL 7:481bce714567 440 }
wolfSSL 7:481bce714567 441 else if (myoptind < argc) {
wolfSSL 7:481bce714567 442 myoptarg = argv[myoptind];
wolfSSL 7:481bce714567 443 myoptind++;
wolfSSL 7:481bce714567 444 }
wolfSSL 7:481bce714567 445 else
wolfSSL 7:481bce714567 446 return '?';
wolfSSL 7:481bce714567 447 }
wolfSSL 7:481bce714567 448
wolfSSL 7:481bce714567 449 return c;
wolfSSL 7:481bce714567 450 }
wolfSSL 7:481bce714567 451
wolfSSL 7:481bce714567 452
wolfSSL 7:481bce714567 453 #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
wolfSSL 7:481bce714567 454
wolfSSL 7:481bce714567 455 static INLINE int PasswordCallBack(char* passwd, int sz, int rw, void* userdata)
wolfSSL 7:481bce714567 456 {
wolfSSL 7:481bce714567 457 (void)rw;
wolfSSL 7:481bce714567 458 (void)userdata;
wolfSSL 7:481bce714567 459 strncpy(passwd, "yassl123", sz);
wolfSSL 7:481bce714567 460 return 8;
wolfSSL 7:481bce714567 461 }
wolfSSL 7:481bce714567 462
wolfSSL 7:481bce714567 463 #endif
wolfSSL 7:481bce714567 464
wolfSSL 7:481bce714567 465
wolfSSL 7:481bce714567 466 #if defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)
wolfSSL 7:481bce714567 467
wolfSSL 7:481bce714567 468 static INLINE void ShowX509(WOLFSSL_X509* x509, const char* hdr)
wolfSSL 7:481bce714567 469 {
wolfSSL 7:481bce714567 470 char* altName;
wolfSSL 7:481bce714567 471 char* issuer;
wolfSSL 7:481bce714567 472 char* subject;
wolfSSL 7:481bce714567 473 byte serial[32];
wolfSSL 7:481bce714567 474 int ret;
wolfSSL 7:481bce714567 475 int sz = sizeof(serial);
wolfSSL 7:481bce714567 476
wolfSSL 7:481bce714567 477 if (x509 == NULL) {
wolfSSL 7:481bce714567 478 printf("%s No Cert\n", hdr);
wolfSSL 7:481bce714567 479 return;
wolfSSL 7:481bce714567 480 }
wolfSSL 7:481bce714567 481
wolfSSL 7:481bce714567 482 issuer = wolfSSL_X509_NAME_oneline(
wolfSSL 7:481bce714567 483 wolfSSL_X509_get_issuer_name(x509), 0, 0);
wolfSSL 7:481bce714567 484 subject = wolfSSL_X509_NAME_oneline(
wolfSSL 7:481bce714567 485 wolfSSL_X509_get_subject_name(x509), 0, 0);
wolfSSL 7:481bce714567 486
wolfSSL 7:481bce714567 487 printf("%s\n issuer : %s\n subject: %s\n", hdr, issuer, subject);
wolfSSL 7:481bce714567 488
wolfSSL 7:481bce714567 489 while ( (altName = wolfSSL_X509_get_next_altname(x509)) != NULL)
wolfSSL 7:481bce714567 490 printf(" altname = %s\n", altName);
wolfSSL 7:481bce714567 491
wolfSSL 7:481bce714567 492 ret = wolfSSL_X509_get_serial_number(x509, serial, &sz);
wolfSSL 7:481bce714567 493 if (ret == SSL_SUCCESS) {
wolfSSL 7:481bce714567 494 int i;
wolfSSL 7:481bce714567 495 int strLen;
wolfSSL 7:481bce714567 496 char serialMsg[80];
wolfSSL 7:481bce714567 497
wolfSSL 7:481bce714567 498 /* testsuite has multiple threads writing to stdout, get output
wolfSSL 7:481bce714567 499 message ready to write once */
wolfSSL 7:481bce714567 500 strLen = sprintf(serialMsg, " serial number");
wolfSSL 7:481bce714567 501 for (i = 0; i < sz; i++)
wolfSSL 7:481bce714567 502 sprintf(serialMsg + strLen + (i*3), ":%02x ", serial[i]);
wolfSSL 7:481bce714567 503 printf("%s\n", serialMsg);
wolfSSL 7:481bce714567 504 }
wolfSSL 7:481bce714567 505
wolfSSL 7:481bce714567 506 XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
wolfSSL 7:481bce714567 507 XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL);
wolfSSL 7:481bce714567 508 }
wolfSSL 7:481bce714567 509
wolfSSL 7:481bce714567 510 #endif /* KEEP_PEER_CERT || SESSION_CERTS */
wolfSSL 7:481bce714567 511
wolfSSL 7:481bce714567 512
wolfSSL 7:481bce714567 513 static INLINE void showPeer(WOLFSSL* ssl)
wolfSSL 7:481bce714567 514 {
wolfSSL 7:481bce714567 515
wolfSSL 7:481bce714567 516 WOLFSSL_CIPHER* cipher;
wolfSSL 7:481bce714567 517 #ifdef KEEP_PEER_CERT
wolfSSL 7:481bce714567 518 WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
wolfSSL 7:481bce714567 519 if (peer)
wolfSSL 7:481bce714567 520 ShowX509(peer, "peer's cert info:");
wolfSSL 7:481bce714567 521 else
wolfSSL 7:481bce714567 522 printf("peer has no cert!\n");
wolfSSL 7:481bce714567 523 wolfSSL_FreeX509(peer);
wolfSSL 7:481bce714567 524 #endif
wolfSSL 7:481bce714567 525 #if defined(SHOW_CERTS) && defined(OPENSSL_EXTRA) && defined(KEEP_OUR_CERT)
wolfSSL 7:481bce714567 526 ShowX509(wolfSSL_get_certificate(ssl), "our cert info:");
wolfSSL 7:481bce714567 527 printf("Peer verify result = %lu\n", wolfSSL_get_verify_result(ssl));
wolfSSL 7:481bce714567 528 #endif /* SHOW_CERTS */
wolfSSL 7:481bce714567 529 printf("SSL version is %s\n", wolfSSL_get_version(ssl));
wolfSSL 7:481bce714567 530
wolfSSL 7:481bce714567 531 cipher = wolfSSL_get_current_cipher(ssl);
wolfSSL 7:481bce714567 532 #ifdef HAVE_QSH
wolfSSL 7:481bce714567 533 printf("SSL cipher suite is %s%s\n", (wolfSSL_isQSH(ssl))? "QSH:": "",
wolfSSL 7:481bce714567 534 wolfSSL_CIPHER_get_name(cipher));
wolfSSL 7:481bce714567 535 #else
wolfSSL 7:481bce714567 536 printf("SSL cipher suite is %s\n", wolfSSL_CIPHER_get_name(cipher));
wolfSSL 7:481bce714567 537 #endif
wolfSSL 7:481bce714567 538
wolfSSL 7:481bce714567 539 #if defined(SESSION_CERTS) && defined(SHOW_CERTS)
wolfSSL 7:481bce714567 540 {
wolfSSL 7:481bce714567 541 WOLFSSL_X509_CHAIN* chain = wolfSSL_get_peer_chain(ssl);
wolfSSL 7:481bce714567 542 int count = wolfSSL_get_chain_count(chain);
wolfSSL 7:481bce714567 543 int i;
wolfSSL 7:481bce714567 544
wolfSSL 7:481bce714567 545 for (i = 0; i < count; i++) {
wolfSSL 7:481bce714567 546 int length;
wolfSSL 7:481bce714567 547 unsigned char buffer[3072];
wolfSSL 7:481bce714567 548 WOLFSSL_X509* chainX509;
wolfSSL 7:481bce714567 549
wolfSSL 7:481bce714567 550 wolfSSL_get_chain_cert_pem(chain,i,buffer, sizeof(buffer), &length);
wolfSSL 7:481bce714567 551 buffer[length] = 0;
wolfSSL 7:481bce714567 552 printf("cert %d has length %d data = \n%s\n", i, length, buffer);
wolfSSL 7:481bce714567 553
wolfSSL 7:481bce714567 554 chainX509 = wolfSSL_get_chain_X509(chain, i);
wolfSSL 7:481bce714567 555 if (chainX509)
wolfSSL 7:481bce714567 556 ShowX509(chainX509, "session cert info:");
wolfSSL 7:481bce714567 557 else
wolfSSL 7:481bce714567 558 printf("get_chain_X509 failed\n");
wolfSSL 7:481bce714567 559 wolfSSL_FreeX509(chainX509);
wolfSSL 7:481bce714567 560 }
wolfSSL 7:481bce714567 561 }
wolfSSL 7:481bce714567 562 #endif
wolfSSL 7:481bce714567 563 (void)ssl;
wolfSSL 7:481bce714567 564 }
wolfSSL 7:481bce714567 565
wolfSSL 7:481bce714567 566
wolfSSL 7:481bce714567 567 static INLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer,
wolfSSL 7:481bce714567 568 word16 port, int udp, int sctp)
wolfSSL 7:481bce714567 569 {
wolfSSL 7:481bce714567 570 int useLookup = 0;
wolfSSL 7:481bce714567 571 (void)useLookup;
wolfSSL 7:481bce714567 572 (void)udp;
wolfSSL 7:481bce714567 573 (void)sctp;
wolfSSL 7:481bce714567 574
wolfSSL 7:481bce714567 575 if (addr == NULL)
wolfSSL 7:481bce714567 576 err_sys("invalid argument to build_addr, addr is NULL");
wolfSSL 7:481bce714567 577
wolfSSL 7:481bce714567 578 memset(addr, 0, sizeof(SOCKADDR_IN_T));
wolfSSL 7:481bce714567 579
wolfSSL 7:481bce714567 580 #ifndef TEST_IPV6
wolfSSL 7:481bce714567 581 /* peer could be in human readable form */
wolfSSL 7:481bce714567 582 if ( (peer != INADDR_ANY) && isalpha((int)peer[0])) {
wolfSSL 7:481bce714567 583 #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 7:481bce714567 584 int err;
wolfSSL 7:481bce714567 585 struct hostent* entry = gethostbyname(peer, &err);
wolfSSL 7:481bce714567 586 #elif defined(WOLFSSL_TIRTOS)
wolfSSL 7:481bce714567 587 struct hostent* entry = DNSGetHostByName(peer);
wolfSSL 7:481bce714567 588 #elif defined(WOLFSSL_VXWORKS)
wolfSSL 7:481bce714567 589 struct hostent* entry = (struct hostent*)hostGetByName((char*)peer);
wolfSSL 7:481bce714567 590 #else
wolfSSL 7:481bce714567 591 struct hostent* entry = gethostbyname(peer);
wolfSSL 7:481bce714567 592 #endif
wolfSSL 7:481bce714567 593
wolfSSL 7:481bce714567 594 if (entry) {
wolfSSL 7:481bce714567 595 XMEMCPY(&addr->sin_addr.s_addr, entry->h_addr_list[0],
wolfSSL 7:481bce714567 596 entry->h_length);
wolfSSL 7:481bce714567 597 useLookup = 1;
wolfSSL 7:481bce714567 598 }
wolfSSL 7:481bce714567 599 else
wolfSSL 7:481bce714567 600 err_sys("no entry for host");
wolfSSL 7:481bce714567 601 }
wolfSSL 7:481bce714567 602 #endif
wolfSSL 7:481bce714567 603
wolfSSL 7:481bce714567 604
wolfSSL 7:481bce714567 605 #ifndef TEST_IPV6
wolfSSL 7:481bce714567 606 #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 7:481bce714567 607 addr->sin_family = PF_INET;
wolfSSL 7:481bce714567 608 #else
wolfSSL 7:481bce714567 609 addr->sin_family = AF_INET_V;
wolfSSL 7:481bce714567 610 #endif
wolfSSL 7:481bce714567 611 addr->sin_port = htons(port);
wolfSSL 7:481bce714567 612 if (peer == INADDR_ANY)
wolfSSL 7:481bce714567 613 addr->sin_addr.s_addr = INADDR_ANY;
wolfSSL 7:481bce714567 614 else {
wolfSSL 7:481bce714567 615 if (!useLookup)
wolfSSL 7:481bce714567 616 addr->sin_addr.s_addr = inet_addr(peer);
wolfSSL 7:481bce714567 617 }
wolfSSL 7:481bce714567 618 #else
wolfSSL 7:481bce714567 619 addr->sin6_family = AF_INET_V;
wolfSSL 7:481bce714567 620 addr->sin6_port = htons(port);
wolfSSL 7:481bce714567 621 if (peer == INADDR_ANY)
wolfSSL 7:481bce714567 622 addr->sin6_addr = in6addr_any;
wolfSSL 7:481bce714567 623 else {
wolfSSL 7:481bce714567 624 #ifdef HAVE_GETADDRINFO
wolfSSL 7:481bce714567 625 struct addrinfo hints;
wolfSSL 7:481bce714567 626 struct addrinfo* answer = NULL;
wolfSSL 7:481bce714567 627 int ret;
wolfSSL 7:481bce714567 628 char strPort[80];
wolfSSL 7:481bce714567 629
wolfSSL 7:481bce714567 630 memset(&hints, 0, sizeof(hints));
wolfSSL 7:481bce714567 631
wolfSSL 7:481bce714567 632 hints.ai_family = AF_INET_V;
wolfSSL 7:481bce714567 633 if (udp) {
wolfSSL 7:481bce714567 634 hints.ai_socktype = SOCK_DGRAM;
wolfSSL 7:481bce714567 635 hints.ai_protocol = IPPROTO_UDP;
wolfSSL 7:481bce714567 636 }
wolfSSL 7:481bce714567 637 #ifdef WOLFSSL_SCTP
wolfSSL 7:481bce714567 638 else if (sctp) {
wolfSSL 7:481bce714567 639 hints.ai_socktype = SOCK_STREAM;
wolfSSL 7:481bce714567 640 hints.ai_protocol = IPPROTO_SCTP;
wolfSSL 7:481bce714567 641 }
wolfSSL 7:481bce714567 642 #endif
wolfSSL 7:481bce714567 643 else {
wolfSSL 7:481bce714567 644 hints.ai_socktype = SOCK_STREAM;
wolfSSL 7:481bce714567 645 hints.ai_protocol = IPPROTO_TCP;
wolfSSL 7:481bce714567 646 }
wolfSSL 7:481bce714567 647
wolfSSL 7:481bce714567 648 SNPRINTF(strPort, sizeof(strPort), "%d", port);
wolfSSL 7:481bce714567 649 strPort[79] = '\0';
wolfSSL 7:481bce714567 650
wolfSSL 7:481bce714567 651 ret = getaddrinfo(peer, strPort, &hints, &answer);
wolfSSL 7:481bce714567 652 if (ret < 0 || answer == NULL)
wolfSSL 7:481bce714567 653 err_sys("getaddrinfo failed");
wolfSSL 7:481bce714567 654
wolfSSL 7:481bce714567 655 XMEMCPY(addr, answer->ai_addr, answer->ai_addrlen);
wolfSSL 7:481bce714567 656 freeaddrinfo(answer);
wolfSSL 7:481bce714567 657 #else
wolfSSL 7:481bce714567 658 printf("no ipv6 getaddrinfo, loopback only tests/examples\n");
wolfSSL 7:481bce714567 659 addr->sin6_addr = in6addr_loopback;
wolfSSL 7:481bce714567 660 #endif
wolfSSL 7:481bce714567 661 }
wolfSSL 7:481bce714567 662 #endif
wolfSSL 7:481bce714567 663 }
wolfSSL 7:481bce714567 664
wolfSSL 7:481bce714567 665
wolfSSL 7:481bce714567 666 static INLINE void tcp_socket(SOCKET_T* sockfd, int udp, int sctp)
wolfSSL 7:481bce714567 667 {
wolfSSL 7:481bce714567 668 (void)sctp;
wolfSSL 7:481bce714567 669
wolfSSL 7:481bce714567 670 if (udp)
wolfSSL 7:481bce714567 671 *sockfd = socket(AF_INET_V, SOCK_DGRAM, IPPROTO_UDP);
wolfSSL 7:481bce714567 672 #ifdef WOLFSSL_SCTP
wolfSSL 7:481bce714567 673 else if (sctp)
wolfSSL 7:481bce714567 674 *sockfd = socket(AF_INET_V, SOCK_STREAM, IPPROTO_SCTP);
wolfSSL 7:481bce714567 675 #endif
wolfSSL 7:481bce714567 676 else
wolfSSL 7:481bce714567 677 *sockfd = socket(AF_INET_V, SOCK_STREAM, IPPROTO_TCP);
wolfSSL 7:481bce714567 678
wolfSSL 7:481bce714567 679 if(WOLFSSL_SOCKET_IS_INVALID(*sockfd)) {
wolfSSL 7:481bce714567 680 err_sys("socket failed\n");
wolfSSL 7:481bce714567 681 }
wolfSSL 7:481bce714567 682
wolfSSL 7:481bce714567 683 #ifndef USE_WINDOWS_API
wolfSSL 7:481bce714567 684 #ifdef SO_NOSIGPIPE
wolfSSL 7:481bce714567 685 {
wolfSSL 7:481bce714567 686 int on = 1;
wolfSSL 7:481bce714567 687 socklen_t len = sizeof(on);
wolfSSL 7:481bce714567 688 int res = setsockopt(*sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, len);
wolfSSL 7:481bce714567 689 if (res < 0)
wolfSSL 7:481bce714567 690 err_sys("setsockopt SO_NOSIGPIPE failed\n");
wolfSSL 7:481bce714567 691 }
wolfSSL 7:481bce714567 692 #elif defined(WOLFSSL_MDK_ARM) || defined (WOLFSSL_TIRTOS) ||\
wolfSSL 7:481bce714567 693 defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 7:481bce714567 694 /* nothing to define */
wolfSSL 7:481bce714567 695 #else /* no S_NOSIGPIPE */
wolfSSL 7:481bce714567 696 signal(SIGPIPE, SIG_IGN);
wolfSSL 7:481bce714567 697 #endif /* S_NOSIGPIPE */
wolfSSL 7:481bce714567 698
wolfSSL 7:481bce714567 699 #if defined(TCP_NODELAY)
wolfSSL 7:481bce714567 700 if (!udp && !sctp)
wolfSSL 7:481bce714567 701 {
wolfSSL 7:481bce714567 702 int on = 1;
wolfSSL 7:481bce714567 703 socklen_t len = sizeof(on);
wolfSSL 7:481bce714567 704 int res = setsockopt(*sockfd, IPPROTO_TCP, TCP_NODELAY, &on, len);
wolfSSL 7:481bce714567 705 if (res < 0)
wolfSSL 7:481bce714567 706 err_sys("setsockopt TCP_NODELAY failed\n");
wolfSSL 7:481bce714567 707 }
wolfSSL 7:481bce714567 708 #endif
wolfSSL 7:481bce714567 709 #endif /* USE_WINDOWS_API */
wolfSSL 7:481bce714567 710 }
wolfSSL 7:481bce714567 711
wolfSSL 7:481bce714567 712 static INLINE void tcp_connect(SOCKET_T* sockfd, const char* ip, word16 port,
wolfSSL 7:481bce714567 713 int udp, int sctp, WOLFSSL* ssl)
wolfSSL 7:481bce714567 714 {
wolfSSL 7:481bce714567 715 SOCKADDR_IN_T addr;
wolfSSL 7:481bce714567 716 build_addr(&addr, ip, port, udp, sctp);
wolfSSL 7:481bce714567 717 if (udp) {
wolfSSL 7:481bce714567 718 wolfSSL_dtls_set_peer(ssl, &addr, sizeof(addr));
wolfSSL 7:481bce714567 719 }
wolfSSL 7:481bce714567 720 tcp_socket(sockfd, udp, sctp);
wolfSSL 7:481bce714567 721
wolfSSL 7:481bce714567 722 if (!udp) {
wolfSSL 7:481bce714567 723 if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
wolfSSL 7:481bce714567 724 err_sys("tcp connect failed");
wolfSSL 7:481bce714567 725 }
wolfSSL 7:481bce714567 726 }
wolfSSL 7:481bce714567 727
wolfSSL 7:481bce714567 728
wolfSSL 7:481bce714567 729 static INLINE void udp_connect(SOCKET_T* sockfd, void* addr, int addrSz)
wolfSSL 7:481bce714567 730 {
wolfSSL 7:481bce714567 731 if (connect(*sockfd, (const struct sockaddr*)addr, addrSz) != 0)
wolfSSL 7:481bce714567 732 err_sys("tcp connect failed");
wolfSSL 7:481bce714567 733 }
wolfSSL 7:481bce714567 734
wolfSSL 7:481bce714567 735
wolfSSL 7:481bce714567 736 enum {
wolfSSL 7:481bce714567 737 TEST_SELECT_FAIL,
wolfSSL 7:481bce714567 738 TEST_TIMEOUT,
wolfSSL 7:481bce714567 739 TEST_RECV_READY,
wolfSSL 7:481bce714567 740 TEST_ERROR_READY
wolfSSL 7:481bce714567 741 };
wolfSSL 7:481bce714567 742
wolfSSL 7:481bce714567 743
wolfSSL 7:481bce714567 744 #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_TCP_NET) && \
wolfSSL 7:481bce714567 745 !defined(WOLFSSL_TIRTOS)
wolfSSL 7:481bce714567 746 static INLINE int tcp_select(SOCKET_T socketfd, int to_sec)
wolfSSL 7:481bce714567 747 {
wolfSSL 7:481bce714567 748 fd_set recvfds, errfds;
wolfSSL 7:481bce714567 749 SOCKET_T nfds = socketfd + 1;
wolfSSL 7:481bce714567 750 struct timeval timeout = { (to_sec > 0) ? to_sec : 0, 0};
wolfSSL 7:481bce714567 751 int result;
wolfSSL 7:481bce714567 752
wolfSSL 7:481bce714567 753 FD_ZERO(&recvfds);
wolfSSL 7:481bce714567 754 FD_SET(socketfd, &recvfds);
wolfSSL 7:481bce714567 755 FD_ZERO(&errfds);
wolfSSL 7:481bce714567 756 FD_SET(socketfd, &errfds);
wolfSSL 7:481bce714567 757
wolfSSL 7:481bce714567 758 result = select(nfds, &recvfds, NULL, &errfds, &timeout);
wolfSSL 7:481bce714567 759
wolfSSL 7:481bce714567 760 if (result == 0)
wolfSSL 7:481bce714567 761 return TEST_TIMEOUT;
wolfSSL 7:481bce714567 762 else if (result > 0) {
wolfSSL 7:481bce714567 763 if (FD_ISSET(socketfd, &recvfds))
wolfSSL 7:481bce714567 764 return TEST_RECV_READY;
wolfSSL 7:481bce714567 765 else if(FD_ISSET(socketfd, &errfds))
wolfSSL 7:481bce714567 766 return TEST_ERROR_READY;
wolfSSL 7:481bce714567 767 }
wolfSSL 7:481bce714567 768
wolfSSL 7:481bce714567 769 return TEST_SELECT_FAIL;
wolfSSL 7:481bce714567 770 }
wolfSSL 7:481bce714567 771 #elif defined(WOLFSSL_TIRTOS)
wolfSSL 7:481bce714567 772 static INLINE int tcp_select(SOCKET_T socketfd, int to_sec)
wolfSSL 7:481bce714567 773 {
wolfSSL 7:481bce714567 774 return TEST_RECV_READY;
wolfSSL 7:481bce714567 775 }
wolfSSL 7:481bce714567 776 #endif /* !WOLFSSL_MDK_ARM */
wolfSSL 7:481bce714567 777
wolfSSL 7:481bce714567 778
wolfSSL 7:481bce714567 779 static INLINE void tcp_listen(SOCKET_T* sockfd, word16* port, int useAnyAddr,
wolfSSL 7:481bce714567 780 int udp, int sctp)
wolfSSL 7:481bce714567 781 {
wolfSSL 7:481bce714567 782 SOCKADDR_IN_T addr;
wolfSSL 7:481bce714567 783
wolfSSL 7:481bce714567 784 /* don't use INADDR_ANY by default, firewall may block, make user switch
wolfSSL 7:481bce714567 785 on */
wolfSSL 7:481bce714567 786 build_addr(&addr, (useAnyAddr ? INADDR_ANY : wolfSSLIP), *port, udp, sctp);
wolfSSL 7:481bce714567 787 tcp_socket(sockfd, udp, sctp);
wolfSSL 7:481bce714567 788
wolfSSL 7:481bce714567 789 #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_ARM)\
wolfSSL 7:481bce714567 790 && !defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 7:481bce714567 791 {
wolfSSL 7:481bce714567 792 int res, on = 1;
wolfSSL 7:481bce714567 793 socklen_t len = sizeof(on);
wolfSSL 7:481bce714567 794 res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
wolfSSL 7:481bce714567 795 if (res < 0)
wolfSSL 7:481bce714567 796 err_sys("setsockopt SO_REUSEADDR failed\n");
wolfSSL 7:481bce714567 797 }
wolfSSL 7:481bce714567 798 #endif
wolfSSL 7:481bce714567 799
wolfSSL 7:481bce714567 800 if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
wolfSSL 7:481bce714567 801 err_sys("tcp bind failed");
wolfSSL 7:481bce714567 802 if (!udp) {
wolfSSL 7:481bce714567 803 if (listen(*sockfd, 5) != 0)
wolfSSL 7:481bce714567 804 err_sys("tcp listen failed");
wolfSSL 7:481bce714567 805 }
wolfSSL 7:481bce714567 806 #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_TIRTOS)
wolfSSL 7:481bce714567 807 if (*port == 0) {
wolfSSL 7:481bce714567 808 socklen_t len = sizeof(addr);
wolfSSL 7:481bce714567 809 if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) {
wolfSSL 7:481bce714567 810 #ifndef TEST_IPV6
wolfSSL 7:481bce714567 811 *port = ntohs(addr.sin_port);
wolfSSL 7:481bce714567 812 #else
wolfSSL 7:481bce714567 813 *port = ntohs(addr.sin6_port);
wolfSSL 7:481bce714567 814 #endif
wolfSSL 7:481bce714567 815 }
wolfSSL 7:481bce714567 816 }
wolfSSL 7:481bce714567 817 #endif
wolfSSL 7:481bce714567 818 }
wolfSSL 7:481bce714567 819
wolfSSL 7:481bce714567 820
wolfSSL 7:481bce714567 821 #if 0
wolfSSL 7:481bce714567 822 static INLINE int udp_read_connect(SOCKET_T sockfd)
wolfSSL 7:481bce714567 823 {
wolfSSL 7:481bce714567 824 SOCKADDR_IN_T cliaddr;
wolfSSL 7:481bce714567 825 byte b[1500];
wolfSSL 7:481bce714567 826 int n;
wolfSSL 7:481bce714567 827 socklen_t len = sizeof(cliaddr);
wolfSSL 7:481bce714567 828
wolfSSL 7:481bce714567 829 n = (int)recvfrom(sockfd, (char*)b, sizeof(b), MSG_PEEK,
wolfSSL 7:481bce714567 830 (struct sockaddr*)&cliaddr, &len);
wolfSSL 7:481bce714567 831 if (n > 0) {
wolfSSL 7:481bce714567 832 if (connect(sockfd, (const struct sockaddr*)&cliaddr,
wolfSSL 7:481bce714567 833 sizeof(cliaddr)) != 0)
wolfSSL 7:481bce714567 834 err_sys("udp connect failed");
wolfSSL 7:481bce714567 835 }
wolfSSL 7:481bce714567 836 else
wolfSSL 7:481bce714567 837 err_sys("recvfrom failed");
wolfSSL 7:481bce714567 838
wolfSSL 7:481bce714567 839 return sockfd;
wolfSSL 7:481bce714567 840 }
wolfSSL 7:481bce714567 841 #endif
wolfSSL 7:481bce714567 842
wolfSSL 7:481bce714567 843 static INLINE void udp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
wolfSSL 7:481bce714567 844 int useAnyAddr, word16 port, func_args* args)
wolfSSL 7:481bce714567 845 {
wolfSSL 7:481bce714567 846 SOCKADDR_IN_T addr;
wolfSSL 7:481bce714567 847
wolfSSL 7:481bce714567 848 (void)args;
wolfSSL 7:481bce714567 849 build_addr(&addr, (useAnyAddr ? INADDR_ANY : wolfSSLIP), port, 1, 0);
wolfSSL 7:481bce714567 850 tcp_socket(sockfd, 1, 0);
wolfSSL 7:481bce714567 851
wolfSSL 7:481bce714567 852
wolfSSL 7:481bce714567 853 #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_ARM) \
wolfSSL 7:481bce714567 854 && !defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 7:481bce714567 855 {
wolfSSL 7:481bce714567 856 int res, on = 1;
wolfSSL 7:481bce714567 857 socklen_t len = sizeof(on);
wolfSSL 7:481bce714567 858 res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
wolfSSL 7:481bce714567 859 if (res < 0)
wolfSSL 7:481bce714567 860 err_sys("setsockopt SO_REUSEADDR failed\n");
wolfSSL 7:481bce714567 861 }
wolfSSL 7:481bce714567 862 #endif
wolfSSL 7:481bce714567 863
wolfSSL 7:481bce714567 864 if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
wolfSSL 7:481bce714567 865 err_sys("tcp bind failed");
wolfSSL 7:481bce714567 866
wolfSSL 7:481bce714567 867 #if (defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API)) && !defined(WOLFSSL_TIRTOS)
wolfSSL 7:481bce714567 868 if (port == 0) {
wolfSSL 7:481bce714567 869 socklen_t len = sizeof(addr);
wolfSSL 7:481bce714567 870 if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) {
wolfSSL 7:481bce714567 871 #ifndef TEST_IPV6
wolfSSL 7:481bce714567 872 port = ntohs(addr.sin_port);
wolfSSL 7:481bce714567 873 #else
wolfSSL 7:481bce714567 874 port = ntohs(addr.sin6_port);
wolfSSL 7:481bce714567 875 #endif
wolfSSL 7:481bce714567 876 }
wolfSSL 7:481bce714567 877 }
wolfSSL 7:481bce714567 878 #endif
wolfSSL 7:481bce714567 879
wolfSSL 7:481bce714567 880 #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__)
wolfSSL 7:481bce714567 881 /* signal ready to accept data */
wolfSSL 7:481bce714567 882 {
wolfSSL 7:481bce714567 883 tcp_ready* ready = args->signal;
wolfSSL 7:481bce714567 884 pthread_mutex_lock(&ready->mutex);
wolfSSL 7:481bce714567 885 ready->ready = 1;
wolfSSL 7:481bce714567 886 ready->port = port;
wolfSSL 7:481bce714567 887 pthread_cond_signal(&ready->cond);
wolfSSL 7:481bce714567 888 pthread_mutex_unlock(&ready->mutex);
wolfSSL 7:481bce714567 889 }
wolfSSL 7:481bce714567 890 #elif defined (WOLFSSL_TIRTOS)
wolfSSL 7:481bce714567 891 /* Need mutex? */
wolfSSL 7:481bce714567 892 tcp_ready* ready = args->signal;
wolfSSL 7:481bce714567 893 ready->ready = 1;
wolfSSL 7:481bce714567 894 ready->port = port;
wolfSSL 7:481bce714567 895 #endif
wolfSSL 7:481bce714567 896
wolfSSL 7:481bce714567 897 *clientfd = *sockfd;
wolfSSL 7:481bce714567 898 }
wolfSSL 7:481bce714567 899
wolfSSL 7:481bce714567 900 static INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
wolfSSL 7:481bce714567 901 func_args* args, word16 port, int useAnyAddr,
wolfSSL 7:481bce714567 902 int udp, int sctp, int ready_file, int do_listen)
wolfSSL 7:481bce714567 903 {
wolfSSL 7:481bce714567 904 SOCKADDR_IN_T client;
wolfSSL 7:481bce714567 905 socklen_t client_len = sizeof(client);
wolfSSL 7:481bce714567 906 tcp_ready* ready = NULL;
wolfSSL 7:481bce714567 907
wolfSSL 7:481bce714567 908 (void) ready; /* Account for case when "ready" is not used */
wolfSSL 7:481bce714567 909
wolfSSL 7:481bce714567 910 if (udp) {
wolfSSL 7:481bce714567 911 udp_accept(sockfd, clientfd, useAnyAddr, port, args);
wolfSSL 7:481bce714567 912 return;
wolfSSL 7:481bce714567 913 }
wolfSSL 7:481bce714567 914
wolfSSL 7:481bce714567 915 if(do_listen) {
wolfSSL 7:481bce714567 916 tcp_listen(sockfd, &port, useAnyAddr, udp, sctp);
wolfSSL 7:481bce714567 917
wolfSSL 7:481bce714567 918 #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__)
wolfSSL 7:481bce714567 919 /* signal ready to tcp_accept */
wolfSSL 7:481bce714567 920 if (args)
wolfSSL 7:481bce714567 921 ready = args->signal;
wolfSSL 7:481bce714567 922 if (ready) {
wolfSSL 7:481bce714567 923 pthread_mutex_lock(&ready->mutex);
wolfSSL 7:481bce714567 924 ready->ready = 1;
wolfSSL 7:481bce714567 925 ready->port = port;
wolfSSL 7:481bce714567 926 pthread_cond_signal(&ready->cond);
wolfSSL 7:481bce714567 927 pthread_mutex_unlock(&ready->mutex);
wolfSSL 7:481bce714567 928 }
wolfSSL 7:481bce714567 929 #elif defined (WOLFSSL_TIRTOS)
wolfSSL 7:481bce714567 930 /* Need mutex? */
wolfSSL 7:481bce714567 931 if (args)
wolfSSL 7:481bce714567 932 ready = args->signal;
wolfSSL 7:481bce714567 933 if (ready) {
wolfSSL 7:481bce714567 934 ready->ready = 1;
wolfSSL 7:481bce714567 935 ready->port = port;
wolfSSL 7:481bce714567 936 }
wolfSSL 7:481bce714567 937 #endif
wolfSSL 7:481bce714567 938
wolfSSL 7:481bce714567 939 if (ready_file) {
wolfSSL 7:481bce714567 940 #if !defined(NO_FILESYSTEM) || defined(FORCE_BUFFER_TEST)
wolfSSL 7:481bce714567 941 FILE* srf = NULL;
wolfSSL 7:481bce714567 942 if (args)
wolfSSL 7:481bce714567 943 ready = args->signal;
wolfSSL 7:481bce714567 944
wolfSSL 7:481bce714567 945 if (ready) {
wolfSSL 7:481bce714567 946 srf = fopen(ready->srfName, "w");
wolfSSL 7:481bce714567 947
wolfSSL 7:481bce714567 948 if (srf) {
wolfSSL 7:481bce714567 949 /* let's write port sever is listening on to ready file
wolfSSL 7:481bce714567 950 external monitor can then do ephemeral ports by passing
wolfSSL 7:481bce714567 951 -p 0 to server on supported platforms with -R ready_file
wolfSSL 7:481bce714567 952 client can then wait for existence of ready_file and see
wolfSSL 7:481bce714567 953 which port the server is listening on. */
wolfSSL 7:481bce714567 954 fprintf(srf, "%d\n", (int)port);
wolfSSL 7:481bce714567 955 fclose(srf);
wolfSSL 7:481bce714567 956 }
wolfSSL 7:481bce714567 957 }
wolfSSL 7:481bce714567 958 #endif
wolfSSL 7:481bce714567 959 }
wolfSSL 7:481bce714567 960 }
wolfSSL 7:481bce714567 961
wolfSSL 7:481bce714567 962 *clientfd = accept(*sockfd, (struct sockaddr*)&client,
wolfSSL 7:481bce714567 963 (ACCEPT_THIRD_T)&client_len);
wolfSSL 7:481bce714567 964 if(WOLFSSL_SOCKET_IS_INVALID(*clientfd)) {
wolfSSL 7:481bce714567 965 err_sys("tcp accept failed");
wolfSSL 7:481bce714567 966 }
wolfSSL 7:481bce714567 967 }
wolfSSL 7:481bce714567 968
wolfSSL 7:481bce714567 969
wolfSSL 7:481bce714567 970 static INLINE void tcp_set_nonblocking(SOCKET_T* sockfd)
wolfSSL 7:481bce714567 971 {
wolfSSL 7:481bce714567 972 #ifdef USE_WINDOWS_API
wolfSSL 7:481bce714567 973 unsigned long blocking = 1;
wolfSSL 7:481bce714567 974 int ret = ioctlsocket(*sockfd, FIONBIO, &blocking);
wolfSSL 7:481bce714567 975 if (ret == SOCKET_ERROR)
wolfSSL 7:481bce714567 976 err_sys("ioctlsocket failed");
wolfSSL 7:481bce714567 977 #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) \
wolfSSL 7:481bce714567 978 || defined (WOLFSSL_TIRTOS)|| defined(WOLFSSL_VXWORKS)
wolfSSL 7:481bce714567 979 /* non blocking not supported, for now */
wolfSSL 7:481bce714567 980 #else
wolfSSL 7:481bce714567 981 int flags = fcntl(*sockfd, F_GETFL, 0);
wolfSSL 7:481bce714567 982 if (flags < 0)
wolfSSL 7:481bce714567 983 err_sys("fcntl get failed");
wolfSSL 7:481bce714567 984 flags = fcntl(*sockfd, F_SETFL, flags | O_NONBLOCK);
wolfSSL 7:481bce714567 985 if (flags < 0)
wolfSSL 7:481bce714567 986 err_sys("fcntl set failed");
wolfSSL 7:481bce714567 987 #endif
wolfSSL 7:481bce714567 988 }
wolfSSL 7:481bce714567 989
wolfSSL 7:481bce714567 990
wolfSSL 7:481bce714567 991 #ifndef NO_PSK
wolfSSL 7:481bce714567 992
wolfSSL 7:481bce714567 993 static INLINE unsigned int my_psk_client_cb(WOLFSSL* ssl, const char* hint,
wolfSSL 7:481bce714567 994 char* identity, unsigned int id_max_len, unsigned char* key,
wolfSSL 7:481bce714567 995 unsigned int key_max_len)
wolfSSL 7:481bce714567 996 {
wolfSSL 7:481bce714567 997 (void)ssl;
wolfSSL 7:481bce714567 998 (void)hint;
wolfSSL 7:481bce714567 999 (void)key_max_len;
wolfSSL 7:481bce714567 1000
wolfSSL 7:481bce714567 1001 /* identity is OpenSSL testing default for openssl s_client, keep same */
wolfSSL 7:481bce714567 1002 strncpy(identity, "Client_identity", id_max_len);
wolfSSL 7:481bce714567 1003
wolfSSL 7:481bce714567 1004
wolfSSL 7:481bce714567 1005 /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using
wolfSSL 7:481bce714567 1006 unsigned binary */
wolfSSL 7:481bce714567 1007 key[0] = 26;
wolfSSL 7:481bce714567 1008 key[1] = 43;
wolfSSL 7:481bce714567 1009 key[2] = 60;
wolfSSL 7:481bce714567 1010 key[3] = 77;
wolfSSL 7:481bce714567 1011
wolfSSL 7:481bce714567 1012 return 4; /* length of key in octets or 0 for error */
wolfSSL 7:481bce714567 1013 }
wolfSSL 7:481bce714567 1014
wolfSSL 7:481bce714567 1015
wolfSSL 7:481bce714567 1016 static INLINE unsigned int my_psk_server_cb(WOLFSSL* ssl, const char* identity,
wolfSSL 7:481bce714567 1017 unsigned char* key, unsigned int key_max_len)
wolfSSL 7:481bce714567 1018 {
wolfSSL 7:481bce714567 1019 (void)ssl;
wolfSSL 7:481bce714567 1020 (void)key_max_len;
wolfSSL 7:481bce714567 1021
wolfSSL 7:481bce714567 1022 /* identity is OpenSSL testing default for openssl s_client, keep same */
wolfSSL 7:481bce714567 1023 if (strncmp(identity, "Client_identity", 15) != 0)
wolfSSL 7:481bce714567 1024 return 0;
wolfSSL 7:481bce714567 1025
wolfSSL 7:481bce714567 1026 /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using
wolfSSL 7:481bce714567 1027 unsigned binary */
wolfSSL 7:481bce714567 1028 key[0] = 26;
wolfSSL 7:481bce714567 1029 key[1] = 43;
wolfSSL 7:481bce714567 1030 key[2] = 60;
wolfSSL 7:481bce714567 1031 key[3] = 77;
wolfSSL 7:481bce714567 1032
wolfSSL 7:481bce714567 1033 return 4; /* length of key in octets or 0 for error */
wolfSSL 7:481bce714567 1034 }
wolfSSL 7:481bce714567 1035
wolfSSL 7:481bce714567 1036 #endif /* NO_PSK */
wolfSSL 7:481bce714567 1037
wolfSSL 7:481bce714567 1038
wolfSSL 7:481bce714567 1039 #if defined(WOLFSSL_USER_CURRTIME)
wolfSSL 7:481bce714567 1040 extern double current_time(int reset);
wolfSSL 7:481bce714567 1041
wolfSSL 7:481bce714567 1042 #elif defined(USE_WINDOWS_API)
wolfSSL 7:481bce714567 1043
wolfSSL 7:481bce714567 1044 #define WIN32_LEAN_AND_MEAN
wolfSSL 7:481bce714567 1045 #include <windows.h>
wolfSSL 7:481bce714567 1046
wolfSSL 7:481bce714567 1047 static INLINE double current_time(int reset)
wolfSSL 7:481bce714567 1048 {
wolfSSL 7:481bce714567 1049 static int init = 0;
wolfSSL 7:481bce714567 1050 static LARGE_INTEGER freq;
wolfSSL 7:481bce714567 1051
wolfSSL 7:481bce714567 1052 LARGE_INTEGER count;
wolfSSL 7:481bce714567 1053
wolfSSL 7:481bce714567 1054 if (!init) {
wolfSSL 7:481bce714567 1055 QueryPerformanceFrequency(&freq);
wolfSSL 7:481bce714567 1056 init = 1;
wolfSSL 7:481bce714567 1057 }
wolfSSL 7:481bce714567 1058
wolfSSL 7:481bce714567 1059 QueryPerformanceCounter(&count);
wolfSSL 7:481bce714567 1060
wolfSSL 7:481bce714567 1061 (void)reset;
wolfSSL 7:481bce714567 1062 return (double)count.QuadPart / freq.QuadPart;
wolfSSL 7:481bce714567 1063 }
wolfSSL 7:481bce714567 1064
wolfSSL 7:481bce714567 1065 #elif defined(WOLFSSL_TIRTOS)
wolfSSL 7:481bce714567 1066 extern double current_time();
wolfSSL 7:481bce714567 1067 #else
wolfSSL 7:481bce714567 1068
wolfSSL 7:481bce714567 1069 #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 7:481bce714567 1070 #include <sys/time.h>
wolfSSL 7:481bce714567 1071
wolfSSL 7:481bce714567 1072 static INLINE double current_time(int reset)
wolfSSL 7:481bce714567 1073 {
wolfSSL 7:481bce714567 1074 struct timeval tv;
wolfSSL 7:481bce714567 1075 gettimeofday(&tv, 0);
wolfSSL 7:481bce714567 1076 (void)reset;
wolfSSL 7:481bce714567 1077
wolfSSL 7:481bce714567 1078 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
wolfSSL 7:481bce714567 1079 }
wolfSSL 7:481bce714567 1080 #else
wolfSSL 7:481bce714567 1081 extern double current_time(int reset);
wolfSSL 7:481bce714567 1082 #endif
wolfSSL 7:481bce714567 1083 #endif /* USE_WINDOWS_API */
wolfSSL 7:481bce714567 1084
wolfSSL 7:481bce714567 1085
wolfSSL 7:481bce714567 1086 #if defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(FORCE_BUFFER_TEST)
wolfSSL 7:481bce714567 1087
wolfSSL 7:481bce714567 1088 enum {
wolfSSL 7:481bce714567 1089 WOLFSSL_CA = 1,
wolfSSL 7:481bce714567 1090 WOLFSSL_CERT = 2,
wolfSSL 7:481bce714567 1091 WOLFSSL_KEY = 3,
wolfSSL 7:481bce714567 1092 WOLFSSL_CERT_CHAIN = 4,
wolfSSL 7:481bce714567 1093 };
wolfSSL 7:481bce714567 1094
wolfSSL 7:481bce714567 1095 static INLINE void load_buffer(WOLFSSL_CTX* ctx, const char* fname, int type)
wolfSSL 7:481bce714567 1096 {
wolfSSL 7:481bce714567 1097 int format = SSL_FILETYPE_PEM;
wolfSSL 7:481bce714567 1098
wolfSSL 7:481bce714567 1099 /* test buffer load */
wolfSSL 7:481bce714567 1100 long sz = 0;
wolfSSL 7:481bce714567 1101 byte buff[10000];
wolfSSL 7:481bce714567 1102 FILE* file = fopen(fname, "rb");
wolfSSL 7:481bce714567 1103
wolfSSL 7:481bce714567 1104 if (!file)
wolfSSL 7:481bce714567 1105 err_sys("can't open file for buffer load "
wolfSSL 7:481bce714567 1106 "Please run from wolfSSL home directory if not");
wolfSSL 7:481bce714567 1107 fseek(file, 0, SEEK_END);
wolfSSL 7:481bce714567 1108 sz = ftell(file);
wolfSSL 7:481bce714567 1109 rewind(file);
wolfSSL 7:481bce714567 1110 fread(buff, sizeof(buff), 1, file);
wolfSSL 7:481bce714567 1111
wolfSSL 7:481bce714567 1112 /* determine format */
wolfSSL 7:481bce714567 1113 if (strstr(fname, ".der"))
wolfSSL 7:481bce714567 1114 format = SSL_FILETYPE_ASN1;
wolfSSL 7:481bce714567 1115
wolfSSL 7:481bce714567 1116 if (type == WOLFSSL_CA) {
wolfSSL 7:481bce714567 1117 if (wolfSSL_CTX_load_verify_buffer(ctx, buff, sz, format)
wolfSSL 7:481bce714567 1118 != SSL_SUCCESS)
wolfSSL 7:481bce714567 1119 err_sys("can't load buffer ca file");
wolfSSL 7:481bce714567 1120 }
wolfSSL 7:481bce714567 1121 else if (type == WOLFSSL_CERT) {
wolfSSL 7:481bce714567 1122 if (wolfSSL_CTX_use_certificate_buffer(ctx, buff, sz,
wolfSSL 7:481bce714567 1123 format) != SSL_SUCCESS)
wolfSSL 7:481bce714567 1124 err_sys("can't load buffer cert file");
wolfSSL 7:481bce714567 1125 }
wolfSSL 7:481bce714567 1126 else if (type == WOLFSSL_KEY) {
wolfSSL 7:481bce714567 1127 if (wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, sz,
wolfSSL 7:481bce714567 1128 format) != SSL_SUCCESS)
wolfSSL 7:481bce714567 1129 err_sys("can't load buffer key file");
wolfSSL 7:481bce714567 1130 }
wolfSSL 7:481bce714567 1131 else if (type == WOLFSSL_CERT_CHAIN) {
wolfSSL 7:481bce714567 1132 if (wolfSSL_CTX_use_certificate_chain_buffer_format(ctx, buff, sz,
wolfSSL 7:481bce714567 1133 format) != SSL_SUCCESS)
wolfSSL 7:481bce714567 1134 err_sys("can't load cert chain buffer");
wolfSSL 7:481bce714567 1135 }
wolfSSL 7:481bce714567 1136
wolfSSL 7:481bce714567 1137 fclose(file);
wolfSSL 7:481bce714567 1138 }
wolfSSL 7:481bce714567 1139
wolfSSL 7:481bce714567 1140 #endif /* NO_FILESYSTEM */
wolfSSL 7:481bce714567 1141
wolfSSL 7:481bce714567 1142 #ifdef VERIFY_CALLBACK
wolfSSL 7:481bce714567 1143
wolfSSL 7:481bce714567 1144 static INLINE int myVerify(int preverify, WOLFSSL_X509_STORE_CTX* store)
wolfSSL 7:481bce714567 1145 {
wolfSSL 7:481bce714567 1146 (void)preverify;
wolfSSL 7:481bce714567 1147 char buffer[WOLFSSL_MAX_ERROR_SZ];
wolfSSL 7:481bce714567 1148
wolfSSL 7:481bce714567 1149 #ifdef OPENSSL_EXTRA
wolfSSL 7:481bce714567 1150 WOLFSSL_X509* peer;
wolfSSL 7:481bce714567 1151 #endif
wolfSSL 7:481bce714567 1152
wolfSSL 7:481bce714567 1153 printf("In verification callback, error = %d, %s\n", store->error,
wolfSSL 7:481bce714567 1154 wolfSSL_ERR_error_string(store->error, buffer));
wolfSSL 7:481bce714567 1155 #ifdef OPENSSL_EXTRA
wolfSSL 7:481bce714567 1156 peer = store->current_cert;
wolfSSL 7:481bce714567 1157 if (peer) {
wolfSSL 7:481bce714567 1158 char* issuer = wolfSSL_X509_NAME_oneline(
wolfSSL 7:481bce714567 1159 wolfSSL_X509_get_issuer_name(peer), 0, 0);
wolfSSL 7:481bce714567 1160 char* subject = wolfSSL_X509_NAME_oneline(
wolfSSL 7:481bce714567 1161 wolfSSL_X509_get_subject_name(peer), 0, 0);
wolfSSL 7:481bce714567 1162 printf("\tPeer's cert info:\n issuer : %s\n subject: %s\n", issuer,
wolfSSL 7:481bce714567 1163 subject);
wolfSSL 7:481bce714567 1164 XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
wolfSSL 7:481bce714567 1165 XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL);
wolfSSL 7:481bce714567 1166 }
wolfSSL 7:481bce714567 1167 else
wolfSSL 7:481bce714567 1168 printf("\tPeer has no cert!\n");
wolfSSL 7:481bce714567 1169 #else
wolfSSL 7:481bce714567 1170 printf("\tPeer certs: %d\n", store->totalCerts);
wolfSSL 7:481bce714567 1171 #ifdef VERIFY_CALLBACK_SHOW_PEER_CERTS
wolfSSL 7:481bce714567 1172 { int i;
wolfSSL 7:481bce714567 1173 for (i=0; i<store->totalCerts; i++) {
wolfSSL 7:481bce714567 1174 WOLFSSL_BUFFER_INFO* cert = &store->certs[i];
wolfSSL 7:481bce714567 1175 printf("\t\tCert %d: Ptr %p, Len %u\n", i, cert->buffer, cert->length);
wolfSSL 7:481bce714567 1176 }
wolfSSL 7:481bce714567 1177 }
wolfSSL 7:481bce714567 1178 #endif
wolfSSL 7:481bce714567 1179 #endif
wolfSSL 7:481bce714567 1180
wolfSSL 7:481bce714567 1181 printf("\tSubject's domain name is %s\n", store->domain);
wolfSSL 7:481bce714567 1182
wolfSSL 7:481bce714567 1183 printf("\tAllowing to continue anyway (shouldn't do this, EVER!!!)\n");
wolfSSL 7:481bce714567 1184 return 1;
wolfSSL 7:481bce714567 1185 }
wolfSSL 7:481bce714567 1186
wolfSSL 7:481bce714567 1187 #endif /* VERIFY_CALLBACK */
wolfSSL 7:481bce714567 1188
wolfSSL 7:481bce714567 1189
wolfSSL 7:481bce714567 1190 static INLINE int myDateCb(int preverify, WOLFSSL_X509_STORE_CTX* store)
wolfSSL 7:481bce714567 1191 {
wolfSSL 7:481bce714567 1192 char buffer[WOLFSSL_MAX_ERROR_SZ];
wolfSSL 7:481bce714567 1193 (void)preverify;
wolfSSL 7:481bce714567 1194
wolfSSL 7:481bce714567 1195 printf("In verification callback, error = %d, %s\n", store->error,
wolfSSL 7:481bce714567 1196 wolfSSL_ERR_error_string(store->error, buffer));
wolfSSL 7:481bce714567 1197 printf("Subject's domain name is %s\n", store->domain);
wolfSSL 7:481bce714567 1198
wolfSSL 7:481bce714567 1199 if (store->error == ASN_BEFORE_DATE_E || store->error == ASN_AFTER_DATE_E) {
wolfSSL 7:481bce714567 1200 printf("Overriding cert date error as example for bad clock testing\n");
wolfSSL 7:481bce714567 1201 return 1;
wolfSSL 7:481bce714567 1202 }
wolfSSL 7:481bce714567 1203 printf("Cert error is not date error, not overriding\n");
wolfSSL 7:481bce714567 1204
wolfSSL 7:481bce714567 1205 return 0;
wolfSSL 7:481bce714567 1206 }
wolfSSL 7:481bce714567 1207
wolfSSL 7:481bce714567 1208
wolfSSL 7:481bce714567 1209 #ifdef HAVE_CRL
wolfSSL 7:481bce714567 1210
wolfSSL 7:481bce714567 1211 static INLINE void CRL_CallBack(const char* url)
wolfSSL 7:481bce714567 1212 {
wolfSSL 7:481bce714567 1213 printf("CRL callback url = %s\n", url);
wolfSSL 7:481bce714567 1214 }
wolfSSL 7:481bce714567 1215
wolfSSL 7:481bce714567 1216 #endif
wolfSSL 7:481bce714567 1217
wolfSSL 7:481bce714567 1218 #ifndef NO_DH
wolfSSL 7:481bce714567 1219 static INLINE void SetDH(WOLFSSL* ssl)
wolfSSL 7:481bce714567 1220 {
wolfSSL 7:481bce714567 1221 /* dh1024 p */
wolfSSL 7:481bce714567 1222 static unsigned char p[] =
wolfSSL 7:481bce714567 1223 {
wolfSSL 7:481bce714567 1224 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
wolfSSL 7:481bce714567 1225 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
wolfSSL 7:481bce714567 1226 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
wolfSSL 7:481bce714567 1227 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
wolfSSL 7:481bce714567 1228 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
wolfSSL 7:481bce714567 1229 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
wolfSSL 7:481bce714567 1230 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
wolfSSL 7:481bce714567 1231 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
wolfSSL 7:481bce714567 1232 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
wolfSSL 7:481bce714567 1233 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
wolfSSL 7:481bce714567 1234 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
wolfSSL 7:481bce714567 1235 };
wolfSSL 7:481bce714567 1236
wolfSSL 7:481bce714567 1237 /* dh1024 g */
wolfSSL 7:481bce714567 1238 static unsigned char g[] =
wolfSSL 7:481bce714567 1239 {
wolfSSL 7:481bce714567 1240 0x02,
wolfSSL 7:481bce714567 1241 };
wolfSSL 7:481bce714567 1242
wolfSSL 7:481bce714567 1243 wolfSSL_SetTmpDH(ssl, p, sizeof(p), g, sizeof(g));
wolfSSL 7:481bce714567 1244 }
wolfSSL 7:481bce714567 1245
wolfSSL 7:481bce714567 1246 static INLINE void SetDHCtx(WOLFSSL_CTX* ctx)
wolfSSL 7:481bce714567 1247 {
wolfSSL 7:481bce714567 1248 /* dh1024 p */
wolfSSL 7:481bce714567 1249 static unsigned char p[] =
wolfSSL 7:481bce714567 1250 {
wolfSSL 7:481bce714567 1251 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
wolfSSL 7:481bce714567 1252 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
wolfSSL 7:481bce714567 1253 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
wolfSSL 7:481bce714567 1254 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
wolfSSL 7:481bce714567 1255 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
wolfSSL 7:481bce714567 1256 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
wolfSSL 7:481bce714567 1257 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
wolfSSL 7:481bce714567 1258 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
wolfSSL 7:481bce714567 1259 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
wolfSSL 7:481bce714567 1260 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
wolfSSL 7:481bce714567 1261 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
wolfSSL 7:481bce714567 1262 };
wolfSSL 7:481bce714567 1263
wolfSSL 7:481bce714567 1264 /* dh1024 g */
wolfSSL 7:481bce714567 1265 static unsigned char g[] =
wolfSSL 7:481bce714567 1266 {
wolfSSL 7:481bce714567 1267 0x02,
wolfSSL 7:481bce714567 1268 };
wolfSSL 7:481bce714567 1269
wolfSSL 7:481bce714567 1270 wolfSSL_CTX_SetTmpDH(ctx, p, sizeof(p), g, sizeof(g));
wolfSSL 7:481bce714567 1271 }
wolfSSL 7:481bce714567 1272 #endif /* NO_DH */
wolfSSL 7:481bce714567 1273
wolfSSL 7:481bce714567 1274 #ifndef NO_CERTS
wolfSSL 7:481bce714567 1275
wolfSSL 7:481bce714567 1276 static INLINE void CaCb(unsigned char* der, int sz, int type)
wolfSSL 7:481bce714567 1277 {
wolfSSL 7:481bce714567 1278 (void)der;
wolfSSL 7:481bce714567 1279 printf("Got CA cache add callback, derSz = %d, type = %d\n", sz, type);
wolfSSL 7:481bce714567 1280 }
wolfSSL 7:481bce714567 1281
wolfSSL 7:481bce714567 1282 #endif /* !NO_CERTS */
wolfSSL 7:481bce714567 1283
wolfSSL 7:481bce714567 1284
wolfSSL 7:481bce714567 1285 /* Wolf Root Directory Helper */
wolfSSL 7:481bce714567 1286 /* KEIL-RL File System does not support relative directory */
wolfSSL 7:481bce714567 1287 #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOLFSSL_TIRTOS)
wolfSSL 7:481bce714567 1288 /* Maximum depth to search for WolfSSL root */
wolfSSL 7:481bce714567 1289 #define MAX_WOLF_ROOT_DEPTH 5
wolfSSL 7:481bce714567 1290
wolfSSL 7:481bce714567 1291 static INLINE int ChangeToWolfRoot(void)
wolfSSL 7:481bce714567 1292 {
wolfSSL 7:481bce714567 1293 #if !defined(NO_FILESYSTEM) || defined(FORCE_BUFFER_TEST)
wolfSSL 7:481bce714567 1294 int depth, res;
wolfSSL 7:481bce714567 1295 FILE* file;
wolfSSL 7:481bce714567 1296 for(depth = 0; depth <= MAX_WOLF_ROOT_DEPTH; depth++) {
wolfSSL 7:481bce714567 1297 file = fopen(ntruKey, "rb");
wolfSSL 7:481bce714567 1298 if (file != NULL) {
wolfSSL 7:481bce714567 1299 fclose(file);
wolfSSL 7:481bce714567 1300 return depth;
wolfSSL 7:481bce714567 1301 }
wolfSSL 7:481bce714567 1302 #ifdef USE_WINDOWS_API
wolfSSL 7:481bce714567 1303 res = SetCurrentDirectoryA("..\\");
wolfSSL 7:481bce714567 1304 #else
wolfSSL 7:481bce714567 1305 res = chdir("../");
wolfSSL 7:481bce714567 1306 #endif
wolfSSL 7:481bce714567 1307 if (res < 0) {
wolfSSL 7:481bce714567 1308 printf("chdir to ../ failed!\n");
wolfSSL 7:481bce714567 1309 break;
wolfSSL 7:481bce714567 1310 }
wolfSSL 7:481bce714567 1311 }
wolfSSL 7:481bce714567 1312
wolfSSL 7:481bce714567 1313 err_sys("wolf root not found");
wolfSSL 7:481bce714567 1314 return -1;
wolfSSL 7:481bce714567 1315 #else
wolfSSL 7:481bce714567 1316 return 0;
wolfSSL 7:481bce714567 1317 #endif
wolfSSL 7:481bce714567 1318 }
wolfSSL 7:481bce714567 1319 #endif /* !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOLFSSL_TIRTOS) */
wolfSSL 7:481bce714567 1320
wolfSSL 7:481bce714567 1321 #ifdef HAVE_STACK_SIZE
wolfSSL 7:481bce714567 1322
wolfSSL 7:481bce714567 1323 typedef THREAD_RETURN WOLFSSL_THREAD (*thread_func)(void* args);
wolfSSL 7:481bce714567 1324
wolfSSL 7:481bce714567 1325
wolfSSL 7:481bce714567 1326 static INLINE void StackSizeCheck(func_args* args, thread_func tf)
wolfSSL 7:481bce714567 1327 {
wolfSSL 7:481bce714567 1328 int ret, i, used;
wolfSSL 7:481bce714567 1329 unsigned char* myStack = NULL;
wolfSSL 7:481bce714567 1330 int stackSize = 1024*128;
wolfSSL 7:481bce714567 1331 pthread_attr_t myAttr;
wolfSSL 7:481bce714567 1332 pthread_t threadId;
wolfSSL 7:481bce714567 1333
wolfSSL 7:481bce714567 1334 #ifdef PTHREAD_STACK_MIN
wolfSSL 7:481bce714567 1335 if (stackSize < PTHREAD_STACK_MIN)
wolfSSL 7:481bce714567 1336 stackSize = PTHREAD_STACK_MIN;
wolfSSL 7:481bce714567 1337 #endif
wolfSSL 7:481bce714567 1338
wolfSSL 7:481bce714567 1339 ret = posix_memalign((void**)&myStack, sysconf(_SC_PAGESIZE), stackSize);
wolfSSL 7:481bce714567 1340 if (ret != 0 || myStack == NULL)
wolfSSL 7:481bce714567 1341 err_sys("posix_memalign failed\n");
wolfSSL 7:481bce714567 1342
wolfSSL 7:481bce714567 1343 XMEMSET(myStack, 0x01, stackSize);
wolfSSL 7:481bce714567 1344
wolfSSL 7:481bce714567 1345 ret = pthread_attr_init(&myAttr);
wolfSSL 7:481bce714567 1346 if (ret != 0)
wolfSSL 7:481bce714567 1347 err_sys("attr_init failed");
wolfSSL 7:481bce714567 1348
wolfSSL 7:481bce714567 1349 ret = pthread_attr_setstack(&myAttr, myStack, stackSize);
wolfSSL 7:481bce714567 1350 if (ret != 0)
wolfSSL 7:481bce714567 1351 err_sys("attr_setstackaddr failed");
wolfSSL 7:481bce714567 1352
wolfSSL 7:481bce714567 1353 ret = pthread_create(&threadId, &myAttr, tf, args);
wolfSSL 7:481bce714567 1354 if (ret != 0) {
wolfSSL 7:481bce714567 1355 perror("pthread_create failed");
wolfSSL 7:481bce714567 1356 exit(EXIT_FAILURE);
wolfSSL 7:481bce714567 1357 }
wolfSSL 7:481bce714567 1358
wolfSSL 7:481bce714567 1359 ret = pthread_join(threadId, NULL);
wolfSSL 7:481bce714567 1360 if (ret != 0)
wolfSSL 7:481bce714567 1361 err_sys("pthread_join failed");
wolfSSL 7:481bce714567 1362
wolfSSL 7:481bce714567 1363 for (i = 0; i < stackSize; i++) {
wolfSSL 7:481bce714567 1364 if (myStack[i] != 0x01) {
wolfSSL 7:481bce714567 1365 break;
wolfSSL 7:481bce714567 1366 }
wolfSSL 7:481bce714567 1367 }
wolfSSL 7:481bce714567 1368
wolfSSL 7:481bce714567 1369 used = stackSize - i;
wolfSSL 7:481bce714567 1370 printf("stack used = %d\n", used);
wolfSSL 7:481bce714567 1371 }
wolfSSL 7:481bce714567 1372
wolfSSL 7:481bce714567 1373
wolfSSL 7:481bce714567 1374 #endif /* HAVE_STACK_SIZE */
wolfSSL 7:481bce714567 1375
wolfSSL 7:481bce714567 1376
wolfSSL 7:481bce714567 1377 #ifdef STACK_TRAP
wolfSSL 7:481bce714567 1378
wolfSSL 7:481bce714567 1379 /* good settings
wolfSSL 7:481bce714567 1380 --enable-debug --disable-shared C_EXTRA_FLAGS="-DUSER_TIME -DTFM_TIMING_RESISTANT -DPOSITIVE_EXP_ONLY -DSTACK_TRAP"
wolfSSL 7:481bce714567 1381
wolfSSL 7:481bce714567 1382 */
wolfSSL 7:481bce714567 1383
wolfSSL 7:481bce714567 1384 #ifdef HAVE_STACK_SIZE
wolfSSL 7:481bce714567 1385 /* client only for now, setrlimit will fail if pthread_create() called */
wolfSSL 7:481bce714567 1386 /* STACK_SIZE does pthread_create() on client */
wolfSSL 7:481bce714567 1387 #error "can't use STACK_TRAP with STACK_SIZE, setrlimit will fail"
wolfSSL 7:481bce714567 1388 #endif /* HAVE_STACK_SIZE */
wolfSSL 7:481bce714567 1389
wolfSSL 7:481bce714567 1390 static INLINE void StackTrap(void)
wolfSSL 7:481bce714567 1391 {
wolfSSL 7:481bce714567 1392 struct rlimit rl;
wolfSSL 7:481bce714567 1393 if (getrlimit(RLIMIT_STACK, &rl) != 0)
wolfSSL 7:481bce714567 1394 err_sys("getrlimit failed");
wolfSSL 7:481bce714567 1395 printf("rlim_cur = %llu\n", rl.rlim_cur);
wolfSSL 7:481bce714567 1396 rl.rlim_cur = 1024*21; /* adjust trap size here */
wolfSSL 7:481bce714567 1397 if (setrlimit(RLIMIT_STACK, &rl) != 0) {
wolfSSL 7:481bce714567 1398 perror("setrlimit");
wolfSSL 7:481bce714567 1399 err_sys("setrlimit failed");
wolfSSL 7:481bce714567 1400 }
wolfSSL 7:481bce714567 1401 }
wolfSSL 7:481bce714567 1402
wolfSSL 7:481bce714567 1403 #else /* STACK_TRAP */
wolfSSL 7:481bce714567 1404
wolfSSL 7:481bce714567 1405 static INLINE void StackTrap(void)
wolfSSL 7:481bce714567 1406 {
wolfSSL 7:481bce714567 1407 }
wolfSSL 7:481bce714567 1408
wolfSSL 7:481bce714567 1409 #endif /* STACK_TRAP */
wolfSSL 7:481bce714567 1410
wolfSSL 7:481bce714567 1411
wolfSSL 7:481bce714567 1412 #ifdef ATOMIC_USER
wolfSSL 7:481bce714567 1413
wolfSSL 7:481bce714567 1414 /* Atomic Encrypt Context example */
wolfSSL 7:481bce714567 1415 typedef struct AtomicEncCtx {
wolfSSL 7:481bce714567 1416 int keySetup; /* have we done key setup yet */
wolfSSL 7:481bce714567 1417 Aes aes; /* for aes example */
wolfSSL 7:481bce714567 1418 } AtomicEncCtx;
wolfSSL 7:481bce714567 1419
wolfSSL 7:481bce714567 1420
wolfSSL 7:481bce714567 1421 /* Atomic Decrypt Context example */
wolfSSL 7:481bce714567 1422 typedef struct AtomicDecCtx {
wolfSSL 7:481bce714567 1423 int keySetup; /* have we done key setup yet */
wolfSSL 7:481bce714567 1424 Aes aes; /* for aes example */
wolfSSL 7:481bce714567 1425 } AtomicDecCtx;
wolfSSL 7:481bce714567 1426
wolfSSL 7:481bce714567 1427
wolfSSL 7:481bce714567 1428 static INLINE int myMacEncryptCb(WOLFSSL* ssl, unsigned char* macOut,
wolfSSL 7:481bce714567 1429 const unsigned char* macIn, unsigned int macInSz, int macContent,
wolfSSL 7:481bce714567 1430 int macVerify, unsigned char* encOut, const unsigned char* encIn,
wolfSSL 7:481bce714567 1431 unsigned int encSz, void* ctx)
wolfSSL 7:481bce714567 1432 {
wolfSSL 7:481bce714567 1433 int ret;
wolfSSL 7:481bce714567 1434 Hmac hmac;
wolfSSL 7:481bce714567 1435 byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
wolfSSL 7:481bce714567 1436 AtomicEncCtx* encCtx = (AtomicEncCtx*)ctx;
wolfSSL 7:481bce714567 1437 const char* tlsStr = "TLS";
wolfSSL 7:481bce714567 1438
wolfSSL 7:481bce714567 1439 /* example supports (d)tls aes */
wolfSSL 7:481bce714567 1440 if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) {
wolfSSL 7:481bce714567 1441 printf("myMacEncryptCb not using AES\n");
wolfSSL 7:481bce714567 1442 return -1;
wolfSSL 7:481bce714567 1443 }
wolfSSL 7:481bce714567 1444
wolfSSL 7:481bce714567 1445 if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) {
wolfSSL 7:481bce714567 1446 printf("myMacEncryptCb not using (D)TLS\n");
wolfSSL 7:481bce714567 1447 return -1;
wolfSSL 7:481bce714567 1448 }
wolfSSL 7:481bce714567 1449
wolfSSL 7:481bce714567 1450 /* hmac, not needed if aead mode */
wolfSSL 7:481bce714567 1451 wolfSSL_SetTlsHmacInner(ssl, myInner, macInSz, macContent, macVerify);
wolfSSL 7:481bce714567 1452
wolfSSL 7:481bce714567 1453 ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
wolfSSL 7:481bce714567 1454 wolfSSL_GetMacSecret(ssl, macVerify), wolfSSL_GetHmacSize(ssl));
wolfSSL 7:481bce714567 1455 if (ret != 0)
wolfSSL 7:481bce714567 1456 return ret;
wolfSSL 7:481bce714567 1457 ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
wolfSSL 7:481bce714567 1458 if (ret != 0)
wolfSSL 7:481bce714567 1459 return ret;
wolfSSL 7:481bce714567 1460 ret = wc_HmacUpdate(&hmac, macIn, macInSz);
wolfSSL 7:481bce714567 1461 if (ret != 0)
wolfSSL 7:481bce714567 1462 return ret;
wolfSSL 7:481bce714567 1463 ret = wc_HmacFinal(&hmac, macOut);
wolfSSL 7:481bce714567 1464 if (ret != 0)
wolfSSL 7:481bce714567 1465 return ret;
wolfSSL 7:481bce714567 1466
wolfSSL 7:481bce714567 1467
wolfSSL 7:481bce714567 1468 /* encrypt setup on first time */
wolfSSL 7:481bce714567 1469 if (encCtx->keySetup == 0) {
wolfSSL 7:481bce714567 1470 int keyLen = wolfSSL_GetKeySize(ssl);
wolfSSL 7:481bce714567 1471 const byte* key;
wolfSSL 7:481bce714567 1472 const byte* iv;
wolfSSL 7:481bce714567 1473
wolfSSL 7:481bce714567 1474 if (wolfSSL_GetSide(ssl) == WOLFSSL_CLIENT_END) {
wolfSSL 7:481bce714567 1475 key = wolfSSL_GetClientWriteKey(ssl);
wolfSSL 7:481bce714567 1476 iv = wolfSSL_GetClientWriteIV(ssl);
wolfSSL 7:481bce714567 1477 }
wolfSSL 7:481bce714567 1478 else {
wolfSSL 7:481bce714567 1479 key = wolfSSL_GetServerWriteKey(ssl);
wolfSSL 7:481bce714567 1480 iv = wolfSSL_GetServerWriteIV(ssl);
wolfSSL 7:481bce714567 1481 }
wolfSSL 7:481bce714567 1482
wolfSSL 7:481bce714567 1483 ret = wc_AesSetKey(&encCtx->aes, key, keyLen, iv, AES_ENCRYPTION);
wolfSSL 7:481bce714567 1484 if (ret != 0) {
wolfSSL 7:481bce714567 1485 printf("AesSetKey failed in myMacEncryptCb\n");
wolfSSL 7:481bce714567 1486 return ret;
wolfSSL 7:481bce714567 1487 }
wolfSSL 7:481bce714567 1488 encCtx->keySetup = 1;
wolfSSL 7:481bce714567 1489 }
wolfSSL 7:481bce714567 1490
wolfSSL 7:481bce714567 1491 /* encrypt */
wolfSSL 7:481bce714567 1492 return wc_AesCbcEncrypt(&encCtx->aes, encOut, encIn, encSz);
wolfSSL 7:481bce714567 1493 }
wolfSSL 7:481bce714567 1494
wolfSSL 7:481bce714567 1495
wolfSSL 7:481bce714567 1496 static INLINE int myDecryptVerifyCb(WOLFSSL* ssl,
wolfSSL 7:481bce714567 1497 unsigned char* decOut, const unsigned char* decIn,
wolfSSL 7:481bce714567 1498 unsigned int decSz, int macContent, int macVerify,
wolfSSL 7:481bce714567 1499 unsigned int* padSz, void* ctx)
wolfSSL 7:481bce714567 1500 {
wolfSSL 7:481bce714567 1501 AtomicDecCtx* decCtx = (AtomicDecCtx*)ctx;
wolfSSL 7:481bce714567 1502 int ret = 0;
wolfSSL 7:481bce714567 1503 int macInSz = 0;
wolfSSL 7:481bce714567 1504 int ivExtra = 0;
wolfSSL 7:481bce714567 1505 int digestSz = wolfSSL_GetHmacSize(ssl);
wolfSSL 7:481bce714567 1506 unsigned int pad = 0;
wolfSSL 7:481bce714567 1507 unsigned int padByte = 0;
wolfSSL 7:481bce714567 1508 Hmac hmac;
wolfSSL 7:481bce714567 1509 byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
wolfSSL 7:481bce714567 1510 byte verify[MAX_DIGEST_SIZE];
wolfSSL 7:481bce714567 1511 const char* tlsStr = "TLS";
wolfSSL 7:481bce714567 1512
wolfSSL 7:481bce714567 1513 /* example supports (d)tls aes */
wolfSSL 7:481bce714567 1514 if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) {
wolfSSL 7:481bce714567 1515 printf("myMacEncryptCb not using AES\n");
wolfSSL 7:481bce714567 1516 return -1;
wolfSSL 7:481bce714567 1517 }
wolfSSL 7:481bce714567 1518
wolfSSL 7:481bce714567 1519 if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) {
wolfSSL 7:481bce714567 1520 printf("myMacEncryptCb not using (D)TLS\n");
wolfSSL 7:481bce714567 1521 return -1;
wolfSSL 7:481bce714567 1522 }
wolfSSL 7:481bce714567 1523
wolfSSL 7:481bce714567 1524 /*decrypt */
wolfSSL 7:481bce714567 1525 if (decCtx->keySetup == 0) {
wolfSSL 7:481bce714567 1526 int keyLen = wolfSSL_GetKeySize(ssl);
wolfSSL 7:481bce714567 1527 const byte* key;
wolfSSL 7:481bce714567 1528 const byte* iv;
wolfSSL 7:481bce714567 1529
wolfSSL 7:481bce714567 1530 /* decrypt is from other side (peer) */
wolfSSL 7:481bce714567 1531 if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
wolfSSL 7:481bce714567 1532 key = wolfSSL_GetClientWriteKey(ssl);
wolfSSL 7:481bce714567 1533 iv = wolfSSL_GetClientWriteIV(ssl);
wolfSSL 7:481bce714567 1534 }
wolfSSL 7:481bce714567 1535 else {
wolfSSL 7:481bce714567 1536 key = wolfSSL_GetServerWriteKey(ssl);
wolfSSL 7:481bce714567 1537 iv = wolfSSL_GetServerWriteIV(ssl);
wolfSSL 7:481bce714567 1538 }
wolfSSL 7:481bce714567 1539
wolfSSL 7:481bce714567 1540 ret = wc_AesSetKey(&decCtx->aes, key, keyLen, iv, AES_DECRYPTION);
wolfSSL 7:481bce714567 1541 if (ret != 0) {
wolfSSL 7:481bce714567 1542 printf("AesSetKey failed in myDecryptVerifyCb\n");
wolfSSL 7:481bce714567 1543 return ret;
wolfSSL 7:481bce714567 1544 }
wolfSSL 7:481bce714567 1545 decCtx->keySetup = 1;
wolfSSL 7:481bce714567 1546 }
wolfSSL 7:481bce714567 1547
wolfSSL 7:481bce714567 1548 /* decrypt */
wolfSSL 7:481bce714567 1549 ret = wc_AesCbcDecrypt(&decCtx->aes, decOut, decIn, decSz);
wolfSSL 7:481bce714567 1550 if (ret != 0)
wolfSSL 7:481bce714567 1551 return ret;
wolfSSL 7:481bce714567 1552
wolfSSL 7:481bce714567 1553 if (wolfSSL_GetCipherType(ssl) == WOLFSSL_AEAD_TYPE) {
wolfSSL 7:481bce714567 1554 *padSz = wolfSSL_GetAeadMacSize(ssl);
wolfSSL 7:481bce714567 1555 return 0; /* hmac, not needed if aead mode */
wolfSSL 7:481bce714567 1556 }
wolfSSL 7:481bce714567 1557
wolfSSL 7:481bce714567 1558 if (wolfSSL_GetCipherType(ssl) == WOLFSSL_BLOCK_TYPE) {
wolfSSL 7:481bce714567 1559 pad = *(decOut + decSz - 1);
wolfSSL 7:481bce714567 1560 padByte = 1;
wolfSSL 7:481bce714567 1561 if (wolfSSL_IsTLSv1_1(ssl))
wolfSSL 7:481bce714567 1562 ivExtra = wolfSSL_GetCipherBlockSize(ssl);
wolfSSL 7:481bce714567 1563 }
wolfSSL 7:481bce714567 1564
wolfSSL 7:481bce714567 1565 *padSz = wolfSSL_GetHmacSize(ssl) + pad + padByte;
wolfSSL 7:481bce714567 1566 macInSz = decSz - ivExtra - digestSz - pad - padByte;
wolfSSL 7:481bce714567 1567
wolfSSL 7:481bce714567 1568 wolfSSL_SetTlsHmacInner(ssl, myInner, macInSz, macContent, macVerify);
wolfSSL 7:481bce714567 1569
wolfSSL 7:481bce714567 1570 ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
wolfSSL 7:481bce714567 1571 wolfSSL_GetMacSecret(ssl, macVerify), digestSz);
wolfSSL 7:481bce714567 1572 if (ret != 0)
wolfSSL 7:481bce714567 1573 return ret;
wolfSSL 7:481bce714567 1574 ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
wolfSSL 7:481bce714567 1575 if (ret != 0)
wolfSSL 7:481bce714567 1576 return ret;
wolfSSL 7:481bce714567 1577 ret = wc_HmacUpdate(&hmac, decOut + ivExtra, macInSz);
wolfSSL 7:481bce714567 1578 if (ret != 0)
wolfSSL 7:481bce714567 1579 return ret;
wolfSSL 7:481bce714567 1580 ret = wc_HmacFinal(&hmac, verify);
wolfSSL 7:481bce714567 1581 if (ret != 0)
wolfSSL 7:481bce714567 1582 return ret;
wolfSSL 7:481bce714567 1583
wolfSSL 7:481bce714567 1584 if (XMEMCMP(verify, decOut + decSz - digestSz - pad - padByte,
wolfSSL 7:481bce714567 1585 digestSz) != 0) {
wolfSSL 7:481bce714567 1586 printf("myDecryptVerify verify failed\n");
wolfSSL 7:481bce714567 1587 return -1;
wolfSSL 7:481bce714567 1588 }
wolfSSL 7:481bce714567 1589
wolfSSL 7:481bce714567 1590 return ret;
wolfSSL 7:481bce714567 1591 }
wolfSSL 7:481bce714567 1592
wolfSSL 7:481bce714567 1593
wolfSSL 7:481bce714567 1594 static INLINE void SetupAtomicUser(WOLFSSL_CTX* ctx, WOLFSSL* ssl)
wolfSSL 7:481bce714567 1595 {
wolfSSL 7:481bce714567 1596 AtomicEncCtx* encCtx;
wolfSSL 7:481bce714567 1597 AtomicDecCtx* decCtx;
wolfSSL 7:481bce714567 1598
wolfSSL 7:481bce714567 1599 encCtx = (AtomicEncCtx*)malloc(sizeof(AtomicEncCtx));
wolfSSL 7:481bce714567 1600 if (encCtx == NULL)
wolfSSL 7:481bce714567 1601 err_sys("AtomicEncCtx malloc failed");
wolfSSL 7:481bce714567 1602 memset(encCtx, 0, sizeof(AtomicEncCtx));
wolfSSL 7:481bce714567 1603
wolfSSL 7:481bce714567 1604 decCtx = (AtomicDecCtx*)malloc(sizeof(AtomicDecCtx));
wolfSSL 7:481bce714567 1605 if (decCtx == NULL) {
wolfSSL 7:481bce714567 1606 free(encCtx);
wolfSSL 7:481bce714567 1607 err_sys("AtomicDecCtx malloc failed");
wolfSSL 7:481bce714567 1608 }
wolfSSL 7:481bce714567 1609 memset(decCtx, 0, sizeof(AtomicDecCtx));
wolfSSL 7:481bce714567 1610
wolfSSL 7:481bce714567 1611 wolfSSL_CTX_SetMacEncryptCb(ctx, myMacEncryptCb);
wolfSSL 7:481bce714567 1612 wolfSSL_SetMacEncryptCtx(ssl, encCtx);
wolfSSL 7:481bce714567 1613
wolfSSL 7:481bce714567 1614 wolfSSL_CTX_SetDecryptVerifyCb(ctx, myDecryptVerifyCb);
wolfSSL 7:481bce714567 1615 wolfSSL_SetDecryptVerifyCtx(ssl, decCtx);
wolfSSL 7:481bce714567 1616 }
wolfSSL 7:481bce714567 1617
wolfSSL 7:481bce714567 1618
wolfSSL 7:481bce714567 1619 static INLINE void FreeAtomicUser(WOLFSSL* ssl)
wolfSSL 7:481bce714567 1620 {
wolfSSL 7:481bce714567 1621 AtomicEncCtx* encCtx = (AtomicEncCtx*)wolfSSL_GetMacEncryptCtx(ssl);
wolfSSL 7:481bce714567 1622 AtomicDecCtx* decCtx = (AtomicDecCtx*)wolfSSL_GetDecryptVerifyCtx(ssl);
wolfSSL 7:481bce714567 1623
wolfSSL 7:481bce714567 1624 free(decCtx);
wolfSSL 7:481bce714567 1625 free(encCtx);
wolfSSL 7:481bce714567 1626 }
wolfSSL 7:481bce714567 1627
wolfSSL 7:481bce714567 1628 #endif /* ATOMIC_USER */
wolfSSL 7:481bce714567 1629
wolfSSL 7:481bce714567 1630 #ifdef WOLFSSL_STATIC_MEMORY
wolfSSL 7:481bce714567 1631 static INLINE int wolfSSL_PrintStats(WOLFSSL_MEM_STATS* stats)
wolfSSL 7:481bce714567 1632 {
wolfSSL 7:481bce714567 1633 word16 i;
wolfSSL 7:481bce714567 1634
wolfSSL 7:481bce714567 1635 if (stats == NULL) {
wolfSSL 7:481bce714567 1636 return 0;
wolfSSL 7:481bce714567 1637 }
wolfSSL 7:481bce714567 1638
wolfSSL 7:481bce714567 1639 /* print to stderr so is on the same pipe as WOLFSSL_DEBUG */
wolfSSL 7:481bce714567 1640 fprintf(stderr, "Total mallocs = %d\n", stats->totalAlloc);
wolfSSL 7:481bce714567 1641 fprintf(stderr, "Total frees = %d\n", stats->totalFr);
wolfSSL 7:481bce714567 1642 fprintf(stderr, "Current mallocs = %d\n", stats->curAlloc);
wolfSSL 7:481bce714567 1643 fprintf(stderr, "Available IO = %d\n", stats->avaIO);
wolfSSL 7:481bce714567 1644 fprintf(stderr, "Max con. handshakes = %d\n", stats->maxHa);
wolfSSL 7:481bce714567 1645 fprintf(stderr, "Max con. IO = %d\n", stats->maxIO);
wolfSSL 7:481bce714567 1646 fprintf(stderr, "State of memory blocks: size : available \n");
wolfSSL 7:481bce714567 1647 for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
wolfSSL 7:481bce714567 1648 fprintf(stderr, " : %d\t : %d\n", stats->blockSz[i],
wolfSSL 7:481bce714567 1649 stats->avaBlock[i]);
wolfSSL 7:481bce714567 1650 }
wolfSSL 7:481bce714567 1651
wolfSSL 7:481bce714567 1652 return 1;
wolfSSL 7:481bce714567 1653 }
wolfSSL 7:481bce714567 1654 #endif /* WOLFSSL_STATIC_MEMORY */
wolfSSL 7:481bce714567 1655
wolfSSL 7:481bce714567 1656 #ifdef HAVE_PK_CALLBACKS
wolfSSL 7:481bce714567 1657
wolfSSL 7:481bce714567 1658 #ifdef HAVE_ECC
wolfSSL 7:481bce714567 1659
wolfSSL 7:481bce714567 1660 static INLINE int myEccSign(WOLFSSL* ssl, const byte* in, word32 inSz,
wolfSSL 7:481bce714567 1661 byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx)
wolfSSL 7:481bce714567 1662 {
wolfSSL 7:481bce714567 1663 WC_RNG rng;
wolfSSL 7:481bce714567 1664 int ret;
wolfSSL 7:481bce714567 1665 word32 idx = 0;
wolfSSL 7:481bce714567 1666 ecc_key myKey;
wolfSSL 7:481bce714567 1667
wolfSSL 7:481bce714567 1668 (void)ssl;
wolfSSL 7:481bce714567 1669 (void)ctx;
wolfSSL 7:481bce714567 1670
wolfSSL 7:481bce714567 1671 ret = wc_InitRng(&rng);
wolfSSL 7:481bce714567 1672 if (ret != 0)
wolfSSL 7:481bce714567 1673 return ret;
wolfSSL 7:481bce714567 1674
wolfSSL 7:481bce714567 1675 wc_ecc_init(&myKey);
wolfSSL 7:481bce714567 1676
wolfSSL 7:481bce714567 1677 ret = wc_EccPrivateKeyDecode(key, &idx, &myKey, keySz);
wolfSSL 7:481bce714567 1678 if (ret == 0)
wolfSSL 7:481bce714567 1679 ret = wc_ecc_sign_hash(in, inSz, out, outSz, &rng, &myKey);
wolfSSL 7:481bce714567 1680 wc_ecc_free(&myKey);
wolfSSL 7:481bce714567 1681 wc_FreeRng(&rng);
wolfSSL 7:481bce714567 1682
wolfSSL 7:481bce714567 1683 return ret;
wolfSSL 7:481bce714567 1684 }
wolfSSL 7:481bce714567 1685
wolfSSL 7:481bce714567 1686
wolfSSL 7:481bce714567 1687 static INLINE int myEccVerify(WOLFSSL* ssl, const byte* sig, word32 sigSz,
wolfSSL 7:481bce714567 1688 const byte* hash, word32 hashSz, const byte* key, word32 keySz,
wolfSSL 7:481bce714567 1689 int* result, void* ctx)
wolfSSL 7:481bce714567 1690 {
wolfSSL 7:481bce714567 1691 int ret;
wolfSSL 7:481bce714567 1692 ecc_key myKey;
wolfSSL 7:481bce714567 1693
wolfSSL 7:481bce714567 1694 (void)ssl;
wolfSSL 7:481bce714567 1695 (void)ctx;
wolfSSL 7:481bce714567 1696
wolfSSL 7:481bce714567 1697 wc_ecc_init(&myKey);
wolfSSL 7:481bce714567 1698
wolfSSL 7:481bce714567 1699 ret = wc_ecc_import_x963(key, keySz, &myKey);
wolfSSL 7:481bce714567 1700 if (ret == 0)
wolfSSL 7:481bce714567 1701 ret = wc_ecc_verify_hash(sig, sigSz, hash, hashSz, result, &myKey);
wolfSSL 7:481bce714567 1702 wc_ecc_free(&myKey);
wolfSSL 7:481bce714567 1703
wolfSSL 7:481bce714567 1704 return ret;
wolfSSL 7:481bce714567 1705 }
wolfSSL 7:481bce714567 1706
wolfSSL 7:481bce714567 1707 static INLINE int myEccSharedSecret(WOLFSSL* ssl, ecc_key* otherKey,
wolfSSL 7:481bce714567 1708 unsigned char* pubKeyDer, unsigned int* pubKeySz,
wolfSSL 7:481bce714567 1709 unsigned char* out, unsigned int* outlen,
wolfSSL 7:481bce714567 1710 int side, void* ctx)
wolfSSL 7:481bce714567 1711 {
wolfSSL 7:481bce714567 1712 int ret;
wolfSSL 7:481bce714567 1713 ecc_key* privKey = NULL;
wolfSSL 7:481bce714567 1714 ecc_key* pubKey = NULL;
wolfSSL 7:481bce714567 1715 ecc_key tmpKey;
wolfSSL 7:481bce714567 1716
wolfSSL 7:481bce714567 1717 (void)ssl;
wolfSSL 7:481bce714567 1718 (void)ctx;
wolfSSL 7:481bce714567 1719
wolfSSL 7:481bce714567 1720 ret = wc_ecc_init(&tmpKey);
wolfSSL 7:481bce714567 1721 if (ret != 0) {
wolfSSL 7:481bce714567 1722 return ret;
wolfSSL 7:481bce714567 1723 }
wolfSSL 7:481bce714567 1724
wolfSSL 7:481bce714567 1725 /* for client: create and export public key */
wolfSSL 7:481bce714567 1726 if (side == WOLFSSL_CLIENT_END) {
wolfSSL 7:481bce714567 1727 WC_RNG rng;
wolfSSL 7:481bce714567 1728
wolfSSL 7:481bce714567 1729 privKey = &tmpKey;
wolfSSL 7:481bce714567 1730 pubKey = otherKey;
wolfSSL 7:481bce714567 1731
wolfSSL 7:481bce714567 1732 ret = wc_InitRng(&rng);
wolfSSL 7:481bce714567 1733 if (ret == 0) {
wolfSSL 7:481bce714567 1734 ret = wc_ecc_make_key_ex(&rng, 0, privKey, otherKey->dp->id);
wolfSSL 7:481bce714567 1735 if (ret == 0)
wolfSSL 7:481bce714567 1736 ret = wc_ecc_export_x963(privKey, pubKeyDer, pubKeySz);
wolfSSL 7:481bce714567 1737 wc_FreeRng(&rng);
wolfSSL 7:481bce714567 1738 }
wolfSSL 7:481bce714567 1739 }
wolfSSL 7:481bce714567 1740
wolfSSL 7:481bce714567 1741 /* for server: import public key */
wolfSSL 7:481bce714567 1742 else if (side == WOLFSSL_SERVER_END) {
wolfSSL 7:481bce714567 1743 privKey = otherKey;
wolfSSL 7:481bce714567 1744 pubKey = &tmpKey;
wolfSSL 7:481bce714567 1745
wolfSSL 7:481bce714567 1746 ret = wc_ecc_import_x963_ex(pubKeyDer, *pubKeySz, pubKey,
wolfSSL 7:481bce714567 1747 otherKey->dp->id);
wolfSSL 7:481bce714567 1748 }
wolfSSL 7:481bce714567 1749 else {
wolfSSL 7:481bce714567 1750 ret = BAD_FUNC_ARG;
wolfSSL 7:481bce714567 1751 }
wolfSSL 7:481bce714567 1752
wolfSSL 7:481bce714567 1753 /* generate shared secret and return it */
wolfSSL 7:481bce714567 1754 if (ret == 0) {
wolfSSL 7:481bce714567 1755 ret = wc_ecc_shared_secret(privKey, pubKey, out, outlen);
wolfSSL 7:481bce714567 1756 }
wolfSSL 7:481bce714567 1757
wolfSSL 7:481bce714567 1758 wc_ecc_free(&tmpKey);
wolfSSL 7:481bce714567 1759
wolfSSL 7:481bce714567 1760 return ret;
wolfSSL 7:481bce714567 1761 }
wolfSSL 7:481bce714567 1762
wolfSSL 7:481bce714567 1763 #endif /* HAVE_ECC */
wolfSSL 7:481bce714567 1764
wolfSSL 7:481bce714567 1765 #ifndef NO_RSA
wolfSSL 7:481bce714567 1766
wolfSSL 7:481bce714567 1767 static INLINE int myRsaSign(WOLFSSL* ssl, const byte* in, word32 inSz,
wolfSSL 7:481bce714567 1768 byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx)
wolfSSL 7:481bce714567 1769 {
wolfSSL 7:481bce714567 1770 WC_RNG rng;
wolfSSL 7:481bce714567 1771 int ret;
wolfSSL 7:481bce714567 1772 word32 idx = 0;
wolfSSL 7:481bce714567 1773 RsaKey myKey;
wolfSSL 7:481bce714567 1774
wolfSSL 7:481bce714567 1775 (void)ssl;
wolfSSL 7:481bce714567 1776 (void)ctx;
wolfSSL 7:481bce714567 1777
wolfSSL 7:481bce714567 1778 ret = wc_InitRng(&rng);
wolfSSL 7:481bce714567 1779 if (ret != 0)
wolfSSL 7:481bce714567 1780 return ret;
wolfSSL 7:481bce714567 1781
wolfSSL 7:481bce714567 1782 wc_InitRsaKey(&myKey, NULL);
wolfSSL 7:481bce714567 1783
wolfSSL 7:481bce714567 1784 ret = wc_RsaPrivateKeyDecode(key, &idx, &myKey, keySz);
wolfSSL 7:481bce714567 1785 if (ret == 0)
wolfSSL 7:481bce714567 1786 ret = wc_RsaSSL_Sign(in, inSz, out, *outSz, &myKey, &rng);
wolfSSL 7:481bce714567 1787 if (ret > 0) { /* save and convert to 0 success */
wolfSSL 7:481bce714567 1788 *outSz = ret;
wolfSSL 7:481bce714567 1789 ret = 0;
wolfSSL 7:481bce714567 1790 }
wolfSSL 7:481bce714567 1791 wc_FreeRsaKey(&myKey);
wolfSSL 7:481bce714567 1792 wc_FreeRng(&rng);
wolfSSL 7:481bce714567 1793
wolfSSL 7:481bce714567 1794 return ret;
wolfSSL 7:481bce714567 1795 }
wolfSSL 7:481bce714567 1796
wolfSSL 7:481bce714567 1797
wolfSSL 7:481bce714567 1798 static INLINE int myRsaVerify(WOLFSSL* ssl, byte* sig, word32 sigSz,
wolfSSL 7:481bce714567 1799 byte** out,
wolfSSL 7:481bce714567 1800 const byte* key, word32 keySz,
wolfSSL 7:481bce714567 1801 void* ctx)
wolfSSL 7:481bce714567 1802 {
wolfSSL 7:481bce714567 1803 int ret;
wolfSSL 7:481bce714567 1804 word32 idx = 0;
wolfSSL 7:481bce714567 1805 RsaKey myKey;
wolfSSL 7:481bce714567 1806
wolfSSL 7:481bce714567 1807 (void)ssl;
wolfSSL 7:481bce714567 1808 (void)ctx;
wolfSSL 7:481bce714567 1809
wolfSSL 7:481bce714567 1810 wc_InitRsaKey(&myKey, NULL);
wolfSSL 7:481bce714567 1811
wolfSSL 7:481bce714567 1812 ret = wc_RsaPublicKeyDecode(key, &idx, &myKey, keySz);
wolfSSL 7:481bce714567 1813 if (ret == 0)
wolfSSL 7:481bce714567 1814 ret = wc_RsaSSL_VerifyInline(sig, sigSz, out, &myKey);
wolfSSL 7:481bce714567 1815 wc_FreeRsaKey(&myKey);
wolfSSL 7:481bce714567 1816
wolfSSL 7:481bce714567 1817 return ret;
wolfSSL 7:481bce714567 1818 }
wolfSSL 7:481bce714567 1819
wolfSSL 7:481bce714567 1820
wolfSSL 7:481bce714567 1821 static INLINE int myRsaEnc(WOLFSSL* ssl, const byte* in, word32 inSz,
wolfSSL 7:481bce714567 1822 byte* out, word32* outSz, const byte* key,
wolfSSL 7:481bce714567 1823 word32 keySz, void* ctx)
wolfSSL 7:481bce714567 1824 {
wolfSSL 7:481bce714567 1825 int ret;
wolfSSL 7:481bce714567 1826 word32 idx = 0;
wolfSSL 7:481bce714567 1827 RsaKey myKey;
wolfSSL 7:481bce714567 1828 WC_RNG rng;
wolfSSL 7:481bce714567 1829
wolfSSL 7:481bce714567 1830 (void)ssl;
wolfSSL 7:481bce714567 1831 (void)ctx;
wolfSSL 7:481bce714567 1832
wolfSSL 7:481bce714567 1833 ret = wc_InitRng(&rng);
wolfSSL 7:481bce714567 1834 if (ret != 0)
wolfSSL 7:481bce714567 1835 return ret;
wolfSSL 7:481bce714567 1836
wolfSSL 7:481bce714567 1837 wc_InitRsaKey(&myKey, NULL);
wolfSSL 7:481bce714567 1838
wolfSSL 7:481bce714567 1839 ret = wc_RsaPublicKeyDecode(key, &idx, &myKey, keySz);
wolfSSL 7:481bce714567 1840 if (ret == 0) {
wolfSSL 7:481bce714567 1841 ret = wc_RsaPublicEncrypt(in, inSz, out, *outSz, &myKey, &rng);
wolfSSL 7:481bce714567 1842 if (ret > 0) {
wolfSSL 7:481bce714567 1843 *outSz = ret;
wolfSSL 7:481bce714567 1844 ret = 0; /* reset to success */
wolfSSL 7:481bce714567 1845 }
wolfSSL 7:481bce714567 1846 }
wolfSSL 7:481bce714567 1847 wc_FreeRsaKey(&myKey);
wolfSSL 7:481bce714567 1848 wc_FreeRng(&rng);
wolfSSL 7:481bce714567 1849
wolfSSL 7:481bce714567 1850 return ret;
wolfSSL 7:481bce714567 1851 }
wolfSSL 7:481bce714567 1852
wolfSSL 7:481bce714567 1853 static INLINE int myRsaDec(WOLFSSL* ssl, byte* in, word32 inSz,
wolfSSL 7:481bce714567 1854 byte** out,
wolfSSL 7:481bce714567 1855 const byte* key, word32 keySz, void* ctx)
wolfSSL 7:481bce714567 1856 {
wolfSSL 7:481bce714567 1857 int ret;
wolfSSL 7:481bce714567 1858 word32 idx = 0;
wolfSSL 7:481bce714567 1859 RsaKey myKey;
wolfSSL 7:481bce714567 1860
wolfSSL 7:481bce714567 1861 (void)ssl;
wolfSSL 7:481bce714567 1862 (void)ctx;
wolfSSL 7:481bce714567 1863
wolfSSL 7:481bce714567 1864 wc_InitRsaKey(&myKey, NULL);
wolfSSL 7:481bce714567 1865
wolfSSL 7:481bce714567 1866 ret = wc_RsaPrivateKeyDecode(key, &idx, &myKey, keySz);
wolfSSL 7:481bce714567 1867 if (ret == 0) {
wolfSSL 7:481bce714567 1868 #ifdef WC_RSA_BLINDING
wolfSSL 7:481bce714567 1869 ret = wc_RsaSetRNG(&myKey, wolfSSL_GetRNG(ssl));
wolfSSL 7:481bce714567 1870 if (ret != 0) {
wolfSSL 7:481bce714567 1871 wc_FreeRsaKey(&myKey);
wolfSSL 7:481bce714567 1872 return ret;
wolfSSL 7:481bce714567 1873 }
wolfSSL 7:481bce714567 1874 #endif
wolfSSL 7:481bce714567 1875 ret = wc_RsaPrivateDecryptInline(in, inSz, out, &myKey);
wolfSSL 7:481bce714567 1876 }
wolfSSL 7:481bce714567 1877 wc_FreeRsaKey(&myKey);
wolfSSL 7:481bce714567 1878
wolfSSL 7:481bce714567 1879 return ret;
wolfSSL 7:481bce714567 1880 }
wolfSSL 7:481bce714567 1881
wolfSSL 7:481bce714567 1882 #endif /* NO_RSA */
wolfSSL 7:481bce714567 1883
wolfSSL 7:481bce714567 1884 static INLINE void SetupPkCallbacks(WOLFSSL_CTX* ctx, WOLFSSL* ssl)
wolfSSL 7:481bce714567 1885 {
wolfSSL 7:481bce714567 1886 (void)ctx;
wolfSSL 7:481bce714567 1887 (void)ssl;
wolfSSL 7:481bce714567 1888
wolfSSL 7:481bce714567 1889 #ifdef HAVE_ECC
wolfSSL 7:481bce714567 1890 wolfSSL_CTX_SetEccSignCb(ctx, myEccSign);
wolfSSL 7:481bce714567 1891 wolfSSL_CTX_SetEccVerifyCb(ctx, myEccVerify);
wolfSSL 7:481bce714567 1892 wolfSSL_CTX_SetEccSharedSecretCb(ctx, myEccSharedSecret);
wolfSSL 7:481bce714567 1893 #endif /* HAVE_ECC */
wolfSSL 7:481bce714567 1894 #ifndef NO_RSA
wolfSSL 7:481bce714567 1895 wolfSSL_CTX_SetRsaSignCb(ctx, myRsaSign);
wolfSSL 7:481bce714567 1896 wolfSSL_CTX_SetRsaVerifyCb(ctx, myRsaVerify);
wolfSSL 7:481bce714567 1897 wolfSSL_CTX_SetRsaEncCb(ctx, myRsaEnc);
wolfSSL 7:481bce714567 1898 wolfSSL_CTX_SetRsaDecCb(ctx, myRsaDec);
wolfSSL 7:481bce714567 1899 #endif /* NO_RSA */
wolfSSL 7:481bce714567 1900 }
wolfSSL 7:481bce714567 1901
wolfSSL 7:481bce714567 1902 #endif /* HAVE_PK_CALLBACKS */
wolfSSL 7:481bce714567 1903
wolfSSL 7:481bce714567 1904
wolfSSL 7:481bce714567 1905
wolfSSL 7:481bce714567 1906
wolfSSL 7:481bce714567 1907
wolfSSL 7:481bce714567 1908 #if defined(__hpux__) || defined(__MINGW32__) || defined (WOLFSSL_TIRTOS) \
wolfSSL 7:481bce714567 1909 || defined(_MSC_VER)
wolfSSL 7:481bce714567 1910
wolfSSL 7:481bce714567 1911 /* HP/UX doesn't have strsep, needed by test/suites.c */
wolfSSL 7:481bce714567 1912 static INLINE char* strsep(char **stringp, const char *delim)
wolfSSL 7:481bce714567 1913 {
wolfSSL 7:481bce714567 1914 char* start;
wolfSSL 7:481bce714567 1915 char* end;
wolfSSL 7:481bce714567 1916
wolfSSL 7:481bce714567 1917 start = *stringp;
wolfSSL 7:481bce714567 1918 if (start == NULL)
wolfSSL 7:481bce714567 1919 return NULL;
wolfSSL 7:481bce714567 1920
wolfSSL 7:481bce714567 1921 if ((end = strpbrk(start, delim))) {
wolfSSL 7:481bce714567 1922 *end++ = '\0';
wolfSSL 7:481bce714567 1923 *stringp = end;
wolfSSL 7:481bce714567 1924 } else {
wolfSSL 7:481bce714567 1925 *stringp = NULL;
wolfSSL 7:481bce714567 1926 }
wolfSSL 7:481bce714567 1927
wolfSSL 7:481bce714567 1928 return start;
wolfSSL 7:481bce714567 1929 }
wolfSSL 7:481bce714567 1930
wolfSSL 7:481bce714567 1931 #endif /* __hpux__ and others */
wolfSSL 7:481bce714567 1932
wolfSSL 7:481bce714567 1933 /* Create unique filename, len is length of tempfn name, assuming
wolfSSL 7:481bce714567 1934 len does not include null terminating character,
wolfSSL 7:481bce714567 1935 num is number of characters in tempfn name to randomize */
wolfSSL 7:481bce714567 1936 static INLINE const char* mymktemp(char *tempfn, int len, int num)
wolfSSL 7:481bce714567 1937 {
wolfSSL 7:481bce714567 1938 int x, size;
wolfSSL 7:481bce714567 1939 static const char alphanum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
wolfSSL 7:481bce714567 1940 "abcdefghijklmnopqrstuvwxyz";
wolfSSL 7:481bce714567 1941 WC_RNG rng;
wolfSSL 7:481bce714567 1942 byte out;
wolfSSL 7:481bce714567 1943
wolfSSL 7:481bce714567 1944 if (tempfn == NULL || len < 1 || num < 1 || len <= num) {
wolfSSL 7:481bce714567 1945 printf("Bad input\n");
wolfSSL 7:481bce714567 1946 return NULL;
wolfSSL 7:481bce714567 1947 }
wolfSSL 7:481bce714567 1948
wolfSSL 7:481bce714567 1949 size = len - 1;
wolfSSL 7:481bce714567 1950
wolfSSL 7:481bce714567 1951 if (wc_InitRng(&rng) != 0) {
wolfSSL 7:481bce714567 1952 printf("InitRng failed\n");
wolfSSL 7:481bce714567 1953 return NULL;
wolfSSL 7:481bce714567 1954 }
wolfSSL 7:481bce714567 1955
wolfSSL 7:481bce714567 1956 for (x = size; x > size - num; x--) {
wolfSSL 7:481bce714567 1957 if (wc_RNG_GenerateBlock(&rng,(byte*)&out, sizeof(out)) != 0) {
wolfSSL 7:481bce714567 1958 printf("RNG_GenerateBlock failed\n");
wolfSSL 7:481bce714567 1959 return NULL;
wolfSSL 7:481bce714567 1960 }
wolfSSL 7:481bce714567 1961 tempfn[x] = alphanum[out % (sizeof(alphanum) - 1)];
wolfSSL 7:481bce714567 1962 }
wolfSSL 7:481bce714567 1963 tempfn[len] = '\0';
wolfSSL 7:481bce714567 1964
wolfSSL 7:481bce714567 1965 wc_FreeRng(&rng);
wolfSSL 7:481bce714567 1966
wolfSSL 7:481bce714567 1967 return tempfn;
wolfSSL 7:481bce714567 1968 }
wolfSSL 7:481bce714567 1969
wolfSSL 7:481bce714567 1970
wolfSSL 7:481bce714567 1971
wolfSSL 7:481bce714567 1972 #if defined(HAVE_SESSION_TICKET) && defined(HAVE_CHACHA) && \
wolfSSL 7:481bce714567 1973 defined(HAVE_POLY1305)
wolfSSL 7:481bce714567 1974
wolfSSL 7:481bce714567 1975 #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
wolfSSL 7:481bce714567 1976
wolfSSL 7:481bce714567 1977 typedef struct key_ctx {
wolfSSL 7:481bce714567 1978 byte name[WOLFSSL_TICKET_NAME_SZ]; /* name for this context */
wolfSSL 7:481bce714567 1979 byte key[CHACHA20_POLY1305_AEAD_KEYSIZE]; /* cipher key */
wolfSSL 7:481bce714567 1980 } key_ctx;
wolfSSL 7:481bce714567 1981
wolfSSL 7:481bce714567 1982 static key_ctx myKey_ctx;
wolfSSL 7:481bce714567 1983 static WC_RNG myKey_rng;
wolfSSL 7:481bce714567 1984
wolfSSL 7:481bce714567 1985 static INLINE int TicketInit(void)
wolfSSL 7:481bce714567 1986 {
wolfSSL 7:481bce714567 1987 int ret = wc_InitRng(&myKey_rng);
wolfSSL 7:481bce714567 1988 if (ret != 0) return ret;
wolfSSL 7:481bce714567 1989
wolfSSL 7:481bce714567 1990 ret = wc_RNG_GenerateBlock(&myKey_rng, myKey_ctx.key, sizeof(myKey_ctx.key));
wolfSSL 7:481bce714567 1991 if (ret != 0) return ret;
wolfSSL 7:481bce714567 1992
wolfSSL 7:481bce714567 1993 ret = wc_RNG_GenerateBlock(&myKey_rng, myKey_ctx.name,sizeof(myKey_ctx.name));
wolfSSL 7:481bce714567 1994 if (ret != 0) return ret;
wolfSSL 7:481bce714567 1995
wolfSSL 7:481bce714567 1996 return 0;
wolfSSL 7:481bce714567 1997 }
wolfSSL 7:481bce714567 1998
wolfSSL 7:481bce714567 1999 static INLINE void TicketCleanup(void)
wolfSSL 7:481bce714567 2000 {
wolfSSL 7:481bce714567 2001 wc_FreeRng(&myKey_rng);
wolfSSL 7:481bce714567 2002 }
wolfSSL 7:481bce714567 2003
wolfSSL 7:481bce714567 2004 static INLINE int myTicketEncCb(WOLFSSL* ssl,
wolfSSL 7:481bce714567 2005 byte key_name[WOLFSSL_TICKET_NAME_SZ],
wolfSSL 7:481bce714567 2006 byte iv[WOLFSSL_TICKET_IV_SZ],
wolfSSL 7:481bce714567 2007 byte mac[WOLFSSL_TICKET_MAC_SZ],
wolfSSL 7:481bce714567 2008 int enc, byte* ticket, int inLen, int* outLen,
wolfSSL 7:481bce714567 2009 void* userCtx)
wolfSSL 7:481bce714567 2010 {
wolfSSL 7:481bce714567 2011 (void)ssl;
wolfSSL 7:481bce714567 2012 (void)userCtx;
wolfSSL 7:481bce714567 2013
wolfSSL 7:481bce714567 2014 int ret;
wolfSSL 7:481bce714567 2015 word16 sLen = htons(inLen);
wolfSSL 7:481bce714567 2016 byte aad[WOLFSSL_TICKET_NAME_SZ + WOLFSSL_TICKET_IV_SZ + 2];
wolfSSL 7:481bce714567 2017 int aadSz = WOLFSSL_TICKET_NAME_SZ + WOLFSSL_TICKET_IV_SZ + 2;
wolfSSL 7:481bce714567 2018 byte* tmp = aad;
wolfSSL 7:481bce714567 2019
wolfSSL 7:481bce714567 2020 if (enc) {
wolfSSL 7:481bce714567 2021 XMEMCPY(key_name, myKey_ctx.name, WOLFSSL_TICKET_NAME_SZ);
wolfSSL 7:481bce714567 2022
wolfSSL 7:481bce714567 2023 ret = wc_RNG_GenerateBlock(&myKey_rng, iv, WOLFSSL_TICKET_IV_SZ);
wolfSSL 7:481bce714567 2024 if (ret != 0) return WOLFSSL_TICKET_RET_REJECT;
wolfSSL 7:481bce714567 2025
wolfSSL 7:481bce714567 2026 /* build aad from key name, iv, and length */
wolfSSL 7:481bce714567 2027 XMEMCPY(tmp, key_name, WOLFSSL_TICKET_NAME_SZ);
wolfSSL 7:481bce714567 2028 tmp += WOLFSSL_TICKET_NAME_SZ;
wolfSSL 7:481bce714567 2029 XMEMCPY(tmp, iv, WOLFSSL_TICKET_IV_SZ);
wolfSSL 7:481bce714567 2030 tmp += WOLFSSL_TICKET_IV_SZ;
wolfSSL 7:481bce714567 2031 XMEMCPY(tmp, &sLen, 2);
wolfSSL 7:481bce714567 2032
wolfSSL 7:481bce714567 2033 ret = wc_ChaCha20Poly1305_Encrypt(myKey_ctx.key, iv,
wolfSSL 7:481bce714567 2034 aad, aadSz,
wolfSSL 7:481bce714567 2035 ticket, inLen,
wolfSSL 7:481bce714567 2036 ticket,
wolfSSL 7:481bce714567 2037 mac);
wolfSSL 7:481bce714567 2038 if (ret != 0) return WOLFSSL_TICKET_RET_REJECT;
wolfSSL 7:481bce714567 2039 *outLen = inLen; /* no padding in this mode */
wolfSSL 7:481bce714567 2040 } else {
wolfSSL 7:481bce714567 2041 /* decrypt */
wolfSSL 7:481bce714567 2042
wolfSSL 7:481bce714567 2043 /* see if we know this key */
wolfSSL 7:481bce714567 2044 if (XMEMCMP(key_name, myKey_ctx.name, WOLFSSL_TICKET_NAME_SZ) != 0){
wolfSSL 7:481bce714567 2045 printf("client presented unknown ticket key name ");
wolfSSL 7:481bce714567 2046 return WOLFSSL_TICKET_RET_FATAL;
wolfSSL 7:481bce714567 2047 }
wolfSSL 7:481bce714567 2048
wolfSSL 7:481bce714567 2049 /* build aad from key name, iv, and length */
wolfSSL 7:481bce714567 2050 XMEMCPY(tmp, key_name, WOLFSSL_TICKET_NAME_SZ);
wolfSSL 7:481bce714567 2051 tmp += WOLFSSL_TICKET_NAME_SZ;
wolfSSL 7:481bce714567 2052 XMEMCPY(tmp, iv, WOLFSSL_TICKET_IV_SZ);
wolfSSL 7:481bce714567 2053 tmp += WOLFSSL_TICKET_IV_SZ;
wolfSSL 7:481bce714567 2054 XMEMCPY(tmp, &sLen, 2);
wolfSSL 7:481bce714567 2055
wolfSSL 7:481bce714567 2056 ret = wc_ChaCha20Poly1305_Decrypt(myKey_ctx.key, iv,
wolfSSL 7:481bce714567 2057 aad, aadSz,
wolfSSL 7:481bce714567 2058 ticket, inLen,
wolfSSL 7:481bce714567 2059 mac,
wolfSSL 7:481bce714567 2060 ticket);
wolfSSL 7:481bce714567 2061 if (ret != 0) return WOLFSSL_TICKET_RET_REJECT;
wolfSSL 7:481bce714567 2062 *outLen = inLen; /* no padding in this mode */
wolfSSL 7:481bce714567 2063 }
wolfSSL 7:481bce714567 2064
wolfSSL 7:481bce714567 2065 return WOLFSSL_TICKET_RET_OK;
wolfSSL 7:481bce714567 2066 }
wolfSSL 7:481bce714567 2067
wolfSSL 7:481bce714567 2068 #endif /* HAVE_SESSION_TICKET && CHACHA20 && POLY1305 */
wolfSSL 7:481bce714567 2069
wolfSSL 7:481bce714567 2070 static INLINE word16 GetRandomPort(void)
wolfSSL 7:481bce714567 2071 {
wolfSSL 7:481bce714567 2072 word16 port = 0;
wolfSSL 7:481bce714567 2073
wolfSSL 7:481bce714567 2074 /* Generate random port for testing */
wolfSSL 7:481bce714567 2075 WC_RNG rng;
wolfSSL 7:481bce714567 2076 if (wc_InitRng(&rng) == 0) {
wolfSSL 7:481bce714567 2077 wc_RNG_GenerateBlock(&rng, (byte*)&port, sizeof(port));
wolfSSL 7:481bce714567 2078 port |= 0xC000; /* Make sure its in the 49152 - 65535 range */
wolfSSL 7:481bce714567 2079 wc_FreeRng(&rng);
wolfSSL 7:481bce714567 2080 }
wolfSSL 7:481bce714567 2081 return port;
wolfSSL 7:481bce714567 2082 }
wolfSSL 7:481bce714567 2083
wolfSSL 7:481bce714567 2084 #endif /* wolfSSL_TEST_H */
wolfSSL 7:481bce714567 2085