Changeset 14422


Ignore:
Timestamp:
Jan 1, 2015, 9:16:36 PM (3 years ago)
Author:
mikedld
Message:

Use libb64 instead of OpenSSL to encode/decode BASE64

Some crypto libraries (like CyaSSL, MatrixSSL and CommonCrypto?) either
don't have or expose this functionality at all, expose only part of it,
or (like OpenSSL) have heavyweight API for it. Also, for the task as
easy as BASE64 encoding and decoding it's much better to use small and
simple specialized library.

Location:
trunk
Files:
15 added
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/CMakeLists.txt

    r14390 r14422  
    2929tr_auto_option(USE_SYSTEM_NATPMP    "Use system natpmp library" AUTO)
    3030tr_auto_option(USE_SYSTEM_UTP       "Use system utp library" AUTO)
     31tr_auto_option(USE_SYSTEM_B64       "Use system b64 library" AUTO)
    3132tr_auto_option(WITH_INOTIFY         "Enable inotify support (on systems that support it)" AUTO)
    3233tr_auto_option(WITH_KQUEUE          "Enable kqueue support (on systems that support it)" AUTO)
     
    105106tr_github_upstream(DHT       jech/dht           bf62643a95 0a2a2abe447d8a73f0084c1bc837e566)
    106107tr_github_upstream(UTP       bittorrent/libutp  7c4f19abdf 8b92aa05abec5f6675cdde6477cd6f51)
     108tr_github_upstream(B64       mikedld/libb64     c1e3323498 10128cb30ad768d2c4caf627e0f69db3)
    107109
    108110if(WIN32)
     
    230232              COMMAND "${CMAKE_COMMAND}" -E copy "${THIRD_PARTY_DIR}/utp_config.h" "<SOURCE_DIR>/utp_config.h")
    231233endif()
     234
     235tr_add_external_auto_library(B64 b64
     236    PATCH_COMMAND "${CMAKE_COMMAND}" -E copy "${THIRD_PARTY_DIR}/b64.cmake" "<SOURCE_DIR>/CMakeLists.txt"
     237          COMMAND "${CMAKE_COMMAND}" -E chdir "<SOURCE_DIR>" patch -p1 -i "${THIRD_PARTY_DIR}/b64-01-newline.patch")
    232238
    233239if(WITH_INOTIFY)
  • trunk/cli/Makefile.am

    r14328 r14422  
    1919    @LIBUPNP_LIBS@ \
    2020    @DHT_LIBS@ \
     21    @LIBB64_LIBS@ \
    2122    @LIBUTP_LIBS@ \
    2223    @LIBEVENT_LIBS@ \
  • trunk/configure.ac

    r14410 r14422  
    215215AC_SUBST(DHT_LIBS)
    216216AC_SUBST(DHT_LIBS_QT)
     217
     218
     219dnl ----------------------------------------------------------------------------
     220dnl
     221dnl  libb64
     222
     223LIBB64_CFLAGS="-I\$(top_srcdir)/third-party/libb64"
     224LIBB64_LIBS="\$(top_builddir)/third-party/libb64/libb64.a"
     225LIBB64_LIBS_QT="\$\${TRANSMISSION_TOP}/third-party/libb64/libb64.a"
     226build_bundled_b64="yes"
     227AC_ARG_ENABLE([external-b64],
     228              AS_HELP_STRING([--enable-external-b64],[Use system libb64]),
     229              [want_external_b64=${enableval}],
     230              [want_external_b64=no])
     231if test "x$want_external_b64" != "xno" ; then
     232    LIBB64_CFLAGS=""
     233    LIBB64_LIBS="-lb64"
     234    LIBB64_LIBS_QT="-lb64"
     235    build_bundled_b64="no"
     236fi
     237
     238AM_CONDITIONAL([BUILD_B64],[test "x$build_bundled_b64" = "xyes"])
     239AC_SUBST(LIBB64_CFLAGS)
     240AC_SUBST(LIBB64_LIBS)
     241AC_SUBST(LIBB64_LIBS_QT)
    217242
    218243
     
    563588                 third-party/Makefile
    564589                 third-party/dht/Makefile
     590                 third-party/libb64/Makefile
    565591                 third-party/libutp/Makefile
    566592                 third-party/libnatpmp/Makefile
  • trunk/daemon/Makefile.am

    r14328 r14422  
    2525    @LIBNATPMP_LIBS@ \
    2626    @DHT_LIBS@ \
     27    @LIBB64_LIBS@ \
    2728    @LIBUTP_LIBS@ \
    2829    @LIBEVENT_LIBS@ \
  • trunk/gtk/Makefile.am

    r14328 r14422  
    9090    @LIBNATPMP_LIBS@ \
    9191    @DHT_LIBS@ \
     92    @LIBB64_LIBS@ \
    9293    @LIBUTP_LIBS@ \
    9394    @GTK_LIBS@ \
  • trunk/libtransmission/CMakeLists.txt

    r14419 r14422  
    172172    ${DHT_INCLUDE_DIRS}
    173173    ${UTP_INCLUDE_DIRS}
     174    ${B64_INCLUDE_DIRS}
    174175)
    175176
     
    188189           ${MINIUPNPC_UPSTREAM_TARGET}
    189190           ${DHT_UPSTREAM_TARGET}
    190            ${UTP_UPSTREAM_TARGET})
     191           ${UTP_UPSTREAM_TARGET}
     192           ${B64_UPSTREAM_TARGET})
    191193    add_dependencies(${TR_NAME} ${UT})
    192194endforeach()
     
    203205    ${DHT_LIBRARIES}
    204206    ${UTP_LIBRARIES}
     207    ${B64_LIBRARIES}
    205208    ${LIBINTL_LIBRARY}
    206209    ${LIBM_LIBRARY}
  • trunk/libtransmission/Makefile.am

    r14420 r14422  
    66AM_CFLAGS = \
    77  @DHT_CFLAGS@ \
     8  @LIBB64_CFLAGS@ \
    89  @LIBUTP_CFLAGS@ \
    910  @LIBUPNP_CFLAGS@ \
     
    174175  @INTLLIBS@ \
    175176  @DHT_LIBS@ \
     177  @LIBB64_LIBS@ \
    176178  @LIBUTP_LIBS@ \
    177179  @LIBCURL_LIBS@ \
  • trunk/libtransmission/crypto-utils-openssl.c

    r14369 r14422  
    1010#include <assert.h>
    1111
    12 #include <openssl/bio.h>
    1312#include <openssl/bn.h>
    14 #include <openssl/buffer.h>
    1513#include <openssl/dh.h>
    1614#include <openssl/err.h>
     
    304302  return check_result (RAND_bytes (buffer, (int) length));
    305303}
    306 
    307 /***
    308 ****
    309 ***/
    310 
    311 void *
    312 tr_base64_encode_impl (const void * input,
    313                        size_t       input_length,
    314                        size_t     * output_length)
    315 {
    316   char * ret = NULL;
    317   int ret_length = 0;
    318   BIO * bmem;
    319   BIO * b64;
    320 
    321   assert (input != NULL);
    322   assert (input_length > 0);
    323 
    324   bmem = BIO_new (BIO_s_mem ());
    325   b64 = BIO_new (BIO_f_base64 ());
    326 
    327   BIO_set_flags (b64, BIO_FLAGS_BASE64_NO_NL);
    328   b64 = BIO_push (b64, bmem);
    329 
    330   if (check_result_eq (BIO_write (b64, input, input_length), (int) input_length) &&
    331       check_result (BIO_flush (b64)))
    332     {
    333       BUF_MEM * bptr;
    334 
    335       BIO_get_mem_ptr (b64, &bptr);
    336       ret = tr_strndup (bptr->data, bptr->length);
    337       ret_length = bptr->length;
    338     }
    339 
    340   BIO_free_all (b64);
    341 
    342   if (output_length != NULL)
    343     *output_length = (size_t) ret_length;
    344 
    345   return ret;
    346 }
    347 
    348 void *
    349 tr_base64_decode_impl (const void * input,
    350                        size_t       input_length,
    351                        size_t     * output_length)
    352 {
    353   char * ret;
    354   int ret_length;
    355   int i;
    356 
    357   assert (input != NULL);
    358   assert (input_length > 0);
    359 
    360   ret = tr_new (char, input_length + 1);
    361 
    362   /* try two times, without and with BIO_FLAGS_BASE64_NO_NL flag */
    363   for (i = 0; i < 2; ++i)
    364     {
    365       BIO * bmem = BIO_new_mem_buf ((void *) input, (int) input_length);
    366       BIO * b64 = BIO_new (BIO_f_base64 ());
    367 
    368       BIO_set_flags (b64, i == 1 ? BIO_FLAGS_BASE64_NO_NL : 0);
    369       bmem = BIO_push (b64, bmem);
    370 
    371       ret_length = BIO_read (bmem, ret, (int) input_length);
    372       if (ret_length < 0 && i == 1)
    373         log_error ();
    374 
    375       BIO_free_all (bmem);
    376 
    377       /* < 0 - fatal error, > 0 - success*/
    378       if (ret_length != 0)
    379         break;
    380     }
    381 
    382   if (ret_length < 0)
    383     {
    384       tr_free (ret);
    385       return NULL;
    386     }
    387 
    388   ret[ret_length] = '\0';
    389 
    390   if (output_length != NULL)
    391     *output_length = (size_t) ret_length;
    392 
    393   return ret;
    394 }
  • trunk/libtransmission/crypto-utils.c

    r14369 r14422  
    1212#include <stdlib.h> /* abs (), srand (), rand () */
    1313#include <string.h> /* memcpy (), memmove (), memset (), strcmp (), strlen () */
     14
     15#include <b64/cdecode.h>
     16#include <b64/cencode.h>
    1417
    1518#include "transmission.h"
     
    198201      if (input_length != 0)
    199202        {
    200           if ((ret = tr_base64_encode_impl (input, input_length, output_length)) != NULL)
    201             return ret;
     203          size_t ret_length;
     204          base64_encodestate state;
     205
     206          ret = tr_new (char, 4 * ((input_length + 2) / 3) + 1);
     207
     208          base64_init_encodestate (&state);
     209          ret_length = base64_encode_block (input, input_length, ret, &state);
     210          ret_length += base64_encode_blockend (ret + ret_length, &state);
     211
     212          if (output_length != NULL)
     213            *output_length = ret_length;
     214
     215          ret[ret_length] = '\0';
     216
     217          return ret;
    202218        }
    203219      else
     
    233249      if (input_length != 0)
    234250        {
    235           if ((ret = tr_base64_decode_impl (input, input_length, output_length)) != NULL)
    236             return ret;
     251          size_t ret_length;
     252          base64_decodestate state;
     253
     254          ret = tr_new (char, input_length / 4 * 3 + 1);
     255
     256          base64_init_decodestate (&state);
     257          ret_length = base64_decode_block (input, input_length, ret, &state);
     258
     259          if (output_length != NULL)
     260            *output_length = ret_length;
     261
     262          ret[ret_length] = '\0';
     263
     264          return ret;
    237265        }
    238266      else
  • trunk/libtransmission/crypto-utils.h

    r14369 r14422  
    183183
    184184/**
    185  * @brief Translate a block of bytes into base64 (internal, do not use).
    186  * @return a newly-allocated null-terminated string that can be freed with tr_free ()
    187  */
    188 void           * tr_base64_encode_impl (const void     * input,
    189                                         size_t           input_length,
    190                                         size_t         * output_length) TR_GNUC_MALLOC;
    191 
    192 /**
    193185 * @brief Translate a block of bytes from base64 into raw form.
    194186 * @return a newly-allocated null-terminated string that can be freed with tr_free ()
     
    203195 */
    204196void           * tr_base64_decode_str  (const char     * input,
    205                                         size_t         * output_length) TR_GNUC_MALLOC;
    206 
    207 /**
    208  * @brief Translate null-terminated string from base64 into raw form (internal, do not use).
    209  * @return a newly-allocated null-terminated string that can be freed with tr_free ()
    210  */
    211 void           * tr_base64_decode_impl (const void     * input,
    212                                         size_t           input_length,
    213197                                        size_t         * output_length) TR_GNUC_MALLOC;
    214198
  • trunk/qt/config.pri.in

    r14409 r14422  
    11DHT_LIBS = @DHT_LIBS_QT@
     2LIBB64_LIBS = @LIBB64_LIBS_QT@
    23LIBUTP_LIBS = @LIBUTP_LIBS_QT@
    34LIBUPNP_LIBS = @LIBUPNP_LIBS_QT@
  • trunk/qt/qtr.pro

    r14415 r14422  
    2929LIBS += $${LIBUTP_LIBS}
    3030LIBS += $${DHT_LIBS}
     31LIBS += $${LIBB64_LIBS}
    3132LIBS += $${LIBUPNP_LIBS}
    3233LIBS += $${LIBNATPMP_LIBS}
  • trunk/third-party/Makefile.am

    r14410 r14422  
    11if BUILD_DHT
    22  DHT_DIR = dht
     3endif
     4if BUILD_B64
     5  B64_DIR = libb64
    36endif
    47if BUILD_UTP
     
    1417SUBDIRS = \
    1518  $(DHT_DIR) \
     19  $(B64_DIR) \
    1620  $(NATPMP_DIR) \
    1721  $(MINIUPNP_DIR) \
  • trunk/utils/Makefile.am

    r14328 r14422  
    3232    @INTLLIBS@ \
    3333    @DHT_LIBS@ \
     34    @LIBB64_LIBS@ \
    3435    @LIBUTP_LIBS@ \
    3536    @LIBEVENT_LIBS@ \
Note: See TracChangeset for help on using the changeset viewer.