From 09acd289e72fc40e91f9f1daf7554d26818d33bb Mon Sep 17 00:00:00 2001 From: Allan McRae Date: Sun, 25 Sep 2016 20:31:01 +1000 Subject: use openssl for base64 --- lib/libalpm/Makefile.am | 1 - lib/libalpm/base64.c | 196 ------------------------------------------------ lib/libalpm/base64.h | 72 ------------------ lib/libalpm/signing.c | 17 +---- lib/libalpm/util.c | 35 +++++++++ lib/libalpm/util.h | 1 + 6 files changed, 38 insertions(+), 284 deletions(-) delete mode 100644 lib/libalpm/base64.c delete mode 100644 lib/libalpm/base64.h diff --git a/lib/libalpm/Makefile.am b/lib/libalpm/Makefile.am index 945a6121..8cff7fc9 100644 --- a/lib/libalpm/Makefile.am +++ b/lib/libalpm/Makefile.am @@ -28,7 +28,6 @@ libalpm_la_SOURCES = \ alpm.h alpm.c \ alpm_list.h alpm_list.c \ backup.h backup.c \ - base64.h base64.c \ be_local.c \ be_package.c \ be_sync.c \ diff --git a/lib/libalpm/base64.c b/lib/libalpm/base64.c deleted file mode 100644 index 31ecca11..00000000 --- a/lib/libalpm/base64.c +++ /dev/null @@ -1,196 +0,0 @@ -/* - * RFC 1521 base64 encoding/decoding - * - * Copyright (C) 2006-2010, Brainspark B.V. - * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -/* - * Pacman Notes: - * - * Taken from the PolarSSL project at www.polarssl.org under terms of the - * GPL. This is from version 0.14.2 of the library, and has been modified - * as following, which may be helpful for future updates: - * * remove "polarssl/config.h" include - * * change include from "polarssl/base64.h" to "base64.h" - * * removal of SELF_TEST code - */ - -#include - -#include "base64.h" - -#if 0 -static const unsigned char base64_enc_map[64] = -{ - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', - 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', - 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', - 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', - 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', - 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', '+', '/' -}; -#endif - -static const unsigned char base64_dec_map[128] = -{ - 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, - 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, - 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, - 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, - 127, 127, 127, 62, 127, 127, 127, 63, 52, 53, - 54, 55, 56, 57, 58, 59, 60, 61, 127, 127, - 127, 64, 127, 127, 127, 0, 1, 2, 3, 4, - 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, - 25, 127, 127, 127, 127, 127, 127, 26, 27, 28, - 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, - 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, - 49, 50, 51, 127, 127, 127, 127, 127 -}; - -#if 0 -/* - * Encode a buffer into base64 format - */ -int base64_encode( unsigned char *dst, size_t *dlen, - const unsigned char *src, size_t slen ) -{ - size_t i, n; - int C1, C2, C3; - unsigned char *p; - - if( slen == 0 ) - return( 0 ); - - n = (slen << 3) / 6; - - switch( (slen << 3) - (n * 6) ) - { - case 2: n += 3; break; - case 4: n += 2; break; - default: break; - } - - if( *dlen < n + 1 ) - { - *dlen = n + 1; - return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL ); - } - - n = (slen / 3) * 3; - - for( i = 0, p = dst; i < n; i += 3 ) - { - C1 = *src++; - C2 = *src++; - C3 = *src++; - - *p++ = base64_enc_map[(C1 >> 2) & 0x3F]; - *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F]; - *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F]; - *p++ = base64_enc_map[C3 & 0x3F]; - } - - if( i < slen ) - { - C1 = *src++; - C2 = ((i + 1) < slen) ? *src++ : 0; - - *p++ = base64_enc_map[(C1 >> 2) & 0x3F]; - *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F]; - - if( (i + 1) < slen ) - *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F]; - else *p++ = '='; - - *p++ = '='; - } - - *dlen = p - dst; - *p = 0; - - return( 0 ); -} -#endif - -/* - * Decode a base64-formatted buffer - */ -int base64_decode( unsigned char *dst, size_t *dlen, - const unsigned char *src, size_t slen ) -{ - size_t i, n; - uint32_t j, x; - unsigned char *p; - - for( i = j = n = 0; i < slen; i++ ) - { - if( ( slen - i ) >= 2 && - src[i] == '\r' && src[i + 1] == '\n' ) - continue; - - if( src[i] == '\n' ) - continue; - - if( src[i] == '=' && ++j > 2 ) - return( POLARSSL_ERR_BASE64_INVALID_CHARACTER ); - - if( src[i] > 127 || base64_dec_map[src[i]] == 127 ) - return( POLARSSL_ERR_BASE64_INVALID_CHARACTER ); - - if( base64_dec_map[src[i]] < 64 && j != 0 ) - return( POLARSSL_ERR_BASE64_INVALID_CHARACTER ); - - n++; - } - - if( n == 0 ) - return( 0 ); - - n = ((n * 6) + 7) >> 3; - - if( *dlen < n ) - { - *dlen = n; - return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL ); - } - - for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ ) - { - if( *src == '\r' || *src == '\n' ) - continue; - - j -= ( base64_dec_map[*src] == 64 ); - x = (x << 6) | ( base64_dec_map[*src] & 0x3F ); - - if( ++n == 4 ) - { - n = 0; - if( j > 0 ) *p++ = (unsigned char)( x >> 16 ); - if( j > 1 ) *p++ = (unsigned char)( x >> 8 ); - if( j > 2 ) *p++ = (unsigned char)( x ); - } - } - - *dlen = p - dst; - - return( 0 ); -} diff --git a/lib/libalpm/base64.h b/lib/libalpm/base64.h deleted file mode 100644 index 9edb864e..00000000 --- a/lib/libalpm/base64.h +++ /dev/null @@ -1,72 +0,0 @@ -/** - * \file base64.h - * - * Copyright (C) 2006-2010, Brainspark B.V. - * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef BASE64_H -#define BASE64_H - -#include - -#define POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL -0x0010 /**< Output buffer too small. */ -#define POLARSSL_ERR_BASE64_INVALID_CHARACTER -0x0012 /**< Invalid character in input. */ - -#if 0 -/** - * \brief Encode a buffer into base64 format - * - * \param dst destination buffer - * \param dlen size of the buffer - * \param src source buffer - * \param slen amount of data to be encoded - * - * \return 0 if successful, or POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL. - * *dlen is always updated to reflect the amount - * of data that has (or would have) been written. - * - * \note Call this function with *dlen = 0 to obtain the - * required buffer size in *dlen - */ -int base64_encode( unsigned char *dst, size_t *dlen, - const unsigned char *src, size_t slen ); -#endif - -/** - * \brief Decode a base64-formatted buffer - * - * \param dst destination buffer - * \param dlen size of the buffer - * \param src source buffer - * \param slen amount of data to be decoded - * - * \return 0 if successful, POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL, or - * POLARSSL_ERR_BASE64_INVALID_DATA if the input data is not - * correct. *dlen is always updated to reflect the amount - * of data that has (or would have) been written. - * - * \note Call this function with *dlen = 0 to obtain the - * required buffer size in *dlen - */ -int base64_decode( unsigned char *dst, size_t *dlen, - const unsigned char *src, size_t slen ); - -#endif /* base64.h */ diff --git a/lib/libalpm/signing.c b/lib/libalpm/signing.c index 6557c200..421e3acb 100644 --- a/lib/libalpm/signing.c +++ b/lib/libalpm/signing.c @@ -29,7 +29,6 @@ /* libalpm */ #include "signing.h" #include "package.h" -#include "base64.h" #include "util.h" #include "log.h" #include "alpm.h" @@ -46,22 +45,10 @@ int SYMEXPORT alpm_decode_signature(const char *base64_data, unsigned char **data, size_t *data_len) { - size_t len = strlen(base64_data); - unsigned char *usline = (unsigned char *)base64_data; - /* reasonable allocation of expected length is 3/4 of encoded length */ - size_t destlen = len * 3 / 4; - MALLOC(*data, destlen, goto error); - if(base64_decode(*data, &destlen, usline, len)) { - free(*data); - goto error; + if(_alpm_base64_decode(base64_data, *data, data_len)) { + return -1; } - *data_len = destlen; return 0; - -error: - *data = NULL; - *data_len = 0; - return -1; } #ifdef HAVE_LIBGPGME diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 7ef4bf34..1c535d94 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -40,6 +40,7 @@ #ifdef HAVE_LIBSSL #include #include +#include #endif /* libalpm */ @@ -936,6 +937,29 @@ static int sha256_file(const char *path, unsigned char output[32]) SHA256_Final(output, &ctx); return 0; } + +static int base64_decode(const char* in, unsigned char *out, size_t *out_len) +{ + BIO *buff, *b64f; + size_t in_len = strlen(in); + + /* reasonable allocation of expected length is 3/4 of encoded length */ + MALLOC(out, in_len * 3 / 4 + 1, return -1); + + b64f = BIO_new(BIO_f_base64()); + buff = BIO_new_mem_buf((void *)in, in_len); + buff = BIO_push(b64f, buff); + + BIO_set_flags(buff, BIO_FLAGS_BASE64_NO_NL); + BIO_set_close(buff, BIO_CLOSE); + *out_len = BIO_read(buff, out, in_len); + out = realloc(out, ((*out_len) + 1) * sizeof(char)); + out[(*out_len)] = '\0'; + + BIO_free_all(buff); + + return 0; +} #endif /** Create a string representing bytes in hexadecimal. @@ -1031,6 +1055,17 @@ int _alpm_test_checksum(const char *filepath, const char *expected, return ret; } +int _alpm_base64_decode(const char *base64_data, unsigned char *out, size_t *out_len) +{ + if(base64_decode(base64_data, out, out_len)) { + out = NULL; + *out_len = 0; + return -1; + } + + return 0; +} + /* Note: does NOT handle sparse files on purpose for speed. */ /** TODO. * Does not handle sparse files on purpose for speed. diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h index 5a2c105d..fd85dbf6 100644 --- a/lib/libalpm/util.h +++ b/lib/libalpm/util.h @@ -132,6 +132,7 @@ int _alpm_str_cmp(const void *s1, const void *s2); char *_alpm_filecache_find(alpm_handle_t *handle, const char *filename); const char *_alpm_filecache_setup(alpm_handle_t *handle); int _alpm_test_checksum(const char *filepath, const char *expected, alpm_pkgvalidation_t type); +int _alpm_base64_decode(const char *in, unsigned char *out, size_t *out_len); int _alpm_archive_fgets(struct archive *a, struct archive_read_buffer *b); int _alpm_splitname(const char *target, char **name, char **version, unsigned long *name_hash); -- cgit v1.2.3-24-g4f1b