cbs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. #include "../internal.h"
  21. void CBS_init(CBS *cbs, const uint8_t *data, size_t len) {
  22. cbs->data = data;
  23. cbs->len = len;
  24. }
  25. static int cbs_get(CBS *cbs, const uint8_t **p, size_t n) {
  26. if (cbs->len < n) {
  27. return 0;
  28. }
  29. *p = cbs->data;
  30. cbs->data += n;
  31. cbs->len -= n;
  32. return 1;
  33. }
  34. int CBS_skip(CBS *cbs, size_t len) {
  35. const uint8_t *dummy;
  36. return cbs_get(cbs, &dummy, len);
  37. }
  38. const uint8_t *CBS_data(const CBS *cbs) {
  39. return cbs->data;
  40. }
  41. size_t CBS_len(const CBS *cbs) {
  42. return cbs->len;
  43. }
  44. int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len) {
  45. OPENSSL_free(*out_ptr);
  46. *out_ptr = NULL;
  47. *out_len = 0;
  48. if (cbs->len == 0) {
  49. return 1;
  50. }
  51. *out_ptr = BUF_memdup(cbs->data, cbs->len);
  52. if (*out_ptr == NULL) {
  53. return 0;
  54. }
  55. *out_len = cbs->len;
  56. return 1;
  57. }
  58. int CBS_strdup(const CBS *cbs, char **out_ptr) {
  59. if (*out_ptr != NULL) {
  60. OPENSSL_free(*out_ptr);
  61. }
  62. *out_ptr = BUF_strndup((const char*)cbs->data, cbs->len);
  63. return (*out_ptr != NULL);
  64. }
  65. int CBS_contains_zero_byte(const CBS *cbs) {
  66. return OPENSSL_memchr(cbs->data, 0, cbs->len) != NULL;
  67. }
  68. int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len) {
  69. if (len != cbs->len) {
  70. return 0;
  71. }
  72. return CRYPTO_memcmp(cbs->data, data, len) == 0;
  73. }
  74. static int cbs_get_u(CBS *cbs, uint32_t *out, size_t len) {
  75. uint32_t result = 0;
  76. const uint8_t *data;
  77. if (!cbs_get(cbs, &data, len)) {
  78. return 0;
  79. }
  80. for (size_t i = 0; i < len; i++) {
  81. result <<= 8;
  82. result |= data[i];
  83. }
  84. *out = result;
  85. return 1;
  86. }
  87. int CBS_get_u8(CBS *cbs, uint8_t *out) {
  88. const uint8_t *v;
  89. if (!cbs_get(cbs, &v, 1)) {
  90. return 0;
  91. }
  92. *out = *v;
  93. return 1;
  94. }
  95. int CBS_get_u16(CBS *cbs, uint16_t *out) {
  96. uint32_t v;
  97. if (!cbs_get_u(cbs, &v, 2)) {
  98. return 0;
  99. }
  100. *out = v;
  101. return 1;
  102. }
  103. int CBS_get_u24(CBS *cbs, uint32_t *out) {
  104. return cbs_get_u(cbs, out, 3);
  105. }
  106. int CBS_get_u32(CBS *cbs, uint32_t *out) {
  107. return cbs_get_u(cbs, out, 4);
  108. }
  109. int CBS_get_last_u8(CBS *cbs, uint8_t *out) {
  110. if (cbs->len == 0) {
  111. return 0;
  112. }
  113. *out = cbs->data[cbs->len - 1];
  114. cbs->len--;
  115. return 1;
  116. }
  117. int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) {
  118. const uint8_t *v;
  119. if (!cbs_get(cbs, &v, len)) {
  120. return 0;
  121. }
  122. CBS_init(out, v, len);
  123. return 1;
  124. }
  125. int CBS_copy_bytes(CBS *cbs, uint8_t *out, size_t len) {
  126. const uint8_t *v;
  127. if (!cbs_get(cbs, &v, len)) {
  128. return 0;
  129. }
  130. OPENSSL_memcpy(out, v, len);
  131. return 1;
  132. }
  133. static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) {
  134. uint32_t len;
  135. if (!cbs_get_u(cbs, &len, len_len)) {
  136. return 0;
  137. }
  138. return CBS_get_bytes(cbs, out, len);
  139. }
  140. int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) {
  141. return cbs_get_length_prefixed(cbs, out, 1);
  142. }
  143. int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) {
  144. return cbs_get_length_prefixed(cbs, out, 2);
  145. }
  146. int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
  147. return cbs_get_length_prefixed(cbs, out, 3);
  148. }
  149. static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
  150. size_t *out_header_len, int ber_ok) {
  151. uint8_t tag, length_byte;
  152. CBS header = *cbs;
  153. CBS throwaway;
  154. if (out == NULL) {
  155. out = &throwaway;
  156. }
  157. if (!CBS_get_u8(&header, &tag) ||
  158. !CBS_get_u8(&header, &length_byte)) {
  159. return 0;
  160. }
  161. /* ITU-T X.690 section 8.1.2.3 specifies the format for identifiers with a tag
  162. * number no greater than 30.
  163. *
  164. * If the number portion is 31 (0x1f, the largest value that fits in the
  165. * allotted bits), then the tag is more than one byte long and the
  166. * continuation bytes contain the tag number. This parser only supports tag
  167. * numbers less than 31 (and thus single-byte tags). */
  168. if ((tag & 0x1f) == 0x1f) {
  169. return 0;
  170. }
  171. if (out_tag != NULL) {
  172. *out_tag = tag;
  173. }
  174. size_t len;
  175. /* The format for the length encoding is specified in ITU-T X.690 section
  176. * 8.1.3. */
  177. if ((length_byte & 0x80) == 0) {
  178. /* Short form length. */
  179. len = ((size_t) length_byte) + 2;
  180. if (out_header_len != NULL) {
  181. *out_header_len = 2;
  182. }
  183. } else {
  184. /* The high bit indicate that this is the long form, while the next 7 bits
  185. * encode the number of subsequent octets used to encode the length (ITU-T
  186. * X.690 clause 8.1.3.5.b). */
  187. const size_t num_bytes = length_byte & 0x7f;
  188. uint32_t len32;
  189. if (ber_ok && (tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) {
  190. /* indefinite length */
  191. if (out_header_len != NULL) {
  192. *out_header_len = 2;
  193. }
  194. return CBS_get_bytes(cbs, out, 2);
  195. }
  196. /* ITU-T X.690 clause 8.1.3.5.c specifies that the value 0xff shall not be
  197. * used as the first byte of the length. If this parser encounters that
  198. * value, num_bytes will be parsed as 127, which will fail the check below.
  199. */
  200. if (num_bytes == 0 || num_bytes > 4) {
  201. return 0;
  202. }
  203. if (!cbs_get_u(&header, &len32, num_bytes)) {
  204. return 0;
  205. }
  206. /* ITU-T X.690 section 10.1 (DER length forms) requires encoding the length
  207. * with the minimum number of octets. */
  208. if (len32 < 128) {
  209. /* Length should have used short-form encoding. */
  210. return 0;
  211. }
  212. if ((len32 >> ((num_bytes-1)*8)) == 0) {
  213. /* Length should have been at least one byte shorter. */
  214. return 0;
  215. }
  216. len = len32;
  217. if (len + 2 + num_bytes < len) {
  218. /* Overflow. */
  219. return 0;
  220. }
  221. len += 2 + num_bytes;
  222. if (out_header_len != NULL) {
  223. *out_header_len = 2 + num_bytes;
  224. }
  225. }
  226. return CBS_get_bytes(cbs, out, len);
  227. }
  228. int CBS_get_any_asn1(CBS *cbs, CBS *out, unsigned *out_tag) {
  229. size_t header_len;
  230. if (!CBS_get_any_asn1_element(cbs, out, out_tag, &header_len)) {
  231. return 0;
  232. }
  233. if (!CBS_skip(out, header_len)) {
  234. assert(0);
  235. return 0;
  236. }
  237. return 1;
  238. }
  239. int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
  240. size_t *out_header_len) {
  241. return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
  242. 0 /* DER only */);
  243. }
  244. int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
  245. size_t *out_header_len) {
  246. return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
  247. 1 /* BER allowed */);
  248. }
  249. static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
  250. int skip_header) {
  251. size_t header_len;
  252. unsigned tag;
  253. CBS throwaway;
  254. if (out == NULL) {
  255. out = &throwaway;
  256. }
  257. if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) ||
  258. tag != tag_value) {
  259. return 0;
  260. }
  261. if (skip_header && !CBS_skip(out, header_len)) {
  262. assert(0);
  263. return 0;
  264. }
  265. return 1;
  266. }
  267. int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) {
  268. return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
  269. }
  270. int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) {
  271. return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
  272. }
  273. int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) {
  274. if (CBS_len(cbs) < 1) {
  275. return 0;
  276. }
  277. return CBS_data(cbs)[0] == tag_value;
  278. }
  279. int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
  280. CBS bytes;
  281. const uint8_t *data;
  282. size_t i, len;
  283. if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) {
  284. return 0;
  285. }
  286. *out = 0;
  287. data = CBS_data(&bytes);
  288. len = CBS_len(&bytes);
  289. if (len == 0) {
  290. /* An INTEGER is encoded with at least one octet. */
  291. return 0;
  292. }
  293. if ((data[0] & 0x80) != 0) {
  294. /* Negative number. */
  295. return 0;
  296. }
  297. if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0) {
  298. /* Extra leading zeros. */
  299. return 0;
  300. }
  301. for (i = 0; i < len; i++) {
  302. if ((*out >> 56) != 0) {
  303. /* Too large to represent as a uint64_t. */
  304. return 0;
  305. }
  306. *out <<= 8;
  307. *out |= data[i];
  308. }
  309. return 1;
  310. }
  311. int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) {
  312. int present = 0;
  313. if (CBS_peek_asn1_tag(cbs, tag)) {
  314. if (!CBS_get_asn1(cbs, out, tag)) {
  315. return 0;
  316. }
  317. present = 1;
  318. }
  319. if (out_present != NULL) {
  320. *out_present = present;
  321. }
  322. return 1;
  323. }
  324. int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
  325. unsigned tag) {
  326. CBS child;
  327. int present;
  328. if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
  329. return 0;
  330. }
  331. if (present) {
  332. if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) ||
  333. CBS_len(&child) != 0) {
  334. return 0;
  335. }
  336. } else {
  337. CBS_init(out, NULL, 0);
  338. }
  339. if (out_present) {
  340. *out_present = present;
  341. }
  342. return 1;
  343. }
  344. int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag,
  345. uint64_t default_value) {
  346. CBS child;
  347. int present;
  348. if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
  349. return 0;
  350. }
  351. if (present) {
  352. if (!CBS_get_asn1_uint64(&child, out) ||
  353. CBS_len(&child) != 0) {
  354. return 0;
  355. }
  356. } else {
  357. *out = default_value;
  358. }
  359. return 1;
  360. }
  361. int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag,
  362. int default_value) {
  363. CBS child, child2;
  364. int present;
  365. if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
  366. return 0;
  367. }
  368. if (present) {
  369. uint8_t boolean;
  370. if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
  371. CBS_len(&child2) != 1 ||
  372. CBS_len(&child) != 0) {
  373. return 0;
  374. }
  375. boolean = CBS_data(&child2)[0];
  376. if (boolean == 0) {
  377. *out = 0;
  378. } else if (boolean == 0xff) {
  379. *out = 1;
  380. } else {
  381. return 0;
  382. }
  383. } else {
  384. *out = default_value;
  385. }
  386. return 1;
  387. }
  388. int CBS_is_valid_asn1_bitstring(const CBS *cbs) {
  389. CBS in = *cbs;
  390. uint8_t num_unused_bits;
  391. if (!CBS_get_u8(&in, &num_unused_bits) ||
  392. num_unused_bits > 7) {
  393. return 0;
  394. }
  395. if (num_unused_bits == 0) {
  396. return 1;
  397. }
  398. /* All num_unused_bits bits must exist and be zeros. */
  399. uint8_t last;
  400. if (!CBS_get_last_u8(&in, &last) ||
  401. (last & ((1 << num_unused_bits) - 1)) != 0) {
  402. return 0;
  403. }
  404. return 1;
  405. }
  406. int CBS_asn1_bitstring_has_bit(const CBS *cbs, unsigned bit) {
  407. if (!CBS_is_valid_asn1_bitstring(cbs)) {
  408. return 0;
  409. }
  410. const unsigned byte_num = (bit >> 3) + 1;
  411. const unsigned bit_num = 7 - (bit & 7);
  412. /* Unused bits are zero, and this function does not distinguish between
  413. * missing and unset bits. Thus it is sufficient to do a byte-level length
  414. * check. */
  415. return byte_num < CBS_len(cbs) &&
  416. (CBS_data(cbs)[byte_num] & (1 << bit_num)) != 0;
  417. }