1
1
libssh/tests/pkd/pkd_keyutil.c

139 строки
4.2 KiB
C
Исходник Обычный вид История

tests: introduce pkd_hello Introduce a sample public-key testing daemon to the 'pkd' test directory, and add support code for cycling through various combinations of different key exchange, cipher, and MAC algorithms. The goal of the 'pkd_hello' test is to make it easy to test interactions between non-libssh clients and a libssh-server, and to provide a starting point for testing new implementations for key types, ciphers, MACs, and so on. The thinking is that testing a new algorithm should be as simple as adding a new line for it in the PKDTESTS_* lists. Macros are used to generate the tests and helper functions for a couple of clients -- here, OpenSSH and dropbear are included for the first cut. If binaries are found for these clients, their test lists will be enabled; when binaries are not found for a given client, those tests are skipped. Tests are run in one large batch by default, but can also be run individually to help with tracking down things like signature bugs that may take many iterations to reproduce. Each test logs its stdout and stderr to its own file, which is cleaned up when a test succeeds. For failures, those logs can be combined with verbose libssh output from pkd itself to start debugging things. Some example usages: pkd_hello Run all tests with default number of iterations. pkd_hello --list List available individual test names. pkd_hello -i 1000 -t torture_pkd_openssh_ecdsa_256_ecdh_sha2_nistp256 Run only the torture_pkd_openssh_ecdsa_256_ecdh_sha2_nistp256 testcase 1000 times. pkd_hello -v -v -v -v -e -o Run all tests with maximum libssh and pkd logging. Included in the tests are passes for all existing kex, cipher, and MAC algorithms. BUG: https://red.libssh.org/issues/144 Signed-off-by: Jon Simons <jon@jonsimons.org> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2014-10-10 23:38:45 +04:00
/*
* pkd_keyutil.c -- pkd test key utilities
*
* (c) 2014 Jon Simons
*/
#include <setjmp.h> // for cmocka
#include <stdarg.h> // for cmocka
#include <unistd.h> // for cmocka
#include <cmocka.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "pkd_client.h"
#include "pkd_keyutil.h"
#include "pkd_util.h"
void setup_rsa_key() {
int rc = 0;
if (access(LIBSSH_RSA_TESTKEY, F_OK) != 0) {
rc = system_checked(OPENSSH_KEYGEN " -t rsa -q -N \"\" -f "
LIBSSH_RSA_TESTKEY);
}
assert_int_equal(rc, 0);
}
void setup_dsa_key() {
int rc = 0;
if (access(LIBSSH_DSA_TESTKEY, F_OK) != 0) {
rc = system_checked(OPENSSH_KEYGEN " -t dsa -q -N \"\" -f "
LIBSSH_DSA_TESTKEY);
}
assert_int_equal(rc, 0);
}
void setup_ecdsa_keys() {
int rc = 0;
if (access(LIBSSH_ECDSA_256_TESTKEY, F_OK) != 0) {
rc = system_checked(OPENSSH_KEYGEN " -t ecdsa -b 256 -q -N \"\" -f "
LIBSSH_ECDSA_256_TESTKEY);
assert_int_equal(rc, 0);
}
if (access(LIBSSH_ECDSA_384_TESTKEY, F_OK) != 0) {
rc = system_checked(OPENSSH_KEYGEN " -t ecdsa -b 384 -q -N \"\" -f "
LIBSSH_ECDSA_384_TESTKEY);
assert_int_equal(rc, 0);
}
if (access(LIBSSH_ECDSA_521_TESTKEY, F_OK) != 0) {
rc = system_checked(OPENSSH_KEYGEN " -t ecdsa -b 521 -q -N \"\" -f "
LIBSSH_ECDSA_521_TESTKEY);
assert_int_equal(rc, 0);
}
}
static void cleanup_key(const char *privkey, const char *pubkey) {
unlink(privkey);
unlink(pubkey);
}
void cleanup_rsa_key() {
cleanup_key(LIBSSH_RSA_TESTKEY, LIBSSH_RSA_TESTKEY ".pub");
}
void cleanup_dsa_key() {
cleanup_key(LIBSSH_DSA_TESTKEY, LIBSSH_DSA_TESTKEY ".pub");
}
void cleanup_ecdsa_keys() {
cleanup_key(LIBSSH_ECDSA_256_TESTKEY, LIBSSH_ECDSA_256_TESTKEY ".pub");
cleanup_key(LIBSSH_ECDSA_384_TESTKEY, LIBSSH_ECDSA_384_TESTKEY ".pub");
cleanup_key(LIBSSH_ECDSA_521_TESTKEY, LIBSSH_ECDSA_521_TESTKEY ".pub");
}
void setup_openssh_client_keys() {
int rc = 0;
if (access(OPENSSH_DSA_TESTKEY, F_OK) != 0) {
rc = system_checked(OPENSSH_KEYGEN " -t dsa -q -N \"\" -f "
OPENSSH_DSA_TESTKEY);
}
assert_int_equal(rc, 0);
if (access(OPENSSH_RSA_TESTKEY, F_OK) != 0) {
rc = system_checked(OPENSSH_KEYGEN " -t rsa -q -N \"\" -f "
OPENSSH_RSA_TESTKEY);
}
assert_int_equal(rc, 0);
if (access(OPENSSH_ECDSA256_TESTKEY, F_OK) != 0) {
rc = system_checked(OPENSSH_KEYGEN " -t ecdsa -b 256 -q -N \"\" -f "
OPENSSH_ECDSA256_TESTKEY);
}
assert_int_equal(rc, 0);
if (access(OPENSSH_ECDSA384_TESTKEY, F_OK) != 0) {
rc = system_checked(OPENSSH_KEYGEN " -t ecdsa -b 384 -q -N \"\" -f "
OPENSSH_ECDSA384_TESTKEY);
}
assert_int_equal(rc, 0);
if (access(OPENSSH_ECDSA521_TESTKEY, F_OK) != 0) {
rc = system_checked(OPENSSH_KEYGEN " -t ecdsa -b 521 -q -N \"\" -f "
OPENSSH_ECDSA521_TESTKEY);
}
assert_int_equal(rc, 0);
if (access(OPENSSH_ED25519_TESTKEY, F_OK) != 0) {
rc = system_checked(OPENSSH_KEYGEN " -t ed25519 -q -N \"\" -f "
OPENSSH_ED25519_TESTKEY);
}
assert_int_equal(rc, 0);
}
void cleanup_openssh_client_keys() {
cleanup_key(OPENSSH_DSA_TESTKEY, OPENSSH_DSA_TESTKEY ".pub");
cleanup_key(OPENSSH_RSA_TESTKEY, OPENSSH_RSA_TESTKEY ".pub");
cleanup_key(OPENSSH_ECDSA256_TESTKEY, OPENSSH_ECDSA256_TESTKEY ".pub");
cleanup_key(OPENSSH_ECDSA384_TESTKEY, OPENSSH_ECDSA384_TESTKEY ".pub");
cleanup_key(OPENSSH_ECDSA521_TESTKEY, OPENSSH_ECDSA521_TESTKEY ".pub");
cleanup_key(OPENSSH_ED25519_TESTKEY, OPENSSH_ED25519_TESTKEY ".pub");
}
void setup_dropbear_client_rsa_key() {
int rc = 0;
if (access(DROPBEAR_RSA_TESTKEY, F_OK) != 0) {
rc = system_checked(DROPBEAR_KEYGEN " -t rsa -f "
DROPBEAR_RSA_TESTKEY " 1>/dev/null 2>/dev/null");
}
assert_int_equal(rc, 0);
}
void cleanup_dropbear_client_rsa_key() {
unlink(DROPBEAR_RSA_TESTKEY);
}