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.
 
 
 
 
 
 

395 lines
8.4 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. 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. if (*out_ptr != NULL) {
  45. OPENSSL_free(*out_ptr);
  46. *out_ptr = NULL;
  47. }
  48. *out_len = 0;
  49. if (cbs->len == 0) {
  50. return 1;
  51. }
  52. *out_ptr = BUF_memdup(cbs->data, cbs->len);
  53. if (*out_ptr == NULL) {
  54. return 0;
  55. }
  56. *out_len = cbs->len;
  57. return 1;
  58. }
  59. int CBS_strdup(const CBS *cbs, char **out_ptr) {
  60. if (*out_ptr != NULL) {
  61. OPENSSL_free(*out_ptr);
  62. }
  63. *out_ptr = BUF_strndup((const char*)cbs->data, cbs->len);
  64. return (*out_ptr != NULL);
  65. }
  66. int CBS_contains_zero_byte(const CBS *cbs) {
  67. return memchr(cbs->data, 0, cbs->len) != NULL;
  68. }
  69. int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len) {
  70. if (len != cbs->len) {
  71. return 0;
  72. }
  73. return CRYPTO_memcmp(cbs->data, data, len) == 0;
  74. }
  75. static int cbs_get_u(CBS *cbs, uint32_t *out, size_t len) {
  76. uint32_t result = 0;
  77. size_t i;
  78. const uint8_t *data;
  79. if (!cbs_get(cbs, &data, len)) {
  80. return 0;
  81. }
  82. for (i = 0; i < len; i++) {
  83. result <<= 8;
  84. result |= data[i];
  85. }
  86. *out = result;
  87. return 1;
  88. }
  89. int CBS_get_u8(CBS *cbs, uint8_t *out) {
  90. const uint8_t *v;
  91. if (!cbs_get(cbs, &v, 1)) {
  92. return 0;
  93. }
  94. *out = *v;
  95. return 1;
  96. }
  97. int CBS_get_u16(CBS *cbs, uint16_t *out) {
  98. uint32_t v;
  99. if (!cbs_get_u(cbs, &v, 2)) {
  100. return 0;
  101. }
  102. *out = v;
  103. return 1;
  104. }
  105. int CBS_get_u24(CBS *cbs, uint32_t *out) {
  106. return cbs_get_u(cbs, out, 3);
  107. }
  108. int CBS_get_u32(CBS *cbs, uint32_t *out) {
  109. return cbs_get_u(cbs, out, 4);
  110. }
  111. int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) {
  112. const uint8_t *v;
  113. if (!cbs_get(cbs, &v, len)) {
  114. return 0;
  115. }
  116. CBS_init(out, v, len);
  117. return 1;
  118. }
  119. static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) {
  120. uint32_t len;
  121. if (!cbs_get_u(cbs, &len, len_len)) {
  122. return 0;
  123. }
  124. return CBS_get_bytes(cbs, out, len);
  125. }
  126. int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) {
  127. return cbs_get_length_prefixed(cbs, out, 1);
  128. }
  129. int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) {
  130. return cbs_get_length_prefixed(cbs, out, 2);
  131. }
  132. int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
  133. return cbs_get_length_prefixed(cbs, out, 3);
  134. }
  135. int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
  136. size_t *out_header_len) {
  137. uint8_t tag, length_byte;
  138. CBS header = *cbs;
  139. CBS throwaway;
  140. if (out == NULL) {
  141. out = &throwaway;
  142. }
  143. if (!CBS_get_u8(&header, &tag) ||
  144. !CBS_get_u8(&header, &length_byte)) {
  145. return 0;
  146. }
  147. if ((tag & 0x1f) == 0x1f) {
  148. /* Long form tags are not supported. */
  149. return 0;
  150. }
  151. if (out_tag != NULL) {
  152. *out_tag = tag;
  153. }
  154. size_t len;
  155. if ((length_byte & 0x80) == 0) {
  156. /* Short form length. */
  157. len = ((size_t) length_byte) + 2;
  158. if (out_header_len != NULL) {
  159. *out_header_len = 2;
  160. }
  161. } else {
  162. /* Long form length. */
  163. const size_t num_bytes = length_byte & 0x7f;
  164. uint32_t len32;
  165. if ((tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) {
  166. /* indefinite length */
  167. *out_header_len = 2;
  168. return CBS_get_bytes(cbs, out, 2);
  169. }
  170. if (num_bytes == 0 || num_bytes > 4) {
  171. return 0;
  172. }
  173. if (!cbs_get_u(&header, &len32, num_bytes)) {
  174. return 0;
  175. }
  176. if (len32 < 128) {
  177. /* Length should have used short-form encoding. */
  178. return 0;
  179. }
  180. if ((len32 >> ((num_bytes-1)*8)) == 0) {
  181. /* Length should have been at least one byte shorter. */
  182. return 0;
  183. }
  184. len = len32;
  185. if (len + 2 + num_bytes < len) {
  186. /* Overflow. */
  187. return 0;
  188. }
  189. len += 2 + num_bytes;
  190. if (out_header_len != NULL) {
  191. *out_header_len = 2 + num_bytes;
  192. }
  193. }
  194. return CBS_get_bytes(cbs, out, len);
  195. }
  196. static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
  197. int skip_header) {
  198. size_t header_len;
  199. unsigned tag;
  200. CBS throwaway;
  201. if (out == NULL) {
  202. out = &throwaway;
  203. }
  204. if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) ||
  205. tag != tag_value ||
  206. (header_len > 0 &&
  207. /* This ensures that the tag is either zero length or
  208. * indefinite-length. */
  209. CBS_len(out) == header_len &&
  210. CBS_data(out)[header_len - 1] == 0x80)) {
  211. return 0;
  212. }
  213. if (skip_header && !CBS_skip(out, header_len)) {
  214. assert(0);
  215. return 0;
  216. }
  217. return 1;
  218. }
  219. int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) {
  220. return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
  221. }
  222. int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) {
  223. return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
  224. }
  225. int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) {
  226. if (CBS_len(cbs) < 1) {
  227. return 0;
  228. }
  229. return CBS_data(cbs)[0] == tag_value;
  230. }
  231. int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
  232. CBS bytes;
  233. const uint8_t *data;
  234. size_t i, len;
  235. if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) {
  236. return 0;
  237. }
  238. *out = 0;
  239. data = CBS_data(&bytes);
  240. len = CBS_len(&bytes);
  241. if (len == 0) {
  242. /* An INTEGER is encoded with at least one octet. */
  243. return 0;
  244. }
  245. if ((data[0] & 0x80) != 0) {
  246. /* Negative number. */
  247. return 0;
  248. }
  249. if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0) {
  250. /* Extra leading zeros. */
  251. return 0;
  252. }
  253. for (i = 0; i < len; i++) {
  254. if ((*out >> 56) != 0) {
  255. /* Too large to represent as a uint64_t. */
  256. return 0;
  257. }
  258. *out <<= 8;
  259. *out |= data[i];
  260. }
  261. return 1;
  262. }
  263. int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) {
  264. if (CBS_peek_asn1_tag(cbs, tag)) {
  265. if (!CBS_get_asn1(cbs, out, tag)) {
  266. return 0;
  267. }
  268. *out_present = 1;
  269. } else {
  270. *out_present = 0;
  271. }
  272. return 1;
  273. }
  274. int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
  275. unsigned tag) {
  276. CBS child;
  277. int present;
  278. if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
  279. return 0;
  280. }
  281. if (present) {
  282. if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) ||
  283. CBS_len(&child) != 0) {
  284. return 0;
  285. }
  286. } else {
  287. CBS_init(out, NULL, 0);
  288. }
  289. if (out_present) {
  290. *out_present = present;
  291. }
  292. return 1;
  293. }
  294. int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag,
  295. uint64_t default_value) {
  296. CBS child;
  297. int present;
  298. if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
  299. return 0;
  300. }
  301. if (present) {
  302. if (!CBS_get_asn1_uint64(&child, out) ||
  303. CBS_len(&child) != 0) {
  304. return 0;
  305. }
  306. } else {
  307. *out = default_value;
  308. }
  309. return 1;
  310. }
  311. int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag,
  312. int default_value) {
  313. CBS child, child2;
  314. int present;
  315. if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
  316. return 0;
  317. }
  318. if (present) {
  319. uint8_t boolean;
  320. if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
  321. CBS_len(&child2) != 1 ||
  322. CBS_len(&child) != 0) {
  323. return 0;
  324. }
  325. boolean = CBS_data(&child2)[0];
  326. if (boolean == 0) {
  327. *out = 0;
  328. } else if (boolean == 0xff) {
  329. *out = 1;
  330. } else {
  331. return 0;
  332. }
  333. } else {
  334. *out = default_value;
  335. }
  336. return 1;
  337. }