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.
main.cpp@98:54b91f4b0c49, 2019-11-18 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Nov 18 14:00:49 2019 +0000
- Revision:
- 98:54b91f4b0c49
- Parent:
- 95:d282bc7f32e4
Merge pull request #264 from dgreen-arm/point-master-at-mbed-os-master
Point master branch at Mbed OS master
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-tls
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Janos Follath |
0:fc70c47eecb4 | 1 | /* |
Janos Follath |
0:fc70c47eecb4 | 2 | * Hello world example of a TLS client: fetch an HTTPS page |
Janos Follath |
0:fc70c47eecb4 | 3 | * |
mbed_official | 66:ce8709d9912c | 4 | * Copyright (C) 2006-2018, Arm Limited, All Rights Reserved |
Janos Follath |
0:fc70c47eecb4 | 5 | * SPDX-License-Identifier: Apache-2.0 |
Janos Follath |
0:fc70c47eecb4 | 6 | * |
Janos Follath |
0:fc70c47eecb4 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
Janos Follath |
0:fc70c47eecb4 | 8 | * not use this file except in compliance with the License. |
Janos Follath |
0:fc70c47eecb4 | 9 | * You may obtain a copy of the License at |
Janos Follath |
0:fc70c47eecb4 | 10 | * |
Janos Follath |
0:fc70c47eecb4 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
Janos Follath |
0:fc70c47eecb4 | 12 | * |
Janos Follath |
0:fc70c47eecb4 | 13 | * Unless required by applicable law or agreed to in writing, software |
Janos Follath |
0:fc70c47eecb4 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Janos Follath |
0:fc70c47eecb4 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Janos Follath |
0:fc70c47eecb4 | 16 | * See the License for the specific language governing permissions and |
Janos Follath |
0:fc70c47eecb4 | 17 | * limitations under the License. |
Janos Follath |
0:fc70c47eecb4 | 18 | * |
mbed_official | 50:b6870173bcac | 19 | * This file is part of Mbed TLS (https://tls.mbed.org) |
mbed_official | 12:1ae41c231014 | 20 | */ |
Janos Follath |
0:fc70c47eecb4 | 21 | |
mbed_official | 66:ce8709d9912c | 22 | /** |
mbed_official | 66:ce8709d9912c | 23 | * \file main.cpp |
mbed_official | 66:ce8709d9912c | 24 | * |
mbed_official | 66:ce8709d9912c | 25 | * \brief An example TLS Client application |
Janos Follath |
0:fc70c47eecb4 | 26 | * |
mbed_official | 66:ce8709d9912c | 27 | * This application sends an HTTPS request to os.mbed.com and searches |
mbed_official | 66:ce8709d9912c | 28 | * for a string in the result. |
mbed_official | 66:ce8709d9912c | 29 | * |
mbed_official | 66:ce8709d9912c | 30 | * This example is implemented as a logic class (HelloHttpsClient) wrapping a |
mbed_official | 66:ce8709d9912c | 31 | * TCP socket. The logic class handles all events, leaving the main loop to just |
mbed_official | 66:ce8709d9912c | 32 | * check if the process has finished. |
Janos Follath |
0:fc70c47eecb4 | 33 | */ |
Janos Follath |
0:fc70c47eecb4 | 34 | |
Janos Follath |
0:fc70c47eecb4 | 35 | #include "mbed.h" |
Janos Follath |
0:fc70c47eecb4 | 36 | |
Janos Follath |
0:fc70c47eecb4 | 37 | #include "mbedtls/platform.h" |
mbed_official | 95:d282bc7f32e4 | 38 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
mbed_official | 95:d282bc7f32e4 | 39 | #include "psa/crypto.h" |
mbed_official | 95:d282bc7f32e4 | 40 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
mbed_official | 12:1ae41c231014 | 41 | |
mbed_official | 66:ce8709d9912c | 42 | #include "HelloHttpsClient.h" |
Janos Follath |
0:fc70c47eecb4 | 43 | |
mbed_official | 66:ce8709d9912c | 44 | /* Domain/IP address of the server to contact */ |
mbed_official | 66:ce8709d9912c | 45 | const char SERVER_NAME[] = "os.mbed.com"; |
mbed_official | 76:b8c07be20122 | 46 | const char SERVER_ADDR[] = "os.mbed.com"; |
Janos Follath |
0:fc70c47eecb4 | 47 | |
mbed_official | 66:ce8709d9912c | 48 | /* Port used to connect to the server */ |
mbed_official | 66:ce8709d9912c | 49 | const int SERVER_PORT = 443; |
Janos Follath |
0:fc70c47eecb4 | 50 | |
Janos Follath |
0:fc70c47eecb4 | 51 | /** |
mbed_official | 66:ce8709d9912c | 52 | * The main function driving the HTTPS client. |
Janos Follath |
0:fc70c47eecb4 | 53 | */ |
mbed_official | 66:ce8709d9912c | 54 | int main() |
mbed_official | 66:ce8709d9912c | 55 | { |
mbed_official | 69:ffefb2e2d149 | 56 | int exit_code = MBEDTLS_EXIT_FAILURE; |
mbed_official | 69:ffefb2e2d149 | 57 | |
mbed_official | 86:e1ceb1075f1a | 58 | if((exit_code = mbedtls_platform_setup(NULL)) != 0) { |
mbed_official | 69:ffefb2e2d149 | 59 | printf("Platform initialization failed with error %d\r\n", exit_code); |
mbed_official | 69:ffefb2e2d149 | 60 | return MBEDTLS_EXIT_FAILURE; |
mbed_official | 69:ffefb2e2d149 | 61 | } |
mbed_official | 95:d282bc7f32e4 | 62 | |
mbed_official | 95:d282bc7f32e4 | 63 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
mbed_official | 95:d282bc7f32e4 | 64 | /* |
mbed_official | 95:d282bc7f32e4 | 65 | * Initialize underlying PSA Crypto implementation. |
mbed_official | 95:d282bc7f32e4 | 66 | * Even if the HTTPS client doesn't make use of |
mbed_official | 95:d282bc7f32e4 | 67 | * PSA-specific API, for example for setting opaque PSKs |
mbed_official | 95:d282bc7f32e4 | 68 | * or opaque private keys, Mbed TLS will use PSA |
mbed_official | 95:d282bc7f32e4 | 69 | * for public and symmetric key operations as well as |
mbed_official | 95:d282bc7f32e4 | 70 | * hashing. |
mbed_official | 95:d282bc7f32e4 | 71 | */ |
mbed_official | 95:d282bc7f32e4 | 72 | psa_status_t status; |
mbed_official | 95:d282bc7f32e4 | 73 | status = psa_crypto_init(); |
mbed_official | 95:d282bc7f32e4 | 74 | if( status != PSA_SUCCESS ) |
mbed_official | 95:d282bc7f32e4 | 75 | { |
mbed_official | 95:d282bc7f32e4 | 76 | printf("psa_crypto_init() failed with %d\r\n", status ); |
mbed_official | 95:d282bc7f32e4 | 77 | return MBEDTLS_EXIT_FAILURE; |
mbed_official | 95:d282bc7f32e4 | 78 | } |
mbed_official | 95:d282bc7f32e4 | 79 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
mbed_official | 95:d282bc7f32e4 | 80 | |
mbed_official | 66:ce8709d9912c | 81 | /* |
mbed_official | 66:ce8709d9912c | 82 | * The default 9600 bps is too slow to print full TLS debug info and could |
mbed_official | 66:ce8709d9912c | 83 | * cause the other party to time out. |
Janos Follath |
0:fc70c47eecb4 | 84 | */ |
Janos Follath |
0:fc70c47eecb4 | 85 | |
mbed_official | 66:ce8709d9912c | 86 | HelloHttpsClient *client; |
Janos Follath |
0:fc70c47eecb4 | 87 | |
mbed_official | 66:ce8709d9912c | 88 | mbedtls_printf("Starting mbed-os-example-tls/tls-client\n"); |
Janos Follath |
0:fc70c47eecb4 | 89 | |
mbed_official | 66:ce8709d9912c | 90 | #if defined(MBED_MAJOR_VERSION) |
mbed_official | 66:ce8709d9912c | 91 | mbedtls_printf("Using Mbed OS %d.%d.%d\n", |
mbed_official | 66:ce8709d9912c | 92 | MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
mbed_official | 66:ce8709d9912c | 93 | #else |
mbed_official | 66:ce8709d9912c | 94 | printf("Using Mbed OS from master.\n"); |
mbed_official | 66:ce8709d9912c | 95 | #endif /* MBEDTLS_MAJOR_VERSION */ |
Janos Follath |
0:fc70c47eecb4 | 96 | |
mbed_official | 66:ce8709d9912c | 97 | /* Allocate a HTTPS client */ |
mbed_official | 86:e1ceb1075f1a | 98 | client = new (std::nothrow) HelloHttpsClient(SERVER_NAME, SERVER_ADDR, SERVER_PORT); |
mbed_official | 76:b8c07be20122 | 99 | |
mbed_official | 66:ce8709d9912c | 100 | if (client == NULL) { |
mbed_official | 66:ce8709d9912c | 101 | mbedtls_printf("Failed to allocate HelloHttpsClient object\n" |
mbed_official | 66:ce8709d9912c | 102 | "\nFAIL\n"); |
mbed_official | 86:e1ceb1075f1a | 103 | mbedtls_platform_teardown(NULL); |
mbed_official | 66:ce8709d9912c | 104 | return exit_code; |
Janos Follath |
0:fc70c47eecb4 | 105 | } |
mbed_official | 33:34783d5deafd | 106 | |
mbed_official | 66:ce8709d9912c | 107 | /* Run the client */ |
mbed_official | 66:ce8709d9912c | 108 | if (client->run() != 0) { |
mbed_official | 66:ce8709d9912c | 109 | mbedtls_printf("\nFAIL\n"); |
mbed_official | 66:ce8709d9912c | 110 | } else { |
mbed_official | 66:ce8709d9912c | 111 | exit_code = MBEDTLS_EXIT_SUCCESS; |
mbed_official | 66:ce8709d9912c | 112 | mbedtls_printf("\nDONE\n"); |
Janos Follath |
0:fc70c47eecb4 | 113 | } |
Janos Follath |
0:fc70c47eecb4 | 114 | |
mbed_official | 66:ce8709d9912c | 115 | delete client; |
Janos Follath |
0:fc70c47eecb4 | 116 | |
mbed_official | 86:e1ceb1075f1a | 117 | mbedtls_platform_teardown(NULL); |
mbed_official | 66:ce8709d9912c | 118 | return exit_code; |
Janos Follath |
0:fc70c47eecb4 | 119 | } |