Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: CyaSSL EthernetInterface-FRDM mbed-rtos mbed
Fork of CyaSSL-EchoClient by
main.cpp@2:dc88e0c4270e, 2015-02-07 (annotated)
- Committer:
- wolfSSL
- Date:
- Sat Feb 07 11:39:58 2015 +0000
- Revision:
- 2:dc88e0c4270e
- Parent:
- 1:0b78d1071fee
- Child:
- 3:0cab91ba32c1
Simple SSL client example for FRDM-K64F
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| wolfSSL | 0:0c584b87ea42 | 1 | #include "mbed.h" |
| wolfSSL | 0:0c584b87ea42 | 2 | #include "EthernetInterface.h" |
| wolfSSL | 0:0c584b87ea42 | 3 | |
| wolfSSL | 0:0c584b87ea42 | 4 | #include <cyassl/ssl.h> |
| wolfSSL | 0:0c584b87ea42 | 5 | |
| wolfSSL | 1:0b78d1071fee | 6 | const int PORT = 443 ; |
| wolfSSL | 0:0c584b87ea42 | 7 | |
| wolfSSL | 1:0b78d1071fee | 8 | #define err_sys(m) { puts(m) ; } |
| wolfSSL | 0:0c584b87ea42 | 9 | |
| wolfSSL | 0:0c584b87ea42 | 10 | TCPSocketConnection socket; |
| wolfSSL | 0:0c584b87ea42 | 11 | |
| wolfSSL | 0:0c584b87ea42 | 12 | static int SocketReceive(CYASSL* ssl, char *buf, int sz, void *ctx) |
| wolfSSL | 0:0c584b87ea42 | 13 | { |
| wolfSSL | 0:0c584b87ea42 | 14 | int n ; |
| wolfSSL | 0:0c584b87ea42 | 15 | int i ; |
| wolfSSL | 0:0c584b87ea42 | 16 | #define RECV_RETRY 3 |
| wolfSSL | 0:0c584b87ea42 | 17 | for(i=0; i<RECV_RETRY; i++) { |
| wolfSSL | 0:0c584b87ea42 | 18 | n = socket.receive(buf, sz) ; |
| wolfSSL | 0:0c584b87ea42 | 19 | if(n >= 0)return n ; |
| wolfSSL | 0:0c584b87ea42 | 20 | } |
| wolfSSL | 0:0c584b87ea42 | 21 | return n ; |
| wolfSSL | 0:0c584b87ea42 | 22 | } |
| wolfSSL | 0:0c584b87ea42 | 23 | |
| wolfSSL | 0:0c584b87ea42 | 24 | static int SocketSend(CYASSL* ssl, char *buf, int sz, void *ctx) |
| wolfSSL | 0:0c584b87ea42 | 25 | { |
| wolfSSL | 0:0c584b87ea42 | 26 | int n ; |
| wolfSSL | 0:0c584b87ea42 | 27 | |
| wolfSSL | 0:0c584b87ea42 | 28 | n = socket.send(buf, sz); |
| wolfSSL | 0:0c584b87ea42 | 29 | if(n > 0) { |
| wolfSSL | 0:0c584b87ea42 | 30 | return n ; |
| wolfSSL | 2:dc88e0c4270e | 31 | } |
| wolfSSL | 0:0c584b87ea42 | 32 | return n ; |
| wolfSSL | 0:0c584b87ea42 | 33 | } |
| wolfSSL | 0:0c584b87ea42 | 34 | |
| wolfSSL | 1:0b78d1071fee | 35 | EthernetInterface eth; |
| wolfSSL | 1:0b78d1071fee | 36 | |
| wolfSSL | 1:0b78d1071fee | 37 | void net_main(void const *av) |
| wolfSSL | 0:0c584b87ea42 | 38 | { |
| wolfSSL | 1:0b78d1071fee | 39 | char server_ip[20] ; |
| wolfSSL | 0:0c584b87ea42 | 40 | CYASSL_CTX* ctx = 0; |
| wolfSSL | 0:0c584b87ea42 | 41 | CYASSL* ssl = 0; |
| wolfSSL | 0:0c584b87ea42 | 42 | |
| wolfSSL | 0:0c584b87ea42 | 43 | CYASSL_METHOD* method = CyaTLSv1_2_client_method(); |
| wolfSSL | 0:0c584b87ea42 | 44 | |
| wolfSSL | 0:0c584b87ea42 | 45 | /* Initialize CyaSSL Context */ |
| wolfSSL | 0:0c584b87ea42 | 46 | ctx = CyaSSL_CTX_new(method); |
| wolfSSL | 0:0c584b87ea42 | 47 | if (ctx == NULL) |
| wolfSSL | 0:0c584b87ea42 | 48 | err_sys("unable to get ctx"); |
| wolfSSL | 0:0c584b87ea42 | 49 | CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0); |
| wolfSSL | 0:0c584b87ea42 | 50 | CyaSSL_SetIORecv(ctx, SocketReceive) ; |
| wolfSSL | 0:0c584b87ea42 | 51 | CyaSSL_SetIOSend(ctx, SocketSend) ; |
| wolfSSL | 0:0c584b87ea42 | 52 | |
| wolfSSL | 0:0c584b87ea42 | 53 | socket.set_blocking(false, 300) ; |
| wolfSSL | 1:0b78d1071fee | 54 | printf("Server IP: ") ; |
| wolfSSL | 1:0b78d1071fee | 55 | scanf("%s", server_ip) ; |
| wolfSSL | 1:0b78d1071fee | 56 | puts(server_ip) ; |
| wolfSSL | 1:0b78d1071fee | 57 | while (socket.connect(server_ip, PORT) < 0) { |
| wolfSSL | 1:0b78d1071fee | 58 | printf("Unable to connect to (%s) on port (%d)\n", server_ip, PORT); |
| wolfSSL | 0:0c584b87ea42 | 59 | wait(1); |
| wolfSSL | 0:0c584b87ea42 | 60 | } |
| wolfSSL | 0:0c584b87ea42 | 61 | printf("TCP Connected\n") ; |
| wolfSSL | 0:0c584b87ea42 | 62 | |
| wolfSSL | 0:0c584b87ea42 | 63 | ssl = CyaSSL_new(ctx); |
| wolfSSL | 0:0c584b87ea42 | 64 | if (ssl == NULL) |
| wolfSSL | 0:0c584b87ea42 | 65 | err_sys("unable to get SSL object"); |
| wolfSSL | 0:0c584b87ea42 | 66 | if (CyaSSL_connect(ssl) != SSL_SUCCESS) { |
| wolfSSL | 0:0c584b87ea42 | 67 | int err = CyaSSL_get_error(ssl, 0); |
| wolfSSL | 0:0c584b87ea42 | 68 | printf("err = %d, %s\n", err, |
| wolfSSL | 1:0b78d1071fee | 69 | CyaSSL_ERR_error_string(err, "\n")); |
| wolfSSL | 0:0c584b87ea42 | 70 | err_sys("SSL Connection Error"); |
| wolfSSL | 0:0c584b87ea42 | 71 | } |
| wolfSSL | 0:0c584b87ea42 | 72 | printf("SSL Connected\n") ; |
| wolfSSL | 0:0c584b87ea42 | 73 | |
| wolfSSL | 2:dc88e0c4270e | 74 | char msg[] = "GET / HTTP/1.0\r\nConnection:close\r\n\r\n" ; |
| wolfSSL | 2:dc88e0c4270e | 75 | printf("Client Request: \n%s\n",msg) ; |
| wolfSSL | 1:0b78d1071fee | 76 | if (CyaSSL_write(ssl, |
| wolfSSL | 1:0b78d1071fee | 77 | /* socket.send(*/ msg, sizeof(msg)-1) != (sizeof(msg)-1)) |
| wolfSSL | 0:0c584b87ea42 | 78 | err_sys("CyaSSL_write failed"); |
| wolfSSL | 0:0c584b87ea42 | 79 | |
| wolfSSL | 0:0c584b87ea42 | 80 | char buf[1024]; |
| wolfSSL | 0:0c584b87ea42 | 81 | int n ; |
| wolfSSL | 1:0b78d1071fee | 82 | puts("Server Response:\n") ; |
| wolfSSL | 1:0b78d1071fee | 83 | do { |
| wolfSSL | 1:0b78d1071fee | 84 | n = CyaSSL_read(ssl, |
| wolfSSL | 1:0b78d1071fee | 85 | /* socket.receive(*/ buf, sizeof(buf)-1); |
| wolfSSL | 2:dc88e0c4270e | 86 | if (n > 0) { |
| wolfSSL | 1:0b78d1071fee | 87 | buf[n] = 0; |
| wolfSSL | 1:0b78d1071fee | 88 | printf("%s", buf); |
| wolfSSL | 1:0b78d1071fee | 89 | } else |
| wolfSSL | 2:dc88e0c4270e | 90 | break ; |
| wolfSSL | 1:0b78d1071fee | 91 | } while(n > 0) ; |
| wolfSSL | 1:0b78d1071fee | 92 | puts("=== === === ===") ; |
| wolfSSL | 1:0b78d1071fee | 93 | CyaSSL_free(ssl) ; |
| wolfSSL | 1:0b78d1071fee | 94 | socket.close(); |
| wolfSSL | 1:0b78d1071fee | 95 | CyaSSL_CTX_free(ctx) ; |
| wolfSSL | 1:0b78d1071fee | 96 | eth.disconnect(); |
| wolfSSL | 1:0b78d1071fee | 97 | } |
| wolfSSL | 1:0b78d1071fee | 98 | |
| wolfSSL | 0:0c584b87ea42 | 99 | |
| wolfSSL | 1:0b78d1071fee | 100 | main() |
| wolfSSL | 1:0b78d1071fee | 101 | { |
| wolfSSL | 1:0b78d1071fee | 102 | |
| wolfSSL | 1:0b78d1071fee | 103 | printf("===== Simple SSL CLIENT ========\n") ; |
| wolfSSL | 1:0b78d1071fee | 104 | /* CyaSSL_Debugging_ON() ; */ |
| wolfSSL | 0:0c584b87ea42 | 105 | |
| wolfSSL | 1:0b78d1071fee | 106 | eth.init(); //Use DHCP |
| wolfSSL | 1:0b78d1071fee | 107 | eth.connect(); |
| wolfSSL | 1:0b78d1071fee | 108 | printf("Client IP: %s\n", eth.getIPAddress()); |
| wolfSSL | 1:0b78d1071fee | 109 | |
| wolfSSL | 1:0b78d1071fee | 110 | #define STACK_SIZE 12000 |
| wolfSSL | 1:0b78d1071fee | 111 | Thread t(net_main, NULL, osPriorityNormal, STACK_SIZE); |
| wolfSSL | 1:0b78d1071fee | 112 | |
| wolfSSL | 1:0b78d1071fee | 113 | while (true) { |
| wolfSSL | 1:0b78d1071fee | 114 | Thread::wait(1000); |
| wolfSSL | 1:0b78d1071fee | 115 | } |
| wolfSSL | 1:0b78d1071fee | 116 | } |
