Merging into master

This commit is contained in:
Takashi Kojo 2014-02-20 15:58:03 +09:00
commit 75864e6e9c
3 changed files with 442 additions and 0 deletions

View File

@ -0,0 +1,360 @@
/* 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 <config.h>
#endif
#include <cyassl/ctaocrypt/settings.h>
#if defined(HAVE_LWIP_NATIVE)
#if defined(CYASSL_IAR_ARM)
#include <stdio.h>
#include <string.h>
#endif
#include "lwip/tcp.h"
#include "lwip/sockets.h"
#include <cyassl/ctaocrypt/settings.h>
#include <cyassl/ssl.h>
#include <cyassl/internal.h>
#include <cyassl/ctaocrypt/memory.h>
#include "https-nb.h"
#if 0
/*Enable debug*/
#include <cstdio>
#define DBG0_PRINTF(x, ...) printf("[HTTPSClient : DBG]"x"\r\n", ##__VA_ARGS__);
#else
/*Disable debug*/
#define DBG0_PRINTF(x, ...)
#endif
#if 0
/*Enable debug*/
#include <cstdio>
#define DBG_PRINTF(x, ...) printf("[HTTPSClient : DBG]"x"\r\n", ##__VA_ARGS__);
#define ERR_PRINTF(x, ...) printf("[HTTPSClient:ERROR]"x"\r\n", ##__VA_ARGS__);
#else
/*Disable debug*/
#define DBG_PRINTF(x, ...)
#define ERR_PRINTF(x, ...)
#endif
static int LwIP_cb_mutex = 0 ;
static unsigned long localPort = 0 ;
static unsigned long getPort(void) {
return (localPort++ + 0x200) & 0x7fff ;
}
static err_t TcpConnectedCallback (void *arg, struct tcp_pcb *pcb, s8_t err)
{
DBG_PRINTF("TcpConnectedCallback(arg=%x, pcb=%x, err=%x)\n", arg, pcb, err) ;
*(enum HTTPS_Stat *)arg = TCP_CONNECTED ;
return ERR_OK;
}
static err_t DataReceiveCallback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
DBG_PRINTF("DataReceiveCallback, pbuf->len=%d, err=%d\n", p->tot_len , err) ;
if(*(enum HTTPS_Stat *)(arg) == WAITING) {
*(enum HTTPS_Stat *)(arg) = HTTP_RECEIVE ;
return ERR_OK ;
} else 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("== HTTPS Client(%x): %d ==\n", nb, count ++) ;
/*** 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("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(LwIP_cb_mutex)return ERR_OK ;
else LwIP_cb_mutex = 1 ;
DBG_PRINTF("TCP_CONNECT(%x)\n", https_nb) ;
DBG_PRINTF("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, TcpConnectedCallback);
if(ret == ERR_OK) {
https_nb->wait_cnt = 0 ;
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:
DBG0_PRINTF("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 ;
}
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,
DataReceiveCallback, NULL, (void *)&https_nb->stat);
https_nb->stat = SSL_CONN ;
case SSL_CONN: /* handshaking */
if(LwIP_cb_mutex) return ERR_OK ;
ret = CyaSSL_connect(https_nb->ssl);
DBG_PRINTF("CyaSSL_connect, ret = %d\n", ret) ;
if(ret == SSL_SUCCESS) {
DBG_PRINTF("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 = 1000000 ;
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 ;
if(LwIP_cb_mutex)return ERR_OK ;
else LwIP_cb_mutex = 1 ; /* lock */
DBG0_PRINTF("SSL CONNECTED(%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->wait_cnt = 0 ;
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(https_nb->ssl, httpbuff, HTTP_BUFF_SIZE) ;
DBG0_PRINTF("HTTPS GET(%x), Received(%d)\n",https_nb, strlen(httpbuff)) ;
/*DBG0_PRINTF*/puts(httpbuff) ;
}
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 ;
return ERR_OK;
case WAITING:
if(https_nb->wait_cnt++ > 1000000) {
LwIP_cb_mutex = 0 ;
https_nb->wait_cnt = 0 ;
if((https_nb->stat >= SSL_CONN)&&(https_nb->stat < SSL_CLOSE)) {
ERR_PRINTF("Wait Time out, go to CyaSSL close") ;
https_nb->stat = SSL_CLOSE ;
} else {
ERR_PRINTF("Wait Time out, go to Begin") ;
https_nb->stat = TCP_CLOSE ;
}
}
return ERR_OK ;
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 server0_em = { IP_ADDR(192,168,11,9) } ;
static struct ip_addr server1_em = { IP_ADDR(31,13,68,33)} ;
void HTTPSClient_main_init() {
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_1,
//server_em, HTTPS_PORT, "xxx.com", "/") ;
server1_em, HTTPS_PORT, "graph.facebook.com", "/takashikojo") ;
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_2,
server0_em, HTTPS_PORT, "xxx.com", "/") ;
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_3,
server1_em, HTTPS_PORT, "graph.facebook.com", "/takashikojo") ;
//server_em, HTTPS_PORT, "xxx.com", "/") ;
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_4,
server0_em, HTTPS_PORT, "xxx.com", "/") ;
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_5,
server0_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 0
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
}
#endif /* NO_MAIN_DRIVER */
#endif /* HAVE_LWIP_NATIVE */

View File

@ -0,0 +1,76 @@
/* 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
*/
#ifndef HTTPS_NB_H
#define HTTPS_NB_H
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) ;
#endif

View File

@ -1323,6 +1323,9 @@ void InitDecodedCert(DecodedCert* cert, byte* source, word32 inSz, void* heap)
cert->extAuthKeyIdSz = 0;
cert->extSubjKeyIdSrc = NULL;
cert->extSubjKeyIdSz = 0;
#ifdef HAVE_ECC
cert->pkCurveOID = 0;
#endif /* HAVE_ECC */
#endif /* OPENSSL_EXTRA */
#ifdef HAVE_ECC
cert->pkCurveOID = 0;
@ -1536,6 +1539,9 @@ static int GetKey(DecodedCert* cert)
if (CheckCurve(cert->pkCurveOID) < 0)
return ECC_CURVE_OID_E;
#ifdef OPENSSL_EXTRA
cert->pkCurveOID = oid;
#endif /* OPENSSL_EXTRA */
/* key header */
b = cert->source[cert->srcIdx++];