Example TLS client with wolfSSL, with cert

Dependencies:   EthernetInterface NTPClient SDFileSystem mbed-rtos mbed wolfSSL

Committer:
wolfSSL
Date:
Tue Jul 21 22:49:46 2015 +0000
Revision:
4:ebcf8e2d846a
Parent:
3:25d42ccf2f12
Child:
5:26b87ccd43d1
client-tls without server verification

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:ea20b58bf112 1 /* client-tcp.c
wolfSSL 0:ea20b58bf112 2 *
wolfSSL 0:ea20b58bf112 3 * Copyright (C) 2006-2015 wolfSSL Inc.
wolfSSL 0:ea20b58bf112 4 *
wolfSSL 0:ea20b58bf112 5 * This file is part of wolfSSL. (formerly known as CyaSSL)
wolfSSL 0:ea20b58bf112 6 *
wolfSSL 0:ea20b58bf112 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 0:ea20b58bf112 8 * it under the terms of the GNU General Public License as published by
wolfSSL 0:ea20b58bf112 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 0:ea20b58bf112 10 * (at your option) any later version.
wolfSSL 0:ea20b58bf112 11 *
wolfSSL 0:ea20b58bf112 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 0:ea20b58bf112 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 0:ea20b58bf112 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 0:ea20b58bf112 15 * GNU General Public License for more details.
wolfSSL 0:ea20b58bf112 16 *
wolfSSL 0:ea20b58bf112 17 * You should have received a copy of the GNU General Public License
wolfSSL 0:ea20b58bf112 18 * along with this program; if not, write to the Free Software
wolfSSL 0:ea20b58bf112 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
wolfSSL 0:ea20b58bf112 20 */
wolfSSL 0:ea20b58bf112 21
wolfSSL 0:ea20b58bf112 22 #include "mbed.h"
wolfSSL 0:ea20b58bf112 23 #include "EthernetInterface.h"
wolfSSL 0:ea20b58bf112 24 #include <stdio.h>
wolfSSL 0:ea20b58bf112 25 #include <stdlib.h>
wolfSSL 0:ea20b58bf112 26 #include <string.h>
wolfSSL 2:53d82dd5e556 27 #include <wolfssl/ssl.h> /* wolfSSL security library */
wolfSSL 3:25d42ccf2f12 28 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 2:53d82dd5e556 29 #include <user_settings.h>
wolfSSL 0:ea20b58bf112 30
wolfSSL 0:ea20b58bf112 31 #define MAXDATASIZE (1024*4)
wolfSSL 0:ea20b58bf112 32
wolfSSL 2:53d82dd5e556 33 static int SocketReceive(WOLFSSL* ssl, char *buf, int sz, void *sock)
wolfSSL 2:53d82dd5e556 34 {
wolfSSL 2:53d82dd5e556 35 return ((TCPSocketConnection *)sock)->receive(buf, sz) ;
wolfSSL 2:53d82dd5e556 36 }
wolfSSL 2:53d82dd5e556 37
wolfSSL 2:53d82dd5e556 38 static int SocketSend(WOLFSSL* ssl, char *buf, int sz, void *sock)
wolfSSL 2:53d82dd5e556 39 {
wolfSSL 2:53d82dd5e556 40 return ((TCPSocketConnection *)sock)->send(buf, sz);
wolfSSL 2:53d82dd5e556 41 }
wolfSSL 2:53d82dd5e556 42
wolfSSL 0:ea20b58bf112 43 static int getline(char *prompt, char *buff, int size)
wolfSSL 0:ea20b58bf112 44 {
wolfSSL 0:ea20b58bf112 45 int sz ;
wolfSSL 0:ea20b58bf112 46
wolfSSL 0:ea20b58bf112 47 printf("%s", prompt) ;
wolfSSL 0:ea20b58bf112 48 for(sz = 0 ; (sz < size) && ((*buff = getchar()) != '\r'); sz++, buff++) {
wolfSSL 0:ea20b58bf112 49 putchar(*buff) ;
wolfSSL 0:ea20b58bf112 50 if(*buff == '\\') {
wolfSSL 0:ea20b58bf112 51 if(++sz >= size)break ;
wolfSSL 0:ea20b58bf112 52 *buff = getchar() ;
wolfSSL 0:ea20b58bf112 53 putchar(*buff) ;
wolfSSL 0:ea20b58bf112 54 switch(*buff) {
wolfSSL 0:ea20b58bf112 55 case 'n' :
wolfSSL 0:ea20b58bf112 56 *buff = '\n' ;
wolfSSL 0:ea20b58bf112 57 break ;
wolfSSL 0:ea20b58bf112 58 case 'r' :
wolfSSL 0:ea20b58bf112 59 *buff = '\r' ;
wolfSSL 0:ea20b58bf112 60 break ;
wolfSSL 0:ea20b58bf112 61 case 't' :
wolfSSL 0:ea20b58bf112 62 *buff = '\t' ;
wolfSSL 0:ea20b58bf112 63 break ;
wolfSSL 0:ea20b58bf112 64 case '\\':
wolfSSL 0:ea20b58bf112 65 *buff = '\\' ;
wolfSSL 0:ea20b58bf112 66 break ;
wolfSSL 0:ea20b58bf112 67 default:
wolfSSL 0:ea20b58bf112 68 buff[1] = buff[0] ;
wolfSSL 0:ea20b58bf112 69 buff[0] = '\\' ;
wolfSSL 0:ea20b58bf112 70 buff++ ;
wolfSSL 0:ea20b58bf112 71 }
wolfSSL 0:ea20b58bf112 72 } else if(*buff == '\b') {
wolfSSL 0:ea20b58bf112 73 if(sz >= 2) {
wolfSSL 0:ea20b58bf112 74 buff-=2 ;
wolfSSL 0:ea20b58bf112 75 sz-=2;
wolfSSL 0:ea20b58bf112 76 }
wolfSSL 0:ea20b58bf112 77 }
wolfSSL 0:ea20b58bf112 78 } ;
wolfSSL 0:ea20b58bf112 79 putchar('\n') ;
wolfSSL 0:ea20b58bf112 80 *buff = '\0' ;
wolfSSL 0:ea20b58bf112 81 return sz ;
wolfSSL 0:ea20b58bf112 82 }
wolfSSL 0:ea20b58bf112 83
wolfSSL 0:ea20b58bf112 84 /*
wolfSSL 0:ea20b58bf112 85 * clients initial contact with server. Socket to connect to: sock
wolfSSL 0:ea20b58bf112 86 */
wolfSSL 2:53d82dd5e556 87 int ClientGreet(TCPSocketConnection *socket, WOLFSSL *ssl)
wolfSSL 0:ea20b58bf112 88 {
wolfSSL 0:ea20b58bf112 89 /* data to send to the server, data recieved from the server */
wolfSSL 0:ea20b58bf112 90 char sendBuff[MAXDATASIZE], rcvBuff[MAXDATASIZE] = {0};
wolfSSL 0:ea20b58bf112 91 int ret ;
wolfSSL 0:ea20b58bf112 92
wolfSSL 0:ea20b58bf112 93 ret = getline("Message for server: ", sendBuff, MAXDATASIZE);
wolfSSL 0:ea20b58bf112 94 printf("Send[%d]:\n%s\n", ret, sendBuff) ;
wolfSSL 2:53d82dd5e556 95 if (wolfSSL_write(ssl, sendBuff, strlen(sendBuff)) < 0) {
wolfSSL 2:53d82dd5e556 96 /* the message is not able to send, or error trying */
wolfSSL 2:53d82dd5e556 97 ret = wolfSSL_get_error(ssl, 0);
wolfSSL 3:25d42ccf2f12 98 printf("Write error[%d]\n", ret, wc_GetErrorString(ret));
wolfSSL 0:ea20b58bf112 99 return EXIT_FAILURE;
wolfSSL 0:ea20b58bf112 100 }
wolfSSL 0:ea20b58bf112 101 printf("Recieved:\n");
wolfSSL 0:ea20b58bf112 102 while(1) {
wolfSSL 3:25d42ccf2f12 103 if ((ret = wolfSSL_read(ssl, rcvBuff, sizeof(rcvBuff)-1)) < 0) {
wolfSSL 0:ea20b58bf112 104 if(ret == 0)break ;
wolfSSL 2:53d82dd5e556 105 /* the server failed to send data, or error trying */
wolfSSL 2:53d82dd5e556 106 ret = wolfSSL_get_error(ssl, 0);
wolfSSL 3:25d42ccf2f12 107 printf("Read error[%d], %s\n", ret, wc_GetErrorString(ret));
wolfSSL 0:ea20b58bf112 108 return EXIT_FAILURE;
wolfSSL 0:ea20b58bf112 109 }
wolfSSL 0:ea20b58bf112 110 rcvBuff[ret] = '\0' ;
wolfSSL 0:ea20b58bf112 111 printf("%s", rcvBuff);
wolfSSL 0:ea20b58bf112 112 if((rcvBuff[ret-3] == '\n')&&
wolfSSL 0:ea20b58bf112 113 (rcvBuff[ret-2] == '\n')&&
wolfSSL 0:ea20b58bf112 114 (rcvBuff[ret-1] == '\n'))break ;
wolfSSL 0:ea20b58bf112 115 }
wolfSSL 0:ea20b58bf112 116 return ret;
wolfSSL 0:ea20b58bf112 117 }
wolfSSL 0:ea20b58bf112 118
wolfSSL 2:53d82dd5e556 119
wolfSSL 2:53d82dd5e556 120 /*
wolfSSL 2:53d82dd5e556 121 * applies TLS 1.2 security layer to data being sent.
wolfSSL 2:53d82dd5e556 122 */
wolfSSL 2:53d82dd5e556 123 int Security(TCPSocketConnection *socket)
wolfSSL 2:53d82dd5e556 124 {
wolfSSL 2:53d82dd5e556 125 WOLFSSL_CTX* ctx;
wolfSSL 2:53d82dd5e556 126 WOLFSSL* ssl; /* create WOLFSSL object */
wolfSSL 2:53d82dd5e556 127 int ret = 0;
wolfSSL 2:53d82dd5e556 128
wolfSSL 2:53d82dd5e556 129 /* create and initiLize WOLFSSL_CTX structure */
wolfSSL 2:53d82dd5e556 130 if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
wolfSSL 2:53d82dd5e556 131 printf("SSL_CTX_new error.\n");
wolfSSL 2:53d82dd5e556 132 return EXIT_FAILURE;
wolfSSL 2:53d82dd5e556 133 }
wolfSSL 2:53d82dd5e556 134
wolfSSL 2:53d82dd5e556 135 wolfSSL_SetIORecv(ctx, SocketReceive) ;
wolfSSL 2:53d82dd5e556 136 wolfSSL_SetIOSend(ctx, SocketSend) ;
wolfSSL 2:53d82dd5e556 137
wolfSSL 2:53d82dd5e556 138 if ((ssl = wolfSSL_new(ctx)) == NULL) {
wolfSSL 2:53d82dd5e556 139 printf("wolfSSL_new error.\n");
wolfSSL 2:53d82dd5e556 140 return EXIT_FAILURE;
wolfSSL 2:53d82dd5e556 141 }
wolfSSL 2:53d82dd5e556 142
wolfSSL 2:53d82dd5e556 143 wolfSSL_SetIOReadCtx(ssl, (void *)socket) ;
wolfSSL 2:53d82dd5e556 144 wolfSSL_SetIOWriteCtx(ssl, (void *)socket) ;
wolfSSL 2:53d82dd5e556 145
wolfSSL 2:53d82dd5e556 146 ret = wolfSSL_connect(ssl);
wolfSSL 2:53d82dd5e556 147 if (ret == SSL_SUCCESS) {
wolfSSL 2:53d82dd5e556 148 printf("TLS Connected\n") ;
wolfSSL 2:53d82dd5e556 149 ret = ClientGreet(socket, ssl);
wolfSSL 2:53d82dd5e556 150 } else {
wolfSSL 2:53d82dd5e556 151 ret = wolfSSL_get_error(ssl, 0);
wolfSSL 3:25d42ccf2f12 152 printf("TLS Connect error[%d], %s\n", ret, wc_GetErrorString(ret));
wolfSSL 2:53d82dd5e556 153 }
wolfSSL 2:53d82dd5e556 154 /* frees all data before client termination */
wolfSSL 2:53d82dd5e556 155 wolfSSL_free(ssl);
wolfSSL 2:53d82dd5e556 156 wolfSSL_CTX_free(ctx);
wolfSSL 2:53d82dd5e556 157 wolfSSL_Cleanup();
wolfSSL 2:53d82dd5e556 158
wolfSSL 2:53d82dd5e556 159 return ret;
wolfSSL 2:53d82dd5e556 160 }
wolfSSL 2:53d82dd5e556 161
wolfSSL 0:ea20b58bf112 162 /*
wolfSSL 0:ea20b58bf112 163 * command line argumentCount and argumentValues
wolfSSL 0:ea20b58bf112 164 */
wolfSSL 1:29638701a63a 165 void net_main(const void *av)
wolfSSL 0:ea20b58bf112 166 {
wolfSSL 1:29638701a63a 167 char server_addr[40] ;
wolfSSL 1:29638701a63a 168 char server_port[10] ;
wolfSSL 4:ebcf8e2d846a 169
wolfSSL 3:25d42ccf2f12 170 wolfSSL_Init(); /* initialize wolfSSL */
wolfSSL 3:25d42ccf2f12 171 /* wolfSSL_Debugging_ON(); */
wolfSSL 0:ea20b58bf112 172 EthernetInterface eth;
wolfSSL 0:ea20b58bf112 173 TCPSocketConnection socket;
wolfSSL 4:ebcf8e2d846a 174
wolfSSL 0:ea20b58bf112 175 eth.init(); //Use DHCP
wolfSSL 0:ea20b58bf112 176 eth.connect();
wolfSSL 0:ea20b58bf112 177 printf("Client Addr: %s\n", eth.getIPAddress());
wolfSSL 0:ea20b58bf112 178
wolfSSL 0:ea20b58bf112 179 getline("Server Addr: ", server_addr, sizeof(server_addr)) ;
wolfSSL 0:ea20b58bf112 180 getline("Server Port: ", server_port, sizeof(server_port)) ;
wolfSSL 0:ea20b58bf112 181
wolfSSL 0:ea20b58bf112 182 while (socket.connect(server_addr, atoi(server_port)) < 0) {
wolfSSL 3:25d42ccf2f12 183 printf("Unable to connect to (%s) on port (%s)\n", server_addr, server_port);
wolfSSL 4:ebcf8e2d846a 184 wait(1.0);
wolfSSL 0:ea20b58bf112 185 }
wolfSSL 0:ea20b58bf112 186 printf("TCP Connected\n") ;
wolfSSL 0:ea20b58bf112 187
wolfSSL 2:53d82dd5e556 188 Security(&socket);
wolfSSL 1:29638701a63a 189 return ;
wolfSSL 1:29638701a63a 190 }
wolfSSL 1:29638701a63a 191
wolfSSL 1:29638701a63a 192 int main(void)
wolfSSL 1:29638701a63a 193 {
wolfSSL 1:29638701a63a 194 #define STACK_SIZE 24000
wolfSSL 1:29638701a63a 195 Thread t(net_main, NULL, osPriorityNormal, STACK_SIZE);
wolfSSL 1:29638701a63a 196 while(1)wait(1.0) ;
wolfSSL 0:ea20b58bf112 197 }