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.

430 lines
15 KiB

  1. From bad7f584eb11076ae37fd51cdb21842935c6ea57 Mon Sep 17 00:00:00 2001
  2. From: Mike Gelfand <mikedld@mikedld.com>
  3. Date: Wed, 26 Oct 2016 20:37:10 +0300
  4. Subject: [PATCH] Add support for mbedtls (formely polarssl)
  5. ---
  6. cmake/FindPolarSSL.cmake | 37 +++++++----
  7. configure.ac | 28 ++++----
  8. libtransmission/CMakeLists.txt | 4 ++
  9. libtransmission/Makefile.am | 3 +
  10. libtransmission/crypto-utils-polarssl.c | 112 +++++++++++++++++++-------------
  11. 5 files changed, 115 insertions(+), 69 deletions(-)
  12. diff --git a/cmake/FindPolarSSL.cmake b/cmake/FindPolarSSL.cmake
  13. index 0a958e0..e4e1ac6 100644
  14. --- a/cmake/FindPolarSSL.cmake
  15. +++ b/cmake/FindPolarSSL.cmake
  16. @@ -8,21 +8,36 @@ if(POLARSSL_PREFER_STATIC_LIB)
  17. endif()
  18. if(UNIX)
  19. - find_package(PkgConfig QUIET)
  20. - pkg_check_modules(_POLARSSL QUIET polarssl)
  21. + find_package(PkgConfig QUIET)
  22. + pkg_check_modules(_MBEDTLS QUIET mbedtls)
  23. endif()
  24. -find_path(POLARSSL_INCLUDE_DIR NAMES polarssl/version.h HINTS ${_POLARSSL_INCLUDEDIR})
  25. -find_library(POLARSSL_LIBRARY NAMES polarssl HINTS ${_POLARSSL_LIBDIR})
  26. +find_path(MBEDTLS_INCLUDE_DIR NAMES mbedtls/version.h HINTS ${_MBEDTLS_INCLUDEDIR})
  27. +find_library(MBEDTLS_LIBRARY NAMES mbedtls HINTS ${_MBEDTLS_LIBDIR})
  28. +if(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARY)
  29. + set(POLARSSL_INCLUDE_DIR ${MBEDTLS_INCLUDE_DIR})
  30. + set(POLARSSL_LIBRARY ${MBEDTLS_LIBRARY})
  31. + set(POLARSSL_VERSION ${_MBEDTLS_VERSION})
  32. + set(POLARSSL_IS_MBEDTLS ON)
  33. +else()
  34. + if(UNIX)
  35. + pkg_check_modules(_POLARSSL QUIET polarssl)
  36. + endif()
  37. +
  38. + find_path(POLARSSL_INCLUDE_DIR NAMES polarssl/version.h HINTS ${_POLARSSL_INCLUDEDIR})
  39. + find_library(POLARSSL_LIBRARY NAMES polarssl HINTS ${_POLARSSL_LIBDIR})
  40. + set(POLARSSL_VERSION ${_POLARSSL_VERSION})
  41. + set(POLARSSL_IS_MBEDTLS OFF)
  42. +endif()
  43. -if(POLARSSL_INCLUDE_DIR)
  44. - if(_POLARSSL_VERSION)
  45. - set(POLARSSL_VERSION ${_POLARSSL_VERSION})
  46. +if(NOT POLARSSL_VERSION AND POLARSSL_INCLUDE_DIR)
  47. + if(POLARSSL_IS_MBEDTLS)
  48. + file(STRINGS "${POLARSSL_INCLUDE_DIR}/mbedtls/version.h" POLARSSL_VERSION_STR REGEX "^#define[\t ]+MBEDTLS_VERSION_STRING[\t ]+\"[^\"]+\"")
  49. else()
  50. file(STRINGS "${POLARSSL_INCLUDE_DIR}/polarssl/version.h" POLARSSL_VERSION_STR REGEX "^#define[\t ]+POLARSSL_VERSION_STRING[\t ]+\"[^\"]+\"")
  51. - if(POLARSSL_VERSION_STR MATCHES "\"([^\"]+)\"")
  52. - set(POLARSSL_VERSION "${CMAKE_MATCH_1}")
  53. - endif()
  54. + endif()
  55. + if(POLARSSL_VERSION_STR MATCHES "\"([^\"]+)\"")
  56. + set(POLARSSL_VERSION "${CMAKE_MATCH_1}")
  57. endif()
  58. endif()
  59. @@ -39,7 +54,7 @@ find_package_handle_standard_args(PolarSSL
  60. POLARSSL_VERSION
  61. )
  62. -mark_as_advanced(POLARSSL_INCLUDE_DIR POLARSSL_LIBRARY)
  63. +mark_as_advanced(MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARY POLARSSL_INCLUDE_DIR POLARSSL_LIBRARY)
  64. if(POLARSSL_PREFER_STATIC_LIB)
  65. set(CMAKE_FIND_LIBRARY_SUFFIXES ${POLARSSL_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
  66. diff --git a/configure.ac b/configure.ac
  67. index aff673b..cb026df 100644
  68. --- a/configure.ac
  69. +++ b/configure.ac
  70. @@ -149,23 +149,26 @@ AS_IF([test "x$want_crypto" = "xauto" -o "x$want_crypto" = "xcyassl"], [
  71. )
  72. ])
  73. AS_IF([test "x$want_crypto" = "xauto" -o "x$want_crypto" = "xpolarssl"], [
  74. - AC_CHECK_HEADER([polarssl/version.h],
  75. - [AC_EGREP_CPP([version_ok], [#include <polarssl/version.h>
  76. - #if defined (POLARSSL_VERSION_NUMBER) && POLARSSL_VERSION_NUMBER >= $POLARSSL_MINIMUM
  77. - version_ok
  78. - #endif],
  79. - [AC_CHECK_LIB([polarssl], [dhm_calc_secret],
  80. - [want_crypto="polarssl"; CRYPTO_PKG="polarssl"; CRYPTO_CFLAGS=""; CRYPTO_LIBS="-lpolarssl"],
  81. + PKG_CHECK_MODULES(MBEDTLS, [mbedtls >= $POLARSSL_MINIMUM],
  82. + [want_crypto="polarssl"; CRYPTO_PKG="polarssl"; CRYPTO_CFLAGS="$MBEDTLS_CFLAGS"; CRYPTO_LIBS="$MBEDTLS_LIBS"; POLARSSL_IS_MBEDTLS=yes],
  83. + [AC_CHECK_HEADER([polarssl/version.h],
  84. + [AC_EGREP_CPP([version_ok], [#include <polarssl/version.h>
  85. + #if defined (POLARSSL_VERSION_NUMBER) && POLARSSL_VERSION_NUMBER >= $POLARSSL_MINIMUM
  86. + version_ok
  87. + #endif],
  88. + [AC_CHECK_LIB([polarssl], [dhm_calc_secret],
  89. + [want_crypto="polarssl"; CRYPTO_PKG="polarssl"; CRYPTO_CFLAGS=""; CRYPTO_LIBS="-lpolarssl"],
  90. + [AS_IF([test "x$want_crypto" = "xpolarssl"],
  91. + [AC_MSG_ERROR([PolarSSL support requested, but library not found.])]
  92. + )]
  93. + )],
  94. [AS_IF([test "x$want_crypto" = "xpolarssl"],
  95. - [AC_MSG_ERROR([PolarSSL support requested, but library not found.])]
  96. + [AC_MSG_ERROR([PolarSSL support requested, but version not suitable.])]
  97. )]
  98. )],
  99. [AS_IF([test "x$want_crypto" = "xpolarssl"],
  100. - [AC_MSG_ERROR([PolarSSL support requested, but version not suitable.])]
  101. + [AC_MSG_ERROR([PolarSSL support requested, but headers not found.])]
  102. )]
  103. - )],
  104. - [AS_IF([test "x$want_crypto" = "xpolarssl"],
  105. - [AC_MSG_ERROR([PolarSSL support requested, but headers not found.])]
  106. )]
  107. )
  108. ])
  109. @@ -180,6 +183,7 @@ AS_IF([test "x$CRYPTO_PKG" = "x"], [
  110. AM_CONDITIONAL([CRYPTO_USE_OPENSSL],[test "x$CRYPTO_PKG" = "xopenssl"])
  111. AM_CONDITIONAL([CRYPTO_USE_CYASSL],[test "x$CRYPTO_PKG" = "xcyassl"])
  112. AM_CONDITIONAL([CRYPTO_USE_POLARSSL],[test "x$CRYPTO_PKG" = "xpolarssl"])
  113. +AM_CONDITIONAL([POLARSSL_IS_MBEDTLS],[test "x$POLARSSL_IS_MBEDTLS" = "xyes"])
  114. AC_SUBST(CRYPTO_PKG)
  115. AC_SUBST(CRYPTO_CFLAGS)
  116. AC_SUBST(CRYPTO_LIBS)
  117. diff --git a/libtransmission/CMakeLists.txt b/libtransmission/CMakeLists.txt
  118. index 4865dee..b5230b9 100644
  119. --- a/libtransmission/CMakeLists.txt
  120. +++ b/libtransmission/CMakeLists.txt
  121. @@ -194,6 +194,10 @@ if(USE_SYSTEM_B64)
  122. add_definitions(-DUSE_SYSTEM_B64)
  123. endif()
  124. +if(POLARSSL_IS_MBEDTLS)
  125. + add_definitions(-DPOLARSSL_IS_MBEDTLS)
  126. +endif()
  127. +
  128. include_directories(
  129. ${PROJECT_SOURCE_DIR}
  130. ${PROJECT_BINARY_DIR}
  131. diff --git a/libtransmission/Makefile.am b/libtransmission/Makefile.am
  132. index f91769a..4d7e85a 100644
  133. --- a/libtransmission/Makefile.am
  134. +++ b/libtransmission/Makefile.am
  135. @@ -102,6 +102,9 @@ libtransmission_a_SOURCES += crypto-utils-cyassl.c
  136. endif
  137. if CRYPTO_USE_POLARSSL
  138. libtransmission_a_SOURCES += crypto-utils-polarssl.c
  139. +if POLARSSL_IS_MBEDTLS
  140. +AM_CPPFLAGS += -DPOLARSSL_IS_MBEDTLS
  141. +endif
  142. endif
  143. noinst_HEADERS = \
  144. diff --git a/libtransmission/crypto-utils-polarssl.c b/libtransmission/crypto-utils-polarssl.c
  145. index 1c59a7f..e98c13d 100644
  146. --- a/libtransmission/crypto-utils-polarssl.c
  147. +++ b/libtransmission/crypto-utils-polarssl.c
  148. @@ -1,5 +1,5 @@
  149. /*
  150. - * This file Copyright (C) 2014-2015 Mnemosyne LLC
  151. + * This file Copyright (C) 2014-2016 Mnemosyne LLC
  152. *
  153. * It may be used under the GNU GPL versions 2 or 3
  154. * or any future license endorsed by Mnemosyne LLC.
  155. @@ -7,15 +7,25 @@
  156. * $Id$
  157. */
  158. +#if defined (POLARSSL_IS_MBEDTLS)
  159. + #define API_HEADER(x) <mbedtls/x>
  160. + #define API(x) mbedtls_##x
  161. + #define API_VERSION_NUMBER MBEDTLS_VERSION_NUMBER
  162. +#else
  163. + #define API_HEADER(x) <polarssl/x>
  164. + #define API(x) x
  165. + #define API_VERSION_NUMBER POLARSSL_VERSION_NUMBER
  166. +#endif
  167. +
  168. #include <assert.h>
  169. -#include <polarssl/arc4.h>
  170. -#include <polarssl/base64.h>
  171. -#include <polarssl/ctr_drbg.h>
  172. -#include <polarssl/dhm.h>
  173. -#include <polarssl/error.h>
  174. -#include <polarssl/sha1.h>
  175. -#include <polarssl/version.h>
  176. +#include API_HEADER (arc4.h)
  177. +#include API_HEADER (base64.h)
  178. +#include API_HEADER (ctr_drbg.h)
  179. +#include API_HEADER (dhm.h)
  180. +#include API_HEADER (error.h)
  181. +#include API_HEADER (sha1.h)
  182. +#include API_HEADER (version.h)
  183. #include "transmission.h"
  184. #include "crypto-utils.h"
  185. @@ -41,7 +51,9 @@ log_polarssl_error (int error_code,
  186. {
  187. char error_message[256];
  188. -#if POLARSSL_VERSION_NUMBER >= 0x01030000
  189. +#if defined (POLARSSL_IS_MBEDTLS)
  190. + mbedtls_strerror (error_code, error_message, sizeof (error_message));
  191. +#elif API_VERSION_NUMBER >= 0x01030000
  192. polarssl_strerror (error_code, error_message, sizeof (error_message));
  193. #else
  194. error_strerror (error_code, error_message, sizeof (error_message));
  195. @@ -51,7 +63,7 @@ log_polarssl_error (int error_code,
  196. }
  197. }
  198. -#define log_error(error_code) log_polarssl_error(error_code, __FILE__, __LINE__)
  199. +#define log_error(error_code) log_polarssl_error ((error_code), __FILE__, __LINE__)
  200. static bool
  201. check_polarssl_result (int result,
  202. @@ -83,15 +95,20 @@ my_rand (void * context UNUSED, unsigned char * buffer, size_t buffer_size)
  203. return 0;
  204. }
  205. -static ctr_drbg_context *
  206. +static API (ctr_drbg_context) *
  207. get_rng (void)
  208. {
  209. - static ctr_drbg_context rng;
  210. + static API (ctr_drbg_context) rng;
  211. static bool rng_initialized = false;
  212. if (!rng_initialized)
  213. {
  214. - if (!check_result (ctr_drbg_init (&rng, &my_rand, NULL, NULL, 0)))
  215. +#if API_VERSION_NUMBER >= 0x02000000
  216. + API (ctr_drbg_init) (&rng);
  217. + if (!check_result (API (ctr_drbg_seed) (&rng, &my_rand, NULL, NULL, 0)))
  218. +#else
  219. + if (!check_result (API (ctr_drbg_init) (&rng, &my_rand, NULL, NULL, 0)))
  220. +#endif
  221. return NULL;
  222. rng_initialized = true;
  223. }
  224. @@ -117,13 +134,13 @@ get_rng_lock (void)
  225. tr_sha1_ctx_t
  226. tr_sha1_init (void)
  227. {
  228. - sha1_context * handle = tr_new0 (sha1_context, 1);
  229. + API (sha1_context) * handle = tr_new0 (API (sha1_context), 1);
  230. -#if POLARSSL_VERSION_NUMBER >= 0x01030800
  231. - sha1_init (handle);
  232. +#if API_VERSION_NUMBER >= 0x01030800
  233. + API (sha1_init) (handle);
  234. #endif
  235. - sha1_starts (handle);
  236. + API (sha1_starts) (handle);
  237. return handle;
  238. }
  239. @@ -139,7 +156,7 @@ tr_sha1_update (tr_sha1_ctx_t handle,
  240. assert (data != NULL);
  241. - sha1_update (handle, data, data_length);
  242. + API (sha1_update) (handle, data, data_length);
  243. return true;
  244. }
  245. @@ -151,11 +168,11 @@ tr_sha1_final (tr_sha1_ctx_t handle,
  246. {
  247. assert (handle != NULL);
  248. - sha1_finish (handle, hash);
  249. + API (sha1_finish) (handle, hash);
  250. }
  251. -#if POLARSSL_VERSION_NUMBER >= 0x01030800
  252. - sha1_free (handle);
  253. +#if API_VERSION_NUMBER >= 0x01030800
  254. + API (sha1_free) (handle);
  255. #endif
  256. tr_free (handle);
  257. @@ -169,10 +186,10 @@ tr_sha1_final (tr_sha1_ctx_t handle,
  258. tr_rc4_ctx_t
  259. tr_rc4_new (void)
  260. {
  261. - arc4_context * handle = tr_new0 (arc4_context, 1);
  262. + API (arc4_context) * handle = tr_new0 (API (arc4_context), 1);
  263. -#if POLARSSL_VERSION_NUMBER >= 0x01030800
  264. - arc4_init (handle);
  265. +#if API_VERSION_NUMBER >= 0x01030800
  266. + API (arc4_init) (handle);
  267. #endif
  268. return handle;
  269. @@ -181,8 +198,8 @@ tr_rc4_new (void)
  270. void
  271. tr_rc4_free (tr_rc4_ctx_t handle)
  272. {
  273. -#if POLARSSL_VERSION_NUMBER >= 0x01030800
  274. - arc4_free (handle);
  275. +#if API_VERSION_NUMBER >= 0x01030800
  276. + API (arc4_free) (handle);
  277. #endif
  278. tr_free (handle);
  279. @@ -196,7 +213,7 @@ tr_rc4_set_key (tr_rc4_ctx_t handle,
  280. assert (handle != NULL);
  281. assert (key != NULL);
  282. - arc4_setup (handle, key, key_length);
  283. + API (arc4_setup) (handle, key, key_length);
  284. }
  285. void
  286. @@ -213,7 +230,7 @@ tr_rc4_process (tr_rc4_ctx_t handle,
  287. assert (input != NULL);
  288. assert (output != NULL);
  289. - arc4_crypt (handle, length, input, output);
  290. + API (arc4_crypt) (handle, length, input, output);
  291. }
  292. /***
  293. @@ -226,19 +243,19 @@ tr_dh_new (const uint8_t * prime_num,
  294. const uint8_t * generator_num,
  295. size_t generator_num_length)
  296. {
  297. - dhm_context * handle = tr_new0 (dhm_context, 1);
  298. + API (dhm_context) * handle = tr_new0 (API (dhm_context), 1);
  299. assert (prime_num != NULL);
  300. assert (generator_num != NULL);
  301. -#if POLARSSL_VERSION_NUMBER >= 0x01030800
  302. - dhm_init (handle);
  303. +#if API_VERSION_NUMBER >= 0x01030800
  304. + API (dhm_init) (handle);
  305. #endif
  306. - if (!check_result (mpi_read_binary (&handle->P, prime_num, prime_num_length)) ||
  307. - !check_result (mpi_read_binary (&handle->G, generator_num, generator_num_length)))
  308. + if (!check_result (API (mpi_read_binary) (&handle->P, prime_num, prime_num_length)) ||
  309. + !check_result (API (mpi_read_binary) (&handle->G, generator_num, generator_num_length)))
  310. {
  311. - dhm_free (handle);
  312. + API (dhm_free) (handle);
  313. return NULL;
  314. }
  315. @@ -253,7 +270,7 @@ tr_dh_free (tr_dh_ctx_t handle)
  316. if (handle == NULL)
  317. return;
  318. - dhm_free (handle);
  319. + API (dhm_free) (handle);
  320. }
  321. bool
  322. @@ -262,7 +279,7 @@ tr_dh_make_key (tr_dh_ctx_t raw_handle,
  323. uint8_t * public_key,
  324. size_t * public_key_length)
  325. {
  326. - dhm_context * handle = raw_handle;
  327. + API (dhm_context) * handle = raw_handle;
  328. assert (handle != NULL);
  329. assert (public_key != NULL);
  330. @@ -270,8 +287,8 @@ tr_dh_make_key (tr_dh_ctx_t raw_handle,
  331. if (public_key_length != NULL)
  332. *public_key_length = handle->len;
  333. - return check_result (dhm_make_public (handle, private_key_length, public_key,
  334. - handle->len, my_rand, NULL));
  335. + return check_result (API (dhm_make_public) (handle, private_key_length, public_key,
  336. + handle->len, my_rand, NULL));
  337. }
  338. tr_dh_secret_t
  339. @@ -279,26 +296,29 @@ tr_dh_agree (tr_dh_ctx_t raw_handle,
  340. const uint8_t * other_public_key,
  341. size_t other_public_key_length)
  342. {
  343. - dhm_context * handle = raw_handle;
  344. + API (dhm_context) * handle = raw_handle;
  345. struct tr_dh_secret * ret;
  346. size_t secret_key_length;
  347. assert (handle != NULL);
  348. assert (other_public_key != NULL);
  349. - if (!check_result (dhm_read_public (handle, other_public_key,
  350. - other_public_key_length)))
  351. + if (!check_result (API (dhm_read_public )(handle, other_public_key,
  352. + other_public_key_length)))
  353. return NULL;
  354. ret = tr_dh_secret_new (handle->len);
  355. secret_key_length = handle->len;
  356. -#if POLARSSL_VERSION_NUMBER >= 0x01030000
  357. - if (!check_result (dhm_calc_secret (handle, ret->key,
  358. - &secret_key_length, my_rand, NULL)))
  359. +#if API_VERSION_NUMBER >= 0x02000000
  360. + if (!check_result (API (dhm_calc_secret) (handle, ret->key, secret_key_length,
  361. + &secret_key_length, my_rand, NULL)))
  362. +#elif API_VERSION_NUMBER >= 0x01030000
  363. + if (!check_result (API (dhm_calc_secret) (handle, ret->key,
  364. + &secret_key_length, my_rand, NULL)))
  365. #else
  366. - if (!check_result (dhm_calc_secret (handle, ret->key, &secret_key_length)))
  367. + if (!check_result (API (dhm_calc_secret) (handle, ret->key, &secret_key_length)))
  368. #endif
  369. {
  370. tr_dh_secret_free (ret);
  371. @@ -324,7 +344,7 @@ tr_rand_buffer (void * buffer,
  372. assert (buffer != NULL);
  373. tr_lockLock (rng_lock);
  374. - ret = check_result (ctr_drbg_random (get_rng (), buffer, length));
  375. + ret = check_result (API (ctr_drbg_random) (get_rng (), buffer, length));
  376. tr_lockUnlock (rng_lock);
  377. return ret;
  378. --
  379. 2.7.4