feisty meow concerns codebase  2.140
ssl_init.cpp
Go to the documentation of this file.
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>
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.
50 
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 
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 
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
provides some initialization for the RSA and blowfish crypto.
Definition: ssl_init.h:40
a platform-independent way to acquire random numbers in a specific range.
Definition: chaos.h:51
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:57
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
unsigned char abyte
A fairly important unit which is seldom defined...
Definition: definitions.h:51
const int SEED_SIZE
Definition: ssl_init.cpp:45
const ssl_init & static_ssl_initializer()
the main method for accessing the SSL initialization support.
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition: averager.h:21
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
#define LOG(s)
Definition: ssl_init.cpp:42
#define SAFE_STATIC_CONST(type, func_name, parms)
this version returns a constant object instead.
#define randomizer()
byte_array random_bytes(int length)