Coverage Report

Created: 2026-10-15 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/vdns/httpsrr.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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
26
#ifdef USE_HTTPSRR
27
28
#include "urldata.h"
29
#include "connect.h"
30
#include "curl_trc.h"
31
#include "vdns/httpsrr.h"
32
#include "curlx/strdup.h"
33
#include "curlx/inet_ntop.h"
34
35
static CURLcode httpsrr_decode_alpn(const uint8_t *cp, size_t len,
36
                                    unsigned char *alpns)
37
0
{
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
0
  int idnum = 0;
45
46
0
  while(len > 0) {
47
0
    size_t tlen = *cp++;
48
0
    enum alpnid id;
49
0
    len--;
50
0
    if(tlen > len)
51
0
      return CURLE_BAD_CONTENT_ENCODING;
52
53
    /* we only store ALPN ids we know about */
54
0
    id = Curl_alpn2alpnid(cp, tlen);
55
0
    if(id != ALPN_none) {
56
0
      if(idnum == MAX_HTTPSRR_ALPNS)
57
0
        break;
58
0
      if(idnum && memchr(alpns, id, idnum))
59
        /* this ALPN id is already stored */
60
0
        ;
61
0
      else
62
0
        alpns[idnum++] = (unsigned char)id;
63
0
    }
64
0
    cp += tlen;
65
0
    len -= tlen;
66
0
  }
67
0
  if(idnum < MAX_HTTPSRR_ALPNS)
68
0
    alpns[idnum] = ALPN_none; /* terminate the list */
69
0
  return CURLE_OK;
70
0
}
71
72
#ifdef CURLVERBOSE
73
74
static CURLcode httpsrr_print_addr(struct dynbuf *dyn,
75
                                   int ai_family,
76
                                   const uint8_t *addr,
77
                                   size_t total_len)
78
0
{
79
0
  char buf[MAX_IPADR_LEN];
80
0
  size_t i, alen = (ai_family == AF_INET6) ? 16 : 4;
81
0
  const char *sep = "";
82
0
  CURLcode result = CURLE_OK;
83
84
0
  for(i = 0; (i < (total_len / alen)) && !result; ++i) {
85
0
    if(curlx_inet_ntop(ai_family, addr + (i * alen), buf, sizeof(buf)))
86
0
      result = curlx_dyn_add(dyn, "<error parsing address>");
87
0
    else
88
0
      result = curlx_dyn_addf(dyn, "%s%s", sep, buf);
89
0
    sep = ",";
90
0
  }
91
0
  return result;
92
0
}
93
94
void Curl_httpsrr_trace(struct Curl_easy *data,
95
                        struct Curl_https_rrinfo *rr)
96
0
{
97
0
  struct dynbuf tmp;
98
0
  CURLcode result;
99
100
0
  if(!rr) {
101
0
    CURL_TRC_DNS(data, "[HTTPS] no record available");
102
0
    return;
103
0
  }
104
0
  curlx_dyn_init(&tmp, 1024);
105
0
  result = Curl_httpsrr_print(&tmp, rr);
106
0
  if(!result)
107
0
    CURL_TRC_DNS(data, "[HTTPS] record: %s", curlx_dyn_ptr(&tmp));
108
0
  else
109
0
    CURL_TRC_DNS(data, "[HTTPS] error printing information");
110
0
  curlx_dyn_free(&tmp);
111
0
}
112
113
CURLcode Curl_httpsrr_print(struct dynbuf *tmp,
114
                            struct Curl_https_rrinfo *rr)
