Jean-Paul Calderone | c7b3c89 | 2011-03-02 19:40:02 -0500 | [diff] [blame] | 1 | # Copyright (C) Jean-Paul Calderone |
| 2 | # See LICENSE for details. |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 3 | # |
| 4 | # Stress tester for thread-related bugs in global_passphrase_callback in |
| 5 | # src/ssl/context.c. In 0.7 and earlier, this will somewhat reliably |
| 6 | # segfault or abort after a few dozen to a few thousand iterations on an SMP |
| 7 | # machine (generally not on a UP machine) due to uses of Python/C API |
| 8 | # without holding the GIL. |
| 9 | |
| 10 | from itertools import count |
| 11 | from threading import Thread |
| 12 | |
| 13 | from OpenSSL.SSL import Context, TLSv1_METHOD |
| 14 | from OpenSSL.crypto import TYPE_RSA, FILETYPE_PEM, PKey, dump_privatekey |
| 15 | |
| 16 | k = PKey() |
| 17 | k.generate_key(TYPE_RSA, 128) |
| 18 | file('pkey.pem', 'w').write(dump_privatekey(FILETYPE_PEM, k, "blowfish", "foobar")) |
| 19 | |
| 20 | count = count() |
| 21 | def go(): |
| 22 | def cb(a, b, c): |
| 23 | print count.next() |
| 24 | return "foobar" |
| 25 | c = Context(TLSv1_METHOD) |
| 26 | c.set_passwd_cb(cb) |
| 27 | while 1: |
| 28 | c.use_privatekey_file('pkey.pem') |
| 29 | |
| 30 | threads = [Thread(target=go, args=()) for i in xrange(2)] |
| 31 | for th in threads: |
| 32 | th.start() |
| 33 | for th in threads: |
| 34 | th.join() |