Repository navigation
Expand file tree
/
Copy pathheartbleed_test.c
More file actions
446 lines (396 loc) · 14.9 KB
/
Copy pathheartbleed_test.c
File metadata and controls
446 lines (396 loc) · 14.9 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* test/heartbleed_test.c */
/*
* Unit test for TLS heartbeats.
*
* Acts as a regression test against the Heartbleed bug (CVE-2014-0160).
*
* Author: Mike Bland (mbland@acm.org, http://mike-bland.com/)
* Date: 2014-04-12
* License: Creative Commons Attribution 4.0 International (CC By 4.0)
* http://creativecommons.org/licenses/by/4.0/deed.en_US
*
* Converted from Google-style to OpenSSL style and contributed to OpenSSL as
* heartbeat_test.c:
* http://git.openssl.org/gitweb/?p=openssl.git;a=blob;f=ssl/heartbeat_test.c
* http://goo.gl/1F7SKs
*
* Pull request: https://github.com/openssl/openssl/pull/81
* Discussion:
* https://groups.google.com/d/msg/mailing.openssl.dev/N96KqJ6WgTs/YelyxMJoqmUJ
* https://groups.google.com/d/msg/mailing.openssl.dev/k_oL10ysLUo/r93f_hHZBwMJ
* https://groups.google.com/d/msg/mailing.openssl.dev/ge4PIs3byZk/Uhp0vLoGlEMJ
*
* URL:
* https://code.google.com/p/mike-bland/source/browse/heartbleed/heartbleed_test.c
* http://goo.gl/w1bGyR
*
* BUILDING AND RUNNING
* --------------------
* Download the build.sh script if you don't want to do everything by hand:
*
* https://code.google.com/p/mike-bland/source/browse/heartbleed/build.sh
* http://goo.gl/erKNJm
*
* Otherwise: As per CVE-2014-0160, download the following tarballs:
*
* The version that introduced the Heartbleed bug:
* http://www.openssl.org/source/openssl-1.0.1-beta1.tar.gz
*
* The version that contains the fix for the Heartbleed bug:
* http://www.openssl.org/source/openssl-1.0.1g.tar.gz
*
* Unpack and run ./config && make for each. Then, add these lines to the
* bottom of test/Makefile:
*
* heartbleed_test.o: heartbleed_test.c
* heartbleed_test: heartbleed_test.o $(DLIBCRYPTO)
* @target=heartbleed_test; $(BUILD_CMD)
*
* Copy this test file to test/heartbleed_test.c and run:
* $ make TESTS=heartbleed_test test
* $ test/heartbleed_test
*
* OUTPUT
* ------
* The program returns zero on success. It will print a message with a count
* of the number of failed tests and return nonzero if any tests fail.
*
* It will print the contents of the request and response buffers for each
* failing test. In a "fixed" version, all the tests should pass and there
* should be no output.
*
* In a "bleeding" version, you'll see:
*
* TestDtls1Heartbleed failed:
* expected payload len: 0
* received: 1024
* sent 26 characters
* "HEARTBLEED "
* received 1024 characters
* "HEARTBLEED \xde\xad\xbe\xef..."
* ** TestDtls1Heartbleed failed **
*
* The contents of the returned buffer in the failing test will depend on the
* contents of memory on your machine.
*
* PSEUDO-XUNIT STYLE
* ------------------
* This test is written in what I call the Pseudo-xUnit style:
* http://mike-bland.com/2014/06/05/pseudo-xunit-pattern.html
*
* MORE INFORMATION
* ----------------
* http://mike-bland.com/2014/04/12/heartbleed.html
* http://mike-bland.com/tags/heartbleed.html
*/
#include "../ssl/ssl_locl.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* As per https://tools.ietf.org/html/rfc6520#section-4 */
const int kMinPaddingSize = 16;
/* Maximum number of payload characters to print as test output */
const int kMaxPrintableCharacters = 1024;
typedef struct {
SSL_CTX *ctx;
SSL *s;
const char* test_case_name;
int (*process_heartbeat)(SSL* s);
unsigned char* payload;
int sent_payload_len;
int expected_return_value;
int return_payload_offset;
int expected_payload_len;
const char* expected_return_payload;
} HeartbleedTestFixture;
static HeartbleedTestFixture SetUp(const char* const test_case_name,
const SSL_METHOD* meth) {
HeartbleedTestFixture fixture;
int setup_ok = 1;
memset(&fixture, 0, sizeof(fixture));
fixture.test_case_name = test_case_name;
fixture.ctx = SSL_CTX_new(meth);
if (!fixture.ctx) {
fprintf(stderr, "Failed to allocate SSL_CTX for test: %s\n",
test_case_name);
setup_ok = 0;
goto fail;
}
fixture.s = SSL_new(fixture.ctx);
if (!fixture.s) {
fprintf(stderr, "Failed to allocate SSL for test: %s\n", test_case_name);
setup_ok = 0;
goto fail;
}
if (!ssl_init_wbio_buffer(fixture.s, 1)) {
fprintf(stderr, "Failed to set up wbio buffer for test: %s\n",
test_case_name);
setup_ok = 0;
goto fail;
}
if (!ssl3_setup_buffers(fixture.s)) {
fprintf(stderr, "Failed to setup buffers for test: %s\n", test_case_name);
setup_ok = 0;
goto fail;
}
/* Clear the memory for the return buffer, since this isn't automatically
* zeroed in opt mode and will cause spurious test failures that will change
* with each execution.
*/
memset(fixture.s->s3->wbuf.buf, 0, fixture.s->s3->wbuf.len);
fail:
if (!setup_ok) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
return fixture;
}
static HeartbleedTestFixture SetUpDtls(const char* const test_case_name) {
HeartbleedTestFixture fixture = SetUp(test_case_name,
DTLSv1_server_method());
fixture.process_heartbeat = dtls1_process_heartbeat;
/* As per dtls1_get_record(), skipping the following from the beginning of
* the returned heartbeat message:
* type-1 byte; version-2 bytes; sequence number-8 bytes; length-2 bytes
*
* And then skipping the 1-byte type encoded by process_heartbeat for
* a total of 14 bytes, at which point we can grab the length and the
* payload we seek.
*/
fixture.return_payload_offset = 14;
return fixture;
}
/* Needed by ssl3_write_bytes() */
static int DummyHandshake(SSL* s) {
return 1;
}
static HeartbleedTestFixture SetUpTls(const char* const test_case_name) {
HeartbleedTestFixture fixture = SetUp(test_case_name,
TLSv1_server_method());
fixture.process_heartbeat = tls1_process_heartbeat;
fixture.s->handshake_func = DummyHandshake;
/* As per do_ssl3_write(), skipping the following from the beginning of
* the returned heartbeat message:
* type-1 byte; version-2 bytes; length-2 bytes
*
* And then skipping the 1-byte type encoded by process_heartbeat for
* a total of 6 bytes, at which point we can grab the length and the payload
* we seek.
*/
fixture.return_payload_offset = 6;
return fixture;
}
static void TearDown(HeartbleedTestFixture fixture) {
ERR_print_errors_fp(stderr);
SSL_free(fixture.s);
SSL_CTX_free(fixture.ctx);
}
static void PrintPayload(const char* const prefix,
const unsigned char *payload, const int n) {
const int end = n < kMaxPrintableCharacters ? n : kMaxPrintableCharacters;
printf("%s %d character%s", prefix, n, n == 1 ? "" : "s");
if (end != n) printf(" (first %d shown)", end);
printf("\n \"");
int i = 0;
for (; i != end; ++i) {
const unsigned char c = payload[i];
if (isprint(c)) fputc(c, stdout);
else printf("\\x%02x", c);
}
printf("\"\n");
}
static int ExecuteHeartbeat(HeartbleedTestFixture fixture) {
int result = 0;
SSL* s = fixture.s;
unsigned char *payload = fixture.payload;
unsigned char sent_buf[kMaxPrintableCharacters + 1];
s->s3->rrec.data = payload;
s->s3->rrec.length = strlen((const char*)payload);
*payload++ = TLS1_HB_REQUEST;
s2n(fixture.sent_payload_len, payload);
/* Make a local copy of the request, since it gets overwritten at some
* point */
memcpy((char *)sent_buf, (const char*)payload, sizeof(sent_buf));
int return_value = fixture.process_heartbeat(s);
if (return_value != fixture.expected_return_value) {
printf("%s failed: expected return value %d, received %d\n",
fixture.test_case_name, fixture.expected_return_value,
return_value);
result = 1;
}
/* If there is any byte alignment, it will be stored in wbuf.offset. */
unsigned const char *p = &(s->s3->wbuf.buf[
fixture.return_payload_offset + s->s3->wbuf.offset]);
int actual_payload_len = 0;
n2s(p, actual_payload_len);
if (actual_payload_len != fixture.expected_payload_len) {
printf("%s failed:\n expected payload len: %d\n received: %d\n",
fixture.test_case_name, fixture.expected_payload_len,
actual_payload_len);
PrintPayload("sent", sent_buf, strlen((const char*)sent_buf));
PrintPayload("received", p, actual_payload_len);
result = 1;
} else {
char* actual_payload = strndup((const char*)p, actual_payload_len);
if (strcmp(actual_payload, fixture.expected_return_payload) != 0) {
printf("%s failed:\n expected payload: \"%s\"\n received: \"%s\"\n",
fixture.test_case_name, fixture.expected_return_payload,
actual_payload);
result = 1;
}
free(actual_payload);
}
if (result != 0) {
printf("** %s failed **\n--------\n", fixture.test_case_name);
}
TearDown(fixture);
return result;
}
static int HonestPayloadSize(unsigned char payload_buf[]) {
/* Omit three-byte pad at the beginning for type and payload length */
return strlen((const char*)&payload_buf[3]) - kMinPaddingSize;
}
static int TestDtls1NotBleeding() {
HeartbleedTestFixture fixture = SetUpDtls(__func__);
/* Three-byte pad at the beginning for type and payload length */
unsigned char payload_buf[] = " Not bleeding, sixteen spaces of padding"
" ";
const int payload_buf_len = HonestPayloadSize(payload_buf);
fixture.payload = &payload_buf[0];
fixture.sent_payload_len = payload_buf_len;
fixture.expected_return_value = 0;
fixture.expected_payload_len = payload_buf_len;
fixture.expected_return_payload = "Not bleeding, sixteen spaces of padding";
return ExecuteHeartbeat(fixture);
}
static int TestDtls1NotBleedingEmptyPayload() {
HeartbleedTestFixture fixture = SetUpDtls(__func__);
/* Three-byte pad at the beginning for type and payload length, plus a NUL
* at the end */
unsigned char payload_buf[4 + kMinPaddingSize];
memset(payload_buf, ' ', sizeof(payload_buf));
payload_buf[sizeof(payload_buf) - 1] = '\0';
const int payload_buf_len = HonestPayloadSize(payload_buf);
fixture.payload = &payload_buf[0];
fixture.sent_payload_len = payload_buf_len;
fixture.expected_return_value = 0;
fixture.expected_payload_len = payload_buf_len;
fixture.expected_return_payload = "";
return ExecuteHeartbeat(fixture);
}
static int TestDtls1Heartbleed() {
HeartbleedTestFixture fixture = SetUpDtls(__func__);
/* Three-byte pad at the beginning for type and payload length */
unsigned char payload_buf[] = " HEARTBLEED ";
fixture.payload = &payload_buf[0];
fixture.sent_payload_len = kMaxPrintableCharacters;
fixture.expected_return_value = 0;
fixture.expected_payload_len = 0;
fixture.expected_return_payload = "";
return ExecuteHeartbeat(fixture);
}
static int TestDtls1HeartbleedEmptyPayload() {
HeartbleedTestFixture fixture = SetUpDtls(__func__);
/* Excluding the NUL at the end, one byte short of type + payload length +
* minimum padding */
unsigned char payload_buf[kMinPaddingSize + 3];
memset(payload_buf, ' ', sizeof(payload_buf));
payload_buf[sizeof(payload_buf) - 1] = '\0';
fixture.payload = &payload_buf[0];
fixture.sent_payload_len = kMaxPrintableCharacters;
fixture.expected_return_value = 0;
fixture.expected_payload_len = 0;
fixture.expected_return_payload = "";
return ExecuteHeartbeat(fixture);
}
static int TestDtls1HeartbleedExcessivePlaintextLength() {
HeartbleedTestFixture fixture = SetUpDtls(__func__);
/* Excluding the NUL at the end, one byte in excess of maximum allowed
* heartbeat message length */
unsigned char payload_buf[SSL3_RT_MAX_PLAIN_LENGTH + 2];
memset(payload_buf, ' ', sizeof(payload_buf));
payload_buf[sizeof(payload_buf) - 1] = '\0';
fixture.payload = &payload_buf[0];
fixture.sent_payload_len = HonestPayloadSize(payload_buf);
fixture.expected_return_value = 0;
fixture.expected_payload_len = 0;
fixture.expected_return_payload = "";
return ExecuteHeartbeat(fixture);
}
static int TestTls1NotBleeding() {
HeartbleedTestFixture fixture = SetUpTls(__func__);
/* Three-byte pad at the beginning for type and payload length */
unsigned char payload_buf[] = " Not bleeding, sixteen spaces of padding"
" ";
const int payload_buf_len = HonestPayloadSize(payload_buf);
fixture.payload = &payload_buf[0];
fixture.sent_payload_len = payload_buf_len;
fixture.expected_return_value = 0;
fixture.expected_payload_len = payload_buf_len;
fixture.expected_return_payload = "Not bleeding, sixteen spaces of padding";
return ExecuteHeartbeat(fixture);
}
static int TestTls1NotBleedingEmptyPayload() {
HeartbleedTestFixture fixture = SetUpTls(__func__);
/* Three-byte pad at the beginning for type and payload length, plus a NUL
* at the end */
unsigned char payload_buf[4 + kMinPaddingSize];
memset(payload_buf, ' ', sizeof(payload_buf));
payload_buf[sizeof(payload_buf) - 1] = '\0';
const int payload_buf_len = HonestPayloadSize(payload_buf);
fixture.payload = &payload_buf[0];
fixture.sent_payload_len = payload_buf_len;
fixture.expected_return_value = 0;
fixture.expected_payload_len = payload_buf_len;
fixture.expected_return_payload = "";
return ExecuteHeartbeat(fixture);
}
static int TestTls1Heartbleed() {
HeartbleedTestFixture fixture = SetUpTls(__func__);
/* Three-byte pad at the beginning for type and payload length */
unsigned char payload_buf[] = " HEARTBLEED ";
fixture.payload = &payload_buf[0];
fixture.sent_payload_len = kMaxPrintableCharacters;
fixture.expected_return_value = 0;
fixture.expected_payload_len = 0;
fixture.expected_return_payload = "";
return ExecuteHeartbeat(fixture);
}
static int TestTls1HeartbleedEmptyPayload() {
HeartbleedTestFixture fixture = SetUpTls(__func__);
/* Excluding the NUL at the end, one byte short of type + payload length +
* minimum padding */
unsigned char payload_buf[kMinPaddingSize + 3];
memset(payload_buf, ' ', sizeof(payload_buf));
payload_buf[sizeof(payload_buf) - 1] = '\0';
fixture.payload = &payload_buf[0];
fixture.sent_payload_len = kMaxPrintableCharacters;
fixture.expected_return_value = 0;
fixture.expected_payload_len = 0;
fixture.expected_return_payload = "";
return ExecuteHeartbeat(fixture);
}
int main(int argc, char *argv[]) {
SSL_library_init();
SSL_load_error_strings();
int num_failed = TestDtls1NotBleeding() +
TestDtls1NotBleedingEmptyPayload() +
TestDtls1Heartbleed() +
TestDtls1HeartbleedEmptyPayload() +
/* The following test causes an assertion failure at
* ssl/d1_pkt.c:dtls1_write_bytes() in versions prior to 1.0.1g: */
(OPENSSL_VERSION_NUMBER >= 0x1000107fL ?
TestDtls1HeartbleedExcessivePlaintextLength() : 0) +
TestTls1NotBleeding() +
TestTls1NotBleedingEmptyPayload() +
TestTls1Heartbleed() +
TestTls1HeartbleedEmptyPayload() +
0;
ERR_print_errors_fp(stderr);
if (num_failed != 0) {
printf("%d test%s failed\n", num_failed, num_failed != 1 ? "s" : "");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}