Skip to content

Commit f056e9d

Browse files
Roytakmichalvasko
authored andcommitted
session mbedtls UPDATE add libssh version check
1 parent 5515fdd commit f056e9d

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

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
@@ -1217,6 +1217,11 @@ nc_tls_privkey_export_openssh(EVP_PKEY *pkey, char **privkey)
12171217
char *pem = NULL;
12181218
ssh_key sshkey = NULL;
12191219

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

12521258
cleanup:
12531259
BIO_free(bio);
@@ -1262,7 +1268,7 @@ nc_tls_privkey_export_wrap(void *pkey, enum nc_privkey_format format, char **pri
12621268
int rc = 0;
12631269
BIO *bio = NULL;
12641270
OSSL_ENCODER_CTX *ctx = NULL;
1265-
const char *output_structure;
1271+
const char *output_structure = NULL;
12661272

12671273
bio = BIO_new(BIO_s_mem());
12681274
if (!bio) {
@@ -1281,12 +1287,22 @@ nc_tls_privkey_export_wrap(void *pkey, enum nc_privkey_format format, char **pri
12811287
output_structure = "PrivateKeyInfo";
12821288
break;
12831289
case NC_PRIVKEY_FORMAT_OPENSSH:
1284-
/* we need to use libssh for this */
12851290
rc = nc_tls_privkey_export_openssh(pkey, privkey);
1286-
goto cleanup;
1291+
if (rc) {
1292+
goto cleanup;
1293+
}
1294+
1295+
if (!*privkey) {
1296+
/* privkey not converted, just convert it to PrivateKeyInfo format */
1297+
output_structure = "PrivateKeyInfo";
1298+
}
1299+
break;
12871300
default:
12881301
ERRINT;
12891302
rc = 1;
1303+
break;
1304+
}
1305+
if (!output_structure) {
12901306
goto cleanup;
12911307
}
12921308

0 commit comments

Comments
 (0)