Connect a secure socket with TLS server authentication through X-NUCLEO_IDW01M1 Wi-Fi.
Dependencies: NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed
Fork of HelloWorld_IDW01M1v2 by
Example of TLS connection using X-NUCLEO-IDW01M1 Wi-Fi expansion board.
The application is meant to be used with mbed OS 2 ("Classic") only (no mbedOS 5 support).
It uses the TLS/SSL feature provided natively by the Wi-Fi module and performs secure connection to https://mbed.org also verifying the server identity.
To avoid expired CA certificates, system time (in epoch) must be manually entered (e..g. using http://www.epochconverter.com/ ) .
Retrieval of current time from an NTP server is shown by this example.
Revision 12:7b6b23225fd0, committed 2017-01-16
- Comitter:
- mapellil
- Date:
- Mon Jan 16 13:20:06 2017 +0000
- Parent:
- 11:0b4472769b08
- Commit message:
- Removed unsecure socket connection (only secure socket connection with server authentication)
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 0b4472769b08 -r 7b6b23225fd0 main.cpp --- a/main.cpp Fri Jan 13 15:31:35 2017 +0000 +++ b/main.cpp Mon Jan 16 13:20:06 2017 +0000 @@ -81,8 +81,8 @@ int err; char * ssid = "STM"; char * seckey = "STMDemo"; - - pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n"); + + pc.printf("\r\nTLS_HelloWorld mbed Application\r\n"); pc.printf("\r\nconnecting to AP\r\n"); if(spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2)) { @@ -92,42 +92,8 @@ return -1; } - const char *ip = spwf.get_ip_address(); - const char *mac = spwf.get_mac_address(); - - pc.printf("\r\nIP Address is: %s\r\n", (ip) ? ip : "No IP"); - pc.printf("\r\nMAC Address is: %s\r\n", (mac) ? mac : "No MAC"); - - SocketAddress addr(&spwf, "st.com"); - pc.printf("\r\nst.com resolved to: %s\r\n", addr.get_ip_address()); - - pc.printf("\r\nconnecting to http://4.ifcfg.me\r\n"); - +// Start Secure Socket connection test (one way server autentication only) TCPSocket socket(&spwf); - err = socket.connect("4.ifcfg.me", 23); - if(err!=0) - { - pc.printf("\r\nCould not connect to Socket, err = %d!!\r\n", err); - return -1; - } else pc.printf("\r\nconnected to host server\r\n"); - - char buffer[100]; - int count = 0; - pc.printf("\r\nReceiving Data\r\n"); - count = socket.recv(buffer, sizeof buffer); - - if(count > 0) - { - buffer [count]='\0'; - printf("%s\r\n", buffer); - } - else pc.printf("\r\nData not received\r\n"); - - pc.printf("\r\nClosing Socket\r\n"); - socket.close(); - pc.printf("\r\nUnsecure Socket Test complete.\r\n"); - -// Start Secure Socket connection test (one way server autentication only) time_t ctTime; ctTime = time(NULL); printf ("Start Secure Socket connection with one way server autentication test\n\r"); @@ -146,6 +112,7 @@ // NOTE: the Wifi API set_secure_mode() and set_unsecure_mode() refers to the whole interface so the socket must be created / opened after // having set the desidered mode (deft unsecure). //#define SEC_MODE1 // two possible ways to secure connect a socket are shown + SocketAddress addr(&spwf, ""); spwf.gethostbyname(&addr,"www.mbed.com"); // ask DNS to resolve URL/IP addr.set_port(443); spwf.set_secure_mode();