33
44#ifdef __cplusplus
55extern "C" {
6- #endif /* __cplusplus */
6+ #endif
77
88#include "acquire_handle.h"
99#include "libacquire_export.h"
10- #include <string.h> /* strncasecmp */
1110
12- /** Checksum algorithms supported */
13- enum Checksum {
14- LIBACQUIRE_CRC32C ,
15- LIBACQUIRE_SHA256 ,
16- LIBACQUIRE_SHA512 ,
17- LIBACQUIRE_UNSUPPORTED_CHECKSUM
18- };
11+ #include "acquire_crc32c.h"
12+ #include "acquire_librhash.h"
13+ #include "acquire_openssl.h"
14+ #include "acquire_wincrypt.h"
1915
20- /**
21- * @brief Convert string to checksum enum.
22- *
23- * Supports at least "CRC32C", "SHA256", "SHA512".
24- *
25- * @param s String of algorithm name (case-insensitive)
26- * @return Enum value or LIBACQUIRE_UNSUPPORTED_CHECKSUM if unknown.
27- */
2816extern LIBACQUIRE_EXPORT enum Checksum string2checksum (const char * s );
29-
30- /* --- Asynchronous API --- */
31-
3217extern LIBACQUIRE_EXPORT int
3318acquire_verify_async_start (struct acquire_handle * handle , const char * filepath ,
3419 enum Checksum algorithm , const char * expected_hash );
35-
3620extern LIBACQUIRE_EXPORT enum acquire_status
3721acquire_verify_async_poll (struct acquire_handle * handle );
38-
3922extern LIBACQUIRE_EXPORT void
4023acquire_verify_async_cancel (struct acquire_handle * handle );
41-
42- /* --- Synchronous API --- */
43-
4424extern LIBACQUIRE_EXPORT int acquire_verify_sync (struct acquire_handle * handle ,
4525 const char * filepath ,
4626 enum Checksum algorithm ,
4727 const char * expected_hash );
4828
49- /* --- Backend internal function declarations --- */
50-
51- #if defined(LIBACQUIRE_USE_COMMON_CRYPTO ) || \
52- defined(LIBACQUIRE_USE_OPENSSL ) || defined(LIBACQUIRE_USE_LIBRESSL )
53- int _openssl_verify_async_start (struct acquire_handle * handle ,
54- const char * filepath , enum Checksum algorithm ,
55- const char * expected_hash );
56- enum acquire_status _openssl_verify_async_poll (struct acquire_handle * handle );
57- void _openssl_verify_async_cancel (struct acquire_handle * handle );
58- #endif
59-
60- #ifdef LIBACQUIRE_USE_WINCRYPT
61- int _wincrypt_verify_async_start (struct acquire_handle * handle ,
62- const char * filepath , enum Checksum algorithm ,
63- const char * expected_hash );
64- enum acquire_status _wincrypt_verify_async_poll (struct acquire_handle * handle );
65- void _wincrypt_verify_async_cancel (struct acquire_handle * handle );
66- #endif
67-
68- #ifdef LIBACQUIRE_USE_LIBRHASH
69- int _librhash_verify_async_start (struct acquire_handle * handle ,
70- const char * filepath , enum Checksum algorithm ,
71- const char * expected_hash );
72- enum acquire_status _librhash_verify_async_poll (struct acquire_handle * handle );
73- void _librhash_verify_async_cancel (struct acquire_handle * handle );
74- #endif
29+ #if defined(LIBACQUIRE_IMPLEMENTATION )
30+ #ifndef ACQUIRE_CHECKSUMS_IMPL_
31+ #define ACQUIRE_CHECKSUMS_IMPL_
32+ #include <string.h>
7533
76- #ifdef LIBACQUIRE_USE_CRC32C
77- int _crc32c_verify_async_start (struct acquire_handle * handle ,
78- const char * filepath , enum Checksum algorithm ,
79- const char * expected_hash );
80- enum acquire_status _crc32c_verify_async_poll (struct acquire_handle * handle );
81- void _crc32c_verify_async_cancel (struct acquire_handle * handle );
82- #endif
83-
84- #if defined(LIBACQUIRE_IMPLEMENTATION ) && defined(LIBACQUIRE_CHECKSUMS_IMPL )
34+ enum Checksum string2checksum (const char * const s ) {
35+ if (s == NULL )
36+ return LIBACQUIRE_UNSUPPORTED_CHECKSUM ;
37+ if (strcasecmp (s , "crc32c" ) == 0 )
38+ return LIBACQUIRE_CRC32C ;
39+ if (strcasecmp (s , "sha256" ) == 0 )
40+ return LIBACQUIRE_SHA256 ;
41+ if (strcasecmp (s , "sha512" ) == 0 )
42+ return LIBACQUIRE_SHA512 ;
43+ return LIBACQUIRE_UNSUPPORTED_CHECKSUM ;
44+ }
8545
8646int acquire_verify_async_start (struct acquire_handle * handle ,
8747 const char * filepath , enum Checksum algorithm ,
@@ -92,120 +52,104 @@ int acquire_verify_async_start(struct acquire_handle *handle,
9252 "Invalid arguments" );
9353 return -1 ;
9454 }
95-
96- /* duplicate labels so can't have a 'global' switch/case */
97- #if defined( LIBACQUIRE_USE_COMMON_CRYPTO ) || defined( LIBACQUIRE_USE_OPENSSL ) || defined( LIBACQUIRE_USE_LIBRESSL )
98- switch ( algorithm ) {
99- case LIBACQUIRE_SHA256 :
100- case LIBACQUIRE_SHA512 :
101- return _openssl_verify_async_start ( handle , filepath , algorithm ,
102- expected_hash ) ;
103- default : break ;
55+ handle -> active_backend = ACQUIRE_BACKEND_NONE ;
56+ handle -> status = ACQUIRE_IDLE ;
57+ handle -> error . code = ACQUIRE_OK ;
58+ handle -> error . message [ 0 ] = '\0' ;
59+ #if defined( LIBACQUIRE_USE_LIBRHASH )
60+ if ( _librhash_verify_async_start ( handle , filepath , algorithm ,
61+ expected_hash ) == 0 ) {
62+ handle -> active_backend = ACQUIRE_BACKEND_CHECKSUM_LIBRHASH ;
63+ return 0 ;
10464 }
105- #endif /* defined(LIBACQUIRE_USE_COMMON_CRYPTO) || defined(LIBACQUIRE_USE_OPENSSL) || defined(LIBACQUIRE_USE_LIBRESSL) */
106- #ifdef LIBACQUIRE_USE_WINCRYPT
107- switch (algorithm ) {
108- case LIBACQUIRE_SHA256 :
109- case LIBACQUIRE_SHA512 :
110- return _wincrypt_verify_async_start (handle , filepath , algorithm ,
111- expected_hash );
112- default : break ;
65+ if (handle -> error .code != ACQUIRE_OK )
66+ return -1 ;
67+ #endif
68+ #if defined(LIBACQUIRE_USE_COMMON_CRYPTO ) || \
69+ defined(LIBACQUIRE_USE_OPENSSL ) || defined(LIBACQUIRE_USE_LIBRESSL )
70+ if (_openssl_verify_async_start (handle , filepath , algorithm , expected_hash ) ==
71+ 0 ) {
72+ handle -> active_backend = ACQUIRE_BACKEND_CHECKSUM_OPENSSL ;
73+ return 0 ;
11374 }
114- #endif /* LIBACQUIRE_USE_WINCRYPT */
115- #ifdef LIBACQUIRE_USE_LIBRHASH
116- switch (algorithm ) {
117- case LIBACQUIRE_CRC32C :
118- case LIBACQUIRE_SHA256 :
119- case LIBACQUIRE_SHA512 :
120- return _librhash_verify_async_start (handle , filepath , algorithm ,
121- expected_hash );
122- default :
123- break ;
75+ if (handle -> error .code != ACQUIRE_OK )
76+ return -1 ;
77+ #endif
78+ #if defined(LIBACQUIRE_USE_WINCRYPT )
79+ if (_wincrypt_verify_async_start (handle , filepath , algorithm ,
80+ expected_hash ) == 0 ) {
81+ handle -> active_backend = ACQUIRE_BACKEND_CHECKSUM_WINCRYPT ;
82+ return 0 ;
12483 }
125- #endif /* LIBACQUIRE_USE_LIBRHASH */
126- #ifdef LIBACQUIRE_USE_CRC32C
127- switch (algorithm ) {
128- case LIBACQUIRE_CRC32C :
129- return _crc32c_verify_async_start (handle , filepath , algorithm ,
130- expected_hash );
131- default : break ;
84+ if (handle -> error .code != ACQUIRE_OK )
85+ return -1 ;
86+ #endif
87+ #if defined(LIBACQUIRE_USE_CRC32C )
88+ if (_crc32c_verify_async_start (handle , filepath , algorithm , expected_hash ) ==
89+ 0 ) {
90+ handle -> active_backend = ACQUIRE_BACKEND_CHECKSUM_CRC32C ;
91+ return 0 ;
13292 }
133- #endif /* LIBACQUIRE_USE_CRC32C */
134-
135- acquire_handle_set_error (
136- handle , ACQUIRE_ERROR_UNSUPPORTED_ARCHIVE_FORMAT ,
137- "Unsupported checksum algorithm or no backend for it " );
93+ if ( handle -> error . code != ACQUIRE_OK )
94+ return -1 ;
95+ #endif
96+ acquire_handle_set_error ( handle , ACQUIRE_ERROR_UNSUPPORTED_ARCHIVE_FORMAT ,
97+ "Unsupported checksum or no backend" );
13898 return -1 ;
13999}
140100
141101enum acquire_status acquire_verify_async_poll (struct acquire_handle * handle ) {
142- if (!handle || ! handle -> backend_handle )
102+ if (!handle )
143103 return ACQUIRE_ERROR ;
144-
104+ switch (handle -> active_backend ) {
105+ #if defined(LIBACQUIRE_USE_LIBRHASH )
106+ case ACQUIRE_BACKEND_CHECKSUM_LIBRHASH :
107+ return _librhash_verify_async_poll (handle );
108+ #endif
145109#if defined(LIBACQUIRE_USE_COMMON_CRYPTO ) || \
146110 defined(LIBACQUIRE_USE_OPENSSL ) || defined(LIBACQUIRE_USE_LIBRESSL )
147- return _openssl_verify_async_poll (handle );
148- #elif defined(LIBACQUIRE_USE_WINCRYPT )
149- return _wincrypt_verify_async_poll (handle );
150- #elif defined(LIBACQUIRE_USE_LIBRHASH )
151- return _librhash_verify_async_poll (handle );
152- #elif defined(LIBACQUIRE_USE_CRC32C )
153- return _crc32c_verify_async_poll (handle );
154- #else
155- acquire_handle_set_error (handle , ACQUIRE_ERROR_UNKNOWN ,
156- "No checksum backend compiled" );
157- return ACQUIRE_ERROR ;
111+ case ACQUIRE_BACKEND_CHECKSUM_OPENSSL :
112+ return _openssl_verify_async_poll (handle );
113+ #endif
114+ #if defined(LIBACQUIRE_USE_WINCRYPT )
115+ case ACQUIRE_BACKEND_CHECKSUM_WINCRYPT :
116+ return _wincrypt_verify_async_poll (handle );
158117#endif
118+ #if defined(LIBACQUIRE_USE_CRC32C )
119+ case ACQUIRE_BACKEND_CHECKSUM_CRC32C :
120+ return _crc32c_verify_async_poll (handle );
121+ #endif
122+ default :
123+ if (handle -> status != ACQUIRE_IN_PROGRESS )
124+ return handle -> status ;
125+ acquire_handle_set_error (handle , ACQUIRE_ERROR_UNKNOWN ,
126+ "No active backend" );
127+ return ACQUIRE_ERROR ;
128+ }
159129}
160130
161131void acquire_verify_async_cancel (struct acquire_handle * handle ) {
162- if (!handle || !handle -> backend_handle )
163- return ;
164-
165- #if defined(LIBACQUIRE_USE_COMMON_CRYPTO ) || \
166- defined(LIBACQUIRE_USE_OPENSSL ) || defined(LIBACQUIRE_USE_LIBRESSL )
167- _openssl_verify_async_cancel (handle );
168- #elif defined(LIBACQUIRE_USE_WINCRYPT )
169- _wincrypt_verify_async_cancel (handle );
170- #elif defined(LIBACQUIRE_USE_LIBRHASH )
171- _librhash_verify_async_cancel (handle );
172- #elif defined(LIBACQUIRE_USE_CRC32C )
173- _crc32c_verify_async_cancel (handle );
174- #endif
132+ if (handle )
133+ handle -> cancel_flag = 1 ;
175134}
176135
177136int acquire_verify_sync (struct acquire_handle * handle , const char * filepath ,
178137 enum Checksum algorithm , const char * expected_hash ) {
179138 enum acquire_status status ;
180- if (!handle || !filepath || !expected_hash )
181- return -1 ;
182-
183139 if (acquire_verify_async_start (handle , filepath , algorithm , expected_hash ) !=
184140 0 )
185141 return -1 ;
186-
187- while ((status = acquire_verify_async_poll (handle )) == ACQUIRE_IN_PROGRESS )
188- ;
189-
142+ do {
143+ status = acquire_verify_async_poll (handle );
144+ } while (status == ACQUIRE_IN_PROGRESS );
190145 return (status == ACQUIRE_COMPLETE ) ? 0 : -1 ;
191146}
192147
193- enum Checksum string2checksum (const char * const s ) {
194- if (s == NULL )
195- return LIBACQUIRE_UNSUPPORTED_CHECKSUM ;
196- if (strncasecmp (s , "CRC32C" , 6 ) == 0 )
197- return LIBACQUIRE_CRC32C ;
198- if (strncasecmp (s , "SHA256" , 6 ) == 0 )
199- return LIBACQUIRE_SHA256 ;
200- if (strncasecmp (s , "SHA512" , 6 ) == 0 )
201- return LIBACQUIRE_SHA512 ;
202- return LIBACQUIRE_UNSUPPORTED_CHECKSUM ;
203- }
204-
205- #endif /* LIBACQUIRE_IMPLEMENTATION && LIBACQUIRE_CHECKSUMS_IMPL */
148+ #endif /* ACQUIRE_CHECKSUMS_IMPL_ */
149+ #endif /* defined(LIBACQUIRE_IMPLEMENTATION) */
206150
207151#ifdef __cplusplus
208152}
209- #endif /* __cplusplus */
153+ #endif
210154
211- #endif /* LIBACQUIRE_ACQUIRE_CHECKSUMS_H */
155+ #endif /* ! LIBACQUIRE_ACQUIRE_CHECKSUMS_H */
0 commit comments