Take LwIP native TCP socket into io.c

This commit is contained in:
Takashi Kojo 2014-02-03 09:18:48 +09:00
parent 80cf1b20b3
commit e39308f059
13 changed files with 1323 additions and 3692 deletions

View File

@ -24,29 +24,22 @@
#endif
#include <cyassl/ctaocrypt/settings.h>
#if defined(CYASSL_MDK_ARM)
#include <stdio.h>
#include <string.h>
#include <rtl.h>
#include "cyassl_MDK_ARM.h"
#endif
#if defined(HAVE_LWIP_NATIVE)
#if defined(CYASSL_IAR_ARM)
#include <stdio.h>
#include <string.h>
#endif
#if defined(CYASSL_LWIP)
#include "lwip/tcp.h"
#include "lwip/sockets.h"
#endif
#include <cyassl/ctaocrypt/settings.h>
#include <cyassl/ssl.h>
#include <cyassl/internal.h>
#include <cyassl/ctaocrypt/memory.h>
#include "SSL-NB.h"
#include "HTTPS-NB.h"
#include "https-nb.h"
#if 0
/*Enable debug*/
@ -58,65 +51,69 @@
#endif
#define ERR_PRINTF(x, ...) printf("[HTTPSClient:ERROR]"x"\r\n", ##__VA_ARGS__);
static int LwIP_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 *cb, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
static err_t DataReceiveCallback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
struct pbuf *next ;
CYASSL_NB *ssl_nb ;
ssl_nb = (CYASSL_NB *)cb ;
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_nb->arg) == WAITING) {
*(enum HTTPS_Stat *)(ssl_nb->arg) = HTTP_RECEIVE ;
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_nb->pbuf) {
next = ssl_nb->pbuf ;
if(ssl->lwipCtx.pbuf) {
next = ssl->lwipCtx.pbuf ;
while(1) {
DBG_PRINTF("pbuf=%x, pbuf->next=%x, ",ssl_nb->pbuf, next) ;
DBG_PRINTF("pbuf=%x, pbuf->next=%x, ",ssl->lwipCtx.pbuf, next) ;
if(next->next)
next = next->next ;
else break ;
}
next->next = p ;
ssl_nb->pbuf->tot_len += p->tot_len ;
ssl->lwipCtx.pbuf->tot_len += p->tot_len ;
} else {
ssl_nb->pbuf = p ;
ssl->lwipCtx.pbuf = p ;
}
ssl_nb->pulled = 0 ;
ssl->lwipCtx.pulled = 0 ;
if(ssl_nb->wait < 0)
ssl_nb->wait = 1000 ;
ssl_nb->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,
@ -129,30 +126,27 @@ void CyaSSL_HTTPS_Client_NB_init(void *nb,
https_nb->serverPort = svPort ;
https_nb->hostname = host ;
https_nb->path = path ;
https_nb->stat = BEGIN ;
}
int CyaSSL_HTTPS_Client_NB(void *nb)
{
char *p ;
int ret ;
CYASSL_HTTPS_NB *https_nb ;
CYASSL_NB *ssl_nb ;
https_nb = (CYASSL_HTTPS_NB *)nb ;
ssl_nb = (CYASSL_NB *)&(https_nb->ssl_nb) ;
/*CyaSSL_Debugging_ON() ; */
CyaSSL_Debugging_ON() ;
switch(https_nb->stat) {
case BEGIN:
printf("======= LwIP: HTTPS Client Test(%x): %d =========\n", nb, count ++) ;
p = (char *)malloc(1) ;
printf("Warter mark: %x\n", p) ;
free(p) ;
{
void * p ;
p = (void *)malloc(1) ;
printf("Watermark=%x\n", p) ;
free(p) ;
}
/*** Assuming LwIP has been initialized ***/
https_nb->stat = INITIALIZED ;
case INITIALIZED:
@ -165,11 +159,11 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
https_nb->stat = IDLE ;
return !ERR_OK ;
}
tcp_arg(https_nb->pcb, (void *)&https_nb->stat) ;
https_nb->localPort = getPort() ;
printf("local Port=%d\n", https_nb->localPort) ;
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) {
@ -182,15 +176,14 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
}
case TCP_CONNECT:
DBG_PRINTF("LwIPtest: TCP_CONNECT\n") ;
if(LwIP_cb_mutex)return ERR_OK ;
else LwIP_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) ;
if(CyaSSL_cb_mutex)return ERR_OK ;
else CyaSSL_cb_mutex = 1 ;
ret = tcp_connect(https_nb->pcb, &(https_nb->serverIP_em),
https_nb->serverPort, DataConnectedCallback);
@ -204,80 +197,186 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
}
case TCP_CONNECTED:
printf("LwIPtest: TCP CONNECTED\n") ;
CyaSSL_cb_mutex = 0 ;
ret = CyaSSL_init_NB(&(https_nb->ssl_nb), https_nb->pcb) ;
if(ret != ERR_OK) {
https_nb->stat = TCP_CLOSE ;
printf("LwIPtest: TCP CONNECTED(%x)\n", https_nb) ;
LwIP_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 ;
}
https_nb->stat = SSL_CONN ;
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 */
ret = CyaSSL_connecting_NB(&(https_nb->ssl_nb)) ;
if(ret == SSL_NB_ERROR) {
ERR_PRINTF("CyaSSL_connecting, ret=%d\n", ret) ;
https_nb->stat = SSL_CLOSE ;
return !ERR_OK ;
} else if(ret == SSL_NB_CONNECTED)
https_nb->stat = HTTP_SEND ;
else return ERR_OK;
if(LwIP_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 ;
LwIP_cb_mutex = 0 ;
}
return ERR_OK ;
case HTTP_SEND:
{
{
#define SEND_BUFF_SIZE 100
char sendBuff[SEND_BUFF_SIZE] ;
int size ;
printf("LwIPtest: SSL CONNECTED\n") ;
CyaSSL_NB_setCallbackArg(ssl_nb, &(https_nb->stat)) ;
if(LwIP_cb_mutex)return ERR_OK ;
else LwIP_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\n") ;
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) ;
if(CyaSSL_cb_mutex)return ERR_OK ;
else CyaSSL_cb_mutex = 1 ; /* lock */
CyaSSL_write(ssl_nb->ssl, sendBuff, size) ;
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] ;
LwIP_cb_mutex = 0 ;
memset(httpbuff, '\0', HTTP_BUFF_SIZE) ;
ret = CyaSSL_read(ssl_nb->ssl, httpbuff, HTTP_BUFF_SIZE) ;
printf("LwIPtest: HTTPS GET, Received(%d)\n",strlen(httpbuff)) ;
CyaSSL_cb_mutex = 0 ;
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_close_NB(ssl_nb) ;
}
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 ;
return ERR_OK ;
case 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;
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
*/
/*********************************************************************/
#ifndef NO_MAIN_DRIVER
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) ;
}
}
#endif /* NO_MAIN_DRIVER */
#endif /* HAVE_LWIP_NATIVE */

