wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Sat Aug 18 22:20:43 2018 +0000
Revision:
15:117db924cf7c
Child:
16:8e0d178b1d1e
wolfSSL 3.15.3

Who changed what in which revision?

UserRevisionLine numberNew 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 15:117db924cf7c 14 #if defined(OPENSSL_EXTRA) && defined(SHOW_CERTS)
wolfSSL 15:117db924cf7c 15 #include <wolfssl/openssl/ssl.h> /* for domain component NID value */
wolfSSL 15:117db924cf7c 16 #endif
wolfSSL 15:117db924cf7c 17
wolfSSL 15:117db924cf7c 18 #ifdef ATOMIC_USER
wolfSSL 15:117db924cf7c 19 #include <wolfssl/wolfcrypt/aes.h>
wolfSSL 15:117db924cf7c 20 #include <wolfssl/wolfcrypt/arc4.h>
wolfSSL 15:117db924cf7c 21 #include <wolfssl/wolfcrypt/hmac.h>
wolfSSL 15:117db924cf7c 22 #endif
wolfSSL 15:117db924cf7c 23 #ifdef HAVE_PK_CALLBACKS
wolfSSL 15:117db924cf7c 24 #include <wolfssl/wolfcrypt/asn.h>
wolfSSL 15:117db924cf7c 25 #ifndef NO_RSA
wolfSSL 15:117db924cf7c 26 #include <wolfssl/wolfcrypt/rsa.h>
wolfSSL 15:117db924cf7c 27 #endif
wolfSSL 15:117db924cf7c 28 #ifdef HAVE_ECC
wolfSSL 15:117db924cf7c 29 #include <wolfssl/wolfcrypt/ecc.h>
wolfSSL 15:117db924cf7c 30 #endif /* HAVE_ECC */
wolfSSL 15:117db924cf7c 31 #ifndef NO_DH
wolfSSL 15:117db924cf7c 32 #include <wolfssl/wolfcrypt/dh.h>
wolfSSL 15:117db924cf7c 33 #endif /* !NO_DH */
wolfSSL 15:117db924cf7c 34 #ifdef HAVE_ED25519
wolfSSL 15:117db924cf7c 35 #include <wolfssl/wolfcrypt/ed25519.h>
wolfSSL 15:117db924cf7c 36 #endif /* HAVE_ED25519 */
wolfSSL 15:117db924cf7c 37 #ifdef HAVE_CURVE25519
wolfSSL 15:117db924cf7c 38 #include <wolfssl/wolfcrypt/curve25519.h>
wolfSSL 15:117db924cf7c 39 #endif /* HAVE_ECC */
wolfSSL 15:117db924cf7c 40 #endif /*HAVE_PK_CALLBACKS */
wolfSSL 15:117db924cf7c 41
wolfSSL 15:117db924cf7c 42 #ifdef USE_WINDOWS_API
wolfSSL 15:117db924cf7c 43 #include <winsock2.h>
wolfSSL 15:117db924cf7c 44 #include <process.h>
wolfSSL 15:117db924cf7c 45 #ifdef TEST_IPV6 /* don't require newer SDK for IPV4 */
wolfSSL 15:117db924cf7c 46 #include <ws2tcpip.h>
wolfSSL 15:117db924cf7c 47 #include <wspiapi.h>
wolfSSL 15:117db924cf7c 48 #endif
wolfSSL 15:117db924cf7c 49 #define SOCKET_T SOCKET
wolfSSL 15:117db924cf7c 50 #define SNPRINTF _snprintf
wolfSSL 15:117db924cf7c 51 #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 15:117db924cf7c 52 #include <string.h>
wolfSSL 15:117db924cf7c 53 #include "rl_net.h"
wolfSSL 15:117db924cf7c 54 #define SOCKET_T int
wolfSSL 15:117db924cf7c 55 typedef int socklen_t ;
wolfSSL 15:117db924cf7c 56 static unsigned long inet_addr(const char *cp)
wolfSSL 15:117db924cf7c 57 {
wolfSSL 15:117db924cf7c 58 unsigned int a[4] ; unsigned long ret ;
wolfSSL 15:117db924cf7c 59 sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) ;
wolfSSL 15:117db924cf7c 60 ret = ((a[3]<<24) + (a[2]<<16) + (a[1]<<8) + a[0]) ;
wolfSSL 15:117db924cf7c 61 return(ret) ;
wolfSSL 15:117db924cf7c 62 }
wolfSSL 15:117db924cf7c 63 #if defined(HAVE_KEIL_RTX)
wolfSSL 15:117db924cf7c 64 #define sleep(t) os_dly_wait(t/1000+1) ;
wolfSSL 15:117db924cf7c 65 #elif defined (WOLFSSL_CMSIS_RTOS)
wolfSSL 15:117db924cf7c 66 #define sleep(t) osDelay(t/1000+1) ;
wolfSSL 15:117db924cf7c 67 #endif
wolfSSL 15:117db924cf7c 68 #elif defined(WOLFSSL_TIRTOS)
wolfSSL 15:117db924cf7c 69 #include <string.h>
wolfSSL 15:117db924cf7c 70 #include <netdb.h>
wolfSSL 15:117db924cf7c 71 #include <sys/types.h>
wolfSSL 15:117db924cf7c 72 #include <arpa/inet.h>
wolfSSL 15:117db924cf7c 73 #include <sys/socket.h>
wolfSSL 15:117db924cf7c 74 #include <ti/sysbios/knl/Task.h>
wolfSSL 15:117db924cf7c 75 struct hostent {
wolfSSL 15:117db924cf7c 76 char *h_name; /* official name of host */
wolfSSL 15:117db924cf7c 77 char **h_aliases; /* alias list */
wolfSSL 15:117db924cf7c 78 int h_addrtype; /* host address type */
wolfSSL 15:117db924cf7c 79 int h_length; /* length of address */
wolfSSL 15:117db924cf7c 80 char **h_addr_list; /* list of addresses from name server */
wolfSSL 15:117db924cf7c 81 };
wolfSSL 15:117db924cf7c 82 #define SOCKET_T int
wolfSSL 15:117db924cf7c 83 #elif defined(WOLFSSL_VXWORKS)
wolfSSL 15:117db924cf7c 84 #include <hostLib.h>
wolfSSL 15:117db924cf7c 85 #include <sockLib.h>
wolfSSL 15:117db924cf7c 86 #include <arpa/inet.h>
wolfSSL 15:117db924cf7c 87 #include <string.h>
wolfSSL 15:117db924cf7c 88 #include <selectLib.h>
wolfSSL 15:117db924cf7c 89 #include <sys/types.h>
wolfSSL 15:117db924cf7c 90 #include <netinet/in.h>
wolfSSL 15:117db924cf7c 91 #include <fcntl.h>
wolfSSL 15:117db924cf7c 92 #include <sys/time.h>
wolfSSL 15:117db924cf7c 93 #include <netdb.h>
wolfSSL 15:117db924cf7c 94 #include <pthread.h>
wolfSSL 15:117db924cf7c 95 #define SOCKET_T int
wolfSSL 15:117db924cf7c 96 #else
wolfSSL 15:117db924cf7c 97 #include <string.h>
wolfSSL 15:117db924cf7c 98 #include <sys/types.h>
wolfSSL 15:117db924cf7c 99 #ifndef WOLFSSL_LEANPSK
wolfSSL 15:117db924cf7c 100 #include <unistd.h>
wolfSSL 15:117db924cf7c 101 #include <netdb.h>
wolfSSL 15:117db924cf7c 102 #include <netinet/in.h>
wolfSSL 15:117db924cf7c 103 #include <netinet/tcp.h>
wolfSSL 15:117db924cf7c 104 #include <arpa/inet.h>
wolfSSL 15:117db924cf7c 105 #include <sys/ioctl.h>
wolfSSL 15:117db924cf7c 106 #include <sys/time.h>
wolfSSL 15:117db924cf7c 107 #include <sys/socket.h>
wolfSSL 15:117db924cf7c 108 #include <pthread.h>
wolfSSL 15:117db924cf7c 109 #include <fcntl.h>
wolfSSL 15:117db924cf7c 110 #ifdef TEST_IPV6
wolfSSL 15:117db924cf7c 111 #include <netdb.h>
wolfSSL 15:117db924cf7c 112 #endif
wolfSSL 15:117db924cf7c 113 #endif
wolfSSL 15:117db924cf7c 114 #define SOCKET_T int
wolfSSL 15:117db924cf7c 115 #ifndef SO_NOSIGPIPE
wolfSSL 15:117db924cf7c 116 #include <signal.h> /* ignore SIGPIPE */
wolfSSL 15:117db924cf7c 117 #endif
wolfSSL 15:117db924cf7c 118 #define SNPRINTF snprintf
wolfSSL 15:117db924cf7c 119 #endif /* USE_WINDOWS_API */
wolfSSL 15:117db924cf7c 120
wolfSSL 15:117db924cf7c 121 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 15:117db924cf7c 122 #include <wolfssl/wolfcrypt/async.h>
wolfSSL 15:117db924cf7c 123 #endif
wolfSSL 15:117db924cf7c 124 #ifdef HAVE_CAVIUM
wolfSSL 15:117db924cf7c 125 #include <wolfssl/wolfcrypt/port/cavium/cavium_nitrox.h>
wolfSSL 15:117db924cf7c 126 #endif
wolfSSL 15:117db924cf7c 127 #ifdef _MSC_VER
wolfSSL 15:117db924cf7c 128 /* disable conversion warning */
wolfSSL 15:117db924cf7c 129 /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
wolfSSL 15:117db924cf7c 130 #pragma warning(disable:4244 4996)
wolfSSL 15:117db924cf7c 131 #endif
wolfSSL 15:117db924cf7c 132
wolfSSL 15:117db924cf7c 133 /* Buffer for benchmark tests */
wolfSSL 15:117db924cf7c 134 #ifndef TEST_BUFFER_SIZE
wolfSSL 15:117db924cf7c 135 #define TEST_BUFFER_SIZE 16384
wolfSSL 15:117db924cf7c 136 #endif
wolfSSL 15:117db924cf7c 137
wolfSSL 15:117db924cf7c 138 #ifndef WOLFSSL_HAVE_MIN
wolfSSL 15:117db924cf7c 139 #define WOLFSSL_HAVE_MIN
wolfSSL 15:117db924cf7c 140 static WC_INLINE word32 min(word32 a, word32 b)
wolfSSL 15:117db924cf7c 141 {
wolfSSL 15:117db924cf7c 142 return a > b ? b : a;
wolfSSL 15:117db924cf7c 143 }
wolfSSL 15:117db924cf7c 144 #endif /* WOLFSSL_HAVE_MIN */
wolfSSL 15:117db924cf7c 145
wolfSSL 15:117db924cf7c 146 /* Socket Handling */
wolfSSL 15:117db924cf7c 147 #ifndef WOLFSSL_SOCKET_INVALID
wolfSSL 15:117db924cf7c 148 #ifdef USE_WINDOWS_API
wolfSSL 15:117db924cf7c 149 #define WOLFSSL_SOCKET_INVALID ((SOCKET_T)INVALID_SOCKET)
wolfSSL 15:117db924cf7c 150 #elif defined(WOLFSSL_TIRTOS)
wolfSSL 15:117db924cf7c 151 #define WOLFSSL_SOCKET_INVALID ((SOCKET_T)-1)
wolfSSL 15:117db924cf7c 152 #else
wolfSSL 15:117db924cf7c 153 #define WOLFSSL_SOCKET_INVALID (SOCKET_T)(0)
wolfSSL 15:117db924cf7c 154 #endif
wolfSSL 15:117db924cf7c 155 #endif /* WOLFSSL_SOCKET_INVALID */
wolfSSL 15:117db924cf7c 156
wolfSSL 15:117db924cf7c 157 #ifndef WOLFSSL_SOCKET_IS_INVALID
wolfSSL 15:117db924cf7c 158 #if defined(USE_WINDOWS_API) || defined(WOLFSSL_TIRTOS)
wolfSSL 15:117db924cf7c 159 #define WOLFSSL_SOCKET_IS_INVALID(s) ((SOCKET_T)(s) == WOLFSSL_SOCKET_INVALID)
wolfSSL 15:117db924cf7c 160 #else
wolfSSL 15:117db924cf7c 161 #define WOLFSSL_SOCKET_IS_INVALID(s) ((SOCKET_T)(s) < WOLFSSL_SOCKET_INVALID)
wolfSSL 15:117db924cf7c 162 #endif
wolfSSL 15:117db924cf7c 163 #endif /* WOLFSSL_SOCKET_IS_INVALID */
wolfSSL 15:117db924cf7c 164
wolfSSL 15:117db924cf7c 165 #if defined(__MACH__) || defined(USE_WINDOWS_API)
wolfSSL 15:117db924cf7c 166 #ifndef _SOCKLEN_T
wolfSSL 15:117db924cf7c 167 typedef int socklen_t;
wolfSSL 15:117db924cf7c 168 #endif
wolfSSL 15:117db924cf7c 169 #endif
wolfSSL 15:117db924cf7c 170
wolfSSL 15:117db924cf7c 171
wolfSSL 15:117db924cf7c 172 /* HPUX doesn't use socklent_t for third parameter to accept, unless
wolfSSL 15:117db924cf7c 173 _XOPEN_SOURCE_EXTENDED is defined */
wolfSSL 15:117db924cf7c 174 #if !defined(__hpux__) && !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_IAR_ARM)\
wolfSSL 15:117db924cf7c 175 && !defined(WOLFSSL_ROWLEY_ARM) && !defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 15:117db924cf7c 176 typedef socklen_t* ACCEPT_THIRD_T;
wolfSSL 15:117db924cf7c 177 #else
wolfSSL 15:117db924cf7c 178 #if defined _XOPEN_SOURCE_EXTENDED
wolfSSL 15:117db924cf7c 179 typedef socklen_t* ACCEPT_THIRD_T;
wolfSSL 15:117db924cf7c 180 #else
wolfSSL 15:117db924cf7c 181 typedef int* ACCEPT_THIRD_T;
wolfSSL 15:117db924cf7c 182 #endif
wolfSSL 15:117db924cf7c 183 #endif
wolfSSL 15:117db924cf7c 184
wolfSSL 15:117db924cf7c 185
wolfSSL 15:117db924cf7c 186
wolfSSL 15:117db924cf7c 187 #ifdef SINGLE_THREADED
wolfSSL 15:117db924cf7c 188 typedef unsigned int THREAD_RETURN;
wolfSSL 15:117db924cf7c 189 typedef void* THREAD_TYPE;
wolfSSL 15:117db924cf7c 190 #define WOLFSSL_THREAD
wolfSSL 15:117db924cf7c 191 #else
wolfSSL 15:117db924cf7c 192 #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
wolfSSL 15:117db924cf7c 193 typedef void* THREAD_RETURN;
wolfSSL 15:117db924cf7c 194 typedef pthread_t THREAD_TYPE;
wolfSSL 15:117db924cf7c 195 #define WOLFSSL_THREAD
wolfSSL 15:117db924cf7c 196 #define INFINITE -1
wolfSSL 15:117db924cf7c 197 #define WAIT_OBJECT_0 0L
wolfSSL 15:117db924cf7c 198 #elif defined(WOLFSSL_MDK_ARM)|| defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 15:117db924cf7c 199 typedef unsigned int THREAD_RETURN;
wolfSSL 15:117db924cf7c 200 typedef int THREAD_TYPE;
wolfSSL 15:117db924cf7c 201 #define WOLFSSL_THREAD
wolfSSL 15:117db924cf7c 202 #elif defined(WOLFSSL_TIRTOS)
wolfSSL 15:117db924cf7c 203 typedef void THREAD_RETURN;
wolfSSL 15:117db924cf7c 204 typedef Task_Handle THREAD_TYPE;
wolfSSL 15:117db924cf7c 205 #define WOLFSSL_THREAD
wolfSSL 15:117db924cf7c 206 #else
wolfSSL 15:117db924cf7c 207 typedef unsigned int THREAD_RETURN;
wolfSSL 15:117db924cf7c 208 typedef intptr_t THREAD_TYPE;
wolfSSL 15:117db924cf7c 209 #define WOLFSSL_THREAD __stdcall
wolfSSL 15:117db924cf7c 210 #endif
wolfSSL 15:117db924cf7c 211 #endif
wolfSSL 15:117db924cf7c 212
wolfSSL 15:117db924cf7c 213
wolfSSL 15:117db924cf7c 214 #ifdef TEST_IPV6
wolfSSL 15:117db924cf7c 215 typedef struct sockaddr_in6 SOCKADDR_IN_T;
wolfSSL 15:117db924cf7c 216 #define AF_INET_V AF_INET6
wolfSSL 15:117db924cf7c 217 #else
wolfSSL 15:117db924cf7c 218 typedef struct sockaddr_in SOCKADDR_IN_T;
wolfSSL 15:117db924cf7c 219 #define AF_INET_V AF_INET
wolfSSL 15:117db924cf7c 220 #endif
wolfSSL 15:117db924cf7c 221
wolfSSL 15:117db924cf7c 222
wolfSSL 15:117db924cf7c 223 #ifndef WOLFSSL_NO_TLS12
wolfSSL 15:117db924cf7c 224 #define SERVER_DEFAULT_VERSION 3
wolfSSL 15:117db924cf7c 225 #else
wolfSSL 15:117db924cf7c 226 #define SERVER_DEFAULT_VERSION 4
wolfSSL 15:117db924cf7c 227 #endif
wolfSSL 15:117db924cf7c 228 #define SERVER_DTLS_DEFAULT_VERSION (-2)
wolfSSL 15:117db924cf7c 229 #define SERVER_INVALID_VERSION (-99)
wolfSSL 15:117db924cf7c 230 #define SERVER_DOWNGRADE_VERSION (-98)
wolfSSL 15:117db924cf7c 231 #ifndef WOLFSSL_NO_TLS12
wolfSSL 15:117db924cf7c 232 #define CLIENT_DEFAULT_VERSION 3
wolfSSL 15:117db924cf7c 233 #else
wolfSSL 15:117db924cf7c 234 #define CLIENT_DEFAULT_VERSION 4
wolfSSL 15:117db924cf7c 235 #endif
wolfSSL 15:117db924cf7c 236 #define CLIENT_DTLS_DEFAULT_VERSION (-2)
wolfSSL 15:117db924cf7c 237 #define CLIENT_INVALID_VERSION (-99)
wolfSSL 15:117db924cf7c 238 #define CLIENT_DOWNGRADE_VERSION (-98)
wolfSSL 15:117db924cf7c 239 #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH)
wolfSSL 15:117db924cf7c 240 #define DEFAULT_MIN_DHKEY_BITS 2048
wolfSSL 15:117db924cf7c 241 #define DEFAULT_MAX_DHKEY_BITS 3072
wolfSSL 15:117db924cf7c 242 #else
wolfSSL 15:117db924cf7c 243 #define DEFAULT_MIN_DHKEY_BITS 1024
wolfSSL 15:117db924cf7c 244 #define DEFAULT_MAX_DHKEY_BITS 2048
wolfSSL 15:117db924cf7c 245 #endif
wolfSSL 15:117db924cf7c 246 #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH)
wolfSSL 15:117db924cf7c 247 #define DEFAULT_MIN_RSAKEY_BITS 2048
wolfSSL 15:117db924cf7c 248 #else
wolfSSL 15:117db924cf7c 249 #define DEFAULT_MIN_RSAKEY_BITS 1024
wolfSSL 15:117db924cf7c 250 #endif
wolfSSL 15:117db924cf7c 251 #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH)
wolfSSL 15:117db924cf7c 252 #define DEFAULT_MIN_ECCKEY_BITS 256
wolfSSL 15:117db924cf7c 253 #else
wolfSSL 15:117db924cf7c 254 #define DEFAULT_MIN_ECCKEY_BITS 224
wolfSSL 15:117db924cf7c 255 #endif
wolfSSL 15:117db924cf7c 256
wolfSSL 15:117db924cf7c 257 /* all certs relative to wolfSSL home directory now */
wolfSSL 15:117db924cf7c 258 #if defined(WOLFSSL_NO_CURRDIR) || defined(WOLFSSL_MDK_SHELL)
wolfSSL 15:117db924cf7c 259 #define caCertFile "certs/ca-cert.pem"
wolfSSL 15:117db924cf7c 260 #define eccCertFile "certs/server-ecc.pem"
wolfSSL 15:117db924cf7c 261 #define eccKeyFile "certs/ecc-key.pem"
wolfSSL 15:117db924cf7c 262 #define eccRsaCertFile "certs/server-ecc-rsa.pem"
wolfSSL 15:117db924cf7c 263 #define svrCertFile "certs/server-cert.pem"
wolfSSL 15:117db924cf7c 264 #define svrKeyFile "certs/server-key.pem"
wolfSSL 15:117db924cf7c 265 #define cliCertFile "certs/client-cert.pem"
wolfSSL 15:117db924cf7c 266 #define cliCertDerFile "certs/client-cert.der"
wolfSSL 15:117db924cf7c 267 #define cliKeyFile "certs/client-key.pem"
wolfSSL 15:117db924cf7c 268 #define ntruCertFile "certs/ntru-cert.pem"
wolfSSL 15:117db924cf7c 269 #define ntruKeyFile "certs/ntru-key.raw"
wolfSSL 15:117db924cf7c 270 #define dhParamFile "certs/dh2048.pem"
wolfSSL 15:117db924cf7c 271 #define cliEccKeyFile "certs/ecc-client-key.pem"
wolfSSL 15:117db924cf7c 272 #define cliEccCertFile "certs/client-ecc-cert.pem"
wolfSSL 15:117db924cf7c 273 #define caEccCertFile "certs/ca-ecc-cert/pem"
wolfSSL 15:117db924cf7c 274 #define crlPemDir "certs/crl"
wolfSSL 15:117db924cf7c 275 #define edCertFile "certs/ed25519/server-ed25519-cert.pem"
wolfSSL 15:117db924cf7c 276 #define edKeyFile "certs/ed25519/server-ed25519-priv.pem"
wolfSSL 15:117db924cf7c 277 #define cliEdCertFile "certs/ed25519/client-ed25519.pem"
wolfSSL 15:117db924cf7c 278 #define cliEdKeyFile "certs/ed25519/client-ed25519-priv.pem"
wolfSSL 15:117db924cf7c 279 #define caEdCertFile "certs/ed25519/ca-ed25519.pem"
wolfSSL 15:117db924cf7c 280 #ifdef HAVE_WNR
wolfSSL 15:117db924cf7c 281 /* Whitewood netRandom default config file */
wolfSSL 15:117db924cf7c 282 #define wnrConfig "wnr-example.conf"
wolfSSL 15:117db924cf7c 283 #endif
wolfSSL 15:117db924cf7c 284 #else
wolfSSL 15:117db924cf7c 285 #define caCertFile "./certs/ca-cert.pem"
wolfSSL 15:117db924cf7c 286 #define eccCertFile "./certs/server-ecc.pem"
wolfSSL 15:117db924cf7c 287 #define eccKeyFile "./certs/ecc-key.pem"
wolfSSL 15:117db924cf7c 288 #define eccRsaCertFile "./certs/server-ecc-rsa.pem"
wolfSSL 15:117db924cf7c 289 #define svrCertFile "./certs/server-cert.pem"
wolfSSL 15:117db924cf7c 290 #define svrKeyFile "./certs/server-key.pem"
wolfSSL 15:117db924cf7c 291 #define cliCertFile "./certs/client-cert.pem"
wolfSSL 15:117db924cf7c 292 #define cliCertDerFile "./certs/client-cert.der"
wolfSSL 15:117db924cf7c 293 #define cliKeyFile "./certs/client-key.pem"
wolfSSL 15:117db924cf7c 294 #define ntruCertFile "./certs/ntru-cert.pem"
wolfSSL 15:117db924cf7c 295 #define ntruKeyFile "./certs/ntru-key.raw"
wolfSSL 15:117db924cf7c 296 #define dhParamFile "./certs/dh2048.pem"
wolfSSL 15:117db924cf7c 297 #define cliEccKeyFile "./certs/ecc-client-key.pem"
wolfSSL 15:117db924cf7c 298 #define cliEccCertFile "./certs/client-ecc-cert.pem"
wolfSSL 15:117db924cf7c 299 #define caEccCertFile "./certs/ca-ecc-cert.pem"
wolfSSL 15:117db924cf7c 300 #define crlPemDir "./certs/crl"
wolfSSL 15:117db924cf7c 301 #define edCertFile "./certs/ed25519/server-ed25519.pem"
wolfSSL 15:117db924cf7c 302 #define edKeyFile "./certs/ed25519/server-ed25519-priv.pem"
wolfSSL 15:117db924cf7c 303 #define cliEdCertFile "./certs/ed25519/client-ed25519.pem"
wolfSSL 15:117db924cf7c 304 #define cliEdKeyFile "./certs/ed25519/client-ed25519-priv.pem"
wolfSSL 15:117db924cf7c 305 #define caEdCertFile "./certs/ed25519/root-ed25519.pem"
wolfSSL 15:117db924cf7c 306 #ifdef HAVE_WNR
wolfSSL 15:117db924cf7c 307 /* Whitewood netRandom default config file */
wolfSSL 15:117db924cf7c 308 #define wnrConfig "./wnr-example.conf"
wolfSSL 15:117db924cf7c 309 #endif
wolfSSL 15:117db924cf7c 310 #endif
wolfSSL 15:117db924cf7c 311
wolfSSL 15:117db924cf7c 312 typedef struct tcp_ready {
wolfSSL 15:117db924cf7c 313 word16 ready; /* predicate */
wolfSSL 15:117db924cf7c 314 word16 port;
wolfSSL 15:117db924cf7c 315 char* srfName; /* server ready file name */
wolfSSL 15:117db924cf7c 316 #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
wolfSSL 15:117db924cf7c 317 pthread_mutex_t mutex;
wolfSSL 15:117db924cf7c 318 pthread_cond_t cond;
wolfSSL 15:117db924cf7c 319 #endif
wolfSSL 15:117db924cf7c 320 } tcp_ready;
wolfSSL 15:117db924cf7c 321
wolfSSL 15:117db924cf7c 322
wolfSSL 15:117db924cf7c 323 static WC_INLINE void InitTcpReady(tcp_ready* ready)
wolfSSL 15:117db924cf7c 324 {
wolfSSL 15:117db924cf7c 325 ready->ready = 0;
wolfSSL 15:117db924cf7c 326 ready->port = 0;
wolfSSL 15:117db924cf7c 327 ready->srfName = NULL;
wolfSSL 15:117db924cf7c 328 #ifdef SINGLE_THREADED
wolfSSL 15:117db924cf7c 329 #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
wolfSSL 15:117db924cf7c 330 pthread_mutex_init(&ready->mutex, 0);
wolfSSL 15:117db924cf7c 331 pthread_cond_init(&ready->cond, 0);
wolfSSL 15:117db924cf7c 332 #endif
wolfSSL 15:117db924cf7c 333 }
wolfSSL 15:117db924cf7c 334
wolfSSL 15:117db924cf7c 335
wolfSSL 15:117db924cf7c 336 static WC_INLINE void FreeTcpReady(tcp_ready* ready)
wolfSSL 15:117db924cf7c 337 {
wolfSSL 15:117db924cf7c 338 #ifdef SINGLE_THREADED
wolfSSL 15:117db924cf7c 339 (void)ready;
wolfSSL 15:117db924cf7c 340 #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
wolfSSL 15:117db924cf7c 341 pthread_mutex_destroy(&ready->mutex);
wolfSSL 15:117db924cf7c 342 pthread_cond_destroy(&ready->cond);
wolfSSL 15:117db924cf7c 343 #else
wolfSSL 15:117db924cf7c 344 (void)ready;
wolfSSL 15:117db924cf7c 345 #endif
wolfSSL 15:117db924cf7c 346 }
wolfSSL 15:117db924cf7c 347
wolfSSL 15:117db924cf7c 348 typedef WOLFSSL_METHOD* (*method_provider)(void);
wolfSSL 15:117db924cf7c 349 typedef void (*ctx_callback)(WOLFSSL_CTX* ctx);
wolfSSL 15:117db924cf7c 350 typedef void (*ssl_callback)(WOLFSSL* ssl);
wolfSSL 15:117db924cf7c 351
wolfSSL 15:117db924cf7c 352 typedef struct callback_functions {
wolfSSL 15:117db924cf7c 353 method_provider method;
wolfSSL 15:117db924cf7c 354 ctx_callback ctx_ready;
wolfSSL 15:117db924cf7c 355 ssl_callback ssl_ready;
wolfSSL 15:117db924cf7c 356 ssl_callback on_result;
wolfSSL 15:117db924cf7c 357 } callback_functions;
wolfSSL 15:117db924cf7c 358
wolfSSL 15:117db924cf7c 359 typedef struct func_args {
wolfSSL 15:117db924cf7c 360 int argc;
wolfSSL 15:117db924cf7c 361 char** argv;
wolfSSL 15:117db924cf7c 362 int return_code;
wolfSSL 15:117db924cf7c 363 tcp_ready* signal;
wolfSSL 15:117db924cf7c 364 callback_functions *callbacks;
wolfSSL 15:117db924cf7c 365 } func_args;
wolfSSL 15:117db924cf7c 366
wolfSSL 15:117db924cf7c 367
wolfSSL 15:117db924cf7c 368
wolfSSL 15:117db924cf7c 369
wolfSSL 15:117db924cf7c 370 void wait_tcp_ready(func_args*);
wolfSSL 15:117db924cf7c 371
wolfSSL 15:117db924cf7c 372 typedef THREAD_RETURN WOLFSSL_THREAD THREAD_FUNC(void*);
wolfSSL 15:117db924cf7c 373
wolfSSL 15:117db924cf7c 374 void start_thread(THREAD_FUNC, func_args*, THREAD_TYPE*);
wolfSSL 15:117db924cf7c 375 void join_thread(THREAD_TYPE);
wolfSSL 15:117db924cf7c 376
wolfSSL 15:117db924cf7c 377 /* wolfSSL */
wolfSSL 15:117db924cf7c 378 #ifndef TEST_IPV6
wolfSSL 15:117db924cf7c 379 static const char* const wolfSSLIP = "127.0.0.1";
wolfSSL 15:117db924cf7c 380 #else
wolfSSL 15:117db924cf7c 381 static const char* const wolfSSLIP = "::1";
wolfSSL 15:117db924cf7c 382 #endif
wolfSSL 15:117db924cf7c 383 static const word16 wolfSSLPort = 11111;
wolfSSL 15:117db924cf7c 384
wolfSSL 15:117db924cf7c 385
wolfSSL 15:117db924cf7c 386 static WC_INLINE WC_NORETURN void err_sys(const char* msg)
wolfSSL 15:117db924cf7c 387 {
wolfSSL 15:117db924cf7c 388 printf("wolfSSL error: %s\n", msg);
wolfSSL 15:117db924cf7c 389
wolfSSL 15:117db924cf7c 390 #if !defined(__GNUC__)
wolfSSL 15:117db924cf7c 391 /* scan-build (which pretends to be gnuc) can get confused and think the
wolfSSL 15:117db924cf7c 392 * msg pointer can be null even when hardcoded and then it won't exit,
wolfSSL 15:117db924cf7c 393 * making null pointer checks above the err_sys() call useless.
wolfSSL 15:117db924cf7c 394 * We could just always exit() but some compilers will complain about no
wolfSSL 15:117db924cf7c 395 * possible return, with gcc we know the attribute to handle that with
wolfSSL 15:117db924cf7c 396 * WC_NORETURN. */
wolfSSL 15:117db924cf7c 397 if (msg)
wolfSSL 15:117db924cf7c 398 #endif
wolfSSL 15:117db924cf7c 399 {
wolfSSL 15:117db924cf7c 400 exit(EXIT_FAILURE);
wolfSSL 15:117db924cf7c 401 }
wolfSSL 15:117db924cf7c 402 }
wolfSSL 15:117db924cf7c 403
wolfSSL 15:117db924cf7c 404
wolfSSL 15:117db924cf7c 405 #define MY_EX_USAGE 2
wolfSSL 15:117db924cf7c 406
wolfSSL 15:117db924cf7c 407 extern int myoptind;
wolfSSL 15:117db924cf7c 408 extern char* myoptarg;
wolfSSL 15:117db924cf7c 409
wolfSSL 15:117db924cf7c 410 static WC_INLINE int mygetopt(int argc, char** argv, const char* optstring)
wolfSSL 15:117db924cf7c 411 {
wolfSSL 15:117db924cf7c 412 static char* next = NULL;
wolfSSL 15:117db924cf7c 413
wolfSSL 15:117db924cf7c 414 char c;
wolfSSL 15:117db924cf7c 415 char* cp;
wolfSSL 15:117db924cf7c 416
wolfSSL 15:117db924cf7c 417 if (myoptind == 0)
wolfSSL 15:117db924cf7c 418 next = NULL; /* we're starting new/over */
wolfSSL 15:117db924cf7c 419
wolfSSL 15:117db924cf7c 420 if (next == NULL || *next == '\0') {
wolfSSL 15:117db924cf7c 421 if (myoptind == 0)
wolfSSL 15:117db924cf7c 422 myoptind++;
wolfSSL 15:117db924cf7c 423
wolfSSL 15:117db924cf7c 424 if (myoptind >= argc || argv[myoptind][0] != '-' ||
wolfSSL 15:117db924cf7c 425 argv[myoptind][1] == '\0') {
wolfSSL 15:117db924cf7c 426 myoptarg = NULL;
wolfSSL 15:117db924cf7c 427 if (myoptind < argc)
wolfSSL 15:117db924cf7c 428 myoptarg = argv[myoptind];
wolfSSL 15:117db924cf7c 429
wolfSSL 15:117db924cf7c 430 return -1;
wolfSSL 15:117db924cf7c 431 }
wolfSSL 15:117db924cf7c 432
wolfSSL 15:117db924cf7c 433 if (strcmp(argv[myoptind], "--") == 0) {
wolfSSL 15:117db924cf7c 434 myoptind++;
wolfSSL 15:117db924cf7c 435 myoptarg = NULL;
wolfSSL 15:117db924cf7c 436
wolfSSL 15:117db924cf7c 437 if (myoptind < argc)
wolfSSL 15:117db924cf7c 438 myoptarg = argv[myoptind];
wolfSSL 15:117db924cf7c 439
wolfSSL 15:117db924cf7c 440 return -1;
wolfSSL 15:117db924cf7c 441 }
wolfSSL 15:117db924cf7c 442
wolfSSL 15:117db924cf7c 443 next = argv[myoptind];
wolfSSL 15:117db924cf7c 444 next++; /* skip - */
wolfSSL 15:117db924cf7c 445 myoptind++;
wolfSSL 15:117db924cf7c 446 }
wolfSSL 15:117db924cf7c 447
wolfSSL 15:117db924cf7c 448 c = *next++;
wolfSSL 15:117db924cf7c 449 /* The C++ strchr can return a different value */
wolfSSL 15:117db924cf7c 450 cp = (char*)strchr(optstring, c);
wolfSSL 15:117db924cf7c 451
wolfSSL 15:117db924cf7c 452 if (cp == NULL || c == ':')
wolfSSL 15:117db924cf7c 453 return '?';
wolfSSL 15:117db924cf7c 454
wolfSSL 15:117db924cf7c 455 cp++;
wolfSSL 15:117db924cf7c 456
wolfSSL 15:117db924cf7c 457 if (*cp == ':') {
wolfSSL 15:117db924cf7c 458 if (*next != '\0') {
wolfSSL 15:117db924cf7c 459 myoptarg = next;
wolfSSL 15:117db924cf7c 460 next = NULL;
wolfSSL 15:117db924cf7c 461 }
wolfSSL 15:117db924cf7c 462 else if (myoptind < argc) {
wolfSSL 15:117db924cf7c 463 myoptarg = argv[myoptind];
wolfSSL 15:117db924cf7c 464 myoptind++;
wolfSSL 15:117db924cf7c 465 }
wolfSSL 15:117db924cf7c 466 else
wolfSSL 15:117db924cf7c 467 return '?';
wolfSSL 15:117db924cf7c 468 }
wolfSSL 15:117db924cf7c 469
wolfSSL 15:117db924cf7c 470 return c;
wolfSSL 15:117db924cf7c 471 }
wolfSSL 15:117db924cf7c 472
wolfSSL 15:117db924cf7c 473
wolfSSL 15:117db924cf7c 474 #ifdef WOLFSSL_ENCRYPTED_KEYS
wolfSSL 15:117db924cf7c 475
wolfSSL 15:117db924cf7c 476 static WC_INLINE int PasswordCallBack(char* passwd, int sz, int rw, void* userdata)
wolfSSL 15:117db924cf7c 477 {
wolfSSL 15:117db924cf7c 478 (void)rw;
wolfSSL 15:117db924cf7c 479 (void)userdata;
wolfSSL 15:117db924cf7c 480 if (userdata != NULL) {
wolfSSL 15:117db924cf7c 481 strncpy(passwd, (char*)userdata, sz);
wolfSSL 15:117db924cf7c 482 return (int)XSTRLEN((char*)userdata);
wolfSSL 15:117db924cf7c 483 }
wolfSSL 15:117db924cf7c 484 else {
wolfSSL 15:117db924cf7c 485 strncpy(passwd, "yassl123", sz);
wolfSSL 15:117db924cf7c 486 return 8;
wolfSSL 15:117db924cf7c 487 }
wolfSSL 15:117db924cf7c 488 }
wolfSSL 15:117db924cf7c 489
wolfSSL 15:117db924cf7c 490 #endif
wolfSSL 15:117db924cf7c 491
wolfSSL 15:117db924cf7c 492
wolfSSL 15:117db924cf7c 493 #if defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)
wolfSSL 15:117db924cf7c 494
wolfSSL 15:117db924cf7c 495 static WC_INLINE void ShowX509(WOLFSSL_X509* x509, const char* hdr)
wolfSSL 15:117db924cf7c 496 {
wolfSSL 15:117db924cf7c 497 char* altName;
wolfSSL 15:117db924cf7c 498 char* issuer;
wolfSSL 15:117db924cf7c 499 char* subject;
wolfSSL 15:117db924cf7c 500 byte serial[32];
wolfSSL 15:117db924cf7c 501 int ret;
wolfSSL 15:117db924cf7c 502 int sz = sizeof(serial);
wolfSSL 15:117db924cf7c 503
wolfSSL 15:117db924cf7c 504 if (x509 == NULL) {
wolfSSL 15:117db924cf7c 505 printf("%s No Cert\n", hdr);
wolfSSL 15:117db924cf7c 506 return;
wolfSSL 15:117db924cf7c 507 }
wolfSSL 15:117db924cf7c 508
wolfSSL 15:117db924cf7c 509 issuer = wolfSSL_X509_NAME_oneline(
wolfSSL 15:117db924cf7c 510 wolfSSL_X509_get_issuer_name(x509), 0, 0);
wolfSSL 15:117db924cf7c 511 subject = wolfSSL_X509_NAME_oneline(
wolfSSL 15:117db924cf7c 512 wolfSSL_X509_get_subject_name(x509), 0, 0);
wolfSSL 15:117db924cf7c 513
wolfSSL 15:117db924cf7c 514 printf("%s\n issuer : %s\n subject: %s\n", hdr, issuer, subject);
wolfSSL 15:117db924cf7c 515
wolfSSL 15:117db924cf7c 516 while ( (altName = wolfSSL_X509_get_next_altname(x509)) != NULL)
wolfSSL 15:117db924cf7c 517 printf(" altname = %s\n", altName);
wolfSSL 15:117db924cf7c 518
wolfSSL 15:117db924cf7c 519 ret = wolfSSL_X509_get_serial_number(x509, serial, &sz);
wolfSSL 15:117db924cf7c 520 if (ret == WOLFSSL_SUCCESS) {
wolfSSL 15:117db924cf7c 521 int i;
wolfSSL 15:117db924cf7c 522 int strLen;
wolfSSL 15:117db924cf7c 523 char serialMsg[80];
wolfSSL 15:117db924cf7c 524
wolfSSL 15:117db924cf7c 525 /* testsuite has multiple threads writing to stdout, get output
wolfSSL 15:117db924cf7c 526 message ready to write once */
wolfSSL 15:117db924cf7c 527 strLen = sprintf(serialMsg, " serial number");
wolfSSL 15:117db924cf7c 528 for (i = 0; i < sz; i++)
wolfSSL 15:117db924cf7c 529 sprintf(serialMsg + strLen + (i*3), ":%02x ", serial[i]);
wolfSSL 15:117db924cf7c 530 printf("%s\n", serialMsg);
wolfSSL 15:117db924cf7c 531 }
wolfSSL 15:117db924cf7c 532
wolfSSL 15:117db924cf7c 533 XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
wolfSSL 15:117db924cf7c 534 XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL);
wolfSSL 15:117db924cf7c 535
wolfSSL 15:117db924cf7c 536 #if defined(OPENSSL_EXTRA) && defined(SHOW_CERTS)
wolfSSL 15:117db924cf7c 537 {
wolfSSL 15:117db924cf7c 538 WOLFSSL_BIO* bio;
wolfSSL 15:117db924cf7c 539 char buf[256]; /* should be size of ASN_NAME_MAX */
wolfSSL 15:117db924cf7c 540 int textSz;
wolfSSL 15:117db924cf7c 541
wolfSSL 15:117db924cf7c 542
wolfSSL 15:117db924cf7c 543 /* print out domain component if certificate has it */
wolfSSL 15:117db924cf7c 544 textSz = wolfSSL_X509_NAME_get_text_by_NID(
wolfSSL 15:117db924cf7c 545 wolfSSL_X509_get_subject_name(x509), NID_domainComponent,
wolfSSL 15:117db924cf7c 546 buf, sizeof(buf));
wolfSSL 15:117db924cf7c 547 if (textSz > 0) {
wolfSSL 15:117db924cf7c 548 printf("Domain Component = %s\n", buf);
wolfSSL 15:117db924cf7c 549 }
wolfSSL 15:117db924cf7c 550
wolfSSL 15:117db924cf7c 551 bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
wolfSSL 15:117db924cf7c 552 if (bio != NULL) {
wolfSSL 15:117db924cf7c 553 wolfSSL_BIO_set_fp(bio, stdout, BIO_NOCLOSE);
wolfSSL 15:117db924cf7c 554 wolfSSL_X509_print(bio, x509);
wolfSSL 15:117db924cf7c 555 wolfSSL_BIO_free(bio);
wolfSSL 15:117db924cf7c 556 }
wolfSSL 15:117db924cf7c 557 }
wolfSSL 15:117db924cf7c 558 #endif
wolfSSL 15:117db924cf7c 559 }
wolfSSL 15:117db924cf7c 560
wolfSSL 15:117db924cf7c 561 #endif /* KEEP_PEER_CERT || SESSION_CERTS */
wolfSSL 15:117db924cf7c 562
wolfSSL 15:117db924cf7c 563 #if defined(SESSION_CERTS) && defined(SHOW_CERTS)
wolfSSL 15:117db924cf7c 564 static WC_INLINE void ShowX509Chain(WOLFSSL_X509_CHAIN* chain, int count,
wolfSSL 15:117db924cf7c 565 const char* hdr)
wolfSSL 15:117db924cf7c 566 {
wolfSSL 15:117db924cf7c 567 int i;
wolfSSL 15:117db924cf7c 568 int length;
wolfSSL 15:117db924cf7c 569 unsigned char buffer[3072];
wolfSSL 15:117db924cf7c 570 WOLFSSL_X509* chainX509;
wolfSSL 15:117db924cf7c 571
wolfSSL 15:117db924cf7c 572 for (i = 0; i < count; i++) {
wolfSSL 15:117db924cf7c 573 wolfSSL_get_chain_cert_pem(chain, i, buffer, sizeof(buffer), &length);
wolfSSL 15:117db924cf7c 574 buffer[length] = 0;
wolfSSL 15:117db924cf7c 575 printf("\n%s: %d has length %d data = \n%s\n", hdr, i, length, buffer);
wolfSSL 15:117db924cf7c 576
wolfSSL 15:117db924cf7c 577 chainX509 = wolfSSL_get_chain_X509(chain, i);
wolfSSL 15:117db924cf7c 578 if (chainX509)
wolfSSL 15:117db924cf7c 579 ShowX509(chainX509, hdr);
wolfSSL 15:117db924cf7c 580 else
wolfSSL 15:117db924cf7c 581 printf("get_chain_X509 failed\n");
wolfSSL 15:117db924cf7c 582 wolfSSL_FreeX509(chainX509);
wolfSSL 15:117db924cf7c 583 }
wolfSSL 15:117db924cf7c 584 }
wolfSSL 15:117db924cf7c 585 #endif
wolfSSL 15:117db924cf7c 586
wolfSSL 15:117db924cf7c 587 static WC_INLINE void showPeer(WOLFSSL* ssl)
wolfSSL 15:117db924cf7c 588 {
wolfSSL 15:117db924cf7c 589 WOLFSSL_CIPHER* cipher;
wolfSSL 15:117db924cf7c 590 #ifdef HAVE_ECC
wolfSSL 15:117db924cf7c 591 const char *name;
wolfSSL 15:117db924cf7c 592 #endif
wolfSSL 15:117db924cf7c 593 #ifndef NO_DH
wolfSSL 15:117db924cf7c 594 int bits;
wolfSSL 15:117db924cf7c 595 #endif
wolfSSL 15:117db924cf7c 596 #ifdef KEEP_PEER_CERT
wolfSSL 15:117db924cf7c 597 WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
wolfSSL 15:117db924cf7c 598 if (peer)
wolfSSL 15:117db924cf7c 599 ShowX509(peer, "peer's cert info:");
wolfSSL 15:117db924cf7c 600 else
wolfSSL 15:117db924cf7c 601 printf("peer has no cert!\n");
wolfSSL 15:117db924cf7c 602 wolfSSL_FreeX509(peer);
wolfSSL 15:117db924cf7c 603 #endif
wolfSSL 15:117db924cf7c 604 #if defined(SHOW_CERTS) && defined(OPENSSL_EXTRA) && defined(KEEP_OUR_CERT)
wolfSSL 15:117db924cf7c 605 ShowX509(wolfSSL_get_certificate(ssl), "our cert info:");
wolfSSL 15:117db924cf7c 606 printf("Peer verify result = %lu\n", wolfSSL_get_verify_result(ssl));
wolfSSL 15:117db924cf7c 607 #endif /* SHOW_CERTS */
wolfSSL 15:117db924cf7c 608 printf("SSL version is %s\n", wolfSSL_get_version(ssl));
wolfSSL 15:117db924cf7c 609
wolfSSL 15:117db924cf7c 610 cipher = wolfSSL_get_current_cipher(ssl);
wolfSSL 15:117db924cf7c 611 #ifdef HAVE_QSH
wolfSSL 15:117db924cf7c 612 printf("SSL cipher suite is %s%s\n", (wolfSSL_isQSH(ssl))? "QSH:": "",
wolfSSL 15:117db924cf7c 613 wolfSSL_CIPHER_get_name(cipher));
wolfSSL 15:117db924cf7c 614 #else
wolfSSL 15:117db924cf7c 615 printf("SSL cipher suite is %s\n", wolfSSL_CIPHER_get_name(cipher));
wolfSSL 15:117db924cf7c 616 #endif
wolfSSL 15:117db924cf7c 617 #ifdef HAVE_ECC
wolfSSL 15:117db924cf7c 618 if ((name = wolfSSL_get_curve_name(ssl)) != NULL)
wolfSSL 15:117db924cf7c 619 printf("SSL curve name is %s\n", name);
wolfSSL 15:117db924cf7c 620 #endif
wolfSSL 15:117db924cf7c 621 #ifndef NO_DH
wolfSSL 15:117db924cf7c 622 if ((bits = wolfSSL_GetDhKey_Sz(ssl)) > 0)
wolfSSL 15:117db924cf7c 623 printf("SSL DH size is %d bits\n", bits);
wolfSSL 15:117db924cf7c 624 #endif
wolfSSL 15:117db924cf7c 625 if (wolfSSL_session_reused(ssl))
wolfSSL 15:117db924cf7c 626 printf("SSL reused session\n");
wolfSSL 15:117db924cf7c 627 #ifdef WOLFSSL_ALT_CERT_CHAINS
wolfSSL 15:117db924cf7c 628 if (wolfSSL_is_peer_alt_cert_chain(ssl))
wolfSSL 15:117db924cf7c 629 printf("Alternate cert chain used\n");
wolfSSL 15:117db924cf7c 630 #endif
wolfSSL 15:117db924cf7c 631
wolfSSL 15:117db924cf7c 632 #if defined(SESSION_CERTS) && defined(SHOW_CERTS)
wolfSSL 15:117db924cf7c 633 {
wolfSSL 15:117db924cf7c 634 WOLFSSL_X509_CHAIN* chain;
wolfSSL 15:117db924cf7c 635
wolfSSL 15:117db924cf7c 636 chain = wolfSSL_get_peer_chain(ssl);
wolfSSL 15:117db924cf7c 637 ShowX509Chain(chain, wolfSSL_get_chain_count(chain), "session cert");
wolfSSL 15:117db924cf7c 638
wolfSSL 15:117db924cf7c 639 #ifdef WOLFSSL_ALT_CERT_CHAINS
wolfSSL 15:117db924cf7c 640 if (wolfSSL_is_peer_alt_cert_chain(ssl)) {
wolfSSL 15:117db924cf7c 641 chain = wolfSSL_get_peer_alt_chain(ssl);
wolfSSL 15:117db924cf7c 642 ShowX509Chain(chain, wolfSSL_get_chain_count(chain), "alt cert");
wolfSSL 15:117db924cf7c 643 }
wolfSSL 15:117db924cf7c 644 #endif
wolfSSL 15:117db924cf7c 645 }
wolfSSL 15:117db924cf7c 646 #endif /* SESSION_CERTS && SHOW_CERTS */
wolfSSL 15:117db924cf7c 647 (void)ssl;
wolfSSL 15:117db924cf7c 648 }
wolfSSL 15:117db924cf7c 649
wolfSSL 15:117db924cf7c 650
wolfSSL 15:117db924cf7c 651 static WC_INLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer,
wolfSSL 15:117db924cf7c 652 word16 port, int udp, int sctp)
wolfSSL 15:117db924cf7c 653 {
wolfSSL 15:117db924cf7c 654 int useLookup = 0;
wolfSSL 15:117db924cf7c 655 (void)useLookup;
wolfSSL 15:117db924cf7c 656 (void)udp;
wolfSSL 15:117db924cf7c 657 (void)sctp;
wolfSSL 15:117db924cf7c 658
wolfSSL 15:117db924cf7c 659 if (addr == NULL)
wolfSSL 15:117db924cf7c 660 err_sys("invalid argument to build_addr, addr is NULL");
wolfSSL 15:117db924cf7c 661
wolfSSL 15:117db924cf7c 662 XMEMSET(addr, 0, sizeof(SOCKADDR_IN_T));
wolfSSL 15:117db924cf7c 663
wolfSSL 15:117db924cf7c 664 #ifndef TEST_IPV6
wolfSSL 15:117db924cf7c 665 /* peer could be in human readable form */
wolfSSL 15:117db924cf7c 666 if ( ((size_t)peer != INADDR_ANY) && isalpha((int)peer[0])) {
wolfSSL 15:117db924cf7c 667 #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 15:117db924cf7c 668 int err;
wolfSSL 15:117db924cf7c 669 struct hostent* entry = gethostbyname(peer, &err);
wolfSSL 15:117db924cf7c 670 #elif defined(WOLFSSL_TIRTOS)
wolfSSL 15:117db924cf7c 671 struct hostent* entry = DNSGetHostByName(peer);
wolfSSL 15:117db924cf7c 672 #elif defined(WOLFSSL_VXWORKS)
wolfSSL 15:117db924cf7c 673 struct hostent* entry = (struct hostent*)hostGetByName((char*)peer);
wolfSSL 15:117db924cf7c 674 #else
wolfSSL 15:117db924cf7c 675 struct hostent* entry = gethostbyname(peer);
wolfSSL 15:117db924cf7c 676 #endif
wolfSSL 15:117db924cf7c 677
wolfSSL 15:117db924cf7c 678 if (entry) {
wolfSSL 15:117db924cf7c 679 XMEMCPY(&addr->sin_addr.s_addr, entry->h_addr_list[0],
wolfSSL 15:117db924cf7c 680 entry->h_length);
wolfSSL 15:117db924cf7c 681 useLookup = 1;
wolfSSL 15:117db924cf7c 682 }
wolfSSL 15:117db924cf7c 683 else
wolfSSL 15:117db924cf7c 684 err_sys("no entry for host");
wolfSSL 15:117db924cf7c 685 }
wolfSSL 15:117db924cf7c 686 #endif
wolfSSL 15:117db924cf7c 687
wolfSSL 15:117db924cf7c 688
wolfSSL 15:117db924cf7c 689 #ifndef TEST_IPV6
wolfSSL 15:117db924cf7c 690 #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 15:117db924cf7c 691 addr->sin_family = PF_INET;
wolfSSL 15:117db924cf7c 692 #else
wolfSSL 15:117db924cf7c 693 addr->sin_family = AF_INET_V;
wolfSSL 15:117db924cf7c 694 #endif
wolfSSL 15:117db924cf7c 695 addr->sin_port = XHTONS(port);
wolfSSL 15:117db924cf7c 696 if ((size_t)peer == INADDR_ANY)
wolfSSL 15:117db924cf7c 697 addr->sin_addr.s_addr = INADDR_ANY;
wolfSSL 15:117db924cf7c 698 else {
wolfSSL 15:117db924cf7c 699 if (!useLookup)
wolfSSL 15:117db924cf7c 700 addr->sin_addr.s_addr = inet_addr(peer);
wolfSSL 15:117db924cf7c 701 }
wolfSSL 15:117db924cf7c 702 #else
wolfSSL 15:117db924cf7c 703 addr->sin6_family = AF_INET_V;
wolfSSL 15:117db924cf7c 704 addr->sin6_port = XHTONS(port);
wolfSSL 15:117db924cf7c 705 if ((size_t)peer == INADDR_ANY) {
wolfSSL 15:117db924cf7c 706 addr->sin6_addr = in6addr_any;
wolfSSL 15:117db924cf7c 707 }
wolfSSL 15:117db924cf7c 708 else {
wolfSSL 15:117db924cf7c 709 #ifdef HAVE_GETADDRINFO
wolfSSL 15:117db924cf7c 710 struct addrinfo hints;
wolfSSL 15:117db924cf7c 711 struct addrinfo* answer = NULL;
wolfSSL 15:117db924cf7c 712 int ret;
wolfSSL 15:117db924cf7c 713 char strPort[80];
wolfSSL 15:117db924cf7c 714
wolfSSL 15:117db924cf7c 715 XMEMSET(&hints, 0, sizeof(hints));
wolfSSL 15:117db924cf7c 716
wolfSSL 15:117db924cf7c 717 hints.ai_family = AF_INET_V;
wolfSSL 15:117db924cf7c 718 if (udp) {
wolfSSL 15:117db924cf7c 719 hints.ai_socktype = SOCK_DGRAM;
wolfSSL 15:117db924cf7c 720 hints.ai_protocol = IPPROTO_UDP;
wolfSSL 15:117db924cf7c 721 }
wolfSSL 15:117db924cf7c 722 #ifdef WOLFSSL_SCTP
wolfSSL 15:117db924cf7c 723 else if (sctp) {
wolfSSL 15:117db924cf7c 724 hints.ai_socktype = SOCK_STREAM;
wolfSSL 15:117db924cf7c 725 hints.ai_protocol = IPPROTO_SCTP;
wolfSSL 15:117db924cf7c 726 }
wolfSSL 15:117db924cf7c 727 #endif
wolfSSL 15:117db924cf7c 728 else {
wolfSSL 15:117db924cf7c 729 hints.ai_socktype = SOCK_STREAM;
wolfSSL 15:117db924cf7c 730 hints.ai_protocol = IPPROTO_TCP;
wolfSSL 15:117db924cf7c 731 }
wolfSSL 15:117db924cf7c 732
wolfSSL 15:117db924cf7c 733 SNPRINTF(strPort, sizeof(strPort), "%d", port);
wolfSSL 15:117db924cf7c 734 strPort[79] = '\0';
wolfSSL 15:117db924cf7c 735
wolfSSL 15:117db924cf7c 736 ret = getaddrinfo(peer, strPort, &hints, &answer);
wolfSSL 15:117db924cf7c 737 if (ret < 0 || answer == NULL)
wolfSSL 15:117db924cf7c 738 err_sys("getaddrinfo failed");
wolfSSL 15:117db924cf7c 739
wolfSSL 15:117db924cf7c 740 XMEMCPY(addr, answer->ai_addr, answer->ai_addrlen);
wolfSSL 15:117db924cf7c 741 freeaddrinfo(answer);
wolfSSL 15:117db924cf7c 742 #else
wolfSSL 15:117db924cf7c 743 printf("no ipv6 getaddrinfo, loopback only tests/examples\n");
wolfSSL 15:117db924cf7c 744 addr->sin6_addr = in6addr_loopback;
wolfSSL 15:117db924cf7c 745 #endif
wolfSSL 15:117db924cf7c 746 }
wolfSSL 15:117db924cf7c 747 #endif
wolfSSL 15:117db924cf7c 748 }
wolfSSL 15:117db924cf7c 749
wolfSSL 15:117db924cf7c 750
wolfSSL 15:117db924cf7c 751 static WC_INLINE void tcp_socket(SOCKET_T* sockfd, int udp, int sctp)
wolfSSL 15:117db924cf7c 752 {
wolfSSL 15:117db924cf7c 753 (void)sctp;
wolfSSL 15:117db924cf7c 754
wolfSSL 15:117db924cf7c 755 if (udp)
wolfSSL 15:117db924cf7c 756 *sockfd = socket(AF_INET_V, SOCK_DGRAM, IPPROTO_UDP);
wolfSSL 15:117db924cf7c 757 #ifdef WOLFSSL_SCTP
wolfSSL 15:117db924cf7c 758 else if (sctp)
wolfSSL 15:117db924cf7c 759 *sockfd = socket(AF_INET_V, SOCK_STREAM, IPPROTO_SCTP);
wolfSSL 15:117db924cf7c 760 #endif
wolfSSL 15:117db924cf7c 761 else
wolfSSL 15:117db924cf7c 762 *sockfd = socket(AF_INET_V, SOCK_STREAM, IPPROTO_TCP);
wolfSSL 15:117db924cf7c 763
wolfSSL 15:117db924cf7c 764 if(WOLFSSL_SOCKET_IS_INVALID(*sockfd)) {
wolfSSL 15:117db924cf7c 765 err_sys("socket failed\n");
wolfSSL 15:117db924cf7c 766 }
wolfSSL 15:117db924cf7c 767
wolfSSL 15:117db924cf7c 768 #ifndef USE_WINDOWS_API
wolfSSL 15:117db924cf7c 769 #ifdef SO_NOSIGPIPE
wolfSSL 15:117db924cf7c 770 {
wolfSSL 15:117db924cf7c 771 int on = 1;
wolfSSL 15:117db924cf7c 772 socklen_t len = sizeof(on);
wolfSSL 15:117db924cf7c 773 int res = setsockopt(*sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, len);
wolfSSL 15:117db924cf7c 774 if (res < 0)
wolfSSL 15:117db924cf7c 775 err_sys("setsockopt SO_NOSIGPIPE failed\n");
wolfSSL 15:117db924cf7c 776 }
wolfSSL 15:117db924cf7c 777 #elif defined(WOLFSSL_MDK_ARM) || defined (WOLFSSL_TIRTOS) ||\
wolfSSL 15:117db924cf7c 778 defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 15:117db924cf7c 779 /* nothing to define */
wolfSSL 15:117db924cf7c 780 #else /* no S_NOSIGPIPE */
wolfSSL 15:117db924cf7c 781 signal(SIGPIPE, SIG_IGN);
wolfSSL 15:117db924cf7c 782 #endif /* S_NOSIGPIPE */
wolfSSL 15:117db924cf7c 783
wolfSSL 15:117db924cf7c 784 #if defined(TCP_NODELAY)
wolfSSL 15:117db924cf7c 785 if (!udp && !sctp)
wolfSSL 15:117db924cf7c 786 {
wolfSSL 15:117db924cf7c 787 int on = 1;
wolfSSL 15:117db924cf7c 788 socklen_t len = sizeof(on);
wolfSSL 15:117db924cf7c 789 int res = setsockopt(*sockfd, IPPROTO_TCP, TCP_NODELAY, &on, len);
wolfSSL 15:117db924cf7c 790 if (res < 0)
wolfSSL 15:117db924cf7c 791 err_sys("setsockopt TCP_NODELAY failed\n");
wolfSSL 15:117db924cf7c 792 }
wolfSSL 15:117db924cf7c 793 #endif
wolfSSL 15:117db924cf7c 794 #endif /* USE_WINDOWS_API */
wolfSSL 15:117db924cf7c 795 }
wolfSSL 15:117db924cf7c 796
wolfSSL 15:117db924cf7c 797 static WC_INLINE void tcp_connect(SOCKET_T* sockfd, const char* ip, word16 port,
wolfSSL 15:117db924cf7c 798 int udp, int sctp, WOLFSSL* ssl)
wolfSSL 15:117db924cf7c 799 {
wolfSSL 15:117db924cf7c 800 SOCKADDR_IN_T addr;
wolfSSL 15:117db924cf7c 801 build_addr(&addr, ip, port, udp, sctp);
wolfSSL 15:117db924cf7c 802 if (udp) {
wolfSSL 15:117db924cf7c 803 wolfSSL_dtls_set_peer(ssl, &addr, sizeof(addr));
wolfSSL 15:117db924cf7c 804 }
wolfSSL 15:117db924cf7c 805 tcp_socket(sockfd, udp, sctp);
wolfSSL 15:117db924cf7c 806
wolfSSL 15:117db924cf7c 807 if (!udp) {
wolfSSL 15:117db924cf7c 808 if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
wolfSSL 15:117db924cf7c 809 err_sys("tcp connect failed");
wolfSSL 15:117db924cf7c 810 }
wolfSSL 15:117db924cf7c 811 }
wolfSSL 15:117db924cf7c 812
wolfSSL 15:117db924cf7c 813
wolfSSL 15:117db924cf7c 814 static WC_INLINE void udp_connect(SOCKET_T* sockfd, void* addr, int addrSz)
wolfSSL 15:117db924cf7c 815 {
wolfSSL 15:117db924cf7c 816 if (connect(*sockfd, (const struct sockaddr*)addr, addrSz) != 0)
wolfSSL 15:117db924cf7c 817 err_sys("tcp connect failed");
wolfSSL 15:117db924cf7c 818 }
wolfSSL 15:117db924cf7c 819
wolfSSL 15:117db924cf7c 820
wolfSSL 15:117db924cf7c 821 enum {
wolfSSL 15:117db924cf7c 822 TEST_SELECT_FAIL,
wolfSSL 15:117db924cf7c 823 TEST_TIMEOUT,
wolfSSL 15:117db924cf7c 824 TEST_RECV_READY,
wolfSSL 15:117db924cf7c 825 TEST_ERROR_READY
wolfSSL 15:117db924cf7c 826 };
wolfSSL 15:117db924cf7c 827
wolfSSL 15:117db924cf7c 828
wolfSSL 15:117db924cf7c 829 #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_TCP_NET) && \
wolfSSL 15:117db924cf7c 830 !defined(WOLFSSL_TIRTOS)
wolfSSL 15:117db924cf7c 831 static WC_INLINE int tcp_select(SOCKET_T socketfd, int to_sec)
wolfSSL 15:117db924cf7c 832 {
wolfSSL 15:117db924cf7c 833 fd_set recvfds, errfds;
wolfSSL 15:117db924cf7c 834 SOCKET_T nfds = socketfd + 1;
wolfSSL 15:117db924cf7c 835 #if !defined(__INTEGRITY)
wolfSSL 15:117db924cf7c 836 struct timeval timeout = {(to_sec > 0) ? to_sec : 0, 0};
wolfSSL 15:117db924cf7c 837 #else
wolfSSL 15:117db924cf7c 838 struct timeval timeout;
wolfSSL 15:117db924cf7c 839 #endif
wolfSSL 15:117db924cf7c 840 int result;
wolfSSL 15:117db924cf7c 841
wolfSSL 15:117db924cf7c 842 FD_ZERO(&recvfds);
wolfSSL 15:117db924cf7c 843 FD_SET(socketfd, &recvfds);
wolfSSL 15:117db924cf7c 844 FD_ZERO(&errfds);
wolfSSL 15:117db924cf7c 845 FD_SET(socketfd, &errfds);
wolfSSL 15:117db924cf7c 846
wolfSSL 15:117db924cf7c 847 #if defined(__INTEGRITY)
wolfSSL 15:117db924cf7c 848 timeout.tv_sec = (long long)(to_sec > 0) ? to_sec : 0, 0;
wolfSSL 15:117db924cf7c 849 #endif
wolfSSL 15:117db924cf7c 850 result = select(nfds, &recvfds, NULL, &errfds, &timeout);
wolfSSL 15:117db924cf7c 851
wolfSSL 15:117db924cf7c 852 if (result == 0)
wolfSSL 15:117db924cf7c 853 return TEST_TIMEOUT;
wolfSSL 15:117db924cf7c 854 else if (result > 0) {
wolfSSL 15:117db924cf7c 855 if (FD_ISSET(socketfd, &recvfds))
wolfSSL 15:117db924cf7c 856 return TEST_RECV_READY;
wolfSSL 15:117db924cf7c 857 else if(FD_ISSET(socketfd, &errfds))
wolfSSL 15:117db924cf7c 858 return TEST_ERROR_READY;
wolfSSL 15:117db924cf7c 859 }
wolfSSL 15:117db924cf7c 860
wolfSSL 15:117db924cf7c 861 return TEST_SELECT_FAIL;
wolfSSL 15:117db924cf7c 862 }
wolfSSL 15:117db924cf7c 863 #elif defined(WOLFSSL_TIRTOS) || defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 15:117db924cf7c 864 static WC_INLINE int tcp_select(SOCKET_T socketfd, int to_sec)
wolfSSL 15:117db924cf7c 865 {
wolfSSL 15:117db924cf7c 866 return TEST_RECV_READY;
wolfSSL 15:117db924cf7c 867 }
wolfSSL 15:117db924cf7c 868 #endif /* !WOLFSSL_MDK_ARM */
wolfSSL 15:117db924cf7c 869
wolfSSL 15:117db924cf7c 870
wolfSSL 15:117db924cf7c 871 static WC_INLINE void tcp_listen(SOCKET_T* sockfd, word16* port, int useAnyAddr,
wolfSSL 15:117db924cf7c 872 int udp, int sctp)
wolfSSL 15:117db924cf7c 873 {
wolfSSL 15:117db924cf7c 874 SOCKADDR_IN_T addr;
wolfSSL 15:117db924cf7c 875
wolfSSL 15:117db924cf7c 876 /* don't use INADDR_ANY by default, firewall may block, make user switch
wolfSSL 15:117db924cf7c 877 on */
wolfSSL 15:117db924cf7c 878 build_addr(&addr, (useAnyAddr ? INADDR_ANY : wolfSSLIP), *port, udp, sctp);
wolfSSL 15:117db924cf7c 879 tcp_socket(sockfd, udp, sctp);
wolfSSL 15:117db924cf7c 880
wolfSSL 15:117db924cf7c 881 #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_ARM)\
wolfSSL 15:117db924cf7c 882 && !defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 15:117db924cf7c 883 {
wolfSSL 15:117db924cf7c 884 int res, on = 1;
wolfSSL 15:117db924cf7c 885 socklen_t len = sizeof(on);
wolfSSL 15:117db924cf7c 886 res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
wolfSSL 15:117db924cf7c 887 if (res < 0)
wolfSSL 15:117db924cf7c 888 err_sys("setsockopt SO_REUSEADDR failed\n");
wolfSSL 15:117db924cf7c 889 }
wolfSSL 15:117db924cf7c 890 #endif
wolfSSL 15:117db924cf7c 891
wolfSSL 15:117db924cf7c 892 if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
wolfSSL 15:117db924cf7c 893 err_sys("tcp bind failed");
wolfSSL 15:117db924cf7c 894 if (!udp) {
wolfSSL 15:117db924cf7c 895 #ifdef WOLFSSL_KEIL_TCP_NET
wolfSSL 15:117db924cf7c 896 #define SOCK_LISTEN_MAX_QUEUE 1
wolfSSL 15:117db924cf7c 897 #else
wolfSSL 15:117db924cf7c 898 #define SOCK_LISTEN_MAX_QUEUE 5
wolfSSL 15:117db924cf7c 899 #endif
wolfSSL 15:117db924cf7c 900 if (listen(*sockfd, SOCK_LISTEN_MAX_QUEUE) != 0)
wolfSSL 15:117db924cf7c 901 err_sys("tcp listen failed");
wolfSSL 15:117db924cf7c 902 }
wolfSSL 15:117db924cf7c 903 #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_TIRTOS)
wolfSSL 15:117db924cf7c 904 if (*port == 0) {
wolfSSL 15:117db924cf7c 905 socklen_t len = sizeof(addr);
wolfSSL 15:117db924cf7c 906 if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) {
wolfSSL 15:117db924cf7c 907 #ifndef TEST_IPV6
wolfSSL 15:117db924cf7c 908 *port = XNTOHS(addr.sin_port);
wolfSSL 15:117db924cf7c 909 #else
wolfSSL 15:117db924cf7c 910 *port = XNTOHS(addr.sin6_port);
wolfSSL 15:117db924cf7c 911 #endif
wolfSSL 15:117db924cf7c 912 }
wolfSSL 15:117db924cf7c 913 }
wolfSSL 15:117db924cf7c 914 #endif
wolfSSL 15:117db924cf7c 915 }
wolfSSL 15:117db924cf7c 916
wolfSSL 15:117db924cf7c 917
wolfSSL 15:117db924cf7c 918 #if 0
wolfSSL 15:117db924cf7c 919 static WC_INLINE int udp_read_connect(SOCKET_T sockfd)
wolfSSL 15:117db924cf7c 920 {
wolfSSL 15:117db924cf7c 921 SOCKADDR_IN_T cliaddr;
wolfSSL 15:117db924cf7c 922 byte b[1500];
wolfSSL 15:117db924cf7c 923 int n;
wolfSSL 15:117db924cf7c 924 socklen_t len = sizeof(cliaddr);
wolfSSL 15:117db924cf7c 925
wolfSSL 15:117db924cf7c 926 n = (int)recvfrom(sockfd, (char*)b, sizeof(b), MSG_PEEK,
wolfSSL 15:117db924cf7c 927 (struct sockaddr*)&cliaddr, &len);
wolfSSL 15:117db924cf7c 928 if (n > 0) {
wolfSSL 15:117db924cf7c 929 if (connect(sockfd, (const struct sockaddr*)&cliaddr,
wolfSSL 15:117db924cf7c 930 sizeof(cliaddr)) != 0)
wolfSSL 15:117db924cf7c 931 err_sys("udp connect failed");
wolfSSL 15:117db924cf7c 932 }
wolfSSL 15:117db924cf7c 933 else
wolfSSL 15:117db924cf7c 934 err_sys("recvfrom failed");
wolfSSL 15:117db924cf7c 935
wolfSSL 15:117db924cf7c 936 return sockfd;
wolfSSL 15:117db924cf7c 937 }
wolfSSL 15:117db924cf7c 938 #endif
wolfSSL 15:117db924cf7c 939
wolfSSL 15:117db924cf7c 940 static WC_INLINE void udp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
wolfSSL 15:117db924cf7c 941 int useAnyAddr, word16 port, func_args* args)
wolfSSL 15:117db924cf7c 942 {
wolfSSL 15:117db924cf7c 943 SOCKADDR_IN_T addr;
wolfSSL 15:117db924cf7c 944
wolfSSL 15:117db924cf7c 945 (void)args;
wolfSSL 15:117db924cf7c 946 build_addr(&addr, (useAnyAddr ? INADDR_ANY : wolfSSLIP), port, 1, 0);
wolfSSL 15:117db924cf7c 947 tcp_socket(sockfd, 1, 0);
wolfSSL 15:117db924cf7c 948
wolfSSL 15:117db924cf7c 949
wolfSSL 15:117db924cf7c 950 #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_ARM) \
wolfSSL 15:117db924cf7c 951 && !defined(WOLFSSL_KEIL_TCP_NET)
wolfSSL 15:117db924cf7c 952 {
wolfSSL 15:117db924cf7c 953 int res, on = 1;
wolfSSL 15:117db924cf7c 954 socklen_t len = sizeof(on);
wolfSSL 15:117db924cf7c 955 res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
wolfSSL 15:117db924cf7c 956 if (res < 0)
wolfSSL 15:117db924cf7c 957 err_sys("setsockopt SO_REUSEADDR failed\n");
wolfSSL 15:117db924cf7c 958 }
wolfSSL 15:117db924cf7c 959 #endif
wolfSSL 15:117db924cf7c 960
wolfSSL 15:117db924cf7c 961 if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
wolfSSL 15:117db924cf7c 962 err_sys("tcp bind failed");
wolfSSL 15:117db924cf7c 963
wolfSSL 15:117db924cf7c 964 #if (defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API)) && !defined(WOLFSSL_TIRTOS)
wolfSSL 15:117db924cf7c 965 if (port == 0) {
wolfSSL 15:117db924cf7c 966 socklen_t len = sizeof(addr);
wolfSSL 15:117db924cf7c 967 if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) {
wolfSSL 15:117db924cf7c 968 #ifndef TEST_IPV6
wolfSSL 15:117db924cf7c 969 port = XNTOHS(addr.sin_port);
wolfSSL 15:117db924cf7c 970 #else
wolfSSL 15:117db924cf7c 971 port = XNTOHS(addr.sin6_port);
wolfSSL 15:117db924cf7c 972 #endif
wolfSSL 15:117db924cf7c 973 }
wolfSSL 15:117db924cf7c 974 }
wolfSSL 15:117db924cf7c 975 #endif
wolfSSL 15:117db924cf7c 976
wolfSSL 15:117db924cf7c 977 #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__)
wolfSSL 15:117db924cf7c 978 /* signal ready to accept data */
wolfSSL 15:117db924cf7c 979 {
wolfSSL 15:117db924cf7c 980 tcp_ready* ready = args->signal;
wolfSSL 15:117db924cf7c 981 pthread_mutex_lock(&ready->mutex);
wolfSSL 15:117db924cf7c 982 ready->ready = 1;
wolfSSL 15:117db924cf7c 983 ready->port = port;
wolfSSL 15:117db924cf7c 984 pthread_cond_signal(&ready->cond);
wolfSSL 15:117db924cf7c 985 pthread_mutex_unlock(&ready->mutex);
wolfSSL 15:117db924cf7c 986 }
wolfSSL 15:117db924cf7c 987 #elif defined (WOLFSSL_TIRTOS)
wolfSSL 15:117db924cf7c 988 /* Need mutex? */
wolfSSL 15:117db924cf7c 989 tcp_ready* ready = args->signal;
wolfSSL 15:117db924cf7c 990 ready->ready = 1;
wolfSSL 15:117db924cf7c 991 ready->port = port;
wolfSSL 15:117db924cf7c 992 #endif
wolfSSL 15:117db924cf7c 993
wolfSSL 15:117db924cf7c 994 *clientfd = *sockfd;
wolfSSL 15:117db924cf7c 995 }
wolfSSL 15:117db924cf7c 996
wolfSSL 15:117db924cf7c 997 static WC_INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
wolfSSL 15:117db924cf7c 998 func_args* args, word16 port, int useAnyAddr,
wolfSSL 15:117db924cf7c 999 int udp, int sctp, int ready_file, int do_listen)
wolfSSL 15:117db924cf7c 1000 {
wolfSSL 15:117db924cf7c 1001 SOCKADDR_IN_T client;
wolfSSL 15:117db924cf7c 1002 socklen_t client_len = sizeof(client);
wolfSSL 15:117db924cf7c 1003 tcp_ready* ready = NULL;
wolfSSL 15:117db924cf7c 1004
wolfSSL 15:117db924cf7c 1005 (void) ready; /* Account for case when "ready" is not used */
wolfSSL 15:117db924cf7c 1006
wolfSSL 15:117db924cf7c 1007 if (udp) {
wolfSSL 15:117db924cf7c 1008 udp_accept(sockfd, clientfd, useAnyAddr, port, args);
wolfSSL 15:117db924cf7c 1009 return;
wolfSSL 15:117db924cf7c 1010 }
wolfSSL 15:117db924cf7c 1011
wolfSSL 15:117db924cf7c 1012 if(do_listen) {
wolfSSL 15:117db924cf7c 1013 tcp_listen(sockfd, &port, useAnyAddr, udp, sctp);
wolfSSL 15:117db924cf7c 1014
wolfSSL 15:117db924cf7c 1015 #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__)
wolfSSL 15:117db924cf7c 1016 /* signal ready to tcp_accept */
wolfSSL 15:117db924cf7c 1017 if (args)
wolfSSL 15:117db924cf7c 1018 ready = args->signal;
wolfSSL 15:117db924cf7c 1019 if (ready) {
wolfSSL 15:117db924cf7c 1020 pthread_mutex_lock(&ready->mutex);
wolfSSL 15:117db924cf7c 1021 ready->ready = 1;
wolfSSL 15:117db924cf7c 1022 ready->port = port;
wolfSSL 15:117db924cf7c 1023 pthread_cond_signal(&ready->cond);
wolfSSL 15:117db924cf7c 1024 pthread_mutex_unlock(&ready->mutex);
wolfSSL 15:117db924cf7c 1025 }
wolfSSL 15:117db924cf7c 1026 #elif defined (WOLFSSL_TIRTOS)
wolfSSL 15:117db924cf7c 1027 /* Need mutex? */
wolfSSL 15:117db924cf7c 1028 if (args)
wolfSSL 15:117db924cf7c 1029 ready = args->signal;
wolfSSL 15:117db924cf7c 1030 if (ready) {
wolfSSL 15:117db924cf7c 1031 ready->ready = 1;
wolfSSL 15:117db924cf7c 1032 ready->port = port;
wolfSSL 15:117db924cf7c 1033 }
wolfSSL 15:117db924cf7c 1034 #endif
wolfSSL 15:117db924cf7c 1035
wolfSSL 15:117db924cf7c 1036 if (ready_file) {
wolfSSL 15:117db924cf7c 1037 #if !defined(NO_FILESYSTEM) || defined(FORCE_BUFFER_TEST)
wolfSSL 15:117db924cf7c 1038 FILE* srf = NULL;
wolfSSL 15:117db924cf7c 1039 if (args)
wolfSSL 15:117db924cf7c 1040 ready = args->signal;
wolfSSL 15:117db924cf7c 1041
wolfSSL 15:117db924cf7c 1042 if (ready) {
wolfSSL 15:117db924cf7c 1043 srf = fopen(ready->srfName, "w");
wolfSSL 15:117db924cf7c 1044
wolfSSL 15:117db924cf7c 1045 if (srf) {
wolfSSL 15:117db924cf7c 1046 /* let's write port sever is listening on to ready file
wolfSSL 15:117db924cf7c 1047 external monitor can then do ephemeral ports by passing
wolfSSL 15:117db924cf7c 1048 -p 0 to server on supported platforms with -R ready_file
wolfSSL 15:117db924cf7c 1049 client can then wait for existence of ready_file and see
wolfSSL 15:117db924cf7c 1050 which port the server is listening on. */
wolfSSL 15:117db924cf7c 1051 fprintf(srf, "%d\n", (int)port);
wolfSSL 15:117db924cf7c 1052 fclose(srf);
wolfSSL 15:117db924cf7c 1053 }
wolfSSL 15:117db924cf7c 1054 }
wolfSSL 15:117db924cf7c 1055 #endif
wolfSSL 15:117db924cf7c 1056 }
wolfSSL 15:117db924cf7c 1057 }
wolfSSL 15:117db924cf7c 1058
wolfSSL 15:117db924cf7c 1059 *clientfd = accept(*sockfd, (struct sockaddr*)&client,
wolfSSL 15:117db924cf7c 1060 (ACCEPT_THIRD_T)&client_len);
wolfSSL 15:117db924cf7c 1061 if(WOLFSSL_SOCKET_IS_INVALID(*clientfd)) {
wolfSSL 15:117db924cf7c 1062 err_sys("tcp accept failed");
wolfSSL 15:117db924cf7c 1063 }
wolfSSL 15:117db924cf7c 1064 }
wolfSSL 15:117db924cf7c 1065
wolfSSL 15:117db924cf7c 1066
wolfSSL 15:117db924cf7c 1067 static WC_INLINE void tcp_set_nonblocking(SOCKET_T* sockfd)
wolfSSL 15:117db924cf7c 1068 {
wolfSSL 15:117db924cf7c 1069 #ifdef USE_WINDOWS_API
wolfSSL 15:117db924cf7c 1070 unsigned long blocking = 1;
wolfSSL 15:117db924cf7c 1071 int ret = ioctlsocket(*sockfd, FIONBIO, &blocking);
wolfSSL 15:117db924cf7c 1072 if (ret == SOCKET_ERROR)
wolfSSL 15:117db924cf7c 1073 err_sys("ioctlsocket failed");
wolfSSL 15:117db924cf7c 1074 #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) \
wolfSSL 15:117db924cf7c 1075 || defined (WOLFSSL_TIRTOS)|| defined(WOLFSSL_VXWORKS)
wolfSSL 15:117db924cf7c 1076 /* non blocking not supported, for now */
wolfSSL 15:117db924cf7c 1077 #else
wolfSSL 15:117db924cf7c 1078 int flags = fcntl(*sockfd, F_GETFL, 0);
wolfSSL 15:117db924cf7c 1079 if (flags < 0)
wolfSSL 15:117db924cf7c 1080 err_sys("fcntl get failed");
wolfSSL 15:117db924cf7c 1081 flags = fcntl(*sockfd, F_SETFL, flags | O_NONBLOCK);
wolfSSL 15:117db924cf7c 1082 if (flags < 0)
wolfSSL 15:117db924cf7c 1083 err_sys("fcntl set failed");
wolfSSL 15:117db924cf7c 1084 #endif
wolfSSL 15:117db924cf7c 1085 }
wolfSSL 15:117db924cf7c 1086
wolfSSL 15:117db924cf7c 1087
wolfSSL 15:117db924cf7c 1088 #ifndef NO_PSK
wolfSSL 15:117db924cf7c 1089
wolfSSL 15:117db924cf7c 1090 /* identity is OpenSSL testing default for openssl s_client, keep same */
wolfSSL 15:117db924cf7c 1091 static const char* kIdentityStr = "Client_identity";
wolfSSL 15:117db924cf7c 1092
wolfSSL 15:117db924cf7c 1093 static WC_INLINE unsigned int my_psk_client_cb(WOLFSSL* ssl, const char* hint,
wolfSSL 15:117db924cf7c 1094 char* identity, unsigned int id_max_len, unsigned char* key,
wolfSSL 15:117db924cf7c 1095 unsigned int key_max_len)
wolfSSL 15:117db924cf7c 1096 {
wolfSSL 15:117db924cf7c 1097 (void)ssl;
wolfSSL 15:117db924cf7c 1098 (void)hint;
wolfSSL 15:117db924cf7c 1099 (void)key_max_len;
wolfSSL 15:117db924cf7c 1100
wolfSSL 15:117db924cf7c 1101 /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
wolfSSL 15:117db924cf7c 1102 strncpy(identity, kIdentityStr, id_max_len);
wolfSSL 15:117db924cf7c 1103
wolfSSL 15:117db924cf7c 1104 if (wolfSSL_GetVersion(ssl) < WOLFSSL_TLSV1_3) {
wolfSSL 15:117db924cf7c 1105 /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using
wolfSSL 15:117db924cf7c 1106 unsigned binary */
wolfSSL 15:117db924cf7c 1107 key[0] = 0x1a;
wolfSSL 15:117db924cf7c 1108 key[1] = 0x2b;
wolfSSL 15:117db924cf7c 1109 key[2] = 0x3c;
wolfSSL 15:117db924cf7c 1110 key[3] = 0x4d;
wolfSSL 15:117db924cf7c 1111
wolfSSL 15:117db924cf7c 1112
wolfSSL 15:117db924cf7c 1113 return 4; /* length of key in octets or 0 for error */
wolfSSL 15:117db924cf7c 1114 }
wolfSSL 15:117db924cf7c 1115 else {
wolfSSL 15:117db924cf7c 1116 int i;
wolfSSL 15:117db924cf7c 1117 int b = 0x01;
wolfSSL 15:117db924cf7c 1118
wolfSSL 15:117db924cf7c 1119 for (i = 0; i < 32; i++, b += 0x22) {
wolfSSL 15:117db924cf7c 1120 if (b >= 0x100)
wolfSSL 15:117db924cf7c 1121 b = 0x01;
wolfSSL 15:117db924cf7c 1122 key[i] = b;
wolfSSL 15:117db924cf7c 1123 }
wolfSSL 15:117db924cf7c 1124
wolfSSL 15:117db924cf7c 1125 return 32; /* length of key in octets or 0 for error */
wolfSSL 15:117db924cf7c 1126 }
wolfSSL 15:117db924cf7c 1127 }
wolfSSL 15:117db924cf7c 1128
wolfSSL 15:117db924cf7c 1129
wolfSSL 15:117db924cf7c 1130 static WC_INLINE unsigned int my_psk_server_cb(WOLFSSL* ssl, const char* identity,
wolfSSL 15:117db924cf7c 1131 unsigned char* key, unsigned int key_max_len)
wolfSSL 15:117db924cf7c 1132 {
wolfSSL 15:117db924cf7c 1133 (void)ssl;
wolfSSL 15:117db924cf7c 1134 (void)key_max_len;
wolfSSL 15:117db924cf7c 1135
wolfSSL 15:117db924cf7c 1136 /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
wolfSSL 15:117db924cf7c 1137 if (strncmp(identity, kIdentityStr, strlen(kIdentityStr)) != 0)
wolfSSL 15:117db924cf7c 1138 return 0;
wolfSSL 15:117db924cf7c 1139
wolfSSL 15:117db924cf7c 1140 if (wolfSSL_GetVersion(ssl) < WOLFSSL_TLSV1_3) {
wolfSSL 15:117db924cf7c 1141 /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using
wolfSSL 15:117db924cf7c 1142 unsigned binary */
wolfSSL 15:117db924cf7c 1143 key[0] = 0x1a;
wolfSSL 15:117db924cf7c 1144 key[1] = 0x2b;
wolfSSL 15:117db924cf7c 1145 key[2] = 0x3c;
wolfSSL 15:117db924cf7c 1146 key[3] = 0x4d;
wolfSSL 15:117db924cf7c 1147
wolfSSL 15:117db924cf7c 1148
wolfSSL 15:117db924cf7c 1149 return 4; /* length of key in octets or 0 for error */
wolfSSL 15:117db924cf7c 1150 }
wolfSSL 15:117db924cf7c 1151 else {
wolfSSL 15:117db924cf7c 1152 int i;
wolfSSL 15:117db924cf7c 1153 int b = 0x01;
wolfSSL 15:117db924cf7c 1154
wolfSSL 15:117db924cf7c 1155 for (i = 0; i < 32; i++, b += 0x22) {
wolfSSL 15:117db924cf7c 1156 if (b >= 0x100)
wolfSSL 15:117db924cf7c 1157 b = 0x01;
wolfSSL 15:117db924cf7c 1158 key[i] = b;
wolfSSL 15:117db924cf7c 1159 }
wolfSSL 15:117db924cf7c 1160
wolfSSL 15:117db924cf7c 1161 return 32; /* length of key in octets or 0 for error */
wolfSSL 15:117db924cf7c 1162 }
wolfSSL 15:117db924cf7c 1163 }
wolfSSL 15:117db924cf7c 1164
wolfSSL 15:117db924cf7c 1165 #endif /* NO_PSK */
wolfSSL 15:117db924cf7c 1166
wolfSSL 15:117db924cf7c 1167
wolfSSL 15:117db924cf7c 1168 #if defined(WOLFSSL_USER_CURRTIME)
wolfSSL 15:117db924cf7c 1169 extern double current_time(int reset);
wolfSSL 15:117db924cf7c 1170
wolfSSL 15:117db924cf7c 1171 #elif defined(USE_WINDOWS_API)
wolfSSL 15:117db924cf7c 1172
wolfSSL 15:117db924cf7c 1173 #define WIN32_LEAN_AND_MEAN
wolfSSL 15:117db924cf7c 1174 #include <windows.h>
wolfSSL 15:117db924cf7c 1175
wolfSSL 15:117db924cf7c 1176 static WC_INLINE double current_time(int reset)
wolfSSL 15:117db924cf7c 1177 {
wolfSSL 15:117db924cf7c 1178 static int init = 0;
wolfSSL 15:117db924cf7c 1179 static LARGE_INTEGER freq;
wolfSSL 15:117db924cf7c 1180
wolfSSL 15:117db924cf7c 1181 LARGE_INTEGER count;
wolfSSL 15:117db924cf7c 1182
wolfSSL 15:117db924cf7c 1183 if (!init) {
wolfSSL 15:117db924cf7c 1184 QueryPerformanceFrequency(&freq);
wolfSSL 15:117db924cf7c 1185 init = 1;
wolfSSL 15:117db924cf7c 1186 }
wolfSSL 15:117db924cf7c 1187
wolfSSL 15:117db924cf7c 1188 QueryPerformanceCounter(&count);
wolfSSL 15:117db924cf7c 1189
wolfSSL 15:117db924cf7c 1190 (void)reset;
wolfSSL 15:117db924cf7c 1191 return (double)count.QuadPart / freq.QuadPart;
wolfSSL 15:117db924cf7c 1192 }
wolfSSL 15:117db924cf7c 1193
wolfSSL 15:117db924cf7c 1194 #elif defined(WOLFSSL_TIRTOS)
wolfSSL 15:117db924cf7c 1195 extern double current_time();
wolfSSL 15:117db924cf7c 1196 #else
wolfSSL 15:117db924cf7c 1197
wolfSSL 15:117db924cf7c 1198 #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_TCP_NET) && !defined(WOLFSSL_CHIBIOS)
wolfSSL 15:117db924cf7c 1199 #include <sys/time.h>
wolfSSL 15:117db924cf7c 1200
wolfSSL 15:117db924cf7c 1201 static WC_INLINE double current_time(int reset)
wolfSSL 15:117db924cf7c 1202 {
wolfSSL 15:117db924cf7c 1203 struct timeval tv;
wolfSSL 15:117db924cf7c 1204 gettimeofday(&tv, 0);
wolfSSL 15:117db924cf7c 1205 (void)reset;
wolfSSL 15:117db924cf7c 1206
wolfSSL 15:117db924cf7c 1207 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
wolfSSL 15:117db924cf7c 1208 }
wolfSSL 15:117db924cf7c 1209 #else
wolfSSL 15:117db924cf7c 1210 extern double current_time(int reset);
wolfSSL 15:117db924cf7c 1211 #endif
wolfSSL 15:117db924cf7c 1212 #endif /* USE_WINDOWS_API */
wolfSSL 15:117db924cf7c 1213
wolfSSL 15:117db924cf7c 1214
wolfSSL 15:117db924cf7c 1215 #if defined(HAVE_OCSP) && defined(WOLFSSL_NONBLOCK_OCSP)
wolfSSL 15:117db924cf7c 1216 static WC_INLINE int OCSPIOCb(void* ioCtx, const char* url, int urlSz,
wolfSSL 15:117db924cf7c 1217 unsigned char* request, int requestSz, unsigned char** response)
wolfSSL 15:117db924cf7c 1218 {
wolfSSL 15:117db924cf7c 1219 #ifdef TEST_NONBLOCK_CERTS
wolfSSL 15:117db924cf7c 1220 static int ioCbCnt = 0;
wolfSSL 15:117db924cf7c 1221 #endif
wolfSSL 15:117db924cf7c 1222
wolfSSL 15:117db924cf7c 1223 (void)ioCtx;
wolfSSL 15:117db924cf7c 1224 (void)url;
wolfSSL 15:117db924cf7c 1225 (void)urlSz;
wolfSSL 15:117db924cf7c 1226 (void)request;
wolfSSL 15:117db924cf7c 1227 (void)requestSz;
wolfSSL 15:117db924cf7c 1228 (void)response;
wolfSSL 15:117db924cf7c 1229
wolfSSL 15:117db924cf7c 1230 #ifdef TEST_NONBLOCK_CERTS
wolfSSL 15:117db924cf7c 1231 if (ioCbCnt) {
wolfSSL 15:117db924cf7c 1232 ioCbCnt = 0;
wolfSSL 15:117db924cf7c 1233 return EmbedOcspLookup(ioCtx, url, urlSz, request, requestSz, response);
wolfSSL 15:117db924cf7c 1234 }
wolfSSL 15:117db924cf7c 1235 else {
wolfSSL 15:117db924cf7c 1236 ioCbCnt = 1;
wolfSSL 15:117db924cf7c 1237 return WOLFSSL_CBIO_ERR_WANT_READ;
wolfSSL 15:117db924cf7c 1238 }
wolfSSL 15:117db924cf7c 1239 #else
wolfSSL 15:117db924cf7c 1240 return EmbedOcspLookup(ioCtx, url, urlSz, request, requestSz, response);
wolfSSL 15:117db924cf7c 1241 #endif
wolfSSL 15:117db924cf7c 1242 }
wolfSSL 15:117db924cf7c 1243
wolfSSL 15:117db924cf7c 1244 static WC_INLINE void OCSPRespFreeCb(void* ioCtx, unsigned char* response)
wolfSSL 15:117db924cf7c 1245 {
wolfSSL 15:117db924cf7c 1246 (void)ioCtx;
wolfSSL 15:117db924cf7c 1247 (void)response;
wolfSSL 15:117db924cf7c 1248 }
wolfSSL 15:117db924cf7c 1249 #endif
wolfSSL 15:117db924cf7c 1250
wolfSSL 15:117db924cf7c 1251 #if !defined(NO_CERTS)
wolfSSL 15:117db924cf7c 1252 #if !defined(NO_FILESYSTEM) || \
wolfSSL 15:117db924cf7c 1253 (defined(NO_FILESYSTEM) && defined(FORCE_BUFFER_TEST))
wolfSSL 15:117db924cf7c 1254
wolfSSL 15:117db924cf7c 1255 /* reads file size, allocates buffer, reads into buffer, returns buffer */
wolfSSL 15:117db924cf7c 1256 static WC_INLINE int load_file(const char* fname, byte** buf, size_t* bufLen)
wolfSSL 15:117db924cf7c 1257 {
wolfSSL 15:117db924cf7c 1258 int ret;
wolfSSL 15:117db924cf7c 1259 long int fileSz;
wolfSSL 15:117db924cf7c 1260 FILE* file;
wolfSSL 15:117db924cf7c 1261
wolfSSL 15:117db924cf7c 1262 if (fname == NULL || buf == NULL || bufLen == NULL)
wolfSSL 15:117db924cf7c 1263 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1264
wolfSSL 15:117db924cf7c 1265 /* set defaults */
wolfSSL 15:117db924cf7c 1266 *buf = NULL;
wolfSSL 15:117db924cf7c 1267 *bufLen = 0;
wolfSSL 15:117db924cf7c 1268
wolfSSL 15:117db924cf7c 1269 /* open file (read-only binary) */
wolfSSL 15:117db924cf7c 1270 file = fopen(fname, "rb");
wolfSSL 15:117db924cf7c 1271 if (!file) {
wolfSSL 15:117db924cf7c 1272 printf("Error loading %s\n", fname);
wolfSSL 15:117db924cf7c 1273 return BAD_PATH_ERROR;
wolfSSL 15:117db924cf7c 1274 }
wolfSSL 15:117db924cf7c 1275
wolfSSL 15:117db924cf7c 1276 fseek(file, 0, SEEK_END);
wolfSSL 15:117db924cf7c 1277 fileSz = (int)ftell(file);
wolfSSL 15:117db924cf7c 1278 rewind(file);
wolfSSL 15:117db924cf7c 1279 if (fileSz > 0) {
wolfSSL 15:117db924cf7c 1280 *bufLen = (size_t)fileSz;
wolfSSL 15:117db924cf7c 1281 *buf = (byte*)malloc(*bufLen);
wolfSSL 15:117db924cf7c 1282 if (*buf == NULL) {
wolfSSL 15:117db924cf7c 1283 ret = MEMORY_E;
wolfSSL 15:117db924cf7c 1284 printf("Error allocating %lu bytes\n", (unsigned long)*bufLen);
wolfSSL 15:117db924cf7c 1285 }
wolfSSL 15:117db924cf7c 1286 else {
wolfSSL 15:117db924cf7c 1287 size_t readLen = fread(*buf, *bufLen, 1, file);
wolfSSL 15:117db924cf7c 1288
wolfSSL 15:117db924cf7c 1289 /* check response code */
wolfSSL 15:117db924cf7c 1290 ret = (readLen > 0) ? 0 : -1;
wolfSSL 15:117db924cf7c 1291 }
wolfSSL 15:117db924cf7c 1292 }
wolfSSL 15:117db924cf7c 1293 else {
wolfSSL 15:117db924cf7c 1294 ret = BUFFER_E;
wolfSSL 15:117db924cf7c 1295 }
wolfSSL 15:117db924cf7c 1296 fclose(file);
wolfSSL 15:117db924cf7c 1297
wolfSSL 15:117db924cf7c 1298 return ret;
wolfSSL 15:117db924cf7c 1299 }
wolfSSL 15:117db924cf7c 1300
wolfSSL 15:117db924cf7c 1301 enum {
wolfSSL 15:117db924cf7c 1302 WOLFSSL_CA = 1,
wolfSSL 15:117db924cf7c 1303 WOLFSSL_CERT = 2,
wolfSSL 15:117db924cf7c 1304 WOLFSSL_KEY = 3,
wolfSSL 15:117db924cf7c 1305 WOLFSSL_CERT_CHAIN = 4,
wolfSSL 15:117db924cf7c 1306 };
wolfSSL 15:117db924cf7c 1307
wolfSSL 15:117db924cf7c 1308 static WC_INLINE void load_buffer(WOLFSSL_CTX* ctx, const char* fname, int type)
wolfSSL 15:117db924cf7c 1309 {
wolfSSL 15:117db924cf7c 1310 int format = WOLFSSL_FILETYPE_PEM;
wolfSSL 15:117db924cf7c 1311 byte* buff = NULL;
wolfSSL 15:117db924cf7c 1312 size_t sz = 0;
wolfSSL 15:117db924cf7c 1313
wolfSSL 15:117db924cf7c 1314 if (load_file(fname, &buff, &sz) != 0) {
wolfSSL 15:117db924cf7c 1315 err_sys("can't open file for buffer load "
wolfSSL 15:117db924cf7c 1316 "Please run from wolfSSL home directory if not");
wolfSSL 15:117db924cf7c 1317 }
wolfSSL 15:117db924cf7c 1318
wolfSSL 15:117db924cf7c 1319 /* determine format */
wolfSSL 15:117db924cf7c 1320 if (strstr(fname, ".der"))
wolfSSL 15:117db924cf7c 1321 format = WOLFSSL_FILETYPE_ASN1;
wolfSSL 15:117db924cf7c 1322
wolfSSL 15:117db924cf7c 1323 if (type == WOLFSSL_CA) {
wolfSSL 15:117db924cf7c 1324 if (wolfSSL_CTX_load_verify_buffer(ctx, buff, (long)sz, format)
wolfSSL 15:117db924cf7c 1325 != WOLFSSL_SUCCESS)
wolfSSL 15:117db924cf7c 1326 err_sys("can't load buffer ca file");
wolfSSL 15:117db924cf7c 1327 }
wolfSSL 15:117db924cf7c 1328 else if (type == WOLFSSL_CERT) {
wolfSSL 15:117db924cf7c 1329 if (wolfSSL_CTX_use_certificate_buffer(ctx, buff, (long)sz,
wolfSSL 15:117db924cf7c 1330 format) != WOLFSSL_SUCCESS)
wolfSSL 15:117db924cf7c 1331 err_sys("can't load buffer cert file");
wolfSSL 15:117db924cf7c 1332 }
wolfSSL 15:117db924cf7c 1333 else if (type == WOLFSSL_KEY) {
wolfSSL 15:117db924cf7c 1334 if (wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, (long)sz,
wolfSSL 15:117db924cf7c 1335 format) != WOLFSSL_SUCCESS)
wolfSSL 15:117db924cf7c 1336 err_sys("can't load buffer key file");
wolfSSL 15:117db924cf7c 1337 }
wolfSSL 15:117db924cf7c 1338 else if (type == WOLFSSL_CERT_CHAIN) {
wolfSSL 15:117db924cf7c 1339 if (wolfSSL_CTX_use_certificate_chain_buffer_format(ctx, buff,
wolfSSL 15:117db924cf7c 1340 (long)sz, format) != WOLFSSL_SUCCESS)
wolfSSL 15:117db924cf7c 1341 err_sys("can't load cert chain buffer");
wolfSSL 15:117db924cf7c 1342 }
wolfSSL 15:117db924cf7c 1343
wolfSSL 15:117db924cf7c 1344 if (buff)
wolfSSL 15:117db924cf7c 1345 free(buff);
wolfSSL 15:117db924cf7c 1346 }
wolfSSL 15:117db924cf7c 1347
wolfSSL 15:117db924cf7c 1348 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 1349 static WC_INLINE int load_key_file(const char* fname, byte** derBuf, word32* derLen)
wolfSSL 15:117db924cf7c 1350 {
wolfSSL 15:117db924cf7c 1351 int ret;
wolfSSL 15:117db924cf7c 1352 byte* buf = NULL;
wolfSSL 15:117db924cf7c 1353 size_t bufLen;
wolfSSL 15:117db924cf7c 1354
wolfSSL 15:117db924cf7c 1355 ret = load_file(fname, &buf, &bufLen);
wolfSSL 15:117db924cf7c 1356 if (ret != 0)
wolfSSL 15:117db924cf7c 1357 return ret;
wolfSSL 15:117db924cf7c 1358
wolfSSL 15:117db924cf7c 1359 *derBuf = (byte*)malloc(bufLen);
wolfSSL 15:117db924cf7c 1360 if (*derBuf == NULL) {
wolfSSL 15:117db924cf7c 1361 free(buf);
wolfSSL 15:117db924cf7c 1362 return MEMORY_E;
wolfSSL 15:117db924cf7c 1363 }
wolfSSL 15:117db924cf7c 1364
wolfSSL 15:117db924cf7c 1365 ret = wc_KeyPemToDer(buf, (word32)bufLen, *derBuf, (word32)bufLen, NULL);
wolfSSL 15:117db924cf7c 1366 if (ret < 0) {
wolfSSL 15:117db924cf7c 1367 free(buf);
wolfSSL 15:117db924cf7c 1368 free(*derBuf);
wolfSSL 15:117db924cf7c 1369 return ret;
wolfSSL 15:117db924cf7c 1370 }
wolfSSL 15:117db924cf7c 1371 *derLen = ret;
wolfSSL 15:117db924cf7c 1372 free(buf);
wolfSSL 15:117db924cf7c 1373
wolfSSL 15:117db924cf7c 1374 return 0;
wolfSSL 15:117db924cf7c 1375 }
wolfSSL 15:117db924cf7c 1376 #endif /* TEST_PK_PRIVKEY */
wolfSSL 15:117db924cf7c 1377
wolfSSL 15:117db924cf7c 1378 #endif /* !NO_FILESYSTEM || (NO_FILESYSTEM && FORCE_BUFFER_TEST) */
wolfSSL 15:117db924cf7c 1379 #endif /* !NO_CERTS */
wolfSSL 15:117db924cf7c 1380
wolfSSL 15:117db924cf7c 1381 static WC_INLINE int myVerify(int preverify, WOLFSSL_X509_STORE_CTX* store)
wolfSSL 15:117db924cf7c 1382 {
wolfSSL 15:117db924cf7c 1383 char buffer[WOLFSSL_MAX_ERROR_SZ];
wolfSSL 15:117db924cf7c 1384 #ifdef OPENSSL_EXTRA
wolfSSL 15:117db924cf7c 1385 WOLFSSL_X509* peer;
wolfSSL 15:117db924cf7c 1386 #endif
wolfSSL 15:117db924cf7c 1387 (void)preverify;
wolfSSL 15:117db924cf7c 1388
wolfSSL 15:117db924cf7c 1389 printf("In verification callback, error = %d, %s\n", store->error,
wolfSSL 15:117db924cf7c 1390 wolfSSL_ERR_error_string(store->error, buffer));
wolfSSL 15:117db924cf7c 1391 #ifdef OPENSSL_EXTRA
wolfSSL 15:117db924cf7c 1392 peer = store->current_cert;
wolfSSL 15:117db924cf7c 1393 if (peer) {
wolfSSL 15:117db924cf7c 1394 char* issuer = wolfSSL_X509_NAME_oneline(
wolfSSL 15:117db924cf7c 1395 wolfSSL_X509_get_issuer_name(peer), 0, 0);
wolfSSL 15:117db924cf7c 1396 char* subject = wolfSSL_X509_NAME_oneline(
wolfSSL 15:117db924cf7c 1397 wolfSSL_X509_get_subject_name(peer), 0, 0);
wolfSSL 15:117db924cf7c 1398 printf("\tPeer's cert info:\n issuer : %s\n subject: %s\n", issuer,
wolfSSL 15:117db924cf7c 1399 subject);
wolfSSL 15:117db924cf7c 1400 XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
wolfSSL 15:117db924cf7c 1401 XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL);
wolfSSL 15:117db924cf7c 1402 }
wolfSSL 15:117db924cf7c 1403 else
wolfSSL 15:117db924cf7c 1404 printf("\tPeer has no cert!\n");
wolfSSL 15:117db924cf7c 1405 #else
wolfSSL 15:117db924cf7c 1406 printf("\tPeer certs: %d\n", store->totalCerts);
wolfSSL 15:117db924cf7c 1407 #ifdef SHOW_CERTS
wolfSSL 15:117db924cf7c 1408 { int i;
wolfSSL 15:117db924cf7c 1409 for (i=0; i<store->totalCerts; i++) {
wolfSSL 15:117db924cf7c 1410 WOLFSSL_BUFFER_INFO* cert = &store->certs[i];
wolfSSL 15:117db924cf7c 1411 printf("\t\tCert %d: Ptr %p, Len %u\n", i, cert->buffer, cert->length);
wolfSSL 15:117db924cf7c 1412 }
wolfSSL 15:117db924cf7c 1413 }
wolfSSL 15:117db924cf7c 1414 #endif
wolfSSL 15:117db924cf7c 1415 #endif
wolfSSL 15:117db924cf7c 1416
wolfSSL 15:117db924cf7c 1417 printf("\tSubject's domain name is %s\n", store->domain);
wolfSSL 15:117db924cf7c 1418
wolfSSL 15:117db924cf7c 1419 printf("\tAllowing to continue anyway (shouldn't do this, EVER!!!)\n");
wolfSSL 15:117db924cf7c 1420 return 1;
wolfSSL 15:117db924cf7c 1421 }
wolfSSL 15:117db924cf7c 1422
wolfSSL 15:117db924cf7c 1423
wolfSSL 15:117db924cf7c 1424 static WC_INLINE int myDateCb(int preverify, WOLFSSL_X509_STORE_CTX* store)
wolfSSL 15:117db924cf7c 1425 {
wolfSSL 15:117db924cf7c 1426 char buffer[WOLFSSL_MAX_ERROR_SZ];
wolfSSL 15:117db924cf7c 1427 (void)preverify;
wolfSSL 15:117db924cf7c 1428
wolfSSL 15:117db924cf7c 1429 printf("In verification callback, error = %d, %s\n", store->error,
wolfSSL 15:117db924cf7c 1430 wolfSSL_ERR_error_string(store->error, buffer));
wolfSSL 15:117db924cf7c 1431 printf("Subject's domain name is %s\n", store->domain);
wolfSSL 15:117db924cf7c 1432
wolfSSL 15:117db924cf7c 1433 if (store->error == ASN_BEFORE_DATE_E || store->error == ASN_AFTER_DATE_E) {
wolfSSL 15:117db924cf7c 1434 printf("Overriding cert date error as example for bad clock testing\n");
wolfSSL 15:117db924cf7c 1435 return 1;
wolfSSL 15:117db924cf7c 1436 }
wolfSSL 15:117db924cf7c 1437 printf("Cert error is not date error, not overriding\n");
wolfSSL 15:117db924cf7c 1438
wolfSSL 15:117db924cf7c 1439 return 0;
wolfSSL 15:117db924cf7c 1440 }
wolfSSL 15:117db924cf7c 1441
wolfSSL 15:117db924cf7c 1442
wolfSSL 15:117db924cf7c 1443 #ifdef HAVE_EXT_CACHE
wolfSSL 15:117db924cf7c 1444
wolfSSL 15:117db924cf7c 1445 static WC_INLINE WOLFSSL_SESSION* mySessGetCb(WOLFSSL* ssl, unsigned char* id,
wolfSSL 15:117db924cf7c 1446 int id_len, int* copy)
wolfSSL 15:117db924cf7c 1447 {
wolfSSL 15:117db924cf7c 1448 (void)ssl;
wolfSSL 15:117db924cf7c 1449 (void)id;
wolfSSL 15:117db924cf7c 1450 (void)id_len;
wolfSSL 15:117db924cf7c 1451 (void)copy;
wolfSSL 15:117db924cf7c 1452
wolfSSL 15:117db924cf7c 1453 /* using internal cache, this is for testing only */
wolfSSL 15:117db924cf7c 1454 return NULL;
wolfSSL 15:117db924cf7c 1455 }
wolfSSL 15:117db924cf7c 1456
wolfSSL 15:117db924cf7c 1457 static WC_INLINE int mySessNewCb(WOLFSSL* ssl, WOLFSSL_SESSION* session)
wolfSSL 15:117db924cf7c 1458 {
wolfSSL 15:117db924cf7c 1459 (void)ssl;
wolfSSL 15:117db924cf7c 1460 (void)session;
wolfSSL 15:117db924cf7c 1461
wolfSSL 15:117db924cf7c 1462 /* using internal cache, this is for testing only */
wolfSSL 15:117db924cf7c 1463 return 0;
wolfSSL 15:117db924cf7c 1464 }
wolfSSL 15:117db924cf7c 1465
wolfSSL 15:117db924cf7c 1466 static WC_INLINE void mySessRemCb(WOLFSSL_CTX* ctx, WOLFSSL_SESSION* session)
wolfSSL 15:117db924cf7c 1467 {
wolfSSL 15:117db924cf7c 1468 (void)ctx;
wolfSSL 15:117db924cf7c 1469 (void)session;
wolfSSL 15:117db924cf7c 1470
wolfSSL 15:117db924cf7c 1471 /* using internal cache, this is for testing only */
wolfSSL 15:117db924cf7c 1472 }
wolfSSL 15:117db924cf7c 1473
wolfSSL 15:117db924cf7c 1474 #endif /* HAVE_EXT_CACHE */
wolfSSL 15:117db924cf7c 1475
wolfSSL 15:117db924cf7c 1476
wolfSSL 15:117db924cf7c 1477 #ifdef HAVE_CRL
wolfSSL 15:117db924cf7c 1478
wolfSSL 15:117db924cf7c 1479 static WC_INLINE void CRL_CallBack(const char* url)
wolfSSL 15:117db924cf7c 1480 {
wolfSSL 15:117db924cf7c 1481 printf("CRL callback url = %s\n", url);
wolfSSL 15:117db924cf7c 1482 }
wolfSSL 15:117db924cf7c 1483
wolfSSL 15:117db924cf7c 1484 #endif
wolfSSL 15:117db924cf7c 1485
wolfSSL 15:117db924cf7c 1486 #ifndef NO_DH
wolfSSL 15:117db924cf7c 1487 static WC_INLINE void SetDH(WOLFSSL* ssl)
wolfSSL 15:117db924cf7c 1488 {
wolfSSL 15:117db924cf7c 1489 /* dh1024 p */
wolfSSL 15:117db924cf7c 1490 static const unsigned char p[] =
wolfSSL 15:117db924cf7c 1491 {
wolfSSL 15:117db924cf7c 1492 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
wolfSSL 15:117db924cf7c 1493 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
wolfSSL 15:117db924cf7c 1494 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
wolfSSL 15:117db924cf7c 1495 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
wolfSSL 15:117db924cf7c 1496 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
wolfSSL 15:117db924cf7c 1497 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
wolfSSL 15:117db924cf7c 1498 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
wolfSSL 15:117db924cf7c 1499 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
wolfSSL 15:117db924cf7c 1500 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
wolfSSL 15:117db924cf7c 1501 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
wolfSSL 15:117db924cf7c 1502 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
wolfSSL 15:117db924cf7c 1503 };
wolfSSL 15:117db924cf7c 1504
wolfSSL 15:117db924cf7c 1505 /* dh1024 g */
wolfSSL 15:117db924cf7c 1506 static const unsigned char g[] =
wolfSSL 15:117db924cf7c 1507 {
wolfSSL 15:117db924cf7c 1508 0x02,
wolfSSL 15:117db924cf7c 1509 };
wolfSSL 15:117db924cf7c 1510
wolfSSL 15:117db924cf7c 1511 wolfSSL_SetTmpDH(ssl, p, sizeof(p), g, sizeof(g));
wolfSSL 15:117db924cf7c 1512 }
wolfSSL 15:117db924cf7c 1513
wolfSSL 15:117db924cf7c 1514 static WC_INLINE void SetDHCtx(WOLFSSL_CTX* ctx)
wolfSSL 15:117db924cf7c 1515 {
wolfSSL 15:117db924cf7c 1516 /* dh1024 p */
wolfSSL 15:117db924cf7c 1517 static const unsigned char p[] =
wolfSSL 15:117db924cf7c 1518 {
wolfSSL 15:117db924cf7c 1519 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
wolfSSL 15:117db924cf7c 1520 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
wolfSSL 15:117db924cf7c 1521 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
wolfSSL 15:117db924cf7c 1522 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
wolfSSL 15:117db924cf7c 1523 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
wolfSSL 15:117db924cf7c 1524 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
wolfSSL 15:117db924cf7c 1525 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
wolfSSL 15:117db924cf7c 1526 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
wolfSSL 15:117db924cf7c 1527 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
wolfSSL 15:117db924cf7c 1528 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
wolfSSL 15:117db924cf7c 1529 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
wolfSSL 15:117db924cf7c 1530 };
wolfSSL 15:117db924cf7c 1531
wolfSSL 15:117db924cf7c 1532 /* dh1024 g */
wolfSSL 15:117db924cf7c 1533 static const unsigned char g[] =
wolfSSL 15:117db924cf7c 1534 {
wolfSSL 15:117db924cf7c 1535 0x02,
wolfSSL 15:117db924cf7c 1536 };
wolfSSL 15:117db924cf7c 1537
wolfSSL 15:117db924cf7c 1538 wolfSSL_CTX_SetTmpDH(ctx, p, sizeof(p), g, sizeof(g));
wolfSSL 15:117db924cf7c 1539 }
wolfSSL 15:117db924cf7c 1540 #endif /* NO_DH */
wolfSSL 15:117db924cf7c 1541
wolfSSL 15:117db924cf7c 1542 #ifndef NO_CERTS
wolfSSL 15:117db924cf7c 1543
wolfSSL 15:117db924cf7c 1544 static WC_INLINE void CaCb(unsigned char* der, int sz, int type)
wolfSSL 15:117db924cf7c 1545 {
wolfSSL 15:117db924cf7c 1546 (void)der;
wolfSSL 15:117db924cf7c 1547 printf("Got CA cache add callback, derSz = %d, type = %d\n", sz, type);
wolfSSL 15:117db924cf7c 1548 }
wolfSSL 15:117db924cf7c 1549
wolfSSL 15:117db924cf7c 1550 #endif /* !NO_CERTS */
wolfSSL 15:117db924cf7c 1551
wolfSSL 15:117db924cf7c 1552
wolfSSL 15:117db924cf7c 1553 /* Wolf Root Directory Helper */
wolfSSL 15:117db924cf7c 1554 /* KEIL-RL File System does not support relative directory */
wolfSSL 15:117db924cf7c 1555 #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOLFSSL_TIRTOS)
wolfSSL 15:117db924cf7c 1556 /* Maximum depth to search for WolfSSL root */
wolfSSL 15:117db924cf7c 1557 #define MAX_WOLF_ROOT_DEPTH 5
wolfSSL 15:117db924cf7c 1558
wolfSSL 15:117db924cf7c 1559 static WC_INLINE int ChangeToWolfRoot(void)
wolfSSL 15:117db924cf7c 1560 {
wolfSSL 15:117db924cf7c 1561 #if !defined(NO_FILESYSTEM) || defined(FORCE_BUFFER_TEST)
wolfSSL 15:117db924cf7c 1562 int depth, res;
wolfSSL 15:117db924cf7c 1563 FILE* file;
wolfSSL 15:117db924cf7c 1564 for(depth = 0; depth <= MAX_WOLF_ROOT_DEPTH; depth++) {
wolfSSL 15:117db924cf7c 1565 file = fopen(ntruKeyFile, "rb");
wolfSSL 15:117db924cf7c 1566 if (file != NULL) {
wolfSSL 15:117db924cf7c 1567 fclose(file);
wolfSSL 15:117db924cf7c 1568 return depth;
wolfSSL 15:117db924cf7c 1569 }
wolfSSL 15:117db924cf7c 1570 #ifdef USE_WINDOWS_API
wolfSSL 15:117db924cf7c 1571 res = SetCurrentDirectoryA("..\\");
wolfSSL 15:117db924cf7c 1572 #else
wolfSSL 15:117db924cf7c 1573 res = chdir("../");
wolfSSL 15:117db924cf7c 1574 #endif
wolfSSL 15:117db924cf7c 1575 if (res < 0) {
wolfSSL 15:117db924cf7c 1576 printf("chdir to ../ failed!\n");
wolfSSL 15:117db924cf7c 1577 break;
wolfSSL 15:117db924cf7c 1578 }
wolfSSL 15:117db924cf7c 1579 }
wolfSSL 15:117db924cf7c 1580
wolfSSL 15:117db924cf7c 1581 err_sys("wolf root not found");
wolfSSL 15:117db924cf7c 1582 return -1;
wolfSSL 15:117db924cf7c 1583 #else
wolfSSL 15:117db924cf7c 1584 return 0;
wolfSSL 15:117db924cf7c 1585 #endif
wolfSSL 15:117db924cf7c 1586 }
wolfSSL 15:117db924cf7c 1587 #endif /* !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOLFSSL_TIRTOS) */
wolfSSL 15:117db924cf7c 1588
wolfSSL 15:117db924cf7c 1589 #ifdef HAVE_STACK_SIZE
wolfSSL 15:117db924cf7c 1590
wolfSSL 15:117db924cf7c 1591 typedef THREAD_RETURN WOLFSSL_THREAD (*thread_func)(void* args);
wolfSSL 15:117db924cf7c 1592 #define STACK_CHECK_VAL 0x01
wolfSSL 15:117db924cf7c 1593
wolfSSL 15:117db924cf7c 1594 static WC_INLINE int StackSizeCheck(func_args* args, thread_func tf)
wolfSSL 15:117db924cf7c 1595 {
wolfSSL 15:117db924cf7c 1596 int ret, i, used;
wolfSSL 15:117db924cf7c 1597 void* status;
wolfSSL 15:117db924cf7c 1598 unsigned char* myStack = NULL;
wolfSSL 15:117db924cf7c 1599 int stackSize = 1024*128;
wolfSSL 15:117db924cf7c 1600 pthread_attr_t myAttr;
wolfSSL 15:117db924cf7c 1601 pthread_t threadId;
wolfSSL 15:117db924cf7c 1602
wolfSSL 15:117db924cf7c 1603 #ifdef PTHREAD_STACK_MIN
wolfSSL 15:117db924cf7c 1604 if (stackSize < PTHREAD_STACK_MIN)
wolfSSL 15:117db924cf7c 1605 stackSize = PTHREAD_STACK_MIN;
wolfSSL 15:117db924cf7c 1606 #endif
wolfSSL 15:117db924cf7c 1607
wolfSSL 15:117db924cf7c 1608 ret = posix_memalign((void**)&myStack, sysconf(_SC_PAGESIZE), stackSize);
wolfSSL 15:117db924cf7c 1609 if (ret != 0 || myStack == NULL)
wolfSSL 15:117db924cf7c 1610 err_sys("posix_memalign failed\n");
wolfSSL 15:117db924cf7c 1611
wolfSSL 15:117db924cf7c 1612 XMEMSET(myStack, STACK_CHECK_VAL, stackSize);
wolfSSL 15:117db924cf7c 1613
wolfSSL 15:117db924cf7c 1614 ret = pthread_attr_init(&myAttr);
wolfSSL 15:117db924cf7c 1615 if (ret != 0)
wolfSSL 15:117db924cf7c 1616 err_sys("attr_init failed");
wolfSSL 15:117db924cf7c 1617
wolfSSL 15:117db924cf7c 1618 ret = pthread_attr_setstack(&myAttr, myStack, stackSize);
wolfSSL 15:117db924cf7c 1619 if (ret != 0)
wolfSSL 15:117db924cf7c 1620 err_sys("attr_setstackaddr failed");
wolfSSL 15:117db924cf7c 1621
wolfSSL 15:117db924cf7c 1622 ret = pthread_create(&threadId, &myAttr, tf, args);
wolfSSL 15:117db924cf7c 1623 if (ret != 0) {
wolfSSL 15:117db924cf7c 1624 perror("pthread_create failed");
wolfSSL 15:117db924cf7c 1625 exit(EXIT_FAILURE);
wolfSSL 15:117db924cf7c 1626 }
wolfSSL 15:117db924cf7c 1627
wolfSSL 15:117db924cf7c 1628 ret = pthread_join(threadId, &status);
wolfSSL 15:117db924cf7c 1629 if (ret != 0)
wolfSSL 15:117db924cf7c 1630 err_sys("pthread_join failed");
wolfSSL 15:117db924cf7c 1631
wolfSSL 15:117db924cf7c 1632 for (i = 0; i < stackSize; i++) {
wolfSSL 15:117db924cf7c 1633 if (myStack[i] != STACK_CHECK_VAL) {
wolfSSL 15:117db924cf7c 1634 break;
wolfSSL 15:117db924cf7c 1635 }
wolfSSL 15:117db924cf7c 1636 }
wolfSSL 15:117db924cf7c 1637
wolfSSL 15:117db924cf7c 1638 free(myStack);
wolfSSL 15:117db924cf7c 1639
wolfSSL 15:117db924cf7c 1640 used = stackSize - i;
wolfSSL 15:117db924cf7c 1641 printf("stack used = %d\n", used);
wolfSSL 15:117db924cf7c 1642
wolfSSL 15:117db924cf7c 1643 return (int)((size_t)status);
wolfSSL 15:117db924cf7c 1644 }
wolfSSL 15:117db924cf7c 1645
wolfSSL 15:117db924cf7c 1646
wolfSSL 15:117db924cf7c 1647 #endif /* HAVE_STACK_SIZE */
wolfSSL 15:117db924cf7c 1648
wolfSSL 15:117db924cf7c 1649
wolfSSL 15:117db924cf7c 1650 #ifdef STACK_TRAP
wolfSSL 15:117db924cf7c 1651
wolfSSL 15:117db924cf7c 1652 /* good settings
wolfSSL 15:117db924cf7c 1653 --enable-debug --disable-shared C_EXTRA_FLAGS="-DUSER_TIME -DTFM_TIMING_RESISTANT -DPOSITIVE_EXP_ONLY -DSTACK_TRAP"
wolfSSL 15:117db924cf7c 1654
wolfSSL 15:117db924cf7c 1655 */
wolfSSL 15:117db924cf7c 1656
wolfSSL 15:117db924cf7c 1657 #ifdef HAVE_STACK_SIZE
wolfSSL 15:117db924cf7c 1658 /* client only for now, setrlimit will fail if pthread_create() called */
wolfSSL 15:117db924cf7c 1659 /* STACK_SIZE does pthread_create() on client */
wolfSSL 15:117db924cf7c 1660 #error "can't use STACK_TRAP with STACK_SIZE, setrlimit will fail"
wolfSSL 15:117db924cf7c 1661 #endif /* HAVE_STACK_SIZE */
wolfSSL 15:117db924cf7c 1662
wolfSSL 15:117db924cf7c 1663 static WC_INLINE void StackTrap(void)
wolfSSL 15:117db924cf7c 1664 {
wolfSSL 15:117db924cf7c 1665 struct rlimit rl;
wolfSSL 15:117db924cf7c 1666 if (getrlimit(RLIMIT_STACK, &rl) != 0)
wolfSSL 15:117db924cf7c 1667 err_sys("getrlimit failed");
wolfSSL 15:117db924cf7c 1668 printf("rlim_cur = %llu\n", rl.rlim_cur);
wolfSSL 15:117db924cf7c 1669 rl.rlim_cur = 1024*21; /* adjust trap size here */
wolfSSL 15:117db924cf7c 1670 if (setrlimit(RLIMIT_STACK, &rl) != 0) {
wolfSSL 15:117db924cf7c 1671 perror("setrlimit");
wolfSSL 15:117db924cf7c 1672 err_sys("setrlimit failed");
wolfSSL 15:117db924cf7c 1673 }
wolfSSL 15:117db924cf7c 1674 }
wolfSSL 15:117db924cf7c 1675
wolfSSL 15:117db924cf7c 1676 #else /* STACK_TRAP */
wolfSSL 15:117db924cf7c 1677
wolfSSL 15:117db924cf7c 1678 static WC_INLINE void StackTrap(void)
wolfSSL 15:117db924cf7c 1679 {
wolfSSL 15:117db924cf7c 1680 }
wolfSSL 15:117db924cf7c 1681
wolfSSL 15:117db924cf7c 1682 #endif /* STACK_TRAP */
wolfSSL 15:117db924cf7c 1683
wolfSSL 15:117db924cf7c 1684
wolfSSL 15:117db924cf7c 1685 #ifdef ATOMIC_USER
wolfSSL 15:117db924cf7c 1686
wolfSSL 15:117db924cf7c 1687 /* Atomic Encrypt Context example */
wolfSSL 15:117db924cf7c 1688 typedef struct AtomicEncCtx {
wolfSSL 15:117db924cf7c 1689 int keySetup; /* have we done key setup yet */
wolfSSL 15:117db924cf7c 1690 Aes aes; /* for aes example */
wolfSSL 15:117db924cf7c 1691 } AtomicEncCtx;
wolfSSL 15:117db924cf7c 1692
wolfSSL 15:117db924cf7c 1693
wolfSSL 15:117db924cf7c 1694 /* Atomic Decrypt Context example */
wolfSSL 15:117db924cf7c 1695 typedef struct AtomicDecCtx {
wolfSSL 15:117db924cf7c 1696 int keySetup; /* have we done key setup yet */
wolfSSL 15:117db924cf7c 1697 Aes aes; /* for aes example */
wolfSSL 15:117db924cf7c 1698 } AtomicDecCtx;
wolfSSL 15:117db924cf7c 1699
wolfSSL 15:117db924cf7c 1700
wolfSSL 15:117db924cf7c 1701 static WC_INLINE int myMacEncryptCb(WOLFSSL* ssl, unsigned char* macOut,
wolfSSL 15:117db924cf7c 1702 const unsigned char* macIn, unsigned int macInSz, int macContent,
wolfSSL 15:117db924cf7c 1703 int macVerify, unsigned char* encOut, const unsigned char* encIn,
wolfSSL 15:117db924cf7c 1704 unsigned int encSz, void* ctx)
wolfSSL 15:117db924cf7c 1705 {
wolfSSL 15:117db924cf7c 1706 int ret;
wolfSSL 15:117db924cf7c 1707 Hmac hmac;
wolfSSL 15:117db924cf7c 1708 byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
wolfSSL 15:117db924cf7c 1709 AtomicEncCtx* encCtx = (AtomicEncCtx*)ctx;
wolfSSL 15:117db924cf7c 1710 const char* tlsStr = "TLS";
wolfSSL 15:117db924cf7c 1711
wolfSSL 15:117db924cf7c 1712 /* example supports (d)tls aes */
wolfSSL 15:117db924cf7c 1713 if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) {
wolfSSL 15:117db924cf7c 1714 printf("myMacEncryptCb not using AES\n");
wolfSSL 15:117db924cf7c 1715 return -1;
wolfSSL 15:117db924cf7c 1716 }
wolfSSL 15:117db924cf7c 1717
wolfSSL 15:117db924cf7c 1718 if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) {
wolfSSL 15:117db924cf7c 1719 printf("myMacEncryptCb not using (D)TLS\n");
wolfSSL 15:117db924cf7c 1720 return -1;
wolfSSL 15:117db924cf7c 1721 }
wolfSSL 15:117db924cf7c 1722
wolfSSL 15:117db924cf7c 1723 /* hmac, not needed if aead mode */
wolfSSL 15:117db924cf7c 1724 wolfSSL_SetTlsHmacInner(ssl, myInner, macInSz, macContent, macVerify);
wolfSSL 15:117db924cf7c 1725
wolfSSL 15:117db924cf7c 1726 ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
wolfSSL 15:117db924cf7c 1727 wolfSSL_GetMacSecret(ssl, macVerify), wolfSSL_GetHmacSize(ssl));
wolfSSL 15:117db924cf7c 1728 if (ret != 0)
wolfSSL 15:117db924cf7c 1729 return ret;
wolfSSL 15:117db924cf7c 1730 ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
wolfSSL 15:117db924cf7c 1731 if (ret != 0)
wolfSSL 15:117db924cf7c 1732 return ret;
wolfSSL 15:117db924cf7c 1733 ret = wc_HmacUpdate(&hmac, macIn, macInSz);
wolfSSL 15:117db924cf7c 1734 if (ret != 0)
wolfSSL 15:117db924cf7c 1735 return ret;
wolfSSL 15:117db924cf7c 1736 ret = wc_HmacFinal(&hmac, macOut);
wolfSSL 15:117db924cf7c 1737 if (ret != 0)
wolfSSL 15:117db924cf7c 1738 return ret;
wolfSSL 15:117db924cf7c 1739
wolfSSL 15:117db924cf7c 1740
wolfSSL 15:117db924cf7c 1741 /* encrypt setup on first time */
wolfSSL 15:117db924cf7c 1742 if (encCtx->keySetup == 0) {
wolfSSL 15:117db924cf7c 1743 int keyLen = wolfSSL_GetKeySize(ssl);
wolfSSL 15:117db924cf7c 1744 const byte* key;
wolfSSL 15:117db924cf7c 1745 const byte* iv;
wolfSSL 15:117db924cf7c 1746
wolfSSL 15:117db924cf7c 1747 if (wolfSSL_GetSide(ssl) == WOLFSSL_CLIENT_END) {
wolfSSL 15:117db924cf7c 1748 key = wolfSSL_GetClientWriteKey(ssl);
wolfSSL 15:117db924cf7c 1749 iv = wolfSSL_GetClientWriteIV(ssl);
wolfSSL 15:117db924cf7c 1750 }
wolfSSL 15:117db924cf7c 1751 else {
wolfSSL 15:117db924cf7c 1752 key = wolfSSL_GetServerWriteKey(ssl);
wolfSSL 15:117db924cf7c 1753 iv = wolfSSL_GetServerWriteIV(ssl);
wolfSSL 15:117db924cf7c 1754 }
wolfSSL 15:117db924cf7c 1755
wolfSSL 15:117db924cf7c 1756 ret = wc_AesSetKey(&encCtx->aes, key, keyLen, iv, AES_ENCRYPTION);
wolfSSL 15:117db924cf7c 1757 if (ret != 0) {
wolfSSL 15:117db924cf7c 1758 printf("AesSetKey failed in myMacEncryptCb\n");
wolfSSL 15:117db924cf7c 1759 return ret;
wolfSSL 15:117db924cf7c 1760 }
wolfSSL 15:117db924cf7c 1761 encCtx->keySetup = 1;
wolfSSL 15:117db924cf7c 1762 }
wolfSSL 15:117db924cf7c 1763
wolfSSL 15:117db924cf7c 1764 /* encrypt */
wolfSSL 15:117db924cf7c 1765 return wc_AesCbcEncrypt(&encCtx->aes, encOut, encIn, encSz);
wolfSSL 15:117db924cf7c 1766 }
wolfSSL 15:117db924cf7c 1767
wolfSSL 15:117db924cf7c 1768
wolfSSL 15:117db924cf7c 1769 static WC_INLINE int myDecryptVerifyCb(WOLFSSL* ssl,
wolfSSL 15:117db924cf7c 1770 unsigned char* decOut, const unsigned char* decIn,
wolfSSL 15:117db924cf7c 1771 unsigned int decSz, int macContent, int macVerify,
wolfSSL 15:117db924cf7c 1772 unsigned int* padSz, void* ctx)
wolfSSL 15:117db924cf7c 1773 {
wolfSSL 15:117db924cf7c 1774 AtomicDecCtx* decCtx = (AtomicDecCtx*)ctx;
wolfSSL 15:117db924cf7c 1775 int ret = 0;
wolfSSL 15:117db924cf7c 1776 int macInSz = 0;
wolfSSL 15:117db924cf7c 1777 int ivExtra = 0;
wolfSSL 15:117db924cf7c 1778 int digestSz = wolfSSL_GetHmacSize(ssl);
wolfSSL 15:117db924cf7c 1779 unsigned int pad = 0;
wolfSSL 15:117db924cf7c 1780 unsigned int padByte = 0;
wolfSSL 15:117db924cf7c 1781 Hmac hmac;
wolfSSL 15:117db924cf7c 1782 byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
wolfSSL 15:117db924cf7c 1783 byte verify[WC_MAX_DIGEST_SIZE];
wolfSSL 15:117db924cf7c 1784 const char* tlsStr = "TLS";
wolfSSL 15:117db924cf7c 1785
wolfSSL 15:117db924cf7c 1786 /* example supports (d)tls aes */
wolfSSL 15:117db924cf7c 1787 if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) {
wolfSSL 15:117db924cf7c 1788 printf("myMacEncryptCb not using AES\n");
wolfSSL 15:117db924cf7c 1789 return -1;
wolfSSL 15:117db924cf7c 1790 }
wolfSSL 15:117db924cf7c 1791
wolfSSL 15:117db924cf7c 1792 if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) {
wolfSSL 15:117db924cf7c 1793 printf("myMacEncryptCb not using (D)TLS\n");
wolfSSL 15:117db924cf7c 1794 return -1;
wolfSSL 15:117db924cf7c 1795 }
wolfSSL 15:117db924cf7c 1796
wolfSSL 15:117db924cf7c 1797 /*decrypt */
wolfSSL 15:117db924cf7c 1798 if (decCtx->keySetup == 0) {
wolfSSL 15:117db924cf7c 1799 int keyLen = wolfSSL_GetKeySize(ssl);
wolfSSL 15:117db924cf7c 1800 const byte* key;
wolfSSL 15:117db924cf7c 1801 const byte* iv;
wolfSSL 15:117db924cf7c 1802
wolfSSL 15:117db924cf7c 1803 /* decrypt is from other side (peer) */
wolfSSL 15:117db924cf7c 1804 if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
wolfSSL 15:117db924cf7c 1805 key = wolfSSL_GetClientWriteKey(ssl);
wolfSSL 15:117db924cf7c 1806 iv = wolfSSL_GetClientWriteIV(ssl);
wolfSSL 15:117db924cf7c 1807 }
wolfSSL 15:117db924cf7c 1808 else {
wolfSSL 15:117db924cf7c 1809 key = wolfSSL_GetServerWriteKey(ssl);
wolfSSL 15:117db924cf7c 1810 iv = wolfSSL_GetServerWriteIV(ssl);
wolfSSL 15:117db924cf7c 1811 }
wolfSSL 15:117db924cf7c 1812
wolfSSL 15:117db924cf7c 1813 ret = wc_AesSetKey(&decCtx->aes, key, keyLen, iv, AES_DECRYPTION);
wolfSSL 15:117db924cf7c 1814 if (ret != 0) {
wolfSSL 15:117db924cf7c 1815 printf("AesSetKey failed in myDecryptVerifyCb\n");
wolfSSL 15:117db924cf7c 1816 return ret;
wolfSSL 15:117db924cf7c 1817 }
wolfSSL 15:117db924cf7c 1818 decCtx->keySetup = 1;
wolfSSL 15:117db924cf7c 1819 }
wolfSSL 15:117db924cf7c 1820
wolfSSL 15:117db924cf7c 1821 /* decrypt */
wolfSSL 15:117db924cf7c 1822 ret = wc_AesCbcDecrypt(&decCtx->aes, decOut, decIn, decSz);
wolfSSL 15:117db924cf7c 1823 if (ret != 0)
wolfSSL 15:117db924cf7c 1824 return ret;
wolfSSL 15:117db924cf7c 1825
wolfSSL 15:117db924cf7c 1826 if (wolfSSL_GetCipherType(ssl) == WOLFSSL_AEAD_TYPE) {
wolfSSL 15:117db924cf7c 1827 *padSz = wolfSSL_GetAeadMacSize(ssl);
wolfSSL 15:117db924cf7c 1828 return 0; /* hmac, not needed if aead mode */
wolfSSL 15:117db924cf7c 1829 }
wolfSSL 15:117db924cf7c 1830
wolfSSL 15:117db924cf7c 1831 if (wolfSSL_GetCipherType(ssl) == WOLFSSL_BLOCK_TYPE) {
wolfSSL 15:117db924cf7c 1832 pad = *(decOut + decSz - 1);
wolfSSL 15:117db924cf7c 1833 padByte = 1;
wolfSSL 15:117db924cf7c 1834 if (wolfSSL_IsTLSv1_1(ssl))
wolfSSL 15:117db924cf7c 1835 ivExtra = wolfSSL_GetCipherBlockSize(ssl);
wolfSSL 15:117db924cf7c 1836 }
wolfSSL 15:117db924cf7c 1837
wolfSSL 15:117db924cf7c 1838 *padSz = wolfSSL_GetHmacSize(ssl) + pad + padByte;
wolfSSL 15:117db924cf7c 1839 macInSz = decSz - ivExtra - digestSz - pad - padByte;
wolfSSL 15:117db924cf7c 1840
wolfSSL 15:117db924cf7c 1841 wolfSSL_SetTlsHmacInner(ssl, myInner, macInSz, macContent, macVerify);
wolfSSL 15:117db924cf7c 1842
wolfSSL 15:117db924cf7c 1843 ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
wolfSSL 15:117db924cf7c 1844 wolfSSL_GetMacSecret(ssl, macVerify), digestSz);
wolfSSL 15:117db924cf7c 1845 if (ret != 0)
wolfSSL 15:117db924cf7c 1846 return ret;
wolfSSL 15:117db924cf7c 1847 ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
wolfSSL 15:117db924cf7c 1848 if (ret != 0)
wolfSSL 15:117db924cf7c 1849 return ret;
wolfSSL 15:117db924cf7c 1850 ret = wc_HmacUpdate(&hmac, decOut + ivExtra, macInSz);
wolfSSL 15:117db924cf7c 1851 if (ret != 0)
wolfSSL 15:117db924cf7c 1852 return ret;
wolfSSL 15:117db924cf7c 1853 ret = wc_HmacFinal(&hmac, verify);
wolfSSL 15:117db924cf7c 1854 if (ret != 0)
wolfSSL 15:117db924cf7c 1855 return ret;
wolfSSL 15:117db924cf7c 1856
wolfSSL 15:117db924cf7c 1857 if (XMEMCMP(verify, decOut + decSz - digestSz - pad - padByte,
wolfSSL 15:117db924cf7c 1858 digestSz) != 0) {
wolfSSL 15:117db924cf7c 1859 printf("myDecryptVerify verify failed\n");
wolfSSL 15:117db924cf7c 1860 return -1;
wolfSSL 15:117db924cf7c 1861 }
wolfSSL 15:117db924cf7c 1862
wolfSSL 15:117db924cf7c 1863 return ret;
wolfSSL 15:117db924cf7c 1864 }
wolfSSL 15:117db924cf7c 1865
wolfSSL 15:117db924cf7c 1866
wolfSSL 15:117db924cf7c 1867 static WC_INLINE void SetupAtomicUser(WOLFSSL_CTX* ctx, WOLFSSL* ssl)
wolfSSL 15:117db924cf7c 1868 {
wolfSSL 15:117db924cf7c 1869 AtomicEncCtx* encCtx;
wolfSSL 15:117db924cf7c 1870 AtomicDecCtx* decCtx;
wolfSSL 15:117db924cf7c 1871
wolfSSL 15:117db924cf7c 1872 encCtx = (AtomicEncCtx*)malloc(sizeof(AtomicEncCtx));
wolfSSL 15:117db924cf7c 1873 if (encCtx == NULL)
wolfSSL 15:117db924cf7c 1874 err_sys("AtomicEncCtx malloc failed");
wolfSSL 15:117db924cf7c 1875 XMEMSET(encCtx, 0, sizeof(AtomicEncCtx));
wolfSSL 15:117db924cf7c 1876
wolfSSL 15:117db924cf7c 1877 decCtx = (AtomicDecCtx*)malloc(sizeof(AtomicDecCtx));
wolfSSL 15:117db924cf7c 1878 if (decCtx == NULL) {
wolfSSL 15:117db924cf7c 1879 free(encCtx);
wolfSSL 15:117db924cf7c 1880 err_sys("AtomicDecCtx malloc failed");
wolfSSL 15:117db924cf7c 1881 }
wolfSSL 15:117db924cf7c 1882 XMEMSET(decCtx, 0, sizeof(AtomicDecCtx));
wolfSSL 15:117db924cf7c 1883
wolfSSL 15:117db924cf7c 1884 wolfSSL_CTX_SetMacEncryptCb(ctx, myMacEncryptCb);
wolfSSL 15:117db924cf7c 1885 wolfSSL_SetMacEncryptCtx(ssl, encCtx);
wolfSSL 15:117db924cf7c 1886
wolfSSL 15:117db924cf7c 1887 wolfSSL_CTX_SetDecryptVerifyCb(ctx, myDecryptVerifyCb);
wolfSSL 15:117db924cf7c 1888 wolfSSL_SetDecryptVerifyCtx(ssl, decCtx);
wolfSSL 15:117db924cf7c 1889 }
wolfSSL 15:117db924cf7c 1890
wolfSSL 15:117db924cf7c 1891
wolfSSL 15:117db924cf7c 1892 static WC_INLINE void FreeAtomicUser(WOLFSSL* ssl)
wolfSSL 15:117db924cf7c 1893 {
wolfSSL 15:117db924cf7c 1894 AtomicEncCtx* encCtx = (AtomicEncCtx*)wolfSSL_GetMacEncryptCtx(ssl);
wolfSSL 15:117db924cf7c 1895 AtomicDecCtx* decCtx = (AtomicDecCtx*)wolfSSL_GetDecryptVerifyCtx(ssl);
wolfSSL 15:117db924cf7c 1896
wolfSSL 15:117db924cf7c 1897 free(decCtx);
wolfSSL 15:117db924cf7c 1898 free(encCtx);
wolfSSL 15:117db924cf7c 1899 }
wolfSSL 15:117db924cf7c 1900
wolfSSL 15:117db924cf7c 1901 #endif /* ATOMIC_USER */
wolfSSL 15:117db924cf7c 1902
wolfSSL 15:117db924cf7c 1903 #ifdef WOLFSSL_STATIC_MEMORY
wolfSSL 15:117db924cf7c 1904 static WC_INLINE int wolfSSL_PrintStats(WOLFSSL_MEM_STATS* stats)
wolfSSL 15:117db924cf7c 1905 {
wolfSSL 15:117db924cf7c 1906 word16 i;
wolfSSL 15:117db924cf7c 1907
wolfSSL 15:117db924cf7c 1908 if (stats == NULL) {
wolfSSL 15:117db924cf7c 1909 return 0;
wolfSSL 15:117db924cf7c 1910 }
wolfSSL 15:117db924cf7c 1911
wolfSSL 15:117db924cf7c 1912 /* print to stderr so is on the same pipe as WOLFSSL_DEBUG */
wolfSSL 15:117db924cf7c 1913 fprintf(stderr, "Total mallocs = %d\n", stats->totalAlloc);
wolfSSL 15:117db924cf7c 1914 fprintf(stderr, "Total frees = %d\n", stats->totalFr);
wolfSSL 15:117db924cf7c 1915 fprintf(stderr, "Current mallocs = %d\n", stats->curAlloc);
wolfSSL 15:117db924cf7c 1916 fprintf(stderr, "Available IO = %d\n", stats->avaIO);
wolfSSL 15:117db924cf7c 1917 fprintf(stderr, "Max con. handshakes = %d\n", stats->maxHa);
wolfSSL 15:117db924cf7c 1918 fprintf(stderr, "Max con. IO = %d\n", stats->maxIO);
wolfSSL 15:117db924cf7c 1919 fprintf(stderr, "State of memory blocks: size : available \n");
wolfSSL 15:117db924cf7c 1920 for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
wolfSSL 15:117db924cf7c 1921 fprintf(stderr, " : %d\t : %d\n", stats->blockSz[i],
wolfSSL 15:117db924cf7c 1922 stats->avaBlock[i]);
wolfSSL 15:117db924cf7c 1923 }
wolfSSL 15:117db924cf7c 1924
wolfSSL 15:117db924cf7c 1925 return 1;
wolfSSL 15:117db924cf7c 1926 }
wolfSSL 15:117db924cf7c 1927 #endif /* WOLFSSL_STATIC_MEMORY */
wolfSSL 15:117db924cf7c 1928
wolfSSL 15:117db924cf7c 1929 #ifdef HAVE_PK_CALLBACKS
wolfSSL 15:117db924cf7c 1930
wolfSSL 15:117db924cf7c 1931 typedef struct PkCbInfo {
wolfSSL 15:117db924cf7c 1932 const char* ourKey;
wolfSSL 15:117db924cf7c 1933 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 1934 union {
wolfSSL 15:117db924cf7c 1935 #ifdef HAVE_ECC
wolfSSL 15:117db924cf7c 1936 ecc_key ecc;
wolfSSL 15:117db924cf7c 1937 #endif
wolfSSL 15:117db924cf7c 1938 #ifdef HAVE_CURVE25519
wolfSSL 15:117db924cf7c 1939 curve25519_key curve;
wolfSSL 15:117db924cf7c 1940 #endif
wolfSSL 15:117db924cf7c 1941 } keyGen;
wolfSSL 15:117db924cf7c 1942 #endif
wolfSSL 15:117db924cf7c 1943 } PkCbInfo;
wolfSSL 15:117db924cf7c 1944
wolfSSL 15:117db924cf7c 1945 #ifdef HAVE_ECC
wolfSSL 15:117db924cf7c 1946
wolfSSL 15:117db924cf7c 1947 static WC_INLINE int myEccKeyGen(WOLFSSL* ssl, ecc_key* key, word32 keySz,
wolfSSL 15:117db924cf7c 1948 int ecc_curve, void* ctx)
wolfSSL 15:117db924cf7c 1949 {
wolfSSL 15:117db924cf7c 1950 int ret;
wolfSSL 15:117db924cf7c 1951 WC_RNG rng;
wolfSSL 15:117db924cf7c 1952 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 1953 ecc_key* new_key = key;
wolfSSL 15:117db924cf7c 1954 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 1955 byte qx[MAX_ECC_BYTES], qy[MAX_ECC_BYTES];
wolfSSL 15:117db924cf7c 1956 word32 qxLen = sizeof(qx), qyLen = sizeof(qy);
wolfSSL 15:117db924cf7c 1957 new_key = &cbInfo->keyGen.ecc;
wolfSSL 15:117db924cf7c 1958 #endif
wolfSSL 15:117db924cf7c 1959
wolfSSL 15:117db924cf7c 1960 (void)ssl;
wolfSSL 15:117db924cf7c 1961 (void)cbInfo;
wolfSSL 15:117db924cf7c 1962
wolfSSL 15:117db924cf7c 1963 ret = wc_InitRng(&rng);
wolfSSL 15:117db924cf7c 1964 if (ret != 0)
wolfSSL 15:117db924cf7c 1965 return ret;
wolfSSL 15:117db924cf7c 1966
wolfSSL 15:117db924cf7c 1967 ret = wc_ecc_init(new_key);
wolfSSL 15:117db924cf7c 1968 if (ret == 0) {
wolfSSL 15:117db924cf7c 1969 /* create new key */
wolfSSL 15:117db924cf7c 1970 ret = wc_ecc_make_key_ex(&rng, keySz, new_key, ecc_curve);
wolfSSL 15:117db924cf7c 1971
wolfSSL 15:117db924cf7c 1972 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 1973 if (ret == 0) {
wolfSSL 15:117db924cf7c 1974 /* extract public portion from new key into `key` arg */
wolfSSL 15:117db924cf7c 1975 ret = wc_ecc_export_public_raw(new_key, qx, &qxLen, qy, &qyLen);
wolfSSL 15:117db924cf7c 1976 if (ret == 0) {
wolfSSL 15:117db924cf7c 1977 /* load public portion only into key */
wolfSSL 15:117db924cf7c 1978 ret = wc_ecc_import_unsigned(key, qx, qy, NULL, ecc_curve);
wolfSSL 15:117db924cf7c 1979 }
wolfSSL 15:117db924cf7c 1980 (void)qxLen;
wolfSSL 15:117db924cf7c 1981 (void)qyLen;
wolfSSL 15:117db924cf7c 1982 }
wolfSSL 15:117db924cf7c 1983 #endif
wolfSSL 15:117db924cf7c 1984 }
wolfSSL 15:117db924cf7c 1985
wolfSSL 15:117db924cf7c 1986 wc_FreeRng(&rng);
wolfSSL 15:117db924cf7c 1987
wolfSSL 15:117db924cf7c 1988 return ret;
wolfSSL 15:117db924cf7c 1989 }
wolfSSL 15:117db924cf7c 1990
wolfSSL 15:117db924cf7c 1991 static WC_INLINE int myEccSign(WOLFSSL* ssl, const byte* in, word32 inSz,
wolfSSL 15:117db924cf7c 1992 byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx)
wolfSSL 15:117db924cf7c 1993 {
wolfSSL 15:117db924cf7c 1994 int ret;
wolfSSL 15:117db924cf7c 1995 WC_RNG rng;
wolfSSL 15:117db924cf7c 1996 word32 idx = 0;
wolfSSL 15:117db924cf7c 1997 ecc_key myKey;
wolfSSL 15:117db924cf7c 1998 byte* keyBuf = (byte*)key;
wolfSSL 15:117db924cf7c 1999 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2000
wolfSSL 15:117db924cf7c 2001 (void)ssl;
wolfSSL 15:117db924cf7c 2002 (void)cbInfo;
wolfSSL 15:117db924cf7c 2003
wolfSSL 15:117db924cf7c 2004 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2005 ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
wolfSSL 15:117db924cf7c 2006 if (ret != 0)
wolfSSL 15:117db924cf7c 2007 return ret;
wolfSSL 15:117db924cf7c 2008 #endif
wolfSSL 15:117db924cf7c 2009
wolfSSL 15:117db924cf7c 2010 ret = wc_InitRng(&rng);
wolfSSL 15:117db924cf7c 2011 if (ret != 0)
wolfSSL 15:117db924cf7c 2012 return ret;
wolfSSL 15:117db924cf7c 2013
wolfSSL 15:117db924cf7c 2014 ret = wc_ecc_init(&myKey);
wolfSSL 15:117db924cf7c 2015 if (ret == 0) {
wolfSSL 15:117db924cf7c 2016 ret = wc_EccPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
wolfSSL 15:117db924cf7c 2017 if (ret == 0)
wolfSSL 15:117db924cf7c 2018 ret = wc_ecc_sign_hash(in, inSz, out, outSz, &rng, &myKey);
wolfSSL 15:117db924cf7c 2019 wc_ecc_free(&myKey);
wolfSSL 15:117db924cf7c 2020 }
wolfSSL 15:117db924cf7c 2021 wc_FreeRng(&rng);
wolfSSL 15:117db924cf7c 2022
wolfSSL 15:117db924cf7c 2023 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2024 free(keyBuf);
wolfSSL 15:117db924cf7c 2025 #endif
wolfSSL 15:117db924cf7c 2026
wolfSSL 15:117db924cf7c 2027 return ret;
wolfSSL 15:117db924cf7c 2028 }
wolfSSL 15:117db924cf7c 2029
wolfSSL 15:117db924cf7c 2030
wolfSSL 15:117db924cf7c 2031 static WC_INLINE int myEccVerify(WOLFSSL* ssl, const byte* sig, word32 sigSz,
wolfSSL 15:117db924cf7c 2032 const byte* hash, word32 hashSz, const byte* key, word32 keySz,
wolfSSL 15:117db924cf7c 2033 int* result, void* ctx)
wolfSSL 15:117db924cf7c 2034 {
wolfSSL 15:117db924cf7c 2035 int ret;
wolfSSL 15:117db924cf7c 2036 word32 idx = 0;
wolfSSL 15:117db924cf7c 2037 ecc_key myKey;
wolfSSL 15:117db924cf7c 2038 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2039
wolfSSL 15:117db924cf7c 2040 (void)ssl;
wolfSSL 15:117db924cf7c 2041 (void)cbInfo;
wolfSSL 15:117db924cf7c 2042
wolfSSL 15:117db924cf7c 2043 ret = wc_ecc_init(&myKey);
wolfSSL 15:117db924cf7c 2044 if (ret == 0) {
wolfSSL 15:117db924cf7c 2045 ret = wc_EccPublicKeyDecode(key, &idx, &myKey, keySz);
wolfSSL 15:117db924cf7c 2046 if (ret == 0)
wolfSSL 15:117db924cf7c 2047 ret = wc_ecc_verify_hash(sig, sigSz, hash, hashSz, result, &myKey);
wolfSSL 15:117db924cf7c 2048 wc_ecc_free(&myKey);
wolfSSL 15:117db924cf7c 2049 }
wolfSSL 15:117db924cf7c 2050
wolfSSL 15:117db924cf7c 2051 return ret;
wolfSSL 15:117db924cf7c 2052 }
wolfSSL 15:117db924cf7c 2053
wolfSSL 15:117db924cf7c 2054 static WC_INLINE int myEccSharedSecret(WOLFSSL* ssl, ecc_key* otherKey,
wolfSSL 15:117db924cf7c 2055 unsigned char* pubKeyDer, unsigned int* pubKeySz,
wolfSSL 15:117db924cf7c 2056 unsigned char* out, unsigned int* outlen,
wolfSSL 15:117db924cf7c 2057 int side, void* ctx)
wolfSSL 15:117db924cf7c 2058 {
wolfSSL 15:117db924cf7c 2059 int ret;
wolfSSL 15:117db924cf7c 2060 ecc_key* privKey = NULL;
wolfSSL 15:117db924cf7c 2061 ecc_key* pubKey = NULL;
wolfSSL 15:117db924cf7c 2062 ecc_key tmpKey;
wolfSSL 15:117db924cf7c 2063 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2064
wolfSSL 15:117db924cf7c 2065 (void)ssl;
wolfSSL 15:117db924cf7c 2066 (void)cbInfo;
wolfSSL 15:117db924cf7c 2067
wolfSSL 15:117db924cf7c 2068 ret = wc_ecc_init(&tmpKey);
wolfSSL 15:117db924cf7c 2069 if (ret != 0) {
wolfSSL 15:117db924cf7c 2070 return ret;
wolfSSL 15:117db924cf7c 2071 }
wolfSSL 15:117db924cf7c 2072
wolfSSL 15:117db924cf7c 2073 /* for client: create and export public key */
wolfSSL 15:117db924cf7c 2074 if (side == WOLFSSL_CLIENT_END) {
wolfSSL 15:117db924cf7c 2075 WC_RNG rng;
wolfSSL 15:117db924cf7c 2076
wolfSSL 15:117db924cf7c 2077 privKey = &tmpKey;
wolfSSL 15:117db924cf7c 2078 pubKey = otherKey;
wolfSSL 15:117db924cf7c 2079
wolfSSL 15:117db924cf7c 2080 ret = wc_InitRng(&rng);
wolfSSL 15:117db924cf7c 2081 if (ret == 0) {
wolfSSL 15:117db924cf7c 2082 ret = wc_ecc_make_key_ex(&rng, 0, privKey, otherKey->dp->id);
wolfSSL 15:117db924cf7c 2083 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 15:117db924cf7c 2084 if (ret == WC_PENDING_E) {
wolfSSL 15:117db924cf7c 2085 ret = wc_AsyncWait(ret, &privKey->asyncDev, WC_ASYNC_FLAG_NONE);
wolfSSL 15:117db924cf7c 2086 }
wolfSSL 15:117db924cf7c 2087 #endif
wolfSSL 15:117db924cf7c 2088 if (ret == 0)
wolfSSL 15:117db924cf7c 2089 ret = wc_ecc_export_x963(privKey, pubKeyDer, pubKeySz);
wolfSSL 15:117db924cf7c 2090 wc_FreeRng(&rng);
wolfSSL 15:117db924cf7c 2091 }
wolfSSL 15:117db924cf7c 2092 }
wolfSSL 15:117db924cf7c 2093
wolfSSL 15:117db924cf7c 2094 /* for server: import public key */
wolfSSL 15:117db924cf7c 2095 else if (side == WOLFSSL_SERVER_END) {
wolfSSL 15:117db924cf7c 2096 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2097 privKey = &cbInfo->keyGen.ecc;
wolfSSL 15:117db924cf7c 2098 #else
wolfSSL 15:117db924cf7c 2099 privKey = otherKey;
wolfSSL 15:117db924cf7c 2100 #endif
wolfSSL 15:117db924cf7c 2101 pubKey = &tmpKey;
wolfSSL 15:117db924cf7c 2102
wolfSSL 15:117db924cf7c 2103 ret = wc_ecc_import_x963_ex(pubKeyDer, *pubKeySz, pubKey,
wolfSSL 15:117db924cf7c 2104 otherKey->dp->id);
wolfSSL 15:117db924cf7c 2105 }
wolfSSL 15:117db924cf7c 2106 else {
wolfSSL 15:117db924cf7c 2107 ret = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 2108 }
wolfSSL 15:117db924cf7c 2109
wolfSSL 15:117db924cf7c 2110 /* generate shared secret and return it */
wolfSSL 15:117db924cf7c 2111 if (ret == 0) {
wolfSSL 15:117db924cf7c 2112 ret = wc_ecc_shared_secret(privKey, pubKey, out, outlen);
wolfSSL 15:117db924cf7c 2113
wolfSSL 15:117db924cf7c 2114 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 15:117db924cf7c 2115 if (ret == WC_PENDING_E) {
wolfSSL 15:117db924cf7c 2116 ret = wc_AsyncWait(ret, &privKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
wolfSSL 15:117db924cf7c 2117 }
wolfSSL 15:117db924cf7c 2118 #endif
wolfSSL 15:117db924cf7c 2119 }
wolfSSL 15:117db924cf7c 2120
wolfSSL 15:117db924cf7c 2121 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2122 if (side == WOLFSSL_SERVER_END) {
wolfSSL 15:117db924cf7c 2123 wc_ecc_free(&cbInfo->keyGen.ecc);
wolfSSL 15:117db924cf7c 2124 }
wolfSSL 15:117db924cf7c 2125 #endif
wolfSSL 15:117db924cf7c 2126
wolfSSL 15:117db924cf7c 2127 wc_ecc_free(&tmpKey);
wolfSSL 15:117db924cf7c 2128
wolfSSL 15:117db924cf7c 2129 return ret;
wolfSSL 15:117db924cf7c 2130 }
wolfSSL 15:117db924cf7c 2131
wolfSSL 15:117db924cf7c 2132 #ifdef HAVE_ED25519
wolfSSL 15:117db924cf7c 2133 static WC_INLINE int myEd25519Sign(WOLFSSL* ssl, const byte* in, word32 inSz,
wolfSSL 15:117db924cf7c 2134 byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx)
wolfSSL 15:117db924cf7c 2135 {
wolfSSL 15:117db924cf7c 2136 int ret;
wolfSSL 15:117db924cf7c 2137 word32 idx = 0;
wolfSSL 15:117db924cf7c 2138 ed25519_key myKey;
wolfSSL 15:117db924cf7c 2139 byte* keyBuf = (byte*)key;
wolfSSL 15:117db924cf7c 2140 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2141
wolfSSL 15:117db924cf7c 2142 (void)ssl;
wolfSSL 15:117db924cf7c 2143 (void)cbInfo;
wolfSSL 15:117db924cf7c 2144
wolfSSL 15:117db924cf7c 2145 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2146 ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
wolfSSL 15:117db924cf7c 2147 if (ret != 0)
wolfSSL 15:117db924cf7c 2148 return ret;
wolfSSL 15:117db924cf7c 2149 #endif
wolfSSL 15:117db924cf7c 2150
wolfSSL 15:117db924cf7c 2151 ret = wc_ed25519_init(&myKey);
wolfSSL 15:117db924cf7c 2152 if (ret == 0) {
wolfSSL 15:117db924cf7c 2153 ret = wc_Ed25519PrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
wolfSSL 15:117db924cf7c 2154 if (ret == 0)
wolfSSL 15:117db924cf7c 2155 ret = wc_ed25519_sign_msg(in, inSz, out, outSz, &myKey);
wolfSSL 15:117db924cf7c 2156 wc_ed25519_free(&myKey);
wolfSSL 15:117db924cf7c 2157 }
wolfSSL 15:117db924cf7c 2158
wolfSSL 15:117db924cf7c 2159 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2160 free(keyBuf);
wolfSSL 15:117db924cf7c 2161 #endif
wolfSSL 15:117db924cf7c 2162
wolfSSL 15:117db924cf7c 2163 return ret;
wolfSSL 15:117db924cf7c 2164 }
wolfSSL 15:117db924cf7c 2165
wolfSSL 15:117db924cf7c 2166
wolfSSL 15:117db924cf7c 2167 static WC_INLINE int myEd25519Verify(WOLFSSL* ssl, const byte* sig, word32 sigSz,
wolfSSL 15:117db924cf7c 2168 const byte* msg, word32 msgSz, const byte* key, word32 keySz,
wolfSSL 15:117db924cf7c 2169 int* result, void* ctx)
wolfSSL 15:117db924cf7c 2170 {
wolfSSL 15:117db924cf7c 2171 int ret;
wolfSSL 15:117db924cf7c 2172 ed25519_key myKey;
wolfSSL 15:117db924cf7c 2173 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2174
wolfSSL 15:117db924cf7c 2175 (void)ssl;
wolfSSL 15:117db924cf7c 2176 (void)cbInfo;
wolfSSL 15:117db924cf7c 2177
wolfSSL 15:117db924cf7c 2178 ret = wc_ed25519_init(&myKey);
wolfSSL 15:117db924cf7c 2179 if (ret == 0) {
wolfSSL 15:117db924cf7c 2180 ret = wc_ed25519_import_public(key, keySz, &myKey);
wolfSSL 15:117db924cf7c 2181 if (ret == 0) {
wolfSSL 15:117db924cf7c 2182 ret = wc_ed25519_verify_msg(sig, sigSz, msg, msgSz, result, &myKey);
wolfSSL 15:117db924cf7c 2183 }
wolfSSL 15:117db924cf7c 2184 wc_ed25519_free(&myKey);
wolfSSL 15:117db924cf7c 2185 }
wolfSSL 15:117db924cf7c 2186
wolfSSL 15:117db924cf7c 2187 return ret;
wolfSSL 15:117db924cf7c 2188 }
wolfSSL 15:117db924cf7c 2189 #endif /* HAVE_ED25519 */
wolfSSL 15:117db924cf7c 2190
wolfSSL 15:117db924cf7c 2191 #ifdef HAVE_CURVE25519
wolfSSL 15:117db924cf7c 2192 static WC_INLINE int myX25519KeyGen(WOLFSSL* ssl, curve25519_key* key,
wolfSSL 15:117db924cf7c 2193 unsigned int keySz, void* ctx)
wolfSSL 15:117db924cf7c 2194 {
wolfSSL 15:117db924cf7c 2195 int ret;
wolfSSL 15:117db924cf7c 2196 WC_RNG rng;
wolfSSL 15:117db924cf7c 2197 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2198
wolfSSL 15:117db924cf7c 2199 (void)ssl;
wolfSSL 15:117db924cf7c 2200 (void)cbInfo;
wolfSSL 15:117db924cf7c 2201
wolfSSL 15:117db924cf7c 2202 ret = wc_InitRng(&rng);
wolfSSL 15:117db924cf7c 2203 if (ret != 0)
wolfSSL 15:117db924cf7c 2204 return ret;
wolfSSL 15:117db924cf7c 2205
wolfSSL 15:117db924cf7c 2206 ret = wc_curve25519_make_key(&rng, keySz, key);
wolfSSL 15:117db924cf7c 2207
wolfSSL 15:117db924cf7c 2208 wc_FreeRng(&rng);
wolfSSL 15:117db924cf7c 2209
wolfSSL 15:117db924cf7c 2210 return ret;
wolfSSL 15:117db924cf7c 2211 }
wolfSSL 15:117db924cf7c 2212
wolfSSL 15:117db924cf7c 2213 static WC_INLINE int myX25519SharedSecret(WOLFSSL* ssl, curve25519_key* otherKey,
wolfSSL 15:117db924cf7c 2214 unsigned char* pubKeyDer, unsigned int* pubKeySz,
wolfSSL 15:117db924cf7c 2215 unsigned char* out, unsigned int* outlen,
wolfSSL 15:117db924cf7c 2216 int side, void* ctx)
wolfSSL 15:117db924cf7c 2217 {
wolfSSL 15:117db924cf7c 2218 int ret;
wolfSSL 15:117db924cf7c 2219 curve25519_key* privKey = NULL;
wolfSSL 15:117db924cf7c 2220 curve25519_key* pubKey = NULL;
wolfSSL 15:117db924cf7c 2221 curve25519_key tmpKey;
wolfSSL 15:117db924cf7c 2222 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2223
wolfSSL 15:117db924cf7c 2224 (void)ssl;
wolfSSL 15:117db924cf7c 2225 (void)cbInfo;
wolfSSL 15:117db924cf7c 2226
wolfSSL 15:117db924cf7c 2227 ret = wc_curve25519_init(&tmpKey);
wolfSSL 15:117db924cf7c 2228 if (ret != 0) {
wolfSSL 15:117db924cf7c 2229 return ret;
wolfSSL 15:117db924cf7c 2230 }
wolfSSL 15:117db924cf7c 2231
wolfSSL 15:117db924cf7c 2232 /* for client: create and export public key */
wolfSSL 15:117db924cf7c 2233 if (side == WOLFSSL_CLIENT_END) {
wolfSSL 15:117db924cf7c 2234 WC_RNG rng;
wolfSSL 15:117db924cf7c 2235
wolfSSL 15:117db924cf7c 2236 privKey = &tmpKey;
wolfSSL 15:117db924cf7c 2237 pubKey = otherKey;
wolfSSL 15:117db924cf7c 2238
wolfSSL 15:117db924cf7c 2239 ret = wc_InitRng(&rng);
wolfSSL 15:117db924cf7c 2240 if (ret == 0) {
wolfSSL 15:117db924cf7c 2241 ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, privKey);
wolfSSL 15:117db924cf7c 2242 if (ret == 0) {
wolfSSL 15:117db924cf7c 2243 ret = wc_curve25519_export_public_ex(privKey, pubKeyDer,
wolfSSL 15:117db924cf7c 2244 pubKeySz, EC25519_LITTLE_ENDIAN);
wolfSSL 15:117db924cf7c 2245 }
wolfSSL 15:117db924cf7c 2246 wc_FreeRng(&rng);
wolfSSL 15:117db924cf7c 2247 }
wolfSSL 15:117db924cf7c 2248 }
wolfSSL 15:117db924cf7c 2249
wolfSSL 15:117db924cf7c 2250 /* for server: import public key */
wolfSSL 15:117db924cf7c 2251 else if (side == WOLFSSL_SERVER_END) {
wolfSSL 15:117db924cf7c 2252 privKey = otherKey;
wolfSSL 15:117db924cf7c 2253 pubKey = &tmpKey;
wolfSSL 15:117db924cf7c 2254
wolfSSL 15:117db924cf7c 2255 ret = wc_curve25519_import_public_ex(pubKeyDer, *pubKeySz, pubKey,
wolfSSL 15:117db924cf7c 2256 EC25519_LITTLE_ENDIAN);
wolfSSL 15:117db924cf7c 2257 }
wolfSSL 15:117db924cf7c 2258 else {
wolfSSL 15:117db924cf7c 2259 ret = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 2260 }
wolfSSL 15:117db924cf7c 2261
wolfSSL 15:117db924cf7c 2262 /* generate shared secret and return it */
wolfSSL 15:117db924cf7c 2263 if (ret == 0) {
wolfSSL 15:117db924cf7c 2264 ret = wc_curve25519_shared_secret_ex(privKey, pubKey, out, outlen,
wolfSSL 15:117db924cf7c 2265 EC25519_LITTLE_ENDIAN);
wolfSSL 15:117db924cf7c 2266 }
wolfSSL 15:117db924cf7c 2267
wolfSSL 15:117db924cf7c 2268 wc_curve25519_free(&tmpKey);
wolfSSL 15:117db924cf7c 2269
wolfSSL 15:117db924cf7c 2270 return ret;
wolfSSL 15:117db924cf7c 2271 }
wolfSSL 15:117db924cf7c 2272 #endif /* HAVE_CURVE25519 */
wolfSSL 15:117db924cf7c 2273
wolfSSL 15:117db924cf7c 2274 #endif /* HAVE_ECC */
wolfSSL 15:117db924cf7c 2275
wolfSSL 15:117db924cf7c 2276 #ifndef NO_DH
wolfSSL 15:117db924cf7c 2277 static WC_INLINE int myDhCallback(WOLFSSL* ssl, struct DhKey* key,
wolfSSL 15:117db924cf7c 2278 const unsigned char* priv, unsigned int privSz,
wolfSSL 15:117db924cf7c 2279 const unsigned char* pubKeyDer, unsigned int pubKeySz,
wolfSSL 15:117db924cf7c 2280 unsigned char* out, unsigned int* outlen,
wolfSSL 15:117db924cf7c 2281 void* ctx)
wolfSSL 15:117db924cf7c 2282 {
wolfSSL 15:117db924cf7c 2283 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2284
wolfSSL 15:117db924cf7c 2285 (void)ssl;
wolfSSL 15:117db924cf7c 2286 (void)cbInfo;
wolfSSL 15:117db924cf7c 2287
wolfSSL 15:117db924cf7c 2288 /* return 0 on success */
wolfSSL 15:117db924cf7c 2289 return wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
wolfSSL 15:117db924cf7c 2290 };
wolfSSL 15:117db924cf7c 2291
wolfSSL 15:117db924cf7c 2292 #endif /* !NO_DH */
wolfSSL 15:117db924cf7c 2293
wolfSSL 15:117db924cf7c 2294 #ifndef NO_RSA
wolfSSL 15:117db924cf7c 2295
wolfSSL 15:117db924cf7c 2296 static WC_INLINE int myRsaSign(WOLFSSL* ssl, const byte* in, word32 inSz,
wolfSSL 15:117db924cf7c 2297 byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx)
wolfSSL 15:117db924cf7c 2298 {
wolfSSL 15:117db924cf7c 2299 WC_RNG rng;
wolfSSL 15:117db924cf7c 2300 int ret;
wolfSSL 15:117db924cf7c 2301 word32 idx = 0;
wolfSSL 15:117db924cf7c 2302 RsaKey myKey;
wolfSSL 15:117db924cf7c 2303 byte* keyBuf = (byte*)key;
wolfSSL 15:117db924cf7c 2304 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2305
wolfSSL 15:117db924cf7c 2306 (void)ssl;
wolfSSL 15:117db924cf7c 2307 (void)cbInfo;
wolfSSL 15:117db924cf7c 2308
wolfSSL 15:117db924cf7c 2309 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2310 ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
wolfSSL 15:117db924cf7c 2311 if (ret != 0)
wolfSSL 15:117db924cf7c 2312 return ret;
wolfSSL 15:117db924cf7c 2313 #endif
wolfSSL 15:117db924cf7c 2314
wolfSSL 15:117db924cf7c 2315 ret = wc_InitRng(&rng);
wolfSSL 15:117db924cf7c 2316 if (ret != 0)
wolfSSL 15:117db924cf7c 2317 return ret;
wolfSSL 15:117db924cf7c 2318
wolfSSL 15:117db924cf7c 2319 ret = wc_InitRsaKey(&myKey, NULL);
wolfSSL 15:117db924cf7c 2320 if (ret == 0) {
wolfSSL 15:117db924cf7c 2321 ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
wolfSSL 15:117db924cf7c 2322 if (ret == 0)
wolfSSL 15:117db924cf7c 2323 ret = wc_RsaSSL_Sign(in, inSz, out, *outSz, &myKey, &rng);
wolfSSL 15:117db924cf7c 2324 if (ret > 0) { /* save and convert to 0 success */
wolfSSL 15:117db924cf7c 2325 *outSz = ret;
wolfSSL 15:117db924cf7c 2326 ret = 0;
wolfSSL 15:117db924cf7c 2327 }
wolfSSL 15:117db924cf7c 2328 wc_FreeRsaKey(&myKey);
wolfSSL 15:117db924cf7c 2329 }
wolfSSL 15:117db924cf7c 2330 wc_FreeRng(&rng);
wolfSSL 15:117db924cf7c 2331
wolfSSL 15:117db924cf7c 2332 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2333 free(keyBuf);
wolfSSL 15:117db924cf7c 2334 #endif
wolfSSL 15:117db924cf7c 2335
wolfSSL 15:117db924cf7c 2336 return ret;
wolfSSL 15:117db924cf7c 2337 }
wolfSSL 15:117db924cf7c 2338
wolfSSL 15:117db924cf7c 2339
wolfSSL 15:117db924cf7c 2340 static WC_INLINE int myRsaVerify(WOLFSSL* ssl, byte* sig, word32 sigSz,
wolfSSL 15:117db924cf7c 2341 byte** out, const byte* key, word32 keySz, void* ctx)
wolfSSL 15:117db924cf7c 2342 {
wolfSSL 15:117db924cf7c 2343 int ret;
wolfSSL 15:117db924cf7c 2344 word32 idx = 0;
wolfSSL 15:117db924cf7c 2345 RsaKey myKey;
wolfSSL 15:117db924cf7c 2346 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2347
wolfSSL 15:117db924cf7c 2348 (void)ssl;
wolfSSL 15:117db924cf7c 2349 (void)cbInfo;
wolfSSL 15:117db924cf7c 2350
wolfSSL 15:117db924cf7c 2351 ret = wc_InitRsaKey(&myKey, NULL);
wolfSSL 15:117db924cf7c 2352 if (ret == 0) {
wolfSSL 15:117db924cf7c 2353 ret = wc_RsaPublicKeyDecode(key, &idx, &myKey, keySz);
wolfSSL 15:117db924cf7c 2354 if (ret == 0)
wolfSSL 15:117db924cf7c 2355 ret = wc_RsaSSL_VerifyInline(sig, sigSz, out, &myKey);
wolfSSL 15:117db924cf7c 2356 wc_FreeRsaKey(&myKey);
wolfSSL 15:117db924cf7c 2357 }
wolfSSL 15:117db924cf7c 2358
wolfSSL 15:117db924cf7c 2359 return ret;
wolfSSL 15:117db924cf7c 2360 }
wolfSSL 15:117db924cf7c 2361
wolfSSL 15:117db924cf7c 2362 static WC_INLINE int myRsaSignCheck(WOLFSSL* ssl, byte* sig, word32 sigSz,
wolfSSL 15:117db924cf7c 2363 byte** out, const byte* key, word32 keySz, void* ctx)
wolfSSL 15:117db924cf7c 2364 {
wolfSSL 15:117db924cf7c 2365 int ret;
wolfSSL 15:117db924cf7c 2366 word32 idx = 0;
wolfSSL 15:117db924cf7c 2367 RsaKey myKey;
wolfSSL 15:117db924cf7c 2368 byte* keyBuf = (byte*)key;
wolfSSL 15:117db924cf7c 2369 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2370
wolfSSL 15:117db924cf7c 2371 (void)ssl;
wolfSSL 15:117db924cf7c 2372 (void)cbInfo;
wolfSSL 15:117db924cf7c 2373
wolfSSL 15:117db924cf7c 2374 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2375 ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
wolfSSL 15:117db924cf7c 2376 if (ret != 0)
wolfSSL 15:117db924cf7c 2377 return ret;
wolfSSL 15:117db924cf7c 2378 #endif
wolfSSL 15:117db924cf7c 2379
wolfSSL 15:117db924cf7c 2380 ret = wc_InitRsaKey(&myKey, NULL);
wolfSSL 15:117db924cf7c 2381 if (ret == 0) {
wolfSSL 15:117db924cf7c 2382 ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
wolfSSL 15:117db924cf7c 2383 if (ret == 0)
wolfSSL 15:117db924cf7c 2384 ret = wc_RsaSSL_VerifyInline(sig, sigSz, out, &myKey);
wolfSSL 15:117db924cf7c 2385 wc_FreeRsaKey(&myKey);
wolfSSL 15:117db924cf7c 2386 }
wolfSSL 15:117db924cf7c 2387 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2388 free(keyBuf);
wolfSSL 15:117db924cf7c 2389 #endif
wolfSSL 15:117db924cf7c 2390
wolfSSL 15:117db924cf7c 2391 return ret;
wolfSSL 15:117db924cf7c 2392 }
wolfSSL 15:117db924cf7c 2393
wolfSSL 15:117db924cf7c 2394 #ifdef WC_RSA_PSS
wolfSSL 15:117db924cf7c 2395 static WC_INLINE int myRsaPssSign(WOLFSSL* ssl, const byte* in, word32 inSz,
wolfSSL 15:117db924cf7c 2396 byte* out, word32* outSz, int hash, int mgf, const byte* key,
wolfSSL 15:117db924cf7c 2397 word32 keySz, void* ctx)
wolfSSL 15:117db924cf7c 2398 {
wolfSSL 15:117db924cf7c 2399 enum wc_HashType hashType = WC_HASH_TYPE_NONE;
wolfSSL 15:117db924cf7c 2400 WC_RNG rng;
wolfSSL 15:117db924cf7c 2401 int ret;
wolfSSL 15:117db924cf7c 2402 word32 idx = 0;
wolfSSL 15:117db924cf7c 2403 RsaKey myKey;
wolfSSL 15:117db924cf7c 2404 byte* keyBuf = (byte*)key;
wolfSSL 15:117db924cf7c 2405 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2406
wolfSSL 15:117db924cf7c 2407 (void)ssl;
wolfSSL 15:117db924cf7c 2408 (void)cbInfo;
wolfSSL 15:117db924cf7c 2409
wolfSSL 15:117db924cf7c 2410 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2411 ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
wolfSSL 15:117db924cf7c 2412 if (ret != 0)
wolfSSL 15:117db924cf7c 2413 return ret;
wolfSSL 15:117db924cf7c 2414 #endif
wolfSSL 15:117db924cf7c 2415
wolfSSL 15:117db924cf7c 2416 switch (hash) {
wolfSSL 15:117db924cf7c 2417 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 2418 case SHA256h:
wolfSSL 15:117db924cf7c 2419 hashType = WC_HASH_TYPE_SHA256;
wolfSSL 15:117db924cf7c 2420 break;
wolfSSL 15:117db924cf7c 2421 #endif
wolfSSL 15:117db924cf7c 2422 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 2423 case SHA384h:
wolfSSL 15:117db924cf7c 2424 hashType = WC_HASH_TYPE_SHA384;
wolfSSL 15:117db924cf7c 2425 break;
wolfSSL 15:117db924cf7c 2426 #endif
wolfSSL 15:117db924cf7c 2427 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 2428 case SHA512h:
wolfSSL 15:117db924cf7c 2429 hashType = WC_HASH_TYPE_SHA512;
wolfSSL 15:117db924cf7c 2430 break;
wolfSSL 15:117db924cf7c 2431 #endif
wolfSSL 15:117db924cf7c 2432 }
wolfSSL 15:117db924cf7c 2433
wolfSSL 15:117db924cf7c 2434 ret = wc_InitRng(&rng);
wolfSSL 15:117db924cf7c 2435 if (ret != 0)
wolfSSL 15:117db924cf7c 2436 return ret;
wolfSSL 15:117db924cf7c 2437
wolfSSL 15:117db924cf7c 2438 ret = wc_InitRsaKey(&myKey, NULL);
wolfSSL 15:117db924cf7c 2439 if (ret == 0) {
wolfSSL 15:117db924cf7c 2440 ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
wolfSSL 15:117db924cf7c 2441 if (ret == 0) {
wolfSSL 15:117db924cf7c 2442 ret = wc_RsaPSS_Sign(in, inSz, out, *outSz, hashType, mgf, &myKey,
wolfSSL 15:117db924cf7c 2443 &rng);
wolfSSL 15:117db924cf7c 2444 }
wolfSSL 15:117db924cf7c 2445 if (ret > 0) { /* save and convert to 0 success */
wolfSSL 15:117db924cf7c 2446 *outSz = ret;
wolfSSL 15:117db924cf7c 2447 ret = 0;
wolfSSL 15:117db924cf7c 2448 }
wolfSSL 15:117db924cf7c 2449 wc_FreeRsaKey(&myKey);
wolfSSL 15:117db924cf7c 2450 }
wolfSSL 15:117db924cf7c 2451 wc_FreeRng(&rng);
wolfSSL 15:117db924cf7c 2452
wolfSSL 15:117db924cf7c 2453 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2454 free(keyBuf);
wolfSSL 15:117db924cf7c 2455 #endif
wolfSSL 15:117db924cf7c 2456
wolfSSL 15:117db924cf7c 2457 return ret;
wolfSSL 15:117db924cf7c 2458 }
wolfSSL 15:117db924cf7c 2459
wolfSSL 15:117db924cf7c 2460
wolfSSL 15:117db924cf7c 2461 static WC_INLINE int myRsaPssVerify(WOLFSSL* ssl, byte* sig, word32 sigSz,
wolfSSL 15:117db924cf7c 2462 byte** out, int hash, int mgf, const byte* key, word32 keySz, void* ctx)
wolfSSL 15:117db924cf7c 2463 {
wolfSSL 15:117db924cf7c 2464 int ret;
wolfSSL 15:117db924cf7c 2465 word32 idx = 0;
wolfSSL 15:117db924cf7c 2466 RsaKey myKey;
wolfSSL 15:117db924cf7c 2467 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2468 enum wc_HashType hashType = WC_HASH_TYPE_NONE;
wolfSSL 15:117db924cf7c 2469
wolfSSL 15:117db924cf7c 2470 (void)ssl;
wolfSSL 15:117db924cf7c 2471 (void)cbInfo;
wolfSSL 15:117db924cf7c 2472
wolfSSL 15:117db924cf7c 2473 switch (hash) {
wolfSSL 15:117db924cf7c 2474 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 2475 case SHA256h:
wolfSSL 15:117db924cf7c 2476 hashType = WC_HASH_TYPE_SHA256;
wolfSSL 15:117db924cf7c 2477 break;
wolfSSL 15:117db924cf7c 2478 #endif
wolfSSL 15:117db924cf7c 2479 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 2480 case SHA384h:
wolfSSL 15:117db924cf7c 2481 hashType = WC_HASH_TYPE_SHA384;
wolfSSL 15:117db924cf7c 2482 break;
wolfSSL 15:117db924cf7c 2483 #endif
wolfSSL 15:117db924cf7c 2484 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 2485 case SHA512h:
wolfSSL 15:117db924cf7c 2486 hashType = WC_HASH_TYPE_SHA512;
wolfSSL 15:117db924cf7c 2487 break;
wolfSSL 15:117db924cf7c 2488 #endif
wolfSSL 15:117db924cf7c 2489 }
wolfSSL 15:117db924cf7c 2490
wolfSSL 15:117db924cf7c 2491 ret = wc_InitRsaKey(&myKey, NULL);
wolfSSL 15:117db924cf7c 2492 if (ret == 0) {
wolfSSL 15:117db924cf7c 2493 ret = wc_RsaPublicKeyDecode(key, &idx, &myKey, keySz);
wolfSSL 15:117db924cf7c 2494 if (ret == 0) {
wolfSSL 15:117db924cf7c 2495 ret = wc_RsaPSS_VerifyInline(sig, sigSz, out, hashType, mgf,
wolfSSL 15:117db924cf7c 2496 &myKey);
wolfSSL 15:117db924cf7c 2497 }
wolfSSL 15:117db924cf7c 2498 wc_FreeRsaKey(&myKey);
wolfSSL 15:117db924cf7c 2499 }
wolfSSL 15:117db924cf7c 2500
wolfSSL 15:117db924cf7c 2501 return ret;
wolfSSL 15:117db924cf7c 2502 }
wolfSSL 15:117db924cf7c 2503
wolfSSL 15:117db924cf7c 2504 static WC_INLINE int myRsaPssSignCheck(WOLFSSL* ssl, byte* sig, word32 sigSz,
wolfSSL 15:117db924cf7c 2505 byte** out, int hash, int mgf, const byte* key, word32 keySz, void* ctx)
wolfSSL 15:117db924cf7c 2506 {
wolfSSL 15:117db924cf7c 2507 int ret;
wolfSSL 15:117db924cf7c 2508 word32 idx = 0;
wolfSSL 15:117db924cf7c 2509 RsaKey myKey;
wolfSSL 15:117db924cf7c 2510 byte* keyBuf = (byte*)key;
wolfSSL 15:117db924cf7c 2511 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2512 enum wc_HashType hashType = WC_HASH_TYPE_NONE;
wolfSSL 15:117db924cf7c 2513
wolfSSL 15:117db924cf7c 2514 (void)ssl;
wolfSSL 15:117db924cf7c 2515 (void)cbInfo;
wolfSSL 15:117db924cf7c 2516
wolfSSL 15:117db924cf7c 2517 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2518 ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
wolfSSL 15:117db924cf7c 2519 if (ret != 0)
wolfSSL 15:117db924cf7c 2520 return ret;
wolfSSL 15:117db924cf7c 2521 #endif
wolfSSL 15:117db924cf7c 2522
wolfSSL 15:117db924cf7c 2523 switch (hash) {
wolfSSL 15:117db924cf7c 2524 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 2525 case SHA256h:
wolfSSL 15:117db924cf7c 2526 hashType = WC_HASH_TYPE_SHA256;
wolfSSL 15:117db924cf7c 2527 break;
wolfSSL 15:117db924cf7c 2528 #endif
wolfSSL 15:117db924cf7c 2529 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 2530 case SHA384h:
wolfSSL 15:117db924cf7c 2531 hashType = WC_HASH_TYPE_SHA384;
wolfSSL 15:117db924cf7c 2532 break;
wolfSSL 15:117db924cf7c 2533 #endif
wolfSSL 15:117db924cf7c 2534 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 2535 case SHA512h:
wolfSSL 15:117db924cf7c 2536 hashType = WC_HASH_TYPE_SHA512;
wolfSSL 15:117db924cf7c 2537 break;
wolfSSL 15:117db924cf7c 2538 #endif
wolfSSL 15:117db924cf7c 2539 }
wolfSSL 15:117db924cf7c 2540
wolfSSL 15:117db924cf7c 2541 ret = wc_InitRsaKey(&myKey, NULL);
wolfSSL 15:117db924cf7c 2542 if (ret == 0) {
wolfSSL 15:117db924cf7c 2543 ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
wolfSSL 15:117db924cf7c 2544 if (ret == 0) {
wolfSSL 15:117db924cf7c 2545 ret = wc_RsaPSS_VerifyInline(sig, sigSz, out, hashType, mgf,
wolfSSL 15:117db924cf7c 2546 &myKey);
wolfSSL 15:117db924cf7c 2547 }
wolfSSL 15:117db924cf7c 2548 wc_FreeRsaKey(&myKey);
wolfSSL 15:117db924cf7c 2549 }
wolfSSL 15:117db924cf7c 2550
wolfSSL 15:117db924cf7c 2551 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2552 free(keyBuf);
wolfSSL 15:117db924cf7c 2553 #endif
wolfSSL 15:117db924cf7c 2554
wolfSSL 15:117db924cf7c 2555 return ret;
wolfSSL 15:117db924cf7c 2556 }
wolfSSL 15:117db924cf7c 2557 #endif
wolfSSL 15:117db924cf7c 2558
wolfSSL 15:117db924cf7c 2559
wolfSSL 15:117db924cf7c 2560 static WC_INLINE int myRsaEnc(WOLFSSL* ssl, const byte* in, word32 inSz,
wolfSSL 15:117db924cf7c 2561 byte* out, word32* outSz, const byte* key,
wolfSSL 15:117db924cf7c 2562 word32 keySz, void* ctx)
wolfSSL 15:117db924cf7c 2563 {
wolfSSL 15:117db924cf7c 2564 int ret;
wolfSSL 15:117db924cf7c 2565 word32 idx = 0;
wolfSSL 15:117db924cf7c 2566 RsaKey myKey;
wolfSSL 15:117db924cf7c 2567 WC_RNG rng;
wolfSSL 15:117db924cf7c 2568 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2569
wolfSSL 15:117db924cf7c 2570 (void)ssl;
wolfSSL 15:117db924cf7c 2571 (void)cbInfo;
wolfSSL 15:117db924cf7c 2572
wolfSSL 15:117db924cf7c 2573 ret = wc_InitRng(&rng);
wolfSSL 15:117db924cf7c 2574 if (ret != 0)
wolfSSL 15:117db924cf7c 2575 return ret;
wolfSSL 15:117db924cf7c 2576
wolfSSL 15:117db924cf7c 2577 ret = wc_InitRsaKey(&myKey, NULL);
wolfSSL 15:117db924cf7c 2578 if (ret == 0) {
wolfSSL 15:117db924cf7c 2579 ret = wc_RsaPublicKeyDecode(key, &idx, &myKey, keySz);
wolfSSL 15:117db924cf7c 2580 if (ret == 0) {
wolfSSL 15:117db924cf7c 2581 ret = wc_RsaPublicEncrypt(in, inSz, out, *outSz, &myKey, &rng);
wolfSSL 15:117db924cf7c 2582 if (ret > 0) {
wolfSSL 15:117db924cf7c 2583 *outSz = ret;
wolfSSL 15:117db924cf7c 2584 ret = 0; /* reset to success */
wolfSSL 15:117db924cf7c 2585 }
wolfSSL 15:117db924cf7c 2586 }
wolfSSL 15:117db924cf7c 2587 wc_FreeRsaKey(&myKey);
wolfSSL 15:117db924cf7c 2588 }
wolfSSL 15:117db924cf7c 2589 wc_FreeRng(&rng);
wolfSSL 15:117db924cf7c 2590
wolfSSL 15:117db924cf7c 2591 return ret;
wolfSSL 15:117db924cf7c 2592 }
wolfSSL 15:117db924cf7c 2593
wolfSSL 15:117db924cf7c 2594 static WC_INLINE int myRsaDec(WOLFSSL* ssl, byte* in, word32 inSz,
wolfSSL 15:117db924cf7c 2595 byte** out,
wolfSSL 15:117db924cf7c 2596 const byte* key, word32 keySz, void* ctx)
wolfSSL 15:117db924cf7c 2597 {
wolfSSL 15:117db924cf7c 2598 int ret;
wolfSSL 15:117db924cf7c 2599 word32 idx = 0;
wolfSSL 15:117db924cf7c 2600 RsaKey myKey;
wolfSSL 15:117db924cf7c 2601 byte* keyBuf = (byte*)key;
wolfSSL 15:117db924cf7c 2602 PkCbInfo* cbInfo = (PkCbInfo*)ctx;
wolfSSL 15:117db924cf7c 2603
wolfSSL 15:117db924cf7c 2604 (void)ssl;
wolfSSL 15:117db924cf7c 2605 (void)cbInfo;
wolfSSL 15:117db924cf7c 2606
wolfSSL 15:117db924cf7c 2607 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2608 ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
wolfSSL 15:117db924cf7c 2609 if (ret != 0)
wolfSSL 15:117db924cf7c 2610 return ret;
wolfSSL 15:117db924cf7c 2611 #endif
wolfSSL 15:117db924cf7c 2612
wolfSSL 15:117db924cf7c 2613 ret = wc_InitRsaKey(&myKey, NULL);
wolfSSL 15:117db924cf7c 2614 if (ret == 0) {
wolfSSL 15:117db924cf7c 2615 ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
wolfSSL 15:117db924cf7c 2616 if (ret == 0) {
wolfSSL 15:117db924cf7c 2617 #ifdef WC_RSA_BLINDING
wolfSSL 15:117db924cf7c 2618 ret = wc_RsaSetRNG(&myKey, wolfSSL_GetRNG(ssl));
wolfSSL 15:117db924cf7c 2619 if (ret != 0) {
wolfSSL 15:117db924cf7c 2620 wc_FreeRsaKey(&myKey);
wolfSSL 15:117db924cf7c 2621 return ret;
wolfSSL 15:117db924cf7c 2622 }
wolfSSL 15:117db924cf7c 2623 #endif
wolfSSL 15:117db924cf7c 2624 ret = wc_RsaPrivateDecryptInline(in, inSz, out, &myKey);
wolfSSL 15:117db924cf7c 2625 }
wolfSSL 15:117db924cf7c 2626 wc_FreeRsaKey(&myKey);
wolfSSL 15:117db924cf7c 2627 }
wolfSSL 15:117db924cf7c 2628
wolfSSL 15:117db924cf7c 2629 #ifdef TEST_PK_PRIVKEY
wolfSSL 15:117db924cf7c 2630 free(keyBuf);
wolfSSL 15:117db924cf7c 2631 #endif
wolfSSL 15:117db924cf7c 2632
wolfSSL 15:117db924cf7c 2633 return ret;
wolfSSL 15:117db924cf7c 2634 }
wolfSSL 15:117db924cf7c 2635
wolfSSL 15:117db924cf7c 2636 #endif /* NO_RSA */
wolfSSL 15:117db924cf7c 2637
wolfSSL 15:117db924cf7c 2638 static WC_INLINE void SetupPkCallbacks(WOLFSSL_CTX* ctx)
wolfSSL 15:117db924cf7c 2639 {
wolfSSL 15:117db924cf7c 2640 (void)ctx;
wolfSSL 15:117db924cf7c 2641
wolfSSL 15:117db924cf7c 2642 #ifdef HAVE_ECC
wolfSSL 15:117db924cf7c 2643 wolfSSL_CTX_SetEccKeyGenCb(ctx, myEccKeyGen);
wolfSSL 15:117db924cf7c 2644 wolfSSL_CTX_SetEccSignCb(ctx, myEccSign);
wolfSSL 15:117db924cf7c 2645 wolfSSL_CTX_SetEccVerifyCb(ctx, myEccVerify);
wolfSSL 15:117db924cf7c 2646 wolfSSL_CTX_SetEccSharedSecretCb(ctx, myEccSharedSecret);
wolfSSL 15:117db924cf7c 2647 #endif /* HAVE_ECC */
wolfSSL 15:117db924cf7c 2648 #ifndef NO_DH
wolfSSL 15:117db924cf7c 2649 wolfSSL_CTX_SetDhAgreeCb(ctx, myDhCallback);
wolfSSL 15:117db924cf7c 2650 #endif
wolfSSL 15:117db924cf7c 2651 #ifdef HAVE_ED25519
wolfSSL 15:117db924cf7c 2652 wolfSSL_CTX_SetEd25519SignCb(ctx, myEd25519Sign);
wolfSSL 15:117db924cf7c 2653 wolfSSL_CTX_SetEd25519VerifyCb(ctx, myEd25519Verify);
wolfSSL 15:117db924cf7c 2654 #endif
wolfSSL 15:117db924cf7c 2655 #ifdef HAVE_CURVE25519
wolfSSL 15:117db924cf7c 2656 wolfSSL_CTX_SetX25519KeyGenCb(ctx, myX25519KeyGen);
wolfSSL 15:117db924cf7c 2657 wolfSSL_CTX_SetX25519SharedSecretCb(ctx, myX25519SharedSecret);
wolfSSL 15:117db924cf7c 2658 #endif
wolfSSL 15:117db924cf7c 2659 #ifndef NO_RSA
wolfSSL 15:117db924cf7c 2660 wolfSSL_CTX_SetRsaSignCb(ctx, myRsaSign);
wolfSSL 15:117db924cf7c 2661 wolfSSL_CTX_SetRsaVerifyCb(ctx, myRsaVerify);
wolfSSL 15:117db924cf7c 2662 wolfSSL_CTX_SetRsaSignCheckCb(ctx, myRsaSignCheck);
wolfSSL 15:117db924cf7c 2663 #ifdef WC_RSA_PSS
wolfSSL 15:117db924cf7c 2664 wolfSSL_CTX_SetRsaPssSignCb(ctx, myRsaPssSign);
wolfSSL 15:117db924cf7c 2665 wolfSSL_CTX_SetRsaPssVerifyCb(ctx, myRsaPssVerify);
wolfSSL 15:117db924cf7c 2666 wolfSSL_CTX_SetRsaPssSignCheckCb(ctx, myRsaPssSignCheck);
wolfSSL 15:117db924cf7c 2667 #endif
wolfSSL 15:117db924cf7c 2668 wolfSSL_CTX_SetRsaEncCb(ctx, myRsaEnc);
wolfSSL 15:117db924cf7c 2669 wolfSSL_CTX_SetRsaDecCb(ctx, myRsaDec);
wolfSSL 15:117db924cf7c 2670 #endif /* NO_RSA */
wolfSSL 15:117db924cf7c 2671 }
wolfSSL 15:117db924cf7c 2672
wolfSSL 15:117db924cf7c 2673 static WC_INLINE void SetupPkCallbackContexts(WOLFSSL* ssl, void* myCtx)
wolfSSL 15:117db924cf7c 2674 {
wolfSSL 15:117db924cf7c 2675 #ifdef HAVE_ECC
wolfSSL 15:117db924cf7c 2676 wolfSSL_SetEccKeyGenCtx(ssl, myCtx);
wolfSSL 15:117db924cf7c 2677 wolfSSL_SetEccSignCtx(ssl, myCtx);
wolfSSL 15:117db924cf7c 2678 wolfSSL_SetEccVerifyCtx(ssl, myCtx);
wolfSSL 15:117db924cf7c 2679 wolfSSL_SetEccSharedSecretCtx(ssl, myCtx);
wolfSSL 15:117db924cf7c 2680 #endif /* HAVE_ECC */
wolfSSL 15:117db924cf7c 2681 #ifndef NO_DH
wolfSSL 15:117db924cf7c 2682 wolfSSL_SetDhAgreeCtx(ssl, myCtx);
wolfSSL 15:117db924cf7c 2683 #endif
wolfSSL 15:117db924cf7c 2684 #ifdef HAVE_ED25519
wolfSSL 15:117db924cf7c 2685 wolfSSL_SetEd25519SignCtx(ssl, myCtx);
wolfSSL 15:117db924cf7c 2686 wolfSSL_SetEd25519VerifyCtx(ssl, myCtx);
wolfSSL 15:117db924cf7c 2687 #endif
wolfSSL 15:117db924cf7c 2688 #ifdef HAVE_CURVE25519
wolfSSL 15:117db924cf7c 2689 wolfSSL_SetX25519KeyGenCtx(ssl, myCtx);
wolfSSL 15:117db924cf7c 2690 wolfSSL_SetX25519SharedSecretCtx(ssl, myCtx);
wolfSSL 15:117db924cf7c 2691 #endif
wolfSSL 15:117db924cf7c 2692 #ifndef NO_RSA
wolfSSL 15:117db924cf7c 2693 wolfSSL_SetRsaSignCtx(ssl, myCtx);
wolfSSL 15:117db924cf7c 2694 wolfSSL_SetRsaVerifyCtx(ssl, myCtx);
wolfSSL 15:117db924cf7c 2695 #ifdef WC_RSA_PSS
wolfSSL 15:117db924cf7c 2696 wolfSSL_SetRsaPssSignCtx(ssl, myCtx);
wolfSSL 15:117db924cf7c 2697 wolfSSL_SetRsaPssVerifyCtx(ssl, myCtx);
wolfSSL 15:117db924cf7c 2698 #endif
wolfSSL 15:117db924cf7c 2699 wolfSSL_SetRsaEncCtx(ssl, myCtx);
wolfSSL 15:117db924cf7c 2700 wolfSSL_SetRsaDecCtx(ssl, myCtx);
wolfSSL 15:117db924cf7c 2701 #endif /* NO_RSA */
wolfSSL 15:117db924cf7c 2702 }
wolfSSL 15:117db924cf7c 2703
wolfSSL 15:117db924cf7c 2704 #endif /* HAVE_PK_CALLBACKS */
wolfSSL 15:117db924cf7c 2705
wolfSSL 15:117db924cf7c 2706
wolfSSL 15:117db924cf7c 2707
wolfSSL 15:117db924cf7c 2708
wolfSSL 15:117db924cf7c 2709 #if defined(__hpux__) || defined(__MINGW32__) || defined (WOLFSSL_TIRTOS) \
wolfSSL 15:117db924cf7c 2710 || defined(_MSC_VER)
wolfSSL 15:117db924cf7c 2711
wolfSSL 15:117db924cf7c 2712 /* HP/UX doesn't have strsep, needed by test/suites.c */
wolfSSL 15:117db924cf7c 2713 static WC_INLINE char* strsep(char **stringp, const char *delim)
wolfSSL 15:117db924cf7c 2714 {
wolfSSL 15:117db924cf7c 2715 char* start;
wolfSSL 15:117db924cf7c 2716 char* end;
wolfSSL 15:117db924cf7c 2717
wolfSSL 15:117db924cf7c 2718 start = *stringp;
wolfSSL 15:117db924cf7c 2719 if (start == NULL)
wolfSSL 15:117db924cf7c 2720 return NULL;
wolfSSL 15:117db924cf7c 2721
wolfSSL 15:117db924cf7c 2722 if ((end = strpbrk(start, delim))) {
wolfSSL 15:117db924cf7c 2723 *end++ = '\0';
wolfSSL 15:117db924cf7c 2724 *stringp = end;
wolfSSL 15:117db924cf7c 2725 } else {
wolfSSL 15:117db924cf7c 2726 *stringp = NULL;
wolfSSL 15:117db924cf7c 2727 }
wolfSSL 15:117db924cf7c 2728
wolfSSL 15:117db924cf7c 2729 return start;
wolfSSL 15:117db924cf7c 2730 }
wolfSSL 15:117db924cf7c 2731
wolfSSL 15:117db924cf7c 2732 #endif /* __hpux__ and others */
wolfSSL 15:117db924cf7c 2733
wolfSSL 15:117db924cf7c 2734 /* Create unique filename, len is length of tempfn name, assuming
wolfSSL 15:117db924cf7c 2735 len does not include null terminating character,
wolfSSL 15:117db924cf7c 2736 num is number of characters in tempfn name to randomize */
wolfSSL 15:117db924cf7c 2737 static WC_INLINE const char* mymktemp(char *tempfn, int len, int num)
wolfSSL 15:117db924cf7c 2738 {
wolfSSL 15:117db924cf7c 2739 int x, size;
wolfSSL 15:117db924cf7c 2740 static const char alphanum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
wolfSSL 15:117db924cf7c 2741 "abcdefghijklmnopqrstuvwxyz";
wolfSSL 15:117db924cf7c 2742 WC_RNG rng;
wolfSSL 15:117db924cf7c 2743 byte out;
wolfSSL 15:117db924cf7c 2744
wolfSSL 15:117db924cf7c 2745 if (tempfn == NULL || len < 1 || num < 1 || len <= num) {
wolfSSL 15:117db924cf7c 2746 printf("Bad input\n");
wolfSSL 15:117db924cf7c 2747 return NULL;
wolfSSL 15:117db924cf7c 2748 }
wolfSSL 15:117db924cf7c 2749
wolfSSL 15:117db924cf7c 2750 size = len - 1;
wolfSSL 15:117db924cf7c 2751
wolfSSL 15:117db924cf7c 2752 if (wc_InitRng(&rng) != 0) {
wolfSSL 15:117db924cf7c 2753 printf("InitRng failed\n");
wolfSSL 15:117db924cf7c 2754 return NULL;
wolfSSL 15:117db924cf7c 2755 }
wolfSSL 15:117db924cf7c 2756
wolfSSL 15:117db924cf7c 2757 for (x = size; x > size - num; x--) {
wolfSSL 15:117db924cf7c 2758 if (wc_RNG_GenerateBlock(&rng,(byte*)&out, sizeof(out)) != 0) {
wolfSSL 15:117db924cf7c 2759 printf("RNG_GenerateBlock failed\n");
wolfSSL 15:117db924cf7c 2760 return NULL;
wolfSSL 15:117db924cf7c 2761 }
wolfSSL 15:117db924cf7c 2762 tempfn[x] = alphanum[out % (sizeof(alphanum) - 1)];
wolfSSL 15:117db924cf7c 2763 }
wolfSSL 15:117db924cf7c 2764 tempfn[len] = '\0';
wolfSSL 15:117db924cf7c 2765
wolfSSL 15:117db924cf7c 2766 wc_FreeRng(&rng);
wolfSSL 15:117db924cf7c 2767
wolfSSL 15:117db924cf7c 2768 return tempfn;
wolfSSL 15:117db924cf7c 2769 }
wolfSSL 15:117db924cf7c 2770
wolfSSL 15:117db924cf7c 2771
wolfSSL 15:117db924cf7c 2772
wolfSSL 15:117db924cf7c 2773 #if defined(HAVE_SESSION_TICKET) && defined(HAVE_CHACHA) && \
wolfSSL 15:117db924cf7c 2774 defined(HAVE_POLY1305)
wolfSSL 15:117db924cf7c 2775
wolfSSL 15:117db924cf7c 2776 #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
wolfSSL 15:117db924cf7c 2777
wolfSSL 15:117db924cf7c 2778 typedef struct key_ctx {
wolfSSL 15:117db924cf7c 2779 byte name[WOLFSSL_TICKET_NAME_SZ]; /* name for this context */
wolfSSL 15:117db924cf7c 2780 byte key[CHACHA20_POLY1305_AEAD_KEYSIZE]; /* cipher key */
wolfSSL 15:117db924cf7c 2781 } key_ctx;
wolfSSL 15:117db924cf7c 2782
wolfSSL 15:117db924cf7c 2783 static key_ctx myKey_ctx;
wolfSSL 15:117db924cf7c 2784 static WC_RNG myKey_rng;
wolfSSL 15:117db924cf7c 2785
wolfSSL 15:117db924cf7c 2786 static WC_INLINE int TicketInit(void)
wolfSSL 15:117db924cf7c 2787 {
wolfSSL 15:117db924cf7c 2788 int ret = wc_InitRng(&myKey_rng);
wolfSSL 15:117db924cf7c 2789 if (ret != 0) return ret;
wolfSSL 15:117db924cf7c 2790
wolfSSL 15:117db924cf7c 2791 ret = wc_RNG_GenerateBlock(&myKey_rng, myKey_ctx.key, sizeof(myKey_ctx.key));
wolfSSL 15:117db924cf7c 2792 if (ret != 0) return ret;
wolfSSL 15:117db924cf7c 2793
wolfSSL 15:117db924cf7c 2794 ret = wc_RNG_GenerateBlock(&myKey_rng, myKey_ctx.name,sizeof(myKey_ctx.name));
wolfSSL 15:117db924cf7c 2795 if (ret != 0) return ret;
wolfSSL 15:117db924cf7c 2796
wolfSSL 15:117db924cf7c 2797 return 0;
wolfSSL 15:117db924cf7c 2798 }
wolfSSL 15:117db924cf7c 2799
wolfSSL 15:117db924cf7c 2800 static WC_INLINE void TicketCleanup(void)
wolfSSL 15:117db924cf7c 2801 {
wolfSSL 15:117db924cf7c 2802 wc_FreeRng(&myKey_rng);
wolfSSL 15:117db924cf7c 2803 }
wolfSSL 15:117db924cf7c 2804
wolfSSL 15:117db924cf7c 2805 static WC_INLINE int myTicketEncCb(WOLFSSL* ssl,
wolfSSL 15:117db924cf7c 2806 byte key_name[WOLFSSL_TICKET_NAME_SZ],
wolfSSL 15:117db924cf7c 2807 byte iv[WOLFSSL_TICKET_IV_SZ],
wolfSSL 15:117db924cf7c 2808 byte mac[WOLFSSL_TICKET_MAC_SZ],
wolfSSL 15:117db924cf7c 2809 int enc, byte* ticket, int inLen, int* outLen,
wolfSSL 15:117db924cf7c 2810 void* userCtx)
wolfSSL 15:117db924cf7c 2811 {
wolfSSL 15:117db924cf7c 2812 (void)ssl;
wolfSSL 15:117db924cf7c 2813 (void)userCtx;
wolfSSL 15:117db924cf7c 2814
wolfSSL 15:117db924cf7c 2815 int ret;
wolfSSL 15:117db924cf7c 2816 word16 sLen = XHTONS(inLen);
wolfSSL 15:117db924cf7c 2817 byte aad[WOLFSSL_TICKET_NAME_SZ + WOLFSSL_TICKET_IV_SZ + 2];
wolfSSL 15:117db924cf7c 2818 int aadSz = WOLFSSL_TICKET_NAME_SZ + WOLFSSL_TICKET_IV_SZ + 2;
wolfSSL 15:117db924cf7c 2819 byte* tmp = aad;
wolfSSL 15:117db924cf7c 2820
wolfSSL 15:117db924cf7c 2821 if (enc) {
wolfSSL 15:117db924cf7c 2822 XMEMCPY(key_name, myKey_ctx.name, WOLFSSL_TICKET_NAME_SZ);
wolfSSL 15:117db924cf7c 2823
wolfSSL 15:117db924cf7c 2824 ret = wc_RNG_GenerateBlock(&myKey_rng, iv, WOLFSSL_TICKET_IV_SZ);
wolfSSL 15:117db924cf7c 2825 if (ret != 0) return WOLFSSL_TICKET_RET_REJECT;
wolfSSL 15:117db924cf7c 2826
wolfSSL 15:117db924cf7c 2827 /* build aad from key name, iv, and length */
wolfSSL 15:117db924cf7c 2828 XMEMCPY(tmp, key_name, WOLFSSL_TICKET_NAME_SZ);
wolfSSL 15:117db924cf7c 2829 tmp += WOLFSSL_TICKET_NAME_SZ;
wolfSSL 15:117db924cf7c 2830 XMEMCPY(tmp, iv, WOLFSSL_TICKET_IV_SZ);
wolfSSL 15:117db924cf7c 2831 tmp += WOLFSSL_TICKET_IV_SZ;
wolfSSL 15:117db924cf7c 2832 XMEMCPY(tmp, &sLen, 2);
wolfSSL 15:117db924cf7c 2833
wolfSSL 15:117db924cf7c 2834 ret = wc_ChaCha20Poly1305_Encrypt(myKey_ctx.key, iv,
wolfSSL 15:117db924cf7c 2835 aad, aadSz,
wolfSSL 15:117db924cf7c 2836 ticket, inLen,
wolfSSL 15:117db924cf7c 2837 ticket,
wolfSSL 15:117db924cf7c 2838 mac);
wolfSSL 15:117db924cf7c 2839 if (ret != 0) return WOLFSSL_TICKET_RET_REJECT;
wolfSSL 15:117db924cf7c 2840 *outLen = inLen; /* no padding in this mode */
wolfSSL 15:117db924cf7c 2841 } else {
wolfSSL 15:117db924cf7c 2842 /* decrypt */
wolfSSL 15:117db924cf7c 2843
wolfSSL 15:117db924cf7c 2844 /* see if we know this key */
wolfSSL 15:117db924cf7c 2845 if (XMEMCMP(key_name, myKey_ctx.name, WOLFSSL_TICKET_NAME_SZ) != 0){
wolfSSL 15:117db924cf7c 2846 printf("client presented unknown ticket key name ");
wolfSSL 15:117db924cf7c 2847 return WOLFSSL_TICKET_RET_FATAL;
wolfSSL 15:117db924cf7c 2848 }
wolfSSL 15:117db924cf7c 2849
wolfSSL 15:117db924cf7c 2850 /* build aad from key name, iv, and length */
wolfSSL 15:117db924cf7c 2851 XMEMCPY(tmp, key_name, WOLFSSL_TICKET_NAME_SZ);
wolfSSL 15:117db924cf7c 2852 tmp += WOLFSSL_TICKET_NAME_SZ;
wolfSSL 15:117db924cf7c 2853 XMEMCPY(tmp, iv, WOLFSSL_TICKET_IV_SZ);
wolfSSL 15:117db924cf7c 2854 tmp += WOLFSSL_TICKET_IV_SZ;
wolfSSL 15:117db924cf7c 2855 XMEMCPY(tmp, &sLen, 2);
wolfSSL 15:117db924cf7c 2856
wolfSSL 15:117db924cf7c 2857 ret = wc_ChaCha20Poly1305_Decrypt(myKey_ctx.key, iv,
wolfSSL 15:117db924cf7c 2858 aad, aadSz,
wolfSSL 15:117db924cf7c 2859 ticket, inLen,
wolfSSL 15:117db924cf7c 2860 mac,
wolfSSL 15:117db924cf7c 2861 ticket);
wolfSSL 15:117db924cf7c 2862 if (ret != 0) return WOLFSSL_TICKET_RET_REJECT;
wolfSSL 15:117db924cf7c 2863 *outLen = inLen; /* no padding in this mode */
wolfSSL 15:117db924cf7c 2864 }
wolfSSL 15:117db924cf7c 2865
wolfSSL 15:117db924cf7c 2866 return WOLFSSL_TICKET_RET_OK;
wolfSSL 15:117db924cf7c 2867 }
wolfSSL 15:117db924cf7c 2868
wolfSSL 15:117db924cf7c 2869 #endif /* HAVE_SESSION_TICKET && CHACHA20 && POLY1305 */
wolfSSL 15:117db924cf7c 2870
wolfSSL 15:117db924cf7c 2871 static WC_INLINE word16 GetRandomPort(void)
wolfSSL 15:117db924cf7c 2872 {
wolfSSL 15:117db924cf7c 2873 word16 port = 0;
wolfSSL 15:117db924cf7c 2874
wolfSSL 15:117db924cf7c 2875 /* Generate random port for testing */
wolfSSL 15:117db924cf7c 2876 WC_RNG rng;
wolfSSL 15:117db924cf7c 2877 if (wc_InitRng(&rng) == 0) {
wolfSSL 15:117db924cf7c 2878 wc_RNG_GenerateBlock(&rng, (byte*)&port, sizeof(port));
wolfSSL 15:117db924cf7c 2879 port |= 0xC000; /* Make sure its in the 49152 - 65535 range */
wolfSSL 15:117db924cf7c 2880 wc_FreeRng(&rng);
wolfSSL 15:117db924cf7c 2881 }
wolfSSL 15:117db924cf7c 2882 return port;
wolfSSL 15:117db924cf7c 2883 }
wolfSSL 15:117db924cf7c 2884
wolfSSL 15:117db924cf7c 2885 #endif /* wolfSSL_TEST_H */
wolfSSL 15:117db924cf7c 2886