Index: third_party/tlslite/tlslite/X509CertChain.py |
diff --git a/third_party/tlslite/tlslite/X509CertChain.py b/third_party/tlslite/tlslite/X509CertChain.py |
index 6bb503e43eefa9f01f9c7ec89cb9393f35b1148a..47de9882c1e0337ab72cc2f1cdbcf7dfabbb081a 100644 |
--- a/third_party/tlslite/tlslite/X509CertChain.py |
+++ b/third_party/tlslite/tlslite/X509CertChain.py |
@@ -1,6 +1,7 @@ |
"""Class representing an X.509 certificate chain.""" |
from utils import cryptomath |
+from X509 import X509 |
class X509CertChain: |
"""This class represents a chain of X.509 certificates. |
@@ -24,6 +25,65 @@ class X509CertChain: |
else: |
self.x509List = [] |
+ def parseChain(self, s): |
Nico
2012/02/29 00:33:34
Could use a basic unit test
|
+ """Parse a PEM-encoded X.509 certificate file chain file. |
+ |
+ @type s: str |
Nico
2012/02/29 00:33:34
s isn't a great parameter name. parameters are par
|
+ @param s: A PEM-encoded (eg: Base64) X.509 certificate file, with every |
+ certificate wrapped within "-----BEGIN CERTIFICATE-----" and |
+ "-----END CERTIFICATE-----" tags). Extraneous data outside such tags, |
+ such as human readable representations, will be ignored. |
+ """ |
+ |
+ class PEMIterator(object): |
+ """Simple iterator over PEM-encoded certificates within a string. |
+ |
+ @type s: string |
+ @ivar s: A string containing PEM-encoded (Base64) certificates, with |
+ every certificate wrapped within "-----BEGIN CERTIFICATE-----" and |
+ "-----END CERTIFICATE-----" tags). Extraneous data outside such |
+ tags, such as human readable representations, will be ignored. |
+ |
+ @type index: integer |
+ @param index: The current offset within s to begin iterating from |
Nico
2012/02/29 00:33:34
@ivar index?
|
+ """ |
+ |
+ def __init__(self, s): |
+ self.s = s |
+ self.index = 0 |
+ |
+ def __iter__(self): |
+ return self |
+ |
+ def next(self): |
+ """Iterates and returns the next L{tlslite.X509.X509} |
+ certificate in s |
+ |
+ @rtype tlslite.X509.X509 |
+ """ |
+ |
+ self.index = self.s.find("-----BEGIN CERTIFICATE-----", |
Nico
2012/02/29 00:33:34
Make these strings "constants"?
|
+ self.index) |
+ if (self.index == -1): |
Nico
2012/02/29 00:33:34
no parens
|
+ raise StopIteration |
+ end = self.s.find("-----END CERTIFICATE-----", self.index) |
+ if (end == -1): |
Nico
2012/02/29 00:33:34
no parens
|
+ raise StopIteration |
+ |
+ certStr = self.s[self.index+len("-----BEGIN CERTIFICATE-----") : |
+ end] |
+ self.index = end + len("-----END CERTIFICATE-----") |
+ bytes = cryptomath.base64ToBytes(certStr) |
+ cert = X509() |
+ cert.parseBinary(bytes) |
+ return cert |
+ |
+ certs = [] |
+ for cert in PEMIterator(s): |
+ certs.append(cert) |
+ self.x509List = certs |
Nico
2012/02/29 00:33:34
Does `self.x509List = list(PEMIerator(s))` work as
|
+ return self |
+ |
def getNumCerts(self): |
"""Get the number of certificates in this chain. |