Example TLS client with wolfSSL, with cert

Dependencies:   EthernetInterface NTPClient SDFileSystem mbed-rtos mbed wolfSSL

Committer:
wolfSSL
Date:
Mon Jul 20 08:07:26 2015 +0000
Revision:
0:ea20b58bf112
Child:
1:29638701a63a
client-tcp

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 0:ea20b58bf112 27
wolfSSL 0:ea20b58bf112 28 #define MAXDATASIZE (1024*4)
wolfSSL 0:ea20b58bf112 29
wolfSSL 0:ea20b58bf112 30 static int getline(char *prompt, char *buff, int size)
wolfSSL 0:ea20b58bf112 31 {
wolfSSL 0:ea20b58bf112 32 int sz ;
wolfSSL 0:ea20b58bf112 33
wolfSSL 0:ea20b58bf112 34 printf("%s", prompt) ;
wolfSSL 0:ea20b58bf112 35 for(sz = 0 ; (sz < size) && ((*buff = getchar()) != '\r'); sz++, buff++) {
wolfSSL 0:ea20b58bf112 36 putchar(*buff) ;
wolfSSL 0:ea20b58bf112 37 if(*buff == '\\') {
wolfSSL 0:ea20b58bf112 38 if(++sz >= size)break ;
wolfSSL 0:ea20b58bf112 39 *buff = getchar() ;
wolfSSL 0:ea20b58bf112 40 putchar(*buff) ;
wolfSSL 0:ea20b58bf112 41 switch(*buff) {
wolfSSL 0:ea20b58bf112 42 case 'n' :
wolfSSL 0:ea20b58bf112 43 *buff = '\n' ;
wolfSSL 0:ea20b58bf112 44 break ;
wolfSSL 0:ea20b58bf112 45 case 'r' :
wolfSSL 0:ea20b58bf112 46 *buff = '\r' ;
wolfSSL 0:ea20b58bf112 47 break ;
wolfSSL 0:ea20b58bf112 48 case 't' :
wolfSSL 0:ea20b58bf112 49 *buff = '\t' ;
wolfSSL 0:ea20b58bf112 50 break ;
wolfSSL 0:ea20b58bf112 51 case '\\':
wolfSSL 0:ea20b58bf112 52 *buff = '\\' ;
wolfSSL 0:ea20b58bf112 53 break ;
wolfSSL 0:ea20b58bf112 54 default:
wolfSSL 0:ea20b58bf112 55 buff[1] = buff[0] ;
wolfSSL 0:ea20b58bf112 56 buff[0] = '\\' ;
wolfSSL 0:ea20b58bf112 57 buff++ ;
wolfSSL 0:ea20b58bf112 58 }
wolfSSL 0:ea20b58bf112 59 } else if(*buff == '\b') {
wolfSSL 0:ea20b58bf112 60 if(sz >= 2) {
wolfSSL 0:ea20b58bf112 61 buff-=2 ;
wolfSSL 0:ea20b58bf112 62 sz-=2;
wolfSSL 0:ea20b58bf112 63 }
wolfSSL 0:ea20b58bf112 64 }
wolfSSL 0:ea20b58bf112 65 } ;
wolfSSL 0:ea20b58bf112 66 putchar('\n') ;
wolfSSL 0:ea20b58bf112 67 *buff = '\0' ;
wolfSSL 0:ea20b58bf112 68 return sz ;
wolfSSL 0:ea20b58bf112 69 }
wolfSSL 0:ea20b58bf112 70
wolfSSL 0:ea20b58bf112 71 /*
wolfSSL 0:ea20b58bf112 72 * clients initial contact with server. Socket to connect to: sock
wolfSSL 0:ea20b58bf112 73 */
wolfSSL 0:ea20b58bf112 74 int ClientGreet(TCPSocketConnection *socket)
wolfSSL 0:ea20b58bf112 75 {
wolfSSL 0:ea20b58bf112 76 /* data to send to the server, data recieved from the server */
wolfSSL 0:ea20b58bf112 77 char sendBuff[MAXDATASIZE], rcvBuff[MAXDATASIZE] = {0};
wolfSSL 0:ea20b58bf112 78 int ret ;
wolfSSL 0:ea20b58bf112 79
wolfSSL 0:ea20b58bf112 80 ret = getline("Message for server: ", sendBuff, MAXDATASIZE);
wolfSSL 0:ea20b58bf112 81 printf("Send[%d]:\n%s\n", ret, sendBuff) ;
wolfSSL 0:ea20b58bf112 82 if ((ret = socket->send(sendBuff, sizeof(sendBuff)-1)) < 0) {
wolfSSL 0:ea20b58bf112 83 printf("Send error: %i", ret);
wolfSSL 0:ea20b58bf112 84 return EXIT_FAILURE;
wolfSSL 0:ea20b58bf112 85 }
wolfSSL 0:ea20b58bf112 86 printf("Recieved:\n");
wolfSSL 0:ea20b58bf112 87 while(1) {
wolfSSL 0:ea20b58bf112 88 if ((ret = socket->receive(rcvBuff, sizeof(rcvBuff)-1)) < 0) {
wolfSSL 0:ea20b58bf112 89 if(ret == 0)break ;
wolfSSL 0:ea20b58bf112 90 printf("Read error. Error: %i\n", ret);
wolfSSL 0:ea20b58bf112 91 return EXIT_FAILURE;
wolfSSL 0:ea20b58bf112 92 }
wolfSSL 0:ea20b58bf112 93 rcvBuff[ret] = '\0' ;
wolfSSL 0:ea20b58bf112 94 printf("%s", rcvBuff);
wolfSSL 0:ea20b58bf112 95 if((rcvBuff[ret-3] == '\n')&&
wolfSSL 0:ea20b58bf112 96 (rcvBuff[ret-2] == '\n')&&
wolfSSL 0:ea20b58bf112 97 (rcvBuff[ret-1] == '\n'))break ;
wolfSSL 0:ea20b58bf112 98 }
wolfSSL 0:ea20b58bf112 99 return ret;
wolfSSL 0:ea20b58bf112 100 }
wolfSSL 0:ea20b58bf112 101
wolfSSL 0:ea20b58bf112 102 /*
wolfSSL 0:ea20b58bf112 103 * command line argumentCount and argumentValues
wolfSSL 0:ea20b58bf112 104 */
wolfSSL 0:ea20b58bf112 105 int main(void)
wolfSSL 0:ea20b58bf112 106 {
wolfSSL 0:ea20b58bf112 107 EthernetInterface eth;
wolfSSL 0:ea20b58bf112 108 TCPSocketConnection socket;
wolfSSL 0:ea20b58bf112 109 char server_addr[40] ;
wolfSSL 0:ea20b58bf112 110 char server_port[10] ;
wolfSSL 0:ea20b58bf112 111
wolfSSL 0:ea20b58bf112 112 eth.init(); //Use DHCP
wolfSSL 0:ea20b58bf112 113 eth.connect();
wolfSSL 0:ea20b58bf112 114 printf("Client Addr: %s\n", eth.getIPAddress());
wolfSSL 0:ea20b58bf112 115
wolfSSL 0:ea20b58bf112 116 getline("Server Addr: ", server_addr, sizeof(server_addr)) ;
wolfSSL 0:ea20b58bf112 117 getline("Server Port: ", server_port, sizeof(server_port)) ;
wolfSSL 0:ea20b58bf112 118
wolfSSL 0:ea20b58bf112 119 while (socket.connect(server_addr, atoi(server_port)) < 0) {
wolfSSL 0:ea20b58bf112 120 printf("Unable to connect to (%s) on port (%d)\n", server_addr, server_port);
wolfSSL 0:ea20b58bf112 121 wait(1.0);
wolfSSL 0:ea20b58bf112 122 }
wolfSSL 0:ea20b58bf112 123 printf("TCP Connected\n") ;
wolfSSL 0:ea20b58bf112 124
wolfSSL 0:ea20b58bf112 125 ClientGreet(&socket);
wolfSSL 0:ea20b58bf112 126 return 0;
wolfSSL 0:ea20b58bf112 127 }