Coverage Report

Created: 2026-12-01 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/md5.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
#ifndef CURL_DISABLE_DIGEST_AUTH
27
28
#include "curl_md5.h"
29
30
#ifdef CURL_HAVE_MD5
31
32
#ifdef USE_OPENSSL_MD5
33
#include <openssl/evp.h>
34
35
typedef EVP_MD_CTX *my_md5_ctx;
36
37
static CURLcode my_md5_init(void *pctx)
38
0
{
39
0
  EVP_MD_CTX ** const ctx = pctx;
40
41
0
  *ctx = EVP_MD_CTX_new();
42
0
  if(!*ctx)
43
0
    return CURLE_OUT_OF_MEMORY;
44
45
0
  if(!EVP_DigestInit_ex(*ctx, EVP_md5(), NULL)) {
46
0
    EVP_MD_CTX_free(*ctx);
47
0
    *ctx = NULL;
48
0
    return CURLE_FAILED_INIT;
49
0
  }
50
0
  return CURLE_OK;
51
0
}
52
53
static CURLcode my_md5_update(void *pctx, const void *input, size_t input_len)
54
0
{
55
0
  EVP_MD_CTX ** const ctx = pctx;
56
0
  return EVP_DigestUpdate(*ctx, input, input_len) ?
57
0
    CURLE_OK : CURLE_BAD_FUNCTION_ARGUMENT;
58
0
}
59
60
static CURLcode my_md5_final(uint8_t *digest, void *pctx)
61
0
{
62
0
  EVP_MD_CTX ** const ctx = pctx;
63
0
  CURLcode result = EVP_DigestFinal_ex(*ctx, digest, NULL) ?
64
0
    CURLE_OK : CURLE_BAD_FUNCTION_ARGUMENT;
65
0
  EVP_MD_CTX_free(*ctx);
66
0
  *ctx = NULL;
67
0
  return result;
68
0
}
69
70
#elif defined(USE_WOLFSSL_MD5)
71
#include <wolfssl/wolfcrypt/md5.h>
72
73
typedef wc_Md5 my_md5_ctx;
74
75
static CURLcode my_md5_init(void *ctx)
76
{
77
  if(wc_InitMd5(ctx))
78
    return CURLE_OUT_OF_MEMORY;
79
  return CURLE_OK;
80
}
81
82
static CURLcode my_md5_update(void *ctx, const void *input, size_t input_len)
83
{
84
  const byte *bytes = input;
85
  do {
86
    word32 ilen = (word32)CURLMIN(input_len, UINT32_MAX);
87
    if(wc_Md5Update(ctx, bytes, ilen))
88
      return CURLE_BAD_FUNCTION_ARGUMENT;
89
    input_len -= ilen;
90
    bytes += ilen;
91
  } while(input_len);
92
  return CURLE_OK;
93
}
94
95
static CURLcode my_md5_final(uint8_t *digest, void *ctx)
96
{
97
  if(wc_Md5Final(ctx, digest))
98
    return CURLE_BAD_FUNCTION_ARGUMENT;
99
  return CURLE_OK;
100
}
101
102
#elif defined(USE_GNUTLS_MD5)
103
#include <nettle/md5.h>
104
#include <nettle/version.h>
105
106
typedef struct md5_ctx my_md5_ctx;
107
108
static CURLcode my_md5_init(void *ctx)
109
{
110
  md5_init(ctx);
111
  return CURLE_OK;
112
}
113
114
static CURLcode my_md5_update(void *ctx, const void *input, size_t input_len)
115
{
116
  md5_update(ctx, input_len, input);
117
  return CURLE_OK;
118
}
119
120
static CURLcode my_md5_final(uint8_t *digest, void *ctx)
121
{
122
#if NETTLE_VERSION_MAJOR >= 4
123
  md5_digest(ctx, digest);
124
#else
125
  md5_digest(ctx, CURL_MD5_DIGEST_LEN, digest);
126
#endif
127
  return CURLE_OK;
128
}
129
130
#elif defined(USE_MBEDTLS_MD5)
131
#include <psa/crypto.h>
132
133
typedef psa_hash_operation_t my_md5_ctx;
134
135
static CURLcode my_md5_init(void *pctx)
136
{
137
  psa_hash_operation_t *ctx = pctx;
138
  *ctx = psa_hash_operation_init();
139
  if(psa_hash_setup(pctx, PSA_ALG_MD5) != PSA_SUCCESS)
140
    return CURLE_FAILED_INIT;
141
  return CURLE_OK;
142
}
143
144
static CURLcode my_md5_update(void *ctx, const void *input, size_t input_len)
145
{
146
  if(psa_hash_update(ctx, input, input_len) != PSA_SUCCESS)
147
    return CURLE_BAD_FUNCTION_ARGUMENT;
148
  return CURLE_OK;
149
}
150
151
static CURLcode my_md5_final(uint8_t *digest, void *ctx)
152
{
153
  size_t actual_length;
154
  if(psa_hash_finish(ctx, digest, CURL_MD5_DIGEST_LEN,
155
                     &actual_length) != PSA_SUCCESS)
156
    return CURLE_BAD_FUNCTION_ARGUMENT;
157
  return CURLE_OK;
158
}
159
160
#elif defined(USE_APPLE_MD5)
161
#include <CommonCrypto/CommonDigest.h>
162
163
/* For Apple operating systems: CommonCrypto has the functions we need.
164
   These functions are available on Tiger and later, as well as iOS 2.0
165
   and later. If you are building for an older cat, well, sorry.
166
167
   Declaring the functions as static like this seems to be a bit more
168
   reliable than defining COMMON_DIGEST_FOR_OPENSSL on older cats. */
