Main Page | Data Structures | File List | Data Fields | Globals

common.h File Reference

This file contains the definitions for many functions used by the client and server components of this system. More...

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <openssl/rsa.h>
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "WFU.h"
#include "common_cmd.h"
#include "common_log.h"

Go to the source code of this file.

Data Structures

struct  wfu_line_array
 The wfu_line_array is used to store an array of text. More...

Defines

#define WFU_CLEAN(ptr, size)   memset(ptr,0,size)
 This macro is used to zero out memory.
#define WFU_CHK_RET(txt, code, ret)   { WFU_log( (txt) ); WFU_set_error( (code) ); return ( (ret) );}
 This macro is used to log a message, assign an error code, and return a value.
#define WFU_CHK_NULL(value, txt, code, ret)   if ( (value) == NULL ) { WFU_log( (txt) ); WFU_set_error( (code) ); return ( (ret) );}
 This macro is used to log a message, assign an error code, and return a value if the given value is NULL.
#define WFU_CHK_NEG(value, txt, code, ret)   if ( (value) < 0 ) { WFU_log( (txt) ); WFU_set_error( (code) ); return ( (ret) );}
 This macro is used to log a message, assign an error code, and return a value if the given value is less than zero.
#define WFU_CHK_ZERO(value, txt, code, ret)   if ( (value) == 0 ) { WFU_log( (txt) ); WFU_set_error( (code) ); return ( (ret) );}
 This macro is used to log a message, assign an error code, and return a value if the given value is zero.
#define WFU_CHK_SSL_2(wfu_err, wfu_ret)
 This macro will check the OpenSSL error queue using the function ERR_get_error().
#define WFU_CHK_SSL_4(ssl, ssl_ret, wfu_err, wfu_ret)
 This macro will check the OpenSSL error queue using the function ERR_get_error().

Functions

wfu_dataWFU_new_wfu_data (int len)
 Create a new wfu_data structure and allocate len number of bytes for the structure.
void WFU_free_wfu_data (struct wfu_data *src)
 Free the memory used for the wfu_data structure and the memory referenced within the structure.
wfu_dataWFU_string2wfu_data (char *src)
 Create a wfu_data container for a null terminated character array.
char * WFU_wfu_data2string (struct wfu_data *src)
 Convert the data referenced in the wfu_data structure to a null terminated character array.
wfu_line_arrayWFU_new_line_array (int len)
 Allocate a new wfu_line_array structure given the specified length.
void WFU_free_wfu_line_array (struct wfu_line_array *src)
 Free the memory for a wfu_line_array structure.
int WFU_line_array_set (struct wfu_line_array *dst, int i, char *s)
 Copy the string pointed to by s into the i-th entry of the wfu_line_array structure.
char * WFU_line_array_get (struct wfu_line_array *dst, int i)
 Return an entry from the wfu_line_array structure.
int WFU_line_array_len (struct wfu_line_array *dst)
 Return the length of the wfu_line_array structure.
char * WFU_bin2hex (struct wfu_data *src)
 Convert the data contained in the wfu_data structure to a null-terminated character array of hex characters.
wfu_dataWFU_hex2bin (char *src)
 Convert the null-terminated character array of hex characters to a binary array stored in a wfu_data structure.
void WFU_print_bn2hex (char *name, BIGNUM *bn)
 Debugging function.
void WFU_print_key (RSA *key)
 Debugging function.
RSA * WFU_read_key ()
 Convert the RSA key information into a RSA structure.
int WFU_rsa_init ()
 Initialize the RSA system.
wfu_dataWFU_rsa_encrypt (struct wfu_data *src, RSA *key)
 Encrypt the information in the src wfu_data structure using the keys in the RSA structure.
wfu_dataWFU_rsa_decrypt (struct wfu_data *src, RSA *key)
 Decrypt the information in the src wfu_data structure using the keys in the RSA structure.
int WFU_SSL_read_int (SSL *ssl)
 Read an integer from the SSL connection.
int WFU_SSL_write_int (SSL *ssl, int value)
 Write an integer to the SSL connection.
char * WFU_SSL_read_string (SSL *ssl)
 Read a character string from the SSL connection.
int WFU_SSL_write_string (SSL *ssl, char *string)
 Write a null-terminated character string to the SSL connection.
wfu_dataWFU_SSL_read_wfu_data (SSL *ssl)
 Read a wfu_data structure from the SSL connection.
