SIGN IN SIGN UP

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.

0 0 1 Java
2024-03-04 19:40:50 -05:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
*
* For information on the structure of the code examples and how to build and run the examples, see
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html.
*
**/
#include <aws/core/Aws.h>
#include <aws/iot/IoTClient.h>
#include <aws/iot/model/CreateKeysAndCertificateRequest.h>
#include <iostream>
#include <fstream>
#include "iot_samples.h"
// snippet-start:[cpp.example_code.iot.CreateKeysAndCertificate]
//! Create keys and certificate for an Aws IoT device.
//! This routine will save certificates and keys to an output folder, if provided.
/*!
\param outputFolder: Location for storing output in files, ignored when string is empty.
\param certificateARNResult: A string to receive the ARN of the created certificate.
\param certificateID: A string to receive the ID of the created certificate.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
bool AwsDoc::IoT::createKeysAndCertificate(const Aws::String &outputFolder,
Aws::String &certificateARNResult,
Aws::String &certificateID,
const Aws::Client::ClientConfiguration &clientConfiguration) {
Aws::IoT::IoTClient client(clientConfiguration);
Aws::IoT::Model::CreateKeysAndCertificateRequest createKeysAndCertificateRequest;
Aws::IoT::Model::CreateKeysAndCertificateOutcome outcome =
client.CreateKeysAndCertificate(createKeysAndCertificateRequest);
if (outcome.IsSuccess()) {
std::cout << "Successfully created a certificate and keys" << std::endl;
certificateARNResult = outcome.GetResult().GetCertificateArn();
certificateID = outcome.GetResult().GetCertificateId();
std::cout << "Certificate ARN: " << certificateARNResult << ", certificate ID: "
<< certificateID << std::endl;
if (!outputFolder.empty()) {
std::cout << "Writing certificate and keys to the folder '" << outputFolder
<< "'." << std::endl;
std::cout << "Be sure these files are stored securely." << std::endl;
Aws::String certificateFilePath = outputFolder + "/certificate.pem.crt";
std::ofstream certificateFile(certificateFilePath);
if (!certificateFile.is_open()) {
std::cerr << "Error opening certificate file, '" << certificateFilePath
<< "'."
<< std::endl;
return false;
}
certificateFile << outcome.GetResult().GetCertificatePem();
certificateFile.close();
const Aws::IoT::Model::KeyPair &keyPair = outcome.GetResult().GetKeyPair();
Aws::String privateKeyFilePath = outputFolder + "/private.pem.key";
std::ofstream privateKeyFile(privateKeyFilePath);
if (!privateKeyFile.is_open()) {
std::cerr << "Error opening private key file, '" << privateKeyFilePath
<< "'."
<< std::endl;
return false;
}
privateKeyFile << keyPair.GetPrivateKey();
privateKeyFile.close();
Aws::String publicKeyFilePath = outputFolder + "/public.pem.key";
std::ofstream publicKeyFile(publicKeyFilePath);
if (!publicKeyFile.is_open()) {
std::cerr << "Error opening public key file, '" << publicKeyFilePath
<< "'."
<< std::endl;
return false;
}
publicKeyFile << keyPair.GetPublicKey();
}
}
else {
std::cerr << "Error creating keys and certificate: "
<< outcome.GetError().GetMessage() << std::endl;
}
return outcome.IsSuccess();
}
// snippet-end:[cpp.example_code.iot.CreateKeysAndCertificate]
/*
*
* main function
*
* Usage: 'run_create_keys_and_certificate <output_folder>'
*
*/
#ifndef EXCLUDE_ACTION_MAIN
int main(int argc, char **argv) {
if (argc != 2) {
std::cout << "Usage: run_create_keys_and_certificate <output_folder>'"
<< std::endl;
return 1;
}
Aws::SDKOptions options;
Aws::InitAPI(options);
{
const Aws::String outputFolder(argv[1]);
Aws::Client::ClientConfiguration clientConfig;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
Aws::String certificateARN;
Aws::String certificateID;
AwsDoc::IoT::createKeysAndCertificate(outputFolder, certificateARN,
certificateID, clientConfig);
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // EXCLUDE_ACTION_MAIN