115
0
{
116
0
  CURLcode result;
117
0
  int i;
118
119
0
  curlx_dyn_reset(tmp);
120
0
  result = curlx_dyn_addf(tmp, "%u %s", rr->priority,
121
0
                          rr->target ? rr->target : ".");
122
0
  if(!result && rr->mandatory)
123
0
    result = curlx_dyn_add(tmp, " mandatory-keys(ignored)");
124
0
  if(!result && rr->alpns[0]) {
125
0
    const char *sep = "", *name;
126
0
    result = curlx_dyn_add(tmp, " alpn=");
127
0
    for(i = 0; !result && (i < 4); ++i) {
128
0
      switch(rr->alpns[i]) {
129
0
      case ALPN_h1:
130
0
        name = "http/1.1";
131
0
        break;
132
0
      case ALPN_h2:
133
0
        name = "h2";
134
0
        break;
135
0
      case ALPN_h3:
136
0
        name = "h3";
137
0
        break;
138
0
      default:
139
0
        name = NULL;
140
0
      }
141
0
      if(name) {
142
0
        result = curlx_dyn_addf(tmp, "%s%s", sep, name);
143
0
        sep = ",";
144
0
      }
145
0
    }
146
0
  }
147
0
  if(!result && rr->port_set) {
148
0
    result = curlx_dyn_addf(tmp, " port=%u", rr->port);
149
0
  }
150
0
  if(!result && rr->no_def_alpn)
151
0
    result = curlx_dyn_add(tmp, " no-default-alpn");
152
0
  if(!result && rr->ipv6hints_len) {
153
0
    result = curlx_dyn_add(tmp, " ipv6hint=");
154
0
    if(!result)
155
0
      result = httpsrr_print_addr(
156
0
        tmp, AF_INET6, rr->ipv6hints, rr->ipv6hints_len);
157
0
  }
158
0
  if(!result && rr->ipv4hints_len) {
159
0
    result = curlx_dyn_add(tmp, " ipv4hint=");
160
0
    if(!result)
161
0
      result = httpsrr_print_addr(
162
0
        tmp, AF_INET, rr->ipv4hints, rr->ipv4hints_len);
163
0
  }
164
0
  if(!result && rr->echconfiglist_len)
165
0
    result = curlx_dyn_addf(tmp, " ech=<%zu bytes>", rr->echconfiglist_len);
166
167
0
  return result;
168
0
}
169
170
#endif /* CURLVERBOSE */
171
172
CURLcode Curl_httpsrr_set(struct Curl_https_rrinfo *rr,
173
                          uint16_t rrkey, const uint8_t *val, size_t vlen)
174
0
{
175
0
  CURLcode result = CURLE_OK;
176
0
  switch(rrkey) {
177
0
  case HTTPS_RR_CODE_MANDATORY:
178
0
    rr->mandatory = TRUE;
179
0
    break;
180
0
  case HTTPS_RR_CODE_ALPN: /* str_list */
181
0
    result = httpsrr_decode_alpn(val, vlen, rr->alpns);
182
0
    break;
183
0
  case HTTPS_RR_CODE_NO_DEF_ALPN:
184
0
    if(vlen) /* no data */
185
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
186
0
    rr->no_def_alpn = TRUE;
187
0
    break;
188
0
  case HTTPS_RR_CODE_IPV4: /* addr4 list */
189
0
    if(!vlen || (vlen & 3)) /* the size must be 4-byte aligned */
190
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
191
0
    curlx_free(rr->ipv4hints);
192
0
    rr->ipv4hints = curlx_memdup(val, vlen);
193
0
    if(!rr->ipv4hints)
194
0
      return CURLE_OUT_OF_MEMORY;
195
0
    rr->ipv4hints_len = vlen;
196
0
    break;
197
0
  case HTTPS_RR_CODE_ECH:
198
0
    if(!vlen)
199
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
200
0
    curlx_free(rr->echconfiglist);
201
0
    rr->echconfiglist = curlx_memdup(val, vlen);
202
0
    if(!rr->echconfiglist)
203
0
      return CURLE_OUT_OF_MEMORY;
204
0
    rr->echconfiglist_len = vlen;
205
0
    break;
206
0
  case HTTPS_RR_CODE_IPV6: /* addr6 list */
207
0
    if(!vlen || (vlen & 15)) /* the size must be 16-byte aligned */
208
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
209
0
    curlx_free(rr->ipv6hints);
210
0
    rr->ipv6hints = curlx_memdup(val, vlen);
211
0
    if(!rr->ipv6hints)
212
0
      return CURLE_OUT_OF_MEMORY;
213
0
    rr->ipv6hints_len = vlen;
214
0
    break;
215
0
  case HTTPS_RR_CODE_PORT:
216
0
    if(vlen != 2)
217
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
218
0
    rr->port = (uint16_t)((val[0] << 8) | val[1]);
219
0
    rr->port_set = TRUE;
220
0
    break;
221
0
  default:
222
    /* unknown code */
223
0
    break;
224
0
  }
225
0
  return result;
226
0
}
227
228
void Curl_httpsrr_destroy(struct Curl_https_rrinfo *rrinfo)
229
8.53k
{
230
8.53k
  if(rrinfo) {
231
0
    curlx_free(rrinfo->target);
232
0
    curlx_free(rrinfo->echconfiglist);
233
0
    curlx_free(rrinfo->ipv4hints);
234
0
    curlx_free(rrinfo->ipv6hints);
235
0
    curlx_free(rrinfo);
236
0
  }
237
8.53k
}
238
239
bool Curl_httpsrr_applicable(struct Curl_easy *data,
240
                             const struct Curl_https_rrinfo *rr)
