1/* 	$OpenBSD: common.c,v 1.5 2021/12/14 21:25:27 deraadt Exp $ */
2/*
3 * Helpers for key API tests
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#ifdef HAVE_STDINT_H
15#include <stdint.h>
16#endif
17#include <stdlib.h>
18#include <string.h>
19#include <unistd.h>
20
21#ifdef WITH_OPENSSL
22#include <openssl/bn.h>
23#include <openssl/rsa.h>
24#include <openssl/dsa.h>
25#include <openssl/objects.h>
26#ifdef OPENSSL_HAS_NISTP256
27# include <openssl/ec.h>
28#endif /* OPENSSL_HAS_NISTP256 */
29#endif /* WITH_OPENSSL */
30
31#include "openbsd-compat/openssl-compat.h"
32
33#include "../test_helper/test_helper.h"
34
35#include "ssherr.h"
36#include "authfile.h"
37#include "sshkey.h"
38#include "sshbuf.h"
39
40#include "common.h"
41
42struct sshbuf *
43load_file(const char *name)
44{
45	struct sshbuf *ret = NULL;
46
47	ASSERT_INT_EQ(sshbuf_load_file(test_data_file(name), &ret), 0);
48	ASSERT_PTR_NE(ret, NULL);
49	return ret;
50}
51
52struct sshbuf *
53load_text_file(const char *name)
54{
55	struct sshbuf *ret = load_file(name);
56	const u_char *p;
57
58	/* Trim whitespace at EOL */
59	for (p = sshbuf_ptr(ret); sshbuf_len(ret) > 0;) {
60		if (p[sshbuf_len(ret) - 1] == '\r' ||
61		    p[sshbuf_len(ret) - 1] == '\t' ||
62		    p[sshbuf_len(ret) - 1] == ' ' ||
63		    p[sshbuf_len(ret) - 1] == '\n')
64			ASSERT_INT_EQ(sshbuf_consume_end(ret, 1), 0);
65		else
66			break;
67	}
68	/* \0 terminate */
69	ASSERT_INT_EQ(sshbuf_put_u8(ret, 0), 0);
70	return ret;
71}
72
73#ifdef WITH_OPENSSL
74BIGNUM *
75load_bignum(const char *name)
76{
77	BIGNUM *ret = NULL;
78	struct sshbuf *buf;
79
80	buf = load_text_file(name);
81	ASSERT_INT_NE(BN_hex2bn(&ret, (const char *)sshbuf_ptr(buf)), 0);
82	sshbuf_free(buf);
83	return ret;
84}
85
86const BIGNUM *
87rsa_n(struct sshkey *k)
88{
89	const BIGNUM *n = NULL;
90
91	ASSERT_PTR_NE(k, NULL);
92	ASSERT_PTR_NE(k->rsa, NULL);
93	RSA_get0_key(k->rsa, &n, NULL, NULL);
94	return n;
95}
96
97const BIGNUM *
98rsa_e(struct sshkey *k)
99{
100	const BIGNUM *e = NULL;
101
102	ASSERT_PTR_NE(k, NULL);
103	ASSERT_PTR_NE(k->rsa, NULL);
104	RSA_get0_key(k->rsa, NULL, &e, NULL);
105	return e;
106}
107
108const BIGNUM *
109rsa_p(struct sshkey *k)
110{
111	const BIGNUM *p = NULL;
112
113	ASSERT_PTR_NE(k, NULL);
114	ASSERT_PTR_NE(k->rsa, NULL);
115	RSA_get0_factors(k->rsa, &p, NULL);
116	return p;
117}
118
119const BIGNUM *
120rsa_q(struct sshkey *k)
121{
122	const BIGNUM *q = NULL;
123
124	ASSERT_PTR_NE(k, NULL);
125	ASSERT_PTR_NE(k->rsa, NULL);
126	RSA_get0_factors(k->rsa, NULL, &q);
127	return q;
128}
129
130const BIGNUM *
131dsa_g(struct sshkey *k)
132{
133	const BIGNUM *g = NULL;
134
135	ASSERT_PTR_NE(k, NULL);
136	ASSERT_PTR_NE(k->dsa, NULL);
137	DSA_get0_pqg(k->dsa, NULL, NULL, &g);
138	return g;
139}
140
141const BIGNUM *
142dsa_pub_key(struct sshkey *k)
143{
144	const BIGNUM *pub_key = NULL;
145
146	ASSERT_PTR_NE(k, NULL);
147	ASSERT_PTR_NE(k->dsa, NULL);
148	DSA_get0_key(k->dsa, &pub_key, NULL);
149	return pub_key;
150}
151
152const BIGNUM *
153dsa_priv_key(struct sshkey *k)
154{
155	const BIGNUM *priv_key = NULL;
156
157	ASSERT_PTR_NE(k, NULL);
158	ASSERT_PTR_NE(k->dsa, NULL);
159	DSA_get0_key(k->dsa, NULL, &priv_key);
160	return priv_key;
161}
162#endif /* WITH_OPENSSL */
163
164