ssl_init.cpp

Go to the documentation of this file.
00001 #ifndef SSL_INIT_IMPLEMENTATION_FILE
00002 #define SSL_INIT_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : SSL initialization helper                                         *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 2005-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "ssl_init.h"
00019 
00020 #include <basis/byte_array.h>
00021 #include <basis/function.h>
00022 #include <basis/chaos.h>
00023 #include <basis/mutex.h>
00024 #include <data_struct/static_memory_gremlin.h>
00025 
00026 #include <openssl/crypto.h>
00027 #include <openssl/err.h>
00028 #include <openssl/rand.h>
00029 
00030 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger(), s)
00031 
00032 const int SEED_SIZE = 100;
00033   // the size of the random seed that we'll use.
00034 
00035 // our global initialization object.
00036 SAFE_STATIC_CONST(ssl_init, static_ssl_initializer, )
00037 
00038 //#define DEBUG_SSL
00039   // uncomment to cause more debugging information to be generated, plus
00040   // more checking to be performed in the SSL support.
00041 
00042 ssl_init::ssl_init()
00043 : _rando(new chaos)
00044 {
00045 #ifdef DEBUG_SSL
00046   CRYPTO_malloc_debug_init();
00047   CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
00048   CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
00049 #endif
00050   RAND_seed(random_bytes(SEED_SIZE).observe(), SEED_SIZE);
00051 }
00052 
00053 ssl_init::~ssl_init()
00054 {
00055   WHACK(_rando);
00056   CRYPTO_cleanup_all_ex_data();
00057   ERR_remove_state(0);
00058   CRYPTO_mem_leaks_fp(stderr);
00059 }
00060 
00061 chaos &ssl_init::randomizer() const { return *_rando; }
00062 
00063 byte_array ssl_init::random_bytes(int length) const
00064 {
00065   byte_array seed;
00066   for (int i = 0; i < length; i++)
00067     seed += byte(_rando->inclusive(0, 255));
00068   return seed;
00069 }
00070 
00071 
00072 #endif //SSL_INIT_IMPLEMENTATION_FILE
00073 

Generated on Fri Sep 5 04:28:38 2008 for HOOPLE Libraries by  doxygen 1.5.1