- Main Page
- Data Structures
- Files

# BasicSSL.c

Go to the documentation of this file.

    00001 /* This source file is part of the ATMEL AVR32-SoftwareFramework-1.3.0-AT32UC3A Release */
    00002
    00003 /*This file has been prepared for Doxygen automatic documentation generation.*/
    00017 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
    00018  *
    00019  * Redistribution and use in source and binary forms, with or without
    00020  * modification, are permitted provided that the following conditions are met:
    00021  *
    00022  * 1. Redistributions of source code must retain the above copyright notice, this
    00023  * list of conditions and the following disclaimer.
    00024  *
    00025  * 2. Redistributions in binary form must reproduce the above copyright notice,
    00026  * this list of conditions and the following disclaimer in the documentation
    00027  * and/or other materials provided with the distribution.
    00028  *
    00029  * 3. The name of Atmel may not be used to endorse or promote products derived
    00030  * from this software without specific prior written permission.
    00031  *
    00032  * 4. This software may only be redistributed and used in connection with an Atmel
    00033  * AVR product.
    00034  *
    00035  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
    00036  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00037  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
    00038  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
    00039  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    00040  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    00041  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    00042  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00043  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00044  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
    00045  *
    00046  */
    00047 /*
    00048  Implements a simplistic WEB server.  Every time a connection is made and
    00049  data is received a dynamic page that shows the current FreeRTOS.org kernel
    00050  statistics is generated and returned.  The connection is then closed.
    00051
    00052  This file was adapted from a FreeRTOS lwIP slip demo supplied by a third
    00053  party.
    00054  */
    00055
    00056 #if (SSL_USED == 1)
    00057
    00058 /* Standard includes. */
    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 /* Scheduler includes. */
    00068 #include "FreeRTOS.h"
    00069 #include "task.h"
    00070 #include "semphr.h"
    00071 #include "partest.h"
    00072 #include "serial.h"
    00073
    00074 /* Demo includes. */
    00075 /* Demo app includes. */
    00076 #include "portmacro.h"
    00077
    00078 #include "BasicSSL.h"
    00079
    00080 /* lwIP includes. */
    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 /* ethernet includes */
    00089 #include "ethernet.h"
    00090 #include "conf_eth.h"
    00091
    00092 //polarssl includes
    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 //#include "debug_ssl.h"
    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     //Allocate all Memory
    00134     buf = malloc(sizeof(unsigned char)*BUF_SIZE);
    00135     memset( buf, 0, sizeof( unsigned char )*BUF_SIZE );
    00136
    00137     /*
    00138      * 0. Initialize the RNG and the session data
    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      * 1. Start the connection
    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         // Wait 1s until next retry
    00164         vTaskDelay(1000);
    00165     }
    00166     while(ret!=0);
    00167
    00168     print_dbg(( "SSL: net_connect Pass\n" ));
    00169
    00170     /*
    00171      * 2. Setup stuff
    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      * 3. Write the GET request
    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      * 7. Read the HTTP response
    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     /* Loop forever */
    00254     net_close( server_fd );
    00255
    00256     //This function Cleanup all Memory
    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