1*33ada582Sdjm /* $OpenBSD: tests.c,v 1.4 2024/01/11 01:45:59 djm Exp $ */
218d198f4Sdjm /*
318d198f4Sdjm * Regress test for sshbuf.h buffer API
418d198f4Sdjm *
518d198f4Sdjm * Placed in the public domain
618d198f4Sdjm */
718d198f4Sdjm
818d198f4Sdjm #include <sys/types.h>
918d198f4Sdjm #include <sys/stat.h>
1018d198f4Sdjm #include <fcntl.h>
1118d198f4Sdjm #include <stdio.h>
1218d198f4Sdjm #include <stdint.h>
1318d198f4Sdjm #include <stdlib.h>
1418d198f4Sdjm #include <string.h>
1518d198f4Sdjm #include <unistd.h>
1618d198f4Sdjm
1718d198f4Sdjm #include <openssl/evp.h>
1818d198f4Sdjm #include <openssl/crypto.h>
1918d198f4Sdjm
2018d198f4Sdjm #include "ssherr.h"
2118d198f4Sdjm #include "authfile.h"
2218d198f4Sdjm #include "sshkey.h"
2318d198f4Sdjm #include "sshbuf.h"
2418d198f4Sdjm #include "sshsig.h"
2518d198f4Sdjm #include "log.h"
2618d198f4Sdjm
2718d198f4Sdjm #include "test_helper.h"
2818d198f4Sdjm
2918d198f4Sdjm static struct sshbuf *
load_file(const char * name)3018d198f4Sdjm load_file(const char *name)
3118d198f4Sdjm {
3218d198f4Sdjm struct sshbuf *ret = NULL;
3318d198f4Sdjm
3418d198f4Sdjm ASSERT_INT_EQ(sshbuf_load_file(test_data_file(name), &ret), 0);
3518d198f4Sdjm ASSERT_PTR_NE(ret, NULL);
3618d198f4Sdjm return ret;
3718d198f4Sdjm }
3818d198f4Sdjm
3918d198f4Sdjm static struct sshkey *
load_key(const char * name)4018d198f4Sdjm load_key(const char *name)
4118d198f4Sdjm {
4218d198f4Sdjm struct sshkey *ret = NULL;
4318d198f4Sdjm ASSERT_INT_EQ(sshkey_load_public(test_data_file(name), &ret, NULL), 0);
4418d198f4Sdjm ASSERT_PTR_NE(ret, NULL);
4518d198f4Sdjm return ret;
4618d198f4Sdjm }
4718d198f4Sdjm
4818d198f4Sdjm static void
check_sig(const char * keyname,const char * signame,const struct sshbuf * msg,const char * namespace)4918d198f4Sdjm check_sig(const char *keyname, const char *signame, const struct sshbuf *msg,
5018d198f4Sdjm const char *namespace)
5118d198f4Sdjm {
5218d198f4Sdjm struct sshkey *k, *sign_key;
5318d198f4Sdjm struct sshbuf *sig, *rawsig;
5418d198f4Sdjm struct sshkey_sig_details *sig_details;
5518d198f4Sdjm
5618d198f4Sdjm k = load_key(keyname);
5718d198f4Sdjm sig = load_file(signame);
5818d198f4Sdjm sign_key = NULL;
5918d198f4Sdjm sig_details = NULL;
6018d198f4Sdjm rawsig = NULL;
6118d198f4Sdjm ASSERT_INT_EQ(sshsig_dearmor(sig, &rawsig), 0);
6218d198f4Sdjm ASSERT_INT_EQ(sshsig_verifyb(rawsig, msg, namespace,
6318d198f4Sdjm &sign_key, &sig_details), 0);
6418d198f4Sdjm ASSERT_INT_EQ(sshkey_equal(k, sign_key), 1);
6518d198f4Sdjm sshkey_free(k);
6618d198f4Sdjm sshkey_free(sign_key);
6718d198f4Sdjm sshkey_sig_details_free(sig_details);
6818d198f4Sdjm sshbuf_free(sig);
6918d198f4Sdjm sshbuf_free(rawsig);
7018d198f4Sdjm }
7118d198f4Sdjm
7218d198f4Sdjm void
tests(void)7318d198f4Sdjm tests(void)
7418d198f4Sdjm {
7518d198f4Sdjm struct sshbuf *msg;
7618d198f4Sdjm char *namespace;
7718d198f4Sdjm
7818d198f4Sdjm #if 0
7918d198f4Sdjm log_init("test_sshsig", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 1);
8018d198f4Sdjm #endif
8118d198f4Sdjm
8218d198f4Sdjm OpenSSL_add_all_algorithms();
8318d198f4Sdjm ERR_load_CRYPTO_strings();
8418d198f4Sdjm
8518d198f4Sdjm TEST_START("load data");
8618d198f4Sdjm msg = load_file("namespace");
8718d198f4Sdjm namespace = sshbuf_dup_string(msg);
8818d198f4Sdjm ASSERT_PTR_NE(namespace, NULL);
8918d198f4Sdjm sshbuf_free(msg);
9018d198f4Sdjm msg = load_file("signed-data");
9118d198f4Sdjm TEST_DONE();
9218d198f4Sdjm
9318d198f4Sdjm TEST_START("check RSA signature");
940415b8c3Sdjm check_sig("rsa.pub", "rsa.sig", msg, namespace);
9518d198f4Sdjm TEST_DONE();
9618d198f4Sdjm
97*33ada582Sdjm #ifdef WITH_DSA
9818d198f4Sdjm TEST_START("check DSA signature");
990415b8c3Sdjm check_sig("dsa.pub", "dsa.sig", msg, namespace);
10018d198f4Sdjm TEST_DONE();
101*33ada582Sdjm #endif
10218d198f4Sdjm
10318d198f4Sdjm TEST_START("check ECDSA signature");
1040415b8c3Sdjm check_sig("ecdsa.pub", "ecdsa.sig", msg, namespace);
10518d198f4Sdjm TEST_DONE();
10618d198f4Sdjm
10718d198f4Sdjm TEST_START("check ED25519 signature");
1080415b8c3Sdjm check_sig("ed25519.pub", "ed25519.sig", msg, namespace);
10918d198f4Sdjm TEST_DONE();
11018d198f4Sdjm
11118d198f4Sdjm TEST_START("check ECDSA-SK signature");
1120415b8c3Sdjm check_sig("ecdsa_sk.pub", "ecdsa_sk.sig", msg, namespace);
11318d198f4Sdjm TEST_DONE();
11418d198f4Sdjm
11518d198f4Sdjm TEST_START("check ED25519-SK signature");
1160415b8c3Sdjm check_sig("ed25519_sk.pub", "ed25519_sk.sig", msg, namespace);
1170415b8c3Sdjm TEST_DONE();
1180415b8c3Sdjm
1190415b8c3Sdjm TEST_START("check ECDSA-SK webauthn signature");
1200415b8c3Sdjm check_sig("ecdsa_sk_webauthn.pub", "ecdsa_sk_webauthn.sig",
1210415b8c3Sdjm msg, namespace);
12218d198f4Sdjm TEST_DONE();
12318d198f4Sdjm
12418d198f4Sdjm sshbuf_free(msg);
12518d198f4Sdjm free(namespace);
12618d198f4Sdjm }
127