~ubuntu-branches/ubuntu/saucy/curl/saucy

« back to all changes in this revision

Viewing changes to src/tool_metalink.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2013-02-12 08:54:32 UTC
  • mfrom: (3.4.34 sid)
  • Revision ID: package-import@ubuntu.com-20130212085432-r1fyi0b37enr93pp
Tags: 7.29.0-1ubuntu1
* Resynchronise with Debian. Remaining changes:
  - Drop dependencies not in main:
    + Build-Depends: Drop stunnel4 and libssh2-1-dev.
    + Drop libssh2-1-dev from binary package Depends.
  - Add new libcurl3-udeb package.
  - Add new curl-udeb package.
* Add warning to debian/patches/series.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
#ifdef USE_METALINK
25
25
 
26
 
#ifdef HAVE_UNISTD_H
27
 
#  include <unistd.h>
28
 
#endif
29
 
 
30
26
#include <sys/stat.h>
31
27
 
32
28
#ifdef HAVE_FCNTL_H
52
48
#  define MD5_CTX    gcry_md_hd_t
53
49
#  define SHA_CTX    gcry_md_hd_t
54
50
#  define SHA256_CTX gcry_md_hd_t
55
 
#elif defined(USE_DARWINSSL)
56
 
/* For darwinssl: CommonCrypto has the functions we need. The library's
57
 
   headers are even backward-compatible with OpenSSL's headers as long as
58
 
   we define COMMON_DIGEST_FOR_OPENSSL first.
 
51
#elif defined(USE_NSS)
 
52
#  include <nss.h>
 
53
#  include <pk11pub.h>
 
54
#  define MD5_CTX    void *
 
55
#  define SHA_CTX    void *
 
56
#  define SHA256_CTX void *
 
57
#  ifdef HAVE_NSS_INITCONTEXT
 
58
     static NSSInitContext *nss_context;
 
59
#  endif
 
60
#elif defined(__MAC_10_4) || defined(__IPHONE_5_0)
 
61
/* For Apple operating systems: CommonCrypto has the functions we need.
 
62
   The library's headers are even backward-compatible with OpenSSL's
 
63
   headers as long as we define COMMON_DIGEST_FOR_OPENSSL first.
59
64
 
60
65
   These functions are available on Tiger and later, as well as iOS 5.0
61
66
   and later. If you're building for an older cat, well, sorry. */
112
117
 
113
118
#ifdef USE_GNUTLS_NETTLE
114
119
 
115
 
static void MD5_Init(MD5_CTX *ctx)
 
120
static int MD5_Init(MD5_CTX *ctx)
116
121
{
117
122
  md5_init(ctx);
 
123
  return 1;
118
124
}
119
125
 
120
126
static void MD5_Update(MD5_CTX *ctx,
129
135
  md5_digest(ctx, 16, digest);
130
136
}
131
137
 
132
 
static void SHA1_Init(SHA_CTX *ctx)
 
138
static int SHA1_Init(SHA_CTX *ctx)
133
139
{
134
140
  sha1_init(ctx);
 
141
  return 1;
135
142
}
136
143
 
137
144
static void SHA1_Update(SHA_CTX *ctx,
146
153
  sha1_digest(ctx, 20, digest);
147
154
}
148
155
 
149
 
static void SHA256_Init(SHA256_CTX *ctx)
 
156
static int SHA256_Init(SHA256_CTX *ctx)
150
157
{
151
158
  sha256_init(ctx);
 
159
  return 1;
152
160
}
153
161
 
