test_sshbuf_getput_crypto.c 7.9 KB

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