| 1 | /*************************************************************************** |
|---|
| 2 | * ioSsl.cc |
|---|
| 3 | * |
|---|
| 4 | * Wed Jun 11 2008 |
|---|
| 5 | * Copyright 2008 Dmytro Milinevskyy |
|---|
| 6 | * milinevskyy@gmail.com |
|---|
| 7 | ****************************************************************************/ |
|---|
| 8 | |
|---|
| 9 | /* |
|---|
| 10 | * This program is free software; you can redistribute it and/or modify |
|---|
| 11 | * it under the terms of the GNU Lesser General Public License version 2.1 as published by |
|---|
| 12 | * the Free Software Foundation; |
|---|
| 13 | * |
|---|
| 14 | * This program is distributed in the hope that it will be useful, |
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | * GNU Library General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU Lesser General Public License |
|---|
| 20 | * along with this program; if not, write to the Free Software |
|---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * vim indentation settings |
|---|
| 26 | * set tabstop=4 |
|---|
| 27 | * set shiftwidth=4 |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #include <libdodo/directives.h> |
|---|
| 31 | |
|---|
| 32 | #ifdef OPENSSL_EXT |
|---|
| 33 | #include <sys/stat.h> |
|---|
| 34 | #include <sys/time.h> |
|---|
| 35 | #include <openssl/ssl.h> |
|---|
| 36 | #include <openssl/rand.h> |
|---|
| 37 | #include <openssl/err.h> |
|---|
| 38 | |
|---|
| 39 | #include "ioSsl.inline" |
|---|
| 40 | |
|---|
| 41 | #include <libdodo/ioSsl.h> |
|---|
| 42 | |
|---|
| 43 | namespace dodo { |
|---|
| 44 | namespace io { |
|---|
| 45 | namespace ssl { |
|---|
| 46 | __openssl___init__ __openssl___init_object__; |
|---|
| 47 | }; |
|---|
| 48 | }; |
|---|
| 49 | }; |
|---|
| 50 | |
|---|
| 51 | //------------------------------------------------------------------- |
|---|
| 52 | |
|---|
| 53 | using namespace dodo::io::ssl; |
|---|
| 54 | |
|---|
| 55 | __certificates__::__certificates__() : keyType(-1) |
|---|
| 56 | { |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | //------------------------------------------------------------------- |
|---|
| 60 | |
|---|
| 61 | __openssl___init__::__openssl___init__() |
|---|
| 62 | { |
|---|
| 63 | SSL_load_error_strings(); |
|---|
| 64 | SSL_library_init(); |
|---|
| 65 | |
|---|
| 66 | struct stat randstat; |
|---|
| 67 | |
|---|
| 68 | if (stat("/dev/random", &randstat) == -1 && stat("/dev/random", &randstat) == -1) { |
|---|
| 69 | char buf[4]; |
|---|
| 70 | struct timeval tv; |
|---|
| 71 | |
|---|
| 72 | for (int i = 0; i < 10000; ++i) { |
|---|
| 73 | if (RAND_status() == 1) |
|---|
| 74 | break; |
|---|
| 75 | |
|---|
| 76 | gettimeofday(&tv, NULL); |
|---|
| 77 | |
|---|
| 78 | buf[0] = tv.tv_usec & 0xF; |
|---|
| 79 | buf[2] = (tv.tv_usec & 0xF0) >> 4; |
|---|
| 80 | buf[3] = (tv.tv_usec & 0xF00) >> 8; |
|---|
| 81 | buf[1] = (tv.tv_usec & 0xF000) >> 12; |
|---|
| 82 | |
|---|
| 83 | RAND_add(buf, sizeof(buf), 0.1); |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | //------------------------------------------------------------------- |
|---|
| 89 | |
|---|
| 90 | __openssl___init__::~__openssl___init__() |
|---|
| 91 | { |
|---|
| 92 | ERR_free_strings(); |
|---|
| 93 | |
|---|
| 94 | RAND_cleanup(); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | //------------------------------------------------------------------- |
|---|
| 98 | |
|---|
| 99 | void |
|---|
| 100 | __openssl___init__::addEntropy() |
|---|
| 101 | { |
|---|
| 102 | struct stat randstat; |
|---|
| 103 | |
|---|
| 104 | if (stat("/dev/random", &randstat) == -1 && stat("/dev/random", &randstat) == -1) { |
|---|
| 105 | char buf[4]; |
|---|
| 106 | struct timeval tv; |
|---|
| 107 | |
|---|
| 108 | for (int i = 0; i < 10000; ++i) { |
|---|
| 109 | if (RAND_status() == 1) |
|---|
| 110 | break; |
|---|
| 111 | |
|---|
| 112 | gettimeofday(&tv, NULL); |
|---|
| 113 | |
|---|
| 114 | buf[0] = tv.tv_usec & 0xF; |
|---|
| 115 | buf[2] = (tv.tv_usec & 0xF0) >> 4; |
|---|
| 116 | buf[3] = (tv.tv_usec & 0xF00) >> 8; |
|---|
| 117 | buf[1] = (tv.tv_usec & 0xF000) >> 12; |
|---|
| 118 | |
|---|
| 119 | RAND_add(buf, sizeof(buf), 0.1); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | //------------------------------------------------------------------- |
|---|
| 125 | #endif |
|---|
| 126 | |
|---|