1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-12-06 13:20:57 +03:00

feat(torture_sk): add validation functions for security key callback responses and resident keys

Signed-off-by: Praneeth Sarode <praneethsarode@gmail.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Eshan Kelkar <eshankelkar@galorithm.com>
This commit is contained in:
Praneeth Sarode
2025-10-23 22:19:20 +05:30
parent c4b2bd34a8
commit e56af9fa79
2 changed files with 114 additions and 4 deletions

View File

@@ -25,7 +25,6 @@
#include "libssh/pki.h" #include "libssh/pki.h"
#include "libssh/sk_api.h" /* For SSH_SK_* flag definitions */ #include "libssh/sk_api.h" /* For SSH_SK_* flag definitions */
/* Helper function to validate ssh_key structure for security keys */
void assert_sk_key_valid(ssh_key key, void assert_sk_key_valid(ssh_key key,
enum ssh_keytypes_e expected_type, enum ssh_keytypes_e expected_type,
bool private) bool private)
@@ -101,6 +100,75 @@ void assert_sk_key_valid(ssh_key key,
} }
} }
void assert_sk_enroll_response(struct sk_enroll_response *response, int flags)
{
assert_non_null(response);
assert_non_null(response->public_key);
assert_true(response->public_key_len > 0);
assert_non_null(response->key_handle);
assert_true(response->key_handle_len > 0);
assert_non_null(response->signature);
assert_true(response->signature_len > 0);
/*
* This check might fail for some authenticators, as returning an
* attestation certificate as part of the attestation statement is not
* mandated by the FIDO2 standard.
*/
assert_non_null(response->attestation_cert);
assert_true(response->attestation_cert_len > 0);
assert_non_null(response->authdata);
assert_true(response->authdata_len > 0);
assert_int_equal(response->flags, flags);
}
void assert_sk_sign_response(struct sk_sign_response *response,
enum ssh_keytypes_e key_type)
{
assert_non_null(response);
assert_non_null(response->sig_r);
assert_true(response->sig_r_len > 0);
/* sig_s is NULL for Ed25519, present for ECDSA */
switch (key_type) {
case SSH_SK_ECDSA:
assert_non_null(response->sig_s);
assert_true(response->sig_s_len > 0);
break;
case SSH_SK_ED25519:
assert_null(response->sig_s);
assert_int_equal(response->sig_s_len, 0);
break;
default:
/* Should not reach here */
assert_true(0);
break;
}
}
void assert_sk_resident_key(struct sk_resident_key *resident_key)
{
assert_non_null(resident_key);
assert_non_null(resident_key->application);
assert_true(strlen(resident_key->application) > 0);
assert_non_null(resident_key->user_id);
assert_true(resident_key->user_id_len > 0);
assert_non_null(resident_key->key.public_key);
assert_true(resident_key->key.public_key_len > 0);
assert_non_null(resident_key->key.key_handle);
assert_true(resident_key->key.key_handle_len > 0);
}
const char *torture_get_sk_pin(void) const char *torture_get_sk_pin(void)
{ {
const char *pin = getenv("TORTURE_SK_PIN"); const char *pin = getenv("TORTURE_SK_PIN");

View File

@@ -28,14 +28,56 @@
#define LIBSSH_STATIC #define LIBSSH_STATIC
#include <stdbool.h>
#include "torture.h" #include "torture.h"
#include "torture_pki.h"
/**
* @brief Validate a security key (ssh_key) structure
*
* Checks that the provided key is not NULL, matches the expected key type,
* and other internal fields.
*
* @param[in] key The key to validate
* @param[in] expected_type The expected key type (e.g., SSH_KEYTYPE_SK_ECDSA)
* @param[in] private true if key should be private, false for public
*/
void assert_sk_key_valid(ssh_key key, void assert_sk_key_valid(ssh_key key,
enum ssh_keytypes_e expected_type, enum ssh_keytypes_e expected_type,
bool private); bool private);
/**
* @brief Validate a security key enrollment response structure
*
* Validates that an sk_enroll_response contains valid data from a FIDO2
* enrollment operation, including public key, key handle, signature,
* attestation certificate, and authenticator data.
*
* @param[in] response The enrollment response to validate
* @param[in] flags The expected flags that should match the response flags
*/
void assert_sk_enroll_response(struct sk_enroll_response *response, int flags);
/**
* @brief Validate a security key sign response structure
*
* Validates that an sk_sign_response contains valid signature data from
* a FIDO2 sign operation.
*
* @param[in] response The sign response to validate
* @param[in] key_type The key type (e.g., SSH_SK_ECDSA, SSH_SK_ED25519)
*/
void assert_sk_sign_response(struct sk_sign_response *response,
enum ssh_keytypes_e key_type);
/**
* @brief Validate a security key resident key structure
*
* Validates that an sk_resident_key contains valid data including application
* identifier, user ID, public key, and key handle.
*
* @param[in] resident_key The resident key to validate
*/
void assert_sk_resident_key(struct sk_resident_key *resident_key);
/** /**
* @brief Get security key PIN from environment variable * @brief Get security key PIN from environment variable
* *