-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PKCS12 connect sample #581
Merged
Merged
Changes from 52 commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
63e89c0
Add PKCS12 sample
TwistedTwigleg 32c6baf
Fix CI yaml
TwistedTwigleg b93428e
Need to compile sample
TwistedTwigleg 3493c36
Fix error due to struct initialization
TwistedTwigleg fb67b67
Is the sample stalling or getting the credentials?
TwistedTwigleg 32af3a4
Adjust PKCS12 file paths
TwistedTwigleg d3e406a
Run the sample directly to, hopefully, see sample logs
TwistedTwigleg ce417c8
Try PKCS12 key path a different way
TwistedTwigleg 668ec6c
Revert back to PKCS12 CI code from JS
TwistedTwigleg 03502c8
Pipe the output from running the sample to hopefully see why it is st…
TwistedTwigleg 335361f
Try adding a timeout to see the output?
TwistedTwigleg c0c938a
Try turning on logs
TwistedTwigleg 5c73f4e
Print the output on timeout
TwistedTwigleg 6463658
Use a pipe to get the output?
TwistedTwigleg 1031f23
Output to CRT logs and try to print that file
TwistedTwigleg 90c551d
Try a relative path for the log file?
TwistedTwigleg e0a9065
Does it even hit the executable? Try passing --help to see if that works
TwistedTwigleg 286b34c
Try running directly with logging
TwistedTwigleg 2216d39
Pass the endpoint directly to see if that resolves the endpoint address
TwistedTwigleg 3e82de4
Merge branch 'main' into pkcs12_sample
TwistedTwigleg 588aa02
Try a hard-coded PKCS12 password to see if it makes a difference
TwistedTwigleg 94f69fa
Try relative paths?
TwistedTwigleg 2fe92b8
Try again
TwistedTwigleg 53a55d5
Revert back to how Python does it again
TwistedTwigleg f64ab14
Modified wrong file path
TwistedTwigleg c8c4fb1
Try getting the PKCS12 key from S3
TwistedTwigleg deb541d
Merge branch 'main' of https://github.com/aws/aws-iot-device-sdk-cpp-…
xiazhvera 8180cee
use a tmp pkcs12 key file path
xiazhvera bb9c14b
test with sudo python3
xiazhvera 5d69e76
update permission
xiazhvera 81df7d3
test with logs
xiazhvera e673793
update security permision
xiazhvera 28a7e7e
update key file path
xiazhvera 0579b77
test with sudo
xiazhvera b7a4cc6
test github action
xiazhvera 6246ca5
test github security
xiazhvera 1d9f762
fix yml
xiazhvera e40efb1
fix yaml
xiazhvera 579b681
fix import pkcs12 key path
xiazhvera 57a6099
try access identity from keychain
xiazhvera 3cbf6de
test identity
xiazhvera 08414b5
test different keychain
xiazhvera 85aa53f
try create local pkcs12 file
xiazhvera 3eeaaf1
fix yaml
xiazhvera 2265ffc
remove comments
xiazhvera c119a4f
update pkcs12 passworkd
xiazhvera 4e1aa9c
test with identity file
xiazhvera 57f6805
kick ci
xiazhvera 5ca781a
clean up the secrets and sample
xiazhvera 90825df
kick ci
xiazhvera 7972f65
fix file path
xiazhvera 268cfc9
clean up commands
xiazhvera 7df1496
improve ci.ym;
xiazhvera 95b1340
Merge branch 'main' of https://github.com/aws/aws-iot-device-sdk-cpp-…
xiazhvera 10e8a65
remove unnecssary file
xiazhvera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"language": "CPP", | ||
"sample_file": "./aws-iot-device-sdk-cpp-v2/build/samples/mqtt/pkcs12_connect/pkcs12-connect", | ||
"sample_region": "us-east-1", | ||
"sample_main_class": "", | ||
"arguments": [ | ||
{ | ||
"name": "--endpoint", | ||
"secret": "ci/endpoint" | ||
}, | ||
{ | ||
"name": "--pkcs12_file", | ||
"data": "./iot_pkcs12_key.p12" | ||
}, | ||
{ | ||
"name": "--pkcs12_password", | ||
"secret": "ci/PubSub/key_pkcs12_password" | ||
}, | ||
{ | ||
"name": "--verbosity", | ||
"data": "Trace" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[INFO] [2023-07-17T23:20:17Z] [00000001e25d1e00] [event-loop] - id=0x101905230: Initializing edge-triggered kqueue | ||
[INFO] [2023-07-17T23:20:17Z] [00000001e25d1e00] [event-loop] - id=0x101905230: starting event-loop thread. | ||
[INFO] [2023-07-17T23:20:17Z] [00000001e25d1e00] [event-loop] - id=0x101a047c0: Initializing edge-triggered kqueue | ||
[INFO] [2023-07-17T23:20:17Z] [000000016ff13000] [event-loop] - id=0x101905230: main loop started | ||
[INFO] [2023-07-17T23:20:17Z] [00000001e25d1e00] [event-loop] - id=0x101a047c0: starting event-loop thread. | ||
[INFO] [2023-07-17T23:20:17Z] [000000016ff13000] [event-loop] - id=0x101905230: default timeout 100s, and max events to process per tick 100 | ||
[INFO] [2023-07-17T23:20:17Z] [00000001e25d1e00] [event-loop] - id=0x101906060: Initializing edge-triggered kqueue | ||
[INFO] [2023-07-17T23:20:17Z] [000000016ff9f000] [event-loop] - id=0x101a047c0: main loop started | ||
[INFO] [2023-07-17T23:20:17Z] [00000001e25d1e00] [event-loop] - id=0x101906060: starting event-loop thread. | ||
[INFO] [2023-07-17T23:20:17Z] [000000016ff9f000] [event-loop] - id=0x101a047c0: default timeout 100s, and max events to process per tick 100 | ||
[INFO] [2023-07-17T23:20:17Z] [00000001e25d1e00] [event-loop] - id=0x1019060b0: Initializing edge-triggered kqueue | ||
[INFO] [2023-07-17T23:20:17Z] [000000017002b000] [event-loop] - id=0x101906060: main loop started | ||
[INFO] [2023-07-17T23:20:17Z] [000000017002b000] [event-loop] - id=0x101906060: default timeout 100s, and max events to process per tick 100 | ||
[INFO] [2023-07-17T23:20:17Z] [00000001e25d1e00] [event-loop] - id=0x1019060b0: starting event-loop thread. | ||
[INFO] [2023-07-17T23:20:17Z] [00000001e25d1e00] [event-loop] - id=0x101905dd0: Initializing edge-triggered kqueue | ||
[INFO] [2023-07-17T23:20:17Z] [00000001700b7000] [event-loop] - id=0x1019060b0: main loop started | ||
[INFO] [2023-07-17T23:20:17Z] [00000001e25d1e00] [event-loop] - id=0x101905dd0: starting event-loop thread. | ||
[INFO] [2023-07-17T23:20:17Z] [00000001700b7000] [event-loop] - id=0x1019060b0: default timeout 100s, and max events to process per tick 100 | ||
[INFO] [2023-07-17T23:20:17Z] [0000000170143000] [event-loop] - id=0x101905dd0: main loop started | ||
[INFO] [2023-07-17T23:20:17Z] [0000000170143000] [event-loop] - id=0x101905dd0: default timeout 100s, and max events to process per tick 100 | ||
[INFO] [2023-07-17T23:20:17Z] [00000001e25d1e00] [dns] - id=0x101a04eb0: Initializing default host resolver with 1 max host entries. | ||
[INFO] [2023-07-17T23:20:17Z] [00000001e25d1e00] [channel-bootstrap] - id=0x101a04fa0: Initializing client bootstrap with event-loop group 0x1019051d0 | ||
[DEBUG] [2023-07-17T23:20:17Z] [00000001e25d1e00] [mqtt-client] - client=0x101a05010: Initalizing MQTT client | ||
[DEBUG] [2023-07-17T23:20:17Z] [00000001e25d1e00] [channel-bootstrap] - id=0x101a04fa0: acquiring bootstrap reference | ||
[DEBUG] [2023-07-17T23:20:17Z] [00000001e25d1e00] [mqtt-client] - client=0x101a05010: Cleaning up MQTT client | ||
[DEBUG] [2023-07-17T23:20:17Z] [00000001e25d1e00] [channel-bootstrap] - id=0x101a04fa0: releasing bootstrap reference | ||
[DEBUG] [2023-07-17T23:20:20Z] [00000001e25d1e00] [tls-handler] - static: certificate and key have been set, setting them up now. | ||
[ERROR] [2023-07-17T23:20:20Z] [00000001e25d1e00] [pki-utils] - static: error importing ECC private key with OSStatus -25257 | ||
[ERROR] [2023-07-17T23:20:20Z] [00000001e25d1e00] [tls-handler] - static: failed to import certificate and private key with error 1038. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
cmake_minimum_required(VERSION 3.1) | ||
# note: cxx-17 requires cmake 3.8, cxx-20 requires cmake 3.12 | ||
project(pkcs12-connect CXX) | ||
|
||
file(GLOB SRC_FILES | ||
"*.cpp" | ||
"../../utils/CommandLineUtils.cpp" | ||
"../../utils/CommandLineUtils.h" | ||
) | ||
|
||
add_executable(${PROJECT_NAME} ${SRC_FILES}) | ||
|
||
set_target_properties(${PROJECT_NAME} PROPERTIES | ||
CXX_STANDARD 14) | ||
|
||
#set warnings | ||
if (MSVC) | ||
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /WX /wd4068) | ||
else () | ||
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wno-long-long -pedantic -Werror) | ||
endif () | ||
|
||
find_package(aws-crt-cpp REQUIRED) | ||
|
||
target_link_libraries(${PROJECT_NAME} AWS::aws-crt-cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# PKCS12 Connect | ||
|
||
[**Return to main sample list**](../README.md) | ||
|
||
This sample is similar to the [Basic Connect](../basic_connect/README.md) sample, in that it connects via Mutual TLS (mTLS) using a certificate and key file. However, unlike the Basic Connect where the certificate and private key file are stored on disk, this sample uses a PKCS#12 file instead. | ||
|
||
**WARNING: MacOS only**. Currently, TLS integration with PKCS12 is only available on MacOS devices. | ||
|
||
Your IoT Core Thing's [Policy](https://docs.aws.amazon.com/iot/latest/developerguide/iot-policies.html) must provide privileges for this sample to connect. Below is a sample policy that can be used on your IoT Core Thing that will allow this sample to run as intended. | ||
|
||
<details> | ||
<summary>(see sample policy)</summary> | ||
<pre> | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"iot:Connect" | ||
], | ||
"Resource": [ | ||
"arn:aws:iot:<b>region</b>:<b>account</b>:client/test-*" | ||
] | ||
} | ||
] | ||
} | ||
</pre> | ||
|
||
Replace with the following with the data from your AWS account: | ||
* `<region>`: The AWS IoT Core region where you created your AWS IoT Core thing you wish to use with this sample. For example `us-east-1`. | ||
* `<account>`: Your AWS IoT Core account ID. This is the set of numbers in the top right next to your AWS account name when using the AWS IoT Core website. | ||
|
||
Note that in a real application, you may want to avoid the use of wildcards in your ClientID or use them selectively. Please follow best practices when working with AWS on production applications using the SDK. Also, for the purposes of this sample, please make sure your policy allows a client ID of `test-*` to connect or use `--client_id <client ID here>` to send the client ID your policy supports. | ||
|
||
</details> | ||
|
||
## How to run | ||
|
||
This sample can be run using the following command: | ||
|
||
```sh | ||
./pkcs12-connect --endpoint <endpoint> --pkcs12_file <path to PKCS12 file> --pkcs12_password <password for PKCS12 file> | ||
``` | ||
|
||
You can also pass a Certificate Authority file (CA) if your certificate and key combination requires it: | ||
|
||
```sh | ||
./pkcs12-connect --endpoint <endpoint> --pkcs12_file <path to PKCS12 file> --pkcs12_password <password for PKCS12 file> --ca_file <path to CA file> | ||
``` | ||
|
||
### How to setup and run | ||
|
||
To use the certificate and key files provided by AWS IoT Core, you will need to convert them into PKCS#12 format and then import them into your Java keystore. You can convert the certificate and key file to PKCS12 using the following command: | ||
|
||
```sh | ||
openssl pkcs12 -export -in <my-certificate.pem.crt> -inkey <my-private-key.pem.key> -out <my-pkcs12-key.pem.key> -name <alias here> -password pass:<password here> | ||
``` | ||
|
||
Once converted, you can then run the PKCS12 connect sample with the following: | ||
|
||
```sh | ||
./pkcs12-connect --endpoint <endpoint> --pkcs12_file <path to PKCS12 file> --pkcs12_password <password for PKCS12 file> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/** | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
#include <aws/crt/Api.h> | ||
#include <aws/crt/UUID.h> | ||
#include <aws/crt/io/Pkcs11.h> | ||
|
||
#include "../../utils/CommandLineUtils.h" | ||
|
||
using namespace Aws::Crt; | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
|
||
/************************ Setup ****************************/ | ||
|
||
// Do the global initialization for the API. | ||
ApiHandle apiHandle; | ||
|
||
/** | ||
* cmdData is the arguments/input from the command line placed into a single struct for | ||
* use in this sample. This handles all of the command line parsing, validating, etc. | ||
* See the Utils/CommandLineUtils for more information. | ||
*/ | ||
Utils::cmdData cmdData = Utils::parseSampleInputPKCS12Connect(argc, argv, &apiHandle); | ||
|
||
// Create the MQTT builder and populate it with data from cmdData. | ||
Aws::Iot::MqttClient client; | ||
struct Aws::Iot::Pkcs12Options options; | ||
options.pkcs12_file = cmdData.input_pkcs12File; | ||
options.pkcs12_password = cmdData.input_pkcs12Password; | ||
Aws::Iot::MqttClientConnectionConfigBuilder clientConfigBuilder(options); | ||
if (!clientConfigBuilder) | ||
{ | ||
fprintf( | ||
stderr, | ||
"MqttClientConnectionConfigBuilder failed: %s\n", | ||
Aws::Crt::ErrorDebugString(Aws::Crt::LastError())); | ||
exit(-1); | ||
} | ||
if (cmdData.input_ca != "") | ||
{ | ||
clientConfigBuilder.WithCertificateAuthority(cmdData.input_ca.c_str()); | ||
} | ||
clientConfigBuilder.WithEndpoint(cmdData.input_endpoint); | ||
|
||
// Create the MQTT connection from the MQTT builder | ||
auto clientConfig = clientConfigBuilder.Build(); | ||
if (!clientConfig) | ||
{ | ||
fprintf( | ||
stderr, | ||
"Client Configuration initialization failed with error %s\n", | ||
Aws::Crt::ErrorDebugString(clientConfig.LastError())); | ||
exit(-1); | ||
} | ||
auto connection = client.NewConnection(clientConfig); | ||
if (!*connection) | ||
{ | ||
fprintf( | ||
stderr, | ||
"MQTT Connection Creation failed with error %s\n", | ||
Aws::Crt::ErrorDebugString(connection->LastError())); | ||
exit(-1); | ||
} | ||
|
||
/** | ||
* In a real world application you probably don't want to enforce synchronous behavior | ||
* but this is a sample console application, so we'll just do that with a condition variable. | ||
*/ | ||
std::promise<bool> connectionCompletedPromise; | ||
std::promise<void> connectionClosedPromise; | ||
|
||
// Invoked when a MQTT connect has completed or failed | ||
auto onConnectionCompleted = | ||
[&](Aws::Crt::Mqtt::MqttConnection &, int errorCode, Aws::Crt::Mqtt::ReturnCode returnCode, bool) { | ||
if (errorCode) | ||
{ | ||
fprintf(stdout, "Connection failed with error %s\n", Aws::Crt::ErrorDebugString(errorCode)); | ||
connectionCompletedPromise.set_value(false); | ||
} | ||
else | ||
{ | ||
fprintf(stdout, "Connection completed with return code %d\n", returnCode); | ||
connectionCompletedPromise.set_value(true); | ||
} | ||
}; | ||
|
||
// Invoked when a MQTT connection was interrupted/lost | ||
auto onInterrupted = [&](Aws::Crt::Mqtt::MqttConnection &, int error) { | ||
fprintf(stdout, "Connection interrupted with error %s\n", Aws::Crt::ErrorDebugString(error)); | ||
}; | ||
|
||
// Invoked when a MQTT connection was interrupted/lost, but then reconnected successfully | ||
auto onResumed = [&](Aws::Crt::Mqtt::MqttConnection &, Aws::Crt::Mqtt::ReturnCode, bool) { | ||
fprintf(stdout, "Connection resumed\n"); | ||
}; | ||
|
||
// Invoked when a disconnect message has completed. | ||
auto onDisconnect = [&](Aws::Crt::Mqtt::MqttConnection &) { | ||
fprintf(stdout, "Disconnect completed\n"); | ||
connectionClosedPromise.set_value(); | ||
}; | ||
|
||
// Assign callbacks | ||
connection->OnConnectionCompleted = std::move(onConnectionCompleted); | ||
connection->OnDisconnect = std::move(onDisconnect); | ||
connection->OnConnectionInterrupted = std::move(onInterrupted); | ||
connection->OnConnectionResumed = std::move(onResumed); | ||
|
||
/************************ Run the sample ****************************/ | ||
|
||
// Connect | ||
fprintf(stdout, "Connecting...\n"); | ||
if (!connection->Connect(cmdData.input_clientId.c_str(), false /*cleanSession*/, 1000 /*keepAliveTimeSecs*/)) | ||
{ | ||
fprintf(stderr, "MQTT Connection failed with error %s\n", Aws::Crt::ErrorDebugString(connection->LastError())); | ||
exit(-1); | ||
} | ||
|
||
// wait for the OnConnectionCompleted callback to fire, which sets connectionCompletedPromise... | ||
if (connectionCompletedPromise.get_future().get() == false) | ||
{ | ||
fprintf(stderr, "Connection failed\n"); | ||
exit(-1); | ||
} | ||
|
||
// Disconnect | ||
if (connection->Disconnect()) | ||
{ | ||
connectionClosedPromise.get_future().wait(); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file should be removed?