Example TLS client with wolfSSL
Dependencies: EthernetInterface-FRDM-k64F mbed-rtos mbed wolfSSL-TLS13-Beta Example-client-tls
Dependents: Example-client-tls
client-tls.cpp@1:c9a3dbb712d0, 2015-07-21 (annotated)
- Committer:
- wolfSSL
- Date:
- Tue Jul 21 22:37:00 2015 +0000
- Revision:
- 1:c9a3dbb712d0
- Parent:
- client-tcp.cpp@0:e23aa8e6ab2f
- Child:
- 2:76d10d65ce5b
client-tls
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 0:e23aa8e6ab2f | 1 | /* client-tcp.c |
wolfSSL | 0:e23aa8e6ab2f | 2 | * |
wolfSSL | 0:e23aa8e6ab2f | 3 | * Copyright (C) 2006-2015 wolfSSL Inc. |
wolfSSL | 0:e23aa8e6ab2f | 4 | * |
wolfSSL | 0:e23aa8e6ab2f | 5 | * This file is part of wolfSSL. (formerly known as CyaSSL) |
wolfSSL | 0:e23aa8e6ab2f | 6 | * |
wolfSSL | 0:e23aa8e6ab2f | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 0:e23aa8e6ab2f | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 0:e23aa8e6ab2f | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 0:e23aa8e6ab2f | 10 | * (at your option) any later version. |
wolfSSL | 0:e23aa8e6ab2f | 11 | * |
wolfSSL | 0:e23aa8e6ab2f | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 0:e23aa8e6ab2f | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 0:e23aa8e6ab2f | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 0:e23aa8e6ab2f | 15 | * GNU General Public License for more details. |
wolfSSL | 0:e23aa8e6ab2f | 16 | * |
wolfSSL | 0:e23aa8e6ab2f | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 0:e23aa8e6ab2f | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 0:e23aa8e6ab2f | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
wolfSSL | 0:e23aa8e6ab2f | 20 | */ |
wolfSSL | 0:e23aa8e6ab2f | 21 | |
wolfSSL | 0:e23aa8e6ab2f | 22 | #include "mbed.h" |
wolfSSL | 0:e23aa8e6ab2f | 23 | #include "EthernetInterface.h" |
wolfSSL | 0:e23aa8e6ab2f | 24 | #include <stdio.h> |
wolfSSL | 0:e23aa8e6ab2f | 25 | #include <stdlib.h> |
wolfSSL | 0:e23aa8e6ab2f | 26 | #include <string.h> |
wolfSSL | 0:e23aa8e6ab2f | 27 | |
wolfSSL | 0:e23aa8e6ab2f | 28 | #define MAXDATASIZE (1024*4) |
wolfSSL | 0:e23aa8e6ab2f | 29 | |
wolfSSL | 0:e23aa8e6ab2f | 30 | static int getline(char *prompt, char *buff, int size) |
wolfSSL | 0:e23aa8e6ab2f | 31 | { |
wolfSSL | 0:e23aa8e6ab2f | 32 | int sz ; |
wolfSSL | 0:e23aa8e6ab2f | 33 | |
wolfSSL | 0:e23aa8e6ab2f | 34 | printf("%s", prompt) ; |
wolfSSL | 0:e23aa8e6ab2f | 35 | for(sz = 0 ; (sz < size) && ((*buff = getchar()) != '\r'); sz++, buff++) { |
wolfSSL | 0:e23aa8e6ab2f | 36 | putchar(*buff) ; |
wolfSSL | 0:e23aa8e6ab2f | 37 | if(*buff == '\\') { |
wolfSSL | 0:e23aa8e6ab2f | 38 | if(++sz >= size)break ; |
wolfSSL | 0:e23aa8e6ab2f | 39 | *buff = getchar() ; |
wolfSSL | 0:e23aa8e6ab2f | 40 | putchar(*buff) ; |
wolfSSL | 0:e23aa8e6ab2f | 41 | switch(*buff) { |
wolfSSL | 0:e23aa8e6ab2f | 42 | case 'n' : |
wolfSSL | 0:e23aa8e6ab2f | 43 | *buff = '\n' ; |
wolfSSL | 0:e23aa8e6ab2f | 44 | break ; |
wolfSSL | 0:e23aa8e6ab2f | 45 | case 'r' : |
wolfSSL | 0:e23aa8e6ab2f | 46 | *buff = '\r' ; |
wolfSSL | 0:e23aa8e6ab2f | 47 | break ; |
wolfSSL | 0:e23aa8e6ab2f | 48 | case 't' : |
wolfSSL | 0:e23aa8e6ab2f | 49 | *buff = '\t' ; |
wolfSSL | 0:e23aa8e6ab2f | 50 | break ; |
wolfSSL | 0:e23aa8e6ab2f | 51 | case '\\': |
wolfSSL | 0:e23aa8e6ab2f | 52 | *buff = '\\' ; |
wolfSSL | 0:e23aa8e6ab2f | 53 | break ; |
wolfSSL | 0:e23aa8e6ab2f | 54 | default: |
wolfSSL | 0:e23aa8e6ab2f | 55 | buff[1] = buff[0] ; |
wolfSSL | 0:e23aa8e6ab2f | 56 | buff[0] = '\\' ; |
wolfSSL | 0:e23aa8e6ab2f | 57 | buff++ ; |
wolfSSL | 0:e23aa8e6ab2f | 58 | } |
wolfSSL | 0:e23aa8e6ab2f | 59 | } else if(*buff == '\b') { |
wolfSSL | 0:e23aa8e6ab2f | 60 | if(sz >= 2) { |
wolfSSL | 0:e23aa8e6ab2f | 61 | buff-=2 ; |
wolfSSL | 0:e23aa8e6ab2f | 62 | sz-=2; |
wolfSSL | 0:e23aa8e6ab2f | 63 | } |
wolfSSL | 0:e23aa8e6ab2f | 64 | } |
wolfSSL | 0:e23aa8e6ab2f | 65 | } ; |
wolfSSL | 0:e23aa8e6ab2f | 66 | putchar('\n') ; |
wolfSSL | 0:e23aa8e6ab2f | 67 | *buff = '\0' ; |
wolfSSL | 0:e23aa8e6ab2f | 68 | return sz ; |
wolfSSL | 0:e23aa8e6ab2f | 69 | } |
wolfSSL | 0:e23aa8e6ab2f | 70 | |
wolfSSL | 0:e23aa8e6ab2f | 71 | /* |
wolfSSL | 0:e23aa8e6ab2f | 72 | * clients initial contact with server. Socket to connect to: sock |
wolfSSL | 0:e23aa8e6ab2f | 73 | */ |
wolfSSL | 0:e23aa8e6ab2f | 74 | int ClientGreet(TCPSocketConnection *socket) |
wolfSSL | 0:e23aa8e6ab2f | 75 | { |
wolfSSL | 0:e23aa8e6ab2f | 76 | /* data to send to the server, data recieved from the server */ |
wolfSSL | 0:e23aa8e6ab2f | 77 | char sendBuff[MAXDATASIZE], rcvBuff[MAXDATASIZE] = {0}; |
wolfSSL | 0:e23aa8e6ab2f | 78 | int ret ; |
wolfSSL | 0:e23aa8e6ab2f | 79 | |
wolfSSL | 0:e23aa8e6ab2f | 80 | ret = getline("Message for server: ", sendBuff, MAXDATASIZE); |
wolfSSL | 0:e23aa8e6ab2f | 81 | printf("Send[%d]:\n%s\n", ret, sendBuff) ; |
wolfSSL | 0:e23aa8e6ab2f | 82 | if ((ret = socket->send(sendBuff, strlen(sendBuff))) < 0) { |
wolfSSL | 0:e23aa8e6ab2f | 83 | printf("Send error: %i", ret); |
wolfSSL | 0:e23aa8e6ab2f | 84 | return EXIT_FAILURE; |
wolfSSL | 0:e23aa8e6ab2f | 85 | } |
wolfSSL | 0:e23aa8e6ab2f | 86 | printf("Recieved:\n"); |
wolfSSL | 0:e23aa8e6ab2f | 87 | while(1) { |
wolfSSL | 0:e23aa8e6ab2f | 88 | if ((ret = socket->receive(rcvBuff, sizeof(rcvBuff)-1)) < 0) { |
wolfSSL | 0:e23aa8e6ab2f | 89 | if(ret == 0)break ; |
wolfSSL | 0:e23aa8e6ab2f | 90 | printf("Read error. Error: %i\n", ret); |
wolfSSL | 0:e23aa8e6ab2f | 91 | return EXIT_FAILURE; |
wolfSSL | 0:e23aa8e6ab2f | 92 | } |
wolfSSL | 0:e23aa8e6ab2f | 93 | rcvBuff[ret] = '\0' ; |
wolfSSL | 0:e23aa8e6ab2f | 94 | printf("%s", rcvBuff); |
wolfSSL | 0:e23aa8e6ab2f | 95 | if((rcvBuff[ret-3] == '\n')&& |
wolfSSL | 0:e23aa8e6ab2f | 96 | (rcvBuff[ret-2] == '\n')&& |
wolfSSL | 0:e23aa8e6ab2f | 97 | (rcvBuff[ret-1] == '\n'))break ; |
wolfSSL | 0:e23aa8e6ab2f | 98 | } |
wolfSSL | 0:e23aa8e6ab2f | 99 | return ret; |
wolfSSL | 0:e23aa8e6ab2f | 100 | } |
wolfSSL | 0:e23aa8e6ab2f | 101 | |
wolfSSL | 0:e23aa8e6ab2f | 102 | /* |
wolfSSL | 0:e23aa8e6ab2f | 103 | * command line argumentCount and argumentValues |
wolfSSL | 0:e23aa8e6ab2f | 104 | */ |
wolfSSL | 0:e23aa8e6ab2f | 105 | void main(const void *av) |
wolfSSL | 0:e23aa8e6ab2f | 106 | { |
wolfSSL | 0:e23aa8e6ab2f | 107 | char server_addr[40] ; |
wolfSSL | 0:e23aa8e6ab2f | 108 | char server_port[10] ; |
wolfSSL | 0:e23aa8e6ab2f | 109 | |
wolfSSL | 0:e23aa8e6ab2f | 110 | EthernetInterface eth; |
wolfSSL | 0:e23aa8e6ab2f | 111 | TCPSocketConnection socket; |
wolfSSL | 0:e23aa8e6ab2f | 112 | |
wolfSSL | 0:e23aa8e6ab2f | 113 | eth.init(); //Use DHCP |
wolfSSL | 0:e23aa8e6ab2f | 114 | eth.connect(); |
wolfSSL | 0:e23aa8e6ab2f | 115 | printf("Client Addr: %s\n", eth.getIPAddress()); |
wolfSSL | 0:e23aa8e6ab2f | 116 | |
wolfSSL | 0:e23aa8e6ab2f | 117 | getline("Server Addr: ", server_addr, sizeof(server_addr)) ; |
wolfSSL | 0:e23aa8e6ab2f | 118 | getline("Server Port: ", server_port, sizeof(server_port)) ; |
wolfSSL | 0:e23aa8e6ab2f | 119 | |
wolfSSL | 0:e23aa8e6ab2f | 120 | while (socket.connect(server_addr, atoi(server_port)) < 0) { |
wolfSSL | 0:e23aa8e6ab2f | 121 | printf("Unable to connect to (%s) on port (%s)\n", server_addr, server_port); |
wolfSSL | 0:e23aa8e6ab2f | 122 | wait(1.0); |
wolfSSL | 0:e23aa8e6ab2f | 123 | } |
wolfSSL | 0:e23aa8e6ab2f | 124 | printf("TCP Connected\n") ; |
wolfSSL | 0:e23aa8e6ab2f | 125 | |
wolfSSL | 0:e23aa8e6ab2f | 126 | ClientGreet(&socket); |
wolfSSL | 0:e23aa8e6ab2f | 127 | return ; |
wolfSSL | 0:e23aa8e6ab2f | 128 | } |