You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

488 lines
11 KiB

  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. 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. if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) {
  281. return 0;
  282. }
  283. *out = 0;
  284. const uint8_t *data = CBS_data(&bytes);
  285. size_t len = CBS_len(&bytes);
  286. if (len == 0) {
  287. // An INTEGER is encoded with at least one octet.
  288. return 0;
  289. }
  290. if ((data[0] & 0x80) != 0) {
  291. // Negative number.
  292. return 0;
  293. }
  294. if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0) {
  295. // Extra leading zeros.
  296. return 0;
  297. }
  298. for (size_t i = 0; i < len; i++) {
  299. if ((*out >> 56) != 0) {
  300. // Too large to represent as a uint64_t.
  301. return 0;
  302. }
  303. *out <<= 8;
  304. *out |= data[i];
  305. }
  306. return 1;
  307. }
  308. int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) {
  309. int present = 0;
  310. if (CBS_peek_asn1_tag(cbs, tag)) {
  311. if (!CBS_get_asn1(cbs, out, tag)) {
  312. return 0;
  313. }
  314. present = 1;
  315. }
  316. if (out_present != NULL) {
  317. *out_present = present;
  318. }
  319. return 1;
  320. }
  321. int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
  322. unsigned tag) {
  323. CBS child;
  324. int present;
  325. if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
  326. return 0;
  327. }
  328. if (present) {
  329. if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) ||
  330. CBS_len(&child) != 0) {
  331. return 0;
  332. }
  333. } else {
  334. CBS_init(out, NULL, 0);
  335. }
  336. if (out_present) {
  337. *out_present = present;
  338. }
  339. return 1;
  340. }
  341. int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag,
  342. uint64_t default_value) {
  343. CBS child;
  344. int present;
  345. if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
  346. return 0;
  347. }
  348. if (present) {
  349. if (!CBS_get_asn1_uint64(&child, out) ||
  350. CBS_len(&child) != 0) {
  351. return 0;
  352. }
  353. } else {
  354. *out = default_value;
  355. }
  356. return 1;
  357. }
  358. int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag,
  359. int default_value) {
  360. CBS child, child2;
  361. int present;
  362. if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
  363. return 0;
  364. }
  365. if (present) {
  366. uint8_t boolean;
  367. if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
  368. CBS_len(&child2) != 1 ||
  369. CBS_len(&child) != 0) {
  370. return 0;
  371. }
  372. boolean = CBS_data(&child2)[0];
  373. if (boolean == 0) {
  374. *out = 0;
  375. } else if (boolean == 0xff) {
  376. *out = 1;
  377. } else {
  378. return 0;
  379. }
  380. } else {
  381. *out = default_value;
  382. }
  383. return 1;
  384. }
  385. int CBS_is_valid_asn1_bitstring(const CBS *cbs) {
  386. CBS in = *cbs;
  387. uint8_t num_unused_bits;
  388. if (!CBS_get_u8(&in, &num_unused_bits) ||
  389. num_unused_bits > 7) {
  390. return 0;
  391. }
  392. if (num_unused_bits == 0) {
  393. return 1;
  394. }
  395. // All num_unused_bits bits must exist and be zeros.
  396. uint8_t last;
  397. if (!CBS_get_last_u8(&in, &last) ||
  398. (last & ((1 << num_unused_bits) - 1)) != 0) {
  399. return 0;
  400. }
  401. return 1;
  402. }
  403. int CBS_asn1_bitstring_has_bit(const CBS *cbs, unsigned bit) {
  404. if (!CBS_is_valid_asn1_bitstring(cbs)) {
  405. return 0;
  406. }
  407. const unsigned byte_num = (bit >> 3) + 1;
  408. const unsigned bit_num = 7 - (bit & 7);
  409. // Unused bits are zero, and this function does not distinguish between
  410. // missing and unset bits. Thus it is sufficient to do a byte-level length
  411. // check.
  412. return byte_num < CBS_len(cbs) &&
  413. (CBS_data(cbs)[byte_num] & (1 << bit_num)) != 0;
  414. }