Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

Factor out the buffering and low-level record code. This begins decoupling the transport from the SSL state machine. The buffering logic is hidden behind an opaque API. Fields like ssl->packet and ssl->packet_length are gone. ssl3_get_record and dtls1_get_record now call low-level tls_open_record and dtls_open_record functions that unpack a single record independent of who owns the buffer. Both may be called in-place. This removes ssl->rstate which was redundant with the buffer length. Future work will push the buffer up the stack until it is above the handshake. Then we can expose SSL_open and SSL_seal APIs which act like *_open_record but return a slightly larger enum due to other events being possible. Likewise the handshake state machine will be detached from its buffer. The existing SSL_read, SSL_write, etc., APIs will be implemented on top of SSL_open, etc., combined with ssl_read_buffer_* and ssl_write_buffer_*. (Which is why ssl_read_buffer_extend still tries to abstract between TLS's and DTLS's fairly different needs.) The new buffering logic does not support read-ahead (removed previously) since it lacks a memmove on ssl_read_buffer_discard for TLS, but this could be added if desired. The old buffering logic wasn't quite right anyway; it tried to avoid the memmove in some cases and could get stuck too far into the buffer and not accept records. (The only time the memmove is optional is in DTLS or if enough of the record header is available to know that the entire next record would fit in the buffer.) The new logic also now actually decrypts the ciphertext in-place again, rather than almost in-place when there's an explicit nonce/IV. (That accidentally switched in https://boringssl-review.googlesource.com/#/c/4792/; see 3d59e04bce96474099ba76786a2337e99ae14505.) BUG=468889 Change-Id: I403c1626253c46897f47c7ae93aeab1064b767b2 Reviewed-on: https://boringssl-review.googlesource.com/5715 Reviewed-by: Adam Langley <agl@google.com>
9 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/buf.h>
  15. #include <openssl/mem.h>
  16. #include <openssl/bytestring.h>
  17. #include <assert.h>
  18. #include <string.h>
  19. #include "internal.h"
  20. void CBS_init(CBS *cbs, const uint8_t *data, size_t len) {
  21. cbs->data = data;
  22. cbs->len = len;
  23. }
  24. static int cbs_get(CBS *cbs, const uint8_t **p, size_t n) {
  25. if (cbs->len < n) {
  26. return 0;
  27. }
  28. *p = cbs->data;
  29. cbs->data += n;
  30. cbs->len -= n;
  31. return 1;
  32. }
  33. int CBS_skip(CBS *cbs, size_t len) {
  34. const uint8_t *dummy;
  35. return cbs_get(cbs, &dummy, len);
  36. }
  37. const uint8_t *CBS_data(const CBS *cbs) {
  38. return cbs->data;
  39. }
  40. size_t CBS_len(const CBS *cbs) {
  41. return cbs->len;
  42. }
  43. int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len) {
  44. OPENSSL_free(*out_ptr);
  45. *out_ptr = NULL;
  46. *out_len = 0;
  47. if (cbs->len == 0) {
  48. return 1;
  49. }
  50. *out_ptr = BUF_memdup(cbs->data, cbs->len);
  51. if (*out_ptr == NULL) {
  52. return 0;
  53. }
  54. *out_len = cbs->len;
  55. return 1;
  56. }
  57. int CBS_strdup(const CBS *cbs, char **out_ptr) {
  58. if (*out_ptr != NULL) {
  59. OPENSSL_free(*out_ptr);
  60. }
  61. *out_ptr = BUF_strndup((const char*)cbs->data, cbs->len);
  62. return (*out_ptr != NULL);
  63. }
  64. int CBS_contains_zero_byte(const CBS *cbs) {
  65. return memchr(cbs->data, 0, cbs->len) != NULL;
  66. }
  67. int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len) {
  68. if (len != cbs->len) {
  69. return 0;
  70. }
  71. return CRYPTO_memcmp(cbs->data, data, len) == 0;
  72. }
  73. static int cbs_get_u(CBS *cbs, uint32_t *out, size_t len) {
  74. uint32_t result = 0;
  75. const uint8_t *data;
  76. if (!cbs_get(cbs, &data, len)) {
  77. return 0;
  78. }
  79. for (size_t i = 0; i < len; i++) {
  80. result <<= 8;
  81. result |= data[i];
  82. }
  83. *out = result;
  84. return 1;
  85. }
  86. int CBS_get_u8(CBS *cbs, uint8_t *out) {
  87. const uint8_t *v;
  88. if (!cbs_get(cbs, &v, 1)) {
  89. return 0;
  90. }
  91. *out = *v;
  92. return 1;
  93. }
  94. int CBS_get_u16(CBS *cbs, uint16_t *out) {
  95. uint32_t v;
  96. if (!cbs_get_u(cbs, &v, 2)) {
  97. return 0;
  98. }
  99. *out = v;
  100. return 1;
  101. }
  102. int CBS_get_u24(CBS *cbs, uint32_t *out) {
  103. return cbs_get_u(cbs, out, 3);
  104. }
  105. int CBS_get_u32(CBS *cbs, uint32_t *out) {
  106. return cbs_get_u(cbs, out, 4);
  107. }
  108. int CBS_get_last_u8(CBS *cbs, uint8_t *out) {
  109. if (cbs->len == 0) {
  110. return 0;
  111. }
  112. *out = cbs->data[cbs->len - 1];
  113. cbs->len--;
  114. return 1;
  115. }
  116. int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) {
  117. const uint8_t *v;
  118. if (!cbs_get(cbs, &v, len)) {
  119. return 0;
  120. }
  121. CBS_init(out, v, len);
  122. return 1;
  123. }
  124. int CBS_copy_bytes(CBS *cbs, uint8_t *out, size_t len) {
  125. const uint8_t *v;
  126. if (!cbs_get(cbs, &v, len)) {
  127. return 0;
  128. }
  129. memcpy(out, v, len);
  130. return 1;
  131. }
  132. static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) {
  133. uint32_t len;
  134. if (!cbs_get_u(cbs, &len, len_len)) {
  135. return 0;
  136. }
  137. return CBS_get_bytes(cbs, out, len);
  138. }
  139. int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) {
  140. return cbs_get_length_prefixed(cbs, out, 1);
  141. }
  142. int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) {
  143. return cbs_get_length_prefixed(cbs, out, 2);
  144. }
  145. int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
  146. return cbs_get_length_prefixed(cbs, out, 3);
  147. }
  148. static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
  149. size_t *out_header_len, int ber_ok) {
  150. uint8_t tag, length_byte;
  151. CBS header = *cbs;
  152. CBS throwaway;
  153. if (out == NULL) {
  154. out = &throwaway;
  155. }
  156. if (!CBS_get_u8(&header, &tag) ||
  157. !CBS_get_u8(&header, &length_byte)) {
  158. return 0;
  159. }
  160. /* ITU-T X.690 section 8.1.2.3 specifies the format for identifiers with a tag
  161. * number no greater than 30.
  162. *
  163. * If the number portion is 31 (0x1f, the largest value that fits in the
  164. * allotted bits), then the tag is more than one byte long and the
  165. * continuation bytes contain the tag number. This parser only supports tag
  166. * numbers less than 31 (and thus single-byte tags). */
  167. if ((tag & 0x1f) == 0x1f) {
  168. return 0;
  169. }
  170. if (out_tag != NULL) {
  171. *out_tag = tag;
  172. }
  173. size_t len;
  174. /* The format for the length encoding is specified in ITU-T X.690 section
  175. * 8.1.3. */
  176. if ((length_byte & 0x80) == 0) {
  177. /* Short form length. */
  178. len = ((size_t) length_byte) + 2;
  179. if (out_header_len != NULL) {
  180. *out_header_len = 2;
  181. }
  182. } else {
  183. /* The high bit indicate that this is the long form, while the next 7 bits
  184. * encode the number of subsequent octets used to encode the length (ITU-T
  185. * X.690 clause 8.1.3.5.b). */
  186. const size_t num_bytes = length_byte & 0x7f;
  187. uint32_t len32;
  188. if (ber_ok && (tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) {
  189. /* indefinite length */
  190. if (out_header_len != NULL) {
  191. *out_header_len = 2;
  192. }
  193. return CBS_get_bytes(cbs, out, 2);
  194. }
  195. /* ITU-T X.690 clause 8.1.3.5.c specifies that the value 0xff shall not be
  196. * used as the first byte of the length. If this parser encounters that
  197. * value, num_bytes will be parsed as 127, which will fail the check below.
  198. */
  199. if (num_bytes == 0 || num_bytes > 4) {
  200. return 0;
  201. }
  202. if (!cbs_get_u(&header, &len32, num_bytes)) {
  203. return 0;
  204. }
  205. /* ITU-T X.690 section 10.1 (DER length forms) requires encoding the length
  206. * with the minimum number of octets. */
  207. if (len32 < 128) {
  208. /* Length should have used short-form encoding. */
  209. return 0;
  210. }
  211. if ((len32 >> ((num_bytes-1)*8)) == 0) {
  212. /* Length should have been at least one byte shorter. */
  213. return 0;
  214. }
  215. len = len32;
  216. if (len + 2 + num_bytes < len) {
  217. /* Overflow. */
  218. return 0;
  219. }
  220. len += 2 + num_bytes;
  221. if (out_header_len != NULL) {
  222. *out_header_len = 2 + num_bytes;
  223. }
  224. }
  225. return CBS_get_bytes(cbs, out, len);
  226. }
  227. int CBS_get_any_asn1(CBS *cbs, CBS *out, unsigned *out_tag) {
  228. size_t header_len;
  229. if (!CBS_get_any_asn1_element(cbs, out, out_tag, &header_len)) {
  230. return 0;
  231. }
  232. if (!CBS_skip(out, header_len)) {
  233. assert(0);
  234. return 0;
  235. }
  236. return 1;
  237. }
  238. int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
  239. size_t *out_header_len) {
  240. return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
  241. 0 /* DER only */);
  242. }
  243. int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
  244. size_t *out_header_len) {
  245. return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
  246. 1 /* BER allowed */);
  247. }
  248. static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
  249. int skip_header) {
  250. size_t header_len;
  251. unsigned tag;
  252. CBS throwaway;
  253. if (out == NULL) {
  254. out = &throwaway;
  255. }
  256. if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) ||
  257. tag != tag_value) {
  258. return 0;
  259. }
  260. if (skip_header && !CBS_skip(out, header_len)) {
  261. assert(0);
  262. return 0;
  263. }
  264. return 1;
  265. }
  266. int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) {
  267. return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
  268. }
  269. int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) {
  270. return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
  271. }
  272. int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) {
  273. if (CBS_len(cbs) < 1) {
  274. return 0;
  275. }
  276. return CBS_data(cbs)[0] == tag_value;
  277. }
  278. int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
  279. CBS bytes;
  280. const uint8_t *data;
  281. size_t i, len;
  282. if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) {
  283. return 0;
  284. }
  285. *out = 0;
  286. data = CBS_data(&bytes);
  287. len = CBS_len(&bytes);
  288. if (len == 0) {
  289. /* An INTEGER is encoded with at least one octet. */
  290. return 0;
  291. }
  292. if ((data[0] & 0x80) != 0) {
  293. /* Negative number. */
  294. return 0;
  295. }
  296. if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0) {
  297. /* Extra leading zeros. */
  298. return 0;
  299. }
  300. for (i = 0; i < len; i++) {
  301. if ((*out >> 56) != 0) {
  302. /* Too large to represent as a uint64_t. */
  303. return 0;
  304. }
  305. *out <<= 8;
  306. *out |= data[i];
  307. }
  308. return 1;
  309. }
  310. int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) {
  311. int present = 0;
  312. if (CBS_peek_asn1_tag(cbs, tag)) {
  313. if (!CBS_get_asn1(cbs, out, tag)) {
  314. return 0;
  315. }
  316. present = 1;
  317. }
  318. if (out_present != NULL) {
  319. *out_present = present;
  320. }
  321. return 1;
  322. }
  323. int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
  324. unsigned tag) {
  325. CBS child;
  326. int present;
  327. if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
  328. return 0;
  329. }
  330. if (present) {
  331. if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) ||
  332. CBS_len(&child) != 0) {
  333. return 0;
  334. }
  335. } else {
  336. CBS_init(out, NULL, 0);
  337. }
  338. if (out_present) {
  339. *out_present = present;
  340. }
  341. return 1;
  342. }
  343. int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag,
  344. uint64_t default_value) {
  345. CBS child;
  346. int present;
  347. if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
  348. return 0;
  349. }
  350. if (present) {
  351. if (!CBS_get_asn1_uint64(&child, out) ||
  352. CBS_len(&child) != 0) {
  353. return 0;
  354. }
  355. } else {
  356. *out = default_value;
  357. }
  358. return 1;
  359. }
  360. int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag,
  361. int default_value) {
  362. CBS child, child2;
  363. int present;
  364. if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
  365. return 0;
  366. }
  367. if (present) {
  368. uint8_t boolean;
  369. if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
  370. CBS_len(&child2) != 1 ||
  371. CBS_len(&child) != 0) {
  372. return 0;
  373. }
  374. boolean = CBS_data(&child2)[0];
  375. if (boolean == 0) {
  376. *out = 0;
  377. } else if (boolean == 0xff) {
  378. *out = 1;
  379. } else {
  380. return 0;
  381. }
  382. } else {
  383. *out = default_value;
  384. }
  385. return 1;
  386. }
  387. int CBS_is_valid_asn1_bitstring(const CBS *cbs) {
  388. CBS in = *cbs;
  389. uint8_t num_unused_bits;
  390. if (!CBS_get_u8(&in, &num_unused_bits) ||
  391. num_unused_bits > 7) {
  392. return 0;
  393. }
  394. if (num_unused_bits == 0) {
  395. return 1;
  396. }
  397. /* All num_unused_bits bits must exist and be zeros. */
  398. uint8_t last;
  399. if (!CBS_get_last_u8(&in, &last) ||
  400. (last & ((1 << num_unused_bits) - 1)) != 0) {
  401. return 0;
  402. }
  403. return 1;
  404. }
  405. int CBS_asn1_bitstring_has_bit(const CBS *cbs, unsigned bit) {
  406. if (!CBS_is_valid_asn1_bitstring(cbs)) {
  407. return 0;
  408. }
  409. const unsigned byte_num = (bit >> 3) + 1;
  410. const unsigned bit_num = 7 - (bit & 7);
  411. /* Unused bits are zero, and this function does not distinguish between
  412. * missing and unset bits. Thus it is sufficient to do a byte-level length
  413. * check. */
  414. return byte_num < CBS_len(cbs) &&
  415. (CBS_data(cbs)[byte_num] & (1 << bit_num)) != 0;
  416. }