| 1 |
/* $OpenBSD: tests.c,v 1.6 2025/05/06 06:05:48 djm Exp $ */ |
| 2 |
/* |
| 3 |
* Regress test for sshbuf.h buffer API |
| 4 |
* |
| 5 |
* Placed in the public domain |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "includes.h" |
| 9 |
|
| 10 |
#include <sys/types.h> |
| 11 |
#include <sys/stat.h> |
| 12 |
#include <fcntl.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <stdint.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <string.h> |
| 17 |
#include <unistd.h> |
| 18 |
|
| 19 |
#ifdef WITH_OPENSSL |
| 20 |
#include <openssl/evp.h> |
| 21 |
#include <openssl/crypto.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#include "ssherr.h" |
| 25 |
#include "authfile.h" |
| 26 |
#include "sshkey.h" |
| 27 |
#include "sshbuf.h" |
| 28 |
#include "sshsig.h" |
| 29 |
#include "log.h" |
| 30 |
|
| 31 |
#include "../test_helper/test_helper.h" |
| 32 |
|
| 33 |
static struct sshbuf * |
| 34 |
load_file(const char *name) |
| 35 |
{ |
| 36 |
struct sshbuf *ret = NULL; |
| 37 |
|
| 38 |
ASSERT_INT_EQ(sshbuf_load_file(test_data_file(name), &ret), 0); |
| 39 |
ASSERT_PTR_NE(ret, NULL); |
| 40 |
return ret; |
| 41 |
} |
| 42 |
|
| 43 |
static struct sshkey * |
| 44 |
load_key(const char *name) |
| 45 |
{ |
| 46 |
struct sshkey *ret = NULL; |
| 47 |
ASSERT_INT_EQ(sshkey_load_public(test_data_file(name), &ret, NULL), 0); |
| 48 |
ASSERT_PTR_NE(ret, NULL); |
| 49 |
return ret; |
| 50 |
} |
| 51 |
|
| 52 |
static void |
| 53 |
check_sig(const char *keyname, const char *signame, const struct sshbuf *msg, |
| 54 |
const char *namespace) |
| 55 |
{ |
| 56 |
struct sshkey *k, *sign_key; |
| 57 |
struct sshbuf *sig, *rawsig; |
| 58 |
struct sshkey_sig_details *sig_details; |
| 59 |
|
| 60 |
k = load_key(keyname); |
| 61 |
sig = load_file(signame); |
| 62 |
sign_key = NULL; |
| 63 |
sig_details = NULL; |
| 64 |
rawsig = NULL; |
| 65 |
ASSERT_INT_EQ(sshsig_dearmor(sig, &rawsig), 0); |
| 66 |
ASSERT_INT_EQ(sshsig_verifyb(rawsig, msg, namespace, |
| 67 |
&sign_key, &sig_details), 0); |
| 68 |
ASSERT_INT_EQ(sshkey_equal(k, sign_key), 1); |
| 69 |
sshkey_free(k); |
| 70 |
sshkey_free(sign_key); |
| 71 |
sshkey_sig_details_free(sig_details); |
| 72 |
sshbuf_free(sig); |
| 73 |
sshbuf_free(rawsig); |
| 74 |
} |
| 75 |
|
| 76 |
void |
| 77 |
tests(void) |
| 78 |
{ |
| 79 |
struct sshbuf *msg; |
| 80 |
char *namespace; |
| 81 |
|
| 82 |
#if 0 |
| 83 |
log_init("test_sshsig", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 1); |
| 84 |
#endif |
| 85 |
|
| 86 |
#ifdef WITH_OPENSSL |
| 87 |
OpenSSL_add_all_algorithms(); |
| 88 |
ERR_load_crypto_strings(); |
| 89 |
#endif |
| 90 |
|
| 91 |
TEST_START("load data"); |
| 92 |
msg = load_file("namespace"); |
| 93 |
namespace = sshbuf_dup_string(msg); |
| 94 |
ASSERT_PTR_NE(namespace, NULL); |
| 95 |
sshbuf_free(msg); |
| 96 |
msg = load_file("signed-data"); |
| 97 |
TEST_DONE(); |
| 98 |
|
| 99 |
#ifdef WITH_OPENSSL |
| 100 |
TEST_START("check RSA signature"); |
| 101 |
check_sig("rsa.pub", "rsa.sig", msg, namespace); |
| 102 |
TEST_DONE(); |
| 103 |
|
| 104 |
|
| 105 |
#ifdef OPENSSL_HAS_ECC |
| 106 |
TEST_START("check ECDSA signature"); |
| 107 |
check_sig("ecdsa.pub", "ecdsa.sig", msg, namespace); |
| 108 |
TEST_DONE(); |
| 109 |
#endif |
| 110 |
#endif |
| 111 |
|
| 112 |
TEST_START("check ED25519 signature"); |
| 113 |
check_sig("ed25519.pub", "ed25519.sig", msg, namespace); |
| 114 |
TEST_DONE(); |
| 115 |
|
| 116 |
#ifdef ENABLE_SK |
| 117 |
#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) |
| 118 |
TEST_START("check ECDSA-SK signature"); |
| 119 |
check_sig("ecdsa_sk.pub", "ecdsa_sk.sig", msg, namespace); |
| 120 |
TEST_DONE(); |
| 121 |
#endif |
| 122 |
|
| 123 |
TEST_START("check ED25519-SK signature"); |
| 124 |
check_sig("ed25519_sk.pub", "ed25519_sk.sig", msg, namespace); |
| 125 |
TEST_DONE(); |
| 126 |
|
| 127 |
#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) |
| 128 |
TEST_START("check ECDSA-SK webauthn signature"); |
| 129 |
check_sig("ecdsa_sk_webauthn.pub", "ecdsa_sk_webauthn.sig", |
| 130 |
msg, namespace); |
| 131 |
TEST_DONE(); |
| 132 |
#endif |
| 133 |
#endif /* ENABLE_SK */ |
| 134 |
|
| 135 |
sshbuf_free(msg); |
| 136 |
free(namespace); |
| 137 |
} |
| 138 |
|
| 139 |
void |
| 140 |
benchmarks(void) |
| 141 |
{ |
| 142 |
printf("no benchmarks\n"); |
| 143 |
} |