OLD | NEW |
1 """Class representing an X.509 certificate.""" | 1 """Class representing an X.509 certificate.""" |
2 | 2 |
3 from utils.ASN1Parser import ASN1Parser | 3 from utils.ASN1Parser import ASN1Parser |
4 from utils.cryptomath import * | 4 from utils.cryptomath import * |
5 from utils.keyfactory import _createPublicRSAKey | 5 from utils.keyfactory import _createPublicRSAKey |
6 | 6 |
7 | 7 |
8 class X509: | 8 class X509: |
9 """This class represents an X.509 certificate. | 9 """This class represents an X.509 certificate. |
10 | 10 |
11 @type bytes: L{array.array} of unsigned bytes | 11 @type bytes: L{array.array} of unsigned bytes |
12 @ivar bytes: The DER-encoded ASN.1 certificate | 12 @ivar bytes: The DER-encoded ASN.1 certificate |
13 | 13 |
14 @type publicKey: L{tlslite.utils.RSAKey.RSAKey} | 14 @type publicKey: L{tlslite.utils.RSAKey.RSAKey} |
15 @ivar publicKey: The subject public key from the certificate. | 15 @ivar publicKey: The subject public key from the certificate. |
| 16 |
| 17 @type subject: L{array.array} of unsigned bytes |
| 18 @ivar subject: The DER-encoded ASN.1 subject distinguished name. |
16 """ | 19 """ |
17 | 20 |
18 def __init__(self): | 21 def __init__(self): |
19 self.bytes = createByteArraySequence([]) | 22 self.bytes = createByteArraySequence([]) |
20 self.publicKey = None | 23 self.publicKey = None |
| 24 self.subject = None |
21 | 25 |
22 def parse(self, s): | 26 def parse(self, s): |
23 """Parse a PEM-encoded X.509 certificate. | 27 """Parse a PEM-encoded X.509 certificate. |
24 | 28 |
25 @type s: str | 29 @type s: str |
26 @param s: A PEM-encoded X.509 certificate (i.e. a base64-encoded | 30 @param s: A PEM-encoded X.509 certificate (i.e. a base64-encoded |
27 certificate wrapped with "-----BEGIN CERTIFICATE-----" and | 31 certificate wrapped with "-----BEGIN CERTIFICATE-----" and |
28 "-----END CERTIFICATE-----" tags). | 32 "-----END CERTIFICATE-----" tags). |
29 """ | 33 """ |
30 | 34 |
(...skipping 25 matching lines...) Expand all Loading... |
56 #Get the tbsCertificate | 60 #Get the tbsCertificate |
57 tbsCertificateP = p.getChild(0) | 61 tbsCertificateP = p.getChild(0) |
58 | 62 |
59 #Is the optional version field present? | 63 #Is the optional version field present? |
60 #This determines which index the key is at. | 64 #This determines which index the key is at. |
61 if tbsCertificateP.value[0]==0xA0: | 65 if tbsCertificateP.value[0]==0xA0: |
62 subjectPublicKeyInfoIndex = 6 | 66 subjectPublicKeyInfoIndex = 6 |
63 else: | 67 else: |
64 subjectPublicKeyInfoIndex = 5 | 68 subjectPublicKeyInfoIndex = 5 |
65 | 69 |
| 70 #Get the subject |
| 71 self.subject = tbsCertificateP.getChildBytes(\ |
| 72 subjectPublicKeyInfoIndex - 1) |
| 73 |
66 #Get the subjectPublicKeyInfo | 74 #Get the subjectPublicKeyInfo |
67 subjectPublicKeyInfoP = tbsCertificateP.getChild(\ | 75 subjectPublicKeyInfoP = tbsCertificateP.getChild(\ |
68 subjectPublicKeyInfoIndex) | 76 subjectPublicKeyInfoIndex) |
69 | 77 |
70 #Get the algorithm | 78 #Get the algorithm |
71 algorithmP = subjectPublicKeyInfoP.getChild(0) | 79 algorithmP = subjectPublicKeyInfoP.getChild(0) |
72 rsaOID = algorithmP.value | 80 rsaOID = algorithmP.value |
73 if list(rsaOID) != [6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0]: | 81 if list(rsaOID) != [6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0]: |
74 raise SyntaxError("Unrecognized AlgorithmIdentifier") | 82 raise SyntaxError("Unrecognized AlgorithmIdentifier") |
75 | 83 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 if e[0] == cryptlib_py.CRYPT_ERROR_NOTFOUND: | 132 if e[0] == cryptlib_py.CRYPT_ERROR_NOTFOUND: |
125 returnVal = None | 133 returnVal = None |
126 return returnVal | 134 return returnVal |
127 finally: | 135 finally: |
128 cryptlib_py.cryptDestroyCert(c) | 136 cryptlib_py.cryptDestroyCert(c) |
129 | 137 |
130 def writeBytes(self): | 138 def writeBytes(self): |
131 return self.bytes | 139 return self.bytes |
132 | 140 |
133 | 141 |
OLD | NEW |