From: Michihiro NAKAJIMA Date: Thu, 29 Jan 2009 08:10:43 +0000 (-0500) Subject: Improve portability of MD5/SHA* headers and functions X-Git-Tag: v2.7.0~384 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2846d884475686eb2e2fbd040581768bba3b317f;p=thirdparty%2Flibarchive.git Improve portability of MD5/SHA* headers and functions used in mtree format. It tested on FreeBSD, NetBSD and Ubuntu. SVN-Revision: 509 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 35894db28..7b54acf02 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -183,8 +183,9 @@ CHECK_INCLUDE_FILE(sys/stat.h HAVE_SYS_STAT_H) CHECK_INCLUDE_FILE(sys/types.h HAVE_SYS_TYPES_H) # -# Find MD5 headers and library +# Find MD5/SHA headers # +CHECK_HEADERS(openssl/md5.h openssl/sha.h) SET(_files "") IF(HAVE_SYS_TYPES_H) SET(_files "sys/types.h") @@ -195,16 +196,57 @@ IF(HAVE_MD5GLOBAL_H) ENDIF(HAVE_MD5GLOBAL_H) SET(_files ${_files} "md5.h") CHECK_INCLUDE_FILES("${_files}" HAVE_MD5_H) -# libmd -FIND_LIBRARY(MD_LIBRARY NAMES md) -IF(MD_LIBRARY) - SET(HAVE_LIBMD 1) - LIST(APPEND ADDITIONAL_LIBS ${MD_LIBRARY}) -ENDIF(MD_LIBRARY) -# -# Find SHA headers -# CHECK_HEADERS(sha.h sha1.h sha2.h sha256.h) +# +# Find MD5/SHA library +# +FIND_LIBRARY(CRYPTO_LIBRARY NAMES crypto) +IF(CRYPTO_LIBRARY) + LIST(APPEND ADDITIONAL_LIBS ${CRYPTO_LIBRARY}) +ELSE(CRYPTO_LIBRARY) + FIND_LIBRARY(SSL_LIBRARY NAMES ssl) + IF(SSL_LIBRARY) + LIST(APPEND ADDITIONAL_LIBS ${SSL_LIBRARY}) + ELSE(SSL_LIBRARY) + FIND_LIBRARY(MD_LIBRARY NAMES md) + IF(MD_LIBRARY) + LIST(APPEND ADDITIONAL_LIBS ${MD_LIBRARY}) + ENDIF(MD_LIBRARY) + ENDIF(SSL_LIBRARY) +ENDIF(CRYPTO_LIBRARY) +# +# Check MD5/SHA functions +# +SET(CMAKE_REQUIRED_LIBRARIES ${ADDITIONAL_LIBS}) +CHECK_FUNCTION_EXISTS(MD5_Init HAVE_MD5_Init) +IF(NOT HAVE_MD5_Init) + CHECK_FUNCTION_EXISTS(MD5Init HAVE_MD5Init) + IF(HAVE_MD5Init) + SET(MD5_Init, "MD5Init") + SET(MD5_Update, "MD5Update") + SET(MD5_Final, "MD5Final") + ENDIF(HAVE_MD5Init) +ENDIF(NOT HAVE_MD5_Init) +IF(HAVE_MD5_Init OR HAVE_MD5Init) + SET(HAVE_MD5 1) +ENDIF(HAVE_MD5_Init OR HAVE_MD5Init) +# +CHECK_FUNCTION_EXISTS(SHA1_Init HAVE_SHA1_Init) +IF(NOT HAVE_SHA1_Init) + CHECK_FUNCTION_EXISTS(SHA1Init HAVE_SHA1Init) + IF(HAVE_SHA1Init) + SET(SHA1_Init, "SHA1Init") + SET(SHA1_Update, "SHA1Update") + SET(SHA1_Final, "SHA1Final") + ENDIF(HAVE_SHA1Init) +ENDIF(NOT HAVE_SHA1_Init) +IF(HAVE_SHA1_Init OR HAVE_SHA1Init) + SET(HAVE_SHA1 1) +ENDIF(HAVE_SHA1_Init OR HAVE_SHA1Init) +# +CHECK_FUNCTION_EXISTS(SHA256_Init HAVE_SHA256) +CHECK_FUNCTION_EXISTS(SHA384_Init HAVE_SHA384) +CHECK_FUNCTION_EXISTS(SHA512_Init HAVE_SHA512) # # Check functions diff --git a/cmake/config.h.in b/cmake/config.h.in index 48d395a6a..6e29225a4 100644 --- a/cmake/config.h.in +++ b/cmake/config.h.in @@ -214,9 +214,6 @@ /* Define to 1 if you have the `lzmadec' library (-llzmadec). */ #cmakedefine HAVE_LIBLZMADEC 1 -/* Define to 1 if you have the `md' library (-lmd). */ -#cmakedefine HAVE_LIBMD 1 - /* Define to 1 if you have the `z' library (-lz). */ #cmakedefine HAVE_LIBZ 1 @@ -251,6 +248,9 @@ /* Define to 1 if you have the header file. */ #cmakedefine HAVE_LZMADEC_H 1 +/* Define to 1 if you have the `MD5' functions. */ +#cmakedefine HAVE_MD5 1 + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_MD5GLOBAL_H 1 @@ -281,6 +281,12 @@ /* Define to 1 if you have the `nl_langinfo' function. */ #cmakedefine HAVE_NL_LANGINFO 1 +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_OPENSSL_MD5_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_OPENSSL_SHA_H 1 + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_PATHS_H 1 @@ -314,15 +320,27 @@ /* Define to 1 if you have the `setlocale' function. */ #cmakedefine HAVE_SETLOCALE 1 +/* Define to 1 if you have the `SHA1' functions. */ +#cmakedefine HAVE_SHA1 1 + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_SHA1_H 1 +/* Define to 1 if you have the `SHA256' functions. */ +#cmakedefine HAVE_SHA256 1 + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_SHA256_H 1 /* Define to 1 if you have the header file. */ #cmakedefine HAVE_SHA2_H 1 +/* Define to 1 if you have the `SHA384' functions. */ +#cmakedefine HAVE_SHA384 1 + +/* Define to 1 if you have the `SHA512' functions. */ +#cmakedefine HAVE_SHA512 1 + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_SHA_H 1 @@ -503,9 +521,27 @@ . */ #cmakedefine MAJOR_IN_SYSMACROS 1 +/* Define to the generates final MD5 hash function. */ +#cmakedefine MD5_Final ${MD5_Final} + +/* Define to the initializes MD5 context function. */ +#cmakedefine MD5_Init ${MD5_Init} + +/* Define to the updates MD5 context function. */ +#cmakedefine MD5_Update ${MD5_Update} + /* Define to 1 if your C compiler doesn't accept -c and -o together. */ #cmakedefine NO_MINUS_C_MINUS_O 1 +/* Define to the generates final SHA1 hash function. */ +#cmakedefine SHA1_Final ${SHA1_Final} + +/* Define to the initializes SHA1 context function. */ +#cmakedefine SHA1_Init ${SHA1_Init} + +/* Define to the updates SHA1 context function. */ +#cmakedefine SHA1_Update ${SHA1_Update} + /* Define to 1 if you have the ANSI C header files. */ #cmakedefine STDC_HEADERS 1 diff --git a/configure.ac b/configure.ac index cb602c8b2..db3058c15 100644 --- a/configure.ac +++ b/configure.ac @@ -195,9 +195,50 @@ if test "x$with_lzmadec" != "xno"; then AC_CHECK_LIB(lzmadec,lzmadec_decode) fi +AC_CHECK_HEADERS([openssl/md5.h openssl/sha.h]) AC_CHECK_HEADERS([md5.h md5global.h]) -AC_CHECK_LIB(md,MD5Init) AC_CHECK_HEADERS([sha.h sha1.h sha2.h sha256.h]) +AC_SEARCH_LIBS(SHA1_Init, crypto ssl md) +AC_CHECK_FUNC(MD5_Init) +if test "x$ac_cv_func_MD5_Init" != "xyes"; then + AC_CHECK_FUNC(MD5Init, + [AC_DEFINE(MD5_Init, MD5Init, + [Define to the initializes MD5 context function.]) + AC_DEFINE(MD5_Update, MD5Update, + [Define to the updates MD5 context function.]) + AC_DEFINE(MD5_Final, MD5Final, + [Define to the generates final MD5 hash function.]) + ]) +fi +if test "x$ac_cv_func_MD5_Init" = "xyes" -o "x$ac_cv_func_MD5Init" = "xyes" ; then + AC_DEFINE(HAVE_MD5, 1, [Define to 1 if you have the `MD5' functions.]) +fi +AC_CHECK_FUNC(SHA1_Init) +if test "x$ac_cv_func_SHA1_Init" != "xyes"; then + AC_CHECK_FUNC(SHA1Init, + [AC_DEFINE(SHA1_Init, SHA1Init, + [Define to the initializes SHA1 context function.]) + AC_DEFINE(SHA1_Update, SHA1Update, + [Define to the updates SHA1 context function.]) + AC_DEFINE(SHA1_Final, SHA1Final, + [Define to the generates final SHA1 hash function.]) + ]) +fi +if test "x$ac_cv_func_SHA1_Init" = "xyes" -o "x$ac_cv_func_SHA1Init" = "xyes" ; then + AC_DEFINE(HAVE_SHA1, 1, [Define to 1 if you have the `SHA1' functions.]) +fi +AC_CHECK_FUNC(SHA256_Init) +if test "x$ac_cv_func_SHA256_Init" = "xyes" ; then + AC_DEFINE(HAVE_SHA256, 1, [Define to 1 if you have the `SHA256' functions.]) +fi +AC_CHECK_FUNC(SHA384_Init) +if test "x$ac_cv_func_SHA384_Init" = "xyes" ; then + AC_DEFINE(HAVE_SHA384, 1, [Define to 1 if you have the `SHA384' functions.]) +fi +AC_CHECK_FUNC(SHA512_Init) +if test "x$ac_cv_func_SHA512_Init" = "xyes" ; then + AC_DEFINE(HAVE_SHA512, 1, [Define to 1 if you have the `SHA512' functions.]) +fi # TODO: Give the user the option of using a pre-existing system # libarchive. This will define HAVE_LIBARCHIVE which will cause diff --git a/libarchive/archive_write_set_format_mtree.c b/libarchive/archive_write_set_format_mtree.c index 79e670bb7..7c4c741ab 100644 --- a/libarchive/archive_write_set_format_mtree.c +++ b/libarchive/archive_write_set_format_mtree.c @@ -33,12 +33,19 @@ __FBSDID("$FreeBSD$"); #include #include #include +#ifdef HAVE_OPENSSL_MD5_H +#include +#else /* HAVE_OPENSSL_MD5_H */ #ifdef HAVE_MD5GLOBAL_H #include #endif #ifdef HAVE_MD5_H #include #endif +#endif /* HAVE_OPENSSL_MD5_H */ +#ifdef HAVE_OPENSSL_SHA_H +#include +#else /* HAVE_OPENSSL_SHA_H */ #ifdef HAVE_SHA_H #include #endif @@ -51,6 +58,7 @@ __FBSDID("$FreeBSD$"); #ifdef HAVE_SHA256_H #include #endif +#endif /* HAVE_OPENSSL_SHA_H */ #include "archive.h" #include "archive_entry.h" @@ -66,17 +74,27 @@ struct mtree_writer { int compute_sum; uint32_t crc; uint64_t crc_len; -#ifdef HAVE_MD5_H +#ifdef HAVE_MD5 MD5_CTX md5ctx; #endif -#if defined(HAVE_SHA_H) || defined(HAVE_SHA1_H) +#ifdef HAVE_SHA1 +#if defined(HAVE_OPENSSL_SHA_H) || defined(HAVE_SHA_H) SHA_CTX sha1ctx; +#else + SHA1_CTX sha1ctx; +#endif #endif -#if defined(HAVE_SHA2_H) || defined(HAVE_SHA256_H) +#ifdef HAVE_SHA256 SHA256_CTX sha256ctx; #endif -#ifdef HAVE_SHA2_H +#ifdef HAVE_SHA384 +#if defined(HAVE_OPENSSL_SHA_H) + SHA512_CTX sha384ctx; +#else SHA384_CTX sha384ctx; +#endif +#endif +#ifdef HAVE_SHA512 SHA512_CTX sha512ctx; #endif /* Keyword options */ @@ -241,15 +259,15 @@ archive_write_mtree_header(struct archive_write *a, mtree->crc_len = 0; } else mtree->compute_sum &= ~F_CKSUM; -#ifdef HAVE_MD5_H +#ifdef HAVE_MD5 if ((mtree->keys & F_MD5) != 0 && archive_entry_filetype(entry) == AE_IFREG) { mtree->compute_sum |= F_MD5; - MD5Init(&mtree->md5ctx); + MD5_Init(&mtree->md5ctx); } else mtree->compute_sum &= ~F_MD5; #endif -#if defined(HAVE_SHA_H) || defined(HAVE_SHA1_H) +#ifdef HAVE_SHA1 if ((mtree->keys & F_SHA1) != 0 && archive_entry_filetype(entry) == AE_IFREG) { mtree->compute_sum |= F_SHA1; @@ -257,7 +275,7 @@ archive_write_mtree_header(struct archive_write *a, } else mtree->compute_sum &= ~F_SHA1; #endif -#if defined(HAVE_SHA2_H) || defined(HAVE_SHA256_H) +#ifdef HAVE_SHA256 if ((mtree->keys & F_SHA256) != 0 && archive_entry_filetype(entry) == AE_IFREG) { mtree->compute_sum |= F_SHA256; @@ -265,13 +283,15 @@ archive_write_mtree_header(struct archive_write *a, } else mtree->compute_sum &= ~F_SHA256; #endif -#ifdef HAVE_SHA2_H +#ifdef HAVE_SHA384 if ((mtree->keys & F_SHA384) != 0 && archive_entry_filetype(entry) == AE_IFREG) { mtree->compute_sum |= F_SHA384; SHA384_Init(&mtree->sha384ctx); } else mtree->compute_sum &= ~F_SHA384; +#endif +#ifdef HAVE_SHA512 if ((mtree->keys & F_SHA512) != 0 && archive_entry_filetype(entry) == AE_IFREG) { mtree->compute_sum |= F_SHA512; @@ -405,16 +425,16 @@ archive_write_mtree_finish_entry(struct archive_write *a) archive_string_sprintf(&mtree->buf, " cksum=%ju", (uintmax_t)mtree->crc); } -#ifdef HAVE_MD5_H +#ifdef HAVE_MD5 if (mtree->compute_sum & F_MD5) { unsigned char buf[16]; - MD5Final(buf, &mtree->md5ctx); + MD5_Final(buf, &mtree->md5ctx); archive_strcat(&mtree->buf, " md5digest="); strappend_bin(&mtree->buf, buf, sizeof(buf)); } #endif -#if defined(HAVE_SHA_H) || defined(HAVE_SHA1_H) +#ifdef HAVE_SHA1 if (mtree->compute_sum & F_SHA1) { unsigned char buf[20]; @@ -423,7 +443,7 @@ archive_write_mtree_finish_entry(struct archive_write *a) strappend_bin(&mtree->buf, buf, sizeof(buf)); } #endif -#if defined(HAVE_SHA2_H) || defined(HAVE_SHA256_H) +#ifdef HAVE_SHA256 if (mtree->compute_sum & F_SHA256) { unsigned char buf[32]; @@ -432,7 +452,7 @@ archive_write_mtree_finish_entry(struct archive_write *a) strappend_bin(&mtree->buf, buf, sizeof(buf)); } #endif -#if defined(HAVE_SHA2_H) +#ifdef HAVE_SHA384 if (mtree->compute_sum & F_SHA384) { unsigned char buf[48]; @@ -440,6 +460,8 @@ archive_write_mtree_finish_entry(struct archive_write *a) archive_strcat(&mtree->buf, " sha384digest="); strappend_bin(&mtree->buf, buf, sizeof(buf)); } +#endif +#ifdef HAVE_SHA512 if (mtree->compute_sum & F_SHA512) { unsigned char buf[64]; @@ -489,23 +511,25 @@ archive_write_mtree_data(struct archive_write *a, const void *buff, size_t n) COMPUTE_CRC(mtree->crc, *p); mtree->crc_len += n; } -#ifdef HAVE_MD5_H +#ifdef HAVE_MD5 if (mtree->compute_sum & F_MD5) - MD5Update(&mtree->md5ctx, buff, n); + MD5_Update(&mtree->md5ctx, buff, n); #endif -#if defined(HAVE_SHA_H) || defined(HAVE_SHA1_H) +#ifdef HAVE_SHA1 if (mtree->compute_sum & F_SHA1) SHA1_Update(&mtree->sha1ctx, buff, n); #endif -#if defined(HAVE_SHA2_H) || defined(HAVE_SHA256_H) +#ifdef HAVE_SHA256 if (mtree->compute_sum & F_SHA256) SHA256_Update(&mtree->sha256ctx, buff, n); #endif -#ifdef HAVE_SHA2_H +#ifdef HAVE_SHA384 if (mtree->compute_sum & F_SHA384) SHA384_Update(&mtree->sha384ctx, buff, n); +#endif +#ifdef HAVE_SHA512 if (mtree->compute_sum & F_SHA512) - SHA384_Update(&mtree->sha512ctx, buff, n); + SHA512_Update(&mtree->sha512ctx, buff, n); #endif return n; } @@ -560,7 +584,7 @@ archive_write_mtree_options(struct archive_write *a, const char *key, keybit = F_SLINK; break; case 'm': -#ifdef HAVE_MD5_H +#ifdef HAVE_MD5 if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0) keybit = F_MD5; @@ -573,20 +597,22 @@ archive_write_mtree_options(struct archive_write *a, const char *key, keybit = F_NLINK; break; case 's': -#if defined(HAVE_SHA_H) || defined(HAVE_SHA1_H) +#ifdef HAVE_SHA1 if (strcmp(key, "sha1") == 0 || strcmp(key, "sha1digest") == 0) keybit = F_SHA1; #endif -#if defined(HAVE_SHA2_H) || defined(HAVE_SHA256_H) +#ifdef HAVE_SHA256 if (strcmp(key, "sha256") == 0 || strcmp(key, "sha256digest") == 0) keybit = F_SHA256; #endif -#ifdef HAVE_SHA2_H +#ifdef HAVE_SHA384 if (strcmp(key, "sha384") == 0 || strcmp(key, "sha384digest") == 0) keybit = F_SHA384; +#endif +#ifdef HAVE_SHA384 if (strcmp(key, "sha512") == 0 || strcmp(key, "sha512digest") == 0) keybit = F_SHA512;