1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
34 #include <metalink/metalink_parser.h>
38 #include "tool_metalink.h"
39 #include "tool_getparam.h"
40 #include "tool_paramhlp.h"
41 #include "tool_cfgable.h"
43 #include "memdebug.h" /* keep this as LAST include */
45 /* Copied from tool_getparam.c */
46 #define GetStr(str,val) do { \
52 *(str) = strdup((val)); \
54 return PARAM_NO_MEM; \
57 #ifdef USE_GNUTLS_NETTLE
59 #include <nettle/md5.h>
60 #include <nettle/sha.h>
62 typedef struct md5_ctx MD5_CTX;
64 static void MD5_Init(MD5_CTX * ctx)
69 static void MD5_Update(MD5_CTX * ctx,
70 const unsigned char * input,
71 unsigned int inputLen)
73 md5_update(ctx, inputLen, input);
76 static void MD5_Final(unsigned char digest[16], MD5_CTX * ctx)
78 md5_digest(ctx, 16, digest);
81 typedef struct sha1_ctx SHA_CTX;
83 static void SHA1_Init(SHA_CTX *ctx)
88 static void SHA1_Update(SHA_CTX *ctx,
89 const unsigned char * input,
90 unsigned int inputLen)
92 sha1_update(ctx, inputLen, input);
95 static void SHA1_Final(unsigned char digest[20], SHA_CTX * ctx)
97 sha1_digest(ctx, 20, digest);
100 typedef struct sha256_ctx SHA256_CTX;
102 static void SHA256_Init(SHA256_CTX *ctx)
107 static void SHA256_Update(SHA256_CTX *ctx,
108 const unsigned char * input,
109 unsigned int inputLen)
111 sha256_update(ctx, inputLen, input);
114 static void SHA256_Final(unsigned char digest[32], SHA256_CTX * ctx)
116 sha256_digest(ctx, 32, digest);
125 typedef gcry_md_hd_t MD5_CTX;
127 static void MD5_Init(MD5_CTX * ctx)
129 gcry_md_open(ctx, GCRY_MD_MD5, 0);
132 static void MD5_Update(MD5_CTX * ctx,
133 const unsigned char * input,
134 unsigned int inputLen)
136 gcry_md_write(*ctx, input, inputLen);
139 static void MD5_Final(unsigned char digest[16], MD5_CTX * ctx)
141 memcpy(digest, gcry_md_read(*ctx, 0), 16);
145 typedef gcry_md_hd_t SHA_CTX;
147 static void SHA1_Init(SHA_CTX * ctx)
149 gcry_md_open(ctx, GCRY_MD_SHA1, 0);
152 static void SHA1_Update(SHA_CTX * ctx,
153 const unsigned char * input,
154 unsigned int inputLen)
156 gcry_md_write(*ctx, input, inputLen);
159 static void SHA1_Final(unsigned char digest[20], SHA_CTX * ctx)
161 memcpy(digest, gcry_md_read(*ctx, 0), 20);
165 typedef gcry_md_hd_t SHA256_CTX;
167 static void SHA256_Init(SHA256_CTX * ctx)
169 gcry_md_open(ctx, GCRY_MD_SHA256, 0);
172 static void SHA256_Update(SHA256_CTX * ctx,
173 const unsigned char * input,
174 unsigned int inputLen)
176 gcry_md_write(*ctx, input, inputLen);
179 static void SHA256_Final(unsigned char digest[32], SHA256_CTX * ctx)
181 memcpy(digest, gcry_md_read(*ctx, 0), 32);
188 # include <openssl/md5.h>
189 # include <openssl/sha.h>
190 #else /* USE_OPENSSL */
192 /* TODO hash functions for other libraries here */
194 #endif /* USE_OPENSSL */
196 #endif /* USE_GNUTLS */
198 #endif /* USE_GNUTLS_NETTLE */
200 #ifdef METALINK_HASH_CHECK
202 const digest_params MD5_DIGEST_PARAMS[] = {
204 (Curl_digest_init_func) MD5_Init,
205 (Curl_digest_update_func) MD5_Update,
206 (Curl_digest_final_func) MD5_Final,
212 const digest_params SHA1_DIGEST_PARAMS[] = {
214 (Curl_digest_init_func) SHA1_Init,
215 (Curl_digest_update_func) SHA1_Update,
216 (Curl_digest_final_func) SHA1_Final,
222 const digest_params SHA256_DIGEST_PARAMS[] = {
224 (Curl_digest_init_func) SHA256_Init,
225 (Curl_digest_update_func) SHA256_Update,
226 (Curl_digest_final_func) SHA256_Final,
232 digest_context *Curl_digest_init(const digest_params *dparams)
234 digest_context *ctxt;
236 /* Create digest context */
237 ctxt = malloc(sizeof *ctxt);
242 ctxt->digest_hashctx = malloc(dparams->digest_ctxtsize);
244 if(!ctxt->digest_hashctx) {
249 ctxt->digest_hash = dparams;
251 dparams->digest_init(ctxt->digest_hashctx);
256 int Curl_digest_update(digest_context *context,
257 const unsigned char *data,
260 (*context->digest_hash->digest_update)(context->digest_hashctx, data, len);
265 int Curl_digest_final(digest_context *context, unsigned char *result)
267 (*context->digest_hash->digest_final)(result, context->digest_hashctx);
269 free(context->digest_hashctx);
275 static const metalink_digest_def SHA256_DIGEST_DEF[] = {
276 {"sha-256", SHA256_DIGEST_PARAMS}
279 static const metalink_digest_def SHA1_DIGEST_DEF[] = {
280 {"sha-1", SHA1_DIGEST_PARAMS}
283 static const metalink_digest_def MD5_DIGEST_DEF[] = {
284 {"md5", MD5_DIGEST_PARAMS}
288 * The alias of supported hash functions in the order by preference
289 * (basically stronger hash comes first). We included "sha-256" and
290 * "sha256". The former is the name defined in the IANA registry named
291 * "Hash Function Textual Names". The latter is widely (and
292 * historically) used in Metalink version 3.
294 static const metalink_digest_alias digest_aliases[] = {
295 {"sha-256", SHA256_DIGEST_DEF},
296 {"sha256", SHA256_DIGEST_DEF},
297 {"sha-1", SHA1_DIGEST_DEF},
298 {"sha1", SHA1_DIGEST_DEF},
299 {"md5", MD5_DIGEST_DEF},
303 static unsigned char hex_to_uint(const char *s)
307 for(i = 0; i < 2; ++i) {
308 v[i] = Curl_raw_toupper(s[i]);
309 if('0' <= v[i] && v[i] <= '9') {
312 else if('A' <= v[i] && v[i] <= 'Z') {
316 return (unsigned char)((v[0] << 4) | v[1]);
320 * Check checksum of file denoted by filename. The expected hash value
321 * is given in hex_hash which is hex-encoded string.
323 * This function returns 1 if it succeeds or one of the following
327 * Checksum didn't match.
329 * Could not open file; or could not read data from file.
331 static int check_hash(const char *filename,
332 const metalink_digest_def *digest_def,
333 const char *hex_hash, FILE *error)
335 unsigned char *result;
336 digest_context *dctx;
340 fprintf(error, "Checking %s checksum of file %s\n", digest_def->hash_name,
342 fd = open(filename, O_RDONLY);
344 fprintf(error, "Could not open file %s: %s\n", filename, strerror(errno));
347 dctx = Curl_digest_init(digest_def->dparams);
348 result = malloc(digest_def->dparams->digest_resultlen);
350 unsigned char buf[4096];
351 ssize_t len = read(fd, buf, sizeof(buf));
356 fprintf(error, "Could not read file %s: %s\n", filename,
358 Curl_digest_final(dctx, result);
362 Curl_digest_update(dctx, buf, (unsigned int)len);
364 Curl_digest_final(dctx, result);
366 for(i = 0; i < digest_def->dparams->digest_resultlen; ++i) {
367 if(hex_to_uint(&hex_hash[i*2]) != result[i]) {
377 int metalink_check_hash(struct Configurable *config,
378 metalinkfile *mlfile,
379 const char *filename)
381 metalink_checksum *chksum;
382 const metalink_digest_alias *digest_alias;
384 if(mlfile->checksum == NULL) {
387 for(digest_alias = digest_aliases; digest_alias->alias_name;
389 for(chksum = mlfile->checksum; chksum; chksum = chksum->next) {
390 if(Curl_raw_equal(digest_alias->alias_name, chksum->hash_name) &&
391 strlen(chksum->hash_value) ==
392 digest_alias->digest_def->dparams->digest_resultlen*2) {
400 if(!digest_alias->alias_name) {
401 fprintf(config->errors, "No supported checksum in Metalink file\n");
404 rv = check_hash(filename, digest_alias->digest_def,
405 chksum->hash_value, config->errors);
407 fprintf(config->errors, "Checksum matched\n");
410 fprintf(config->errors, "Checksum didn't match\n");
415 #endif /* METALINK_HASH_CHECK */
417 #ifdef HAVE_LIBMETALINK
419 static metalink_checksum *new_metalink_checksum(const char *hash_name,
420 const char *hash_value)
422 metalink_checksum *chksum;
423 chksum = malloc(sizeof(metalink_checksum));
425 chksum->hash_name = strdup(hash_name);
426 chksum->hash_value = strdup(hash_value);
430 static metalink_resource *new_metalink_resource(const char *url)
432 metalink_resource *res;
433 res = malloc(sizeof(metalink_resource));
435 res->url = strdup(url);
439 static metalinkfile *new_metalinkfile(metalink_file_t *fileinfo)
442 f = (metalinkfile*)malloc(sizeof(metalinkfile));
444 f->filename = strdup(fileinfo->name);
447 if(fileinfo->checksums) {
448 metalink_checksum_t **p;
449 metalink_checksum root, *tail;
452 for(p = fileinfo->checksums; *p; ++p) {
453 metalink_checksum *chksum;
454 chksum = new_metalink_checksum((*p)->type, (*p)->hash);
458 f->checksum = root.next;
460 if(fileinfo->resources) {
461 metalink_resource_t **p;
462 metalink_resource root, *tail;
465 for(p = fileinfo->resources; *p; ++p) {
466 metalink_resource *res;
467 res = new_metalink_resource((*p)->url);
471 f->resource = root.next;
476 int parse_metalink(struct Configurable *config, const char *infile)
479 metalink_t* metalink;
480 metalink_file_t **files;
482 r = metalink_parse_file(infile, &metalink);
486 if(metalink->files == NULL) {
487 fprintf(config->errors, "\nMetalink does not contain any file.\n");
488 metalink_delete(metalink);
491 for(files = metalink->files; *files; ++files) {
493 /* Skip an entry which has no resource. */
494 if(!(*files)->resources) {
495 fprintf(config->errors, "\nFile %s does not have any resource.\n",
499 if(config->url_get ||
500 ((config->url_get = config->url_list) != NULL)) {
501 /* there's a node here, if it already is filled-in continue to
502 find an "empty" node */
503 while(config->url_get && (config->url_get->flags & GETOUT_URL))
504 config->url_get = config->url_get->next;
507 /* now there might or might not be an available node to fill in! */
511 url = config->url_get;
513 /* there was no free node, create one! */
514 url = new_getout(config);
517 metalinkfile *mlfile;
518 mlfile = new_metalinkfile(*files);
520 /* Set name as url */
521 GetStr(&url->url, mlfile->filename);
523 /* set flag metalink here */
524 url->flags |= GETOUT_URL | GETOUT_METALINK;
526 if(config->metalinkfile_list) {
527 config->metalinkfile_last->next = mlfile;
528 config->metalinkfile_last = mlfile;
531 config->metalinkfile_list = config->metalinkfile_last = mlfile;
535 metalink_delete(metalink);
539 #endif /* HAVE_LIBMETALINK */
542 * Returns nonzero if content_type includes mediatype.
544 static int check_content_type(const char *content_type, const char *media_type)
546 const char *ptr = content_type;
547 size_t media_type_len = strlen(media_type);
548 for(; *ptr && (*ptr == ' ' || *ptr == '\t'); ++ptr);
552 return Curl_raw_nequal(ptr, media_type, media_type_len) &&
553 (*(ptr+media_type_len) == '\0' || *(ptr+media_type_len) == ' ' ||
554 *(ptr+media_type_len) == '\t' || *(ptr+media_type_len) == ';');
557 int check_metalink_content_type(const char *content_type)
559 return check_content_type(content_type, "application/metalink+xml");
562 int count_next_metalink_resource(metalinkfile *mlfile)
565 metalink_resource *res;
566 for(res = mlfile->resource; res; res = res->next, ++count);
570 static void delete_metalink_checksum(metalink_checksum *chksum)
575 Curl_safefree(chksum->hash_value);
576 Curl_safefree(chksum->hash_name);
577 Curl_safefree(chksum);
580 static void delete_metalink_resource(metalink_resource *res)
585 Curl_safefree(res->url);
589 static void delete_metalinkfile(metalinkfile *mlfile)
591 metalink_checksum *mc;
592 metalink_resource *res;
596 Curl_safefree(mlfile->filename);
597 for(mc = mlfile->checksum; mc;) {
598 metalink_checksum *next;
600 delete_metalink_checksum(mc);
603 for(res = mlfile->resource; res;) {
604 metalink_resource *next;
606 delete_metalink_resource(res);
609 Curl_safefree(mlfile);
612 void clean_metalink(struct Configurable *config)
614 while(config->metalinkfile_list) {
615 metalinkfile *mlfile = config->metalinkfile_list;
616 config->metalinkfile_list = config->metalinkfile_list->next;
617 delete_metalinkfile(mlfile);
619 config->metalinkfile_last = 0;