working through some issues
[feisty_meow.git] / nucleus / library / crypto / ssl_init.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : SSL initialization helper                                         *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 2005-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "ssl_init.h"
16
17 #include <basis/functions.h>
18 #include <basis/mutex.h>
19 #include <loggers/program_wide_logger.h>
20 #include <structures/static_memory_gremlin.h>
21
22 #include <openssl/crypto.h>
23 #include <openssl/err.h>
24 #include <openssl/rand.h>
25
26 using namespace basis;
27 using namespace loggers;
28 using namespace mathematics;
29 using namespace structures;
30
31 namespace crypto {
32
33 //#define DEBUG_SSL
34   // uncomment to cause more debugging information to be generated, plus
35   // more checking to be performed in the SSL support.
36
37 #ifdef DEBUG_SSL
38   #undef LOG
39   #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
40 #else
41   #undef LOG
42   #define LOG(s)
43 #endif
44
45 const int SEED_SIZE = 100;
46   // the size of the random seed that we'll use.
47
48 // our global initialization object.
49 SAFE_STATIC_CONST(ssl_init, static_ssl_initializer, )
50
51 ssl_init::ssl_init()
52 : c_rando()
53 {
54   FUNCDEF("ctor");
55 #ifdef DEBUG_SSL
56   LOG("prior to crypto debug init");
57   CRYPTO_malloc_debug_init();
58   LOG("prior to dbg set options");
59   CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
60   LOG("prior to mem ctrl");
61   CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
62 #endif
63   LOG("prior to rand seed");
64   RAND_seed(random_bytes(SEED_SIZE).observe(), SEED_SIZE);
65   LOG("after rand seed");
66 }
67
68 ssl_init::~ssl_init()
69 {
70   FUNCDEF("destructor");
71   LOG("prior to crypto cleanup");
72   CRYPTO_cleanup_all_ex_data();
73
74 //hmmm: deprecated
75 //  LOG("prior to err remove state");
76 //  ERR_remove_thread_state(NULL);
77
78
79 //THIS HAD TO be removed in most recent openssl; does it exist?
80 //  LOG("prior to mem leaks fp");
81 //  CRYPTO_mem_leaks_fp(stderr);
82 //  LOG("after mem leaks fp");
83 }
84
85 const chaos &ssl_init::randomizer() const { return c_rando; }
86
87 byte_array ssl_init::random_bytes(int length) const
88 {
89   byte_array seed;
90   for (int i = 0; i < length; i++)
91     seed += abyte(c_rando.inclusive(0, 255));
92   return seed;
93 }
94
95 } //namespace.
96