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