diff --git a/lib/Makefile.am b/lib/Makefile.am index 035b0d54..96094112 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -6,9 +6,9 @@ libfko_source_files = \ fko_decode.c fko_encryption.c fko_error.c fko_funcs.c fko_message.c \ fko_message.h fko_nat_access.c fko_rand_value.c fko_server_auth.c \ fko.h fko_limits.h fko_timestamp.c fko_hmac.c hmac.c hmac.h \ - fko_user.c fko_util.h md5.c md5.h rijndael.c rijndael.h sha1.c sha1.h \ - sha2.c sha2.h strlcat.c strlcpy.c fko_state.h fko_context.h \ - gpgme_funcs.c gpgme_funcs.h + fko_user.c fko_util.c fko_util.h md5.c md5.h rijndael.c rijndael.h \ + sha1.c sha1.h sha2.c sha2.h strlcat.c strlcpy.c fko_state.h \ + fko_context.h gpgme_funcs.c gpgme_funcs.h libfko_la_SOURCES = $(libfko_source_files) diff --git a/lib/fko_decode.c b/lib/fko_decode.c index e4642ccf..56cf1da1 100644 --- a/lib/fko_decode.c +++ b/lib/fko_decode.c @@ -42,8 +42,7 @@ fko_decode_spa_data(fko_ctx_t ctx) char *tbuf, *ndx, *tmp; int t_size, i; - if (ctx->encoded_msg_len < MIN_SPA_ENCODED_MSG_SIZE - || ctx->encoded_msg_len > MAX_SPA_ENCODED_MSG_SIZE) + if (! is_valid_encoded_msg_len(ctx->encoded_msg_len)) return(FKO_ERROR_INVALID_DATA); /* Make sure there are enough fields in the SPA packet diff --git a/lib/fko_encode.c b/lib/fko_encode.c index 8c1417bb..2587dd95 100644 --- a/lib/fko_encode.c +++ b/lib/fko_encode.c @@ -197,7 +197,7 @@ fko_encode_spa_data(fko_ctx_t ctx) ctx->encoded_msg_len = strnlen(ctx->encoded_msg, MAX_SPA_ENCODED_MSG_SIZE); - if(ctx->encoded_msg_len == MAX_SPA_ENCODED_MSG_SIZE) + if(! is_valid_encoded_msg_len(ctx->encoded_msg_len)); return(FKO_ERROR_INVALID_DATA); /* At this point we can compute the digest for this SPA data. diff --git a/lib/fko_encryption.c b/lib/fko_encryption.c index 754822be..4a5bac1c 100644 --- a/lib/fko_encryption.c +++ b/lib/fko_encryption.c @@ -50,6 +50,12 @@ _rijndael_encrypt(fko_ctx_t ctx, const char *enc_key, const int enc_key_len) unsigned char *ciphertext; int cipher_len; + if (! is_valid_encoded_msg_len(ctx->encoded_msg_len)) + return(FKO_ERROR_INVALID_DATA); + + if (! is_valid_digest_len(ctx->digest_len)) + return(FKO_ERROR_INVALID_DATA); + /* Make a bucket big enough to hold the enc msg + digest (plaintext) * and populate it appropriately. */ @@ -170,10 +176,10 @@ _rijndael_decrypt(fko_ctx_t ctx, if(pt_len < (cipher_len - 32)) return(FKO_ERROR_DECRYPTION_SIZE); - if(ctx->encoded_msg == NULL || pt_len < MIN_SPA_ENCODED_MSG_SIZE) + if(ctx->encoded_msg == NULL) return(FKO_ERROR_INVALID_DATA); - if(pt_len == MAX_SPA_ENCODED_MSG_SIZE) + if(! is_valid_encoded_msg_len(pt_len)) return(FKO_ERROR_INVALID_DATA); ctx->encoded_msg_len = pt_len; @@ -211,6 +217,12 @@ gpg_encrypt(fko_ctx_t ctx, const char *enc_key) size_t cipher_len; char *empty_key = ""; + if (! is_valid_encoded_msg_len(ctx->encoded_msg_len)) + return(FKO_ERROR_INVALID_DATA); + + if (! is_valid_digest_len(ctx->digest_len)) + return(FKO_ERROR_INVALID_DATA); + /* First make sure we have a recipient key set. */ if(ctx->gpg_recipient == NULL) @@ -342,10 +354,10 @@ gpg_decrypt(fko_ctx_t ctx, const char *dec_key) pt_len = strnlen(ctx->encoded_msg, MAX_SPA_ENCODED_MSG_SIZE); - if(ctx->encoded_msg == NULL || pt_len < MIN_SPA_ENCODED_MSG_SIZE) + if(ctx->encoded_msg == NULL) return(FKO_ERROR_INVALID_DATA); - if(pt_len == MAX_SPA_ENCODED_MSG_SIZE) + if(! is_valid_encoded_msg_len(pt_len)) return(FKO_ERROR_INVALID_DATA); ctx->encoded_msg_len = pt_len; @@ -454,7 +466,7 @@ fko_encrypt_spa_data(fko_ctx_t ctx, const char *enc_key, const int enc_key_len) * check for a somewhat arbitrary minimum length for the encoded * data. */ - if(ctx->encoded_msg_len < MIN_SPA_ENCODED_MSG_SIZE) + if (! is_valid_encoded_msg_len(ctx->encoded_msg_len)) return(FKO_ERROR_MISSING_ENCODED_DATA); /* Encrypt according to type and return... diff --git a/lib/fko_util.c b/lib/fko_util.c new file mode 100644 index 00000000..2a4f3304 --- /dev/null +++ b/lib/fko_util.c @@ -0,0 +1,67 @@ +/* + ***************************************************************************** + * + * File: fko_util.c + * + * Author: Michael Rash + * + * Purpose: Set/Get the current username. + * + * Copyright 2012 Michael Rash (mbr@cipherdyne.org) + * + * License (GNU Public License): + * + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + ***************************************************************************** +*/ +#include "fko_common.h" +#include "fko.h" + +/* Validate encoded message length +*/ +int +is_valid_encoded_msg_len(const int len) +{ + if(len < MIN_SPA_ENCODED_MSG_SIZE || len >= MAX_SPA_ENCODED_MSG_SIZE) + return(0); + + return(1); +} + +/* Validate digest length +*/ +int +is_valid_digest_len(const int len) +{ + switch(len) + { + case MD5_B64_LENGTH: + break; + case SHA1_B64_LENGTH: + break; + case SHA256_B64_LENGTH: + break; + case SHA384_B64_LENGTH: + break; + case SHA512_B64_LENGTH: + break; + default: + return(0); + } + + return(1); +} diff --git a/lib/fko_util.h b/lib/fko_util.h index 729eacce..97aeddc8 100644 --- a/lib/fko_util.h +++ b/lib/fko_util.h @@ -35,6 +35,9 @@ /* Function prototypes */ +int is_valid_encoded_msg_len(const int len); +int is_valid_digest_len(const int len); + size_t strlcat(char *dst, const char *src, size_t siz); size_t strlcpy(char *dst, const char *src, size_t siz);