Skip to content

Commit

Permalink
Merge pull request #1055 from agl/9518-agl-rsakeyloading
Browse files Browse the repository at this point in the history
Author: agl

Reviewer: glyph, alex

Fixes: ticket:9518

Don't corrupt RSA private keys during loading.
  • Loading branch information
glyph authored Sep 3, 2018
2 parents 1928146 + 8e6d503 commit 2a18aba
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 47 deletions.
1 change: 1 addition & 0 deletions src/twisted/conch/newsfragments/9518.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RSA private keys are no longer corrupted during loading, allowing OpenSSL's fast-path to operate for RSA signing.
6 changes: 2 additions & 4 deletions src/twisted/conch/ssh/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,6 @@ def _fromString_PRIVATE_OPENSSH(cls, data, passphrase):
n, e, d, p, q, dmp1, dmq1, iqmp = [
long(value) for value in decodedKey[1:9]
]
if p > q: # Make p smaller than q
p, q = q, p
return cls(
rsa.RSAPrivateNumbers(
p=p,
Expand Down Expand Up @@ -1110,8 +1108,8 @@ def _toString_OPENSSH(self, extra):
b' PRIVATE KEY-----'))]
if self.type() == 'RSA':
p, q = data['p'], data['q']
objData = (0, data['n'], data['e'], data['d'], q, p,
data['d'] % (q - 1), data['d'] % (p - 1),
objData = (0, data['n'], data['e'], data['d'], p, q,
data['d'] % (p - 1), data['d'] % (q - 1),
data['u'])
else:
objData = (0, data['p'], data['q'], data['g'], data['y'],
Expand Down
89 changes: 46 additions & 43 deletions src/twisted/conch/test/keydata.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@
'477930426013703886193016416453215950072147440344656137718959053'
'897268663969428680144841987624962928576808352739627262941675617'
'7724661940425316604626522633351193810751757014073'),
'q': long('176444974592327996338888725079951900172097062203378367409936859'
'072670162290963119826394224277287608693818012745872307600855894'
'647300295516866118620024751601329775653542084052616260193174546'
'400544176890518564317596334518015173606460860373958663673307503'
'231977779632583864454001476729233959405710696795574874403'),
'p': long('152689878451107675391723141129365667732639179427453246378763774'
'448531436802867910180261906924087589684175595016060014593521649'
'964959248408388984465569934780790357826811592229318702991401054'
'226302790395714901636384511513449977061729214247279176398290513'
'085108930550446985490864812445551198848562639933888780317'),
'q': long('176444974592327996338888725079951900172097062203378367409936859'
'072670162290963119826394224277287608693818012745872307600855894'
'647300295516866118620024751601329775653542084052616260193174546'
'400544176890518564317596334518015173606460860373958663673307503'
'231977779632583864454001476729233959405710696795574874403'),
'u': long('936018002388095842969518498561007090965136403384715613439364803'
'229386793506402222847415019772053080458257034241832795210460612'
'924445085372678524176842007912276654532773301546269997020970818'
Expand Down Expand Up @@ -160,23 +160,26 @@
u2geG+bdu3NjuWfmsid/z88wm8KY/dkYNi82LvE9gXqf4QMtR9s0UWI53U/prKiL
2dbzhMQXuXGdBghCeE27xSr0w1jNVSvtvjNfBOp75gQkY/It1z0bbNWcY0MvkoiN
Pm7aGDfYDyVniR25RjReyc7Ei+2SWjMHD9+GCPmS6dvrOAg2yc3NCgFIWzk+esrG
gKnc1DkCgYEA+0QXi6Q2vh43Haf2YWwExKrdeD4HjB4zAq4DFIeDeuWefQhnqPKq
vxJwz3Kpp8cLHYjVIP2cY8pHMFVOi8TP9H8WpJISdKEJwsRunIwz76Xl9+ArrU9c
EaoahDdb/Xrqw818sMjkH1Rjtcev3/QJp/zHJfxc6ZHXksWYHlbTsSMCgYEA2XAG
2OK81HiRUJTUwRuJOGxGZFpRoJoHPUiPA1HMaxKOfRqxZedxdTngMgV1jRhMr5Ox
SbFmX3hietEMyuZNQ7Oc9Gt95gyY3M8hYo7VLhLeBK7XJG6DMaIVokQ9IqliJiK5
su1UCp0Ig6cHDf8ZGI7Yqx3aSJwxaBGhZm3j2B0CgYBpZjfGQN0hxQTzsLg+R5R8
dvwt6z85PJXDQwFRxEKX8+gWpMbu7NRJEFA4BO47zdfQzMwyaZAHoBtan/4xzR46
fnEeGZQaTk8M319S1dEXbuzXnLZVnduOIV+8JIi2/K+r8O+kLLDcn4awAxK4i+Ld
D8DuI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BzucXHdFMXLw90iYm/mrW/Uhyrkb6e8PTGWBE
HaUTp4D1YynUel0GBxZd9os9y2Q64oRaTYwGLS2dHOuDTHg9eVTO
-----END RSA PRIVATE KEY-----'''

# Some versions of OpenSSH generate these (slightly different keys)
# Some versions of OpenSSH generate these (slightly different keys): the PKCS#1
# structure is wrapped in an extra ASN.1 SEQUENCE and there's an empty SEQUENCE
# following it. It is not any standard key format and was probably a bug in
# OpenSSH at some point.
privateRSA_openssh_alternate = b"""-----BEGIN RSA PRIVATE KEY-----
MIIEqTCCBKMCAQACggEBANVqrHgj1tYb7CWhUMR3Y1CERQFVQhQqKuDQYO7U6aOtSvo5Bl6EVXVf
ADa/b6oqP4MmN8FpLlv98PPSfdaYzTpAeNXKqBjAEZMkCQyBTI/3nO0TFmqkBOlJd8PkVWSzeWie
Expand All @@ -187,19 +190,19 @@
+LMkYIS5zmmVmvni/B9RTScV2ysnre+0aay+fRDrhkdwc7QAh5UVOzf55xTngLtoHhvm3btzY7ln
5rInf8/PMJvCmP3ZGDYvNi7xPYF6n+EDLUfbNFFiOd1P6ayoi9nW84TEF7lxnQYIQnhNu8Uq9MNY
zVUr7b4zXwTqe+YEJGPyLdc9G2zVnGNDL5KIjT5u2hg32A8lZ4kduUY0XsnOxIvtklozBw/fhgj5
kunb6zgINsnNzQoBSFs5PnrKxoCp3NQ5AoGBAPtEF4ukNr4eNx2n9mFsBMSq3Xg+B4weMwKuAxSH
g3rlnn0IZ6jyqr8ScM9yqafHCx2I1SD9nGPKRzBVTovEz/R/FqSSEnShCcLEbpyMM++l5ffgK61P
XBGqGoQ3W/166sPNfLDI5B9UY7XHr9/0Caf8xyX8XOmR15LFmB5W07EjAoGBANlwBtjivNR4kVCU
1MEbiThsRmRaUaCaBz1IjwNRzGsSjn0asWXncXU54DIFdY0YTK+TsUmxZl94YnrRDMrmTUOznPRr
feYMmNzPIWKO1S4S3gSu1yRugzGiFaJEPSKpYiYiubLtVAqdCIOnBw3/GRiO2Ksd2kicMWg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VnduOIV+8
JIi2/K+r8O+kLLDcn4awAxK4i+LdD8DuIz1KUP4vuClGWL+2JwKBgQCFSxt6mxIQN54frV7a/saW
/t81a7k04haXkiYJvb1wIAOnNb0tG6DSB0cr1N6oqAcHG7gEIKcnQTxsOTnpQc7nFx3RTFy8PdIm
Jv5q1v1Icq5G+nvD0xlgRB2lE6eA9WMp1HpdBgcWXfaLPctkOuKEWk2MBi0tnRzrg0x4PXlUzjAA
------END RSA PRIVATE KEY------"""
-----END RSA PRIVATE KEY-----"""

# Encrypted with the passphrase 'encrypted'
privateRSA_openssh_encrypted = b"""-----BEGIN RSA PRIVATE KEY-----
Expand All @@ -217,20 +220,20 @@
T5VWGDl3ZX/eM8YlXDupewOpb56g55Aevl0jERktqJdl+g9D+PXnCsJgxMcJbfl5
tWY9KoMxV+2Fj68SHdr/acCp7xgUMwHvVLFfYCeK/hpEe9O8vOAMTlXuq9zCMDAx
kL2kcSFbZHerc4TijtjXEALo06gYHEcLRtI6lvYrzbbmpCD7J7AnMzS3SQ2FzbFM
GARKfxBnYp0dh+B1ZjIygn3cWD6I+8gSS9m5tgrUMWC2ejFzfznCclk/XEjnczAp
y+vspB+ryx+8M6j7oPebYqQHG5slOwH/jLwDuS/05lGql5rBgpEiTmOQtXQRXMoq
X8NdGE6dUm3Gi+LVXMubbNJPD/omMbbWwpQ+LQHEBgBGa4zJZofljF19tmu868iJ
6u8V3+BT7bEJ3Svb/hesuy6IWgzPTayXcBeqvxsf44cmFKbgXlk1d77MN22RVVai
B//r6igT6SMo2Cs14X2N+gibA/IvKDVYA0EGd0S8zVlJ3HFT1V6IgUTmWHxgqlMu
k+bEkxq8mwgmJQFy69dUQeVRLaE/0kQ3mbMMeyT9nxLzwzZYqZ5trMBarH8V02kq
lx+qyS71+xOy7+r07hANX4j37+4RwM79Dc/pYxAnhXmMDP7V+GEBf9PImuhEsCce
4aLpqzq3s7lQPVE4unM+9mLR2C/oCHRxISsqa6nWOsdPtvpdqKuw9DMAv3dK5Be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my+7
uWpZrh3hkmKUtfdiyeqUUj1ypwZ6boZO1UZo0xTrpubmKQEvd2957YrEEVsi0LeB
uEzSlUXrwIV7Qw2VhoLxIaCyl5j4nOKetUeAjqVNi9makh0x4Ion5osxxYvYS9s/
Y48ATMnwm3+CdN6LE3IykHyHs7JuZmawWDR4CKJB6M1r0X+Xwgs0tQ==
-----END RSA PRIVATE KEY-----"""

# Encrypted with the passphrase 'testxp'. NB: this key was generated by
Expand Down

0 comments on commit 2a18aba

Please sign in to comment.