View File

@ -18,6 +18,9 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifndef HTTPS_NB_H
#define HTTPS_NB_H
extern int CyaSSL_GetDataFromPbuf(char *buff, struct pbuf *p, int size) ;
@ -37,6 +40,7 @@ enum HTTPS_Stat {
TCP_CONNECTED,
SSL_INIT,
SSL_CONN,
SSL_CONN_WAITING,
HTTP_SEND,
HTTP_RECEIVE,
HTTP_DONE,
@ -50,7 +54,8 @@ enum HTTPS_Stat {
#define HTTPS_PORT 443
typedef struct {
CYASSL_NB ssl_nb ;
CYASSL *ssl ;
CYASSL_CTX *ctx ;
enum HTTPS_Stat stat ;
struct tcp_pcb * pcb ;
unsigned long ipaddress ;
@ -61,8 +66,11 @@ typedef struct {
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) ;
extern int CyaSSL_HTTPS_Client_NB(void *nb) ;
#endif

View File

@ -1,96 +0,0 @@
/* HTTPS-MAIN.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 <config.h>
#endif
#include "lwip/tcp.h"
#include "lwip/sockets.h"
#include <cyassl/ctaocrypt/settings.h>
#include <cyassl/ssl.h>
#include <cyassl/ctaocrypt/memory.h>
#include "SSL-NB.h"
#include "HTTPS-NB.h"
#include "HTTPS-main.h"
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 ;
extern void CyaSSL_HTTPS_Client_NB_init(void *nb,
struct ip_addr svIP, unsigned long svPort, char *host, char *path) ;
#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) } ;
static int i = 0 ;
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(void)
{
if(i++ < 10000)return ;
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 0
if((i % 5) == 0) { /* wait for initializing TCP/IP, DHCP */
CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_5) ;
}
#endif
}