241
0
{
242
0
  if(!data->conn)
243
0
    return FALSE;
244
0
  return Curl_httpsrr_is_for_peer(data->conn->origin, rr);
245
0
}
246
247
bool Curl_httpsrr_is_for_peer(struct Curl_peer *peer,
248
                              const struct Curl_https_rrinfo *rr)
249
12
{
250
12
  if(!peer || !rr)
251
12
    return FALSE;
252
0
  return (!rr->target || !rr->target[0] ||
253
0
          (rr->target[0] == '.' && !rr->target[1])) &&
254
0
         (!rr->port_set || rr->port == peer->port);
255
12
}
256
257
#ifdef USE_ARES
258
259
static CURLcode httpsrr_opt(const ares_dns_rr_t *rr,
260
                            ares_dns_rr_key_t key, size_t idx,
261
                            struct Curl_https_rrinfo *hinfo)
262
{
263
  const unsigned char *val = NULL;
264
  unsigned short code;
265
  size_t len = 0;
266
267
  code = ares_dns_rr_get_opt(rr, key, idx, &val, &len);
268
  return Curl_httpsrr_set(hinfo, code, val, len);
269
}
270
271
CURLcode Curl_httpsrr_from_ares(const ares_dns_record_t *dnsrec,
272
                                struct Curl_https_rrinfo **phinfo)
273
{
274
  struct Curl_https_rrinfo *hinfo = NULL;
275
  CURLcode result = CURLE_OUT_OF_MEMORY;
276
  size_t i;
277
278
  hinfo = curlx_calloc(1, sizeof(*hinfo));
279
  if(!hinfo)
280
    goto out;
281
282
  for(i = 0; i < ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); i++) {
283
    const char *target;
284
    size_t opt;
285
    const ares_dns_rr_t *rr =
286
      ares_dns_record_rr_get_const(dnsrec, ARES_SECTION_ANSWER, i);
287
    if(ares_dns_rr_get_type(rr) != ARES_REC_TYPE_HTTPS)
288
      continue;
289
    /* When SvcPriority is 0, the SVCB record is in AliasMode. Otherwise, it
290
       is in ServiceMode */
291
    target = ares_dns_rr_get_str(rr, ARES_RR_HTTPS_TARGET);
292
    if(target && target[0]) {
293
      curlx_free(hinfo->target);
294
      hinfo->target = curlx_strdup(target);
295
      if(!hinfo->target) {
296
        result = CURLE_OUT_OF_MEMORY;
297
        goto out;
298
      }
299
    }
300
    hinfo->priority = ares_dns_rr_get_u16(rr, ARES_RR_HTTPS_PRIORITY);
301
    for(opt = 0; opt < ares_dns_rr_get_opt_cnt(rr, ARES_RR_HTTPS_PARAMS);
302
        opt++) {
303
      result = httpsrr_opt(rr, ARES_RR_HTTPS_PARAMS, opt, hinfo);
304
      if(result)
305
        goto out;
306
    }
307
  }
308
  result = CURLE_OK;
309
310
out:
311
  if(result) {
312
    *phinfo = NULL;
313
    Curl_httpsrr_destroy(hinfo);
314
  }
315
  else {
316
    DEBUGASSERT(hinfo);
317
    *phinfo = hinfo;
318
  }
319
  return result;
320
}
321
322
#endif /* USE_ARES */
323
324
#endif /* USE_HTTPSRR */