Jean-Paul Calderone | c7b3c89 | 2011-03-02 19:40:02 -0500 | [diff] [blame] | 1 | # Copyright (C) Jean-Paul Calderone |
| 2 | # See LICENSE for details. |
| 3 | |
Jean-Paul Calderone | 19555b9 | 2008-02-19 22:29:57 -0500 | [diff] [blame] | 4 | import sys |
| 5 | |
Jean-Paul Calderone | 8e37f76 | 2011-09-14 10:00:50 -0400 | [diff] [blame] | 6 | from OpenSSL.crypto import ( |
Jonathan Giannuzzi | 99eff82 | 2014-03-23 22:48:41 +0100 | [diff] [blame] | 7 | FILETYPE_PEM, TYPE_DSA, Error, PKey, X509, load_privatekey, CRL, Revoked) |
Jean-Paul Calderone | 19555b9 | 2008-02-19 22:29:57 -0500 | [diff] [blame] | 8 | |
Jean-Paul Calderone | 8e37f76 | 2011-09-14 10:00:50 -0400 | [diff] [blame] | 9 | |
| 10 | |
| 11 | class BaseChecker(object): |
Jean-Paul Calderone | 19555b9 | 2008-02-19 22:29:57 -0500 | [diff] [blame] | 12 | def __init__(self, iterations): |
| 13 | self.iterations = iterations |
| 14 | |
| 15 | |
Jean-Paul Calderone | 8e37f76 | 2011-09-14 10:00:50 -0400 | [diff] [blame] | 16 | |
| 17 | class Checker_X509_get_pubkey(BaseChecker): |
| 18 | """ |
| 19 | Leak checks for L{X509.get_pubkey}. |
| 20 | """ |
Jean-Paul Calderone | 19555b9 | 2008-02-19 22:29:57 -0500 | [diff] [blame] | 21 | def check_exception(self): |
| 22 | """ |
| 23 | Call the method repeatedly such that it will raise an exception. |
| 24 | """ |
| 25 | for i in xrange(self.iterations): |
| 26 | cert = X509() |
| 27 | try: |
| 28 | cert.get_pubkey() |
| 29 | except Error: |
| 30 | pass |
| 31 | |
| 32 | |
| 33 | def check_success(self): |
| 34 | """ |
| 35 | Call the method repeatedly such that it will return a PKey object. |
| 36 | """ |
| 37 | small = xrange(3) |
| 38 | for i in xrange(self.iterations): |
| 39 | key = PKey() |
| 40 | key.generate_key(TYPE_DSA, 256) |
| 41 | for i in small: |
| 42 | cert = X509() |
| 43 | cert.set_pubkey(key) |
| 44 | for i in small: |
| 45 | cert.get_pubkey() |
| 46 | |
| 47 | |
Jean-Paul Calderone | 8e37f76 | 2011-09-14 10:00:50 -0400 | [diff] [blame] | 48 | |
| 49 | class Checker_load_privatekey(BaseChecker): |
| 50 | """ |
| 51 | Leak checks for :py:obj:`load_privatekey`. |
| 52 | """ |
| 53 | ENCRYPTED_PEM = """\ |
| 54 | -----BEGIN RSA PRIVATE KEY----- |
| 55 | Proc-Type: 4,ENCRYPTED |
| 56 | DEK-Info: BF-CBC,3763C340F9B5A1D0 |
| 57 | |
| 58 | a/DO10mLjHLCAOG8/Hc5Lbuh3pfjvcTZiCexShP+tupkp0VxW2YbZjML8uoXrpA6 |
| 59 | fSPUo7cEC+r96GjV03ZIVhjmsxxesdWMpfkzXRpG8rUbWEW2KcCJWdSX8bEkuNW3 |
| 60 | uvAXdXZwiOrm56ANDo/48gj27GcLwnlA8ld39+ylAzkUJ1tcMVzzTjfcyd6BMFpR |
| 61 | Yjg23ikseug6iWEsZQormdl0ITdYzmFpM+YYsG7kmmmi4UjCEYfb9zFaqJn+WZT2 |
| 62 | qXxmo2ZPFzmEVkuB46mf5GCqMwLRN2QTbIZX2+Dljj1Hfo5erf5jROewE/yzcTwO |
| 63 | FCB5K3c2kkTv2KjcCAimjxkE+SBKfHg35W0wB0AWkXpVFO5W/TbHg4tqtkpt/KMn |
| 64 | /MPnSxvYr/vEqYMfW4Y83c45iqK0Cyr2pwY60lcn8Kk= |
| 65 | -----END RSA PRIVATE KEY----- |
| 66 | """ |
| 67 | def check_load_privatekey_callback(self): |
| 68 | """ |
| 69 | Call the function with an encrypted PEM and a passphrase callback. |
| 70 | """ |
| 71 | for i in xrange(self.iterations * 10): |
| 72 | load_privatekey( |
| 73 | FILETYPE_PEM, self.ENCRYPTED_PEM, lambda *args: "hello, secret") |
| 74 | |
| 75 | |
| 76 | def check_load_privatekey_callback_incorrect(self): |
| 77 | """ |
| 78 | Call the function with an encrypted PEM and a passphrase callback which |
| 79 | returns the wrong passphrase. |
| 80 | """ |
| 81 | for i in xrange(self.iterations * 10): |
| 82 | try: |
| 83 | load_privatekey( |
| 84 | FILETYPE_PEM, self.ENCRYPTED_PEM, |
| 85 | lambda *args: "hello, public") |
| 86 | except Error: |
| 87 | pass |
| 88 | |
| 89 | |
| 90 | def check_load_privatekey_callback_wrong_type(self): |
| 91 | """ |
| 92 | Call the function with an encrypted PEM and a passphrase callback which |
| 93 | returns a non-string. |
| 94 | """ |
| 95 | for i in xrange(self.iterations * 10): |
| 96 | try: |
| 97 | load_privatekey( |
| 98 | FILETYPE_PEM, self.ENCRYPTED_PEM, |
| 99 | lambda *args: {}) |
Jean-Paul Calderone | 2a864f1 | 2011-09-14 11:10:29 -0400 | [diff] [blame] | 100 | except ValueError: |
Jean-Paul Calderone | 8e37f76 | 2011-09-14 10:00:50 -0400 | [diff] [blame] | 101 | pass |
| 102 | |
| 103 | |
Jonathan Giannuzzi | 99eff82 | 2014-03-23 22:48:41 +0100 | [diff] [blame] | 104 | |
| 105 | class Checker_CRL_get_revoked(BaseChecker): |
| 106 | """ |
| 107 | Leak checks for L{CRL.get_revoked}. |
| 108 | """ |
| 109 | def check_get_revoked(self): |
| 110 | """ |
Jonathan Giannuzzi | 3b97ec1 | 2014-03-24 00:47:06 +0100 | [diff] [blame^] | 111 | Create a CRL object with 100 Revoked objects, then call the |
Jonathan Giannuzzi | 99eff82 | 2014-03-23 22:48:41 +0100 | [diff] [blame] | 112 | get_revoked method repeatedly. |
| 113 | """ |
| 114 | crl = CRL() |
Jonathan Giannuzzi | 3b97ec1 | 2014-03-24 00:47:06 +0100 | [diff] [blame^] | 115 | for i in xrange(100): |
| 116 | crl.add_revoked(Revoked()) |
Jonathan Giannuzzi | 99eff82 | 2014-03-23 22:48:41 +0100 | [diff] [blame] | 117 | for i in xrange(self.iterations): |
| 118 | crl.get_revoked() |
| 119 | |
| 120 | |
| 121 | |
Jean-Paul Calderone | 19555b9 | 2008-02-19 22:29:57 -0500 | [diff] [blame] | 122 | def vmsize(): |
| 123 | return [x for x in file('/proc/self/status').readlines() if 'VmSize' in x] |
| 124 | |
| 125 | |
| 126 | def main(iterations='1000'): |
| 127 | iterations = int(iterations) |
| 128 | for klass in globals(): |
| 129 | if klass.startswith('Checker_'): |
| 130 | klass = globals()[klass] |
| 131 | print klass |
| 132 | checker = klass(iterations) |
| 133 | for meth in dir(checker): |
| 134 | if meth.startswith('check_'): |
| 135 | print '\t', meth, vmsize(), '...', |
| 136 | getattr(checker, meth)() |
| 137 | print vmsize() |
| 138 | |
| 139 | |
| 140 | if __name__ == '__main__': |
| 141 | main(*sys.argv[1:]) |