154
162
static void SHA256_Update(SHA256_CTX *ctx,
165
173
 
166
174
#elif defined(USE_GNUTLS)
167
175
 
168
 
static void MD5_Init(MD5_CTX *ctx)
 
176
static int MD5_Init(MD5_CTX *ctx)
169
177
{
170
178
  gcry_md_open(ctx, GCRY_MD_MD5, 0);
 
179
  return 1;
171
180
}
172
181
 
173
182
static void MD5_Update(MD5_CTX *ctx,
183
192
  gcry_md_close(*ctx);
184
193
}
185
194
 
186
 
static void SHA1_Init(SHA_CTX *ctx)
 
195
static int SHA1_Init(SHA_CTX *ctx)
187
196
{
188
197
  gcry_md_open(ctx, GCRY_MD_SHA1, 0);
 
198
  return 1;
189
199
}
190
200
 
191
201
static void SHA1_Update(SHA_CTX *ctx,
201
211
  gcry_md_close(*ctx);
202
212
}
203
213
 
204
 
static void SHA256_Init(SHA256_CTX *ctx)
 
214
static int SHA256_Init(SHA256_CTX *ctx)
205
215
{
206
216
  gcry_md_open(ctx, GCRY_MD_SHA256, 0);
 
217
  return 1;
207
218
}
208
219
 
209
220
static void SHA256_Update(SHA256_CTX *ctx,
219
230
  gcry_md_close(*ctx);
220
231
}
221
232
 
222
 
#elif defined(_WIN32)
 
233
#elif defined(USE_NSS)
 
234
 
 
235
static int nss_hash_init(void **pctx, SECOidTag hash_alg)
 
236
{
 
237
  PK11Context *ctx;
 
238
 
 
239
  /* we have to initialize NSS if not initialized alraedy */
 
240
#ifdef HAVE_NSS_INITCONTEXT
 
241
  if(!NSS_IsInitialized() && !nss_context) {
 
242
    static NSSInitParameters params;
 
243
    params.length = sizeof params;
 
244
    nss_context = NSS_InitContext("", "", "", "", &params, NSS_INIT_READONLY
 
245
        | NSS_INIT_NOCERTDB   | NSS_INIT_NOMODDB       | NSS_INIT_FORCEOPEN
 
246
        | NSS_INIT_NOROOTINIT | NSS_INIT_OPTIMIZESPACE | NSS_INIT_PK11RELOAD);
 
247
  }
 
248
#endif
 
249
 
 
250
  ctx = PK11_CreateDigestContext(hash_alg);
 
251
  if(!ctx)
 
252
    return /* failure */ 0;
 
253
 
 
254
  if(PK11_DigestBegin(ctx) != SECSuccess) {
 
255
    PK11_DestroyContext(ctx, PR_TRUE);
 
256
    return /* failure */ 0;
 
257
  }
 
258
 
 
259
  *pctx = ctx;
 
260
  return /* success */ 1;
 
261
}
 
262
 
 
263
static void nss_hash_final(void **pctx, unsigned char *out, unsigned int len)
 
264
{
 
265
  PK11Context *ctx = *pctx;
 
266
  unsigned int outlen;
 
267
  PK11_DigestFinal(ctx, out, &outlen, len);
 
268
  PK11_DestroyContext(ctx, PR_TRUE);
 
269
}
 
270
 
 
271
static int MD5_Init(MD5_CTX *pctx)
 
272
{
 
273
  return nss_hash_init(pctx, SEC_OID_MD5);
 
274
}
 
275
 
 
276
static void MD5_Update(MD5_CTX *pctx,
 
277
                       const unsigned char *input,
 
278
                       unsigned int input_len)
 
279
{
 
280
  PK11_DigestOp(*pctx, input, input_len);
 
281
}
 
282
 
 
283
static void MD5_Final(unsigned char digest[16], MD5_CTX *pctx)
 
284
{
 
285
  nss_hash_final(pctx, digest, 16);
 
286
}
 
287
 
 
288
static int SHA1_Init(SHA_CTX *pctx)
 
289
{
 
290
  return nss_hash_init(pctx, SEC_OID_SHA1);
 
291
}
 
292
 
 
293
static void SHA1_Update(SHA_CTX *pctx,
 
294
                        const unsigned char *input,
 
295
                        unsigned int input_len)
 
296
{
 
297
  PK11_DigestOp(*pctx, input, input_len);
 
298
}
 
299
 
 
300
static void SHA1_Final(unsigned char digest[20], SHA_CTX *pctx)
 
301
{
 
302
  nss_hash_final(pctx, digest, 20);
 
303
}
 
304
 
 
305
static int SHA256_Init(SHA256_CTX *pctx)
 
306
{
 
307
  return nss_hash_init(pctx, SEC_OID_SHA256);
 
308
}
 
309
 
 
310
static void SHA256_Update(SHA256_CTX *pctx,
 
311
                          const unsigned char *input,
 
312
                          unsigned int input_len)
 
313
{
 
314
  PK11_DigestOp(*pctx, input, input_len);
 
315
}
 
316
 
 
317
static void SHA256_Final(unsigned char digest[32], SHA256_CTX *pctx)
 
318
{
 
319
  nss_hash_final(pctx, digest, 32);
 
320
}
 
321
 
 
322
#elif defined(_WIN32) && !defined(USE_SSLEAY)
223
323
 
224
324
static void win32_crypto_final(struct win32_crypto_hash *ctx,
225
325
                               unsigned char *digest,
235
335
    CryptReleaseContext(ctx->hCryptProv, 0);
236
336
}
237
337
 
238
 
static void MD5_Init(MD5_CTX *ctx)
 
338
static int MD5_Init(MD5_CTX *ctx)
239
339
{
240
340
  if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL,
241
341
                         PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
242
342
    CryptCreateHash(ctx->hCryptProv, CALG_MD5, 0, 0, &ctx->hHash);
243
343
  }
 
344
  return 1;
244
345
}
245
346
 
