1*a91a2465SEd Maste /* $OpenBSD: tests.c,v 1.4 2024/01/11 01:45:59 djm Exp $ */
219261079SEd Maste /*
319261079SEd Maste * Regress test for sshbuf.h buffer API
419261079SEd Maste *
519261079SEd Maste * Placed in the public domain
619261079SEd Maste */
719261079SEd Maste
819261079SEd Maste #include "includes.h"
919261079SEd Maste
1019261079SEd Maste #include <sys/types.h>
1119261079SEd Maste #include <sys/stat.h>
1219261079SEd Maste #include <fcntl.h>
1319261079SEd Maste #include <stdio.h>
1419261079SEd Maste #ifdef HAVE_STDINT_H
1519261079SEd Maste #include <stdint.h>
1619261079SEd Maste #endif
1719261079SEd Maste #include <stdlib.h>
1819261079SEd Maste #include <string.h>
1919261079SEd Maste #include <unistd.h>
2019261079SEd Maste
211323ec57SEd Maste #ifdef WITH_OPENSSL
2219261079SEd Maste #include <openssl/evp.h>
2319261079SEd Maste #include <openssl/crypto.h>
241323ec57SEd Maste #endif
2519261079SEd Maste
2619261079SEd Maste #include "ssherr.h"
2719261079SEd Maste #include "authfile.h"
2819261079SEd Maste #include "sshkey.h"
2919261079SEd Maste #include "sshbuf.h"
3019261079SEd Maste #include "sshsig.h"
3119261079SEd Maste #include "log.h"
3219261079SEd Maste
3319261079SEd Maste #include "../test_helper/test_helper.h"
3419261079SEd Maste
3519261079SEd Maste static struct sshbuf *
load_file(const char * name)3619261079SEd Maste load_file(const char *name)
3719261079SEd Maste {
3819261079SEd Maste struct sshbuf *ret = NULL;
3919261079SEd Maste
4019261079SEd Maste ASSERT_INT_EQ(sshbuf_load_file(test_data_file(name), &ret), 0);
4119261079SEd Maste ASSERT_PTR_NE(ret, NULL);
4219261079SEd Maste return ret;
4319261079SEd Maste }
4419261079SEd Maste
4519261079SEd Maste static struct sshkey *
load_key(const char * name)4619261079SEd Maste load_key(const char *name)
4719261079SEd Maste {
4819261079SEd Maste struct sshkey *ret = NULL;
4919261079SEd Maste ASSERT_INT_EQ(sshkey_load_public(test_data_file(name), &ret, NULL), 0);
5019261079SEd Maste ASSERT_PTR_NE(ret, NULL);
5119261079SEd Maste return ret;
5219261079SEd Maste }
5319261079SEd Maste
5419261079SEd Maste static void
check_sig(const char * keyname,const char * signame,const struct sshbuf * msg,const char * namespace)5519261079SEd Maste check_sig(const char *keyname, const char *signame, const struct sshbuf *msg,
5619261079SEd Maste const char *namespace)
5719261079SEd Maste {
5819261079SEd Maste struct sshkey *k, *sign_key;
5919261079SEd Maste struct sshbuf *sig, *rawsig;
6019261079SEd Maste struct sshkey_sig_details *sig_details;
6119261079SEd Maste
6219261079SEd Maste k = load_key(keyname);
6319261079SEd Maste sig = load_file(signame);
6419261079SEd Maste sign_key = NULL;
6519261079SEd Maste sig_details = NULL;
6619261079SEd Maste rawsig = NULL;
6719261079SEd Maste ASSERT_INT_EQ(sshsig_dearmor(sig, &rawsig), 0);
6819261079SEd Maste ASSERT_INT_EQ(sshsig_verifyb(rawsig, msg, namespace,
6919261079SEd Maste &sign_key, &sig_details), 0);
7019261079SEd Maste ASSERT_INT_EQ(sshkey_equal(k, sign_key), 1);
7119261079SEd Maste sshkey_free(k);
7219261079SEd Maste sshkey_free(sign_key);
7319261079SEd Maste sshkey_sig_details_free(sig_details);
7419261079SEd Maste sshbuf_free(sig);
7519261079SEd Maste sshbuf_free(rawsig);
7619261079SEd Maste }
7719261079SEd Maste
7819261079SEd Maste void
tests(void)7919261079SEd Maste tests(void)
8019261079SEd Maste {
8119261079SEd Maste struct sshbuf *msg;
8219261079SEd Maste char *namespace;
8319261079SEd Maste
8419261079SEd Maste #if 0
8519261079SEd Maste log_init("test_sshsig", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 1);
8619261079SEd Maste #endif
8719261079SEd Maste
8819261079SEd Maste #ifdef WITH_OPENSSL
8919261079SEd Maste OpenSSL_add_all_algorithms();
90535af610SEd Maste ERR_load_crypto_strings();
9119261079SEd Maste #endif
9219261079SEd Maste
9319261079SEd Maste TEST_START("load data");
9419261079SEd Maste msg = load_file("namespace");
9519261079SEd Maste namespace = sshbuf_dup_string(msg);
9619261079SEd Maste ASSERT_PTR_NE(namespace, NULL);
9719261079SEd Maste sshbuf_free(msg);
9819261079SEd Maste msg = load_file("signed-data");
9919261079SEd Maste TEST_DONE();
10019261079SEd Maste
10119261079SEd Maste #ifdef WITH_OPENSSL
10219261079SEd Maste TEST_START("check RSA signature");
10319261079SEd Maste check_sig("rsa.pub", "rsa.sig", msg, namespace);
10419261079SEd Maste TEST_DONE();
10519261079SEd Maste
106*a91a2465SEd Maste #ifdef WITH_DSA
10719261079SEd Maste TEST_START("check DSA signature");
10819261079SEd Maste check_sig("dsa.pub", "dsa.sig", msg, namespace);
10919261079SEd Maste TEST_DONE();
110*a91a2465SEd Maste #endif
11119261079SEd Maste
11219261079SEd Maste #ifdef OPENSSL_HAS_ECC
11319261079SEd Maste TEST_START("check ECDSA signature");
11419261079SEd Maste check_sig("ecdsa.pub", "ecdsa.sig", msg, namespace);
11519261079SEd Maste TEST_DONE();
11619261079SEd Maste #endif
11719261079SEd Maste #endif
11819261079SEd Maste
11919261079SEd Maste TEST_START("check ED25519 signature");
12019261079SEd Maste check_sig("ed25519.pub", "ed25519.sig", msg, namespace);
12119261079SEd Maste TEST_DONE();
12219261079SEd Maste
1231323ec57SEd Maste #ifdef ENABLE_SK
12419261079SEd Maste #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
12519261079SEd Maste TEST_START("check ECDSA-SK signature");
12619261079SEd Maste check_sig("ecdsa_sk.pub", "ecdsa_sk.sig", msg, namespace);
12719261079SEd Maste TEST_DONE();
12819261079SEd Maste #endif
12919261079SEd Maste
13019261079SEd Maste TEST_START("check ED25519-SK signature");
13119261079SEd Maste check_sig("ed25519_sk.pub", "ed25519_sk.sig", msg, namespace);
13219261079SEd Maste TEST_DONE();
13319261079SEd Maste
13419261079SEd Maste #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
13519261079SEd Maste TEST_START("check ECDSA-SK webauthn signature");
13619261079SEd Maste check_sig("ecdsa_sk_webauthn.pub", "ecdsa_sk_webauthn.sig",
13719261079SEd Maste msg, namespace);
13819261079SEd Maste TEST_DONE();
13919261079SEd Maste #endif
1401323ec57SEd Maste #endif /* ENABLE_SK */
14119261079SEd Maste
14219261079SEd Maste sshbuf_free(msg);
14319261079SEd Maste free(namespace);
14419261079SEd Maste }
145