Skip to content

Commit 1ad19f2

Browse files
committed
keystored CHANGE generate new SSH hostkey, do not use the OpenSSH one
Fixes #337
1 parent bf43336 commit 1ad19f2

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

keystored/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,17 @@ endif()
131131

132132
option(SSH_KEY_INSTALL "Enable ssh key import" ON)
133133
if (SSH_KEY_INSTALL)
134+
if (NOT SSH_KEYGEN_EXECUTABLE)
135+
find_program(SSH_KEYGEN_EXECUTABLE ssh-keygen)
136+
endif()
137+
if (NOT SSH_KEYGEN_EXECUTABLE)
138+
message(FATAL_ERROR "Unable to find ssh-keygen, set SSH_KEYGEN_EXECUTABLE manually.")
139+
endif()
134140
install(CODE "
135141
set(ENV{SYSREPOCFG} ${SYSREPOCFG_EXECUTABLE})
136142
set(ENV{CHMOD} ${CHMOD_EXECUTABLE})
137143
set(ENV{OPENSSL} ${OPENSSL_EXECUTABLE})
144+
set(ENV{SSH_KEYGEN} ${SSH_KEYGEN_EXECUTABLE})
138145
set(ENV{KEYSTORED_KEYS_DIR} ${KEYSTORED_KEYS_DIR})
139146
set(ENV{KEYSTORED_CHECK_SSH_KEY} ${KEYSTORED_CHECK_SSH_KEY})
140147
execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/ssh-key-import.sh)")

keystored/keystored.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ ks_privkey_get_cb(const char *xpath, sr_val_t **values, size_t *values_cnt, uint
137137
}
138138
name += 18;
139139

140-
if (asprintf(&path, "%s/%.*s.pub.pem", KEYSTORED_KEYS_DIR, (int)(strchr(name, '\'') - name), name) == -1) {
140+
if (asprintf(&path, "%s/%.*s.pem.pub", KEYSTORED_KEYS_DIR, (int)(strchr(name, '\'') - name), name) == -1) {
141141
SRP_LOG_ERR("Memory allocation failed (%s:%d).", __FILE__, __LINE__);
142142
return SR_ERR_NOMEM;
143143
}
@@ -337,7 +337,7 @@ ks_privkey_gen_cb(const char *UNUSED(xpath), const sr_node_t *input, const size_
337337
goto cleanup;
338338
}
339339
sprintf(priv_path, "%s/%s.pem", KEYSTORED_KEYS_DIR, input[0].data.string_val);
340-
sprintf(pub_path, "%s/%s.pub.pem", KEYSTORED_KEYS_DIR, input[0].data.string_val);
340+
sprintf(pub_path, "%s/%s.pem.pub", KEYSTORED_KEYS_DIR, input[0].data.string_val);
341341

342342
if (!(pid = fork())) {
343343
/* child */
@@ -451,7 +451,7 @@ ks_privkey_load_cb(const char *UNUSED(xpath), const sr_node_t *input, const size
451451
goto cleanup;
452452
}
453453
sprintf(priv_path, "%s/%s.pem", KEYSTORED_KEYS_DIR, input[0].data.string_val);
454-
sprintf(pub_path, "%s/%s.pub.pem", KEYSTORED_KEYS_DIR, input[0].data.string_val);
454+
sprintf(pub_path, "%s/%s.pem.pub", KEYSTORED_KEYS_DIR, input[0].data.string_val);
455455

456456
fd = open(priv_path, O_CREAT | O_TRUNC | O_WRONLY, 00600);
457457
if (fd == -1) {

keystored/scripts/ssh-key-import.sh

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local_path=$(dirname $0)
99
: ${SYSREPOCFG:=sysrepocfg}
1010
: ${CHMOD:=chmod}
1111
: ${OPENSSL:=openssl}
12+
: ${SSH_KEYGEN:=ssh-keygen}
1213
: ${STOCK_KEY_CONFIG:=$local_path/../stock_key_config.xml}
1314
: ${KEYSTORED_KEYS_DIR:=/etc/keystored/keys}
1415

@@ -21,13 +22,14 @@ if [ $KEYSTORED_CHECK_SSH_KEY -eq 0 ]; then
2122
echo "- Warning: Assuming that an external script will provide the SSH key in a PEM format at \"${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem\"."
2223
echo "- Importing ietf-keystore stock key configuration..."
2324
$SYSREPOCFG -d startup -i ${STOCK_KEY_CONFIG} ietf-keystore
24-
elif [ -r /etc/ssh/ssh_host_rsa_key ]; then
25-
cp /etc/ssh/ssh_host_rsa_key ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem
25+
else
26+
if [ -r ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem -a -r ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem.pub ]; then
27+
echo "- SSH hostkey found, no need to generate a new one."
28+
else
29+
echo "- SSH hostkey not found, generating a new one..."
30+
$SSH_KEYGEN -m pem -t rsa -q -N "" -f ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem
31+
fi
2632
$CHMOD go-rw ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem
27-
$OPENSSL rsa -pubout -in ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pem \
28-
-out ${KEYSTORED_KEYS_DIR}/ssh_host_rsa_key.pub.pem
2933
echo "- Importing ietf-keystore stock key configuration..."
3034
$SYSREPOCFG -d startup -i ${STOCK_KEY_CONFIG} ietf-keystore
31-
else
32-
echo "- Warning: Cannot read the SSH hostkey at /etc/ssh/ssh_host_rsa_key, skipping."
3335
fi

0 commit comments

Comments
 (0)