246
347
static void MD5_Update(MD5_CTX *ctx,
255
356
  win32_crypto_final(ctx, digest, 16);
256
357
}
257
358
 
258
 
static void SHA1_Init(SHA_CTX *ctx)
 
359
static int SHA1_Init(SHA_CTX *ctx)
259
360
{
260
361
  if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL,
261
362
                         PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
262
363
    CryptCreateHash(ctx->hCryptProv, CALG_SHA1, 0, 0, &ctx->hHash);
263
364
  }
 
365
  return 1;
264
366
}
265
367
 
266
368
static void SHA1_Update(SHA_CTX *ctx,
275
377
  win32_crypto_final(ctx, digest, 20);
276
378
}
277
379
 
278
 
static void SHA256_Init(SHA256_CTX *ctx)
 
380
static int SHA256_Init(SHA256_CTX *ctx)
279
381
{
280
382
  if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL,
281
383
                         PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
282
384
    CryptCreateHash(ctx->hCryptProv, CALG_SHA_256, 0, 0, &ctx->hHash);
283
385
  }
 
386
  return 1;
284
387
}
285
388
 
286
389
static void SHA256_Update(SHA256_CTX *ctx,
374
477
 
375
478
  ctxt->digest_hash = dparams;
376
479
 
377
 
  dparams->digest_init(ctxt->digest_hashctx);
 
480
  if(dparams->digest_init(ctxt->digest_hashctx) != 1) {
 
481
    free(ctxt);
 
482
    return NULL;
 
483
  }
378
484
 
379
485
  return ctxt;
380
486
}
425
531
 *   Checksum didn't match.
426
532
 * -1:
427
533
 *   Could not open file; or could not read data from file.
 
534
 * -2:
 
535
 *   Hash algorithm not available.
428
536
 */
429
537
static int check_hash(const char *filename,
430
538
                      const metalink_digest_def *digest_def,
446
554
            digest_def->hash_name, strerror(errno));
447
555
    return -1;
448
556
  }
 
557
 
449
558
  dctx = Curl_digest_init(digest_def->dparams);
 
559
  if(!dctx) {
 
560
    fprintf(error, "Metalink: validating (%s) [%s] FAILED (%s)\n", filename,
 
561
            digest_def->hash_name, "failed to initialize hash algorithm");
 
562
    close(fd);
 
563
    return -2;
 
564
  }
 
565
 
450
566
  result = malloc(digest_def->dparams->digest_resultlen);
451
567
  while(1) {
452
568
    unsigned char buf[4096];
773
889
  config->metalinkfile_last = 0;
774
890
}
775
891
 
 
892
void metalink_cleanup(void)
 
893
{
 
894
#if defined(USE_NSS) && defined(HAVE_NSS_INITCONTEXT)
 
895
  if(nss_context) {
 
896
    NSS_ShutdownContext(nss_context);
 
897
    nss_context = NULL;
 
898
  }
 
899
#endif
 
900
}
 
901
 
776
902
#endif /* USE_METALINK */