xref: /freebsd/crypto/openssh/regress/unittests/sshkey/common.c (revision 3d9fd9fcb432750f3716b28f6ccb0104cd9d351a)
1*3d9fd9fcSEd Maste /* 	$OpenBSD: common.c,v 1.6 2024/08/15 00:52:23 djm Exp $ */
2a0ee8cc6SDag-Erling Smørgrav /*
3a0ee8cc6SDag-Erling Smørgrav  * Helpers for key API tests
4a0ee8cc6SDag-Erling Smørgrav  *
5a0ee8cc6SDag-Erling Smørgrav  * Placed in the public domain
6a0ee8cc6SDag-Erling Smørgrav  */
7a0ee8cc6SDag-Erling Smørgrav 
8a0ee8cc6SDag-Erling Smørgrav #include "includes.h"
9a0ee8cc6SDag-Erling Smørgrav 
10a0ee8cc6SDag-Erling Smørgrav #include <sys/types.h>
11a0ee8cc6SDag-Erling Smørgrav #include <sys/stat.h>
12a0ee8cc6SDag-Erling Smørgrav #include <fcntl.h>
13a0ee8cc6SDag-Erling Smørgrav #include <stdio.h>
14a0ee8cc6SDag-Erling Smørgrav #ifdef HAVE_STDINT_H
15a0ee8cc6SDag-Erling Smørgrav #include <stdint.h>
16a0ee8cc6SDag-Erling Smørgrav #endif
17a0ee8cc6SDag-Erling Smørgrav #include <stdlib.h>
18a0ee8cc6SDag-Erling Smørgrav #include <string.h>
19a0ee8cc6SDag-Erling Smørgrav #include <unistd.h>
20a0ee8cc6SDag-Erling Smørgrav 
2119261079SEd Maste #ifdef WITH_OPENSSL
22a0ee8cc6SDag-Erling Smørgrav #include <openssl/bn.h>
23a0ee8cc6SDag-Erling Smørgrav #include <openssl/rsa.h>
24a0ee8cc6SDag-Erling Smørgrav #include <openssl/dsa.h>
25a0ee8cc6SDag-Erling Smørgrav #include <openssl/objects.h>
26a0ee8cc6SDag-Erling Smørgrav #ifdef OPENSSL_HAS_NISTP256
27a0ee8cc6SDag-Erling Smørgrav # include <openssl/ec.h>
2819261079SEd Maste #endif /* OPENSSL_HAS_NISTP256 */
2919261079SEd Maste #endif /* WITH_OPENSSL */
30a0ee8cc6SDag-Erling Smørgrav 
312f513db7SEd Maste #include "openbsd-compat/openssl-compat.h"
322f513db7SEd Maste 
33a0ee8cc6SDag-Erling Smørgrav #include "../test_helper/test_helper.h"
34a0ee8cc6SDag-Erling Smørgrav 
35a0ee8cc6SDag-Erling Smørgrav #include "ssherr.h"
36a0ee8cc6SDag-Erling Smørgrav #include "authfile.h"
37a0ee8cc6SDag-Erling Smørgrav #include "sshkey.h"
38a0ee8cc6SDag-Erling Smørgrav #include "sshbuf.h"
39a0ee8cc6SDag-Erling Smørgrav 
40a0ee8cc6SDag-Erling Smørgrav #include "common.h"
41a0ee8cc6SDag-Erling Smørgrav 
42a0ee8cc6SDag-Erling Smørgrav struct sshbuf *
load_file(const char * name)43a0ee8cc6SDag-Erling Smørgrav load_file(const char *name)
44a0ee8cc6SDag-Erling Smørgrav {
4519261079SEd Maste 	struct sshbuf *ret = NULL;
46a0ee8cc6SDag-Erling Smørgrav 
4719261079SEd Maste 	ASSERT_INT_EQ(sshbuf_load_file(test_data_file(name), &ret), 0);
4819261079SEd Maste 	ASSERT_PTR_NE(ret, NULL);
49a0ee8cc6SDag-Erling Smørgrav 	return ret;
50a0ee8cc6SDag-Erling Smørgrav }
51a0ee8cc6SDag-Erling Smørgrav 
52a0ee8cc6SDag-Erling Smørgrav struct sshbuf *
load_text_file(const char * name)53a0ee8cc6SDag-Erling Smørgrav load_text_file(const char *name)
54a0ee8cc6SDag-Erling Smørgrav {
55a0ee8cc6SDag-Erling Smørgrav 	struct sshbuf *ret = load_file(name);
56a0ee8cc6SDag-Erling Smørgrav 	const u_char *p;
57a0ee8cc6SDag-Erling Smørgrav 
58a0ee8cc6SDag-Erling Smørgrav 	/* Trim whitespace at EOL */
59a0ee8cc6SDag-Erling Smørgrav 	for (p = sshbuf_ptr(ret); sshbuf_len(ret) > 0;) {
60a0ee8cc6SDag-Erling Smørgrav 		if (p[sshbuf_len(ret) - 1] == '\r' ||
61a0ee8cc6SDag-Erling Smørgrav 		    p[sshbuf_len(ret) - 1] == '\t' ||
62a0ee8cc6SDag-Erling Smørgrav 		    p[sshbuf_len(ret) - 1] == ' ' ||
63a0ee8cc6SDag-Erling Smørgrav 		    p[sshbuf_len(ret) - 1] == '\n')
64a0ee8cc6SDag-Erling Smørgrav 			ASSERT_INT_EQ(sshbuf_consume_end(ret, 1), 0);
65a0ee8cc6SDag-Erling Smørgrav 		else
66a0ee8cc6SDag-Erling Smørgrav 			break;
67a0ee8cc6SDag-Erling Smørgrav 	}
68a0ee8cc6SDag-Erling Smørgrav 	/* \0 terminate */
69a0ee8cc6SDag-Erling Smørgrav 	ASSERT_INT_EQ(sshbuf_put_u8(ret, 0), 0);
70a0ee8cc6SDag-Erling Smørgrav 	return ret;
71a0ee8cc6SDag-Erling Smørgrav }
72a0ee8cc6SDag-Erling Smørgrav 
7319261079SEd Maste #ifdef WITH_OPENSSL
74a0ee8cc6SDag-Erling Smørgrav BIGNUM *
load_bignum(const char * name)75a0ee8cc6SDag-Erling Smørgrav load_bignum(const char *name)
76a0ee8cc6SDag-Erling Smørgrav {
77a0ee8cc6SDag-Erling Smørgrav 	BIGNUM *ret = NULL;
78a0ee8cc6SDag-Erling Smørgrav 	struct sshbuf *buf;
79a0ee8cc6SDag-Erling Smørgrav 
80a0ee8cc6SDag-Erling Smørgrav 	buf = load_text_file(name);
81a0ee8cc6SDag-Erling Smørgrav 	ASSERT_INT_NE(BN_hex2bn(&ret, (const char *)sshbuf_ptr(buf)), 0);
82a0ee8cc6SDag-Erling Smørgrav 	sshbuf_free(buf);
83a0ee8cc6SDag-Erling Smørgrav 	return ret;
84a0ee8cc6SDag-Erling Smørgrav }
85a0ee8cc6SDag-Erling Smørgrav 
862a01feabSEd Maste const BIGNUM *
rsa_n(struct sshkey * k)872a01feabSEd Maste rsa_n(struct sshkey *k)
882a01feabSEd Maste {
892a01feabSEd Maste 	const BIGNUM *n = NULL;
902a01feabSEd Maste 
912a01feabSEd Maste 	ASSERT_PTR_NE(k, NULL);
92*3d9fd9fcSEd Maste 	ASSERT_PTR_NE(k->pkey, NULL);
93*3d9fd9fcSEd Maste 	RSA_get0_key(EVP_PKEY_get0_RSA(k->pkey), &n, NULL, NULL);
942a01feabSEd Maste 	return n;
952a01feabSEd Maste }
962a01feabSEd Maste 
972a01feabSEd Maste const BIGNUM *
rsa_e(struct sshkey * k)982a01feabSEd Maste rsa_e(struct sshkey *k)
992a01feabSEd Maste {
1002a01feabSEd Maste 	const BIGNUM *e = NULL;
1012a01feabSEd Maste 
1022a01feabSEd Maste 	ASSERT_PTR_NE(k, NULL);
103*3d9fd9fcSEd Maste 	ASSERT_PTR_NE(k->pkey, NULL);
104*3d9fd9fcSEd Maste 	RSA_get0_key(EVP_PKEY_get0_RSA(k->pkey), NULL, &e, NULL);
1052a01feabSEd Maste 	return e;
1062a01feabSEd Maste }
1072a01feabSEd Maste 
1082a01feabSEd Maste const BIGNUM *
rsa_p(struct sshkey * k)1092a01feabSEd Maste rsa_p(struct sshkey *k)
1102a01feabSEd Maste {
1112a01feabSEd Maste 	const BIGNUM *p = NULL;
1122a01feabSEd Maste 
1132a01feabSEd Maste 	ASSERT_PTR_NE(k, NULL);
114*3d9fd9fcSEd Maste 	ASSERT_PTR_NE(EVP_PKEY_get0_RSA(k->pkey), NULL);
115*3d9fd9fcSEd Maste 	RSA_get0_factors(EVP_PKEY_get0_RSA(k->pkey), &p, NULL);
1162a01feabSEd Maste 	return p;
1172a01feabSEd Maste }
1182a01feabSEd Maste 
1192a01feabSEd Maste const BIGNUM *
rsa_q(struct sshkey * k)1202a01feabSEd Maste rsa_q(struct sshkey *k)
1212a01feabSEd Maste {
1222a01feabSEd Maste 	const BIGNUM *q = NULL;
1232a01feabSEd Maste 
1242a01feabSEd Maste 	ASSERT_PTR_NE(k, NULL);
125*3d9fd9fcSEd Maste 	ASSERT_PTR_NE(EVP_PKEY_get0_RSA(k->pkey), NULL);
126*3d9fd9fcSEd Maste 	RSA_get0_factors(EVP_PKEY_get0_RSA(k->pkey), NULL, &q);
1272a01feabSEd Maste 	return q;
1282a01feabSEd Maste }
1292a01feabSEd Maste 
1302a01feabSEd Maste const BIGNUM *
dsa_g(struct sshkey * k)1312a01feabSEd Maste dsa_g(struct sshkey *k)
1322a01feabSEd Maste {
1332a01feabSEd Maste 	const BIGNUM *g = NULL;
1342a01feabSEd Maste 
1352a01feabSEd Maste 	ASSERT_PTR_NE(k, NULL);
1362a01feabSEd Maste 	ASSERT_PTR_NE(k->dsa, NULL);
1372a01feabSEd Maste 	DSA_get0_pqg(k->dsa, NULL, NULL, &g);
1382a01feabSEd Maste 	return g;
1392a01feabSEd Maste }
1402a01feabSEd Maste 
1412a01feabSEd Maste const BIGNUM *
dsa_pub_key(struct sshkey * k)1422a01feabSEd Maste dsa_pub_key(struct sshkey *k)
1432a01feabSEd Maste {
1442a01feabSEd Maste 	const BIGNUM *pub_key = NULL;
1452a01feabSEd Maste 
1462a01feabSEd Maste 	ASSERT_PTR_NE(k, NULL);
1472a01feabSEd Maste 	ASSERT_PTR_NE(k->dsa, NULL);
1482a01feabSEd Maste 	DSA_get0_key(k->dsa, &pub_key, NULL);
1492a01feabSEd Maste 	return pub_key;
1502a01feabSEd Maste }
1512a01feabSEd Maste 
1522a01feabSEd Maste const BIGNUM *
dsa_priv_key(struct sshkey * k)1532a01feabSEd Maste dsa_priv_key(struct sshkey *k)
1542a01feabSEd Maste {
1552a01feabSEd Maste 	const BIGNUM *priv_key = NULL;
1562a01feabSEd Maste 
1572a01feabSEd Maste 	ASSERT_PTR_NE(k, NULL);
1582a01feabSEd Maste 	ASSERT_PTR_NE(k->dsa, NULL);
1592a01feabSEd Maste 	DSA_get0_key(k->dsa, NULL, &priv_key);
1602a01feabSEd Maste 	return priv_key;
1612a01feabSEd Maste }
16219261079SEd Maste #endif /* WITH_OPENSSL */
1632a01feabSEd Maste 
164