int WFU_SSL_write_wfu_data (SSL *ssl, struct wfu_data *src)
 Write a wfu_data structure to the SSL connection.
int WFU_SSL_buffer_read (SSL *ssl, char *buffer, int length)
 Read a specified number of bytes from the SSL connection into a buffer.
int WFU_SSL_buffer_write (SSL *ssl, char *buffer, int length)
 Write a specified number of bytes from the buffer to the SSL connection.
char * WFU_SSL_error_text (int code)
 Returns an a pointer to a static string explaining the SSL error indicated by the error code.


Detailed Description

This file contains the definitions for many functions used by the client and server components of this system.


Define Documentation

#define WFU_CHK_NEG value,
txt,
code,
ret   )     if ( (value) < 0 ) { WFU_log( (txt) ); WFU_set_error( (code) ); return ( (ret) );}
 

This macro is used to log a message, assign an error code, and return a value if the given value is less than zero.

Parameters:
value The value to be compared with 0.
txt The text message to be written to the log.
code An error code to add to the wfu_error_no.
ret The return value.

#define WFU_CHK_NULL value,
txt,
code,
ret   )     if ( (value) == NULL ) { WFU_log( (txt) ); WFU_set_error( (code) ); return ( (ret) );}
 

This macro is used to log a message, assign an error code, and return a value if the given value is NULL.

Parameters:
value The value to be compared with NULL.
txt The text message to be written to the log.
code An error code to add to the wfu_error_no.
ret The return value.

#define WFU_CHK_RET txt,
code,
ret   )     { WFU_log( (txt) ); WFU_set_error( (code) ); return ( (ret) );}
 

This macro is used to log a message, assign an error code, and return a value.

Parameters:
txt The text message to be written to the log.
code An error code to add to the wfu_error_no.
ret The return value.

#define WFU_CHK_SSL_2 wfu_err,
wfu_ret   ) 
 

Value:

if ( (wfu_error_buffer=ERR_get_error()) ) \
    { \
      for( ; wfu_error_buffer; wfu_error_buffer=ERR_get_error()) \
        { \
          snprintf(wfu_log_buffer,LOG_BUFFER_SIZE, \
            "ERR_error_string('%s')\n",ERR_error_string(wfu_error_buffer,NULL)); \
          WFU_log(NULL); \
        } \
      WFU_set_error( wfu_err );  \
      return ( wfu_ret ); \
    }
This macro will check the OpenSSL error queue using the function ERR_get_error().

If an error is present in the error queue, the error message will be converted into text and written to the log. In addition, wfu_err will be recorded in wfu_error_no and wfu_ret will be returned.

Parameters:
wfu_err The error code to add to wfu_error_no.
wfu_ret The return value.

#define WFU_CHK_SSL_4 ssl,
ssl_ret,
wfu_err,
wfu_ret   ) 
 

Value:

if ( (wfu_error_buffer=ERR_get_error()) ) \
    { \
      for( ; wfu_error_buffer; wfu_error_buffer=ERR_get_error()) \
        { \
          snprintf(wfu_log_buffer,LOG_BUFFER_SIZE, \
            "ERR_error_string('%s')\n",ERR_error_string(wfu_error_buffer,NULL)); \
          WFU_log(NULL); \
        } \
      WFU_set_error( wfu_err );  \
      return ( wfu_ret ); \
    } \
  if  (! ( SSL_ERROR_NONE == (wfu_error_buffer = SSL_get_error(ssl,ssl_ret)) ) )\
    { \
      printf("SSL_get_error = [%d] %s\n", wfu_error_buffer,WFU_SSL_error_text(wfu_error_buffer)); \
      WFU_set_error( wfu_err );  \
      return ( wfu_ret ); \
    }
This macro will check the OpenSSL error queue using the function ERR_get_error().

If an error is present in the error queue, the error message will be converted into text and written to the log. In addition, wfu_err will be recorded in wfu_error_no and wfu_ret will be returned.

If no errors were found in the error queue, then SSL_get_error() is checked for error conditions. If an error is present, the error message will be converted into text using WFU_SSL_error_text() and written to the log. In addition, wfu_err will be recorded in wfu_error_no and wfu_ret will be returned.

Parameters:
ssl The ssl structure used for the previous SSL function call.
ssl_ret The return value from the previous SSL function call.
wfu_err The error code to add to wfu_error_no.
wfu_ret The return value.

#define WFU_CHK_ZERO value,
txt,
code,
ret   )     if ( (value) == 0 ) { WFU_log( (txt) ); WFU_set_error( (code) ); return ( (ret) );}
 

