HardenedBSD src tree
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

archive_hmac.c 6.2 KiB

Update vendor/libarchive/dist to git b5818e39e128eca4951e2ab10467d4d850a2ba57 Relevant vendor changes: Issue #795: XAR - do not try to add xattrs without an allocated name PR #812: non-recursive option for extract and list PR #958: support reading metadata from compressed files PR #999: add --exclude-vcs option to bsdtar Issue #1062: treat empty archives with a GNU volume header as valid PR #1074: Handle ZIP files with trailing 0s in the extra fields (Android APK archives) PR #1109: Ignore padding in Zip extra field data (Android APK archives) PR #1167: fix problems related to unreadable directories Issue #1168: fix handling of strtol() and strtoul() PR #1172: RAR5 - fix invalid window buffer read in E8E9 filter PR #1174: ZIP reader - fix of MSZIP signature parsing PR #1175: gzip filter - fix reading files larger than 4GB from memory PR #1177: gzip filter - fix memory leak with repeated header reads PR #1180: ZIP reader - add support for Info-ZIP Unicode Path Extra Field PR #1181: RAR5 - fix merge_block() recursion (OSS-Fuzz 12999, 13029, 13144, 13478, 13490) PR #1183: fix memory leak when decompressing ZIP files with LZMA PR #1184: fix RAR5 OSS-Fuzz issues 12466, 14490, 14491, 12817 OSS-Fuzz 12466: RAR5 - fix buffer overflow when parsing huffman tables OSS-Fuzz 14490, 14491: RAR5 - fix bad shift-left operations OSS-Fuzz 12817: RAR5 - handle a case with truncated huffman tables PR #1186: RAR5 - fix invalid type used for dictionary size mask (OSS-Fuzz 14537) PR #1187: RAR5 - fix integer overflow (OSS-Fuzz 14555) PR #1190: RAR5 - RAR5 don't try to unpack entries marked as directories (OSS-Fuzz 14574) PR #1196: RAR5 - fix a potential SIGSEGV on 32-bit builds OSS-Fuzz 2582: RAR - fix use after free if there is an invalid entry OSS-Fuzz 14331: RAR5 - fix maximum owner name length OSS-Fuzz 13965: RAR5 - use unsigned int for volume number + range check Additional RAR5 reader changes: - support symlinks, hardlinks, file owner, file group, versioned files - change ARCHIVE_FORMAT_RAR_V5 to 0x100000 - set correct mode for readonly directories - support readonly, hidden and system Windows file attributes NOTE: a version bump of libarchive will happen in the following days
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*-
  2. * Copyright (c) 2014 Michihiro NAKAJIMA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. #ifdef HAVE_STRING_H
  27. #include <string.h>
  28. #endif
  29. #include "archive.h"
  30. #include "archive_hmac_private.h"
  31. /*
  32. * On systems that do not support any recognized crypto libraries,
  33. * the archive_hmac.c file is expected to define no usable symbols.
  34. *
  35. * But some compilers and linkers choke on empty object files, so
  36. * define a public symbol that will always exist. This could
  37. * be removed someday if this file gains another always-present
  38. * symbol definition.
  39. */
  40. int __libarchive_hmac_build_hack(void) {
  41. return 0;
  42. }
  43. #ifdef ARCHIVE_HMAC_USE_Apple_CommonCrypto
  44. static int
  45. __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
  46. {
  47. CCHmacInit(ctx, kCCHmacAlgSHA1, key, key_len);
  48. return 0;
  49. }
  50. static void
  51. __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
  52. size_t data_len)
  53. {
  54. CCHmacUpdate(ctx, data, data_len);
  55. }
  56. static void
  57. __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
  58. {
  59. CCHmacFinal(ctx, out);
  60. *out_len = 20;
  61. }
  62. static void
  63. __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
  64. {
  65. memset(ctx, 0, sizeof(*ctx));
  66. }
  67. #elif defined(_WIN32) && !defined(__CYGWIN__) && defined(HAVE_BCRYPT_H)
  68. #ifndef BCRYPT_HASH_REUSABLE_FLAG
  69. # define BCRYPT_HASH_REUSABLE_FLAG 0x00000020
  70. #endif
  71. static int
  72. __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
  73. {
  74. #ifdef __GNUC__
  75. #pragma GCC diagnostic ignored "-Wcast-qual"
  76. #endif
  77. BCRYPT_ALG_HANDLE hAlg;
  78. BCRYPT_HASH_HANDLE hHash;
  79. DWORD hash_len;
  80. PBYTE hash;
  81. ULONG result;
  82. NTSTATUS status;
  83. ctx->hAlg = NULL;
  84. status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA1_ALGORITHM,
  85. MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
  86. if (!BCRYPT_SUCCESS(status))
  87. return -1;
  88. status = BCryptGetProperty(hAlg, BCRYPT_HASH_LENGTH, (PUCHAR)&hash_len,
  89. sizeof(hash_len), &result, 0);
  90. if (!BCRYPT_SUCCESS(status)) {
  91. BCryptCloseAlgorithmProvider(hAlg, 0);
  92. return -1;
  93. }
  94. hash = (PBYTE)HeapAlloc(GetProcessHeap(), 0, hash_len);
  95. if (hash == NULL) {
  96. BCryptCloseAlgorithmProvider(hAlg, 0);
  97. return -1;
  98. }
  99. status = BCryptCreateHash(hAlg, &hHash, NULL, 0,
  100. (PUCHAR)key, (ULONG)key_len, BCRYPT_HASH_REUSABLE_FLAG);
  101. if (!BCRYPT_SUCCESS(status)) {
  102. BCryptCloseAlgorithmProvider(hAlg, 0);
  103. HeapFree(GetProcessHeap(), 0, hash);
  104. return -1;
  105. }
  106. ctx->hAlg = hAlg;
  107. ctx->hHash = hHash;
  108. ctx->hash_len = hash_len;
  109. ctx->hash = hash;
  110. return 0;
  111. }
  112. static void
  113. __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
  114. size_t data_len)
  115. {
  116. BCryptHashData(ctx->hHash, (PUCHAR)(uintptr_t)data, (ULONG)data_len, 0);
  117. }
  118. static void
  119. __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
  120. {
  121. BCryptFinishHash(ctx->hHash, ctx->hash, ctx->hash_len, 0);
  122. if (ctx->hash_len == *out_len)
  123. memcpy(out, ctx->hash, *out_len);
  124. }
  125. static void
  126. __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
  127. {
  128. if (ctx->hAlg != NULL) {
  129. BCryptCloseAlgorithmProvider(ctx->hAlg, 0);
  130. HeapFree(GetProcessHeap(), 0, ctx->hash);
  131. ctx->hAlg = NULL;
  132. }
  133. }
  134. #elif defined(HAVE_LIBNETTLE) && defined(HAVE_NETTLE_HMAC_H)
  135. static int
  136. __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
  137. {
  138. hmac_sha1_set_key(ctx, key_len, key);
  139. return 0;
  140. }
  141. static void
  142. __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
  143. size_t data_len)
  144. {
  145. hmac_sha1_update(ctx, data_len, data);
  146. }
  147. static void
  148. __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
  149. {
  150. hmac_sha1_digest(ctx, (unsigned)*out_len, out);
  151. }
  152. static void
  153. __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
  154. {
  155. memset(ctx, 0, sizeof(*ctx));
  156. }
  157. #elif defined(HAVE_LIBCRYPTO)
  158. static int
  159. __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
  160. {
  161. *ctx = HMAC_CTX_new();
  162. if (*ctx == NULL)
  163. return -1;
  164. HMAC_Init_ex(*ctx, key, key_len, EVP_sha1(), NULL);
  165. return 0;
  166. }
  167. static void
  168. __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
  169. size_t data_len)
  170. {
  171. HMAC_Update(*ctx, data, data_len);
  172. }
  173. static void
  174. __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
  175. {
  176. unsigned int len = (unsigned int)*out_len;
  177. HMAC_Final(*ctx, out, &len);
  178. *out_len = len;
  179. }
  180. static void
  181. __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
  182. {
  183. HMAC_CTX_free(*ctx);
  184. *ctx = NULL;
  185. }
  186. #else
  187. /* Stub */
  188. static int
  189. __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
  190. {
  191. (void)ctx;/* UNUSED */
  192. (void)key;/* UNUSED */
  193. (void)key_len;/* UNUSED */
  194. return -1;
  195. }
  196. static void
  197. __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
  198. size_t data_len)
  199. {
  200. (void)ctx;/* UNUSED */
  201. (void)data;/* UNUSED */
  202. (void)data_len;/* UNUSED */
  203. }
  204. static void
  205. __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
  206. {
  207. (void)ctx;/* UNUSED */
  208. (void)out;/* UNUSED */
  209. (void)out_len;/* UNUSED */
  210. }
  211. static void
  212. __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
  213. {
  214. (void)ctx;/* UNUSED */
  215. }
  216. #endif
  217. const struct archive_hmac __archive_hmac = {
  218. &__hmac_sha1_init,
  219. &__hmac_sha1_update,
  220. &__hmac_sha1_final,
  221. &__hmac_sha1_cleanup,
  222. };