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"
36 #include "tool_metalink.h"
37 #include "tool_getparam.h"
38 #include "tool_paramhlp.h"
40 #include "memdebug.h" /* keep this as LAST include */
42 /* Copied from tool_getparam.c */
43 #define GetStr(str,val) do { \
49 *(str) = strdup((val)); \
51 return PARAM_NO_MEM; \
54 #ifdef USE_GNUTLS_NETTLE
56 #include <nettle/md5.h>
57 #include <nettle/sha.h>
59 typedef struct md5_ctx MD5_CTX;
61 static void MD5_Init(MD5_CTX * ctx)
66 static void MD5_Update(MD5_CTX * ctx,
67 const unsigned char * input,
68 unsigned int inputLen)
70 md5_update(ctx, inputLen, input);
73 static void MD5_Final(unsigned char digest[16], MD5_CTX * ctx)
75 md5_digest(ctx, 16, digest);
78 typedef struct sha1_ctx SHA_CTX;
80 static void SHA1_Init(SHA_CTX *ctx)
85 static void SHA1_Update(SHA_CTX *ctx,
86 const unsigned char * input,
87 unsigned int inputLen)
89 sha1_update(ctx, inputLen, input);
92 static void SHA1_Final(unsigned char digest[20], SHA_CTX * ctx)
94 sha1_digest(ctx, 20, digest);
97 typedef struct sha256_ctx SHA256_CTX;
99 static void SHA256_Init(SHA256_CTX *ctx)
104 static void SHA256_Update(SHA256_CTX *ctx,
105 const unsigned char * input,
106 unsigned int inputLen)
108 sha256_update(ctx, inputLen, input);
111 static void SHA256_Final(unsigned char digest[32], SHA256_CTX * ctx)
113 sha256_digest(ctx, 32, digest);
122 typedef gcry_md_hd_t MD5_CTX;
124 static void MD5_Init(MD5_CTX * ctx)
126 gcry_md_open(ctx, GCRY_MD_MD5, 0);
129 static void MD5_Update(MD5_CTX * ctx,
130 const unsigned char * input,
131 unsigned int inputLen)
133 gcry_md_write(*ctx, input, inputLen);
136 static void MD5_Final(unsigned char digest[16], MD5_CTX * ctx)
138 memcpy(digest, gcry_md_read(*ctx, 0), 16);
142 typedef gcry_md_hd_t SHA_CTX;
144 static void SHA1_Init(SHA_CTX * ctx)
146 gcry_md_open(ctx, GCRY_MD_SHA1, 0);
149 static void SHA1_Update(SHA_CTX * ctx,
150 const unsigned char * input,
151 unsigned int inputLen)
153 gcry_md_write(*ctx, input, inputLen);
156 static void SHA1_Final(unsigned char digest[20], SHA_CTX * ctx)
158 memcpy(digest, gcry_md_read(*ctx, 0), 20);
162 typedef gcry_md_hd_t SHA256_CTX;
164 static void SHA256_Init(SHA256_CTX * ctx)
166 gcry_md_open(ctx, GCRY_MD_SHA256, 0);
169 static void SHA256_Update(SHA256_CTX * ctx,
170 const unsigned char * input,
171 unsigned int inputLen)
173 gcry_md_write(*ctx, input, inputLen);
176 static void SHA256_Final(unsigned char digest[32], SHA256_CTX * ctx)
178 memcpy(digest, gcry_md_read(*ctx, 0), 32);
187 # include <openssl/md5.h>
188 # include <openssl/sha.h>
190 /* TODO What to do if USE_OPENSSL is undefined? */
193 #else /* USE_SSLEAY */
195 /* TODO hash functions for other libraries here */
197 #endif /* USE_SSLEAY */
199 #endif /* USE_GNUTLS */
201 #endif /* USE_GNUTLS_NETTLE */
203 const digest_params MD5_DIGEST_PARAMS[] = {
205 (Curl_digest_init_func) MD5_Init,
206 (Curl_digest_update_func) MD5_Update,
207 (Curl_digest_final_func) MD5_Final,
213 const digest_params SHA1_DIGEST_PARAMS[] = {
215 (Curl_digest_init_func) SHA1_Init,
216 (Curl_digest_update_func) SHA1_Update,
217 (Curl_digest_final_func) SHA1_Final,
223 const digest_params SHA256_DIGEST_PARAMS[] = {
225 (Curl_digest_init_func) SHA256_Init,
226 (Curl_digest_update_func) SHA256_Update,
227 (Curl_digest_final_func) SHA256_Final,
233 digest_context *Curl_digest_init(const digest_params *dparams)
235 digest_context *ctxt;
237 /* Create digest context */
238 ctxt = malloc(sizeof *ctxt);
243 ctxt->digest_hashctx = malloc(dparams->digest_ctxtsize);
245 if(!ctxt->digest_hashctx) {
250 ctxt->digest_hash = dparams;
252 dparams->digest_init(ctxt->digest_hashctx);
257 int Curl_digest_update(digest_context *context,
258 const unsigned char *data,
261 (*context->digest_hash->digest_update)(context->digest_hashctx, data, len);
266 int Curl_digest_final(digest_context *context, unsigned char *result)
268 (*context->digest_hash->digest_final)(result, context->digest_hashctx);
270 free(context->digest_hashctx);
276 struct metalinkfile *new_metalinkfile(metalink_file_t *metalinkfile) {
277 struct metalinkfile *f;
278 f = (struct metalinkfile*)malloc(sizeof(struct metalinkfile));
279 f->file = metalinkfile;
284 struct metalink *new_metalink(metalink_t *metalink) {
286 ml = (struct metalink*)malloc(sizeof(struct metalink));
287 ml->metalink = metalink;
292 int count_next_metalink_resource(struct metalinkfile *mlfile)
295 metalink_resource_t **mlres;
296 for(mlres = mlfile->file->resources; *mlres; ++mlres, ++count);
300 void clean_metalink(struct Configurable *config)
302 while(config->metalinkfile_list) {
303 struct metalinkfile *mlfile = config->metalinkfile_list;
304 config->metalinkfile_list = config->metalinkfile_list->next;
305 Curl_safefree(mlfile);
307 config->metalinkfile_last = 0;
308 while(config->metalink_list) {
309 struct metalink *ml = config->metalink_list;
310 config->metalink_list = config->metalink_list->next;
311 metalink_delete(ml->metalink);
314 config->metalink_last = 0;
317 int parse_metalink(struct Configurable *config, const char *infile)
320 metalink_t* metalink;
321 metalink_file_t **files;
324 r = metalink_parse_file(infile, &metalink);
329 if(metalink->files == NULL) {
330 fprintf(config->errors, "\nMetalink does not contain any file.\n");
333 ml = new_metalink(metalink);
335 if(config->metalink_list) {
336 config->metalink_last->next = ml;
337 config->metalink_last = ml;
340 config->metalink_list = config->metalink_last = ml;
343 for(files = metalink->files; *files; ++files) {
345 /* Skip an entry which has no resource. */
346 if(!(*files)->resources) {
347 fprintf(config->errors, "\nFile %s does not have any resource.\n",
351 if(config->url_get ||
352 ((config->url_get = config->url_list) != NULL)) {
353 /* there's a node here, if it already is filled-in continue to
354 find an "empty" node */
355 while(config->url_get && (config->url_get->flags & GETOUT_URL))
356 config->url_get = config->url_get->next;
359 /* now there might or might not be an available node to fill in! */
363 url = config->url_get;
365 /* there was no free node, create one! */
366 url=new_getout(config);
369 struct metalinkfile *mlfile;
370 /* Set name as url */
371 GetStr(&url->url, (*files)->name);
373 /* set flag metalink here */
374 url->flags |= GETOUT_URL | GETOUT_METALINK;
375 mlfile = new_metalinkfile(*files);
377 if(config->metalinkfile_list) {
378 config->metalinkfile_last->next = mlfile;
379 config->metalinkfile_last = mlfile;
382 config->metalinkfile_list = config->metalinkfile_last = mlfile;
390 * Returns nonzero if content_type includes mediatype.
392 static int check_content_type(const char *content_type, const char *media_type)
394 const char *ptr = content_type;
395 size_t media_type_len = strlen(media_type);
396 for(; *ptr && (*ptr == ' ' || *ptr == '\t'); ++ptr);
400 return Curl_raw_nequal(ptr, media_type, media_type_len) &&
401 (*(ptr+media_type_len) == '\0' || *(ptr+media_type_len) == ' ' ||
402 *(ptr+media_type_len) == '\t' || *(ptr+media_type_len) == ';');
405 int check_metalink_content_type(const char *content_type)
407 return check_content_type(content_type, "application/metalink+xml");
410 static const metalink_digest_def SHA256_DIGEST_DEF[] = {
411 {"sha-256", SHA256_DIGEST_PARAMS}
414 static const metalink_digest_def SHA1_DIGEST_DEF[] = {
415 {"sha-1", SHA1_DIGEST_PARAMS}
418 static const metalink_digest_def MD5_DIGEST_DEF[] = {
419 {"md5", MD5_DIGEST_PARAMS}
423 * The alias of supported hash functions in the order by preference
424 * (basically stronger hash comes first). We included "sha-256" and
425 * "sha256". The former is the name defined in the IANA registry named
426 * "Hash Function Textual Names". The latter is widely (and
427 * historically) used in Metalink version 3.
429 static const metalink_digest_alias digest_aliases[] = {
430 {"sha-256", SHA256_DIGEST_DEF},
431 {"sha256", SHA256_DIGEST_DEF},
432 {"sha-1", SHA1_DIGEST_DEF},
433 {"sha1", SHA1_DIGEST_DEF},
434 {"md5", MD5_DIGEST_DEF},
438 static unsigned char hex_to_uint(const char *s)
442 for(i = 0; i < 2; ++i) {
443 v[i] = Curl_raw_toupper(s[i]);
444 if('0' <= v[i] && v[i] <= '9') {
447 else if('A' <= v[i] && v[i] <= 'Z') {
451 return (unsigned char)((v[0] << 4) | v[1]);
455 * Check checksum of file denoted by filename. The expected hash value
456 * is given in hex_hash which is hex-encoded string.
458 * This function returns 1 if it succeeds or one of the following
462 * Checksum didn't match.
464 * Could not open file; or could not read data from file.
466 static int check_hash(const char *filename,
467 const metalink_digest_def *digest_def,
468 const char *hex_hash, FILE *error)
470 unsigned char *result;
471 digest_context *dctx;
475 fprintf(error, "Checking %s checksum of file %s\n", digest_def->hash_name,
477 fd = open(filename, O_RDONLY);
479 fprintf(error, "Could not open file %s: %s\n", filename, strerror(errno));
482 dctx = Curl_digest_init(digest_def->dparams);
483 result = malloc(digest_def->dparams->digest_resultlen);
485 unsigned char buf[4096];
486 ssize_t len = read(fd, buf, sizeof(buf));
491 fprintf(error, "Could not read file %s: %s\n", filename,
493 Curl_digest_final(dctx, result);
497 Curl_digest_update(dctx, buf, (unsigned int)len);
499 Curl_digest_final(dctx, result);
501 for(i = 0; i < digest_def->dparams->digest_resultlen; ++i) {
502 if(hex_to_uint(&hex_hash[i*2]) != result[i]) {
512 int metalink_check_hash(struct Configurable *config,
513 struct metalinkfile *mlfile,
514 const char *filename)
516 metalink_checksum_t **checksum;
517 const metalink_digest_alias *digest_alias;
519 if(!mlfile->file->checksums) {
522 for(digest_alias = digest_aliases; digest_alias->alias_name;
524 for(checksum = mlfile->file->checksums; *checksum; ++checksum) {
525 if(Curl_raw_equal(digest_alias->alias_name, (*checksum)->type) &&
526 strlen((*checksum)->hash) ==
527 digest_alias->digest_def->dparams->digest_resultlen*2) {
535 if(!digest_alias->alias_name) {
536 fprintf(config->errors, "No supported checksum in Metalink file\n");
539 rv = check_hash(filename, digest_alias->digest_def,
540 (*checksum)->hash, config->errors);
542 fprintf(config->errors, "Checksum matched\n");
545 fprintf(config->errors, "Checksum didn't match\n");