From 30a95c3c82e2f77ca399363fa0fbab3a7525b81a Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Tue, 7 Jan 2014 21:32:05 -0500 Subject: [PATCH] Make libtpms compileable with OpenSSL Implement missing base64 decoder support when using OpenSSL library. Signed-off-by: Stefan Berger Acked-by: Corey Bryant --- config.h.in | 6 +++++ configure.in | 6 +++++ src/tpm_library.c | 63 ++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/config.h.in b/config.h.in index d32aea7d..c0429a1c 100644 --- a/config.h.in +++ b/config.h.in @@ -64,6 +64,12 @@ /* Define to 1 if you have the ANSI C header files. */ #undef STDC_HEADERS +/* use freebl crypto library */ +#undef USE_FREEBL_CRYPTO_LIBRARY + +/* use openssl crypto library */ +#undef USE_OPENSSL_CRYPTO_LIBRARY + /* Version number of package */ #undef VERSION diff --git a/configure.in b/configure.in index 1ae1584d..2fc0fc7d 100644 --- a/configure.in +++ b/configure.in @@ -74,10 +74,16 @@ case "$cryptolib" in freebl) AM_CONDITIONAL(LIBTPMS_USE_FREEBL, true) AM_CONDITIONAL(LIBTPMS_USE_OPENSSL, false) + AC_DEFINE([USE_FREEBL_CRYPTO_LIBRARY], + [1], + [use freebl crypto library]) ;; openssl) AM_CONDITIONAL(LIBTPMS_USE_FREEBL, false) AM_CONDITIONAL(LIBTPMS_USE_OPENSSL, true) + AC_DEFINE([USE_OPENSSL_CRYPTO_LIBRARY], + [1], + [use openssl crypto library]) ;; esac diff --git a/src/tpm_library.c b/src/tpm_library.c index a9b9f3b2..99198056 100644 --- a/src/tpm_library.c +++ b/src/tpm_library.c @@ -37,13 +37,22 @@ /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /********************************************************************************/ +#include + #include #include #include #include #include -#include +#ifdef USE_FREEBL_CRYPTO_LIBRARY +# include +#endif + +#ifdef USE_OPENSSL_CRYPTO_LIBRARY +# include +# include +#endif #include "tpm_debug.h" #include "tpm_error.h" @@ -256,6 +265,46 @@ static int is_base64ltr(char c) c == '='); } +#ifdef USE_OPENSSL_CRYPTO_LIBRARY +static unsigned char *TPMLIB_OpenSSL_Base64Decode(char *input, + unsigned int outputlen) +{ + BIO *b64, *bmem; + unsigned char *res = NULL; + int n; + TPM_RESULT rc; + + b64 = BIO_new(BIO_f_base64()); + if (!b64) { + return NULL; + } + + bmem = BIO_new_mem_buf(input, strlen(input)); + if (!bmem) { + BIO_free(b64); + goto cleanup; + } + bmem = BIO_push(b64, bmem); + BIO_set_flags(bmem, BIO_FLAGS_BASE64_NO_NL); + + rc = TPM_Malloc(&res, outputlen); + if (rc != TPM_SUCCESS) { + goto cleanup; + } + + n = BIO_read(bmem, res, outputlen); + if (n <= 0) { + TPM_Free(res); + res = NULL; + goto cleanup; + } + +cleanup: + BIO_free_all(bmem); + + return res; +} +#endif /* * Base64 decode the string starting at 'start' and the last @@ -289,10 +338,12 @@ static unsigned char *TPMLIB_Base64Decode(const char *start, const char *end, while (s < end) { c = *s; - if (c != '=' && is_base64ltr(c)) { + if (is_base64ltr(c)) { *d = c; d++; - numbase64chars++; + if (c != '=') { + numbase64chars++; + } } else if (c == 0) { break; } @@ -314,7 +365,13 @@ static unsigned char *TPMLIB_Base64Decode(const char *start, const char *end, break; } +#ifdef USE_FREEBL_CRYPTO_LIBRARY ret = (unsigned char *)PL_Base64Decode(input, 0, NULL); +#endif + +#ifdef USE_OPENSSL_CRYPTO_LIBRARY + ret = TPMLIB_OpenSSL_Base64Decode(input, *length); +#endif err_exit: free(input);