ssl_access (http->https)

Dependencies:   CyaSSL EthernetInterface mbed-rtos mbed

Fork of ssl_access by shinichi satoh

Committer:
wolfSSL
Date:
Mon Dec 08 05:10:41 2014 +0000
Revision:
2:29a1370416cb
Parent:
1:ac91b4f8d818
Child:
5:962734db89e5
Cleaned for the workshop

Who changed what in which revision?

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