SSL/TLS Library
CyaSSL is SSL/TLS library for embedded systems.
cyassl/test.h@0:9d17e4342598, 2014-04-20 (annotated)
- Committer:
- wolfSSL
- Date:
- Sun Apr 20 12:40:57 2014 +0000
- Revision:
- 0:9d17e4342598
CyaSSL SSL/TLS Library 2.9.4;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 0:9d17e4342598 | 1 | /* test.h */ |
wolfSSL | 0:9d17e4342598 | 2 | |
wolfSSL | 0:9d17e4342598 | 3 | #ifndef CyaSSL_TEST_H |
wolfSSL | 0:9d17e4342598 | 4 | #define CyaSSL_TEST_H |
wolfSSL | 0:9d17e4342598 | 5 | |
wolfSSL | 0:9d17e4342598 | 6 | #include <stdio.h> |
wolfSSL | 0:9d17e4342598 | 7 | #include <stdlib.h> |
wolfSSL | 0:9d17e4342598 | 8 | #include <assert.h> |
wolfSSL | 0:9d17e4342598 | 9 | #include <ctype.h> |
wolfSSL | 0:9d17e4342598 | 10 | #include <cyassl/ssl.h> |
wolfSSL | 0:9d17e4342598 | 11 | #include <cyassl/ctaocrypt/types.h> |
wolfSSL | 0:9d17e4342598 | 12 | |
wolfSSL | 0:9d17e4342598 | 13 | #ifdef ATOMIC_USER |
wolfSSL | 0:9d17e4342598 | 14 | #include <cyassl/ctaocrypt/aes.h> |
wolfSSL | 0:9d17e4342598 | 15 | #include <cyassl/ctaocrypt/arc4.h> |
wolfSSL | 0:9d17e4342598 | 16 | #include <cyassl/ctaocrypt/hmac.h> |
wolfSSL | 0:9d17e4342598 | 17 | #endif |
wolfSSL | 0:9d17e4342598 | 18 | #ifdef HAVE_PK_CALLBACKS |
wolfSSL | 0:9d17e4342598 | 19 | #include <cyassl/ctaocrypt/random.h> |
wolfSSL | 0:9d17e4342598 | 20 | #include <cyassl/ctaocrypt/asn.h> |
wolfSSL | 0:9d17e4342598 | 21 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 22 | #include <cyassl/ctaocrypt/ecc.h> |
wolfSSL | 0:9d17e4342598 | 23 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 24 | #endif /*HAVE_PK_CALLBACKS */ |
wolfSSL | 0:9d17e4342598 | 25 | |
wolfSSL | 0:9d17e4342598 | 26 | #ifdef USE_WINDOWS_API |
wolfSSL | 0:9d17e4342598 | 27 | #include <winsock2.h> |
wolfSSL | 0:9d17e4342598 | 28 | #include <process.h> |
wolfSSL | 0:9d17e4342598 | 29 | #ifdef TEST_IPV6 /* don't require newer SDK for IPV4 */ |
wolfSSL | 0:9d17e4342598 | 30 | #include <ws2tcpip.h> |
wolfSSL | 0:9d17e4342598 | 31 | #include <wspiapi.h> |
wolfSSL | 0:9d17e4342598 | 32 | #endif |
wolfSSL | 0:9d17e4342598 | 33 | #define SOCKET_T SOCKET |
wolfSSL | 0:9d17e4342598 | 34 | #define SNPRINTF _snprintf |
wolfSSL | 0:9d17e4342598 | 35 | #elif defined(CYASSL_MDK_ARM) |
wolfSSL | 0:9d17e4342598 | 36 | #include <string.h> |
wolfSSL | 0:9d17e4342598 | 37 | #else |
wolfSSL | 0:9d17e4342598 | 38 | #include <string.h> |
wolfSSL | 0:9d17e4342598 | 39 | #include <sys/types.h> |
wolfSSL | 0:9d17e4342598 | 40 | #ifndef CYASSL_LEANPSK |
wolfSSL | 0:9d17e4342598 | 41 | #include <unistd.h> |
wolfSSL | 0:9d17e4342598 | 42 | #include <netdb.h> |
wolfSSL | 0:9d17e4342598 | 43 | #include <netinet/in.h> |
wolfSSL | 0:9d17e4342598 | 44 | #include <netinet/tcp.h> |
wolfSSL | 0:9d17e4342598 | 45 | #include <arpa/inet.h> |
wolfSSL | 0:9d17e4342598 | 46 | #include <sys/ioctl.h> |
wolfSSL | 0:9d17e4342598 | 47 | #include <sys/time.h> |
wolfSSL | 0:9d17e4342598 | 48 | #include <sys/socket.h> |
wolfSSL | 0:9d17e4342598 | 49 | #include <pthread.h> |
wolfSSL | 0:9d17e4342598 | 50 | #include <fcntl.h> |
wolfSSL | 0:9d17e4342598 | 51 | #ifdef TEST_IPV6 |
wolfSSL | 0:9d17e4342598 | 52 | #include <netdb.h> |
wolfSSL | 0:9d17e4342598 | 53 | #endif |
wolfSSL | 0:9d17e4342598 | 54 | #endif |
wolfSSL | 0:9d17e4342598 | 55 | #define SOCKET_T int |
wolfSSL | 0:9d17e4342598 | 56 | #ifndef SO_NOSIGPIPE |
wolfSSL | 0:9d17e4342598 | 57 | #include <signal.h> /* ignore SIGPIPE */ |
wolfSSL | 0:9d17e4342598 | 58 | #endif |
wolfSSL | 0:9d17e4342598 | 59 | #define SNPRINTF snprintf |
wolfSSL | 0:9d17e4342598 | 60 | #endif /* USE_WINDOWS_API */ |
wolfSSL | 0:9d17e4342598 | 61 | |
wolfSSL | 0:9d17e4342598 | 62 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 63 | #include "cavium_sysdep.h" |
wolfSSL | 0:9d17e4342598 | 64 | #include "cavium_common.h" |
wolfSSL | 0:9d17e4342598 | 65 | #include "cavium_ioctl.h" |
wolfSSL | 0:9d17e4342598 | 66 | #endif |
wolfSSL | 0:9d17e4342598 | 67 | |
wolfSSL | 0:9d17e4342598 | 68 | #ifdef _MSC_VER |
wolfSSL | 0:9d17e4342598 | 69 | /* disable conversion warning */ |
wolfSSL | 0:9d17e4342598 | 70 | /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */ |
wolfSSL | 0:9d17e4342598 | 71 | #pragma warning(disable:4244 4996) |
wolfSSL | 0:9d17e4342598 | 72 | #endif |
wolfSSL | 0:9d17e4342598 | 73 | |
wolfSSL | 0:9d17e4342598 | 74 | |
wolfSSL | 0:9d17e4342598 | 75 | #if defined(__MACH__) || defined(USE_WINDOWS_API) |
wolfSSL | 0:9d17e4342598 | 76 | #ifndef _SOCKLEN_T |
wolfSSL | 0:9d17e4342598 | 77 | typedef int socklen_t; |
wolfSSL | 0:9d17e4342598 | 78 | #endif |
wolfSSL | 0:9d17e4342598 | 79 | #endif |
wolfSSL | 0:9d17e4342598 | 80 | |
wolfSSL | 0:9d17e4342598 | 81 | |
wolfSSL | 0:9d17e4342598 | 82 | /* HPUX doesn't use socklent_t for third parameter to accept, unless |
wolfSSL | 0:9d17e4342598 | 83 | _XOPEN_SOURCE_EXTENDED is defined */ |
wolfSSL | 0:9d17e4342598 | 84 | #if !defined(__hpux__) && !defined(CYASSL_MDK_ARM) && !defined(CYASSL_IAR_ARM) |
wolfSSL | 0:9d17e4342598 | 85 | typedef socklen_t* ACCEPT_THIRD_T; |
wolfSSL | 0:9d17e4342598 | 86 | #else |
wolfSSL | 0:9d17e4342598 | 87 | #if defined _XOPEN_SOURCE_EXTENDED |
wolfSSL | 0:9d17e4342598 | 88 | typedef socklen_t* ACCEPT_THIRD_T; |
wolfSSL | 0:9d17e4342598 | 89 | #else |
wolfSSL | 0:9d17e4342598 | 90 | typedef int* ACCEPT_THIRD_T; |
wolfSSL | 0:9d17e4342598 | 91 | #endif |
wolfSSL | 0:9d17e4342598 | 92 | #endif |
wolfSSL | 0:9d17e4342598 | 93 | |
wolfSSL | 0:9d17e4342598 | 94 | |
wolfSSL | 0:9d17e4342598 | 95 | #ifdef USE_WINDOWS_API |
wolfSSL | 0:9d17e4342598 | 96 | #define CloseSocket(s) closesocket(s) |
wolfSSL | 0:9d17e4342598 | 97 | #define StartTCP() { WSADATA wsd; WSAStartup(0x0002, &wsd); } |
wolfSSL | 0:9d17e4342598 | 98 | #elif defined(CYASSL_MDK_ARM) |
wolfSSL | 0:9d17e4342598 | 99 | #define CloseSocket(s) closesocket(s) |
wolfSSL | 0:9d17e4342598 | 100 | #define StartTCP() |
wolfSSL | 0:9d17e4342598 | 101 | #else |
wolfSSL | 0:9d17e4342598 | 102 | #define CloseSocket(s) close(s) |
wolfSSL | 0:9d17e4342598 | 103 | #define StartTCP() |
wolfSSL | 0:9d17e4342598 | 104 | #endif |
wolfSSL | 0:9d17e4342598 | 105 | |
wolfSSL | 0:9d17e4342598 | 106 | |
wolfSSL | 0:9d17e4342598 | 107 | #ifdef SINGLE_THREADED |
wolfSSL | 0:9d17e4342598 | 108 | typedef unsigned int THREAD_RETURN; |
wolfSSL | 0:9d17e4342598 | 109 | typedef void* THREAD_TYPE; |
wolfSSL | 0:9d17e4342598 | 110 | #define CYASSL_THREAD |
wolfSSL | 0:9d17e4342598 | 111 | #else |
wolfSSL | 0:9d17e4342598 | 112 | #if defined(_POSIX_THREADS) && !defined(__MINGW32__) |
wolfSSL | 0:9d17e4342598 | 113 | typedef void* THREAD_RETURN; |
wolfSSL | 0:9d17e4342598 | 114 | typedef pthread_t THREAD_TYPE; |
wolfSSL | 0:9d17e4342598 | 115 | #define CYASSL_THREAD |
wolfSSL | 0:9d17e4342598 | 116 | #define INFINITE -1 |
wolfSSL | 0:9d17e4342598 | 117 | #define WAIT_OBJECT_0 0L |
wolfSSL | 0:9d17e4342598 | 118 | #elif defined(CYASSL_MDK_ARM) |
wolfSSL | 0:9d17e4342598 | 119 | typedef unsigned int THREAD_RETURN; |
wolfSSL | 0:9d17e4342598 | 120 | typedef int THREAD_TYPE; |
wolfSSL | 0:9d17e4342598 | 121 | #define CYASSL_THREAD |
wolfSSL | 0:9d17e4342598 | 122 | #else |
wolfSSL | 0:9d17e4342598 | 123 | typedef unsigned int THREAD_RETURN; |
wolfSSL | 0:9d17e4342598 | 124 | typedef intptr_t THREAD_TYPE; |
wolfSSL | 0:9d17e4342598 | 125 | #define CYASSL_THREAD __stdcall |
wolfSSL | 0:9d17e4342598 | 126 | #endif |
wolfSSL | 0:9d17e4342598 | 127 | #endif |
wolfSSL | 0:9d17e4342598 | 128 | |
wolfSSL | 0:9d17e4342598 | 129 | |
wolfSSL | 0:9d17e4342598 | 130 | #ifdef TEST_IPV6 |
wolfSSL | 0:9d17e4342598 | 131 | typedef struct sockaddr_in6 SOCKADDR_IN_T; |
wolfSSL | 0:9d17e4342598 | 132 | #define AF_INET_V AF_INET6 |
wolfSSL | 0:9d17e4342598 | 133 | #else |
wolfSSL | 0:9d17e4342598 | 134 | typedef struct sockaddr_in SOCKADDR_IN_T; |
wolfSSL | 0:9d17e4342598 | 135 | #define AF_INET_V AF_INET |
wolfSSL | 0:9d17e4342598 | 136 | #endif |
wolfSSL | 0:9d17e4342598 | 137 | |
wolfSSL | 0:9d17e4342598 | 138 | |
wolfSSL | 0:9d17e4342598 | 139 | #define SERVER_DEFAULT_VERSION 3 |
wolfSSL | 0:9d17e4342598 | 140 | #define SERVER_DTLS_DEFAULT_VERSION (-2) |
wolfSSL | 0:9d17e4342598 | 141 | #define SERVER_INVALID_VERSION (-99) |
wolfSSL | 0:9d17e4342598 | 142 | #define CLIENT_DEFAULT_VERSION 3 |
wolfSSL | 0:9d17e4342598 | 143 | #define CLIENT_DTLS_DEFAULT_VERSION (-2) |
wolfSSL | 0:9d17e4342598 | 144 | #define CLIENT_INVALID_VERSION (-99) |
wolfSSL | 0:9d17e4342598 | 145 | |
wolfSSL | 0:9d17e4342598 | 146 | /* all certs relative to CyaSSL home directory now */ |
wolfSSL | 0:9d17e4342598 | 147 | #define caCert "./certs/ca-cert.pem" |
wolfSSL | 0:9d17e4342598 | 148 | #define eccCert "./certs/server-ecc.pem" |
wolfSSL | 0:9d17e4342598 | 149 | #define eccKey "./certs/ecc-key.pem" |
wolfSSL | 0:9d17e4342598 | 150 | #define svrCert "./certs/server-cert.pem" |
wolfSSL | 0:9d17e4342598 | 151 | #define svrKey "./certs/server-key.pem" |
wolfSSL | 0:9d17e4342598 | 152 | #define cliCert "./certs/client-cert.pem" |
wolfSSL | 0:9d17e4342598 | 153 | #define cliKey "./certs/client-key.pem" |
wolfSSL | 0:9d17e4342598 | 154 | #define ntruCert "./certs/ntru-cert.pem" |
wolfSSL | 0:9d17e4342598 | 155 | #define ntruKey "./certs/ntru-key.raw" |
wolfSSL | 0:9d17e4342598 | 156 | #define dhParam "./certs/dh2048.pem" |
wolfSSL | 0:9d17e4342598 | 157 | #define cliEccKey "./certs/ecc-client-key.pem" |
wolfSSL | 0:9d17e4342598 | 158 | #define cliEccCert "./certs/client-ecc-cert.pem" |
wolfSSL | 0:9d17e4342598 | 159 | #define crlPemDir "./certs/crl" |
wolfSSL | 0:9d17e4342598 | 160 | |
wolfSSL | 0:9d17e4342598 | 161 | typedef struct tcp_ready { |
wolfSSL | 0:9d17e4342598 | 162 | word16 ready; /* predicate */ |
wolfSSL | 0:9d17e4342598 | 163 | word16 port; |
wolfSSL | 0:9d17e4342598 | 164 | #if defined(_POSIX_THREADS) && !defined(__MINGW32__) |
wolfSSL | 0:9d17e4342598 | 165 | pthread_mutex_t mutex; |
wolfSSL | 0:9d17e4342598 | 166 | pthread_cond_t cond; |
wolfSSL | 0:9d17e4342598 | 167 | #endif |
wolfSSL | 0:9d17e4342598 | 168 | } tcp_ready; |
wolfSSL | 0:9d17e4342598 | 169 | |
wolfSSL | 0:9d17e4342598 | 170 | |
wolfSSL | 0:9d17e4342598 | 171 | void InitTcpReady(tcp_ready*); |
wolfSSL | 0:9d17e4342598 | 172 | void FreeTcpReady(tcp_ready*); |
wolfSSL | 0:9d17e4342598 | 173 | |
wolfSSL | 0:9d17e4342598 | 174 | typedef CYASSL_METHOD* (*method_provider)(void); |
wolfSSL | 0:9d17e4342598 | 175 | typedef void (*ctx_callback)(CYASSL_CTX* ctx); |
wolfSSL | 0:9d17e4342598 | 176 | typedef void (*ssl_callback)(CYASSL* ssl); |
wolfSSL | 0:9d17e4342598 | 177 | |
wolfSSL | 0:9d17e4342598 | 178 | typedef struct callback_functions { |
wolfSSL | 0:9d17e4342598 | 179 | method_provider method; |
wolfSSL | 0:9d17e4342598 | 180 | ctx_callback ctx_ready; |
wolfSSL | 0:9d17e4342598 | 181 | ssl_callback ssl_ready; |
wolfSSL | 0:9d17e4342598 | 182 | ssl_callback on_result; |
wolfSSL | 0:9d17e4342598 | 183 | } callback_functions; |
wolfSSL | 0:9d17e4342598 | 184 | |
wolfSSL | 0:9d17e4342598 | 185 | typedef struct func_args { |
wolfSSL | 0:9d17e4342598 | 186 | int argc; |
wolfSSL | 0:9d17e4342598 | 187 | char** argv; |
wolfSSL | 0:9d17e4342598 | 188 | int return_code; |
wolfSSL | 0:9d17e4342598 | 189 | tcp_ready* signal; |
wolfSSL | 0:9d17e4342598 | 190 | callback_functions *callbacks; |
wolfSSL | 0:9d17e4342598 | 191 | } func_args; |
wolfSSL | 0:9d17e4342598 | 192 | |
wolfSSL | 0:9d17e4342598 | 193 | void wait_tcp_ready(func_args*); |
wolfSSL | 0:9d17e4342598 | 194 | |
wolfSSL | 0:9d17e4342598 | 195 | typedef THREAD_RETURN CYASSL_THREAD THREAD_FUNC(void*); |
wolfSSL | 0:9d17e4342598 | 196 | |
wolfSSL | 0:9d17e4342598 | 197 | void start_thread(THREAD_FUNC, func_args*, THREAD_TYPE*); |
wolfSSL | 0:9d17e4342598 | 198 | void join_thread(THREAD_TYPE); |
wolfSSL | 0:9d17e4342598 | 199 | |
wolfSSL | 0:9d17e4342598 | 200 | /* yaSSL */ |
wolfSSL | 0:9d17e4342598 | 201 | #ifndef TEST_IPV6 |
wolfSSL | 0:9d17e4342598 | 202 | static const char* const yasslIP = "127.0.0.1"; |
wolfSSL | 0:9d17e4342598 | 203 | #else |
wolfSSL | 0:9d17e4342598 | 204 | static const char* const yasslIP = "::1"; |
wolfSSL | 0:9d17e4342598 | 205 | #endif |
wolfSSL | 0:9d17e4342598 | 206 | static const word16 yasslPort = 11111; |
wolfSSL | 0:9d17e4342598 | 207 | |
wolfSSL | 0:9d17e4342598 | 208 | static INLINE void err_sys(const char* msg) |
wolfSSL | 0:9d17e4342598 | 209 | { |
wolfSSL | 0:9d17e4342598 | 210 | printf("yassl error: %s\n", msg); |
wolfSSL | 0:9d17e4342598 | 211 | if (msg) |
wolfSSL | 0:9d17e4342598 | 212 | exit(EXIT_FAILURE); |
wolfSSL | 0:9d17e4342598 | 213 | } |
wolfSSL | 0:9d17e4342598 | 214 | |
wolfSSL | 0:9d17e4342598 | 215 | |
wolfSSL | 0:9d17e4342598 | 216 | #define MY_EX_USAGE 2 |
wolfSSL | 0:9d17e4342598 | 217 | |
wolfSSL | 0:9d17e4342598 | 218 | extern int myoptind; |
wolfSSL | 0:9d17e4342598 | 219 | extern char* myoptarg; |
wolfSSL | 0:9d17e4342598 | 220 | |
wolfSSL | 0:9d17e4342598 | 221 | static INLINE int mygetopt(int argc, char** argv, const char* optstring) |
wolfSSL | 0:9d17e4342598 | 222 | { |
wolfSSL | 0:9d17e4342598 | 223 | static char* next = NULL; |
wolfSSL | 0:9d17e4342598 | 224 | |
wolfSSL | 0:9d17e4342598 | 225 | char c; |
wolfSSL | 0:9d17e4342598 | 226 | char* cp; |
wolfSSL | 0:9d17e4342598 | 227 | |
wolfSSL | 0:9d17e4342598 | 228 | if (myoptind == 0) |
wolfSSL | 0:9d17e4342598 | 229 | next = NULL; /* we're starting new/over */ |
wolfSSL | 0:9d17e4342598 | 230 | |
wolfSSL | 0:9d17e4342598 | 231 | if (next == NULL || *next == '\0') { |
wolfSSL | 0:9d17e4342598 | 232 | if (myoptind == 0) |
wolfSSL | 0:9d17e4342598 | 233 | myoptind++; |
wolfSSL | 0:9d17e4342598 | 234 | |
wolfSSL | 0:9d17e4342598 | 235 | if (myoptind >= argc || argv[myoptind][0] != '-' || |
wolfSSL | 0:9d17e4342598 | 236 | argv[myoptind][1] == '\0') { |
wolfSSL | 0:9d17e4342598 | 237 | myoptarg = NULL; |
wolfSSL | 0:9d17e4342598 | 238 | if (myoptind < argc) |
wolfSSL | 0:9d17e4342598 | 239 | myoptarg = argv[myoptind]; |
wolfSSL | 0:9d17e4342598 | 240 | |
wolfSSL | 0:9d17e4342598 | 241 | return -1; |
wolfSSL | 0:9d17e4342598 | 242 | } |
wolfSSL | 0:9d17e4342598 | 243 | |
wolfSSL | 0:9d17e4342598 | 244 | if (strcmp(argv[myoptind], "--") == 0) { |
wolfSSL | 0:9d17e4342598 | 245 | myoptind++; |
wolfSSL | 0:9d17e4342598 | 246 | myoptarg = NULL; |
wolfSSL | 0:9d17e4342598 | 247 | |
wolfSSL | 0:9d17e4342598 | 248 | if (myoptind < argc) |
wolfSSL | 0:9d17e4342598 | 249 | myoptarg = argv[myoptind]; |
wolfSSL | 0:9d17e4342598 | 250 | |
wolfSSL | 0:9d17e4342598 | 251 | return -1; |
wolfSSL | 0:9d17e4342598 | 252 | } |
wolfSSL | 0:9d17e4342598 | 253 | |
wolfSSL | 0:9d17e4342598 | 254 | next = argv[myoptind]; |
wolfSSL | 0:9d17e4342598 | 255 | next++; /* skip - */ |
wolfSSL | 0:9d17e4342598 | 256 | myoptind++; |
wolfSSL | 0:9d17e4342598 | 257 | } |
wolfSSL | 0:9d17e4342598 | 258 | |
wolfSSL | 0:9d17e4342598 | 259 | c = *next++; |
wolfSSL | 0:9d17e4342598 | 260 | /* The C++ strchr can return a different value */ |
wolfSSL | 0:9d17e4342598 | 261 | cp = (char*)strchr(optstring, c); |
wolfSSL | 0:9d17e4342598 | 262 | |
wolfSSL | 0:9d17e4342598 | 263 | if (cp == NULL || c == ':') |
wolfSSL | 0:9d17e4342598 | 264 | return '?'; |
wolfSSL | 0:9d17e4342598 | 265 | |
wolfSSL | 0:9d17e4342598 | 266 | cp++; |
wolfSSL | 0:9d17e4342598 | 267 | |
wolfSSL | 0:9d17e4342598 | 268 | if (*cp == ':') { |
wolfSSL | 0:9d17e4342598 | 269 | if (*next != '\0') { |
wolfSSL | 0:9d17e4342598 | 270 | myoptarg = next; |
wolfSSL | 0:9d17e4342598 | 271 | next = NULL; |
wolfSSL | 0:9d17e4342598 | 272 | } |
wolfSSL | 0:9d17e4342598 | 273 | else if (myoptind < argc) { |
wolfSSL | 0:9d17e4342598 | 274 | myoptarg = argv[myoptind]; |
wolfSSL | 0:9d17e4342598 | 275 | myoptind++; |
wolfSSL | 0:9d17e4342598 | 276 | } |
wolfSSL | 0:9d17e4342598 | 277 | else |
wolfSSL | 0:9d17e4342598 | 278 | return '?'; |
wolfSSL | 0:9d17e4342598 | 279 | } |
wolfSSL | 0:9d17e4342598 | 280 | |
wolfSSL | 0:9d17e4342598 | 281 | return c; |
wolfSSL | 0:9d17e4342598 | 282 | } |
wolfSSL | 0:9d17e4342598 | 283 | |
wolfSSL | 0:9d17e4342598 | 284 | |
wolfSSL | 0:9d17e4342598 | 285 | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) |
wolfSSL | 0:9d17e4342598 | 286 | |
wolfSSL | 0:9d17e4342598 | 287 | static INLINE int PasswordCallBack(char* passwd, int sz, int rw, void* userdata) |
wolfSSL | 0:9d17e4342598 | 288 | { |
wolfSSL | 0:9d17e4342598 | 289 | (void)rw; |
wolfSSL | 0:9d17e4342598 | 290 | (void)userdata; |
wolfSSL | 0:9d17e4342598 | 291 | strncpy(passwd, "yassl123", sz); |
wolfSSL | 0:9d17e4342598 | 292 | return 8; |
wolfSSL | 0:9d17e4342598 | 293 | } |
wolfSSL | 0:9d17e4342598 | 294 | |
wolfSSL | 0:9d17e4342598 | 295 | #endif |
wolfSSL | 0:9d17e4342598 | 296 | |
wolfSSL | 0:9d17e4342598 | 297 | |
wolfSSL | 0:9d17e4342598 | 298 | #if defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) |
wolfSSL | 0:9d17e4342598 | 299 | |
wolfSSL | 0:9d17e4342598 | 300 | static INLINE void ShowX509(CYASSL_X509* x509, const char* hdr) |
wolfSSL | 0:9d17e4342598 | 301 | { |
wolfSSL | 0:9d17e4342598 | 302 | char* altName; |
wolfSSL | 0:9d17e4342598 | 303 | char* issuer = CyaSSL_X509_NAME_oneline( |
wolfSSL | 0:9d17e4342598 | 304 | CyaSSL_X509_get_issuer_name(x509), 0, 0); |
wolfSSL | 0:9d17e4342598 | 305 | char* subject = CyaSSL_X509_NAME_oneline( |
wolfSSL | 0:9d17e4342598 | 306 | CyaSSL_X509_get_subject_name(x509), 0, 0); |
wolfSSL | 0:9d17e4342598 | 307 | byte serial[32]; |
wolfSSL | 0:9d17e4342598 | 308 | int ret; |
wolfSSL | 0:9d17e4342598 | 309 | int sz = sizeof(serial); |
wolfSSL | 0:9d17e4342598 | 310 | |
wolfSSL | 0:9d17e4342598 | 311 | printf("%s\n issuer : %s\n subject: %s\n", hdr, issuer, subject); |
wolfSSL | 0:9d17e4342598 | 312 | |
wolfSSL | 0:9d17e4342598 | 313 | while ( (altName = CyaSSL_X509_get_next_altname(x509)) != NULL) |
wolfSSL | 0:9d17e4342598 | 314 | printf(" altname = %s\n", altName); |
wolfSSL | 0:9d17e4342598 | 315 | |
wolfSSL | 0:9d17e4342598 | 316 | ret = CyaSSL_X509_get_serial_number(x509, serial, &sz); |
wolfSSL | 0:9d17e4342598 | 317 | if (ret == SSL_SUCCESS) { |
wolfSSL | 0:9d17e4342598 | 318 | int i; |
wolfSSL | 0:9d17e4342598 | 319 | int strLen; |
wolfSSL | 0:9d17e4342598 | 320 | char serialMsg[80]; |
wolfSSL | 0:9d17e4342598 | 321 | |
wolfSSL | 0:9d17e4342598 | 322 | /* testsuite has multiple threads writing to stdout, get output |
wolfSSL | 0:9d17e4342598 | 323 | message ready to write once */ |
wolfSSL | 0:9d17e4342598 | 324 | strLen = sprintf(serialMsg, " serial number"); |
wolfSSL | 0:9d17e4342598 | 325 | for (i = 0; i < sz; i++) |
wolfSSL | 0:9d17e4342598 | 326 | sprintf(serialMsg + strLen + (i*3), ":%02x ", serial[i]); |
wolfSSL | 0:9d17e4342598 | 327 | printf("%s\n", serialMsg); |
wolfSSL | 0:9d17e4342598 | 328 | } |
wolfSSL | 0:9d17e4342598 | 329 | |
wolfSSL | 0:9d17e4342598 | 330 | XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL); |
wolfSSL | 0:9d17e4342598 | 331 | XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL); |
wolfSSL | 0:9d17e4342598 | 332 | } |
wolfSSL | 0:9d17e4342598 | 333 | |
wolfSSL | 0:9d17e4342598 | 334 | #endif /* KEEP_PEER_CERT || SESSION_CERTS */ |
wolfSSL | 0:9d17e4342598 | 335 | |
wolfSSL | 0:9d17e4342598 | 336 | |
wolfSSL | 0:9d17e4342598 | 337 | static INLINE void showPeer(CYASSL* ssl) |
wolfSSL | 0:9d17e4342598 | 338 | { |
wolfSSL | 0:9d17e4342598 | 339 | |
wolfSSL | 0:9d17e4342598 | 340 | CYASSL_CIPHER* cipher; |
wolfSSL | 0:9d17e4342598 | 341 | #ifdef KEEP_PEER_CERT |
wolfSSL | 0:9d17e4342598 | 342 | CYASSL_X509* peer = CyaSSL_get_peer_certificate(ssl); |
wolfSSL | 0:9d17e4342598 | 343 | if (peer) |
wolfSSL | 0:9d17e4342598 | 344 | ShowX509(peer, "peer's cert info:"); |
wolfSSL | 0:9d17e4342598 | 345 | else |
wolfSSL | 0:9d17e4342598 | 346 | printf("peer has no cert!\n"); |
wolfSSL | 0:9d17e4342598 | 347 | #endif |
wolfSSL | 0:9d17e4342598 | 348 | printf("SSL version is %s\n", CyaSSL_get_version(ssl)); |
wolfSSL | 0:9d17e4342598 | 349 | |
wolfSSL | 0:9d17e4342598 | 350 | cipher = CyaSSL_get_current_cipher(ssl); |
wolfSSL | 0:9d17e4342598 | 351 | printf("SSL cipher suite is %s\n", CyaSSL_CIPHER_get_name(cipher)); |
wolfSSL | 0:9d17e4342598 | 352 | |
wolfSSL | 0:9d17e4342598 | 353 | #if defined(SESSION_CERTS) && defined(SHOW_CERTS) |
wolfSSL | 0:9d17e4342598 | 354 | { |
wolfSSL | 0:9d17e4342598 | 355 | CYASSL_X509_CHAIN* chain = CyaSSL_get_peer_chain(ssl); |
wolfSSL | 0:9d17e4342598 | 356 | int count = CyaSSL_get_chain_count(chain); |
wolfSSL | 0:9d17e4342598 | 357 | int i; |
wolfSSL | 0:9d17e4342598 | 358 | |
wolfSSL | 0:9d17e4342598 | 359 | for (i = 0; i < count; i++) { |
wolfSSL | 0:9d17e4342598 | 360 | int length; |
wolfSSL | 0:9d17e4342598 | 361 | unsigned char buffer[3072]; |
wolfSSL | 0:9d17e4342598 | 362 | CYASSL_X509* chainX509; |
wolfSSL | 0:9d17e4342598 | 363 | |
wolfSSL | 0:9d17e4342598 | 364 | CyaSSL_get_chain_cert_pem(chain,i,buffer, sizeof(buffer), &length); |
wolfSSL | 0:9d17e4342598 | 365 | buffer[length] = 0; |
wolfSSL | 0:9d17e4342598 | 366 | printf("cert %d has length %d data = \n%s\n", i, length, buffer); |
wolfSSL | 0:9d17e4342598 | 367 | |
wolfSSL | 0:9d17e4342598 | 368 | chainX509 = CyaSSL_get_chain_X509(chain, i); |
wolfSSL | 0:9d17e4342598 | 369 | if (chainX509) |
wolfSSL | 0:9d17e4342598 | 370 | ShowX509(chainX509, "session cert info:"); |
wolfSSL | 0:9d17e4342598 | 371 | else |
wolfSSL | 0:9d17e4342598 | 372 | printf("get_chain_X509 failed\n"); |
wolfSSL | 0:9d17e4342598 | 373 | CyaSSL_FreeX509(chainX509); |
wolfSSL | 0:9d17e4342598 | 374 | } |
wolfSSL | 0:9d17e4342598 | 375 | } |
wolfSSL | 0:9d17e4342598 | 376 | #endif |
wolfSSL | 0:9d17e4342598 | 377 | (void)ssl; |
wolfSSL | 0:9d17e4342598 | 378 | } |
wolfSSL | 0:9d17e4342598 | 379 | |
wolfSSL | 0:9d17e4342598 | 380 | |
wolfSSL | 0:9d17e4342598 | 381 | static INLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer, |
wolfSSL | 0:9d17e4342598 | 382 | word16 port, int udp) |
wolfSSL | 0:9d17e4342598 | 383 | { |
wolfSSL | 0:9d17e4342598 | 384 | int useLookup = 0; |
wolfSSL | 0:9d17e4342598 | 385 | (void)useLookup; |
wolfSSL | 0:9d17e4342598 | 386 | (void)udp; |
wolfSSL | 0:9d17e4342598 | 387 | |
wolfSSL | 0:9d17e4342598 | 388 | memset(addr, 0, sizeof(SOCKADDR_IN_T)); |
wolfSSL | 0:9d17e4342598 | 389 | |
wolfSSL | 0:9d17e4342598 | 390 | #ifndef TEST_IPV6 |
wolfSSL | 0:9d17e4342598 | 391 | /* peer could be in human readable form */ |
wolfSSL | 0:9d17e4342598 | 392 | if ( (peer != INADDR_ANY) && isalpha((int)peer[0])) { |
wolfSSL | 0:9d17e4342598 | 393 | #ifdef CYASSL_MDK_ARM |
wolfSSL | 0:9d17e4342598 | 394 | int err; |
wolfSSL | 0:9d17e4342598 | 395 | struct hostent* entry = gethostbyname(peer, &err); |
wolfSSL | 0:9d17e4342598 | 396 | #else |
wolfSSL | 0:9d17e4342598 | 397 | struct hostent* entry = gethostbyname(peer); |
wolfSSL | 0:9d17e4342598 | 398 | #endif |
wolfSSL | 0:9d17e4342598 | 399 | |
wolfSSL | 0:9d17e4342598 | 400 | if (entry) { |
wolfSSL | 0:9d17e4342598 | 401 | memcpy(&addr->sin_addr.s_addr, entry->h_addr_list[0], |
wolfSSL | 0:9d17e4342598 | 402 | entry->h_length); |
wolfSSL | 0:9d17e4342598 | 403 | useLookup = 1; |
wolfSSL | 0:9d17e4342598 | 404 | } |
wolfSSL | 0:9d17e4342598 | 405 | else |
wolfSSL | 0:9d17e4342598 | 406 | err_sys("no entry for host"); |
wolfSSL | 0:9d17e4342598 | 407 | } |
wolfSSL | 0:9d17e4342598 | 408 | #endif |
wolfSSL | 0:9d17e4342598 | 409 | |
wolfSSL | 0:9d17e4342598 | 410 | |
wolfSSL | 0:9d17e4342598 | 411 | #ifndef TEST_IPV6 |
wolfSSL | 0:9d17e4342598 | 412 | #if defined(CYASSL_MDK_ARM) |
wolfSSL | 0:9d17e4342598 | 413 | addr->sin_family = PF_INET; |
wolfSSL | 0:9d17e4342598 | 414 | #else |
wolfSSL | 0:9d17e4342598 | 415 | addr->sin_family = AF_INET_V; |
wolfSSL | 0:9d17e4342598 | 416 | #endif |
wolfSSL | 0:9d17e4342598 | 417 | addr->sin_port = htons(port); |
wolfSSL | 0:9d17e4342598 | 418 | if (peer == INADDR_ANY) |
wolfSSL | 0:9d17e4342598 | 419 | addr->sin_addr.s_addr = INADDR_ANY; |
wolfSSL | 0:9d17e4342598 | 420 | else { |
wolfSSL | 0:9d17e4342598 | 421 | if (!useLookup) |
wolfSSL | 0:9d17e4342598 | 422 | addr->sin_addr.s_addr = inet_addr(peer); |
wolfSSL | 0:9d17e4342598 | 423 | } |
wolfSSL | 0:9d17e4342598 | 424 | #else |
wolfSSL | 0:9d17e4342598 | 425 | addr->sin6_family = AF_INET_V; |
wolfSSL | 0:9d17e4342598 | 426 | addr->sin6_port = htons(port); |
wolfSSL | 0:9d17e4342598 | 427 | if (peer == INADDR_ANY) |
wolfSSL | 0:9d17e4342598 | 428 | addr->sin6_addr = in6addr_any; |
wolfSSL | 0:9d17e4342598 | 429 | else { |
wolfSSL | 0:9d17e4342598 | 430 | #ifdef HAVE_GETADDRINFO |
wolfSSL | 0:9d17e4342598 | 431 | struct addrinfo hints; |
wolfSSL | 0:9d17e4342598 | 432 | struct addrinfo* answer = NULL; |
wolfSSL | 0:9d17e4342598 | 433 | int ret; |
wolfSSL | 0:9d17e4342598 | 434 | char strPort[80]; |
wolfSSL | 0:9d17e4342598 | 435 | |
wolfSSL | 0:9d17e4342598 | 436 | memset(&hints, 0, sizeof(hints)); |
wolfSSL | 0:9d17e4342598 | 437 | |
wolfSSL | 0:9d17e4342598 | 438 | hints.ai_family = AF_INET_V; |
wolfSSL | 0:9d17e4342598 | 439 | hints.ai_socktype = udp ? SOCK_DGRAM : SOCK_STREAM; |
wolfSSL | 0:9d17e4342598 | 440 | hints.ai_protocol = udp ? IPPROTO_UDP : IPPROTO_TCP; |
wolfSSL | 0:9d17e4342598 | 441 | |
wolfSSL | 0:9d17e4342598 | 442 | SNPRINTF(strPort, sizeof(strPort), "%d", port); |
wolfSSL | 0:9d17e4342598 | 443 | strPort[79] = '\0'; |
wolfSSL | 0:9d17e4342598 | 444 | |
wolfSSL | 0:9d17e4342598 | 445 | ret = getaddrinfo(peer, strPort, &hints, &answer); |
wolfSSL | 0:9d17e4342598 | 446 | if (ret < 0 || answer == NULL) |
wolfSSL | 0:9d17e4342598 | 447 | err_sys("getaddrinfo failed"); |
wolfSSL | 0:9d17e4342598 | 448 | |
wolfSSL | 0:9d17e4342598 | 449 | memcpy(addr, answer->ai_addr, answer->ai_addrlen); |
wolfSSL | 0:9d17e4342598 | 450 | freeaddrinfo(answer); |
wolfSSL | 0:9d17e4342598 | 451 | #else |
wolfSSL | 0:9d17e4342598 | 452 | printf("no ipv6 getaddrinfo, loopback only tests/examples\n"); |
wolfSSL | 0:9d17e4342598 | 453 | addr->sin6_addr = in6addr_loopback; |
wolfSSL | 0:9d17e4342598 | 454 | #endif |
wolfSSL | 0:9d17e4342598 | 455 | } |
wolfSSL | 0:9d17e4342598 | 456 | #endif |
wolfSSL | 0:9d17e4342598 | 457 | } |
wolfSSL | 0:9d17e4342598 | 458 | |
wolfSSL | 0:9d17e4342598 | 459 | |
wolfSSL | 0:9d17e4342598 | 460 | static INLINE void tcp_socket(SOCKET_T* sockfd, int udp) |
wolfSSL | 0:9d17e4342598 | 461 | { |
wolfSSL | 0:9d17e4342598 | 462 | if (udp) |
wolfSSL | 0:9d17e4342598 | 463 | *sockfd = socket(AF_INET_V, SOCK_DGRAM, 0); |
wolfSSL | 0:9d17e4342598 | 464 | else |
wolfSSL | 0:9d17e4342598 | 465 | *sockfd = socket(AF_INET_V, SOCK_STREAM, 0); |
wolfSSL | 0:9d17e4342598 | 466 | |
wolfSSL | 0:9d17e4342598 | 467 | #ifdef USE_WINDOWS_API |
wolfSSL | 0:9d17e4342598 | 468 | if (*sockfd == INVALID_SOCKET) |
wolfSSL | 0:9d17e4342598 | 469 | err_sys("socket failed\n"); |
wolfSSL | 0:9d17e4342598 | 470 | #else |
wolfSSL | 0:9d17e4342598 | 471 | if (*sockfd < 0) |
wolfSSL | 0:9d17e4342598 | 472 | err_sys("socket failed\n"); |
wolfSSL | 0:9d17e4342598 | 473 | #endif |
wolfSSL | 0:9d17e4342598 | 474 | |
wolfSSL | 0:9d17e4342598 | 475 | #ifndef USE_WINDOWS_API |
wolfSSL | 0:9d17e4342598 | 476 | #ifdef SO_NOSIGPIPE |
wolfSSL | 0:9d17e4342598 | 477 | { |
wolfSSL | 0:9d17e4342598 | 478 | int on = 1; |
wolfSSL | 0:9d17e4342598 | 479 | socklen_t len = sizeof(on); |
wolfSSL | 0:9d17e4342598 | 480 | int res = setsockopt(*sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, len); |
wolfSSL | 0:9d17e4342598 | 481 | if (res < 0) |
wolfSSL | 0:9d17e4342598 | 482 | err_sys("setsockopt SO_NOSIGPIPE failed\n"); |
wolfSSL | 0:9d17e4342598 | 483 | } |
wolfSSL | 0:9d17e4342598 | 484 | #elif defined(CYASSL_MDK_ARM) |
wolfSSL | 0:9d17e4342598 | 485 | /* nothing to define */ |
wolfSSL | 0:9d17e4342598 | 486 | #else /* no S_NOSIGPIPE */ |
wolfSSL | 0:9d17e4342598 | 487 | signal(SIGPIPE, SIG_IGN); |
wolfSSL | 0:9d17e4342598 | 488 | #endif /* S_NOSIGPIPE */ |
wolfSSL | 0:9d17e4342598 | 489 | |
wolfSSL | 0:9d17e4342598 | 490 | #if defined(TCP_NODELAY) |
wolfSSL | 0:9d17e4342598 | 491 | if (!udp) |
wolfSSL | 0:9d17e4342598 | 492 | { |
wolfSSL | 0:9d17e4342598 | 493 | int on = 1; |
wolfSSL | 0:9d17e4342598 | 494 | socklen_t len = sizeof(on); |
wolfSSL | 0:9d17e4342598 | 495 | int res = setsockopt(*sockfd, IPPROTO_TCP, TCP_NODELAY, &on, len); |
wolfSSL | 0:9d17e4342598 | 496 | if (res < 0) |
wolfSSL | 0:9d17e4342598 | 497 | err_sys("setsockopt TCP_NODELAY failed\n"); |
wolfSSL | 0:9d17e4342598 | 498 | } |
wolfSSL | 0:9d17e4342598 | 499 | #endif |
wolfSSL | 0:9d17e4342598 | 500 | #endif /* USE_WINDOWS_API */ |
wolfSSL | 0:9d17e4342598 | 501 | } |
wolfSSL | 0:9d17e4342598 | 502 | |
wolfSSL | 0:9d17e4342598 | 503 | static INLINE void tcp_connect(SOCKET_T* sockfd, const char* ip, word16 port, |
wolfSSL | 0:9d17e4342598 | 504 | int udp) |
wolfSSL | 0:9d17e4342598 | 505 | { |
wolfSSL | 0:9d17e4342598 | 506 | SOCKADDR_IN_T addr; |
wolfSSL | 0:9d17e4342598 | 507 | build_addr(&addr, ip, port, udp); |
wolfSSL | 0:9d17e4342598 | 508 | tcp_socket(sockfd, udp); |
wolfSSL | 0:9d17e4342598 | 509 | |
wolfSSL | 0:9d17e4342598 | 510 | if (!udp) { |
wolfSSL | 0:9d17e4342598 | 511 | if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0) |
wolfSSL | 0:9d17e4342598 | 512 | err_sys("tcp connect failed"); |
wolfSSL | 0:9d17e4342598 | 513 | } |
wolfSSL | 0:9d17e4342598 | 514 | } |
wolfSSL | 0:9d17e4342598 | 515 | |
wolfSSL | 0:9d17e4342598 | 516 | |
wolfSSL | 0:9d17e4342598 | 517 | static INLINE void udp_connect(SOCKET_T* sockfd, void* addr, int addrSz) |
wolfSSL | 0:9d17e4342598 | 518 | { |
wolfSSL | 0:9d17e4342598 | 519 | if (connect(*sockfd, (const struct sockaddr*)addr, addrSz) != 0) |
wolfSSL | 0:9d17e4342598 | 520 | err_sys("tcp connect failed"); |
wolfSSL | 0:9d17e4342598 | 521 | } |
wolfSSL | 0:9d17e4342598 | 522 | |
wolfSSL | 0:9d17e4342598 | 523 | |
wolfSSL | 0:9d17e4342598 | 524 | enum { |
wolfSSL | 0:9d17e4342598 | 525 | TEST_SELECT_FAIL, |
wolfSSL | 0:9d17e4342598 | 526 | TEST_TIMEOUT, |
wolfSSL | 0:9d17e4342598 | 527 | TEST_RECV_READY, |
wolfSSL | 0:9d17e4342598 | 528 | TEST_ERROR_READY |
wolfSSL | 0:9d17e4342598 | 529 | }; |
wolfSSL | 0:9d17e4342598 | 530 | |
wolfSSL | 0:9d17e4342598 | 531 | |
wolfSSL | 0:9d17e4342598 | 532 | #if !defined(CYASSL_MDK_ARM) |
wolfSSL | 0:9d17e4342598 | 533 | static INLINE int tcp_select(SOCKET_T socketfd, int to_sec) |
wolfSSL | 0:9d17e4342598 | 534 | { |
wolfSSL | 0:9d17e4342598 | 535 | fd_set recvfds, errfds; |
wolfSSL | 0:9d17e4342598 | 536 | SOCKET_T nfds = socketfd + 1; |
wolfSSL | 0:9d17e4342598 | 537 | struct timeval timeout = { (to_sec > 0) ? to_sec : 0, 0}; |
wolfSSL | 0:9d17e4342598 | 538 | int result; |
wolfSSL | 0:9d17e4342598 | 539 | |
wolfSSL | 0:9d17e4342598 | 540 | FD_ZERO(&recvfds); |
wolfSSL | 0:9d17e4342598 | 541 | FD_SET(socketfd, &recvfds); |
wolfSSL | 0:9d17e4342598 | 542 | FD_ZERO(&errfds); |
wolfSSL | 0:9d17e4342598 | 543 | FD_SET(socketfd, &errfds); |
wolfSSL | 0:9d17e4342598 | 544 | |
wolfSSL | 0:9d17e4342598 | 545 | result = select(nfds, &recvfds, NULL, &errfds, &timeout); |
wolfSSL | 0:9d17e4342598 | 546 | |
wolfSSL | 0:9d17e4342598 | 547 | if (result == 0) |
wolfSSL | 0:9d17e4342598 | 548 | return TEST_TIMEOUT; |
wolfSSL | 0:9d17e4342598 | 549 | else if (result > 0) { |
wolfSSL | 0:9d17e4342598 | 550 | if (FD_ISSET(socketfd, &recvfds)) |
wolfSSL | 0:9d17e4342598 | 551 | return TEST_RECV_READY; |
wolfSSL | 0:9d17e4342598 | 552 | else if(FD_ISSET(socketfd, &errfds)) |
wolfSSL | 0:9d17e4342598 | 553 | return TEST_ERROR_READY; |
wolfSSL | 0:9d17e4342598 | 554 | } |
wolfSSL | 0:9d17e4342598 | 555 | |
wolfSSL | 0:9d17e4342598 | 556 | return TEST_SELECT_FAIL; |
wolfSSL | 0:9d17e4342598 | 557 | } |
wolfSSL | 0:9d17e4342598 | 558 | #endif /* !CYASSL_MDK_ARM */ |
wolfSSL | 0:9d17e4342598 | 559 | |
wolfSSL | 0:9d17e4342598 | 560 | |
wolfSSL | 0:9d17e4342598 | 561 | static INLINE void tcp_listen(SOCKET_T* sockfd, word16* port, int useAnyAddr, |
wolfSSL | 0:9d17e4342598 | 562 | int udp) |
wolfSSL | 0:9d17e4342598 | 563 | { |
wolfSSL | 0:9d17e4342598 | 564 | SOCKADDR_IN_T addr; |
wolfSSL | 0:9d17e4342598 | 565 | |
wolfSSL | 0:9d17e4342598 | 566 | /* don't use INADDR_ANY by default, firewall may block, make user switch |
wolfSSL | 0:9d17e4342598 | 567 | on */ |
wolfSSL | 0:9d17e4342598 | 568 | build_addr(&addr, (useAnyAddr ? INADDR_ANY : yasslIP), *port, udp); |
wolfSSL | 0:9d17e4342598 | 569 | tcp_socket(sockfd, udp); |
wolfSSL | 0:9d17e4342598 | 570 | |
wolfSSL | 0:9d17e4342598 | 571 | #if !defined(USE_WINDOWS_API) && !defined(CYASSL_MDK_ARM) |
wolfSSL | 0:9d17e4342598 | 572 | { |
wolfSSL | 0:9d17e4342598 | 573 | int res, on = 1; |
wolfSSL | 0:9d17e4342598 | 574 | socklen_t len = sizeof(on); |
wolfSSL | 0:9d17e4342598 | 575 | res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len); |
wolfSSL | 0:9d17e4342598 | 576 | if (res < 0) |
wolfSSL | 0:9d17e4342598 | 577 | err_sys("setsockopt SO_REUSEADDR failed\n"); |
wolfSSL | 0:9d17e4342598 | 578 | } |
wolfSSL | 0:9d17e4342598 | 579 | #endif |
wolfSSL | 0:9d17e4342598 | 580 | |
wolfSSL | 0:9d17e4342598 | 581 | if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0) |
wolfSSL | 0:9d17e4342598 | 582 | err_sys("tcp bind failed"); |
wolfSSL | 0:9d17e4342598 | 583 | if (!udp) { |
wolfSSL | 0:9d17e4342598 | 584 | if (listen(*sockfd, 5) != 0) |
wolfSSL | 0:9d17e4342598 | 585 | err_sys("tcp listen failed"); |
wolfSSL | 0:9d17e4342598 | 586 | } |
wolfSSL | 0:9d17e4342598 | 587 | #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) |
wolfSSL | 0:9d17e4342598 | 588 | if (*port == 0) { |
wolfSSL | 0:9d17e4342598 | 589 | socklen_t len = sizeof(addr); |
wolfSSL | 0:9d17e4342598 | 590 | if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) { |
wolfSSL | 0:9d17e4342598 | 591 | #ifndef TEST_IPV6 |
wolfSSL | 0:9d17e4342598 | 592 | *port = ntohs(addr.sin_port); |
wolfSSL | 0:9d17e4342598 | 593 | #else |
wolfSSL | 0:9d17e4342598 | 594 | *port = ntohs(addr.sin6_port); |
wolfSSL | 0:9d17e4342598 | 595 | #endif |
wolfSSL | 0:9d17e4342598 | 596 | } |
wolfSSL | 0:9d17e4342598 | 597 | } |
wolfSSL | 0:9d17e4342598 | 598 | #endif |
wolfSSL | 0:9d17e4342598 | 599 | } |
wolfSSL | 0:9d17e4342598 | 600 | |
wolfSSL | 0:9d17e4342598 | 601 | |
wolfSSL | 0:9d17e4342598 | 602 | static INLINE int udp_read_connect(SOCKET_T sockfd) |
wolfSSL | 0:9d17e4342598 | 603 | { |
wolfSSL | 0:9d17e4342598 | 604 | SOCKADDR_IN_T cliaddr; |
wolfSSL | 0:9d17e4342598 | 605 | byte b[1500]; |
wolfSSL | 0:9d17e4342598 | 606 | int n; |
wolfSSL | 0:9d17e4342598 | 607 | socklen_t len = sizeof(cliaddr); |
wolfSSL | 0:9d17e4342598 | 608 | |
wolfSSL | 0:9d17e4342598 | 609 | n = (int)recvfrom(sockfd, (char*)b, sizeof(b), MSG_PEEK, |
wolfSSL | 0:9d17e4342598 | 610 | (struct sockaddr*)&cliaddr, &len); |
wolfSSL | 0:9d17e4342598 | 611 | if (n > 0) { |
wolfSSL | 0:9d17e4342598 | 612 | if (connect(sockfd, (const struct sockaddr*)&cliaddr, |
wolfSSL | 0:9d17e4342598 | 613 | sizeof(cliaddr)) != 0) |
wolfSSL | 0:9d17e4342598 | 614 | err_sys("udp connect failed"); |
wolfSSL | 0:9d17e4342598 | 615 | } |
wolfSSL | 0:9d17e4342598 | 616 | else |
wolfSSL | 0:9d17e4342598 | 617 | err_sys("recvfrom failed"); |
wolfSSL | 0:9d17e4342598 | 618 | |
wolfSSL | 0:9d17e4342598 | 619 | return sockfd; |
wolfSSL | 0:9d17e4342598 | 620 | } |
wolfSSL | 0:9d17e4342598 | 621 | |
wolfSSL | 0:9d17e4342598 | 622 | static INLINE void udp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd, |
wolfSSL | 0:9d17e4342598 | 623 | int useAnyAddr, word16 port, func_args* args) |
wolfSSL | 0:9d17e4342598 | 624 | { |
wolfSSL | 0:9d17e4342598 | 625 | SOCKADDR_IN_T addr; |
wolfSSL | 0:9d17e4342598 | 626 | |
wolfSSL | 0:9d17e4342598 | 627 | (void)args; |
wolfSSL | 0:9d17e4342598 | 628 | build_addr(&addr, (useAnyAddr ? INADDR_ANY : yasslIP), port, 1); |
wolfSSL | 0:9d17e4342598 | 629 | tcp_socket(sockfd, 1); |
wolfSSL | 0:9d17e4342598 | 630 | |
wolfSSL | 0:9d17e4342598 | 631 | |
wolfSSL | 0:9d17e4342598 | 632 | #if !defined(USE_WINDOWS_API) && !defined(CYASSL_MDK_ARM) |
wolfSSL | 0:9d17e4342598 | 633 | { |
wolfSSL | 0:9d17e4342598 | 634 | int res, on = 1; |
wolfSSL | 0:9d17e4342598 | 635 | socklen_t len = sizeof(on); |
wolfSSL | 0:9d17e4342598 | 636 | res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len); |
wolfSSL | 0:9d17e4342598 | 637 | if (res < 0) |
wolfSSL | 0:9d17e4342598 | 638 | err_sys("setsockopt SO_REUSEADDR failed\n"); |
wolfSSL | 0:9d17e4342598 | 639 | } |
wolfSSL | 0:9d17e4342598 | 640 | #endif |
wolfSSL | 0:9d17e4342598 | 641 | |
wolfSSL | 0:9d17e4342598 | 642 | if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0) |
wolfSSL | 0:9d17e4342598 | 643 | err_sys("tcp bind failed"); |
wolfSSL | 0:9d17e4342598 | 644 | |
wolfSSL | 0:9d17e4342598 | 645 | #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) |
wolfSSL | 0:9d17e4342598 | 646 | if (port == 0) { |
wolfSSL | 0:9d17e4342598 | 647 | socklen_t len = sizeof(addr); |
wolfSSL | 0:9d17e4342598 | 648 | if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) { |
wolfSSL | 0:9d17e4342598 | 649 | #ifndef TEST_IPV6 |
wolfSSL | 0:9d17e4342598 | 650 | port = ntohs(addr.sin_port); |
wolfSSL | 0:9d17e4342598 | 651 | #else |
wolfSSL | 0:9d17e4342598 | 652 | port = ntohs(addr.sin6_port); |
wolfSSL | 0:9d17e4342598 | 653 | #endif |
wolfSSL | 0:9d17e4342598 | 654 | } |
wolfSSL | 0:9d17e4342598 | 655 | } |
wolfSSL | 0:9d17e4342598 | 656 | #endif |
wolfSSL | 0:9d17e4342598 | 657 | |
wolfSSL | 0:9d17e4342598 | 658 | #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__) |
wolfSSL | 0:9d17e4342598 | 659 | /* signal ready to accept data */ |
wolfSSL | 0:9d17e4342598 | 660 | { |
wolfSSL | 0:9d17e4342598 | 661 | tcp_ready* ready = args->signal; |
wolfSSL | 0:9d17e4342598 | 662 | pthread_mutex_lock(&ready->mutex); |
wolfSSL | 0:9d17e4342598 | 663 | ready->ready = 1; |
wolfSSL | 0:9d17e4342598 | 664 | ready->port = port; |
wolfSSL | 0:9d17e4342598 | 665 | pthread_cond_signal(&ready->cond); |
wolfSSL | 0:9d17e4342598 | 666 | pthread_mutex_unlock(&ready->mutex); |
wolfSSL | 0:9d17e4342598 | 667 | } |
wolfSSL | 0:9d17e4342598 | 668 | #endif |
wolfSSL | 0:9d17e4342598 | 669 | |
wolfSSL | 0:9d17e4342598 | 670 | *clientfd = udp_read_connect(*sockfd); |
wolfSSL | 0:9d17e4342598 | 671 | } |
wolfSSL | 0:9d17e4342598 | 672 | |
wolfSSL | 0:9d17e4342598 | 673 | static INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd, |
wolfSSL | 0:9d17e4342598 | 674 | func_args* args, word16 port, int useAnyAddr, |
wolfSSL | 0:9d17e4342598 | 675 | int udp) |
wolfSSL | 0:9d17e4342598 | 676 | { |
wolfSSL | 0:9d17e4342598 | 677 | SOCKADDR_IN_T client; |
wolfSSL | 0:9d17e4342598 | 678 | socklen_t client_len = sizeof(client); |
wolfSSL | 0:9d17e4342598 | 679 | |
wolfSSL | 0:9d17e4342598 | 680 | if (udp) { |
wolfSSL | 0:9d17e4342598 | 681 | udp_accept(sockfd, clientfd, useAnyAddr, port, args); |
wolfSSL | 0:9d17e4342598 | 682 | return; |
wolfSSL | 0:9d17e4342598 | 683 | } |
wolfSSL | 0:9d17e4342598 | 684 | |
wolfSSL | 0:9d17e4342598 | 685 | tcp_listen(sockfd, &port, useAnyAddr, udp); |
wolfSSL | 0:9d17e4342598 | 686 | |
wolfSSL | 0:9d17e4342598 | 687 | #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__) |
wolfSSL | 0:9d17e4342598 | 688 | /* signal ready to tcp_accept */ |
wolfSSL | 0:9d17e4342598 | 689 | { |
wolfSSL | 0:9d17e4342598 | 690 | tcp_ready* ready = args->signal; |
wolfSSL | 0:9d17e4342598 | 691 | pthread_mutex_lock(&ready->mutex); |
wolfSSL | 0:9d17e4342598 | 692 | ready->ready = 1; |
wolfSSL | 0:9d17e4342598 | 693 | ready->port = port; |
wolfSSL | 0:9d17e4342598 | 694 | pthread_cond_signal(&ready->cond); |
wolfSSL | 0:9d17e4342598 | 695 | pthread_mutex_unlock(&ready->mutex); |
wolfSSL | 0:9d17e4342598 | 696 | } |
wolfSSL | 0:9d17e4342598 | 697 | #endif |
wolfSSL | 0:9d17e4342598 | 698 | |
wolfSSL | 0:9d17e4342598 | 699 | *clientfd = accept(*sockfd, (struct sockaddr*)&client, |
wolfSSL | 0:9d17e4342598 | 700 | (ACCEPT_THIRD_T)&client_len); |
wolfSSL | 0:9d17e4342598 | 701 | #ifdef USE_WINDOWS_API |
wolfSSL | 0:9d17e4342598 | 702 | if (*clientfd == INVALID_SOCKET) |
wolfSSL | 0:9d17e4342598 | 703 | err_sys("tcp accept failed"); |
wolfSSL | 0:9d17e4342598 | 704 | #else |
wolfSSL | 0:9d17e4342598 | 705 | if (*clientfd == -1) |
wolfSSL | 0:9d17e4342598 | 706 | err_sys("tcp accept failed"); |
wolfSSL | 0:9d17e4342598 | 707 | #endif |
wolfSSL | 0:9d17e4342598 | 708 | } |
wolfSSL | 0:9d17e4342598 | 709 | |
wolfSSL | 0:9d17e4342598 | 710 | |
wolfSSL | 0:9d17e4342598 | 711 | static INLINE void tcp_set_nonblocking(SOCKET_T* sockfd) |
wolfSSL | 0:9d17e4342598 | 712 | { |
wolfSSL | 0:9d17e4342598 | 713 | #ifdef USE_WINDOWS_API |
wolfSSL | 0:9d17e4342598 | 714 | unsigned long blocking = 1; |
wolfSSL | 0:9d17e4342598 | 715 | int ret = ioctlsocket(*sockfd, FIONBIO, &blocking); |
wolfSSL | 0:9d17e4342598 | 716 | if (ret == SOCKET_ERROR) |
wolfSSL | 0:9d17e4342598 | 717 | err_sys("ioctlsocket failed"); |
wolfSSL | 0:9d17e4342598 | 718 | #elif defined(CYASSL_MDK_ARM) |
wolfSSL | 0:9d17e4342598 | 719 | /* non blocking not suppported, for now */ |
wolfSSL | 0:9d17e4342598 | 720 | #else |
wolfSSL | 0:9d17e4342598 | 721 | int flags = fcntl(*sockfd, F_GETFL, 0); |
wolfSSL | 0:9d17e4342598 | 722 | if (flags < 0) |
wolfSSL | 0:9d17e4342598 | 723 | err_sys("fcntl get failed"); |
wolfSSL | 0:9d17e4342598 | 724 | flags = fcntl(*sockfd, F_SETFL, flags | O_NONBLOCK); |
wolfSSL | 0:9d17e4342598 | 725 | if (flags < 0) |
wolfSSL | 0:9d17e4342598 | 726 | err_sys("fcntl set failed"); |
wolfSSL | 0:9d17e4342598 | 727 | #endif |
wolfSSL | 0:9d17e4342598 | 728 | } |
wolfSSL | 0:9d17e4342598 | 729 | |
wolfSSL | 0:9d17e4342598 | 730 | |
wolfSSL | 0:9d17e4342598 | 731 | #ifndef NO_PSK |
wolfSSL | 0:9d17e4342598 | 732 | |
wolfSSL | 0:9d17e4342598 | 733 | static INLINE unsigned int my_psk_client_cb(CYASSL* ssl, const char* hint, |
wolfSSL | 0:9d17e4342598 | 734 | char* identity, unsigned int id_max_len, unsigned char* key, |
wolfSSL | 0:9d17e4342598 | 735 | unsigned int key_max_len) |
wolfSSL | 0:9d17e4342598 | 736 | { |
wolfSSL | 0:9d17e4342598 | 737 | (void)ssl; |
wolfSSL | 0:9d17e4342598 | 738 | (void)hint; |
wolfSSL | 0:9d17e4342598 | 739 | (void)key_max_len; |
wolfSSL | 0:9d17e4342598 | 740 | |
wolfSSL | 0:9d17e4342598 | 741 | /* identity is OpenSSL testing default for openssl s_client, keep same */ |
wolfSSL | 0:9d17e4342598 | 742 | strncpy(identity, "Client_identity", id_max_len); |
wolfSSL | 0:9d17e4342598 | 743 | |
wolfSSL | 0:9d17e4342598 | 744 | |
wolfSSL | 0:9d17e4342598 | 745 | /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using |
wolfSSL | 0:9d17e4342598 | 746 | unsigned binary */ |
wolfSSL | 0:9d17e4342598 | 747 | key[0] = 26; |
wolfSSL | 0:9d17e4342598 | 748 | key[1] = 43; |
wolfSSL | 0:9d17e4342598 | 749 | key[2] = 60; |
wolfSSL | 0:9d17e4342598 | 750 | key[3] = 77; |
wolfSSL | 0:9d17e4342598 | 751 | |
wolfSSL | 0:9d17e4342598 | 752 | return 4; /* length of key in octets or 0 for error */ |
wolfSSL | 0:9d17e4342598 | 753 | } |
wolfSSL | 0:9d17e4342598 | 754 | |
wolfSSL | 0:9d17e4342598 | 755 | |
wolfSSL | 0:9d17e4342598 | 756 | static INLINE unsigned int my_psk_server_cb(CYASSL* ssl, const char* identity, |
wolfSSL | 0:9d17e4342598 | 757 | unsigned char* key, unsigned int key_max_len) |
wolfSSL | 0:9d17e4342598 | 758 | { |
wolfSSL | 0:9d17e4342598 | 759 | (void)ssl; |
wolfSSL | 0:9d17e4342598 | 760 | (void)key_max_len; |
wolfSSL | 0:9d17e4342598 | 761 | |
wolfSSL | 0:9d17e4342598 | 762 | /* identity is OpenSSL testing default for openssl s_client, keep same */ |
wolfSSL | 0:9d17e4342598 | 763 | if (strncmp(identity, "Client_identity", 15) != 0) |
wolfSSL | 0:9d17e4342598 | 764 | return 0; |
wolfSSL | 0:9d17e4342598 | 765 | |
wolfSSL | 0:9d17e4342598 | 766 | /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using |
wolfSSL | 0:9d17e4342598 | 767 | unsigned binary */ |
wolfSSL | 0:9d17e4342598 | 768 | key[0] = 26; |
wolfSSL | 0:9d17e4342598 | 769 | key[1] = 43; |
wolfSSL | 0:9d17e4342598 | 770 | key[2] = 60; |
wolfSSL | 0:9d17e4342598 | 771 | key[3] = 77; |
wolfSSL | 0:9d17e4342598 | 772 | |
wolfSSL | 0:9d17e4342598 | 773 | return 4; /* length of key in octets or 0 for error */ |
wolfSSL | 0:9d17e4342598 | 774 | } |
wolfSSL | 0:9d17e4342598 | 775 | |
wolfSSL | 0:9d17e4342598 | 776 | #endif /* NO_PSK */ |
wolfSSL | 0:9d17e4342598 | 777 | |
wolfSSL | 0:9d17e4342598 | 778 | |
wolfSSL | 0:9d17e4342598 | 779 | #ifdef USE_WINDOWS_API |
wolfSSL | 0:9d17e4342598 | 780 | |
wolfSSL | 0:9d17e4342598 | 781 | #define WIN32_LEAN_AND_MEAN |
wolfSSL | 0:9d17e4342598 | 782 | #include <windows.h> |
wolfSSL | 0:9d17e4342598 | 783 | |
wolfSSL | 0:9d17e4342598 | 784 | static INLINE double current_time() |
wolfSSL | 0:9d17e4342598 | 785 | { |
wolfSSL | 0:9d17e4342598 | 786 | static int init = 0; |
wolfSSL | 0:9d17e4342598 | 787 | static LARGE_INTEGER freq; |
wolfSSL | 0:9d17e4342598 | 788 | |
wolfSSL | 0:9d17e4342598 | 789 | LARGE_INTEGER count; |
wolfSSL | 0:9d17e4342598 | 790 | |
wolfSSL | 0:9d17e4342598 | 791 | if (!init) { |
wolfSSL | 0:9d17e4342598 | 792 | QueryPerformanceFrequency(&freq); |
wolfSSL | 0:9d17e4342598 | 793 | init = 1; |
wolfSSL | 0:9d17e4342598 | 794 | } |
wolfSSL | 0:9d17e4342598 | 795 | |
wolfSSL | 0:9d17e4342598 | 796 | QueryPerformanceCounter(&count); |
wolfSSL | 0:9d17e4342598 | 797 | |
wolfSSL | 0:9d17e4342598 | 798 | return (double)count.QuadPart / freq.QuadPart; |
wolfSSL | 0:9d17e4342598 | 799 | } |
wolfSSL | 0:9d17e4342598 | 800 | |
wolfSSL | 0:9d17e4342598 | 801 | #else |
wolfSSL | 0:9d17e4342598 | 802 | |
wolfSSL | 0:9d17e4342598 | 803 | #if !defined(CYASSL_MDK_ARM) |
wolfSSL | 0:9d17e4342598 | 804 | #include <sys/time.h> |
wolfSSL | 0:9d17e4342598 | 805 | |
wolfSSL | 0:9d17e4342598 | 806 | static INLINE double current_time(void) |
wolfSSL | 0:9d17e4342598 | 807 | { |
wolfSSL | 0:9d17e4342598 | 808 | struct timeval tv; |
wolfSSL | 0:9d17e4342598 | 809 | gettimeofday(&tv, 0); |
wolfSSL | 0:9d17e4342598 | 810 | |
wolfSSL | 0:9d17e4342598 | 811 | return (double)tv.tv_sec + (double)tv.tv_usec / 1000000; |
wolfSSL | 0:9d17e4342598 | 812 | } |
wolfSSL | 0:9d17e4342598 | 813 | |
wolfSSL | 0:9d17e4342598 | 814 | #endif |
wolfSSL | 0:9d17e4342598 | 815 | #endif /* USE_WINDOWS_API */ |
wolfSSL | 0:9d17e4342598 | 816 | |
wolfSSL | 0:9d17e4342598 | 817 | |
wolfSSL | 0:9d17e4342598 | 818 | #if defined(NO_FILESYSTEM) && !defined(NO_CERTS) |
wolfSSL | 0:9d17e4342598 | 819 | |
wolfSSL | 0:9d17e4342598 | 820 | enum { |
wolfSSL | 0:9d17e4342598 | 821 | CYASSL_CA = 1, |
wolfSSL | 0:9d17e4342598 | 822 | CYASSL_CERT = 2, |
wolfSSL | 0:9d17e4342598 | 823 | CYASSL_KEY = 3 |
wolfSSL | 0:9d17e4342598 | 824 | }; |
wolfSSL | 0:9d17e4342598 | 825 | |
wolfSSL | 0:9d17e4342598 | 826 | static INLINE void load_buffer(CYASSL_CTX* ctx, const char* fname, int type) |
wolfSSL | 0:9d17e4342598 | 827 | { |
wolfSSL | 0:9d17e4342598 | 828 | /* test buffer load */ |
wolfSSL | 0:9d17e4342598 | 829 | long sz = 0; |
wolfSSL | 0:9d17e4342598 | 830 | byte buff[10000]; |
wolfSSL | 0:9d17e4342598 | 831 | FILE* file = fopen(fname, "rb"); |
wolfSSL | 0:9d17e4342598 | 832 | |
wolfSSL | 0:9d17e4342598 | 833 | if (!file) |
wolfSSL | 0:9d17e4342598 | 834 | err_sys("can't open file for buffer load " |
wolfSSL | 0:9d17e4342598 | 835 | "Please run from CyaSSL home directory if not"); |
wolfSSL | 0:9d17e4342598 | 836 | fseek(file, 0, SEEK_END); |
wolfSSL | 0:9d17e4342598 | 837 | sz = ftell(file); |
wolfSSL | 0:9d17e4342598 | 838 | rewind(file); |
wolfSSL | 0:9d17e4342598 | 839 | fread(buff, sizeof(buff), 1, file); |
wolfSSL | 0:9d17e4342598 | 840 | |
wolfSSL | 0:9d17e4342598 | 841 | if (type == CYASSL_CA) { |
wolfSSL | 0:9d17e4342598 | 842 | if (CyaSSL_CTX_load_verify_buffer(ctx, buff, sz, SSL_FILETYPE_PEM) |
wolfSSL | 0:9d17e4342598 | 843 | != SSL_SUCCESS) |
wolfSSL | 0:9d17e4342598 | 844 | err_sys("can't load buffer ca file"); |
wolfSSL | 0:9d17e4342598 | 845 | } |
wolfSSL | 0:9d17e4342598 | 846 | else if (type == CYASSL_CERT) { |
wolfSSL | 0:9d17e4342598 | 847 | if (CyaSSL_CTX_use_certificate_buffer(ctx, buff, sz, |
wolfSSL | 0:9d17e4342598 | 848 | SSL_FILETYPE_PEM) != SSL_SUCCESS) |
wolfSSL | 0:9d17e4342598 | 849 | err_sys("can't load buffer cert file"); |
wolfSSL | 0:9d17e4342598 | 850 | } |
wolfSSL | 0:9d17e4342598 | 851 | else if (type == CYASSL_KEY) { |
wolfSSL | 0:9d17e4342598 | 852 | if (CyaSSL_CTX_use_PrivateKey_buffer(ctx, buff, sz, |
wolfSSL | 0:9d17e4342598 | 853 | SSL_FILETYPE_PEM) != SSL_SUCCESS) |
wolfSSL | 0:9d17e4342598 | 854 | err_sys("can't load buffer key file"); |
wolfSSL | 0:9d17e4342598 | 855 | } |
wolfSSL | 0:9d17e4342598 | 856 | } |
wolfSSL | 0:9d17e4342598 | 857 | |
wolfSSL | 0:9d17e4342598 | 858 | #endif /* NO_FILESYSTEM */ |
wolfSSL | 0:9d17e4342598 | 859 | |
wolfSSL | 0:9d17e4342598 | 860 | #ifdef VERIFY_CALLBACK |
wolfSSL | 0:9d17e4342598 | 861 | |
wolfSSL | 0:9d17e4342598 | 862 | static INLINE int myVerify(int preverify, CYASSL_X509_STORE_CTX* store) |
wolfSSL | 0:9d17e4342598 | 863 | { |
wolfSSL | 0:9d17e4342598 | 864 | (void)preverify; |
wolfSSL | 0:9d17e4342598 | 865 | char buffer[CYASSL_MAX_ERROR_SZ]; |
wolfSSL | 0:9d17e4342598 | 866 | |
wolfSSL | 0:9d17e4342598 | 867 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 868 | CYASSL_X509* peer; |
wolfSSL | 0:9d17e4342598 | 869 | #endif |
wolfSSL | 0:9d17e4342598 | 870 | |
wolfSSL | 0:9d17e4342598 | 871 | printf("In verification callback, error = %d, %s\n", store->error, |
wolfSSL | 0:9d17e4342598 | 872 | CyaSSL_ERR_error_string(store->error, buffer)); |
wolfSSL | 0:9d17e4342598 | 873 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 874 | peer = store->current_cert; |
wolfSSL | 0:9d17e4342598 | 875 | if (peer) { |
wolfSSL | 0:9d17e4342598 | 876 | char* issuer = CyaSSL_X509_NAME_oneline( |
wolfSSL | 0:9d17e4342598 | 877 | CyaSSL_X509_get_issuer_name(peer), 0, 0); |
wolfSSL | 0:9d17e4342598 | 878 | char* subject = CyaSSL_X509_NAME_oneline( |
wolfSSL | 0:9d17e4342598 | 879 | CyaSSL_X509_get_subject_name(peer), 0, 0); |
wolfSSL | 0:9d17e4342598 | 880 | printf("peer's cert info:\n issuer : %s\n subject: %s\n", issuer, |
wolfSSL | 0:9d17e4342598 | 881 | subject); |
wolfSSL | 0:9d17e4342598 | 882 | XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL); |
wolfSSL | 0:9d17e4342598 | 883 | XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL); |
wolfSSL | 0:9d17e4342598 | 884 | } |
wolfSSL | 0:9d17e4342598 | 885 | else |
wolfSSL | 0:9d17e4342598 | 886 | printf("peer has no cert!\n"); |
wolfSSL | 0:9d17e4342598 | 887 | #endif |
wolfSSL | 0:9d17e4342598 | 888 | printf("Subject's domain name is %s\n", store->domain); |
wolfSSL | 0:9d17e4342598 | 889 | |
wolfSSL | 0:9d17e4342598 | 890 | printf("Allowing to continue anyway (shouldn't do this, EVER!!!)\n"); |
wolfSSL | 0:9d17e4342598 | 891 | return 1; |
wolfSSL | 0:9d17e4342598 | 892 | } |
wolfSSL | 0:9d17e4342598 | 893 | |
wolfSSL | 0:9d17e4342598 | 894 | #endif /* VERIFY_CALLBACK */ |
wolfSSL | 0:9d17e4342598 | 895 | |
wolfSSL | 0:9d17e4342598 | 896 | |
wolfSSL | 0:9d17e4342598 | 897 | #ifdef HAVE_CRL |
wolfSSL | 0:9d17e4342598 | 898 | |
wolfSSL | 0:9d17e4342598 | 899 | static INLINE void CRL_CallBack(const char* url) |
wolfSSL | 0:9d17e4342598 | 900 | { |
wolfSSL | 0:9d17e4342598 | 901 | printf("CRL callback url = %s\n", url); |
wolfSSL | 0:9d17e4342598 | 902 | } |
wolfSSL | 0:9d17e4342598 | 903 | |
wolfSSL | 0:9d17e4342598 | 904 | #endif |
wolfSSL | 0:9d17e4342598 | 905 | |
wolfSSL | 0:9d17e4342598 | 906 | #ifndef NO_CERTS |
wolfSSL | 0:9d17e4342598 | 907 | |
wolfSSL | 0:9d17e4342598 | 908 | static INLINE void CaCb(unsigned char* der, int sz, int type) |
wolfSSL | 0:9d17e4342598 | 909 | { |
wolfSSL | 0:9d17e4342598 | 910 | (void)der; |
wolfSSL | 0:9d17e4342598 | 911 | printf("Got CA cache add callback, derSz = %d, type = %d\n", sz, type); |
wolfSSL | 0:9d17e4342598 | 912 | } |
wolfSSL | 0:9d17e4342598 | 913 | |
wolfSSL | 0:9d17e4342598 | 914 | |
wolfSSL | 0:9d17e4342598 | 915 | static INLINE void SetDH(CYASSL* ssl) |
wolfSSL | 0:9d17e4342598 | 916 | { |
wolfSSL | 0:9d17e4342598 | 917 | /* dh1024 p */ |
wolfSSL | 0:9d17e4342598 | 918 | static unsigned char p[] = |
wolfSSL | 0:9d17e4342598 | 919 | { |
wolfSSL | 0:9d17e4342598 | 920 | 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3, |
wolfSSL | 0:9d17e4342598 | 921 | 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E, |
wolfSSL | 0:9d17e4342598 | 922 | 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59, |
wolfSSL | 0:9d17e4342598 | 923 | 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2, |
wolfSSL | 0:9d17e4342598 | 924 | 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD, |
wolfSSL | 0:9d17e4342598 | 925 | 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF, |
wolfSSL | 0:9d17e4342598 | 926 | 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02, |
wolfSSL | 0:9d17e4342598 | 927 | 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C, |
wolfSSL | 0:9d17e4342598 | 928 | 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7, |
wolfSSL | 0:9d17e4342598 | 929 | 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50, |
wolfSSL | 0:9d17e4342598 | 930 | 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B, |
wolfSSL | 0:9d17e4342598 | 931 | }; |
wolfSSL | 0:9d17e4342598 | 932 | |
wolfSSL | 0:9d17e4342598 | 933 | /* dh1024 g */ |
wolfSSL | 0:9d17e4342598 | 934 | static unsigned char g[] = |
wolfSSL | 0:9d17e4342598 | 935 | { |
wolfSSL | 0:9d17e4342598 | 936 | 0x02, |
wolfSSL | 0:9d17e4342598 | 937 | }; |
wolfSSL | 0:9d17e4342598 | 938 | |
wolfSSL | 0:9d17e4342598 | 939 | CyaSSL_SetTmpDH(ssl, p, sizeof(p), g, sizeof(g)); |
wolfSSL | 0:9d17e4342598 | 940 | } |
wolfSSL | 0:9d17e4342598 | 941 | |
wolfSSL | 0:9d17e4342598 | 942 | static INLINE void SetDHCtx(CYASSL_CTX* ctx) |
wolfSSL | 0:9d17e4342598 | 943 | { |
wolfSSL | 0:9d17e4342598 | 944 | /* dh1024 p */ |
wolfSSL | 0:9d17e4342598 | 945 | static unsigned char p[] = |
wolfSSL | 0:9d17e4342598 | 946 | { |
wolfSSL | 0:9d17e4342598 | 947 | 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3, |
wolfSSL | 0:9d17e4342598 | 948 | 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E, |
wolfSSL | 0:9d17e4342598 | 949 | 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59, |
wolfSSL | 0:9d17e4342598 | 950 | 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2, |
wolfSSL | 0:9d17e4342598 | 951 | 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD, |
wolfSSL | 0:9d17e4342598 | 952 | 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF, |
wolfSSL | 0:9d17e4342598 | 953 | 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02, |
wolfSSL | 0:9d17e4342598 | 954 | 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C, |
wolfSSL | 0:9d17e4342598 | 955 | 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7, |
wolfSSL | 0:9d17e4342598 | 956 | 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50, |
wolfSSL | 0:9d17e4342598 | 957 | 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B, |
wolfSSL | 0:9d17e4342598 | 958 | }; |
wolfSSL | 0:9d17e4342598 | 959 | |
wolfSSL | 0:9d17e4342598 | 960 | /* dh1024 g */ |
wolfSSL | 0:9d17e4342598 | 961 | static unsigned char g[] = |
wolfSSL | 0:9d17e4342598 | 962 | { |
wolfSSL | 0:9d17e4342598 | 963 | 0x02, |
wolfSSL | 0:9d17e4342598 | 964 | }; |
wolfSSL | 0:9d17e4342598 | 965 | |
wolfSSL | 0:9d17e4342598 | 966 | CyaSSL_CTX_SetTmpDH(ctx, p, sizeof(p), g, sizeof(g)); |
wolfSSL | 0:9d17e4342598 | 967 | } |
wolfSSL | 0:9d17e4342598 | 968 | |
wolfSSL | 0:9d17e4342598 | 969 | #endif /* !NO_CERTS */ |
wolfSSL | 0:9d17e4342598 | 970 | |
wolfSSL | 0:9d17e4342598 | 971 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 972 | |
wolfSSL | 0:9d17e4342598 | 973 | static INLINE int OpenNitroxDevice(int dma_mode,int dev_id) |
wolfSSL | 0:9d17e4342598 | 974 | { |
wolfSSL | 0:9d17e4342598 | 975 | Csp1CoreAssignment core_assign; |
wolfSSL | 0:9d17e4342598 | 976 | Uint32 device; |
wolfSSL | 0:9d17e4342598 | 977 | |
wolfSSL | 0:9d17e4342598 | 978 | if (CspInitialize(CAVIUM_DIRECT,CAVIUM_DEV_ID)) |
wolfSSL | 0:9d17e4342598 | 979 | return -1; |
wolfSSL | 0:9d17e4342598 | 980 | if (Csp1GetDevType(&device)) |
wolfSSL | 0:9d17e4342598 | 981 | return -1; |
wolfSSL | 0:9d17e4342598 | 982 | if (device != NPX_DEVICE) { |
wolfSSL | 0:9d17e4342598 | 983 | if (ioctl(gpkpdev_hdlr[CAVIUM_DEV_ID], IOCTL_CSP1_GET_CORE_ASSIGNMENT, |
wolfSSL | 0:9d17e4342598 | 984 | (Uint32 *)&core_assign)!= 0) |
wolfSSL | 0:9d17e4342598 | 985 | return -1; |
wolfSSL | 0:9d17e4342598 | 986 | } |
wolfSSL | 0:9d17e4342598 | 987 | CspShutdown(CAVIUM_DEV_ID); |
wolfSSL | 0:9d17e4342598 | 988 | |
wolfSSL | 0:9d17e4342598 | 989 | return CspInitialize(dma_mode, dev_id); |
wolfSSL | 0:9d17e4342598 | 990 | } |
wolfSSL | 0:9d17e4342598 | 991 | |
wolfSSL | 0:9d17e4342598 | 992 | #endif /* HAVE_CAVIUM */ |
wolfSSL | 0:9d17e4342598 | 993 | |
wolfSSL | 0:9d17e4342598 | 994 | |
wolfSSL | 0:9d17e4342598 | 995 | #ifdef USE_WINDOWS_API |
wolfSSL | 0:9d17e4342598 | 996 | |
wolfSSL | 0:9d17e4342598 | 997 | /* do back x number of directories */ |
wolfSSL | 0:9d17e4342598 | 998 | static INLINE void ChangeDirBack(int x) |
wolfSSL | 0:9d17e4342598 | 999 | { |
wolfSSL | 0:9d17e4342598 | 1000 | char path[MAX_PATH]; |
wolfSSL | 0:9d17e4342598 | 1001 | |
wolfSSL | 0:9d17e4342598 | 1002 | if (x == 1) |
wolfSSL | 0:9d17e4342598 | 1003 | strncpy(path, "..\\", MAX_PATH); |
wolfSSL | 0:9d17e4342598 | 1004 | else if (x == 2) |
wolfSSL | 0:9d17e4342598 | 1005 | strncpy(path, "..\\..\\", MAX_PATH); |
wolfSSL | 0:9d17e4342598 | 1006 | else if (x == 3) |
wolfSSL | 0:9d17e4342598 | 1007 | strncpy(path, "..\\..\\..\\", MAX_PATH); |
wolfSSL | 0:9d17e4342598 | 1008 | else if (x == 4) |
wolfSSL | 0:9d17e4342598 | 1009 | strncpy(path, "..\\..\\..\\..\\", MAX_PATH); |
wolfSSL | 0:9d17e4342598 | 1010 | else |
wolfSSL | 0:9d17e4342598 | 1011 | strncpy(path, ".\\", MAX_PATH); |
wolfSSL | 0:9d17e4342598 | 1012 | |
wolfSSL | 0:9d17e4342598 | 1013 | SetCurrentDirectoryA(path); |
wolfSSL | 0:9d17e4342598 | 1014 | } |
wolfSSL | 0:9d17e4342598 | 1015 | |
wolfSSL | 0:9d17e4342598 | 1016 | /* does current dir contain str */ |
wolfSSL | 0:9d17e4342598 | 1017 | static INLINE int CurrentDir(const char* str) |
wolfSSL | 0:9d17e4342598 | 1018 | { |
wolfSSL | 0:9d17e4342598 | 1019 | char path[MAX_PATH]; |
wolfSSL | 0:9d17e4342598 | 1020 | char* baseName; |
wolfSSL | 0:9d17e4342598 | 1021 | |
wolfSSL | 0:9d17e4342598 | 1022 | GetCurrentDirectoryA(sizeof(path), path); |
wolfSSL | 0:9d17e4342598 | 1023 | |
wolfSSL | 0:9d17e4342598 | 1024 | baseName = strrchr(path, '\\'); |
wolfSSL | 0:9d17e4342598 | 1025 | if (baseName) |
wolfSSL | 0:9d17e4342598 | 1026 | baseName++; |
wolfSSL | 0:9d17e4342598 | 1027 | else |
wolfSSL | 0:9d17e4342598 | 1028 | baseName = path; |
wolfSSL | 0:9d17e4342598 | 1029 | |
wolfSSL | 0:9d17e4342598 | 1030 | if (strstr(baseName, str)) |
wolfSSL | 0:9d17e4342598 | 1031 | return 1; |
wolfSSL | 0:9d17e4342598 | 1032 | |
wolfSSL | 0:9d17e4342598 | 1033 | return 0; |
wolfSSL | 0:9d17e4342598 | 1034 | } |
wolfSSL | 0:9d17e4342598 | 1035 | |
wolfSSL | 0:9d17e4342598 | 1036 | #elif defined(CYASSL_MDK_ARM) |
wolfSSL | 0:9d17e4342598 | 1037 | /* KEIL-RL File System does not support relative directry */ |
wolfSSL | 0:9d17e4342598 | 1038 | #else |
wolfSSL | 0:9d17e4342598 | 1039 | |
wolfSSL | 0:9d17e4342598 | 1040 | #ifndef MAX_PATH |
wolfSSL | 0:9d17e4342598 | 1041 | #define MAX_PATH 256 |
wolfSSL | 0:9d17e4342598 | 1042 | #endif |
wolfSSL | 0:9d17e4342598 | 1043 | |
wolfSSL | 0:9d17e4342598 | 1044 | /* do back x number of directories */ |
wolfSSL | 0:9d17e4342598 | 1045 | static INLINE void ChangeDirBack(int x) |
wolfSSL | 0:9d17e4342598 | 1046 | { |
wolfSSL | 0:9d17e4342598 | 1047 | char path[MAX_PATH]; |
wolfSSL | 0:9d17e4342598 | 1048 | |
wolfSSL | 0:9d17e4342598 | 1049 | if (x == 1) |
wolfSSL | 0:9d17e4342598 | 1050 | strncpy(path, "../", MAX_PATH); |
wolfSSL | 0:9d17e4342598 | 1051 | else if (x == 2) |
wolfSSL | 0:9d17e4342598 | 1052 | strncpy(path, "../../", MAX_PATH); |
wolfSSL | 0:9d17e4342598 | 1053 | else if (x == 3) |
wolfSSL | 0:9d17e4342598 | 1054 | strncpy(path, "../../../", MAX_PATH); |
wolfSSL | 0:9d17e4342598 | 1055 | else if (x == 4) |
wolfSSL | 0:9d17e4342598 | 1056 | strncpy(path, "../../../../", MAX_PATH); |
wolfSSL | 0:9d17e4342598 | 1057 | else |
wolfSSL | 0:9d17e4342598 | 1058 | strncpy(path, "./", MAX_PATH); |
wolfSSL | 0:9d17e4342598 | 1059 | |
wolfSSL | 0:9d17e4342598 | 1060 | if (chdir(path) < 0) |
wolfSSL | 0:9d17e4342598 | 1061 | printf("chdir to %s failed\n", path); |
wolfSSL | 0:9d17e4342598 | 1062 | } |
wolfSSL | 0:9d17e4342598 | 1063 | |
wolfSSL | 0:9d17e4342598 | 1064 | /* does current dir contain str */ |
wolfSSL | 0:9d17e4342598 | 1065 | static INLINE int CurrentDir(const char* str) |
wolfSSL | 0:9d17e4342598 | 1066 | { |
wolfSSL | 0:9d17e4342598 | 1067 | char path[MAX_PATH]; |
wolfSSL | 0:9d17e4342598 | 1068 | char* baseName; |
wolfSSL | 0:9d17e4342598 | 1069 | |
wolfSSL | 0:9d17e4342598 | 1070 | if (getcwd(path, sizeof(path)) == NULL) { |
wolfSSL | 0:9d17e4342598 | 1071 | printf("no current dir?\n"); |
wolfSSL | 0:9d17e4342598 | 1072 | return 0; |
wolfSSL | 0:9d17e4342598 | 1073 | } |
wolfSSL | 0:9d17e4342598 | 1074 | |
wolfSSL | 0:9d17e4342598 | 1075 | baseName = strrchr(path, '/'); |
wolfSSL | 0:9d17e4342598 | 1076 | if (baseName) |
wolfSSL | 0:9d17e4342598 | 1077 | baseName++; |
wolfSSL | 0:9d17e4342598 | 1078 | else |
wolfSSL | 0:9d17e4342598 | 1079 | baseName = path; |
wolfSSL | 0:9d17e4342598 | 1080 | |
wolfSSL | 0:9d17e4342598 | 1081 | if (strstr(baseName, str)) |
wolfSSL | 0:9d17e4342598 | 1082 | return 1; |
wolfSSL | 0:9d17e4342598 | 1083 | |
wolfSSL | 0:9d17e4342598 | 1084 | return 0; |
wolfSSL | 0:9d17e4342598 | 1085 | } |
wolfSSL | 0:9d17e4342598 | 1086 | |
wolfSSL | 0:9d17e4342598 | 1087 | #endif /* USE_WINDOWS_API */ |
wolfSSL | 0:9d17e4342598 | 1088 | |
wolfSSL | 0:9d17e4342598 | 1089 | |
wolfSSL | 0:9d17e4342598 | 1090 | #ifdef USE_CYASSL_MEMORY |
wolfSSL | 0:9d17e4342598 | 1091 | |
wolfSSL | 0:9d17e4342598 | 1092 | typedef struct memoryStats { |
wolfSSL | 0:9d17e4342598 | 1093 | size_t totalAllocs; /* number of allocations */ |
wolfSSL | 0:9d17e4342598 | 1094 | size_t totalBytes; /* total number of bytes allocated */ |
wolfSSL | 0:9d17e4342598 | 1095 | size_t peakBytes; /* concurrent max bytes */ |
wolfSSL | 0:9d17e4342598 | 1096 | size_t currentBytes; /* total current bytes in use */ |
wolfSSL | 0:9d17e4342598 | 1097 | } memoryStats; |
wolfSSL | 0:9d17e4342598 | 1098 | |
wolfSSL | 0:9d17e4342598 | 1099 | typedef struct memHint { |
wolfSSL | 0:9d17e4342598 | 1100 | size_t thisSize; /* size of this memory */ |
wolfSSL | 0:9d17e4342598 | 1101 | void* thisMemory; /* actual memory for user */ |
wolfSSL | 0:9d17e4342598 | 1102 | } memHint; |
wolfSSL | 0:9d17e4342598 | 1103 | |
wolfSSL | 0:9d17e4342598 | 1104 | typedef struct memoryTrack { |
wolfSSL | 0:9d17e4342598 | 1105 | union { |
wolfSSL | 0:9d17e4342598 | 1106 | memHint hint; |
wolfSSL | 0:9d17e4342598 | 1107 | byte alignit[16]; /* make sure we have strong alignment */ |
wolfSSL | 0:9d17e4342598 | 1108 | } u; |
wolfSSL | 0:9d17e4342598 | 1109 | } memoryTrack; |
wolfSSL | 0:9d17e4342598 | 1110 | |
wolfSSL | 0:9d17e4342598 | 1111 | #if defined(CYASSL_TRACK_MEMORY) |
wolfSSL | 0:9d17e4342598 | 1112 | #define DO_MEM_STATS |
wolfSSL | 0:9d17e4342598 | 1113 | static memoryStats ourMemStats; |
wolfSSL | 0:9d17e4342598 | 1114 | #endif |
wolfSSL | 0:9d17e4342598 | 1115 | |
wolfSSL | 0:9d17e4342598 | 1116 | static INLINE void* TrackMalloc(size_t sz) |
wolfSSL | 0:9d17e4342598 | 1117 | { |
wolfSSL | 0:9d17e4342598 | 1118 | memoryTrack* mt; |
wolfSSL | 0:9d17e4342598 | 1119 | |
wolfSSL | 0:9d17e4342598 | 1120 | if (sz == 0) |
wolfSSL | 0:9d17e4342598 | 1121 | return NULL; |
wolfSSL | 0:9d17e4342598 | 1122 | |
wolfSSL | 0:9d17e4342598 | 1123 | mt = (memoryTrack*)malloc(sizeof(memoryTrack) + sz); |
wolfSSL | 0:9d17e4342598 | 1124 | if (mt == NULL) |
wolfSSL | 0:9d17e4342598 | 1125 | return NULL; |
wolfSSL | 0:9d17e4342598 | 1126 | |
wolfSSL | 0:9d17e4342598 | 1127 | mt->u.hint.thisSize = sz; |
wolfSSL | 0:9d17e4342598 | 1128 | mt->u.hint.thisMemory = (byte*)mt + sizeof(memoryTrack); |
wolfSSL | 0:9d17e4342598 | 1129 | |
wolfSSL | 0:9d17e4342598 | 1130 | #ifdef DO_MEM_STATS |
wolfSSL | 0:9d17e4342598 | 1131 | ourMemStats.totalAllocs++; |
wolfSSL | 0:9d17e4342598 | 1132 | ourMemStats.totalBytes += sz; |
wolfSSL | 0:9d17e4342598 | 1133 | ourMemStats.currentBytes += sz; |
wolfSSL | 0:9d17e4342598 | 1134 | if (ourMemStats.currentBytes > ourMemStats.peakBytes) |
wolfSSL | 0:9d17e4342598 | 1135 | ourMemStats.peakBytes = ourMemStats.currentBytes; |
wolfSSL | 0:9d17e4342598 | 1136 | #endif |
wolfSSL | 0:9d17e4342598 | 1137 | |
wolfSSL | 0:9d17e4342598 | 1138 | return mt->u.hint.thisMemory; |
wolfSSL | 0:9d17e4342598 | 1139 | } |
wolfSSL | 0:9d17e4342598 | 1140 | |
wolfSSL | 0:9d17e4342598 | 1141 | |
wolfSSL | 0:9d17e4342598 | 1142 | static INLINE void TrackFree(void* ptr) |
wolfSSL | 0:9d17e4342598 | 1143 | { |
wolfSSL | 0:9d17e4342598 | 1144 | memoryTrack* mt; |
wolfSSL | 0:9d17e4342598 | 1145 | |
wolfSSL | 0:9d17e4342598 | 1146 | if (ptr == NULL) |
wolfSSL | 0:9d17e4342598 | 1147 | return; |
wolfSSL | 0:9d17e4342598 | 1148 | |
wolfSSL | 0:9d17e4342598 | 1149 | mt = (memoryTrack*)ptr; |
wolfSSL | 0:9d17e4342598 | 1150 | --mt; /* same as minus sizeof(memoryTrack), removes header */ |
wolfSSL | 0:9d17e4342598 | 1151 | |
wolfSSL | 0:9d17e4342598 | 1152 | #ifdef DO_MEM_STATS |
wolfSSL | 0:9d17e4342598 | 1153 | ourMemStats.currentBytes -= mt->u.hint.thisSize; |
wolfSSL | 0:9d17e4342598 | 1154 | #endif |
wolfSSL | 0:9d17e4342598 | 1155 | |
wolfSSL | 0:9d17e4342598 | 1156 | free(mt); |
wolfSSL | 0:9d17e4342598 | 1157 | } |
wolfSSL | 0:9d17e4342598 | 1158 | |
wolfSSL | 0:9d17e4342598 | 1159 | |
wolfSSL | 0:9d17e4342598 | 1160 | static INLINE void* TrackRealloc(void* ptr, size_t sz) |
wolfSSL | 0:9d17e4342598 | 1161 | { |
wolfSSL | 0:9d17e4342598 | 1162 | void* ret = TrackMalloc(sz); |
wolfSSL | 0:9d17e4342598 | 1163 | |
wolfSSL | 0:9d17e4342598 | 1164 | if (ptr) { |
wolfSSL | 0:9d17e4342598 | 1165 | /* if realloc is bigger, don't overread old ptr */ |
wolfSSL | 0:9d17e4342598 | 1166 | memoryTrack* mt = (memoryTrack*)ptr; |
wolfSSL | 0:9d17e4342598 | 1167 | --mt; /* same as minus sizeof(memoryTrack), removes header */ |
wolfSSL | 0:9d17e4342598 | 1168 | |
wolfSSL | 0:9d17e4342598 | 1169 | if (mt->u.hint.thisSize < sz) |
wolfSSL | 0:9d17e4342598 | 1170 | sz = mt->u.hint.thisSize; |
wolfSSL | 0:9d17e4342598 | 1171 | } |
wolfSSL | 0:9d17e4342598 | 1172 | |
wolfSSL | 0:9d17e4342598 | 1173 | if (ret && ptr) |
wolfSSL | 0:9d17e4342598 | 1174 | memcpy(ret, ptr, sz); |
wolfSSL | 0:9d17e4342598 | 1175 | |
wolfSSL | 0:9d17e4342598 | 1176 | if (ret) |
wolfSSL | 0:9d17e4342598 | 1177 | TrackFree(ptr); |
wolfSSL | 0:9d17e4342598 | 1178 | |
wolfSSL | 0:9d17e4342598 | 1179 | return ret; |
wolfSSL | 0:9d17e4342598 | 1180 | } |
wolfSSL | 0:9d17e4342598 | 1181 | |
wolfSSL | 0:9d17e4342598 | 1182 | static INLINE void InitMemoryTracker(void) |
wolfSSL | 0:9d17e4342598 | 1183 | { |
wolfSSL | 0:9d17e4342598 | 1184 | if (CyaSSL_SetAllocators(TrackMalloc, TrackFree, TrackRealloc) != 0) |
wolfSSL | 0:9d17e4342598 | 1185 | err_sys("CyaSSL SetAllocators failed for track memory"); |
wolfSSL | 0:9d17e4342598 | 1186 | |
wolfSSL | 0:9d17e4342598 | 1187 | #ifdef DO_MEM_STATS |
wolfSSL | 0:9d17e4342598 | 1188 | ourMemStats.totalAllocs = 0; |
wolfSSL | 0:9d17e4342598 | 1189 | ourMemStats.totalBytes = 0; |
wolfSSL | 0:9d17e4342598 | 1190 | ourMemStats.peakBytes = 0; |
wolfSSL | 0:9d17e4342598 | 1191 | ourMemStats.currentBytes = 0; |
wolfSSL | 0:9d17e4342598 | 1192 | #endif |
wolfSSL | 0:9d17e4342598 | 1193 | } |
wolfSSL | 0:9d17e4342598 | 1194 | |
wolfSSL | 0:9d17e4342598 | 1195 | static INLINE void ShowMemoryTracker(void) |
wolfSSL | 0:9d17e4342598 | 1196 | { |
wolfSSL | 0:9d17e4342598 | 1197 | #ifdef DO_MEM_STATS |
wolfSSL | 0:9d17e4342598 | 1198 | printf("total Allocs = %9lu\n", |
wolfSSL | 0:9d17e4342598 | 1199 | (unsigned long)ourMemStats.totalAllocs); |
wolfSSL | 0:9d17e4342598 | 1200 | printf("total Bytes = %9lu\n", |
wolfSSL | 0:9d17e4342598 | 1201 | (unsigned long)ourMemStats.totalBytes); |
wolfSSL | 0:9d17e4342598 | 1202 | printf("peak Bytes = %9lu\n", |
wolfSSL | 0:9d17e4342598 | 1203 | (unsigned long)ourMemStats.peakBytes); |
wolfSSL | 0:9d17e4342598 | 1204 | printf("current Bytes = %9lu\n", |
wolfSSL | 0:9d17e4342598 | 1205 | (unsigned long)ourMemStats.currentBytes); |
wolfSSL | 0:9d17e4342598 | 1206 | #endif |
wolfSSL | 0:9d17e4342598 | 1207 | } |
wolfSSL | 0:9d17e4342598 | 1208 | |
wolfSSL | 0:9d17e4342598 | 1209 | #endif /* USE_CYASSL_MEMORY */ |
wolfSSL | 0:9d17e4342598 | 1210 | |
wolfSSL | 0:9d17e4342598 | 1211 | |
wolfSSL | 0:9d17e4342598 | 1212 | #ifdef HAVE_STACK_SIZE |
wolfSSL | 0:9d17e4342598 | 1213 | |
wolfSSL | 0:9d17e4342598 | 1214 | typedef THREAD_RETURN CYASSL_THREAD (*thread_func)(void* args); |
wolfSSL | 0:9d17e4342598 | 1215 | |
wolfSSL | 0:9d17e4342598 | 1216 | |
wolfSSL | 0:9d17e4342598 | 1217 | static INLINE void StackSizeCheck(func_args* args, thread_func tf) |
wolfSSL | 0:9d17e4342598 | 1218 | { |
wolfSSL | 0:9d17e4342598 | 1219 | int ret, i, used; |
wolfSSL | 0:9d17e4342598 | 1220 | unsigned char* myStack; |
wolfSSL | 0:9d17e4342598 | 1221 | int stackSize = 1024*128; |
wolfSSL | 0:9d17e4342598 | 1222 | pthread_attr_t myAttr; |
wolfSSL | 0:9d17e4342598 | 1223 | pthread_t threadId; |
wolfSSL | 0:9d17e4342598 | 1224 | |
wolfSSL | 0:9d17e4342598 | 1225 | #ifdef PTHREAD_STACK_MIN |
wolfSSL | 0:9d17e4342598 | 1226 | if (stackSize < PTHREAD_STACK_MIN) |
wolfSSL | 0:9d17e4342598 | 1227 | stackSize = PTHREAD_STACK_MIN; |
wolfSSL | 0:9d17e4342598 | 1228 | #endif |
wolfSSL | 0:9d17e4342598 | 1229 | |
wolfSSL | 0:9d17e4342598 | 1230 | ret = posix_memalign((void**)&myStack, sysconf(_SC_PAGESIZE), stackSize); |
wolfSSL | 0:9d17e4342598 | 1231 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 1232 | err_sys("posix_memalign failed\n"); |
wolfSSL | 0:9d17e4342598 | 1233 | |
wolfSSL | 0:9d17e4342598 | 1234 | memset(myStack, 0x01, stackSize); |
wolfSSL | 0:9d17e4342598 | 1235 | |
wolfSSL | 0:9d17e4342598 | 1236 | ret = pthread_attr_init(&myAttr); |
wolfSSL | 0:9d17e4342598 | 1237 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 1238 | err_sys("attr_init failed"); |
wolfSSL | 0:9d17e4342598 | 1239 | |
wolfSSL | 0:9d17e4342598 | 1240 | ret = pthread_attr_setstack(&myAttr, myStack, stackSize); |
wolfSSL | 0:9d17e4342598 | 1241 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 1242 | err_sys("attr_setstackaddr failed"); |
wolfSSL | 0:9d17e4342598 | 1243 | |
wolfSSL | 0:9d17e4342598 | 1244 | ret = pthread_create(&threadId, &myAttr, tf, args); |
wolfSSL | 0:9d17e4342598 | 1245 | if (ret != 0) { |
wolfSSL | 0:9d17e4342598 | 1246 | perror("pthread_create failed"); |
wolfSSL | 0:9d17e4342598 | 1247 | exit(EXIT_FAILURE); |
wolfSSL | 0:9d17e4342598 | 1248 | } |
wolfSSL | 0:9d17e4342598 | 1249 | |
wolfSSL | 0:9d17e4342598 | 1250 | ret = pthread_join(threadId, NULL); |
wolfSSL | 0:9d17e4342598 | 1251 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 1252 | err_sys("pthread_join failed"); |
wolfSSL | 0:9d17e4342598 | 1253 | |
wolfSSL | 0:9d17e4342598 | 1254 | for (i = 0; i < stackSize; i++) { |
wolfSSL | 0:9d17e4342598 | 1255 | if (myStack[i] != 0x01) { |
wolfSSL | 0:9d17e4342598 | 1256 | break; |
wolfSSL | 0:9d17e4342598 | 1257 | } |
wolfSSL | 0:9d17e4342598 | 1258 | } |
wolfSSL | 0:9d17e4342598 | 1259 | |
wolfSSL | 0:9d17e4342598 | 1260 | used = stackSize - i; |
wolfSSL | 0:9d17e4342598 | 1261 | printf("stack used = %d\n", used); |
wolfSSL | 0:9d17e4342598 | 1262 | } |
wolfSSL | 0:9d17e4342598 | 1263 | |
wolfSSL | 0:9d17e4342598 | 1264 | |
wolfSSL | 0:9d17e4342598 | 1265 | #endif /* HAVE_STACK_SIZE */ |
wolfSSL | 0:9d17e4342598 | 1266 | |
wolfSSL | 0:9d17e4342598 | 1267 | |
wolfSSL | 0:9d17e4342598 | 1268 | #ifdef STACK_TRAP |
wolfSSL | 0:9d17e4342598 | 1269 | |
wolfSSL | 0:9d17e4342598 | 1270 | /* good settings |
wolfSSL | 0:9d17e4342598 | 1271 | --enable-debug --disable-shared C_EXTRA_FLAGS="-DUSER_TIME -DTFM_TIMING_RESISTANT -DPOSITIVE_EXP_ONLY -DSTACK_TRAP" |
wolfSSL | 0:9d17e4342598 | 1272 | |
wolfSSL | 0:9d17e4342598 | 1273 | */ |
wolfSSL | 0:9d17e4342598 | 1274 | |
wolfSSL | 0:9d17e4342598 | 1275 | #ifdef HAVE_STACK_SIZE |
wolfSSL | 0:9d17e4342598 | 1276 | /* client only for now, setrlimit will fail if pthread_create() called */ |
wolfSSL | 0:9d17e4342598 | 1277 | /* STACK_SIZE does pthread_create() on client */ |
wolfSSL | 0:9d17e4342598 | 1278 | #error "can't use STACK_TRAP with STACK_SIZE, setrlimit will fail" |
wolfSSL | 0:9d17e4342598 | 1279 | #endif /* HAVE_STACK_SIZE */ |
wolfSSL | 0:9d17e4342598 | 1280 | |
wolfSSL | 0:9d17e4342598 | 1281 | static INLINE void StackTrap(void) |
wolfSSL | 0:9d17e4342598 | 1282 | { |
wolfSSL | 0:9d17e4342598 | 1283 | struct rlimit rl; |
wolfSSL | 0:9d17e4342598 | 1284 | if (getrlimit(RLIMIT_STACK, &rl) != 0) |
wolfSSL | 0:9d17e4342598 | 1285 | err_sys("getrlimit failed"); |
wolfSSL | 0:9d17e4342598 | 1286 | printf("rlim_cur = %llu\n", rl.rlim_cur); |
wolfSSL | 0:9d17e4342598 | 1287 | rl.rlim_cur = 1024*21; /* adjust trap size here */ |
wolfSSL | 0:9d17e4342598 | 1288 | if (setrlimit(RLIMIT_STACK, &rl) != 0) { |
wolfSSL | 0:9d17e4342598 | 1289 | perror("setrlimit"); |
wolfSSL | 0:9d17e4342598 | 1290 | err_sys("setrlimit failed"); |
wolfSSL | 0:9d17e4342598 | 1291 | } |
wolfSSL | 0:9d17e4342598 | 1292 | } |
wolfSSL | 0:9d17e4342598 | 1293 | |
wolfSSL | 0:9d17e4342598 | 1294 | #else /* STACK_TRAP */ |
wolfSSL | 0:9d17e4342598 | 1295 | |
wolfSSL | 0:9d17e4342598 | 1296 | static INLINE void StackTrap(void) |
wolfSSL | 0:9d17e4342598 | 1297 | { |
wolfSSL | 0:9d17e4342598 | 1298 | } |
wolfSSL | 0:9d17e4342598 | 1299 | |
wolfSSL | 0:9d17e4342598 | 1300 | #endif /* STACK_TRAP */ |
wolfSSL | 0:9d17e4342598 | 1301 | |
wolfSSL | 0:9d17e4342598 | 1302 | |
wolfSSL | 0:9d17e4342598 | 1303 | #ifdef ATOMIC_USER |
wolfSSL | 0:9d17e4342598 | 1304 | |
wolfSSL | 0:9d17e4342598 | 1305 | /* Atomic Encrypt Context example */ |
wolfSSL | 0:9d17e4342598 | 1306 | typedef struct AtomicEncCtx { |
wolfSSL | 0:9d17e4342598 | 1307 | int keySetup; /* have we done key setup yet */ |
wolfSSL | 0:9d17e4342598 | 1308 | Aes aes; /* for aes example */ |
wolfSSL | 0:9d17e4342598 | 1309 | } AtomicEncCtx; |
wolfSSL | 0:9d17e4342598 | 1310 | |
wolfSSL | 0:9d17e4342598 | 1311 | |
wolfSSL | 0:9d17e4342598 | 1312 | /* Atomic Decrypt Context example */ |
wolfSSL | 0:9d17e4342598 | 1313 | typedef struct AtomicDecCtx { |
wolfSSL | 0:9d17e4342598 | 1314 | int keySetup; /* have we done key setup yet */ |
wolfSSL | 0:9d17e4342598 | 1315 | Aes aes; /* for aes example */ |
wolfSSL | 0:9d17e4342598 | 1316 | } AtomicDecCtx; |
wolfSSL | 0:9d17e4342598 | 1317 | |
wolfSSL | 0:9d17e4342598 | 1318 | |
wolfSSL | 0:9d17e4342598 | 1319 | static INLINE int myMacEncryptCb(CYASSL* ssl, unsigned char* macOut, |
wolfSSL | 0:9d17e4342598 | 1320 | const unsigned char* macIn, unsigned int macInSz, int macContent, |
wolfSSL | 0:9d17e4342598 | 1321 | int macVerify, unsigned char* encOut, const unsigned char* encIn, |
wolfSSL | 0:9d17e4342598 | 1322 | unsigned int encSz, void* ctx) |
wolfSSL | 0:9d17e4342598 | 1323 | { |
wolfSSL | 0:9d17e4342598 | 1324 | int ret; |
wolfSSL | 0:9d17e4342598 | 1325 | Hmac hmac; |
wolfSSL | 0:9d17e4342598 | 1326 | byte myInner[CYASSL_TLS_HMAC_INNER_SZ]; |
wolfSSL | 0:9d17e4342598 | 1327 | AtomicEncCtx* encCtx = (AtomicEncCtx*)ctx; |
wolfSSL | 0:9d17e4342598 | 1328 | const char* tlsStr = "TLS"; |
wolfSSL | 0:9d17e4342598 | 1329 | |
wolfSSL | 0:9d17e4342598 | 1330 | /* example supports (d)tls aes */ |
wolfSSL | 0:9d17e4342598 | 1331 | if (CyaSSL_GetBulkCipher(ssl) != cyassl_aes) { |
wolfSSL | 0:9d17e4342598 | 1332 | printf("myMacEncryptCb not using AES\n"); |
wolfSSL | 0:9d17e4342598 | 1333 | return -1; |
wolfSSL | 0:9d17e4342598 | 1334 | } |
wolfSSL | 0:9d17e4342598 | 1335 | |
wolfSSL | 0:9d17e4342598 | 1336 | if (strstr(CyaSSL_get_version(ssl), tlsStr) == NULL) { |
wolfSSL | 0:9d17e4342598 | 1337 | printf("myMacEncryptCb not using (D)TLS\n"); |
wolfSSL | 0:9d17e4342598 | 1338 | return -1; |
wolfSSL | 0:9d17e4342598 | 1339 | } |
wolfSSL | 0:9d17e4342598 | 1340 | |
wolfSSL | 0:9d17e4342598 | 1341 | /* hmac, not needed if aead mode */ |
wolfSSL | 0:9d17e4342598 | 1342 | CyaSSL_SetTlsHmacInner(ssl, myInner, macInSz, macContent, macVerify); |
wolfSSL | 0:9d17e4342598 | 1343 | |
wolfSSL | 0:9d17e4342598 | 1344 | ret = HmacSetKey(&hmac, CyaSSL_GetHmacType(ssl), |
wolfSSL | 0:9d17e4342598 | 1345 | CyaSSL_GetMacSecret(ssl, macVerify), CyaSSL_GetHmacSize(ssl)); |
wolfSSL | 0:9d17e4342598 | 1346 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 1347 | return ret; |
wolfSSL | 0:9d17e4342598 | 1348 | HmacUpdate(&hmac, myInner, sizeof(myInner)); |
wolfSSL | 0:9d17e4342598 | 1349 | HmacUpdate(&hmac, macIn, macInSz); |
wolfSSL | 0:9d17e4342598 | 1350 | HmacFinal(&hmac, macOut); |
wolfSSL | 0:9d17e4342598 | 1351 | |
wolfSSL | 0:9d17e4342598 | 1352 | |
wolfSSL | 0:9d17e4342598 | 1353 | /* encrypt setup on first time */ |
wolfSSL | 0:9d17e4342598 | 1354 | if (encCtx->keySetup == 0) { |
wolfSSL | 0:9d17e4342598 | 1355 | int keyLen = CyaSSL_GetKeySize(ssl); |
wolfSSL | 0:9d17e4342598 | 1356 | const byte* key; |
wolfSSL | 0:9d17e4342598 | 1357 | const byte* iv; |
wolfSSL | 0:9d17e4342598 | 1358 | |
wolfSSL | 0:9d17e4342598 | 1359 | if (CyaSSL_GetSide(ssl) == CYASSL_CLIENT_END) { |
wolfSSL | 0:9d17e4342598 | 1360 | key = CyaSSL_GetClientWriteKey(ssl); |
wolfSSL | 0:9d17e4342598 | 1361 | iv = CyaSSL_GetClientWriteIV(ssl); |
wolfSSL | 0:9d17e4342598 | 1362 | } |
wolfSSL | 0:9d17e4342598 | 1363 | else { |
wolfSSL | 0:9d17e4342598 | 1364 | key = CyaSSL_GetServerWriteKey(ssl); |
wolfSSL | 0:9d17e4342598 | 1365 | iv = CyaSSL_GetServerWriteIV(ssl); |
wolfSSL | 0:9d17e4342598 | 1366 | } |
wolfSSL | 0:9d17e4342598 | 1367 | |
wolfSSL | 0:9d17e4342598 | 1368 | ret = AesSetKey(&encCtx->aes, key, keyLen, iv, AES_ENCRYPTION); |
wolfSSL | 0:9d17e4342598 | 1369 | if (ret != 0) { |
wolfSSL | 0:9d17e4342598 | 1370 | printf("AesSetKey failed in myMacEncryptCb\n"); |
wolfSSL | 0:9d17e4342598 | 1371 | return ret; |
wolfSSL | 0:9d17e4342598 | 1372 | } |
wolfSSL | 0:9d17e4342598 | 1373 | encCtx->keySetup = 1; |
wolfSSL | 0:9d17e4342598 | 1374 | } |
wolfSSL | 0:9d17e4342598 | 1375 | |
wolfSSL | 0:9d17e4342598 | 1376 | /* encrypt */ |
wolfSSL | 0:9d17e4342598 | 1377 | return AesCbcEncrypt(&encCtx->aes, encOut, encIn, encSz); |
wolfSSL | 0:9d17e4342598 | 1378 | } |
wolfSSL | 0:9d17e4342598 | 1379 | |
wolfSSL | 0:9d17e4342598 | 1380 | |
wolfSSL | 0:9d17e4342598 | 1381 | static INLINE int myDecryptVerifyCb(CYASSL* ssl, |
wolfSSL | 0:9d17e4342598 | 1382 | unsigned char* decOut, const unsigned char* decIn, |
wolfSSL | 0:9d17e4342598 | 1383 | unsigned int decSz, int macContent, int macVerify, |
wolfSSL | 0:9d17e4342598 | 1384 | unsigned int* padSz, void* ctx) |
wolfSSL | 0:9d17e4342598 | 1385 | { |
wolfSSL | 0:9d17e4342598 | 1386 | AtomicDecCtx* decCtx = (AtomicDecCtx*)ctx; |
wolfSSL | 0:9d17e4342598 | 1387 | int ret = 0; |
wolfSSL | 0:9d17e4342598 | 1388 | int macInSz = 0; |
wolfSSL | 0:9d17e4342598 | 1389 | int ivExtra = 0; |
wolfSSL | 0:9d17e4342598 | 1390 | int digestSz = CyaSSL_GetHmacSize(ssl); |
wolfSSL | 0:9d17e4342598 | 1391 | unsigned int pad = 0; |
wolfSSL | 0:9d17e4342598 | 1392 | unsigned int padByte = 0; |
wolfSSL | 0:9d17e4342598 | 1393 | Hmac hmac; |
wolfSSL | 0:9d17e4342598 | 1394 | byte myInner[CYASSL_TLS_HMAC_INNER_SZ]; |
wolfSSL | 0:9d17e4342598 | 1395 | byte verify[MAX_DIGEST_SIZE]; |
wolfSSL | 0:9d17e4342598 | 1396 | const char* tlsStr = "TLS"; |
wolfSSL | 0:9d17e4342598 | 1397 | |
wolfSSL | 0:9d17e4342598 | 1398 | /* example supports (d)tls aes */ |
wolfSSL | 0:9d17e4342598 | 1399 | if (CyaSSL_GetBulkCipher(ssl) != cyassl_aes) { |
wolfSSL | 0:9d17e4342598 | 1400 | printf("myMacEncryptCb not using AES\n"); |
wolfSSL | 0:9d17e4342598 | 1401 | return -1; |
wolfSSL | 0:9d17e4342598 | 1402 | } |
wolfSSL | 0:9d17e4342598 | 1403 | |
wolfSSL | 0:9d17e4342598 | 1404 | if (strstr(CyaSSL_get_version(ssl), tlsStr) == NULL) { |
wolfSSL | 0:9d17e4342598 | 1405 | printf("myMacEncryptCb not using (D)TLS\n"); |
wolfSSL | 0:9d17e4342598 | 1406 | return -1; |
wolfSSL | 0:9d17e4342598 | 1407 | } |
wolfSSL | 0:9d17e4342598 | 1408 | |
wolfSSL | 0:9d17e4342598 | 1409 | /*decrypt */ |
wolfSSL | 0:9d17e4342598 | 1410 | if (decCtx->keySetup == 0) { |
wolfSSL | 0:9d17e4342598 | 1411 | int keyLen = CyaSSL_GetKeySize(ssl); |
wolfSSL | 0:9d17e4342598 | 1412 | const byte* key; |
wolfSSL | 0:9d17e4342598 | 1413 | const byte* iv; |
wolfSSL | 0:9d17e4342598 | 1414 | |
wolfSSL | 0:9d17e4342598 | 1415 | /* decrypt is from other side (peer) */ |
wolfSSL | 0:9d17e4342598 | 1416 | if (CyaSSL_GetSide(ssl) == CYASSL_SERVER_END) { |
wolfSSL | 0:9d17e4342598 | 1417 | key = CyaSSL_GetClientWriteKey(ssl); |
wolfSSL | 0:9d17e4342598 | 1418 | iv = CyaSSL_GetClientWriteIV(ssl); |
wolfSSL | 0:9d17e4342598 | 1419 | } |
wolfSSL | 0:9d17e4342598 | 1420 | else { |
wolfSSL | 0:9d17e4342598 | 1421 | key = CyaSSL_GetServerWriteKey(ssl); |
wolfSSL | 0:9d17e4342598 | 1422 | iv = CyaSSL_GetServerWriteIV(ssl); |
wolfSSL | 0:9d17e4342598 | 1423 | } |
wolfSSL | 0:9d17e4342598 | 1424 | |
wolfSSL | 0:9d17e4342598 | 1425 | ret = AesSetKey(&decCtx->aes, key, keyLen, iv, AES_DECRYPTION); |
wolfSSL | 0:9d17e4342598 | 1426 | if (ret != 0) { |
wolfSSL | 0:9d17e4342598 | 1427 | printf("AesSetKey failed in myDecryptVerifyCb\n"); |
wolfSSL | 0:9d17e4342598 | 1428 | return ret; |
wolfSSL | 0:9d17e4342598 | 1429 | } |
wolfSSL | 0:9d17e4342598 | 1430 | decCtx->keySetup = 1; |
wolfSSL | 0:9d17e4342598 | 1431 | } |
wolfSSL | 0:9d17e4342598 | 1432 | |
wolfSSL | 0:9d17e4342598 | 1433 | /* decrypt */ |
wolfSSL | 0:9d17e4342598 | 1434 | ret = AesCbcDecrypt(&decCtx->aes, decOut, decIn, decSz); |
wolfSSL | 0:9d17e4342598 | 1435 | |
wolfSSL | 0:9d17e4342598 | 1436 | if (CyaSSL_GetCipherType(ssl) == CYASSL_AEAD_TYPE) { |
wolfSSL | 0:9d17e4342598 | 1437 | *padSz = CyaSSL_GetAeadMacSize(ssl); |
wolfSSL | 0:9d17e4342598 | 1438 | return 0; /* hmac, not needed if aead mode */ |
wolfSSL | 0:9d17e4342598 | 1439 | } |
wolfSSL | 0:9d17e4342598 | 1440 | |
wolfSSL | 0:9d17e4342598 | 1441 | if (CyaSSL_GetCipherType(ssl) == CYASSL_BLOCK_TYPE) { |
wolfSSL | 0:9d17e4342598 | 1442 | pad = *(decOut + decSz - 1); |
wolfSSL | 0:9d17e4342598 | 1443 | padByte = 1; |
wolfSSL | 0:9d17e4342598 | 1444 | if (CyaSSL_IsTLSv1_1(ssl)) |
wolfSSL | 0:9d17e4342598 | 1445 | ivExtra = CyaSSL_GetCipherBlockSize(ssl); |
wolfSSL | 0:9d17e4342598 | 1446 | } |
wolfSSL | 0:9d17e4342598 | 1447 | |
wolfSSL | 0:9d17e4342598 | 1448 | *padSz = CyaSSL_GetHmacSize(ssl) + pad + padByte; |
wolfSSL | 0:9d17e4342598 | 1449 | macInSz = decSz - ivExtra - digestSz - pad - padByte; |
wolfSSL | 0:9d17e4342598 | 1450 | |
wolfSSL | 0:9d17e4342598 | 1451 | CyaSSL_SetTlsHmacInner(ssl, myInner, macInSz, macContent, macVerify); |
wolfSSL | 0:9d17e4342598 | 1452 | |
wolfSSL | 0:9d17e4342598 | 1453 | ret = HmacSetKey(&hmac, CyaSSL_GetHmacType(ssl), |
wolfSSL | 0:9d17e4342598 | 1454 | CyaSSL_GetMacSecret(ssl, macVerify), digestSz); |
wolfSSL | 0:9d17e4342598 | 1455 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 1456 | return ret; |
wolfSSL | 0:9d17e4342598 | 1457 | HmacUpdate(&hmac, myInner, sizeof(myInner)); |
wolfSSL | 0:9d17e4342598 | 1458 | HmacUpdate(&hmac, decOut + ivExtra, macInSz); |
wolfSSL | 0:9d17e4342598 | 1459 | HmacFinal(&hmac, verify); |
wolfSSL | 0:9d17e4342598 | 1460 | |
wolfSSL | 0:9d17e4342598 | 1461 | if (memcmp(verify, decOut + decSz - digestSz - pad - padByte, |
wolfSSL | 0:9d17e4342598 | 1462 | digestSz) != 0) { |
wolfSSL | 0:9d17e4342598 | 1463 | printf("myDecryptVerify verify failed\n"); |
wolfSSL | 0:9d17e4342598 | 1464 | return -1; |
wolfSSL | 0:9d17e4342598 | 1465 | } |
wolfSSL | 0:9d17e4342598 | 1466 | |
wolfSSL | 0:9d17e4342598 | 1467 | return ret; |
wolfSSL | 0:9d17e4342598 | 1468 | } |
wolfSSL | 0:9d17e4342598 | 1469 | |
wolfSSL | 0:9d17e4342598 | 1470 | |
wolfSSL | 0:9d17e4342598 | 1471 | static INLINE void SetupAtomicUser(CYASSL_CTX* ctx, CYASSL* ssl) |
wolfSSL | 0:9d17e4342598 | 1472 | { |
wolfSSL | 0:9d17e4342598 | 1473 | AtomicEncCtx* encCtx; |
wolfSSL | 0:9d17e4342598 | 1474 | AtomicDecCtx* decCtx; |
wolfSSL | 0:9d17e4342598 | 1475 | |
wolfSSL | 0:9d17e4342598 | 1476 | encCtx = (AtomicEncCtx*)malloc(sizeof(AtomicEncCtx)); |
wolfSSL | 0:9d17e4342598 | 1477 | if (encCtx == NULL) |
wolfSSL | 0:9d17e4342598 | 1478 | err_sys("AtomicEncCtx malloc failed"); |
wolfSSL | 0:9d17e4342598 | 1479 | memset(encCtx, 0, sizeof(AtomicEncCtx)); |
wolfSSL | 0:9d17e4342598 | 1480 | |
wolfSSL | 0:9d17e4342598 | 1481 | decCtx = (AtomicDecCtx*)malloc(sizeof(AtomicDecCtx)); |
wolfSSL | 0:9d17e4342598 | 1482 | if (decCtx == NULL) { |
wolfSSL | 0:9d17e4342598 | 1483 | free(encCtx); |
wolfSSL | 0:9d17e4342598 | 1484 | err_sys("AtomicDecCtx malloc failed"); |
wolfSSL | 0:9d17e4342598 | 1485 | } |
wolfSSL | 0:9d17e4342598 | 1486 | memset(decCtx, 0, sizeof(AtomicDecCtx)); |
wolfSSL | 0:9d17e4342598 | 1487 | |
wolfSSL | 0:9d17e4342598 | 1488 | CyaSSL_CTX_SetMacEncryptCb(ctx, myMacEncryptCb); |
wolfSSL | 0:9d17e4342598 | 1489 | CyaSSL_SetMacEncryptCtx(ssl, encCtx); |
wolfSSL | 0:9d17e4342598 | 1490 | |
wolfSSL | 0:9d17e4342598 | 1491 | CyaSSL_CTX_SetDecryptVerifyCb(ctx, myDecryptVerifyCb); |
wolfSSL | 0:9d17e4342598 | 1492 | CyaSSL_SetDecryptVerifyCtx(ssl, decCtx); |
wolfSSL | 0:9d17e4342598 | 1493 | } |
wolfSSL | 0:9d17e4342598 | 1494 | |
wolfSSL | 0:9d17e4342598 | 1495 | |
wolfSSL | 0:9d17e4342598 | 1496 | static INLINE void FreeAtomicUser(CYASSL* ssl) |
wolfSSL | 0:9d17e4342598 | 1497 | { |
wolfSSL | 0:9d17e4342598 | 1498 | AtomicEncCtx* encCtx = CyaSSL_GetMacEncryptCtx(ssl); |
wolfSSL | 0:9d17e4342598 | 1499 | AtomicDecCtx* decCtx = CyaSSL_GetDecryptVerifyCtx(ssl); |
wolfSSL | 0:9d17e4342598 | 1500 | |
wolfSSL | 0:9d17e4342598 | 1501 | free(decCtx); |
wolfSSL | 0:9d17e4342598 | 1502 | free(encCtx); |
wolfSSL | 0:9d17e4342598 | 1503 | } |
wolfSSL | 0:9d17e4342598 | 1504 | |
wolfSSL | 0:9d17e4342598 | 1505 | #endif /* ATOMIC_USER */ |
wolfSSL | 0:9d17e4342598 | 1506 | |
wolfSSL | 0:9d17e4342598 | 1507 | |
wolfSSL | 0:9d17e4342598 | 1508 | #ifdef HAVE_PK_CALLBACKS |
wolfSSL | 0:9d17e4342598 | 1509 | |
wolfSSL | 0:9d17e4342598 | 1510 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 1511 | |
wolfSSL | 0:9d17e4342598 | 1512 | static INLINE int myEccSign(CYASSL* ssl, const byte* in, word32 inSz, |
wolfSSL | 0:9d17e4342598 | 1513 | byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx) |
wolfSSL | 0:9d17e4342598 | 1514 | { |
wolfSSL | 0:9d17e4342598 | 1515 | RNG rng; |
wolfSSL | 0:9d17e4342598 | 1516 | int ret; |
wolfSSL | 0:9d17e4342598 | 1517 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 1518 | ecc_key myKey; |
wolfSSL | 0:9d17e4342598 | 1519 | |
wolfSSL | 0:9d17e4342598 | 1520 | (void)ssl; |
wolfSSL | 0:9d17e4342598 | 1521 | (void)ctx; |
wolfSSL | 0:9d17e4342598 | 1522 | |
wolfSSL | 0:9d17e4342598 | 1523 | InitRng(&rng); |
wolfSSL | 0:9d17e4342598 | 1524 | ecc_init(&myKey); |
wolfSSL | 0:9d17e4342598 | 1525 | |
wolfSSL | 0:9d17e4342598 | 1526 | ret = EccPrivateKeyDecode(key, &idx, &myKey, keySz); |
wolfSSL | 0:9d17e4342598 | 1527 | if (ret == 0) |
wolfSSL | 0:9d17e4342598 | 1528 | ret = ecc_sign_hash(in, inSz, out, outSz, &rng, &myKey); |
wolfSSL | 0:9d17e4342598 | 1529 | ecc_free(&myKey); |
wolfSSL | 0:9d17e4342598 | 1530 | |
wolfSSL | 0:9d17e4342598 | 1531 | return ret; |
wolfSSL | 0:9d17e4342598 | 1532 | } |
wolfSSL | 0:9d17e4342598 | 1533 | |
wolfSSL | 0:9d17e4342598 | 1534 | |
wolfSSL | 0:9d17e4342598 | 1535 | static INLINE int myEccVerify(CYASSL* ssl, const byte* sig, word32 sigSz, |
wolfSSL | 0:9d17e4342598 | 1536 | const byte* hash, word32 hashSz, const byte* key, word32 keySz, |
wolfSSL | 0:9d17e4342598 | 1537 | int* result, void* ctx) |
wolfSSL | 0:9d17e4342598 | 1538 | { |
wolfSSL | 0:9d17e4342598 | 1539 | int ret; |
wolfSSL | 0:9d17e4342598 | 1540 | ecc_key myKey; |
wolfSSL | 0:9d17e4342598 | 1541 | |
wolfSSL | 0:9d17e4342598 | 1542 | (void)ssl; |
wolfSSL | 0:9d17e4342598 | 1543 | (void)ctx; |
wolfSSL | 0:9d17e4342598 | 1544 | |
wolfSSL | 0:9d17e4342598 | 1545 | ecc_init(&myKey); |
wolfSSL | 0:9d17e4342598 | 1546 | |
wolfSSL | 0:9d17e4342598 | 1547 | ret = ecc_import_x963(key, keySz, &myKey); |
wolfSSL | 0:9d17e4342598 | 1548 | if (ret == 0) |
wolfSSL | 0:9d17e4342598 | 1549 | ret = ecc_verify_hash(sig, sigSz, hash, hashSz, result, &myKey); |
wolfSSL | 0:9d17e4342598 | 1550 | ecc_free(&myKey); |
wolfSSL | 0:9d17e4342598 | 1551 | |
wolfSSL | 0:9d17e4342598 | 1552 | return ret; |
wolfSSL | 0:9d17e4342598 | 1553 | } |
wolfSSL | 0:9d17e4342598 | 1554 | |
wolfSSL | 0:9d17e4342598 | 1555 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 1556 | |
wolfSSL | 0:9d17e4342598 | 1557 | #ifndef NO_RSA |
wolfSSL | 0:9d17e4342598 | 1558 | |
wolfSSL | 0:9d17e4342598 | 1559 | static INLINE int myRsaSign(CYASSL* ssl, const byte* in, word32 inSz, |
wolfSSL | 0:9d17e4342598 | 1560 | byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx) |
wolfSSL | 0:9d17e4342598 | 1561 | { |
wolfSSL | 0:9d17e4342598 | 1562 | RNG rng; |
wolfSSL | 0:9d17e4342598 | 1563 | int ret; |
wolfSSL | 0:9d17e4342598 | 1564 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 1565 | RsaKey myKey; |
wolfSSL | 0:9d17e4342598 | 1566 | |
wolfSSL | 0:9d17e4342598 | 1567 | (void)ssl; |
wolfSSL | 0:9d17e4342598 | 1568 | (void)ctx; |
wolfSSL | 0:9d17e4342598 | 1569 | |
wolfSSL | 0:9d17e4342598 | 1570 | InitRng(&rng); |
wolfSSL | 0:9d17e4342598 | 1571 | InitRsaKey(&myKey, NULL); |
wolfSSL | 0:9d17e4342598 | 1572 | |
wolfSSL | 0:9d17e4342598 | 1573 | ret = RsaPrivateKeyDecode(key, &idx, &myKey, keySz); |
wolfSSL | 0:9d17e4342598 | 1574 | if (ret == 0) |
wolfSSL | 0:9d17e4342598 | 1575 | ret = RsaSSL_Sign(in, inSz, out, *outSz, &myKey, &rng); |
wolfSSL | 0:9d17e4342598 | 1576 | if (ret > 0) { /* save and convert to 0 success */ |
wolfSSL | 0:9d17e4342598 | 1577 | *outSz = ret; |
wolfSSL | 0:9d17e4342598 | 1578 | ret = 0; |
wolfSSL | 0:9d17e4342598 | 1579 | } |
wolfSSL | 0:9d17e4342598 | 1580 | FreeRsaKey(&myKey); |
wolfSSL | 0:9d17e4342598 | 1581 | |
wolfSSL | 0:9d17e4342598 | 1582 | return ret; |
wolfSSL | 0:9d17e4342598 | 1583 | } |
wolfSSL | 0:9d17e4342598 | 1584 | |
wolfSSL | 0:9d17e4342598 | 1585 | |
wolfSSL | 0:9d17e4342598 | 1586 | static INLINE int myRsaVerify(CYASSL* ssl, byte* sig, word32 sigSz, |
wolfSSL | 0:9d17e4342598 | 1587 | byte** out, |
wolfSSL | 0:9d17e4342598 | 1588 | const byte* key, word32 keySz, |
wolfSSL | 0:9d17e4342598 | 1589 | void* ctx) |
wolfSSL | 0:9d17e4342598 | 1590 | { |
wolfSSL | 0:9d17e4342598 | 1591 | int ret; |
wolfSSL | 0:9d17e4342598 | 1592 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 1593 | RsaKey myKey; |
wolfSSL | 0:9d17e4342598 | 1594 | |
wolfSSL | 0:9d17e4342598 | 1595 | (void)ssl; |
wolfSSL | 0:9d17e4342598 | 1596 | (void)ctx; |
wolfSSL | 0:9d17e4342598 | 1597 | |
wolfSSL | 0:9d17e4342598 | 1598 | InitRsaKey(&myKey, NULL); |
wolfSSL | 0:9d17e4342598 | 1599 | |
wolfSSL | 0:9d17e4342598 | 1600 | ret = RsaPublicKeyDecode(key, &idx, &myKey, keySz); |
wolfSSL | 0:9d17e4342598 | 1601 | if (ret == 0) |
wolfSSL | 0:9d17e4342598 | 1602 | ret = RsaSSL_VerifyInline(sig, sigSz, out, &myKey); |
wolfSSL | 0:9d17e4342598 | 1603 | FreeRsaKey(&myKey); |
wolfSSL | 0:9d17e4342598 | 1604 | |
wolfSSL | 0:9d17e4342598 | 1605 | return ret; |
wolfSSL | 0:9d17e4342598 | 1606 | } |
wolfSSL | 0:9d17e4342598 | 1607 | |
wolfSSL | 0:9d17e4342598 | 1608 | |
wolfSSL | 0:9d17e4342598 | 1609 | static INLINE int myRsaEnc(CYASSL* ssl, const byte* in, word32 inSz, |
wolfSSL | 0:9d17e4342598 | 1610 | byte* out, word32* outSz, const byte* key, |
wolfSSL | 0:9d17e4342598 | 1611 | word32 keySz, void* ctx) |
wolfSSL | 0:9d17e4342598 | 1612 | { |
wolfSSL | 0:9d17e4342598 | 1613 | int ret; |
wolfSSL | 0:9d17e4342598 | 1614 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 1615 | RsaKey myKey; |
wolfSSL | 0:9d17e4342598 | 1616 | RNG rng; |
wolfSSL | 0:9d17e4342598 | 1617 | |
wolfSSL | 0:9d17e4342598 | 1618 | (void)ssl; |
wolfSSL | 0:9d17e4342598 | 1619 | (void)ctx; |
wolfSSL | 0:9d17e4342598 | 1620 | |
wolfSSL | 0:9d17e4342598 | 1621 | InitRng(&rng); |
wolfSSL | 0:9d17e4342598 | 1622 | InitRsaKey(&myKey, NULL); |
wolfSSL | 0:9d17e4342598 | 1623 | |
wolfSSL | 0:9d17e4342598 | 1624 | ret = RsaPublicKeyDecode(key, &idx, &myKey, keySz); |
wolfSSL | 0:9d17e4342598 | 1625 | if (ret == 0) { |
wolfSSL | 0:9d17e4342598 | 1626 | ret = RsaPublicEncrypt(in, inSz, out, *outSz, &myKey, &rng); |
wolfSSL | 0:9d17e4342598 | 1627 | if (ret > 0) { |
wolfSSL | 0:9d17e4342598 | 1628 | *outSz = ret; |
wolfSSL | 0:9d17e4342598 | 1629 | ret = 0; /* reset to success */ |
wolfSSL | 0:9d17e4342598 | 1630 | } |
wolfSSL | 0:9d17e4342598 | 1631 | } |
wolfSSL | 0:9d17e4342598 | 1632 | FreeRsaKey(&myKey); |
wolfSSL | 0:9d17e4342598 | 1633 | |
wolfSSL | 0:9d17e4342598 | 1634 | return ret; |
wolfSSL | 0:9d17e4342598 | 1635 | } |
wolfSSL | 0:9d17e4342598 | 1636 | |
wolfSSL | 0:9d17e4342598 | 1637 | static INLINE int myRsaDec(CYASSL* ssl, byte* in, word32 inSz, |
wolfSSL | 0:9d17e4342598 | 1638 | byte** out, |
wolfSSL | 0:9d17e4342598 | 1639 | const byte* key, word32 keySz, void* ctx) |
wolfSSL | 0:9d17e4342598 | 1640 | { |
wolfSSL | 0:9d17e4342598 | 1641 | int ret; |
wolfSSL | 0:9d17e4342598 | 1642 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 1643 | RsaKey myKey; |
wolfSSL | 0:9d17e4342598 | 1644 | |
wolfSSL | 0:9d17e4342598 | 1645 | (void)ssl; |
wolfSSL | 0:9d17e4342598 | 1646 | (void)ctx; |
wolfSSL | 0:9d17e4342598 | 1647 | |
wolfSSL | 0:9d17e4342598 | 1648 | InitRsaKey(&myKey, NULL); |
wolfSSL | 0:9d17e4342598 | 1649 | |
wolfSSL | 0:9d17e4342598 | 1650 | ret = RsaPrivateKeyDecode(key, &idx, &myKey, keySz); |
wolfSSL | 0:9d17e4342598 | 1651 | if (ret == 0) { |
wolfSSL | 0:9d17e4342598 | 1652 | ret = RsaPrivateDecryptInline(in, inSz, out, &myKey); |
wolfSSL | 0:9d17e4342598 | 1653 | } |
wolfSSL | 0:9d17e4342598 | 1654 | FreeRsaKey(&myKey); |
wolfSSL | 0:9d17e4342598 | 1655 | |
wolfSSL | 0:9d17e4342598 | 1656 | return ret; |
wolfSSL | 0:9d17e4342598 | 1657 | } |
wolfSSL | 0:9d17e4342598 | 1658 | |
wolfSSL | 0:9d17e4342598 | 1659 | #endif /* NO_RSA */ |
wolfSSL | 0:9d17e4342598 | 1660 | |
wolfSSL | 0:9d17e4342598 | 1661 | static INLINE void SetupPkCallbacks(CYASSL_CTX* ctx, CYASSL* ssl) |
wolfSSL | 0:9d17e4342598 | 1662 | { |
wolfSSL | 0:9d17e4342598 | 1663 | (void)ctx; |
wolfSSL | 0:9d17e4342598 | 1664 | (void)ssl; |
wolfSSL | 0:9d17e4342598 | 1665 | |
wolfSSL | 0:9d17e4342598 | 1666 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 1667 | CyaSSL_CTX_SetEccSignCb(ctx, myEccSign); |
wolfSSL | 0:9d17e4342598 | 1668 | CyaSSL_CTX_SetEccVerifyCb(ctx, myEccVerify); |
wolfSSL | 0:9d17e4342598 | 1669 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 1670 | #ifndef NO_RSA |
wolfSSL | 0:9d17e4342598 | 1671 | CyaSSL_CTX_SetRsaSignCb(ctx, myRsaSign); |
wolfSSL | 0:9d17e4342598 | 1672 | CyaSSL_CTX_SetRsaVerifyCb(ctx, myRsaVerify); |
wolfSSL | 0:9d17e4342598 | 1673 | CyaSSL_CTX_SetRsaEncCb(ctx, myRsaEnc); |
wolfSSL | 0:9d17e4342598 | 1674 | CyaSSL_CTX_SetRsaDecCb(ctx, myRsaDec); |
wolfSSL | 0:9d17e4342598 | 1675 | #endif /* NO_RSA */ |
wolfSSL | 0:9d17e4342598 | 1676 | } |
wolfSSL | 0:9d17e4342598 | 1677 | |
wolfSSL | 0:9d17e4342598 | 1678 | #endif /* HAVE_PK_CALLBACKS */ |
wolfSSL | 0:9d17e4342598 | 1679 | |
wolfSSL | 0:9d17e4342598 | 1680 | |
wolfSSL | 0:9d17e4342598 | 1681 | |
wolfSSL | 0:9d17e4342598 | 1682 | |
wolfSSL | 0:9d17e4342598 | 1683 | |
wolfSSL | 0:9d17e4342598 | 1684 | #if defined(__hpux__) || defined(__MINGW32__) |
wolfSSL | 0:9d17e4342598 | 1685 | |
wolfSSL | 0:9d17e4342598 | 1686 | /* HP/UX doesn't have strsep, needed by test/suites.c */ |
wolfSSL | 0:9d17e4342598 | 1687 | static INLINE char* strsep(char **stringp, const char *delim) |
wolfSSL | 0:9d17e4342598 | 1688 | { |
wolfSSL | 0:9d17e4342598 | 1689 | char* start; |
wolfSSL | 0:9d17e4342598 | 1690 | char* end; |
wolfSSL | 0:9d17e4342598 | 1691 | |
wolfSSL | 0:9d17e4342598 | 1692 | start = *stringp; |
wolfSSL | 0:9d17e4342598 | 1693 | if (start == NULL) |
wolfSSL | 0:9d17e4342598 | 1694 | return NULL; |
wolfSSL | 0:9d17e4342598 | 1695 | |
wolfSSL | 0:9d17e4342598 | 1696 | if ((end = strpbrk(start, delim))) { |
wolfSSL | 0:9d17e4342598 | 1697 | *end++ = '\0'; |
wolfSSL | 0:9d17e4342598 | 1698 | *stringp = end; |
wolfSSL | 0:9d17e4342598 | 1699 | } else { |
wolfSSL | 0:9d17e4342598 | 1700 | *stringp = NULL; |
wolfSSL | 0:9d17e4342598 | 1701 | } |
wolfSSL | 0:9d17e4342598 | 1702 | |
wolfSSL | 0:9d17e4342598 | 1703 | return start; |
wolfSSL | 0:9d17e4342598 | 1704 | } |
wolfSSL | 0:9d17e4342598 | 1705 | |
wolfSSL | 0:9d17e4342598 | 1706 | #endif /* __hpux__ */ |
wolfSSL | 0:9d17e4342598 | 1707 | |
wolfSSL | 0:9d17e4342598 | 1708 | #endif /* CyaSSL_TEST_H */ |
wolfSSL | 0:9d17e4342598 | 1709 |