00001
00002
00003
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #if (SSL_USED == 1)
00057
00058
00059 #include <stdio.h>
00060 #include <string.h>
00061
00062 #include "print_funcs.h"
00063 #include "gpio.h"
00064
00065 #include "conf_eth.h"
00066
00067
00068 #include "FreeRTOS.h"
00069 #include "task.h"
00070 #include "semphr.h"
00071 #include "partest.h"
00072 #include "serial.h"
00073
00074
00075
00076 #include "portmacro.h"
00077
00078 #include "BasicSSL.h"
00079
00080
00081 #include "lwip/api.h"
00082 #include "lwip/tcpip.h"
00083 #include "lwip/memp.h"
00084 #include "lwip/stats.h"
00085 #include "lwip/netdb.h"
00086 #include "netif/loopif.h"
00087
00088
00089 #include "ethernet.h"
00090 #include "conf_eth.h"
00091
00092
00093 #include "polarssl/net.h"
00094 #include "polarssl/ssl.h"
00095 #include "polarssl/havege.h"
00096
00097 #define BUF_SIZE 200
00098
00099 #define GET_REQUEST \
00100 "GET /hello/ HTTP/1.1\r\n" \
00101 "Host: MyServerName\r\n\r\n"
00102
00103 #define DEBUG_LEVEL 1
00104
00105
00106
00107 char tmpbuf[100];
00108 #define PRINTF_DBG(...) { \
00109 sprintf(tmpbuf,__VA_ARGS__); \
00110 print_dbg(tmpbuf); \
00111 }\
00112
00113 #define PRINTF_MALLOC(x) print_dbg("Malloc addr: 0x"); \
00114 print_dbg_hex((int) x); \
00115 print_dbg("\n"); \
00116
00117 void my_debug(void *ctx, int level, char *str) {
00118 if (level < DEBUG_LEVEL) {
00119 PRINTF_DBG( "%s", str );
00120 }
00121 }
00122
00123 portTASK_FUNCTION( vBasicSSLClient, pvParameters )
00124 {
00125 int ret =0;
00126 int len =0;
00127 int server_fd =0;
00128 unsigned char *buf;
00129 havege_state *hs;
00130 ssl_context *ssl;
00131 ssl_session *ssn;
00132
00133
00134 buf = malloc(sizeof(unsigned char)*BUF_SIZE);
00135 memset( buf, 0, sizeof( unsigned char )*BUF_SIZE );
00136
00137
00138
00139
00140 print_dbg(( "SSL: ! init havedge\n" ));
00141 hs = malloc(sizeof(havege_state));
00142 havege_init( hs );
00143
00144 ssl = malloc(sizeof(ssl_context));
00145 memset( ssl, 0, sizeof( ssl_context ) );
00146
00147 ssn = malloc(sizeof(ssl_session));
00148 memset( ssn, 0, sizeof( ssl_session ) );
00149
00150
00151
00152
00153
00154
00155 do {
00156 print_dbg(( "SSL: net_connect to server\n" ));
00157 PRINTF_DBG( "SSL: Connecting to tcp/%s/%4d...\n", SSL_SERVER_NAME, SSL_SERVER_PORT );
00158 if( ( ret = net_connect( &server_fd, SSL_SERVER_NAME, SSL_SERVER_PORT ) ) != 0 )
00159 {
00160 print_dbg(( "SSL: ! net_connect Fail\n" ));
00161 PRINTF_DBG( "SSL: ! net_connect returned 0x%4x\n", -ret );
00162 }
00163
00164 vTaskDelay(1000);
00165 }
00166 while(ret!=0);
00167
00168 print_dbg(( "SSL: net_connect Pass\n" ));
00169
00170
00171
00172
00173 PRINTF_DBG("SSL: Setting up the SSL/TLS structure...");
00174
00175 if( ( ret = ssl_init( ssl ) ) != 0 )
00176 {
00177 PRINTF_DBG( " failed\n");
00178 PRINTF_DBG( "SSL: ! ssl_init returned 0x%4.4x\n\n", -ret );
00179 goto exit;
00180 }
00181
00182 print_dbg(( " ok\n" ));
00183
00184 ssl_set_endpoint( ssl, SSL_IS_CLIENT );
00185 ssl_set_authmode( ssl, SSL_VERIFY_NONE );
00186
00187 ssl_set_rng( ssl, havege_rand, hs );
00188 ssl_set_dbg( ssl, my_debug, stdout );
00189 ssl_set_bio( ssl, net_recv, &server_fd, net_send, &server_fd );
00190
00191 ssl_set_ciphers( ssl, ssl_default_ciphers );
00192 ssl_set_session( ssl, 1, 600, ssn );
00193
00194
00195
00196
00197 PRINTF_DBG( "SSL: Write ...\n" );
00198 len = sprintf( (char *) buf, GET_REQUEST );
00199
00200 while( ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
00201 {
00202 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
00203 {
00204 PRINTF_DBG( " failed\n");
00205 PRINTF_DBG( "SSL : ! ssl_write returned %x(%x)\n\n", ret,-ret );
00206 goto exit;
00207 }
00208 }
00209
00210 len = ret;
00211 print_dbg("\n--------------------------------------------------------------------\n");
00212 PRINTF_DBG( "SSL %s\n", (char *) buf );
00213 print_dbg("\n--------------------------------------------------------------------\n");
00214 PRINTF_DBG( "SSL: Successfully write %d bytes to server\n", len);
00215
00216
00217
00218
00219 print_dbg( "SSL: Read from server:" );
00220 do
00221 {
00222 len = BUF_SIZE - 1;
00223 memset( buf, 0, sizeof( unsigned char )*BUF_SIZE );
00224 ret = ssl_read( ssl, buf, len );
00225 if( ret == POLARSSL_ERR_NET_TRY_AGAIN ) {
00226 print_dbg("SSL: POLARSSL_ERR_NET_TRY_AGAIN\n");
00227 continue;
00228 }
00229
00230 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ) {
00231 print_dbg("SSL: POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY\n");
00232 break;
00233 }
00234
00235 if( ret <= 0 )
00236 {
00237
00238 break;
00239 }
00240
00241 len = ret;
00242 print_dbg("\n--------------------------------------------------------------------\n");
00243 PRINTF_DBG( "%s", (char *) buf );
00244 print_dbg("\n--------------------------------------------------------------------\n");
00245 PRINTF_DBG("SSL: Successfully read %d bytes from server\n",len);
00246 }
00247 while( 0 );
00248
00249 exit:
00250 print_dbg("SSL: END \n");
00251 vTaskDelay(1000);
00252
00253
00254 net_close( server_fd );
00255
00256
00257 ssl_free( ssl );
00258 memset( ssl, 0, sizeof( ssl ) );
00259 for(;; ) {gpio_clr_gpio_pin(LED3_GPIO);vTaskDelay(250);}
00260
00261 }
00262
00263 #endif