OLD | NEW |
| 1 # Author: Trevor Perrin |
| 2 # See the LICENSE file for legal information regarding use of this file. |
| 3 |
1 """OpenSSL/M2Crypto RSA implementation.""" | 4 """OpenSSL/M2Crypto RSA implementation.""" |
2 | 5 |
3 from cryptomath import * | 6 from .cryptomath import * |
4 | 7 |
5 from rsakey import * | 8 from .rsakey import * |
6 from python_rsakey import Python_RSAKey | 9 from .python_rsakey import Python_RSAKey |
7 | 10 |
8 #copied from M2Crypto.util.py, so when we load the local copy of m2 | 11 #copied from M2Crypto.util.py, so when we load the local copy of m2 |
9 #we can still use it | 12 #we can still use it |
10 def password_callback(v, prompt1='Enter private key passphrase:', | 13 def password_callback(v, prompt1='Enter private key passphrase:', |
11 prompt2='Verify passphrase:'): | 14 prompt2='Verify passphrase:'): |
12 from getpass import getpass | 15 from getpass import getpass |
13 while 1: | 16 while 1: |
14 try: | 17 try: |
15 p1=getpass(prompt1) | 18 p1=getpass(prompt1) |
16 if v: | 19 if v: |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 elif name == 'n': | 51 elif name == 'n': |
49 if not self.rsa: | 52 if not self.rsa: |
50 return 0 | 53 return 0 |
51 return mpiToNumber(m2.rsa_get_n(self.rsa)) | 54 return mpiToNumber(m2.rsa_get_n(self.rsa)) |
52 else: | 55 else: |
53 raise AttributeError | 56 raise AttributeError |
54 | 57 |
55 def hasPrivateKey(self): | 58 def hasPrivateKey(self): |
56 return self._hasPrivateKey | 59 return self._hasPrivateKey |
57 | 60 |
58 def hash(self): | |
59 return Python_RSAKey(self.n, self.e).hash() | |
60 | |
61 def _rawPrivateKeyOp(self, m): | 61 def _rawPrivateKeyOp(self, m): |
62 s = numberToString(m) | 62 b = numberToByteArray(m, numBytes(self.n)) |
63 byteLength = numBytes(self.n) | 63 s = m2.rsa_private_encrypt(self.rsa, bytes(b), m2.no_padding) |
64 if len(s)== byteLength: | 64 c = bytesToNumber(bytearray(s)) |
65 pass | |
66 elif len(s) == byteLength-1: | |
67 s = '\0' + s | |
68 else: | |
69 raise AssertionError() | |
70 c = stringToNumber(m2.rsa_private_encrypt(self.rsa, s, | |
71 m2.no_padding)) | |
72 return c | 65 return c |
73 | 66 |
74 def _rawPublicKeyOp(self, c): | 67 def _rawPublicKeyOp(self, c): |
75 s = numberToString(c) | 68 b = numberToByteArray(c, numBytes(self.n)) |
76 byteLength = numBytes(self.n) | 69 s = m2.rsa_public_decrypt(self.rsa, bytes(b), m2.no_padding) |
77 if len(s)== byteLength: | 70 m = bytesToNumber(bytearray(s)) |
78 pass | |
79 elif len(s) == byteLength-1: | |
80 s = '\0' + s | |
81 else: | |
82 raise AssertionError() | |
83 m = stringToNumber(m2.rsa_public_decrypt(self.rsa, s, | |
84 m2.no_padding)) | |
85 return m | 71 return m |
86 | 72 |
87 def acceptsPassword(self): return True | 73 def acceptsPassword(self): return True |
88 | 74 |
89 def write(self, password=None): | 75 def write(self, password=None): |
90 bio = m2.bio_new(m2.bio_s_mem()) | 76 bio = m2.bio_new(m2.bio_s_mem()) |
91 if self._hasPrivateKey: | 77 if self._hasPrivateKey: |
92 if password: | 78 if password: |
93 def f(v): return password | 79 def f(v): return password |
94 m2.rsa_write_key(self.rsa, bio, m2.des_ede_cbc(), f) | 80 m2.rsa_write_key(self.rsa, bio, m2.des_ede_cbc(), f) |
95 else: | 81 else: |
96 def f(): pass | 82 def f(): pass |
97 m2.rsa_write_key_no_cipher(self.rsa, bio, f) | 83 m2.rsa_write_key_no_cipher(self.rsa, bio, f) |
98 else: | 84 else: |
99 if password: | 85 if password: |
100 raise AssertionError() | 86 raise AssertionError() |
101 m2.rsa_write_pub_key(self.rsa, bio) | 87 m2.rsa_write_pub_key(self.rsa, bio) |
102 s = m2.bio_read(bio, m2.bio_ctrl_pending(bio)) | 88 s = m2.bio_read(bio, m2.bio_ctrl_pending(bio)) |
103 m2.bio_free(bio) | 89 m2.bio_free(bio) |
104 return s | 90 return s |
105 | 91 |
106 def writeXMLPublicKey(self, indent=''): | |
107 return Python_RSAKey(self.n, self.e).write(indent) | |
108 | |
109 def generate(bits): | 92 def generate(bits): |
110 key = OpenSSL_RSAKey() | 93 key = OpenSSL_RSAKey() |
111 def f():pass | 94 def f():pass |
112 key.rsa = m2.rsa_generate_key(bits, 3, f) | 95 key.rsa = m2.rsa_generate_key(bits, 3, f) |
113 key._hasPrivateKey = True | 96 key._hasPrivateKey = True |
114 return key | 97 return key |
115 generate = staticmethod(generate) | 98 generate = staticmethod(generate) |
116 | 99 |
117 def parse(s, passwordCallback=None): | 100 def parse(s, passwordCallback=None): |
| 101 # Skip forward to the first PEM header |
| 102 start = s.find("-----BEGIN ") |
| 103 if start == -1: |
| 104 raise SyntaxError() |
| 105 s = s[start:] |
118 if s.startswith("-----BEGIN "): | 106 if s.startswith("-----BEGIN "): |
119 if passwordCallback==None: | 107 if passwordCallback==None: |
120 callback = password_callback | 108 callback = password_callback |
121 else: | 109 else: |
122 def f(v, prompt1=None, prompt2=None): | 110 def f(v, prompt1=None, prompt2=None): |
123 return passwordCallback() | 111 return passwordCallback() |
124 callback = f | 112 callback = f |
125 bio = m2.bio_new(m2.bio_s_mem()) | 113 bio = m2.bio_new(m2.bio_s_mem()) |
126 try: | 114 try: |
127 m2.bio_write(bio, s) | 115 m2.bio_write(bio, s) |
(...skipping 11 matching lines...) Expand all Loading... |
139 key._hasPrivateKey = False | 127 key._hasPrivateKey = False |
140 else: | 128 else: |
141 raise SyntaxError() | 129 raise SyntaxError() |
142 return key | 130 return key |
143 finally: | 131 finally: |
144 m2.bio_free(bio) | 132 m2.bio_free(bio) |
145 else: | 133 else: |
146 raise SyntaxError() | 134 raise SyntaxError() |
147 | 135 |
148 parse = staticmethod(parse) | 136 parse = staticmethod(parse) |
OLD | NEW |