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/provider.h>
25 #include <openssl/rand.h>
26 
27 using namespace basis;
28 using namespace loggers;
29 using namespace mathematics;
30 using namespace structures;
31 
32 namespace crypto {
33 
34 //#define DEBUG_SSL
35  // uncomment to cause more debugging information to be generated, plus
36  // more checking to be performed in the SSL support.
37 
38 #ifdef DEBUG_SSL
39  #undef LOG
40  #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
41 #else
42  #undef LOG
43  #define LOG(s)
44 #endif
45 
46 const int SEED_SIZE = 100;
47  // the size of the random seed that we'll use.
48 
49 // our global initialization object.
51 
53 : c_rando()
54 {
55  FUNCDEF("ctor");
56 
57  // new code needed because blowfish is considered legacy code now. ugh.
58  OSSL_PROVIDER *legacy_provider = OSSL_PROVIDER_load(NULL_POINTER, "legacy");
59  // also load the default provider or the standard, still accepted, algorithms will not be available.
60  OSSL_PROVIDER *default_provider = OSSL_PROVIDER_load(NULL, "default");
61 //hmmm: do we need to clean up these providers?
62 
63 #ifdef DEBUG_SSL
64  LOG("prior to crypto debug init");
65  CRYPTO_malloc_debug_init();
66  LOG("prior to dbg set options");
67  CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
68  LOG("prior to mem ctrl");
69  CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
70 #endif
71  LOG("prior to rand seed");
72  RAND_seed(random_bytes(SEED_SIZE).observe(), SEED_SIZE);
73  LOG("after rand seed");
74 }
75 
76 ssl_init::~ssl_init()
77 {
78  FUNCDEF("destructor");
79  LOG("prior to crypto cleanup");
80  CRYPTO_cleanup_all_ex_data();
81 
82 //hmmm: deprecated
83 // LOG("prior to err remove state");
84 // ERR_remove_thread_state(NULL);
85 
86 
87 //THIS HAD TO be removed in most recent openssl; does it exist?
88 // LOG("prior to mem leaks fp");
89 // CRYPTO_mem_leaks_fp(stderr);
90 // LOG("after mem leaks fp");
91 }
92 
93 const chaos &ssl_init::randomizer() const { return c_rando; }
94 
96 {
97  byte_array seed;
98  for (int i = 0; i < length; i++)
99  seed += abyte(c_rando.inclusive(0, 255));
100  return seed;
101 }
102 
103 } //namespace.
104 
105 
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 NULL_POINTER
The value representing a pointer to nothing.
Definition: definitions.h:32
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:54
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:46
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:43
#define SAFE_STATIC_CONST(type, func_name, parms)
this version returns a constant object instead.
#define randomizer()
byte_array random_bytes(int length)