tool_metalink: allow to handle failure of hash alg initialization

This commit is contained in:
Kamil Dudka 2012-10-30 14:21:54 +01:00
parent cf75a64651
commit dca8ae5f02
2 changed files with 35 additions and 13 deletions

View File

@ -112,9 +112,10 @@ struct win32_crypto_hash {
#ifdef USE_GNUTLS_NETTLE #ifdef USE_GNUTLS_NETTLE
static void MD5_Init(MD5_CTX *ctx) static int MD5_Init(MD5_CTX *ctx)
{ {
md5_init(ctx); md5_init(ctx);
return 0;
} }
static void MD5_Update(MD5_CTX *ctx, static void MD5_Update(MD5_CTX *ctx,
@ -129,9 +130,10 @@ static void MD5_Final(unsigned char digest[16], MD5_CTX *ctx)
md5_digest(ctx, 16, digest); md5_digest(ctx, 16, digest);
} }
static void SHA1_Init(SHA_CTX *ctx) static int SHA1_Init(SHA_CTX *ctx)
{ {
sha1_init(ctx); sha1_init(ctx);
return 0;
} }
static void SHA1_Update(SHA_CTX *ctx, static void SHA1_Update(SHA_CTX *ctx,
@ -146,9 +148,10 @@ static void SHA1_Final(unsigned char digest[20], SHA_CTX *ctx)
sha1_digest(ctx, 20, digest); sha1_digest(ctx, 20, digest);
} }
static void SHA256_Init(SHA256_CTX *ctx) static int SHA256_Init(SHA256_CTX *ctx)
{ {
sha256_init(ctx); sha256_init(ctx);
return 0;
} }
static void SHA256_Update(SHA256_CTX *ctx, static void SHA256_Update(SHA256_CTX *ctx,
@ -165,9 +168,10 @@ static void SHA256_Final(unsigned char digest[32], SHA256_CTX *ctx)
#elif defined(USE_GNUTLS) #elif defined(USE_GNUTLS)
static void MD5_Init(MD5_CTX *ctx) static int MD5_Init(MD5_CTX *ctx)
{ {
gcry_md_open(ctx, GCRY_MD_MD5, 0); gcry_md_open(ctx, GCRY_MD_MD5, 0);
return 0;
} }
static void MD5_Update(MD5_CTX *ctx, static void MD5_Update(MD5_CTX *ctx,
@ -183,9 +187,10 @@ static void MD5_Final(unsigned char digest[16], MD5_CTX *ctx)
gcry_md_close(*ctx); gcry_md_close(*ctx);
} }
static void SHA1_Init(SHA_CTX *ctx) static int SHA1_Init(SHA_CTX *ctx)
{ {
gcry_md_open(ctx, GCRY_MD_SHA1, 0); gcry_md_open(ctx, GCRY_MD_SHA1, 0);
return 0;
} }
static void SHA1_Update(SHA_CTX *ctx, static void SHA1_Update(SHA_CTX *ctx,
@ -201,9 +206,10 @@ static void SHA1_Final(unsigned char digest[20], SHA_CTX *ctx)
gcry_md_close(*ctx); gcry_md_close(*ctx);
} }
static void SHA256_Init(SHA256_CTX *ctx) static int SHA256_Init(SHA256_CTX *ctx)
{ {
gcry_md_open(ctx, GCRY_MD_SHA256, 0); gcry_md_open(ctx, GCRY_MD_SHA256, 0);
return 0;
} }
static void SHA256_Update(SHA256_CTX *ctx, static void SHA256_Update(SHA256_CTX *ctx,
@ -235,12 +241,13 @@ static void win32_crypto_final(struct win32_crypto_hash *ctx,
CryptReleaseContext(ctx->hCryptProv, 0); CryptReleaseContext(ctx->hCryptProv, 0);
} }
static void MD5_Init(MD5_CTX *ctx) static int MD5_Init(MD5_CTX *ctx)
{ {
if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL, if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL,
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
CryptCreateHash(ctx->hCryptProv, CALG_MD5, 0, 0, &ctx->hHash); CryptCreateHash(ctx->hCryptProv, CALG_MD5, 0, 0, &ctx->hHash);
} }
return 0;
} }
static void MD5_Update(MD5_CTX *ctx, static void MD5_Update(MD5_CTX *ctx,
@ -255,12 +262,13 @@ static void MD5_Final(unsigned char digest[16], MD5_CTX *ctx)
win32_crypto_final(ctx, digest, 16); win32_crypto_final(ctx, digest, 16);
} }
static void SHA1_Init(SHA_CTX *ctx) static int SHA1_Init(SHA_CTX *ctx)
{ {
if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL, if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL,
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
CryptCreateHash(ctx->hCryptProv, CALG_SHA1, 0, 0, &ctx->hHash); CryptCreateHash(ctx->hCryptProv, CALG_SHA1, 0, 0, &ctx->hHash);
} }
return 0;
} }
static void SHA1_Update(SHA_CTX *ctx, static void SHA1_Update(SHA_CTX *ctx,
@ -275,12 +283,13 @@ static void SHA1_Final(unsigned char digest[20], SHA_CTX *ctx)
win32_crypto_final(ctx, digest, 20); win32_crypto_final(ctx, digest, 20);
} }
static void SHA256_Init(SHA256_CTX *ctx) static int SHA256_Init(SHA256_CTX *ctx)
{ {
if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL, if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL,
PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) { PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
CryptCreateHash(ctx->hCryptProv, CALG_SHA_256, 0, 0, &ctx->hHash); CryptCreateHash(ctx->hCryptProv, CALG_SHA_256, 0, 0, &ctx->hHash);
} }
return 0;
} }
static void SHA256_Update(SHA256_CTX *ctx, static void SHA256_Update(SHA256_CTX *ctx,
@ -374,7 +383,10 @@ digest_context *Curl_digest_init(const digest_params *dparams)
ctxt->digest_hash = dparams; ctxt->digest_hash = dparams;
dparams->digest_init(ctxt->digest_hashctx); if(dparams->digest_init(ctxt->digest_hashctx) != 0) {
free(ctxt);
return NULL;
}
return ctxt; return ctxt;
} }
@ -425,6 +437,8 @@ static unsigned char hex_to_uint(const char *s)
* Checksum didn't match. * Checksum didn't match.
* -1: * -1:
* Could not open file; or could not read data from file. * Could not open file; or could not read data from file.
* -2:
* Hash algorithm not available.
*/ */
static int check_hash(const char *filename, static int check_hash(const char *filename,
const metalink_digest_def *digest_def, const metalink_digest_def *digest_def,
@ -446,7 +460,15 @@ static int check_hash(const char *filename,
digest_def->hash_name, strerror(errno)); digest_def->hash_name, strerror(errno));
return -1; return -1;
} }
dctx = Curl_digest_init(digest_def->dparams); dctx = Curl_digest_init(digest_def->dparams);
if(!dctx) {
fprintf(error, "Metalink: validating (%s) [%s] FAILED (%s)\n", filename,
digest_def->hash_name, "failed to initialize hash algorithm");
close(fd);
return -2;
}
result = malloc(digest_def->dparams->digest_resultlen); result = malloc(digest_def->dparams->digest_resultlen);
while(1) { while(1) {
unsigned char buf[4096]; unsigned char buf[4096];

View File

@ -23,7 +23,7 @@
***************************************************************************/ ***************************************************************************/
#include "tool_setup.h" #include "tool_setup.h"
typedef void (* Curl_digest_init_func)(void *context); typedef int (* Curl_digest_init_func)(void *context);
typedef void (* Curl_digest_update_func)(void *context, typedef void (* Curl_digest_update_func)(void *context,
const unsigned char *data, const unsigned char *data,
unsigned int len); unsigned int len);
@ -137,8 +137,8 @@ int check_metalink_content_type(const char *content_type);
* -1: * -1:
* Could not open file; or could not read data from file. * Could not open file; or could not read data from file.
* -2: * -2:
* No checksum in Metalink supported; or Metalink does not contain * No checksum in Metalink supported, hash algorithm not available, or
* checksum. * Metalink does not contain checksum.
*/ */
int metalink_check_hash(struct Configurable *config, int metalink_check_hash(struct Configurable *config,
metalinkfile *mlfile, metalinkfile *mlfile,