Skip to content

Commit

Permalink
Change commands in configuration mode
Browse files Browse the repository at this point in the history
  • Loading branch information
matsujirushi committed Oct 27, 2020
1 parent bf1c7c5 commit af7bab5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 20 deletions.
1 change: 1 addition & 0 deletions include/Signature.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
#include <string>

std::string GenerateEncryptedSignature(const std::string& symmetricKey, const std::vector<uint8_t>& signature);
std::string ComputeDerivedSymmetricKey(const std::string& masterKey, const std::string& registrationId);
59 changes: 39 additions & 20 deletions src/CliMode.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <Arduino.h>
#include "CliMode.h"
#include "Storage.h"
#include "Signature.h"

#define END_CHAR ('\r')
#define TAB_CHAR ('\t')
Expand Down Expand Up @@ -28,19 +29,21 @@ static void display_settings_command(int argc, char** argv);
static void wifissid_command(int argc, char** argv);
static void wifipwd_Command(int argc, char** argv);
static void az_idscope_command(int argc, char** argv);
static void az_deviceid_command(int argc, char** argv);
static void az_saskey_command(int argc, char** argv);
static void az_regid_command(int argc, char** argv);
static void az_symkey_command(int argc, char** argv);
static void az_iotc_command(int argc, char** argv);

static const struct console_command cmds[] =
{
{"help" , "Help document" , help_command },
{"reset_factory_settings", "Reset factory settings" , reset_factory_settings_command },
{"show_settings" , "Display settings" , display_settings_command },
{"set_wifissid" , "Set Wi-Fi SSID" , wifissid_command },
{"set_wifipwd" , "Set Wi-Fi password" , wifipwd_Command },
{"set_az_idscope" , "Set id scope of Azure IoT Central" , az_idscope_command },
{"set_az_deviceid" , "Set device id of Azure IoT Central" , az_deviceid_command },
{"set_az_saskey" , "Set SAS key of Azure IoT Central" , az_saskey_command },
{"help" , "Help document" , help_command },
{"reset_factory_settings", "Reset factory settings" , reset_factory_settings_command },
{"show_settings" , "Display settings" , display_settings_command },
{"set_wifissid" , "Set Wi-Fi SSID" , wifissid_command },
{"set_wifipwd" , "Set Wi-Fi password" , wifipwd_Command },
{"set_az_idscope" , "Set id scope of Azure IoT DPS" , az_idscope_command },
{"set_az_regid" , "Set registration id of Azure IoT DPS" , az_regid_command },
{"set_az_symkey" , "Set symmetric key of Azure IoT DPS" , az_symkey_command },
{"set_az_iotc" , "Set connection information of Azure IoT Central", az_iotc_command }
};

static const int cmd_count = sizeof(cmds) / sizeof(cmds[0]);
Expand Down Expand Up @@ -72,9 +75,9 @@ static void display_settings_command(int argc, char** argv)
{
Serial.printf("Wi-Fi SSID = %s" DLM, Storage::WiFiSSID.c_str());
Serial.printf("Wi-Fi password = %s" DLM, Storage::WiFiPassword.c_str());
Serial.printf("Id scope of Azure IoT Central = %s" DLM, Storage::IdScope.c_str());
Serial.printf("Device id of Azure IoT Central = %s" DLM, Storage::RegistrationId.c_str());
Serial.printf("SAS key of Azure IoT Central = %s" DLM, Storage::SymmetricKey.c_str());
Serial.printf("Id scope of Azure IoT DPS = %s" DLM, Storage::IdScope.c_str());
Serial.printf("Registration id of Azure IoT DPS = %s" DLM, Storage::RegistrationId.c_str());
Serial.printf("Symmetric key of Azure IoT DPS = %s" DLM, Storage::SymmetricKey.c_str());
}

static void wifissid_command(int argc, char** argv)
Expand Down Expand Up @@ -109,7 +112,7 @@ static void az_idscope_command(int argc, char** argv)
{
if (argc != 2)
{
Serial.printf("ERROR: Usage: %s <Id scope>. Please provide the id scope of the Azure IoT Central." DLM, argv[0]);
Serial.printf("ERROR: Usage: %s <Id scope>. Please provide the id scope of the Azure IoT DPS." DLM, argv[0]);
return;
}

Expand All @@ -119,32 +122,48 @@ static void az_idscope_command(int argc, char** argv)
Serial.print("Set id scope successfully." DLM);
}