View File

@ -1,34 +0,0 @@
/* HTTPS-main.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
*/
#ifndef __HTTPS_MAIN_H__
#define __HTTPS_MAIN_H__
extern CYASSL_HTTPS_NB CyaSSL_HTTPS_Client_1 ;
extern CYASSL_HTTPS_NB CyaSSL_HTTPS_Client_2 ;
extern CYASSL_HTTPS_NB CyaSSL_HTTPS_Client_3 ;
extern CYASSL_HTTPS_NB CyaSSL_HTTPS_Client_4 ;
extern CYASSL_HTTPS_NB CyaSSL_HTTPS_Client_5 ;
extern void HTTPS_Clinet_main_init(void) ;
extern void HTTPS_Clinet_main(void) ;
#endif

View File

@ -1,210 +0,0 @@
/* SSL-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 <config.h>
#endif
#include <cyassl/ctaocrypt/settings.h>
#if defined(CYASSL_MDK_ARM)
#include <stdio.h>
#include <string.h>
#include <rtl.h>
#include "cyassl_MDK_ARM.h"
#endif
#if defined(CYASSL_IAR_ARM)
#include <stdio.h>
#include <string.h>
#endif
#if defined(CYASSL_LWIP)
#include "lwip/tcp.h"
#include "lwip/pbuf.h"
#include "lwip/sockets.h"
#endif
#include <cyassl/ctaocrypt/settings.h>
#include <cyassl/ssl.h>
#include <cyassl/internal.h>
#include <SSL-NB.h>
#if 0
/*Enable debug*/
#include <cstdio>
#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("[SSLClient:ERROR]"x"\r\n", ##__VA_ARGS__);
#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
CyaSSL_CALLBACK_MUTEX CyaSSL_cb_mutex = 0 ;
static err_t CyaSSL_connectCallback(void *cb, struct tcp_pcb *pcb, struct pbuf *p, s8_t err)
{
struct pbuf *next ;
CYASSL_NB *ssl_nb ;
ssl_nb = (CYASSL_NB *)cb ;
if((cb == NULL)||(pcb == NULL))
ERR_PRINTF("CyaSSL_connectCallBack, cb=%x, pcb=%d\n", cb, pcb) ;
if(p && (err == 0)) {
printf("pbuf=%x\n", p) ;
DBG_PRINTF_CB("LwIPtest: CyaSSL connect, started(CyaSSL_connectCallBack1), 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_nb->pbuf) {
next = ssl_nb->pbuf ;
while(1) {
if(next->next)
next = next->next ;
else break ;
}
next->next = p ;
ssl_nb->pbuf->tot_len += p->tot_len ;
} else {
ssl_nb->pbuf = p ;
}
ssl_nb->pulled = 0 ;
if(ssl_nb->wait < 0)
ssl_nb->wait = 10000 ;
return ERR_OK;
}
static 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_init_NB(CYASSL_NB *nb, struct tcp_pcb * pcb)
{
CYASSL_NB *ssl_nb ;
ssl_nb = nb ;
/*CyaSSLv3_client_method()
CyaTLSv1_client_method()
CyaTLSv1_1_client_method()
CyaTLSv1_2_client_method() */
ssl_nb->ctx = CyaSSL_CTX_new(CyaTLSv1_2_client_method());
if (ssl_nb->ctx == NULL) {
ERR_PRINTF("CyaSSL_CTX_new: unable to get ctx");
return !ERR_OK ;
}
CyaSSL_CTX_set_verify(ssl_nb->ctx, SSL_VERIFY_NONE, 0);
ssl_nb->ssl = CyaSSL_new(ssl_nb->ctx);
if (ssl_nb->ssl == NULL) {
ERR_PRINTF("CyaSSL_new: unable to get SSL object");
return !ERR_OK ;
}
ssl_nb->pcb = pcb ;
ssl_nb->pbuf = NULL ;
ssl_nb->pulled = 0 ;
ssl_nb->stat = SSL_NB_CONN ;
/* set up callbacks */
CyaSSL_SetIOReadCtx (ssl_nb->ssl, (void *)ssl_nb) ;
CyaSSL_SetIOWriteCtx(ssl_nb->ssl, (void *)ssl_nb) ;
tcp_recv(ssl_nb->pcb, CyaSSL_connectCallback);
tcp_sent(ssl_nb->pcb, DataSentCallback);
tcp_arg(ssl_nb->pcb, (void *)ssl_nb) ;
CyaSSL_SetVersion(ssl_nb->ssl, CYASSL_TLSV1_2) ;
CyaSSL_set_using_nonblock(ssl_nb->ssl, (0==0)) ;
/* Non-blocking CyaSSL_connect */
DBG_PRINTF("Return CyaSSL_init_NB = %x\n", ssl_nb) ;
return ERR_OK ;
}
/*** Non-Bloking Cyassl_connect, ... */
/* to be called in infinit loop ***/
int CyaSSL_connecting_NB(CYASSL_NB *ssl_nb)
{
int ret ;
switch(ssl_nb->stat) {
case SSL_NB_CONN:
if(CyaSSL_cb_mutex)return SSL_NB_WAITING ;
ret = CyaSSL_connect(ssl_nb->ssl);
DBG_PRINTF("LwIPtest: SSL Connecting(CyaSSL_connect), ret = %d\n", ret) ;
if(ret == SSL_CONNECT_WAITING) {
if(CyaSSL_cb_mutex)
return SSL_NB_WAITING ;
else CyaSSL_cb_mutex = 1 ; /* lock */
ssl_nb->wait = -1 ; /* wait until first callback */
ssl_nb->stat = SSL_NB_WAITING ;
return SSL_NB_CONNECTING ;
} else if(ret == SSL_CONNECTING) {
return SSL_NB_CONNECTING ;
} else if(ret == SSL_SUCCESS) {
ssl_nb->stat = SSL_NB_WAITING ;
DBG_PRINTF("LwIPtest: SSL Connected\n") ;
return SSL_NB_CONNECTED ;
} else {
ret = CyaSSL_get_error(ssl_nb->ssl, NULL) ;
ssl_nb->stat = SSL_NB_WAITING ;
return SSL_NB_CONNECTING ;
}
case SSL_NB_WAITING:
if(ssl_nb->wait-- == 0) { /* counting down after the callback
for multiple callbacks */
ssl_nb->stat = SSL_NB_CONN ;
CyaSSL_cb_mutex = 0 ;
}
return SSL_NB_CONNECTING ;
default:
return SSL_NB_ERROR ;
}
}
/** disconnect */
int CyaSSL_close_NB(CYASSL_NB *ssl_nb)
{
CyaSSL_shutdown(ssl_nb->ssl);
CyaSSL_free(ssl_nb->ssl);
CyaSSL_CTX_free(ssl_nb->ctx);
ssl_nb->stat = SSL_NB_BEGIN ;
return ERR_OK ;
}
void CyaSSL_NB_setCallbackArg(CYASSL_NB *ssl_nb, void *arg)
{
ssl_nb->arg = arg ;
}

View File

@ -1,68 +0,0 @@
/* SSLcon-NB.h
*
* Copyright (C) 2006-2013 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
*/
#ifndef __SSLCONN_NB_H__
#define __SSLCONN_NB_H__
#define mem_malloc malloc
#define mem_free free
enum SSL_Stat {
SSL_NB_BEGIN,
SSL_NB_WAITING,
SSL_NB_CONN,
} ;
enum SSL_NB_Ret {
SSL_NB_ERROR,
SSL_NB_CONNECTING,
SSL_NB_CONNECTED,
} ;
typedef struct CyaSSL_nb {
CYASSL *ssl ;
CYASSL_CTX *ctx ;
struct tcp_pcb * pcb ;
int pulled ;
struct pbuf *pbuf ;
enum SSL_Stat stat ;
int wait ;
void * arg ; /* arg for application */
int idle_count ;
} CYASSL_NB ;
extern int CyaSSL_init_NB(CYASSL_NB *nb, struct tcp_pcb * pcb) ;
extern int CyaSSL_connecting_NB(CYASSL_NB *ssl_nb) ;
extern int CyaSSL_close_NB(CYASSL_NB *ssl_nb) ;
extern void CyaSSL_NB_setCallbackArg(CYASSL_NB *ssl_nb, void *arg) ;
/* Set it to CYASSL_NB.arg for callback arg */
extern int CyaSSL_write(struct CYASSL *pcbSSL, const void *buffer, int len) ;
extern int CyaSSL_recv(struct CYASSL *pcbSSL, void *buffer, int len, int flg) ;
extern int CyaSSL_read(struct CYASSL *pcbSSL, void *buffer, int len) ;
extern void CyaSSL_PbufFree(struct pbuf * p) ;
typedef int CyaSSL_CALLBACK_MUTEX ;
extern CyaSSL_CALLBACK_MUTEX CyaSSL_cb_mutex ;
#endif

File diff suppressed because it is too large Load Diff

View File

@ -930,7 +930,7 @@
</option>
<option>
<name>IarchiveOutput</name>
<state>C:\ROOT\CyaSSL-Release\LwIP-native\cyassl\IDE\IAR-EWARM\Projects\CyaSSL-Lib\Debug\Exe\CyaSSL-Lib.a</state>
<state>C:\ROOT\CyaSSL-Release\LwIP-Release\cyassl-test\IDE\IAR-EWARM\Projects\CyaSSL-Lib\Debug\Exe\CyaSSL-Lib.a</state>
</option>
</data>
</settings>
@ -1875,13 +1875,7 @@
<group>
<name>Apps</name>
<file>
<name>$PROJ_DIR$\..\..\CyaSSL\HTTPS-maic.c</name>
</file>
<file>
<name>$PROJ_DIR$\..\..\CyaSSL\HTTPS-NB.c</name>
</file>
<file>
<name>$PROJ_DIR$\..\..\CyaSSL\SSL-NB.c</name>
<name>$PROJ_DIR$\..\..\CyaSSL\https-nb.c</name>
</file>
</group>
<group>

View File

@ -964,6 +964,8 @@ int SetCipherList(Suites*, const char* list);
#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 */
@ -1533,8 +1535,7 @@ typedef void (*hmacfp) (CYASSL*, byte*, const byte*, word32, int, int);
/* client connect state for nonblocking restart */
enum ConnectState {
CONNECT_INITIAL = 0,
CONNECT_BEGIN,
CONNECT_BEGIN = 0,
CLIENT_HELLO_SENT,
HELLO_AGAIN, /* HELLO_AGAIN s for DTLS case */
HELLO_AGAIN_REPLY,
@ -1801,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 {
@ -1912,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

File diff suppressed because it is too large Load Diff

View File

@ -675,10 +675,7 @@ enum { /* ssl Constants */
SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN = 103,
SSL_R_SSLV3_ALERT_BAD_CERTIFICATE = 104,
PEM_BUFSIZE = 1024,
/* for non-blocking CyaSSL_connect */
SSL_CONNECTING = 201, /* Ready for the next step */
SSL_CONNECT_WAITING = 202 /* Waiting for the response */
PEM_BUFSIZE = 1024
};
@ -942,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);

View File

@ -1434,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;

117
src/io.c
View File

@ -1050,7 +1050,15 @@ void CyaSSL_SetIO_NetX(CYASSL* ssl, NX_TCP_SOCKET* nxSocket, ULONG waitOption)
#include "lwip/tcp.h"
#include "lwip/pbuf.h"
#include "lwip/sockets.h"
#include "SSL-NB.h"
#if 0
/*Enable debug*/
#include <cstdio>
#define DBG_PRINTF(x, ...) printf("[SSLClient : DBG]"x"\r\n", ##__VA_ARGS__);
#else
/*Disable debug*/
#define DBG_PRINTF(x, ...)
#endif
#if 0
/*Enable debug*/
@ -1060,9 +1068,13 @@ void CyaSSL_SetIO_NetX(CYASSL* ssl, NX_TCP_SOCKET* nxSocket, ULONG waitOption)
#define DBG_PRINTF_CB(x, ...)
#endif
void CyaSSL_PbufFree(struct pbuf *p)
#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;
@ -1072,20 +1084,20 @@ void CyaSSL_PbufFree(struct pbuf *p)
pbuf_free(p);
}
static int CyaSSL_GetDataFromPbuf(char *buff, CYASSL_NB *ssl_nb, int size)
static int CyaSSL_GetDataFromPbuf(char *buff, CYASSL *ssl, int size)
{
struct pbuf *p ;
struct pbuf *p_next ;
int totalLen ;
int skipLen = 0 ;
p = ssl_nb->pbuf ;
if(p->tot_len < (ssl_nb->pulled + size))
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_nb->pulled - skipLen) ){
skipLen = (ssl_nb->pulled - skipLen) ;
if(p->len && p->len > (ssl->lwipCtx.pulled - skipLen) ){
skipLen = (ssl->lwipCtx.pulled - skipLen) ;
break ;
} else {
skipLen += p->len ;
@ -1113,25 +1125,62 @@ static int CyaSSL_GetDataFromPbuf(char *buff, CYASSL_NB *ssl_nb, int size)
p = p_next ;
} else break ;
}
ssl_nb->pulled += totalLen ;
if(ssl_nb->pbuf->tot_len <= ssl_nb->pulled) {
CyaSSL_PbufFree(ssl_nb->pbuf) ;
ssl_nb->pbuf = NULL ;
tcp_recved(ssl_nb->pcb,ssl_nb->pbuf->tot_len) ;
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 ;
CYASSL_NB *ssl_nb ;
ssl_nb = (CYASSL_NB *)cb ;
DBG_PRINTF_CB("CyaSSL_LwIP_Receive: ssl_nb = %x\n", ssl_nb) ;
DBG_PRINTF_CB("CyaSSL_LwIP_Receive: ssl_nb = %x\n", ssl) ;
if(ssl_nb->pbuf) {
DBG_PRINTF_CB("Received Len=%d, Want Len= %d\n", ssl_nb->pbuf->tot_len, sz) ;
ret = CyaSSL_GetDataFromPbuf(buf, ssl_nb, sz) ;
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 {
@ -1143,13 +1192,35 @@ int CyaSSL_LwIP_Receive(CYASSL* ssl, char *buf, int sz, void *cb)
int CyaSSL_LwIP_Send(CYASSL* ssl, char *buf, int sz, void *cb)
{
CYASSL_NB *ssl_nb ;
ssl_nb = (CYASSL_NB *)cb ;
DBG_PRINTF_CB("CyaSSL_LwIP_Send: ssl_nb = %x\n", ssl_nb) ;
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) ;
tcp_write(ssl_nb->pcb, buf, sz, TCP_WRITE_FLAG_COPY) ;
return 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 */