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
27using namespace basis;
28using namespace loggers;
29using namespace mathematics;
30using namespace structures;
31
32namespace 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#undef ALWAYS_LOG
39#define ALWAYS_LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
40#ifdef DEBUG_SSL
41 #undef LOG
42 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
43#else
44 #undef LOG
45 #define LOG(s)
46#endif
47
48const int SEED_SIZE = 100;
49 // the size of the random seed that we'll use.
50
51// our global initialization object.
53
55: c_rando(),
56 c_default_provider(NULL_POINTER),
57 c_legacy_provider(NULL_POINTER)
58{
59 FUNCDEF("ctor");
60
61 LOG("prior to provider setup");
62 // new code needed because blowfish is considered legacy code now. ugh.
63 c_legacy_provider = OSSL_PROVIDER_load(NULL_POINTER, "legacy");
64 if (!c_legacy_provider) {
65 ALWAYS_LOG("failed to load legacy openssl provider! mega boofer fail!");
66 exit(1);
67 }
68 // also load the default provider or the standard, still accepted, algorithms will not be available.
69 c_default_provider = OSSL_PROVIDER_load(NULL_POINTER, "default");
70 if (!c_default_provider) {
71 ALWAYS_LOG("failed to load default openssl provider! mega flopsweat fail!");
72 exit(1);
73 }
74 LOG("after provider setup");
75
76 LOG("prior to rand seed");
77 RAND_seed(random_bytes(SEED_SIZE).observe(), SEED_SIZE);
78 LOG("after rand seed");
79}
80
82{
83 FUNCDEF("destructor");
84 LOG("prior to crypto cleanup");
85
86 // clean up the providers again. not super necessary since the program will
87 // exit shortly, but it's good to be tidy.
88 if (c_default_provider) OSSL_PROVIDER_unload(c_default_provider);
89 if (c_legacy_provider) OSSL_PROVIDER_unload(c_legacy_provider);
90
91 CRYPTO_cleanup_all_ex_data();
92}
93
94const chaos &ssl_init::randomizer() const { return c_rando; }
95
97{
98 byte_array seed;
99 for (int i = 0; i < length; i++)
100 seed += abyte(c_rando.inclusive(0, 255));
101 return seed;
102}
103
104} //namespace.
105
106
#define ALWAYS_LOG(t)
#define LOG(s)
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:43
const mathematics::chaos & randomizer() const
provides a random number generator for any encryption routines.
Definition ssl_init.cpp:94
basis::byte_array random_bytes(int length) const
can be used to generate a random array of "length" bytes.
Definition ssl_init.cpp:96
a platform-independent way to acquire random numbers in a specific range.
Definition chaos.h:51
int inclusive(int low, int high) const
< Returns a pseudo-random number r, such that "low" <= r <= "high".
Definition chaos.h:88
#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:48
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 SAFE_STATIC_CONST(type, func_name, parms)
this version returns a constant object instead.
byte_array random_bytes(int length)