@@ -27,13 +27,15 @@ void _openssl_verify_async_cancel(struct acquire_handle *handle);
2727#endif
2828
2929#ifdef LIBACQUIRE_IMPLEMENTATION
30- #ifndef ACQUIRE_OPENSSL_IMPL_
31- #define ACQUIRE_OPENSSL_IMPL_
3230
3331#if (defined(LIBACQUIRE_USE_COMMON_CRYPTO ) && LIBACQUIRE_USE_COMMON_CRYPTO || \
3432 defined(LIBACQUIRE_USE_OPENSSL ) && LIBACQUIRE_USE_OPENSSL || \
3533 defined(LIBACQUIRE_USE_LIBRESSL ) && LIBACQUIRE_USE_LIBRESSL )
3634
35+ #ifndef EVP_MAX_MD_SIZE
36+ #define EVP_MAX_MD_SIZE 64
37+ #endif /* !EVP_MAX_MD_SIZE */
38+
3739#include "acquire_handle.h"
3840#include <errno.h>
3941#include <stdlib.h>
@@ -97,7 +99,8 @@ int _openssl_verify_async_start(struct acquire_handle *handle,
9799 }
98100 be = (struct openssl_backend * )calloc (1 , sizeof (struct openssl_backend ));
99101 if (!be ) {
100- acquire_handle_set_error (handle , ACQUIRE_ERROR_OUT_OF_MEMORY , "openssl" );
102+ acquire_handle_set_error (handle , ACQUIRE_ERROR_OUT_OF_MEMORY ,
103+ "openssl backend memory allocation failed" );
101104 return -1 ;
102105 }
103106 be -> file = fopen (filepath , "rb" );
@@ -108,7 +111,7 @@ int _openssl_verify_async_start(struct acquire_handle *handle,
108111 return -1 ;
109112 }
110113
111- #if defined(LIBACQUIRE_USE_COMMON_CRYPTO ) || ! LIBACQUIRE_USE_COMMON_CRYPTO
114+ #if defined(LIBACQUIRE_USE_COMMON_CRYPTO ) && LIBACQUIRE_USE_COMMON_CRYPTO
112115 be -> algorithm = algorithm ;
113116 switch (algorithm ) {
114117 case LIBACQUIRE_SHA256 :
@@ -118,15 +121,21 @@ int _openssl_verify_async_start(struct acquire_handle *handle,
118121 CC_SHA512_Init (& be -> ctx .sha512 );
119122 break ;
120123 default :
121- break ;
124+ /* Should have been caught by the switch above, but for safety: */
125+ cleanup_openssl_backend (handle );
126+ acquire_handle_set_error (
127+ handle , ACQUIRE_ERROR_UNSUPPORTED_CHECKSUM_FORMAT ,
128+ "Internal error: unsupported algorithm in CommonCrypto backend" );
129+ return -1 ;
122130 }
123131#else
124132 const EVP_MD * md =
125133 (algorithm == LIBACQUIRE_SHA256 ) ? EVP_sha256 () : EVP_sha512 ();
126134 be -> ctx = EVP_MD_CTX_new ();
127135 if (!be -> ctx || (1 != EVP_DigestInit_ex (be -> ctx , md , NULL ))) {
128136 cleanup_openssl_backend (handle );
129- acquire_handle_set_error (handle , ACQUIRE_ERROR_UNKNOWN , "EVP init failed" );
137+ acquire_handle_set_error (handle , ACQUIRE_ERROR_UNKNOWN ,
138+ "EVP_DigestInit_ex failed" );
130139 return -1 ;
131140 }
132141#endif
@@ -146,7 +155,8 @@ enum acquire_status _openssl_verify_async_poll(struct acquire_handle *handle) {
146155 if (handle -> status != ACQUIRE_IN_PROGRESS )
147156 return handle -> status ;
148157 if (handle -> cancel_flag ) {
149- acquire_handle_set_error (handle , ACQUIRE_ERROR_CANCELLED , "Cancelled" );
158+ acquire_handle_set_error (handle , ACQUIRE_ERROR_CANCELLED ,
159+ "Checksum cancelled" );
150160 cleanup_openssl_backend (handle );
151161 return ACQUIRE_ERROR ;
152162 }
@@ -162,11 +172,14 @@ enum acquire_status _openssl_verify_async_poll(struct acquire_handle *handle) {
162172 CC_SHA512_Update (& be -> ctx .sha512 , buffer , (CC_LONG )bytes_read );
163173 break ;
164174 default :
175+ acquire_handle_set_error (handle , ACQUIRE_ERROR_UNKNOWN ,
176+ "Internal CC algorithm error" );
165177 break ;
166178 }
167179#else
168180 if (1 != EVP_DigestUpdate (be -> ctx , buffer , bytes_read ))
169- acquire_handle_set_error (handle , ACQUIRE_ERROR_UNKNOWN , "EVP_Update" );
181+ acquire_handle_set_error (handle , ACQUIRE_ERROR_UNKNOWN ,
182+ "EVP_DigestUpdate failed" );
170183#endif
171184 if (handle -> error .code == ACQUIRE_OK ) {
172185 handle -> bytes_processed += bytes_read ;
@@ -177,11 +190,11 @@ enum acquire_status _openssl_verify_async_poll(struct acquire_handle *handle) {
177190 acquire_handle_set_error (handle , ACQUIRE_ERROR_FILE_READ_FAILED , "%s" ,
178191 strerror (errno ));
179192 } else {
180- unsigned char hash [CC_SHA512_DIGEST_LENGTH ];
181- char computed_hex [130 ];
193+ unsigned char hash [EVP_MAX_MD_SIZE ];
194+ char computed_hex [EVP_MAX_MD_SIZE * 2 + 1 ];
182195 unsigned int len = 0 ;
183196 int i ;
184- #ifdef LIBACQUIRE_USE_COMMON_CRYPTO
197+ #if defined( LIBACQUIRE_USE_COMMON_CRYPTO ) && LIBACQUIRE_USE_COMMON_CRYPTO
185198 switch (be -> algorithm ) {
186199 case LIBACQUIRE_SHA256 :
187200 len = CC_SHA256_DIGEST_LENGTH ;
@@ -192,22 +205,25 @@ enum acquire_status _openssl_verify_async_poll(struct acquire_handle *handle) {
192205 CC_SHA512_Final (hash , & be -> ctx .sha512 );
193206 break ;
194207 default :
208+ acquire_handle_set_error (handle , ACQUIRE_ERROR_UNKNOWN ,
209+ "Internal CC algorithm error" );
195210 break ;
196211 }
197212#else
198213 if (1 != EVP_DigestFinal_ex (be -> ctx , hash , & len ))
199- acquire_handle_set_error (handle , ACQUIRE_ERROR_UNKNOWN , "EVP_Final" );
214+ acquire_handle_set_error (handle , ACQUIRE_ERROR_UNKNOWN ,
215+ "EVP_DigestFinal_ex failed" );
200216#endif
201217 if (handle -> error .code == ACQUIRE_OK ) {
202- for (i = 0 ; i < len ; i ++ )
218+ for (i = 0 ; ( unsigned int ) i < len ; i ++ )
203219 sprintf (computed_hex + (i * 2 ), "%02x" , hash [i ]);
204220 computed_hex [len * 2 ] = '\0' ;
205221 if (strncasecmp (computed_hex , be -> expected_hash , len * 2 ) == 0 )
206222 handle -> status = ACQUIRE_COMPLETE ;
207223 else
208224 acquire_handle_set_error (handle , ACQUIRE_ERROR_UNKNOWN ,
209- "Hash mismatch: %s != %s" , be -> expected_hash ,
210- computed_hex );
225+ "Hash mismatch: expected %s, got %s" ,
226+ be -> expected_hash , computed_hex );
211227 }
212228 }
213229 cleanup_openssl_backend (handle );
@@ -222,7 +238,6 @@ void _openssl_verify_async_cancel(struct acquire_handle *handle) {
222238 LIBACQUIRE_USE_COMMON_CRYPTO || defined(LIBACQUIRE_USE_OPENSSL) && \
223239 LIBACQUIRE_USE_OPENSSL || defined(LIBACQUIRE_USE_LIBRESSL) && \
224240 LIBACQUIRE_USE_LIBRESSL) */
225- #endif /* ACQUIRE_OPENSSL_IMPL_ */
226241#endif /* defined(LIBACQUIRE_IMPLEMENTATION) */
227242
228243#ifdef __cplusplus
0 commit comments