This macro is used to log a message, assign an error code, and return a value if the given value is zero.

Parameters:
value The value to be compared with 0.
txt The text message to be written to the log.
code An error code to add to the wfu_error_no.
ret The return value.

#define WFU_CLEAN ptr,
size   )     memset(ptr,0,size)
 

This macro is used to zero out memory.

Parameters:
ptr A pointer to the memory location to be cleaned.
size The size of the memory to be cleaned.


Function Documentation

char* WFU_bin2hex struct wfu_data src  ) 
 

Convert the data contained in the wfu_data structure to a null-terminated character array of hex characters.

Parameters:
src A pointer to a wfu_data structure.
Return values:
NULL indicates failure
Poiner Upon success, a pointer to the newly created character array.
Note:
The memory referenced by the return value should be deallocated.

void WFU_free_wfu_data struct wfu_data src  ) 
 

Free the memory used for the wfu_data structure and the memory referenced within the structure.

Parameters:
src A pointer to the wfu_data structure to be deallocated.

void WFU_free_wfu_line_array struct wfu_line_array src  ) 
 

Free the memory for a wfu_line_array structure.

Parameters:
src A pointer to a wfu_line_array structure.

struct wfu_data* WFU_hex2bin char *  src  ) 
 

Convert the null-terminated character array of hex characters to a binary array stored in a wfu_data structure.

Parameters:
src A pointer to a null-terminated character string.
Return values:
NULL indicates failure
Poiner Upon success, a pointer to the newly created character array.
Note:
The memory referenced by the return value should be deallocated using WFU_free_wfu_data().

char* WFU_line_array_get struct wfu_line_array dst,
int  i
 

Return an entry from the wfu_line_array structure.

Parameters:
dst A pointer to a wfu_line_array structure.
i The index to be returned.
Return values:
Pointer Upon success, a pointer to the character array at entry i.
NULL indicates failure.

int WFU_line_array_len struct wfu_line_array dst  ) 
 

Return the length of the wfu_line_array structure.

Parameters:
dst A pointer to a wfu_line_array structure.
Return values:
0 indicates a null array.
>0 indicates the length of the array.

int WFU_line_array_set struct wfu_line_array dst,
int  i,
char *  s
 

Copy the string pointed to by s into the i-th entry of the wfu_line_array structure.

Parameters:
dst A pointer to a wfu_line_array structure.
i The index of the target entry.
s The source string to be copied.
Return values:
0 indicates success.
1 indicates failure.

struct wfu_line_array* WFU_new_line_array int  len  ) 
 

Allocate a new wfu_line_array structure given the specified length.

Parameters:
len The number of strings that will be stored in this array
Return values:
Pointer A pointer to the newly allocated wfu_line_array structure.
NULL A NULL value is returned upon failure.
Note:
The memory referenced by the return value should be deallocated using WFU_free_wfu_line_array().

struct wfu_data* WFU_new_wfu_data int  len  ) 
 

Create a new wfu_data structure and allocate len number of bytes for the structure.

Parameters:
len The number of bytes to allocate for the wfu_data structure.
Return values:
Pointer A pointer to the newly allocated wfu_data structure.
NULL A NULL value is returned upon failure.
Note:
The memory referenced by the return value should be deallocated using WFU_free_wfu_data().

RSA* WFU_read_key  ) 
 

Convert the RSA key information into a RSA structure.

Return values:
NULL indicates failure
Poiner Upon success, a pointer to the newly created RSA structure.
Note:
The memory referenced by the return value should be deallocated using WFU_free_wfu_data().

struct wfu_data* WFU_rsa_decrypt struct wfu_data src,
RSA *  key
 

Decrypt the information in the src wfu_data structure using the keys in the RSA structure.

Parameters:
src A pointer to the wfu_data structure to be decrypted.
key A pointer to the RSA structure containing the key information.
Return values:
NULL indicates failure
Poiner Upon success, the function returns a pointer to the newly created wfu_data structure containing the decrypted information.
Note:
The memory referenced by the return value should be deallocated using WFU_free_wfu_data().

struct wfu_data* WFU_rsa_encrypt struct wfu_data src,
RSA *  key
 

Encrypt the information in the src wfu_data structure using the keys in the RSA structure.

