1 /* $OpenBSD: test_sshbuf_getput_crypto.c,v 1.2 2019/01/21 12:29:35 djm Exp $ */
2 /*
3 * Regress test for sshbuf.h buffer API
4 *
5 * Placed in the public domain
6 */
7
8 #include "includes.h"
9
10 #ifdef WITH_OPENSSL
11
12 #include <sys/types.h>
13 #include <sys/param.h>
14 #include <stdio.h>
15 #ifdef HAVE_STDINT_H
16 # include <stdint.h>
17 #endif
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <openssl/bn.h>
22 #include <openssl/objects.h>
23 #ifdef OPENSSL_HAS_NISTP256
24 # include <openssl/ec.h>
25 #endif
26
27 #include "../test_helper/test_helper.h"
28 #include "ssherr.h"
29 #include "sshbuf.h"
30
31 void sshbuf_getput_crypto_tests(void);
32
33 void
sshbuf_getput_crypto_tests(void)34 sshbuf_getput_crypto_tests(void)
35 {
36 struct sshbuf *p1;
37 BIGNUM *bn, *bn2;
38 const char *hexbn1 = "0102030405060708090a0b0c0d0e0f10";
39 /* This one has MSB set to test bignum2 encoding negative-avoidance */
40 const char *hexbn2 = "f0e0d0c0b0a0908070605040302010007fff11";
41 u_char expbn1[] = {
42 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
43 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
44 };
45 u_char expbn2[] = {
46 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0, 0x90, 0x80,
47 0x70, 0x60, 0x50, 0x40, 0x30, 0x20, 0x10, 0x00,
48 0x7f, 0xff, 0x11
49 };
50 #if defined(OPENSSL_HAS_ECC) && defined(OPENSSL_HAS_NISTP256)
51 const u_char *d;
52 size_t s;
53 BIGNUM *bn_x, *bn_y;
54 int ec256_nid = NID_X9_62_prime256v1;
55 char *ec256_x = "0C828004839D0106AA59575216191357"
56 "34B451459DADB586677EF9DF55784999";
57 char *ec256_y = "4D196B50F0B4E94B3C73E3A9D4CD9DF2"
58 "C8F9A35E42BDD047550F69D80EC23CD4";
59 u_char expec256[] = {
60 0x04,
61 0x0c, 0x82, 0x80, 0x04, 0x83, 0x9d, 0x01, 0x06,
62 0xaa, 0x59, 0x57, 0x52, 0x16, 0x19, 0x13, 0x57,
63 0x34, 0xb4, 0x51, 0x45, 0x9d, 0xad, 0xb5, 0x86,
64 0x67, 0x7e, 0xf9, 0xdf, 0x55, 0x78, 0x49, 0x99,
65 0x4d, 0x19, 0x6b, 0x50, 0xf0, 0xb4, 0xe9, 0x4b,
66 0x3c, 0x73, 0xe3, 0xa9, 0xd4, 0xcd, 0x9d, 0xf2,
67 0xc8, 0xf9, 0xa3, 0x5e, 0x42, 0xbd, 0xd0, 0x47,
68 0x55, 0x0f, 0x69, 0xd8, 0x0e, 0xc2, 0x3c, 0xd4
69 };
70 EC_KEY *eck;
71 EC_POINT *ecp;
72 #endif
73 int r;
74
75 #define MKBN(b, bnn) \
76 do { \
77 bnn = NULL; \
78 ASSERT_INT_GT(BN_hex2bn(&bnn, b), 0); \
79 } while (0)
80
81 TEST_START("sshbuf_put_bignum2");
82 MKBN(hexbn1, bn);
83 p1 = sshbuf_new();
84 ASSERT_PTR_NE(p1, NULL);
85 ASSERT_INT_EQ(sshbuf_put_bignum2(p1, bn), 0);
86 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn1) + 4);
87 ASSERT_U32_EQ(PEEK_U32(sshbuf_ptr(p1)), (u_int32_t)BN_num_bytes(bn));
88 ASSERT_MEM_EQ(sshbuf_ptr(p1) + 4, expbn1, sizeof(expbn1));
89 BN_free(bn);
90 sshbuf_free(p1);
91 TEST_DONE();
92
93 TEST_START("sshbuf_put_bignum2 limited");
94 MKBN(hexbn1, bn);
95 p1 = sshbuf_new();
96 ASSERT_PTR_NE(p1, NULL);
97 ASSERT_INT_EQ(sshbuf_set_max_size(p1, sizeof(expbn1) + 3), 0);
98 r = sshbuf_put_bignum2(p1, bn);
99 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
100 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
101 BN_free(bn);
102 sshbuf_free(p1);
103 TEST_DONE();
104
105 TEST_START("sshbuf_put_bignum2 bn2");
106 MKBN(hexbn2, bn);
107 p1 = sshbuf_new();
108 ASSERT_PTR_NE(p1, NULL);
109 ASSERT_INT_EQ(sshbuf_put_bignum2(p1, bn), 0);
110 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn2) + 4 + 1); /* MSB */
111 ASSERT_U32_EQ(PEEK_U32(sshbuf_ptr(p1)), (u_int32_t)BN_num_bytes(bn) + 1);
112 ASSERT_U8_EQ(*(sshbuf_ptr(p1) + 4), 0x00);
113 ASSERT_MEM_EQ(sshbuf_ptr(p1) + 5, expbn2, sizeof(expbn2));
114 BN_free(bn);
115 sshbuf_free(p1);
116 TEST_DONE();
117
118 TEST_START("sshbuf_put_bignum2 bn2 limited");
119 MKBN(hexbn2, bn);
120 p1 = sshbuf_new();
121 ASSERT_PTR_NE(p1, NULL);
122 ASSERT_INT_EQ(sshbuf_set_max_size(p1, sizeof(expbn2) + 3), 0);
123 r = sshbuf_put_bignum2(p1, bn);
124 ASSERT_INT_EQ(r, SSH_ERR_NO_BUFFER_SPACE);
125 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 0);
126 BN_free(bn);
127 sshbuf_free(p1);
128 TEST_DONE();
129
130 TEST_START("sshbuf_get_bignum2");
131 MKBN(hexbn1, bn);
132 p1 = sshbuf_new();
133 ASSERT_PTR_NE(p1, NULL);
134 ASSERT_INT_EQ(sshbuf_put_u32(p1, BN_num_bytes(bn)), 0);
135 ASSERT_INT_EQ(sshbuf_put(p1, expbn1, sizeof(expbn1)), 0);
136 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4 + sizeof(expbn1));
137 ASSERT_INT_EQ(sshbuf_put_u16(p1, 0xd00f), 0);
138 bn2 = NULL;
139 ASSERT_INT_EQ(sshbuf_get_bignum2(p1, &bn2), 0);
140 ASSERT_BIGNUM_EQ(bn, bn2);
141 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
142 BN_free(bn);
143 BN_free(bn2);
144 sshbuf_free(p1);
145 TEST_DONE();
146
147 TEST_START("sshbuf_get_bignum2 truncated");
148 MKBN(hexbn1, bn);
149 p1 = sshbuf_new();
150 ASSERT_PTR_NE(p1, NULL);
151 ASSERT_INT_EQ(sshbuf_put_u32(p1, BN_num_bytes(bn)), 0);
152 ASSERT_INT_EQ(sshbuf_put(p1, expbn1, sizeof(expbn1) - 1), 0);
153 bn2 = NULL;
154 r = sshbuf_get_bignum2(p1, &bn2);
155 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
156 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn1) + 3);
157 BN_free(bn);
158 BN_free(bn2);
159 sshbuf_free(p1);
160 TEST_DONE();
161
162 TEST_START("sshbuf_get_bignum2 giant");
163 MKBN(hexbn1, bn);
164 p1 = sshbuf_new();
165 ASSERT_PTR_NE(p1, NULL);
166 ASSERT_INT_EQ(sshbuf_put_u32(p1, 65536), 0);
167 ASSERT_INT_EQ(sshbuf_reserve(p1, 65536, NULL), 0);
168 bn2 = NULL;
169 r = sshbuf_get_bignum2(p1, &bn2);
170 ASSERT_INT_EQ(r, SSH_ERR_BIGNUM_TOO_LARGE);
171 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 65536 + 4);
172 BN_free(bn);
173 BN_free(bn2);
174 sshbuf_free(p1);
175 TEST_DONE();
176
177 TEST_START("sshbuf_get_bignum2 bn2");
178 MKBN(hexbn2, bn);
179 p1 = sshbuf_new();
180 ASSERT_PTR_NE(p1, NULL);
181 ASSERT_INT_EQ(sshbuf_put_u32(p1, BN_num_bytes(bn) + 1), 0); /* MSB */
182 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x00), 0);
183 ASSERT_INT_EQ(sshbuf_put(p1, expbn2, sizeof(expbn2)), 0);
184 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 4 + 1 + sizeof(expbn2));
185 ASSERT_INT_EQ(sshbuf_put_u16(p1, 0xd00f), 0);
186 bn2 = NULL;
187 ASSERT_INT_EQ(sshbuf_get_bignum2(p1, &bn2), 0);
188 ASSERT_BIGNUM_EQ(bn, bn2);
189 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 2);
190 BN_free(bn);
191 BN_free(bn2);
192 sshbuf_free(p1);
193 TEST_DONE();
194
195 TEST_START("sshbuf_get_bignum2 bn2 truncated");
196 MKBN(hexbn2, bn);
197 p1 = sshbuf_new();
198 ASSERT_PTR_NE(p1, NULL);
199 ASSERT_INT_EQ(sshbuf_put_u32(p1, BN_num_bytes(bn) + 1), 0);
200 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x00), 0);
201 ASSERT_INT_EQ(sshbuf_put(p1, expbn2, sizeof(expbn2) - 1), 0);
202 bn2 = NULL;
203 r = sshbuf_get_bignum2(p1, &bn2);
204 ASSERT_INT_EQ(r, SSH_ERR_MESSAGE_INCOMPLETE);
205 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn2) + 1 + 4 - 1);
206 BN_free(bn);
207 BN_free(bn2);
208 sshbuf_free(p1);
209 TEST_DONE();
210
211 TEST_START("sshbuf_get_bignum2 bn2 negative");
212 MKBN(hexbn2, bn);
213 p1 = sshbuf_new();
214 ASSERT_PTR_NE(p1, NULL);
215 ASSERT_INT_EQ(sshbuf_put_u32(p1, BN_num_bytes(bn)), 0);
216 ASSERT_INT_EQ(sshbuf_put(p1, expbn2, sizeof(expbn2)), 0);
217 bn2 = NULL;
218 r = sshbuf_get_bignum2(p1, &bn2);
219 ASSERT_INT_EQ(r, SSH_ERR_BIGNUM_IS_NEGATIVE);
220 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expbn2) + 4);
221 BN_free(bn);
222 BN_free(bn2);
223 sshbuf_free(p1);
224 TEST_DONE();
225
226 #if defined(OPENSSL_HAS_ECC) && defined(OPENSSL_HAS_NISTP256)
227 TEST_START("sshbuf_put_ec");
228 eck = EC_KEY_new_by_curve_name(ec256_nid);
229 ASSERT_PTR_NE(eck, NULL);
230 ecp = EC_POINT_new(EC_KEY_get0_group(eck));
231 ASSERT_PTR_NE(ecp, NULL);
232 MKBN(ec256_x, bn_x);
233 MKBN(ec256_y, bn_y);
234 ASSERT_INT_EQ(EC_POINT_set_affine_coordinates_GFp(
235 EC_KEY_get0_group(eck), ecp, bn_x, bn_y, NULL), 1);
236 ASSERT_INT_EQ(EC_KEY_set_public_key(eck, ecp), 1);
237 BN_free(bn_x);
238 BN_free(bn_y);
239 EC_POINT_free(ecp);
240 p1 = sshbuf_new();
241 ASSERT_PTR_NE(p1, NULL);
242 ASSERT_INT_EQ(sshbuf_put_eckey(p1, eck), 0);
243 ASSERT_INT_EQ(sshbuf_get_string_direct(p1, &d, &s), 0);
244 ASSERT_SIZE_T_EQ(s, sizeof(expec256));
245 ASSERT_MEM_EQ(d, expec256, sizeof(expec256));
246 sshbuf_free(p1);
247 EC_KEY_free(eck);
248 TEST_DONE();
249
250 TEST_START("sshbuf_get_ec");
251 eck = EC_KEY_new_by_curve_name(ec256_nid);
252 ASSERT_PTR_NE(eck, NULL);
253 p1 = sshbuf_new();
254 ASSERT_PTR_NE(p1, NULL);
255 ASSERT_INT_EQ(sshbuf_put_string(p1, expec256, sizeof(expec256)), 0);
256 ASSERT_SIZE_T_EQ(sshbuf_len(p1), sizeof(expec256) + 4);
257 ASSERT_INT_EQ(sshbuf_put_u8(p1, 0x00), 0);
258 ASSERT_INT_EQ(sshbuf_get_eckey(p1, eck), 0);
259 bn_x = BN_new();
260 bn_y = BN_new();
261 ASSERT_PTR_NE(bn_x, NULL);
262 ASSERT_PTR_NE(bn_y, NULL);
263 ASSERT_INT_EQ(EC_POINT_get_affine_coordinates_GFp(
264 EC_KEY_get0_group(eck), EC_KEY_get0_public_key(eck),
265 bn_x, bn_y, NULL), 1);
266 MKBN(ec256_x, bn);
267 MKBN(ec256_y, bn2);
268 ASSERT_INT_EQ(BN_cmp(bn_x, bn), 0);
269 ASSERT_INT_EQ(BN_cmp(bn_y, bn2), 0);
270 ASSERT_SIZE_T_EQ(sshbuf_len(p1), 1);
271 sshbuf_free(p1);
272 EC_KEY_free(eck);
273 BN_free(bn_x);
274 BN_free(bn_y);
275 BN_free(bn);
276 BN_free(bn2);
277 TEST_DONE();
278 #endif
279 }
280
281 #endif /* WITH_OPENSSL */
282