From 168985ed9ff78971c8a1cdefafcae844b124c157 Mon Sep 17 00:00:00 2001 From: Takashi Kojo Date: Sun, 2 Feb 2014 18:09:25 +0900 Subject: [PATCH] LwIP native TCP Socket --- IDE/IAR-EWARM/CyaSSL/https-nb.c | 385 ++++ IDE/IAR-EWARM/CyaSSL/https-nb.h | 71 + .../Projects/CyaSSL-Lib/CyaSSL-Lib.ewp | 2012 +++++++++++++++++ .../Projects/CyaSSL-Lib/CyaSSL-Lib.eww | 10 + ctaocrypt/src/random.c | 6 +- cyassl/ctaocrypt/settings.h | 10 + cyassl/internal.h | 23 +- cyassl/ssl.h | 4 +- src/internal.c | 9 + src/io.c | 179 ++ 10 files changed, 2704 insertions(+), 5 deletions(-) create mode 100644 IDE/IAR-EWARM/CyaSSL/https-nb.c create mode 100644 IDE/IAR-EWARM/CyaSSL/https-nb.h create mode 100644 IDE/IAR-EWARM/Projects/CyaSSL-Lib/CyaSSL-Lib.ewp create mode 100644 IDE/IAR-EWARM/Projects/CyaSSL-Lib/CyaSSL-Lib.eww diff --git a/IDE/IAR-EWARM/CyaSSL/https-nb.c b/IDE/IAR-EWARM/CyaSSL/https-nb.c new file mode 100644 index 000000000..6f4da3de7 --- /dev/null +++ b/IDE/IAR-EWARM/CyaSSL/https-nb.c @@ -0,0 +1,385 @@ +/* HTTPS-NB.c + * + * Copyright (C) 2006-2014 wolfSSL Inc. + * + * This file is part of CyaSSL. + * + * CyaSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * CyaSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include + +#if defined(CYASSL_MDK_ARM) + #include + #include + #include + #include "cyassl_MDK_ARM.h" +#endif + +#if defined(CYASSL_IAR_ARM) + #include + #include +#endif + +#if defined(CYASSL_LWIP) +#include "lwip/tcp.h" +#include "lwip/sockets.h" +#endif + +#include +#include +#include +#include +#include "https-nb.h" + +#if 0 +/*Enable debug*/ +#include +#define DBG_PRINTF(x, ...) printf("[HTTPSClient : DBG]"x"\r\n", ##__VA_ARGS__); +#else +/*Disable debug*/ +#define DBG_PRINTF(x, ...) +#endif + +#define ERR_PRINTF(x, ...) printf("[HTTPSClient:ERROR]"x"\r\n", ##__VA_ARGS__); + +static int CyaSSL_cb_mutex = 0 ; + +static unsigned long localPort = 0 ; +static unsigned long getPort(void) { + return (localPort++ + 0x200) & 0x7fff ; +} + +static err_t DataConnectedCallback (void *arg, struct tcp_pcb *pcb, s8_t err) +{ + DBG_PRINTF("DataConnectedCallback(arg=%x, pcb=%x, err=%x)\n", arg, pcb, err) ; + *(enum HTTPS_Stat *)arg = TCP_CONNECTED ; + return ERR_OK; +} + +static err_t DataSentCallback (void *arg, struct tcp_pcb *pcb, u16_t err) +{ + DBG_PRINTF("LwIPtest: Data Sent(SentCallBack1)\n") ; + return ERR_OK; +} + +static err_t DataReceiveCallback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) +{ + struct pbuf *next ; + CYASSL *ssl ; + ssl = (CYASSL *)arg ; + + DBG_PRINTF("LwIPtest: Data Received(DataReceiveCallback), pbuf->len=%d, err=%d\n", p->tot_len , err) ; + + if(p==0) { /* throw away */ + return ERR_OK ; + } + if(*(enum HTTPS_Stat *)(ssl->lwipCtx.arg) == WAITING) { + *(enum HTTPS_Stat *)(ssl->lwipCtx.arg) = HTTP_RECEIVE ; + } else { + CyaSSL_PbufFree(p) ; + tcp_recved(pcb,p->tot_len) ; + return ERR_OK ; + } + /* put it into the queue */ + if(ssl->lwipCtx.pbuf) { + next = ssl->lwipCtx.pbuf ; + while(1) { + DBG_PRINTF("pbuf=%x, pbuf->next=%x, ",ssl->lwipCtx.pbuf, next) ; + if(next->next) + next = next->next ; + else break ; + } + next->next = p ; + ssl->lwipCtx.pbuf->tot_len += p->tot_len ; + } else { + ssl->lwipCtx.pbuf = p ; + } + ssl->lwipCtx.pulled = 0 ; + + if(ssl->lwipCtx.wait < 0) + ssl->lwipCtx.wait = 1000 ; + ssl->lwipCtx.pulled = 0 ; + return ERR_OK; +} + +static int count = 0 ; + +void CyaSSL_HTTPS_Client_NB_init(void *nb, + struct ip_addr svIP, unsigned long svPort, char *host, char *path) +{ + CYASSL_HTTPS_NB *https_nb ; + https_nb = (CYASSL_HTTPS_NB *)nb ; + + https_nb->serverIP_em = svIP ; + https_nb->serverPort = svPort ; + https_nb->hostname = host ; + https_nb->path = path ; + https_nb->stat = BEGIN ; +} + +int CyaSSL_HTTPS_Client_NB(void *nb) +{ + int ret ; + CYASSL_HTTPS_NB *https_nb ; + + https_nb = (CYASSL_HTTPS_NB *)nb ; + + CyaSSL_Debugging_ON() ; + + switch(https_nb->stat) { + case BEGIN: + printf("======= LwIP: HTTPS Client Test(%x): %d =========\n", nb, count ++) ; + { + void * p ; + p = (void *)malloc(1) ; + printf("Watermark=%x\n", p) ; + free(p) ; + } + /*** Assuming LwIP has been initialized ***/ + https_nb->stat = INITIALIZED ; + case INITIALIZED: + https_nb->pcb = tcp_new(); + if(https_nb->pcb) { + tcp_arg(https_nb->pcb, (void *)&(https_nb->stat)) ; + DBG_PRINTF("LwIPtest: New PCB(tcp_new=%x), &https->stat=%x\n", https_nb->pcb, &https_nb->stat) ; + } else { + ERR_PRINTF("tcp_new, ret=%d\n", https_nb->pcb) ; + https_nb->stat = IDLE ; + return !ERR_OK ; + } + + tcp_arg(https_nb->pcb, (void *)&https_nb->stat) ; + + https_nb->localPort = getPort() ; + DBG_PRINTF("local Port=%d\n", https_nb->localPort) ; + ret = tcp_bind (https_nb->pcb, &(https_nb->localIP_em), + https_nb->localPort) ; + if(ret == ERR_OK) { + https_nb->stat = TCP_CONNECT ; + return ERR_OK; + } else { + ERR_PRINTF("tcp_bind, ret=%d\n", ret) ; + https_nb->stat = INITIALIZED ; + return !ERR_OK ; + } + + case TCP_CONNECT: + if(CyaSSL_cb_mutex)return ERR_OK ; + else CyaSSL_cb_mutex = 1 ; + DBG_PRINTF("LwIPtest: TCP_CONNECT(%x)\n", https_nb) ; + DBG_PRINTF("LwIPtest: Server IP Addrress(%d.%d.%d.%d)\n", + (*(unsigned long *)&https_nb->serverIP_em&0xff), + (*(unsigned long *)&https_nb->serverIP_em>>8)&0xff, + (*(unsigned long *)&https_nb->serverIP_em>>16)&0xff, + (*(unsigned long *)&https_nb->serverIP_em>>24)&0xff) ; + ret = tcp_connect(https_nb->pcb, &(https_nb->serverIP_em), + https_nb->serverPort, DataConnectedCallback); + + if(ret == ERR_OK) { + https_nb->stat = WAITING ; + return ERR_OK; + } else { + ERR_PRINTF("tcp_connect, ret=%d\n", ret) ; + https_nb->stat = TCP_CLOSE ; + return !ERR_OK; + } + + case TCP_CONNECTED: + printf("LwIPtest: TCP CONNECTED(%x)\n", https_nb) ; + CyaSSL_cb_mutex = 0 ; + + /*CyaSSLv3_client_method() + CyaTLSv1_client_method() + CyaTLSv1_1_client_method() + CyaTLSv1_2_client_method() */ + https_nb->ctx = CyaSSL_CTX_new(CyaTLSv1_2_client_method()); + if (https_nb->ctx == NULL) { + ERR_PRINTF("CyaSSL_CTX_new: unable to get ctx"); + return !ERR_OK ; + } + + CyaSSL_CTX_set_verify(https_nb->ctx, SSL_VERIFY_NONE, 0); + + https_nb->ssl = CyaSSL_new(https_nb->ctx); + if (https_nb->ssl == NULL) { + ERR_PRINTF("CyaSSL_new: unable to get SSL object"); + return !ERR_OK ; + } + + CyaSSL_SetIO_LwIP(https_nb->ssl, https_nb->pcb); + CyaSSL_SetVersion(https_nb->ssl, CYASSL_TLSV1_2) ; + https_nb->stat = SSL_CONN ; + + case SSL_CONN: /* handshaking */ + + if(CyaSSL_cb_mutex) return ERR_OK ; + ret = CyaSSL_connect(https_nb->ssl); + DBG_PRINTF("LwIPtest: SSL Connecting(CyaSSL_connect), ret = %d\n", ret) ; + if(ret == SSL_SUCCESS) { + https_nb->stat = SSL_CONN_WAITING ; + DBG_PRINTF("LwIPtest: SSL Connected\n") ; + https_nb->stat = HTTP_SEND ; + } else { + ret = CyaSSL_get_error(https_nb->ssl, NULL) ; + if(ret == SSL_ERROR_WANT_READ) { + https_nb->ssl->lwipCtx.wait = -1 ; + https_nb->stat = SSL_CONN_WAITING ; + return ERR_OK ; + } else { + ERR_PRINTF("CyaSSL_connecting_NB:ssl=%x, ret=%d\n", https_nb->ssl, ret) ; + return !ERR_OK ; + } + } + return ERR_OK ; + + case SSL_CONN_WAITING: + if(https_nb->ssl->lwipCtx.wait-- == 0) { /* counting down after the callback + for multiple callbacks */ + https_nb->stat = SSL_CONN ; + CyaSSL_cb_mutex = 0 ; + } + return ERR_OK ; + + case HTTP_SEND: + { + #define SEND_BUFF_SIZE 100 + char sendBuff[SEND_BUFF_SIZE] ; + int size ; + if(CyaSSL_cb_mutex)return ERR_OK ; + else CyaSSL_cb_mutex = 1 ; /* lock */ + printf("LwIPtest: SSL CONNECTED(%x)\n", https_nb) ; + CyaSSL_NB_setCallbackArg(https_nb->ssl, &(https_nb->stat)) ; + tcp_sent(https_nb->pcb, DataSentCallback); + tcp_recv(https_nb->pcb, DataReceiveCallback); + + DBG_PRINTF("LwIPtest: HTTPS GET(%x)\n", https_nb) ; + sprintf(sendBuff, + "GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", + https_nb->path, https_nb->hostname) ; + size = strlen((char const *)sendBuff) ; + + CyaSSL_write(https_nb->ssl, sendBuff, size) ; + + https_nb->stat = WAITING ; + return ERR_OK; + } + + case HTTP_RECEIVE: + { + #define HTTP_BUFF_SIZE 2048 + char httpbuff[HTTP_BUFF_SIZE] ; + + CyaSSL_cb_mutex = 0 ; + memset(httpbuff, '\0', HTTP_BUFF_SIZE) ; + ret = CyaSSL_read(https_nb->ssl, httpbuff, HTTP_BUFF_SIZE) ; + printf("LwIPtest: HTTPS GET(%x), Received(%d)\n",https_nb, strlen(httpbuff)) ; + /* puts(httpbuff) ;*/ + puts("===================\n") ; + } + case SSL_CLOSE: + { + CYASSL_CTX *ctx ; ; + + ctx = https_nb->ssl->ctx ; + DBG_PRINTF("CyaSSL_close(%x)", https_nb->ssl) ; + CyaSSL_shutdown(https_nb->ssl); + CyaSSL_free(https_nb->ssl); + CyaSSL_CTX_free(ctx); ; + https_nb->stat = TCP_CLOSE ; + } + case TCP_CLOSE: + tcp_close(https_nb->pcb) ; + + https_nb->idle = 0 ; + https_nb->stat = IDLE ; + + case IDLE: + https_nb->idle ++ ; + if(https_nb->idle > 50000) + https_nb->stat = BEGIN ; + case WAITING: + default: + return ERR_OK; + } +} + +/*********************************************************************/ +/* + Usage Example: + CyaSSL_HTTPS_Client_NB_init + CyaSSL_HTTPS_Client_NB + */ +/*********************************************************************/ + +CYASSL_HTTPS_NB CyaSSL_HTTPS_Client_1 ; +void *CyaSSL_HTTPS_ClientP_1 = (void *)&CyaSSL_HTTPS_Client_1 ; +CYASSL_HTTPS_NB CyaSSL_HTTPS_Client_2 ; +void *CyaSSL_HTTPS_ClientP_2 = (void *)&CyaSSL_HTTPS_Client_2 ; +CYASSL_HTTPS_NB CyaSSL_HTTPS_Client_3 ; +void *CyaSSL_HTTPS_ClientP_3 = (void *)&CyaSSL_HTTPS_Client_3 ; +CYASSL_HTTPS_NB CyaSSL_HTTPS_Client_4 ; +void *CyaSSL_HTTPS_ClientP_4 = (void *)&CyaSSL_HTTPS_Client_4 ; +CYASSL_HTTPS_NB CyaSSL_HTTPS_Client_5 ; +void *CyaSSL_HTTPS_ClientP_5 = (void *)&CyaSSL_HTTPS_Client_5 ; + + +#define HTTPS_PORT 443 +#define IP_ADDR(a,b,c,d) (((a)|((b)<<8)|((c)<<16)|(d)<<24)) +static struct ip_addr server_em = { IP_ADDR(192,168,11,9) } ; + + +void HTTPSClient_main_init() { + + CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_1, + server_em, HTTPS_PORT, "xxx.com", "/") ; + CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_2, + server_em, HTTPS_PORT, "xxx.com", "/") ; + CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_3, + server_em, HTTPS_PORT, "xxx.com", "/") ; + CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_4, + server_em, HTTPS_PORT, "xxx.com", "/") ; + CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_5, + server_em, HTTPS_PORT, "xxx.com", "/") ; +} + +void HTTPSClient_main(int i) +{ + + if((i % 1) == 0) { /* wait for initializing TCP/IP, DHCP */ + CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_1) ; + } + + if((i % 2) == 0) { /* wait for initializing TCP/IP, DHCP */ + CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_2) ; + } + + if((i % 3) == 0) { /* wait for initializing TCP/IP, DHCP */ + CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_3) ; + } + + if((i % 4) == 0) { /* wait for initializing TCP/IP, DHCP */ + CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_4) ; + } + + if((i % 5) == 0) { /* wait for initializing TCP/IP, DHCP */ + CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_5) ; + } + +} \ No newline at end of file diff --git a/IDE/IAR-EWARM/CyaSSL/https-nb.h b/IDE/IAR-EWARM/CyaSSL/https-nb.h new file mode 100644 index 000000000..978ccb028 --- /dev/null +++ b/IDE/IAR-EWARM/CyaSSL/https-nb.h @@ -0,0 +1,71 @@ +/* HTTPS-NB.h + * + * Copyright (C) 2006-2014 wolfSSL Inc. + * + * This file is part of CyaSSL. + * + * CyaSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * CyaSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +extern int CyaSSL_GetDataFromPbuf(char *buff, struct pbuf *p, int size) ; + +#define IP4_LONG_2_ADDR(ipaddr, iplong) \ + (ipaddr)->addr = htonl(((u32_t)(iplong) & 0xFF000000) | \ + ((u32_t)(iplong) & 0xFF0000) | \ + ((u32_t)(iplong) & 0xFF00) | \ + (u32_t)(iplong) & 0xFF) + +#define IP_ADDR(a,b,c,d) (((a)|((b)<<8)|((c)<<16)|(d)<<24)) + +enum HTTPS_Stat { + BEGIN, + GET_MYIP, + INITIALIZED, + TCP_CONNECT, + TCP_CONNECTED, + SSL_INIT, + SSL_CONN, + SSL_CONN_WAITING, + HTTP_SEND, + HTTP_RECEIVE, + HTTP_DONE, + SSL_CLOSE, + TCP_CLOSE, + WAITING, + IDLE +} ; + + +#define HTTPS_PORT 443 + +typedef struct { + CYASSL *ssl ; + CYASSL_CTX *ctx ; + enum HTTPS_Stat stat ; + struct tcp_pcb * pcb ; + unsigned long ipaddress ; + struct ip_addr localIP_em; + unsigned long localPort ; + struct ip_addr serverIP_em ; + unsigned long serverPort ; + char *hostname ; + char *path ; + int idle ; + int wait_cnt ; /* wait tick counter */ +} CYASSL_HTTPS_NB ; + +extern void CyaSSL_HTTPS_Client_NB_init(void *nb, + struct ip_addr svIP, unsigned long svPort, char *host, char *path) ; +extern int CyaSSL_HTTPS_Client_NB(void *nb) ; \ No newline at end of file diff --git a/IDE/IAR-EWARM/Projects/CyaSSL-Lib/CyaSSL-Lib.ewp b/IDE/IAR-EWARM/Projects/CyaSSL-Lib/CyaSSL-Lib.ewp new file mode 100644 index 000000000..c9d8e1473 --- /dev/null +++ b/IDE/IAR-EWARM/Projects/CyaSSL-Lib/CyaSSL-Lib.ewp @@ -0,0 +1,2012 @@ + + + + 2 + + Debug + + ARM + + 1 + + General + 3 + + 22 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ICCARM + 2 + + 29 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + AARM + 2 + + 9 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + OBJCOPY + 0 + + 1 + 1 + 1 + + + + + + + + + CUSTOM + 3 + + + + + + + BICOMP + 0 + + + + BUILDACTION + 1 + + + + + + + ILINK + 0 + + 16 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + IARCHIVE + 0 + + 0 + 1 + 1 + + + + + + + BILINK + 0 + + + + + Release + + ARM + + 0 + + General + 3 + + 22 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ICCARM + 2 + + 29 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + AARM + 2 + + 9 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + OBJCOPY + 0 + + 1 + 1 + 0 + + + + + + + + + CUSTOM + 3 + + + + + + + BICOMP + 0 + + + + BUILDACTION + 1 + + + + + + + ILINK + 0 + + 16 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + IARCHIVE + 0 + + 0 + 1 + 0 + + + + + + + BILINK + 0 + + + + + Apps + + $PROJ_DIR$\..\..\CyaSSL\https-maic.c + + + $PROJ_DIR$\..\..\CyaSSL\https-nb.c + + + + CyaSSL + + $PROJ_DIR$\..\..\..\..\src\internal.c + + + $PROJ_DIR$\..\..\..\..\src\io.c + + + $PROJ_DIR$\..\..\..\..\src\keys.c + + + $PROJ_DIR$\..\..\..\..\src\ssl.c + + + $PROJ_DIR$\..\..\..\..\src\tls.c + + + + wolfCrypt + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\aes.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\arc4.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\asm.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\asn.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\blake2b.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\camellia.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\coding.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\compress.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\des3.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\dh.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\dsa.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\ecc.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\ecc_fp.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\error.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\hc128.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\hmac.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\integer.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\logging.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\md2.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\md4.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\md5.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\memory-orig.c + + Debug + + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\memory.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\misc.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\port.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\pwdbased.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\rabbit.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\random.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\ripemd.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\rsa.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\sha.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\sha256.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\sha512.c + + + $PROJ_DIR$\..\..\..\..\ctaocrypt\src\tfm.c + + + + + diff --git a/IDE/IAR-EWARM/Projects/CyaSSL-Lib/CyaSSL-Lib.eww b/IDE/IAR-EWARM/Projects/CyaSSL-Lib/CyaSSL-Lib.eww new file mode 100644 index 000000000..17ce82d66 --- /dev/null +++ b/IDE/IAR-EWARM/Projects/CyaSSL-Lib/CyaSSL-Lib.eww @@ -0,0 +1,10 @@ + + + + + $WS_DIR$\CyaSSL-Lib.ewp + + + + + diff --git a/ctaocrypt/src/random.c b/ctaocrypt/src/random.c index 03dd75553..f88aab83b 100644 --- a/ctaocrypt/src/random.c +++ b/ctaocrypt/src/random.c @@ -51,7 +51,8 @@ #include #include #else - #if !defined(NO_DEV_RANDOM) && !defined(CYASSL_MDK_ARM) + #if !defined(NO_DEV_RANDOM) && !defined(CYASSL_MDK_ARM) \ + && !defined(CYASSL_IAR_ARM) #include #ifndef EBSNET #include @@ -484,7 +485,8 @@ int GenerateSeed(OS_Seed* os, byte* output, word32 sz) return 0; } -#elif defined(CYASSL_SAFERTOS) || defined(CYASSL_LEANPSK) +#elif defined(CYASSL_SAFERTOS) || defined(CYASSL_LEANPSK) \ + || defined(CYASSL_IAR_ARM) #warning "write a real random seed!!!!, just for testing now" diff --git a/cyassl/ctaocrypt/settings.h b/cyassl/ctaocrypt/settings.h index 22dea06d0..07926942b 100644 --- a/cyassl/ctaocrypt/settings.h +++ b/cyassl/ctaocrypt/settings.h @@ -81,6 +81,8 @@ /* Uncomment next line if using QL SEP settings */ /* #define CYASSL_QL */ +/* Uncomment next line if using LwIP native TCP socket settings */ +/* #define HAVE_LWIP_NATIVE */ #include @@ -114,6 +116,14 @@ #include "nx_api.h" #endif +#if defined(HAVE_LWIP_NATIVE) /* using LwIP native TCP socket */ + #define CYASSL_LWIP + #define NO_WRITEV + #define SINGLE_THREADED + #define CYASSL_USER_IO + #define NO_FILESYSTEM +#endif + #ifdef MICROCHIP_PIC32 #define SIZEOF_LONG_LONG 8 #define SINGLE_THREADED diff --git a/cyassl/internal.h b/cyassl/internal.h index 104a9c1cd..9c903814a 100644 --- a/cyassl/internal.h +++ b/cyassl/internal.h @@ -960,8 +960,13 @@ int SetCipherList(Suites*, const char* list); #ifdef HAVE_NETX CYASSL_LOCAL int NetX_Receive(CYASSL *ssl, char *buf, int sz, void *ctx); CYASSL_LOCAL int NetX_Send(CYASSL *ssl, char *buf, int sz, void *ctx); -#endif /* HAVE_NETX */ - +#endif +#ifdef HAVE_LWIP_NATIVE + CYASSL_LOCAL int CyaSSL_LwIP_Send(CYASSL* ssl, char *buf, int sz, void *cb); + CYASSL_LOCAL int CyaSSL_LwIP_Receive(CYASSL* ssl, char *buf, int sz, void *cb); + CYASSL_LOCAL void CyaSSL_NB_setCallbackArg(CYASSL *ssl, void *arg) ; + CYASSL_LOCAL void CyaSSL_PbufFree(void *p); +#endif /* HAVE_{tcp stack} */ /* CyaSSL Cipher type just points back to SSL */ struct CYASSL_CIPHER { @@ -1797,6 +1802,17 @@ typedef struct DtlsMsg { #endif +#ifdef HAVE_LWIP_NATIVE + /* LwIP native tpc socket context */ + typedef struct LwIP_native_Ctx { + struct tcp_pcb * pcb ; + int pulled ; + struct pbuf *pbuf ; + int wait ; + void * arg ; /* arg for application */ + int idle_count ; + } LwIP_native_Ctx ; +#endif /* CyaSSL ssl type */ struct CYASSL { @@ -1908,6 +1924,9 @@ struct CYASSL { #ifdef HAVE_NETX NetX_Ctx nxCtx; /* NetX IO Context */ #endif +#ifdef HAVE_LWIP_NATIVE + LwIP_native_Ctx lwipCtx; /* NetX IO Context */ +#endif #ifdef SESSION_INDEX int sessionIndex; /* Session's location in the cache. */ #endif diff --git a/cyassl/ssl.h b/cyassl/ssl.h index aa7056246..b791c2a49 100644 --- a/cyassl/ssl.h +++ b/cyassl/ssl.h @@ -939,7 +939,9 @@ CYASSL_API void CyaSSL_SetIOWriteFlags(CYASSL* ssl, int flags); CYASSL_API void CyaSSL_SetIO_NetX(CYASSL* ssl, NX_TCP_SOCKET* nxsocket, ULONG waitoption); #endif - +#ifdef HAVE_LWIP_NATIVE + CYASSL_API int CyaSSL_SetIO_LwIP(CYASSL* ssl, void *pcb); +#endif typedef int (*CallbackGenCookie)(CYASSL* ssl, unsigned char* buf, int sz, void* ctx); CYASSL_API void CyaSSL_CTX_SetGenCookie(CYASSL_CTX*, CallbackGenCookie); diff --git a/src/internal.c b/src/internal.c index d797d75cc..964f7af6a 100644 --- a/src/internal.c +++ b/src/internal.c @@ -397,6 +397,11 @@ int InitSSL_Ctx(CYASSL_CTX* ctx, CYASSL_METHOD* method) ctx->CBIORecv = NetX_Receive; ctx->CBIOSend = NetX_Send; #endif +#ifdef HAVE_LWIP_NATIVE + ctx->CBIORecv = CyaSSL_LwIP_Receive ; + ctx->CBIOSend = CyaSSL_LwIP_Send ; +#endif + ctx->partialWrite = 0; ctx->verifyCallback = 0; @@ -1429,6 +1434,10 @@ int InitSSL(CYASSL* ssl, CYASSL_CTX* ctx) ssl->IOCB_ReadCtx = &ssl->nxCtx; /* default NetX IO ctx, same for read */ ssl->IOCB_WriteCtx = &ssl->nxCtx; /* and write */ #endif +#ifdef HAVE_LWIP_NATIVE + ssl->lwipCtx.pbuf = NULL ; + ssl->lwipCtx.pulled = 0 ; +#endif #ifdef CYASSL_DTLS ssl->IOCB_CookieCtx = NULL; /* we don't use for default cb */ ssl->dtls_expected_rx = MAX_MTU; diff --git a/src/io.c b/src/io.c index 55f28eed3..02ff56803 100644 --- a/src/io.c +++ b/src/io.c @@ -1045,3 +1045,182 @@ void CyaSSL_SetIO_NetX(CYASSL* ssl, NX_TCP_SOCKET* nxSocket, ULONG waitOption) #endif /* HAVE_NETX */ +#ifdef HAVE_LWIP_NATIVE + +#include "lwip/tcp.h" +#include "lwip/pbuf.h" +#include "lwip/sockets.h" + +#if 0 +/*Enable debug*/ +#include +#define DBG_PRINTF(x, ...) printf("[SSLClient : DBG]"x"\r\n", ##__VA_ARGS__); +#else +/*Disable debug*/ +#define DBG_PRINTF(x, ...) +#endif + +#if 0 +/*Enable debug*/ +#define DBG_PRINTF_CB(x, ...) printf("[HTTPSClient : DBG]"x"\r\n", ##__VA_ARGS__); +#else +/*Disable debug*/ +#define DBG_PRINTF_CB(x, ...) +#endif + +#define ERR_PRINTF(x, ...) printf("[SSLClient:ERROR]"x"\r\n", ##__VA_ARGS__); + +void CyaSSL_PbufFree(void *vp) +{ + struct pbuf *p ; + struct pbuf * next; + p = (struct pbuf *) vp ; + while(p->next != NULL) + { + next = p->next; + pbuf_free(p); + p = next; + } + pbuf_free(p); +} + +static int CyaSSL_GetDataFromPbuf(char *buff, CYASSL *ssl, int size) +{ + struct pbuf *p ; + struct pbuf *p_next ; + int totalLen ; + int skipLen = 0 ; + + p = ssl->lwipCtx.pbuf ; + if(p->tot_len < (ssl->lwipCtx.pulled + size)) + return 0 ; + + while(p) { /* skip the part pulled before */ + if(p->len && p->len > (ssl->lwipCtx.pulled - skipLen) ){ + skipLen = (ssl->lwipCtx.pulled - skipLen) ; + break ; + } else { + skipLen += p->len ; + if(p->next) + p = p->next ; + else return 0 ; + } + } + + totalLen = 0 ; + while(p){ + if(p->len) { + if((p->len - skipLen) > (size - totalLen)) { /* buffer full */ + memcpy(&buff[totalLen], (const char *)&(((char *)(p->payload))[skipLen]), size-totalLen) ; + totalLen = size ; + break ; + } else { + memcpy(&buff[totalLen], (const char *)&(((char *)(p->payload))[skipLen]), p->len - skipLen) ; + totalLen += (p->len-skipLen) ; + skipLen = 0 ; + } + } + if(p->next){ + p_next = p->next ; + p = p_next ; + } else break ; + } + ssl->lwipCtx.pulled += totalLen ; + if(ssl->lwipCtx.pbuf->tot_len <= ssl->lwipCtx.pulled) { + CyaSSL_PbufFree(ssl->lwipCtx.pbuf) ; + ssl->lwipCtx.pbuf = NULL ; + tcp_recved(ssl->lwipCtx.pcb,ssl->lwipCtx.pbuf->tot_len) ; + } + return totalLen; +} + +err_t CyaSSL_connectCallback(void *cb, struct tcp_pcb *pcb, struct pbuf *p, s8_t err) +{ + struct pbuf *next ; + CYASSL *ssl ; + ssl = (CYASSL *)cb ; + + if((cb == NULL)||(pcb == NULL)) + ERR_PRINTF("CyaSSL_connectCallBack, cb=%x, pcb=%d\n", cb, pcb) ; + if(p && (err == 0)) { + DBG_PRINTF_CB("CyaSSL_connectCallBack, pbuf=%x, err=%d, tot_len=%d\n", p, err, p->tot_len) ; + }else { + ERR_PRINTF("CyaSSL_connectCallBack, pbuf=%x, err=%d\n", p, err) ; + return ERR_OK; /* don't go to SSL_CONN */ + } + + if(ssl->lwipCtx.pbuf) { + next = ssl->lwipCtx.pbuf ; + while(1) { + if(next->next) + next = next->next ; + else break ; + } + next->next = p ; + ssl->lwipCtx.pbuf->tot_len += p->tot_len ; + } else { + ssl->lwipCtx.pbuf = p ; + } + ssl->lwipCtx.pulled = 0 ; + if(ssl->lwipCtx.wait < 0) + ssl->lwipCtx.wait = 10000 ; + return ERR_OK; +} + +err_t DataSentCallback (void *arg, struct tcp_pcb *pcb, u16_t err) +{ + DBG_PRINTF_CB("LwIPtest: Data Sent(SentCallBack1), err=%d\n", err) ; + return ERR_OK; +} + +int CyaSSL_LwIP_Receive(CYASSL* ssl, char *buf, int sz, void *cb) +{ + int ret ; + DBG_PRINTF_CB("CyaSSL_LwIP_Receive: ssl_nb = %x\n", ssl) ; + + if(ssl->lwipCtx.pbuf) { + DBG_PRINTF_CB("Received Len=%d, Want Len= %d\n", ssl->lwipCtx.pbuf->tot_len, sz) ; + ret = CyaSSL_GetDataFromPbuf(buf, ssl, sz) ; + if(ret == 0) + ret = CYASSL_CBIO_ERR_WANT_READ ; + } else { + DBG_PRINTF_CB("No Received Data\n") ; + ret = CYASSL_CBIO_ERR_WANT_READ ; + } + return ret ; +} + +int CyaSSL_LwIP_Send(CYASSL* ssl, char *buf, int sz, void *cb) +{ + err_t ret ; + + DBG_PRINTF_CB("CyaSSL_LwIP_Send: ssl = %x\n", ssl) ; + DBG_PRINTF_CB("Send buf[0,1,2,3]=%x,%x,%x,%x, sz=%d\n", buf[0], buf[1], buf[2], buf[3], sz) ; + ret = tcp_write(ssl->lwipCtx.pcb, buf, sz, TCP_WRITE_FLAG_COPY) ; + if(ret == ERR_OK) + return sz ; + else { + ERR_PRINTF("Send ssl=%x, ret=%d\n", ssl, ret) ; + return -1 ; + } +} + +void CyaSSL_NB_setCallbackArg(CYASSL *ssl, void *arg) +{ + ssl->lwipCtx.arg = arg ; +} + +int CyaSSL_SetIO_LwIP(CYASSL* ssl, void* pcb) +{ + if (ssl && pcb) { + ssl->lwipCtx.pcb = (struct tcp_pcb *)pcb ; + tcp_recv(pcb, CyaSSL_connectCallback); + tcp_sent(pcb, DataSentCallback); + tcp_arg (pcb, (void *)ssl) ; + } else return BAD_FUNC_ARG ; + return ERR_OK ; +} + +#endif /* HAVE_LWIP_NATIVE */ + +