169
typedef CC_MD5_CTX my_md5_ctx;
170
171
static CURLcode my_md5_init(void *ctx)
172
{
173
  (void)CC_MD5_Init(ctx);  /* always returns 1 */
174
  return CURLE_OK;
175
}
176
177
static CURLcode my_md5_update(void *ctx, const void *input, size_t input_len)
178
{
179
  const uint8_t *bytes = input;
180
  do {
181
    CC_LONG ilen = (CC_LONG)CURLMIN(input_len, UINT32_MAX);
182
    CC_MD5_Update(ctx, bytes, ilen);  /* always returns 1 */
183
    input_len -= ilen;
184
    bytes += ilen;
185
  } while(input_len);
186
  return CURLE_OK;
187
}
188
189
static CURLcode my_md5_final(uint8_t *digest, void *ctx)
190
{
191
  CC_MD5_Final(digest, ctx);  /* always returns 1 */
192
  return CURLE_OK;
193
}
194
195
#elif defined(USE_WIN32_MD5)
196
#include <bcrypt.h>
197
198
struct md5_ctx {
199
  BCRYPT_ALG_HANDLE hAlg;
200
  BCRYPT_HASH_HANDLE hHash;
201
};
202
typedef struct md5_ctx my_md5_ctx;
203
204
static CURLcode my_md5_init(void *pctx)
205
{
206
  struct md5_ctx *ctx = pctx;
207
208
  ctx->hAlg = ctx->hHash = NULL;
209
  if(!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&ctx->hAlg,
210
                                                 BCRYPT_MD5_ALGORITHM,
211
                                                 NULL, 0)))
212
    return CURLE_FAILED_INIT;
213
  if(!BCRYPT_SUCCESS(BCryptCreateHash(ctx->hAlg, &ctx->hHash,
214
                                      NULL, 0, NULL, 0, 0))) {
215
    BCryptCloseAlgorithmProvider(ctx->hAlg, 0);
216
    ctx->hAlg = NULL;
217
    return CURLE_FAILED_INIT;
218
  }
219
220
  return CURLE_OK;
221
}
222
223
static CURLcode my_md5_update(void *pctx, const void *input, size_t input_len)
224
{
225
  struct md5_ctx *ctx = pctx;
226
  const BYTE *bytes = input;
227
  do {
228
    ULONG ilen = (ULONG)CURLMIN(input_len, ULONG_MAX);
229
    if(!BCRYPT_SUCCESS(BCryptHashData(ctx->hHash, CURL_UNCONST(bytes),
230
                                      ilen, 0)))
231
      return CURLE_BAD_FUNCTION_ARGUMENT;
232
    input_len -= ilen;
233
    bytes += ilen;
234
  } while(input_len);
235
  return CURLE_OK;
236
}
237
238
static CURLcode my_md5_final(uint8_t *digest, void *pctx)
239
{
240
  struct md5_ctx *ctx = pctx;
241
  CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
242
  if(BCRYPT_SUCCESS(BCryptFinishHash(ctx->hHash,
243
                                     digest, CURL_MD5_DIGEST_LEN, 0)))
244
    result = CURLE_OK;
245
  BCryptDestroyHash(ctx->hHash);
246
  ctx->hHash = NULL;
247
  if(ctx->hAlg) {
248
    BCryptCloseAlgorithmProvider(ctx->hAlg, 0);
249
    ctx->hAlg = NULL;
250
  }
251
  return result;
252
}
253
254
#endif /* CRYPTO LIBS */
255
256
const struct HMAC_params Curl_HMAC_MD5 = {
257
  my_md5_init,        /* Hash initialization function. */
258
  my_md5_update,      /* Hash update function. */
259
  my_md5_final,       /* Hash computation end function. */
260
  sizeof(my_md5_ctx), /* Size of hash context structure. */
261
  64,                 /* Maximum key length. */
262
  CURL_MD5_DIGEST_LEN /* Result size. */
263
};
264
265
/*
266
 * Returns CURLE_OK on success.
267
 * @unittest: 1601
268
 */
269
CURLcode Curl_md5it(uint8_t *digest, const void *input, size_t input_len)
270
0
{
271
0
  my_md5_ctx ctx;
272
0
  CURLcode result = my_md5_init(&ctx);
273
0
  if(result)
274
0
    return result;
275
276
0
  result = my_md5_update(&ctx, input, input_len);
277
0
  if(result) {
278
0
    (void)my_md5_final(digest, &ctx);
279
0
    return result;
280
0
  }
281
282
0
  return my_md5_final(digest, &ctx);
283
0
}
284
285
void *Curl_MD5_init(void)
286
0
{
287
0
  my_md5_ctx *ctx = curlx_malloc(sizeof(*ctx));  /* Create MD5 context */
288
0
  if(!ctx)
289
0
    return ctx;
290
291
0
  if(my_md5_init(ctx)) {
292
0
    curlx_free(ctx);
293
0
    return NULL;
294
0
  }
295
296
0
  return ctx;
297
0
}
298
299
CURLcode Curl_MD5_update(void *ctx, const void *input, size_t input_len)
300
0
{
301
0
  return my_md5_update(ctx, input, input_len);
302
0
}
303
304
CURLcode Curl_MD5_final(void *ctx, uint8_t *digest)
305
0
{
306
0
  CURLcode result = my_md5_final(digest, ctx);
307
0
  curlx_free(ctx);
308
0
  return result;
309
0
}
310
311
#endif /* CURL_HAVE_MD5 */
312
313
#endif /* !CURL_DISABLE_DIGEST_AUTH */