Hello world example of a TLS client: fetch an HTTPS page. The canonical source for this example lives at https://github.com/ARMmbed/mbed-os-example-tls

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /*
00002  *  Hello world example of a TLS client: fetch an HTTPS page
00003  *
00004  *  Copyright (C) 2006-2018, Arm Limited, All Rights Reserved
00005  *  SPDX-License-Identifier: Apache-2.0
00006  *
00007  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00008  *  not use this file except in compliance with the License.
00009  *  You may obtain a copy of the License at
00010  *
00011  *  http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  *  Unless required by applicable law or agreed to in writing, software
00014  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00015  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *  See the License for the specific language governing permissions and
00017  *  limitations under the License.
00018  *
00019  *  This file is part of Mbed TLS (https://tls.mbed.org)
00020  */
00021 
00022 /**
00023  * \file main.cpp
00024  *
00025  * \brief An example TLS Client application
00026  *
00027  * This application sends an HTTPS request to os.mbed.com and searches
00028  * for a string in the result.
00029  *
00030  * This example is implemented as a logic class (HelloHttpsClient) wrapping a
00031  * TCP socket. The logic class handles all events, leaving the main loop to just
00032  * check if the process  has finished.
00033  */
00034 
00035 #include "mbed.h"
00036 
00037 #include "mbedtls/platform.h"
00038 #if defined(MBEDTLS_USE_PSA_CRYPTO)
00039 #include "psa/crypto.h"
00040 #endif /* MBEDTLS_USE_PSA_CRYPTO */
00041 
00042 #include "HelloHttpsClient.h"
00043 
00044 /* Domain/IP address of the server to contact */
00045 const char SERVER_NAME[] = "os.mbed.com";
00046 const char SERVER_ADDR[] = "os.mbed.com";
00047 
00048 /* Port used to connect to the server */
00049 const int SERVER_PORT = 443;
00050 
00051 /**
00052  * The main function driving the HTTPS client.
00053  */
00054 int main()
00055 {
00056     int exit_code = MBEDTLS_EXIT_FAILURE;
00057 
00058     if((exit_code = mbedtls_platform_setup(NULL)) != 0) {
00059         printf("Platform initialization failed with error %d\r\n", exit_code);
00060         return MBEDTLS_EXIT_FAILURE;
00061     }
00062 
00063 #if defined(MBEDTLS_USE_PSA_CRYPTO)
00064     /*
00065      * Initialize underlying PSA Crypto implementation.
00066      * Even if the HTTPS client doesn't make use of
00067      * PSA-specific API, for example for setting opaque PSKs
00068      * or opaque private keys, Mbed TLS will use PSA
00069      * for public and symmetric key operations as well as
00070      * hashing.
00071      */
00072     psa_status_t status;
00073     status = psa_crypto_init();
00074     if( status != PSA_SUCCESS )
00075     {
00076         printf("psa_crypto_init() failed with %d\r\n", status );
00077         return MBEDTLS_EXIT_FAILURE;
00078     }
00079 #endif /* MBEDTLS_USE_PSA_CRYPTO */
00080 
00081     /*
00082      * The default 9600 bps is too slow to print full TLS debug info and could
00083      * cause the other party to time out.
00084      */
00085 
00086     HelloHttpsClient *client;
00087 
00088     mbedtls_printf("Starting mbed-os-example-tls/tls-client\n");
00089 
00090 #if defined(MBED_MAJOR_VERSION)
00091     mbedtls_printf("Using Mbed OS %d.%d.%d\n",
00092                    MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00093 #else
00094     printf("Using Mbed OS from master.\n");
00095 #endif /* MBEDTLS_MAJOR_VERSION */
00096 
00097     /* Allocate a HTTPS client */
00098     client = new (std::nothrow) HelloHttpsClient(SERVER_NAME, SERVER_ADDR, SERVER_PORT);
00099 
00100     if (client == NULL) {
00101         mbedtls_printf("Failed to allocate HelloHttpsClient object\n"
00102                        "\nFAIL\n");
00103         mbedtls_platform_teardown(NULL);
00104         return exit_code;
00105     }
00106 
00107     /* Run the client */
00108     if (client->run() != 0) {
00109         mbedtls_printf("\nFAIL\n");
00110     } else {
00111         exit_code = MBEDTLS_EXIT_SUCCESS;
00112         mbedtls_printf("\nDONE\n");
00113     }
00114 
00115     delete client;
00116 
00117     mbedtls_platform_teardown(NULL);
00118     return exit_code;
00119 }