Skip to content

Commit a6a4e1e

Browse files
committed
confd: fix SSH host key generation warnings
Fix PEM file reconstruction issues that caused "unrecognised raw private key format" errors during system boot: 1. Fix base64 content formatting with proper 64-character line wrapping using printf+fold instead of echo 2. Use generic PKCS#8 headers (BEGIN PRIVATE KEY) instead of PKCS#1 RSA-specific headers, maintaining support for future key types like ECDSA 3. Update ssh-keygen format flag to PKCS8 for correct conversion 4. Skip gen_hostkey() when keys are empty to prevent invalid PEM import attempts Fixes #1289 Signed-off-by: Joachim Wiberg <[email protected]>
1 parent bd2f346 commit a6a4e1e

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

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

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

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

11+
# openssl genpkey -quiet -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -outform PEM
1112
openssl genpkey -quiet -algorithm RSA -pkeyopt rsa_keygen_bits:$BIT -outform PEM > "$KEY"
12-
openssl rsa -RSAPublicKey_out < "$KEY" > "$PUB"
13+
openssl pkey -pubout < "$KEY" 2>/dev/null > "$PUB"
1314

1415
exit 0

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
#!/bin/bash
2-
# Store and convert RSA PUBLIC/PRIVATE KEYs to be able to use them in
3-
# OpenSSHd.
1+
#!/bin/sh
2+
# Generate OpenSSH host key pair from same keys as NETCONF
43
set -e
4+
umask 0077
55

66
NAME="$1"
77
DIR="$2"
88
PUBLIC="$3"
99
PRIVATE="$4"
1010
TMP="$(mktemp)"
1111

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

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

20-
ssh-keygen -i -m PKCS8 -f "$TMP" > "$DIR/$NAME.pub"
24+
ssh-keygen -i -f "$TMP" -m PKCS8 > "$DIR/$NAME.pub"
2125
rm "$TMP"
26+
2227
chmod 0600 "$DIR/$NAME.pub"
2328
chmod 0600 "$DIR/$NAME"
2429
chown sshd:sshd "$DIR/$NAME.pub"

doc/ChangeLog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Change Log
33

44
All notable changes to the project are documented in this file.
55

6-
[v25.11.0][] - 2025-11-28
6+
[v25.11.0][UNRELEASED]
77
-------------------------
88

99
> [!NOTE]
@@ -79,6 +79,7 @@ All notable changes to the project are documented in this file.
7979
existing invalid configurations are automatically corrected during upgrade
8080
- Fix #1255: serious regression in boot time, introduced in v25.10, delays the
8181
boot step "Mounting filesystems ...", from 30 seconds up to five minutes!
82+
- Fix #1289: SSH host key generation warning at boot after factory reset
8283
- Fix broken intra-document links in container and tunnel documentation
8384

8485
[latest-boot]: https://github.com/kernelkit/infix/releases/latest-boot

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)