09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 6 ■ TLS AND SSL<br />

the defaults for the wrap_socket() call in the Standard Library—is if you need to speak to browsers or<br />

other third-party clients that might not have upgraded to full-fledged TLS yet.<br />

Loose Ends<br />

When adding cryptography to your application, it is always a good idea to read up-to-date documentation.<br />

The advice given in this chapter would have been quite different if this revision <strong>of</strong> the book had happened<br />

even just one or two years earlier, and in two or three more years it will doubtless be out <strong>of</strong> date.<br />

In particular, the idea has been around for a long time in the public-key cryptography literature that<br />

there should exist certificate revocation lists, where client certificates and even certificate-authority<br />

certificates could be listed if they are discovered to have been compromised and must no longer be<br />

trusted. That way, instead <strong>of</strong> everyone waiting for operating system updates or browser upgrades to<br />

bring the news that an old CA certificate should no longer be trusted, they could instantly be protected<br />

against any client certificates minted with the stolen private key.<br />

Also, security vulnerabilities continue to be discovered not only in particular programs but also in<br />

the design <strong>of</strong> various security protocols themselves—SSL version 2 was, in fact, the victim <strong>of</strong> just such a<br />

discovery in the mid-1990s, which is why many people simply turn it <strong>of</strong>f as an option when using TLS.<br />

All <strong>of</strong> which is to say: use this chapter as a basic API reference and introduction to the whole topic <strong>of</strong><br />

secure sockets, but consult something more up-to-date if you are creating new s<strong>of</strong>tware more than a<br />

year after this book comes out, to make sure the protocols still operate well if used as shown here. As <strong>of</strong><br />

this writing, the Standard Library documentation, <strong>Python</strong> blogs, and Stack Overflow questions about<br />

cryptography are all good places to look.<br />

Summary<br />

Computer security is a large and complicated subject. At its core is the fact that an intruder or<br />

troublemaker will take advantage <strong>of</strong> almost any mistake you make—even an apparently very small one—<br />

to try to leverage control over your systems and s<strong>of</strong>tware.<br />

<strong>Network</strong>s are the locus <strong>of</strong> much security effort because the IP protocols, by default, copy all your<br />

information into packets verbatim, where it can be read by anyone watching your packets go past.<br />

Passive sniffing, man-in-the-middle attacks, connection hijacking, and replay attacks are all possible if<br />

an adversary has control over the network between a client and server.<br />

Fortunately, mathematicians have invented public-key cryptography, which has been packaged as<br />

the TLS protocol for protecting IP sockets. It grew out <strong>of</strong> an older, less secure protocol named SSL, from<br />

which most s<strong>of</strong>tware libraries that speak TLS take their name.<br />

The <strong>Python</strong> Standard Library now supplies an ssl package (though it has to be downloaded<br />

separately for <strong>Python</strong> 2.5), which can leverage the OpenSSL library to secure your own application<br />

sockets. This makes it impossible for a third party to masquerade as a properly certified server machine,<br />

and also encrypts all data so that an observer cannot determine what your client and server programs<br />

are saying to one another.<br />

There are two keys to using the ssl package. First, you should always wrap the bare socket you<br />

create with its wrap_socket() function, giving the right arguments for the kind <strong>of</strong> connection and<br />

certificate assurances that you need. Second, if you expect the other side to provide a certificate, then<br />

you should run match_hostname() to make sure that they are claiming the identity that you expect.<br />

The security playing field shifts every few years, with old protocols obsoleted and new ones<br />

developed, so keep abreast <strong>of</strong> news if you are writing security-sensitive applications.<br />

98

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!