From f4a8c0fda84ec5ebafb68506ff0059f3dbeae396 Mon Sep 17 00:00:00 2001 From: Michael Rash Date: Fri, 18 Apr 2014 21:39:54 -0400 Subject: [PATCH] [libfko] for fuzzing purposes, added fko_set_encoded_data() to bypass encryption and authentication for SPA payloads --- lib/fko.h | 2 ++ lib/fko_encode.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/lib/fko.h b/lib/fko.h index 4c301b07..f4e5f0b4 100644 --- a/lib/fko.h +++ b/lib/fko.h @@ -381,6 +381,8 @@ DLL_API int fko_set_spa_hmac(fko_ctx_t ctx, const char * const hmac_key, DLL_API int fko_get_spa_hmac(fko_ctx_t ctx, char **enc_data); DLL_API int fko_get_encoded_data(fko_ctx_t ctx, char **enc_data); +DLL_API int fko_set_encoded_data(fko_ctx_t ctx, const char * const encoded_msg, + const int msg_len, const int do_digest, const int digest_type); /* Get context data functions */ diff --git a/lib/fko_encode.c b/lib/fko_encode.c index 889efeb5..06128cfe 100644 --- a/lib/fko_encode.c +++ b/lib/fko_encode.c @@ -241,4 +241,74 @@ fko_get_encoded_data(fko_ctx_t ctx, char **enc_msg) return(FKO_SUCCESS); } +/* Set the fko SPA encoded data (this is a convenience + * function mostly used for tests that involve fuzzing). +*/ +int +fko_set_encoded_data(fko_ctx_t ctx, + const char * const encoded_msg, const int msg_len, + const int require_digest, const int digest_type) +{ + char *tbuf = NULL; + int res = FKO_SUCCESS, mlen; + + /* Must be initialized + */ + if(!CTX_INITIALIZED(ctx)) + return(FKO_ERROR_CTX_NOT_INITIALIZED); + + if(encoded_msg == NULL) + return(FKO_ERROR_INVALID_DATA); + + ctx->encoded_msg = strdup(encoded_msg); + + ctx->state |= FKO_DATA_MODIFIED; + + if(ctx->encoded_msg == NULL) + return(FKO_ERROR_MEMORY_ALLOCATION); + + /* allow arbitrary length (i.e. let the decode routines validate + * SPA message length). + */ + ctx->encoded_msg_len = msg_len; + + if(require_digest) + { + fko_set_spa_digest_type(ctx, FKO_DIGEST_SHA256); + if((res = fko_set_spa_digest(ctx)) != FKO_SUCCESS) + { + return res; + } + + /* append the digest to the encoded message buffer + */ + mlen = ctx->encoded_msg_len + ctx->digest_len + 2; + tbuf = calloc(1, mlen); + if(tbuf == NULL) + return(FKO_ERROR_MEMORY_ALLOCATION); + + /* memcpy since the provided encoded buffer might + * have an embedded NULL? + */ + mlen = snprintf(tbuf, mlen, "%s:%s", ctx->encoded_msg, ctx->digest); + + if(ctx->encoded_msg != NULL) + free(ctx->encoded_msg); + + ctx->encoded_msg = strdup(tbuf); + if(ctx->encoded_msg == NULL) + { + free(tbuf); + return(FKO_ERROR_MEMORY_ALLOCATION); + } + + ctx->encoded_msg_len = mlen; + free(tbuf); + } + + FKO_CLEAR_SPA_DATA_MODIFIED(ctx); + + return(FKO_SUCCESS); +} + /***EOF***/