|
| 1 | +Using Certitude |
| 2 | +=============== |
| 3 | + |
| 4 | +Certitude has one job: validating the TLS certificates a server has sent you. |
| 5 | +To do that, you need to pass Certitude the TLS certificate chain sent by the |
| 6 | +server, and the hostname you're expecting to connect to. |
| 7 | + |
| 8 | +Getting A Certificate Chain |
| 9 | +--------------------------- |
| 10 | + |
| 11 | +Certitude expects the TLS certificate chain as a list of TLS certificates |
| 12 | +stored in the DER representation. Unfortunately, the Python standard library's |
| 13 | +``ssl`` module is not capable of providing the entire certificate chain, only |
| 14 | +the leaf certificate. This means that to use Certitude you will need to use |
| 15 | +`pyopenssl`_ or something like it: it's just the only way to guarantee that |
| 16 | +you get the complete certificate chain. |
| 17 | + |
| 18 | +To get a certificate chain from PyOpenSSL, you'll want to make the connection |
| 19 | +as normal and then call ``get_peer_cert_chain()``. This will get you your cert |
| 20 | +chain as a list of ``X509`` objects. These will need decoding. |
| 21 | + |
| 22 | +Given an already existing connection ``cnx``, you can get your list of |
| 23 | +certificates like this: |
| 24 | + |
| 25 | +.. code-block:: python |
| 26 | +
|
| 27 | + certs = cnx.get_peer_cert_chain() |
| 28 | +
|
| 29 | + encoded_certs = [ |
| 30 | + OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_ASN1, cert) |
| 31 | + for cert in certs |
| 32 | + ] |
| 33 | +
|
| 34 | +Validating The Chain |
| 35 | +-------------------- |
| 36 | + |
| 37 | +Once you have the chain in place, it's simple enough to validate it. Simply |
| 38 | +pass the chain into ``certitude.validate_cert_chain`` along with a unicode |
| 39 | +string containing the expected hostname. For example: |
| 40 | + |
| 41 | +.. code-block:: python |
| 42 | +
|
| 43 | + valid = validate_cert_chain(encoded_certs, u'http2bin.org') |
| 44 | +
|
| 45 | +
|
| 46 | +The ``validate_cert_chain`` function returns ``True`` if the cert chain is |
| 47 | +valid, and ``False`` in any other case. |
| 48 | + |
| 49 | +Notes |
| 50 | +----- |
| 51 | + |
| 52 | +When validating certificates using certitude you'll likely want to *disable* |
| 53 | +OpenSSL's certificate validation. This is because OpenSSL and the |
| 54 | +platform-specific TLS validation code will build their certificate chains |
| 55 | +differently. In particular, OpenSSL may be *unable* to validate a chain that |
| 56 | +the system library believes is valid. For that reason, put OpenSSL into the |
| 57 | +``VERIFY_NONE`` mode and then handle the validation manually, *after* the |
| 58 | +connection is made but **before you send any data on it**. |
| 59 | + |
| 60 | +We cannot stress this enough: **you must validate the certificates before |
| 61 | +sending or receiving data on the connection**. |
| 62 | + |
| 63 | + |
| 64 | +.. _pyopenssl: https://pyopenssl.readthedocs.io/en/stable/ |
0 commit comments