Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more
wolfssl/test.h@16:8e0d178b1d1e, 2020-06-04 (annotated)
- Committer:
- wolfSSL
- Date:
- Thu Jun 04 23:57:22 2020 +0000
- Revision:
- 16:8e0d178b1d1e
- Parent:
- 15:117db924cf7c
wolfSSL 4.4.0
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| wolfSSL | 15:117db924cf7c | 1 | /* test.h */ |
| wolfSSL | 15:117db924cf7c | 2 | |
| wolfSSL | 15:117db924cf7c | 3 | #ifndef wolfSSL_TEST_H |
| wolfSSL | 15:117db924cf7c | 4 | #define wolfSSL_TEST_H |
| wolfSSL | 15:117db924cf7c | 5 | |
| wolfSSL | 15:117db924cf7c | 6 | #include <stdio.h> |
| wolfSSL | 15:117db924cf7c | 7 | #include <stdlib.h> |
| wolfSSL | 15:117db924cf7c | 8 | #include <assert.h> |
| wolfSSL | 15:117db924cf7c | 9 | #include <ctype.h> |
| wolfSSL | 15:117db924cf7c | 10 | #include <wolfssl/wolfcrypt/types.h> |
| wolfSSL | 15:117db924cf7c | 11 | #include <wolfssl/wolfcrypt/error-crypt.h> |
| wolfSSL | 15:117db924cf7c | 12 | #include <wolfssl/wolfcrypt/random.h> |
| wolfSSL | 15:117db924cf7c | 13 | #include <wolfssl/wolfcrypt/mem_track.h> |
| wolfSSL | 16:8e0d178b1d1e | 14 | #if defined(SHOW_CERTS) && \ |
| wolfSSL | 16:8e0d178b1d1e | 15 | (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) |
| wolfSSL | 16:8e0d178b1d1e | 16 | #include <wolfssl/wolfcrypt/asn.h> /* for domain component NID value */ |
| wolfSSL | 15:117db924cf7c | 17 | #endif |
| wolfSSL | 15:117db924cf7c | 18 | |
| wolfSSL | 15:117db924cf7c | 19 | #ifdef ATOMIC_USER |
| wolfSSL | 15:117db924cf7c | 20 | #include <wolfssl/wolfcrypt/aes.h> |
| wolfSSL | 15:117db924cf7c | 21 | #include <wolfssl/wolfcrypt/arc4.h> |
| wolfSSL | 15:117db924cf7c | 22 | #include <wolfssl/wolfcrypt/hmac.h> |
| wolfSSL | 15:117db924cf7c | 23 | #endif |
| wolfSSL | 15:117db924cf7c | 24 | #ifdef HAVE_PK_CALLBACKS |
| wolfSSL | 15:117db924cf7c | 25 | #include <wolfssl/wolfcrypt/asn.h> |
| wolfSSL | 15:117db924cf7c | 26 | #ifndef NO_RSA |
| wolfSSL | 15:117db924cf7c | 27 | #include <wolfssl/wolfcrypt/rsa.h> |
| wolfSSL | 15:117db924cf7c | 28 | #endif |
| wolfSSL | 15:117db924cf7c | 29 | #ifdef HAVE_ECC |
| wolfSSL | 15:117db924cf7c | 30 | #include <wolfssl/wolfcrypt/ecc.h> |
| wolfSSL | 15:117db924cf7c | 31 | #endif /* HAVE_ECC */ |
| wolfSSL | 15:117db924cf7c | 32 | #ifndef NO_DH |
| wolfSSL | 15:117db924cf7c | 33 | #include <wolfssl/wolfcrypt/dh.h> |
| wolfSSL | 15:117db924cf7c | 34 | #endif /* !NO_DH */ |
| wolfSSL | 15:117db924cf7c | 35 | #ifdef HAVE_ED25519 |
| wolfSSL | 15:117db924cf7c | 36 | #include <wolfssl/wolfcrypt/ed25519.h> |
| wolfSSL | 15:117db924cf7c | 37 | #endif /* HAVE_ED25519 */ |
| wolfSSL | 15:117db924cf7c | 38 | #ifdef HAVE_CURVE25519 |
| wolfSSL | 15:117db924cf7c | 39 | #include <wolfssl/wolfcrypt/curve25519.h> |
| wolfSSL | 15:117db924cf7c | 40 | #endif /* HAVE_ECC */ |
| wolfSSL | 16:8e0d178b1d1e | 41 | #ifdef HAVE_ED448 |
| wolfSSL | 16:8e0d178b1d1e | 42 | #include <wolfssl/wolfcrypt/ed448.h> |
| wolfSSL | 16:8e0d178b1d1e | 43 | #endif /* HAVE_ED448 */ |
| wolfSSL | 16:8e0d178b1d1e | 44 | #ifdef HAVE_CURVE448 |
| wolfSSL | 16:8e0d178b1d1e | 45 | #include <wolfssl/wolfcrypt/curve448.h> |
| wolfSSL | 16:8e0d178b1d1e | 46 | #endif /* HAVE_ECC */ |
| wolfSSL | 15:117db924cf7c | 47 | #endif /*HAVE_PK_CALLBACKS */ |
| wolfSSL | 15:117db924cf7c | 48 | |
| wolfSSL | 15:117db924cf7c | 49 | #ifdef USE_WINDOWS_API |
| wolfSSL | 15:117db924cf7c | 50 | #include <winsock2.h> |
| wolfSSL | 15:117db924cf7c | 51 | #include <process.h> |
| wolfSSL | 15:117db924cf7c | 52 | #ifdef TEST_IPV6 /* don't require newer SDK for IPV4 */ |
| wolfSSL | 15:117db924cf7c | 53 | #include <ws2tcpip.h> |
| wolfSSL | 15:117db924cf7c | 54 | #include <wspiapi.h> |
| wolfSSL | 15:117db924cf7c | 55 | #endif |
| wolfSSL | 15:117db924cf7c | 56 | #define SOCKET_T SOCKET |
| wolfSSL | 15:117db924cf7c | 57 | #define SNPRINTF _snprintf |
| wolfSSL | 15:117db924cf7c | 58 | #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) |
| wolfSSL | 15:117db924cf7c | 59 | #include <string.h> |
| wolfSSL | 15:117db924cf7c | 60 | #include "rl_net.h" |
| wolfSSL | 15:117db924cf7c | 61 | #define SOCKET_T int |
| wolfSSL | 16:8e0d178b1d1e | 62 | typedef int socklen_t ; |
| wolfSSL | 16:8e0d178b1d1e | 63 | #define inet_addr wolfSSL_inet_addr |
| wolfSSL | 16:8e0d178b1d1e | 64 | static unsigned long wolfSSL_inet_addr(const char *cp) |
| wolfSSL | 15:117db924cf7c | 65 | { |
| wolfSSL | 15:117db924cf7c | 66 | unsigned int a[4] ; unsigned long ret ; |
| wolfSSL | 15:117db924cf7c | 67 | sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) ; |
| wolfSSL | 15:117db924cf7c | 68 | ret = ((a[3]<<24) + (a[2]<<16) + (a[1]<<8) + a[0]) ; |
| wolfSSL | 15:117db924cf7c | 69 | return(ret) ; |
| wolfSSL | 15:117db924cf7c | 70 | } |
| wolfSSL | 16:8e0d178b1d1e | 71 | #if defined(HAVE_KEIL_RTX) |
| wolfSSL | 16:8e0d178b1d1e | 72 | #define sleep(t) os_dly_wait(t/1000+1); |
| wolfSSL | 16:8e0d178b1d1e | 73 | #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2) |
| wolfSSL | 16:8e0d178b1d1e | 74 | #define sleep(t) osDelay(t/1000+1); |
| wolfSSL | 15:117db924cf7c | 75 | #endif |
| wolfSSL | 15:117db924cf7c | 76 | #elif defined(WOLFSSL_TIRTOS) |
| wolfSSL | 15:117db924cf7c | 77 | #include <string.h> |
| wolfSSL | 15:117db924cf7c | 78 | #include <netdb.h> |
| wolfSSL | 15:117db924cf7c | 79 | #include <sys/types.h> |
| wolfSSL | 15:117db924cf7c | 80 | #include <arpa/inet.h> |
| wolfSSL | 15:117db924cf7c | 81 | #include <sys/socket.h> |
| wolfSSL | 15:117db924cf7c | 82 | #include <ti/sysbios/knl/Task.h> |
| wolfSSL | 15:117db924cf7c | 83 | struct hostent { |
| wolfSSL | 15:117db924cf7c | 84 | char *h_name; /* official name of host */ |
| wolfSSL | 15:117db924cf7c | 85 | char **h_aliases; /* alias list */ |
| wolfSSL | 15:117db924cf7c | 86 | int h_addrtype; /* host address type */ |
| wolfSSL | 15:117db924cf7c | 87 | int h_length; /* length of address */ |
| wolfSSL | 15:117db924cf7c | 88 | char **h_addr_list; /* list of addresses from name server */ |
| wolfSSL | 15:117db924cf7c | 89 | }; |
| wolfSSL | 15:117db924cf7c | 90 | #define SOCKET_T int |
| wolfSSL | 15:117db924cf7c | 91 | #elif defined(WOLFSSL_VXWORKS) |
| wolfSSL | 15:117db924cf7c | 92 | #include <hostLib.h> |
| wolfSSL | 15:117db924cf7c | 93 | #include <sockLib.h> |
| wolfSSL | 15:117db924cf7c | 94 | #include <arpa/inet.h> |
| wolfSSL | 15:117db924cf7c | 95 | #include <string.h> |
| wolfSSL | 15:117db924cf7c | 96 | #include <selectLib.h> |
| wolfSSL | 15:117db924cf7c | 97 | #include <sys/types.h> |
| wolfSSL | 15:117db924cf7c | 98 | #include <netinet/in.h> |
| wolfSSL | 15:117db924cf7c | 99 | #include <fcntl.h> |
| wolfSSL | 15:117db924cf7c | 100 | #include <sys/time.h> |
| wolfSSL | 15:117db924cf7c | 101 | #include <netdb.h> |
| wolfSSL | 15:117db924cf7c | 102 | #include <pthread.h> |
| wolfSSL | 15:117db924cf7c | 103 | #define SOCKET_T int |
| wolfSSL | 16:8e0d178b1d1e | 104 | #elif defined(WOLFSSL_ZEPHYR) |
| wolfSSL | 16:8e0d178b1d1e | 105 | #include <string.h> |
| wolfSSL | 16:8e0d178b1d1e | 106 | #include <sys/types.h> |
| wolfSSL | 16:8e0d178b1d1e | 107 | #include <net/socket.h> |
| wolfSSL | 16:8e0d178b1d1e | 108 | #define SOCKET_T int |
| wolfSSL | 16:8e0d178b1d1e | 109 | #define SOL_SOCKET 1 |
| wolfSSL | 16:8e0d178b1d1e | 110 | #define SO_REUSEADDR 201 |
| wolfSSL | 16:8e0d178b1d1e | 111 | #define WOLFSSL_USE_GETADDRINFO |
| wolfSSL | 16:8e0d178b1d1e | 112 | |
| wolfSSL | 16:8e0d178b1d1e | 113 | static unsigned long inet_addr(const char *cp) |
| wolfSSL | 16:8e0d178b1d1e | 114 | { |
| wolfSSL | 16:8e0d178b1d1e | 115 | unsigned int a[4]; unsigned long ret; |
| wolfSSL | 16:8e0d178b1d1e | 116 | int i, j; |
| wolfSSL | 16:8e0d178b1d1e | 117 | for (i=0, j=0; i<4; i++) { |
| wolfSSL | 16:8e0d178b1d1e | 118 | a[i] = 0; |
| wolfSSL | 16:8e0d178b1d1e | 119 | while (cp[j] != '.' && cp[j] != '\0') { |
| wolfSSL | 16:8e0d178b1d1e | 120 | a[i] *= 10; |
| wolfSSL | 16:8e0d178b1d1e | 121 | a[i] += cp[j] - '0'; |
| wolfSSL | 16:8e0d178b1d1e | 122 | j++; |
| wolfSSL | 16:8e0d178b1d1e | 123 | } |
| wolfSSL | 16:8e0d178b1d1e | 124 | } |
| wolfSSL | 16:8e0d178b1d1e | 125 | ret = ((a[3]<<24) + (a[2]<<16) + (a[1]<<8) + a[0]) ; |
| wolfSSL | 16:8e0d178b1d1e | 126 | return(ret) ; |
| wolfSSL | 16:8e0d178b1d1e | 127 | } |
| wolfSSL | 15:117db924cf7c | 128 | #else |
| wolfSSL | 15:117db924cf7c | 129 | #include <string.h> |
| wolfSSL | 15:117db924cf7c | 130 | #include <sys/types.h> |
| wolfSSL | 15:117db924cf7c | 131 | #ifndef WOLFSSL_LEANPSK |
| wolfSSL | 15:117db924cf7c | 132 | #include <unistd.h> |
| wolfSSL | 15:117db924cf7c | 133 | #include <netdb.h> |
| wolfSSL | 15:117db924cf7c | 134 | #include <netinet/in.h> |
| wolfSSL | 15:117db924cf7c | 135 | #include <netinet/tcp.h> |
| wolfSSL | 15:117db924cf7c | 136 | #include <arpa/inet.h> |
| wolfSSL | 15:117db924cf7c | 137 | #include <sys/ioctl.h> |
| wolfSSL | 15:117db924cf7c | 138 | #include <sys/time.h> |
| wolfSSL | 15:117db924cf7c | 139 | #include <sys/socket.h> |
| wolfSSL | 15:117db924cf7c | 140 | #include <pthread.h> |
| wolfSSL | 15:117db924cf7c | 141 | #include <fcntl.h> |
| wolfSSL | 15:117db924cf7c | 142 | #ifdef TEST_IPV6 |
| wolfSSL | 15:117db924cf7c | 143 | #include <netdb.h> |
| wolfSSL | 15:117db924cf7c | 144 | #endif |
| wolfSSL | 15:117db924cf7c | 145 | #endif |
| wolfSSL | 15:117db924cf7c | 146 | #define SOCKET_T int |
| wolfSSL | 15:117db924cf7c | 147 | #ifndef SO_NOSIGPIPE |
| wolfSSL | 15:117db924cf7c | 148 | #include <signal.h> /* ignore SIGPIPE */ |
| wolfSSL | 15:117db924cf7c | 149 | #endif |
| wolfSSL | 15:117db924cf7c | 150 | #define SNPRINTF snprintf |
| wolfSSL | 15:117db924cf7c | 151 | #endif /* USE_WINDOWS_API */ |
| wolfSSL | 15:117db924cf7c | 152 | |
| wolfSSL | 15:117db924cf7c | 153 | #ifdef WOLFSSL_ASYNC_CRYPT |
| wolfSSL | 15:117db924cf7c | 154 | #include <wolfssl/wolfcrypt/async.h> |
| wolfSSL | 15:117db924cf7c | 155 | #endif |
| wolfSSL | 15:117db924cf7c | 156 | #ifdef HAVE_CAVIUM |
| wolfSSL | 15:117db924cf7c | 157 | #include <wolfssl/wolfcrypt/port/cavium/cavium_nitrox.h> |
| wolfSSL | 15:117db924cf7c | 158 | #endif |
| wolfSSL | 15:117db924cf7c | 159 | #ifdef _MSC_VER |
| wolfSSL | 15:117db924cf7c | 160 | /* disable conversion warning */ |
| wolfSSL | 15:117db924cf7c | 161 | /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */ |
| wolfSSL | 15:117db924cf7c | 162 | #pragma warning(disable:4244 4996) |
| wolfSSL | 15:117db924cf7c | 163 | #endif |
| wolfSSL | 15:117db924cf7c | 164 | |
| wolfSSL | 16:8e0d178b1d1e | 165 | #ifndef WOLFSSL_CIPHER_LIST_MAX_SIZE |
| wolfSSL | 16:8e0d178b1d1e | 166 | #define WOLFSSL_CIPHER_LIST_MAX_SIZE 4096 |
| wolfSSL | 16:8e0d178b1d1e | 167 | #endif |
| wolfSSL | 15:117db924cf7c | 168 | /* Buffer for benchmark tests */ |
| wolfSSL | 15:117db924cf7c | 169 | #ifndef TEST_BUFFER_SIZE |
| wolfSSL | 16:8e0d178b1d1e | 170 | #define TEST_BUFFER_SIZE 16384 |
| wolfSSL | 15:117db924cf7c | 171 | #endif |
| wolfSSL | 15:117db924cf7c | 172 | |
| wolfSSL | 15:117db924cf7c | 173 | #ifndef WOLFSSL_HAVE_MIN |
| wolfSSL | 15:117db924cf7c | 174 | #define WOLFSSL_HAVE_MIN |
| wolfSSL | 15:117db924cf7c | 175 | static WC_INLINE word32 min(word32 a, word32 b) |
| wolfSSL | 15:117db924cf7c | 176 | { |
| wolfSSL | 15:117db924cf7c | 177 | return a > b ? b : a; |
| wolfSSL | 15:117db924cf7c | 178 | } |
| wolfSSL | 15:117db924cf7c | 179 | #endif /* WOLFSSL_HAVE_MIN */ |
| wolfSSL | 15:117db924cf7c | 180 | |
| wolfSSL | 15:117db924cf7c | 181 | /* Socket Handling */ |
| wolfSSL | 15:117db924cf7c | 182 | #ifndef WOLFSSL_SOCKET_INVALID |
| wolfSSL | 15:117db924cf7c | 183 | #ifdef USE_WINDOWS_API |
| wolfSSL | 15:117db924cf7c | 184 | #define WOLFSSL_SOCKET_INVALID ((SOCKET_T)INVALID_SOCKET) |
| wolfSSL | 15:117db924cf7c | 185 | #elif defined(WOLFSSL_TIRTOS) |
| wolfSSL | 15:117db924cf7c | 186 | #define WOLFSSL_SOCKET_INVALID ((SOCKET_T)-1) |
| wolfSSL | 15:117db924cf7c | 187 | #else |
| wolfSSL | 15:117db924cf7c | 188 | #define WOLFSSL_SOCKET_INVALID (SOCKET_T)(0) |
| wolfSSL | 15:117db924cf7c | 189 | #endif |
| wolfSSL | 15:117db924cf7c | 190 | #endif /* WOLFSSL_SOCKET_INVALID */ |
| wolfSSL | 15:117db924cf7c | 191 | |
| wolfSSL | 15:117db924cf7c | 192 | #ifndef WOLFSSL_SOCKET_IS_INVALID |
| wolfSSL | 15:117db924cf7c | 193 | #if defined(USE_WINDOWS_API) || defined(WOLFSSL_TIRTOS) |
| wolfSSL | 15:117db924cf7c | 194 | #define WOLFSSL_SOCKET_IS_INVALID(s) ((SOCKET_T)(s) == WOLFSSL_SOCKET_INVALID) |
| wolfSSL | 15:117db924cf7c | 195 | #else |
| wolfSSL | 15:117db924cf7c | 196 | #define WOLFSSL_SOCKET_IS_INVALID(s) ((SOCKET_T)(s) < WOLFSSL_SOCKET_INVALID) |
| wolfSSL | 15:117db924cf7c | 197 | #endif |
| wolfSSL | 15:117db924cf7c | 198 | #endif /* WOLFSSL_SOCKET_IS_INVALID */ |
| wolfSSL | 15:117db924cf7c | 199 | |
| wolfSSL | 15:117db924cf7c | 200 | #if defined(__MACH__) || defined(USE_WINDOWS_API) |
| wolfSSL | 15:117db924cf7c | 201 | #ifndef _SOCKLEN_T |
| wolfSSL | 15:117db924cf7c | 202 | typedef int socklen_t; |
| wolfSSL | 15:117db924cf7c | 203 | #endif |
| wolfSSL | 15:117db924cf7c | 204 | #endif |
| wolfSSL | 15:117db924cf7c | 205 | |
| wolfSSL | 15:117db924cf7c | 206 | |
| wolfSSL | 15:117db924cf7c | 207 | /* HPUX doesn't use socklent_t for third parameter to accept, unless |
| wolfSSL | 15:117db924cf7c | 208 | _XOPEN_SOURCE_EXTENDED is defined */ |
| wolfSSL | 15:117db924cf7c | 209 | #if !defined(__hpux__) && !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_IAR_ARM)\ |
| wolfSSL | 15:117db924cf7c | 210 | && !defined(WOLFSSL_ROWLEY_ARM) && !defined(WOLFSSL_KEIL_TCP_NET) |
| wolfSSL | 15:117db924cf7c | 211 | typedef socklen_t* ACCEPT_THIRD_T; |
| wolfSSL | 15:117db924cf7c | 212 | #else |
| wolfSSL | 15:117db924cf7c | 213 | #if defined _XOPEN_SOURCE_EXTENDED |
| wolfSSL | 15:117db924cf7c | 214 | typedef socklen_t* ACCEPT_THIRD_T; |
| wolfSSL | 15:117db924cf7c | 215 | #else |
| wolfSSL | 15:117db924cf7c | 216 | typedef int* ACCEPT_THIRD_T; |
| wolfSSL | 15:117db924cf7c | 217 | #endif |
| wolfSSL | 15:117db924cf7c | 218 | #endif |
| wolfSSL | 15:117db924cf7c | 219 | |
| wolfSSL | 15:117db924cf7c | 220 | |
| wolfSSL | 15:117db924cf7c | 221 | |
| wolfSSL | 15:117db924cf7c | 222 | #ifdef SINGLE_THREADED |
| wolfSSL | 15:117db924cf7c | 223 | typedef unsigned int THREAD_RETURN; |
| wolfSSL | 15:117db924cf7c | 224 | typedef void* THREAD_TYPE; |
| wolfSSL | 15:117db924cf7c | 225 | #define WOLFSSL_THREAD |
| wolfSSL | 15:117db924cf7c | 226 | #else |
| wolfSSL | 15:117db924cf7c | 227 | #if defined(_POSIX_THREADS) && !defined(__MINGW32__) |
| wolfSSL | 15:117db924cf7c | 228 | typedef void* THREAD_RETURN; |
| wolfSSL | 15:117db924cf7c | 229 | typedef pthread_t THREAD_TYPE; |
| wolfSSL | 15:117db924cf7c | 230 | #define WOLFSSL_THREAD |
| wolfSSL | 15:117db924cf7c | 231 | #define INFINITE -1 |
| wolfSSL | 15:117db924cf7c | 232 | #define WAIT_OBJECT_0 0L |
| wolfSSL | 15:117db924cf7c | 233 | #elif defined(WOLFSSL_MDK_ARM)|| defined(WOLFSSL_KEIL_TCP_NET) |
| wolfSSL | 15:117db924cf7c | 234 | typedef unsigned int THREAD_RETURN; |
| wolfSSL | 15:117db924cf7c | 235 | typedef int THREAD_TYPE; |
| wolfSSL | 15:117db924cf7c | 236 | #define WOLFSSL_THREAD |
| wolfSSL | 15:117db924cf7c | 237 | #elif defined(WOLFSSL_TIRTOS) |
| wolfSSL | 15:117db924cf7c | 238 | typedef void THREAD_RETURN; |
| wolfSSL | 15:117db924cf7c | 239 | typedef Task_Handle THREAD_TYPE; |
| wolfSSL | 15:117db924cf7c | 240 | #define WOLFSSL_THREAD |
| wolfSSL | 16:8e0d178b1d1e | 241 | #elif defined(WOLFSSL_ZEPHYR) |
| wolfSSL | 16:8e0d178b1d1e | 242 | typedef void THREAD_RETURN; |
| wolfSSL | 16:8e0d178b1d1e | 243 | typedef struct k_thread THREAD_TYPE; |
| wolfSSL | 16:8e0d178b1d1e | 244 | #define WOLFSSL_THREAD |
| wolfSSL | 15:117db924cf7c | 245 | #else |
| wolfSSL | 15:117db924cf7c | 246 | typedef unsigned int THREAD_RETURN; |
| wolfSSL | 15:117db924cf7c | 247 | typedef intptr_t THREAD_TYPE; |
| wolfSSL | 15:117db924cf7c | 248 | #define WOLFSSL_THREAD __stdcall |
| wolfSSL | 15:117db924cf7c | 249 | #endif |
| wolfSSL | 15:117db924cf7c | 250 | #endif |
| wolfSSL | 15:117db924cf7c | 251 | |
| wolfSSL | 15:117db924cf7c | 252 | |
| wolfSSL | 15:117db924cf7c | 253 | #ifdef TEST_IPV6 |
| wolfSSL | 15:117db924cf7c | 254 | typedef struct sockaddr_in6 SOCKADDR_IN_T; |
| wolfSSL | 15:117db924cf7c | 255 | #define AF_INET_V AF_INET6 |
| wolfSSL | 15:117db924cf7c | 256 | #else |
| wolfSSL | 15:117db924cf7c | 257 | typedef struct sockaddr_in SOCKADDR_IN_T; |
| wolfSSL | 15:117db924cf7c | 258 | #define AF_INET_V AF_INET |
| wolfSSL | 15:117db924cf7c | 259 | #endif |
| wolfSSL | 15:117db924cf7c | 260 | |
| wolfSSL | 15:117db924cf7c | 261 | |
| wolfSSL | 15:117db924cf7c | 262 | #ifndef WOLFSSL_NO_TLS12 |
| wolfSSL | 15:117db924cf7c | 263 | #define SERVER_DEFAULT_VERSION 3 |
| wolfSSL | 15:117db924cf7c | 264 | #else |
| wolfSSL | 15:117db924cf7c | 265 | #define SERVER_DEFAULT_VERSION 4 |
| wolfSSL | 15:117db924cf7c | 266 | #endif |
| wolfSSL | 15:117db924cf7c | 267 | #define SERVER_DTLS_DEFAULT_VERSION (-2) |
| wolfSSL | 15:117db924cf7c | 268 | #define SERVER_INVALID_VERSION (-99) |
| wolfSSL | 15:117db924cf7c | 269 | #define SERVER_DOWNGRADE_VERSION (-98) |
| wolfSSL | 15:117db924cf7c | 270 | #ifndef WOLFSSL_NO_TLS12 |
| wolfSSL | 15:117db924cf7c | 271 | #define CLIENT_DEFAULT_VERSION 3 |
| wolfSSL | 15:117db924cf7c | 272 | #else |
| wolfSSL | 15:117db924cf7c | 273 | #define CLIENT_DEFAULT_VERSION 4 |
| wolfSSL | 15:117db924cf7c | 274 | #endif |
| wolfSSL | 15:117db924cf7c | 275 | #define CLIENT_DTLS_DEFAULT_VERSION (-2) |
| wolfSSL | 15:117db924cf7c | 276 | #define CLIENT_INVALID_VERSION (-99) |
| wolfSSL | 15:117db924cf7c | 277 | #define CLIENT_DOWNGRADE_VERSION (-98) |
| wolfSSL | 16:8e0d178b1d1e | 278 | #define EITHER_DOWNGRADE_VERSION (-97) |
| wolfSSL | 15:117db924cf7c | 279 | #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH) |
| wolfSSL | 15:117db924cf7c | 280 | #define DEFAULT_MIN_DHKEY_BITS 2048 |
| wolfSSL | 15:117db924cf7c | 281 | #define DEFAULT_MAX_DHKEY_BITS 3072 |
| wolfSSL | 15:117db924cf7c | 282 | #else |
| wolfSSL | 15:117db924cf7c | 283 | #define DEFAULT_MIN_DHKEY_BITS 1024 |
| wolfSSL | 15:117db924cf7c | 284 | #define DEFAULT_MAX_DHKEY_BITS 2048 |
| wolfSSL | 15:117db924cf7c | 285 | #endif |
| wolfSSL | 15:117db924cf7c | 286 | #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH) |
| wolfSSL | 15:117db924cf7c | 287 | #define DEFAULT_MIN_RSAKEY_BITS 2048 |
| wolfSSL | 15:117db924cf7c | 288 | #else |
| wolfSSL | 15:117db924cf7c | 289 | #define DEFAULT_MIN_RSAKEY_BITS 1024 |
| wolfSSL | 15:117db924cf7c | 290 | #endif |
| wolfSSL | 15:117db924cf7c | 291 | #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH) |
| wolfSSL | 15:117db924cf7c | 292 | #define DEFAULT_MIN_ECCKEY_BITS 256 |
| wolfSSL | 15:117db924cf7c | 293 | #else |
| wolfSSL | 15:117db924cf7c | 294 | #define DEFAULT_MIN_ECCKEY_BITS 224 |
| wolfSSL | 15:117db924cf7c | 295 | #endif |
| wolfSSL | 15:117db924cf7c | 296 | |
| wolfSSL | 15:117db924cf7c | 297 | /* all certs relative to wolfSSL home directory now */ |
| wolfSSL | 15:117db924cf7c | 298 | #if defined(WOLFSSL_NO_CURRDIR) || defined(WOLFSSL_MDK_SHELL) |
| wolfSSL | 16:8e0d178b1d1e | 299 | #define caCertFile "certs/ca-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 300 | #define eccCertFile "certs/server-ecc.pem" |
| wolfSSL | 16:8e0d178b1d1e | 301 | #define eccKeyFile "certs/ecc-key.pem" |
| wolfSSL | 16:8e0d178b1d1e | 302 | #define eccRsaCertFile "certs/server-ecc-rsa.pem" |
| wolfSSL | 16:8e0d178b1d1e | 303 | #define svrCertFile "certs/server-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 304 | #define svrKeyFile "certs/server-key.pem" |
| wolfSSL | 16:8e0d178b1d1e | 305 | #define cliCertFile "certs/client-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 306 | #define cliCertDerFile "certs/client-cert.der" |
| wolfSSL | 16:8e0d178b1d1e | 307 | #define cliCertFileExt "certs/client-cert-ext.pem" |
| wolfSSL | 16:8e0d178b1d1e | 308 | #define cliCertDerFileExt "certs/client-cert-ext.der" |
| wolfSSL | 16:8e0d178b1d1e | 309 | #define cliKeyFile "certs/client-key.pem" |
| wolfSSL | 16:8e0d178b1d1e | 310 | #define ntruCertFile "certs/ntru-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 311 | #define ntruKeyFile "certs/ntru-key.raw" |
| wolfSSL | 16:8e0d178b1d1e | 312 | #define dhParamFile "certs/dh2048.pem" |
| wolfSSL | 16:8e0d178b1d1e | 313 | #define cliEccKeyFile "certs/ecc-client-key.pem" |
| wolfSSL | 16:8e0d178b1d1e | 314 | #define cliEccCertFile "certs/client-ecc-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 315 | #define caEccCertFile "certs/ca-ecc-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 316 | #define crlPemDir "certs/crl" |
| wolfSSL | 16:8e0d178b1d1e | 317 | #define edCertFile "certs/ed25519/server-ed25519-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 318 | #define edKeyFile "certs/ed25519/server-ed25519-priv.pem" |
| wolfSSL | 16:8e0d178b1d1e | 319 | #define cliEdCertFile "certs/ed25519/client-ed25519.pem" |
| wolfSSL | 16:8e0d178b1d1e | 320 | #define cliEdKeyFile "certs/ed25519/client-ed25519-priv.pem" |
| wolfSSL | 16:8e0d178b1d1e | 321 | #define caEdCertFile "certs/ed25519/ca-ed25519.pem" |
| wolfSSL | 16:8e0d178b1d1e | 322 | #define ed448CertFile "certs/ed448/server-ed448-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 323 | #define ed448KeyFile "certs/ed448/server-ed448-priv.pem" |
| wolfSSL | 16:8e0d178b1d1e | 324 | #define cliEd448CertFile "certs/ed448/client-ed448.pem" |
| wolfSSL | 16:8e0d178b1d1e | 325 | #define cliEd448KeyFile "certs/ed448/client-ed448-priv.pem" |
| wolfSSL | 16:8e0d178b1d1e | 326 | #define caEd448CertFile "certs/ed448/ca-ed448.pem" |
| wolfSSL | 15:117db924cf7c | 327 | #ifdef HAVE_WNR |
| wolfSSL | 15:117db924cf7c | 328 | /* Whitewood netRandom default config file */ |
| wolfSSL | 16:8e0d178b1d1e | 329 | #define wnrConfig "wnr-example.conf" |
| wolfSSL | 15:117db924cf7c | 330 | #endif |
| wolfSSL | 15:117db924cf7c | 331 | #else |
| wolfSSL | 16:8e0d178b1d1e | 332 | #define caCertFile "./certs/ca-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 333 | #define eccCertFile "./certs/server-ecc.pem" |
| wolfSSL | 16:8e0d178b1d1e | 334 | #define eccKeyFile "./certs/ecc-key.pem" |
| wolfSSL | 16:8e0d178b1d1e | 335 | #define eccRsaCertFile "./certs/server-ecc-rsa.pem" |
| wolfSSL | 16:8e0d178b1d1e | 336 | #define svrCertFile "./certs/server-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 337 | #define svrKeyFile "./certs/server-key.pem" |
| wolfSSL | 16:8e0d178b1d1e | 338 | #define cliCertFile "./certs/client-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 339 | #define cliCertDerFile "./certs/client-cert.der" |
| wolfSSL | 16:8e0d178b1d1e | 340 | #define cliCertFileExt "./certs/client-cert-ext.pem" |
| wolfSSL | 16:8e0d178b1d1e | 341 | #define cliCertDerFileExt "./certs/client-cert-ext.der" |
| wolfSSL | 16:8e0d178b1d1e | 342 | #define cliKeyFile "./certs/client-key.pem" |
| wolfSSL | 16:8e0d178b1d1e | 343 | #define ntruCertFile "./certs/ntru-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 344 | #define ntruKeyFile "./certs/ntru-key.raw" |
| wolfSSL | 16:8e0d178b1d1e | 345 | #define dhParamFile "./certs/dh2048.pem" |
| wolfSSL | 16:8e0d178b1d1e | 346 | #define cliEccKeyFile "./certs/ecc-client-key.pem" |
| wolfSSL | 16:8e0d178b1d1e | 347 | #define cliEccCertFile "./certs/client-ecc-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 348 | #define caEccCertFile "./certs/ca-ecc-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 349 | #define crlPemDir "./certs/crl" |
| wolfSSL | 16:8e0d178b1d1e | 350 | #define edCertFile "./certs/ed25519/server-ed25519-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 351 | #define edKeyFile "./certs/ed25519/server-ed25519-priv.pem" |
| wolfSSL | 16:8e0d178b1d1e | 352 | #define cliEdCertFile "./certs/ed25519/client-ed25519.pem" |
| wolfSSL | 16:8e0d178b1d1e | 353 | #define cliEdKeyFile "./certs/ed25519/client-ed25519-priv.pem" |
| wolfSSL | 16:8e0d178b1d1e | 354 | #define caEdCertFile "./certs/ed25519/ca-ed25519.pem" |
| wolfSSL | 16:8e0d178b1d1e | 355 | #define ed448CertFile "./certs/ed448/server-ed448-cert.pem" |
| wolfSSL | 16:8e0d178b1d1e | 356 | #define ed448KeyFile "./certs/ed448/server-ed448-priv.pem" |
| wolfSSL | 16:8e0d178b1d1e | 357 | #define cliEd448CertFile "./certs/ed448/client-ed448.pem" |
| wolfSSL | 16:8e0d178b1d1e | 358 | #define cliEd448KeyFile "./certs/ed448/client-ed448-priv.pem" |
| wolfSSL | 16:8e0d178b1d1e | 359 | #define caEd448CertFile "./certs/ed448/ca-ed448.pem" |
| wolfSSL | 15:117db924cf7c | 360 | #ifdef HAVE_WNR |
| wolfSSL | 15:117db924cf7c | 361 | /* Whitewood netRandom default config file */ |
| wolfSSL | 16:8e0d178b1d1e | 362 | #define wnrConfig "./wnr-example.conf" |
| wolfSSL | 15:117db924cf7c | 363 | #endif |
| wolfSSL | 15:117db924cf7c | 364 | #endif |
| wolfSSL | 15:117db924cf7c | 365 | |
| wolfSSL | 15:117db924cf7c | 366 | typedef struct tcp_ready { |
| wolfSSL | 15:117db924cf7c | 367 | word16 ready; /* predicate */ |
| wolfSSL | 15:117db924cf7c | 368 | word16 port; |
| wolfSSL | 15:117db924cf7c | 369 | char* srfName; /* server ready file name */ |
| wolfSSL | 15:117db924cf7c | 370 | #if defined(_POSIX_THREADS) && !defined(__MINGW32__) |
| wolfSSL | 15:117db924cf7c | 371 | pthread_mutex_t mutex; |
| wolfSSL | 15:117db924cf7c | 372 | pthread_cond_t cond; |
| wolfSSL | 15:117db924cf7c | 373 | #endif |
| wolfSSL | 15:117db924cf7c | 374 | } tcp_ready; |
| wolfSSL | 15:117db924cf7c | 375 | |
| wolfSSL | 15:117db924cf7c | 376 | |
| wolfSSL | 15:117db924cf7c | 377 | static WC_INLINE void InitTcpReady(tcp_ready* ready) |
| wolfSSL | 15:117db924cf7c | 378 | { |
| wolfSSL | 15:117db924cf7c | 379 | ready->ready = 0; |
| wolfSSL | 15:117db924cf7c | 380 | ready->port = 0; |
| wolfSSL | 15:117db924cf7c | 381 | ready->srfName = NULL; |
| wolfSSL | 15:117db924cf7c | 382 | #ifdef SINGLE_THREADED |
| wolfSSL | 15:117db924cf7c | 383 | #elif defined(_POSIX_THREADS) && !defined(__MINGW32__) |
| wolfSSL | 15:117db924cf7c | 384 | pthread_mutex_init(&ready->mutex, 0); |
| wolfSSL | 15:117db924cf7c | 385 | pthread_cond_init(&ready->cond, 0); |
| wolfSSL | 15:117db924cf7c | 386 | #endif |
| wolfSSL | 15:117db924cf7c | 387 | } |
| wolfSSL | 15:117db924cf7c | 388 | |
| wolfSSL | 15:117db924cf7c | 389 | |
| wolfSSL | 15:117db924cf7c | 390 | static WC_INLINE void FreeTcpReady(tcp_ready* ready) |
| wolfSSL | 15:117db924cf7c | 391 | { |
| wolfSSL | 15:117db924cf7c | 392 | #ifdef SINGLE_THREADED |
| wolfSSL | 15:117db924cf7c | 393 | (void)ready; |
| wolfSSL | 15:117db924cf7c | 394 | #elif defined(_POSIX_THREADS) && !defined(__MINGW32__) |
| wolfSSL | 15:117db924cf7c | 395 | pthread_mutex_destroy(&ready->mutex); |
| wolfSSL | 15:117db924cf7c | 396 | pthread_cond_destroy(&ready->cond); |
| wolfSSL | 15:117db924cf7c | 397 | #else |
| wolfSSL | 15:117db924cf7c | 398 | (void)ready; |
| wolfSSL | 15:117db924cf7c | 399 | #endif |
| wolfSSL | 15:117db924cf7c | 400 | } |
| wolfSSL | 15:117db924cf7c | 401 | |
| wolfSSL | 15:117db924cf7c | 402 | typedef WOLFSSL_METHOD* (*method_provider)(void); |
| wolfSSL | 15:117db924cf7c | 403 | typedef void (*ctx_callback)(WOLFSSL_CTX* ctx); |
| wolfSSL | 15:117db924cf7c | 404 | typedef void (*ssl_callback)(WOLFSSL* ssl); |
| wolfSSL | 15:117db924cf7c | 405 | |
| wolfSSL | 15:117db924cf7c | 406 | typedef struct callback_functions { |
| wolfSSL | 15:117db924cf7c | 407 | method_provider method; |
| wolfSSL | 15:117db924cf7c | 408 | ctx_callback ctx_ready; |
| wolfSSL | 15:117db924cf7c | 409 | ssl_callback ssl_ready; |
| wolfSSL | 15:117db924cf7c | 410 | ssl_callback on_result; |
| wolfSSL | 16:8e0d178b1d1e | 411 | WOLFSSL_CTX* ctx; |
| wolfSSL | 15:117db924cf7c | 412 | } callback_functions; |
| wolfSSL | 15:117db924cf7c | 413 | |
| wolfSSL | 15:117db924cf7c | 414 | typedef struct func_args { |
| wolfSSL | 15:117db924cf7c | 415 | int argc; |
| wolfSSL | 15:117db924cf7c | 416 | char** argv; |
| wolfSSL | 15:117db924cf7c | 417 | int return_code; |
| wolfSSL | 15:117db924cf7c | 418 | tcp_ready* signal; |
| wolfSSL | 15:117db924cf7c | 419 | callback_functions *callbacks; |
| wolfSSL | 15:117db924cf7c | 420 | } func_args; |
| wolfSSL | 15:117db924cf7c | 421 | |
| wolfSSL | 15:117db924cf7c | 422 | |
| wolfSSL | 15:117db924cf7c | 423 | |
| wolfSSL | 15:117db924cf7c | 424 | |
| wolfSSL | 15:117db924cf7c | 425 | void wait_tcp_ready(func_args*); |
| wolfSSL | 15:117db924cf7c | 426 | |
| wolfSSL | 16:8e0d178b1d1e | 427 | #ifdef WOLFSSL_ZEPHYR |
| wolfSSL | 16:8e0d178b1d1e | 428 | typedef void THREAD_FUNC(void*, void*, void*); |
| wolfSSL | 16:8e0d178b1d1e | 429 | #else |
| wolfSSL | 15:117db924cf7c | 430 | typedef THREAD_RETURN WOLFSSL_THREAD THREAD_FUNC(void*); |
| wolfSSL | 16:8e0d178b1d1e | 431 | #endif |
| wolfSSL | 15:117db924cf7c | 432 | |
| wolfSSL | 15:117db924cf7c | 433 | void start_thread(THREAD_FUNC, func_args*, THREAD_TYPE*); |
| wolfSSL | 15:117db924cf7c | 434 | void join_thread(THREAD_TYPE); |
| wolfSSL | 15:117db924cf7c | 435 | |
| wolfSSL | 15:117db924cf7c | 436 | /* wolfSSL */ |
| wolfSSL | 15:117db924cf7c | 437 | #ifndef TEST_IPV6 |
| wolfSSL | 15:117db924cf7c | 438 | static const char* const wolfSSLIP = "127.0.0.1"; |
| wolfSSL | 15:117db924cf7c | 439 | #else |
| wolfSSL | 15:117db924cf7c | 440 | static const char* const wolfSSLIP = "::1"; |
| wolfSSL | 15:117db924cf7c | 441 | #endif |
| wolfSSL | 15:117db924cf7c | 442 | static const word16 wolfSSLPort = 11111; |
| wolfSSL | 15:117db924cf7c | 443 | |
| wolfSSL | 15:117db924cf7c | 444 | |
| wolfSSL | 16:8e0d178b1d1e | 445 | |
| wolfSSL | 16:8e0d178b1d1e | 446 | #ifndef MY_EX_USAGE |
| wolfSSL | 16:8e0d178b1d1e | 447 | #define MY_EX_USAGE 2 |
| wolfSSL | 16:8e0d178b1d1e | 448 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 449 | |
| wolfSSL | 16:8e0d178b1d1e | 450 | #ifndef EXIT_FAILURE |
| wolfSSL | 16:8e0d178b1d1e | 451 | #define EXIT_FAILURE 1 |
| wolfSSL | 16:8e0d178b1d1e | 452 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 453 | |
| wolfSSL | 16:8e0d178b1d1e | 454 | #if defined(WOLFSSL_FORCE_MALLOC_FAIL_TEST) || defined(WOLFSSL_ZEPHYR) |
| wolfSSL | 16:8e0d178b1d1e | 455 | #ifndef EXIT_SUCCESS |
| wolfSSL | 16:8e0d178b1d1e | 456 | #define EXIT_SUCCESS 0 |
| wolfSSL | 16:8e0d178b1d1e | 457 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 458 | #define XEXIT(rc) return rc |
| wolfSSL | 16:8e0d178b1d1e | 459 | #define XEXIT_T(rc) return (THREAD_RETURN)rc |
| wolfSSL | 16:8e0d178b1d1e | 460 | #else |
| wolfSSL | 16:8e0d178b1d1e | 461 | #define XEXIT(rc) exit((int)(rc)) |
| wolfSSL | 16:8e0d178b1d1e | 462 | #define XEXIT_T(rc) exit((int)(rc)) |
| wolfSSL | 16:8e0d178b1d1e | 463 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 464 | |
| wolfSSL | 16:8e0d178b1d1e | 465 | |
| wolfSSL | 16:8e0d178b1d1e | 466 | static WC_INLINE |
| wolfSSL | 16:8e0d178b1d1e | 467 | #if defined(WOLFSSL_FORCE_MALLOC_FAIL_TEST) || defined(WOLFSSL_ZEPHYR) |
| wolfSSL | 16:8e0d178b1d1e | 468 | THREAD_RETURN |
| wolfSSL | 16:8e0d178b1d1e | 469 | #else |
| wolfSSL | 16:8e0d178b1d1e | 470 | WC_NORETURN void |
| wolfSSL | 16:8e0d178b1d1e | 471 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 472 | err_sys(const char* msg) |
| wolfSSL | 15:117db924cf7c | 473 | { |
| wolfSSL | 15:117db924cf7c | 474 | printf("wolfSSL error: %s\n", msg); |
| wolfSSL | 15:117db924cf7c | 475 | |
| wolfSSL | 15:117db924cf7c | 476 | #if !defined(__GNUC__) |
| wolfSSL | 15:117db924cf7c | 477 | /* scan-build (which pretends to be gnuc) can get confused and think the |
| wolfSSL | 15:117db924cf7c | 478 | * msg pointer can be null even when hardcoded and then it won't exit, |
| wolfSSL | 15:117db924cf7c | 479 | * making null pointer checks above the err_sys() call useless. |
| wolfSSL | 15:117db924cf7c | 480 | * We could just always exit() but some compilers will complain about no |
| wolfSSL | 15:117db924cf7c | 481 | * possible return, with gcc we know the attribute to handle that with |
| wolfSSL | 15:117db924cf7c | 482 | * WC_NORETURN. */ |
| wolfSSL | 15:117db924cf7c | 483 | if (msg) |
| wolfSSL | 15:117db924cf7c | 484 | #endif |
| wolfSSL | 15:117db924cf7c | 485 | { |
| wolfSSL | 16:8e0d178b1d1e | 486 | XEXIT_T(EXIT_FAILURE); |
| wolfSSL | 15:117db924cf7c | 487 | } |
| wolfSSL | 15:117db924cf7c | 488 | } |
| wolfSSL | 15:117db924cf7c | 489 | |
| wolfSSL | 15:117db924cf7c | 490 | |
| wolfSSL | 15:117db924cf7c | 491 | extern int myoptind; |
| wolfSSL | 15:117db924cf7c | 492 | extern char* myoptarg; |
| wolfSSL | 15:117db924cf7c | 493 | |
| wolfSSL | 15:117db924cf7c | 494 | static WC_INLINE int mygetopt(int argc, char** argv, const char* optstring) |
| wolfSSL | 15:117db924cf7c | 495 | { |
| wolfSSL | 15:117db924cf7c | 496 | static char* next = NULL; |
| wolfSSL | 15:117db924cf7c | 497 | |
| wolfSSL | 15:117db924cf7c | 498 | char c; |
| wolfSSL | 15:117db924cf7c | 499 | char* cp; |
| wolfSSL | 15:117db924cf7c | 500 | |
| wolfSSL | 16:8e0d178b1d1e | 501 | /* Added sanity check becuase scan-build complains argv[myoptind] access |
| wolfSSL | 16:8e0d178b1d1e | 502 | * results in a null pointer dereference. */ |
| wolfSSL | 16:8e0d178b1d1e | 503 | if (argv == NULL) { |
| wolfSSL | 16:8e0d178b1d1e | 504 | myoptarg = NULL; |
| wolfSSL | 16:8e0d178b1d1e | 505 | return -1; |
| wolfSSL | 16:8e0d178b1d1e | 506 | } |
| wolfSSL | 16:8e0d178b1d1e | 507 | |
| wolfSSL | 15:117db924cf7c | 508 | if (myoptind == 0) |
| wolfSSL | 15:117db924cf7c | 509 | next = NULL; /* we're starting new/over */ |
| wolfSSL | 15:117db924cf7c | 510 | |
| wolfSSL | 15:117db924cf7c | 511 | if (next == NULL || *next == '\0') { |
| wolfSSL | 15:117db924cf7c | 512 | if (myoptind == 0) |
| wolfSSL | 15:117db924cf7c | 513 | myoptind++; |
| wolfSSL | 15:117db924cf7c | 514 | |
| wolfSSL | 16:8e0d178b1d1e | 515 | if (myoptind >= argc || argv[myoptind] == NULL || |
| wolfSSL | 16:8e0d178b1d1e | 516 | argv[myoptind][0] != '-' || argv[myoptind][1] == '\0') { |
| wolfSSL | 15:117db924cf7c | 517 | myoptarg = NULL; |
| wolfSSL | 15:117db924cf7c | 518 | if (myoptind < argc) |
| wolfSSL | 15:117db924cf7c | 519 | myoptarg = argv[myoptind]; |
| wolfSSL | 15:117db924cf7c | 520 | |
| wolfSSL | 15:117db924cf7c | 521 | return -1; |
| wolfSSL | 15:117db924cf7c | 522 | } |
| wolfSSL | 15:117db924cf7c | 523 | |
| wolfSSL | 15:117db924cf7c | 524 | if (strcmp(argv[myoptind], "--") == 0) { |
| wolfSSL | 15:117db924cf7c | 525 | myoptind++; |
| wolfSSL | 15:117db924cf7c | 526 | myoptarg = NULL; |
| wolfSSL | 15:117db924cf7c | 527 | |
| wolfSSL | 15:117db924cf7c | 528 | if (myoptind < argc) |
| wolfSSL | 15:117db924cf7c | 529 | myoptarg = argv[myoptind]; |
| wolfSSL | 15:117db924cf7c | 530 | |
| wolfSSL | 15:117db924cf7c | 531 | return -1; |
| wolfSSL | 15:117db924cf7c | 532 | } |
| wolfSSL | 15:117db924cf7c | 533 | |
| wolfSSL | 15:117db924cf7c | 534 | next = argv[myoptind]; |
| wolfSSL | 15:117db924cf7c | 535 | next++; /* skip - */ |
| wolfSSL | 15:117db924cf7c | 536 | myoptind++; |
| wolfSSL | 15:117db924cf7c | 537 | } |
| wolfSSL | 15:117db924cf7c | 538 | |
| wolfSSL | 15:117db924cf7c | 539 | c = *next++; |
| wolfSSL | 15:117db924cf7c | 540 | /* The C++ strchr can return a different value */ |
| wolfSSL | 15:117db924cf7c | 541 | cp = (char*)strchr(optstring, c); |
| wolfSSL | 15:117db924cf7c | 542 | |
| wolfSSL | 15:117db924cf7c | 543 | if (cp == NULL || c == ':') |
| wolfSSL | 15:117db924cf7c | 544 | return '?'; |
| wolfSSL | 15:117db924cf7c | 545 | |
| wolfSSL | 15:117db924cf7c | 546 | cp++; |
| wolfSSL | 15:117db924cf7c | 547 | |
| wolfSSL | 15:117db924cf7c | 548 | if (*cp == ':') { |
| wolfSSL | 15:117db924cf7c | 549 | if (*next != '\0') { |
| wolfSSL | 15:117db924cf7c | 550 | myoptarg = next; |
| wolfSSL | 15:117db924cf7c | 551 | next = NULL; |
| wolfSSL | 15:117db924cf7c | 552 | } |
| wolfSSL | 15:117db924cf7c | 553 | else if (myoptind < argc) { |
| wolfSSL | 15:117db924cf7c | 554 | myoptarg = argv[myoptind]; |
| wolfSSL | 15:117db924cf7c | 555 | myoptind++; |
| wolfSSL | 15:117db924cf7c | 556 | } |
| wolfSSL | 15:117db924cf7c | 557 | else |
| wolfSSL | 15:117db924cf7c | 558 | return '?'; |
| wolfSSL | 15:117db924cf7c | 559 | } |
| wolfSSL | 15:117db924cf7c | 560 | |
| wolfSSL | 15:117db924cf7c | 561 | return c; |
| wolfSSL | 15:117db924cf7c | 562 | } |
| wolfSSL | 15:117db924cf7c | 563 | |
| wolfSSL | 15:117db924cf7c | 564 | |
| wolfSSL | 15:117db924cf7c | 565 | #ifdef WOLFSSL_ENCRYPTED_KEYS |
| wolfSSL | 15:117db924cf7c | 566 | |
| wolfSSL | 15:117db924cf7c | 567 | static WC_INLINE int PasswordCallBack(char* passwd, int sz, int rw, void* userdata) |
| wolfSSL | 15:117db924cf7c | 568 | { |
| wolfSSL | 15:117db924cf7c | 569 | (void)rw; |
| wolfSSL | 15:117db924cf7c | 570 | (void)userdata; |
| wolfSSL | 15:117db924cf7c | 571 | if (userdata != NULL) { |
| wolfSSL | 15:117db924cf7c | 572 | strncpy(passwd, (char*)userdata, sz); |
| wolfSSL | 15:117db924cf7c | 573 | return (int)XSTRLEN((char*)userdata); |
| wolfSSL | 15:117db924cf7c | 574 | } |
| wolfSSL | 15:117db924cf7c | 575 | else { |
| wolfSSL | 15:117db924cf7c | 576 | strncpy(passwd, "yassl123", sz); |
| wolfSSL | 15:117db924cf7c | 577 | return 8; |
| wolfSSL | 15:117db924cf7c | 578 | } |
| wolfSSL | 15:117db924cf7c | 579 | } |
| wolfSSL | 15:117db924cf7c | 580 | |
| wolfSSL | 15:117db924cf7c | 581 | #endif |
| wolfSSL | 15:117db924cf7c | 582 | |
| wolfSSL | 16:8e0d178b1d1e | 583 | static const char* client_showpeer_msg[][8] = { |
| wolfSSL | 16:8e0d178b1d1e | 584 | /* English */ |
| wolfSSL | 16:8e0d178b1d1e | 585 | { |
| wolfSSL | 16:8e0d178b1d1e | 586 | "SSL version is", |
| wolfSSL | 16:8e0d178b1d1e | 587 | "SSL cipher suite is", |
| wolfSSL | 16:8e0d178b1d1e | 588 | "SSL curve name is", |
| wolfSSL | 16:8e0d178b1d1e | 589 | "SSL DH size is", |
| wolfSSL | 16:8e0d178b1d1e | 590 | "SSL reused session", |
| wolfSSL | 16:8e0d178b1d1e | 591 | "Alternate cert chain used", |
| wolfSSL | 16:8e0d178b1d1e | 592 | "peer's cert info:", |
| wolfSSL | 16:8e0d178b1d1e | 593 | NULL |
| wolfSSL | 16:8e0d178b1d1e | 594 | }, |
| wolfSSL | 16:8e0d178b1d1e | 595 | #ifndef NO_MULTIBYTE_PRINT |
| wolfSSL | 16:8e0d178b1d1e | 596 | /* Japanese */ |
| wolfSSL | 16:8e0d178b1d1e | 597 | { |
| wolfSSL | 16:8e0d178b1d1e | 598 | "SSL バージョンは", |
| wolfSSL | 16:8e0d178b1d1e | 599 | "SSL 暗号スイートは", |
| wolfSSL | 16:8e0d178b1d1e | 600 | "SSL 曲線名は", |
| wolfSSL | 16:8e0d178b1d1e | 601 | "SSL DH サイズは", |
| wolfSSL | 16:8e0d178b1d1e | 602 | "SSL 再利用セッション", |
| wolfSSL | 16:8e0d178b1d1e | 603 | "代替証明チェーンを使用", |
| wolfSSL | 16:8e0d178b1d1e | 604 | "相手方証明書情報", |
| wolfSSL | 16:8e0d178b1d1e | 605 | NULL |
| wolfSSL | 16:8e0d178b1d1e | 606 | }, |
| wolfSSL | 16:8e0d178b1d1e | 607 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 608 | }; |
| wolfSSL | 16:8e0d178b1d1e | 609 | |
| wolfSSL | 16:8e0d178b1d1e | 610 | #if defined(KEEP_PEER_CERT) || defined(KEEP_OUR_CERT) || defined(SESSION_CERTS) |
| wolfSSL | 16:8e0d178b1d1e | 611 | static const char* client_showx509_msg[][5] = { |
| wolfSSL | 16:8e0d178b1d1e | 612 | /* English */ |
| wolfSSL | 16:8e0d178b1d1e | 613 | { |
| wolfSSL | 16:8e0d178b1d1e | 614 | "issuer", |
| wolfSSL | 16:8e0d178b1d1e | 615 | "subject", |
| wolfSSL | 16:8e0d178b1d1e | 616 | "altname", |
| wolfSSL | 16:8e0d178b1d1e | 617 | "serial number", |
| wolfSSL | 16:8e0d178b1d1e | 618 | NULL |
| wolfSSL | 16:8e0d178b1d1e | 619 | }, |
| wolfSSL | 16:8e0d178b1d1e | 620 | #ifndef NO_MULTIBYTE_PRINT |
| wolfSSL | 16:8e0d178b1d1e | 621 | /* Japanese */ |
| wolfSSL | 16:8e0d178b1d1e | 622 | { |
| wolfSSL | 16:8e0d178b1d1e | 623 | "発行者", |
| wolfSSL | 16:8e0d178b1d1e | 624 | "サブジェクト", |
| wolfSSL | 16:8e0d178b1d1e | 625 | "代替名", |
| wolfSSL | 16:8e0d178b1d1e | 626 | "シリアル番号", |
| wolfSSL | 16:8e0d178b1d1e | 627 | NULL |
| wolfSSL | 16:8e0d178b1d1e | 628 | }, |
| wolfSSL | 16:8e0d178b1d1e | 629 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 630 | }; |
| wolfSSL | 16:8e0d178b1d1e | 631 | |
| wolfSSL | 16:8e0d178b1d1e | 632 | /* lng_index is to specify the language for displaying message. */ |
| wolfSSL | 16:8e0d178b1d1e | 633 | /* 0:English, 1:Japanese */ |
| wolfSSL | 16:8e0d178b1d1e | 634 | static WC_INLINE void ShowX509Ex(WOLFSSL_X509* x509, const char* hdr, |
| wolfSSL | 16:8e0d178b1d1e | 635 | int lng_index) |
| wolfSSL | 15:117db924cf7c | 636 | { |
| wolfSSL | 15:117db924cf7c | 637 | char* altName; |
| wolfSSL | 15:117db924cf7c | 638 | char* issuer; |
| wolfSSL | 15:117db924cf7c | 639 | char* subject; |
| wolfSSL | 15:117db924cf7c | 640 | byte serial[32]; |
| wolfSSL | 15:117db924cf7c | 641 | int ret; |
| wolfSSL | 15:117db924cf7c | 642 | int sz = sizeof(serial); |
| wolfSSL | 16:8e0d178b1d1e | 643 | const char** words = client_showx509_msg[lng_index]; |
| wolfSSL | 15:117db924cf7c | 644 | |
| wolfSSL | 15:117db924cf7c | 645 | if (x509 == NULL) { |
| wolfSSL | 15:117db924cf7c | 646 | printf("%s No Cert\n", hdr); |
| wolfSSL | 15:117db924cf7c | 647 | return; |
| wolfSSL | 15:117db924cf7c | 648 | } |
| wolfSSL | 15:117db924cf7c | 649 | |
| wolfSSL | 15:117db924cf7c | 650 | issuer = wolfSSL_X509_NAME_oneline( |
| wolfSSL | 15:117db924cf7c | 651 | wolfSSL_X509_get_issuer_name(x509), 0, 0); |
| wolfSSL | 15:117db924cf7c | 652 | subject = wolfSSL_X509_NAME_oneline( |
| wolfSSL | 15:117db924cf7c | 653 | wolfSSL_X509_get_subject_name(x509), 0, 0); |
| wolfSSL | 15:117db924cf7c | 654 | |
| wolfSSL | 16:8e0d178b1d1e | 655 | printf("%s\n %s : %s\n %s: %s\n", hdr, words[0], issuer, words[1], subject); |
| wolfSSL | 15:117db924cf7c | 656 | |
| wolfSSL | 15:117db924cf7c | 657 | while ( (altName = wolfSSL_X509_get_next_altname(x509)) != NULL) |
| wolfSSL | 16:8e0d178b1d1e | 658 | printf(" %s = %s\n", words[2], altName); |
| wolfSSL | 15:117db924cf7c | 659 | |
| wolfSSL | 15:117db924cf7c | 660 | ret = wolfSSL_X509_get_serial_number(x509, serial, &sz); |
| wolfSSL | 15:117db924cf7c | 661 | if (ret == WOLFSSL_SUCCESS) { |
| wolfSSL | 15:117db924cf7c | 662 | int i; |
| wolfSSL | 15:117db924cf7c | 663 | int strLen; |
| wolfSSL | 15:117db924cf7c | 664 | char serialMsg[80]; |
| wolfSSL | 15:117db924cf7c | 665 | |
| wolfSSL | 15:117db924cf7c | 666 | /* testsuite has multiple threads writing to stdout, get output |
| wolfSSL | 15:117db924cf7c | 667 | message ready to write once */ |
| wolfSSL | 16:8e0d178b1d1e | 668 | strLen = sprintf(serialMsg, " %s", words[3]); |
| wolfSSL | 15:117db924cf7c | 669 | for (i = 0; i < sz; i++) |
| wolfSSL | 15:117db924cf7c | 670 | sprintf(serialMsg + strLen + (i*3), ":%02x ", serial[i]); |
| wolfSSL | 15:117db924cf7c | 671 | printf("%s\n", serialMsg); |
| wolfSSL | 15:117db924cf7c | 672 | } |
| wolfSSL | 15:117db924cf7c | 673 | |
| wolfSSL | 15:117db924cf7c | 674 | XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL); |
| wolfSSL | 15:117db924cf7c | 675 | XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL); |
| wolfSSL | 15:117db924cf7c | 676 | |
| wolfSSL | 16:8e0d178b1d1e | 677 | #if defined(SHOW_CERTS) && defined(OPENSSL_EXTRA) |
| wolfSSL | 15:117db924cf7c | 678 | { |
| wolfSSL | 15:117db924cf7c | 679 | WOLFSSL_BIO* bio; |
| wolfSSL | 15:117db924cf7c | 680 | char buf[256]; /* should be size of ASN_NAME_MAX */ |
| wolfSSL | 15:117db924cf7c | 681 | int textSz; |
| wolfSSL | 15:117db924cf7c | 682 | |
| wolfSSL | 15:117db924cf7c | 683 | /* print out domain component if certificate has it */ |
| wolfSSL | 15:117db924cf7c | 684 | textSz = wolfSSL_X509_NAME_get_text_by_NID( |
| wolfSSL | 15:117db924cf7c | 685 | wolfSSL_X509_get_subject_name(x509), NID_domainComponent, |
| wolfSSL | 15:117db924cf7c | 686 | buf, sizeof(buf)); |
| wolfSSL | 15:117db924cf7c | 687 | if (textSz > 0) { |
| wolfSSL | 15:117db924cf7c | 688 | printf("Domain Component = %s\n", buf); |
| wolfSSL | 15:117db924cf7c | 689 | } |
| wolfSSL | 15:117db924cf7c | 690 | |
| wolfSSL | 15:117db924cf7c | 691 | bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file()); |
| wolfSSL | 15:117db924cf7c | 692 | if (bio != NULL) { |
| wolfSSL | 15:117db924cf7c | 693 | wolfSSL_BIO_set_fp(bio, stdout, BIO_NOCLOSE); |
| wolfSSL | 15:117db924cf7c | 694 | wolfSSL_X509_print(bio, x509); |
| wolfSSL | 15:117db924cf7c | 695 | wolfSSL_BIO_free(bio); |
| wolfSSL | 15:117db924cf7c | 696 | } |
| wolfSSL | 15:117db924cf7c | 697 | } |
| wolfSSL | 16:8e0d178b1d1e | 698 | #endif /* SHOW_CERTS && OPENSSL_EXTRA */ |
| wolfSSL | 15:117db924cf7c | 699 | } |
| wolfSSL | 16:8e0d178b1d1e | 700 | /* original ShowX509 to maintain compatibility */ |
| wolfSSL | 16:8e0d178b1d1e | 701 | static WC_INLINE void ShowX509(WOLFSSL_X509* x509, const char* hdr) |
| wolfSSL | 16:8e0d178b1d1e | 702 | { |
| wolfSSL | 16:8e0d178b1d1e | 703 | ShowX509Ex(x509, hdr, 0); |
| wolfSSL | 16:8e0d178b1d1e | 704 | } |
| wolfSSL | 16:8e0d178b1d1e | 705 | |
| wolfSSL | 16:8e0d178b1d1e | 706 | #endif /* KEEP_PEER_CERT || KEEP_OUR_CERT || SESSION_CERTS */ |
| wolfSSL | 16:8e0d178b1d1e | 707 | |
| wolfSSL | 16:8e0d178b1d1e | 708 | #if defined(SHOW_CERTS) && defined(SESSION_CERTS) && \ |
| wolfSSL | 16:8e0d178b1d1e | 709 | (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) |
| wolfSSL | 15:117db924cf7c | 710 | static WC_INLINE void ShowX509Chain(WOLFSSL_X509_CHAIN* chain, int count, |
| wolfSSL | 15:117db924cf7c | 711 | const char* hdr) |
| wolfSSL | 15:117db924cf7c | 712 | { |
| wolfSSL | 15:117db924cf7c | 713 | int i; |
| wolfSSL | 15:117db924cf7c | 714 | int length; |
| wolfSSL | 15:117db924cf7c | 715 | unsigned char buffer[3072]; |
| wolfSSL | 15:117db924cf7c | 716 | WOLFSSL_X509* chainX509; |
| wolfSSL | 15:117db924cf7c | 717 | |
| wolfSSL | 15:117db924cf7c | 718 | for (i = 0; i < count; i++) { |
| wolfSSL | 15:117db924cf7c | 719 | wolfSSL_get_chain_cert_pem(chain, i, buffer, sizeof(buffer), &length); |
| wolfSSL | 15:117db924cf7c | 720 | buffer[length] = 0; |
| wolfSSL | 15:117db924cf7c | 721 | printf("\n%s: %d has length %d data = \n%s\n", hdr, i, length, buffer); |
| wolfSSL | 15:117db924cf7c | 722 | |
| wolfSSL | 15:117db924cf7c | 723 | chainX509 = wolfSSL_get_chain_X509(chain, i); |
| wolfSSL | 15:117db924cf7c | 724 | if (chainX509) |
| wolfSSL | 15:117db924cf7c | 725 | ShowX509(chainX509, hdr); |
| wolfSSL | 15:117db924cf7c | 726 | else |
| wolfSSL | 15:117db924cf7c | 727 | printf("get_chain_X509 failed\n"); |
| wolfSSL | 15:117db924cf7c | 728 | wolfSSL_FreeX509(chainX509); |
| wolfSSL | 15:117db924cf7c | 729 | } |
| wolfSSL | 15:117db924cf7c | 730 | } |
| wolfSSL | 16:8e0d178b1d1e | 731 | #endif /* SHOW_CERTS && SESSION_CERTS */ |
| wolfSSL | 16:8e0d178b1d1e | 732 | |
| wolfSSL | 16:8e0d178b1d1e | 733 | /* lng_index is to specify the language for displaying message. */ |
| wolfSSL | 16:8e0d178b1d1e | 734 | /* 0:English, 1:Japanese */ |
| wolfSSL | 16:8e0d178b1d1e | 735 | static WC_INLINE void showPeerEx(WOLFSSL* ssl, int lng_index) |
| wolfSSL | 15:117db924cf7c | 736 | { |
| wolfSSL | 15:117db924cf7c | 737 | WOLFSSL_CIPHER* cipher; |
| wolfSSL | 16:8e0d178b1d1e | 738 | const char** words = client_showpeer_msg[lng_index]; |
| wolfSSL | 16:8e0d178b1d1e | 739 | |
| wolfSSL | 16:8e0d178b1d1e | 740 | #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448) || \ |
| wolfSSL | 16:8e0d178b1d1e | 741 | !defined(NO_DH) |
| wolfSSL | 15:117db924cf7c | 742 | const char *name; |
| wolfSSL | 15:117db924cf7c | 743 | #endif |
| wolfSSL | 15:117db924cf7c | 744 | #ifndef NO_DH |
| wolfSSL | 15:117db924cf7c | 745 | int bits; |
| wolfSSL | 15:117db924cf7c | 746 | #endif |
| wolfSSL | 15:117db924cf7c | 747 | #ifdef KEEP_PEER_CERT |
| wolfSSL | 15:117db924cf7c | 748 | WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl); |
| wolfSSL | 15:117db924cf7c | 749 | if (peer) |
| wolfSSL | 16:8e0d178b1d1e | 750 | ShowX509Ex(peer, words[6], lng_index); |
| wolfSSL | 15:117db924cf7c | 751 | else |
| wolfSSL | 15:117db924cf7c | 752 | printf("peer has no cert!\n"); |
| wolfSSL | 15:117db924cf7c | 753 | wolfSSL_FreeX509(peer); |
| wolfSSL | 15:117db924cf7c | 754 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 755 | #if defined(SHOW_CERTS) && defined(KEEP_OUR_CERT) && \ |
| wolfSSL | 16:8e0d178b1d1e | 756 | (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) |
| wolfSSL | 15:117db924cf7c | 757 | ShowX509(wolfSSL_get_certificate(ssl), "our cert info:"); |
| wolfSSL | 15:117db924cf7c | 758 | printf("Peer verify result = %lu\n", wolfSSL_get_verify_result(ssl)); |
| wolfSSL | 16:8e0d178b1d1e | 759 | #endif /* SHOW_CERTS && KEEP_OUR_CERT */ |
| wolfSSL | 16:8e0d178b1d1e | 760 | printf("%s %s\n", words[0], wolfSSL_get_version(ssl)); |
| wolfSSL | 15:117db924cf7c | 761 | |
| wolfSSL | 15:117db924cf7c | 762 | cipher = wolfSSL_get_current_cipher(ssl); |
| wolfSSL | 15:117db924cf7c | 763 | #ifdef HAVE_QSH |
| wolfSSL | 16:8e0d178b1d1e | 764 | printf("%s %s%s\n", words[1], (wolfSSL_isQSH(ssl))? "QSH:": "", |
| wolfSSL | 15:117db924cf7c | 765 | wolfSSL_CIPHER_get_name(cipher)); |
| wolfSSL | 15:117db924cf7c | 766 | #else |
| wolfSSL | 16:8e0d178b1d1e | 767 | printf("%s %s\n", words[1], wolfSSL_CIPHER_get_name(cipher)); |
| wolfSSL | 15:117db924cf7c | 768 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 769 | #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448) || \ |
| wolfSSL | 16:8e0d178b1d1e | 770 | !defined(NO_DH) |
| wolfSSL | 15:117db924cf7c | 771 | if ((name = wolfSSL_get_curve_name(ssl)) != NULL) |
| wolfSSL | 16:8e0d178b1d1e | 772 | printf("%s %s\n", words[2], name); |
| wolfSSL | 15:117db924cf7c | 773 | #endif |
| wolfSSL | 15:117db924cf7c | 774 | #ifndef NO_DH |
| wolfSSL | 16:8e0d178b1d1e | 775 | else if ((bits = wolfSSL_GetDhKey_Sz(ssl)) > 0) |
| wolfSSL | 16:8e0d178b1d1e | 776 | printf("%s %d bits\n", words[3], bits); |
| wolfSSL | 15:117db924cf7c | 777 | #endif |
| wolfSSL | 15:117db924cf7c | 778 | if (wolfSSL_session_reused(ssl)) |
| wolfSSL | 16:8e0d178b1d1e | 779 | printf("%s\n", words[4]); |
| wolfSSL | 15:117db924cf7c | 780 | #ifdef WOLFSSL_ALT_CERT_CHAINS |
| wolfSSL | 15:117db924cf7c | 781 | if (wolfSSL_is_peer_alt_cert_chain(ssl)) |
| wolfSSL | 16:8e0d178b1d1e | 782 | printf("%s\n", words[5]); |
| wolfSSL | 15:117db924cf7c | 783 | #endif |
| wolfSSL | 15:117db924cf7c | 784 | |
| wolfSSL | 16:8e0d178b1d1e | 785 | #if defined(SHOW_CERTS) && defined(SESSION_CERTS) && \ |
| wolfSSL | 16:8e0d178b1d1e | 786 | (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) |
| wolfSSL | 15:117db924cf7c | 787 | { |
| wolfSSL | 15:117db924cf7c | 788 | WOLFSSL_X509_CHAIN* chain; |
| wolfSSL | 15:117db924cf7c | 789 | |
| wolfSSL | 15:117db924cf7c | 790 | chain = wolfSSL_get_peer_chain(ssl); |
| wolfSSL | 15:117db924cf7c | 791 | ShowX509Chain(chain, wolfSSL_get_chain_count(chain), "session cert"); |
| wolfSSL | 15:117db924cf7c | 792 | |
| wolfSSL | 15:117db924cf7c | 793 | #ifdef WOLFSSL_ALT_CERT_CHAINS |
| wolfSSL | 15:117db924cf7c | 794 | if (wolfSSL_is_peer_alt_cert_chain(ssl)) { |
| wolfSSL | 15:117db924cf7c | 795 | chain = wolfSSL_get_peer_alt_chain(ssl); |
| wolfSSL | 15:117db924cf7c | 796 | ShowX509Chain(chain, wolfSSL_get_chain_count(chain), "alt cert"); |
| wolfSSL | 15:117db924cf7c | 797 | } |
| wolfSSL | 15:117db924cf7c | 798 | #endif |
| wolfSSL | 15:117db924cf7c | 799 | } |
| wolfSSL | 16:8e0d178b1d1e | 800 | #endif /* SHOW_CERTS && SESSION_CERTS */ |
| wolfSSL | 15:117db924cf7c | 801 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 802 | } |
| wolfSSL | 16:8e0d178b1d1e | 803 | /* original showPeer to maintain compatibility */ |
| wolfSSL | 16:8e0d178b1d1e | 804 | static WC_INLINE void showPeer(WOLFSSL* ssl) |
| wolfSSL | 16:8e0d178b1d1e | 805 | { |
| wolfSSL | 16:8e0d178b1d1e | 806 | showPeerEx(ssl, 0); |
| wolfSSL | 16:8e0d178b1d1e | 807 | } |
| wolfSSL | 15:117db924cf7c | 808 | |
| wolfSSL | 15:117db924cf7c | 809 | static WC_INLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer, |
| wolfSSL | 15:117db924cf7c | 810 | word16 port, int udp, int sctp) |
| wolfSSL | 15:117db924cf7c | 811 | { |
| wolfSSL | 15:117db924cf7c | 812 | int useLookup = 0; |
| wolfSSL | 15:117db924cf7c | 813 | (void)useLookup; |
| wolfSSL | 15:117db924cf7c | 814 | (void)udp; |
| wolfSSL | 15:117db924cf7c | 815 | (void)sctp; |
| wolfSSL | 15:117db924cf7c | 816 | |
| wolfSSL | 15:117db924cf7c | 817 | if (addr == NULL) |
| wolfSSL | 15:117db924cf7c | 818 | err_sys("invalid argument to build_addr, addr is NULL"); |
| wolfSSL | 15:117db924cf7c | 819 | |
| wolfSSL | 15:117db924cf7c | 820 | XMEMSET(addr, 0, sizeof(SOCKADDR_IN_T)); |
| wolfSSL | 15:117db924cf7c | 821 | |
| wolfSSL | 15:117db924cf7c | 822 | #ifndef TEST_IPV6 |
| wolfSSL | 15:117db924cf7c | 823 | /* peer could be in human readable form */ |
| wolfSSL | 15:117db924cf7c | 824 | if ( ((size_t)peer != INADDR_ANY) && isalpha((int)peer[0])) { |
| wolfSSL | 16:8e0d178b1d1e | 825 | #ifndef WOLFSSL_USE_GETADDRINFO |
| wolfSSL | 15:117db924cf7c | 826 | #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) |
| wolfSSL | 15:117db924cf7c | 827 | int err; |
| wolfSSL | 15:117db924cf7c | 828 | struct hostent* entry = gethostbyname(peer, &err); |
| wolfSSL | 15:117db924cf7c | 829 | #elif defined(WOLFSSL_TIRTOS) |
| wolfSSL | 15:117db924cf7c | 830 | struct hostent* entry = DNSGetHostByName(peer); |
| wolfSSL | 15:117db924cf7c | 831 | #elif defined(WOLFSSL_VXWORKS) |
| wolfSSL | 15:117db924cf7c | 832 | struct hostent* entry = (struct hostent*)hostGetByName((char*)peer); |
| wolfSSL | 15:117db924cf7c | 833 | #else |
| wolfSSL | 15:117db924cf7c | 834 | struct hostent* entry = gethostbyname(peer); |
| wolfSSL | 15:117db924cf7c | 835 | #endif |
| wolfSSL | 15:117db924cf7c | 836 | |
| wolfSSL | 15:117db924cf7c | 837 | if (entry) { |
| wolfSSL | 15:117db924cf7c | 838 | XMEMCPY(&addr->sin_addr.s_addr, entry->h_addr_list[0], |
| wolfSSL | 15:117db924cf7c | 839 | entry->h_length); |
| wolfSSL | 15:117db924cf7c | 840 | useLookup = 1; |
| wolfSSL | 15:117db924cf7c | 841 | } |
| wolfSSL | 16:8e0d178b1d1e | 842 | #else |
| wolfSSL | 16:8e0d178b1d1e | 843 | struct zsock_addrinfo hints, *addrInfo; |
| wolfSSL | 16:8e0d178b1d1e | 844 | char portStr[6]; |
| wolfSSL | 16:8e0d178b1d1e | 845 | XSNPRINTF(portStr, sizeof(portStr), "%d", port); |
| wolfSSL | 16:8e0d178b1d1e | 846 | memset(&hints, 0, sizeof(hints)); |
| wolfSSL | 16:8e0d178b1d1e | 847 | hints.ai_family = AF_UNSPEC; |
| wolfSSL | 16:8e0d178b1d1e | 848 | hints.ai_socktype = udp ? SOCK_DGRAM : SOCK_STREAM; |
| wolfSSL | 16:8e0d178b1d1e | 849 | hints.ai_protocol = udp ? IPPROTO_UDP : IPPROTO_TCP; |
| wolfSSL | 16:8e0d178b1d1e | 850 | if (getaddrinfo((char*)peer, portStr, &hints, &addrInfo) == 0) { |
| wolfSSL | 16:8e0d178b1d1e | 851 | XMEMCPY(addr, addrInfo->ai_addr, sizeof(*addr)); |
| wolfSSL | 16:8e0d178b1d1e | 852 | useLookup = 1; |
| wolfSSL | 16:8e0d178b1d1e | 853 | } |
| wolfSSL | 16:8e0d178b1d1e | 854 | #endif |
| wolfSSL | 15:117db924cf7c | 855 | else |
| wolfSSL | 15:117db924cf7c | 856 | err_sys("no entry for host"); |
| wolfSSL | 15:117db924cf7c | 857 | } |
| wolfSSL | 15:117db924cf7c | 858 | #endif |
| wolfSSL | 15:117db924cf7c | 859 | |
| wolfSSL | 15:117db924cf7c | 860 | |
| wolfSSL | 15:117db924cf7c | 861 | #ifndef TEST_IPV6 |
| wolfSSL | 15:117db924cf7c | 862 | #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) |
| wolfSSL | 15:117db924cf7c | 863 | addr->sin_family = PF_INET; |
| wolfSSL | 15:117db924cf7c | 864 | #else |
| wolfSSL | 15:117db924cf7c | 865 | addr->sin_family = AF_INET_V; |
| wolfSSL | 15:117db924cf7c | 866 | #endif |
| wolfSSL | 15:117db924cf7c | 867 | addr->sin_port = XHTONS(port); |
| wolfSSL | 15:117db924cf7c | 868 | if ((size_t)peer == INADDR_ANY) |
| wolfSSL | 15:117db924cf7c | 869 | addr->sin_addr.s_addr = INADDR_ANY; |
| wolfSSL | 15:117db924cf7c | 870 | else { |
| wolfSSL | 15:117db924cf7c | 871 | if (!useLookup) |
| wolfSSL | 15:117db924cf7c | 872 | addr->sin_addr.s_addr = inet_addr(peer); |
| wolfSSL | 15:117db924cf7c | 873 | } |
| wolfSSL | 15:117db924cf7c | 874 | #else |
| wolfSSL | 15:117db924cf7c | 875 | addr->sin6_family = AF_INET_V; |
| wolfSSL | 15:117db924cf7c | 876 | addr->sin6_port = XHTONS(port); |
| wolfSSL | 15:117db924cf7c | 877 | if ((size_t)peer == INADDR_ANY) { |
| wolfSSL | 15:117db924cf7c | 878 | addr->sin6_addr = in6addr_any; |
| wolfSSL | 15:117db924cf7c | 879 | } |
| wolfSSL | 15:117db924cf7c | 880 | else { |
| wolfSSL | 16:8e0d178b1d1e | 881 | #if defined(HAVE_GETADDRINFO) || defined(WOLF_C99) |
| wolfSSL | 15:117db924cf7c | 882 | struct addrinfo hints; |
| wolfSSL | 15:117db924cf7c | 883 | struct addrinfo* answer = NULL; |
| wolfSSL | 15:117db924cf7c | 884 | int ret; |
| wolfSSL | 15:117db924cf7c | 885 | char strPort[80]; |
| wolfSSL | 15:117db924cf7c | 886 | |
| wolfSSL | 15:117db924cf7c | 887 | XMEMSET(&hints, 0, sizeof(hints)); |
| wolfSSL | 15:117db924cf7c | 888 | |
| wolfSSL | 15:117db924cf7c | 889 | hints.ai_family = AF_INET_V; |
| wolfSSL | 15:117db924cf7c | 890 | if (udp) { |
| wolfSSL | 15:117db924cf7c | 891 | hints.ai_socktype = SOCK_DGRAM; |
| wolfSSL | 15:117db924cf7c | 892 | hints.ai_protocol = IPPROTO_UDP; |
| wolfSSL | 15:117db924cf7c | 893 | } |
| wolfSSL | 15:117db924cf7c | 894 | #ifdef WOLFSSL_SCTP |
| wolfSSL | 15:117db924cf7c | 895 | else if (sctp) { |
| wolfSSL | 15:117db924cf7c | 896 | hints.ai_socktype = SOCK_STREAM; |
| wolfSSL | 15:117db924cf7c | 897 | hints.ai_protocol = IPPROTO_SCTP; |
| wolfSSL | 15:117db924cf7c | 898 | } |
| wolfSSL | 15:117db924cf7c | 899 | #endif |
| wolfSSL | 15:117db924cf7c | 900 | else { |
| wolfSSL | 15:117db924cf7c | 901 | hints.ai_socktype = SOCK_STREAM; |
| wolfSSL | 15:117db924cf7c | 902 | hints.ai_protocol = IPPROTO_TCP; |
| wolfSSL | 15:117db924cf7c | 903 | } |
| wolfSSL | 15:117db924cf7c | 904 | |
| wolfSSL | 15:117db924cf7c | 905 | SNPRINTF(strPort, sizeof(strPort), "%d", port); |
| wolfSSL | 15:117db924cf7c | 906 | strPort[79] = '\0'; |
| wolfSSL | 15:117db924cf7c | 907 | |
| wolfSSL | 15:117db924cf7c | 908 | ret = getaddrinfo(peer, strPort, &hints, &answer); |
| wolfSSL | 15:117db924cf7c | 909 | if (ret < 0 || answer == NULL) |
| wolfSSL | 15:117db924cf7c | 910 | err_sys("getaddrinfo failed"); |
| wolfSSL | 15:117db924cf7c | 911 | |
| wolfSSL | 15:117db924cf7c | 912 | XMEMCPY(addr, answer->ai_addr, answer->ai_addrlen); |
| wolfSSL | 15:117db924cf7c | 913 | freeaddrinfo(answer); |
| wolfSSL | 15:117db924cf7c | 914 | #else |
| wolfSSL | 15:117db924cf7c | 915 | printf("no ipv6 getaddrinfo, loopback only tests/examples\n"); |
| wolfSSL | 15:117db924cf7c | 916 | addr->sin6_addr = in6addr_loopback; |
| wolfSSL | 15:117db924cf7c | 917 | #endif |
| wolfSSL | 15:117db924cf7c | 918 | } |
| wolfSSL | 15:117db924cf7c | 919 | #endif |
| wolfSSL | 15:117db924cf7c | 920 | } |
| wolfSSL | 15:117db924cf7c | 921 | |
| wolfSSL | 15:117db924cf7c | 922 | |
| wolfSSL | 15:117db924cf7c | 923 | static WC_INLINE void tcp_socket(SOCKET_T* sockfd, int udp, int sctp) |
| wolfSSL | 15:117db924cf7c | 924 | { |
| wolfSSL | 15:117db924cf7c | 925 | (void)sctp; |
| wolfSSL | 15:117db924cf7c | 926 | |
| wolfSSL | 15:117db924cf7c | 927 | if (udp) |
| wolfSSL | 15:117db924cf7c | 928 | *sockfd = socket(AF_INET_V, SOCK_DGRAM, IPPROTO_UDP); |
| wolfSSL | 15:117db924cf7c | 929 | #ifdef WOLFSSL_SCTP |
| wolfSSL | 15:117db924cf7c | 930 | else if (sctp) |
| wolfSSL | 15:117db924cf7c | 931 | *sockfd = socket(AF_INET_V, SOCK_STREAM, IPPROTO_SCTP); |
| wolfSSL | 15:117db924cf7c | 932 | #endif |
| wolfSSL | 15:117db924cf7c | 933 | else |
| wolfSSL | 15:117db924cf7c | 934 | *sockfd = socket(AF_INET_V, SOCK_STREAM, IPPROTO_TCP); |
| wolfSSL | 15:117db924cf7c | 935 | |
| wolfSSL | 15:117db924cf7c | 936 | if(WOLFSSL_SOCKET_IS_INVALID(*sockfd)) { |
| wolfSSL | 15:117db924cf7c | 937 | err_sys("socket failed\n"); |
| wolfSSL | 15:117db924cf7c | 938 | } |
| wolfSSL | 15:117db924cf7c | 939 | |
| wolfSSL | 15:117db924cf7c | 940 | #ifndef USE_WINDOWS_API |
| wolfSSL | 15:117db924cf7c | 941 | #ifdef SO_NOSIGPIPE |
| wolfSSL | 15:117db924cf7c | 942 | { |
| wolfSSL | 15:117db924cf7c | 943 | int on = 1; |
| wolfSSL | 15:117db924cf7c | 944 | socklen_t len = sizeof(on); |
| wolfSSL | 15:117db924cf7c | 945 | int res = setsockopt(*sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, len); |
| wolfSSL | 15:117db924cf7c | 946 | if (res < 0) |
| wolfSSL | 15:117db924cf7c | 947 | err_sys("setsockopt SO_NOSIGPIPE failed\n"); |
| wolfSSL | 15:117db924cf7c | 948 | } |
| wolfSSL | 15:117db924cf7c | 949 | #elif defined(WOLFSSL_MDK_ARM) || defined (WOLFSSL_TIRTOS) ||\ |
| wolfSSL | 16:8e0d178b1d1e | 950 | defined(WOLFSSL_KEIL_TCP_NET) || defined(WOLFSSL_ZEPHYR) |
| wolfSSL | 15:117db924cf7c | 951 | /* nothing to define */ |
| wolfSSL | 15:117db924cf7c | 952 | #else /* no S_NOSIGPIPE */ |
| wolfSSL | 15:117db924cf7c | 953 | signal(SIGPIPE, SIG_IGN); |
| wolfSSL | 15:117db924cf7c | 954 | #endif /* S_NOSIGPIPE */ |
| wolfSSL | 15:117db924cf7c | 955 | |
| wolfSSL | 15:117db924cf7c | 956 | #if defined(TCP_NODELAY) |
| wolfSSL | 15:117db924cf7c | 957 | if (!udp && !sctp) |
| wolfSSL | 15:117db924cf7c | 958 | { |
| wolfSSL | 15:117db924cf7c | 959 | int on = 1; |
| wolfSSL | 15:117db924cf7c | 960 | socklen_t len = sizeof(on); |
| wolfSSL | 15:117db924cf7c | 961 | int res = setsockopt(*sockfd, IPPROTO_TCP, TCP_NODELAY, &on, len); |
| wolfSSL | 15:117db924cf7c | 962 | if (res < 0) |
| wolfSSL | 15:117db924cf7c | 963 | err_sys("setsockopt TCP_NODELAY failed\n"); |
| wolfSSL | 15:117db924cf7c | 964 | } |
| wolfSSL | 15:117db924cf7c | 965 | #endif |
| wolfSSL | 15:117db924cf7c | 966 | #endif /* USE_WINDOWS_API */ |
| wolfSSL | 15:117db924cf7c | 967 | } |
| wolfSSL | 15:117db924cf7c | 968 | |
| wolfSSL | 15:117db924cf7c | 969 | static WC_INLINE void tcp_connect(SOCKET_T* sockfd, const char* ip, word16 port, |
| wolfSSL | 15:117db924cf7c | 970 | int udp, int sctp, WOLFSSL* ssl) |
| wolfSSL | 15:117db924cf7c | 971 | { |
| wolfSSL | 15:117db924cf7c | 972 | SOCKADDR_IN_T addr; |
| wolfSSL | 15:117db924cf7c | 973 | build_addr(&addr, ip, port, udp, sctp); |
| wolfSSL | 15:117db924cf7c | 974 | if (udp) { |
| wolfSSL | 15:117db924cf7c | 975 | wolfSSL_dtls_set_peer(ssl, &addr, sizeof(addr)); |
| wolfSSL | 15:117db924cf7c | 976 | } |
| wolfSSL | 15:117db924cf7c | 977 | tcp_socket(sockfd, udp, sctp); |
| wolfSSL | 15:117db924cf7c | 978 | |
| wolfSSL | 15:117db924cf7c | 979 | if (!udp) { |
| wolfSSL | 15:117db924cf7c | 980 | if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0) |
| wolfSSL | 15:117db924cf7c | 981 | err_sys("tcp connect failed"); |
| wolfSSL | 15:117db924cf7c | 982 | } |
| wolfSSL | 15:117db924cf7c | 983 | } |
| wolfSSL | 15:117db924cf7c | 984 | |
| wolfSSL | 15:117db924cf7c | 985 | |
| wolfSSL | 15:117db924cf7c | 986 | static WC_INLINE void udp_connect(SOCKET_T* sockfd, void* addr, int addrSz) |
| wolfSSL | 15:117db924cf7c | 987 | { |
| wolfSSL | 15:117db924cf7c | 988 | if (connect(*sockfd, (const struct sockaddr*)addr, addrSz) != 0) |
| wolfSSL | 15:117db924cf7c | 989 | err_sys("tcp connect failed"); |
| wolfSSL | 15:117db924cf7c | 990 | } |
| wolfSSL | 15:117db924cf7c | 991 | |
| wolfSSL | 15:117db924cf7c | 992 | |
| wolfSSL | 15:117db924cf7c | 993 | enum { |
| wolfSSL | 15:117db924cf7c | 994 | TEST_SELECT_FAIL, |
| wolfSSL | 15:117db924cf7c | 995 | TEST_TIMEOUT, |
| wolfSSL | 15:117db924cf7c | 996 | TEST_RECV_READY, |
| wolfSSL | 16:8e0d178b1d1e | 997 | TEST_SEND_READY, |
| wolfSSL | 15:117db924cf7c | 998 | TEST_ERROR_READY |
| wolfSSL | 15:117db924cf7c | 999 | }; |
| wolfSSL | 15:117db924cf7c | 1000 | |
| wolfSSL | 15:117db924cf7c | 1001 | |
| wolfSSL | 15:117db924cf7c | 1002 | #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_TCP_NET) && \ |
| wolfSSL | 15:117db924cf7c | 1003 | !defined(WOLFSSL_TIRTOS) |
| wolfSSL | 16:8e0d178b1d1e | 1004 | static WC_INLINE int tcp_select_ex(SOCKET_T socketfd, int to_sec, int rx) |
| wolfSSL | 15:117db924cf7c | 1005 | { |
| wolfSSL | 16:8e0d178b1d1e | 1006 | fd_set fds, errfds; |
| wolfSSL | 16:8e0d178b1d1e | 1007 | fd_set* recvfds = NULL; |
| wolfSSL | 16:8e0d178b1d1e | 1008 | fd_set* sendfds = NULL; |
| wolfSSL | 15:117db924cf7c | 1009 | SOCKET_T nfds = socketfd + 1; |
| wolfSSL | 15:117db924cf7c | 1010 | #if !defined(__INTEGRITY) |
| wolfSSL | 15:117db924cf7c | 1011 | struct timeval timeout = {(to_sec > 0) ? to_sec : 0, 0}; |
| wolfSSL | 15:117db924cf7c | 1012 | #else |
| wolfSSL | 15:117db924cf7c | 1013 | struct timeval timeout; |
| wolfSSL | 15:117db924cf7c | 1014 | #endif |
| wolfSSL | 15:117db924cf7c | 1015 | int result; |
| wolfSSL | 15:117db924cf7c | 1016 | |
| wolfSSL | 16:8e0d178b1d1e | 1017 | FD_ZERO(&fds); |
| wolfSSL | 16:8e0d178b1d1e | 1018 | FD_SET(socketfd, &fds); |
| wolfSSL | 15:117db924cf7c | 1019 | FD_ZERO(&errfds); |
| wolfSSL | 15:117db924cf7c | 1020 | FD_SET(socketfd, &errfds); |
| wolfSSL | 15:117db924cf7c | 1021 | |
| wolfSSL | 16:8e0d178b1d1e | 1022 | if (rx) |
| wolfSSL | 16:8e0d178b1d1e | 1023 | recvfds = &fds; |
| wolfSSL | 16:8e0d178b1d1e | 1024 | else |
| wolfSSL | 16:8e0d178b1d1e | 1025 | sendfds = &fds; |
| wolfSSL | 16:8e0d178b1d1e | 1026 | |
| wolfSSL | 15:117db924cf7c | 1027 | #if defined(__INTEGRITY) |
| wolfSSL | 15:117db924cf7c | 1028 | timeout.tv_sec = (long long)(to_sec > 0) ? to_sec : 0, 0; |
| wolfSSL | 15:117db924cf7c | 1029 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 1030 | result = select(nfds, recvfds, sendfds, &errfds, &timeout); |
| wolfSSL | 15:117db924cf7c | 1031 | |
| wolfSSL | 15:117db924cf7c | 1032 | if (result == 0) |
| wolfSSL | 15:117db924cf7c | 1033 | return TEST_TIMEOUT; |
| wolfSSL | 15:117db924cf7c | 1034 | else if (result > 0) { |
| wolfSSL | 16:8e0d178b1d1e | 1035 | if (FD_ISSET(socketfd, &fds)) { |
| wolfSSL | 16:8e0d178b1d1e | 1036 | if (rx) |
| wolfSSL | 16:8e0d178b1d1e | 1037 | return TEST_RECV_READY; |
| wolfSSL | 16:8e0d178b1d1e | 1038 | else |
| wolfSSL | 16:8e0d178b1d1e | 1039 | return TEST_SEND_READY; |
| wolfSSL | 16:8e0d178b1d1e | 1040 | } |
| wolfSSL | 15:117db924cf7c | 1041 | else if(FD_ISSET(socketfd, &errfds)) |
| wolfSSL | 15:117db924cf7c | 1042 | return TEST_ERROR_READY; |
| wolfSSL | 15:117db924cf7c | 1043 | } |
| wolfSSL | 15:117db924cf7c | 1044 | |
| wolfSSL | 15:117db924cf7c | 1045 | return TEST_SELECT_FAIL; |
| wolfSSL | 15:117db924cf7c | 1046 | } |
| wolfSSL | 16:8e0d178b1d1e | 1047 | |
| wolfSSL | 16:8e0d178b1d1e | 1048 | static WC_INLINE int tcp_select(SOCKET_T socketfd, int to_sec) |
| wolfSSL | 16:8e0d178b1d1e | 1049 | { |
| wolfSSL | 16:8e0d178b1d1e | 1050 | return tcp_select_ex(socketfd, to_sec, 1); |
| wolfSSL | 16:8e0d178b1d1e | 1051 | } |
| wolfSSL | 16:8e0d178b1d1e | 1052 | |
| wolfSSL | 16:8e0d178b1d1e | 1053 | static WC_INLINE int tcp_select_tx(SOCKET_T socketfd, int to_sec) |
| wolfSSL | 16:8e0d178b1d1e | 1054 | { |
| wolfSSL | 16:8e0d178b1d1e | 1055 | return tcp_select_ex(socketfd, to_sec, 0); |
| wolfSSL | 16:8e0d178b1d1e | 1056 | } |
| wolfSSL | 16:8e0d178b1d1e | 1057 | |
| wolfSSL | 15:117db924cf7c | 1058 | #elif defined(WOLFSSL_TIRTOS) || defined(WOLFSSL_KEIL_TCP_NET) |
| wolfSSL | 15:117db924cf7c | 1059 | static WC_INLINE int tcp_select(SOCKET_T socketfd, int to_sec) |
| wolfSSL | 15:117db924cf7c | 1060 | { |
| wolfSSL | 15:117db924cf7c | 1061 | return TEST_RECV_READY; |
| wolfSSL | 15:117db924cf7c | 1062 | } |
| wolfSSL | 16:8e0d178b1d1e | 1063 | static WC_INLINE int tcp_select_tx(SOCKET_T socketfd, int to_sec) |
| wolfSSL | 16:8e0d178b1d1e | 1064 | { |
| wolfSSL | 16:8e0d178b1d1e | 1065 | return TEST_SEND_READY; |
| wolfSSL | 16:8e0d178b1d1e | 1066 | } |
| wolfSSL | 15:117db924cf7c | 1067 | #endif /* !WOLFSSL_MDK_ARM */ |
| wolfSSL | 15:117db924cf7c | 1068 | |
| wolfSSL | 15:117db924cf7c | 1069 | |
| wolfSSL | 15:117db924cf7c | 1070 | static WC_INLINE void tcp_listen(SOCKET_T* sockfd, word16* port, int useAnyAddr, |
| wolfSSL | 15:117db924cf7c | 1071 | int udp, int sctp) |
| wolfSSL | 15:117db924cf7c | 1072 | { |
| wolfSSL | 15:117db924cf7c | 1073 | SOCKADDR_IN_T addr; |
| wolfSSL | 15:117db924cf7c | 1074 | |
| wolfSSL | 15:117db924cf7c | 1075 | /* don't use INADDR_ANY by default, firewall may block, make user switch |
| wolfSSL | 15:117db924cf7c | 1076 | on */ |
| wolfSSL | 15:117db924cf7c | 1077 | build_addr(&addr, (useAnyAddr ? INADDR_ANY : wolfSSLIP), *port, udp, sctp); |
| wolfSSL | 15:117db924cf7c | 1078 | tcp_socket(sockfd, udp, sctp); |
| wolfSSL | 15:117db924cf7c | 1079 | |
| wolfSSL | 15:117db924cf7c | 1080 | #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_ARM)\ |
| wolfSSL | 16:8e0d178b1d1e | 1081 | && !defined(WOLFSSL_KEIL_TCP_NET) && !defined(WOLFSSL_ZEPHYR) |
| wolfSSL | 15:117db924cf7c | 1082 | { |
| wolfSSL | 15:117db924cf7c | 1083 | int res, on = 1; |
| wolfSSL | 15:117db924cf7c | 1084 | socklen_t len = sizeof(on); |
| wolfSSL | 15:117db924cf7c | 1085 | res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len); |
| wolfSSL | 15:117db924cf7c | 1086 | if (res < 0) |
| wolfSSL | 15:117db924cf7c | 1087 | err_sys("setsockopt SO_REUSEADDR failed\n"); |
| wolfSSL | 15:117db924cf7c | 1088 | } |
| wolfSSL | 15:117db924cf7c | 1089 | #endif |
| wolfSSL | 15:117db924cf7c | 1090 | |
| wolfSSL | 15:117db924cf7c | 1091 | if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0) |
| wolfSSL | 15:117db924cf7c | 1092 | err_sys("tcp bind failed"); |
| wolfSSL | 15:117db924cf7c | 1093 | if (!udp) { |
| wolfSSL | 15:117db924cf7c | 1094 | #ifdef WOLFSSL_KEIL_TCP_NET |
| wolfSSL | 15:117db924cf7c | 1095 | #define SOCK_LISTEN_MAX_QUEUE 1 |
| wolfSSL | 15:117db924cf7c | 1096 | #else |
| wolfSSL | 15:117db924cf7c | 1097 | #define SOCK_LISTEN_MAX_QUEUE 5 |
| wolfSSL | 15:117db924cf7c | 1098 | #endif |
| wolfSSL | 15:117db924cf7c | 1099 | if (listen(*sockfd, SOCK_LISTEN_MAX_QUEUE) != 0) |
| wolfSSL | 15:117db924cf7c | 1100 | err_sys("tcp listen failed"); |
| wolfSSL | 15:117db924cf7c | 1101 | } |
| wolfSSL | 16:8e0d178b1d1e | 1102 | #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_TIRTOS) \ |
| wolfSSL | 16:8e0d178b1d1e | 1103 | && !defined(WOLFSSL_ZEPHYR) |
| wolfSSL | 15:117db924cf7c | 1104 | if (*port == 0) { |
| wolfSSL | 15:117db924cf7c | 1105 | socklen_t len = sizeof(addr); |
| wolfSSL | 15:117db924cf7c | 1106 | if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) { |
| wolfSSL | 15:117db924cf7c | 1107 | #ifndef TEST_IPV6 |
| wolfSSL | 15:117db924cf7c | 1108 | *port = XNTOHS(addr.sin_port); |
| wolfSSL | 15:117db924cf7c | 1109 | #else |
| wolfSSL | 15:117db924cf7c | 1110 | *port = XNTOHS(addr.sin6_port); |
| wolfSSL | 15:117db924cf7c | 1111 | #endif |
| wolfSSL | 15:117db924cf7c | 1112 | } |
| wolfSSL | 15:117db924cf7c | 1113 | } |
| wolfSSL | 15:117db924cf7c | 1114 | #endif |
| wolfSSL | 15:117db924cf7c | 1115 | } |
| wolfSSL | 15:117db924cf7c | 1116 | |
| wolfSSL | 15:117db924cf7c | 1117 | |
| wolfSSL | 15:117db924cf7c | 1118 | #if 0 |
| wolfSSL | 15:117db924cf7c | 1119 | static WC_INLINE int udp_read_connect(SOCKET_T sockfd) |
| wolfSSL | 15:117db924cf7c | 1120 | { |
| wolfSSL | 15:117db924cf7c | 1121 | SOCKADDR_IN_T cliaddr; |
| wolfSSL | 15:117db924cf7c | 1122 | byte b[1500]; |
| wolfSSL | 15:117db924cf7c | 1123 | int n; |
| wolfSSL | 15:117db924cf7c | 1124 | socklen_t len = sizeof(cliaddr); |
| wolfSSL | 15:117db924cf7c | 1125 | |
| wolfSSL | 15:117db924cf7c | 1126 | n = (int)recvfrom(sockfd, (char*)b, sizeof(b), MSG_PEEK, |
| wolfSSL | 15:117db924cf7c | 1127 | (struct sockaddr*)&cliaddr, &len); |
| wolfSSL | 15:117db924cf7c | 1128 | if (n > 0) { |
| wolfSSL | 15:117db924cf7c | 1129 | if (connect(sockfd, (const struct sockaddr*)&cliaddr, |
| wolfSSL | 15:117db924cf7c | 1130 | sizeof(cliaddr)) != 0) |
| wolfSSL | 15:117db924cf7c | 1131 | err_sys("udp connect failed"); |
| wolfSSL | 15:117db924cf7c | 1132 | } |
| wolfSSL | 15:117db924cf7c | 1133 | else |
| wolfSSL | 15:117db924cf7c | 1134 | err_sys("recvfrom failed"); |
| wolfSSL | 15:117db924cf7c | 1135 | |
| wolfSSL | 15:117db924cf7c | 1136 | return sockfd; |
| wolfSSL | 15:117db924cf7c | 1137 | } |
| wolfSSL | 15:117db924cf7c | 1138 | #endif |
| wolfSSL | 15:117db924cf7c | 1139 | |
| wolfSSL | 15:117db924cf7c | 1140 | static WC_INLINE void udp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd, |
| wolfSSL | 15:117db924cf7c | 1141 | int useAnyAddr, word16 port, func_args* args) |
| wolfSSL | 15:117db924cf7c | 1142 | { |
| wolfSSL | 15:117db924cf7c | 1143 | SOCKADDR_IN_T addr; |
| wolfSSL | 15:117db924cf7c | 1144 | |
| wolfSSL | 15:117db924cf7c | 1145 | (void)args; |
| wolfSSL | 15:117db924cf7c | 1146 | build_addr(&addr, (useAnyAddr ? INADDR_ANY : wolfSSLIP), port, 1, 0); |
| wolfSSL | 15:117db924cf7c | 1147 | tcp_socket(sockfd, 1, 0); |
| wolfSSL | 15:117db924cf7c | 1148 | |
| wolfSSL | 15:117db924cf7c | 1149 | |
| wolfSSL | 15:117db924cf7c | 1150 | #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_ARM) \ |
| wolfSSL | 16:8e0d178b1d1e | 1151 | && !defined(WOLFSSL_KEIL_TCP_NET) && !defined(WOLFSSL_ZEPHYR) |
| wolfSSL | 15:117db924cf7c | 1152 | { |
| wolfSSL | 15:117db924cf7c | 1153 | int res, on = 1; |
| wolfSSL | 15:117db924cf7c | 1154 | socklen_t len = sizeof(on); |
| wolfSSL | 15:117db924cf7c | 1155 | res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len); |
| wolfSSL | 15:117db924cf7c | 1156 | if (res < 0) |
| wolfSSL | 15:117db924cf7c | 1157 | err_sys("setsockopt SO_REUSEADDR failed\n"); |
| wolfSSL | 15:117db924cf7c | 1158 | } |
| wolfSSL | 15:117db924cf7c | 1159 | #endif |
| wolfSSL | 15:117db924cf7c | 1160 | |
| wolfSSL | 15:117db924cf7c | 1161 | if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0) |
| wolfSSL | 15:117db924cf7c | 1162 | err_sys("tcp bind failed"); |
| wolfSSL | 15:117db924cf7c | 1163 | |
| wolfSSL | 15:117db924cf7c | 1164 | #if (defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API)) && !defined(WOLFSSL_TIRTOS) |
| wolfSSL | 15:117db924cf7c | 1165 | if (port == 0) { |
| wolfSSL | 15:117db924cf7c | 1166 | socklen_t len = sizeof(addr); |
| wolfSSL | 15:117db924cf7c | 1167 | if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) { |
| wolfSSL | 15:117db924cf7c | 1168 | #ifndef TEST_IPV6 |
| wolfSSL | 15:117db924cf7c | 1169 | port = XNTOHS(addr.sin_port); |
| wolfSSL | 15:117db924cf7c | 1170 | #else |
| wolfSSL | 15:117db924cf7c | 1171 | port = XNTOHS(addr.sin6_port); |
| wolfSSL | 15:117db924cf7c | 1172 | #endif |
| wolfSSL | 15:117db924cf7c | 1173 | } |
| wolfSSL | 15:117db924cf7c | 1174 | } |
| wolfSSL | 15:117db924cf7c | 1175 | #endif |
| wolfSSL | 15:117db924cf7c | 1176 | |
| wolfSSL | 15:117db924cf7c | 1177 | #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__) |
| wolfSSL | 15:117db924cf7c | 1178 | /* signal ready to accept data */ |
| wolfSSL | 15:117db924cf7c | 1179 | { |
| wolfSSL | 15:117db924cf7c | 1180 | tcp_ready* ready = args->signal; |
| wolfSSL | 15:117db924cf7c | 1181 | pthread_mutex_lock(&ready->mutex); |
| wolfSSL | 15:117db924cf7c | 1182 | ready->ready = 1; |
| wolfSSL | 15:117db924cf7c | 1183 | ready->port = port; |
| wolfSSL | 15:117db924cf7c | 1184 | pthread_cond_signal(&ready->cond); |
| wolfSSL | 15:117db924cf7c | 1185 | pthread_mutex_unlock(&ready->mutex); |
| wolfSSL | 15:117db924cf7c | 1186 | } |
| wolfSSL | 15:117db924cf7c | 1187 | #elif defined (WOLFSSL_TIRTOS) |
| wolfSSL | 15:117db924cf7c | 1188 | /* Need mutex? */ |
| wolfSSL | 15:117db924cf7c | 1189 | tcp_ready* ready = args->signal; |
| wolfSSL | 15:117db924cf7c | 1190 | ready->ready = 1; |
| wolfSSL | 15:117db924cf7c | 1191 | ready->port = port; |
| wolfSSL | 16:8e0d178b1d1e | 1192 | #else |
| wolfSSL | 16:8e0d178b1d1e | 1193 | (void)port; |
| wolfSSL | 15:117db924cf7c | 1194 | #endif |
| wolfSSL | 15:117db924cf7c | 1195 | |
| wolfSSL | 15:117db924cf7c | 1196 | *clientfd = *sockfd; |
| wolfSSL | 15:117db924cf7c | 1197 | } |
| wolfSSL | 15:117db924cf7c | 1198 | |
| wolfSSL | 15:117db924cf7c | 1199 | static WC_INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd, |
| wolfSSL | 15:117db924cf7c | 1200 | func_args* args, word16 port, int useAnyAddr, |
| wolfSSL | 15:117db924cf7c | 1201 | int udp, int sctp, int ready_file, int do_listen) |
| wolfSSL | 15:117db924cf7c | 1202 | { |
| wolfSSL | 15:117db924cf7c | 1203 | SOCKADDR_IN_T client; |
| wolfSSL | 15:117db924cf7c | 1204 | socklen_t client_len = sizeof(client); |
| wolfSSL | 15:117db924cf7c | 1205 | tcp_ready* ready = NULL; |
| wolfSSL | 15:117db924cf7c | 1206 | |
| wolfSSL | 15:117db924cf7c | 1207 | (void) ready; /* Account for case when "ready" is not used */ |
| wolfSSL | 15:117db924cf7c | 1208 | |
| wolfSSL | 15:117db924cf7c | 1209 | if (udp) { |
| wolfSSL | 15:117db924cf7c | 1210 | udp_accept(sockfd, clientfd, useAnyAddr, port, args); |
| wolfSSL | 15:117db924cf7c | 1211 | return; |
| wolfSSL | 15:117db924cf7c | 1212 | } |
| wolfSSL | 15:117db924cf7c | 1213 | |
| wolfSSL | 15:117db924cf7c | 1214 | if(do_listen) { |
| wolfSSL | 15:117db924cf7c | 1215 | tcp_listen(sockfd, &port, useAnyAddr, udp, sctp); |
| wolfSSL | 15:117db924cf7c | 1216 | |
| wolfSSL | 15:117db924cf7c | 1217 | #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__) |
| wolfSSL | 15:117db924cf7c | 1218 | /* signal ready to tcp_accept */ |
| wolfSSL | 15:117db924cf7c | 1219 | if (args) |
| wolfSSL | 15:117db924cf7c | 1220 | ready = args->signal; |
| wolfSSL | 15:117db924cf7c | 1221 | if (ready) { |
| wolfSSL | 15:117db924cf7c | 1222 | pthread_mutex_lock(&ready->mutex); |
| wolfSSL | 15:117db924cf7c | 1223 | ready->ready = 1; |
| wolfSSL | 15:117db924cf7c | 1224 | ready->port = port; |
| wolfSSL | 15:117db924cf7c | 1225 | pthread_cond_signal(&ready->cond); |
| wolfSSL | 15:117db924cf7c | 1226 | pthread_mutex_unlock(&ready->mutex); |
| wolfSSL | 15:117db924cf7c | 1227 | } |
| wolfSSL | 15:117db924cf7c | 1228 | #elif defined (WOLFSSL_TIRTOS) |
| wolfSSL | 15:117db924cf7c | 1229 | /* Need mutex? */ |
| wolfSSL | 15:117db924cf7c | 1230 | if (args) |
| wolfSSL | 15:117db924cf7c | 1231 | ready = args->signal; |
| wolfSSL | 15:117db924cf7c | 1232 | if (ready) { |
| wolfSSL | 15:117db924cf7c | 1233 | ready->ready = 1; |
| wolfSSL | 15:117db924cf7c | 1234 | ready->port = port; |
| wolfSSL | 15:117db924cf7c | 1235 | } |
| wolfSSL | 15:117db924cf7c | 1236 | #endif |
| wolfSSL | 15:117db924cf7c | 1237 | |
| wolfSSL | 15:117db924cf7c | 1238 | if (ready_file) { |
| wolfSSL | 15:117db924cf7c | 1239 | #if !defined(NO_FILESYSTEM) || defined(FORCE_BUFFER_TEST) |
| wolfSSL | 16:8e0d178b1d1e | 1240 | XFILE srf = NULL; |
| wolfSSL | 15:117db924cf7c | 1241 | if (args) |
| wolfSSL | 15:117db924cf7c | 1242 | ready = args->signal; |
| wolfSSL | 15:117db924cf7c | 1243 | |
| wolfSSL | 15:117db924cf7c | 1244 | if (ready) { |
| wolfSSL | 16:8e0d178b1d1e | 1245 | srf = XFOPEN(ready->srfName, "w"); |
| wolfSSL | 15:117db924cf7c | 1246 | |
| wolfSSL | 15:117db924cf7c | 1247 | if (srf) { |
| wolfSSL | 15:117db924cf7c | 1248 | /* let's write port sever is listening on to ready file |
| wolfSSL | 15:117db924cf7c | 1249 | external monitor can then do ephemeral ports by passing |
| wolfSSL | 15:117db924cf7c | 1250 | -p 0 to server on supported platforms with -R ready_file |
| wolfSSL | 15:117db924cf7c | 1251 | client can then wait for existence of ready_file and see |
| wolfSSL | 15:117db924cf7c | 1252 | which port the server is listening on. */ |
| wolfSSL | 15:117db924cf7c | 1253 | fprintf(srf, "%d\n", (int)port); |
| wolfSSL | 15:117db924cf7c | 1254 | fclose(srf); |
| wolfSSL | 15:117db924cf7c | 1255 | } |
| wolfSSL | 15:117db924cf7c | 1256 | } |
| wolfSSL | 15:117db924cf7c | 1257 | #endif |
| wolfSSL | 15:117db924cf7c | 1258 | } |
| wolfSSL | 15:117db924cf7c | 1259 | } |
| wolfSSL | 15:117db924cf7c | 1260 | |
| wolfSSL | 15:117db924cf7c | 1261 | *clientfd = accept(*sockfd, (struct sockaddr*)&client, |
| wolfSSL | 15:117db924cf7c | 1262 | (ACCEPT_THIRD_T)&client_len); |
| wolfSSL | 15:117db924cf7c | 1263 | if(WOLFSSL_SOCKET_IS_INVALID(*clientfd)) { |
| wolfSSL | 15:117db924cf7c | 1264 | err_sys("tcp accept failed"); |
| wolfSSL | 15:117db924cf7c | 1265 | } |
| wolfSSL | 15:117db924cf7c | 1266 | } |
| wolfSSL | 15:117db924cf7c | 1267 | |
| wolfSSL | 15:117db924cf7c | 1268 | |
| wolfSSL | 15:117db924cf7c | 1269 | static WC_INLINE void tcp_set_nonblocking(SOCKET_T* sockfd) |
| wolfSSL | 15:117db924cf7c | 1270 | { |
| wolfSSL | 15:117db924cf7c | 1271 | #ifdef USE_WINDOWS_API |
| wolfSSL | 15:117db924cf7c | 1272 | unsigned long blocking = 1; |
| wolfSSL | 15:117db924cf7c | 1273 | int ret = ioctlsocket(*sockfd, FIONBIO, &blocking); |
| wolfSSL | 15:117db924cf7c | 1274 | if (ret == SOCKET_ERROR) |
| wolfSSL | 15:117db924cf7c | 1275 | err_sys("ioctlsocket failed"); |
| wolfSSL | 15:117db924cf7c | 1276 | #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) \ |
| wolfSSL | 16:8e0d178b1d1e | 1277 | || defined (WOLFSSL_TIRTOS)|| defined(WOLFSSL_VXWORKS) \ |
| wolfSSL | 16:8e0d178b1d1e | 1278 | || defined(WOLFSSL_ZEPHYR) |
| wolfSSL | 15:117db924cf7c | 1279 | /* non blocking not supported, for now */ |
| wolfSSL | 15:117db924cf7c | 1280 | #else |
| wolfSSL | 15:117db924cf7c | 1281 | int flags = fcntl(*sockfd, F_GETFL, 0); |
| wolfSSL | 15:117db924cf7c | 1282 | if (flags < 0) |
| wolfSSL | 15:117db924cf7c | 1283 | err_sys("fcntl get failed"); |
| wolfSSL | 15:117db924cf7c | 1284 | flags = fcntl(*sockfd, F_SETFL, flags | O_NONBLOCK); |
| wolfSSL | 15:117db924cf7c | 1285 | if (flags < 0) |
| wolfSSL | 15:117db924cf7c | 1286 | err_sys("fcntl set failed"); |
| wolfSSL | 15:117db924cf7c | 1287 | #endif |
| wolfSSL | 15:117db924cf7c | 1288 | } |
| wolfSSL | 15:117db924cf7c | 1289 | |
| wolfSSL | 15:117db924cf7c | 1290 | |
| wolfSSL | 15:117db924cf7c | 1291 | #ifndef NO_PSK |
| wolfSSL | 15:117db924cf7c | 1292 | |
| wolfSSL | 15:117db924cf7c | 1293 | /* identity is OpenSSL testing default for openssl s_client, keep same */ |
| wolfSSL | 15:117db924cf7c | 1294 | static const char* kIdentityStr = "Client_identity"; |
| wolfSSL | 15:117db924cf7c | 1295 | |
| wolfSSL | 15:117db924cf7c | 1296 | static WC_INLINE unsigned int my_psk_client_cb(WOLFSSL* ssl, const char* hint, |
| wolfSSL | 15:117db924cf7c | 1297 | char* identity, unsigned int id_max_len, unsigned char* key, |
| wolfSSL | 15:117db924cf7c | 1298 | unsigned int key_max_len) |
| wolfSSL | 15:117db924cf7c | 1299 | { |
| wolfSSL | 15:117db924cf7c | 1300 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 1301 | (void)hint; |
| wolfSSL | 15:117db924cf7c | 1302 | (void)key_max_len; |
| wolfSSL | 15:117db924cf7c | 1303 | |
| wolfSSL | 15:117db924cf7c | 1304 | /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */ |
| wolfSSL | 15:117db924cf7c | 1305 | strncpy(identity, kIdentityStr, id_max_len); |
| wolfSSL | 15:117db924cf7c | 1306 | |
| wolfSSL | 15:117db924cf7c | 1307 | if (wolfSSL_GetVersion(ssl) < WOLFSSL_TLSV1_3) { |
| wolfSSL | 15:117db924cf7c | 1308 | /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using |
| wolfSSL | 15:117db924cf7c | 1309 | unsigned binary */ |
| wolfSSL | 15:117db924cf7c | 1310 | key[0] = 0x1a; |
| wolfSSL | 15:117db924cf7c | 1311 | key[1] = 0x2b; |
| wolfSSL | 15:117db924cf7c | 1312 | key[2] = 0x3c; |
| wolfSSL | 15:117db924cf7c | 1313 | key[3] = 0x4d; |
| wolfSSL | 15:117db924cf7c | 1314 | |
| wolfSSL | 15:117db924cf7c | 1315 | return 4; /* length of key in octets or 0 for error */ |
| wolfSSL | 15:117db924cf7c | 1316 | } |
| wolfSSL | 15:117db924cf7c | 1317 | else { |
| wolfSSL | 15:117db924cf7c | 1318 | int i; |
| wolfSSL | 15:117db924cf7c | 1319 | int b = 0x01; |
| wolfSSL | 15:117db924cf7c | 1320 | |
| wolfSSL | 15:117db924cf7c | 1321 | for (i = 0; i < 32; i++, b += 0x22) { |
| wolfSSL | 15:117db924cf7c | 1322 | if (b >= 0x100) |
| wolfSSL | 15:117db924cf7c | 1323 | b = 0x01; |
| wolfSSL | 15:117db924cf7c | 1324 | key[i] = b; |
| wolfSSL | 15:117db924cf7c | 1325 | } |
| wolfSSL | 15:117db924cf7c | 1326 | |
| wolfSSL | 15:117db924cf7c | 1327 | return 32; /* length of key in octets or 0 for error */ |
| wolfSSL | 15:117db924cf7c | 1328 | } |
| wolfSSL | 15:117db924cf7c | 1329 | } |
| wolfSSL | 15:117db924cf7c | 1330 | |
| wolfSSL | 15:117db924cf7c | 1331 | |
| wolfSSL | 15:117db924cf7c | 1332 | static WC_INLINE unsigned int my_psk_server_cb(WOLFSSL* ssl, const char* identity, |
| wolfSSL | 15:117db924cf7c | 1333 | unsigned char* key, unsigned int key_max_len) |
| wolfSSL | 15:117db924cf7c | 1334 | { |
| wolfSSL | 15:117db924cf7c | 1335 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 1336 | (void)key_max_len; |
| wolfSSL | 15:117db924cf7c | 1337 | |
| wolfSSL | 15:117db924cf7c | 1338 | /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */ |
| wolfSSL | 15:117db924cf7c | 1339 | if (strncmp(identity, kIdentityStr, strlen(kIdentityStr)) != 0) |
| wolfSSL | 15:117db924cf7c | 1340 | return 0; |
| wolfSSL | 15:117db924cf7c | 1341 | |
| wolfSSL | 15:117db924cf7c | 1342 | if (wolfSSL_GetVersion(ssl) < WOLFSSL_TLSV1_3) { |
| wolfSSL | 15:117db924cf7c | 1343 | /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using |
| wolfSSL | 15:117db924cf7c | 1344 | unsigned binary */ |
| wolfSSL | 15:117db924cf7c | 1345 | key[0] = 0x1a; |
| wolfSSL | 15:117db924cf7c | 1346 | key[1] = 0x2b; |
| wolfSSL | 15:117db924cf7c | 1347 | key[2] = 0x3c; |
| wolfSSL | 15:117db924cf7c | 1348 | key[3] = 0x4d; |
| wolfSSL | 15:117db924cf7c | 1349 | |
| wolfSSL | 15:117db924cf7c | 1350 | return 4; /* length of key in octets or 0 for error */ |
| wolfSSL | 15:117db924cf7c | 1351 | } |
| wolfSSL | 15:117db924cf7c | 1352 | else { |
| wolfSSL | 15:117db924cf7c | 1353 | int i; |
| wolfSSL | 15:117db924cf7c | 1354 | int b = 0x01; |
| wolfSSL | 15:117db924cf7c | 1355 | |
| wolfSSL | 15:117db924cf7c | 1356 | for (i = 0; i < 32; i++, b += 0x22) { |
| wolfSSL | 15:117db924cf7c | 1357 | if (b >= 0x100) |
| wolfSSL | 15:117db924cf7c | 1358 | b = 0x01; |
| wolfSSL | 15:117db924cf7c | 1359 | key[i] = b; |
| wolfSSL | 15:117db924cf7c | 1360 | } |
| wolfSSL | 15:117db924cf7c | 1361 | |
| wolfSSL | 15:117db924cf7c | 1362 | return 32; /* length of key in octets or 0 for error */ |
| wolfSSL | 15:117db924cf7c | 1363 | } |
| wolfSSL | 15:117db924cf7c | 1364 | } |
| wolfSSL | 15:117db924cf7c | 1365 | |
| wolfSSL | 16:8e0d178b1d1e | 1366 | |
| wolfSSL | 16:8e0d178b1d1e | 1367 | static WC_INLINE unsigned int my_psk_client_tls13_cb(WOLFSSL* ssl, |
| wolfSSL | 16:8e0d178b1d1e | 1368 | const char* hint, char* identity, unsigned int id_max_len, |
| wolfSSL | 16:8e0d178b1d1e | 1369 | unsigned char* key, unsigned int key_max_len, const char** ciphersuite) |
| wolfSSL | 16:8e0d178b1d1e | 1370 | { |
| wolfSSL | 16:8e0d178b1d1e | 1371 | int i; |
| wolfSSL | 16:8e0d178b1d1e | 1372 | int b = 0x01; |
| wolfSSL | 16:8e0d178b1d1e | 1373 | |
| wolfSSL | 16:8e0d178b1d1e | 1374 | (void)ssl; |
| wolfSSL | 16:8e0d178b1d1e | 1375 | (void)hint; |
| wolfSSL | 16:8e0d178b1d1e | 1376 | (void)key_max_len; |
| wolfSSL | 16:8e0d178b1d1e | 1377 | |
| wolfSSL | 16:8e0d178b1d1e | 1378 | /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */ |
| wolfSSL | 16:8e0d178b1d1e | 1379 | strncpy(identity, kIdentityStr, id_max_len); |
| wolfSSL | 16:8e0d178b1d1e | 1380 | |
| wolfSSL | 16:8e0d178b1d1e | 1381 | for (i = 0; i < 32; i++, b += 0x22) { |
| wolfSSL | 16:8e0d178b1d1e | 1382 | if (b >= 0x100) |
| wolfSSL | 16:8e0d178b1d1e | 1383 | b = 0x01; |
| wolfSSL | 16:8e0d178b1d1e | 1384 | key[i] = b; |
| wolfSSL | 16:8e0d178b1d1e | 1385 | } |
| wolfSSL | 16:8e0d178b1d1e | 1386 | |
| wolfSSL | 16:8e0d178b1d1e | 1387 | *ciphersuite = "TLS13-AES128-GCM-SHA256"; |
| wolfSSL | 16:8e0d178b1d1e | 1388 | |
| wolfSSL | 16:8e0d178b1d1e | 1389 | return 32; /* length of key in octets or 0 for error */ |
| wolfSSL | 16:8e0d178b1d1e | 1390 | } |
| wolfSSL | 16:8e0d178b1d1e | 1391 | |
| wolfSSL | 16:8e0d178b1d1e | 1392 | |
| wolfSSL | 16:8e0d178b1d1e | 1393 | static WC_INLINE unsigned int my_psk_server_tls13_cb(WOLFSSL* ssl, |
| wolfSSL | 16:8e0d178b1d1e | 1394 | const char* identity, unsigned char* key, unsigned int key_max_len, |
| wolfSSL | 16:8e0d178b1d1e | 1395 | const char** ciphersuite) |
| wolfSSL | 16:8e0d178b1d1e | 1396 | { |
| wolfSSL | 16:8e0d178b1d1e | 1397 | int i; |
| wolfSSL | 16:8e0d178b1d1e | 1398 | int b = 0x01; |
| wolfSSL | 16:8e0d178b1d1e | 1399 | |
| wolfSSL | 16:8e0d178b1d1e | 1400 | (void)ssl; |
| wolfSSL | 16:8e0d178b1d1e | 1401 | (void)key_max_len; |
| wolfSSL | 16:8e0d178b1d1e | 1402 | |
| wolfSSL | 16:8e0d178b1d1e | 1403 | /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */ |
| wolfSSL | 16:8e0d178b1d1e | 1404 | if (strncmp(identity, kIdentityStr, strlen(kIdentityStr)) != 0) |
| wolfSSL | 16:8e0d178b1d1e | 1405 | return 0; |
| wolfSSL | 16:8e0d178b1d1e | 1406 | |
| wolfSSL | 16:8e0d178b1d1e | 1407 | for (i = 0; i < 32; i++, b += 0x22) { |
| wolfSSL | 16:8e0d178b1d1e | 1408 | if (b >= 0x100) |
| wolfSSL | 16:8e0d178b1d1e | 1409 | b = 0x01; |
| wolfSSL | 16:8e0d178b1d1e | 1410 | key[i] = b; |
| wolfSSL | 16:8e0d178b1d1e | 1411 | } |
| wolfSSL | 16:8e0d178b1d1e | 1412 | |
| wolfSSL | 16:8e0d178b1d1e | 1413 | *ciphersuite = "TLS13-AES128-GCM-SHA256"; |
| wolfSSL | 16:8e0d178b1d1e | 1414 | |
| wolfSSL | 16:8e0d178b1d1e | 1415 | return 32; /* length of key in octets or 0 for error */ |
| wolfSSL | 16:8e0d178b1d1e | 1416 | } |
| wolfSSL | 16:8e0d178b1d1e | 1417 | |
| wolfSSL | 15:117db924cf7c | 1418 | #endif /* NO_PSK */ |
| wolfSSL | 15:117db924cf7c | 1419 | |
| wolfSSL | 15:117db924cf7c | 1420 | |
| wolfSSL | 15:117db924cf7c | 1421 | #if defined(WOLFSSL_USER_CURRTIME) |
| wolfSSL | 15:117db924cf7c | 1422 | extern double current_time(int reset); |
| wolfSSL | 15:117db924cf7c | 1423 | |
| wolfSSL | 15:117db924cf7c | 1424 | #elif defined(USE_WINDOWS_API) |
| wolfSSL | 15:117db924cf7c | 1425 | |
| wolfSSL | 15:117db924cf7c | 1426 | #define WIN32_LEAN_AND_MEAN |
| wolfSSL | 15:117db924cf7c | 1427 | #include <windows.h> |
| wolfSSL | 15:117db924cf7c | 1428 | |
| wolfSSL | 15:117db924cf7c | 1429 | static WC_INLINE double current_time(int reset) |
| wolfSSL | 15:117db924cf7c | 1430 | { |
| wolfSSL | 15:117db924cf7c | 1431 | static int init = 0; |
| wolfSSL | 15:117db924cf7c | 1432 | static LARGE_INTEGER freq; |
| wolfSSL | 15:117db924cf7c | 1433 | |
| wolfSSL | 15:117db924cf7c | 1434 | LARGE_INTEGER count; |
| wolfSSL | 15:117db924cf7c | 1435 | |
| wolfSSL | 15:117db924cf7c | 1436 | if (!init) { |
| wolfSSL | 15:117db924cf7c | 1437 | QueryPerformanceFrequency(&freq); |
| wolfSSL | 15:117db924cf7c | 1438 | init = 1; |
| wolfSSL | 15:117db924cf7c | 1439 | } |
| wolfSSL | 15:117db924cf7c | 1440 | |
| wolfSSL | 15:117db924cf7c | 1441 | QueryPerformanceCounter(&count); |
| wolfSSL | 15:117db924cf7c | 1442 | |
| wolfSSL | 15:117db924cf7c | 1443 | (void)reset; |
| wolfSSL | 15:117db924cf7c | 1444 | return (double)count.QuadPart / freq.QuadPart; |
| wolfSSL | 15:117db924cf7c | 1445 | } |
| wolfSSL | 15:117db924cf7c | 1446 | |
| wolfSSL | 15:117db924cf7c | 1447 | #elif defined(WOLFSSL_TIRTOS) |
| wolfSSL | 15:117db924cf7c | 1448 | extern double current_time(); |
| wolfSSL | 16:8e0d178b1d1e | 1449 | #elif defined(WOLFSSL_ZEPHYR) |
| wolfSSL | 16:8e0d178b1d1e | 1450 | extern double current_time(); |
| wolfSSL | 15:117db924cf7c | 1451 | #else |
| wolfSSL | 15:117db924cf7c | 1452 | |
| wolfSSL | 15:117db924cf7c | 1453 | #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_TCP_NET) && !defined(WOLFSSL_CHIBIOS) |
| wolfSSL | 15:117db924cf7c | 1454 | #include <sys/time.h> |
| wolfSSL | 15:117db924cf7c | 1455 | |
| wolfSSL | 15:117db924cf7c | 1456 | static WC_INLINE double current_time(int reset) |
| wolfSSL | 15:117db924cf7c | 1457 | { |
| wolfSSL | 15:117db924cf7c | 1458 | struct timeval tv; |
| wolfSSL | 15:117db924cf7c | 1459 | gettimeofday(&tv, 0); |
| wolfSSL | 15:117db924cf7c | 1460 | (void)reset; |
| wolfSSL | 15:117db924cf7c | 1461 | |
| wolfSSL | 15:117db924cf7c | 1462 | return (double)tv.tv_sec + (double)tv.tv_usec / 1000000; |
| wolfSSL | 15:117db924cf7c | 1463 | } |
| wolfSSL | 15:117db924cf7c | 1464 | #else |
| wolfSSL | 15:117db924cf7c | 1465 | extern double current_time(int reset); |
| wolfSSL | 15:117db924cf7c | 1466 | #endif |
| wolfSSL | 15:117db924cf7c | 1467 | #endif /* USE_WINDOWS_API */ |
| wolfSSL | 15:117db924cf7c | 1468 | |
| wolfSSL | 15:117db924cf7c | 1469 | |
| wolfSSL | 15:117db924cf7c | 1470 | #if defined(HAVE_OCSP) && defined(WOLFSSL_NONBLOCK_OCSP) |
| wolfSSL | 15:117db924cf7c | 1471 | static WC_INLINE int OCSPIOCb(void* ioCtx, const char* url, int urlSz, |
| wolfSSL | 15:117db924cf7c | 1472 | unsigned char* request, int requestSz, unsigned char** response) |
| wolfSSL | 15:117db924cf7c | 1473 | { |
| wolfSSL | 15:117db924cf7c | 1474 | #ifdef TEST_NONBLOCK_CERTS |
| wolfSSL | 15:117db924cf7c | 1475 | static int ioCbCnt = 0; |
| wolfSSL | 15:117db924cf7c | 1476 | #endif |
| wolfSSL | 15:117db924cf7c | 1477 | |
| wolfSSL | 15:117db924cf7c | 1478 | (void)ioCtx; |
| wolfSSL | 15:117db924cf7c | 1479 | (void)url; |
| wolfSSL | 15:117db924cf7c | 1480 | (void)urlSz; |
| wolfSSL | 15:117db924cf7c | 1481 | (void)request; |
| wolfSSL | 15:117db924cf7c | 1482 | (void)requestSz; |
| wolfSSL | 15:117db924cf7c | 1483 | (void)response; |
| wolfSSL | 15:117db924cf7c | 1484 | |
| wolfSSL | 15:117db924cf7c | 1485 | #ifdef TEST_NONBLOCK_CERTS |
| wolfSSL | 15:117db924cf7c | 1486 | if (ioCbCnt) { |
| wolfSSL | 15:117db924cf7c | 1487 | ioCbCnt = 0; |
| wolfSSL | 15:117db924cf7c | 1488 | return EmbedOcspLookup(ioCtx, url, urlSz, request, requestSz, response); |
| wolfSSL | 15:117db924cf7c | 1489 | } |
| wolfSSL | 15:117db924cf7c | 1490 | else { |
| wolfSSL | 15:117db924cf7c | 1491 | ioCbCnt = 1; |
| wolfSSL | 15:117db924cf7c | 1492 | return WOLFSSL_CBIO_ERR_WANT_READ; |
| wolfSSL | 15:117db924cf7c | 1493 | } |
| wolfSSL | 15:117db924cf7c | 1494 | #else |
| wolfSSL | 15:117db924cf7c | 1495 | return EmbedOcspLookup(ioCtx, url, urlSz, request, requestSz, response); |
| wolfSSL | 15:117db924cf7c | 1496 | #endif |
| wolfSSL | 15:117db924cf7c | 1497 | } |
| wolfSSL | 15:117db924cf7c | 1498 | |
| wolfSSL | 15:117db924cf7c | 1499 | static WC_INLINE void OCSPRespFreeCb(void* ioCtx, unsigned char* response) |
| wolfSSL | 15:117db924cf7c | 1500 | { |
| wolfSSL | 15:117db924cf7c | 1501 | (void)ioCtx; |
| wolfSSL | 15:117db924cf7c | 1502 | (void)response; |
| wolfSSL | 15:117db924cf7c | 1503 | } |
| wolfSSL | 15:117db924cf7c | 1504 | #endif |
| wolfSSL | 15:117db924cf7c | 1505 | |
| wolfSSL | 15:117db924cf7c | 1506 | #if !defined(NO_CERTS) |
| wolfSSL | 15:117db924cf7c | 1507 | #if !defined(NO_FILESYSTEM) || \ |
| wolfSSL | 15:117db924cf7c | 1508 | (defined(NO_FILESYSTEM) && defined(FORCE_BUFFER_TEST)) |
| wolfSSL | 15:117db924cf7c | 1509 | |
| wolfSSL | 15:117db924cf7c | 1510 | /* reads file size, allocates buffer, reads into buffer, returns buffer */ |
| wolfSSL | 15:117db924cf7c | 1511 | static WC_INLINE int load_file(const char* fname, byte** buf, size_t* bufLen) |
| wolfSSL | 15:117db924cf7c | 1512 | { |
| wolfSSL | 15:117db924cf7c | 1513 | int ret; |
| wolfSSL | 15:117db924cf7c | 1514 | long int fileSz; |
| wolfSSL | 16:8e0d178b1d1e | 1515 | XFILE file; |
| wolfSSL | 15:117db924cf7c | 1516 | |
| wolfSSL | 15:117db924cf7c | 1517 | if (fname == NULL || buf == NULL || bufLen == NULL) |
| wolfSSL | 15:117db924cf7c | 1518 | return BAD_FUNC_ARG; |
| wolfSSL | 15:117db924cf7c | 1519 | |
| wolfSSL | 15:117db924cf7c | 1520 | /* set defaults */ |
| wolfSSL | 15:117db924cf7c | 1521 | *buf = NULL; |
| wolfSSL | 15:117db924cf7c | 1522 | *bufLen = 0; |
| wolfSSL | 15:117db924cf7c | 1523 | |
| wolfSSL | 15:117db924cf7c | 1524 | /* open file (read-only binary) */ |
| wolfSSL | 16:8e0d178b1d1e | 1525 | file = XFOPEN(fname, "rb"); |
| wolfSSL | 15:117db924cf7c | 1526 | if (!file) { |
| wolfSSL | 15:117db924cf7c | 1527 | printf("Error loading %s\n", fname); |
| wolfSSL | 15:117db924cf7c | 1528 | return BAD_PATH_ERROR; |
| wolfSSL | 15:117db924cf7c | 1529 | } |
| wolfSSL | 15:117db924cf7c | 1530 | |
| wolfSSL | 15:117db924cf7c | 1531 | fseek(file, 0, SEEK_END); |
| wolfSSL | 15:117db924cf7c | 1532 | fileSz = (int)ftell(file); |
| wolfSSL | 15:117db924cf7c | 1533 | rewind(file); |
| wolfSSL | 15:117db924cf7c | 1534 | if (fileSz > 0) { |
| wolfSSL | 15:117db924cf7c | 1535 | *bufLen = (size_t)fileSz; |
| wolfSSL | 15:117db924cf7c | 1536 | *buf = (byte*)malloc(*bufLen); |
| wolfSSL | 15:117db924cf7c | 1537 | if (*buf == NULL) { |
| wolfSSL | 15:117db924cf7c | 1538 | ret = MEMORY_E; |
| wolfSSL | 15:117db924cf7c | 1539 | printf("Error allocating %lu bytes\n", (unsigned long)*bufLen); |
| wolfSSL | 15:117db924cf7c | 1540 | } |
| wolfSSL | 15:117db924cf7c | 1541 | else { |
| wolfSSL | 15:117db924cf7c | 1542 | size_t readLen = fread(*buf, *bufLen, 1, file); |
| wolfSSL | 15:117db924cf7c | 1543 | |
| wolfSSL | 15:117db924cf7c | 1544 | /* check response code */ |
| wolfSSL | 15:117db924cf7c | 1545 | ret = (readLen > 0) ? 0 : -1; |
| wolfSSL | 15:117db924cf7c | 1546 | } |
| wolfSSL | 15:117db924cf7c | 1547 | } |
| wolfSSL | 15:117db924cf7c | 1548 | else { |
| wolfSSL | 15:117db924cf7c | 1549 | ret = BUFFER_E; |
| wolfSSL | 15:117db924cf7c | 1550 | } |
| wolfSSL | 15:117db924cf7c | 1551 | fclose(file); |
| wolfSSL | 15:117db924cf7c | 1552 | |
| wolfSSL | 15:117db924cf7c | 1553 | return ret; |
| wolfSSL | 15:117db924cf7c | 1554 | } |
| wolfSSL | 15:117db924cf7c | 1555 | |
| wolfSSL | 15:117db924cf7c | 1556 | enum { |
| wolfSSL | 15:117db924cf7c | 1557 | WOLFSSL_CA = 1, |
| wolfSSL | 15:117db924cf7c | 1558 | WOLFSSL_CERT = 2, |
| wolfSSL | 15:117db924cf7c | 1559 | WOLFSSL_KEY = 3, |
| wolfSSL | 15:117db924cf7c | 1560 | WOLFSSL_CERT_CHAIN = 4, |
| wolfSSL | 15:117db924cf7c | 1561 | }; |
| wolfSSL | 15:117db924cf7c | 1562 | |
| wolfSSL | 15:117db924cf7c | 1563 | static WC_INLINE void load_buffer(WOLFSSL_CTX* ctx, const char* fname, int type) |
| wolfSSL | 15:117db924cf7c | 1564 | { |
| wolfSSL | 15:117db924cf7c | 1565 | int format = WOLFSSL_FILETYPE_PEM; |
| wolfSSL | 15:117db924cf7c | 1566 | byte* buff = NULL; |
| wolfSSL | 15:117db924cf7c | 1567 | size_t sz = 0; |
| wolfSSL | 15:117db924cf7c | 1568 | |
| wolfSSL | 15:117db924cf7c | 1569 | if (load_file(fname, &buff, &sz) != 0) { |
| wolfSSL | 15:117db924cf7c | 1570 | err_sys("can't open file for buffer load " |
| wolfSSL | 15:117db924cf7c | 1571 | "Please run from wolfSSL home directory if not"); |
| wolfSSL | 15:117db924cf7c | 1572 | } |
| wolfSSL | 15:117db924cf7c | 1573 | |
| wolfSSL | 15:117db924cf7c | 1574 | /* determine format */ |
| wolfSSL | 15:117db924cf7c | 1575 | if (strstr(fname, ".der")) |
| wolfSSL | 15:117db924cf7c | 1576 | format = WOLFSSL_FILETYPE_ASN1; |
| wolfSSL | 15:117db924cf7c | 1577 | |
| wolfSSL | 15:117db924cf7c | 1578 | if (type == WOLFSSL_CA) { |
| wolfSSL | 15:117db924cf7c | 1579 | if (wolfSSL_CTX_load_verify_buffer(ctx, buff, (long)sz, format) |
| wolfSSL | 15:117db924cf7c | 1580 | != WOLFSSL_SUCCESS) |
| wolfSSL | 15:117db924cf7c | 1581 | err_sys("can't load buffer ca file"); |
| wolfSSL | 15:117db924cf7c | 1582 | } |
| wolfSSL | 15:117db924cf7c | 1583 | else if (type == WOLFSSL_CERT) { |
| wolfSSL | 15:117db924cf7c | 1584 | if (wolfSSL_CTX_use_certificate_buffer(ctx, buff, (long)sz, |
| wolfSSL | 15:117db924cf7c | 1585 | format) != WOLFSSL_SUCCESS) |
| wolfSSL | 15:117db924cf7c | 1586 | err_sys("can't load buffer cert file"); |
| wolfSSL | 15:117db924cf7c | 1587 | } |
| wolfSSL | 15:117db924cf7c | 1588 | else if (type == WOLFSSL_KEY) { |
| wolfSSL | 15:117db924cf7c | 1589 | if (wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, (long)sz, |
| wolfSSL | 15:117db924cf7c | 1590 | format) != WOLFSSL_SUCCESS) |
| wolfSSL | 15:117db924cf7c | 1591 | err_sys("can't load buffer key file"); |
| wolfSSL | 15:117db924cf7c | 1592 | } |
| wolfSSL | 15:117db924cf7c | 1593 | else if (type == WOLFSSL_CERT_CHAIN) { |
| wolfSSL | 15:117db924cf7c | 1594 | if (wolfSSL_CTX_use_certificate_chain_buffer_format(ctx, buff, |
| wolfSSL | 15:117db924cf7c | 1595 | (long)sz, format) != WOLFSSL_SUCCESS) |
| wolfSSL | 15:117db924cf7c | 1596 | err_sys("can't load cert chain buffer"); |
| wolfSSL | 15:117db924cf7c | 1597 | } |
| wolfSSL | 15:117db924cf7c | 1598 | |
| wolfSSL | 15:117db924cf7c | 1599 | if (buff) |
| wolfSSL | 15:117db924cf7c | 1600 | free(buff); |
| wolfSSL | 15:117db924cf7c | 1601 | } |
| wolfSSL | 15:117db924cf7c | 1602 | |
| wolfSSL | 16:8e0d178b1d1e | 1603 | static WC_INLINE void load_ssl_buffer(WOLFSSL* ssl, const char* fname, int type) |
| wolfSSL | 16:8e0d178b1d1e | 1604 | { |
| wolfSSL | 16:8e0d178b1d1e | 1605 | int format = WOLFSSL_FILETYPE_PEM; |
| wolfSSL | 16:8e0d178b1d1e | 1606 | byte* buff = NULL; |
| wolfSSL | 16:8e0d178b1d1e | 1607 | size_t sz = 0; |
| wolfSSL | 16:8e0d178b1d1e | 1608 | |
| wolfSSL | 16:8e0d178b1d1e | 1609 | if (load_file(fname, &buff, &sz) != 0) { |
| wolfSSL | 16:8e0d178b1d1e | 1610 | err_sys("can't open file for buffer load " |
| wolfSSL | 16:8e0d178b1d1e | 1611 | "Please run from wolfSSL home directory if not"); |
| wolfSSL | 16:8e0d178b1d1e | 1612 | } |
| wolfSSL | 16:8e0d178b1d1e | 1613 | |
| wolfSSL | 16:8e0d178b1d1e | 1614 | /* determine format */ |
| wolfSSL | 16:8e0d178b1d1e | 1615 | if (strstr(fname, ".der")) |
| wolfSSL | 16:8e0d178b1d1e | 1616 | format = WOLFSSL_FILETYPE_ASN1; |
| wolfSSL | 16:8e0d178b1d1e | 1617 | |
| wolfSSL | 16:8e0d178b1d1e | 1618 | if (type == WOLFSSL_CA) { |
| wolfSSL | 16:8e0d178b1d1e | 1619 | /* verify certs (CA's) use the shared ctx->cm (WOLFSSL_CERT_MANAGER) */ |
| wolfSSL | 16:8e0d178b1d1e | 1620 | WOLFSSL_CTX* ctx = wolfSSL_get_SSL_CTX(ssl); |
| wolfSSL | 16:8e0d178b1d1e | 1621 | if (wolfSSL_CTX_load_verify_buffer(ctx, buff, (long)sz, format) |
| wolfSSL | 16:8e0d178b1d1e | 1622 | != WOLFSSL_SUCCESS) |
| wolfSSL | 16:8e0d178b1d1e | 1623 | err_sys("can't load buffer ca file"); |
| wolfSSL | 16:8e0d178b1d1e | 1624 | } |
| wolfSSL | 16:8e0d178b1d1e | 1625 | else if (type == WOLFSSL_CERT) { |
| wolfSSL | 16:8e0d178b1d1e | 1626 | if (wolfSSL_use_certificate_buffer(ssl, buff, (long)sz, |
| wolfSSL | 16:8e0d178b1d1e | 1627 | format) != WOLFSSL_SUCCESS) |
| wolfSSL | 16:8e0d178b1d1e | 1628 | err_sys("can't load buffer cert file"); |
| wolfSSL | 16:8e0d178b1d1e | 1629 | } |
| wolfSSL | 16:8e0d178b1d1e | 1630 | else if (type == WOLFSSL_KEY) { |
| wolfSSL | 16:8e0d178b1d1e | 1631 | if (wolfSSL_use_PrivateKey_buffer(ssl, buff, (long)sz, |
| wolfSSL | 16:8e0d178b1d1e | 1632 | format) != WOLFSSL_SUCCESS) |
| wolfSSL | 16:8e0d178b1d1e | 1633 | err_sys("can't load buffer key file"); |
| wolfSSL | 16:8e0d178b1d1e | 1634 | } |
| wolfSSL | 16:8e0d178b1d1e | 1635 | else if (type == WOLFSSL_CERT_CHAIN) { |
| wolfSSL | 16:8e0d178b1d1e | 1636 | if (wolfSSL_use_certificate_chain_buffer_format(ssl, buff, |
| wolfSSL | 16:8e0d178b1d1e | 1637 | (long)sz, format) != WOLFSSL_SUCCESS) |
| wolfSSL | 16:8e0d178b1d1e | 1638 | err_sys("can't load cert chain buffer"); |
| wolfSSL | 16:8e0d178b1d1e | 1639 | } |
| wolfSSL | 16:8e0d178b1d1e | 1640 | |
| wolfSSL | 16:8e0d178b1d1e | 1641 | if (buff) |
| wolfSSL | 16:8e0d178b1d1e | 1642 | free(buff); |
| wolfSSL | 16:8e0d178b1d1e | 1643 | } |
| wolfSSL | 16:8e0d178b1d1e | 1644 | |
| wolfSSL | 15:117db924cf7c | 1645 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 1646 | static WC_INLINE int load_key_file(const char* fname, byte** derBuf, word32* derLen) |
| wolfSSL | 15:117db924cf7c | 1647 | { |
| wolfSSL | 15:117db924cf7c | 1648 | int ret; |
| wolfSSL | 15:117db924cf7c | 1649 | byte* buf = NULL; |
| wolfSSL | 15:117db924cf7c | 1650 | size_t bufLen; |
| wolfSSL | 15:117db924cf7c | 1651 | |
| wolfSSL | 15:117db924cf7c | 1652 | ret = load_file(fname, &buf, &bufLen); |
| wolfSSL | 15:117db924cf7c | 1653 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 1654 | return ret; |
| wolfSSL | 15:117db924cf7c | 1655 | |
| wolfSSL | 15:117db924cf7c | 1656 | *derBuf = (byte*)malloc(bufLen); |
| wolfSSL | 15:117db924cf7c | 1657 | if (*derBuf == NULL) { |
| wolfSSL | 15:117db924cf7c | 1658 | free(buf); |
| wolfSSL | 15:117db924cf7c | 1659 | return MEMORY_E; |
| wolfSSL | 15:117db924cf7c | 1660 | } |
| wolfSSL | 15:117db924cf7c | 1661 | |
| wolfSSL | 15:117db924cf7c | 1662 | ret = wc_KeyPemToDer(buf, (word32)bufLen, *derBuf, (word32)bufLen, NULL); |
| wolfSSL | 15:117db924cf7c | 1663 | if (ret < 0) { |
| wolfSSL | 15:117db924cf7c | 1664 | free(buf); |
| wolfSSL | 15:117db924cf7c | 1665 | free(*derBuf); |
| wolfSSL | 15:117db924cf7c | 1666 | return ret; |
| wolfSSL | 15:117db924cf7c | 1667 | } |
| wolfSSL | 15:117db924cf7c | 1668 | *derLen = ret; |
| wolfSSL | 15:117db924cf7c | 1669 | free(buf); |
| wolfSSL | 15:117db924cf7c | 1670 | |
| wolfSSL | 15:117db924cf7c | 1671 | return 0; |
| wolfSSL | 15:117db924cf7c | 1672 | } |
| wolfSSL | 15:117db924cf7c | 1673 | #endif /* TEST_PK_PRIVKEY */ |
| wolfSSL | 15:117db924cf7c | 1674 | |
| wolfSSL | 15:117db924cf7c | 1675 | #endif /* !NO_FILESYSTEM || (NO_FILESYSTEM && FORCE_BUFFER_TEST) */ |
| wolfSSL | 15:117db924cf7c | 1676 | #endif /* !NO_CERTS */ |
| wolfSSL | 15:117db924cf7c | 1677 | |
| wolfSSL | 16:8e0d178b1d1e | 1678 | static int myVerifyFail = 0; |
| wolfSSL | 16:8e0d178b1d1e | 1679 | |
| wolfSSL | 16:8e0d178b1d1e | 1680 | /* The verify callback is called for every certificate only when |
| wolfSSL | 16:8e0d178b1d1e | 1681 | * --enable-opensslextra is defined because it sets WOLFSSL_ALWAYS_VERIFY_CB and |
| wolfSSL | 16:8e0d178b1d1e | 1682 | * WOLFSSL_VERIFY_CB_ALL_CERTS. |
| wolfSSL | 16:8e0d178b1d1e | 1683 | * Normal cases of the verify callback only occur on certificate failures when the |
| wolfSSL | 16:8e0d178b1d1e | 1684 | * wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, myVerifyCb); is called |
| wolfSSL | 16:8e0d178b1d1e | 1685 | */ |
| wolfSSL | 16:8e0d178b1d1e | 1686 | |
| wolfSSL | 15:117db924cf7c | 1687 | static WC_INLINE int myVerify(int preverify, WOLFSSL_X509_STORE_CTX* store) |
| wolfSSL | 15:117db924cf7c | 1688 | { |
| wolfSSL | 15:117db924cf7c | 1689 | char buffer[WOLFSSL_MAX_ERROR_SZ]; |
| wolfSSL | 16:8e0d178b1d1e | 1690 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
| wolfSSL | 15:117db924cf7c | 1691 | WOLFSSL_X509* peer; |
| wolfSSL | 16:8e0d178b1d1e | 1692 | #if defined(SHOW_CERTS) && !defined(NO_FILESYSTEM) |
| wolfSSL | 16:8e0d178b1d1e | 1693 | WOLFSSL_BIO* bio = NULL; |
| wolfSSL | 16:8e0d178b1d1e | 1694 | WOLFSSL_STACK* sk = NULL; |
| wolfSSL | 16:8e0d178b1d1e | 1695 | X509* x509 = NULL; |
| wolfSSL | 16:8e0d178b1d1e | 1696 | int i = 0; |
| wolfSSL | 16:8e0d178b1d1e | 1697 | #endif |
| wolfSSL | 15:117db924cf7c | 1698 | #endif |
| wolfSSL | 15:117db924cf7c | 1699 | (void)preverify; |
| wolfSSL | 15:117db924cf7c | 1700 | |
| wolfSSL | 16:8e0d178b1d1e | 1701 | /* Verify Callback Arguments: |
| wolfSSL | 16:8e0d178b1d1e | 1702 | * preverify: 1=Verify Okay, 0=Failure |
| wolfSSL | 16:8e0d178b1d1e | 1703 | * store->error: Failure error code (0 indicates no failure) |
| wolfSSL | 16:8e0d178b1d1e | 1704 | * store->current_cert: Current WOLFSSL_X509 object (only with OPENSSL_EXTRA) |
| wolfSSL | 16:8e0d178b1d1e | 1705 | * store->error_depth: Current Index |
| wolfSSL | 16:8e0d178b1d1e | 1706 | * store->domain: Subject CN as string (null term) |
| wolfSSL | 16:8e0d178b1d1e | 1707 | * store->totalCerts: Number of certs presented by peer |
| wolfSSL | 16:8e0d178b1d1e | 1708 | * store->certs[i]: A `WOLFSSL_BUFFER_INFO` with plain DER for each cert |
| wolfSSL | 16:8e0d178b1d1e | 1709 | * store->store: WOLFSSL_X509_STORE with CA cert chain |
| wolfSSL | 16:8e0d178b1d1e | 1710 | * store->store->cm: WOLFSSL_CERT_MANAGER |
| wolfSSL | 16:8e0d178b1d1e | 1711 | * store->ex_data: The WOLFSSL object pointer |
| wolfSSL | 16:8e0d178b1d1e | 1712 | * store->discardSessionCerts: When set to non-zero value session certs |
| wolfSSL | 16:8e0d178b1d1e | 1713 | will be discarded (only with SESSION_CERTS) |
| wolfSSL | 16:8e0d178b1d1e | 1714 | */ |
| wolfSSL | 16:8e0d178b1d1e | 1715 | |
| wolfSSL | 15:117db924cf7c | 1716 | printf("In verification callback, error = %d, %s\n", store->error, |
| wolfSSL | 15:117db924cf7c | 1717 | wolfSSL_ERR_error_string(store->error, buffer)); |
| wolfSSL | 16:8e0d178b1d1e | 1718 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
| wolfSSL | 15:117db924cf7c | 1719 | peer = store->current_cert; |
| wolfSSL | 15:117db924cf7c | 1720 | if (peer) { |
| wolfSSL | 15:117db924cf7c | 1721 | char* issuer = wolfSSL_X509_NAME_oneline( |
| wolfSSL | 15:117db924cf7c | 1722 | wolfSSL_X509_get_issuer_name(peer), 0, 0); |
| wolfSSL | 15:117db924cf7c | 1723 | char* subject = wolfSSL_X509_NAME_oneline( |
| wolfSSL | 15:117db924cf7c | 1724 | wolfSSL_X509_get_subject_name(peer), 0, 0); |
| wolfSSL | 15:117db924cf7c | 1725 | printf("\tPeer's cert info:\n issuer : %s\n subject: %s\n", issuer, |
| wolfSSL | 15:117db924cf7c | 1726 | subject); |
| wolfSSL | 15:117db924cf7c | 1727 | XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL); |
| wolfSSL | 15:117db924cf7c | 1728 | XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL); |
| wolfSSL | 16:8e0d178b1d1e | 1729 | #if defined(SHOW_CERTS) && !defined(NO_FILESYSTEM) |
| wolfSSL | 16:8e0d178b1d1e | 1730 | /* avoid printing duplicate certs */ |
| wolfSSL | 16:8e0d178b1d1e | 1731 | if (store->depth == 1) { |
| wolfSSL | 16:8e0d178b1d1e | 1732 | /* retrieve x509 certs and display them on stdout */ |
| wolfSSL | 16:8e0d178b1d1e | 1733 | sk = wolfSSL_X509_STORE_GetCerts(store); |
| wolfSSL | 16:8e0d178b1d1e | 1734 | |
| wolfSSL | 16:8e0d178b1d1e | 1735 | for (i = 0; i < wolfSSL_sk_X509_num(sk); i++) { |
| wolfSSL | 16:8e0d178b1d1e | 1736 | x509 = wolfSSL_sk_X509_value(sk, i); |
| wolfSSL | 16:8e0d178b1d1e | 1737 | bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file()); |
| wolfSSL | 16:8e0d178b1d1e | 1738 | if (bio != NULL) { |
| wolfSSL | 16:8e0d178b1d1e | 1739 | wolfSSL_BIO_set_fp(bio, stdout, BIO_NOCLOSE); |
| wolfSSL | 16:8e0d178b1d1e | 1740 | wolfSSL_X509_print(bio, x509); |
| wolfSSL | 16:8e0d178b1d1e | 1741 | wolfSSL_BIO_free(bio); |
| wolfSSL | 16:8e0d178b1d1e | 1742 | } |
| wolfSSL | 16:8e0d178b1d1e | 1743 | } |
| wolfSSL | 16:8e0d178b1d1e | 1744 | wolfSSL_sk_X509_free(sk); |
| wolfSSL | 16:8e0d178b1d1e | 1745 | } |
| wolfSSL | 16:8e0d178b1d1e | 1746 | #endif |
| wolfSSL | 15:117db924cf7c | 1747 | } |
| wolfSSL | 15:117db924cf7c | 1748 | else |
| wolfSSL | 15:117db924cf7c | 1749 | printf("\tPeer has no cert!\n"); |
| wolfSSL | 15:117db924cf7c | 1750 | #else |
| wolfSSL | 15:117db924cf7c | 1751 | printf("\tPeer certs: %d\n", store->totalCerts); |
| wolfSSL | 15:117db924cf7c | 1752 | #ifdef SHOW_CERTS |
| wolfSSL | 15:117db924cf7c | 1753 | { int i; |
| wolfSSL | 15:117db924cf7c | 1754 | for (i=0; i<store->totalCerts; i++) { |
| wolfSSL | 15:117db924cf7c | 1755 | WOLFSSL_BUFFER_INFO* cert = &store->certs[i]; |
| wolfSSL | 15:117db924cf7c | 1756 | printf("\t\tCert %d: Ptr %p, Len %u\n", i, cert->buffer, cert->length); |
| wolfSSL | 15:117db924cf7c | 1757 | } |
| wolfSSL | 15:117db924cf7c | 1758 | } |
| wolfSSL | 16:8e0d178b1d1e | 1759 | #endif /* SHOW_CERTS */ |
| wolfSSL | 16:8e0d178b1d1e | 1760 | #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */ |
| wolfSSL | 16:8e0d178b1d1e | 1761 | |
| wolfSSL | 16:8e0d178b1d1e | 1762 | printf("\tSubject's domain name at %d is %s\n", store->error_depth, store->domain); |
| wolfSSL | 16:8e0d178b1d1e | 1763 | |
| wolfSSL | 16:8e0d178b1d1e | 1764 | /* Testing forced fail case by return zero */ |
| wolfSSL | 16:8e0d178b1d1e | 1765 | if (myVerifyFail) { |
| wolfSSL | 16:8e0d178b1d1e | 1766 | return 0; /* test failure case */ |
| wolfSSL | 16:8e0d178b1d1e | 1767 | } |
| wolfSSL | 16:8e0d178b1d1e | 1768 | |
| wolfSSL | 16:8e0d178b1d1e | 1769 | /* If error indicate we are overriding it for testing purposes */ |
| wolfSSL | 16:8e0d178b1d1e | 1770 | if (store->error != 0) { |
| wolfSSL | 16:8e0d178b1d1e | 1771 | printf("\tAllowing failed certificate check, testing only " |
| wolfSSL | 16:8e0d178b1d1e | 1772 | "(shouldn't do this in production)\n"); |
| wolfSSL | 16:8e0d178b1d1e | 1773 | } |
| wolfSSL | 16:8e0d178b1d1e | 1774 | |
| wolfSSL | 16:8e0d178b1d1e | 1775 | /* A non-zero return code indicates failure override */ |
| wolfSSL | 15:117db924cf7c | 1776 | return 1; |
| wolfSSL | 15:117db924cf7c | 1777 | } |
| wolfSSL | 15:117db924cf7c | 1778 | |
| wolfSSL | 15:117db924cf7c | 1779 | |
| wolfSSL | 15:117db924cf7c | 1780 | static WC_INLINE int myDateCb(int preverify, WOLFSSL_X509_STORE_CTX* store) |
| wolfSSL | 15:117db924cf7c | 1781 | { |
| wolfSSL | 15:117db924cf7c | 1782 | char buffer[WOLFSSL_MAX_ERROR_SZ]; |
| wolfSSL | 15:117db924cf7c | 1783 | (void)preverify; |
| wolfSSL | 15:117db924cf7c | 1784 | |
| wolfSSL | 15:117db924cf7c | 1785 | printf("In verification callback, error = %d, %s\n", store->error, |
| wolfSSL | 15:117db924cf7c | 1786 | wolfSSL_ERR_error_string(store->error, buffer)); |
| wolfSSL | 15:117db924cf7c | 1787 | printf("Subject's domain name is %s\n", store->domain); |
| wolfSSL | 15:117db924cf7c | 1788 | |
| wolfSSL | 15:117db924cf7c | 1789 | if (store->error == ASN_BEFORE_DATE_E || store->error == ASN_AFTER_DATE_E) { |
| wolfSSL | 15:117db924cf7c | 1790 | printf("Overriding cert date error as example for bad clock testing\n"); |
| wolfSSL | 15:117db924cf7c | 1791 | return 1; |
| wolfSSL | 15:117db924cf7c | 1792 | } |
| wolfSSL | 15:117db924cf7c | 1793 | printf("Cert error is not date error, not overriding\n"); |
| wolfSSL | 15:117db924cf7c | 1794 | |
| wolfSSL | 15:117db924cf7c | 1795 | return 0; |
| wolfSSL | 15:117db924cf7c | 1796 | } |
| wolfSSL | 15:117db924cf7c | 1797 | |
| wolfSSL | 15:117db924cf7c | 1798 | |
| wolfSSL | 15:117db924cf7c | 1799 | #ifdef HAVE_EXT_CACHE |
| wolfSSL | 15:117db924cf7c | 1800 | |
| wolfSSL | 15:117db924cf7c | 1801 | static WC_INLINE WOLFSSL_SESSION* mySessGetCb(WOLFSSL* ssl, unsigned char* id, |
| wolfSSL | 15:117db924cf7c | 1802 | int id_len, int* copy) |
| wolfSSL | 15:117db924cf7c | 1803 | { |
| wolfSSL | 15:117db924cf7c | 1804 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 1805 | (void)id; |
| wolfSSL | 15:117db924cf7c | 1806 | (void)id_len; |
| wolfSSL | 15:117db924cf7c | 1807 | (void)copy; |
| wolfSSL | 15:117db924cf7c | 1808 | |
| wolfSSL | 15:117db924cf7c | 1809 | /* using internal cache, this is for testing only */ |
| wolfSSL | 15:117db924cf7c | 1810 | return NULL; |
| wolfSSL | 15:117db924cf7c | 1811 | } |
| wolfSSL | 15:117db924cf7c | 1812 | |
| wolfSSL | 15:117db924cf7c | 1813 | static WC_INLINE int mySessNewCb(WOLFSSL* ssl, WOLFSSL_SESSION* session) |
| wolfSSL | 15:117db924cf7c | 1814 | { |
| wolfSSL | 15:117db924cf7c | 1815 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 1816 | (void)session; |
| wolfSSL | 15:117db924cf7c | 1817 | |
| wolfSSL | 15:117db924cf7c | 1818 | /* using internal cache, this is for testing only */ |
| wolfSSL | 15:117db924cf7c | 1819 | return 0; |
| wolfSSL | 15:117db924cf7c | 1820 | } |
| wolfSSL | 15:117db924cf7c | 1821 | |
| wolfSSL | 15:117db924cf7c | 1822 | static WC_INLINE void mySessRemCb(WOLFSSL_CTX* ctx, WOLFSSL_SESSION* session) |
| wolfSSL | 15:117db924cf7c | 1823 | { |
| wolfSSL | 15:117db924cf7c | 1824 | (void)ctx; |
| wolfSSL | 15:117db924cf7c | 1825 | (void)session; |
| wolfSSL | 15:117db924cf7c | 1826 | |
| wolfSSL | 15:117db924cf7c | 1827 | /* using internal cache, this is for testing only */ |
| wolfSSL | 15:117db924cf7c | 1828 | } |
| wolfSSL | 15:117db924cf7c | 1829 | |
| wolfSSL | 15:117db924cf7c | 1830 | #endif /* HAVE_EXT_CACHE */ |
| wolfSSL | 15:117db924cf7c | 1831 | |
| wolfSSL | 15:117db924cf7c | 1832 | |
| wolfSSL | 15:117db924cf7c | 1833 | #ifdef HAVE_CRL |
| wolfSSL | 15:117db924cf7c | 1834 | |
| wolfSSL | 15:117db924cf7c | 1835 | static WC_INLINE void CRL_CallBack(const char* url) |
| wolfSSL | 15:117db924cf7c | 1836 | { |
| wolfSSL | 15:117db924cf7c | 1837 | printf("CRL callback url = %s\n", url); |
| wolfSSL | 15:117db924cf7c | 1838 | } |
| wolfSSL | 15:117db924cf7c | 1839 | |
| wolfSSL | 15:117db924cf7c | 1840 | #endif |
| wolfSSL | 15:117db924cf7c | 1841 | |
| wolfSSL | 15:117db924cf7c | 1842 | #ifndef NO_DH |
| wolfSSL | 15:117db924cf7c | 1843 | static WC_INLINE void SetDH(WOLFSSL* ssl) |
| wolfSSL | 15:117db924cf7c | 1844 | { |
| wolfSSL | 15:117db924cf7c | 1845 | /* dh1024 p */ |
| wolfSSL | 15:117db924cf7c | 1846 | static const unsigned char p[] = |
| wolfSSL | 15:117db924cf7c | 1847 | { |
| wolfSSL | 15:117db924cf7c | 1848 | 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3, |
| wolfSSL | 15:117db924cf7c | 1849 | 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E, |
| wolfSSL | 15:117db924cf7c | 1850 | 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59, |
| wolfSSL | 15:117db924cf7c | 1851 | 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2, |
| wolfSSL | 15:117db924cf7c | 1852 | 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD, |
| wolfSSL | 15:117db924cf7c | 1853 | 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF, |
| wolfSSL | 15:117db924cf7c | 1854 | 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02, |
| wolfSSL | 15:117db924cf7c | 1855 | 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C, |
| wolfSSL | 15:117db924cf7c | 1856 | 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7, |
| wolfSSL | 15:117db924cf7c | 1857 | 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50, |
| wolfSSL | 15:117db924cf7c | 1858 | 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B, |
| wolfSSL | 15:117db924cf7c | 1859 | }; |
| wolfSSL | 15:117db924cf7c | 1860 | |
| wolfSSL | 15:117db924cf7c | 1861 | /* dh1024 g */ |
| wolfSSL | 15:117db924cf7c | 1862 | static const unsigned char g[] = |
| wolfSSL | 15:117db924cf7c | 1863 | { |
| wolfSSL | 15:117db924cf7c | 1864 | 0x02, |
| wolfSSL | 15:117db924cf7c | 1865 | }; |
| wolfSSL | 15:117db924cf7c | 1866 | |
| wolfSSL | 15:117db924cf7c | 1867 | wolfSSL_SetTmpDH(ssl, p, sizeof(p), g, sizeof(g)); |
| wolfSSL | 15:117db924cf7c | 1868 | } |
| wolfSSL | 15:117db924cf7c | 1869 | |
| wolfSSL | 15:117db924cf7c | 1870 | static WC_INLINE void SetDHCtx(WOLFSSL_CTX* ctx) |
| wolfSSL | 15:117db924cf7c | 1871 | { |
| wolfSSL | 15:117db924cf7c | 1872 | /* dh1024 p */ |
| wolfSSL | 15:117db924cf7c | 1873 | static const unsigned char p[] = |
| wolfSSL | 15:117db924cf7c | 1874 | { |
| wolfSSL | 15:117db924cf7c | 1875 | 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3, |
| wolfSSL | 15:117db924cf7c | 1876 | 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E, |
| wolfSSL | 15:117db924cf7c | 1877 | 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59, |
| wolfSSL | 15:117db924cf7c | 1878 | 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2, |
| wolfSSL | 15:117db924cf7c | 1879 | 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD, |
| wolfSSL | 15:117db924cf7c | 1880 | 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF, |
| wolfSSL | 15:117db924cf7c | 1881 | 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02, |
| wolfSSL | 15:117db924cf7c | 1882 | 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C, |
| wolfSSL | 15:117db924cf7c | 1883 | 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7, |
| wolfSSL | 15:117db924cf7c | 1884 | 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50, |
| wolfSSL | 15:117db924cf7c | 1885 | 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B, |
| wolfSSL | 15:117db924cf7c | 1886 | }; |
| wolfSSL | 15:117db924cf7c | 1887 | |
| wolfSSL | 15:117db924cf7c | 1888 | /* dh1024 g */ |
| wolfSSL | 15:117db924cf7c | 1889 | static const unsigned char g[] = |
| wolfSSL | 15:117db924cf7c | 1890 | { |
| wolfSSL | 15:117db924cf7c | 1891 | 0x02, |
| wolfSSL | 15:117db924cf7c | 1892 | }; |
| wolfSSL | 15:117db924cf7c | 1893 | |
| wolfSSL | 15:117db924cf7c | 1894 | wolfSSL_CTX_SetTmpDH(ctx, p, sizeof(p), g, sizeof(g)); |
| wolfSSL | 15:117db924cf7c | 1895 | } |
| wolfSSL | 15:117db924cf7c | 1896 | #endif /* NO_DH */ |
| wolfSSL | 15:117db924cf7c | 1897 | |
| wolfSSL | 15:117db924cf7c | 1898 | #ifndef NO_CERTS |
| wolfSSL | 15:117db924cf7c | 1899 | |
| wolfSSL | 15:117db924cf7c | 1900 | static WC_INLINE void CaCb(unsigned char* der, int sz, int type) |
| wolfSSL | 15:117db924cf7c | 1901 | { |
| wolfSSL | 15:117db924cf7c | 1902 | (void)der; |
| wolfSSL | 15:117db924cf7c | 1903 | printf("Got CA cache add callback, derSz = %d, type = %d\n", sz, type); |
| wolfSSL | 15:117db924cf7c | 1904 | } |
| wolfSSL | 15:117db924cf7c | 1905 | |
| wolfSSL | 15:117db924cf7c | 1906 | #endif /* !NO_CERTS */ |
| wolfSSL | 15:117db924cf7c | 1907 | |
| wolfSSL | 15:117db924cf7c | 1908 | |
| wolfSSL | 15:117db924cf7c | 1909 | /* Wolf Root Directory Helper */ |
| wolfSSL | 15:117db924cf7c | 1910 | /* KEIL-RL File System does not support relative directory */ |
| wolfSSL | 15:117db924cf7c | 1911 | #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOLFSSL_TIRTOS) |
| wolfSSL | 15:117db924cf7c | 1912 | /* Maximum depth to search for WolfSSL root */ |
| wolfSSL | 15:117db924cf7c | 1913 | #define MAX_WOLF_ROOT_DEPTH 5 |
| wolfSSL | 15:117db924cf7c | 1914 | |
| wolfSSL | 15:117db924cf7c | 1915 | static WC_INLINE int ChangeToWolfRoot(void) |
| wolfSSL | 15:117db924cf7c | 1916 | { |
| wolfSSL | 15:117db924cf7c | 1917 | #if !defined(NO_FILESYSTEM) || defined(FORCE_BUFFER_TEST) |
| wolfSSL | 15:117db924cf7c | 1918 | int depth, res; |
| wolfSSL | 16:8e0d178b1d1e | 1919 | XFILE file; |
| wolfSSL | 15:117db924cf7c | 1920 | for(depth = 0; depth <= MAX_WOLF_ROOT_DEPTH; depth++) { |
| wolfSSL | 16:8e0d178b1d1e | 1921 | file = XFOPEN(ntruKeyFile, "rb"); |
| wolfSSL | 15:117db924cf7c | 1922 | if (file != NULL) { |
| wolfSSL | 15:117db924cf7c | 1923 | fclose(file); |
| wolfSSL | 15:117db924cf7c | 1924 | return depth; |
| wolfSSL | 15:117db924cf7c | 1925 | } |
| wolfSSL | 15:117db924cf7c | 1926 | #ifdef USE_WINDOWS_API |
| wolfSSL | 15:117db924cf7c | 1927 | res = SetCurrentDirectoryA("..\\"); |
| wolfSSL | 15:117db924cf7c | 1928 | #else |
| wolfSSL | 15:117db924cf7c | 1929 | res = chdir("../"); |
| wolfSSL | 15:117db924cf7c | 1930 | #endif |
| wolfSSL | 15:117db924cf7c | 1931 | if (res < 0) { |
| wolfSSL | 15:117db924cf7c | 1932 | printf("chdir to ../ failed!\n"); |
| wolfSSL | 15:117db924cf7c | 1933 | break; |
| wolfSSL | 15:117db924cf7c | 1934 | } |
| wolfSSL | 15:117db924cf7c | 1935 | } |
| wolfSSL | 15:117db924cf7c | 1936 | |
| wolfSSL | 15:117db924cf7c | 1937 | err_sys("wolf root not found"); |
| wolfSSL | 15:117db924cf7c | 1938 | return -1; |
| wolfSSL | 15:117db924cf7c | 1939 | #else |
| wolfSSL | 15:117db924cf7c | 1940 | return 0; |
| wolfSSL | 15:117db924cf7c | 1941 | #endif |
| wolfSSL | 15:117db924cf7c | 1942 | } |
| wolfSSL | 15:117db924cf7c | 1943 | #endif /* !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOLFSSL_TIRTOS) */ |
| wolfSSL | 15:117db924cf7c | 1944 | |
| wolfSSL | 15:117db924cf7c | 1945 | #ifdef HAVE_STACK_SIZE |
| wolfSSL | 15:117db924cf7c | 1946 | |
| wolfSSL | 15:117db924cf7c | 1947 | typedef THREAD_RETURN WOLFSSL_THREAD (*thread_func)(void* args); |
| wolfSSL | 15:117db924cf7c | 1948 | #define STACK_CHECK_VAL 0x01 |
| wolfSSL | 15:117db924cf7c | 1949 | |
| wolfSSL | 15:117db924cf7c | 1950 | static WC_INLINE int StackSizeCheck(func_args* args, thread_func tf) |
| wolfSSL | 15:117db924cf7c | 1951 | { |
| wolfSSL | 15:117db924cf7c | 1952 | int ret, i, used; |
| wolfSSL | 15:117db924cf7c | 1953 | void* status; |
| wolfSSL | 15:117db924cf7c | 1954 | unsigned char* myStack = NULL; |
| wolfSSL | 16:8e0d178b1d1e | 1955 | int stackSize = 1024*152; |
| wolfSSL | 15:117db924cf7c | 1956 | pthread_attr_t myAttr; |
| wolfSSL | 15:117db924cf7c | 1957 | pthread_t threadId; |
| wolfSSL | 15:117db924cf7c | 1958 | |
| wolfSSL | 15:117db924cf7c | 1959 | #ifdef PTHREAD_STACK_MIN |
| wolfSSL | 15:117db924cf7c | 1960 | if (stackSize < PTHREAD_STACK_MIN) |
| wolfSSL | 15:117db924cf7c | 1961 | stackSize = PTHREAD_STACK_MIN; |
| wolfSSL | 15:117db924cf7c | 1962 | #endif |
| wolfSSL | 15:117db924cf7c | 1963 | |
| wolfSSL | 15:117db924cf7c | 1964 | ret = posix_memalign((void**)&myStack, sysconf(_SC_PAGESIZE), stackSize); |
| wolfSSL | 15:117db924cf7c | 1965 | if (ret != 0 || myStack == NULL) |
| wolfSSL | 15:117db924cf7c | 1966 | err_sys("posix_memalign failed\n"); |
| wolfSSL | 15:117db924cf7c | 1967 | |
| wolfSSL | 15:117db924cf7c | 1968 | XMEMSET(myStack, STACK_CHECK_VAL, stackSize); |
| wolfSSL | 15:117db924cf7c | 1969 | |
| wolfSSL | 15:117db924cf7c | 1970 | ret = pthread_attr_init(&myAttr); |
| wolfSSL | 15:117db924cf7c | 1971 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 1972 | err_sys("attr_init failed"); |
| wolfSSL | 15:117db924cf7c | 1973 | |
| wolfSSL | 15:117db924cf7c | 1974 | ret = pthread_attr_setstack(&myAttr, myStack, stackSize); |
| wolfSSL | 15:117db924cf7c | 1975 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 1976 | err_sys("attr_setstackaddr failed"); |
| wolfSSL | 15:117db924cf7c | 1977 | |
| wolfSSL | 15:117db924cf7c | 1978 | ret = pthread_create(&threadId, &myAttr, tf, args); |
| wolfSSL | 15:117db924cf7c | 1979 | if (ret != 0) { |
| wolfSSL | 15:117db924cf7c | 1980 | perror("pthread_create failed"); |
| wolfSSL | 15:117db924cf7c | 1981 | exit(EXIT_FAILURE); |
| wolfSSL | 15:117db924cf7c | 1982 | } |
| wolfSSL | 15:117db924cf7c | 1983 | |
| wolfSSL | 15:117db924cf7c | 1984 | ret = pthread_join(threadId, &status); |
| wolfSSL | 15:117db924cf7c | 1985 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 1986 | err_sys("pthread_join failed"); |
| wolfSSL | 15:117db924cf7c | 1987 | |
| wolfSSL | 15:117db924cf7c | 1988 | for (i = 0; i < stackSize; i++) { |
| wolfSSL | 15:117db924cf7c | 1989 | if (myStack[i] != STACK_CHECK_VAL) { |
| wolfSSL | 15:117db924cf7c | 1990 | break; |
| wolfSSL | 15:117db924cf7c | 1991 | } |
| wolfSSL | 15:117db924cf7c | 1992 | } |
| wolfSSL | 15:117db924cf7c | 1993 | |
| wolfSSL | 15:117db924cf7c | 1994 | free(myStack); |
| wolfSSL | 15:117db924cf7c | 1995 | |
| wolfSSL | 15:117db924cf7c | 1996 | used = stackSize - i; |
| wolfSSL | 15:117db924cf7c | 1997 | printf("stack used = %d\n", used); |
| wolfSSL | 15:117db924cf7c | 1998 | |
| wolfSSL | 15:117db924cf7c | 1999 | return (int)((size_t)status); |
| wolfSSL | 15:117db924cf7c | 2000 | } |
| wolfSSL | 15:117db924cf7c | 2001 | |
| wolfSSL | 15:117db924cf7c | 2002 | |
| wolfSSL | 15:117db924cf7c | 2003 | #endif /* HAVE_STACK_SIZE */ |
| wolfSSL | 15:117db924cf7c | 2004 | |
| wolfSSL | 15:117db924cf7c | 2005 | |
| wolfSSL | 15:117db924cf7c | 2006 | #ifdef STACK_TRAP |
| wolfSSL | 15:117db924cf7c | 2007 | |
| wolfSSL | 15:117db924cf7c | 2008 | /* good settings |
| wolfSSL | 15:117db924cf7c | 2009 | --enable-debug --disable-shared C_EXTRA_FLAGS="-DUSER_TIME -DTFM_TIMING_RESISTANT -DPOSITIVE_EXP_ONLY -DSTACK_TRAP" |
| wolfSSL | 15:117db924cf7c | 2010 | |
| wolfSSL | 15:117db924cf7c | 2011 | */ |
| wolfSSL | 15:117db924cf7c | 2012 | |
| wolfSSL | 15:117db924cf7c | 2013 | #ifdef HAVE_STACK_SIZE |
| wolfSSL | 15:117db924cf7c | 2014 | /* client only for now, setrlimit will fail if pthread_create() called */ |
| wolfSSL | 15:117db924cf7c | 2015 | /* STACK_SIZE does pthread_create() on client */ |
| wolfSSL | 15:117db924cf7c | 2016 | #error "can't use STACK_TRAP with STACK_SIZE, setrlimit will fail" |
| wolfSSL | 15:117db924cf7c | 2017 | #endif /* HAVE_STACK_SIZE */ |
| wolfSSL | 15:117db924cf7c | 2018 | |
| wolfSSL | 15:117db924cf7c | 2019 | static WC_INLINE void StackTrap(void) |
| wolfSSL | 15:117db924cf7c | 2020 | { |
| wolfSSL | 15:117db924cf7c | 2021 | struct rlimit rl; |
| wolfSSL | 15:117db924cf7c | 2022 | if (getrlimit(RLIMIT_STACK, &rl) != 0) |
| wolfSSL | 15:117db924cf7c | 2023 | err_sys("getrlimit failed"); |
| wolfSSL | 15:117db924cf7c | 2024 | printf("rlim_cur = %llu\n", rl.rlim_cur); |
| wolfSSL | 15:117db924cf7c | 2025 | rl.rlim_cur = 1024*21; /* adjust trap size here */ |
| wolfSSL | 15:117db924cf7c | 2026 | if (setrlimit(RLIMIT_STACK, &rl) != 0) { |
| wolfSSL | 15:117db924cf7c | 2027 | perror("setrlimit"); |
| wolfSSL | 15:117db924cf7c | 2028 | err_sys("setrlimit failed"); |
| wolfSSL | 15:117db924cf7c | 2029 | } |
| wolfSSL | 15:117db924cf7c | 2030 | } |
| wolfSSL | 15:117db924cf7c | 2031 | |
| wolfSSL | 15:117db924cf7c | 2032 | #else /* STACK_TRAP */ |
| wolfSSL | 15:117db924cf7c | 2033 | |
| wolfSSL | 15:117db924cf7c | 2034 | static WC_INLINE void StackTrap(void) |
| wolfSSL | 15:117db924cf7c | 2035 | { |
| wolfSSL | 15:117db924cf7c | 2036 | } |
| wolfSSL | 15:117db924cf7c | 2037 | |
| wolfSSL | 15:117db924cf7c | 2038 | #endif /* STACK_TRAP */ |
| wolfSSL | 15:117db924cf7c | 2039 | |
| wolfSSL | 15:117db924cf7c | 2040 | |
| wolfSSL | 16:8e0d178b1d1e | 2041 | #if defined(ATOMIC_USER) && !defined(WOLFSSL_AEAD_ONLY) |
| wolfSSL | 15:117db924cf7c | 2042 | |
| wolfSSL | 15:117db924cf7c | 2043 | /* Atomic Encrypt Context example */ |
| wolfSSL | 15:117db924cf7c | 2044 | typedef struct AtomicEncCtx { |
| wolfSSL | 15:117db924cf7c | 2045 | int keySetup; /* have we done key setup yet */ |
| wolfSSL | 15:117db924cf7c | 2046 | Aes aes; /* for aes example */ |
| wolfSSL | 15:117db924cf7c | 2047 | } AtomicEncCtx; |
| wolfSSL | 15:117db924cf7c | 2048 | |
| wolfSSL | 15:117db924cf7c | 2049 | |
| wolfSSL | 15:117db924cf7c | 2050 | /* Atomic Decrypt Context example */ |
| wolfSSL | 15:117db924cf7c | 2051 | typedef struct AtomicDecCtx { |
| wolfSSL | 15:117db924cf7c | 2052 | int keySetup; /* have we done key setup yet */ |
| wolfSSL | 15:117db924cf7c | 2053 | Aes aes; /* for aes example */ |
| wolfSSL | 15:117db924cf7c | 2054 | } AtomicDecCtx; |
| wolfSSL | 15:117db924cf7c | 2055 | |
| wolfSSL | 15:117db924cf7c | 2056 | |
| wolfSSL | 15:117db924cf7c | 2057 | static WC_INLINE int myMacEncryptCb(WOLFSSL* ssl, unsigned char* macOut, |
| wolfSSL | 15:117db924cf7c | 2058 | const unsigned char* macIn, unsigned int macInSz, int macContent, |
| wolfSSL | 15:117db924cf7c | 2059 | int macVerify, unsigned char* encOut, const unsigned char* encIn, |
| wolfSSL | 15:117db924cf7c | 2060 | unsigned int encSz, void* ctx) |
| wolfSSL | 15:117db924cf7c | 2061 | { |
| wolfSSL | 15:117db924cf7c | 2062 | int ret; |
| wolfSSL | 15:117db924cf7c | 2063 | Hmac hmac; |
| wolfSSL | 15:117db924cf7c | 2064 | byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ]; |
| wolfSSL | 15:117db924cf7c | 2065 | AtomicEncCtx* encCtx = (AtomicEncCtx*)ctx; |
| wolfSSL | 15:117db924cf7c | 2066 | const char* tlsStr = "TLS"; |
| wolfSSL | 15:117db924cf7c | 2067 | |
| wolfSSL | 15:117db924cf7c | 2068 | /* example supports (d)tls aes */ |
| wolfSSL | 15:117db924cf7c | 2069 | if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) { |
| wolfSSL | 15:117db924cf7c | 2070 | printf("myMacEncryptCb not using AES\n"); |
| wolfSSL | 15:117db924cf7c | 2071 | return -1; |
| wolfSSL | 15:117db924cf7c | 2072 | } |
| wolfSSL | 15:117db924cf7c | 2073 | |
| wolfSSL | 15:117db924cf7c | 2074 | if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) { |
| wolfSSL | 15:117db924cf7c | 2075 | printf("myMacEncryptCb not using (D)TLS\n"); |
| wolfSSL | 15:117db924cf7c | 2076 | return -1; |
| wolfSSL | 15:117db924cf7c | 2077 | } |
| wolfSSL | 15:117db924cf7c | 2078 | |
| wolfSSL | 15:117db924cf7c | 2079 | /* hmac, not needed if aead mode */ |
| wolfSSL | 15:117db924cf7c | 2080 | wolfSSL_SetTlsHmacInner(ssl, myInner, macInSz, macContent, macVerify); |
| wolfSSL | 15:117db924cf7c | 2081 | |
| wolfSSL | 16:8e0d178b1d1e | 2082 | ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID); |
| wolfSSL | 16:8e0d178b1d1e | 2083 | if (ret != 0) |
| wolfSSL | 16:8e0d178b1d1e | 2084 | return ret; |
| wolfSSL | 15:117db924cf7c | 2085 | ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl), |
| wolfSSL | 15:117db924cf7c | 2086 | wolfSSL_GetMacSecret(ssl, macVerify), wolfSSL_GetHmacSize(ssl)); |
| wolfSSL | 15:117db924cf7c | 2087 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 2088 | return ret; |
| wolfSSL | 15:117db924cf7c | 2089 | ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner)); |
| wolfSSL | 15:117db924cf7c | 2090 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 2091 | return ret; |
| wolfSSL | 15:117db924cf7c | 2092 | ret = wc_HmacUpdate(&hmac, macIn, macInSz); |
| wolfSSL | 15:117db924cf7c | 2093 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 2094 | return ret; |
| wolfSSL | 15:117db924cf7c | 2095 | ret = wc_HmacFinal(&hmac, macOut); |
| wolfSSL | 15:117db924cf7c | 2096 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 2097 | return ret; |
| wolfSSL | 15:117db924cf7c | 2098 | |
| wolfSSL | 15:117db924cf7c | 2099 | |
| wolfSSL | 15:117db924cf7c | 2100 | /* encrypt setup on first time */ |
| wolfSSL | 15:117db924cf7c | 2101 | if (encCtx->keySetup == 0) { |
| wolfSSL | 15:117db924cf7c | 2102 | int keyLen = wolfSSL_GetKeySize(ssl); |
| wolfSSL | 15:117db924cf7c | 2103 | const byte* key; |
| wolfSSL | 15:117db924cf7c | 2104 | const byte* iv; |
| wolfSSL | 15:117db924cf7c | 2105 | |
| wolfSSL | 15:117db924cf7c | 2106 | if (wolfSSL_GetSide(ssl) == WOLFSSL_CLIENT_END) { |
| wolfSSL | 15:117db924cf7c | 2107 | key = wolfSSL_GetClientWriteKey(ssl); |
| wolfSSL | 15:117db924cf7c | 2108 | iv = wolfSSL_GetClientWriteIV(ssl); |
| wolfSSL | 15:117db924cf7c | 2109 | } |
| wolfSSL | 15:117db924cf7c | 2110 | else { |
| wolfSSL | 15:117db924cf7c | 2111 | key = wolfSSL_GetServerWriteKey(ssl); |
| wolfSSL | 15:117db924cf7c | 2112 | iv = wolfSSL_GetServerWriteIV(ssl); |
| wolfSSL | 15:117db924cf7c | 2113 | } |
| wolfSSL | 15:117db924cf7c | 2114 | |
| wolfSSL | 15:117db924cf7c | 2115 | ret = wc_AesSetKey(&encCtx->aes, key, keyLen, iv, AES_ENCRYPTION); |
| wolfSSL | 15:117db924cf7c | 2116 | if (ret != 0) { |
| wolfSSL | 15:117db924cf7c | 2117 | printf("AesSetKey failed in myMacEncryptCb\n"); |
| wolfSSL | 15:117db924cf7c | 2118 | return ret; |
| wolfSSL | 15:117db924cf7c | 2119 | } |
| wolfSSL | 15:117db924cf7c | 2120 | encCtx->keySetup = 1; |
| wolfSSL | 15:117db924cf7c | 2121 | } |
| wolfSSL | 15:117db924cf7c | 2122 | |
| wolfSSL | 15:117db924cf7c | 2123 | /* encrypt */ |
| wolfSSL | 15:117db924cf7c | 2124 | return wc_AesCbcEncrypt(&encCtx->aes, encOut, encIn, encSz); |
| wolfSSL | 15:117db924cf7c | 2125 | } |
| wolfSSL | 15:117db924cf7c | 2126 | |
| wolfSSL | 15:117db924cf7c | 2127 | |
| wolfSSL | 15:117db924cf7c | 2128 | static WC_INLINE int myDecryptVerifyCb(WOLFSSL* ssl, |
| wolfSSL | 15:117db924cf7c | 2129 | unsigned char* decOut, const unsigned char* decIn, |
| wolfSSL | 15:117db924cf7c | 2130 | unsigned int decSz, int macContent, int macVerify, |
| wolfSSL | 15:117db924cf7c | 2131 | unsigned int* padSz, void* ctx) |
| wolfSSL | 15:117db924cf7c | 2132 | { |
| wolfSSL | 15:117db924cf7c | 2133 | AtomicDecCtx* decCtx = (AtomicDecCtx*)ctx; |
| wolfSSL | 15:117db924cf7c | 2134 | int ret = 0; |
| wolfSSL | 15:117db924cf7c | 2135 | int macInSz = 0; |
| wolfSSL | 15:117db924cf7c | 2136 | int ivExtra = 0; |
| wolfSSL | 15:117db924cf7c | 2137 | int digestSz = wolfSSL_GetHmacSize(ssl); |
| wolfSSL | 15:117db924cf7c | 2138 | unsigned int pad = 0; |
| wolfSSL | 15:117db924cf7c | 2139 | unsigned int padByte = 0; |
| wolfSSL | 15:117db924cf7c | 2140 | Hmac hmac; |
| wolfSSL | 15:117db924cf7c | 2141 | byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ]; |
| wolfSSL | 15:117db924cf7c | 2142 | byte verify[WC_MAX_DIGEST_SIZE]; |
| wolfSSL | 15:117db924cf7c | 2143 | const char* tlsStr = "TLS"; |
| wolfSSL | 15:117db924cf7c | 2144 | |
| wolfSSL | 15:117db924cf7c | 2145 | /* example supports (d)tls aes */ |
| wolfSSL | 15:117db924cf7c | 2146 | if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) { |
| wolfSSL | 15:117db924cf7c | 2147 | printf("myMacEncryptCb not using AES\n"); |
| wolfSSL | 15:117db924cf7c | 2148 | return -1; |
| wolfSSL | 15:117db924cf7c | 2149 | } |
| wolfSSL | 15:117db924cf7c | 2150 | |
| wolfSSL | 15:117db924cf7c | 2151 | if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) { |
| wolfSSL | 15:117db924cf7c | 2152 | printf("myMacEncryptCb not using (D)TLS\n"); |
| wolfSSL | 15:117db924cf7c | 2153 | return -1; |
| wolfSSL | 15:117db924cf7c | 2154 | } |
| wolfSSL | 15:117db924cf7c | 2155 | |
| wolfSSL | 15:117db924cf7c | 2156 | /*decrypt */ |
| wolfSSL | 15:117db924cf7c | 2157 | if (decCtx->keySetup == 0) { |
| wolfSSL | 15:117db924cf7c | 2158 | int keyLen = wolfSSL_GetKeySize(ssl); |
| wolfSSL | 15:117db924cf7c | 2159 | const byte* key; |
| wolfSSL | 15:117db924cf7c | 2160 | const byte* iv; |
| wolfSSL | 15:117db924cf7c | 2161 | |
| wolfSSL | 15:117db924cf7c | 2162 | /* decrypt is from other side (peer) */ |
| wolfSSL | 15:117db924cf7c | 2163 | if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) { |
| wolfSSL | 15:117db924cf7c | 2164 | key = wolfSSL_GetClientWriteKey(ssl); |
| wolfSSL | 15:117db924cf7c | 2165 | iv = wolfSSL_GetClientWriteIV(ssl); |
| wolfSSL | 15:117db924cf7c | 2166 | } |
| wolfSSL | 15:117db924cf7c | 2167 | else { |
| wolfSSL | 15:117db924cf7c | 2168 | key = wolfSSL_GetServerWriteKey(ssl); |
| wolfSSL | 15:117db924cf7c | 2169 | iv = wolfSSL_GetServerWriteIV(ssl); |
| wolfSSL | 15:117db924cf7c | 2170 | } |
| wolfSSL | 15:117db924cf7c | 2171 | |
| wolfSSL | 15:117db924cf7c | 2172 | ret = wc_AesSetKey(&decCtx->aes, key, keyLen, iv, AES_DECRYPTION); |
| wolfSSL | 15:117db924cf7c | 2173 | if (ret != 0) { |
| wolfSSL | 15:117db924cf7c | 2174 | printf("AesSetKey failed in myDecryptVerifyCb\n"); |
| wolfSSL | 15:117db924cf7c | 2175 | return ret; |
| wolfSSL | 15:117db924cf7c | 2176 | } |
| wolfSSL | 15:117db924cf7c | 2177 | decCtx->keySetup = 1; |
| wolfSSL | 15:117db924cf7c | 2178 | } |
| wolfSSL | 15:117db924cf7c | 2179 | |
| wolfSSL | 15:117db924cf7c | 2180 | /* decrypt */ |
| wolfSSL | 15:117db924cf7c | 2181 | ret = wc_AesCbcDecrypt(&decCtx->aes, decOut, decIn, decSz); |
| wolfSSL | 15:117db924cf7c | 2182 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 2183 | return ret; |
| wolfSSL | 15:117db924cf7c | 2184 | |
| wolfSSL | 15:117db924cf7c | 2185 | if (wolfSSL_GetCipherType(ssl) == WOLFSSL_AEAD_TYPE) { |
| wolfSSL | 15:117db924cf7c | 2186 | *padSz = wolfSSL_GetAeadMacSize(ssl); |
| wolfSSL | 15:117db924cf7c | 2187 | return 0; /* hmac, not needed if aead mode */ |
| wolfSSL | 15:117db924cf7c | 2188 | } |
| wolfSSL | 15:117db924cf7c | 2189 | |
| wolfSSL | 15:117db924cf7c | 2190 | if (wolfSSL_GetCipherType(ssl) == WOLFSSL_BLOCK_TYPE) { |
| wolfSSL | 15:117db924cf7c | 2191 | pad = *(decOut + decSz - 1); |
| wolfSSL | 15:117db924cf7c | 2192 | padByte = 1; |
| wolfSSL | 15:117db924cf7c | 2193 | if (wolfSSL_IsTLSv1_1(ssl)) |
| wolfSSL | 15:117db924cf7c | 2194 | ivExtra = wolfSSL_GetCipherBlockSize(ssl); |
| wolfSSL | 15:117db924cf7c | 2195 | } |
| wolfSSL | 15:117db924cf7c | 2196 | |
| wolfSSL | 15:117db924cf7c | 2197 | *padSz = wolfSSL_GetHmacSize(ssl) + pad + padByte; |
| wolfSSL | 15:117db924cf7c | 2198 | macInSz = decSz - ivExtra - digestSz - pad - padByte; |
| wolfSSL | 15:117db924cf7c | 2199 | |
| wolfSSL | 15:117db924cf7c | 2200 | wolfSSL_SetTlsHmacInner(ssl, myInner, macInSz, macContent, macVerify); |
| wolfSSL | 15:117db924cf7c | 2201 | |
| wolfSSL | 16:8e0d178b1d1e | 2202 | ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID); |
| wolfSSL | 16:8e0d178b1d1e | 2203 | if (ret != 0) |
| wolfSSL | 16:8e0d178b1d1e | 2204 | return ret; |
| wolfSSL | 15:117db924cf7c | 2205 | ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl), |
| wolfSSL | 15:117db924cf7c | 2206 | wolfSSL_GetMacSecret(ssl, macVerify), digestSz); |
| wolfSSL | 15:117db924cf7c | 2207 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 2208 | return ret; |
| wolfSSL | 15:117db924cf7c | 2209 | ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner)); |
| wolfSSL | 15:117db924cf7c | 2210 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 2211 | return ret; |
| wolfSSL | 15:117db924cf7c | 2212 | ret = wc_HmacUpdate(&hmac, decOut + ivExtra, macInSz); |
| wolfSSL | 15:117db924cf7c | 2213 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 2214 | return ret; |
| wolfSSL | 15:117db924cf7c | 2215 | ret = wc_HmacFinal(&hmac, verify); |
| wolfSSL | 15:117db924cf7c | 2216 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 2217 | return ret; |
| wolfSSL | 15:117db924cf7c | 2218 | |
| wolfSSL | 15:117db924cf7c | 2219 | if (XMEMCMP(verify, decOut + decSz - digestSz - pad - padByte, |
| wolfSSL | 15:117db924cf7c | 2220 | digestSz) != 0) { |
| wolfSSL | 15:117db924cf7c | 2221 | printf("myDecryptVerify verify failed\n"); |
| wolfSSL | 15:117db924cf7c | 2222 | return -1; |
| wolfSSL | 15:117db924cf7c | 2223 | } |
| wolfSSL | 15:117db924cf7c | 2224 | |
| wolfSSL | 15:117db924cf7c | 2225 | return ret; |
| wolfSSL | 15:117db924cf7c | 2226 | } |
| wolfSSL | 15:117db924cf7c | 2227 | |
| wolfSSL | 16:8e0d178b1d1e | 2228 | #if defined(HAVE_ENCRYPT_THEN_MAC) |
| wolfSSL | 16:8e0d178b1d1e | 2229 | |
| wolfSSL | 16:8e0d178b1d1e | 2230 | static WC_INLINE int myEncryptMacCb(WOLFSSL* ssl, unsigned char* macOut, |
| wolfSSL | 16:8e0d178b1d1e | 2231 | int content, int macVerify, unsigned char* encOut, |
| wolfSSL | 16:8e0d178b1d1e | 2232 | const unsigned char* encIn, unsigned int encSz, void* ctx) |
| wolfSSL | 16:8e0d178b1d1e | 2233 | { |
| wolfSSL | 16:8e0d178b1d1e | 2234 | int ret; |
| wolfSSL | 16:8e0d178b1d1e | 2235 | Hmac hmac; |
| wolfSSL | 16:8e0d178b1d1e | 2236 | AtomicEncCtx* encCtx = (AtomicEncCtx*)ctx; |
| wolfSSL | 16:8e0d178b1d1e | 2237 | byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ]; |
| wolfSSL | 16:8e0d178b1d1e | 2238 | const char* tlsStr = "TLS"; |
| wolfSSL | 16:8e0d178b1d1e | 2239 | |
| wolfSSL | 16:8e0d178b1d1e | 2240 | /* example supports (d)tls aes */ |
| wolfSSL | 16:8e0d178b1d1e | 2241 | if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) { |
| wolfSSL | 16:8e0d178b1d1e | 2242 | printf("myMacEncryptCb not using AES\n"); |
| wolfSSL | 16:8e0d178b1d1e | 2243 | return -1; |
| wolfSSL | 16:8e0d178b1d1e | 2244 | } |
| wolfSSL | 16:8e0d178b1d1e | 2245 | |
| wolfSSL | 16:8e0d178b1d1e | 2246 | if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) { |
| wolfSSL | 16:8e0d178b1d1e | 2247 | printf("myMacEncryptCb not using (D)TLS\n"); |
| wolfSSL | 16:8e0d178b1d1e | 2248 | return -1; |
| wolfSSL | 16:8e0d178b1d1e | 2249 | } |
| wolfSSL | 16:8e0d178b1d1e | 2250 | |
| wolfSSL | 16:8e0d178b1d1e | 2251 | /* encrypt setup on first time */ |
| wolfSSL | 16:8e0d178b1d1e | 2252 | if (encCtx->keySetup == 0) { |
| wolfSSL | 16:8e0d178b1d1e | 2253 | int keyLen = wolfSSL_GetKeySize(ssl); |
| wolfSSL | 16:8e0d178b1d1e | 2254 | const byte* key; |
| wolfSSL | 16:8e0d178b1d1e | 2255 | const byte* iv; |
| wolfSSL | 16:8e0d178b1d1e | 2256 | |
| wolfSSL | 16:8e0d178b1d1e | 2257 | if (wolfSSL_GetSide(ssl) == WOLFSSL_CLIENT_END) { |
| wolfSSL | 16:8e0d178b1d1e | 2258 | key = wolfSSL_GetClientWriteKey(ssl); |
| wolfSSL | 16:8e0d178b1d1e | 2259 | iv = wolfSSL_GetClientWriteIV(ssl); |
| wolfSSL | 16:8e0d178b1d1e | 2260 | } |
| wolfSSL | 16:8e0d178b1d1e | 2261 | else { |
| wolfSSL | 16:8e0d178b1d1e | 2262 | key = wolfSSL_GetServerWriteKey(ssl); |
| wolfSSL | 16:8e0d178b1d1e | 2263 | iv = wolfSSL_GetServerWriteIV(ssl); |
| wolfSSL | 16:8e0d178b1d1e | 2264 | } |
| wolfSSL | 16:8e0d178b1d1e | 2265 | |
| wolfSSL | 16:8e0d178b1d1e | 2266 | ret = wc_AesSetKey(&encCtx->aes, key, keyLen, iv, AES_ENCRYPTION); |
| wolfSSL | 16:8e0d178b1d1e | 2267 | if (ret != 0) { |
| wolfSSL | 16:8e0d178b1d1e | 2268 | printf("AesSetKey failed in myMacEncryptCb\n"); |
| wolfSSL | 16:8e0d178b1d1e | 2269 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2270 | } |
| wolfSSL | 16:8e0d178b1d1e | 2271 | encCtx->keySetup = 1; |
| wolfSSL | 16:8e0d178b1d1e | 2272 | } |
| wolfSSL | 16:8e0d178b1d1e | 2273 | |
| wolfSSL | 16:8e0d178b1d1e | 2274 | /* encrypt */ |
| wolfSSL | 16:8e0d178b1d1e | 2275 | ret = wc_AesCbcEncrypt(&encCtx->aes, encOut, encIn, encSz); |
| wolfSSL | 16:8e0d178b1d1e | 2276 | if (ret != 0) |
| wolfSSL | 16:8e0d178b1d1e | 2277 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2278 | |
| wolfSSL | 16:8e0d178b1d1e | 2279 | /* Reconstruct record header. */ |
| wolfSSL | 16:8e0d178b1d1e | 2280 | wolfSSL_SetTlsHmacInner(ssl, myInner, encSz, content, macVerify); |
| wolfSSL | 16:8e0d178b1d1e | 2281 | |
| wolfSSL | 16:8e0d178b1d1e | 2282 | ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID); |
| wolfSSL | 16:8e0d178b1d1e | 2283 | if (ret != 0) |
| wolfSSL | 16:8e0d178b1d1e | 2284 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2285 | ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl), |
| wolfSSL | 16:8e0d178b1d1e | 2286 | wolfSSL_GetMacSecret(ssl, macVerify), wolfSSL_GetHmacSize(ssl)); |
| wolfSSL | 16:8e0d178b1d1e | 2287 | if (ret != 0) |
| wolfSSL | 16:8e0d178b1d1e | 2288 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2289 | ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner)); |
| wolfSSL | 16:8e0d178b1d1e | 2290 | if (ret != 0) |
| wolfSSL | 16:8e0d178b1d1e | 2291 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2292 | ret = wc_HmacUpdate(&hmac, encOut, encSz); |
| wolfSSL | 16:8e0d178b1d1e | 2293 | if (ret != 0) |
| wolfSSL | 16:8e0d178b1d1e | 2294 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2295 | return wc_HmacFinal(&hmac, macOut); |
| wolfSSL | 16:8e0d178b1d1e | 2296 | } |
| wolfSSL | 16:8e0d178b1d1e | 2297 | |
| wolfSSL | 16:8e0d178b1d1e | 2298 | |
| wolfSSL | 16:8e0d178b1d1e | 2299 | static WC_INLINE int myVerifyDecryptCb(WOLFSSL* ssl, |
| wolfSSL | 16:8e0d178b1d1e | 2300 | unsigned char* decOut, const unsigned char* decIn, |
| wolfSSL | 16:8e0d178b1d1e | 2301 | unsigned int decSz, int content, int macVerify, |
| wolfSSL | 16:8e0d178b1d1e | 2302 | unsigned int* padSz, void* ctx) |
| wolfSSL | 16:8e0d178b1d1e | 2303 | { |
| wolfSSL | 16:8e0d178b1d1e | 2304 | AtomicDecCtx* decCtx = (AtomicDecCtx*)ctx; |
| wolfSSL | 16:8e0d178b1d1e | 2305 | int ret = 0; |
| wolfSSL | 16:8e0d178b1d1e | 2306 | int digestSz = wolfSSL_GetHmacSize(ssl); |
| wolfSSL | 16:8e0d178b1d1e | 2307 | Hmac hmac; |
| wolfSSL | 16:8e0d178b1d1e | 2308 | byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ]; |
| wolfSSL | 16:8e0d178b1d1e | 2309 | byte verify[WC_MAX_DIGEST_SIZE]; |
| wolfSSL | 16:8e0d178b1d1e | 2310 | const char* tlsStr = "TLS"; |
| wolfSSL | 16:8e0d178b1d1e | 2311 | |
| wolfSSL | 16:8e0d178b1d1e | 2312 | /* example supports (d)tls aes */ |
| wolfSSL | 16:8e0d178b1d1e | 2313 | if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) { |
| wolfSSL | 16:8e0d178b1d1e | 2314 | printf("myMacEncryptCb not using AES\n"); |
| wolfSSL | 16:8e0d178b1d1e | 2315 | return -1; |
| wolfSSL | 16:8e0d178b1d1e | 2316 | } |
| wolfSSL | 16:8e0d178b1d1e | 2317 | |
| wolfSSL | 16:8e0d178b1d1e | 2318 | if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) { |
| wolfSSL | 16:8e0d178b1d1e | 2319 | printf("myMacEncryptCb not using (D)TLS\n"); |
| wolfSSL | 16:8e0d178b1d1e | 2320 | return -1; |
| wolfSSL | 16:8e0d178b1d1e | 2321 | } |
| wolfSSL | 16:8e0d178b1d1e | 2322 | |
| wolfSSL | 16:8e0d178b1d1e | 2323 | /* Reconstruct record header. */ |
| wolfSSL | 16:8e0d178b1d1e | 2324 | wolfSSL_SetTlsHmacInner(ssl, myInner, decSz, content, macVerify); |
| wolfSSL | 16:8e0d178b1d1e | 2325 | |
| wolfSSL | 16:8e0d178b1d1e | 2326 | ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID); |
| wolfSSL | 16:8e0d178b1d1e | 2327 | if (ret != 0) |
| wolfSSL | 16:8e0d178b1d1e | 2328 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2329 | ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl), |
| wolfSSL | 16:8e0d178b1d1e | 2330 | wolfSSL_GetMacSecret(ssl, macVerify), digestSz); |
| wolfSSL | 16:8e0d178b1d1e | 2331 | if (ret != 0) |
| wolfSSL | 16:8e0d178b1d1e | 2332 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2333 | ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner)); |
| wolfSSL | 16:8e0d178b1d1e | 2334 | if (ret != 0) |
| wolfSSL | 16:8e0d178b1d1e | 2335 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2336 | ret = wc_HmacUpdate(&hmac, decIn, decSz); |
| wolfSSL | 16:8e0d178b1d1e | 2337 | if (ret != 0) |
| wolfSSL | 16:8e0d178b1d1e | 2338 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2339 | ret = wc_HmacFinal(&hmac, verify); |
| wolfSSL | 16:8e0d178b1d1e | 2340 | if (ret != 0) |
| wolfSSL | 16:8e0d178b1d1e | 2341 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2342 | |
| wolfSSL | 16:8e0d178b1d1e | 2343 | if (XMEMCMP(verify, decOut + decSz, digestSz) != 0) { |
| wolfSSL | 16:8e0d178b1d1e | 2344 | printf("myDecryptVerify verify failed\n"); |
| wolfSSL | 16:8e0d178b1d1e | 2345 | return -1; |
| wolfSSL | 16:8e0d178b1d1e | 2346 | } |
| wolfSSL | 16:8e0d178b1d1e | 2347 | |
| wolfSSL | 16:8e0d178b1d1e | 2348 | /* decrypt */ |
| wolfSSL | 16:8e0d178b1d1e | 2349 | if (decCtx->keySetup == 0) { |
| wolfSSL | 16:8e0d178b1d1e | 2350 | int keyLen = wolfSSL_GetKeySize(ssl); |
| wolfSSL | 16:8e0d178b1d1e | 2351 | const byte* key; |
| wolfSSL | 16:8e0d178b1d1e | 2352 | const byte* iv; |
| wolfSSL | 16:8e0d178b1d1e | 2353 | |
| wolfSSL | 16:8e0d178b1d1e | 2354 | /* decrypt is from other side (peer) */ |
| wolfSSL | 16:8e0d178b1d1e | 2355 | if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) { |
| wolfSSL | 16:8e0d178b1d1e | 2356 | key = wolfSSL_GetClientWriteKey(ssl); |
| wolfSSL | 16:8e0d178b1d1e | 2357 | iv = wolfSSL_GetClientWriteIV(ssl); |
| wolfSSL | 16:8e0d178b1d1e | 2358 | } |
| wolfSSL | 16:8e0d178b1d1e | 2359 | else { |
| wolfSSL | 16:8e0d178b1d1e | 2360 | key = wolfSSL_GetServerWriteKey(ssl); |
| wolfSSL | 16:8e0d178b1d1e | 2361 | iv = wolfSSL_GetServerWriteIV(ssl); |
| wolfSSL | 16:8e0d178b1d1e | 2362 | } |
| wolfSSL | 16:8e0d178b1d1e | 2363 | |
| wolfSSL | 16:8e0d178b1d1e | 2364 | ret = wc_AesSetKey(&decCtx->aes, key, keyLen, iv, AES_DECRYPTION); |
| wolfSSL | 16:8e0d178b1d1e | 2365 | if (ret != 0) { |
| wolfSSL | 16:8e0d178b1d1e | 2366 | printf("AesSetKey failed in myDecryptVerifyCb\n"); |
| wolfSSL | 16:8e0d178b1d1e | 2367 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2368 | } |
| wolfSSL | 16:8e0d178b1d1e | 2369 | decCtx->keySetup = 1; |
| wolfSSL | 16:8e0d178b1d1e | 2370 | } |
| wolfSSL | 16:8e0d178b1d1e | 2371 | |
| wolfSSL | 16:8e0d178b1d1e | 2372 | /* decrypt */ |
| wolfSSL | 16:8e0d178b1d1e | 2373 | ret = wc_AesCbcDecrypt(&decCtx->aes, decOut, decIn, decSz); |
| wolfSSL | 16:8e0d178b1d1e | 2374 | if (ret != 0) |
| wolfSSL | 16:8e0d178b1d1e | 2375 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2376 | |
| wolfSSL | 16:8e0d178b1d1e | 2377 | *padSz = *(decOut + decSz - 1) + 1; |
| wolfSSL | 16:8e0d178b1d1e | 2378 | |
| wolfSSL | 16:8e0d178b1d1e | 2379 | return 0; |
| wolfSSL | 16:8e0d178b1d1e | 2380 | } |
| wolfSSL | 16:8e0d178b1d1e | 2381 | |
| wolfSSL | 16:8e0d178b1d1e | 2382 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 2383 | |
| wolfSSL | 15:117db924cf7c | 2384 | |
| wolfSSL | 15:117db924cf7c | 2385 | static WC_INLINE void SetupAtomicUser(WOLFSSL_CTX* ctx, WOLFSSL* ssl) |
| wolfSSL | 15:117db924cf7c | 2386 | { |
| wolfSSL | 15:117db924cf7c | 2387 | AtomicEncCtx* encCtx; |
| wolfSSL | 15:117db924cf7c | 2388 | AtomicDecCtx* decCtx; |
| wolfSSL | 15:117db924cf7c | 2389 | |
| wolfSSL | 15:117db924cf7c | 2390 | encCtx = (AtomicEncCtx*)malloc(sizeof(AtomicEncCtx)); |
| wolfSSL | 15:117db924cf7c | 2391 | if (encCtx == NULL) |
| wolfSSL | 15:117db924cf7c | 2392 | err_sys("AtomicEncCtx malloc failed"); |
| wolfSSL | 15:117db924cf7c | 2393 | XMEMSET(encCtx, 0, sizeof(AtomicEncCtx)); |
| wolfSSL | 15:117db924cf7c | 2394 | |
| wolfSSL | 15:117db924cf7c | 2395 | decCtx = (AtomicDecCtx*)malloc(sizeof(AtomicDecCtx)); |
| wolfSSL | 15:117db924cf7c | 2396 | if (decCtx == NULL) { |
| wolfSSL | 15:117db924cf7c | 2397 | free(encCtx); |
| wolfSSL | 15:117db924cf7c | 2398 | err_sys("AtomicDecCtx malloc failed"); |
| wolfSSL | 15:117db924cf7c | 2399 | } |
| wolfSSL | 15:117db924cf7c | 2400 | XMEMSET(decCtx, 0, sizeof(AtomicDecCtx)); |
| wolfSSL | 15:117db924cf7c | 2401 | |
| wolfSSL | 15:117db924cf7c | 2402 | wolfSSL_CTX_SetMacEncryptCb(ctx, myMacEncryptCb); |
| wolfSSL | 15:117db924cf7c | 2403 | wolfSSL_SetMacEncryptCtx(ssl, encCtx); |
| wolfSSL | 15:117db924cf7c | 2404 | |
| wolfSSL | 15:117db924cf7c | 2405 | wolfSSL_CTX_SetDecryptVerifyCb(ctx, myDecryptVerifyCb); |
| wolfSSL | 15:117db924cf7c | 2406 | wolfSSL_SetDecryptVerifyCtx(ssl, decCtx); |
| wolfSSL | 16:8e0d178b1d1e | 2407 | |
| wolfSSL | 16:8e0d178b1d1e | 2408 | #if defined(HAVE_ENCRYPT_THEN_MAC) |
| wolfSSL | 16:8e0d178b1d1e | 2409 | wolfSSL_CTX_SetEncryptMacCb(ctx, myEncryptMacCb); |
| wolfSSL | 16:8e0d178b1d1e | 2410 | wolfSSL_SetEncryptMacCtx(ssl, encCtx); |
| wolfSSL | 16:8e0d178b1d1e | 2411 | |
| wolfSSL | 16:8e0d178b1d1e | 2412 | wolfSSL_CTX_SetVerifyDecryptCb(ctx, myVerifyDecryptCb); |
| wolfSSL | 16:8e0d178b1d1e | 2413 | wolfSSL_SetVerifyDecryptCtx(ssl, decCtx); |
| wolfSSL | 16:8e0d178b1d1e | 2414 | #endif |
| wolfSSL | 15:117db924cf7c | 2415 | } |
| wolfSSL | 15:117db924cf7c | 2416 | |
| wolfSSL | 15:117db924cf7c | 2417 | |
| wolfSSL | 15:117db924cf7c | 2418 | static WC_INLINE void FreeAtomicUser(WOLFSSL* ssl) |
| wolfSSL | 15:117db924cf7c | 2419 | { |
| wolfSSL | 15:117db924cf7c | 2420 | AtomicEncCtx* encCtx = (AtomicEncCtx*)wolfSSL_GetMacEncryptCtx(ssl); |
| wolfSSL | 15:117db924cf7c | 2421 | AtomicDecCtx* decCtx = (AtomicDecCtx*)wolfSSL_GetDecryptVerifyCtx(ssl); |
| wolfSSL | 15:117db924cf7c | 2422 | |
| wolfSSL | 16:8e0d178b1d1e | 2423 | /* Encrypt-Then-MAC callbacks use same contexts. */ |
| wolfSSL | 16:8e0d178b1d1e | 2424 | |
| wolfSSL | 15:117db924cf7c | 2425 | free(decCtx); |
| wolfSSL | 15:117db924cf7c | 2426 | free(encCtx); |
| wolfSSL | 15:117db924cf7c | 2427 | } |
| wolfSSL | 15:117db924cf7c | 2428 | |
| wolfSSL | 15:117db924cf7c | 2429 | #endif /* ATOMIC_USER */ |
| wolfSSL | 15:117db924cf7c | 2430 | |
| wolfSSL | 15:117db924cf7c | 2431 | #ifdef WOLFSSL_STATIC_MEMORY |
| wolfSSL | 15:117db924cf7c | 2432 | static WC_INLINE int wolfSSL_PrintStats(WOLFSSL_MEM_STATS* stats) |
| wolfSSL | 15:117db924cf7c | 2433 | { |
| wolfSSL | 15:117db924cf7c | 2434 | word16 i; |
| wolfSSL | 15:117db924cf7c | 2435 | |
| wolfSSL | 15:117db924cf7c | 2436 | if (stats == NULL) { |
| wolfSSL | 15:117db924cf7c | 2437 | return 0; |
| wolfSSL | 15:117db924cf7c | 2438 | } |
| wolfSSL | 15:117db924cf7c | 2439 | |
| wolfSSL | 15:117db924cf7c | 2440 | /* print to stderr so is on the same pipe as WOLFSSL_DEBUG */ |
| wolfSSL | 15:117db924cf7c | 2441 | fprintf(stderr, "Total mallocs = %d\n", stats->totalAlloc); |
| wolfSSL | 15:117db924cf7c | 2442 | fprintf(stderr, "Total frees = %d\n", stats->totalFr); |
| wolfSSL | 15:117db924cf7c | 2443 | fprintf(stderr, "Current mallocs = %d\n", stats->curAlloc); |
| wolfSSL | 15:117db924cf7c | 2444 | fprintf(stderr, "Available IO = %d\n", stats->avaIO); |
| wolfSSL | 15:117db924cf7c | 2445 | fprintf(stderr, "Max con. handshakes = %d\n", stats->maxHa); |
| wolfSSL | 15:117db924cf7c | 2446 | fprintf(stderr, "Max con. IO = %d\n", stats->maxIO); |
| wolfSSL | 15:117db924cf7c | 2447 | fprintf(stderr, "State of memory blocks: size : available \n"); |
| wolfSSL | 15:117db924cf7c | 2448 | for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) { |
| wolfSSL | 15:117db924cf7c | 2449 | fprintf(stderr, " : %d\t : %d\n", stats->blockSz[i], |
| wolfSSL | 15:117db924cf7c | 2450 | stats->avaBlock[i]); |
| wolfSSL | 15:117db924cf7c | 2451 | } |
| wolfSSL | 15:117db924cf7c | 2452 | |
| wolfSSL | 15:117db924cf7c | 2453 | return 1; |
| wolfSSL | 15:117db924cf7c | 2454 | } |
| wolfSSL | 15:117db924cf7c | 2455 | #endif /* WOLFSSL_STATIC_MEMORY */ |
| wolfSSL | 15:117db924cf7c | 2456 | |
| wolfSSL | 15:117db924cf7c | 2457 | #ifdef HAVE_PK_CALLBACKS |
| wolfSSL | 15:117db924cf7c | 2458 | |
| wolfSSL | 15:117db924cf7c | 2459 | typedef struct PkCbInfo { |
| wolfSSL | 15:117db924cf7c | 2460 | const char* ourKey; |
| wolfSSL | 15:117db924cf7c | 2461 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 2462 | union { |
| wolfSSL | 15:117db924cf7c | 2463 | #ifdef HAVE_ECC |
| wolfSSL | 15:117db924cf7c | 2464 | ecc_key ecc; |
| wolfSSL | 15:117db924cf7c | 2465 | #endif |
| wolfSSL | 15:117db924cf7c | 2466 | #ifdef HAVE_CURVE25519 |
| wolfSSL | 15:117db924cf7c | 2467 | curve25519_key curve; |
| wolfSSL | 15:117db924cf7c | 2468 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 2469 | #ifdef HAVE_CURVE448 |
| wolfSSL | 16:8e0d178b1d1e | 2470 | curve448_key curve; |
| wolfSSL | 16:8e0d178b1d1e | 2471 | #endif |
| wolfSSL | 15:117db924cf7c | 2472 | } keyGen; |
| wolfSSL | 15:117db924cf7c | 2473 | #endif |
| wolfSSL | 15:117db924cf7c | 2474 | } PkCbInfo; |
| wolfSSL | 15:117db924cf7c | 2475 | |
| wolfSSL | 16:8e0d178b1d1e | 2476 | #if defined(DEBUG_PK_CB) || defined(TEST_PK_PRIVKEY) |
| wolfSSL | 16:8e0d178b1d1e | 2477 | #define WOLFSSL_PKMSG(_f_, ...) printf(_f_, ##__VA_ARGS__) |
| wolfSSL | 16:8e0d178b1d1e | 2478 | #else |
| wolfSSL | 16:8e0d178b1d1e | 2479 | #define WOLFSSL_PKMSG(_f_, ...) |
| wolfSSL | 16:8e0d178b1d1e | 2480 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 2481 | |
| wolfSSL | 15:117db924cf7c | 2482 | #ifdef HAVE_ECC |
| wolfSSL | 15:117db924cf7c | 2483 | |
| wolfSSL | 16:8e0d178b1d1e | 2484 | static WC_INLINE int myEccKeyGen(WOLFSSL* ssl, ecc_key* key, word32 keySz, |
| wolfSSL | 15:117db924cf7c | 2485 | int ecc_curve, void* ctx) |
| wolfSSL | 15:117db924cf7c | 2486 | { |
| wolfSSL | 15:117db924cf7c | 2487 | int ret; |
| wolfSSL | 15:117db924cf7c | 2488 | WC_RNG rng; |
| wolfSSL | 15:117db924cf7c | 2489 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 16:8e0d178b1d1e | 2490 | ecc_key* new_key; |
| wolfSSL | 15:117db924cf7c | 2491 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 2492 | byte qx[MAX_ECC_BYTES], qy[MAX_ECC_BYTES]; |
| wolfSSL | 15:117db924cf7c | 2493 | word32 qxLen = sizeof(qx), qyLen = sizeof(qy); |
| wolfSSL | 16:8e0d178b1d1e | 2494 | |
| wolfSSL | 15:117db924cf7c | 2495 | new_key = &cbInfo->keyGen.ecc; |
| wolfSSL | 16:8e0d178b1d1e | 2496 | #else |
| wolfSSL | 16:8e0d178b1d1e | 2497 | new_key = key; |
| wolfSSL | 15:117db924cf7c | 2498 | #endif |
| wolfSSL | 15:117db924cf7c | 2499 | |
| wolfSSL | 15:117db924cf7c | 2500 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 2501 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 2502 | |
| wolfSSL | 16:8e0d178b1d1e | 2503 | WOLFSSL_PKMSG("PK ECC KeyGen: keySz %d, Curve ID %d\n", keySz, ecc_curve); |
| wolfSSL | 16:8e0d178b1d1e | 2504 | |
| wolfSSL | 15:117db924cf7c | 2505 | ret = wc_InitRng(&rng); |
| wolfSSL | 15:117db924cf7c | 2506 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 2507 | return ret; |
| wolfSSL | 15:117db924cf7c | 2508 | |
| wolfSSL | 15:117db924cf7c | 2509 | ret = wc_ecc_init(new_key); |
| wolfSSL | 15:117db924cf7c | 2510 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 2511 | /* create new key */ |
| wolfSSL | 15:117db924cf7c | 2512 | ret = wc_ecc_make_key_ex(&rng, keySz, new_key, ecc_curve); |
| wolfSSL | 15:117db924cf7c | 2513 | |
| wolfSSL | 15:117db924cf7c | 2514 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 2515 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 2516 | /* extract public portion from new key into `key` arg */ |
| wolfSSL | 15:117db924cf7c | 2517 | ret = wc_ecc_export_public_raw(new_key, qx, &qxLen, qy, &qyLen); |
| wolfSSL | 15:117db924cf7c | 2518 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 2519 | /* load public portion only into key */ |
| wolfSSL | 15:117db924cf7c | 2520 | ret = wc_ecc_import_unsigned(key, qx, qy, NULL, ecc_curve); |
| wolfSSL | 15:117db924cf7c | 2521 | } |
| wolfSSL | 15:117db924cf7c | 2522 | (void)qxLen; |
| wolfSSL | 15:117db924cf7c | 2523 | (void)qyLen; |
| wolfSSL | 15:117db924cf7c | 2524 | } |
| wolfSSL | 15:117db924cf7c | 2525 | #endif |
| wolfSSL | 15:117db924cf7c | 2526 | } |
| wolfSSL | 15:117db924cf7c | 2527 | |
| wolfSSL | 16:8e0d178b1d1e | 2528 | WOLFSSL_PKMSG("PK ECC KeyGen: ret %d\n", ret); |
| wolfSSL | 16:8e0d178b1d1e | 2529 | |
| wolfSSL | 15:117db924cf7c | 2530 | wc_FreeRng(&rng); |
| wolfSSL | 15:117db924cf7c | 2531 | |
| wolfSSL | 15:117db924cf7c | 2532 | return ret; |
| wolfSSL | 15:117db924cf7c | 2533 | } |
| wolfSSL | 15:117db924cf7c | 2534 | |
| wolfSSL | 15:117db924cf7c | 2535 | static WC_INLINE int myEccSign(WOLFSSL* ssl, const byte* in, word32 inSz, |
| wolfSSL | 15:117db924cf7c | 2536 | byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx) |
| wolfSSL | 15:117db924cf7c | 2537 | { |
| wolfSSL | 15:117db924cf7c | 2538 | int ret; |
| wolfSSL | 15:117db924cf7c | 2539 | WC_RNG rng; |
| wolfSSL | 15:117db924cf7c | 2540 | word32 idx = 0; |
| wolfSSL | 15:117db924cf7c | 2541 | ecc_key myKey; |
| wolfSSL | 15:117db924cf7c | 2542 | byte* keyBuf = (byte*)key; |
| wolfSSL | 15:117db924cf7c | 2543 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 2544 | |
| wolfSSL | 15:117db924cf7c | 2545 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 2546 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 2547 | |
| wolfSSL | 16:8e0d178b1d1e | 2548 | WOLFSSL_PKMSG("PK ECC Sign: inSz %d, keySz %d\n", inSz, keySz); |
| wolfSSL | 16:8e0d178b1d1e | 2549 | |
| wolfSSL | 15:117db924cf7c | 2550 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 2551 | ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz); |
| wolfSSL | 15:117db924cf7c | 2552 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 2553 | return ret; |
| wolfSSL | 15:117db924cf7c | 2554 | #endif |
| wolfSSL | 15:117db924cf7c | 2555 | |
| wolfSSL | 15:117db924cf7c | 2556 | ret = wc_InitRng(&rng); |
| wolfSSL | 15:117db924cf7c | 2557 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 2558 | return ret; |
| wolfSSL | 15:117db924cf7c | 2559 | |
| wolfSSL | 15:117db924cf7c | 2560 | ret = wc_ecc_init(&myKey); |
| wolfSSL | 15:117db924cf7c | 2561 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 2562 | ret = wc_EccPrivateKeyDecode(keyBuf, &idx, &myKey, keySz); |
| wolfSSL | 16:8e0d178b1d1e | 2563 | if (ret == 0) { |
| wolfSSL | 16:8e0d178b1d1e | 2564 | WOLFSSL_PKMSG("PK ECC Sign: Curve ID %d\n", myKey.dp->id); |
| wolfSSL | 15:117db924cf7c | 2565 | ret = wc_ecc_sign_hash(in, inSz, out, outSz, &rng, &myKey); |
| wolfSSL | 16:8e0d178b1d1e | 2566 | } |
| wolfSSL | 15:117db924cf7c | 2567 | wc_ecc_free(&myKey); |
| wolfSSL | 15:117db924cf7c | 2568 | } |
| wolfSSL | 15:117db924cf7c | 2569 | wc_FreeRng(&rng); |
| wolfSSL | 15:117db924cf7c | 2570 | |
| wolfSSL | 15:117db924cf7c | 2571 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 2572 | free(keyBuf); |
| wolfSSL | 15:117db924cf7c | 2573 | #endif |
| wolfSSL | 15:117db924cf7c | 2574 | |
| wolfSSL | 16:8e0d178b1d1e | 2575 | WOLFSSL_PKMSG("PK ECC Sign: ret %d outSz %d\n", ret, *outSz); |
| wolfSSL | 16:8e0d178b1d1e | 2576 | |
| wolfSSL | 15:117db924cf7c | 2577 | return ret; |
| wolfSSL | 15:117db924cf7c | 2578 | } |
| wolfSSL | 15:117db924cf7c | 2579 | |
| wolfSSL | 15:117db924cf7c | 2580 | |
| wolfSSL | 15:117db924cf7c | 2581 | static WC_INLINE int myEccVerify(WOLFSSL* ssl, const byte* sig, word32 sigSz, |
| wolfSSL | 15:117db924cf7c | 2582 | const byte* hash, word32 hashSz, const byte* key, word32 keySz, |
| wolfSSL | 15:117db924cf7c | 2583 | int* result, void* ctx) |
| wolfSSL | 15:117db924cf7c | 2584 | { |
| wolfSSL | 15:117db924cf7c | 2585 | int ret; |
| wolfSSL | 15:117db924cf7c | 2586 | word32 idx = 0; |
| wolfSSL | 15:117db924cf7c | 2587 | ecc_key myKey; |
| wolfSSL | 15:117db924cf7c | 2588 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 2589 | |
| wolfSSL | 15:117db924cf7c | 2590 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 2591 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 2592 | |
| wolfSSL | 16:8e0d178b1d1e | 2593 | WOLFSSL_PKMSG("PK ECC Verify: sigSz %d, hashSz %d, keySz %d\n", sigSz, hashSz, keySz); |
| wolfSSL | 16:8e0d178b1d1e | 2594 | |
| wolfSSL | 15:117db924cf7c | 2595 | ret = wc_ecc_init(&myKey); |
| wolfSSL | 15:117db924cf7c | 2596 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 2597 | ret = wc_EccPublicKeyDecode(key, &idx, &myKey, keySz); |
| wolfSSL | 15:117db924cf7c | 2598 | if (ret == 0) |
| wolfSSL | 15:117db924cf7c | 2599 | ret = wc_ecc_verify_hash(sig, sigSz, hash, hashSz, result, &myKey); |
| wolfSSL | 15:117db924cf7c | 2600 | wc_ecc_free(&myKey); |
| wolfSSL | 15:117db924cf7c | 2601 | } |
| wolfSSL | 15:117db924cf7c | 2602 | |
| wolfSSL | 16:8e0d178b1d1e | 2603 | WOLFSSL_PKMSG("PK ECC Verify: ret %d, result %d\n", ret, *result); |
| wolfSSL | 16:8e0d178b1d1e | 2604 | |
| wolfSSL | 15:117db924cf7c | 2605 | return ret; |
| wolfSSL | 15:117db924cf7c | 2606 | } |
| wolfSSL | 15:117db924cf7c | 2607 | |
| wolfSSL | 15:117db924cf7c | 2608 | static WC_INLINE int myEccSharedSecret(WOLFSSL* ssl, ecc_key* otherKey, |
| wolfSSL | 15:117db924cf7c | 2609 | unsigned char* pubKeyDer, unsigned int* pubKeySz, |
| wolfSSL | 15:117db924cf7c | 2610 | unsigned char* out, unsigned int* outlen, |
| wolfSSL | 15:117db924cf7c | 2611 | int side, void* ctx) |
| wolfSSL | 15:117db924cf7c | 2612 | { |
| wolfSSL | 15:117db924cf7c | 2613 | int ret; |
| wolfSSL | 15:117db924cf7c | 2614 | ecc_key* privKey = NULL; |
| wolfSSL | 15:117db924cf7c | 2615 | ecc_key* pubKey = NULL; |
| wolfSSL | 15:117db924cf7c | 2616 | ecc_key tmpKey; |
| wolfSSL | 15:117db924cf7c | 2617 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 2618 | |
| wolfSSL | 15:117db924cf7c | 2619 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 2620 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 2621 | |
| wolfSSL | 16:8e0d178b1d1e | 2622 | WOLFSSL_PKMSG("PK ECC PMS: Side %s, Peer Curve %d\n", |
| wolfSSL | 16:8e0d178b1d1e | 2623 | side == WOLFSSL_CLIENT_END ? "client" : "server", otherKey->dp->id); |
| wolfSSL | 16:8e0d178b1d1e | 2624 | |
| wolfSSL | 15:117db924cf7c | 2625 | ret = wc_ecc_init(&tmpKey); |
| wolfSSL | 15:117db924cf7c | 2626 | if (ret != 0) { |
| wolfSSL | 15:117db924cf7c | 2627 | return ret; |
| wolfSSL | 15:117db924cf7c | 2628 | } |
| wolfSSL | 15:117db924cf7c | 2629 | |
| wolfSSL | 15:117db924cf7c | 2630 | /* for client: create and export public key */ |
| wolfSSL | 15:117db924cf7c | 2631 | if (side == WOLFSSL_CLIENT_END) { |
| wolfSSL | 15:117db924cf7c | 2632 | WC_RNG rng; |
| wolfSSL | 15:117db924cf7c | 2633 | |
| wolfSSL | 15:117db924cf7c | 2634 | privKey = &tmpKey; |
| wolfSSL | 15:117db924cf7c | 2635 | pubKey = otherKey; |
| wolfSSL | 15:117db924cf7c | 2636 | |
| wolfSSL | 15:117db924cf7c | 2637 | ret = wc_InitRng(&rng); |
| wolfSSL | 15:117db924cf7c | 2638 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 2639 | ret = wc_ecc_make_key_ex(&rng, 0, privKey, otherKey->dp->id); |
| wolfSSL | 15:117db924cf7c | 2640 | #ifdef WOLFSSL_ASYNC_CRYPT |
| wolfSSL | 15:117db924cf7c | 2641 | if (ret == WC_PENDING_E) { |
| wolfSSL | 15:117db924cf7c | 2642 | ret = wc_AsyncWait(ret, &privKey->asyncDev, WC_ASYNC_FLAG_NONE); |
| wolfSSL | 15:117db924cf7c | 2643 | } |
| wolfSSL | 15:117db924cf7c | 2644 | #endif |
| wolfSSL | 15:117db924cf7c | 2645 | if (ret == 0) |
| wolfSSL | 15:117db924cf7c | 2646 | ret = wc_ecc_export_x963(privKey, pubKeyDer, pubKeySz); |
| wolfSSL | 15:117db924cf7c | 2647 | wc_FreeRng(&rng); |
| wolfSSL | 15:117db924cf7c | 2648 | } |
| wolfSSL | 15:117db924cf7c | 2649 | } |
| wolfSSL | 15:117db924cf7c | 2650 | |
| wolfSSL | 15:117db924cf7c | 2651 | /* for server: import public key */ |
| wolfSSL | 15:117db924cf7c | 2652 | else if (side == WOLFSSL_SERVER_END) { |
| wolfSSL | 15:117db924cf7c | 2653 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 2654 | privKey = &cbInfo->keyGen.ecc; |
| wolfSSL | 15:117db924cf7c | 2655 | #else |
| wolfSSL | 15:117db924cf7c | 2656 | privKey = otherKey; |
| wolfSSL | 15:117db924cf7c | 2657 | #endif |
| wolfSSL | 15:117db924cf7c | 2658 | pubKey = &tmpKey; |
| wolfSSL | 15:117db924cf7c | 2659 | |
| wolfSSL | 15:117db924cf7c | 2660 | ret = wc_ecc_import_x963_ex(pubKeyDer, *pubKeySz, pubKey, |
| wolfSSL | 15:117db924cf7c | 2661 | otherKey->dp->id); |
| wolfSSL | 15:117db924cf7c | 2662 | } |
| wolfSSL | 15:117db924cf7c | 2663 | else { |
| wolfSSL | 15:117db924cf7c | 2664 | ret = BAD_FUNC_ARG; |
| wolfSSL | 15:117db924cf7c | 2665 | } |
| wolfSSL | 15:117db924cf7c | 2666 | |
| wolfSSL | 15:117db924cf7c | 2667 | /* generate shared secret and return it */ |
| wolfSSL | 15:117db924cf7c | 2668 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 2669 | ret = wc_ecc_shared_secret(privKey, pubKey, out, outlen); |
| wolfSSL | 15:117db924cf7c | 2670 | |
| wolfSSL | 15:117db924cf7c | 2671 | #ifdef WOLFSSL_ASYNC_CRYPT |
| wolfSSL | 15:117db924cf7c | 2672 | if (ret == WC_PENDING_E) { |
| wolfSSL | 15:117db924cf7c | 2673 | ret = wc_AsyncWait(ret, &privKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); |
| wolfSSL | 15:117db924cf7c | 2674 | } |
| wolfSSL | 15:117db924cf7c | 2675 | #endif |
| wolfSSL | 15:117db924cf7c | 2676 | } |
| wolfSSL | 15:117db924cf7c | 2677 | |
| wolfSSL | 15:117db924cf7c | 2678 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 2679 | if (side == WOLFSSL_SERVER_END) { |
| wolfSSL | 15:117db924cf7c | 2680 | wc_ecc_free(&cbInfo->keyGen.ecc); |
| wolfSSL | 15:117db924cf7c | 2681 | } |
| wolfSSL | 15:117db924cf7c | 2682 | #endif |
| wolfSSL | 15:117db924cf7c | 2683 | |
| wolfSSL | 15:117db924cf7c | 2684 | wc_ecc_free(&tmpKey); |
| wolfSSL | 15:117db924cf7c | 2685 | |
| wolfSSL | 16:8e0d178b1d1e | 2686 | WOLFSSL_PKMSG("PK ECC PMS: ret %d, PubKeySz %d, OutLen %d\n", ret, *pubKeySz, *outlen); |
| wolfSSL | 16:8e0d178b1d1e | 2687 | |
| wolfSSL | 15:117db924cf7c | 2688 | return ret; |
| wolfSSL | 15:117db924cf7c | 2689 | } |
| wolfSSL | 15:117db924cf7c | 2690 | |
| wolfSSL | 15:117db924cf7c | 2691 | #ifdef HAVE_ED25519 |
| wolfSSL | 15:117db924cf7c | 2692 | static WC_INLINE int myEd25519Sign(WOLFSSL* ssl, const byte* in, word32 inSz, |
| wolfSSL | 15:117db924cf7c | 2693 | byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx) |
| wolfSSL | 15:117db924cf7c | 2694 | { |
| wolfSSL | 15:117db924cf7c | 2695 | int ret; |
| wolfSSL | 15:117db924cf7c | 2696 | word32 idx = 0; |
| wolfSSL | 15:117db924cf7c | 2697 | ed25519_key myKey; |
| wolfSSL | 15:117db924cf7c | 2698 | byte* keyBuf = (byte*)key; |
| wolfSSL | 15:117db924cf7c | 2699 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 2700 | |
| wolfSSL | 15:117db924cf7c | 2701 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 2702 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 2703 | |
| wolfSSL | 16:8e0d178b1d1e | 2704 | WOLFSSL_PKMSG("PK 25519 Sign: inSz %d, keySz %d\n", inSz, keySz); |
| wolfSSL | 16:8e0d178b1d1e | 2705 | |
| wolfSSL | 15:117db924cf7c | 2706 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 2707 | ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz); |
| wolfSSL | 15:117db924cf7c | 2708 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 2709 | return ret; |
| wolfSSL | 15:117db924cf7c | 2710 | #endif |
| wolfSSL | 15:117db924cf7c | 2711 | |
| wolfSSL | 15:117db924cf7c | 2712 | ret = wc_ed25519_init(&myKey); |
| wolfSSL | 15:117db924cf7c | 2713 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 2714 | ret = wc_Ed25519PrivateKeyDecode(keyBuf, &idx, &myKey, keySz); |
| wolfSSL | 15:117db924cf7c | 2715 | if (ret == 0) |
| wolfSSL | 15:117db924cf7c | 2716 | ret = wc_ed25519_sign_msg(in, inSz, out, outSz, &myKey); |
| wolfSSL | 15:117db924cf7c | 2717 | wc_ed25519_free(&myKey); |
| wolfSSL | 15:117db924cf7c | 2718 | } |
| wolfSSL | 15:117db924cf7c | 2719 | |
| wolfSSL | 15:117db924cf7c | 2720 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 2721 | free(keyBuf); |
| wolfSSL | 15:117db924cf7c | 2722 | #endif |
| wolfSSL | 15:117db924cf7c | 2723 | |
| wolfSSL | 16:8e0d178b1d1e | 2724 | WOLFSSL_PKMSG("PK 25519 Sign: ret %d, outSz %d\n", ret, *outSz); |
| wolfSSL | 16:8e0d178b1d1e | 2725 | |
| wolfSSL | 15:117db924cf7c | 2726 | return ret; |
| wolfSSL | 15:117db924cf7c | 2727 | } |
| wolfSSL | 15:117db924cf7c | 2728 | |
| wolfSSL | 15:117db924cf7c | 2729 | |
| wolfSSL | 15:117db924cf7c | 2730 | static WC_INLINE int myEd25519Verify(WOLFSSL* ssl, const byte* sig, word32 sigSz, |
| wolfSSL | 15:117db924cf7c | 2731 | const byte* msg, word32 msgSz, const byte* key, word32 keySz, |
| wolfSSL | 15:117db924cf7c | 2732 | int* result, void* ctx) |
| wolfSSL | 15:117db924cf7c | 2733 | { |
| wolfSSL | 15:117db924cf7c | 2734 | int ret; |
| wolfSSL | 15:117db924cf7c | 2735 | ed25519_key myKey; |
| wolfSSL | 15:117db924cf7c | 2736 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 2737 | |
| wolfSSL | 15:117db924cf7c | 2738 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 2739 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 2740 | |
| wolfSSL | 16:8e0d178b1d1e | 2741 | WOLFSSL_PKMSG("PK 25519 Verify: sigSz %d, msgSz %d, keySz %d\n", sigSz, msgSz, keySz); |
| wolfSSL | 16:8e0d178b1d1e | 2742 | |
| wolfSSL | 15:117db924cf7c | 2743 | ret = wc_ed25519_init(&myKey); |
| wolfSSL | 15:117db924cf7c | 2744 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 2745 | ret = wc_ed25519_import_public(key, keySz, &myKey); |
| wolfSSL | 15:117db924cf7c | 2746 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 2747 | ret = wc_ed25519_verify_msg(sig, sigSz, msg, msgSz, result, &myKey); |
| wolfSSL | 15:117db924cf7c | 2748 | } |
| wolfSSL | 15:117db924cf7c | 2749 | wc_ed25519_free(&myKey); |
| wolfSSL | 15:117db924cf7c | 2750 | } |
| wolfSSL | 15:117db924cf7c | 2751 | |
| wolfSSL | 16:8e0d178b1d1e | 2752 | WOLFSSL_PKMSG("PK 25519 Verify: ret %d, result %d\n", ret, *result); |
| wolfSSL | 16:8e0d178b1d1e | 2753 | |
| wolfSSL | 15:117db924cf7c | 2754 | return ret; |
| wolfSSL | 15:117db924cf7c | 2755 | } |
| wolfSSL | 15:117db924cf7c | 2756 | #endif /* HAVE_ED25519 */ |
| wolfSSL | 15:117db924cf7c | 2757 | |
| wolfSSL | 15:117db924cf7c | 2758 | #ifdef HAVE_CURVE25519 |
| wolfSSL | 16:8e0d178b1d1e | 2759 | static WC_INLINE int myX25519KeyGen(WOLFSSL* ssl, curve25519_key* key, |
| wolfSSL | 15:117db924cf7c | 2760 | unsigned int keySz, void* ctx) |
| wolfSSL | 15:117db924cf7c | 2761 | { |
| wolfSSL | 15:117db924cf7c | 2762 | int ret; |
| wolfSSL | 15:117db924cf7c | 2763 | WC_RNG rng; |
| wolfSSL | 15:117db924cf7c | 2764 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 2765 | |
| wolfSSL | 15:117db924cf7c | 2766 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 2767 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 2768 | |
| wolfSSL | 16:8e0d178b1d1e | 2769 | WOLFSSL_PKMSG("PK 25519 KeyGen: keySz %d\n", keySz); |
| wolfSSL | 16:8e0d178b1d1e | 2770 | |
| wolfSSL | 15:117db924cf7c | 2771 | ret = wc_InitRng(&rng); |
| wolfSSL | 15:117db924cf7c | 2772 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 2773 | return ret; |
| wolfSSL | 15:117db924cf7c | 2774 | |
| wolfSSL | 15:117db924cf7c | 2775 | ret = wc_curve25519_make_key(&rng, keySz, key); |
| wolfSSL | 15:117db924cf7c | 2776 | |
| wolfSSL | 15:117db924cf7c | 2777 | wc_FreeRng(&rng); |
| wolfSSL | 15:117db924cf7c | 2778 | |
| wolfSSL | 16:8e0d178b1d1e | 2779 | WOLFSSL_PKMSG("PK 25519 KeyGen: ret %d\n", ret); |
| wolfSSL | 16:8e0d178b1d1e | 2780 | |
| wolfSSL | 15:117db924cf7c | 2781 | return ret; |
| wolfSSL | 15:117db924cf7c | 2782 | } |
| wolfSSL | 15:117db924cf7c | 2783 | |
| wolfSSL | 15:117db924cf7c | 2784 | static WC_INLINE int myX25519SharedSecret(WOLFSSL* ssl, curve25519_key* otherKey, |
| wolfSSL | 15:117db924cf7c | 2785 | unsigned char* pubKeyDer, unsigned int* pubKeySz, |
| wolfSSL | 15:117db924cf7c | 2786 | unsigned char* out, unsigned int* outlen, |
| wolfSSL | 15:117db924cf7c | 2787 | int side, void* ctx) |
| wolfSSL | 15:117db924cf7c | 2788 | { |
| wolfSSL | 15:117db924cf7c | 2789 | int ret; |
| wolfSSL | 15:117db924cf7c | 2790 | curve25519_key* privKey = NULL; |
| wolfSSL | 15:117db924cf7c | 2791 | curve25519_key* pubKey = NULL; |
| wolfSSL | 15:117db924cf7c | 2792 | curve25519_key tmpKey; |
| wolfSSL | 15:117db924cf7c | 2793 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 2794 | |
| wolfSSL | 15:117db924cf7c | 2795 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 2796 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 2797 | |
| wolfSSL | 16:8e0d178b1d1e | 2798 | WOLFSSL_PKMSG("PK 25519 PMS: side %s\n", |
| wolfSSL | 16:8e0d178b1d1e | 2799 | side == WOLFSSL_CLIENT_END ? "client" : "server"); |
| wolfSSL | 16:8e0d178b1d1e | 2800 | |
| wolfSSL | 15:117db924cf7c | 2801 | ret = wc_curve25519_init(&tmpKey); |
| wolfSSL | 15:117db924cf7c | 2802 | if (ret != 0) { |
| wolfSSL | 15:117db924cf7c | 2803 | return ret; |
| wolfSSL | 15:117db924cf7c | 2804 | } |
| wolfSSL | 15:117db924cf7c | 2805 | |
| wolfSSL | 15:117db924cf7c | 2806 | /* for client: create and export public key */ |
| wolfSSL | 15:117db924cf7c | 2807 | if (side == WOLFSSL_CLIENT_END) { |
| wolfSSL | 15:117db924cf7c | 2808 | WC_RNG rng; |
| wolfSSL | 15:117db924cf7c | 2809 | |
| wolfSSL | 15:117db924cf7c | 2810 | privKey = &tmpKey; |
| wolfSSL | 15:117db924cf7c | 2811 | pubKey = otherKey; |
| wolfSSL | 15:117db924cf7c | 2812 | |
| wolfSSL | 15:117db924cf7c | 2813 | ret = wc_InitRng(&rng); |
| wolfSSL | 15:117db924cf7c | 2814 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 2815 | ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, privKey); |
| wolfSSL | 15:117db924cf7c | 2816 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 2817 | ret = wc_curve25519_export_public_ex(privKey, pubKeyDer, |
| wolfSSL | 15:117db924cf7c | 2818 | pubKeySz, EC25519_LITTLE_ENDIAN); |
| wolfSSL | 15:117db924cf7c | 2819 | } |
| wolfSSL | 15:117db924cf7c | 2820 | wc_FreeRng(&rng); |
| wolfSSL | 15:117db924cf7c | 2821 | } |
| wolfSSL | 15:117db924cf7c | 2822 | } |
| wolfSSL | 15:117db924cf7c | 2823 | |
| wolfSSL | 15:117db924cf7c | 2824 | /* for server: import public key */ |
| wolfSSL | 15:117db924cf7c | 2825 | else if (side == WOLFSSL_SERVER_END) { |
| wolfSSL | 15:117db924cf7c | 2826 | privKey = otherKey; |
| wolfSSL | 15:117db924cf7c | 2827 | pubKey = &tmpKey; |
| wolfSSL | 15:117db924cf7c | 2828 | |
| wolfSSL | 15:117db924cf7c | 2829 | ret = wc_curve25519_import_public_ex(pubKeyDer, *pubKeySz, pubKey, |
| wolfSSL | 15:117db924cf7c | 2830 | EC25519_LITTLE_ENDIAN); |
| wolfSSL | 15:117db924cf7c | 2831 | } |
| wolfSSL | 15:117db924cf7c | 2832 | else { |
| wolfSSL | 15:117db924cf7c | 2833 | ret = BAD_FUNC_ARG; |
| wolfSSL | 15:117db924cf7c | 2834 | } |
| wolfSSL | 15:117db924cf7c | 2835 | |
| wolfSSL | 15:117db924cf7c | 2836 | /* generate shared secret and return it */ |
| wolfSSL | 15:117db924cf7c | 2837 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 2838 | ret = wc_curve25519_shared_secret_ex(privKey, pubKey, out, outlen, |
| wolfSSL | 15:117db924cf7c | 2839 | EC25519_LITTLE_ENDIAN); |
| wolfSSL | 15:117db924cf7c | 2840 | } |
| wolfSSL | 15:117db924cf7c | 2841 | |
| wolfSSL | 15:117db924cf7c | 2842 | wc_curve25519_free(&tmpKey); |
| wolfSSL | 15:117db924cf7c | 2843 | |
| wolfSSL | 16:8e0d178b1d1e | 2844 | WOLFSSL_PKMSG("PK 25519 PMS: ret %d, pubKeySz %d, outLen %d\n", |
| wolfSSL | 16:8e0d178b1d1e | 2845 | ret, *pubKeySz, *outlen); |
| wolfSSL | 16:8e0d178b1d1e | 2846 | |
| wolfSSL | 15:117db924cf7c | 2847 | return ret; |
| wolfSSL | 15:117db924cf7c | 2848 | } |
| wolfSSL | 15:117db924cf7c | 2849 | #endif /* HAVE_CURVE25519 */ |
| wolfSSL | 15:117db924cf7c | 2850 | |
| wolfSSL | 16:8e0d178b1d1e | 2851 | #ifdef HAVE_ED448 |
| wolfSSL | 16:8e0d178b1d1e | 2852 | static WC_INLINE int myEd448Sign(WOLFSSL* ssl, const byte* in, word32 inSz, |
| wolfSSL | 16:8e0d178b1d1e | 2853 | byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx) |
| wolfSSL | 16:8e0d178b1d1e | 2854 | { |
| wolfSSL | 16:8e0d178b1d1e | 2855 | int ret; |
| wolfSSL | 16:8e0d178b1d1e | 2856 | word32 idx = 0; |
| wolfSSL | 16:8e0d178b1d1e | 2857 | ed448_key myKey; |
| wolfSSL | 16:8e0d178b1d1e | 2858 | byte* keyBuf = (byte*)key; |
| wolfSSL | 16:8e0d178b1d1e | 2859 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 16:8e0d178b1d1e | 2860 | |
| wolfSSL | 16:8e0d178b1d1e | 2861 | (void)ssl; |
| wolfSSL | 16:8e0d178b1d1e | 2862 | (void)cbInfo; |
| wolfSSL | 16:8e0d178b1d1e | 2863 | |
| wolfSSL | 16:8e0d178b1d1e | 2864 | WOLFSSL_PKMSG("PK 448 Sign: inSz %d, keySz %d\n", inSz, keySz); |
| wolfSSL | 16:8e0d178b1d1e | 2865 | |
| wolfSSL | 16:8e0d178b1d1e | 2866 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 16:8e0d178b1d1e | 2867 | ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz); |
| wolfSSL | 16:8e0d178b1d1e | 2868 | if (ret != 0) |
| wolfSSL | 16:8e0d178b1d1e | 2869 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2870 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 2871 | |
| wolfSSL | 16:8e0d178b1d1e | 2872 | ret = wc_ed448_init(&myKey); |
| wolfSSL | 16:8e0d178b1d1e | 2873 | if (ret == 0) { |
| wolfSSL | 16:8e0d178b1d1e | 2874 | ret = wc_Ed448PrivateKeyDecode(keyBuf, &idx, &myKey, keySz); |
| wolfSSL | 16:8e0d178b1d1e | 2875 | if (ret == 0) |
| wolfSSL | 16:8e0d178b1d1e | 2876 | ret = wc_ed448_sign_msg(in, inSz, out, outSz, &myKey); |
| wolfSSL | 16:8e0d178b1d1e | 2877 | wc_ed448_free(&myKey); |
| wolfSSL | 16:8e0d178b1d1e | 2878 | } |
| wolfSSL | 16:8e0d178b1d1e | 2879 | |
| wolfSSL | 16:8e0d178b1d1e | 2880 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 16:8e0d178b1d1e | 2881 | free(keyBuf); |
| wolfSSL | 16:8e0d178b1d1e | 2882 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 2883 | |
| wolfSSL | 16:8e0d178b1d1e | 2884 | WOLFSSL_PKMSG("PK 448 Sign: ret %d, outSz %d\n", ret, *outSz); |
| wolfSSL | 16:8e0d178b1d1e | 2885 | |
| wolfSSL | 16:8e0d178b1d1e | 2886 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2887 | } |
| wolfSSL | 16:8e0d178b1d1e | 2888 | |
| wolfSSL | 16:8e0d178b1d1e | 2889 | |
| wolfSSL | 16:8e0d178b1d1e | 2890 | static WC_INLINE int myEd448Verify(WOLFSSL* ssl, const byte* sig, word32 sigSz, |
| wolfSSL | 16:8e0d178b1d1e | 2891 | const byte* msg, word32 msgSz, const byte* key, word32 keySz, |
| wolfSSL | 16:8e0d178b1d1e | 2892 | int* result, void* ctx) |
| wolfSSL | 16:8e0d178b1d1e | 2893 | { |
| wolfSSL | 16:8e0d178b1d1e | 2894 | int ret; |
| wolfSSL | 16:8e0d178b1d1e | 2895 | ed448_key myKey; |
| wolfSSL | 16:8e0d178b1d1e | 2896 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 16:8e0d178b1d1e | 2897 | |
| wolfSSL | 16:8e0d178b1d1e | 2898 | (void)ssl; |
| wolfSSL | 16:8e0d178b1d1e | 2899 | (void)cbInfo; |
| wolfSSL | 16:8e0d178b1d1e | 2900 | |
| wolfSSL | 16:8e0d178b1d1e | 2901 | WOLFSSL_PKMSG("PK 448 Verify: sigSz %d, msgSz %d, keySz %d\n", sigSz, msgSz, |
| wolfSSL | 16:8e0d178b1d1e | 2902 | keySz); |
| wolfSSL | 16:8e0d178b1d1e | 2903 | |
| wolfSSL | 16:8e0d178b1d1e | 2904 | ret = wc_ed448_init(&myKey); |
| wolfSSL | 16:8e0d178b1d1e | 2905 | if (ret == 0) { |
| wolfSSL | 16:8e0d178b1d1e | 2906 | ret = wc_ed448_import_public(key, keySz, &myKey); |
| wolfSSL | 16:8e0d178b1d1e | 2907 | if (ret == 0) { |
| wolfSSL | 16:8e0d178b1d1e | 2908 | ret = wc_ed448_verify_msg(sig, sigSz, msg, msgSz, result, &myKey); |
| wolfSSL | 16:8e0d178b1d1e | 2909 | } |
| wolfSSL | 16:8e0d178b1d1e | 2910 | wc_ed448_free(&myKey); |
| wolfSSL | 16:8e0d178b1d1e | 2911 | } |
| wolfSSL | 16:8e0d178b1d1e | 2912 | |
| wolfSSL | 16:8e0d178b1d1e | 2913 | WOLFSSL_PKMSG("PK 448 Verify: ret %d, result %d\n", ret, *result); |
| wolfSSL | 16:8e0d178b1d1e | 2914 | |
| wolfSSL | 16:8e0d178b1d1e | 2915 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2916 | } |
| wolfSSL | 16:8e0d178b1d1e | 2917 | #endif /* HAVE_ED448 */ |
| wolfSSL | 16:8e0d178b1d1e | 2918 | |
| wolfSSL | 16:8e0d178b1d1e | 2919 | #ifdef HAVE_CURVE448 |
| wolfSSL | 16:8e0d178b1d1e | 2920 | static WC_INLINE int myX448KeyGen(WOLFSSL* ssl, curve448_key* key, |
| wolfSSL | 16:8e0d178b1d1e | 2921 | unsigned int keySz, void* ctx) |
| wolfSSL | 16:8e0d178b1d1e | 2922 | { |
| wolfSSL | 16:8e0d178b1d1e | 2923 | int ret; |
| wolfSSL | 16:8e0d178b1d1e | 2924 | WC_RNG rng; |
| wolfSSL | 16:8e0d178b1d1e | 2925 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 16:8e0d178b1d1e | 2926 | |
| wolfSSL | 16:8e0d178b1d1e | 2927 | (void)ssl; |
| wolfSSL | 16:8e0d178b1d1e | 2928 | (void)cbInfo; |
| wolfSSL | 16:8e0d178b1d1e | 2929 | |
| wolfSSL | 16:8e0d178b1d1e | 2930 | WOLFSSL_PKMSG("PK 448 KeyGen: keySz %d\n", keySz); |
| wolfSSL | 16:8e0d178b1d1e | 2931 | |
| wolfSSL | 16:8e0d178b1d1e | 2932 | ret = wc_InitRng(&rng); |
| wolfSSL | 16:8e0d178b1d1e | 2933 | if (ret != 0) |
| wolfSSL | 16:8e0d178b1d1e | 2934 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2935 | |
| wolfSSL | 16:8e0d178b1d1e | 2936 | ret = wc_curve448_make_key(&rng, keySz, key); |
| wolfSSL | 16:8e0d178b1d1e | 2937 | |
| wolfSSL | 16:8e0d178b1d1e | 2938 | wc_FreeRng(&rng); |
| wolfSSL | 16:8e0d178b1d1e | 2939 | |
| wolfSSL | 16:8e0d178b1d1e | 2940 | WOLFSSL_PKMSG("PK 448 KeyGen: ret %d\n", ret); |
| wolfSSL | 16:8e0d178b1d1e | 2941 | |
| wolfSSL | 16:8e0d178b1d1e | 2942 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2943 | } |
| wolfSSL | 16:8e0d178b1d1e | 2944 | |
| wolfSSL | 16:8e0d178b1d1e | 2945 | static WC_INLINE int myX448SharedSecret(WOLFSSL* ssl, curve448_key* otherKey, |
| wolfSSL | 16:8e0d178b1d1e | 2946 | unsigned char* pubKeyDer, unsigned int* pubKeySz, |
| wolfSSL | 16:8e0d178b1d1e | 2947 | unsigned char* out, unsigned int* outlen, |
| wolfSSL | 16:8e0d178b1d1e | 2948 | int side, void* ctx) |
| wolfSSL | 16:8e0d178b1d1e | 2949 | { |
| wolfSSL | 16:8e0d178b1d1e | 2950 | int ret; |
| wolfSSL | 16:8e0d178b1d1e | 2951 | curve448_key* privKey = NULL; |
| wolfSSL | 16:8e0d178b1d1e | 2952 | curve448_key* pubKey = NULL; |
| wolfSSL | 16:8e0d178b1d1e | 2953 | curve448_key tmpKey; |
| wolfSSL | 16:8e0d178b1d1e | 2954 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 16:8e0d178b1d1e | 2955 | |
| wolfSSL | 16:8e0d178b1d1e | 2956 | (void)ssl; |
| wolfSSL | 16:8e0d178b1d1e | 2957 | (void)cbInfo; |
| wolfSSL | 16:8e0d178b1d1e | 2958 | |
| wolfSSL | 16:8e0d178b1d1e | 2959 | WOLFSSL_PKMSG("PK 448 PMS: side %s\n", |
| wolfSSL | 16:8e0d178b1d1e | 2960 | side == WOLFSSL_CLIENT_END ? "client" : "server"); |
| wolfSSL | 16:8e0d178b1d1e | 2961 | |
| wolfSSL | 16:8e0d178b1d1e | 2962 | ret = wc_curve448_init(&tmpKey); |
| wolfSSL | 16:8e0d178b1d1e | 2963 | if (ret != 0) { |
| wolfSSL | 16:8e0d178b1d1e | 2964 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 2965 | } |
| wolfSSL | 16:8e0d178b1d1e | 2966 | |
| wolfSSL | 16:8e0d178b1d1e | 2967 | /* for client: create and export public key */ |
| wolfSSL | 16:8e0d178b1d1e | 2968 | if (side == WOLFSSL_CLIENT_END) { |
| wolfSSL | 16:8e0d178b1d1e | 2969 | WC_RNG rng; |
| wolfSSL | 16:8e0d178b1d1e | 2970 | |
| wolfSSL | 16:8e0d178b1d1e | 2971 | privKey = &tmpKey; |
| wolfSSL | 16:8e0d178b1d1e | 2972 | pubKey = otherKey; |
| wolfSSL | 16:8e0d178b1d1e | 2973 | |
| wolfSSL | 16:8e0d178b1d1e | 2974 | ret = wc_InitRng(&rng); |
| wolfSSL | 16:8e0d178b1d1e | 2975 | if (ret == 0) { |
| wolfSSL | 16:8e0d178b1d1e | 2976 | ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, privKey); |
| wolfSSL | 16:8e0d178b1d1e | 2977 | if (ret == 0) { |
| wolfSSL | 16:8e0d178b1d1e | 2978 | ret = wc_curve448_export_public_ex(privKey, pubKeyDer, |
| wolfSSL | 16:8e0d178b1d1e | 2979 | pubKeySz, EC448_LITTLE_ENDIAN); |
| wolfSSL | 16:8e0d178b1d1e | 2980 | } |
| wolfSSL | 16:8e0d178b1d1e | 2981 | wc_FreeRng(&rng); |
| wolfSSL | 16:8e0d178b1d1e | 2982 | } |
| wolfSSL | 16:8e0d178b1d1e | 2983 | } |
| wolfSSL | 16:8e0d178b1d1e | 2984 | |
| wolfSSL | 16:8e0d178b1d1e | 2985 | /* for server: import public key */ |
| wolfSSL | 16:8e0d178b1d1e | 2986 | else if (side == WOLFSSL_SERVER_END) { |
| wolfSSL | 16:8e0d178b1d1e | 2987 | privKey = otherKey; |
| wolfSSL | 16:8e0d178b1d1e | 2988 | pubKey = &tmpKey; |
| wolfSSL | 16:8e0d178b1d1e | 2989 | |
| wolfSSL | 16:8e0d178b1d1e | 2990 | ret = wc_curve448_import_public_ex(pubKeyDer, *pubKeySz, pubKey, |
| wolfSSL | 16:8e0d178b1d1e | 2991 | EC448_LITTLE_ENDIAN); |
| wolfSSL | 16:8e0d178b1d1e | 2992 | } |
| wolfSSL | 16:8e0d178b1d1e | 2993 | else { |
| wolfSSL | 16:8e0d178b1d1e | 2994 | ret = BAD_FUNC_ARG; |
| wolfSSL | 16:8e0d178b1d1e | 2995 | } |
| wolfSSL | 16:8e0d178b1d1e | 2996 | |
| wolfSSL | 16:8e0d178b1d1e | 2997 | /* generate shared secret and return it */ |
| wolfSSL | 16:8e0d178b1d1e | 2998 | if (ret == 0) { |
| wolfSSL | 16:8e0d178b1d1e | 2999 | ret = wc_curve448_shared_secret_ex(privKey, pubKey, out, outlen, |
| wolfSSL | 16:8e0d178b1d1e | 3000 | EC448_LITTLE_ENDIAN); |
| wolfSSL | 16:8e0d178b1d1e | 3001 | } |
| wolfSSL | 16:8e0d178b1d1e | 3002 | |
| wolfSSL | 16:8e0d178b1d1e | 3003 | wc_curve448_free(&tmpKey); |
| wolfSSL | 16:8e0d178b1d1e | 3004 | |
| wolfSSL | 16:8e0d178b1d1e | 3005 | WOLFSSL_PKMSG("PK 448 PMS: ret %d, pubKeySz %d, outLen %d\n", |
| wolfSSL | 16:8e0d178b1d1e | 3006 | ret, *pubKeySz, *outlen); |
| wolfSSL | 16:8e0d178b1d1e | 3007 | |
| wolfSSL | 16:8e0d178b1d1e | 3008 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 3009 | } |
| wolfSSL | 16:8e0d178b1d1e | 3010 | #endif /* HAVE_CURVE448 */ |
| wolfSSL | 16:8e0d178b1d1e | 3011 | |
| wolfSSL | 15:117db924cf7c | 3012 | #endif /* HAVE_ECC */ |
| wolfSSL | 15:117db924cf7c | 3013 | |
| wolfSSL | 15:117db924cf7c | 3014 | #ifndef NO_DH |
| wolfSSL | 15:117db924cf7c | 3015 | static WC_INLINE int myDhCallback(WOLFSSL* ssl, struct DhKey* key, |
| wolfSSL | 15:117db924cf7c | 3016 | const unsigned char* priv, unsigned int privSz, |
| wolfSSL | 15:117db924cf7c | 3017 | const unsigned char* pubKeyDer, unsigned int pubKeySz, |
| wolfSSL | 15:117db924cf7c | 3018 | unsigned char* out, unsigned int* outlen, |
| wolfSSL | 15:117db924cf7c | 3019 | void* ctx) |
| wolfSSL | 15:117db924cf7c | 3020 | { |
| wolfSSL | 16:8e0d178b1d1e | 3021 | int ret; |
| wolfSSL | 15:117db924cf7c | 3022 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 3023 | |
| wolfSSL | 15:117db924cf7c | 3024 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 3025 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 3026 | |
| wolfSSL | 15:117db924cf7c | 3027 | /* return 0 on success */ |
| wolfSSL | 16:8e0d178b1d1e | 3028 | ret = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz); |
| wolfSSL | 16:8e0d178b1d1e | 3029 | |
| wolfSSL | 16:8e0d178b1d1e | 3030 | WOLFSSL_PKMSG("PK ED Agree: ret %d, privSz %d, pubKeySz %d, outlen %d\n", |
| wolfSSL | 16:8e0d178b1d1e | 3031 | ret, privSz, pubKeySz, *outlen); |
| wolfSSL | 16:8e0d178b1d1e | 3032 | |
| wolfSSL | 16:8e0d178b1d1e | 3033 | return ret; |
| wolfSSL | 15:117db924cf7c | 3034 | }; |
| wolfSSL | 15:117db924cf7c | 3035 | |
| wolfSSL | 15:117db924cf7c | 3036 | #endif /* !NO_DH */ |
| wolfSSL | 15:117db924cf7c | 3037 | |
| wolfSSL | 15:117db924cf7c | 3038 | #ifndef NO_RSA |
| wolfSSL | 15:117db924cf7c | 3039 | |
| wolfSSL | 15:117db924cf7c | 3040 | static WC_INLINE int myRsaSign(WOLFSSL* ssl, const byte* in, word32 inSz, |
| wolfSSL | 15:117db924cf7c | 3041 | byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx) |
| wolfSSL | 15:117db924cf7c | 3042 | { |
| wolfSSL | 15:117db924cf7c | 3043 | WC_RNG rng; |
| wolfSSL | 15:117db924cf7c | 3044 | int ret; |
| wolfSSL | 15:117db924cf7c | 3045 | word32 idx = 0; |
| wolfSSL | 15:117db924cf7c | 3046 | RsaKey myKey; |
| wolfSSL | 15:117db924cf7c | 3047 | byte* keyBuf = (byte*)key; |
| wolfSSL | 15:117db924cf7c | 3048 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 3049 | |
| wolfSSL | 15:117db924cf7c | 3050 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 3051 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 3052 | |
| wolfSSL | 16:8e0d178b1d1e | 3053 | WOLFSSL_PKMSG("PK RSA Sign: inSz %d, keySz %d\n", inSz, keySz); |
| wolfSSL | 16:8e0d178b1d1e | 3054 | |
| wolfSSL | 15:117db924cf7c | 3055 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 3056 | ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz); |
| wolfSSL | 15:117db924cf7c | 3057 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 3058 | return ret; |
| wolfSSL | 15:117db924cf7c | 3059 | #endif |
| wolfSSL | 15:117db924cf7c | 3060 | |
| wolfSSL | 15:117db924cf7c | 3061 | ret = wc_InitRng(&rng); |
| wolfSSL | 15:117db924cf7c | 3062 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 3063 | return ret; |
| wolfSSL | 15:117db924cf7c | 3064 | |
| wolfSSL | 15:117db924cf7c | 3065 | ret = wc_InitRsaKey(&myKey, NULL); |
| wolfSSL | 15:117db924cf7c | 3066 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 3067 | ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz); |
| wolfSSL | 15:117db924cf7c | 3068 | if (ret == 0) |
| wolfSSL | 15:117db924cf7c | 3069 | ret = wc_RsaSSL_Sign(in, inSz, out, *outSz, &myKey, &rng); |
| wolfSSL | 15:117db924cf7c | 3070 | if (ret > 0) { /* save and convert to 0 success */ |
| wolfSSL | 15:117db924cf7c | 3071 | *outSz = ret; |
| wolfSSL | 15:117db924cf7c | 3072 | ret = 0; |
| wolfSSL | 15:117db924cf7c | 3073 | } |
| wolfSSL | 15:117db924cf7c | 3074 | wc_FreeRsaKey(&myKey); |
| wolfSSL | 15:117db924cf7c | 3075 | } |
| wolfSSL | 15:117db924cf7c | 3076 | wc_FreeRng(&rng); |
| wolfSSL | 15:117db924cf7c | 3077 | |
| wolfSSL | 15:117db924cf7c | 3078 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 3079 | free(keyBuf); |
| wolfSSL | 15:117db924cf7c | 3080 | #endif |
| wolfSSL | 15:117db924cf7c | 3081 | |
| wolfSSL | 16:8e0d178b1d1e | 3082 | WOLFSSL_PKMSG("PK RSA Sign: ret %d, outSz %d\n", ret, *outSz); |
| wolfSSL | 16:8e0d178b1d1e | 3083 | |
| wolfSSL | 15:117db924cf7c | 3084 | return ret; |
| wolfSSL | 15:117db924cf7c | 3085 | } |
| wolfSSL | 15:117db924cf7c | 3086 | |
| wolfSSL | 15:117db924cf7c | 3087 | |
| wolfSSL | 15:117db924cf7c | 3088 | static WC_INLINE int myRsaVerify(WOLFSSL* ssl, byte* sig, word32 sigSz, |
| wolfSSL | 15:117db924cf7c | 3089 | byte** out, const byte* key, word32 keySz, void* ctx) |
| wolfSSL | 15:117db924cf7c | 3090 | { |
| wolfSSL | 15:117db924cf7c | 3091 | int ret; |
| wolfSSL | 15:117db924cf7c | 3092 | word32 idx = 0; |
| wolfSSL | 15:117db924cf7c | 3093 | RsaKey myKey; |
| wolfSSL | 15:117db924cf7c | 3094 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 3095 | |
| wolfSSL | 15:117db924cf7c | 3096 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 3097 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 3098 | |
| wolfSSL | 16:8e0d178b1d1e | 3099 | WOLFSSL_PKMSG("PK RSA Verify: sigSz %d, keySz %d\n", sigSz, keySz); |
| wolfSSL | 16:8e0d178b1d1e | 3100 | |
| wolfSSL | 15:117db924cf7c | 3101 | ret = wc_InitRsaKey(&myKey, NULL); |
| wolfSSL | 15:117db924cf7c | 3102 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 3103 | ret = wc_RsaPublicKeyDecode(key, &idx, &myKey, keySz); |
| wolfSSL | 15:117db924cf7c | 3104 | if (ret == 0) |
| wolfSSL | 15:117db924cf7c | 3105 | ret = wc_RsaSSL_VerifyInline(sig, sigSz, out, &myKey); |
| wolfSSL | 15:117db924cf7c | 3106 | wc_FreeRsaKey(&myKey); |
| wolfSSL | 15:117db924cf7c | 3107 | } |
| wolfSSL | 15:117db924cf7c | 3108 | |
| wolfSSL | 16:8e0d178b1d1e | 3109 | WOLFSSL_PKMSG("PK RSA Verify: ret %d\n", ret); |
| wolfSSL | 16:8e0d178b1d1e | 3110 | |
| wolfSSL | 15:117db924cf7c | 3111 | return ret; |
| wolfSSL | 15:117db924cf7c | 3112 | } |
| wolfSSL | 15:117db924cf7c | 3113 | |
| wolfSSL | 15:117db924cf7c | 3114 | static WC_INLINE int myRsaSignCheck(WOLFSSL* ssl, byte* sig, word32 sigSz, |
| wolfSSL | 15:117db924cf7c | 3115 | byte** out, const byte* key, word32 keySz, void* ctx) |
| wolfSSL | 15:117db924cf7c | 3116 | { |
| wolfSSL | 15:117db924cf7c | 3117 | int ret; |
| wolfSSL | 15:117db924cf7c | 3118 | word32 idx = 0; |
| wolfSSL | 15:117db924cf7c | 3119 | RsaKey myKey; |
| wolfSSL | 15:117db924cf7c | 3120 | byte* keyBuf = (byte*)key; |
| wolfSSL | 15:117db924cf7c | 3121 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 3122 | |
| wolfSSL | 15:117db924cf7c | 3123 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 3124 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 3125 | |
| wolfSSL | 16:8e0d178b1d1e | 3126 | WOLFSSL_PKMSG("PK RSA SignCheck: sigSz %d, keySz %d\n", sigSz, keySz); |
| wolfSSL | 16:8e0d178b1d1e | 3127 | |
| wolfSSL | 15:117db924cf7c | 3128 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 3129 | ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz); |
| wolfSSL | 15:117db924cf7c | 3130 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 3131 | return ret; |
| wolfSSL | 15:117db924cf7c | 3132 | #endif |
| wolfSSL | 15:117db924cf7c | 3133 | |
| wolfSSL | 15:117db924cf7c | 3134 | ret = wc_InitRsaKey(&myKey, NULL); |
| wolfSSL | 15:117db924cf7c | 3135 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 3136 | ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz); |
| wolfSSL | 15:117db924cf7c | 3137 | if (ret == 0) |
| wolfSSL | 15:117db924cf7c | 3138 | ret = wc_RsaSSL_VerifyInline(sig, sigSz, out, &myKey); |
| wolfSSL | 15:117db924cf7c | 3139 | wc_FreeRsaKey(&myKey); |
| wolfSSL | 15:117db924cf7c | 3140 | } |
| wolfSSL | 15:117db924cf7c | 3141 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 3142 | free(keyBuf); |
| wolfSSL | 15:117db924cf7c | 3143 | #endif |
| wolfSSL | 15:117db924cf7c | 3144 | |
| wolfSSL | 16:8e0d178b1d1e | 3145 | WOLFSSL_PKMSG("PK RSA SignCheck: ret %d\n", ret); |
| wolfSSL | 16:8e0d178b1d1e | 3146 | |
| wolfSSL | 15:117db924cf7c | 3147 | return ret; |
| wolfSSL | 15:117db924cf7c | 3148 | } |
| wolfSSL | 15:117db924cf7c | 3149 | |
| wolfSSL | 15:117db924cf7c | 3150 | #ifdef WC_RSA_PSS |
| wolfSSL | 15:117db924cf7c | 3151 | static WC_INLINE int myRsaPssSign(WOLFSSL* ssl, const byte* in, word32 inSz, |
| wolfSSL | 15:117db924cf7c | 3152 | byte* out, word32* outSz, int hash, int mgf, const byte* key, |
| wolfSSL | 15:117db924cf7c | 3153 | word32 keySz, void* ctx) |
| wolfSSL | 15:117db924cf7c | 3154 | { |
| wolfSSL | 15:117db924cf7c | 3155 | enum wc_HashType hashType = WC_HASH_TYPE_NONE; |
| wolfSSL | 15:117db924cf7c | 3156 | WC_RNG rng; |
| wolfSSL | 15:117db924cf7c | 3157 | int ret; |
| wolfSSL | 15:117db924cf7c | 3158 | word32 idx = 0; |
| wolfSSL | 15:117db924cf7c | 3159 | RsaKey myKey; |
| wolfSSL | 15:117db924cf7c | 3160 | byte* keyBuf = (byte*)key; |
| wolfSSL | 15:117db924cf7c | 3161 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 3162 | |
| wolfSSL | 15:117db924cf7c | 3163 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 3164 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 3165 | |
| wolfSSL | 16:8e0d178b1d1e | 3166 | WOLFSSL_PKMSG("PK RSA PSS Sign: inSz %d, hash %d, mgf %d, keySz %d\n", |
| wolfSSL | 16:8e0d178b1d1e | 3167 | inSz, hash, mgf, keySz); |
| wolfSSL | 16:8e0d178b1d1e | 3168 | |
| wolfSSL | 15:117db924cf7c | 3169 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 3170 | ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz); |
| wolfSSL | 15:117db924cf7c | 3171 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 3172 | return ret; |
| wolfSSL | 15:117db924cf7c | 3173 | #endif |
| wolfSSL | 15:117db924cf7c | 3174 | |
| wolfSSL | 15:117db924cf7c | 3175 | switch (hash) { |
| wolfSSL | 15:117db924cf7c | 3176 | #ifndef NO_SHA256 |
| wolfSSL | 15:117db924cf7c | 3177 | case SHA256h: |
| wolfSSL | 15:117db924cf7c | 3178 | hashType = WC_HASH_TYPE_SHA256; |
| wolfSSL | 15:117db924cf7c | 3179 | break; |
| wolfSSL | 15:117db924cf7c | 3180 | #endif |
| wolfSSL | 15:117db924cf7c | 3181 | #ifdef WOLFSSL_SHA384 |
| wolfSSL | 15:117db924cf7c | 3182 | case SHA384h: |
| wolfSSL | 15:117db924cf7c | 3183 | hashType = WC_HASH_TYPE_SHA384; |
| wolfSSL | 15:117db924cf7c | 3184 | break; |
| wolfSSL | 15:117db924cf7c | 3185 | #endif |
| wolfSSL | 15:117db924cf7c | 3186 | #ifdef WOLFSSL_SHA512 |
| wolfSSL | 15:117db924cf7c | 3187 | case SHA512h: |
| wolfSSL | 15:117db924cf7c | 3188 | hashType = WC_HASH_TYPE_SHA512; |
| wolfSSL | 15:117db924cf7c | 3189 | break; |
| wolfSSL | 15:117db924cf7c | 3190 | #endif |
| wolfSSL | 15:117db924cf7c | 3191 | } |
| wolfSSL | 15:117db924cf7c | 3192 | |
| wolfSSL | 15:117db924cf7c | 3193 | ret = wc_InitRng(&rng); |
| wolfSSL | 15:117db924cf7c | 3194 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 3195 | return ret; |
| wolfSSL | 15:117db924cf7c | 3196 | |
| wolfSSL | 15:117db924cf7c | 3197 | ret = wc_InitRsaKey(&myKey, NULL); |
| wolfSSL | 15:117db924cf7c | 3198 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 3199 | ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz); |
| wolfSSL | 15:117db924cf7c | 3200 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 3201 | ret = wc_RsaPSS_Sign(in, inSz, out, *outSz, hashType, mgf, &myKey, |
| wolfSSL | 15:117db924cf7c | 3202 | &rng); |
| wolfSSL | 15:117db924cf7c | 3203 | } |
| wolfSSL | 15:117db924cf7c | 3204 | if (ret > 0) { /* save and convert to 0 success */ |
| wolfSSL | 15:117db924cf7c | 3205 | *outSz = ret; |
| wolfSSL | 15:117db924cf7c | 3206 | ret = 0; |
| wolfSSL | 15:117db924cf7c | 3207 | } |
| wolfSSL | 15:117db924cf7c | 3208 | wc_FreeRsaKey(&myKey); |
| wolfSSL | 15:117db924cf7c | 3209 | } |
| wolfSSL | 15:117db924cf7c | 3210 | wc_FreeRng(&rng); |
| wolfSSL | 15:117db924cf7c | 3211 | |
| wolfSSL | 15:117db924cf7c | 3212 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 3213 | free(keyBuf); |
| wolfSSL | 15:117db924cf7c | 3214 | #endif |
| wolfSSL | 15:117db924cf7c | 3215 | |
| wolfSSL | 16:8e0d178b1d1e | 3216 | WOLFSSL_PKMSG("PK RSA PSS Sign: ret %d, outSz %d\n", ret, *outSz); |
| wolfSSL | 16:8e0d178b1d1e | 3217 | |
| wolfSSL | 15:117db924cf7c | 3218 | return ret; |
| wolfSSL | 15:117db924cf7c | 3219 | } |
| wolfSSL | 15:117db924cf7c | 3220 | |
| wolfSSL | 15:117db924cf7c | 3221 | |
| wolfSSL | 15:117db924cf7c | 3222 | static WC_INLINE int myRsaPssVerify(WOLFSSL* ssl, byte* sig, word32 sigSz, |
| wolfSSL | 15:117db924cf7c | 3223 | byte** out, int hash, int mgf, const byte* key, word32 keySz, void* ctx) |
| wolfSSL | 15:117db924cf7c | 3224 | { |
| wolfSSL | 15:117db924cf7c | 3225 | int ret; |
| wolfSSL | 15:117db924cf7c | 3226 | word32 idx = 0; |
| wolfSSL | 15:117db924cf7c | 3227 | RsaKey myKey; |
| wolfSSL | 15:117db924cf7c | 3228 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 3229 | enum wc_HashType hashType = WC_HASH_TYPE_NONE; |
| wolfSSL | 15:117db924cf7c | 3230 | |
| wolfSSL | 15:117db924cf7c | 3231 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 3232 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 3233 | |
| wolfSSL | 16:8e0d178b1d1e | 3234 | WOLFSSL_PKMSG("PK RSA PSS Verify: sigSz %d, hash %d, mgf %d, keySz %d\n", |
| wolfSSL | 16:8e0d178b1d1e | 3235 | sigSz, hash, mgf, keySz); |
| wolfSSL | 16:8e0d178b1d1e | 3236 | |
| wolfSSL | 15:117db924cf7c | 3237 | switch (hash) { |
| wolfSSL | 15:117db924cf7c | 3238 | #ifndef NO_SHA256 |
| wolfSSL | 15:117db924cf7c | 3239 | case SHA256h: |
| wolfSSL | 15:117db924cf7c | 3240 | hashType = WC_HASH_TYPE_SHA256; |
| wolfSSL | 15:117db924cf7c | 3241 | break; |
| wolfSSL | 15:117db924cf7c | 3242 | #endif |
| wolfSSL | 15:117db924cf7c | 3243 | #ifdef WOLFSSL_SHA384 |
| wolfSSL | 15:117db924cf7c | 3244 | case SHA384h: |
| wolfSSL | 15:117db924cf7c | 3245 | hashType = WC_HASH_TYPE_SHA384; |
| wolfSSL | 15:117db924cf7c | 3246 | break; |
| wolfSSL | 15:117db924cf7c | 3247 | #endif |
| wolfSSL | 15:117db924cf7c | 3248 | #ifdef WOLFSSL_SHA512 |
| wolfSSL | 15:117db924cf7c | 3249 | case SHA512h: |
| wolfSSL | 15:117db924cf7c | 3250 | hashType = WC_HASH_TYPE_SHA512; |
| wolfSSL | 15:117db924cf7c | 3251 | break; |
| wolfSSL | 15:117db924cf7c | 3252 | #endif |
| wolfSSL | 15:117db924cf7c | 3253 | } |
| wolfSSL | 15:117db924cf7c | 3254 | |
| wolfSSL | 15:117db924cf7c | 3255 | ret = wc_InitRsaKey(&myKey, NULL); |
| wolfSSL | 15:117db924cf7c | 3256 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 3257 | ret = wc_RsaPublicKeyDecode(key, &idx, &myKey, keySz); |
| wolfSSL | 15:117db924cf7c | 3258 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 3259 | ret = wc_RsaPSS_VerifyInline(sig, sigSz, out, hashType, mgf, |
| wolfSSL | 15:117db924cf7c | 3260 | &myKey); |
| wolfSSL | 15:117db924cf7c | 3261 | } |
| wolfSSL | 15:117db924cf7c | 3262 | wc_FreeRsaKey(&myKey); |
| wolfSSL | 15:117db924cf7c | 3263 | } |
| wolfSSL | 15:117db924cf7c | 3264 | |
| wolfSSL | 16:8e0d178b1d1e | 3265 | WOLFSSL_PKMSG("PK RSA PSS Verify: ret %d\n", ret); |
| wolfSSL | 16:8e0d178b1d1e | 3266 | |
| wolfSSL | 15:117db924cf7c | 3267 | return ret; |
| wolfSSL | 15:117db924cf7c | 3268 | } |
| wolfSSL | 15:117db924cf7c | 3269 | |
| wolfSSL | 15:117db924cf7c | 3270 | static WC_INLINE int myRsaPssSignCheck(WOLFSSL* ssl, byte* sig, word32 sigSz, |
| wolfSSL | 15:117db924cf7c | 3271 | byte** out, int hash, int mgf, const byte* key, word32 keySz, void* ctx) |
| wolfSSL | 15:117db924cf7c | 3272 | { |
| wolfSSL | 15:117db924cf7c | 3273 | int ret; |
| wolfSSL | 15:117db924cf7c | 3274 | word32 idx = 0; |
| wolfSSL | 15:117db924cf7c | 3275 | RsaKey myKey; |
| wolfSSL | 15:117db924cf7c | 3276 | byte* keyBuf = (byte*)key; |
| wolfSSL | 15:117db924cf7c | 3277 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 3278 | enum wc_HashType hashType = WC_HASH_TYPE_NONE; |
| wolfSSL | 15:117db924cf7c | 3279 | |
| wolfSSL | 15:117db924cf7c | 3280 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 3281 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 3282 | |
| wolfSSL | 16:8e0d178b1d1e | 3283 | WOLFSSL_PKMSG("PK RSA PSS SignCheck: sigSz %d, hash %d, mgf %d, keySz %d\n", |
| wolfSSL | 16:8e0d178b1d1e | 3284 | sigSz, hash, mgf, keySz); |
| wolfSSL | 16:8e0d178b1d1e | 3285 | |
| wolfSSL | 15:117db924cf7c | 3286 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 3287 | ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz); |
| wolfSSL | 15:117db924cf7c | 3288 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 3289 | return ret; |
| wolfSSL | 15:117db924cf7c | 3290 | #endif |
| wolfSSL | 15:117db924cf7c | 3291 | |
| wolfSSL | 15:117db924cf7c | 3292 | switch (hash) { |
| wolfSSL | 15:117db924cf7c | 3293 | #ifndef NO_SHA256 |
| wolfSSL | 15:117db924cf7c | 3294 | case SHA256h: |
| wolfSSL | 15:117db924cf7c | 3295 | hashType = WC_HASH_TYPE_SHA256; |
| wolfSSL | 15:117db924cf7c | 3296 | break; |
| wolfSSL | 15:117db924cf7c | 3297 | #endif |
| wolfSSL | 15:117db924cf7c | 3298 | #ifdef WOLFSSL_SHA384 |
| wolfSSL | 15:117db924cf7c | 3299 | case SHA384h: |
| wolfSSL | 15:117db924cf7c | 3300 | hashType = WC_HASH_TYPE_SHA384; |
| wolfSSL | 15:117db924cf7c | 3301 | break; |
| wolfSSL | 15:117db924cf7c | 3302 | #endif |
| wolfSSL | 15:117db924cf7c | 3303 | #ifdef WOLFSSL_SHA512 |
| wolfSSL | 15:117db924cf7c | 3304 | case SHA512h: |
| wolfSSL | 15:117db924cf7c | 3305 | hashType = WC_HASH_TYPE_SHA512; |
| wolfSSL | 15:117db924cf7c | 3306 | break; |
| wolfSSL | 15:117db924cf7c | 3307 | #endif |
| wolfSSL | 15:117db924cf7c | 3308 | } |
| wolfSSL | 15:117db924cf7c | 3309 | |
| wolfSSL | 15:117db924cf7c | 3310 | ret = wc_InitRsaKey(&myKey, NULL); |
| wolfSSL | 15:117db924cf7c | 3311 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 3312 | ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz); |
| wolfSSL | 15:117db924cf7c | 3313 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 3314 | ret = wc_RsaPSS_VerifyInline(sig, sigSz, out, hashType, mgf, |
| wolfSSL | 15:117db924cf7c | 3315 | &myKey); |
| wolfSSL | 15:117db924cf7c | 3316 | } |
| wolfSSL | 15:117db924cf7c | 3317 | wc_FreeRsaKey(&myKey); |
| wolfSSL | 15:117db924cf7c | 3318 | } |
| wolfSSL | 15:117db924cf7c | 3319 | |
| wolfSSL | 15:117db924cf7c | 3320 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 3321 | free(keyBuf); |
| wolfSSL | 15:117db924cf7c | 3322 | #endif |
| wolfSSL | 15:117db924cf7c | 3323 | |
| wolfSSL | 16:8e0d178b1d1e | 3324 | WOLFSSL_PKMSG("PK RSA PSS SignCheck: ret %d\n", ret); |
| wolfSSL | 16:8e0d178b1d1e | 3325 | |
| wolfSSL | 15:117db924cf7c | 3326 | return ret; |
| wolfSSL | 15:117db924cf7c | 3327 | } |
| wolfSSL | 15:117db924cf7c | 3328 | #endif |
| wolfSSL | 15:117db924cf7c | 3329 | |
| wolfSSL | 15:117db924cf7c | 3330 | |
| wolfSSL | 15:117db924cf7c | 3331 | static WC_INLINE int myRsaEnc(WOLFSSL* ssl, const byte* in, word32 inSz, |
| wolfSSL | 15:117db924cf7c | 3332 | byte* out, word32* outSz, const byte* key, |
| wolfSSL | 15:117db924cf7c | 3333 | word32 keySz, void* ctx) |
| wolfSSL | 15:117db924cf7c | 3334 | { |
| wolfSSL | 15:117db924cf7c | 3335 | int ret; |
| wolfSSL | 15:117db924cf7c | 3336 | word32 idx = 0; |
| wolfSSL | 15:117db924cf7c | 3337 | RsaKey myKey; |
| wolfSSL | 15:117db924cf7c | 3338 | WC_RNG rng; |
| wolfSSL | 15:117db924cf7c | 3339 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 3340 | |
| wolfSSL | 15:117db924cf7c | 3341 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 3342 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 3343 | |
| wolfSSL | 16:8e0d178b1d1e | 3344 | WOLFSSL_PKMSG("PK RSA Enc: inSz %d, keySz %d\n", inSz, keySz); |
| wolfSSL | 16:8e0d178b1d1e | 3345 | |
| wolfSSL | 15:117db924cf7c | 3346 | ret = wc_InitRng(&rng); |
| wolfSSL | 15:117db924cf7c | 3347 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 3348 | return ret; |
| wolfSSL | 15:117db924cf7c | 3349 | |
| wolfSSL | 15:117db924cf7c | 3350 | ret = wc_InitRsaKey(&myKey, NULL); |
| wolfSSL | 15:117db924cf7c | 3351 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 3352 | ret = wc_RsaPublicKeyDecode(key, &idx, &myKey, keySz); |
| wolfSSL | 15:117db924cf7c | 3353 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 3354 | ret = wc_RsaPublicEncrypt(in, inSz, out, *outSz, &myKey, &rng); |
| wolfSSL | 15:117db924cf7c | 3355 | if (ret > 0) { |
| wolfSSL | 15:117db924cf7c | 3356 | *outSz = ret; |
| wolfSSL | 15:117db924cf7c | 3357 | ret = 0; /* reset to success */ |
| wolfSSL | 15:117db924cf7c | 3358 | } |
| wolfSSL | 15:117db924cf7c | 3359 | } |
| wolfSSL | 15:117db924cf7c | 3360 | wc_FreeRsaKey(&myKey); |
| wolfSSL | 15:117db924cf7c | 3361 | } |
| wolfSSL | 15:117db924cf7c | 3362 | wc_FreeRng(&rng); |
| wolfSSL | 15:117db924cf7c | 3363 | |
| wolfSSL | 16:8e0d178b1d1e | 3364 | WOLFSSL_PKMSG("PK RSA Enc: ret %d, outSz %d\n", ret, *outSz); |
| wolfSSL | 16:8e0d178b1d1e | 3365 | |
| wolfSSL | 15:117db924cf7c | 3366 | return ret; |
| wolfSSL | 15:117db924cf7c | 3367 | } |
| wolfSSL | 15:117db924cf7c | 3368 | |
| wolfSSL | 15:117db924cf7c | 3369 | static WC_INLINE int myRsaDec(WOLFSSL* ssl, byte* in, word32 inSz, |
| wolfSSL | 15:117db924cf7c | 3370 | byte** out, |
| wolfSSL | 15:117db924cf7c | 3371 | const byte* key, word32 keySz, void* ctx) |
| wolfSSL | 15:117db924cf7c | 3372 | { |
| wolfSSL | 15:117db924cf7c | 3373 | int ret; |
| wolfSSL | 15:117db924cf7c | 3374 | word32 idx = 0; |
| wolfSSL | 15:117db924cf7c | 3375 | RsaKey myKey; |
| wolfSSL | 15:117db924cf7c | 3376 | byte* keyBuf = (byte*)key; |
| wolfSSL | 15:117db924cf7c | 3377 | PkCbInfo* cbInfo = (PkCbInfo*)ctx; |
| wolfSSL | 15:117db924cf7c | 3378 | |
| wolfSSL | 15:117db924cf7c | 3379 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 3380 | (void)cbInfo; |
| wolfSSL | 15:117db924cf7c | 3381 | |
| wolfSSL | 16:8e0d178b1d1e | 3382 | WOLFSSL_PKMSG("PK RSA Dec: inSz %d, keySz %d\n", inSz, keySz); |
| wolfSSL | 16:8e0d178b1d1e | 3383 | |
| wolfSSL | 15:117db924cf7c | 3384 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 3385 | ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz); |
| wolfSSL | 15:117db924cf7c | 3386 | if (ret != 0) |
| wolfSSL | 15:117db924cf7c | 3387 | return ret; |
| wolfSSL | 15:117db924cf7c | 3388 | #endif |
| wolfSSL | 15:117db924cf7c | 3389 | |
| wolfSSL | 15:117db924cf7c | 3390 | ret = wc_InitRsaKey(&myKey, NULL); |
| wolfSSL | 15:117db924cf7c | 3391 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 3392 | ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz); |
| wolfSSL | 15:117db924cf7c | 3393 | if (ret == 0) { |
| wolfSSL | 15:117db924cf7c | 3394 | #ifdef WC_RSA_BLINDING |
| wolfSSL | 15:117db924cf7c | 3395 | ret = wc_RsaSetRNG(&myKey, wolfSSL_GetRNG(ssl)); |
| wolfSSL | 15:117db924cf7c | 3396 | if (ret != 0) { |
| wolfSSL | 15:117db924cf7c | 3397 | wc_FreeRsaKey(&myKey); |
| wolfSSL | 15:117db924cf7c | 3398 | return ret; |
| wolfSSL | 15:117db924cf7c | 3399 | } |
| wolfSSL | 15:117db924cf7c | 3400 | #endif |
| wolfSSL | 15:117db924cf7c | 3401 | ret = wc_RsaPrivateDecryptInline(in, inSz, out, &myKey); |
| wolfSSL | 15:117db924cf7c | 3402 | } |
| wolfSSL | 15:117db924cf7c | 3403 | wc_FreeRsaKey(&myKey); |
| wolfSSL | 15:117db924cf7c | 3404 | } |
| wolfSSL | 15:117db924cf7c | 3405 | |
| wolfSSL | 15:117db924cf7c | 3406 | #ifdef TEST_PK_PRIVKEY |
| wolfSSL | 15:117db924cf7c | 3407 | free(keyBuf); |
| wolfSSL | 15:117db924cf7c | 3408 | #endif |
| wolfSSL | 15:117db924cf7c | 3409 | |
| wolfSSL | 16:8e0d178b1d1e | 3410 | WOLFSSL_PKMSG("PK RSA Dec: ret %d\n", ret); |
| wolfSSL | 16:8e0d178b1d1e | 3411 | |
| wolfSSL | 15:117db924cf7c | 3412 | return ret; |
| wolfSSL | 15:117db924cf7c | 3413 | } |
| wolfSSL | 15:117db924cf7c | 3414 | |
| wolfSSL | 15:117db924cf7c | 3415 | #endif /* NO_RSA */ |
| wolfSSL | 15:117db924cf7c | 3416 | |
| wolfSSL | 15:117db924cf7c | 3417 | static WC_INLINE void SetupPkCallbacks(WOLFSSL_CTX* ctx) |
| wolfSSL | 15:117db924cf7c | 3418 | { |
| wolfSSL | 15:117db924cf7c | 3419 | (void)ctx; |
| wolfSSL | 15:117db924cf7c | 3420 | |
| wolfSSL | 15:117db924cf7c | 3421 | #ifdef HAVE_ECC |
| wolfSSL | 15:117db924cf7c | 3422 | wolfSSL_CTX_SetEccKeyGenCb(ctx, myEccKeyGen); |
| wolfSSL | 15:117db924cf7c | 3423 | wolfSSL_CTX_SetEccSignCb(ctx, myEccSign); |
| wolfSSL | 15:117db924cf7c | 3424 | wolfSSL_CTX_SetEccVerifyCb(ctx, myEccVerify); |
| wolfSSL | 15:117db924cf7c | 3425 | wolfSSL_CTX_SetEccSharedSecretCb(ctx, myEccSharedSecret); |
| wolfSSL | 15:117db924cf7c | 3426 | #endif /* HAVE_ECC */ |
| wolfSSL | 15:117db924cf7c | 3427 | #ifndef NO_DH |
| wolfSSL | 15:117db924cf7c | 3428 | wolfSSL_CTX_SetDhAgreeCb(ctx, myDhCallback); |
| wolfSSL | 15:117db924cf7c | 3429 | #endif |
| wolfSSL | 15:117db924cf7c | 3430 | #ifdef HAVE_ED25519 |
| wolfSSL | 15:117db924cf7c | 3431 | wolfSSL_CTX_SetEd25519SignCb(ctx, myEd25519Sign); |
| wolfSSL | 15:117db924cf7c | 3432 | wolfSSL_CTX_SetEd25519VerifyCb(ctx, myEd25519Verify); |
| wolfSSL | 15:117db924cf7c | 3433 | #endif |
| wolfSSL | 15:117db924cf7c | 3434 | #ifdef HAVE_CURVE25519 |
| wolfSSL | 15:117db924cf7c | 3435 | wolfSSL_CTX_SetX25519KeyGenCb(ctx, myX25519KeyGen); |
| wolfSSL | 15:117db924cf7c | 3436 | wolfSSL_CTX_SetX25519SharedSecretCb(ctx, myX25519SharedSecret); |
| wolfSSL | 15:117db924cf7c | 3437 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 3438 | #ifdef HAVE_ED448 |
| wolfSSL | 16:8e0d178b1d1e | 3439 | wolfSSL_CTX_SetEd448SignCb(ctx, myEd448Sign); |
| wolfSSL | 16:8e0d178b1d1e | 3440 | wolfSSL_CTX_SetEd448VerifyCb(ctx, myEd448Verify); |
| wolfSSL | 16:8e0d178b1d1e | 3441 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 3442 | #ifdef HAVE_CURVE448 |
| wolfSSL | 16:8e0d178b1d1e | 3443 | wolfSSL_CTX_SetX448KeyGenCb(ctx, myX448KeyGen); |
| wolfSSL | 16:8e0d178b1d1e | 3444 | wolfSSL_CTX_SetX448SharedSecretCb(ctx, myX448SharedSecret); |
| wolfSSL | 16:8e0d178b1d1e | 3445 | #endif |
| wolfSSL | 15:117db924cf7c | 3446 | #ifndef NO_RSA |
| wolfSSL | 15:117db924cf7c | 3447 | wolfSSL_CTX_SetRsaSignCb(ctx, myRsaSign); |
| wolfSSL | 15:117db924cf7c | 3448 | wolfSSL_CTX_SetRsaVerifyCb(ctx, myRsaVerify); |
| wolfSSL | 15:117db924cf7c | 3449 | wolfSSL_CTX_SetRsaSignCheckCb(ctx, myRsaSignCheck); |
| wolfSSL | 15:117db924cf7c | 3450 | #ifdef WC_RSA_PSS |
| wolfSSL | 15:117db924cf7c | 3451 | wolfSSL_CTX_SetRsaPssSignCb(ctx, myRsaPssSign); |
| wolfSSL | 15:117db924cf7c | 3452 | wolfSSL_CTX_SetRsaPssVerifyCb(ctx, myRsaPssVerify); |
| wolfSSL | 15:117db924cf7c | 3453 | wolfSSL_CTX_SetRsaPssSignCheckCb(ctx, myRsaPssSignCheck); |
| wolfSSL | 15:117db924cf7c | 3454 | #endif |
| wolfSSL | 15:117db924cf7c | 3455 | wolfSSL_CTX_SetRsaEncCb(ctx, myRsaEnc); |
| wolfSSL | 15:117db924cf7c | 3456 | wolfSSL_CTX_SetRsaDecCb(ctx, myRsaDec); |
| wolfSSL | 15:117db924cf7c | 3457 | #endif /* NO_RSA */ |
| wolfSSL | 15:117db924cf7c | 3458 | } |
| wolfSSL | 15:117db924cf7c | 3459 | |
| wolfSSL | 15:117db924cf7c | 3460 | static WC_INLINE void SetupPkCallbackContexts(WOLFSSL* ssl, void* myCtx) |
| wolfSSL | 15:117db924cf7c | 3461 | { |
| wolfSSL | 15:117db924cf7c | 3462 | #ifdef HAVE_ECC |
| wolfSSL | 15:117db924cf7c | 3463 | wolfSSL_SetEccKeyGenCtx(ssl, myCtx); |
| wolfSSL | 15:117db924cf7c | 3464 | wolfSSL_SetEccSignCtx(ssl, myCtx); |
| wolfSSL | 15:117db924cf7c | 3465 | wolfSSL_SetEccVerifyCtx(ssl, myCtx); |
| wolfSSL | 15:117db924cf7c | 3466 | wolfSSL_SetEccSharedSecretCtx(ssl, myCtx); |
| wolfSSL | 15:117db924cf7c | 3467 | #endif /* HAVE_ECC */ |
| wolfSSL | 15:117db924cf7c | 3468 | #ifndef NO_DH |
| wolfSSL | 15:117db924cf7c | 3469 | wolfSSL_SetDhAgreeCtx(ssl, myCtx); |
| wolfSSL | 15:117db924cf7c | 3470 | #endif |
| wolfSSL | 15:117db924cf7c | 3471 | #ifdef HAVE_ED25519 |
| wolfSSL | 15:117db924cf7c | 3472 | wolfSSL_SetEd25519SignCtx(ssl, myCtx); |
| wolfSSL | 15:117db924cf7c | 3473 | wolfSSL_SetEd25519VerifyCtx(ssl, myCtx); |
| wolfSSL | 15:117db924cf7c | 3474 | #endif |
| wolfSSL | 15:117db924cf7c | 3475 | #ifdef HAVE_CURVE25519 |
| wolfSSL | 15:117db924cf7c | 3476 | wolfSSL_SetX25519KeyGenCtx(ssl, myCtx); |
| wolfSSL | 15:117db924cf7c | 3477 | wolfSSL_SetX25519SharedSecretCtx(ssl, myCtx); |
| wolfSSL | 15:117db924cf7c | 3478 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 3479 | #ifdef HAVE_ED448 |
| wolfSSL | 16:8e0d178b1d1e | 3480 | wolfSSL_SetEd448SignCtx(ssl, myCtx); |
| wolfSSL | 16:8e0d178b1d1e | 3481 | wolfSSL_SetEd448VerifyCtx(ssl, myCtx); |
| wolfSSL | 16:8e0d178b1d1e | 3482 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 3483 | #ifdef HAVE_CURVE448 |
| wolfSSL | 16:8e0d178b1d1e | 3484 | wolfSSL_SetX448KeyGenCtx(ssl, myCtx); |
| wolfSSL | 16:8e0d178b1d1e | 3485 | wolfSSL_SetX448SharedSecretCtx(ssl, myCtx); |
| wolfSSL | 16:8e0d178b1d1e | 3486 | #endif |
| wolfSSL | 15:117db924cf7c | 3487 | #ifndef NO_RSA |
| wolfSSL | 15:117db924cf7c | 3488 | wolfSSL_SetRsaSignCtx(ssl, myCtx); |
| wolfSSL | 15:117db924cf7c | 3489 | wolfSSL_SetRsaVerifyCtx(ssl, myCtx); |
| wolfSSL | 15:117db924cf7c | 3490 | #ifdef WC_RSA_PSS |
| wolfSSL | 15:117db924cf7c | 3491 | wolfSSL_SetRsaPssSignCtx(ssl, myCtx); |
| wolfSSL | 15:117db924cf7c | 3492 | wolfSSL_SetRsaPssVerifyCtx(ssl, myCtx); |
| wolfSSL | 15:117db924cf7c | 3493 | #endif |
| wolfSSL | 15:117db924cf7c | 3494 | wolfSSL_SetRsaEncCtx(ssl, myCtx); |
| wolfSSL | 15:117db924cf7c | 3495 | wolfSSL_SetRsaDecCtx(ssl, myCtx); |
| wolfSSL | 15:117db924cf7c | 3496 | #endif /* NO_RSA */ |
| wolfSSL | 15:117db924cf7c | 3497 | } |
| wolfSSL | 15:117db924cf7c | 3498 | |
| wolfSSL | 15:117db924cf7c | 3499 | #endif /* HAVE_PK_CALLBACKS */ |
| wolfSSL | 15:117db924cf7c | 3500 | |
| wolfSSL | 15:117db924cf7c | 3501 | |
| wolfSSL | 15:117db924cf7c | 3502 | |
| wolfSSL | 15:117db924cf7c | 3503 | |
| wolfSSL | 15:117db924cf7c | 3504 | #if defined(__hpux__) || defined(__MINGW32__) || defined (WOLFSSL_TIRTOS) \ |
| wolfSSL | 15:117db924cf7c | 3505 | || defined(_MSC_VER) |
| wolfSSL | 15:117db924cf7c | 3506 | |
| wolfSSL | 15:117db924cf7c | 3507 | /* HP/UX doesn't have strsep, needed by test/suites.c */ |
| wolfSSL | 15:117db924cf7c | 3508 | static WC_INLINE char* strsep(char **stringp, const char *delim) |
| wolfSSL | 15:117db924cf7c | 3509 | { |
| wolfSSL | 15:117db924cf7c | 3510 | char* start; |
| wolfSSL | 15:117db924cf7c | 3511 | char* end; |
| wolfSSL | 15:117db924cf7c | 3512 | |
| wolfSSL | 15:117db924cf7c | 3513 | start = *stringp; |
| wolfSSL | 15:117db924cf7c | 3514 | if (start == NULL) |
| wolfSSL | 15:117db924cf7c | 3515 | return NULL; |
| wolfSSL | 15:117db924cf7c | 3516 | |
| wolfSSL | 15:117db924cf7c | 3517 | if ((end = strpbrk(start, delim))) { |
| wolfSSL | 15:117db924cf7c | 3518 | *end++ = '\0'; |
| wolfSSL | 15:117db924cf7c | 3519 | *stringp = end; |
| wolfSSL | 15:117db924cf7c | 3520 | } else { |
| wolfSSL | 15:117db924cf7c | 3521 | *stringp = NULL; |
| wolfSSL | 15:117db924cf7c | 3522 | } |
| wolfSSL | 15:117db924cf7c | 3523 | |
| wolfSSL | 15:117db924cf7c | 3524 | return start; |
| wolfSSL | 15:117db924cf7c | 3525 | } |
| wolfSSL | 15:117db924cf7c | 3526 | |
| wolfSSL | 15:117db924cf7c | 3527 | #endif /* __hpux__ and others */ |
| wolfSSL | 15:117db924cf7c | 3528 | |
| wolfSSL | 15:117db924cf7c | 3529 | /* Create unique filename, len is length of tempfn name, assuming |
| wolfSSL | 15:117db924cf7c | 3530 | len does not include null terminating character, |
| wolfSSL | 15:117db924cf7c | 3531 | num is number of characters in tempfn name to randomize */ |
| wolfSSL | 15:117db924cf7c | 3532 | static WC_INLINE const char* mymktemp(char *tempfn, int len, int num) |
| wolfSSL | 15:117db924cf7c | 3533 | { |
| wolfSSL | 15:117db924cf7c | 3534 | int x, size; |
| wolfSSL | 15:117db924cf7c | 3535 | static const char alphanum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| wolfSSL | 15:117db924cf7c | 3536 | "abcdefghijklmnopqrstuvwxyz"; |
| wolfSSL | 15:117db924cf7c | 3537 | WC_RNG rng; |
| wolfSSL | 15:117db924cf7c | 3538 | byte out; |
| wolfSSL | 15:117db924cf7c | 3539 | |
| wolfSSL | 15:117db924cf7c | 3540 | if (tempfn == NULL || len < 1 || num < 1 || len <= num) { |
| wolfSSL | 15:117db924cf7c | 3541 | printf("Bad input\n"); |
| wolfSSL | 15:117db924cf7c | 3542 | return NULL; |
| wolfSSL | 15:117db924cf7c | 3543 | } |
| wolfSSL | 15:117db924cf7c | 3544 | |
| wolfSSL | 15:117db924cf7c | 3545 | size = len - 1; |
| wolfSSL | 15:117db924cf7c | 3546 | |
| wolfSSL | 15:117db924cf7c | 3547 | if (wc_InitRng(&rng) != 0) { |
| wolfSSL | 15:117db924cf7c | 3548 | printf("InitRng failed\n"); |
| wolfSSL | 15:117db924cf7c | 3549 | return NULL; |
| wolfSSL | 15:117db924cf7c | 3550 | } |
| wolfSSL | 15:117db924cf7c | 3551 | |
| wolfSSL | 15:117db924cf7c | 3552 | for (x = size; x > size - num; x--) { |
| wolfSSL | 15:117db924cf7c | 3553 | if (wc_RNG_GenerateBlock(&rng,(byte*)&out, sizeof(out)) != 0) { |
| wolfSSL | 15:117db924cf7c | 3554 | printf("RNG_GenerateBlock failed\n"); |
| wolfSSL | 15:117db924cf7c | 3555 | return NULL; |
| wolfSSL | 15:117db924cf7c | 3556 | } |
| wolfSSL | 15:117db924cf7c | 3557 | tempfn[x] = alphanum[out % (sizeof(alphanum) - 1)]; |
| wolfSSL | 15:117db924cf7c | 3558 | } |
| wolfSSL | 15:117db924cf7c | 3559 | tempfn[len] = '\0'; |
| wolfSSL | 15:117db924cf7c | 3560 | |
| wolfSSL | 15:117db924cf7c | 3561 | wc_FreeRng(&rng); |
| wolfSSL | 16:8e0d178b1d1e | 3562 | (void)rng; /* for WC_NO_RNG case */ |
| wolfSSL | 15:117db924cf7c | 3563 | |
| wolfSSL | 15:117db924cf7c | 3564 | return tempfn; |
| wolfSSL | 15:117db924cf7c | 3565 | } |
| wolfSSL | 15:117db924cf7c | 3566 | |
| wolfSSL | 15:117db924cf7c | 3567 | |
| wolfSSL | 15:117db924cf7c | 3568 | |
| wolfSSL | 15:117db924cf7c | 3569 | #if defined(HAVE_SESSION_TICKET) && defined(HAVE_CHACHA) && \ |
| wolfSSL | 15:117db924cf7c | 3570 | defined(HAVE_POLY1305) |
| wolfSSL | 15:117db924cf7c | 3571 | |
| wolfSSL | 15:117db924cf7c | 3572 | #include <wolfssl/wolfcrypt/chacha20_poly1305.h> |
| wolfSSL | 15:117db924cf7c | 3573 | |
| wolfSSL | 15:117db924cf7c | 3574 | typedef struct key_ctx { |
| wolfSSL | 15:117db924cf7c | 3575 | byte name[WOLFSSL_TICKET_NAME_SZ]; /* name for this context */ |
| wolfSSL | 15:117db924cf7c | 3576 | byte key[CHACHA20_POLY1305_AEAD_KEYSIZE]; /* cipher key */ |
| wolfSSL | 15:117db924cf7c | 3577 | } key_ctx; |
| wolfSSL | 15:117db924cf7c | 3578 | |
| wolfSSL | 16:8e0d178b1d1e | 3579 | static THREAD_LS_T key_ctx myKey_ctx; |
| wolfSSL | 16:8e0d178b1d1e | 3580 | static THREAD_LS_T WC_RNG myKey_rng; |
| wolfSSL | 15:117db924cf7c | 3581 | |
| wolfSSL | 15:117db924cf7c | 3582 | static WC_INLINE int TicketInit(void) |
| wolfSSL | 15:117db924cf7c | 3583 | { |
| wolfSSL | 15:117db924cf7c | 3584 | int ret = wc_InitRng(&myKey_rng); |
| wolfSSL | 15:117db924cf7c | 3585 | if (ret != 0) return ret; |
| wolfSSL | 15:117db924cf7c | 3586 | |
| wolfSSL | 15:117db924cf7c | 3587 | ret = wc_RNG_GenerateBlock(&myKey_rng, myKey_ctx.key, sizeof(myKey_ctx.key)); |
| wolfSSL | 15:117db924cf7c | 3588 | if (ret != 0) return ret; |
| wolfSSL | 15:117db924cf7c | 3589 | |
| wolfSSL | 15:117db924cf7c | 3590 | ret = wc_RNG_GenerateBlock(&myKey_rng, myKey_ctx.name,sizeof(myKey_ctx.name)); |
| wolfSSL | 15:117db924cf7c | 3591 | if (ret != 0) return ret; |
| wolfSSL | 15:117db924cf7c | 3592 | |
| wolfSSL | 15:117db924cf7c | 3593 | return 0; |
| wolfSSL | 15:117db924cf7c | 3594 | } |
| wolfSSL | 15:117db924cf7c | 3595 | |
| wolfSSL | 15:117db924cf7c | 3596 | static WC_INLINE void TicketCleanup(void) |
| wolfSSL | 15:117db924cf7c | 3597 | { |
| wolfSSL | 15:117db924cf7c | 3598 | wc_FreeRng(&myKey_rng); |
| wolfSSL | 15:117db924cf7c | 3599 | } |
| wolfSSL | 15:117db924cf7c | 3600 | |
| wolfSSL | 15:117db924cf7c | 3601 | static WC_INLINE int myTicketEncCb(WOLFSSL* ssl, |
| wolfSSL | 15:117db924cf7c | 3602 | byte key_name[WOLFSSL_TICKET_NAME_SZ], |
| wolfSSL | 15:117db924cf7c | 3603 | byte iv[WOLFSSL_TICKET_IV_SZ], |
| wolfSSL | 15:117db924cf7c | 3604 | byte mac[WOLFSSL_TICKET_MAC_SZ], |
| wolfSSL | 15:117db924cf7c | 3605 | int enc, byte* ticket, int inLen, int* outLen, |
| wolfSSL | 15:117db924cf7c | 3606 | void* userCtx) |
| wolfSSL | 15:117db924cf7c | 3607 | { |
| wolfSSL | 15:117db924cf7c | 3608 | (void)ssl; |
| wolfSSL | 15:117db924cf7c | 3609 | (void)userCtx; |
| wolfSSL | 15:117db924cf7c | 3610 | |
| wolfSSL | 15:117db924cf7c | 3611 | int ret; |
| wolfSSL | 15:117db924cf7c | 3612 | word16 sLen = XHTONS(inLen); |
| wolfSSL | 15:117db924cf7c | 3613 | byte aad[WOLFSSL_TICKET_NAME_SZ + WOLFSSL_TICKET_IV_SZ + 2]; |
| wolfSSL | 15:117db924cf7c | 3614 | int aadSz = WOLFSSL_TICKET_NAME_SZ + WOLFSSL_TICKET_IV_SZ + 2; |
| wolfSSL | 15:117db924cf7c | 3615 | byte* tmp = aad; |
| wolfSSL | 15:117db924cf7c | 3616 | |
| wolfSSL | 15:117db924cf7c | 3617 | if (enc) { |
| wolfSSL | 15:117db924cf7c | 3618 | XMEMCPY(key_name, myKey_ctx.name, WOLFSSL_TICKET_NAME_SZ); |
| wolfSSL | 15:117db924cf7c | 3619 | |
| wolfSSL | 15:117db924cf7c | 3620 | ret = wc_RNG_GenerateBlock(&myKey_rng, iv, WOLFSSL_TICKET_IV_SZ); |
| wolfSSL | 15:117db924cf7c | 3621 | if (ret != 0) return WOLFSSL_TICKET_RET_REJECT; |
| wolfSSL | 15:117db924cf7c | 3622 | |
| wolfSSL | 15:117db924cf7c | 3623 | /* build aad from key name, iv, and length */ |
| wolfSSL | 15:117db924cf7c | 3624 | XMEMCPY(tmp, key_name, WOLFSSL_TICKET_NAME_SZ); |
| wolfSSL | 15:117db924cf7c | 3625 | tmp += WOLFSSL_TICKET_NAME_SZ; |
| wolfSSL | 15:117db924cf7c | 3626 | XMEMCPY(tmp, iv, WOLFSSL_TICKET_IV_SZ); |
| wolfSSL | 15:117db924cf7c | 3627 | tmp += WOLFSSL_TICKET_IV_SZ; |
| wolfSSL | 15:117db924cf7c | 3628 | XMEMCPY(tmp, &sLen, 2); |
| wolfSSL | 15:117db924cf7c | 3629 | |
| wolfSSL | 15:117db924cf7c | 3630 | ret = wc_ChaCha20Poly1305_Encrypt(myKey_ctx.key, iv, |
| wolfSSL | 15:117db924cf7c | 3631 | aad, aadSz, |
| wolfSSL | 15:117db924cf7c | 3632 | ticket, inLen, |
| wolfSSL | 15:117db924cf7c | 3633 | ticket, |
| wolfSSL | 15:117db924cf7c | 3634 | mac); |
| wolfSSL | 15:117db924cf7c | 3635 | if (ret != 0) return WOLFSSL_TICKET_RET_REJECT; |
| wolfSSL | 15:117db924cf7c | 3636 | *outLen = inLen; /* no padding in this mode */ |
| wolfSSL | 15:117db924cf7c | 3637 | } else { |
| wolfSSL | 15:117db924cf7c | 3638 | /* decrypt */ |
| wolfSSL | 15:117db924cf7c | 3639 | |
| wolfSSL | 15:117db924cf7c | 3640 | /* see if we know this key */ |
| wolfSSL | 15:117db924cf7c | 3641 | if (XMEMCMP(key_name, myKey_ctx.name, WOLFSSL_TICKET_NAME_SZ) != 0){ |
| wolfSSL | 15:117db924cf7c | 3642 | printf("client presented unknown ticket key name "); |
| wolfSSL | 15:117db924cf7c | 3643 | return WOLFSSL_TICKET_RET_FATAL; |
| wolfSSL | 15:117db924cf7c | 3644 | } |
| wolfSSL | 15:117db924cf7c | 3645 | |
| wolfSSL | 15:117db924cf7c | 3646 | /* build aad from key name, iv, and length */ |
| wolfSSL | 15:117db924cf7c | 3647 | XMEMCPY(tmp, key_name, WOLFSSL_TICKET_NAME_SZ); |
| wolfSSL | 15:117db924cf7c | 3648 | tmp += WOLFSSL_TICKET_NAME_SZ; |
| wolfSSL | 15:117db924cf7c | 3649 | XMEMCPY(tmp, iv, WOLFSSL_TICKET_IV_SZ); |
| wolfSSL | 15:117db924cf7c | 3650 | tmp += WOLFSSL_TICKET_IV_SZ; |
| wolfSSL | 15:117db924cf7c | 3651 | XMEMCPY(tmp, &sLen, 2); |
| wolfSSL | 15:117db924cf7c | 3652 | |
| wolfSSL | 15:117db924cf7c | 3653 | ret = wc_ChaCha20Poly1305_Decrypt(myKey_ctx.key, iv, |
| wolfSSL | 15:117db924cf7c | 3654 | aad, aadSz, |
| wolfSSL | 15:117db924cf7c | 3655 | ticket, inLen, |
| wolfSSL | 15:117db924cf7c | 3656 | mac, |
| wolfSSL | 15:117db924cf7c | 3657 | ticket); |
| wolfSSL | 15:117db924cf7c | 3658 | if (ret != 0) return WOLFSSL_TICKET_RET_REJECT; |
| wolfSSL | 15:117db924cf7c | 3659 | *outLen = inLen; /* no padding in this mode */ |
| wolfSSL | 15:117db924cf7c | 3660 | } |
| wolfSSL | 15:117db924cf7c | 3661 | |
| wolfSSL | 15:117db924cf7c | 3662 | return WOLFSSL_TICKET_RET_OK; |
| wolfSSL | 15:117db924cf7c | 3663 | } |
| wolfSSL | 15:117db924cf7c | 3664 | |
| wolfSSL | 15:117db924cf7c | 3665 | #endif /* HAVE_SESSION_TICKET && CHACHA20 && POLY1305 */ |
| wolfSSL | 15:117db924cf7c | 3666 | |
| wolfSSL | 15:117db924cf7c | 3667 | static WC_INLINE word16 GetRandomPort(void) |
| wolfSSL | 15:117db924cf7c | 3668 | { |
| wolfSSL | 15:117db924cf7c | 3669 | word16 port = 0; |
| wolfSSL | 15:117db924cf7c | 3670 | |
| wolfSSL | 15:117db924cf7c | 3671 | /* Generate random port for testing */ |
| wolfSSL | 15:117db924cf7c | 3672 | WC_RNG rng; |
| wolfSSL | 15:117db924cf7c | 3673 | if (wc_InitRng(&rng) == 0) { |
| wolfSSL | 16:8e0d178b1d1e | 3674 | if (wc_RNG_GenerateBlock(&rng, (byte*)&port, sizeof(port)) == 0) { |
| wolfSSL | 16:8e0d178b1d1e | 3675 | port |= 0xC000; /* Make sure its in the 49152 - 65535 range */ |
| wolfSSL | 16:8e0d178b1d1e | 3676 | } |
| wolfSSL | 15:117db924cf7c | 3677 | wc_FreeRng(&rng); |
| wolfSSL | 15:117db924cf7c | 3678 | } |
| wolfSSL | 16:8e0d178b1d1e | 3679 | (void)rng; /* for WC_NO_RNG case */ |
| wolfSSL | 15:117db924cf7c | 3680 | return port; |
| wolfSSL | 15:117db924cf7c | 3681 | } |
| wolfSSL | 15:117db924cf7c | 3682 | |
| wolfSSL | 15:117db924cf7c | 3683 | #endif /* wolfSSL_TEST_H */ |
| wolfSSL | 15:117db924cf7c | 3684 |