New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
check the return value of EVP_MD_CTX_create(EVP_MD_CTX_new) #8133
Changes from all commits
3c42f1d
acdf958
c5d892a
cf03812
e9b8fe8
eda32a3
bb2fad5
3eb7861
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,10 +82,14 @@ struct sha256_ctx { | |
}; | ||
typedef struct sha256_ctx my_sha256_ctx; | ||
|
||
static void my_sha256_init(my_sha256_ctx *ctx) | ||
static CURLcode my_sha256_init(my_sha256_ctx *ctx) | ||
{ | ||
ctx->openssl_ctx = EVP_MD_CTX_create(); | ||
if(!ctx->openssl_ctx) | ||
return CURLE_OUT_OF_MEMORY; | ||
|
||
EVP_DigestInit_ex(ctx->openssl_ctx, EVP_sha256(), NULL); | ||
return CURLE_OK; | ||
} | ||
|
||
static void my_sha256_update(my_sha256_ctx *ctx, | ||
|
@@ -112,9 +116,10 @@ static void my_sha256_final(unsigned char *digest, my_sha256_ctx *ctx) | |
|
||
typedef struct sha256_ctx my_sha256_ctx; | ||
|
||
static void my_sha256_init(my_sha256_ctx *ctx) | ||
static CURLcode my_sha256_init(my_sha256_ctx *ctx) | ||
{ | ||
sha256_init(ctx); | ||
return CURLE_OK; | ||
} | ||
|
||
static void my_sha256_update(my_sha256_ctx *ctx, | ||
|
@@ -140,13 +145,14 @@ static void my_sha256_final(unsigned char *digest, my_sha256_ctx *ctx) | |
|
||
typedef mbedtls_sha256_context my_sha256_ctx; | ||
|
||
static void my_sha256_init(my_sha256_ctx *ctx) | ||
static CURLcode my_sha256_init(my_sha256_ctx *ctx) | ||
{ | ||
#if !defined(HAS_MBEDTLS_RESULT_CODE_BASED_FUNCTIONS) | ||
(void) mbedtls_sha256_starts(ctx, 0); | ||
#else | ||
(void) mbedtls_sha256_starts_ret(ctx, 0); | ||
#endif | ||
return CURLE_OK; | ||
} | ||
|
||
static void my_sha256_update(my_sha256_ctx *ctx, | ||
|
@@ -183,9 +189,10 @@ static void my_sha256_final(unsigned char *digest, my_sha256_ctx *ctx) | |
|
||
typedef CC_SHA256_CTX my_sha256_ctx; | ||
|
||
static void my_sha256_init(my_sha256_ctx *ctx) | ||
static CURLcode my_sha256_init(my_sha256_ctx *ctx) | ||
{ | ||
(void) CC_SHA256_Init(ctx); | ||
return CURLE_OK; | ||
} | ||
|
||
static void my_sha256_update(my_sha256_ctx *ctx, | ||
|
@@ -214,12 +221,14 @@ typedef struct sha256_ctx my_sha256_ctx; | |
#define CALG_SHA_256 0x0000800c | ||
#endif | ||
|
||
static void my_sha256_init(my_sha256_ctx *ctx) | ||
static CURLcode my_sha256_init(my_sha256_ctx *ctx) | ||
{ | ||
if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL, PROV_RSA_AES, | ||
CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) { | ||
CryptCreateHash(ctx->hCryptProv, CALG_SHA_256, 0, 0, &ctx->hHash); | ||
} | ||
|
||
return CURLE_OK; | ||
} | ||
|
||
static void my_sha256_update(my_sha256_ctx *ctx, | ||
|
@@ -375,7 +384,7 @@ static int sha256_compress(struct sha256_state *md, | |
} | ||
|
||
/* Initialize the hash state */ | ||
static void my_sha256_init(struct sha256_state *md) | ||
static CURLcode my_sha256_init(struct sha256_state *md) | ||
{ | ||
md->curlen = 0; | ||
md->length = 0; | ||
|
@@ -387,14 +396,16 @@ static void my_sha256_init(struct sha256_state *md) | |
md->state[5] = 0x9B05688CUL; | ||
md->state[6] = 0x1F83D9ABUL; | ||
md->state[7] = 0x5BE0CD19UL; | ||
|
||
return CURLE_OK; | ||
} | ||
|
||
/* | ||
Process a block of memory though the hash | ||
@param md The hash state | ||
@param in The data to hash | ||
@param inlen The length of the data (octets) | ||
@return CRYPT_OK if successful | ||
@return 0 if successful | ||
*/ | ||
static int my_sha256_update(struct sha256_state *md, | ||
const unsigned char *in, | ||
|
@@ -435,7 +446,7 @@ static int my_sha256_update(struct sha256_state *md, | |
Terminate the hash to get the digest | ||
@param md The hash state | ||
@param out [out] The destination of the hash (32 bytes) | ||
@return CRYPT_OK if successful | ||
@return 0 if successful | ||
*/ | ||
static int my_sha256_final(unsigned char *out, | ||
struct sha256_state *md) | ||
|
@@ -491,15 +502,22 @@ static int my_sha256_final(unsigned char *out, | |
* output [in/out] - The output buffer. | ||
* input [in] - The input data. | ||
* length [in] - The input length. | ||
* | ||
* Returns CURLE_OK on success. | ||
*/ | ||
void Curl_sha256it(unsigned char *output, const unsigned char *input, | ||
CURLcode Curl_sha256it(unsigned char *output, const unsigned char *input, | ||
const size_t length) | ||
{ | ||
CURLcode ret; | ||
my_sha256_ctx ctx; | ||
|
||
my_sha256_init(&ctx); | ||
ret = my_sha256_init(&ctx); | ||
if(ret != CURLE_OK) | ||
return ret; | ||
|
||
my_sha256_update(&ctx, input, curlx_uztoui(length)); | ||
my_sha256_final(output, &ctx); | ||
return ret; | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't comment on line 527, but the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I forgot to update it though you reminded me. I will do it after work... |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please make
Curl_sha256it
return error/success instead and check that instead of relying on the output data. Yes, that's a bigger change but is much cleaner and nicer code.