session server BUGIX fix x509 key null print
diff --git a/src/session.c b/src/session.c
index 38d7d09..9091143 100644
--- a/src/session.c
+++ b/src/session.c
@@ -111,13 +111,13 @@
 {
     switch (format) {
     case NC_PRIVKEY_FORMAT_RSA:
-        return "RSA";
+        return " RSA ";
     case NC_PRIVKEY_FORMAT_EC:
-        return "EC";
+        return " EC ";
     case NC_PRIVKEY_FORMAT_X509:
-        return NULL;
+        return " ";
     case NC_PRIVKEY_FORMAT_OPENSSH:
-        return "OPENSSH";
+        return " OPENSSH ";
     default:
         return NULL;
     }
diff --git a/src/session_server_ssh.c b/src/session_server_ssh.c
index b76bc0f..cb14011 100644
--- a/src/session_server_ssh.c
+++ b/src/session_server_ssh.c
@@ -75,33 +75,33 @@
     }
 
     /* write header */
-    written = fwrite("-----BEGIN ", 1, 11, file);
+    written = fwrite("-----BEGIN", 1, 10, file);
     if (privkey_format) {
         written += fwrite(privkey_format, 1, strlen(privkey_format), file);
-        written += fwrite(" PRIVATE KEY-----\n", 1, 18, file);
-    } else {
         written += fwrite("PRIVATE KEY-----\n", 1, 17, file);
+    } else {
+        written += fwrite(" PRIVATE KEY-----\n", 1, 18, file);
     }
 
     /* write data */
     written += fwrite(in, 1, strlen(in), file);
 
     /* write footer */
-    written += fwrite("\n-----END ", 1, 10, file);
+    written += fwrite("\n-----END", 1, 9, file);
     if (privkey_format) {
         written += fwrite(privkey_format, 1, strlen(privkey_format), file);
-        written += fwrite(" PRIVATE KEY-----", 1, 17, file);
-    } else {
         written += fwrite("PRIVATE KEY-----", 1, 16, file);
+    } else {
+        written += fwrite(" PRIVATE KEY-----", 1, 17, file);
     }
 
     fclose(file);
 
     /* checksum */
     if (privkey_format) {
-        len = 11 + strlen(privkey_format) + 18 + strlen(in) + 10 + strlen(privkey_format) + 17;
+        len = 10 + strlen(privkey_format) + 17 + strlen(in) + 9 + strlen(privkey_format) + 16;
     } else {
-        len = 11 + 17 + strlen(in) + 10 + 16;
+        len = 10 + 18 + strlen(in) + 9 + 17;
     }
 
     if ((unsigned)written != len) {
diff --git a/src/session_server_tls.c b/src/session_server_tls.c
index 1760549..8764770 100644
--- a/src/session_server_tls.c
+++ b/src/session_server_tls.c
@@ -137,8 +137,12 @@
         return NULL;
     }
 
-    if (asprintf(&buf, "%s%s%s%s%s%s%s", "-----BEGIN ", key_str, " PRIVATE KEY-----\n", in, "\n-----END ",
-            key_str, " PRIVATE KEY-----") == -1) {
+    if (!key_str) {
+        /* avoid writing (null) for possibly unknown key formats */
+        key_str = "";
+    }
+    if (asprintf(&buf, "%s%s%s%s%s%s%s", "-----BEGIN", key_str, "PRIVATE KEY-----\n", in, "\n-----END",
+            key_str, "PRIVATE KEY-----") == -1) {
         return NULL;
     }
     bio = BIO_new_mem_buf(buf, strlen(buf));