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