xref: /openbsd-src/regress/usr.bin/ssh/unittests/sshkey/common.c (revision 123df87b87cd13dad2ee6e095f329c7c51f69244)
1*123df87bSdjm /* 	$OpenBSD: common.c,v 1.6 2024/08/15 00:52:23 djm Exp $ */
2a7772ff7Sdjm /*
3a7772ff7Sdjm  * Helpers for key API tests
4a7772ff7Sdjm  *
5a7772ff7Sdjm  * Placed in the public domain
6a7772ff7Sdjm  */
7a7772ff7Sdjm 
8a7772ff7Sdjm #include <sys/types.h>
9a7772ff7Sdjm #include <sys/stat.h>
10a7772ff7Sdjm #include <fcntl.h>
11a7772ff7Sdjm #include <stdio.h>
12a7772ff7Sdjm #include <stdint.h>
13a7772ff7Sdjm #include <stdlib.h>
14a7772ff7Sdjm #include <string.h>
15a7772ff7Sdjm #include <unistd.h>
16a7772ff7Sdjm 
17a7772ff7Sdjm #include <openssl/bn.h>
18a7772ff7Sdjm #include <openssl/ec.h>
19a7772ff7Sdjm #include <openssl/rsa.h>
20a7772ff7Sdjm #include <openssl/dsa.h>
21a7772ff7Sdjm #include <openssl/objects.h>
22a7772ff7Sdjm 
23a7772ff7Sdjm #include "test_helper.h"
24a7772ff7Sdjm 
25a7772ff7Sdjm #include "ssherr.h"
26a7772ff7Sdjm #include "authfile.h"
27a7772ff7Sdjm #include "sshkey.h"
28a7772ff7Sdjm #include "sshbuf.h"
29a7772ff7Sdjm 
30a7772ff7Sdjm #include "common.h"
31a7772ff7Sdjm 
32a7772ff7Sdjm struct sshbuf *
33a7772ff7Sdjm load_file(const char *name)
34a7772ff7Sdjm {
350ad1e263Sdjm 	struct sshbuf *ret = NULL;
36a7772ff7Sdjm 
370ad1e263Sdjm 	ASSERT_INT_EQ(sshbuf_load_file(test_data_file(name), &ret), 0);
380ad1e263Sdjm 	ASSERT_PTR_NE(ret, NULL);
39a7772ff7Sdjm 	return ret;
40a7772ff7Sdjm }
41a7772ff7Sdjm 
42a7772ff7Sdjm struct sshbuf *
43a7772ff7Sdjm load_text_file(const char *name)
44a7772ff7Sdjm {
45a7772ff7Sdjm 	struct sshbuf *ret = load_file(name);
46a7772ff7Sdjm 	const u_char *p;
47a7772ff7Sdjm 
48a7772ff7Sdjm 	/* Trim whitespace at EOL */
49a7772ff7Sdjm 	for (p = sshbuf_ptr(ret); sshbuf_len(ret) > 0;) {
50a7772ff7Sdjm 		if (p[sshbuf_len(ret) - 1] == '\r' ||
51a7772ff7Sdjm 		    p[sshbuf_len(ret) - 1] == '\t' ||
52a7772ff7Sdjm 		    p[sshbuf_len(ret) - 1] == ' ' ||
53a7772ff7Sdjm 		    p[sshbuf_len(ret) - 1] == '\n')
54a7772ff7Sdjm 			ASSERT_INT_EQ(sshbuf_consume_end(ret, 1), 0);
55a7772ff7Sdjm 		else
56a7772ff7Sdjm 			break;
57a7772ff7Sdjm 	}
58a7772ff7Sdjm 	/* \0 terminate */
59a7772ff7Sdjm 	ASSERT_INT_EQ(sshbuf_put_u8(ret, 0), 0);
60a7772ff7Sdjm 	return ret;
61a7772ff7Sdjm }
62a7772ff7Sdjm 
63a7772ff7Sdjm BIGNUM *
64a7772ff7Sdjm load_bignum(const char *name)
65a7772ff7Sdjm {
66a7772ff7Sdjm 	BIGNUM *ret = NULL;
67a7772ff7Sdjm 	struct sshbuf *buf;
68a7772ff7Sdjm 
69a7772ff7Sdjm 	buf = load_text_file(name);
70a7772ff7Sdjm 	ASSERT_INT_NE(BN_hex2bn(&ret, (const char *)sshbuf_ptr(buf)), 0);
71a7772ff7Sdjm 	sshbuf_free(buf);
72a7772ff7Sdjm 	return ret;
73a7772ff7Sdjm }
74a7772ff7Sdjm 
75fdf0c4e1Sdjm const BIGNUM *
76fdf0c4e1Sdjm rsa_n(struct sshkey *k)
77fdf0c4e1Sdjm {
78fdf0c4e1Sdjm 	const BIGNUM *n = NULL;
79fdf0c4e1Sdjm 
80fdf0c4e1Sdjm 	ASSERT_PTR_NE(k, NULL);
81*123df87bSdjm 	ASSERT_PTR_NE(k->pkey, NULL);
82*123df87bSdjm 	RSA_get0_key(EVP_PKEY_get0_RSA(k->pkey), &n, NULL, NULL);
83fdf0c4e1Sdjm 	return n;
84fdf0c4e1Sdjm }
85fdf0c4e1Sdjm 
86fdf0c4e1Sdjm const BIGNUM *
87fdf0c4e1Sdjm rsa_e(struct sshkey *k)
88fdf0c4e1Sdjm {
89fdf0c4e1Sdjm 	const BIGNUM *e = NULL;
90fdf0c4e1Sdjm 
91fdf0c4e1Sdjm 	ASSERT_PTR_NE(k, NULL);
92*123df87bSdjm 	ASSERT_PTR_NE(k->pkey, NULL);
93*123df87bSdjm 	RSA_get0_key(EVP_PKEY_get0_RSA(k->pkey), NULL, &e, NULL);
94fdf0c4e1Sdjm 	return e;
95fdf0c4e1Sdjm }
96fdf0c4e1Sdjm 
97fdf0c4e1Sdjm const BIGNUM *
98fdf0c4e1Sdjm rsa_p(struct sshkey *k)
99fdf0c4e1Sdjm {
100fdf0c4e1Sdjm 	const BIGNUM *p = NULL;
101fdf0c4e1Sdjm 
102fdf0c4e1Sdjm 	ASSERT_PTR_NE(k, NULL);
103*123df87bSdjm 	ASSERT_PTR_NE(EVP_PKEY_get0_RSA(k->pkey), NULL);
104*123df87bSdjm 	RSA_get0_factors(EVP_PKEY_get0_RSA(k->pkey), &p, NULL);
105fdf0c4e1Sdjm 	return p;
106fdf0c4e1Sdjm }
107fdf0c4e1Sdjm 
108fdf0c4e1Sdjm const BIGNUM *
109fdf0c4e1Sdjm rsa_q(struct sshkey *k)
110fdf0c4e1Sdjm {
111fdf0c4e1Sdjm 	const BIGNUM *q = NULL;
112fdf0c4e1Sdjm 
113fdf0c4e1Sdjm 	ASSERT_PTR_NE(k, NULL);
114*123df87bSdjm 	ASSERT_PTR_NE(EVP_PKEY_get0_RSA(k->pkey), NULL);
115*123df87bSdjm 	RSA_get0_factors(EVP_PKEY_get0_RSA(k->pkey), NULL, &q);
116fdf0c4e1Sdjm 	return q;
117fdf0c4e1Sdjm }
118fdf0c4e1Sdjm 
119fdf0c4e1Sdjm const BIGNUM *
120fdf0c4e1Sdjm dsa_g(struct sshkey *k)
121fdf0c4e1Sdjm {
122fdf0c4e1Sdjm 	const BIGNUM *g = NULL;
123fdf0c4e1Sdjm 
124fdf0c4e1Sdjm 	ASSERT_PTR_NE(k, NULL);
125fdf0c4e1Sdjm 	ASSERT_PTR_NE(k->dsa, NULL);
126fdf0c4e1Sdjm 	DSA_get0_pqg(k->dsa, NULL, NULL, &g);
127fdf0c4e1Sdjm 	return g;
128fdf0c4e1Sdjm }
129fdf0c4e1Sdjm 
130fdf0c4e1Sdjm const BIGNUM *
131fdf0c4e1Sdjm dsa_pub_key(struct sshkey *k)
132fdf0c4e1Sdjm {
133fdf0c4e1Sdjm 	const BIGNUM *pub_key = NULL;
134fdf0c4e1Sdjm 
135fdf0c4e1Sdjm 	ASSERT_PTR_NE(k, NULL);
136fdf0c4e1Sdjm 	ASSERT_PTR_NE(k->dsa, NULL);
137fdf0c4e1Sdjm 	DSA_get0_key(k->dsa, &pub_key, NULL);
138fdf0c4e1Sdjm 	return pub_key;
139fdf0c4e1Sdjm }
140fdf0c4e1Sdjm 
141fdf0c4e1Sdjm const BIGNUM *
142fdf0c4e1Sdjm dsa_priv_key(struct sshkey *k)
143fdf0c4e1Sdjm {
144fdf0c4e1Sdjm 	const BIGNUM *priv_key = NULL;
145fdf0c4e1Sdjm 
146fdf0c4e1Sdjm 	ASSERT_PTR_NE(k, NULL);
147fdf0c4e1Sdjm 	ASSERT_PTR_NE(k->dsa, NULL);
148fdf0c4e1Sdjm 	DSA_get0_key(k->dsa, NULL, &priv_key);
149fdf0c4e1Sdjm 	return priv_key;
150fdf0c4e1Sdjm }
151fdf0c4e1Sdjm 
152