Skip to content

Commit f51c7d9

Browse files
committed
confd: fix SSH host key generation warnings
Fix several issues in SSH host key generation and import that caused warnings in system logs: 1. mkkeys: Switch from openssl genpkey (PKCS#8) to genrsa (PKCS#1) to match the expected format in mksshkey 2. mksshkey: Fix PEM file reconstruction by properly formatting base64 content with 64-character line wrapping using printf+fold. The previous approach concatenated the END marker to the last base64 line, causing "unrecognised raw private key format" errors 3. mksshkey: Correct ssh-keygen format flag from PKCS8 to PEM for public key conversion 4. confd:keystore.c: Skip gen_hostkey() when keys are empty to prevent attempting to import invalid PEM files during SR_EV_UPDATE events before keys are populated in the config tree 5. mksshkey: Convert from bash to POSIX sh (no bashisms were used) This eliminates the "do_convert_from_pem: unrecognised raw private key format" error messages during system boot and SSH key configuration. Fixes #1289 Signed-off-by: Joachim Wiberg <[email protected]>
1 parent bd2f346 commit f51c7d9

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

board/common/rootfs/usr/libexec/infix/mkkeys

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ PUB=$2
88

99
mkdir -p "$(dirname "$KEY")" "$(dirname "$PUB")"
1010

11-
openssl genpkey -quiet -algorithm RSA -pkeyopt rsa_keygen_bits:$BIT -outform PEM > "$KEY"
12-
openssl rsa -RSAPublicKey_out < "$KEY" > "$PUB"
11+
openssl genrsa -traditional $BIT 2>/dev/null > "$KEY"
12+
openssl rsa -RSAPublicKey_out < "$KEY" 2>/dev/null > "$PUB"
1313

1414
exit 0

board/common/rootfs/usr/libexec/infix/mksshkey

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
#!/bin/bash
2-
# Store and convert RSA PUBLIC/PRIVATE KEYs to be able to use them in
3-
# OpenSSHd.
1+
#!/bin/sh
42
set -e
3+
umask 0077
54

65
NAME="$1"
76
DIR="$2"
87
PUBLIC="$3"
98
PRIVATE="$4"
109
TMP="$(mktemp)"
1110

12-
echo -e '-----BEGIN RSA PRIVATE KEY-----' > "$DIR/$NAME"
13-
echo "$PRIVATE" >> "$DIR/$NAME"
14-
echo -e '-----END RSA PRIVATE KEY-----' >> "$DIR/$NAME"
11+
{
12+
echo '-----BEGIN RSA PRIVATE KEY-----'
13+
printf '%s\n' "$PRIVATE" | fold -w 64
14+
echo '-----END RSA PRIVATE KEY-----'
15+
} > "$DIR/$NAME"
1516

16-
echo -e "-----BEGIN RSA PUBLIC KEY-----" > "$TMP"
17-
echo -e "$PUBLIC" >> "$TMP"
18-
echo -e "-----END RSA PUBLIC KEY-----" >> "$TMP"
17+
{
18+
echo "-----BEGIN RSA PUBLIC KEY-----"
19+
printf '%s\n' "$PUBLIC" | fold -w 64
20+
echo "-----END RSA PUBLIC KEY-----"
21+
} > "$TMP"
1922

20-
ssh-keygen -i -m PKCS8 -f "$TMP" > "$DIR/$NAME.pub"
23+
ssh-keygen -i -f "$TMP" -m PEM > "$DIR/$NAME.pub"
2124
rm "$TMP"
25+
2226
chmod 0600 "$DIR/$NAME.pub"
2327
chmod 0600 "$DIR/$NAME"
2428
chown sshd:sshd "$DIR/$NAME.pub"

doc/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ All notable changes to the project are documented in this file.
7171

7272
### Fixes
7373

74+
- Fix #1289: SSH host key generation warning at boot after factory reset
7475
- Fix #855: User admin sometimes fails to be added to `wheel` group
7576
- Fix #1112: setting hostname via DHCP client sometimes gets overridden by the
7677
configured system hostname

src/confd/src/keystore.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ static int gen_hostkey(const char *name, struct lyd_node *change)
6464
rc = SR_ERR_INTERNAL;
6565
}
6666

67-
if (systemf("/usr/libexec/infix/mksshkey %s %s %s %s", name, SSH_HOSTKEYS_NEXT, public_key, private_key))
67+
if (systemf("/usr/libexec/infix/mksshkey %s %s %s %s", name,
68+
SSH_HOSTKEYS_NEXT, public_key, private_key))
6869
rc = SR_ERR_INTERNAL;
6970

7071
return rc;
@@ -156,7 +157,7 @@ static int keystore_update(sr_session_ctx_t *session, struct lyd_node *config, s
156157
}
157158

158159
int keystore_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff,
159-
sr_event_t event, struct confd *confd)
160+
sr_event_t event, struct confd *confd)
160161
{
161162
struct lyd_node *changes, *change;
162163
int rc = SR_ERR_OK;
@@ -189,6 +190,7 @@ int keystore_change(sr_session_ctx_t *session, struct lyd_node *config, struct l
189190
changes = lydx_get_descendant(config, "keystore", "asymmetric-keys", "asymmetric-key", NULL);
190191
LYX_LIST_FOR_EACH(changes, change, "asymmetric-key") {
191192
const char *name = lydx_get_cattr(change, "name");
193+
const char *private_key, *public_key;
192194
const char *type;
193195

194196
type = lydx_get_cattr(change, "private-key-format");
@@ -203,6 +205,12 @@ int keystore_change(sr_session_ctx_t *session, struct lyd_node *config, struct l
203205
continue;
204206
}
205207

208+
/* Only generate hostkey if both keys exist */
209+
private_key = lydx_get_cattr(change, "cleartext-private-key");
210+
public_key = lydx_get_cattr(change, "public-key");
211+
if (!private_key || !public_key || !*private_key || !*public_key)
212+
continue;
213+
206214
gen_hostkey(name, change);
207215
}
208216

0 commit comments

Comments
 (0)