Execution example of ssl access

Dependencies:   CyaSSL EthernetInterface mbed-rtos mbed

Fork of ssl_access_exe by shinichi satoh

Committer:
thursday1024
Date:
Wed Jul 22 08:23:57 2015 +0000
Revision:
6:1b761393c52c
Parent:
5:962734db89e5
ssl access executable

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thursday1024 6:1b761393c52c 1 // ssl_access_exe main.cpp
wolfSSL 0:0c584b87ea42 2 #include "mbed.h"
wolfSSL 0:0c584b87ea42 3 #include "EthernetInterface.h"
wolfSSL 0:0c584b87ea42 4
thursday1024 6:1b761393c52c 5 #include <cyassl/ssl.h>
wolfSSL 0:0c584b87ea42 6
thursday1024 6:1b761393c52c 7 #define PORT 443
wolfSSL 0:0c584b87ea42 8
wolfSSL 2:29a1370416cb 9 #define err_sys(m) puts(m)
wolfSSL 0:0c584b87ea42 10
wolfSSL 0:0c584b87ea42 11 TCPSocketConnection socket;
wolfSSL 0:0c584b87ea42 12
thursday1024 6:1b761393c52c 13
wolfSSL 0:0c584b87ea42 14 static int SocketReceive(CYASSL* ssl, char *buf, int sz, void *ctx)
wolfSSL 0:0c584b87ea42 15 {
wolfSSL 0:0c584b87ea42 16 int n ;
wolfSSL 0:0c584b87ea42 17 int i ;
wolfSSL 0:0c584b87ea42 18 #define RECV_RETRY 3
wolfSSL 0:0c584b87ea42 19 for(i=0; i<RECV_RETRY; i++) {
wolfSSL 0:0c584b87ea42 20 n = socket.receive(buf, sz) ;
wolfSSL 0:0c584b87ea42 21 if(n >= 0)return n ;
wolfSSL 0:0c584b87ea42 22 }
wolfSSL 0:0c584b87ea42 23 printf("SocketReceive:%d/%d\n", n, sz) ;
wolfSSL 0:0c584b87ea42 24 return n ;
wolfSSL 0:0c584b87ea42 25 }
wolfSSL 0:0c584b87ea42 26
wolfSSL 0:0c584b87ea42 27 static int SocketSend(CYASSL* ssl, char *buf, int sz, void *ctx)
wolfSSL 0:0c584b87ea42 28 {
wolfSSL 0:0c584b87ea42 29 int n ;
wolfSSL 0:0c584b87ea42 30
wolfSSL 0:0c584b87ea42 31 n = socket.send(buf, sz);
wolfSSL 0:0c584b87ea42 32 if(n > 0) {
wolfSSL 0:0c584b87ea42 33 return n ;
wolfSSL 0:0c584b87ea42 34 } else printf("SocketSend:%d/%d\n", n, sz);
wolfSSL 0:0c584b87ea42 35 return n ;
wolfSSL 0:0c584b87ea42 36 }
thursday1024 6:1b761393c52c 37
wolfSSL 0:0c584b87ea42 38
wolfSSL 1:ac91b4f8d818 39 EthernetInterface eth;
wolfSSL 1:ac91b4f8d818 40
wolfSSL 5:962734db89e5 41 void net_main(void const *av)
wolfSSL 0:0c584b87ea42 42 {
wolfSSL 1:ac91b4f8d818 43 char server_ip[20] ;
wolfSSL 2:29a1370416cb 44
wolfSSL 2:29a1370416cb 45 eth.init(); //Use DHCP
thursday1024 6:1b761393c52c 46 //printf("===== Simple TCP Client ========\n") ;
wolfSSL 2:29a1370416cb 47 printf("===== Simple SSL Client ========\n") ;
thursday1024 6:1b761393c52c 48
wolfSSL 2:29a1370416cb 49 while(1) {
wolfSSL 2:29a1370416cb 50 if(eth.connect()== 0)break ;
wolfSSL 2:29a1370416cb 51 wait(0.1);
wolfSSL 2:29a1370416cb 52 }
wolfSSL 2:29a1370416cb 53 printf("Client IP: %s\n", eth.getIPAddress());
wolfSSL 2:29a1370416cb 54
thursday1024 6:1b761393c52c 55
wolfSSL 0:0c584b87ea42 56 CYASSL_CTX* ctx = 0;
wolfSSL 0:0c584b87ea42 57 CYASSL* ssl = 0;
wolfSSL 0:0c584b87ea42 58
wolfSSL 0:0c584b87ea42 59 CYASSL_METHOD* method = CyaTLSv1_2_client_method();
thursday1024 6:1b761393c52c 60
thursday1024 6:1b761393c52c 61
wolfSSL 0:0c584b87ea42 62 /* Initialize CyaSSL Context */
wolfSSL 0:0c584b87ea42 63 ctx = CyaSSL_CTX_new(method);
wolfSSL 0:0c584b87ea42 64 if (ctx == NULL)
wolfSSL 0:0c584b87ea42 65 err_sys("unable to get ctx");
wolfSSL 0:0c584b87ea42 66 CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
wolfSSL 0:0c584b87ea42 67 CyaSSL_SetIORecv(ctx, SocketReceive) ;
wolfSSL 0:0c584b87ea42 68 CyaSSL_SetIOSend(ctx, SocketSend) ;
wolfSSL 0:0c584b87ea42 69
wolfSSL 0:0c584b87ea42 70 socket.set_blocking(false, 300) ;
wolfSSL 1:ac91b4f8d818 71 printf("Server IP: ") ;
wolfSSL 2:29a1370416cb 72 for(int i=0; i<sizeof(server_ip); i++) {
wolfSSL 2:29a1370416cb 73 if((server_ip[i] = getchar()) == '\r') {
wolfSSL 2:29a1370416cb 74 server_ip[i] = '\0' ;
wolfSSL 2:29a1370416cb 75 putchar('\n') ;
wolfSSL 2:29a1370416cb 76 break ;
wolfSSL 2:29a1370416cb 77 } else putchar(server_ip[i]) ;
wolfSSL 2:29a1370416cb 78 }
wolfSSL 2:29a1370416cb 79
wolfSSL 1:ac91b4f8d818 80 while (socket.connect(server_ip, PORT) < 0) {
wolfSSL 1:ac91b4f8d818 81 printf("Unable to connect to (%s) on port (%d)\n", server_ip, PORT);
wolfSSL 0:0c584b87ea42 82 wait(1);
wolfSSL 0:0c584b87ea42 83 }
wolfSSL 0:0c584b87ea42 84 printf("TCP Connected\n") ;
wolfSSL 0:0c584b87ea42 85
wolfSSL 0:0c584b87ea42 86 ssl = CyaSSL_new(ctx);
wolfSSL 0:0c584b87ea42 87 if (ssl == NULL)
wolfSSL 0:0c584b87ea42 88 err_sys("unable to get SSL object");
wolfSSL 0:0c584b87ea42 89 if (CyaSSL_connect(ssl) != SSL_SUCCESS) {
wolfSSL 0:0c584b87ea42 90 int err = CyaSSL_get_error(ssl, 0);
wolfSSL 0:0c584b87ea42 91 printf("err = %d, %s\n", err,
wolfSSL 1:ac91b4f8d818 92 CyaSSL_ERR_error_string(err, "\n"));
wolfSSL 0:0c584b87ea42 93 err_sys("SSL Connection Error");
wolfSSL 0:0c584b87ea42 94 }
wolfSSL 0:0c584b87ea42 95 printf("SSL Connected\n") ;
thursday1024 6:1b761393c52c 96
wolfSSL 0:0c584b87ea42 97
wolfSSL 5:962734db89e5 98 char msg[] = "GET /congrats.html HTTP/1.0\r\nConnection: Close\r\n\r\n" ;
wolfSSL 0:0c584b87ea42 99 // const char msg[] = "Hello World\r\n" ;
wolfSSL 0:0c584b87ea42 100
wolfSSL 2:29a1370416cb 101 if (
thursday1024 6:1b761393c52c 102
thursday1024 6:1b761393c52c 103 CyaSSL_write(ssl, msg, sizeof(msg)-1) != (sizeof(msg)-1))
thursday1024 6:1b761393c52c 104 //socket.send(msg, sizeof(msg)-1) != (sizeof(msg)-1))
wolfSSL 0:0c584b87ea42 105 err_sys("CyaSSL_write failed");
wolfSSL 0:0c584b87ea42 106
wolfSSL 0:0c584b87ea42 107 char buf[1024];
wolfSSL 0:0c584b87ea42 108 int n ;
wolfSSL 1:ac91b4f8d818 109 puts("Server Response:\n") ;
wolfSSL 1:ac91b4f8d818 110 do {
thursday1024 6:1b761393c52c 111 n = CyaSSL_read(ssl, buf, sizeof(buf)-1);
thursday1024 6:1b761393c52c 112 //socket.receive(buf, sizeof(buf)-1);
wolfSSL 1:ac91b4f8d818 113 if (n >= 0) {
wolfSSL 1:ac91b4f8d818 114 buf[n] = 0;
wolfSSL 1:ac91b4f8d818 115 printf("%s", buf);
wolfSSL 5:962734db89e5 116 } else break ;
wolfSSL 1:ac91b4f8d818 117 } while(n > 0) ;
wolfSSL 1:ac91b4f8d818 118 puts("=== === === ===") ;
thursday1024 6:1b761393c52c 119 CyaSSL_free(ssl) ;
wolfSSL 1:ac91b4f8d818 120 socket.close();
thursday1024 6:1b761393c52c 121 CyaSSL_CTX_free(ctx) ;
wolfSSL 1:ac91b4f8d818 122 eth.disconnect();
wolfSSL 1:ac91b4f8d818 123 }
wolfSSL 5:962734db89e5 124
wolfSSL 5:962734db89e5 125 main()
wolfSSL 5:962734db89e5 126 {
wolfSSL 5:962734db89e5 127
wolfSSL 5:962734db89e5 128 #define STACK_SIZE 20000
wolfSSL 5:962734db89e5 129 Thread t(net_main, NULL, osPriorityNormal, STACK_SIZE);
wolfSSL 5:962734db89e5 130
wolfSSL 5:962734db89e5 131 while (true) {
wolfSSL 5:962734db89e5 132 Thread::wait(1000);
wolfSSL 5:962734db89e5 133 }
wolfSSL 5:962734db89e5 134 }