httpsrr.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "httpsrr.h"
  28. #include "connect.h"
  29. #include "curl_trc.h"
  30. #include "strdup.h"
  31. static CURLcode httpsrr_decode_alpn(const uint8_t *cp, size_t len,
  32. unsigned char *alpns)
  33. {
  34. /*
  35. * The wire-format value for "alpn" consists of at least one alpn-id
  36. * prefixed by its length as a single octet, and these length-value pairs
  37. * are concatenated to form the SvcParamValue. These pairs MUST exactly fill
  38. * the SvcParamValue; otherwise, the SvcParamValue is malformed.
  39. */
  40. int idnum = 0;
  41. while(len > 0) {
  42. size_t tlen = *cp++;
  43. enum alpnid id;
  44. len--;
  45. if(tlen > len)
  46. return CURLE_BAD_CONTENT_ENCODING;
  47. /* we only store ALPN ids we know about */
  48. id = Curl_alpn2alpnid(cp, tlen);
  49. if(id != ALPN_none) {
  50. if(idnum == MAX_HTTPSRR_ALPNS)
  51. break;
  52. if(idnum && memchr(alpns, id, idnum))
  53. /* this ALPN id is already stored */
  54. ;
  55. else
  56. alpns[idnum++] = (unsigned char)id;
  57. }
  58. cp += tlen;
  59. len -= tlen;
  60. }
  61. if(idnum < MAX_HTTPSRR_ALPNS)
  62. alpns[idnum] = ALPN_none; /* terminate the list */
  63. return CURLE_OK;
  64. }
  65. CURLcode Curl_httpsrr_set(struct Curl_easy *data,
  66. struct Curl_https_rrinfo *hi,
  67. uint16_t rrkey, const uint8_t *val, size_t vlen)
  68. {
  69. CURLcode result = CURLE_OK;
  70. switch(rrkey) {
  71. case HTTPS_RR_CODE_MANDATORY:
  72. CURL_TRC_DNS(data, "HTTPS RR MANDATORY left to implement");
  73. break;
  74. case HTTPS_RR_CODE_ALPN: /* str_list */
  75. result = httpsrr_decode_alpn(val, vlen, hi->alpns);
  76. CURL_TRC_DNS(data, "HTTPS RR ALPN: %u %u %u %u",
  77. hi->alpns[0], hi->alpns[1], hi->alpns[2], hi->alpns[3]);
  78. break;
  79. case HTTPS_RR_CODE_NO_DEF_ALPN:
  80. if(vlen) /* no data */
  81. return CURLE_BAD_FUNCTION_ARGUMENT;
  82. hi->no_def_alpn = TRUE;
  83. CURL_TRC_DNS(data, "HTTPS RR no-def-alpn");
  84. break;
  85. case HTTPS_RR_CODE_IPV4: /* addr4 list */
  86. if(!vlen || (vlen & 3)) /* the size must be 4-byte aligned */
  87. return CURLE_BAD_FUNCTION_ARGUMENT;
  88. curlx_free(hi->ipv4hints);
  89. hi->ipv4hints = Curl_memdup(val, vlen);
  90. if(!hi->ipv4hints)
  91. return CURLE_OUT_OF_MEMORY;
  92. hi->ipv4hints_len = vlen;
  93. CURL_TRC_DNS(data, "HTTPS RR IPv4");
  94. break;
  95. case HTTPS_RR_CODE_ECH:
  96. if(!vlen)
  97. return CURLE_BAD_FUNCTION_ARGUMENT;
  98. curlx_free(hi->echconfiglist);
  99. hi->echconfiglist = Curl_memdup(val, vlen);
  100. if(!hi->echconfiglist)
  101. return CURLE_OUT_OF_MEMORY;
  102. hi->echconfiglist_len = vlen;
  103. CURL_TRC_DNS(data, "HTTPS RR ECH");
  104. break;
  105. case HTTPS_RR_CODE_IPV6: /* addr6 list */
  106. if(!vlen || (vlen & 15)) /* the size must be 16-byte aligned */
  107. return CURLE_BAD_FUNCTION_ARGUMENT;
  108. curlx_free(hi->ipv6hints);
  109. hi->ipv6hints = Curl_memdup(val, vlen);
  110. if(!hi->ipv6hints)
  111. return CURLE_OUT_OF_MEMORY;
  112. hi->ipv6hints_len = vlen;
  113. CURL_TRC_DNS(data, "HTTPS RR IPv6");
  114. break;
  115. case HTTPS_RR_CODE_PORT:
  116. if(vlen != 2)
  117. return CURLE_BAD_FUNCTION_ARGUMENT;
  118. hi->port = (unsigned short)((val[0] << 8) | val[1]);
  119. CURL_TRC_DNS(data, "HTTPS RR port %u", hi->port);
  120. break;
  121. default:
  122. CURL_TRC_DNS(data, "HTTPS RR unknown code");
  123. break;
  124. }
  125. return result;
  126. }
  127. struct Curl_https_rrinfo *
  128. Curl_httpsrr_dup_move(struct Curl_https_rrinfo *rrinfo)
  129. {
  130. struct Curl_https_rrinfo *dup = Curl_memdup(rrinfo, sizeof(*rrinfo));
  131. if(dup)
  132. memset(rrinfo, 0, sizeof(*rrinfo));
  133. return dup;
  134. }
  135. void Curl_httpsrr_cleanup(struct Curl_https_rrinfo *rrinfo)
  136. {
  137. Curl_safefree(rrinfo->target);
  138. Curl_safefree(rrinfo->echconfiglist);
  139. Curl_safefree(rrinfo->ipv4hints);
  140. Curl_safefree(rrinfo->ipv6hints);
  141. Curl_safefree(rrinfo->rrname);
  142. }
  143. #ifdef USE_ARES
  144. static CURLcode httpsrr_opt(struct Curl_easy *data,
  145. const ares_dns_rr_t *rr,
  146. ares_dns_rr_key_t key, size_t idx,
  147. struct Curl_https_rrinfo *hinfo)
  148. {
  149. const unsigned char *val = NULL;
  150. unsigned short code;
  151. size_t len = 0;
  152. code = ares_dns_rr_get_opt(rr, key, idx, &val, &len);
  153. return Curl_httpsrr_set(data, hinfo, code, val, len);
  154. }
  155. CURLcode Curl_httpsrr_from_ares(struct Curl_easy *data,
  156. const ares_dns_record_t *dnsrec,
  157. struct Curl_https_rrinfo *hinfo)
  158. {
  159. CURLcode result = CURLE_OK;
  160. size_t i;
  161. for(i = 0; i < ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); i++) {
  162. const char *target;
  163. size_t opt;
  164. const ares_dns_rr_t *rr =
  165. ares_dns_record_rr_get_const(dnsrec, ARES_SECTION_ANSWER, i);
  166. if(ares_dns_rr_get_type(rr) != ARES_REC_TYPE_HTTPS)
  167. continue;
  168. /* When SvcPriority is 0, the SVCB record is in AliasMode. Otherwise, it
  169. is in ServiceMode */
  170. target = ares_dns_rr_get_str(rr, ARES_RR_HTTPS_TARGET);
  171. if(target && target[0]) {
  172. curlx_free(hinfo->target);
  173. hinfo->target = curlx_strdup(target);
  174. if(!hinfo->target) {
  175. result = CURLE_OUT_OF_MEMORY;
  176. goto out;
  177. }
  178. CURL_TRC_DNS(data, "HTTPS RR target: %s", hinfo->target);
  179. }
  180. CURL_TRC_DNS(data, "HTTPS RR priority: %u",
  181. ares_dns_rr_get_u16(rr, ARES_RR_HTTPS_PRIORITY));
  182. for(opt = 0; opt < ares_dns_rr_get_opt_cnt(rr, ARES_RR_HTTPS_PARAMS);
  183. opt++) {
  184. result = httpsrr_opt(data, rr, ARES_RR_HTTPS_PARAMS, opt, hinfo);
  185. if(result)
  186. break;
  187. }
  188. }
  189. out:
  190. Curl_safefree(hinfo->rrname);
  191. return result;
  192. }
  193. #endif /* USE_ARES */
  194. #endif /* USE_HTTPSRR */