static void az_deviceid_command(int argc, char** argv)
static void az_regid_command(int argc, char** argv)
{
if (argc != 2)
{
Serial.printf("ERROR: Usage: %s <Device id>. Please provide the device id of the Azure IoT Central." DLM, argv[0]);
Serial.printf("ERROR: Usage: %s <Registration id>. Please provide the registraion id of the Azure IoT DPS." DLM, argv[0]);
return;
}

Storage::RegistrationId = argv[1];
Storage::Save();

Serial.print("Set device id successfully." DLM);
Serial.print("Set registration id successfully." DLM);
}

static void az_saskey_command(int argc, char** argv)
static void az_symkey_command(int argc, char** argv)
{
if (argc != 2)
{
Serial.printf("ERROR: Usage: %s <SAS key>. Please provide the SAS key of the Azure IoT Central." DLM, argv[0]);
Serial.printf("ERROR: Usage: %s <Symmetric key>. Please provide the symmetric key of the Azure IoT DPS." DLM, argv[0]);
return;
}

Storage::SymmetricKey = argv[1];
Storage::Save();

Serial.print("Set SAS key successfully." DLM);
Serial.print("Set symmetric key successfully." DLM);
}

static void az_iotc_command(int argc, char** argv)
{
if (argc != 4)
{
Serial.printf("ERROR: Usage: %s <Id scope> <SAS key> <Device id>." DLM, argv[0]);
return;
}

Storage::IdScope = argv[1];
Storage::RegistrationId = argv[3];
Storage::SymmetricKey = ComputeDerivedSymmetricKey(argv[2], argv[3]);
Storage::Save();

Serial.print("Set connection information of Azure IoT Central successfully." DLM);
}

static bool CliGetInput(char* inbuf, int* bp)
Expand Down
33 changes: 33 additions & 0 deletions src/Signature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,36 @@ std::string GenerateEncryptedSignature(const std::string& symmetricKey, const st

return std::string(b64encHmacsha256Signature, b64encHmacsha256SignatureLength);
}

std::string ComputeDerivedSymmetricKey(const std::string& masterKey, const std::string& registrationId)
{
unsigned char base64DecodedMasterKey[masterKey.size() + 1];

// Base64-decode device key
// <-- masterKey
// --> base64DecodedMasterKey
size_t base64DecodedMasterKeyLength;
if (mbedtls_base64_decode(base64DecodedMasterKey, sizeof(base64DecodedMasterKey), &base64DecodedMasterKeyLength, (unsigned char*)&masterKey[0], masterKey.size()) != 0) abort();
if (base64DecodedMasterKeyLength == 0) abort();

// SHA-256 encrypt
// <-- base64DecodedMasterKey
// <-- registrationId
// --> derivedSymmetricKey
uint8_t derivedSymmetricKey[32]; // SHA-256
mbedtls_md_context_t ctx;
const mbedtls_md_type_t mdType{ MBEDTLS_MD_SHA256 };
if (mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(mdType), 1) != 0) abort();
if (mbedtls_md_hmac_starts(&ctx, base64DecodedMasterKey, base64DecodedMasterKeyLength) != 0) abort();
if (mbedtls_md_hmac_update(&ctx, (const unsigned char*)&registrationId[0], registrationId.size()) != 0) abort();
if (mbedtls_md_hmac_finish(&ctx, derivedSymmetricKey) != 0) abort();

// Base64 encode encrypted signature
// <-- derivedSymmetricKey
// --> b64encDerivedSymmetricKey
char b64encDerivedSymmetricKey[(size_t)(sizeof(derivedSymmetricKey) * 1.5f) + 1];
size_t b64encDerivedSymmetricKeyLength;
if (mbedtls_base64_encode((unsigned char*)b64encDerivedSymmetricKey, sizeof(b64encDerivedSymmetricKey), &b64encDerivedSymmetricKeyLength, derivedSymmetricKey, mbedtls_md_get_size(mbedtls_md_info_from_type(mdType))) != 0) abort();

return std::string(b64encDerivedSymmetricKey, b64encDerivedSymmetricKeyLength);
}

0 comments on commit af7bab5

Please sign in to comment.