09.11.2016 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

C H A P T E R 6<br />

■ ■ ■<br />

TLS and SSL<br />

The short story is this: before you send sensitive data across a network, you need pro<strong>of</strong> <strong>of</strong> the identity <strong>of</strong><br />

the machine that you think is on the other end <strong>of</strong> the socket, and while sending the data, you need it<br />

protected against the prying eyes <strong>of</strong> anyone controlling the gateways and network switches that see all <strong>of</strong><br />

your packets. The solution to this problem is to use Transport Layer Security (TLS). Because earlier<br />

versions <strong>of</strong> TLS were called the Secure Sockets Layer (SSL), nearly all <strong>of</strong> the libraries that you will use to<br />

speak TLS actually still have SSL somewhere in the name.<br />

Simple enough?<br />

The actual libraries introduced in this chapter, and all <strong>of</strong> the program listings that we will discuss,<br />

are going be about TLS; that is really the only take-home coding lesson you will find here. But we will do<br />

the actual examples last, once you have enough context to understand what they can—and cannot—do<br />

for the security <strong>of</strong> your network programs.<br />

Computer Security<br />

The subject <strong>of</strong> computer security is vast, with its own pundits, blogs, magazines, conferences, academic<br />

journals, consultancies, and product lines. And the subject is deserving <strong>of</strong> this extensive treatment: it is<br />

important, it is <strong>of</strong>ten done badly, and we programmers are still—as a pr<strong>of</strong>ession—a very long way from<br />

even beginning to consistently get it right.<br />

The field also has its share <strong>of</strong> charlatans and con men. And, <strong>of</strong> course, there are legions who work to<br />

subvert security mechanisms. Script kiddies seek surreptitious control over thousands <strong>of</strong> Windows<br />

desktops to power their next denial-<strong>of</strong>-service attack against their middle school. Organized criminals<br />

want to steal millions <strong>of</strong> dollars (or pounds sterling, or yen, or rupees, depending on their jurisdiction)<br />

through intercepted credit card numbers and identity theft. Militaries and governments seek to protect<br />

their own information systems while gaining the ability to commandeer or disable those <strong>of</strong> their rivals.<br />

How can we competently dare to write programs that will be on the same network as such<br />

significant threats?<br />

It may be foolish for me to attempt to answer this question; I should perhaps, at this point, be<br />

sending you right into the vast literature on the subject <strong>of</strong> secure s<strong>of</strong>tware, and not risking <strong>of</strong>fering you a<br />

few pointers when volumes <strong>of</strong> wisdom are what you really need. But since some readers may read this<br />

book on a tight schedule, tasked with adding TLS to some hastily written network program just days<br />

before it ships, I will go ahead and <strong>of</strong>fer a few short thoughts on creating half-decent s<strong>of</strong>tware.<br />

First, always have thorough tests. Use Ned Batchelder's coverage tool to measure how much <strong>of</strong> your<br />

code is being tested at all. Freely refactor your code until testing each module is not just possible, but<br />

downright convenient! When a module is difficult to test, that is an excellent signal that the module has<br />

too many hard-coded entanglements with other parts <strong>of</strong> the code, and that it will be difficult to predict<br />

how the system as a whole will behave. The ease with which code can be tested, in other words, is <strong>of</strong>ten<br />

directly related to how easy it is to draw a boundary around which other parts <strong>of</strong> the system the code can<br />

directly affect.<br />

87

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

Saved successfully!

Ooh no, something went wrong!