Skip to content

Commit 80ae943

Browse files
committed
session mbedtls UPDATE add libssh version check
1 parent ad242b8 commit 80ae943

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

src/session_mbedtls.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,11 @@ nc_tls_privkey_export_openssh(const char *pk, char **privkey)
14261426
int rc = 0;
14271427
ssh_key sshkey = NULL;
14281428

1429+
*privkey = NULL;
1430+
1431+
/* older versions of libssh (< v0.11.0) do not support exporting to OpenSSH format,
1432+
* signal this to the caller by returning success with NULL privkey */
1433+
#if (LIBSSH_VERSION_MAJOR > 0) || (LIBSSH_VERSION_MAJOR == 0 && LIBSSH_VERSION_MINOR >= 11)
14291434
/* load the SEC1/PKCS#1 using libssh */
14301435
if (ssh_pki_import_privkey_base64(pk, NULL, NULL, NULL, &sshkey)) {
14311436
ERR(NULL, "Importing the private key to libssh failed (%s).", ssh_get_error(NULL));
@@ -1439,6 +1444,7 @@ nc_tls_privkey_export_openssh(const char *pk, char **privkey)
14391444
rc = 1;
14401445
goto cleanup;
14411446
}
1447+
#endif // (LIBSSH_VERSION_MAJOR > 0) || (LIBSSH_VERSION_MAJOR == 0 && LIBSSH_VERSION_MINOR >= 11)
14421448

14431449
cleanup:
14441450
ssh_key_free(sshkey);
@@ -1475,8 +1481,16 @@ nc_tls_privkey_export_wrap(void *pkey, enum nc_privkey_format format, char **pri
14751481
}
14761482

14771483
if (format == NC_PRIVKEY_FORMAT_OPENSSH) {
1478-
/* convert it to OpenSSH format */
14791484
rc = nc_tls_privkey_export_openssh(pk, privkey);
1485+
if (rc) {
1486+
goto cleanup;
1487+
}
1488+
1489+
if (!*privkey) {
1490+
/* privkey not converted, just use the PEM as is (PKCS#1 or SEC1) */
1491+
*privkey = pk;
1492+
pk = NULL;
1493+
}
14801494
} else {
14811495
/* return the PEM as is (PKCS#1 or SEC1), mbedtls can not do NC_PRIVKEY_FORMAT_X509 */
14821496
*privkey = pk;

src/session_openssl.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,11 @@ nc_tls_privkey_export_openssh(EVP_PKEY *pkey, char **privkey)
12161216
char *pem = NULL;
12171217
ssh_key sshkey = NULL;
12181218

1219+
*privkey = NULL;
1220+
1221+
/* older versions of libssh (< v0.11.0) do not support exporting to OpenSSH format,
1222+
* signal this to the caller by returning success with NULL privkey */
1223+
#if (LIBSSH_VERSION_MAJOR > 0) || (LIBSSH_VERSION_MAJOR == 0 && LIBSSH_VERSION_MINOR >= 11)
12191224
bio = BIO_new(BIO_s_mem());
12201225
if (!bio) {
12211226
ERR(NULL, "Creating new bio failed (%s).", ERR_reason_error_string(ERR_get_error()));
@@ -1247,6 +1252,7 @@ nc_tls_privkey_export_openssh(EVP_PKEY *pkey, char **privkey)
12471252
rc = 1;
12481253
goto cleanup;
12491254
}
1255+
#endif
12501256

12511257
cleanup:
12521258
BIO_free(bio);
@@ -1261,7 +1267,7 @@ nc_tls_privkey_export_wrap(void *pkey, enum nc_privkey_format format, char **pri
12611267
int rc = 0;
12621268
BIO *bio = NULL;
12631269
OSSL_ENCODER_CTX *ctx = NULL;
1264-
const char *output_structure;
1270+
const char *output_structure = NULL;
12651271

12661272
bio = BIO_new(BIO_s_mem());
12671273
if (!bio) {
@@ -1280,12 +1286,22 @@ nc_tls_privkey_export_wrap(void *pkey, enum nc_privkey_format format, char **pri
12801286
output_structure = "PrivateKeyInfo";
12811287
break;
12821288
case NC_PRIVKEY_FORMAT_OPENSSH:
1283-
/* we need to use libssh for this */
12841289
rc = nc_tls_privkey_export_openssh(pkey, privkey);
1285-
goto cleanup;
1290+
if (rc) {
1291+
goto cleanup;
1292+
}
1293+
1294+
if (!*privkey) {
1295+
/* privkey not converted, just convert it to PrivateKeyInfo format */
1296+
output_structure = "PrivateKeyInfo";
1297+
}
1298+
break;
12861299
default:
12871300
ERRINT;
12881301
rc = 1;
1302+
break;
1303+
}
1304+
if (!output_structure) {
12891305
goto cleanup;
12901306
}
12911307

0 commit comments

Comments
 (0)