Coverage Report

Created: 2026-12-01 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/creds.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
#include <stddef.h>  /* for offsetof() */
27
28
#include "creds.h"
29
#include "curl_trc.h"
30
#include "strcase.h"
31
#include "urldata.h"
32
33
34
CURLcode Curl_creds_create(const char *user,
35
                           const char *passwd,
36
                           const char *oauth_bearer,
37
                           const char *sasl_authzid,
38
                           const char *sasl_service,
39
                           uint8_t source,
40
                           struct Curl_creds **pcreds)
41
38.8k
{
42
38.8k
  struct Curl_creds *creds = NULL;
43
38.8k
  size_t ulen = user ? strlen(user) : 0;
44
38.8k
  size_t plen = passwd ? strlen(passwd) : 0;
45
38.8k
  size_t olen = oauth_bearer ? strlen(oauth_bearer) : 0;
46
38.8k
  size_t salen = sasl_authzid ? strlen(sasl_authzid) : 0;
47
38.8k
  size_t sslen = sasl_service ? strlen(sasl_service) : 0;
48
38.8k
  char *s, *buf;
49
38.8k
  size_t bufsize;
50
38.8k
  CURLcode result = CURLE_OK;
51
52
38.8k
  Curl_creds_unlink(pcreds);
53
54
  /* Everything empty/NULL, this is the NULL credential */
55
38.8k
  if(!user && !passwd && !olen && !salen && !sslen)
56
30.4k
    goto out;
57
58
8.36k
  if((ulen > CURL_MAX_INPUT_LENGTH) ||
59
8.36k
     (plen > CURL_MAX_INPUT_LENGTH) ||
60
8.36k
     (olen > CURL_MAX_INPUT_LENGTH) ||
61
8.36k
     (salen > CURL_MAX_INPUT_LENGTH) ||
62
8.36k
     (sslen > CURL_MAX_INPUT_LENGTH)) {
63
0
    result = CURLE_BAD_FUNCTION_ARGUMENT;
64
0
    goto out;
65
0
  }
66
67
  /* null-terminator for user already part of struct */
68
8.36k
  bufsize = ulen + plen + 1 + olen + 1 + salen + 1 + sslen + 1;
69
8.36k
  creds = curlx_calloc(1, sizeof(*creds) + bufsize);
70
8.36k
  if(!creds) {
71
1
    result = CURLE_OUT_OF_MEMORY;
72
1
    goto out;
73
1
  }
74
75
8.36k
  creds->bufsize = bufsize;
76
8.36k
  Curl_refcount_init(&creds->refcount, 1);
77
8.36k
  creds->source = source;
78
  /* Some compilers try to be too smart about our dynamic struct size */
79
8.36k
  buf = ((char *)creds) + offsetof(struct Curl_creds, buf);
80
8.36k
  creds->user = s = buf;
81
8.36k
  if(ulen)
82
2.45k
    memcpy(s, user, ulen + 1);
83
8.36k
  creds->passwd = s = buf + ulen + 1;
84
8.36k
  if(plen)
85
757
    memcpy(s, passwd, plen + 1);
86
8.36k
  creds->oauth_bearer = s = buf + ulen + 1 + plen + 1;
87
8.36k
  if(olen)
88
902
    memcpy(s, oauth_bearer, olen + 1);
89
8.36k
  creds->sasl_authzid = s = buf + ulen + 1 + plen + 1 + olen + 1;
90
8.36k
  if(salen)
91
501
    memcpy(s, sasl_authzid, salen + 1);
92
8.36k
  creds->sasl_service = s = buf + ulen + 1 + plen + 1 + olen + 1 + salen + 1;
93
8.36k
  if(sslen)
94
0
    memcpy(s, sasl_service, sslen + 1);
95
96
38.8k
out:
97
38.8k
  if(!result)
98
38.8k
    *pcreds = creds;
99
1
  else
100
1
    Curl_creds_unlink(&creds);
101
38.8k
  return result;
102
8.36k
}
103
104
CURLcode Curl_creds_merge(const char *user,
105
                          const char *passwd,
106
                          struct Curl_creds *creds_in,
107
                          uint8_t source,
108
                          struct Curl_creds **pcreds_out)