Parameters:
src A pointer to the wfu_data structure to be encrypted.
key A pointer to the RSA structure containing the key information.
Return values:
NULL indicates failure
Poiner Upon success, the function returns a pointer to the newly created wfu_data structure containing the encrypted information.
Note:
The memory referenced by the return value should be deallocated using WFU_free_wfu_data().

int WFU_SSL_buffer_read SSL *  ssl,
char *  buffer,
int  length
 

Read a specified number of bytes from the SSL connection into a buffer.

Parameters:
ssl A pointer to the SSL structure.
buffer A pointer to the buffer.
length The number of bytes to read.
Return values:
-1 indicates failure.
>0 Indicates success. The return value indicates the number of bytes read from the connection.
Note:
The wfu_error_no will contain WFU_ERR_SSL_RW and WFU_ERR_SSL upon failure.

int WFU_SSL_buffer_write SSL *  ssl,
char *  buffer,
int  length
 

Write a specified number of bytes from the buffer to the SSL connection.

Parameters:
ssl A pointer to the SSL structure.
buffer A pointer to the buffer.
length The number of bytes to written.
Return values:
-1 indicates failure.
>0 Indicates success. The return value indicates the number of bytes written to the connection.
Note:
The wfu_error_no will contain WFU_ERR_SSL_RW and WFU_ERR_SSL upon failure.

char* WFU_SSL_error_text int  code  ) 
 

Returns an a pointer to a static string explaining the SSL error indicated by the error code.

Parameters:
code The error code.

int WFU_SSL_read_int SSL *  ssl  ) 
 

Read an integer from the SSL connection.

Parameters:
ssl A pointer to the SSL structure.
Return values:
int The return value is the integer read from the ssl connection.
Note:
The wfu_error_no will contain WFU_ERR_SSL_RW and WFU_ERR_SSL upon failure.

char* WFU_SSL_read_string SSL *  ssl  ) 
 

Read a character string from the SSL connection.

Parameters:
ssl A pointer to the SSL structure.
Return values:
NULL indicates failure.
Pointer Upon success, the function will return a pointer to a null-terminated character array.
Note:
The wfu_error_no will contain WFU_ERR_SSL_RW and WFU_ERR_SSL upon failure.

The memory for the return value should be deallocated.

struct wfu_data* WFU_SSL_read_wfu_data SSL *  ssl  ) 
 

Read a wfu_data structure from the SSL connection.

Parameters:
ssl A pointer to the SSL structure.
Return values:
NULL indicates failure.
Pointer This function returns a pointer to a newly allocated wfu_data structure upon success.
Note:
The wfu_error_no will contain WFU_ERR_SSL_RW and WFU_ERR_SSL upon failure.

The memory for the return value should be deallocated.

int WFU_SSL_write_int SSL *  ssl,
int  value
 

Write an integer to the SSL connection.

Parameters:
ssl A pointer to the SSL structure.
value The integer to be sent.
Return values:
>0 indicates success.
-1 indicates failure.
Note:
The wfu_error_no will contain WFU_ERR_SSL_RW and WFU_ERR_SSL upon failure.

int WFU_SSL_write_string SSL *  ssl,
char *  string
 

Write a null-terminated character string to the SSL connection.

Parameters:
ssl A pointer to the SSL structure.
string A pointer to the character array to be written.
Return values:
-1 indicates failure.
>0 indicates success.
Note:
The wfu_error_no will contain WFU_ERR_SSL_RW and WFU_ERR_SSL upon failure.

int WFU_SSL_write_wfu_data SSL *  ssl,
struct wfu_data src
 

Write a wfu_data structure to the SSL connection.

Parameters:
ssl A pointer to the SSL structure.
src A pointer to the wfu_data structure.
Return values:
-1 indicates failure.
>0 indicates success.
Note:
The wfu_error_no will contain WFU_ERR_SSL_RW and WFU_ERR_SSL upon failure.

struct wfu_data* WFU_string2wfu_data char *  src  ) 
 

Create a wfu_data container for a null terminated character array.

Parameters:
src A pointer to the source character string.
Note:
This function will allocate the memory necessary to store a copy of the string. This copy will not be null terminated.

The memory referenced by the return value should be deallocated using WFU_free_wfu_data().

char* WFU_wfu_data2string struct wfu_data src  ) 
 

Convert the data referenced in the wfu_data structure to a null terminated character array.

Parameters:
src A pointer to the wfu_data structure.
Note:
The memory referenced by the return value should be deallocated.


Generated on Wed Oct 10 12:38:20 2007 for WFUCrypt by  doxygen 1.3.9.1