confd: fix SSH host key generation warnings

Validate keys in gen_hostkey() before passing empty keys to shell scripts,
preventing:

Nov 04 2024 10:54:25 confd[2697]: SSH key (genkey) does not exist, generating...
Nov 04 2024 10:54:25 confd[2697]: writing RSA key
Nov 04 2024 10:54:26 confd[2697]: do_convert_from_pkcs8: /tmp/tmp.FH1Hr1 is not a recognised public key format

Also, fix base64 content formatting with proper 64-character line wrapping
using printf+fold instead of echo.

Use PKCS#1 RSA format for public keys as required by netopeer2-server, while
keeping PKCS#8 format for private keys.  Use proper ssh-keygen format flag
(PKCS8) for correct conversion.

Fixes #1289

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2025-12-02 09:50:24 +01:00
parent bd2f3467f8
commit d23f9ea25b
4 changed files with 26 additions and 14 deletions
+7 -2
View File
@@ -59,12 +59,17 @@ static int gen_hostkey(const char *name, struct lyd_node *change)
private_key = lydx_get_cattr(change, "cleartext-private-key");
public_key = lydx_get_cattr(change, "public-key");
/* Validate keys before use */
if (!private_key || !public_key || !*private_key || !*public_key)
return SR_ERR_OK;
if (mkdir(SSH_HOSTKEYS_NEXT, 0600) && (errno != EEXIST)) {
ERRNO("Failed creating %s", SSH_HOSTKEYS_NEXT);
rc = SR_ERR_INTERNAL;
}
if (systemf("/usr/libexec/infix/mksshkey %s %s %s %s", name, SSH_HOSTKEYS_NEXT, public_key, private_key))
if (systemf("/usr/libexec/infix/mksshkey %s %s %s %s", name,
SSH_HOSTKEYS_NEXT, public_key, private_key))
rc = SR_ERR_INTERNAL;
return rc;
@@ -156,7 +161,7 @@ static int keystore_update(sr_session_ctx_t *session, struct lyd_node *config, s
}
int keystore_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff,
sr_event_t event, struct confd *confd)
sr_event_t event, struct confd *confd)
{
struct lyd_node *changes, *change;
int rc = SR_ERR_OK;