Linux: Support keys in config file (#1093)
- Signing and encryption keys are set through the configuration file. - SoftEeprom update.
This commit is contained in:
committed by
Mikael Falkvidd
parent
4d90c00f98
commit
3c0b2727a5
@@ -113,8 +113,20 @@ MySensors options:
|
||||
EOF
|
||||
}
|
||||
|
||||
# Colors
|
||||
GREEN='\033[0;32m'
|
||||
RED='\033[0;31m'
|
||||
YELLOW='\033[1;33m'
|
||||
LIGHT_BLUE='\033[1;34m'
|
||||
NO_COLOR='\033[0m'
|
||||
|
||||
OK="${GREEN}[OK]${NO_COLOR}"
|
||||
ERROR="${RED}[ERROR]${NO_COLOR}"
|
||||
FAILED="${YELLOW}[FAILED]${NO_COLOR}"
|
||||
SECTION="${LIGHT_BLUE}[SECTION]${NO_COLOR}"
|
||||
|
||||
function die {
|
||||
echo "[ERROR] $1"
|
||||
printf "${ERROR} $1 \n"
|
||||
exit $2
|
||||
}
|
||||
|
||||
@@ -497,12 +509,12 @@ CC=${CC:-gcc}
|
||||
CXX=${CXX:-g++}
|
||||
|
||||
if [ -z "${SOC}" ]; then
|
||||
echo "[SECTION] Detecting target machine."
|
||||
printf "${SECTION} Detecting target machine.\n"
|
||||
info=($(detect_machine))
|
||||
SOC=${info[0]}
|
||||
TYPE=${info[1]}
|
||||
CPU=${info[2]}
|
||||
echo " [OK] machine detected: SoC=${SOC}, Type=${TYPE}, CPU=${CPU}."
|
||||
printf " ${OK} machine detected: SoC=${SOC}, Type=${TYPE}, CPU=${CPU}.\n"
|
||||
fi
|
||||
|
||||
if [ -z "${CPUFLAGS}" ]; then
|
||||
@@ -512,23 +524,23 @@ fi
|
||||
if [[ $SOC == "BCM2835" || $SOC == "BCM2836" || $SOC == "BCM2837" ]]; then
|
||||
CPPFLAGS="-DLINUX_ARCH_RASPBERRYPI $CPPFLAGS"
|
||||
else
|
||||
echo "[SECTION] Checking GPIO Sysfs."
|
||||
printf "${SECTION} Checking GPIO Sysfs.\n"
|
||||
if [[ $(eval 'ls /sys/class/gpio/export 2>/dev/null') ]]; then
|
||||
echo " [OK] /sys/class/gpio/export found"
|
||||
printf " ${OK} /sys/class/gpio/export found.\n"
|
||||
else
|
||||
echo " [WARNING] /sys/class/gpio/export not found."
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -z "${SPI_DRIVER}" ]; then
|
||||
echo "[SECTION] Detecting SPI driver."
|
||||
printf "${SECTION} Detecting SPI driver.\n"
|
||||
if [[ $SOC == "BCM2835" || $SOC == "BCM2836" || $SOC == "BCM2837" ]]; then
|
||||
SPI_DRIVER=BCM
|
||||
elif [[ $(eval 'ls /dev/spidev* 2>/dev/null') ]]; then
|
||||
SPI_DRIVER=SPIDEV
|
||||
fi
|
||||
if [ -n "${SPI_DRIVER}" ]; then
|
||||
echo " [OK] SPI driver detected:${SPI_DRIVER}."
|
||||
printf " ${OK} SPI driver detected:${SPI_DRIVER}.\n"
|
||||
else
|
||||
echo " [WARNING] No supported SPI driver detected. Using SPIDEV."
|
||||
SPI_DRIVER=SPIDEV
|
||||
@@ -551,6 +563,8 @@ if [ -n "${SPI_DRIVER}" ]; then
|
||||
esac
|
||||
fi
|
||||
|
||||
printf "${SECTION} Gateway configuration.\n"
|
||||
|
||||
if [[ ${debug} == "enable" ]]; then
|
||||
CPPFLAGS="-DMY_DEBUG $CPPFLAGS"
|
||||
fi
|
||||
@@ -567,6 +581,7 @@ elif [[ ${gateway_type} == "mqtt" ]]; then
|
||||
else
|
||||
die "Invalid gateway type." 2
|
||||
fi
|
||||
printf " ${OK} Type: ${gateway_type}.\n"
|
||||
|
||||
if [[ ${transport_type} == "none" ]]; then
|
||||
# Transport disabled
|
||||
@@ -582,20 +597,22 @@ elif [[ ${transport_type} == "rfm95" ]]; then
|
||||
else
|
||||
die "Invalid transport type." 3
|
||||
fi
|
||||
printf " ${OK} Transport: ${transport_type}.\n"
|
||||
|
||||
if [[ ${signing} == "none" ]]; then
|
||||
# Signing disabled
|
||||
:
|
||||
printf " ${OK} Signing: Disabled.\n"
|
||||
elif [[ ${signing} == "software" ]]; then
|
||||
CPPFLAGS="-DMY_SIGNING_SOFT $CPPFLAGS"
|
||||
if [[ ${signing_request_signatures} == true ]]; then
|
||||
CPPFLAGS="-DMY_SIGNING_REQUEST_SIGNATURES $CPPFLAGS"
|
||||
fi
|
||||
printf " ${OK} Signing: Enabled - Using key from config file.\n"
|
||||
elif [[ ${signing} == "password" ]]; then
|
||||
if [ -z "${security_password}" ]; then
|
||||
die "You need to set the password for signing with --my-security-password= option" 6
|
||||
die "You need to set the password for signing with --my-security-password option" 6
|
||||
fi
|
||||
echo "Simplified signing using password"
|
||||
printf " ${OK} Signing: Enabled - Simplified signing using password.\n"
|
||||
CPPFLAGS="-DMY_SIGNING_SIMPLE_PASSWD=\\\"${security_password}\\\" $CPPFLAGS"
|
||||
else
|
||||
die "Invalid signing type." 7
|
||||
@@ -604,41 +621,44 @@ fi
|
||||
if [[ ${encryption} == true ]]; then
|
||||
# Encryption enabled on some transport
|
||||
if [ -z "${security_password}" ]; then
|
||||
echo "Encryption key from configuration"
|
||||
printf " ${OK} Encryption: Enabled - Using key from config file.\n"
|
||||
else
|
||||
echo "Simplified encryption using password"
|
||||
printf " ${OK} Encryption: Enabled - Simplified encryption using password.\n"
|
||||
CPPFLAGS="-DMY_ENCRYPTION_SIMPLE_PASSWD=\\\"${security_password}\\\" $CPPFLAGS"
|
||||
fi
|
||||
else
|
||||
printf " ${OK} Encryption: Disabled.\n"
|
||||
fi
|
||||
|
||||
LDFLAGS="-pthread $LDFLAGS"
|
||||
CPPFLAGS="$CPUFLAGS $CPPFLAGS"
|
||||
|
||||
echo "[SECTION] Detecting init system."
|
||||
printf "${SECTION} Detecting init system.\n"
|
||||
if [ "${NO_INIT}" ]; then
|
||||
echo " [OK] no init system chosen."
|
||||
printf " ${OK} No init system chosen.\n"
|
||||
elif [ -x /usr/bin/systemctl ] || [ -x /bin/systemctl ]; then
|
||||
INIT_SYSTEM=systemd
|
||||
echo " [OK] init system detected: systemd."
|
||||
printf " ${OK} Init system detected: systemd.\n"
|
||||
elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
|
||||
INIT_SYSTEM=sysvinit
|
||||
echo " [OK] init system detected: sysvinit."
|
||||
printf " ${OK} Init system detected: sysvinit.\n"
|
||||
else
|
||||
echo " [FAILED] unknown init system."
|
||||
printf " ${FAILED} Unknown init system."
|
||||
fi
|
||||
|
||||
echo "[SECTION] Saving configuration."
|
||||
printf "${SECTION} Saving configuration.\n"
|
||||
echo -n "" > Makefile.inc
|
||||
for param in ${params}; do
|
||||
if [[ ${!param} ]]; then
|
||||
echo "${param}=${!param}" >> Makefile.inc
|
||||
fi
|
||||
done
|
||||
printf " ${OK} Saved.\n"
|
||||
|
||||
if [ -z "${NO_CLEAN}" ]; then
|
||||
echo "[SECTION] Cleaning previous builds."
|
||||
printf "${SECTION} Cleaning previous builds.\n"
|
||||
make clean >/dev/null
|
||||
fi
|
||||
|
||||
|
||||
echo "[OK] Finished."
|
||||
printf " ${OK} Finished.\n"
|
||||
|
||||
+4
-4
@@ -182,10 +182,10 @@
|
||||
* * To generate HMAC key @verbatim --gen-soft-hmac-key @endverbatim
|
||||
* * To generate %AES key @verbatim --gen-aes-key @endverbatim
|
||||
* * To generate a soft serial number @verbatim --gen-soft-serial @endverbatim
|
||||
* 2. Store keys/values to the gateway, execute @c mysgw with arguments
|
||||
* * To store HMAC key @verbatim --set-soft-hmac-key=<DATA> @endverbatim
|
||||
* * To store %AES key @verbatim --set-aes-key=<DATA> @endverbatim
|
||||
* * To store soft serial number @verbatim --set-soft-serial-key=<DATA> @endverbatim
|
||||
* 2. Update the gateway config file with the generated keys/valeus
|
||||
* * For HMAC key @verbatim soft_hmac_key=<DATA> @endverbatim
|
||||
* * For %AES key @verbatim aes_key=<DATA> @endverbatim
|
||||
* * For soft serial number @verbatim soft_serial_key=<DATA> @endverbatim
|
||||
*
|
||||
* You are now set and ready to use message signing in your network.
|
||||
* As of now, the following restrictions will be applied to your nodes:
|
||||
|
||||
@@ -60,9 +60,6 @@ int SoftEeprom::init(const char *fileName, size_t length)
|
||||
|
||||
_length = length;
|
||||
_values = new uint8_t[_length];
|
||||
for (size_t i = 0; i < _length; ++i) {
|
||||
_values[i] = 0xFF;
|
||||
}
|
||||
|
||||
if (stat(_fileName, &fileInfo) != 0) {
|
||||
//File does not exist. Create it.
|
||||
@@ -72,11 +69,16 @@ int SoftEeprom::init(const char *fileName, size_t length)
|
||||
logError("Unable to create config file %s.\n", _fileName);
|
||||
return -1;
|
||||
}
|
||||
// Fill the eeprom with 1s
|
||||
for (size_t i = 0; i < _length; ++i) {
|
||||
_values[i] = 0xFF;
|
||||
}
|
||||
myFile.write((const char*)_values, _length);
|
||||
myFile.close();
|
||||
} else if (fileInfo.st_size < 0 || (size_t)fileInfo.st_size != _length) {
|
||||
logError("EEPROM file %s is not the correct size of %zu. Please remove the file and a new one will be created.\n",
|
||||
_fileName, _length);
|
||||
destroy();
|
||||
return -1;
|
||||
} else {
|
||||
//Read config into local memory.
|
||||
@@ -100,27 +102,19 @@ void SoftEeprom::destroy()
|
||||
if (_fileName) {
|
||||
free(_fileName);
|
||||
}
|
||||
_length = 0;
|
||||
}
|
||||
|
||||
void SoftEeprom::readBlock(void* buf, void* addr, size_t length)
|
||||
{
|
||||
static bool config_to_mem = false;
|
||||
unsigned long int offs = reinterpret_cast<unsigned long int>(addr);
|
||||
|
||||
if (!config_to_mem && length) {
|
||||
//Read config into local memory.
|
||||
std::ifstream myFile(_fileName, std::ios::in | std::ios::binary);
|
||||
if (!myFile) {
|
||||
logError("Unable to open config to file %s for reading.\n", _fileName);
|
||||
exit(1);
|
||||
}
|
||||
myFile.read((char*)_values, _length);
|
||||
myFile.close();
|
||||
|
||||
config_to_mem = true;
|
||||
if (!length) {
|
||||
logError("EEPROM being read without being initialized!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (length && offs + length <= _length) {
|
||||
if (offs + length <= _length) {
|
||||
memcpy(buf, _values+offs, length);
|
||||
}
|
||||
}
|
||||
@@ -129,7 +123,16 @@ void SoftEeprom::writeBlock(void* buf, void* addr, size_t length)
|
||||
{
|
||||
unsigned long int offs = reinterpret_cast<unsigned long int>(addr);
|
||||
|
||||
if (length && offs + length <= _length) {
|
||||
if (!length) {
|
||||
logError("EEPROM being written without being initialized!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (offs + length <= _length) {
|
||||
if (memcmp(_values+offs, buf, length) == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(_values+offs, buf, length);
|
||||
|
||||
std::ofstream myFile(_fileName, std::ios::out | std::ios::in | std::ios::binary);
|
||||
|
||||
+61
-6
@@ -49,12 +49,15 @@ int config_parse(const char *config_file)
|
||||
return -1;
|
||||
}
|
||||
|
||||
conf.verbose = 4;
|
||||
conf.verbose = 7;
|
||||
conf.log_pipe = 0;
|
||||
conf.log_pipe_file = NULL;
|
||||
conf.syslog = 0;
|
||||
conf.eeprom_file = NULL;
|
||||
conf.eeprom_size = 1024;
|
||||
conf.eeprom_size = 0;
|
||||
conf.soft_hmac_key = NULL;
|
||||
conf.soft_serial_key = NULL;
|
||||
conf.aes_key = NULL;
|
||||
|
||||
while (fgets(buf, 1024, fptr)) {
|
||||
if (buf[0] != '#' && buf[0] != 10 && buf[0] != 13) {
|
||||
@@ -145,6 +148,21 @@ int config_parse(const char *config_file)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
} else if (!strncmp(buf, "soft_hmac_key=", 14)) {
|
||||
if (_config_parse_string(&(buf[14]), "soft_hmac_key", &conf.soft_hmac_key)) {
|
||||
fclose(fptr);
|
||||
return -1;
|
||||
}
|
||||
} else if (!strncmp(buf, "soft_serial_key=", 16)) {
|
||||
if (_config_parse_string(&(buf[16]), "soft_serial_key", &conf.soft_serial_key)) {
|
||||
fclose(fptr);
|
||||
return -1;
|
||||
}
|
||||
} else if (!strncmp(buf, "aes_key=", 8)) {
|
||||
if (_config_parse_string(&(buf[8]), "aes_key", &conf.aes_key)) {
|
||||
fclose(fptr);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
logWarning("Unknown config option \"%s\".\n", buf);
|
||||
}
|
||||
@@ -172,12 +190,24 @@ int config_parse(const char *config_file)
|
||||
|
||||
void config_cleanup(void)
|
||||
{
|
||||
if (conf.eeprom_file) {
|
||||
free(conf.eeprom_file);
|
||||
if (conf.log_filepath) {
|
||||
free(conf.log_filepath);
|
||||
}
|
||||
if (conf.log_pipe_file) {
|
||||
free(conf.log_pipe_file);
|
||||
}
|
||||
if (conf.eeprom_file) {
|
||||
free(conf.eeprom_file);
|
||||
}
|
||||
if (conf.soft_hmac_key) {
|
||||
free(conf.soft_hmac_key);
|
||||
}
|
||||
if (conf.soft_serial_key) {
|
||||
free(conf.soft_serial_key);
|
||||
}
|
||||
if (conf.aes_key) {
|
||||
free(conf.aes_key);
|
||||
}
|
||||
}
|
||||
|
||||
int _config_create(const char *config_file)
|
||||
@@ -185,12 +215,15 @@ int _config_create(const char *config_file)
|
||||
FILE *myFile;
|
||||
int ret;
|
||||
|
||||
const char default_conf[] = "# Logging verbosity: debug,info,notice,warn,err\n" \
|
||||
const char default_conf[] = "# Logging\n" \
|
||||
"# Verbosity: debug,info,notice,warn,err\n" \
|
||||
"verbose=debug\n" \
|
||||
"\n" \
|
||||
"# Enable logging to a file.\n" \
|
||||
"log_file=0\n" \
|
||||
"# Log file path.\n" \
|
||||
"log_filepath=/tmp/mysgw.log\n" \
|
||||
"\n" \
|
||||
"# Enable logging to a named pipe.\n" \
|
||||
"# Use this option to view your gateway's log messages\n" \
|
||||
"# from the log_pipe_file defined bellow.\n" \
|
||||
@@ -198,10 +231,32 @@ int _config_create(const char *config_file)
|
||||
"# cat \"log_pipe_file\"\n" \
|
||||
"log_pipe=0\n" \
|
||||
"log_pipe_file=/tmp/mysgw.pipe\n" \
|
||||
"\n" \
|
||||
"# Enable logging to syslog.\n" \
|
||||
"syslog=0\n" \
|
||||
"\n" \
|
||||
"# EEPROM settings\n" \
|
||||
"eeprom_file=/etc/mysensors.eeprom\n" \
|
||||
"eeprom_size=1024\n";
|
||||
"eeprom_size=1024\n" \
|
||||
"\n" \
|
||||
"# Software signing settings\n" \
|
||||
"# Note: The gateway must have been built with signing\n" \
|
||||
"# support to use the options below.\n" \
|
||||
"#\n" \
|
||||
"# To generate a HMAC key run mysgw with: --gen-soft-hmac-key\n" \
|
||||
"# copy the new key in the line below and uncomment it.\n" \
|
||||
"#soft_hmac_key=\n" \
|
||||
"# To generate a serial key run mysgw with: --gen-soft-serial-key\n" \
|
||||
"# copy the new key in the line below and uncomment it.\n" \
|
||||
"#soft_serial_key=\n" \
|
||||
"\n" \
|
||||
"# Encryption settings\n" \
|
||||
"# Note: The gateway must have been built with encryption\n" \
|
||||
"# support to use the options below.\n" \
|
||||
"#\n" \
|
||||
"# To generate a AES key run mysgw with: --gen-aes-key\n" \
|
||||
"# copy the new key in the line below and uncomment it.\n" \
|
||||
"#aes_key=\n";
|
||||
|
||||
myFile = fopen(config_file, "w");
|
||||
if (!myFile) {
|
||||
|
||||
@@ -33,6 +33,9 @@ struct config {
|
||||
int syslog;
|
||||
char *eeprom_file;
|
||||
int eeprom_size;
|
||||
char *soft_hmac_key;
|
||||
char *soft_serial_key;
|
||||
char *aes_key;
|
||||
} conf;
|
||||
|
||||
int config_parse(const char *config_file);
|
||||
|
||||
@@ -111,13 +111,7 @@ void print_usage()
|
||||
" --daemon Run as a daemon.\n" \
|
||||
" --gen-soft-hmac-key Generate and print a soft hmac key.\n" \
|
||||
" --gen-soft-serial-key Generate and print a soft serial key.\n" \
|
||||
" --gen-aes-key Generate and print an aes encryption key.\n" \
|
||||
" --print-soft-hmac-key Print the soft hmac key from the config file.\n" \
|
||||
" --print-soft-serial-key Print the soft serial key from the config file.\n" \
|
||||
" --print-aes-key Print the aes encryption key from the config file.\n" \
|
||||
" --set-soft-hmac-key Write a soft hmac key to the config file.\n" \
|
||||
" --set-soft-serial-key Write a soft serial key to the config file.\n" \
|
||||
" --set-aes-key Write an aes encryption key to the config file.\n");
|
||||
" --gen-aes-key Generate and print an aes encryption key.\n");
|
||||
}
|
||||
|
||||
void print_soft_sign_hmac_key(uint8_t *key_ptr = NULL)
|
||||
@@ -129,7 +123,7 @@ void print_soft_sign_hmac_key(uint8_t *key_ptr = NULL)
|
||||
key_ptr = key;
|
||||
}
|
||||
|
||||
printf("SOFT_HMAC_KEY | ");
|
||||
printf("soft_hmac_key=");
|
||||
for (int i = 0; i < 32; i++) {
|
||||
printf("%02X", key_ptr[i]);
|
||||
}
|
||||
@@ -146,20 +140,26 @@ void print_soft_sign_hmac_key(uint8_t *key_ptr = NULL)
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
void generate_soft_sign_hmac_key()
|
||||
void generate_soft_sign_hmac_key(char *config_file = NULL)
|
||||
{
|
||||
uint8_t key[32];
|
||||
|
||||
printf("Generating key...");
|
||||
while (hwGetentropy(&key, sizeof(key)) != sizeof(key));
|
||||
printf(" done.\n");
|
||||
|
||||
printf("To use the new key, update the value in %s witn:\n",
|
||||
config_file?config_file:MY_LINUX_CONFIG_FILE);
|
||||
print_soft_sign_hmac_key(key);
|
||||
|
||||
printf("To use this key, run mysgw with:\n"
|
||||
" --set-soft-hmac-key=");
|
||||
for (int i = 0; i < 32; i++) {
|
||||
printf("%02X", key[i]);
|
||||
}
|
||||
printf("\n");
|
||||
#if defined(MY_SIGNING_SIMPLE_PASSWD)
|
||||
printf("Note: The gateway was built with simplified signing using the password: %s\n" \
|
||||
" Any key set with soft_hmac_key option in the config file is ignored.\n\n",
|
||||
MY_SIGNING_SIMPLE_PASSWD);
|
||||
#elif !defined(MY_SIGNING_FEATURE)
|
||||
printf("Note: The gateway was not built with signing support.\n" \
|
||||
" Any key set with soft_hmac_key option in the config file is ignored.\n\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
void set_soft_sign_hmac_key(char *key_str)
|
||||
@@ -167,7 +167,7 @@ void set_soft_sign_hmac_key(char *key_str)
|
||||
uint8_t key[32];
|
||||
|
||||
if (strlen(key_str) != 64) {
|
||||
printf("invalid key!\n");
|
||||
logWarning("Invalid HMAC key!\n");
|
||||
} else {
|
||||
for (int i = 0; i < 64; ++i) {
|
||||
int n;
|
||||
@@ -187,7 +187,6 @@ void set_soft_sign_hmac_key(char *key_str)
|
||||
}
|
||||
}
|
||||
hwWriteConfigBlock(&key, reinterpret_cast<void*>EEPROM_SIGNING_SOFT_HMAC_KEY_ADDRESS, 32);
|
||||
print_soft_sign_hmac_key();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,7 +199,7 @@ void print_soft_sign_serial_key(uint8_t *key_ptr = NULL)
|
||||
key_ptr = key;
|
||||
}
|
||||
|
||||
printf("SOFT_SERIAL | ");
|
||||
printf("soft_serial_key=");
|
||||
for (int i = 0; i < 9; i++) {
|
||||
printf("%02X", key_ptr[i]);
|
||||
}
|
||||
@@ -217,20 +216,26 @@ void print_soft_sign_serial_key(uint8_t *key_ptr = NULL)
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
void generate_soft_sign_serial_key()
|
||||
void generate_soft_sign_serial_key(char *config_file = NULL)
|
||||
{
|
||||
uint8_t key[9];
|
||||
|
||||
printf("Generating key...");
|
||||
while (hwGetentropy(&key, sizeof(key)) != sizeof(key));
|
||||
printf(" done.\n");
|
||||
|
||||
printf("To use the new key, update the value in %s witn:\n",
|
||||
config_file?config_file:MY_LINUX_CONFIG_FILE);
|
||||
print_soft_sign_serial_key(key);
|
||||
|
||||
printf("To use this key, run mysgw with:\n"
|
||||
" --set-soft-serial-key=");
|
||||
for (int i = 0; i < 9; i++) {
|
||||
printf("%02X", key[i]);
|
||||
}
|
||||
printf("\n");
|
||||
#if defined(MY_SIGNING_SIMPLE_PASSWD)
|
||||
printf("Note: The gateway was built with simplified signing using the password: %s\n" \
|
||||
" Any key set with soft_serial_key option in the config file is ignored.\n\n",
|
||||
MY_SIGNING_SIMPLE_PASSWD);
|
||||
#elif !defined(MY_SIGNING_FEATURE)
|
||||
printf("Note: The gateway was not built with signing support.\n" \
|
||||
" Any key set with soft_serial_key option in the config file is ignored.\n\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
void set_soft_sign_serial_key(char *key_str)
|
||||
@@ -238,7 +243,7 @@ void set_soft_sign_serial_key(char *key_str)
|
||||
uint8_t key[9];
|
||||
|
||||
if (strlen(key_str) != 18) {
|
||||
printf("invalid key!\n");
|
||||
logWarning("Invalid soft serial key!\n");
|
||||
} else {
|
||||
for (int i = 0; i < 18; ++i) {
|
||||
int n;
|
||||
@@ -258,7 +263,6 @@ void set_soft_sign_serial_key(char *key_str)
|
||||
}
|
||||
}
|
||||
hwWriteConfigBlock(&key, reinterpret_cast<void*>EEPROM_SIGNING_SOFT_SERIAL_ADDRESS, 9);
|
||||
print_soft_sign_serial_key();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,16 +271,11 @@ void print_aes_key(uint8_t *key_ptr = NULL)
|
||||
uint8_t key[16];
|
||||
|
||||
if (key_ptr == NULL) {
|
||||
#ifdef MY_ENCRYPTION_SIMPLE_PASSWD
|
||||
memset(key, 0, 16);
|
||||
memcpy(key, MY_ENCRYPTION_SIMPLE_PASSWD, strnlen(MY_ENCRYPTION_SIMPLE_PASSWD, 16));
|
||||
#else
|
||||
hwReadConfigBlock(&key, reinterpret_cast<void*>EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 16);
|
||||
#endif
|
||||
key_ptr = key;
|
||||
}
|
||||
|
||||
printf("AES_KEY | ");
|
||||
printf("aes_key=");
|
||||
for (int i = 0; i < 16; i++) {
|
||||
printf("%02X", key_ptr[i]);
|
||||
}
|
||||
@@ -293,20 +292,26 @@ void print_aes_key(uint8_t *key_ptr = NULL)
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
void generate_aes_key()
|
||||
void generate_aes_key(char *config_file = NULL)
|
||||
{
|
||||
uint8_t key[16];
|
||||
|
||||
printf("Generating key...");
|
||||
while (hwGetentropy(&key, sizeof(key)) != sizeof(key));
|
||||
printf(" done.\n");
|
||||
|
||||
printf("To use the new key, update the value in %s witn:\n",
|
||||
config_file?config_file:MY_LINUX_CONFIG_FILE);
|
||||
print_aes_key(key);
|
||||
|
||||
printf("To use this key, run mysgw with:\n"
|
||||
" --set-aes-key=");
|
||||
for (int i = 0; i < 16; i++) {
|
||||
printf("%02X", key[i]);
|
||||
}
|
||||
printf("\n");
|
||||
#if defined(MY_ENCRYPTION_SIMPLE_PASSWD)
|
||||
printf("Note: The gateway was built with simplified encryption using the password: %s\n" \
|
||||
" Any key set with aes_key option in the config file is ignored.\n\n",
|
||||
MY_ENCRYPTION_SIMPLE_PASSWD);
|
||||
#elif !defined(MY_ENCRYPTION_FEATURE)
|
||||
printf("Note: The gateway was not built with encryption support.\n" \
|
||||
" Any key set with aes_key option in the config file is ignored.\n\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
void set_aes_key(char *key_str)
|
||||
@@ -314,7 +319,7 @@ void set_aes_key(char *key_str)
|
||||
uint8_t key[16];
|
||||
|
||||
if (strlen(key_str) != 32) {
|
||||
printf("invalid key!\n");
|
||||
logWarning("Invalid AES key!\n");
|
||||
} else {
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
int n;
|
||||
@@ -334,14 +339,16 @@ void set_aes_key(char *key_str)
|
||||
}
|
||||
}
|
||||
hwWriteConfigBlock(&key, reinterpret_cast<void*>EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 16);
|
||||
print_aes_key();
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int opt, daemon = 0, quiet = 0;
|
||||
char *key = NULL, *config_file = NULL;
|
||||
char *config_file = NULL;
|
||||
bool gen_soft_sign_hmac_key = false;
|
||||
bool gen_soft_sign_serial_key = false;
|
||||
bool gen_aes_key = false;
|
||||
|
||||
/* register the signal handler */
|
||||
signal(SIGINT, handle_sigint);
|
||||
@@ -358,17 +365,11 @@ int main(int argc, char *argv[])
|
||||
{"gen-soft-hmac-key", no_argument, 0, 'A'},
|
||||
{"gen-soft-serial-key", no_argument, 0, 'B'},
|
||||
{"gen-aes-key", no_argument, 0, 'C'},
|
||||
{"print-soft-hmac-key", no_argument, 0, 'D'},
|
||||
{"print-soft-serial-key", no_argument, 0, 'E'},
|
||||
{"print-aes-key", no_argument, 0, 'F'},
|
||||
{"set-soft-hmac-key", required_argument, 0, 'G'},
|
||||
{"set-soft-serial-key", required_argument, 0, 'H'},
|
||||
{"set-aes-key", required_argument, 0, 'I'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
int long_index = 0;
|
||||
while ((opt = getopt_long(argc, argv,"chqABCDEFGHIJ", long_options, &long_index )) != -1) {
|
||||
while ((opt = getopt_long(argc, argv,"chqABCJ", long_options, &long_index )) != -1) {
|
||||
switch (opt) {
|
||||
case 'c':
|
||||
config_file = strdup(optarg);
|
||||
@@ -380,35 +381,14 @@ int main(int argc, char *argv[])
|
||||
quiet = 1;
|
||||
break;
|
||||
case 'A':
|
||||
generate_soft_sign_hmac_key();
|
||||
exit(EXIT_SUCCESS);
|
||||
gen_soft_sign_hmac_key = true;
|
||||
break;
|
||||
case 'B':
|
||||
generate_soft_sign_serial_key();
|
||||
exit(EXIT_SUCCESS);
|
||||
gen_soft_sign_serial_key = true;
|
||||
break;
|
||||
case 'C':
|
||||
generate_aes_key();
|
||||
exit(EXIT_SUCCESS);
|
||||
case 'D':
|
||||
print_soft_sign_hmac_key();
|
||||
exit(EXIT_SUCCESS);
|
||||
case 'E':
|
||||
print_soft_sign_serial_key();
|
||||
exit(EXIT_SUCCESS);
|
||||
case 'F':
|
||||
print_aes_key();
|
||||
exit(EXIT_SUCCESS);
|
||||
case 'G':
|
||||
key = strdup(optarg);
|
||||
set_soft_sign_hmac_key(key);
|
||||
exit(EXIT_SUCCESS);
|
||||
case 'H':
|
||||
key = strdup(optarg);
|
||||
set_soft_sign_serial_key(key);
|
||||
exit(EXIT_SUCCESS);
|
||||
case 'I':
|
||||
key = strdup(optarg);
|
||||
set_aes_key(key);
|
||||
exit(EXIT_SUCCESS);
|
||||
gen_aes_key = true;
|
||||
break;
|
||||
case 'J':
|
||||
daemon = 1;
|
||||
break;
|
||||
@@ -418,6 +398,19 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
|
||||
if (gen_soft_sign_hmac_key || gen_soft_sign_serial_key || gen_aes_key) {
|
||||
if (gen_soft_sign_hmac_key) {
|
||||
generate_soft_sign_hmac_key(config_file);
|
||||
}
|
||||
if (gen_soft_sign_serial_key) {
|
||||
generate_soft_sign_serial_key(config_file);
|
||||
}
|
||||
if (gen_aes_key) {
|
||||
generate_aes_key(config_file);
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
if (daemon) {
|
||||
if (daemonize() != 0) {
|
||||
exit(EXIT_FAILURE);
|
||||
@@ -428,9 +421,6 @@ int main(int argc, char *argv[])
|
||||
if (config_parse(config_file?config_file:MY_LINUX_CONFIG_FILE) != 0) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (config_file) {
|
||||
free(config_file);
|
||||
}
|
||||
|
||||
logSetQuiet(quiet);
|
||||
logSetLevel(conf.verbose);
|
||||
@@ -456,6 +446,38 @@ int main(int argc, char *argv[])
|
||||
|
||||
_begin(); // Startup MySensors library
|
||||
|
||||
// EEPROM is initialized within _begin()
|
||||
// any operation on it must be done hereafter
|
||||
|
||||
#if defined(MY_SIGNING_FEATURE) && !defined(MY_SIGNING_SIMPLE_PASSWD)
|
||||
// Check if we need to update the signing keys in EEPROM
|
||||
if (conf.soft_hmac_key) {
|
||||
set_soft_sign_hmac_key(conf.soft_hmac_key);
|
||||
} else {
|
||||
logError("soft_hmac_key was not found in %s\n", config_file?config_file:MY_LINUX_CONFIG_FILE);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (conf.soft_serial_key) {
|
||||
set_soft_sign_serial_key(conf.soft_serial_key);
|
||||
} else {
|
||||
logError("soft_serial_key was not found in %s\n", config_file?config_file:MY_LINUX_CONFIG_FILE);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
#endif
|
||||
#if defined(MY_ENCRYPTION_FEATURE) && !defined(MY_ENCRYPTION_SIMPLE_PASSWD)
|
||||
// Check if we need to update the encryption key in EEPROM
|
||||
if (conf.aes_key) {
|
||||
set_aes_key(conf.aes_key);
|
||||
} else {
|
||||
logError("aes_key was not found in %s\n", config_file?config_file:MY_LINUX_CONFIG_FILE);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (config_file) {
|
||||
free(config_file);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
_process(); // Process incoming data
|
||||
if (loop) {
|
||||
|
||||
Reference in New Issue
Block a user