mod_entropy.c
4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* \file
*
* this filter generates a sha1 from the current microtime and request
* useses this to fill the linux random source.
*
* inspired by timed_entropyd.
*
* ATTENTION: This module is not portable right now as i don't know
* howto fill the random source for other systems. It is linux only.
*
* Most time was spend in figuring out how to write apache modules.
*
* \author Georg Hopp <georg@steffers.org
*
* \copyright
* Copyright © 2012 Georg Hopp
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _POSIX_C_SOURCE 199309L
#include "httpd.h"
#include "http_core.h"
#include "http_connection.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "ap_release.h"
#include "apu.h"
#include "apr_general.h"
#include "apr_sha1.h"
#include <time.h>
int add_entropy(const unsigned char *, size_t);
module AP_MODULE_DECLARE_DATA entropy_module;
/**
* add header values to sha1
*/
static
int
header_do_print(void * rec, const char * key, const char * value)
{
apr_sha1_ctx_t * sha1_ctx = rec;
apr_sha1_update(sha1_ctx, value, strlen(value));
return 1;
}
static
apr_status_t
entropy_filter_in(
ap_filter_t * filter,
apr_bucket_brigade * brigade,
ap_input_mode_t mode,
apr_read_type_e block,
apr_off_t readbytes)
{
apr_bucket * bucket;
apr_status_t status;
request_rec * request = filter->r;
conn_rec * connection = filter->c;
apr_sha1_ctx_t sha1_ctx;
unsigned char digest[APR_SHA1_DIGESTSIZE];
ap_version_t ap_version;
char * remote_ip;
struct timespec ts;
ap_get_server_revision(&ap_version);
clock_gettime(CLOCK_REALTIME, &ts);
apr_sha1_init(&sha1_ctx);
/**
* add current microtime to sha1
*/
apr_sha1_update_binary(
&sha1_ctx,
(const unsigned char *)&ts,
sizeof(ts));
/**
* add client ip to sha1
*/
#if 4 > AP_SERVER_MINORVERSION_NUMBER
remote_ip = connection->remote_ip;
#else
remote_ip = connection->client_ip;
#endif
apr_sha1_update(&sha1_ctx, remote_ip, strlen(remote_ip));
/**
* add request line to sha1
*/
apr_sha1_update(
&sha1_ctx,
request->the_request,
strlen(request->the_request));
/**
* add all header values to sha1
*/
apr_table_do(header_do_print, &sha1_ctx, request->headers_in, NULL);
/**
* get the request body and add it to the sha1
*/
status = ap_get_brigade(filter->next, brigade, mode, block, readbytes);
if (status == APR_SUCCESS) {
for (
bucket = APR_BRIGADE_FIRST(brigade);
bucket != APR_BRIGADE_SENTINEL(brigade);
bucket = APR_BUCKET_NEXT(bucket)) {
if (!(APR_BUCKET_IS_METADATA(bucket))) {
const char * buffer;
apr_size_t nbuffer;
status = apr_bucket_read(
bucket,
&buffer,
&nbuffer,
APR_BLOCK_READ);
if (status == APR_SUCCESS) {
apr_sha1_update(&sha1_ctx, buffer, nbuffer);
}
}
}
}
/**
* get the sha1 digest
*/
apr_sha1_final(digest, &sha1_ctx);
/**
* fill /dev/random with sha1 from current request
*/
add_entropy(digest, APR_SHA1_DIGESTSIZE);
/**
* after we are done remove us from filter queue
*/
ap_remove_input_filter(filter);
return status;
}
/**
* apache module initialization
*/
static
void
entropy_register_hook(apr_pool_t *p)
{
ap_register_input_filter(
"ENTROPY",
entropy_filter_in,
NULL,
AP_FTYPE_CONTENT_SET);
}
module AP_MODULE_DECLARE_DATA entropy_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
NULL, /* create per-server config structure */
NULL, /* merge per-server config structures */
NULL, /* command apr_table_t */
entropy_register_hook /* register hooks */
};
// vim: set ts=4 sw=4: