added lib/fko_util.c with basic length checking functions

This commit is contained in:
Michael Rash
2012-07-26 18:01:36 -04:00
parent bdb6cc0eb1
commit d561fdd4d7
6 changed files with 92 additions and 11 deletions
+3 -3
View File
@@ -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)
+1 -2
View File
@@ -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
+1 -1
View File
@@ -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.
+17 -5
View File
@@ -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...
+67
View File
@@ -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);
}
+3
View File
@@ -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);