1
0

httpsrr.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef USE_HTTPSRR
  26. #include "urldata.h"
  27. #include "curl_addrinfo.h"
  28. #include "httpsrr.h"
  29. #include "connect.h"
  30. #include "sendf.h"
  31. #include "strdup.h"
  32. /* The last 2 #include files should be in this order */
  33. #include "curl_memory.h"
  34. #include "memdebug.h"
  35. static CURLcode httpsrr_decode_alpn(const char *cp, size_t len,
  36. unsigned char *alpns)
  37. {
  38. /*
  39. * The wire-format value for "alpn" consists of at least one alpn-id
  40. * prefixed by its length as a single octet, and these length-value pairs
  41. * are concatenated to form the SvcParamValue. These pairs MUST exactly fill
  42. * the SvcParamValue; otherwise, the SvcParamValue is malformed.
  43. */
  44. int idnum = 0;
  45. while(len > 0) {
  46. size_t tlen = (size_t) *cp++;
  47. enum alpnid id;
  48. len--;
  49. if(tlen > len)
  50. return CURLE_BAD_CONTENT_ENCODING;
  51. /* we only store ALPN ids we know about */
  52. id = Curl_alpn2alpnid(cp, tlen);
  53. if(id != ALPN_none) {
  54. if(idnum == MAX_HTTPSRR_ALPNS)
  55. break;
  56. if(idnum && memchr(alpns, id, idnum))
  57. /* this ALPN id is already stored */
  58. ;
  59. else
  60. alpns[idnum++] = (unsigned char)id;
  61. }
  62. cp += tlen;
  63. len -= tlen;
  64. }
  65. if(idnum < MAX_HTTPSRR_ALPNS)
  66. alpns[idnum] = ALPN_none; /* terminate the list */
  67. return CURLE_OK;
  68. }
  69. CURLcode Curl_httpsrr_set(struct Curl_easy *data,
  70. struct Curl_https_rrinfo *hi,
  71. uint16_t rrkey, const uint8_t *val, size_t vlen)
  72. {
  73. CURLcode result = CURLE_OK;
  74. switch(rrkey) {
  75. case HTTPS_RR_CODE_MANDATORY:
  76. CURL_TRC_DNS(data, "HTTPS RR MANDATORY left to implement");
  77. break;
  78. case HTTPS_RR_CODE_ALPN: /* str_list */
  79. result = httpsrr_decode_alpn((const char *)val, vlen, hi->alpns);
  80. CURL_TRC_DNS(data, "HTTPS RR ALPN: %u %u %u %u",
  81. hi->alpns[0], hi->alpns[1], hi->alpns[2], hi->alpns[3]);
  82. break;
  83. case HTTPS_RR_CODE_NO_DEF_ALPN:
  84. if(vlen) /* no data */
  85. return CURLE_BAD_FUNCTION_ARGUMENT;
  86. hi->no_def_alpn = TRUE;
  87. CURL_TRC_DNS(data, "HTTPS RR no-def-alpn");
  88. break;
  89. case HTTPS_RR_CODE_IPV4: /* addr4 list */
  90. if(!vlen || (vlen & 3)) /* the size must be 4-byte aligned */
  91. return CURLE_BAD_FUNCTION_ARGUMENT;
  92. free(hi->ipv4hints);
  93. hi->ipv4hints = Curl_memdup(val, vlen);
  94. if(!hi->ipv4hints)
  95. return CURLE_OUT_OF_MEMORY;
  96. hi->ipv4hints_len = vlen;
  97. CURL_TRC_DNS(data, "HTTPS RR IPv4");
  98. break;
  99. case HTTPS_RR_CODE_ECH:
  100. if(!vlen)
  101. return CURLE_BAD_FUNCTION_ARGUMENT;
  102. free(hi->echconfiglist);
  103. hi->echconfiglist = Curl_memdup(val, vlen);
  104. if(!hi->echconfiglist)
  105. return CURLE_OUT_OF_MEMORY;
  106. hi->echconfiglist_len = vlen;
  107. CURL_TRC_DNS(data, "HTTPS RR ECH");
  108. break;
  109. case HTTPS_RR_CODE_IPV6: /* addr6 list */
  110. if(!vlen || (vlen & 15)) /* the size must be 16-byte aligned */
  111. return CURLE_BAD_FUNCTION_ARGUMENT;
  112. free(hi->ipv6hints);
  113. hi->ipv6hints = Curl_memdup(val, vlen);
  114. if(!hi->ipv6hints)
  115. return CURLE_OUT_OF_MEMORY;
  116. hi->ipv6hints_len = vlen;
  117. CURL_TRC_DNS(data, "HTTPS RR IPv6");
  118. break;
  119. case HTTPS_RR_CODE_PORT:
  120. if(vlen != 2)
  121. return CURLE_BAD_FUNCTION_ARGUMENT;
  122. hi->port = (unsigned short)((val[0] << 8) | val[1]);
  123. CURL_TRC_DNS(data, "HTTPS RR port %u", hi->port);
  124. break;
  125. default:
  126. CURL_TRC_DNS(data, "HTTPS RR unknown code");
  127. break;
  128. }
  129. return result;
  130. }
  131. struct Curl_https_rrinfo *
  132. Curl_httpsrr_dup_move(struct Curl_https_rrinfo *rrinfo)
  133. {
  134. struct Curl_https_rrinfo *dup = Curl_memdup(rrinfo, sizeof(*rrinfo));
  135. if(dup)
  136. memset(rrinfo, 0, sizeof(*rrinfo));
  137. return dup;
  138. }
  139. void Curl_httpsrr_cleanup(struct Curl_https_rrinfo *rrinfo)
  140. {
  141. Curl_safefree(rrinfo->target);
  142. Curl_safefree(rrinfo->echconfiglist);
  143. Curl_safefree(rrinfo->ipv4hints);
  144. Curl_safefree(rrinfo->ipv6hints);
  145. Curl_safefree(rrinfo->rrname);
  146. }
  147. #ifdef USE_ARES
  148. static CURLcode httpsrr_opt(struct Curl_easy *data,
  149. const ares_dns_rr_t *rr,
  150. ares_dns_rr_key_t key, size_t idx,
  151. struct Curl_https_rrinfo *hinfo)
  152. {
  153. const unsigned char *val = NULL;
  154. unsigned short code;
  155. size_t len = 0;
  156. code = ares_dns_rr_get_opt(rr, key, idx, &val, &len);
  157. return Curl_httpsrr_set(data, hinfo, code, val, len);
  158. }
  159. CURLcode Curl_httpsrr_from_ares(struct Curl_easy *data,
  160. const ares_dns_record_t *dnsrec,
  161. struct Curl_https_rrinfo *hinfo)
  162. {
  163. CURLcode result = CURLE_OK;
  164. size_t i;
  165. for(i = 0; i < ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); i++) {
  166. const char *target;
  167. size_t opt;
  168. const ares_dns_rr_t *rr =
  169. ares_dns_record_rr_get_const(dnsrec, ARES_SECTION_ANSWER, i);
  170. if(ares_dns_rr_get_type(rr) != ARES_REC_TYPE_HTTPS)
  171. continue;
  172. /* When SvcPriority is 0, the SVCB record is in AliasMode. Otherwise, it
  173. is in ServiceMode */
  174. target = ares_dns_rr_get_str(rr, ARES_RR_HTTPS_TARGET);
  175. if(target && target[0]) {
  176. free(hinfo->target);
  177. hinfo->target = strdup(target);
  178. if(!hinfo->target) {
  179. result = CURLE_OUT_OF_MEMORY;
  180. goto out;
  181. }
  182. CURL_TRC_DNS(data, "HTTPS RR target: %s", hinfo->target);
  183. }
  184. CURL_TRC_DNS(data, "HTTPS RR priority: %u",
  185. ares_dns_rr_get_u16(rr, ARES_RR_HTTPS_PRIORITY));
  186. for(opt = 0; opt < ares_dns_rr_get_opt_cnt(rr, ARES_RR_HTTPS_PARAMS);
  187. opt++) {
  188. result = httpsrr_opt(data, rr, ARES_RR_HTTPS_PARAMS, opt, hinfo);
  189. if(result)
  190. break;
  191. }
  192. }
  193. out:
  194. Curl_safefree(hinfo->rrname);
  195. return result;
  196. }
  197. #endif /* USE_ARES */
  198. #endif /* USE_HTTPSRR */