Skip to content

Commit 09f5588

Browse files
authored
Merge pull request #394 from inikolcev/bad-certificate-parsing
If parsing of the certificate fails throw BadCertificate error
2 parents 807a3db + 11d1ef0 commit 09f5588

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

tlslite/messages.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,8 +1170,13 @@ def _parse_tls12(self, p):
11701170
certificate_list = []
11711171
while index != chainLength:
11721172
certBytes = p.getVarBytes(3)
1173+
if not certBytes:
1174+
raise DecodeError("Client certificate is empty")
11731175
x509 = X509()
1174-
x509.parseBinary(certBytes)
1176+
try:
1177+
x509.parseBinary(certBytes)
1178+
except SyntaxError:
1179+
raise BadCertificateError("Certificate could not be parsed")
11751180
certificate_list.append(x509)
11761181
index += len(certBytes)+3
11771182
if certificate_list:

tlslite/tlsrecordlayer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from .utils.compat import *
1919
from .utils.cryptomath import *
20-
from .utils.codec import Parser
20+
from .utils.codec import Parser, BadCertificateError
2121
from .utils.lists import to_str_delimiter, getFirstMatching
2222
from .errors import *
2323
from .messages import *
@@ -1201,9 +1201,13 @@ def _getMsg(self, expectedType, secondaryType=None, constructorType=None):
12011201
raise AssertionError()
12021202

12031203
#If an exception was raised by a Parser or Message instance:
1204+
except BadCertificateError as e:
1205+
for result in self._sendError(AlertDescription.bad_certificate,
1206+
formatExceptionTrace(e)):
1207+
yield result
12041208
except SyntaxError as e:
12051209
for result in self._sendError(AlertDescription.decode_error,
1206-
formatExceptionTrace(e)):
1210+
formatExceptionTrace(e)):
12071211
yield result
12081212

12091213
#Returns next record or next handshake message

tlslite/utils/codec.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class DecodeError(SyntaxError):
1515
pass
1616

1717

18+
class BadCertificateError(SyntaxError):
19+
"""Exception raised in case of bad certificate."""
20+
pass
21+
22+
1823
class Writer(object):
1924
"""Serialisation helper for complex byte-based structures."""
2025

0 commit comments

Comments
 (0)