109
36.4k
{
110
36.4k
  struct Curl_creds *creds_out = NULL;
111
36.4k
  CURLcode result;
112
113
36.4k
  if(!creds_in) {
114
34.6k
    result = Curl_creds_create(user, passwd, NULL, NULL, NULL,
115
34.6k
                               source, &creds_out);
116
34.6k
  }
117
1.79k
  else {
118
1.79k
    result = Curl_creds_create(user ? user : Curl_creds_user(creds_in),
119
1.79k
                               passwd ? passwd : Curl_creds_passwd(creds_in),
120
1.79k
                               Curl_creds_oauth_bearer(creds_in),
121
1.79k
                               Curl_creds_sasl_authzid(creds_in),
122
1.79k
                               Curl_creds_sasl_service(creds_in),
123
1.79k
                               source, &creds_out);
124
1.79k
  }
125
36.4k
  Curl_creds_link(pcreds_out, creds_out);
126
36.4k
  Curl_creds_unlink(&creds_out);
127
36.4k
  return result;
128
36.4k
}
129
130
void Curl_creds_link(struct Curl_creds **pdest, struct Curl_creds *src)
131
43.1k
{
132
43.1k
  if(*pdest != src) {
133
9.30k
    Curl_creds_unlink(pdest);
134
9.30k
    *pdest = src;
135
9.30k
    if(src) {
136
9.27k
      uint32_t newcount = Curl_refcount_incr(&src->refcount);
137
9.27k
      (void)newcount;
138
9.27k
      DEBUGASSERT(newcount < UINT32_MAX);
139
9.27k
    }
140
9.30k
  }
141
43.1k
}
142
143
void Curl_creds_unlink(struct Curl_creds **pcreds)
144
480k
{
145
480k
  if(*pcreds) {
146
17.6k
    struct Curl_creds *creds = *pcreds;
147
17.6k
    uint32_t newcount;
148
149
17.6k
    *pcreds = NULL;
150
151
17.6k
    newcount = Curl_refcount_decr(&creds->refcount);
152
17.6k
    DEBUGASSERT(newcount < UINT32_MAX); /* wraparound check */
153
154
17.6k
    if(!newcount) {
155
8.36k
      curlx_memzero(creds, sizeof(*creds) + creds->bufsize);
156
8.36k
      curlx_free(creds);
157
8.36k
    }
158
17.6k
  }
159
480k
}
160
161
bool Curl_creds_same(struct Curl_creds *c1, struct Curl_creds *c2)
162
43.4k
{
163
43.4k
  return (c1 == c2) ||
164
6.61k
         (c1 && c2 &&
165
4.06k
          !Curl_timestrcmp(c1->user, c2->user) &&
166
3.92k
          !Curl_timestrcmp(c1->passwd, c2->passwd) &&
167
3.92k
          !Curl_timestrcmp(c1->oauth_bearer, c2->oauth_bearer) &&
168
3.92k
          !Curl_timestrcmp(c1->sasl_authzid, c2->sasl_authzid) &&
169
3.92k
          !Curl_timestrcmp(c1->sasl_service, c2->sasl_service));
170
43.4k
}
171
172
bool Curl_creds_equal(struct Curl_creds *c1, struct Curl_creds *c2)
173
36.9k
{
174
36.9k
  return Curl_creds_same(c1, c2) &&
175
34.3k
         ((c1 == c2) || (c1 && c2 && (c1->source == c2->source)));
176
36.9k
}
177
178
#ifdef CURLVERBOSE
179
void Curl_creds_trace(struct Curl_easy *data, struct Curl_creds *creds,
180
                      const char *msg)
181
36.9k
{
182
36.9k
  if(creds) {
183
4.06k
    CURL_TRC_M(data, "%s: user=%s, passwd=%s, "
184
4.06k
               "sasl_authzid=%s, oauth_bearer=%s, source=%d",
185
4.06k
               msg,
186
4.06k
               Curl_creds_user(creds),
187
4.06k
               Curl_creds_has_passwd(creds) ? "***" : "",
188
4.06k
               Curl_creds_sasl_authzid(creds),
189
4.06k
               Curl_creds_has_oauth_bearer(creds) ? "***" : "",
190
4.06k
               creds->source);
191
4.06k
  }
192
32.9k
  else
193
32.9k
    CURL_TRC_M(data, "%s: -", msg);
194
36.9k
}
195
#endif