wolfSSL SSL/TLS library, support up to TLS1.3
Dependents: CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more
src/wolfio.c@17:a5f916481144, 2020-06-05 (annotated)
- Committer:
- wolfSSL
- Date:
- Fri Jun 05 00:11:07 2020 +0000
- Revision:
- 17:a5f916481144
- Parent:
- 16:8e0d178b1d1e
wolfSSL 4.4.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 14:167253f4e170 | 1 | /* wolfio.c |
wolfSSL | 14:167253f4e170 | 2 | * |
wolfSSL | 16:8e0d178b1d1e | 3 | * Copyright (C) 2006-2020 wolfSSL Inc. |
wolfSSL | 14:167253f4e170 | 4 | * |
wolfSSL | 14:167253f4e170 | 5 | * This file is part of wolfSSL. |
wolfSSL | 14:167253f4e170 | 6 | * |
wolfSSL | 14:167253f4e170 | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 14:167253f4e170 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 14:167253f4e170 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 14:167253f4e170 | 10 | * (at your option) any later version. |
wolfSSL | 14:167253f4e170 | 11 | * |
wolfSSL | 14:167253f4e170 | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 14:167253f4e170 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 14:167253f4e170 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 14:167253f4e170 | 15 | * GNU General Public License for more details. |
wolfSSL | 14:167253f4e170 | 16 | * |
wolfSSL | 14:167253f4e170 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 14:167253f4e170 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 14:167253f4e170 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
wolfSSL | 14:167253f4e170 | 20 | */ |
wolfSSL | 14:167253f4e170 | 21 | |
wolfSSL | 14:167253f4e170 | 22 | |
wolfSSL | 14:167253f4e170 | 23 | |
wolfSSL | 14:167253f4e170 | 24 | #ifdef HAVE_CONFIG_H |
wolfSSL | 14:167253f4e170 | 25 | #include <config.h> |
wolfSSL | 14:167253f4e170 | 26 | #endif |
wolfSSL | 14:167253f4e170 | 27 | |
wolfSSL | 14:167253f4e170 | 28 | #include <wolfssl/wolfcrypt/settings.h> |
wolfSSL | 14:167253f4e170 | 29 | |
wolfSSL | 14:167253f4e170 | 30 | #ifndef WOLFCRYPT_ONLY |
wolfSSL | 14:167253f4e170 | 31 | |
wolfSSL | 14:167253f4e170 | 32 | #ifdef _WIN32_WCE |
wolfSSL | 14:167253f4e170 | 33 | /* On WinCE winsock2.h must be included before windows.h for socket stuff */ |
wolfSSL | 14:167253f4e170 | 34 | #include <winsock2.h> |
wolfSSL | 14:167253f4e170 | 35 | #endif |
wolfSSL | 14:167253f4e170 | 36 | |
wolfSSL | 14:167253f4e170 | 37 | #include <wolfssl/internal.h> |
wolfSSL | 14:167253f4e170 | 38 | #include <wolfssl/error-ssl.h> |
wolfSSL | 14:167253f4e170 | 39 | #include <wolfssl/wolfio.h> |
wolfSSL | 14:167253f4e170 | 40 | |
wolfSSL | 14:167253f4e170 | 41 | #if defined(HAVE_HTTP_CLIENT) |
wolfSSL | 16:8e0d178b1d1e | 42 | #include <stdlib.h> /* strtol() */ |
wolfSSL | 14:167253f4e170 | 43 | #endif |
wolfSSL | 14:167253f4e170 | 44 | |
wolfSSL | 14:167253f4e170 | 45 | /* |
wolfSSL | 14:167253f4e170 | 46 | Possible IO enable options: |
wolfSSL | 14:167253f4e170 | 47 | * WOLFSSL_USER_IO: Disables default Embed* callbacks and default: off |
wolfSSL | 14:167253f4e170 | 48 | allows user to define their own using |
wolfSSL | 14:167253f4e170 | 49 | wolfSSL_CTX_SetIORecv and wolfSSL_CTX_SetIOSend |
wolfSSL | 14:167253f4e170 | 50 | * USE_WOLFSSL_IO: Enables the wolfSSL IO functions default: off |
wolfSSL | 14:167253f4e170 | 51 | * HAVE_HTTP_CLIENT: Enables HTTP client API's default: off |
wolfSSL | 14:167253f4e170 | 52 | (unless HAVE_OCSP or HAVE_CRL_IO defined) |
wolfSSL | 14:167253f4e170 | 53 | * HAVE_IO_TIMEOUT: Enables support for connect timeout default: off |
wolfSSL | 14:167253f4e170 | 54 | */ |
wolfSSL | 14:167253f4e170 | 55 | |
wolfSSL | 14:167253f4e170 | 56 | |
wolfSSL | 14:167253f4e170 | 57 | /* if user writes own I/O callbacks they can define WOLFSSL_USER_IO to remove |
wolfSSL | 14:167253f4e170 | 58 | automatic setting of default I/O functions EmbedSend() and EmbedReceive() |
wolfSSL | 14:167253f4e170 | 59 | but they'll still need SetCallback xxx() at end of file |
wolfSSL | 14:167253f4e170 | 60 | */ |
wolfSSL | 14:167253f4e170 | 61 | |
wolfSSL | 14:167253f4e170 | 62 | #if defined(USE_WOLFSSL_IO) || defined(HAVE_HTTP_CLIENT) |
wolfSSL | 14:167253f4e170 | 63 | |
wolfSSL | 14:167253f4e170 | 64 | /* Translates return codes returned from |
wolfSSL | 14:167253f4e170 | 65 | * send() and recv() if need be. |
wolfSSL | 14:167253f4e170 | 66 | */ |
wolfSSL | 14:167253f4e170 | 67 | static WC_INLINE int TranslateReturnCode(int old, int sd) |
wolfSSL | 14:167253f4e170 | 68 | { |
wolfSSL | 14:167253f4e170 | 69 | (void)sd; |
wolfSSL | 14:167253f4e170 | 70 | |
wolfSSL | 14:167253f4e170 | 71 | #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
wolfSSL | 14:167253f4e170 | 72 | if (old == 0) { |
wolfSSL | 14:167253f4e170 | 73 | errno = SOCKET_EWOULDBLOCK; |
wolfSSL | 14:167253f4e170 | 74 | return -1; /* convert to BSD style wouldblock as error */ |
wolfSSL | 14:167253f4e170 | 75 | } |
wolfSSL | 14:167253f4e170 | 76 | |
wolfSSL | 14:167253f4e170 | 77 | if (old < 0) { |
wolfSSL | 14:167253f4e170 | 78 | errno = RTCS_geterror(sd); |
wolfSSL | 14:167253f4e170 | 79 | if (errno == RTCSERR_TCP_CONN_CLOSING) |
wolfSSL | 14:167253f4e170 | 80 | return 0; /* convert to BSD style closing */ |
wolfSSL | 14:167253f4e170 | 81 | if (errno == RTCSERR_TCP_CONN_RLSD) |
wolfSSL | 14:167253f4e170 | 82 | errno = SOCKET_ECONNRESET; |
wolfSSL | 14:167253f4e170 | 83 | if (errno == RTCSERR_TCP_TIMED_OUT) |
wolfSSL | 14:167253f4e170 | 84 | errno = SOCKET_EAGAIN; |
wolfSSL | 14:167253f4e170 | 85 | } |
wolfSSL | 14:167253f4e170 | 86 | #endif |
wolfSSL | 14:167253f4e170 | 87 | |
wolfSSL | 14:167253f4e170 | 88 | return old; |
wolfSSL | 14:167253f4e170 | 89 | } |
wolfSSL | 14:167253f4e170 | 90 | |
wolfSSL | 14:167253f4e170 | 91 | static WC_INLINE int wolfSSL_LastError(void) |
wolfSSL | 14:167253f4e170 | 92 | { |
wolfSSL | 14:167253f4e170 | 93 | #ifdef USE_WINDOWS_API |
wolfSSL | 14:167253f4e170 | 94 | return WSAGetLastError(); |
wolfSSL | 14:167253f4e170 | 95 | #elif defined(EBSNET) |
wolfSSL | 14:167253f4e170 | 96 | return xn_getlasterror(); |
wolfSSL | 14:167253f4e170 | 97 | #else |
wolfSSL | 14:167253f4e170 | 98 | return errno; |
wolfSSL | 14:167253f4e170 | 99 | #endif |
wolfSSL | 14:167253f4e170 | 100 | } |
wolfSSL | 14:167253f4e170 | 101 | |
wolfSSL | 14:167253f4e170 | 102 | #endif /* USE_WOLFSSL_IO || HAVE_HTTP_CLIENT */ |
wolfSSL | 14:167253f4e170 | 103 | |
wolfSSL | 14:167253f4e170 | 104 | |
wolfSSL | 14:167253f4e170 | 105 | #ifdef OPENSSL_EXTRA |
wolfSSL | 14:167253f4e170 | 106 | /* Use the WOLFSSL read BIO for receiving data. This is set by the function |
wolfSSL | 14:167253f4e170 | 107 | * wolfSSL_set_bio and can also be set by wolfSSL_CTX_SetIORecv. |
wolfSSL | 14:167253f4e170 | 108 | * |
wolfSSL | 14:167253f4e170 | 109 | * ssl WOLFSSL struct passed in that has this function set as the receive |
wolfSSL | 14:167253f4e170 | 110 | * callback. |
wolfSSL | 14:167253f4e170 | 111 | * buf buffer to fill with data read |
wolfSSL | 14:167253f4e170 | 112 | * sz size of buf buffer |
wolfSSL | 14:167253f4e170 | 113 | * ctx a user set context |
wolfSSL | 14:167253f4e170 | 114 | * |
wolfSSL | 14:167253f4e170 | 115 | * returns the amount of data read or want read. See WOLFSSL_CBIO_ERR_* values. |
wolfSSL | 14:167253f4e170 | 116 | */ |
wolfSSL | 14:167253f4e170 | 117 | int BioReceive(WOLFSSL* ssl, char* buf, int sz, void* ctx) |
wolfSSL | 14:167253f4e170 | 118 | { |
wolfSSL | 14:167253f4e170 | 119 | int recvd = WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 120 | |
wolfSSL | 14:167253f4e170 | 121 | WOLFSSL_ENTER("BioReceive"); |
wolfSSL | 14:167253f4e170 | 122 | |
wolfSSL | 14:167253f4e170 | 123 | if (ssl->biord == NULL) { |
wolfSSL | 14:167253f4e170 | 124 | WOLFSSL_MSG("WOLFSSL biord not set"); |
wolfSSL | 14:167253f4e170 | 125 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 126 | } |
wolfSSL | 14:167253f4e170 | 127 | |
wolfSSL | 16:8e0d178b1d1e | 128 | if (ssl->biord->method && ssl->biord->method->readCb) { |
wolfSSL | 16:8e0d178b1d1e | 129 | WOLFSSL_MSG("Calling custom biord"); |
wolfSSL | 16:8e0d178b1d1e | 130 | recvd = ssl->biord->method->readCb(ssl->biord, buf, sz); |
wolfSSL | 16:8e0d178b1d1e | 131 | if (recvd < 0 && recvd != WOLFSSL_CBIO_ERR_WANT_READ) |
wolfSSL | 16:8e0d178b1d1e | 132 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 16:8e0d178b1d1e | 133 | return recvd; |
wolfSSL | 16:8e0d178b1d1e | 134 | } |
wolfSSL | 16:8e0d178b1d1e | 135 | |
wolfSSL | 14:167253f4e170 | 136 | switch (ssl->biord->type) { |
wolfSSL | 14:167253f4e170 | 137 | case WOLFSSL_BIO_MEMORY: |
wolfSSL | 14:167253f4e170 | 138 | case WOLFSSL_BIO_BIO: |
wolfSSL | 14:167253f4e170 | 139 | if (wolfSSL_BIO_ctrl_pending(ssl->biord) == 0) { |
wolfSSL | 16:8e0d178b1d1e | 140 | WOLFSSL_MSG("BIO want read"); |
wolfSSL | 14:167253f4e170 | 141 | return WOLFSSL_CBIO_ERR_WANT_READ; |
wolfSSL | 14:167253f4e170 | 142 | } |
wolfSSL | 14:167253f4e170 | 143 | recvd = wolfSSL_BIO_read(ssl->biord, buf, sz); |
wolfSSL | 14:167253f4e170 | 144 | if (recvd <= 0) { |
wolfSSL | 16:8e0d178b1d1e | 145 | WOLFSSL_MSG("BIO general error"); |
wolfSSL | 14:167253f4e170 | 146 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 147 | } |
wolfSSL | 14:167253f4e170 | 148 | break; |
wolfSSL | 14:167253f4e170 | 149 | |
wolfSSL | 14:167253f4e170 | 150 | default: |
wolfSSL | 14:167253f4e170 | 151 | WOLFSSL_MSG("This BIO type is unknown / unsupported"); |
wolfSSL | 14:167253f4e170 | 152 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 153 | } |
wolfSSL | 14:167253f4e170 | 154 | |
wolfSSL | 14:167253f4e170 | 155 | (void)ctx; |
wolfSSL | 14:167253f4e170 | 156 | return recvd; |
wolfSSL | 14:167253f4e170 | 157 | } |
wolfSSL | 14:167253f4e170 | 158 | |
wolfSSL | 14:167253f4e170 | 159 | |
wolfSSL | 14:167253f4e170 | 160 | /* Use the WOLFSSL write BIO for sending data. This is set by the function |
wolfSSL | 14:167253f4e170 | 161 | * wolfSSL_set_bio and can also be set by wolfSSL_CTX_SetIOSend. |
wolfSSL | 14:167253f4e170 | 162 | * |
wolfSSL | 14:167253f4e170 | 163 | * ssl WOLFSSL struct passed in that has this function set as the send callback. |
wolfSSL | 14:167253f4e170 | 164 | * buf buffer with data to write out |
wolfSSL | 14:167253f4e170 | 165 | * sz size of buf buffer |
wolfSSL | 14:167253f4e170 | 166 | * ctx a user set context |
wolfSSL | 14:167253f4e170 | 167 | * |
wolfSSL | 14:167253f4e170 | 168 | * returns the amount of data sent or want send. See WOLFSSL_CBIO_ERR_* values. |
wolfSSL | 14:167253f4e170 | 169 | */ |
wolfSSL | 14:167253f4e170 | 170 | int BioSend(WOLFSSL* ssl, char *buf, int sz, void *ctx) |
wolfSSL | 14:167253f4e170 | 171 | { |
wolfSSL | 14:167253f4e170 | 172 | int sent = WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 173 | |
wolfSSL | 16:8e0d178b1d1e | 174 | WOLFSSL_ENTER("BioSend"); |
wolfSSL | 16:8e0d178b1d1e | 175 | |
wolfSSL | 14:167253f4e170 | 176 | if (ssl->biowr == NULL) { |
wolfSSL | 16:8e0d178b1d1e | 177 | WOLFSSL_MSG("WOLFSSL biowr not set"); |
wolfSSL | 14:167253f4e170 | 178 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 179 | } |
wolfSSL | 14:167253f4e170 | 180 | |
wolfSSL | 16:8e0d178b1d1e | 181 | if (ssl->biowr->method && ssl->biowr->method->writeCb) { |
wolfSSL | 16:8e0d178b1d1e | 182 | WOLFSSL_MSG("Calling custom biowr"); |
wolfSSL | 16:8e0d178b1d1e | 183 | sent = ssl->biowr->method->writeCb(ssl->biowr, buf, sz); |
wolfSSL | 16:8e0d178b1d1e | 184 | if (sent < 0) { |
wolfSSL | 16:8e0d178b1d1e | 185 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 16:8e0d178b1d1e | 186 | } |
wolfSSL | 16:8e0d178b1d1e | 187 | return sent; |
wolfSSL | 16:8e0d178b1d1e | 188 | } |
wolfSSL | 16:8e0d178b1d1e | 189 | |
wolfSSL | 14:167253f4e170 | 190 | switch (ssl->biowr->type) { |
wolfSSL | 14:167253f4e170 | 191 | case WOLFSSL_BIO_MEMORY: |
wolfSSL | 14:167253f4e170 | 192 | case WOLFSSL_BIO_BIO: |
wolfSSL | 14:167253f4e170 | 193 | sent = wolfSSL_BIO_write(ssl->biowr, buf, sz); |
wolfSSL | 14:167253f4e170 | 194 | if (sent < 0) { |
wolfSSL | 14:167253f4e170 | 195 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 196 | } |
wolfSSL | 14:167253f4e170 | 197 | break; |
wolfSSL | 14:167253f4e170 | 198 | |
wolfSSL | 14:167253f4e170 | 199 | default: |
wolfSSL | 14:167253f4e170 | 200 | WOLFSSL_MSG("This BIO type is unknown / unsupported"); |
wolfSSL | 14:167253f4e170 | 201 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 202 | } |
wolfSSL | 14:167253f4e170 | 203 | (void)ctx; |
wolfSSL | 14:167253f4e170 | 204 | |
wolfSSL | 14:167253f4e170 | 205 | return sent; |
wolfSSL | 14:167253f4e170 | 206 | } |
wolfSSL | 14:167253f4e170 | 207 | #endif |
wolfSSL | 14:167253f4e170 | 208 | |
wolfSSL | 14:167253f4e170 | 209 | |
wolfSSL | 14:167253f4e170 | 210 | #ifdef USE_WOLFSSL_IO |
wolfSSL | 14:167253f4e170 | 211 | |
wolfSSL | 14:167253f4e170 | 212 | /* The receive embedded callback |
wolfSSL | 14:167253f4e170 | 213 | * return : nb bytes read, or error |
wolfSSL | 14:167253f4e170 | 214 | */ |
wolfSSL | 14:167253f4e170 | 215 | int EmbedReceive(WOLFSSL *ssl, char *buf, int sz, void *ctx) |
wolfSSL | 14:167253f4e170 | 216 | { |
wolfSSL | 14:167253f4e170 | 217 | int sd = *(int*)ctx; |
wolfSSL | 14:167253f4e170 | 218 | int recvd; |
wolfSSL | 14:167253f4e170 | 219 | |
wolfSSL | 14:167253f4e170 | 220 | recvd = wolfIO_Recv(sd, buf, sz, ssl->rflags); |
wolfSSL | 14:167253f4e170 | 221 | if (recvd < 0) { |
wolfSSL | 14:167253f4e170 | 222 | int err = wolfSSL_LastError(); |
wolfSSL | 14:167253f4e170 | 223 | WOLFSSL_MSG("Embed Receive error"); |
wolfSSL | 14:167253f4e170 | 224 | |
wolfSSL | 14:167253f4e170 | 225 | if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) { |
wolfSSL | 14:167253f4e170 | 226 | WOLFSSL_MSG("\tWould block"); |
wolfSSL | 14:167253f4e170 | 227 | return WOLFSSL_CBIO_ERR_WANT_READ; |
wolfSSL | 14:167253f4e170 | 228 | } |
wolfSSL | 14:167253f4e170 | 229 | else if (err == SOCKET_ECONNRESET) { |
wolfSSL | 14:167253f4e170 | 230 | WOLFSSL_MSG("\tConnection reset"); |
wolfSSL | 14:167253f4e170 | 231 | return WOLFSSL_CBIO_ERR_CONN_RST; |
wolfSSL | 14:167253f4e170 | 232 | } |
wolfSSL | 14:167253f4e170 | 233 | else if (err == SOCKET_EINTR) { |
wolfSSL | 14:167253f4e170 | 234 | WOLFSSL_MSG("\tSocket interrupted"); |
wolfSSL | 14:167253f4e170 | 235 | return WOLFSSL_CBIO_ERR_ISR; |
wolfSSL | 14:167253f4e170 | 236 | } |
wolfSSL | 14:167253f4e170 | 237 | else if (err == SOCKET_ECONNABORTED) { |
wolfSSL | 14:167253f4e170 | 238 | WOLFSSL_MSG("\tConnection aborted"); |
wolfSSL | 14:167253f4e170 | 239 | return WOLFSSL_CBIO_ERR_CONN_CLOSE; |
wolfSSL | 14:167253f4e170 | 240 | } |
wolfSSL | 14:167253f4e170 | 241 | else { |
wolfSSL | 14:167253f4e170 | 242 | WOLFSSL_MSG("\tGeneral error"); |
wolfSSL | 14:167253f4e170 | 243 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 244 | } |
wolfSSL | 14:167253f4e170 | 245 | } |
wolfSSL | 14:167253f4e170 | 246 | else if (recvd == 0) { |
wolfSSL | 14:167253f4e170 | 247 | WOLFSSL_MSG("Embed receive connection closed"); |
wolfSSL | 14:167253f4e170 | 248 | return WOLFSSL_CBIO_ERR_CONN_CLOSE; |
wolfSSL | 14:167253f4e170 | 249 | } |
wolfSSL | 14:167253f4e170 | 250 | |
wolfSSL | 14:167253f4e170 | 251 | return recvd; |
wolfSSL | 14:167253f4e170 | 252 | } |
wolfSSL | 14:167253f4e170 | 253 | |
wolfSSL | 14:167253f4e170 | 254 | /* The send embedded callback |
wolfSSL | 14:167253f4e170 | 255 | * return : nb bytes sent, or error |
wolfSSL | 14:167253f4e170 | 256 | */ |
wolfSSL | 14:167253f4e170 | 257 | int EmbedSend(WOLFSSL* ssl, char *buf, int sz, void *ctx) |
wolfSSL | 14:167253f4e170 | 258 | { |
wolfSSL | 14:167253f4e170 | 259 | int sd = *(int*)ctx; |
wolfSSL | 14:167253f4e170 | 260 | int sent; |
wolfSSL | 14:167253f4e170 | 261 | |
wolfSSL | 16:8e0d178b1d1e | 262 | #ifdef WOLFSSL_MAX_SEND_SZ |
wolfSSL | 16:8e0d178b1d1e | 263 | if (sz > WOLFSSL_MAX_SEND_SZ) |
wolfSSL | 16:8e0d178b1d1e | 264 | sz = WOLFSSL_MAX_SEND_SZ; |
wolfSSL | 16:8e0d178b1d1e | 265 | #endif |
wolfSSL | 16:8e0d178b1d1e | 266 | |
wolfSSL | 14:167253f4e170 | 267 | sent = wolfIO_Send(sd, buf, sz, ssl->wflags); |
wolfSSL | 14:167253f4e170 | 268 | if (sent < 0) { |
wolfSSL | 14:167253f4e170 | 269 | int err = wolfSSL_LastError(); |
wolfSSL | 14:167253f4e170 | 270 | WOLFSSL_MSG("Embed Send error"); |
wolfSSL | 14:167253f4e170 | 271 | |
wolfSSL | 14:167253f4e170 | 272 | if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) { |
wolfSSL | 14:167253f4e170 | 273 | WOLFSSL_MSG("\tWould Block"); |
wolfSSL | 14:167253f4e170 | 274 | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
wolfSSL | 14:167253f4e170 | 275 | } |
wolfSSL | 14:167253f4e170 | 276 | else if (err == SOCKET_ECONNRESET) { |
wolfSSL | 14:167253f4e170 | 277 | WOLFSSL_MSG("\tConnection reset"); |
wolfSSL | 14:167253f4e170 | 278 | return WOLFSSL_CBIO_ERR_CONN_RST; |
wolfSSL | 14:167253f4e170 | 279 | } |
wolfSSL | 14:167253f4e170 | 280 | else if (err == SOCKET_EINTR) { |
wolfSSL | 14:167253f4e170 | 281 | WOLFSSL_MSG("\tSocket interrupted"); |
wolfSSL | 14:167253f4e170 | 282 | return WOLFSSL_CBIO_ERR_ISR; |
wolfSSL | 14:167253f4e170 | 283 | } |
wolfSSL | 14:167253f4e170 | 284 | else if (err == SOCKET_EPIPE) { |
wolfSSL | 14:167253f4e170 | 285 | WOLFSSL_MSG("\tSocket EPIPE"); |
wolfSSL | 14:167253f4e170 | 286 | return WOLFSSL_CBIO_ERR_CONN_CLOSE; |
wolfSSL | 14:167253f4e170 | 287 | } |
wolfSSL | 14:167253f4e170 | 288 | else { |
wolfSSL | 14:167253f4e170 | 289 | WOLFSSL_MSG("\tGeneral error"); |
wolfSSL | 14:167253f4e170 | 290 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 291 | } |
wolfSSL | 14:167253f4e170 | 292 | } |
wolfSSL | 14:167253f4e170 | 293 | |
wolfSSL | 14:167253f4e170 | 294 | return sent; |
wolfSSL | 14:167253f4e170 | 295 | } |
wolfSSL | 14:167253f4e170 | 296 | |
wolfSSL | 14:167253f4e170 | 297 | |
wolfSSL | 14:167253f4e170 | 298 | #ifdef WOLFSSL_DTLS |
wolfSSL | 14:167253f4e170 | 299 | |
wolfSSL | 14:167253f4e170 | 300 | #include <wolfssl/wolfcrypt/sha.h> |
wolfSSL | 14:167253f4e170 | 301 | |
wolfSSL | 14:167253f4e170 | 302 | #define SENDTO_FUNCTION sendto |
wolfSSL | 14:167253f4e170 | 303 | #define RECVFROM_FUNCTION recvfrom |
wolfSSL | 14:167253f4e170 | 304 | |
wolfSSL | 14:167253f4e170 | 305 | |
wolfSSL | 14:167253f4e170 | 306 | /* The receive embedded callback |
wolfSSL | 14:167253f4e170 | 307 | * return : nb bytes read, or error |
wolfSSL | 14:167253f4e170 | 308 | */ |
wolfSSL | 14:167253f4e170 | 309 | int EmbedReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx) |
wolfSSL | 14:167253f4e170 | 310 | { |
wolfSSL | 14:167253f4e170 | 311 | WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx; |
wolfSSL | 14:167253f4e170 | 312 | int recvd; |
wolfSSL | 14:167253f4e170 | 313 | int err; |
wolfSSL | 14:167253f4e170 | 314 | int sd = dtlsCtx->rfd; |
wolfSSL | 14:167253f4e170 | 315 | int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl); |
wolfSSL | 14:167253f4e170 | 316 | SOCKADDR_S peer; |
wolfSSL | 14:167253f4e170 | 317 | XSOCKLENT peerSz = sizeof(peer); |
wolfSSL | 14:167253f4e170 | 318 | |
wolfSSL | 14:167253f4e170 | 319 | WOLFSSL_ENTER("EmbedReceiveFrom()"); |
wolfSSL | 14:167253f4e170 | 320 | |
wolfSSL | 14:167253f4e170 | 321 | if (ssl->options.handShakeDone) |
wolfSSL | 14:167253f4e170 | 322 | dtls_timeout = 0; |
wolfSSL | 14:167253f4e170 | 323 | |
wolfSSL | 14:167253f4e170 | 324 | if (!wolfSSL_get_using_nonblock(ssl)) { |
wolfSSL | 14:167253f4e170 | 325 | #ifdef USE_WINDOWS_API |
wolfSSL | 14:167253f4e170 | 326 | DWORD timeout = dtls_timeout * 1000; |
wolfSSL | 14:167253f4e170 | 327 | #else |
wolfSSL | 14:167253f4e170 | 328 | struct timeval timeout; |
wolfSSL | 14:167253f4e170 | 329 | XMEMSET(&timeout, 0, sizeof(timeout)); |
wolfSSL | 14:167253f4e170 | 330 | timeout.tv_sec = dtls_timeout; |
wolfSSL | 14:167253f4e170 | 331 | #endif |
wolfSSL | 14:167253f4e170 | 332 | if (setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, |
wolfSSL | 14:167253f4e170 | 333 | sizeof(timeout)) != 0) { |
wolfSSL | 14:167253f4e170 | 334 | WOLFSSL_MSG("setsockopt rcvtimeo failed"); |
wolfSSL | 14:167253f4e170 | 335 | } |
wolfSSL | 14:167253f4e170 | 336 | } |
wolfSSL | 14:167253f4e170 | 337 | |
wolfSSL | 14:167253f4e170 | 338 | recvd = (int)RECVFROM_FUNCTION(sd, buf, sz, ssl->rflags, |
wolfSSL | 14:167253f4e170 | 339 | (SOCKADDR*)&peer, &peerSz); |
wolfSSL | 14:167253f4e170 | 340 | |
wolfSSL | 14:167253f4e170 | 341 | recvd = TranslateReturnCode(recvd, sd); |
wolfSSL | 14:167253f4e170 | 342 | |
wolfSSL | 14:167253f4e170 | 343 | if (recvd < 0) { |
wolfSSL | 14:167253f4e170 | 344 | err = wolfSSL_LastError(); |
wolfSSL | 14:167253f4e170 | 345 | WOLFSSL_MSG("Embed Receive From error"); |
wolfSSL | 14:167253f4e170 | 346 | |
wolfSSL | 14:167253f4e170 | 347 | if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) { |
wolfSSL | 14:167253f4e170 | 348 | if (wolfSSL_dtls_get_using_nonblock(ssl)) { |
wolfSSL | 14:167253f4e170 | 349 | WOLFSSL_MSG("\tWould block"); |
wolfSSL | 14:167253f4e170 | 350 | return WOLFSSL_CBIO_ERR_WANT_READ; |
wolfSSL | 14:167253f4e170 | 351 | } |
wolfSSL | 14:167253f4e170 | 352 | else { |
wolfSSL | 14:167253f4e170 | 353 | WOLFSSL_MSG("\tSocket timeout"); |
wolfSSL | 14:167253f4e170 | 354 | return WOLFSSL_CBIO_ERR_TIMEOUT; |
wolfSSL | 14:167253f4e170 | 355 | } |
wolfSSL | 14:167253f4e170 | 356 | } |
wolfSSL | 14:167253f4e170 | 357 | else if (err == SOCKET_ECONNRESET) { |
wolfSSL | 14:167253f4e170 | 358 | WOLFSSL_MSG("\tConnection reset"); |
wolfSSL | 14:167253f4e170 | 359 | return WOLFSSL_CBIO_ERR_CONN_RST; |
wolfSSL | 14:167253f4e170 | 360 | } |
wolfSSL | 14:167253f4e170 | 361 | else if (err == SOCKET_EINTR) { |
wolfSSL | 14:167253f4e170 | 362 | WOLFSSL_MSG("\tSocket interrupted"); |
wolfSSL | 14:167253f4e170 | 363 | return WOLFSSL_CBIO_ERR_ISR; |
wolfSSL | 14:167253f4e170 | 364 | } |
wolfSSL | 14:167253f4e170 | 365 | else if (err == SOCKET_ECONNREFUSED) { |
wolfSSL | 14:167253f4e170 | 366 | WOLFSSL_MSG("\tConnection refused"); |
wolfSSL | 14:167253f4e170 | 367 | return WOLFSSL_CBIO_ERR_WANT_READ; |
wolfSSL | 14:167253f4e170 | 368 | } |
wolfSSL | 14:167253f4e170 | 369 | else { |
wolfSSL | 14:167253f4e170 | 370 | WOLFSSL_MSG("\tGeneral error"); |
wolfSSL | 14:167253f4e170 | 371 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 372 | } |
wolfSSL | 14:167253f4e170 | 373 | } |
wolfSSL | 14:167253f4e170 | 374 | else { |
wolfSSL | 14:167253f4e170 | 375 | if (dtlsCtx->peer.sz > 0 |
wolfSSL | 14:167253f4e170 | 376 | && peerSz != (XSOCKLENT)dtlsCtx->peer.sz |
wolfSSL | 14:167253f4e170 | 377 | && XMEMCMP(&peer, dtlsCtx->peer.sa, peerSz) != 0) { |
wolfSSL | 14:167253f4e170 | 378 | WOLFSSL_MSG(" Ignored packet from invalid peer"); |
wolfSSL | 14:167253f4e170 | 379 | return WOLFSSL_CBIO_ERR_WANT_READ; |
wolfSSL | 14:167253f4e170 | 380 | } |
wolfSSL | 14:167253f4e170 | 381 | } |
wolfSSL | 14:167253f4e170 | 382 | |
wolfSSL | 14:167253f4e170 | 383 | return recvd; |
wolfSSL | 14:167253f4e170 | 384 | } |
wolfSSL | 14:167253f4e170 | 385 | |
wolfSSL | 14:167253f4e170 | 386 | |
wolfSSL | 14:167253f4e170 | 387 | /* The send embedded callback |
wolfSSL | 14:167253f4e170 | 388 | * return : nb bytes sent, or error |
wolfSSL | 14:167253f4e170 | 389 | */ |
wolfSSL | 14:167253f4e170 | 390 | int EmbedSendTo(WOLFSSL* ssl, char *buf, int sz, void *ctx) |
wolfSSL | 14:167253f4e170 | 391 | { |
wolfSSL | 14:167253f4e170 | 392 | WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx; |
wolfSSL | 14:167253f4e170 | 393 | int sd = dtlsCtx->wfd; |
wolfSSL | 14:167253f4e170 | 394 | int sent; |
wolfSSL | 14:167253f4e170 | 395 | int err; |
wolfSSL | 14:167253f4e170 | 396 | |
wolfSSL | 14:167253f4e170 | 397 | WOLFSSL_ENTER("EmbedSendTo()"); |
wolfSSL | 14:167253f4e170 | 398 | |
wolfSSL | 16:8e0d178b1d1e | 399 | sent = (int)SENDTO_FUNCTION(sd, buf, sz, ssl->wflags, |
wolfSSL | 14:167253f4e170 | 400 | (const SOCKADDR*)dtlsCtx->peer.sa, |
wolfSSL | 14:167253f4e170 | 401 | dtlsCtx->peer.sz); |
wolfSSL | 14:167253f4e170 | 402 | |
wolfSSL | 14:167253f4e170 | 403 | sent = TranslateReturnCode(sent, sd); |
wolfSSL | 14:167253f4e170 | 404 | |
wolfSSL | 14:167253f4e170 | 405 | if (sent < 0) { |
wolfSSL | 14:167253f4e170 | 406 | err = wolfSSL_LastError(); |
wolfSSL | 14:167253f4e170 | 407 | WOLFSSL_MSG("Embed Send To error"); |
wolfSSL | 14:167253f4e170 | 408 | |
wolfSSL | 14:167253f4e170 | 409 | if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) { |
wolfSSL | 14:167253f4e170 | 410 | WOLFSSL_MSG("\tWould Block"); |
wolfSSL | 14:167253f4e170 | 411 | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
wolfSSL | 14:167253f4e170 | 412 | } |
wolfSSL | 14:167253f4e170 | 413 | else if (err == SOCKET_ECONNRESET) { |
wolfSSL | 14:167253f4e170 | 414 | WOLFSSL_MSG("\tConnection reset"); |
wolfSSL | 14:167253f4e170 | 415 | return WOLFSSL_CBIO_ERR_CONN_RST; |
wolfSSL | 14:167253f4e170 | 416 | } |
wolfSSL | 14:167253f4e170 | 417 | else if (err == SOCKET_EINTR) { |
wolfSSL | 14:167253f4e170 | 418 | WOLFSSL_MSG("\tSocket interrupted"); |
wolfSSL | 14:167253f4e170 | 419 | return WOLFSSL_CBIO_ERR_ISR; |
wolfSSL | 14:167253f4e170 | 420 | } |
wolfSSL | 14:167253f4e170 | 421 | else if (err == SOCKET_EPIPE) { |
wolfSSL | 14:167253f4e170 | 422 | WOLFSSL_MSG("\tSocket EPIPE"); |
wolfSSL | 14:167253f4e170 | 423 | return WOLFSSL_CBIO_ERR_CONN_CLOSE; |
wolfSSL | 14:167253f4e170 | 424 | } |
wolfSSL | 14:167253f4e170 | 425 | else { |
wolfSSL | 14:167253f4e170 | 426 | WOLFSSL_MSG("\tGeneral error"); |
wolfSSL | 14:167253f4e170 | 427 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 428 | } |
wolfSSL | 14:167253f4e170 | 429 | } |
wolfSSL | 14:167253f4e170 | 430 | |
wolfSSL | 14:167253f4e170 | 431 | return sent; |
wolfSSL | 14:167253f4e170 | 432 | } |
wolfSSL | 14:167253f4e170 | 433 | |
wolfSSL | 14:167253f4e170 | 434 | |
wolfSSL | 14:167253f4e170 | 435 | #ifdef WOLFSSL_MULTICAST |
wolfSSL | 14:167253f4e170 | 436 | |
wolfSSL | 14:167253f4e170 | 437 | /* The alternate receive embedded callback for Multicast |
wolfSSL | 14:167253f4e170 | 438 | * return : nb bytes read, or error |
wolfSSL | 14:167253f4e170 | 439 | */ |
wolfSSL | 14:167253f4e170 | 440 | int EmbedReceiveFromMcast(WOLFSSL *ssl, char *buf, int sz, void *ctx) |
wolfSSL | 14:167253f4e170 | 441 | { |
wolfSSL | 14:167253f4e170 | 442 | WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx; |
wolfSSL | 14:167253f4e170 | 443 | int recvd; |
wolfSSL | 14:167253f4e170 | 444 | int err; |
wolfSSL | 14:167253f4e170 | 445 | int sd = dtlsCtx->rfd; |
wolfSSL | 14:167253f4e170 | 446 | |
wolfSSL | 14:167253f4e170 | 447 | WOLFSSL_ENTER("EmbedReceiveFromMcast()"); |
wolfSSL | 14:167253f4e170 | 448 | |
wolfSSL | 14:167253f4e170 | 449 | recvd = (int)RECVFROM_FUNCTION(sd, buf, sz, ssl->rflags, NULL, NULL); |
wolfSSL | 14:167253f4e170 | 450 | |
wolfSSL | 14:167253f4e170 | 451 | recvd = TranslateReturnCode(recvd, sd); |
wolfSSL | 14:167253f4e170 | 452 | |
wolfSSL | 14:167253f4e170 | 453 | if (recvd < 0) { |
wolfSSL | 14:167253f4e170 | 454 | err = wolfSSL_LastError(); |
wolfSSL | 14:167253f4e170 | 455 | WOLFSSL_MSG("Embed Receive From error"); |
wolfSSL | 14:167253f4e170 | 456 | |
wolfSSL | 14:167253f4e170 | 457 | if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) { |
wolfSSL | 14:167253f4e170 | 458 | if (wolfSSL_dtls_get_using_nonblock(ssl)) { |
wolfSSL | 14:167253f4e170 | 459 | WOLFSSL_MSG("\tWould block"); |
wolfSSL | 14:167253f4e170 | 460 | return WOLFSSL_CBIO_ERR_WANT_READ; |
wolfSSL | 14:167253f4e170 | 461 | } |
wolfSSL | 14:167253f4e170 | 462 | else { |
wolfSSL | 14:167253f4e170 | 463 | WOLFSSL_MSG("\tSocket timeout"); |
wolfSSL | 14:167253f4e170 | 464 | return WOLFSSL_CBIO_ERR_TIMEOUT; |
wolfSSL | 14:167253f4e170 | 465 | } |
wolfSSL | 14:167253f4e170 | 466 | } |
wolfSSL | 14:167253f4e170 | 467 | else if (err == SOCKET_ECONNRESET) { |
wolfSSL | 14:167253f4e170 | 468 | WOLFSSL_MSG("\tConnection reset"); |
wolfSSL | 14:167253f4e170 | 469 | return WOLFSSL_CBIO_ERR_CONN_RST; |
wolfSSL | 14:167253f4e170 | 470 | } |
wolfSSL | 14:167253f4e170 | 471 | else if (err == SOCKET_EINTR) { |
wolfSSL | 14:167253f4e170 | 472 | WOLFSSL_MSG("\tSocket interrupted"); |
wolfSSL | 14:167253f4e170 | 473 | return WOLFSSL_CBIO_ERR_ISR; |
wolfSSL | 14:167253f4e170 | 474 | } |
wolfSSL | 14:167253f4e170 | 475 | else if (err == SOCKET_ECONNREFUSED) { |
wolfSSL | 14:167253f4e170 | 476 | WOLFSSL_MSG("\tConnection refused"); |
wolfSSL | 14:167253f4e170 | 477 | return WOLFSSL_CBIO_ERR_WANT_READ; |
wolfSSL | 14:167253f4e170 | 478 | } |
wolfSSL | 14:167253f4e170 | 479 | else { |
wolfSSL | 14:167253f4e170 | 480 | WOLFSSL_MSG("\tGeneral error"); |
wolfSSL | 14:167253f4e170 | 481 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 482 | } |
wolfSSL | 14:167253f4e170 | 483 | } |
wolfSSL | 14:167253f4e170 | 484 | |
wolfSSL | 14:167253f4e170 | 485 | return recvd; |
wolfSSL | 14:167253f4e170 | 486 | } |
wolfSSL | 14:167253f4e170 | 487 | #endif /* WOLFSSL_MULTICAST */ |
wolfSSL | 14:167253f4e170 | 488 | |
wolfSSL | 14:167253f4e170 | 489 | |
wolfSSL | 14:167253f4e170 | 490 | /* The DTLS Generate Cookie callback |
wolfSSL | 14:167253f4e170 | 491 | * return : number of bytes copied into buf, or error |
wolfSSL | 14:167253f4e170 | 492 | */ |
wolfSSL | 14:167253f4e170 | 493 | int EmbedGenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *ctx) |
wolfSSL | 14:167253f4e170 | 494 | { |
wolfSSL | 14:167253f4e170 | 495 | int sd = ssl->wfd; |
wolfSSL | 14:167253f4e170 | 496 | SOCKADDR_S peer; |
wolfSSL | 14:167253f4e170 | 497 | XSOCKLENT peerSz = sizeof(peer); |
wolfSSL | 14:167253f4e170 | 498 | byte digest[WC_SHA256_DIGEST_SIZE]; |
wolfSSL | 14:167253f4e170 | 499 | int ret = 0; |
wolfSSL | 14:167253f4e170 | 500 | |
wolfSSL | 14:167253f4e170 | 501 | (void)ctx; |
wolfSSL | 14:167253f4e170 | 502 | |
wolfSSL | 14:167253f4e170 | 503 | XMEMSET(&peer, 0, sizeof(peer)); |
wolfSSL | 14:167253f4e170 | 504 | if (getpeername(sd, (SOCKADDR*)&peer, &peerSz) != 0) { |
wolfSSL | 14:167253f4e170 | 505 | WOLFSSL_MSG("getpeername failed in EmbedGenerateCookie"); |
wolfSSL | 14:167253f4e170 | 506 | return GEN_COOKIE_E; |
wolfSSL | 14:167253f4e170 | 507 | } |
wolfSSL | 14:167253f4e170 | 508 | |
wolfSSL | 14:167253f4e170 | 509 | ret = wc_Sha256Hash((byte*)&peer, peerSz, digest); |
wolfSSL | 14:167253f4e170 | 510 | if (ret != 0) |
wolfSSL | 14:167253f4e170 | 511 | return ret; |
wolfSSL | 14:167253f4e170 | 512 | |
wolfSSL | 14:167253f4e170 | 513 | if (sz > WC_SHA256_DIGEST_SIZE) |
wolfSSL | 14:167253f4e170 | 514 | sz = WC_SHA256_DIGEST_SIZE; |
wolfSSL | 14:167253f4e170 | 515 | XMEMCPY(buf, digest, sz); |
wolfSSL | 14:167253f4e170 | 516 | |
wolfSSL | 14:167253f4e170 | 517 | return sz; |
wolfSSL | 14:167253f4e170 | 518 | } |
wolfSSL | 14:167253f4e170 | 519 | |
wolfSSL | 14:167253f4e170 | 520 | #ifdef WOLFSSL_SESSION_EXPORT |
wolfSSL | 14:167253f4e170 | 521 | |
wolfSSL | 14:167253f4e170 | 522 | /* get the peer information in human readable form (ip, port, family) |
wolfSSL | 14:167253f4e170 | 523 | * default function assumes BSD sockets |
wolfSSL | 16:8e0d178b1d1e | 524 | * can be overridden with wolfSSL_CTX_SetIOGetPeer |
wolfSSL | 14:167253f4e170 | 525 | */ |
wolfSSL | 14:167253f4e170 | 526 | int EmbedGetPeer(WOLFSSL* ssl, char* ip, int* ipSz, |
wolfSSL | 14:167253f4e170 | 527 | unsigned short* port, int* fam) |
wolfSSL | 14:167253f4e170 | 528 | { |
wolfSSL | 14:167253f4e170 | 529 | SOCKADDR_S peer; |
wolfSSL | 14:167253f4e170 | 530 | word32 peerSz; |
wolfSSL | 14:167253f4e170 | 531 | int ret; |
wolfSSL | 14:167253f4e170 | 532 | |
wolfSSL | 14:167253f4e170 | 533 | if (ssl == NULL || ip == NULL || ipSz == NULL || |
wolfSSL | 14:167253f4e170 | 534 | port == NULL || fam == NULL) { |
wolfSSL | 14:167253f4e170 | 535 | return BAD_FUNC_ARG; |
wolfSSL | 14:167253f4e170 | 536 | } |
wolfSSL | 14:167253f4e170 | 537 | |
wolfSSL | 14:167253f4e170 | 538 | /* get peer information stored in ssl struct */ |
wolfSSL | 14:167253f4e170 | 539 | peerSz = sizeof(SOCKADDR_S); |
wolfSSL | 14:167253f4e170 | 540 | if ((ret = wolfSSL_dtls_get_peer(ssl, (void*)&peer, &peerSz)) |
wolfSSL | 14:167253f4e170 | 541 | != WOLFSSL_SUCCESS) { |
wolfSSL | 14:167253f4e170 | 542 | return ret; |
wolfSSL | 14:167253f4e170 | 543 | } |
wolfSSL | 14:167253f4e170 | 544 | |
wolfSSL | 14:167253f4e170 | 545 | /* extract family, ip, and port */ |
wolfSSL | 14:167253f4e170 | 546 | *fam = ((SOCKADDR_S*)&peer)->ss_family; |
wolfSSL | 14:167253f4e170 | 547 | switch (*fam) { |
wolfSSL | 14:167253f4e170 | 548 | case WOLFSSL_IP4: |
wolfSSL | 14:167253f4e170 | 549 | if (XINET_NTOP(*fam, &(((SOCKADDR_IN*)&peer)->sin_addr), |
wolfSSL | 14:167253f4e170 | 550 | ip, *ipSz) == NULL) { |
wolfSSL | 14:167253f4e170 | 551 | WOLFSSL_MSG("XINET_NTOP error"); |
wolfSSL | 14:167253f4e170 | 552 | return SOCKET_ERROR_E; |
wolfSSL | 14:167253f4e170 | 553 | } |
wolfSSL | 14:167253f4e170 | 554 | *port = XNTOHS(((SOCKADDR_IN*)&peer)->sin_port); |
wolfSSL | 14:167253f4e170 | 555 | break; |
wolfSSL | 14:167253f4e170 | 556 | |
wolfSSL | 14:167253f4e170 | 557 | case WOLFSSL_IP6: |
wolfSSL | 14:167253f4e170 | 558 | #ifdef WOLFSSL_IPV6 |
wolfSSL | 14:167253f4e170 | 559 | if (XINET_NTOP(*fam, &(((SOCKADDR_IN6*)&peer)->sin6_addr), |
wolfSSL | 14:167253f4e170 | 560 | ip, *ipSz) == NULL) { |
wolfSSL | 14:167253f4e170 | 561 | WOLFSSL_MSG("XINET_NTOP error"); |
wolfSSL | 14:167253f4e170 | 562 | return SOCKET_ERROR_E; |
wolfSSL | 14:167253f4e170 | 563 | } |
wolfSSL | 14:167253f4e170 | 564 | *port = XNTOHS(((SOCKADDR_IN6*)&peer)->sin6_port); |
wolfSSL | 14:167253f4e170 | 565 | #endif /* WOLFSSL_IPV6 */ |
wolfSSL | 14:167253f4e170 | 566 | break; |
wolfSSL | 14:167253f4e170 | 567 | |
wolfSSL | 14:167253f4e170 | 568 | default: |
wolfSSL | 14:167253f4e170 | 569 | WOLFSSL_MSG("Unknown family type"); |
wolfSSL | 14:167253f4e170 | 570 | return SOCKET_ERROR_E; |
wolfSSL | 14:167253f4e170 | 571 | } |
wolfSSL | 14:167253f4e170 | 572 | ip[*ipSz - 1] = '\0'; /* make sure has terminator */ |
wolfSSL | 14:167253f4e170 | 573 | *ipSz = (word16)XSTRLEN(ip); |
wolfSSL | 14:167253f4e170 | 574 | |
wolfSSL | 14:167253f4e170 | 575 | return WOLFSSL_SUCCESS; |
wolfSSL | 14:167253f4e170 | 576 | } |
wolfSSL | 14:167253f4e170 | 577 | |
wolfSSL | 14:167253f4e170 | 578 | /* set the peer information in human readable form (ip, port, family) |
wolfSSL | 14:167253f4e170 | 579 | * default function assumes BSD sockets |
wolfSSL | 16:8e0d178b1d1e | 580 | * can be overridden with wolfSSL_CTX_SetIOSetPeer |
wolfSSL | 14:167253f4e170 | 581 | */ |
wolfSSL | 14:167253f4e170 | 582 | int EmbedSetPeer(WOLFSSL* ssl, char* ip, int ipSz, |
wolfSSL | 14:167253f4e170 | 583 | unsigned short port, int fam) |
wolfSSL | 14:167253f4e170 | 584 | { |
wolfSSL | 14:167253f4e170 | 585 | int ret; |
wolfSSL | 14:167253f4e170 | 586 | SOCKADDR_S addr; |
wolfSSL | 14:167253f4e170 | 587 | |
wolfSSL | 14:167253f4e170 | 588 | /* sanity checks on arguments */ |
wolfSSL | 14:167253f4e170 | 589 | if (ssl == NULL || ip == NULL || ipSz < 0 || ipSz > DTLS_EXPORT_IP) { |
wolfSSL | 14:167253f4e170 | 590 | return BAD_FUNC_ARG; |
wolfSSL | 14:167253f4e170 | 591 | } |
wolfSSL | 14:167253f4e170 | 592 | |
wolfSSL | 14:167253f4e170 | 593 | addr.ss_family = fam; |
wolfSSL | 14:167253f4e170 | 594 | switch (addr.ss_family) { |
wolfSSL | 14:167253f4e170 | 595 | case WOLFSSL_IP4: |
wolfSSL | 14:167253f4e170 | 596 | if (XINET_PTON(addr.ss_family, ip, |
wolfSSL | 14:167253f4e170 | 597 | &(((SOCKADDR_IN*)&addr)->sin_addr)) <= 0) { |
wolfSSL | 14:167253f4e170 | 598 | WOLFSSL_MSG("XINET_PTON error"); |
wolfSSL | 14:167253f4e170 | 599 | return SOCKET_ERROR_E; |
wolfSSL | 14:167253f4e170 | 600 | } |
wolfSSL | 14:167253f4e170 | 601 | ((SOCKADDR_IN*)&addr)->sin_port = XHTONS(port); |
wolfSSL | 14:167253f4e170 | 602 | |
wolfSSL | 14:167253f4e170 | 603 | /* peer sa is free'd in SSL_ResourceFree */ |
wolfSSL | 14:167253f4e170 | 604 | if ((ret = wolfSSL_dtls_set_peer(ssl, (SOCKADDR_IN*)&addr, |
wolfSSL | 14:167253f4e170 | 605 | sizeof(SOCKADDR_IN)))!= WOLFSSL_SUCCESS) { |
wolfSSL | 14:167253f4e170 | 606 | WOLFSSL_MSG("Import DTLS peer info error"); |
wolfSSL | 14:167253f4e170 | 607 | return ret; |
wolfSSL | 14:167253f4e170 | 608 | } |
wolfSSL | 14:167253f4e170 | 609 | break; |
wolfSSL | 14:167253f4e170 | 610 | |
wolfSSL | 14:167253f4e170 | 611 | case WOLFSSL_IP6: |
wolfSSL | 14:167253f4e170 | 612 | #ifdef WOLFSSL_IPV6 |
wolfSSL | 14:167253f4e170 | 613 | if (XINET_PTON(addr.ss_family, ip, |
wolfSSL | 14:167253f4e170 | 614 | &(((SOCKADDR_IN6*)&addr)->sin6_addr)) <= 0) { |
wolfSSL | 14:167253f4e170 | 615 | WOLFSSL_MSG("XINET_PTON error"); |
wolfSSL | 14:167253f4e170 | 616 | return SOCKET_ERROR_E; |
wolfSSL | 14:167253f4e170 | 617 | } |
wolfSSL | 14:167253f4e170 | 618 | ((SOCKADDR_IN6*)&addr)->sin6_port = XHTONS(port); |
wolfSSL | 14:167253f4e170 | 619 | |
wolfSSL | 14:167253f4e170 | 620 | /* peer sa is free'd in SSL_ResourceFree */ |
wolfSSL | 14:167253f4e170 | 621 | if ((ret = wolfSSL_dtls_set_peer(ssl, (SOCKADDR_IN6*)&addr, |
wolfSSL | 14:167253f4e170 | 622 | sizeof(SOCKADDR_IN6)))!= WOLFSSL_SUCCESS) { |
wolfSSL | 14:167253f4e170 | 623 | WOLFSSL_MSG("Import DTLS peer info error"); |
wolfSSL | 14:167253f4e170 | 624 | return ret; |
wolfSSL | 14:167253f4e170 | 625 | } |
wolfSSL | 14:167253f4e170 | 626 | #endif /* WOLFSSL_IPV6 */ |
wolfSSL | 14:167253f4e170 | 627 | break; |
wolfSSL | 14:167253f4e170 | 628 | |
wolfSSL | 14:167253f4e170 | 629 | default: |
wolfSSL | 14:167253f4e170 | 630 | WOLFSSL_MSG("Unknown address family"); |
wolfSSL | 14:167253f4e170 | 631 | return BUFFER_E; |
wolfSSL | 14:167253f4e170 | 632 | } |
wolfSSL | 14:167253f4e170 | 633 | |
wolfSSL | 14:167253f4e170 | 634 | return WOLFSSL_SUCCESS; |
wolfSSL | 14:167253f4e170 | 635 | } |
wolfSSL | 14:167253f4e170 | 636 | #endif /* WOLFSSL_SESSION_EXPORT */ |
wolfSSL | 14:167253f4e170 | 637 | #endif /* WOLFSSL_DTLS */ |
wolfSSL | 14:167253f4e170 | 638 | |
wolfSSL | 14:167253f4e170 | 639 | |
wolfSSL | 14:167253f4e170 | 640 | int wolfIO_Recv(SOCKET_T sd, char *buf, int sz, int rdFlags) |
wolfSSL | 14:167253f4e170 | 641 | { |
wolfSSL | 14:167253f4e170 | 642 | int recvd; |
wolfSSL | 14:167253f4e170 | 643 | |
wolfSSL | 14:167253f4e170 | 644 | recvd = (int)RECV_FUNCTION(sd, buf, sz, rdFlags); |
wolfSSL | 14:167253f4e170 | 645 | recvd = TranslateReturnCode(recvd, sd); |
wolfSSL | 14:167253f4e170 | 646 | |
wolfSSL | 14:167253f4e170 | 647 | return recvd; |
wolfSSL | 14:167253f4e170 | 648 | } |
wolfSSL | 14:167253f4e170 | 649 | |
wolfSSL | 14:167253f4e170 | 650 | int wolfIO_Send(SOCKET_T sd, char *buf, int sz, int wrFlags) |
wolfSSL | 14:167253f4e170 | 651 | { |
wolfSSL | 14:167253f4e170 | 652 | int sent; |
wolfSSL | 14:167253f4e170 | 653 | |
wolfSSL | 14:167253f4e170 | 654 | sent = (int)SEND_FUNCTION(sd, buf, sz, wrFlags); |
wolfSSL | 14:167253f4e170 | 655 | sent = TranslateReturnCode(sent, sd); |
wolfSSL | 14:167253f4e170 | 656 | |
wolfSSL | 14:167253f4e170 | 657 | return sent; |
wolfSSL | 14:167253f4e170 | 658 | } |
wolfSSL | 14:167253f4e170 | 659 | |
wolfSSL | 14:167253f4e170 | 660 | #endif /* USE_WOLFSSL_IO */ |
wolfSSL | 14:167253f4e170 | 661 | |
wolfSSL | 14:167253f4e170 | 662 | |
wolfSSL | 14:167253f4e170 | 663 | #ifdef HAVE_HTTP_CLIENT |
wolfSSL | 14:167253f4e170 | 664 | |
wolfSSL | 14:167253f4e170 | 665 | #ifndef HAVE_IO_TIMEOUT |
wolfSSL | 14:167253f4e170 | 666 | #define io_timeout_sec 0 |
wolfSSL | 14:167253f4e170 | 667 | #else |
wolfSSL | 14:167253f4e170 | 668 | |
wolfSSL | 14:167253f4e170 | 669 | #ifndef DEFAULT_TIMEOUT_SEC |
wolfSSL | 14:167253f4e170 | 670 | #define DEFAULT_TIMEOUT_SEC 0 /* no timeout */ |
wolfSSL | 14:167253f4e170 | 671 | #endif |
wolfSSL | 14:167253f4e170 | 672 | |
wolfSSL | 14:167253f4e170 | 673 | static int io_timeout_sec = DEFAULT_TIMEOUT_SEC; |
wolfSSL | 14:167253f4e170 | 674 | |
wolfSSL | 14:167253f4e170 | 675 | void wolfIO_SetTimeout(int to_sec) |
wolfSSL | 14:167253f4e170 | 676 | { |
wolfSSL | 14:167253f4e170 | 677 | io_timeout_sec = to_sec; |
wolfSSL | 14:167253f4e170 | 678 | } |
wolfSSL | 14:167253f4e170 | 679 | |
wolfSSL | 14:167253f4e170 | 680 | int wolfIO_SetBlockingMode(SOCKET_T sockfd, int non_blocking) |
wolfSSL | 14:167253f4e170 | 681 | { |
wolfSSL | 14:167253f4e170 | 682 | int ret = 0; |
wolfSSL | 14:167253f4e170 | 683 | |
wolfSSL | 14:167253f4e170 | 684 | #ifdef USE_WINDOWS_API |
wolfSSL | 14:167253f4e170 | 685 | unsigned long blocking = non_blocking; |
wolfSSL | 14:167253f4e170 | 686 | ret = ioctlsocket(sockfd, FIONBIO, &blocking); |
wolfSSL | 14:167253f4e170 | 687 | if (ret == SOCKET_ERROR) |
wolfSSL | 14:167253f4e170 | 688 | ret = -1; |
wolfSSL | 14:167253f4e170 | 689 | #else |
wolfSSL | 14:167253f4e170 | 690 | ret = fcntl(sockfd, F_GETFL, 0); |
wolfSSL | 14:167253f4e170 | 691 | if (ret >= 0) { |
wolfSSL | 14:167253f4e170 | 692 | if (non_blocking) |
wolfSSL | 14:167253f4e170 | 693 | ret |= O_NONBLOCK; |
wolfSSL | 14:167253f4e170 | 694 | else |
wolfSSL | 14:167253f4e170 | 695 | ret &= ~O_NONBLOCK; |
wolfSSL | 14:167253f4e170 | 696 | ret = fcntl(sockfd, F_SETFL, ret); |
wolfSSL | 14:167253f4e170 | 697 | } |
wolfSSL | 14:167253f4e170 | 698 | #endif |
wolfSSL | 14:167253f4e170 | 699 | if (ret < 0) { |
wolfSSL | 14:167253f4e170 | 700 | WOLFSSL_MSG("wolfIO_SetBlockingMode failed"); |
wolfSSL | 14:167253f4e170 | 701 | } |
wolfSSL | 14:167253f4e170 | 702 | |
wolfSSL | 14:167253f4e170 | 703 | return ret; |
wolfSSL | 14:167253f4e170 | 704 | } |
wolfSSL | 14:167253f4e170 | 705 | |
wolfSSL | 14:167253f4e170 | 706 | int wolfIO_Select(SOCKET_T sockfd, int to_sec) |
wolfSSL | 14:167253f4e170 | 707 | { |
wolfSSL | 14:167253f4e170 | 708 | fd_set rfds, wfds; |
wolfSSL | 14:167253f4e170 | 709 | int nfds = 0; |
wolfSSL | 14:167253f4e170 | 710 | struct timeval timeout = { (to_sec > 0) ? to_sec : 0, 0}; |
wolfSSL | 14:167253f4e170 | 711 | int ret; |
wolfSSL | 14:167253f4e170 | 712 | |
wolfSSL | 14:167253f4e170 | 713 | #ifndef USE_WINDOWS_API |
wolfSSL | 14:167253f4e170 | 714 | nfds = (int)sockfd + 1; |
wolfSSL | 14:167253f4e170 | 715 | #endif |
wolfSSL | 14:167253f4e170 | 716 | |
wolfSSL | 14:167253f4e170 | 717 | FD_ZERO(&rfds); |
wolfSSL | 14:167253f4e170 | 718 | FD_SET(sockfd, &rfds); |
wolfSSL | 14:167253f4e170 | 719 | wfds = rfds; |
wolfSSL | 14:167253f4e170 | 720 | |
wolfSSL | 14:167253f4e170 | 721 | ret = select(nfds, &rfds, &wfds, NULL, &timeout); |
wolfSSL | 14:167253f4e170 | 722 | if (ret == 0) { |
wolfSSL | 14:167253f4e170 | 723 | #ifdef DEBUG_HTTP |
wolfSSL | 14:167253f4e170 | 724 | printf("Timeout: %d\n", ret); |
wolfSSL | 14:167253f4e170 | 725 | #endif |
wolfSSL | 14:167253f4e170 | 726 | return HTTP_TIMEOUT; |
wolfSSL | 14:167253f4e170 | 727 | } |
wolfSSL | 14:167253f4e170 | 728 | else if (ret > 0) { |
wolfSSL | 14:167253f4e170 | 729 | if (FD_ISSET(sockfd, &wfds)) { |
wolfSSL | 14:167253f4e170 | 730 | if (!FD_ISSET(sockfd, &rfds)) { |
wolfSSL | 14:167253f4e170 | 731 | return 0; |
wolfSSL | 14:167253f4e170 | 732 | } |
wolfSSL | 14:167253f4e170 | 733 | } |
wolfSSL | 14:167253f4e170 | 734 | } |
wolfSSL | 14:167253f4e170 | 735 | return SOCKET_ERROR_E; |
wolfSSL | 14:167253f4e170 | 736 | } |
wolfSSL | 14:167253f4e170 | 737 | #endif /* HAVE_IO_TIMEOUT */ |
wolfSSL | 14:167253f4e170 | 738 | |
wolfSSL | 14:167253f4e170 | 739 | static int wolfIO_Word16ToString(char* d, word16 number) |
wolfSSL | 14:167253f4e170 | 740 | { |
wolfSSL | 14:167253f4e170 | 741 | int i = 0; |
wolfSSL | 14:167253f4e170 | 742 | word16 order = 10000; |
wolfSSL | 14:167253f4e170 | 743 | word16 digit; |
wolfSSL | 14:167253f4e170 | 744 | |
wolfSSL | 14:167253f4e170 | 745 | if (d == NULL) |
wolfSSL | 14:167253f4e170 | 746 | return i; |
wolfSSL | 14:167253f4e170 | 747 | |
wolfSSL | 14:167253f4e170 | 748 | if (number == 0) |
wolfSSL | 14:167253f4e170 | 749 | d[i++] = '0'; |
wolfSSL | 14:167253f4e170 | 750 | else { |
wolfSSL | 14:167253f4e170 | 751 | while (order) { |
wolfSSL | 14:167253f4e170 | 752 | digit = number / order; |
wolfSSL | 14:167253f4e170 | 753 | if (i > 0 || digit != 0) |
wolfSSL | 14:167253f4e170 | 754 | d[i++] = (char)digit + '0'; |
wolfSSL | 14:167253f4e170 | 755 | if (digit != 0) |
wolfSSL | 14:167253f4e170 | 756 | number %= digit * order; |
wolfSSL | 14:167253f4e170 | 757 | |
wolfSSL | 14:167253f4e170 | 758 | order = (order > 1) ? order / 10 : 0; |
wolfSSL | 14:167253f4e170 | 759 | } |
wolfSSL | 14:167253f4e170 | 760 | } |
wolfSSL | 14:167253f4e170 | 761 | d[i] = 0; /* null terminate */ |
wolfSSL | 14:167253f4e170 | 762 | |
wolfSSL | 14:167253f4e170 | 763 | return i; |
wolfSSL | 14:167253f4e170 | 764 | } |
wolfSSL | 14:167253f4e170 | 765 | |
wolfSSL | 14:167253f4e170 | 766 | int wolfIO_TcpConnect(SOCKET_T* sockfd, const char* ip, word16 port, int to_sec) |
wolfSSL | 14:167253f4e170 | 767 | { |
wolfSSL | 14:167253f4e170 | 768 | #ifdef HAVE_SOCKADDR |
wolfSSL | 14:167253f4e170 | 769 | int ret = 0; |
wolfSSL | 14:167253f4e170 | 770 | SOCKADDR_S addr; |
wolfSSL | 14:167253f4e170 | 771 | int sockaddr_len = sizeof(SOCKADDR_IN); |
wolfSSL | 16:8e0d178b1d1e | 772 | /* use gethostbyname for c99 */ |
wolfSSL | 16:8e0d178b1d1e | 773 | #if defined(HAVE_GETADDRINFO) && !defined(WOLF_C99) |
wolfSSL | 14:167253f4e170 | 774 | ADDRINFO hints; |
wolfSSL | 14:167253f4e170 | 775 | ADDRINFO* answer = NULL; |
wolfSSL | 14:167253f4e170 | 776 | char strPort[6]; |
wolfSSL | 14:167253f4e170 | 777 | #else |
wolfSSL | 14:167253f4e170 | 778 | HOSTENT* entry; |
wolfSSL | 14:167253f4e170 | 779 | SOCKADDR_IN *sin; |
wolfSSL | 14:167253f4e170 | 780 | #endif |
wolfSSL | 14:167253f4e170 | 781 | |
wolfSSL | 14:167253f4e170 | 782 | XMEMSET(&addr, 0, sizeof(addr)); |
wolfSSL | 14:167253f4e170 | 783 | |
wolfSSL | 14:167253f4e170 | 784 | #ifdef WOLFIO_DEBUG |
wolfSSL | 14:167253f4e170 | 785 | printf("TCP Connect: %s:%d\n", ip, port); |
wolfSSL | 14:167253f4e170 | 786 | #endif |
wolfSSL | 14:167253f4e170 | 787 | |
wolfSSL | 16:8e0d178b1d1e | 788 | /* use gethostbyname for c99 */ |
wolfSSL | 16:8e0d178b1d1e | 789 | #if defined(HAVE_GETADDRINFO) && !defined(WOLF_C99) |
wolfSSL | 14:167253f4e170 | 790 | XMEMSET(&hints, 0, sizeof(hints)); |
wolfSSL | 14:167253f4e170 | 791 | hints.ai_family = AF_UNSPEC; |
wolfSSL | 14:167253f4e170 | 792 | hints.ai_socktype = SOCK_STREAM; |
wolfSSL | 14:167253f4e170 | 793 | hints.ai_protocol = IPPROTO_TCP; |
wolfSSL | 14:167253f4e170 | 794 | |
wolfSSL | 14:167253f4e170 | 795 | if (wolfIO_Word16ToString(strPort, port) == 0) { |
wolfSSL | 14:167253f4e170 | 796 | WOLFSSL_MSG("invalid port number for responder"); |
wolfSSL | 14:167253f4e170 | 797 | return -1; |
wolfSSL | 14:167253f4e170 | 798 | } |
wolfSSL | 14:167253f4e170 | 799 | |
wolfSSL | 14:167253f4e170 | 800 | if (getaddrinfo(ip, strPort, &hints, &answer) < 0 || answer == NULL) { |
wolfSSL | 14:167253f4e170 | 801 | WOLFSSL_MSG("no addr info for responder"); |
wolfSSL | 14:167253f4e170 | 802 | return -1; |
wolfSSL | 14:167253f4e170 | 803 | } |
wolfSSL | 14:167253f4e170 | 804 | |
wolfSSL | 14:167253f4e170 | 805 | sockaddr_len = answer->ai_addrlen; |
wolfSSL | 14:167253f4e170 | 806 | XMEMCPY(&addr, answer->ai_addr, sockaddr_len); |
wolfSSL | 14:167253f4e170 | 807 | freeaddrinfo(answer); |
wolfSSL | 14:167253f4e170 | 808 | #else |
wolfSSL | 14:167253f4e170 | 809 | entry = gethostbyname(ip); |
wolfSSL | 14:167253f4e170 | 810 | sin = (SOCKADDR_IN *)&addr; |
wolfSSL | 14:167253f4e170 | 811 | |
wolfSSL | 14:167253f4e170 | 812 | if (entry) { |
wolfSSL | 14:167253f4e170 | 813 | sin->sin_family = AF_INET; |
wolfSSL | 14:167253f4e170 | 814 | sin->sin_port = XHTONS(port); |
wolfSSL | 14:167253f4e170 | 815 | XMEMCPY(&sin->sin_addr.s_addr, entry->h_addr_list[0], entry->h_length); |
wolfSSL | 14:167253f4e170 | 816 | } |
wolfSSL | 14:167253f4e170 | 817 | else { |
wolfSSL | 14:167253f4e170 | 818 | WOLFSSL_MSG("no addr info for responder"); |
wolfSSL | 14:167253f4e170 | 819 | return -1; |
wolfSSL | 14:167253f4e170 | 820 | } |
wolfSSL | 14:167253f4e170 | 821 | #endif |
wolfSSL | 14:167253f4e170 | 822 | |
wolfSSL | 14:167253f4e170 | 823 | *sockfd = (SOCKET_T)socket(addr.ss_family, SOCK_STREAM, 0); |
wolfSSL | 14:167253f4e170 | 824 | |
wolfSSL | 14:167253f4e170 | 825 | #ifdef USE_WINDOWS_API |
wolfSSL | 14:167253f4e170 | 826 | if (*sockfd == INVALID_SOCKET) { |
wolfSSL | 14:167253f4e170 | 827 | WOLFSSL_MSG("bad socket fd, out of fds?"); |
wolfSSL | 14:167253f4e170 | 828 | return -1; |
wolfSSL | 14:167253f4e170 | 829 | } |
wolfSSL | 14:167253f4e170 | 830 | #else |
wolfSSL | 14:167253f4e170 | 831 | if (*sockfd < 0) { |
wolfSSL | 14:167253f4e170 | 832 | WOLFSSL_MSG("bad socket fd, out of fds?"); |
wolfSSL | 14:167253f4e170 | 833 | return -1; |
wolfSSL | 14:167253f4e170 | 834 | } |
wolfSSL | 14:167253f4e170 | 835 | #endif |
wolfSSL | 14:167253f4e170 | 836 | |
wolfSSL | 14:167253f4e170 | 837 | #ifdef HAVE_IO_TIMEOUT |
wolfSSL | 14:167253f4e170 | 838 | /* if timeout value provided then set socket non-blocking */ |
wolfSSL | 14:167253f4e170 | 839 | if (to_sec > 0) { |
wolfSSL | 14:167253f4e170 | 840 | wolfIO_SetBlockingMode(*sockfd, 1); |
wolfSSL | 14:167253f4e170 | 841 | } |
wolfSSL | 14:167253f4e170 | 842 | #else |
wolfSSL | 14:167253f4e170 | 843 | (void)to_sec; |
wolfSSL | 14:167253f4e170 | 844 | #endif |
wolfSSL | 14:167253f4e170 | 845 | |
wolfSSL | 14:167253f4e170 | 846 | ret = connect(*sockfd, (SOCKADDR *)&addr, sockaddr_len); |
wolfSSL | 14:167253f4e170 | 847 | #ifdef HAVE_IO_TIMEOUT |
wolfSSL | 14:167253f4e170 | 848 | if (ret != 0) { |
wolfSSL | 14:167253f4e170 | 849 | if ((errno == EINPROGRESS) && (to_sec > 0)) { |
wolfSSL | 14:167253f4e170 | 850 | /* wait for connect to complete */ |
wolfSSL | 14:167253f4e170 | 851 | ret = wolfIO_Select(*sockfd, to_sec); |
wolfSSL | 14:167253f4e170 | 852 | |
wolfSSL | 14:167253f4e170 | 853 | /* restore blocking mode */ |
wolfSSL | 14:167253f4e170 | 854 | wolfIO_SetBlockingMode(*sockfd, 0); |
wolfSSL | 14:167253f4e170 | 855 | } |
wolfSSL | 14:167253f4e170 | 856 | } |
wolfSSL | 14:167253f4e170 | 857 | #endif |
wolfSSL | 14:167253f4e170 | 858 | if (ret != 0) { |
wolfSSL | 14:167253f4e170 | 859 | WOLFSSL_MSG("Responder tcp connect failed"); |
wolfSSL | 14:167253f4e170 | 860 | return -1; |
wolfSSL | 14:167253f4e170 | 861 | } |
wolfSSL | 14:167253f4e170 | 862 | return ret; |
wolfSSL | 14:167253f4e170 | 863 | #else |
wolfSSL | 14:167253f4e170 | 864 | (void)sockfd; |
wolfSSL | 14:167253f4e170 | 865 | (void)ip; |
wolfSSL | 14:167253f4e170 | 866 | (void)port; |
wolfSSL | 14:167253f4e170 | 867 | (void)to_sec; |
wolfSSL | 14:167253f4e170 | 868 | return -1; |
wolfSSL | 14:167253f4e170 | 869 | #endif /* HAVE_SOCKADDR */ |
wolfSSL | 14:167253f4e170 | 870 | } |
wolfSSL | 14:167253f4e170 | 871 | |
wolfSSL | 14:167253f4e170 | 872 | #ifndef HTTP_SCRATCH_BUFFER_SIZE |
wolfSSL | 14:167253f4e170 | 873 | #define HTTP_SCRATCH_BUFFER_SIZE 512 |
wolfSSL | 14:167253f4e170 | 874 | #endif |
wolfSSL | 14:167253f4e170 | 875 | #ifndef MAX_URL_ITEM_SIZE |
wolfSSL | 14:167253f4e170 | 876 | #define MAX_URL_ITEM_SIZE 80 |
wolfSSL | 14:167253f4e170 | 877 | #endif |
wolfSSL | 14:167253f4e170 | 878 | |
wolfSSL | 14:167253f4e170 | 879 | int wolfIO_DecodeUrl(const char* url, int urlSz, char* outName, char* outPath, |
wolfSSL | 14:167253f4e170 | 880 | word16* outPort) |
wolfSSL | 14:167253f4e170 | 881 | { |
wolfSSL | 14:167253f4e170 | 882 | int result = -1; |
wolfSSL | 14:167253f4e170 | 883 | |
wolfSSL | 14:167253f4e170 | 884 | if (url == NULL || urlSz == 0) { |
wolfSSL | 14:167253f4e170 | 885 | if (outName) |
wolfSSL | 14:167253f4e170 | 886 | *outName = 0; |
wolfSSL | 14:167253f4e170 | 887 | if (outPath) |
wolfSSL | 14:167253f4e170 | 888 | *outPath = 0; |
wolfSSL | 14:167253f4e170 | 889 | if (outPort) |
wolfSSL | 14:167253f4e170 | 890 | *outPort = 0; |
wolfSSL | 14:167253f4e170 | 891 | } |
wolfSSL | 14:167253f4e170 | 892 | else { |
wolfSSL | 14:167253f4e170 | 893 | int i, cur; |
wolfSSL | 14:167253f4e170 | 894 | |
wolfSSL | 14:167253f4e170 | 895 | /* need to break the url down into scheme, address, and port */ |
wolfSSL | 14:167253f4e170 | 896 | /* "http://example.com:8080/" */ |
wolfSSL | 14:167253f4e170 | 897 | /* "http://[::1]:443/" */ |
wolfSSL | 14:167253f4e170 | 898 | if (XSTRNCMP(url, "http://", 7) == 0) { |
wolfSSL | 14:167253f4e170 | 899 | cur = 7; |
wolfSSL | 14:167253f4e170 | 900 | } else cur = 0; |
wolfSSL | 14:167253f4e170 | 901 | |
wolfSSL | 14:167253f4e170 | 902 | i = 0; |
wolfSSL | 14:167253f4e170 | 903 | if (url[cur] == '[') { |
wolfSSL | 14:167253f4e170 | 904 | cur++; |
wolfSSL | 14:167253f4e170 | 905 | /* copy until ']' */ |
wolfSSL | 16:8e0d178b1d1e | 906 | while (i < MAX_URL_ITEM_SIZE-1 && cur < urlSz && url[cur] != 0 && |
wolfSSL | 16:8e0d178b1d1e | 907 | url[cur] != ']') { |
wolfSSL | 14:167253f4e170 | 908 | if (outName) |
wolfSSL | 14:167253f4e170 | 909 | outName[i] = url[cur]; |
wolfSSL | 14:167253f4e170 | 910 | i++; cur++; |
wolfSSL | 14:167253f4e170 | 911 | } |
wolfSSL | 14:167253f4e170 | 912 | cur++; /* skip ']' */ |
wolfSSL | 14:167253f4e170 | 913 | } |
wolfSSL | 14:167253f4e170 | 914 | else { |
wolfSSL | 16:8e0d178b1d1e | 915 | while (i < MAX_URL_ITEM_SIZE-1 && cur < urlSz && url[cur] != 0 && |
wolfSSL | 16:8e0d178b1d1e | 916 | url[cur] != ':' && url[cur] != '/') { |
wolfSSL | 14:167253f4e170 | 917 | if (outName) |
wolfSSL | 14:167253f4e170 | 918 | outName[i] = url[cur]; |
wolfSSL | 14:167253f4e170 | 919 | i++; cur++; |
wolfSSL | 14:167253f4e170 | 920 | } |
wolfSSL | 14:167253f4e170 | 921 | } |
wolfSSL | 14:167253f4e170 | 922 | if (outName) |
wolfSSL | 14:167253f4e170 | 923 | outName[i] = 0; |
wolfSSL | 14:167253f4e170 | 924 | /* Need to pick out the path after the domain name */ |
wolfSSL | 14:167253f4e170 | 925 | |
wolfSSL | 14:167253f4e170 | 926 | if (cur < urlSz && url[cur] == ':') { |
wolfSSL | 14:167253f4e170 | 927 | char port[6]; |
wolfSSL | 14:167253f4e170 | 928 | int j; |
wolfSSL | 14:167253f4e170 | 929 | word32 bigPort = 0; |
wolfSSL | 14:167253f4e170 | 930 | i = 0; |
wolfSSL | 14:167253f4e170 | 931 | cur++; |
wolfSSL | 16:8e0d178b1d1e | 932 | while (i < 6 && cur < urlSz && url[cur] != 0 && url[cur] != '/') { |
wolfSSL | 16:8e0d178b1d1e | 933 | port[i] = url[cur]; |
wolfSSL | 16:8e0d178b1d1e | 934 | i++; cur++; |
wolfSSL | 14:167253f4e170 | 935 | } |
wolfSSL | 14:167253f4e170 | 936 | |
wolfSSL | 14:167253f4e170 | 937 | for (j = 0; j < i; j++) { |
wolfSSL | 14:167253f4e170 | 938 | if (port[j] < '0' || port[j] > '9') return -1; |
wolfSSL | 14:167253f4e170 | 939 | bigPort = (bigPort * 10) + (port[j] - '0'); |
wolfSSL | 14:167253f4e170 | 940 | } |
wolfSSL | 14:167253f4e170 | 941 | if (outPort) |
wolfSSL | 14:167253f4e170 | 942 | *outPort = (word16)bigPort; |
wolfSSL | 14:167253f4e170 | 943 | } |
wolfSSL | 14:167253f4e170 | 944 | else if (outPort) |
wolfSSL | 14:167253f4e170 | 945 | *outPort = 80; |
wolfSSL | 14:167253f4e170 | 946 | |
wolfSSL | 14:167253f4e170 | 947 | |
wolfSSL | 14:167253f4e170 | 948 | if (cur < urlSz && url[cur] == '/') { |
wolfSSL | 14:167253f4e170 | 949 | i = 0; |
wolfSSL | 16:8e0d178b1d1e | 950 | while (i < MAX_URL_ITEM_SIZE-1 && cur < urlSz && url[cur] != 0) { |
wolfSSL | 14:167253f4e170 | 951 | if (outPath) |
wolfSSL | 14:167253f4e170 | 952 | outPath[i] = url[cur]; |
wolfSSL | 14:167253f4e170 | 953 | i++; cur++; |
wolfSSL | 14:167253f4e170 | 954 | } |
wolfSSL | 14:167253f4e170 | 955 | if (outPath) |
wolfSSL | 14:167253f4e170 | 956 | outPath[i] = 0; |
wolfSSL | 14:167253f4e170 | 957 | } |
wolfSSL | 14:167253f4e170 | 958 | else if (outPath) { |
wolfSSL | 14:167253f4e170 | 959 | outPath[0] = '/'; |
wolfSSL | 14:167253f4e170 | 960 | outPath[1] = 0; |
wolfSSL | 14:167253f4e170 | 961 | } |
wolfSSL | 14:167253f4e170 | 962 | |
wolfSSL | 14:167253f4e170 | 963 | result = 0; |
wolfSSL | 14:167253f4e170 | 964 | } |
wolfSSL | 14:167253f4e170 | 965 | |
wolfSSL | 14:167253f4e170 | 966 | return result; |
wolfSSL | 14:167253f4e170 | 967 | } |
wolfSSL | 14:167253f4e170 | 968 | |
wolfSSL | 16:8e0d178b1d1e | 969 | static int wolfIO_HttpProcessResponseBuf(int sfd, byte **recvBuf, |
wolfSSL | 16:8e0d178b1d1e | 970 | int* recvBufSz, int chunkSz, char* start, int len, int dynType, void* heap) |
wolfSSL | 14:167253f4e170 | 971 | { |
wolfSSL | 14:167253f4e170 | 972 | byte* newRecvBuf = NULL; |
wolfSSL | 14:167253f4e170 | 973 | int newRecvSz = *recvBufSz + chunkSz; |
wolfSSL | 14:167253f4e170 | 974 | int pos = 0; |
wolfSSL | 14:167253f4e170 | 975 | |
wolfSSL | 14:167253f4e170 | 976 | WOLFSSL_MSG("Processing HTTP response"); |
wolfSSL | 14:167253f4e170 | 977 | #ifdef WOLFIO_DEBUG |
wolfSSL | 14:167253f4e170 | 978 | printf("HTTP Chunk %d->%d\n", *recvBufSz, chunkSz); |
wolfSSL | 14:167253f4e170 | 979 | #endif |
wolfSSL | 14:167253f4e170 | 980 | |
wolfSSL | 16:8e0d178b1d1e | 981 | (void)heap; |
wolfSSL | 16:8e0d178b1d1e | 982 | (void)dynType; |
wolfSSL | 16:8e0d178b1d1e | 983 | |
wolfSSL | 16:8e0d178b1d1e | 984 | if (chunkSz < 0 || len < 0) { |
wolfSSL | 16:8e0d178b1d1e | 985 | WOLFSSL_MSG("wolfIO_HttpProcessResponseBuf invalid chunk or length size"); |
wolfSSL | 16:8e0d178b1d1e | 986 | return MEMORY_E; |
wolfSSL | 16:8e0d178b1d1e | 987 | } |
wolfSSL | 16:8e0d178b1d1e | 988 | |
wolfSSL | 16:8e0d178b1d1e | 989 | if (newRecvSz <= 0) { |
wolfSSL | 16:8e0d178b1d1e | 990 | WOLFSSL_MSG("wolfIO_HttpProcessResponseBuf new receive size overflow"); |
wolfSSL | 16:8e0d178b1d1e | 991 | return MEMORY_E; |
wolfSSL | 16:8e0d178b1d1e | 992 | } |
wolfSSL | 16:8e0d178b1d1e | 993 | |
wolfSSL | 14:167253f4e170 | 994 | newRecvBuf = (byte*)XMALLOC(newRecvSz, heap, dynType); |
wolfSSL | 14:167253f4e170 | 995 | if (newRecvBuf == NULL) { |
wolfSSL | 14:167253f4e170 | 996 | WOLFSSL_MSG("wolfIO_HttpProcessResponseBuf malloc failed"); |
wolfSSL | 14:167253f4e170 | 997 | return MEMORY_E; |
wolfSSL | 14:167253f4e170 | 998 | } |
wolfSSL | 14:167253f4e170 | 999 | |
wolfSSL | 14:167253f4e170 | 1000 | /* if buffer already exists, then we are growing it */ |
wolfSSL | 14:167253f4e170 | 1001 | if (*recvBuf) { |
wolfSSL | 14:167253f4e170 | 1002 | XMEMCPY(&newRecvBuf[pos], *recvBuf, *recvBufSz); |
wolfSSL | 14:167253f4e170 | 1003 | XFREE(*recvBuf, heap, dynType); |
wolfSSL | 14:167253f4e170 | 1004 | pos += *recvBufSz; |
wolfSSL | 14:167253f4e170 | 1005 | *recvBuf = NULL; |
wolfSSL | 14:167253f4e170 | 1006 | } |
wolfSSL | 14:167253f4e170 | 1007 | |
wolfSSL | 14:167253f4e170 | 1008 | /* copy the remainder of the httpBuf into the respBuf */ |
wolfSSL | 14:167253f4e170 | 1009 | if (len != 0) { |
wolfSSL | 16:8e0d178b1d1e | 1010 | if (pos + len <= newRecvSz) { |
wolfSSL | 16:8e0d178b1d1e | 1011 | XMEMCPY(&newRecvBuf[pos], start, len); |
wolfSSL | 16:8e0d178b1d1e | 1012 | pos += len; |
wolfSSL | 16:8e0d178b1d1e | 1013 | } |
wolfSSL | 16:8e0d178b1d1e | 1014 | else { |
wolfSSL | 16:8e0d178b1d1e | 1015 | WOLFSSL_MSG("wolfIO_HttpProcessResponseBuf bad size"); |
wolfSSL | 16:8e0d178b1d1e | 1016 | XFREE(newRecvBuf, heap, dynType); |
wolfSSL | 16:8e0d178b1d1e | 1017 | return -1; |
wolfSSL | 16:8e0d178b1d1e | 1018 | } |
wolfSSL | 14:167253f4e170 | 1019 | } |
wolfSSL | 14:167253f4e170 | 1020 | |
wolfSSL | 14:167253f4e170 | 1021 | /* receive the remainder of chunk */ |
wolfSSL | 14:167253f4e170 | 1022 | while (len < chunkSz) { |
wolfSSL | 14:167253f4e170 | 1023 | int rxSz = wolfIO_Recv(sfd, (char*)&newRecvBuf[pos], chunkSz-len, 0); |
wolfSSL | 14:167253f4e170 | 1024 | if (rxSz > 0) { |
wolfSSL | 14:167253f4e170 | 1025 | len += rxSz; |
wolfSSL | 14:167253f4e170 | 1026 | pos += rxSz; |
wolfSSL | 14:167253f4e170 | 1027 | } |
wolfSSL | 14:167253f4e170 | 1028 | else { |
wolfSSL | 14:167253f4e170 | 1029 | WOLFSSL_MSG("wolfIO_HttpProcessResponseBuf recv failed"); |
wolfSSL | 14:167253f4e170 | 1030 | XFREE(newRecvBuf, heap, dynType); |
wolfSSL | 14:167253f4e170 | 1031 | return -1; |
wolfSSL | 14:167253f4e170 | 1032 | } |
wolfSSL | 14:167253f4e170 | 1033 | } |
wolfSSL | 14:167253f4e170 | 1034 | |
wolfSSL | 14:167253f4e170 | 1035 | *recvBuf = newRecvBuf; |
wolfSSL | 14:167253f4e170 | 1036 | *recvBufSz = newRecvSz; |
wolfSSL | 14:167253f4e170 | 1037 | |
wolfSSL | 14:167253f4e170 | 1038 | return 0; |
wolfSSL | 14:167253f4e170 | 1039 | } |
wolfSSL | 14:167253f4e170 | 1040 | |
wolfSSL | 14:167253f4e170 | 1041 | int wolfIO_HttpProcessResponse(int sfd, const char** appStrList, |
wolfSSL | 14:167253f4e170 | 1042 | byte** respBuf, byte* httpBuf, int httpBufSz, int dynType, void* heap) |
wolfSSL | 14:167253f4e170 | 1043 | { |
wolfSSL | 14:167253f4e170 | 1044 | int result = 0; |
wolfSSL | 14:167253f4e170 | 1045 | int len = 0; |
wolfSSL | 14:167253f4e170 | 1046 | char *start, *end; |
wolfSSL | 14:167253f4e170 | 1047 | int respBufSz = 0; |
wolfSSL | 14:167253f4e170 | 1048 | int isChunked = 0, chunkSz = 0; |
wolfSSL | 14:167253f4e170 | 1049 | enum phr_state { phr_init, phr_http_start, phr_have_length, phr_have_type, |
wolfSSL | 14:167253f4e170 | 1050 | phr_wait_end, phr_get_chunk_len, phr_get_chunk_data, |
wolfSSL | 14:167253f4e170 | 1051 | phr_http_end |
wolfSSL | 14:167253f4e170 | 1052 | } state = phr_init; |
wolfSSL | 14:167253f4e170 | 1053 | |
wolfSSL | 14:167253f4e170 | 1054 | *respBuf = NULL; |
wolfSSL | 14:167253f4e170 | 1055 | start = end = NULL; |
wolfSSL | 14:167253f4e170 | 1056 | do { |
wolfSSL | 14:167253f4e170 | 1057 | if (state == phr_get_chunk_data) { |
wolfSSL | 14:167253f4e170 | 1058 | /* get chunk of data */ |
wolfSSL | 14:167253f4e170 | 1059 | result = wolfIO_HttpProcessResponseBuf(sfd, respBuf, &respBufSz, |
wolfSSL | 14:167253f4e170 | 1060 | chunkSz, start, len, dynType, heap); |
wolfSSL | 14:167253f4e170 | 1061 | |
wolfSSL | 14:167253f4e170 | 1062 | state = (result != 0) ? phr_http_end : phr_get_chunk_len; |
wolfSSL | 14:167253f4e170 | 1063 | end = NULL; |
wolfSSL | 14:167253f4e170 | 1064 | len = 0; |
wolfSSL | 14:167253f4e170 | 1065 | } |
wolfSSL | 14:167253f4e170 | 1066 | |
wolfSSL | 14:167253f4e170 | 1067 | /* read data if no \r\n or first time */ |
wolfSSL | 14:167253f4e170 | 1068 | if (end == NULL) { |
wolfSSL | 14:167253f4e170 | 1069 | result = wolfIO_Recv(sfd, (char*)httpBuf+len, httpBufSz-len-1, 0); |
wolfSSL | 14:167253f4e170 | 1070 | if (result > 0) { |
wolfSSL | 14:167253f4e170 | 1071 | len += result; |
wolfSSL | 14:167253f4e170 | 1072 | start = (char*)httpBuf; |
wolfSSL | 14:167253f4e170 | 1073 | start[len] = 0; |
wolfSSL | 14:167253f4e170 | 1074 | } |
wolfSSL | 14:167253f4e170 | 1075 | else { |
wolfSSL | 14:167253f4e170 | 1076 | WOLFSSL_MSG("wolfIO_HttpProcessResponse recv http from peer failed"); |
wolfSSL | 14:167253f4e170 | 1077 | return -1; |
wolfSSL | 14:167253f4e170 | 1078 | } |
wolfSSL | 14:167253f4e170 | 1079 | } |
wolfSSL | 14:167253f4e170 | 1080 | end = XSTRSTR(start, "\r\n"); /* locate end */ |
wolfSSL | 14:167253f4e170 | 1081 | |
wolfSSL | 14:167253f4e170 | 1082 | /* handle incomplete rx */ |
wolfSSL | 14:167253f4e170 | 1083 | if (end == NULL) { |
wolfSSL | 14:167253f4e170 | 1084 | if (len != 0) |
wolfSSL | 14:167253f4e170 | 1085 | XMEMMOVE(httpBuf, start, len); |
wolfSSL | 14:167253f4e170 | 1086 | start = end = NULL; |
wolfSSL | 14:167253f4e170 | 1087 | } |
wolfSSL | 14:167253f4e170 | 1088 | /* when start is "\r\n" */ |
wolfSSL | 14:167253f4e170 | 1089 | else if (end == start) { |
wolfSSL | 14:167253f4e170 | 1090 | /* if waiting for end or need chunk len */ |
wolfSSL | 14:167253f4e170 | 1091 | if (state == phr_wait_end || state == phr_get_chunk_len) { |
wolfSSL | 14:167253f4e170 | 1092 | state = (isChunked) ? phr_get_chunk_len : phr_http_end; |
wolfSSL | 14:167253f4e170 | 1093 | len -= 2; start += 2; /* skip \r\n */ |
wolfSSL | 14:167253f4e170 | 1094 | } |
wolfSSL | 14:167253f4e170 | 1095 | else { |
wolfSSL | 14:167253f4e170 | 1096 | WOLFSSL_MSG("wolfIO_HttpProcessResponse header ended early"); |
wolfSSL | 14:167253f4e170 | 1097 | return -1; |
wolfSSL | 14:167253f4e170 | 1098 | } |
wolfSSL | 14:167253f4e170 | 1099 | } |
wolfSSL | 14:167253f4e170 | 1100 | else { |
wolfSSL | 14:167253f4e170 | 1101 | *end = 0; /* null terminate */ |
wolfSSL | 14:167253f4e170 | 1102 | len -= (int)(end - start) + 2; |
wolfSSL | 14:167253f4e170 | 1103 | /* adjust len to remove the first line including the /r/n */ |
wolfSSL | 14:167253f4e170 | 1104 | |
wolfSSL | 14:167253f4e170 | 1105 | #ifdef WOLFIO_DEBUG |
wolfSSL | 14:167253f4e170 | 1106 | printf("HTTP Resp: %s\n", start); |
wolfSSL | 14:167253f4e170 | 1107 | #endif |
wolfSSL | 14:167253f4e170 | 1108 | |
wolfSSL | 14:167253f4e170 | 1109 | switch (state) { |
wolfSSL | 14:167253f4e170 | 1110 | case phr_init: |
wolfSSL | 16:8e0d178b1d1e | 1111 | if (XSTRLEN(start) < 15) { /* 15 is the length of the two |
wolfSSL | 16:8e0d178b1d1e | 1112 | constant strings we're about to |
wolfSSL | 16:8e0d178b1d1e | 1113 | compare against. */ |
wolfSSL | 16:8e0d178b1d1e | 1114 | WOLFSSL_MSG("wolfIO_HttpProcessResponse HTTP header too short."); |
wolfSSL | 16:8e0d178b1d1e | 1115 | return -1; |
wolfSSL | 16:8e0d178b1d1e | 1116 | } |
wolfSSL | 14:167253f4e170 | 1117 | if (XSTRNCASECMP(start, "HTTP/1", 6) == 0) { |
wolfSSL | 14:167253f4e170 | 1118 | start += 9; |
wolfSSL | 14:167253f4e170 | 1119 | if (XSTRNCASECMP(start, "200 OK", 6) != 0) { |
wolfSSL | 14:167253f4e170 | 1120 | WOLFSSL_MSG("wolfIO_HttpProcessResponse not OK"); |
wolfSSL | 14:167253f4e170 | 1121 | return -1; |
wolfSSL | 14:167253f4e170 | 1122 | } |
wolfSSL | 14:167253f4e170 | 1123 | state = phr_http_start; |
wolfSSL | 14:167253f4e170 | 1124 | } |
wolfSSL | 14:167253f4e170 | 1125 | break; |
wolfSSL | 14:167253f4e170 | 1126 | case phr_http_start: |
wolfSSL | 14:167253f4e170 | 1127 | case phr_have_length: |
wolfSSL | 14:167253f4e170 | 1128 | case phr_have_type: |
wolfSSL | 16:8e0d178b1d1e | 1129 | if (XSTRLEN(start) < 13) { /* 13 is the shortest of the following |
wolfSSL | 16:8e0d178b1d1e | 1130 | next lines we're checking for. */ |
wolfSSL | 16:8e0d178b1d1e | 1131 | WOLFSSL_MSG("wolfIO_HttpProcessResponse content type is too short."); |
wolfSSL | 16:8e0d178b1d1e | 1132 | return -1; |
wolfSSL | 16:8e0d178b1d1e | 1133 | } |
wolfSSL | 16:8e0d178b1d1e | 1134 | |
wolfSSL | 14:167253f4e170 | 1135 | if (XSTRNCASECMP(start, "Content-Type:", 13) == 0) { |
wolfSSL | 14:167253f4e170 | 1136 | int i; |
wolfSSL | 14:167253f4e170 | 1137 | |
wolfSSL | 14:167253f4e170 | 1138 | start += 13; |
wolfSSL | 16:8e0d178b1d1e | 1139 | while (*start == ' ') start++; |
wolfSSL | 14:167253f4e170 | 1140 | |
wolfSSL | 14:167253f4e170 | 1141 | /* try and match against appStrList */ |
wolfSSL | 14:167253f4e170 | 1142 | i = 0; |
wolfSSL | 14:167253f4e170 | 1143 | while (appStrList[i] != NULL) { |
wolfSSL | 14:167253f4e170 | 1144 | if (XSTRNCASECMP(start, appStrList[i], |
wolfSSL | 14:167253f4e170 | 1145 | XSTRLEN(appStrList[i])) == 0) { |
wolfSSL | 14:167253f4e170 | 1146 | break; |
wolfSSL | 14:167253f4e170 | 1147 | } |
wolfSSL | 14:167253f4e170 | 1148 | i++; |
wolfSSL | 14:167253f4e170 | 1149 | } |
wolfSSL | 14:167253f4e170 | 1150 | if (appStrList[i] == NULL) { |
wolfSSL | 14:167253f4e170 | 1151 | WOLFSSL_MSG("wolfIO_HttpProcessResponse appstr mismatch"); |
wolfSSL | 14:167253f4e170 | 1152 | return -1; |
wolfSSL | 14:167253f4e170 | 1153 | } |
wolfSSL | 14:167253f4e170 | 1154 | state = (state == phr_http_start) ? phr_have_type : phr_wait_end; |
wolfSSL | 14:167253f4e170 | 1155 | } |
wolfSSL | 14:167253f4e170 | 1156 | else if (XSTRNCASECMP(start, "Content-Length:", 15) == 0) { |
wolfSSL | 14:167253f4e170 | 1157 | start += 15; |
wolfSSL | 16:8e0d178b1d1e | 1158 | while (*start == ' ') start++; |
wolfSSL | 16:8e0d178b1d1e | 1159 | chunkSz = XATOI(start); |
wolfSSL | 14:167253f4e170 | 1160 | state = (state == phr_http_start) ? phr_have_length : phr_wait_end; |
wolfSSL | 14:167253f4e170 | 1161 | } |
wolfSSL | 14:167253f4e170 | 1162 | else if (XSTRNCASECMP(start, "Transfer-Encoding:", 18) == 0) { |
wolfSSL | 14:167253f4e170 | 1163 | start += 18; |
wolfSSL | 16:8e0d178b1d1e | 1164 | while (*start == ' ') start++; |
wolfSSL | 14:167253f4e170 | 1165 | if (XSTRNCASECMP(start, "chunked", 7) == 0) { |
wolfSSL | 14:167253f4e170 | 1166 | isChunked = 1; |
wolfSSL | 14:167253f4e170 | 1167 | state = (state == phr_http_start) ? phr_have_length : phr_wait_end; |
wolfSSL | 14:167253f4e170 | 1168 | } |
wolfSSL | 14:167253f4e170 | 1169 | } |
wolfSSL | 14:167253f4e170 | 1170 | break; |
wolfSSL | 14:167253f4e170 | 1171 | case phr_get_chunk_len: |
wolfSSL | 14:167253f4e170 | 1172 | chunkSz = (int)strtol(start, NULL, 16); /* hex format */ |
wolfSSL | 14:167253f4e170 | 1173 | state = (chunkSz == 0) ? phr_http_end : phr_get_chunk_data; |
wolfSSL | 14:167253f4e170 | 1174 | break; |
wolfSSL | 14:167253f4e170 | 1175 | case phr_get_chunk_data: |
wolfSSL | 14:167253f4e170 | 1176 | /* processing for chunk data done above, since \r\n isn't required */ |
wolfSSL | 14:167253f4e170 | 1177 | case phr_wait_end: |
wolfSSL | 14:167253f4e170 | 1178 | case phr_http_end: |
wolfSSL | 14:167253f4e170 | 1179 | /* do nothing */ |
wolfSSL | 14:167253f4e170 | 1180 | break; |
wolfSSL | 14:167253f4e170 | 1181 | } /* switch (state) */ |
wolfSSL | 14:167253f4e170 | 1182 | |
wolfSSL | 14:167253f4e170 | 1183 | /* skip to end plus \r\n */ |
wolfSSL | 14:167253f4e170 | 1184 | start = end + 2; |
wolfSSL | 14:167253f4e170 | 1185 | } |
wolfSSL | 14:167253f4e170 | 1186 | } while (state != phr_http_end); |
wolfSSL | 14:167253f4e170 | 1187 | |
wolfSSL | 14:167253f4e170 | 1188 | if (!isChunked) { |
wolfSSL | 14:167253f4e170 | 1189 | result = wolfIO_HttpProcessResponseBuf(sfd, respBuf, &respBufSz, chunkSz, |
wolfSSL | 14:167253f4e170 | 1190 | start, len, dynType, heap); |
wolfSSL | 14:167253f4e170 | 1191 | } |
wolfSSL | 14:167253f4e170 | 1192 | |
wolfSSL | 14:167253f4e170 | 1193 | if (result >= 0) { |
wolfSSL | 14:167253f4e170 | 1194 | result = respBufSz; |
wolfSSL | 14:167253f4e170 | 1195 | } |
wolfSSL | 14:167253f4e170 | 1196 | else { |
wolfSSL | 14:167253f4e170 | 1197 | WOLFSSL_ERROR(result); |
wolfSSL | 14:167253f4e170 | 1198 | } |
wolfSSL | 14:167253f4e170 | 1199 | |
wolfSSL | 14:167253f4e170 | 1200 | return result; |
wolfSSL | 14:167253f4e170 | 1201 | } |
wolfSSL | 16:8e0d178b1d1e | 1202 | int wolfIO_HttpBuildRequest(const char *reqType, const char *domainName, |
wolfSSL | 16:8e0d178b1d1e | 1203 | const char *path, int pathLen, int reqSz, const char *contentType, |
wolfSSL | 16:8e0d178b1d1e | 1204 | byte *buf, int bufSize) |
wolfSSL | 16:8e0d178b1d1e | 1205 | { |
wolfSSL | 16:8e0d178b1d1e | 1206 | return wolfIO_HttpBuildRequest_ex(reqType, domainName, path, pathLen, reqSz, contentType, "", buf, bufSize); |
wolfSSL | 16:8e0d178b1d1e | 1207 | } |
wolfSSL | 14:167253f4e170 | 1208 | |
wolfSSL | 16:8e0d178b1d1e | 1209 | int wolfIO_HttpBuildRequest_ex(const char *reqType, const char *domainName, |
wolfSSL | 16:8e0d178b1d1e | 1210 | const char *path, int pathLen, int reqSz, const char *contentType, |
wolfSSL | 16:8e0d178b1d1e | 1211 | const char *exHdrs, byte *buf, int bufSize) |
wolfSSL | 16:8e0d178b1d1e | 1212 | { |
wolfSSL | 16:8e0d178b1d1e | 1213 | word32 reqTypeLen, domainNameLen, reqSzStrLen, contentTypeLen, exHdrsLen, maxLen; |
wolfSSL | 14:167253f4e170 | 1214 | char reqSzStr[6]; |
wolfSSL | 14:167253f4e170 | 1215 | char* req = (char*)buf; |
wolfSSL | 14:167253f4e170 | 1216 | const char* blankStr = " "; |
wolfSSL | 14:167253f4e170 | 1217 | const char* http11Str = " HTTP/1.1"; |
wolfSSL | 14:167253f4e170 | 1218 | const char* hostStr = "\r\nHost: "; |
wolfSSL | 14:167253f4e170 | 1219 | const char* contentLenStr = "\r\nContent-Length: "; |
wolfSSL | 14:167253f4e170 | 1220 | const char* contentTypeStr = "\r\nContent-Type: "; |
wolfSSL | 16:8e0d178b1d1e | 1221 | const char* singleCrLfStr = "\r\n"; |
wolfSSL | 14:167253f4e170 | 1222 | const char* doubleCrLfStr = "\r\n\r\n"; |
wolfSSL | 14:167253f4e170 | 1223 | word32 blankStrLen, http11StrLen, hostStrLen, contentLenStrLen, |
wolfSSL | 16:8e0d178b1d1e | 1224 | contentTypeStrLen, singleCrLfStrLen, doubleCrLfStrLen; |
wolfSSL | 14:167253f4e170 | 1225 | |
wolfSSL | 14:167253f4e170 | 1226 | reqTypeLen = (word32)XSTRLEN(reqType); |
wolfSSL | 14:167253f4e170 | 1227 | domainNameLen = (word32)XSTRLEN(domainName); |
wolfSSL | 14:167253f4e170 | 1228 | reqSzStrLen = wolfIO_Word16ToString(reqSzStr, (word16)reqSz); |
wolfSSL | 14:167253f4e170 | 1229 | contentTypeLen = (word32)XSTRLEN(contentType); |
wolfSSL | 14:167253f4e170 | 1230 | |
wolfSSL | 14:167253f4e170 | 1231 | blankStrLen = (word32)XSTRLEN(blankStr); |
wolfSSL | 14:167253f4e170 | 1232 | http11StrLen = (word32)XSTRLEN(http11Str); |
wolfSSL | 14:167253f4e170 | 1233 | hostStrLen = (word32)XSTRLEN(hostStr); |
wolfSSL | 14:167253f4e170 | 1234 | contentLenStrLen = (word32)XSTRLEN(contentLenStr); |
wolfSSL | 14:167253f4e170 | 1235 | contentTypeStrLen = (word32)XSTRLEN(contentTypeStr); |
wolfSSL | 16:8e0d178b1d1e | 1236 | |
wolfSSL | 16:8e0d178b1d1e | 1237 | if(exHdrs){ |
wolfSSL | 16:8e0d178b1d1e | 1238 | singleCrLfStrLen = (word32)XSTRLEN(singleCrLfStr); |
wolfSSL | 16:8e0d178b1d1e | 1239 | exHdrsLen = (word32)XSTRLEN(exHdrs); |
wolfSSL | 16:8e0d178b1d1e | 1240 | } else { |
wolfSSL | 16:8e0d178b1d1e | 1241 | singleCrLfStrLen = 0; |
wolfSSL | 16:8e0d178b1d1e | 1242 | exHdrsLen = 0; |
wolfSSL | 16:8e0d178b1d1e | 1243 | } |
wolfSSL | 16:8e0d178b1d1e | 1244 | |
wolfSSL | 14:167253f4e170 | 1245 | doubleCrLfStrLen = (word32)XSTRLEN(doubleCrLfStr); |
wolfSSL | 14:167253f4e170 | 1246 | |
wolfSSL | 14:167253f4e170 | 1247 | /* determine max length and check it */ |
wolfSSL | 14:167253f4e170 | 1248 | maxLen = |
wolfSSL | 14:167253f4e170 | 1249 | reqTypeLen + |
wolfSSL | 14:167253f4e170 | 1250 | blankStrLen + |
wolfSSL | 14:167253f4e170 | 1251 | pathLen + |
wolfSSL | 14:167253f4e170 | 1252 | http11StrLen + |
wolfSSL | 14:167253f4e170 | 1253 | hostStrLen + |
wolfSSL | 14:167253f4e170 | 1254 | domainNameLen + |
wolfSSL | 14:167253f4e170 | 1255 | contentLenStrLen + |
wolfSSL | 14:167253f4e170 | 1256 | reqSzStrLen + |
wolfSSL | 14:167253f4e170 | 1257 | contentTypeStrLen + |
wolfSSL | 14:167253f4e170 | 1258 | contentTypeLen + |
wolfSSL | 16:8e0d178b1d1e | 1259 | singleCrLfStrLen + |
wolfSSL | 16:8e0d178b1d1e | 1260 | exHdrsLen + |
wolfSSL | 14:167253f4e170 | 1261 | doubleCrLfStrLen + |
wolfSSL | 14:167253f4e170 | 1262 | 1 /* null term */; |
wolfSSL | 14:167253f4e170 | 1263 | if (maxLen > (word32)bufSize) |
wolfSSL | 14:167253f4e170 | 1264 | return 0; |
wolfSSL | 14:167253f4e170 | 1265 | |
wolfSSL | 16:8e0d178b1d1e | 1266 | XSTRNCPY((char*)buf, reqType, bufSize); |
wolfSSL | 16:8e0d178b1d1e | 1267 | buf += reqTypeLen; bufSize -= reqTypeLen; |
wolfSSL | 16:8e0d178b1d1e | 1268 | XSTRNCPY((char*)buf, blankStr, bufSize); |
wolfSSL | 16:8e0d178b1d1e | 1269 | buf += blankStrLen; bufSize -= blankStrLen; |
wolfSSL | 16:8e0d178b1d1e | 1270 | XSTRNCPY((char*)buf, path, bufSize); |
wolfSSL | 16:8e0d178b1d1e | 1271 | buf += pathLen; bufSize -= pathLen; |
wolfSSL | 16:8e0d178b1d1e | 1272 | XSTRNCPY((char*)buf, http11Str, bufSize); |
wolfSSL | 16:8e0d178b1d1e | 1273 | buf += http11StrLen; bufSize -= http11StrLen; |
wolfSSL | 14:167253f4e170 | 1274 | if (domainNameLen > 0) { |
wolfSSL | 16:8e0d178b1d1e | 1275 | XSTRNCPY((char*)buf, hostStr, bufSize); |
wolfSSL | 16:8e0d178b1d1e | 1276 | buf += hostStrLen; bufSize -= hostStrLen; |
wolfSSL | 16:8e0d178b1d1e | 1277 | XSTRNCPY((char*)buf, domainName, bufSize); |
wolfSSL | 16:8e0d178b1d1e | 1278 | buf += domainNameLen; bufSize -= domainNameLen; |
wolfSSL | 14:167253f4e170 | 1279 | } |
wolfSSL | 14:167253f4e170 | 1280 | if (reqSz > 0 && reqSzStrLen > 0) { |
wolfSSL | 16:8e0d178b1d1e | 1281 | XSTRNCPY((char*)buf, contentLenStr, bufSize); |
wolfSSL | 16:8e0d178b1d1e | 1282 | buf += contentLenStrLen; bufSize -= contentLenStrLen; |
wolfSSL | 16:8e0d178b1d1e | 1283 | XSTRNCPY((char*)buf, reqSzStr, bufSize); |
wolfSSL | 16:8e0d178b1d1e | 1284 | buf += reqSzStrLen; bufSize -= reqSzStrLen; |
wolfSSL | 14:167253f4e170 | 1285 | } |
wolfSSL | 14:167253f4e170 | 1286 | if (contentTypeLen > 0) { |
wolfSSL | 16:8e0d178b1d1e | 1287 | XSTRNCPY((char*)buf, contentTypeStr, bufSize); |
wolfSSL | 16:8e0d178b1d1e | 1288 | buf += contentTypeStrLen; bufSize -= contentTypeStrLen; |
wolfSSL | 16:8e0d178b1d1e | 1289 | XSTRNCPY((char*)buf, contentType, bufSize); |
wolfSSL | 16:8e0d178b1d1e | 1290 | buf += contentTypeLen; bufSize -= contentTypeLen; |
wolfSSL | 14:167253f4e170 | 1291 | } |
wolfSSL | 16:8e0d178b1d1e | 1292 | if (exHdrsLen > 0) |
wolfSSL | 16:8e0d178b1d1e | 1293 | { |
wolfSSL | 16:8e0d178b1d1e | 1294 | XSTRNCPY((char *)buf, singleCrLfStr, bufSize); |
wolfSSL | 16:8e0d178b1d1e | 1295 | buf += singleCrLfStrLen; |
wolfSSL | 16:8e0d178b1d1e | 1296 | bufSize -= singleCrLfStrLen; |
wolfSSL | 16:8e0d178b1d1e | 1297 | XSTRNCPY((char *)buf, exHdrs, bufSize); |
wolfSSL | 16:8e0d178b1d1e | 1298 | buf += exHdrsLen; |
wolfSSL | 16:8e0d178b1d1e | 1299 | bufSize -= exHdrsLen; |
wolfSSL | 16:8e0d178b1d1e | 1300 | } |
wolfSSL | 16:8e0d178b1d1e | 1301 | XSTRNCPY((char*)buf, doubleCrLfStr, bufSize); |
wolfSSL | 14:167253f4e170 | 1302 | buf += doubleCrLfStrLen; |
wolfSSL | 14:167253f4e170 | 1303 | |
wolfSSL | 14:167253f4e170 | 1304 | #ifdef WOLFIO_DEBUG |
wolfSSL | 14:167253f4e170 | 1305 | printf("HTTP %s: %s", reqType, req); |
wolfSSL | 14:167253f4e170 | 1306 | #endif |
wolfSSL | 14:167253f4e170 | 1307 | |
wolfSSL | 14:167253f4e170 | 1308 | /* calculate actual length based on original and new pointer */ |
wolfSSL | 14:167253f4e170 | 1309 | return (int)((char*)buf - req); |
wolfSSL | 14:167253f4e170 | 1310 | } |
wolfSSL | 14:167253f4e170 | 1311 | |
wolfSSL | 14:167253f4e170 | 1312 | |
wolfSSL | 14:167253f4e170 | 1313 | #ifdef HAVE_OCSP |
wolfSSL | 14:167253f4e170 | 1314 | |
wolfSSL | 14:167253f4e170 | 1315 | int wolfIO_HttpBuildRequestOcsp(const char* domainName, const char* path, |
wolfSSL | 14:167253f4e170 | 1316 | int ocspReqSz, byte* buf, int bufSize) |
wolfSSL | 14:167253f4e170 | 1317 | { |
wolfSSL | 16:8e0d178b1d1e | 1318 | const char *cacheCtl = "Cache-Control: no-cache"; |
wolfSSL | 16:8e0d178b1d1e | 1319 | return wolfIO_HttpBuildRequest_ex("POST", domainName, path, (int)XSTRLEN(path), |
wolfSSL | 16:8e0d178b1d1e | 1320 | ocspReqSz, "application/ocsp-request", cacheCtl, buf, bufSize); |
wolfSSL | 14:167253f4e170 | 1321 | } |
wolfSSL | 14:167253f4e170 | 1322 | |
wolfSSL | 14:167253f4e170 | 1323 | /* return: >0 OCSP Response Size |
wolfSSL | 14:167253f4e170 | 1324 | * -1 error */ |
wolfSSL | 14:167253f4e170 | 1325 | int wolfIO_HttpProcessResponseOcsp(int sfd, byte** respBuf, |
wolfSSL | 14:167253f4e170 | 1326 | byte* httpBuf, int httpBufSz, void* heap) |
wolfSSL | 14:167253f4e170 | 1327 | { |
wolfSSL | 14:167253f4e170 | 1328 | const char* appStrList[] = { |
wolfSSL | 14:167253f4e170 | 1329 | "application/ocsp-response", |
wolfSSL | 14:167253f4e170 | 1330 | NULL |
wolfSSL | 14:167253f4e170 | 1331 | }; |
wolfSSL | 14:167253f4e170 | 1332 | |
wolfSSL | 14:167253f4e170 | 1333 | return wolfIO_HttpProcessResponse(sfd, appStrList, |
wolfSSL | 14:167253f4e170 | 1334 | respBuf, httpBuf, httpBufSz, DYNAMIC_TYPE_OCSP, heap); |
wolfSSL | 14:167253f4e170 | 1335 | } |
wolfSSL | 14:167253f4e170 | 1336 | |
wolfSSL | 14:167253f4e170 | 1337 | /* in default wolfSSL callback ctx is the heap pointer */ |
wolfSSL | 14:167253f4e170 | 1338 | int EmbedOcspLookup(void* ctx, const char* url, int urlSz, |
wolfSSL | 14:167253f4e170 | 1339 | byte* ocspReqBuf, int ocspReqSz, byte** ocspRespBuf) |
wolfSSL | 14:167253f4e170 | 1340 | { |
wolfSSL | 14:167253f4e170 | 1341 | SOCKET_T sfd = 0; |
wolfSSL | 14:167253f4e170 | 1342 | word16 port; |
wolfSSL | 14:167253f4e170 | 1343 | int ret = -1; |
wolfSSL | 14:167253f4e170 | 1344 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 14:167253f4e170 | 1345 | char* path; |
wolfSSL | 14:167253f4e170 | 1346 | char* domainName; |
wolfSSL | 14:167253f4e170 | 1347 | #else |
wolfSSL | 14:167253f4e170 | 1348 | char path[MAX_URL_ITEM_SIZE]; |
wolfSSL | 14:167253f4e170 | 1349 | char domainName[MAX_URL_ITEM_SIZE]; |
wolfSSL | 14:167253f4e170 | 1350 | #endif |
wolfSSL | 14:167253f4e170 | 1351 | |
wolfSSL | 14:167253f4e170 | 1352 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 14:167253f4e170 | 1353 | path = (char*)XMALLOC(MAX_URL_ITEM_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 14:167253f4e170 | 1354 | if (path == NULL) |
wolfSSL | 14:167253f4e170 | 1355 | return MEMORY_E; |
wolfSSL | 14:167253f4e170 | 1356 | |
wolfSSL | 16:8e0d178b1d1e | 1357 | domainName = (char*)XMALLOC(MAX_URL_ITEM_SIZE, NULL, |
wolfSSL | 16:8e0d178b1d1e | 1358 | DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 14:167253f4e170 | 1359 | if (domainName == NULL) { |
wolfSSL | 14:167253f4e170 | 1360 | XFREE(path, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 14:167253f4e170 | 1361 | return MEMORY_E; |
wolfSSL | 14:167253f4e170 | 1362 | } |
wolfSSL | 14:167253f4e170 | 1363 | #endif |
wolfSSL | 14:167253f4e170 | 1364 | |
wolfSSL | 14:167253f4e170 | 1365 | if (ocspReqBuf == NULL || ocspReqSz == 0) { |
wolfSSL | 14:167253f4e170 | 1366 | WOLFSSL_MSG("OCSP request is required for lookup"); |
wolfSSL | 14:167253f4e170 | 1367 | } |
wolfSSL | 14:167253f4e170 | 1368 | else if (ocspRespBuf == NULL) { |
wolfSSL | 14:167253f4e170 | 1369 | WOLFSSL_MSG("Cannot save OCSP response"); |
wolfSSL | 14:167253f4e170 | 1370 | } |
wolfSSL | 14:167253f4e170 | 1371 | else if (wolfIO_DecodeUrl(url, urlSz, domainName, path, &port) < 0) { |
wolfSSL | 14:167253f4e170 | 1372 | WOLFSSL_MSG("Unable to decode OCSP URL"); |
wolfSSL | 14:167253f4e170 | 1373 | } |
wolfSSL | 14:167253f4e170 | 1374 | else { |
wolfSSL | 14:167253f4e170 | 1375 | /* Note, the library uses the EmbedOcspRespFree() callback to |
wolfSSL | 14:167253f4e170 | 1376 | * free this buffer. */ |
wolfSSL | 14:167253f4e170 | 1377 | int httpBufSz = HTTP_SCRATCH_BUFFER_SIZE; |
wolfSSL | 14:167253f4e170 | 1378 | byte* httpBuf = (byte*)XMALLOC(httpBufSz, ctx, DYNAMIC_TYPE_OCSP); |
wolfSSL | 14:167253f4e170 | 1379 | |
wolfSSL | 14:167253f4e170 | 1380 | if (httpBuf == NULL) { |
wolfSSL | 14:167253f4e170 | 1381 | WOLFSSL_MSG("Unable to create OCSP response buffer"); |
wolfSSL | 14:167253f4e170 | 1382 | } |
wolfSSL | 14:167253f4e170 | 1383 | else { |
wolfSSL | 14:167253f4e170 | 1384 | httpBufSz = wolfIO_HttpBuildRequestOcsp(domainName, path, ocspReqSz, |
wolfSSL | 14:167253f4e170 | 1385 | httpBuf, httpBufSz); |
wolfSSL | 14:167253f4e170 | 1386 | |
wolfSSL | 14:167253f4e170 | 1387 | ret = wolfIO_TcpConnect(&sfd, domainName, port, io_timeout_sec); |
wolfSSL | 16:8e0d178b1d1e | 1388 | if ((ret != 0) || ((int)sfd < 0)) { |
wolfSSL | 14:167253f4e170 | 1389 | WOLFSSL_MSG("OCSP Responder connection failed"); |
wolfSSL | 14:167253f4e170 | 1390 | } |
wolfSSL | 14:167253f4e170 | 1391 | else if (wolfIO_Send(sfd, (char*)httpBuf, httpBufSz, 0) != |
wolfSSL | 14:167253f4e170 | 1392 | httpBufSz) { |
wolfSSL | 14:167253f4e170 | 1393 | WOLFSSL_MSG("OCSP http request failed"); |
wolfSSL | 14:167253f4e170 | 1394 | } |
wolfSSL | 14:167253f4e170 | 1395 | else if (wolfIO_Send(sfd, (char*)ocspReqBuf, ocspReqSz, 0) != |
wolfSSL | 14:167253f4e170 | 1396 | ocspReqSz) { |
wolfSSL | 14:167253f4e170 | 1397 | WOLFSSL_MSG("OCSP ocsp request failed"); |
wolfSSL | 14:167253f4e170 | 1398 | } |
wolfSSL | 14:167253f4e170 | 1399 | else { |
wolfSSL | 14:167253f4e170 | 1400 | ret = wolfIO_HttpProcessResponseOcsp(sfd, ocspRespBuf, httpBuf, |
wolfSSL | 14:167253f4e170 | 1401 | HTTP_SCRATCH_BUFFER_SIZE, ctx); |
wolfSSL | 14:167253f4e170 | 1402 | } |
wolfSSL | 14:167253f4e170 | 1403 | |
wolfSSL | 14:167253f4e170 | 1404 | CloseSocket(sfd); |
wolfSSL | 14:167253f4e170 | 1405 | XFREE(httpBuf, ctx, DYNAMIC_TYPE_OCSP); |
wolfSSL | 14:167253f4e170 | 1406 | } |
wolfSSL | 14:167253f4e170 | 1407 | } |
wolfSSL | 14:167253f4e170 | 1408 | |
wolfSSL | 14:167253f4e170 | 1409 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 14:167253f4e170 | 1410 | XFREE(path, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 14:167253f4e170 | 1411 | XFREE(domainName, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 14:167253f4e170 | 1412 | #endif |
wolfSSL | 14:167253f4e170 | 1413 | |
wolfSSL | 14:167253f4e170 | 1414 | return ret; |
wolfSSL | 14:167253f4e170 | 1415 | } |
wolfSSL | 14:167253f4e170 | 1416 | |
wolfSSL | 14:167253f4e170 | 1417 | /* in default callback ctx is heap hint */ |
wolfSSL | 14:167253f4e170 | 1418 | void EmbedOcspRespFree(void* ctx, byte *resp) |
wolfSSL | 14:167253f4e170 | 1419 | { |
wolfSSL | 14:167253f4e170 | 1420 | if (resp) |
wolfSSL | 14:167253f4e170 | 1421 | XFREE(resp, ctx, DYNAMIC_TYPE_OCSP); |
wolfSSL | 14:167253f4e170 | 1422 | |
wolfSSL | 14:167253f4e170 | 1423 | (void)ctx; |
wolfSSL | 14:167253f4e170 | 1424 | } |
wolfSSL | 14:167253f4e170 | 1425 | #endif /* HAVE_OCSP */ |
wolfSSL | 14:167253f4e170 | 1426 | |
wolfSSL | 14:167253f4e170 | 1427 | |
wolfSSL | 14:167253f4e170 | 1428 | #if defined(HAVE_CRL) && defined(HAVE_CRL_IO) |
wolfSSL | 14:167253f4e170 | 1429 | |
wolfSSL | 14:167253f4e170 | 1430 | int wolfIO_HttpBuildRequestCrl(const char* url, int urlSz, |
wolfSSL | 14:167253f4e170 | 1431 | const char* domainName, byte* buf, int bufSize) |
wolfSSL | 14:167253f4e170 | 1432 | { |
wolfSSL | 16:8e0d178b1d1e | 1433 | const char *cacheCtl = "Cache-Control: no-cache"; |
wolfSSL | 16:8e0d178b1d1e | 1434 | return wolfIO_HttpBuildRequest_ex("GET", domainName, url, urlSz, 0, "", |
wolfSSL | 16:8e0d178b1d1e | 1435 | cacheCtl, buf, bufSize); |
wolfSSL | 14:167253f4e170 | 1436 | } |
wolfSSL | 14:167253f4e170 | 1437 | |
wolfSSL | 14:167253f4e170 | 1438 | int wolfIO_HttpProcessResponseCrl(WOLFSSL_CRL* crl, int sfd, byte* httpBuf, |
wolfSSL | 14:167253f4e170 | 1439 | int httpBufSz) |
wolfSSL | 14:167253f4e170 | 1440 | { |
wolfSSL | 14:167253f4e170 | 1441 | int result; |
wolfSSL | 14:167253f4e170 | 1442 | byte *respBuf = NULL; |
wolfSSL | 14:167253f4e170 | 1443 | |
wolfSSL | 14:167253f4e170 | 1444 | const char* appStrList[] = { |
wolfSSL | 14:167253f4e170 | 1445 | "application/pkix-crl", |
wolfSSL | 14:167253f4e170 | 1446 | "application/x-pkcs7-crl", |
wolfSSL | 14:167253f4e170 | 1447 | NULL |
wolfSSL | 14:167253f4e170 | 1448 | }; |
wolfSSL | 14:167253f4e170 | 1449 | |
wolfSSL | 14:167253f4e170 | 1450 | result = wolfIO_HttpProcessResponse(sfd, appStrList, |
wolfSSL | 14:167253f4e170 | 1451 | &respBuf, httpBuf, httpBufSz, DYNAMIC_TYPE_CRL, crl->heap); |
wolfSSL | 14:167253f4e170 | 1452 | if (result >= 0) { |
wolfSSL | 14:167253f4e170 | 1453 | result = BufferLoadCRL(crl, respBuf, result, WOLFSSL_FILETYPE_ASN1, 0); |
wolfSSL | 14:167253f4e170 | 1454 | } |
wolfSSL | 14:167253f4e170 | 1455 | XFREE(respBuf, crl->heap, DYNAMIC_TYPE_CRL); |
wolfSSL | 14:167253f4e170 | 1456 | |
wolfSSL | 14:167253f4e170 | 1457 | return result; |
wolfSSL | 14:167253f4e170 | 1458 | } |
wolfSSL | 14:167253f4e170 | 1459 | |
wolfSSL | 14:167253f4e170 | 1460 | int EmbedCrlLookup(WOLFSSL_CRL* crl, const char* url, int urlSz) |
wolfSSL | 14:167253f4e170 | 1461 | { |
wolfSSL | 14:167253f4e170 | 1462 | SOCKET_T sfd = 0; |
wolfSSL | 14:167253f4e170 | 1463 | word16 port; |
wolfSSL | 14:167253f4e170 | 1464 | int ret = -1; |
wolfSSL | 14:167253f4e170 | 1465 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 14:167253f4e170 | 1466 | char* domainName; |
wolfSSL | 14:167253f4e170 | 1467 | #else |
wolfSSL | 14:167253f4e170 | 1468 | char domainName[MAX_URL_ITEM_SIZE]; |
wolfSSL | 14:167253f4e170 | 1469 | #endif |
wolfSSL | 14:167253f4e170 | 1470 | |
wolfSSL | 14:167253f4e170 | 1471 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 14:167253f4e170 | 1472 | domainName = (char*)XMALLOC(MAX_URL_ITEM_SIZE, crl->heap, |
wolfSSL | 14:167253f4e170 | 1473 | DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 14:167253f4e170 | 1474 | if (domainName == NULL) { |
wolfSSL | 14:167253f4e170 | 1475 | return MEMORY_E; |
wolfSSL | 14:167253f4e170 | 1476 | } |
wolfSSL | 14:167253f4e170 | 1477 | #endif |
wolfSSL | 14:167253f4e170 | 1478 | |
wolfSSL | 14:167253f4e170 | 1479 | if (wolfIO_DecodeUrl(url, urlSz, domainName, NULL, &port) < 0) { |
wolfSSL | 14:167253f4e170 | 1480 | WOLFSSL_MSG("Unable to decode CRL URL"); |
wolfSSL | 14:167253f4e170 | 1481 | } |
wolfSSL | 14:167253f4e170 | 1482 | else { |
wolfSSL | 14:167253f4e170 | 1483 | int httpBufSz = HTTP_SCRATCH_BUFFER_SIZE; |
wolfSSL | 14:167253f4e170 | 1484 | byte* httpBuf = (byte*)XMALLOC(httpBufSz, crl->heap, |
wolfSSL | 14:167253f4e170 | 1485 | DYNAMIC_TYPE_CRL); |
wolfSSL | 14:167253f4e170 | 1486 | if (httpBuf == NULL) { |
wolfSSL | 14:167253f4e170 | 1487 | WOLFSSL_MSG("Unable to create CRL response buffer"); |
wolfSSL | 14:167253f4e170 | 1488 | } |
wolfSSL | 14:167253f4e170 | 1489 | else { |
wolfSSL | 14:167253f4e170 | 1490 | httpBufSz = wolfIO_HttpBuildRequestCrl(url, urlSz, domainName, |
wolfSSL | 14:167253f4e170 | 1491 | httpBuf, httpBufSz); |
wolfSSL | 14:167253f4e170 | 1492 | |
wolfSSL | 14:167253f4e170 | 1493 | ret = wolfIO_TcpConnect(&sfd, domainName, port, io_timeout_sec); |
wolfSSL | 14:167253f4e170 | 1494 | if ((ret != 0) || (sfd < 0)) { |
wolfSSL | 14:167253f4e170 | 1495 | WOLFSSL_MSG("CRL connection failed"); |
wolfSSL | 14:167253f4e170 | 1496 | } |
wolfSSL | 14:167253f4e170 | 1497 | else if (wolfIO_Send(sfd, (char*)httpBuf, httpBufSz, 0) |
wolfSSL | 14:167253f4e170 | 1498 | != httpBufSz) { |
wolfSSL | 14:167253f4e170 | 1499 | WOLFSSL_MSG("CRL http get failed"); |
wolfSSL | 14:167253f4e170 | 1500 | } |
wolfSSL | 14:167253f4e170 | 1501 | else { |
wolfSSL | 14:167253f4e170 | 1502 | ret = wolfIO_HttpProcessResponseCrl(crl, sfd, httpBuf, |
wolfSSL | 14:167253f4e170 | 1503 | HTTP_SCRATCH_BUFFER_SIZE); |
wolfSSL | 14:167253f4e170 | 1504 | } |
wolfSSL | 14:167253f4e170 | 1505 | |
wolfSSL | 14:167253f4e170 | 1506 | CloseSocket(sfd); |
wolfSSL | 14:167253f4e170 | 1507 | XFREE(httpBuf, crl->heap, DYNAMIC_TYPE_CRL); |
wolfSSL | 14:167253f4e170 | 1508 | } |
wolfSSL | 14:167253f4e170 | 1509 | } |
wolfSSL | 14:167253f4e170 | 1510 | |
wolfSSL | 14:167253f4e170 | 1511 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 14:167253f4e170 | 1512 | XFREE(domainName, crl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 14:167253f4e170 | 1513 | #endif |
wolfSSL | 14:167253f4e170 | 1514 | |
wolfSSL | 14:167253f4e170 | 1515 | return ret; |
wolfSSL | 14:167253f4e170 | 1516 | } |
wolfSSL | 14:167253f4e170 | 1517 | #endif /* HAVE_CRL && HAVE_CRL_IO */ |
wolfSSL | 14:167253f4e170 | 1518 | |
wolfSSL | 14:167253f4e170 | 1519 | #endif /* HAVE_HTTP_CLIENT */ |
wolfSSL | 14:167253f4e170 | 1520 | |
wolfSSL | 14:167253f4e170 | 1521 | |
wolfSSL | 14:167253f4e170 | 1522 | |
wolfSSL | 16:8e0d178b1d1e | 1523 | void wolfSSL_CTX_SetIORecv(WOLFSSL_CTX *ctx, CallbackIORecv CBIORecv) |
wolfSSL | 14:167253f4e170 | 1524 | { |
wolfSSL | 16:8e0d178b1d1e | 1525 | if (ctx) { |
wolfSSL | 16:8e0d178b1d1e | 1526 | ctx->CBIORecv = CBIORecv; |
wolfSSL | 14:167253f4e170 | 1527 | #ifdef OPENSSL_EXTRA |
wolfSSL | 16:8e0d178b1d1e | 1528 | ctx->cbioFlag |= WOLFSSL_CBIO_RECV; |
wolfSSL | 14:167253f4e170 | 1529 | #endif |
wolfSSL | 16:8e0d178b1d1e | 1530 | } |
wolfSSL | 16:8e0d178b1d1e | 1531 | } |
wolfSSL | 16:8e0d178b1d1e | 1532 | |
wolfSSL | 16:8e0d178b1d1e | 1533 | |
wolfSSL | 16:8e0d178b1d1e | 1534 | void wolfSSL_CTX_SetIOSend(WOLFSSL_CTX *ctx, CallbackIOSend CBIOSend) |
wolfSSL | 16:8e0d178b1d1e | 1535 | { |
wolfSSL | 16:8e0d178b1d1e | 1536 | if (ctx) { |
wolfSSL | 16:8e0d178b1d1e | 1537 | ctx->CBIOSend = CBIOSend; |
wolfSSL | 16:8e0d178b1d1e | 1538 | #ifdef OPENSSL_EXTRA |
wolfSSL | 16:8e0d178b1d1e | 1539 | ctx->cbioFlag |= WOLFSSL_CBIO_SEND; |
wolfSSL | 16:8e0d178b1d1e | 1540 | #endif |
wolfSSL | 16:8e0d178b1d1e | 1541 | } |
wolfSSL | 14:167253f4e170 | 1542 | } |
wolfSSL | 14:167253f4e170 | 1543 | |
wolfSSL | 14:167253f4e170 | 1544 | |
wolfSSL | 16:8e0d178b1d1e | 1545 | /* sets the IO callback to use for receives at WOLFSSL level */ |
wolfSSL | 16:8e0d178b1d1e | 1546 | void wolfSSL_SSLSetIORecv(WOLFSSL *ssl, CallbackIORecv CBIORecv) |
wolfSSL | 14:167253f4e170 | 1547 | { |
wolfSSL | 16:8e0d178b1d1e | 1548 | if (ssl) { |
wolfSSL | 16:8e0d178b1d1e | 1549 | ssl->CBIORecv = CBIORecv; |
wolfSSL | 14:167253f4e170 | 1550 | #ifdef OPENSSL_EXTRA |
wolfSSL | 16:8e0d178b1d1e | 1551 | ssl->cbioFlag |= WOLFSSL_CBIO_RECV; |
wolfSSL | 14:167253f4e170 | 1552 | #endif |
wolfSSL | 16:8e0d178b1d1e | 1553 | } |
wolfSSL | 14:167253f4e170 | 1554 | } |
wolfSSL | 14:167253f4e170 | 1555 | |
wolfSSL | 14:167253f4e170 | 1556 | |
wolfSSL | 16:8e0d178b1d1e | 1557 | /* sets the IO callback to use for sends at WOLFSSL level */ |
wolfSSL | 16:8e0d178b1d1e | 1558 | void wolfSSL_SSLSetIOSend(WOLFSSL *ssl, CallbackIOSend CBIOSend) |
wolfSSL | 14:167253f4e170 | 1559 | { |
wolfSSL | 16:8e0d178b1d1e | 1560 | if (ssl) { |
wolfSSL | 16:8e0d178b1d1e | 1561 | ssl->CBIOSend = CBIOSend; |
wolfSSL | 16:8e0d178b1d1e | 1562 | #ifdef OPENSSL_EXTRA |
wolfSSL | 16:8e0d178b1d1e | 1563 | ssl->cbioFlag |= WOLFSSL_CBIO_SEND; |
wolfSSL | 16:8e0d178b1d1e | 1564 | #endif |
wolfSSL | 16:8e0d178b1d1e | 1565 | } |
wolfSSL | 14:167253f4e170 | 1566 | } |
wolfSSL | 14:167253f4e170 | 1567 | |
wolfSSL | 14:167253f4e170 | 1568 | |
wolfSSL | 16:8e0d178b1d1e | 1569 | void wolfSSL_SetIOReadCtx(WOLFSSL* ssl, void *rctx) |
wolfSSL | 14:167253f4e170 | 1570 | { |
wolfSSL | 16:8e0d178b1d1e | 1571 | if (ssl) |
wolfSSL | 16:8e0d178b1d1e | 1572 | ssl->IOCB_ReadCtx = rctx; |
wolfSSL | 14:167253f4e170 | 1573 | } |
wolfSSL | 14:167253f4e170 | 1574 | |
wolfSSL | 14:167253f4e170 | 1575 | |
wolfSSL | 16:8e0d178b1d1e | 1576 | void wolfSSL_SetIOWriteCtx(WOLFSSL* ssl, void *wctx) |
wolfSSL | 16:8e0d178b1d1e | 1577 | { |
wolfSSL | 16:8e0d178b1d1e | 1578 | if (ssl) |
wolfSSL | 16:8e0d178b1d1e | 1579 | ssl->IOCB_WriteCtx = wctx; |
wolfSSL | 16:8e0d178b1d1e | 1580 | } |
wolfSSL | 16:8e0d178b1d1e | 1581 | |
wolfSSL | 16:8e0d178b1d1e | 1582 | |
wolfSSL | 16:8e0d178b1d1e | 1583 | void* wolfSSL_GetIOReadCtx(WOLFSSL* ssl) |
wolfSSL | 14:167253f4e170 | 1584 | { |
wolfSSL | 14:167253f4e170 | 1585 | if (ssl) |
wolfSSL | 14:167253f4e170 | 1586 | return ssl->IOCB_ReadCtx; |
wolfSSL | 14:167253f4e170 | 1587 | |
wolfSSL | 14:167253f4e170 | 1588 | return NULL; |
wolfSSL | 14:167253f4e170 | 1589 | } |
wolfSSL | 14:167253f4e170 | 1590 | |
wolfSSL | 14:167253f4e170 | 1591 | |
wolfSSL | 16:8e0d178b1d1e | 1592 | void* wolfSSL_GetIOWriteCtx(WOLFSSL* ssl) |
wolfSSL | 14:167253f4e170 | 1593 | { |
wolfSSL | 14:167253f4e170 | 1594 | if (ssl) |
wolfSSL | 14:167253f4e170 | 1595 | return ssl->IOCB_WriteCtx; |
wolfSSL | 14:167253f4e170 | 1596 | |
wolfSSL | 14:167253f4e170 | 1597 | return NULL; |
wolfSSL | 14:167253f4e170 | 1598 | } |
wolfSSL | 14:167253f4e170 | 1599 | |
wolfSSL | 14:167253f4e170 | 1600 | |
wolfSSL | 16:8e0d178b1d1e | 1601 | void wolfSSL_SetIOReadFlags(WOLFSSL* ssl, int flags) |
wolfSSL | 14:167253f4e170 | 1602 | { |
wolfSSL | 16:8e0d178b1d1e | 1603 | if (ssl) |
wolfSSL | 16:8e0d178b1d1e | 1604 | ssl->rflags = flags; |
wolfSSL | 14:167253f4e170 | 1605 | } |
wolfSSL | 14:167253f4e170 | 1606 | |
wolfSSL | 14:167253f4e170 | 1607 | |
wolfSSL | 16:8e0d178b1d1e | 1608 | void wolfSSL_SetIOWriteFlags(WOLFSSL* ssl, int flags) |
wolfSSL | 14:167253f4e170 | 1609 | { |
wolfSSL | 16:8e0d178b1d1e | 1610 | if (ssl) |
wolfSSL | 16:8e0d178b1d1e | 1611 | ssl->wflags = flags; |
wolfSSL | 14:167253f4e170 | 1612 | } |
wolfSSL | 14:167253f4e170 | 1613 | |
wolfSSL | 14:167253f4e170 | 1614 | |
wolfSSL | 14:167253f4e170 | 1615 | #ifdef WOLFSSL_DTLS |
wolfSSL | 14:167253f4e170 | 1616 | |
wolfSSL | 16:8e0d178b1d1e | 1617 | void wolfSSL_CTX_SetGenCookie(WOLFSSL_CTX* ctx, CallbackGenCookie cb) |
wolfSSL | 14:167253f4e170 | 1618 | { |
wolfSSL | 16:8e0d178b1d1e | 1619 | if (ctx) |
wolfSSL | 16:8e0d178b1d1e | 1620 | ctx->CBIOCookie = cb; |
wolfSSL | 14:167253f4e170 | 1621 | } |
wolfSSL | 14:167253f4e170 | 1622 | |
wolfSSL | 14:167253f4e170 | 1623 | |
wolfSSL | 16:8e0d178b1d1e | 1624 | void wolfSSL_SetCookieCtx(WOLFSSL* ssl, void *ctx) |
wolfSSL | 14:167253f4e170 | 1625 | { |
wolfSSL | 16:8e0d178b1d1e | 1626 | if (ssl) |
wolfSSL | 16:8e0d178b1d1e | 1627 | ssl->IOCB_CookieCtx = ctx; |
wolfSSL | 14:167253f4e170 | 1628 | } |
wolfSSL | 14:167253f4e170 | 1629 | |
wolfSSL | 14:167253f4e170 | 1630 | |
wolfSSL | 16:8e0d178b1d1e | 1631 | void* wolfSSL_GetCookieCtx(WOLFSSL* ssl) |
wolfSSL | 14:167253f4e170 | 1632 | { |
wolfSSL | 14:167253f4e170 | 1633 | if (ssl) |
wolfSSL | 14:167253f4e170 | 1634 | return ssl->IOCB_CookieCtx; |
wolfSSL | 14:167253f4e170 | 1635 | |
wolfSSL | 14:167253f4e170 | 1636 | return NULL; |
wolfSSL | 14:167253f4e170 | 1637 | } |
wolfSSL | 14:167253f4e170 | 1638 | |
wolfSSL | 14:167253f4e170 | 1639 | #ifdef WOLFSSL_SESSION_EXPORT |
wolfSSL | 14:167253f4e170 | 1640 | |
wolfSSL | 16:8e0d178b1d1e | 1641 | void wolfSSL_CTX_SetIOGetPeer(WOLFSSL_CTX* ctx, CallbackGetPeer cb) |
wolfSSL | 14:167253f4e170 | 1642 | { |
wolfSSL | 16:8e0d178b1d1e | 1643 | if (ctx) |
wolfSSL | 16:8e0d178b1d1e | 1644 | ctx->CBGetPeer = cb; |
wolfSSL | 14:167253f4e170 | 1645 | } |
wolfSSL | 14:167253f4e170 | 1646 | |
wolfSSL | 14:167253f4e170 | 1647 | |
wolfSSL | 16:8e0d178b1d1e | 1648 | void wolfSSL_CTX_SetIOSetPeer(WOLFSSL_CTX* ctx, CallbackSetPeer cb) |
wolfSSL | 14:167253f4e170 | 1649 | { |
wolfSSL | 16:8e0d178b1d1e | 1650 | if (ctx) |
wolfSSL | 16:8e0d178b1d1e | 1651 | ctx->CBSetPeer = cb; |
wolfSSL | 14:167253f4e170 | 1652 | } |
wolfSSL | 14:167253f4e170 | 1653 | |
wolfSSL | 14:167253f4e170 | 1654 | #endif /* WOLFSSL_SESSION_EXPORT */ |
wolfSSL | 14:167253f4e170 | 1655 | #endif /* WOLFSSL_DTLS */ |
wolfSSL | 14:167253f4e170 | 1656 | |
wolfSSL | 14:167253f4e170 | 1657 | |
wolfSSL | 14:167253f4e170 | 1658 | #ifdef HAVE_NETX |
wolfSSL | 14:167253f4e170 | 1659 | |
wolfSSL | 14:167253f4e170 | 1660 | /* The NetX receive callback |
wolfSSL | 14:167253f4e170 | 1661 | * return : bytes read, or error |
wolfSSL | 14:167253f4e170 | 1662 | */ |
wolfSSL | 14:167253f4e170 | 1663 | int NetX_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx) |
wolfSSL | 14:167253f4e170 | 1664 | { |
wolfSSL | 14:167253f4e170 | 1665 | NetX_Ctx* nxCtx = (NetX_Ctx*)ctx; |
wolfSSL | 14:167253f4e170 | 1666 | ULONG left; |
wolfSSL | 14:167253f4e170 | 1667 | ULONG total; |
wolfSSL | 14:167253f4e170 | 1668 | ULONG copied = 0; |
wolfSSL | 14:167253f4e170 | 1669 | UINT status; |
wolfSSL | 14:167253f4e170 | 1670 | |
wolfSSL | 14:167253f4e170 | 1671 | (void)ssl; |
wolfSSL | 14:167253f4e170 | 1672 | |
wolfSSL | 14:167253f4e170 | 1673 | if (nxCtx == NULL || nxCtx->nxSocket == NULL) { |
wolfSSL | 14:167253f4e170 | 1674 | WOLFSSL_MSG("NetX Recv NULL parameters"); |
wolfSSL | 14:167253f4e170 | 1675 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 1676 | } |
wolfSSL | 14:167253f4e170 | 1677 | |
wolfSSL | 14:167253f4e170 | 1678 | if (nxCtx->nxPacket == NULL) { |
wolfSSL | 14:167253f4e170 | 1679 | status = nx_tcp_socket_receive(nxCtx->nxSocket, &nxCtx->nxPacket, |
wolfSSL | 14:167253f4e170 | 1680 | nxCtx->nxWait); |
wolfSSL | 14:167253f4e170 | 1681 | if (status != NX_SUCCESS) { |
wolfSSL | 14:167253f4e170 | 1682 | WOLFSSL_MSG("NetX Recv receive error"); |
wolfSSL | 14:167253f4e170 | 1683 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 1684 | } |
wolfSSL | 14:167253f4e170 | 1685 | } |
wolfSSL | 14:167253f4e170 | 1686 | |
wolfSSL | 14:167253f4e170 | 1687 | if (nxCtx->nxPacket) { |
wolfSSL | 14:167253f4e170 | 1688 | status = nx_packet_length_get(nxCtx->nxPacket, &total); |
wolfSSL | 14:167253f4e170 | 1689 | if (status != NX_SUCCESS) { |
wolfSSL | 14:167253f4e170 | 1690 | WOLFSSL_MSG("NetX Recv length get error"); |
wolfSSL | 14:167253f4e170 | 1691 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 1692 | } |
wolfSSL | 14:167253f4e170 | 1693 | |
wolfSSL | 14:167253f4e170 | 1694 | left = total - nxCtx->nxOffset; |
wolfSSL | 14:167253f4e170 | 1695 | status = nx_packet_data_extract_offset(nxCtx->nxPacket, nxCtx->nxOffset, |
wolfSSL | 14:167253f4e170 | 1696 | buf, sz, &copied); |
wolfSSL | 14:167253f4e170 | 1697 | if (status != NX_SUCCESS) { |
wolfSSL | 14:167253f4e170 | 1698 | WOLFSSL_MSG("NetX Recv data extract offset error"); |
wolfSSL | 14:167253f4e170 | 1699 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 1700 | } |
wolfSSL | 14:167253f4e170 | 1701 | |
wolfSSL | 14:167253f4e170 | 1702 | nxCtx->nxOffset += copied; |
wolfSSL | 14:167253f4e170 | 1703 | |
wolfSSL | 14:167253f4e170 | 1704 | if (copied == left) { |
wolfSSL | 14:167253f4e170 | 1705 | WOLFSSL_MSG("NetX Recv Drained packet"); |
wolfSSL | 14:167253f4e170 | 1706 | nx_packet_release(nxCtx->nxPacket); |
wolfSSL | 14:167253f4e170 | 1707 | nxCtx->nxPacket = NULL; |
wolfSSL | 14:167253f4e170 | 1708 | nxCtx->nxOffset = 0; |
wolfSSL | 14:167253f4e170 | 1709 | } |
wolfSSL | 14:167253f4e170 | 1710 | } |
wolfSSL | 14:167253f4e170 | 1711 | |
wolfSSL | 14:167253f4e170 | 1712 | return copied; |
wolfSSL | 14:167253f4e170 | 1713 | } |
wolfSSL | 14:167253f4e170 | 1714 | |
wolfSSL | 14:167253f4e170 | 1715 | |
wolfSSL | 14:167253f4e170 | 1716 | /* The NetX send callback |
wolfSSL | 14:167253f4e170 | 1717 | * return : bytes sent, or error |
wolfSSL | 14:167253f4e170 | 1718 | */ |
wolfSSL | 14:167253f4e170 | 1719 | int NetX_Send(WOLFSSL* ssl, char *buf, int sz, void *ctx) |
wolfSSL | 14:167253f4e170 | 1720 | { |
wolfSSL | 14:167253f4e170 | 1721 | NetX_Ctx* nxCtx = (NetX_Ctx*)ctx; |
wolfSSL | 14:167253f4e170 | 1722 | NX_PACKET* packet; |
wolfSSL | 14:167253f4e170 | 1723 | NX_PACKET_POOL* pool; /* shorthand */ |
wolfSSL | 14:167253f4e170 | 1724 | UINT status; |
wolfSSL | 14:167253f4e170 | 1725 | |
wolfSSL | 14:167253f4e170 | 1726 | (void)ssl; |
wolfSSL | 14:167253f4e170 | 1727 | |
wolfSSL | 14:167253f4e170 | 1728 | if (nxCtx == NULL || nxCtx->nxSocket == NULL) { |
wolfSSL | 14:167253f4e170 | 1729 | WOLFSSL_MSG("NetX Send NULL parameters"); |
wolfSSL | 14:167253f4e170 | 1730 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 1731 | } |
wolfSSL | 14:167253f4e170 | 1732 | |
wolfSSL | 14:167253f4e170 | 1733 | pool = nxCtx->nxSocket->nx_tcp_socket_ip_ptr->nx_ip_default_packet_pool; |
wolfSSL | 14:167253f4e170 | 1734 | status = nx_packet_allocate(pool, &packet, NX_TCP_PACKET, |
wolfSSL | 14:167253f4e170 | 1735 | nxCtx->nxWait); |
wolfSSL | 14:167253f4e170 | 1736 | if (status != NX_SUCCESS) { |
wolfSSL | 14:167253f4e170 | 1737 | WOLFSSL_MSG("NetX Send packet alloc error"); |
wolfSSL | 14:167253f4e170 | 1738 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 1739 | } |
wolfSSL | 14:167253f4e170 | 1740 | |
wolfSSL | 14:167253f4e170 | 1741 | status = nx_packet_data_append(packet, buf, sz, pool, nxCtx->nxWait); |
wolfSSL | 14:167253f4e170 | 1742 | if (status != NX_SUCCESS) { |
wolfSSL | 14:167253f4e170 | 1743 | nx_packet_release(packet); |
wolfSSL | 14:167253f4e170 | 1744 | WOLFSSL_MSG("NetX Send data append error"); |
wolfSSL | 14:167253f4e170 | 1745 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 1746 | } |
wolfSSL | 14:167253f4e170 | 1747 | |
wolfSSL | 14:167253f4e170 | 1748 | status = nx_tcp_socket_send(nxCtx->nxSocket, packet, nxCtx->nxWait); |
wolfSSL | 14:167253f4e170 | 1749 | if (status != NX_SUCCESS) { |
wolfSSL | 14:167253f4e170 | 1750 | nx_packet_release(packet); |
wolfSSL | 14:167253f4e170 | 1751 | WOLFSSL_MSG("NetX Send socket send error"); |
wolfSSL | 14:167253f4e170 | 1752 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 1753 | } |
wolfSSL | 14:167253f4e170 | 1754 | |
wolfSSL | 14:167253f4e170 | 1755 | return sz; |
wolfSSL | 14:167253f4e170 | 1756 | } |
wolfSSL | 14:167253f4e170 | 1757 | |
wolfSSL | 14:167253f4e170 | 1758 | |
wolfSSL | 14:167253f4e170 | 1759 | /* like set_fd, but for default NetX context */ |
wolfSSL | 14:167253f4e170 | 1760 | void wolfSSL_SetIO_NetX(WOLFSSL* ssl, NX_TCP_SOCKET* nxSocket, ULONG waitOption) |
wolfSSL | 14:167253f4e170 | 1761 | { |
wolfSSL | 14:167253f4e170 | 1762 | if (ssl) { |
wolfSSL | 14:167253f4e170 | 1763 | ssl->nxCtx.nxSocket = nxSocket; |
wolfSSL | 14:167253f4e170 | 1764 | ssl->nxCtx.nxWait = waitOption; |
wolfSSL | 14:167253f4e170 | 1765 | } |
wolfSSL | 14:167253f4e170 | 1766 | } |
wolfSSL | 14:167253f4e170 | 1767 | |
wolfSSL | 14:167253f4e170 | 1768 | #endif /* HAVE_NETX */ |
wolfSSL | 14:167253f4e170 | 1769 | |
wolfSSL | 14:167253f4e170 | 1770 | |
wolfSSL | 14:167253f4e170 | 1771 | #ifdef MICRIUM |
wolfSSL | 14:167253f4e170 | 1772 | |
wolfSSL | 14:167253f4e170 | 1773 | /* Micrium uTCP/IP port, using the NetSock API |
wolfSSL | 14:167253f4e170 | 1774 | * TCP and UDP are currently supported with the callbacks below. |
wolfSSL | 14:167253f4e170 | 1775 | * |
wolfSSL | 14:167253f4e170 | 1776 | * WOLFSSL_SESSION_EXPORT is not yet supported, would need EmbedGetPeer() |
wolfSSL | 14:167253f4e170 | 1777 | * and EmbedSetPeer() callbacks implemented. |
wolfSSL | 14:167253f4e170 | 1778 | * |
wolfSSL | 14:167253f4e170 | 1779 | * HAVE_CRL is not yet supported, would need an EmbedCrlLookup() |
wolfSSL | 14:167253f4e170 | 1780 | * callback implemented. |
wolfSSL | 14:167253f4e170 | 1781 | * |
wolfSSL | 14:167253f4e170 | 1782 | * HAVE_OCSP is not yet supported, would need an EmbedOCSPLookup() |
wolfSSL | 14:167253f4e170 | 1783 | * callback implemented. |
wolfSSL | 14:167253f4e170 | 1784 | */ |
wolfSSL | 14:167253f4e170 | 1785 | |
wolfSSL | 14:167253f4e170 | 1786 | /* The Micrium uTCP/IP send callback |
wolfSSL | 14:167253f4e170 | 1787 | * return : bytes sent, or error |
wolfSSL | 14:167253f4e170 | 1788 | */ |
wolfSSL | 14:167253f4e170 | 1789 | int MicriumSend(WOLFSSL* ssl, char* buf, int sz, void* ctx) |
wolfSSL | 14:167253f4e170 | 1790 | { |
wolfSSL | 14:167253f4e170 | 1791 | NET_SOCK_ID sd = *(int*)ctx; |
wolfSSL | 14:167253f4e170 | 1792 | NET_SOCK_RTN_CODE ret; |
wolfSSL | 14:167253f4e170 | 1793 | NET_ERR err; |
wolfSSL | 14:167253f4e170 | 1794 | |
wolfSSL | 14:167253f4e170 | 1795 | ret = NetSock_TxData(sd, buf, sz, ssl->wflags, &err); |
wolfSSL | 14:167253f4e170 | 1796 | if (ret < 0) { |
wolfSSL | 14:167253f4e170 | 1797 | WOLFSSL_MSG("Embed Send error"); |
wolfSSL | 14:167253f4e170 | 1798 | |
wolfSSL | 14:167253f4e170 | 1799 | if (err == NET_ERR_TX) { |
wolfSSL | 14:167253f4e170 | 1800 | WOLFSSL_MSG("\tWould block"); |
wolfSSL | 14:167253f4e170 | 1801 | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
wolfSSL | 14:167253f4e170 | 1802 | |
wolfSSL | 14:167253f4e170 | 1803 | } else { |
wolfSSL | 14:167253f4e170 | 1804 | WOLFSSL_MSG("\tGeneral error"); |
wolfSSL | 14:167253f4e170 | 1805 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 1806 | } |
wolfSSL | 14:167253f4e170 | 1807 | } |
wolfSSL | 14:167253f4e170 | 1808 | |
wolfSSL | 14:167253f4e170 | 1809 | return ret; |
wolfSSL | 14:167253f4e170 | 1810 | } |
wolfSSL | 14:167253f4e170 | 1811 | |
wolfSSL | 14:167253f4e170 | 1812 | /* The Micrium uTCP/IP receive callback |
wolfSSL | 14:167253f4e170 | 1813 | * return : nb bytes read, or error |
wolfSSL | 14:167253f4e170 | 1814 | */ |
wolfSSL | 14:167253f4e170 | 1815 | int MicriumReceive(WOLFSSL *ssl, char *buf, int sz, void *ctx) |
wolfSSL | 14:167253f4e170 | 1816 | { |
wolfSSL | 14:167253f4e170 | 1817 | NET_SOCK_ID sd = *(int*)ctx; |
wolfSSL | 14:167253f4e170 | 1818 | NET_SOCK_RTN_CODE ret; |
wolfSSL | 14:167253f4e170 | 1819 | NET_ERR err; |
wolfSSL | 14:167253f4e170 | 1820 | |
wolfSSL | 14:167253f4e170 | 1821 | #ifdef WOLFSSL_DTLS |
wolfSSL | 14:167253f4e170 | 1822 | { |
wolfSSL | 14:167253f4e170 | 1823 | int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl); |
wolfSSL | 14:167253f4e170 | 1824 | if (wolfSSL_dtls(ssl) |
wolfSSL | 14:167253f4e170 | 1825 | && !wolfSSL_dtls_get_using_nonblock(ssl) |
wolfSSL | 14:167253f4e170 | 1826 | && dtls_timeout != 0) { |
wolfSSL | 14:167253f4e170 | 1827 | /* needs timeout in milliseconds */ |
wolfSSL | 14:167253f4e170 | 1828 | NetSock_CfgTimeoutRxQ_Set(sd, dtls_timeout * 1000, &err); |
wolfSSL | 14:167253f4e170 | 1829 | if (err != NET_SOCK_ERR_NONE) { |
wolfSSL | 14:167253f4e170 | 1830 | WOLFSSL_MSG("NetSock_CfgTimeoutRxQ_Set failed"); |
wolfSSL | 14:167253f4e170 | 1831 | } |
wolfSSL | 14:167253f4e170 | 1832 | } |
wolfSSL | 14:167253f4e170 | 1833 | } |
wolfSSL | 14:167253f4e170 | 1834 | #endif |
wolfSSL | 14:167253f4e170 | 1835 | |
wolfSSL | 14:167253f4e170 | 1836 | ret = NetSock_RxData(sd, buf, sz, ssl->rflags, &err); |
wolfSSL | 14:167253f4e170 | 1837 | if (ret < 0) { |
wolfSSL | 14:167253f4e170 | 1838 | WOLFSSL_MSG("Embed Receive error"); |
wolfSSL | 14:167253f4e170 | 1839 | |
wolfSSL | 14:167253f4e170 | 1840 | if (err == NET_ERR_RX || err == NET_SOCK_ERR_RX_Q_EMPTY || |
wolfSSL | 14:167253f4e170 | 1841 | err == NET_ERR_FAULT_LOCK_ACQUIRE) { |
wolfSSL | 14:167253f4e170 | 1842 | if (!wolfSSL_dtls(ssl) || wolfSSL_dtls_get_using_nonblock(ssl)) { |
wolfSSL | 14:167253f4e170 | 1843 | WOLFSSL_MSG("\tWould block"); |
wolfSSL | 14:167253f4e170 | 1844 | return WOLFSSL_CBIO_ERR_WANT_READ; |
wolfSSL | 14:167253f4e170 | 1845 | } |
wolfSSL | 14:167253f4e170 | 1846 | else { |
wolfSSL | 14:167253f4e170 | 1847 | WOLFSSL_MSG("\tSocket timeout"); |
wolfSSL | 14:167253f4e170 | 1848 | return WOLFSSL_CBIO_ERR_TIMEOUT; |
wolfSSL | 14:167253f4e170 | 1849 | } |
wolfSSL | 14:167253f4e170 | 1850 | |
wolfSSL | 14:167253f4e170 | 1851 | } else if (err == NET_SOCK_ERR_CLOSED) { |
wolfSSL | 14:167253f4e170 | 1852 | WOLFSSL_MSG("Embed receive connection closed"); |
wolfSSL | 14:167253f4e170 | 1853 | return WOLFSSL_CBIO_ERR_CONN_CLOSE; |
wolfSSL | 14:167253f4e170 | 1854 | |
wolfSSL | 14:167253f4e170 | 1855 | } else { |
wolfSSL | 14:167253f4e170 | 1856 | WOLFSSL_MSG("\tGeneral error"); |
wolfSSL | 14:167253f4e170 | 1857 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 1858 | } |
wolfSSL | 14:167253f4e170 | 1859 | } |
wolfSSL | 14:167253f4e170 | 1860 | |
wolfSSL | 14:167253f4e170 | 1861 | return ret; |
wolfSSL | 14:167253f4e170 | 1862 | } |
wolfSSL | 14:167253f4e170 | 1863 | |
wolfSSL | 14:167253f4e170 | 1864 | /* The Micrium uTCP/IP receivefrom callback |
wolfSSL | 14:167253f4e170 | 1865 | * return : nb bytes read, or error |
wolfSSL | 14:167253f4e170 | 1866 | */ |
wolfSSL | 14:167253f4e170 | 1867 | int MicriumReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx) |
wolfSSL | 14:167253f4e170 | 1868 | { |
wolfSSL | 14:167253f4e170 | 1869 | WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx; |
wolfSSL | 14:167253f4e170 | 1870 | NET_SOCK_ID sd = dtlsCtx->rfd; |
wolfSSL | 14:167253f4e170 | 1871 | NET_SOCK_ADDR peer; |
wolfSSL | 14:167253f4e170 | 1872 | NET_SOCK_ADDR_LEN peerSz = sizeof(peer); |
wolfSSL | 14:167253f4e170 | 1873 | NET_SOCK_RTN_CODE ret; |
wolfSSL | 14:167253f4e170 | 1874 | NET_ERR err; |
wolfSSL | 14:167253f4e170 | 1875 | int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl); |
wolfSSL | 14:167253f4e170 | 1876 | |
wolfSSL | 14:167253f4e170 | 1877 | WOLFSSL_ENTER("MicriumReceiveFrom()"); |
wolfSSL | 14:167253f4e170 | 1878 | |
wolfSSL | 14:167253f4e170 | 1879 | if (ssl->options.handShakeDone) |
wolfSSL | 14:167253f4e170 | 1880 | dtls_timeout = 0; |
wolfSSL | 14:167253f4e170 | 1881 | |
wolfSSL | 14:167253f4e170 | 1882 | if (!wolfSSL_dtls_get_using_nonblock(ssl)) { |
wolfSSL | 14:167253f4e170 | 1883 | /* needs timeout in milliseconds */ |
wolfSSL | 14:167253f4e170 | 1884 | NetSock_CfgTimeoutRxQ_Set(sd, dtls_timeout * 1000, &err); |
wolfSSL | 14:167253f4e170 | 1885 | if (err != NET_SOCK_ERR_NONE) { |
wolfSSL | 14:167253f4e170 | 1886 | WOLFSSL_MSG("NetSock_CfgTimeoutRxQ_Set failed"); |
wolfSSL | 14:167253f4e170 | 1887 | } |
wolfSSL | 14:167253f4e170 | 1888 | } |
wolfSSL | 14:167253f4e170 | 1889 | |
wolfSSL | 14:167253f4e170 | 1890 | ret = NetSock_RxDataFrom(sd, buf, sz, ssl->rflags, &peer, &peerSz, |
wolfSSL | 14:167253f4e170 | 1891 | 0, 0, 0, &err); |
wolfSSL | 14:167253f4e170 | 1892 | if (ret < 0) { |
wolfSSL | 14:167253f4e170 | 1893 | WOLFSSL_MSG("Embed Receive From error"); |
wolfSSL | 14:167253f4e170 | 1894 | |
wolfSSL | 14:167253f4e170 | 1895 | if (err == NET_ERR_RX || err == NET_SOCK_ERR_RX_Q_EMPTY || |
wolfSSL | 14:167253f4e170 | 1896 | err == NET_ERR_FAULT_LOCK_ACQUIRE) { |
wolfSSL | 14:167253f4e170 | 1897 | if (wolfSSL_dtls_get_using_nonblock(ssl)) { |
wolfSSL | 14:167253f4e170 | 1898 | WOLFSSL_MSG("\tWould block"); |
wolfSSL | 14:167253f4e170 | 1899 | return WOLFSSL_CBIO_ERR_WANT_READ; |
wolfSSL | 14:167253f4e170 | 1900 | } |
wolfSSL | 14:167253f4e170 | 1901 | else { |
wolfSSL | 14:167253f4e170 | 1902 | WOLFSSL_MSG("\tSocket timeout"); |
wolfSSL | 14:167253f4e170 | 1903 | return WOLFSSL_CBIO_ERR_TIMEOUT; |
wolfSSL | 14:167253f4e170 | 1904 | } |
wolfSSL | 14:167253f4e170 | 1905 | } else { |
wolfSSL | 14:167253f4e170 | 1906 | WOLFSSL_MSG("\tGeneral error"); |
wolfSSL | 14:167253f4e170 | 1907 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 1908 | } |
wolfSSL | 14:167253f4e170 | 1909 | } |
wolfSSL | 14:167253f4e170 | 1910 | else { |
wolfSSL | 14:167253f4e170 | 1911 | if (dtlsCtx->peer.sz > 0 |
wolfSSL | 14:167253f4e170 | 1912 | && peerSz != (NET_SOCK_ADDR_LEN)dtlsCtx->peer.sz |
wolfSSL | 14:167253f4e170 | 1913 | && XMEMCMP(&peer, dtlsCtx->peer.sa, peerSz) != 0) { |
wolfSSL | 14:167253f4e170 | 1914 | WOLFSSL_MSG("\tIgnored packet from invalid peer"); |
wolfSSL | 14:167253f4e170 | 1915 | return WOLFSSL_CBIO_ERR_WANT_READ; |
wolfSSL | 14:167253f4e170 | 1916 | } |
wolfSSL | 14:167253f4e170 | 1917 | } |
wolfSSL | 14:167253f4e170 | 1918 | |
wolfSSL | 14:167253f4e170 | 1919 | return ret; |
wolfSSL | 14:167253f4e170 | 1920 | } |
wolfSSL | 14:167253f4e170 | 1921 | |
wolfSSL | 14:167253f4e170 | 1922 | /* The Micrium uTCP/IP sendto callback |
wolfSSL | 14:167253f4e170 | 1923 | * return : nb bytes sent, or error |
wolfSSL | 14:167253f4e170 | 1924 | */ |
wolfSSL | 14:167253f4e170 | 1925 | int MicriumSendTo(WOLFSSL* ssl, char *buf, int sz, void *ctx) |
wolfSSL | 14:167253f4e170 | 1926 | { |
wolfSSL | 14:167253f4e170 | 1927 | WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx; |
wolfSSL | 14:167253f4e170 | 1928 | NET_SOCK_ID sd = dtlsCtx->wfd; |
wolfSSL | 14:167253f4e170 | 1929 | NET_SOCK_RTN_CODE ret; |
wolfSSL | 14:167253f4e170 | 1930 | NET_ERR err; |
wolfSSL | 14:167253f4e170 | 1931 | |
wolfSSL | 14:167253f4e170 | 1932 | WOLFSSL_ENTER("MicriumSendTo()"); |
wolfSSL | 14:167253f4e170 | 1933 | |
wolfSSL | 16:8e0d178b1d1e | 1934 | ret = NetSock_TxDataTo(sd, buf, sz, ssl->wflags, |
wolfSSL | 14:167253f4e170 | 1935 | (NET_SOCK_ADDR*)dtlsCtx->peer.sa, |
wolfSSL | 14:167253f4e170 | 1936 | (NET_SOCK_ADDR_LEN)dtlsCtx->peer.sz, |
wolfSSL | 14:167253f4e170 | 1937 | &err); |
wolfSSL | 14:167253f4e170 | 1938 | if (err < 0) { |
wolfSSL | 14:167253f4e170 | 1939 | WOLFSSL_MSG("Embed Send To error"); |
wolfSSL | 14:167253f4e170 | 1940 | |
wolfSSL | 14:167253f4e170 | 1941 | if (err == NET_ERR_TX) { |
wolfSSL | 14:167253f4e170 | 1942 | WOLFSSL_MSG("\tWould block"); |
wolfSSL | 14:167253f4e170 | 1943 | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
wolfSSL | 14:167253f4e170 | 1944 | |
wolfSSL | 14:167253f4e170 | 1945 | } else { |
wolfSSL | 14:167253f4e170 | 1946 | WOLFSSL_MSG("\tGeneral error"); |
wolfSSL | 14:167253f4e170 | 1947 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 14:167253f4e170 | 1948 | } |
wolfSSL | 14:167253f4e170 | 1949 | } |
wolfSSL | 14:167253f4e170 | 1950 | |
wolfSSL | 14:167253f4e170 | 1951 | return ret; |
wolfSSL | 14:167253f4e170 | 1952 | } |
wolfSSL | 14:167253f4e170 | 1953 | |
wolfSSL | 14:167253f4e170 | 1954 | /* Micrium DTLS Generate Cookie callback |
wolfSSL | 14:167253f4e170 | 1955 | * return : number of bytes copied into buf, or error |
wolfSSL | 14:167253f4e170 | 1956 | */ |
wolfSSL | 14:167253f4e170 | 1957 | int MicriumGenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *ctx) |
wolfSSL | 14:167253f4e170 | 1958 | { |
wolfSSL | 14:167253f4e170 | 1959 | NET_SOCK_ADDR peer; |
wolfSSL | 14:167253f4e170 | 1960 | NET_SOCK_ADDR_LEN peerSz = sizeof(peer); |
wolfSSL | 14:167253f4e170 | 1961 | byte digest[WC_SHA_DIGEST_SIZE]; |
wolfSSL | 14:167253f4e170 | 1962 | int ret = 0; |
wolfSSL | 14:167253f4e170 | 1963 | |
wolfSSL | 14:167253f4e170 | 1964 | (void)ctx; |
wolfSSL | 14:167253f4e170 | 1965 | |
wolfSSL | 14:167253f4e170 | 1966 | XMEMSET(&peer, 0, sizeof(peer)); |
wolfSSL | 14:167253f4e170 | 1967 | if (wolfSSL_dtls_get_peer(ssl, (void*)&peer, |
wolfSSL | 14:167253f4e170 | 1968 | (unsigned int*)&peerSz) != WOLFSSL_SUCCESS) { |
wolfSSL | 14:167253f4e170 | 1969 | WOLFSSL_MSG("getpeername failed in MicriumGenerateCookie"); |
wolfSSL | 14:167253f4e170 | 1970 | return GEN_COOKIE_E; |
wolfSSL | 14:167253f4e170 | 1971 | } |
wolfSSL | 14:167253f4e170 | 1972 | |
wolfSSL | 14:167253f4e170 | 1973 | ret = wc_ShaHash((byte*)&peer, peerSz, digest); |
wolfSSL | 14:167253f4e170 | 1974 | if (ret != 0) |
wolfSSL | 14:167253f4e170 | 1975 | return ret; |
wolfSSL | 14:167253f4e170 | 1976 | |
wolfSSL | 14:167253f4e170 | 1977 | if (sz > WC_SHA_DIGEST_SIZE) |
wolfSSL | 14:167253f4e170 | 1978 | sz = WC_SHA_DIGEST_SIZE; |
wolfSSL | 14:167253f4e170 | 1979 | XMEMCPY(buf, digest, sz); |
wolfSSL | 14:167253f4e170 | 1980 | |
wolfSSL | 14:167253f4e170 | 1981 | return sz; |
wolfSSL | 14:167253f4e170 | 1982 | } |
wolfSSL | 14:167253f4e170 | 1983 | |
wolfSSL | 14:167253f4e170 | 1984 | #endif /* MICRIUM */ |
wolfSSL | 14:167253f4e170 | 1985 | |
wolfSSL | 16:8e0d178b1d1e | 1986 | #if defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) |
wolfSSL | 16:8e0d178b1d1e | 1987 | |
wolfSSL | 16:8e0d178b1d1e | 1988 | #include <os/os_error.h> |
wolfSSL | 16:8e0d178b1d1e | 1989 | #include <os/os_mbuf.h> |
wolfSSL | 16:8e0d178b1d1e | 1990 | #include <os/os_mempool.h> |
wolfSSL | 16:8e0d178b1d1e | 1991 | |
wolfSSL | 16:8e0d178b1d1e | 1992 | #define MB_NAME "wolfssl_mb" |
wolfSSL | 16:8e0d178b1d1e | 1993 | |
wolfSSL | 16:8e0d178b1d1e | 1994 | typedef struct Mynewt_Ctx { |
wolfSSL | 16:8e0d178b1d1e | 1995 | struct mn_socket *mnSocket; /* send/recv socket handler */ |
wolfSSL | 16:8e0d178b1d1e | 1996 | struct mn_sockaddr_in mnSockAddrIn; /* socket address */ |
wolfSSL | 16:8e0d178b1d1e | 1997 | struct os_mbuf *mnPacket; /* incoming packet handle |
wolfSSL | 16:8e0d178b1d1e | 1998 | for short reads */ |
wolfSSL | 16:8e0d178b1d1e | 1999 | int reading; /* reading flag */ |
wolfSSL | 16:8e0d178b1d1e | 2000 | |
wolfSSL | 16:8e0d178b1d1e | 2001 | /* private */ |
wolfSSL | 16:8e0d178b1d1e | 2002 | void *mnMemBuffer; /* memory buffer for mempool */ |
wolfSSL | 16:8e0d178b1d1e | 2003 | struct os_mempool mnMempool; /* mempool */ |
wolfSSL | 16:8e0d178b1d1e | 2004 | struct os_mbuf_pool mnMbufpool; /* mbuf pool */ |
wolfSSL | 16:8e0d178b1d1e | 2005 | } Mynewt_Ctx; |
wolfSSL | 16:8e0d178b1d1e | 2006 | |
wolfSSL | 16:8e0d178b1d1e | 2007 | void mynewt_ctx_clear(void *ctx) { |
wolfSSL | 16:8e0d178b1d1e | 2008 | Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx*)ctx; |
wolfSSL | 16:8e0d178b1d1e | 2009 | if(!mynewt_ctx) return; |
wolfSSL | 16:8e0d178b1d1e | 2010 | |
wolfSSL | 16:8e0d178b1d1e | 2011 | if(mynewt_ctx->mnPacket) { |
wolfSSL | 16:8e0d178b1d1e | 2012 | os_mbuf_free_chain(mynewt_ctx->mnPacket); |
wolfSSL | 16:8e0d178b1d1e | 2013 | mynewt_ctx->mnPacket = NULL; |
wolfSSL | 16:8e0d178b1d1e | 2014 | } |
wolfSSL | 16:8e0d178b1d1e | 2015 | os_mempool_clear(&mynewt_ctx->mnMempool); |
wolfSSL | 16:8e0d178b1d1e | 2016 | XFREE(mynewt_ctx->mnMemBuffer, 0, 0); |
wolfSSL | 16:8e0d178b1d1e | 2017 | XFREE(mynewt_ctx, 0, 0); |
wolfSSL | 16:8e0d178b1d1e | 2018 | } |
wolfSSL | 16:8e0d178b1d1e | 2019 | |
wolfSSL | 16:8e0d178b1d1e | 2020 | /* return Mynewt_Ctx instance */ |
wolfSSL | 16:8e0d178b1d1e | 2021 | void* mynewt_ctx_new() { |
wolfSSL | 16:8e0d178b1d1e | 2022 | int rc = 0; |
wolfSSL | 16:8e0d178b1d1e | 2023 | Mynewt_Ctx *mynewt_ctx; |
wolfSSL | 16:8e0d178b1d1e | 2024 | int mem_buf_count = MYNEWT_VAL(WOLFSSL_MNSOCK_MEM_BUF_COUNT); |
wolfSSL | 16:8e0d178b1d1e | 2025 | int mem_buf_size = MYNEWT_VAL(WOLFSSL_MNSOCK_MEM_BUF_SIZE); |
wolfSSL | 16:8e0d178b1d1e | 2026 | int mempool_bytes = OS_MEMPOOL_BYTES(mem_buf_count, mem_buf_size); |
wolfSSL | 16:8e0d178b1d1e | 2027 | |
wolfSSL | 16:8e0d178b1d1e | 2028 | mynewt_ctx = (Mynewt_Ctx *)XMALLOC(sizeof(struct Mynewt_Ctx), |
wolfSSL | 16:8e0d178b1d1e | 2029 | NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 16:8e0d178b1d1e | 2030 | if(!mynewt_ctx) return NULL; |
wolfSSL | 16:8e0d178b1d1e | 2031 | |
wolfSSL | 16:8e0d178b1d1e | 2032 | XMEMSET(mynewt_ctx, 0, sizeof(Mynewt_Ctx)); |
wolfSSL | 16:8e0d178b1d1e | 2033 | mynewt_ctx->mnMemBuffer = XMALLOC(mempool_bytes, 0, 0); |
wolfSSL | 16:8e0d178b1d1e | 2034 | if(!mynewt_ctx->mnMemBuffer) { |
wolfSSL | 16:8e0d178b1d1e | 2035 | mynewt_ctx_clear((void*)mynewt_ctx); |
wolfSSL | 16:8e0d178b1d1e | 2036 | return NULL; |
wolfSSL | 16:8e0d178b1d1e | 2037 | } |
wolfSSL | 16:8e0d178b1d1e | 2038 | |
wolfSSL | 16:8e0d178b1d1e | 2039 | rc = os_mempool_init(&mynewt_ctx->mnMempool, |
wolfSSL | 16:8e0d178b1d1e | 2040 | mem_buf_count, mem_buf_size, |
wolfSSL | 16:8e0d178b1d1e | 2041 | mynewt_ctx->mnMemBuffer, MB_NAME); |
wolfSSL | 16:8e0d178b1d1e | 2042 | if(rc != 0) { |
wolfSSL | 16:8e0d178b1d1e | 2043 | mynewt_ctx_clear((void*)mynewt_ctx); |
wolfSSL | 16:8e0d178b1d1e | 2044 | return NULL; |
wolfSSL | 16:8e0d178b1d1e | 2045 | } |
wolfSSL | 16:8e0d178b1d1e | 2046 | rc = os_mbuf_pool_init(&mynewt_ctx->mnMbufpool, &mynewt_ctx->mnMempool, |
wolfSSL | 16:8e0d178b1d1e | 2047 | mem_buf_count, mem_buf_size); |
wolfSSL | 16:8e0d178b1d1e | 2048 | if(rc != 0) { |
wolfSSL | 16:8e0d178b1d1e | 2049 | mynewt_ctx_clear((void*)mynewt_ctx); |
wolfSSL | 16:8e0d178b1d1e | 2050 | return NULL; |
wolfSSL | 16:8e0d178b1d1e | 2051 | } |
wolfSSL | 16:8e0d178b1d1e | 2052 | |
wolfSSL | 16:8e0d178b1d1e | 2053 | return mynewt_ctx; |
wolfSSL | 16:8e0d178b1d1e | 2054 | } |
wolfSSL | 16:8e0d178b1d1e | 2055 | |
wolfSSL | 16:8e0d178b1d1e | 2056 | static void mynewt_sock_writable(void *arg, int err); |
wolfSSL | 16:8e0d178b1d1e | 2057 | static void mynewt_sock_readable(void *arg, int err); |
wolfSSL | 16:8e0d178b1d1e | 2058 | static const union mn_socket_cb mynewt_sock_cbs = { |
wolfSSL | 16:8e0d178b1d1e | 2059 | .socket.writable = mynewt_sock_writable, |
wolfSSL | 16:8e0d178b1d1e | 2060 | .socket.readable = mynewt_sock_readable, |
wolfSSL | 16:8e0d178b1d1e | 2061 | }; |
wolfSSL | 16:8e0d178b1d1e | 2062 | static void mynewt_sock_writable(void *arg, int err) |
wolfSSL | 16:8e0d178b1d1e | 2063 | { |
wolfSSL | 16:8e0d178b1d1e | 2064 | /* do nothing */ |
wolfSSL | 16:8e0d178b1d1e | 2065 | } |
wolfSSL | 16:8e0d178b1d1e | 2066 | static void mynewt_sock_readable(void *arg, int err) |
wolfSSL | 16:8e0d178b1d1e | 2067 | { |
wolfSSL | 16:8e0d178b1d1e | 2068 | Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx *)arg; |
wolfSSL | 16:8e0d178b1d1e | 2069 | if (err && mynewt_ctx->reading) { |
wolfSSL | 16:8e0d178b1d1e | 2070 | mynewt_ctx->reading = 0; |
wolfSSL | 16:8e0d178b1d1e | 2071 | } |
wolfSSL | 16:8e0d178b1d1e | 2072 | } |
wolfSSL | 16:8e0d178b1d1e | 2073 | |
wolfSSL | 16:8e0d178b1d1e | 2074 | /* The Mynewt receive callback |
wolfSSL | 16:8e0d178b1d1e | 2075 | * return : bytes read, or error |
wolfSSL | 16:8e0d178b1d1e | 2076 | */ |
wolfSSL | 16:8e0d178b1d1e | 2077 | int Mynewt_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx) |
wolfSSL | 16:8e0d178b1d1e | 2078 | { |
wolfSSL | 16:8e0d178b1d1e | 2079 | Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx*)ctx; |
wolfSSL | 16:8e0d178b1d1e | 2080 | int rc = 0; |
wolfSSL | 16:8e0d178b1d1e | 2081 | struct mn_sockaddr_in from; |
wolfSSL | 16:8e0d178b1d1e | 2082 | struct os_mbuf *m; |
wolfSSL | 16:8e0d178b1d1e | 2083 | int read_sz = 0; |
wolfSSL | 16:8e0d178b1d1e | 2084 | uint16_t total; |
wolfSSL | 16:8e0d178b1d1e | 2085 | |
wolfSSL | 16:8e0d178b1d1e | 2086 | if (mynewt_ctx == NULL || mynewt_ctx->mnSocket == NULL) { |
wolfSSL | 16:8e0d178b1d1e | 2087 | WOLFSSL_MSG("Mynewt Recv NULL parameters"); |
wolfSSL | 16:8e0d178b1d1e | 2088 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 16:8e0d178b1d1e | 2089 | } |
wolfSSL | 16:8e0d178b1d1e | 2090 | |
wolfSSL | 16:8e0d178b1d1e | 2091 | if(mynewt_ctx->mnPacket == NULL) { |
wolfSSL | 16:8e0d178b1d1e | 2092 | mynewt_ctx->mnPacket = os_mbuf_get_pkthdr(&mynewt_ctx->mnMbufpool, 0); |
wolfSSL | 16:8e0d178b1d1e | 2093 | if(mynewt_ctx->mnPacket == NULL) { |
wolfSSL | 16:8e0d178b1d1e | 2094 | return MEMORY_E; |
wolfSSL | 16:8e0d178b1d1e | 2095 | } |
wolfSSL | 16:8e0d178b1d1e | 2096 | |
wolfSSL | 16:8e0d178b1d1e | 2097 | mynewt_ctx->reading = 1; |
wolfSSL | 16:8e0d178b1d1e | 2098 | while(mynewt_ctx->reading && rc == 0) { |
wolfSSL | 16:8e0d178b1d1e | 2099 | rc = mn_recvfrom(mynewt_ctx->mnSocket, &m, (struct mn_sockaddr *) &from); |
wolfSSL | 16:8e0d178b1d1e | 2100 | if(rc == MN_ECONNABORTED) { |
wolfSSL | 16:8e0d178b1d1e | 2101 | rc = 0; |
wolfSSL | 16:8e0d178b1d1e | 2102 | mynewt_ctx->reading = 0; |
wolfSSL | 16:8e0d178b1d1e | 2103 | break; |
wolfSSL | 16:8e0d178b1d1e | 2104 | } |
wolfSSL | 16:8e0d178b1d1e | 2105 | if (!(rc == 0 || rc == MN_EAGAIN)) { |
wolfSSL | 16:8e0d178b1d1e | 2106 | WOLFSSL_MSG("Mynewt Recv receive error"); |
wolfSSL | 16:8e0d178b1d1e | 2107 | mynewt_ctx->reading = 0; |
wolfSSL | 16:8e0d178b1d1e | 2108 | break; |
wolfSSL | 16:8e0d178b1d1e | 2109 | } |
wolfSSL | 16:8e0d178b1d1e | 2110 | if(rc == 0) { |
wolfSSL | 16:8e0d178b1d1e | 2111 | int len = OS_MBUF_PKTLEN(m); |
wolfSSL | 16:8e0d178b1d1e | 2112 | if(len == 0) { |
wolfSSL | 16:8e0d178b1d1e | 2113 | break; |
wolfSSL | 16:8e0d178b1d1e | 2114 | } |
wolfSSL | 16:8e0d178b1d1e | 2115 | rc = os_mbuf_appendfrom(mynewt_ctx->mnPacket, m, 0, len); |
wolfSSL | 16:8e0d178b1d1e | 2116 | if(rc != 0) { |
wolfSSL | 16:8e0d178b1d1e | 2117 | WOLFSSL_MSG("Mynewt Recv os_mbuf_appendfrom error"); |
wolfSSL | 16:8e0d178b1d1e | 2118 | break; |
wolfSSL | 16:8e0d178b1d1e | 2119 | } |
wolfSSL | 16:8e0d178b1d1e | 2120 | os_mbuf_free_chain(m); |
wolfSSL | 16:8e0d178b1d1e | 2121 | m = NULL; |
wolfSSL | 16:8e0d178b1d1e | 2122 | } else if(rc == MN_EAGAIN) { |
wolfSSL | 16:8e0d178b1d1e | 2123 | /* continue to until reading all of packet data. */ |
wolfSSL | 16:8e0d178b1d1e | 2124 | rc = 0; |
wolfSSL | 16:8e0d178b1d1e | 2125 | break; |
wolfSSL | 16:8e0d178b1d1e | 2126 | } |
wolfSSL | 16:8e0d178b1d1e | 2127 | } |
wolfSSL | 16:8e0d178b1d1e | 2128 | if(rc != 0) { |
wolfSSL | 16:8e0d178b1d1e | 2129 | mynewt_ctx->reading = 0; |
wolfSSL | 16:8e0d178b1d1e | 2130 | os_mbuf_free_chain(mynewt_ctx->mnPacket); |
wolfSSL | 16:8e0d178b1d1e | 2131 | mynewt_ctx->mnPacket = NULL; |
wolfSSL | 16:8e0d178b1d1e | 2132 | return rc; |
wolfSSL | 16:8e0d178b1d1e | 2133 | } |
wolfSSL | 16:8e0d178b1d1e | 2134 | } |
wolfSSL | 16:8e0d178b1d1e | 2135 | |
wolfSSL | 16:8e0d178b1d1e | 2136 | if(mynewt_ctx->mnPacket) { |
wolfSSL | 16:8e0d178b1d1e | 2137 | total = OS_MBUF_PKTLEN(mynewt_ctx->mnPacket); |
wolfSSL | 16:8e0d178b1d1e | 2138 | read_sz = (total >= sz)? sz : total; |
wolfSSL | 16:8e0d178b1d1e | 2139 | |
wolfSSL | 16:8e0d178b1d1e | 2140 | os_mbuf_copydata(mynewt_ctx->mnPacket, 0, read_sz, (void*)buf); |
wolfSSL | 16:8e0d178b1d1e | 2141 | os_mbuf_adj(mynewt_ctx->mnPacket, read_sz); |
wolfSSL | 16:8e0d178b1d1e | 2142 | |
wolfSSL | 16:8e0d178b1d1e | 2143 | if (read_sz == total) { |
wolfSSL | 16:8e0d178b1d1e | 2144 | WOLFSSL_MSG("Mynewt Recv Drained packet"); |
wolfSSL | 16:8e0d178b1d1e | 2145 | os_mbuf_free_chain(mynewt_ctx->mnPacket); |
wolfSSL | 16:8e0d178b1d1e | 2146 | mynewt_ctx->mnPacket = NULL; |
wolfSSL | 16:8e0d178b1d1e | 2147 | } |
wolfSSL | 16:8e0d178b1d1e | 2148 | } |
wolfSSL | 16:8e0d178b1d1e | 2149 | |
wolfSSL | 16:8e0d178b1d1e | 2150 | return read_sz; |
wolfSSL | 16:8e0d178b1d1e | 2151 | } |
wolfSSL | 16:8e0d178b1d1e | 2152 | |
wolfSSL | 16:8e0d178b1d1e | 2153 | /* The Mynewt send callback |
wolfSSL | 16:8e0d178b1d1e | 2154 | * return : bytes sent, or error |
wolfSSL | 16:8e0d178b1d1e | 2155 | */ |
wolfSSL | 16:8e0d178b1d1e | 2156 | int Mynewt_Send(WOLFSSL* ssl, char *buf, int sz, void *ctx) |
wolfSSL | 16:8e0d178b1d1e | 2157 | { |
wolfSSL | 16:8e0d178b1d1e | 2158 | Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx*)ctx; |
wolfSSL | 16:8e0d178b1d1e | 2159 | int rc = 0; |
wolfSSL | 16:8e0d178b1d1e | 2160 | struct os_mbuf *m; |
wolfSSL | 16:8e0d178b1d1e | 2161 | int write_sz = 0; |
wolfSSL | 16:8e0d178b1d1e | 2162 | m = os_msys_get_pkthdr(sz, 0); |
wolfSSL | 16:8e0d178b1d1e | 2163 | if (!m) { |
wolfSSL | 16:8e0d178b1d1e | 2164 | WOLFSSL_MSG("Mynewt Send os_msys_get_pkthdr error"); |
wolfSSL | 16:8e0d178b1d1e | 2165 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 16:8e0d178b1d1e | 2166 | } |
wolfSSL | 16:8e0d178b1d1e | 2167 | rc = os_mbuf_copyinto(m, 0, buf, sz); |
wolfSSL | 16:8e0d178b1d1e | 2168 | if (rc != 0) { |
wolfSSL | 16:8e0d178b1d1e | 2169 | WOLFSSL_MSG("Mynewt Send os_mbuf_copyinto error"); |
wolfSSL | 16:8e0d178b1d1e | 2170 | os_mbuf_free_chain(m); |
wolfSSL | 16:8e0d178b1d1e | 2171 | return rc; |
wolfSSL | 16:8e0d178b1d1e | 2172 | } |
wolfSSL | 16:8e0d178b1d1e | 2173 | rc = mn_sendto(mynewt_ctx->mnSocket, m, (struct mn_sockaddr *)&mynewt_ctx->mnSockAddrIn); |
wolfSSL | 16:8e0d178b1d1e | 2174 | if(rc != 0) { |
wolfSSL | 16:8e0d178b1d1e | 2175 | WOLFSSL_MSG("Mynewt Send mn_sendto error"); |
wolfSSL | 16:8e0d178b1d1e | 2176 | os_mbuf_free_chain(m); |
wolfSSL | 16:8e0d178b1d1e | 2177 | return rc; |
wolfSSL | 16:8e0d178b1d1e | 2178 | } |
wolfSSL | 16:8e0d178b1d1e | 2179 | write_sz = sz; |
wolfSSL | 16:8e0d178b1d1e | 2180 | return write_sz; |
wolfSSL | 16:8e0d178b1d1e | 2181 | } |
wolfSSL | 16:8e0d178b1d1e | 2182 | |
wolfSSL | 16:8e0d178b1d1e | 2183 | /* like set_fd, but for default NetX context */ |
wolfSSL | 16:8e0d178b1d1e | 2184 | void wolfSSL_SetIO_Mynewt(WOLFSSL* ssl, struct mn_socket* mnSocket, struct mn_sockaddr_in* mnSockAddrIn) |
wolfSSL | 16:8e0d178b1d1e | 2185 | { |
wolfSSL | 16:8e0d178b1d1e | 2186 | if (ssl && ssl->mnCtx) { |
wolfSSL | 16:8e0d178b1d1e | 2187 | Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx *)ssl->mnCtx; |
wolfSSL | 16:8e0d178b1d1e | 2188 | mynewt_ctx->mnSocket = mnSocket; |
wolfSSL | 16:8e0d178b1d1e | 2189 | memcpy(&mynewt_ctx->mnSockAddrIn, mnSockAddrIn, sizeof(struct mn_sockaddr_in)); |
wolfSSL | 16:8e0d178b1d1e | 2190 | mn_socket_set_cbs(mynewt_ctx->mnSocket, mnSocket, &mynewt_sock_cbs); |
wolfSSL | 16:8e0d178b1d1e | 2191 | } |
wolfSSL | 16:8e0d178b1d1e | 2192 | } |
wolfSSL | 16:8e0d178b1d1e | 2193 | |
wolfSSL | 16:8e0d178b1d1e | 2194 | #endif /* defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) */ |
wolfSSL | 16:8e0d178b1d1e | 2195 | |
wolfSSL | 16:8e0d178b1d1e | 2196 | #ifdef WOLFSSL_UIP |
wolfSSL | 16:8e0d178b1d1e | 2197 | #include <uip.h> |
wolfSSL | 16:8e0d178b1d1e | 2198 | #include <stdio.h> |
wolfSSL | 16:8e0d178b1d1e | 2199 | |
wolfSSL | 16:8e0d178b1d1e | 2200 | /* uIP TCP/IP port, using the native tcp/udp socket api. |
wolfSSL | 16:8e0d178b1d1e | 2201 | * TCP and UDP are currently supported with the callbacks below. |
wolfSSL | 16:8e0d178b1d1e | 2202 | * |
wolfSSL | 16:8e0d178b1d1e | 2203 | */ |
wolfSSL | 16:8e0d178b1d1e | 2204 | /* The uIP tcp send callback |
wolfSSL | 16:8e0d178b1d1e | 2205 | * return : bytes sent, or error |
wolfSSL | 16:8e0d178b1d1e | 2206 | */ |
wolfSSL | 16:8e0d178b1d1e | 2207 | int uIPSend(WOLFSSL* ssl, char* buf, int sz, void* _ctx) |
wolfSSL | 16:8e0d178b1d1e | 2208 | { |
wolfSSL | 16:8e0d178b1d1e | 2209 | uip_wolfssl_ctx *ctx = (struct uip_wolfssl_ctx *)_ctx; |
wolfSSL | 16:8e0d178b1d1e | 2210 | int ret; |
wolfSSL | 16:8e0d178b1d1e | 2211 | unsigned int max_sendlen; |
wolfSSL | 16:8e0d178b1d1e | 2212 | int total_written = 0; |
wolfSSL | 16:8e0d178b1d1e | 2213 | (void)ssl; |
wolfSSL | 16:8e0d178b1d1e | 2214 | do { |
wolfSSL | 16:8e0d178b1d1e | 2215 | unsigned int bytes_left = sz - total_written; |
wolfSSL | 16:8e0d178b1d1e | 2216 | max_sendlen = tcp_socket_max_sendlen(&ctx->conn.tcp); |
wolfSSL | 16:8e0d178b1d1e | 2217 | if (bytes_left > max_sendlen) { |
wolfSSL | 16:8e0d178b1d1e | 2218 | printf("Send limited by buffer\r\n"); |
wolfSSL | 16:8e0d178b1d1e | 2219 | bytes_left = max_sendlen; |
wolfSSL | 16:8e0d178b1d1e | 2220 | } |
wolfSSL | 16:8e0d178b1d1e | 2221 | if (bytes_left == 0) { |
wolfSSL | 16:8e0d178b1d1e | 2222 | printf("Buffer full!\r\n"); |
wolfSSL | 16:8e0d178b1d1e | 2223 | break; |
wolfSSL | 16:8e0d178b1d1e | 2224 | } |
wolfSSL | 16:8e0d178b1d1e | 2225 | ret = tcp_socket_send(&ctx->conn.tcp, (unsigned char *)buf + total_written, bytes_left); |
wolfSSL | 16:8e0d178b1d1e | 2226 | if (ret <= 0) |
wolfSSL | 16:8e0d178b1d1e | 2227 | break; |
wolfSSL | 16:8e0d178b1d1e | 2228 | total_written += ret; |
wolfSSL | 16:8e0d178b1d1e | 2229 | } while(total_written < sz); |
wolfSSL | 16:8e0d178b1d1e | 2230 | if (total_written == 0) |
wolfSSL | 16:8e0d178b1d1e | 2231 | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
wolfSSL | 16:8e0d178b1d1e | 2232 | return total_written; |
wolfSSL | 16:8e0d178b1d1e | 2233 | } |
wolfSSL | 16:8e0d178b1d1e | 2234 | |
wolfSSL | 16:8e0d178b1d1e | 2235 | int uIPSendTo(WOLFSSL* ssl, char* buf, int sz, void* _ctx) |
wolfSSL | 16:8e0d178b1d1e | 2236 | { |
wolfSSL | 16:8e0d178b1d1e | 2237 | uip_wolfssl_ctx *ctx = (struct uip_wolfssl_ctx *)_ctx; |
wolfSSL | 16:8e0d178b1d1e | 2238 | int ret = 0; |
wolfSSL | 16:8e0d178b1d1e | 2239 | (void)ssl; |
wolfSSL | 16:8e0d178b1d1e | 2240 | ret = udp_socket_sendto(&ctx->conn.udp, (unsigned char *)buf, sz, &ctx->peer_addr, ctx->peer_port ); |
wolfSSL | 16:8e0d178b1d1e | 2241 | if (ret == 0) |
wolfSSL | 16:8e0d178b1d1e | 2242 | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
wolfSSL | 16:8e0d178b1d1e | 2243 | return ret; |
wolfSSL | 16:8e0d178b1d1e | 2244 | } |
wolfSSL | 16:8e0d178b1d1e | 2245 | |
wolfSSL | 16:8e0d178b1d1e | 2246 | /* The uIP uTCP/IP receive callback |
wolfSSL | 16:8e0d178b1d1e | 2247 | * return : nb bytes read, or error |
wolfSSL | 16:8e0d178b1d1e | 2248 | */ |
wolfSSL | 16:8e0d178b1d1e | 2249 | int uIPReceive(WOLFSSL *ssl, char *buf, int sz, void *_ctx) |
wolfSSL | 16:8e0d178b1d1e | 2250 | { |
wolfSSL | 16:8e0d178b1d1e | 2251 | uip_wolfssl_ctx *ctx = (uip_wolfssl_ctx *)_ctx; |
wolfSSL | 16:8e0d178b1d1e | 2252 | if (!ctx || !ctx->ssl_rx_databuf) |
wolfSSL | 16:8e0d178b1d1e | 2253 | return -1; |
wolfSSL | 16:8e0d178b1d1e | 2254 | (void)ssl; |
wolfSSL | 16:8e0d178b1d1e | 2255 | if (ctx->ssl_rb_len > 0) { |
wolfSSL | 16:8e0d178b1d1e | 2256 | if (sz > ctx->ssl_rb_len - ctx->ssl_rb_off) |
wolfSSL | 16:8e0d178b1d1e | 2257 | sz = ctx->ssl_rb_len - ctx->ssl_rb_off; |
wolfSSL | 16:8e0d178b1d1e | 2258 | XMEMCPY(buf, ctx->ssl_rx_databuf + ctx->ssl_rb_off, sz); |
wolfSSL | 16:8e0d178b1d1e | 2259 | ctx->ssl_rb_off += sz; |
wolfSSL | 16:8e0d178b1d1e | 2260 | if (ctx->ssl_rb_off >= ctx->ssl_rb_len) { |
wolfSSL | 16:8e0d178b1d1e | 2261 | ctx->ssl_rb_len = 0; |
wolfSSL | 16:8e0d178b1d1e | 2262 | ctx->ssl_rb_off = 0; |
wolfSSL | 16:8e0d178b1d1e | 2263 | } |
wolfSSL | 16:8e0d178b1d1e | 2264 | return sz; |
wolfSSL | 16:8e0d178b1d1e | 2265 | } else { |
wolfSSL | 16:8e0d178b1d1e | 2266 | return WOLFSSL_CBIO_ERR_WANT_READ; |
wolfSSL | 16:8e0d178b1d1e | 2267 | } |
wolfSSL | 16:8e0d178b1d1e | 2268 | } |
wolfSSL | 16:8e0d178b1d1e | 2269 | |
wolfSSL | 16:8e0d178b1d1e | 2270 | /* uIP DTLS Generate Cookie callback |
wolfSSL | 16:8e0d178b1d1e | 2271 | * return : number of bytes copied into buf, or error |
wolfSSL | 16:8e0d178b1d1e | 2272 | */ |
wolfSSL | 16:8e0d178b1d1e | 2273 | int uIPGenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *_ctx) |
wolfSSL | 16:8e0d178b1d1e | 2274 | { |
wolfSSL | 16:8e0d178b1d1e | 2275 | uip_wolfssl_ctx *ctx = (uip_wolfssl_ctx *)_ctx; |
wolfSSL | 16:8e0d178b1d1e | 2276 | byte token[32]; |
wolfSSL | 16:8e0d178b1d1e | 2277 | byte digest[WC_SHA_DIGEST_SIZE]; |
wolfSSL | 16:8e0d178b1d1e | 2278 | int ret = 0; |
wolfSSL | 16:8e0d178b1d1e | 2279 | XMEMSET(token, 0, sizeof(token)); |
wolfSSL | 16:8e0d178b1d1e | 2280 | XMEMCPY(token, &ctx->peer_addr, sizeof(uip_ipaddr_t)); |
wolfSSL | 16:8e0d178b1d1e | 2281 | XMEMCPY(token + sizeof(uip_ipaddr_t), &ctx->peer_port, sizeof(word16)); |
wolfSSL | 16:8e0d178b1d1e | 2282 | ret = wc_ShaHash(token, sizeof(uip_ipaddr_t) + sizeof(word16), digest); |
wolfSSL | 16:8e0d178b1d1e | 2283 | if (ret != 0) |
wolfSSL | 16:8e0d178b1d1e | 2284 | return ret; |
wolfSSL | 16:8e0d178b1d1e | 2285 | if (sz > WC_SHA_DIGEST_SIZE) |
wolfSSL | 16:8e0d178b1d1e | 2286 | sz = WC_SHA_DIGEST_SIZE; |
wolfSSL | 16:8e0d178b1d1e | 2287 | XMEMCPY(buf, digest, sz); |
wolfSSL | 16:8e0d178b1d1e | 2288 | return sz; |
wolfSSL | 16:8e0d178b1d1e | 2289 | } |
wolfSSL | 16:8e0d178b1d1e | 2290 | |
wolfSSL | 16:8e0d178b1d1e | 2291 | #endif /* WOLFSSL_UIP */ |
wolfSSL | 16:8e0d178b1d1e | 2292 | |
wolfSSL | 16:8e0d178b1d1e | 2293 | #ifdef WOLFSSL_GNRC |
wolfSSL | 16:8e0d178b1d1e | 2294 | |
wolfSSL | 16:8e0d178b1d1e | 2295 | #include <net/sock.h> |
wolfSSL | 16:8e0d178b1d1e | 2296 | #include <net/sock/tcp.h> |
wolfSSL | 16:8e0d178b1d1e | 2297 | #include <stdio.h> |
wolfSSL | 16:8e0d178b1d1e | 2298 | |
wolfSSL | 16:8e0d178b1d1e | 2299 | /* GNRC TCP/IP port, using the native tcp/udp socket api. |
wolfSSL | 16:8e0d178b1d1e | 2300 | * TCP and UDP are currently supported with the callbacks below. |
wolfSSL | 16:8e0d178b1d1e | 2301 | * |
wolfSSL | 16:8e0d178b1d1e | 2302 | */ |
wolfSSL | 16:8e0d178b1d1e | 2303 | /* The GNRC tcp send callback |
wolfSSL | 16:8e0d178b1d1e | 2304 | * return : bytes sent, or error |
wolfSSL | 16:8e0d178b1d1e | 2305 | */ |
wolfSSL | 16:8e0d178b1d1e | 2306 | |
wolfSSL | 16:8e0d178b1d1e | 2307 | int GNRC_SendTo(WOLFSSL* ssl, char* buf, int sz, void* _ctx) |
wolfSSL | 16:8e0d178b1d1e | 2308 | { |
wolfSSL | 16:8e0d178b1d1e | 2309 | sock_tls_t *ctx = (sock_tls_t *)_ctx; |
wolfSSL | 16:8e0d178b1d1e | 2310 | int ret = 0; |
wolfSSL | 16:8e0d178b1d1e | 2311 | (void)ssl; |
wolfSSL | 16:8e0d178b1d1e | 2312 | if (!ctx) |
wolfSSL | 16:8e0d178b1d1e | 2313 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 16:8e0d178b1d1e | 2314 | ret = sock_udp_send(&ctx->conn.udp, (unsigned char *)buf, sz, &ctx->peer_addr); |
wolfSSL | 16:8e0d178b1d1e | 2315 | if (ret == 0) |
wolfSSL | 16:8e0d178b1d1e | 2316 | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
wolfSSL | 16:8e0d178b1d1e | 2317 | return ret; |
wolfSSL | 16:8e0d178b1d1e | 2318 | } |
wolfSSL | 16:8e0d178b1d1e | 2319 | |
wolfSSL | 16:8e0d178b1d1e | 2320 | /* The GNRC TCP/IP receive callback |
wolfSSL | 16:8e0d178b1d1e | 2321 | * return : nb bytes read, or error |
wolfSSL | 16:8e0d178b1d1e | 2322 | */ |
wolfSSL | 16:8e0d178b1d1e | 2323 | int GNRC_ReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *_ctx) |
wolfSSL | 16:8e0d178b1d1e | 2324 | { |
wolfSSL | 16:8e0d178b1d1e | 2325 | sock_udp_ep_t ep; |
wolfSSL | 16:8e0d178b1d1e | 2326 | int ret; |
wolfSSL | 16:8e0d178b1d1e | 2327 | uint32_t timeout = wolfSSL_dtls_get_current_timeout(ssl) * 1000000; |
wolfSSL | 16:8e0d178b1d1e | 2328 | sock_tls_t *ctx = (sock_tls_t *)_ctx; |
wolfSSL | 16:8e0d178b1d1e | 2329 | if (!ctx) |
wolfSSL | 16:8e0d178b1d1e | 2330 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 16:8e0d178b1d1e | 2331 | (void)ssl; |
wolfSSL | 16:8e0d178b1d1e | 2332 | if (wolfSSL_get_using_nonblock(ctx->ssl)) { |
wolfSSL | 16:8e0d178b1d1e | 2333 | timeout = 0; |
wolfSSL | 16:8e0d178b1d1e | 2334 | } |
wolfSSL | 16:8e0d178b1d1e | 2335 | ret = sock_udp_recv(&ctx->conn.udp, buf, sz, timeout, &ep); |
wolfSSL | 16:8e0d178b1d1e | 2336 | if (ret > 0) { |
wolfSSL | 16:8e0d178b1d1e | 2337 | if (ctx->peer_addr.port == 0) |
wolfSSL | 16:8e0d178b1d1e | 2338 | XMEMCPY(&ctx->peer_addr, &ep, sizeof(sock_udp_ep_t)); |
wolfSSL | 16:8e0d178b1d1e | 2339 | } |
wolfSSL | 16:8e0d178b1d1e | 2340 | if (ret == -ETIMEDOUT) { |
wolfSSL | 16:8e0d178b1d1e | 2341 | return WOLFSSL_CBIO_ERR_WANT_READ; |
wolfSSL | 16:8e0d178b1d1e | 2342 | } |
wolfSSL | 16:8e0d178b1d1e | 2343 | return ret; |
wolfSSL | 16:8e0d178b1d1e | 2344 | } |
wolfSSL | 16:8e0d178b1d1e | 2345 | |
wolfSSL | 16:8e0d178b1d1e | 2346 | /* GNRC DTLS Generate Cookie callback |
wolfSSL | 16:8e0d178b1d1e | 2347 | * return : number of bytes copied into buf, or error |
wolfSSL | 16:8e0d178b1d1e | 2348 | */ |
wolfSSL | 16:8e0d178b1d1e | 2349 | #define GNRC_MAX_TOKEN_SIZE (32) |
wolfSSL | 16:8e0d178b1d1e | 2350 | int GNRC_GenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *_ctx) |
wolfSSL | 16:8e0d178b1d1e | 2351 | { |
wolfSSL | 16:8e0d178b1d1e | 2352 | sock_tls_t *ctx = (sock_tls_t *)_ctx; |
wolfSSL | 16:8e0d178b1d1e | 2353 | if (!ctx) |
wolfSSL | 16:8e0d178b1d1e | 2354 | return WOLFSSL_CBIO_ERR_GENERAL; |
wolfSSL | 16:8e0d178b1d1e | 2355 | byte token[GNRC_MAX_TOKEN_SIZE]; |
wolfSSL | 16:8e0d178b1d1e | 2356 | byte digest[WC_SHA_DIGEST_SIZE]; |
wolfSSL | 16:8e0d178b1d1e | 2357 | int ret = 0; |
wolfSSL | 16:8e0d178b1d1e | 2358 | size_t token_size = sizeof(sock_udp_ep_t); |
wolfSSL | 16:8e0d178b1d1e | 2359 | (void)ssl; |
wolfSSL | 16:8e0d178b1d1e | 2360 | if (token_size > GNRC_MAX_TOKEN_SIZE) |
wolfSSL | 16:8e0d178b1d1e | 2361 | token_size = GNRC_MAX_TOKEN_SIZE; |
wolfSSL | 16:8e0d178b1d1e | 2362 | XMEMSET(token, 0, GNRC_MAX_TOKEN_SIZE); |
wolfSSL | 16:8e0d178b1d1e | 2363 | XMEMCPY(token, &ctx->peer_addr, token_size); |
wolfSSL | 16:8e0d178b1d1e | 2364 | ret = wc_ShaHash(token, token_size, digest); |
wolfSSL | 16:8e0d178b1d1e | 2365 | if (ret != 0) |
wolfSSL | 16:8e0d178b1d1e | 2366 | return ret; |
wolfSSL | 16:8e0d178b1d1e | 2367 | if (sz > WC_SHA_DIGEST_SIZE) |
wolfSSL | 16:8e0d178b1d1e | 2368 | sz = WC_SHA_DIGEST_SIZE; |
wolfSSL | 16:8e0d178b1d1e | 2369 | XMEMCPY(buf, digest, sz); |
wolfSSL | 16:8e0d178b1d1e | 2370 | return sz; |
wolfSSL | 16:8e0d178b1d1e | 2371 | } |
wolfSSL | 16:8e0d178b1d1e | 2372 | |
wolfSSL | 16:8e0d178b1d1e | 2373 | #endif /* WOLFSSL_GNRC */ |
wolfSSL | 16:8e0d178b1d1e | 2374 | |
wolfSSL | 14:167253f4e170 | 2375 | #endif /* WOLFCRYPT_ONLY */ |
wolfSSL | 14:167253f4e170 | 2376 |