@@ -162,6 +162,19 @@ def _get_asn1_time(timestamp):
162162 return string_result
163163
164164
165+ class _X509NameInvalidator (object ):
166+ def __init__ (self ):
167+ self ._names = []
168+
169+ def add (self , name ):
170+ self ._names .append (name )
171+
172+ def clear (self ):
173+ for name in self ._names :
174+ # Breaks the object, but also prevents UAF!
175+ del name ._name
176+
177+
165178class PKey (object ):
166179 """
167180 A class representing an DSA or RSA public key or key pair.
@@ -1032,6 +1045,17 @@ def __init__(self):
10321045 _openssl_assert (x509 != _ffi .NULL )
10331046 self ._x509 = _ffi .gc (x509 , _lib .X509_free )
10341047
1048+ self ._issuer_invalidator = _X509NameInvalidator ()
1049+ self ._subject_invalidator = _X509NameInvalidator ()
1050+
1051+ @classmethod
1052+ def _from_raw_x509_ptr (cls , x509 ):
1053+ cert = cls .__new__ (cls )
1054+ cert ._x509 = _ffi .gc (x509 , _lib .X509_free )
1055+ cert ._issuer_invalidator = _X509NameInvalidator ()
1056+ cert ._subject_invalidator = _X509NameInvalidator ()
1057+ return cert
1058+
10351059 def to_cryptography (self ):
10361060 """
10371061 Export as a ``cryptography`` certificate.
@@ -1382,7 +1406,9 @@ def get_issuer(self):
13821406 :return: The issuer of this certificate.
13831407 :rtype: :class:`X509Name`
13841408 """
1385- return self ._get_name (_lib .X509_get_issuer_name )
1409+ name = self ._get_name (_lib .X509_get_issuer_name )
1410+ self ._issuer_invalidator .add (name )
1411+ return name
13861412
13871413 def set_issuer (self , issuer ):
13881414 """
@@ -1393,7 +1419,8 @@ def set_issuer(self, issuer):
13931419
13941420 :return: ``None``
13951421 """
1396- return self ._set_name (_lib .X509_set_issuer_name , issuer )
1422+ self ._set_name (_lib .X509_set_issuer_name , issuer )
1423+ self ._issuer_invalidator .clear ()
13971424
13981425 def get_subject (self ):
13991426 """
@@ -1407,7 +1434,9 @@ def get_subject(self):
14071434 :return: The subject of this certificate.
14081435 :rtype: :class:`X509Name`
14091436 """
1410- return self ._get_name (_lib .X509_get_subject_name )
1437+ name = self ._get_name (_lib .X509_get_subject_name )
1438+ self ._subject_invalidator .add (name )
1439+ return name
14111440
14121441 def set_subject (self , subject ):
14131442 """
@@ -1418,7 +1447,8 @@ def set_subject(self, subject):
14181447
14191448 :return: ``None``
14201449 """
1421- return self ._set_name (_lib .X509_set_subject_name , subject )
1450+ self ._set_name (_lib .X509_set_subject_name , subject )
1451+ self ._subject_invalidator .clear ()
14221452
14231453 def get_extension_count (self ):
14241454 """
@@ -1691,8 +1721,7 @@ def _exception_from_context(self):
16911721 # expect this call to never return :class:`None`.
16921722 _x509 = _lib .X509_STORE_CTX_get_current_cert (self ._store_ctx )
16931723 _cert = _lib .X509_dup (_x509 )
1694- pycert = X509 .__new__ (X509 )
1695- pycert ._x509 = _ffi .gc (_cert , _lib .X509_free )
1724+ pycert = X509 ._from_raw_x509_ptr (_cert )
16961725 return X509StoreContextError (errors , pycert )
16971726
16981727 def set_store (self , store ):
@@ -1755,9 +1784,7 @@ def load_certificate(type, buffer):
17551784 if x509 == _ffi .NULL :
17561785 _raise_current_error ()
17571786
1758- cert = X509 .__new__ (X509 )
1759- cert ._x509 = _ffi .gc (x509 , _lib .X509_free )
1760- return cert
1787+ return X509 ._from_raw_x509_ptr (x509 )
17611788
17621789
17631790def dump_certificate (type , cert ):
0 commit comments