Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. 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. size_t i;
  77. const uint8_t *data;
  78. if (!cbs_get(cbs, &data, len)) {
  79. return 0;
  80. }
  81. for (i = 0; i < len; i++) {
  82. result <<= 8;
  83. result |= data[i];
  84. }
  85. *out = result;
  86. return 1;
  87. }
  88. int CBS_get_u8(CBS *cbs, uint8_t *out) {
  89. const uint8_t *v;
  90. if (!cbs_get(cbs, &v, 1)) {
  91. return 0;
  92. }
  93. *out = *v;
  94. return 1;
  95. }
  96. int CBS_get_u16(CBS *cbs, uint16_t *out) {
  97. uint32_t v;
  98. if (!cbs_get_u(cbs, &v, 2)) {
  99. return 0;
  100. }
  101. *out = v;
  102. return 1;
  103. }
  104. int CBS_get_u24(CBS *cbs, uint32_t *out) {
  105. return cbs_get_u(cbs, out, 3);
  106. }
  107. int CBS_get_u32(CBS *cbs, uint32_t *out) {
  108. return cbs_get_u(cbs, out, 4);
  109. }
  110. int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) {
  111. const uint8_t *v;
  112. if (!cbs_get(cbs, &v, len)) {
  113. return 0;
  114. }
  115. CBS_init(out, v, len);
  116. return 1;
  117. }
  118. static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) {
  119. uint32_t len;
  120. if (!cbs_get_u(cbs, &len, len_len)) {
  121. return 0;
  122. }
  123. return CBS_get_bytes(cbs, out, len);
  124. }
  125. int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) {
  126. return cbs_get_length_prefixed(cbs, out, 1);
  127. }
  128. int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) {
  129. return cbs_get_length_prefixed(cbs, out, 2);
  130. }
  131. int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
  132. return cbs_get_length_prefixed(cbs, out, 3);
  133. }
  134. int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
  135. size_t *out_header_len) {
  136. uint8_t tag, length_byte;
  137. CBS header = *cbs;
  138. CBS throwaway;
  139. if (out == NULL) {
  140. out = &throwaway;
  141. }
  142. if (!CBS_get_u8(&header, &tag) ||
  143. !CBS_get_u8(&header, &length_byte)) {
  144. return 0;
  145. }
  146. if ((tag & 0x1f) == 0x1f) {
  147. /* Long form tags are not supported. */
  148. return 0;
  149. }
  150. if (out_tag != NULL) {
  151. *out_tag = tag;
  152. }
  153. size_t len;
  154. if ((length_byte & 0x80) == 0) {
  155. /* Short form length. */
  156. len = ((size_t) length_byte) + 2;
  157. if (out_header_len != NULL) {
  158. *out_header_len = 2;
  159. }
  160. } else {
  161. /* Long form length. */
  162. const size_t num_bytes = length_byte & 0x7f;
  163. uint32_t len32;
  164. if ((tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) {
  165. /* indefinite length */
  166. *out_header_len = 2;
  167. return CBS_get_bytes(cbs, out, 2);
  168. }
  169. if (num_bytes == 0 || num_bytes > 4) {
  170. return 0;
  171. }
  172. if (!cbs_get_u(&header, &len32, num_bytes)) {
  173. return 0;
  174. }
  175. if (len32 < 128) {
  176. /* Length should have used short-form encoding. */
  177. return 0;
  178. }
  179. if ((len32 >> ((num_bytes-1)*8)) == 0) {
  180. /* Length should have been at least one byte shorter. */
  181. return 0;
  182. }
  183. len = len32;
  184. if (len + 2 + num_bytes < len) {
  185. /* Overflow. */
  186. return 0;
  187. }
  188. len += 2 + num_bytes;
  189. if (out_header_len != NULL) {
  190. *out_header_len = 2 + num_bytes;
  191. }
  192. }
  193. return CBS_get_bytes(cbs, out, len);
  194. }
  195. static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
  196. int skip_header) {
  197. size_t header_len;
  198. unsigned tag;
  199. CBS throwaway;
  200. if (out == NULL) {
  201. out = &throwaway;
  202. }
  203. if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) ||
  204. tag != tag_value ||
  205. (header_len > 0 &&
  206. /* This ensures that the tag is either zero length or
  207. * indefinite-length. */
  208. CBS_len(out) == header_len &&
  209. CBS_data(out)[header_len - 1] == 0x80)) {
  210. return 0;
  211. }
  212. if (skip_header && !CBS_skip(out, header_len)) {
  213. assert(0);
  214. return 0;
  215. }
  216. return 1;
  217. }
  218. int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) {
  219. return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
  220. }
  221. int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) {
  222. return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
  223. }
  224. int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
  225. CBS bytes;
  226. const uint8_t *data;
  227. size_t i, len;
  228. if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) {
  229. return 0;
  230. }
  231. *out = 0;
  232. data = CBS_data(&bytes);
  233. len = CBS_len(&bytes);
  234. if (len > 0 && (data[0] & 0x80) != 0) {
  235. /* negative number */
  236. return 0;
  237. }
  238. for (i = 0; i < len; i++) {
  239. if ((*out >> 56) != 0) {
  240. /* Too large to represent as a uint64_t. */
  241. return 0;
  242. }
  243. *out <<= 8;
  244. *out |= data[i];
  245. }
  246. return 1;
  247. }