OpenSSL is a free implementation of the SSL/TLS protocol, which is the most widely used protocol for secure network communications. This library can be used programmatically, and can be used from the command line to secure most TCP-based network protocols.OpenSSL is also a general-purpose cryptographic library with implementations of RSA, DSA, and DH public key algorithms; various message digest algorithms, such as MD5, SHA1, and RIPE-MD160; and a wide variety of symmetric ciphers, including 3DES, RC4, IDEA, and many others (the upcoming 0.9.7 release contains support for AES, the Advanced Encryption Standard). Support for X.509 certificates, various PKCS standards, and S/MIME v2 for secure electronic mail is also included. Instead of getting bogged down in the technical details of how SSL works under the hood, this book provides only the information that is necessary to use OpenSSL safely and effectively. The reader is taken step by step from understanding the challenges faced in communicating securely to using the OpenSSL tools to best meet those challenges. System and network administrators will benefit from the thorough treatment of the OpenSSL command-line interface, as well as from step-by-step directions for obtaining certificates and setting up their own certification authority. Developers will benefit from the in-depth discussions and examples of how to use OpenSSL in their own programs. Although OpenSSL is written in C, information on how to use OpenSSL with Perl, Python, and PHP is also included.
Author(s): John Viega, Matt Messier, Pravir Chandra
Edition: 1
Publisher: O'Reilly Media
Year: 2002
Language: English
Pages: 338
Network Security with OpenSSL.pdf......Page 0
Table of Content......Page 2
Dedication......Page 6
Preface......Page 7
About This Book......Page 8
Conventions Used in This Book......Page 10
Acknowledgments......Page 11
1.1 Cryptography for the Rest of Us......Page 13
1.1.1 Goals of Cryptography......Page 14
1.1.2 Cryptographic Algorithms......Page 15
Figure 1-1. Symmetric key cryptography......Page 16
Figure 1-2. Public key cryptography......Page 17
1.1.2.3 Cryptographic hash functions and Message Authentication Codes......Page 19
Figure 1-3. An overview of direct communication in SSL......Page 20
Figure 1-4. A man-in-the-middle attack......Page 21
1.3.1 Efficiency......Page 22
1.3.2 Keys in the Clear......Page 23
1.3.3 Bad Server Credentials......Page 24
1.3.4 Certificate Validation......Page 25
1.3.5 Poor Entropy......Page 26
1.3.6 Insecure Cryptography......Page 27
1.4.3 Protection Against Software Flaws......Page 28
1.5 OpenSSL Basics......Page 29
1.6 Securing Third-Party Software......Page 30
Figure 1-5. Stunnel proxies......Page 31
Figure 1-6. Load balancing with Stunnel for cryptographic acceleration......Page 32
1.6.2 Client-Side Proxies......Page 33
2.1.1 Configuration Files......Page 35
Example 2-1. An excerpt from the default OpenSSL configuration file......Page 36
2.2 Message Digest Algorithms......Page 37
2.2.1 Examples......Page 38
2.3 Symmetric Ciphers......Page 39
2.4 Public Key Cryptography......Page 40
2.4.2 Digital Signature Algorithm......Page 41
2.4.2.1 Examples......Page 42
2.4.3.1 Examples......Page 43
2.5 S/MIME......Page 44
2.6 Passwords and Passphrases......Page 45
2.7 Seeding the Pseudorandom Number Generator......Page 47
3.1 Certificates......Page 49
3.1.1.1 Private Certification Authorities......Page 50
3.1.2 Certificate Hierarchies......Page 51
Table 3-1. Common bit settings for the keyUsage extension......Page 52
3.1.4 Certificate Revocation Lists......Page 53
3.1.5 Online Certificate Status Protocol......Page 55
3.2 Obtaining a Certificate......Page 56
3.2.1 Personal Certificates......Page 57
3.2.2 Code-Signing Certificates......Page 58
3.3 Setting Up a Certification Authority......Page 59
Example 3-1. Creating the CA's environment......Page 60
3.3.2 Building an OpenSSL Configuration File......Page 61
3.3.3 Creating a Self-Signed Root Certificate......Page 62
Example 3-4. Configuration file additions for generating a self-signed root certificate......Page 63
example and the configuration file, the key would in fact be 2048 bits)......Page 64
3.3.4 Issuing Certificates......Page 65
Example 3-7. The resulting certificate request......Page 66
Example 3-8. Issuing a certificate from a certificate request......Page 67
Example 3-9. Revoking a certificate......Page 69
Example 3-10. A certificate revocation list......Page 70
4.1 Multithread Support......Page 72
Example 4-1. Static locking callbacks for WIN32 and POSIX threads systems......Page 73
4.1.2 Dynamic Locking Callbacks......Page 75
Example 4-2. E xtensions to the library to support the dynamic locking mechanism......Page 77
4.2.1 Manipulating Error Queues......Page 78
4.2.2 Human-Readable Error Messages......Page 80
4.3 Abstract Input/Output......Page 82
4.3.1.2 File sources/sinks......Page 87
4.3.1.3 Socket sources/sinks......Page 88
Example 4-6. Creating a socket BIO......Page 89
Example 4-7. Creating BIO pairs......Page 90
Example 4-8. Assembling and using a BIO chain......Page 91
4.4 Random Number Generation......Page 92
4.4.1 Seeding the PRNG......Page 93
Example 4-10. Seeding OpenSSL's PRNG with /dev/random......Page 95
Example 4-11. Seeding OpenSSL's PRNG with EGADS......Page 96
4.5 Arbitrary Precision Math......Page 97
Example 4-14. The wrong way and the right way to copy a BIGNUM......Page 98
Example 4-15. Converting between BIGNUM and binary representations......Page 99
Table 4-1. Arithmetic functions for BIGNUMs......Page 100
4.5.3 Generating Prime Numbers......Page 101
Example 4-16. Generating a pseudorandom prime number with BN_generate_prime()......Page 102
Table 4-2. Supported hardware and software engines......Page 103
Table 4-3. Flags for ENGINE_set_default......Page 104
5.1.1 The Application(s) to Secure......Page 105
Example 5-1. common.h......Page 106
Example 5-3. client.c......Page 107
Example 5-4. The server application......Page 108
5.1.2 Step 1: SSL Version Selection and Certificate Preparation......Page 109
Table 5-1. Functions to retrieve pointers to SSL_METHOD objects......Page 110
5.1.2.2 Certificate preparation......Page 111
Example 5-5. client1.c......Page 113
Example 5-6. server1.c......Page 117
5.1.3.2 Incorporating trusted certificates......Page 119
5.1.3.3 Certificate verification......Page 120
Example 5-7. A verify callback (implemented in common.c and prototyped in common.h)......Page 122
5.1.3.5 Post-connection assertions......Page 123
Example 5-8. A function to do post-connection assertions (implemented in common.c and prototyped in common.h)......Page 125
Example 5-9. client2.c......Page 127
Example 5-10. server2.c......Page 129
5.1.4 Step 3: SSL Options and Cipher Suites......Page 131
5.1.4.2 Ephemeral keying......Page 132
5.1.4.3 Cipher suite selection......Page 133
Example 5-11. client3.c......Page 134
Example 5-12. server3.c......Page 135
5.1.4.5 Beyond the example......Page 136
5.2 Advanced Programming with SSL......Page 137
5.2.1.1 Client-side SSL sessions......Page 138
5.2.1.2 Server-side SSL sessions......Page 139
5.2.1.3 An on-disk, session caching framework......Page 140
Example 5-14. A framework for external session caching......Page 141
Table 5-2. Return values of SSL_read and SSL_write......Page 142
Example 5-15. A sample I/O call template......Page 143
5.2.2.3 Non-blocking I/O......Page 144
Example 5-16. A sample non-blocking I/O loop......Page 145
5.2.3 SSL Renegotiations......Page 150
Example 5-17. Code fragment to force a renegotiation from a server......Page 151
Example 5-18. Code to cause forced renegotiation in order to request stronger client authentication and distinguish the sessions......Page 152
5.2.3.3 Further notes......Page 153
6.1.1 Block Ciphers and Stream Ciphers......Page 155
6.1.2 Basic Block Cipher Modes......Page 156
6.2.1 Available Ciphers......Page 157
Table 6-3. Referencing the CAST5 cipher......Page 158
Table 6-6. Referencing 3DES......Page 159
6.2.1.9 RC4?......Page 160
6.2.2 Initializing Symmetric Ciphers......Page 161
Example 6-1. Preparing to use Blowfish in CBC mode for encryption......Page 163
6.2.3 Specifying Key Length and Other Options......Page 164
6.2.4 Encryption......Page 166
Example 6-3. Encrypting plaintext 100 bytes at a time......Page 167
Example 6-4. Performing incremental encryption......Page 168
Example 6-5. Decrypting ciphertext......Page 169
6.2.6 Handling UDP Traffic with Counter Mode......Page 170
Example 6-7. Encryption and decryption using counter mode......Page 172
6.3 General Recommendations......Page 173
7.1 Overview of Hashes and MACs......Page 174
7.2 Hashing with the EVP API......Page 175
Table 7-1. Message digests and the EVP interface......Page 176
Example 7-1. Computing a hash value using the EVP API......Page 177
Example 7-3. Computing SHA1 hashes of files......Page 178
7.3 Using MACs......Page 180
Example 7-5. A binary comparison function......Page 181
Example 7-6. Computing a MAC using HMAC_Init, HMAC_Update, and HMAC_Final......Page 183
7.3.1.1 CBC-MAC......Page 184
Example 7-7. cbcmac.h......Page 185
Example 7-8. cbcmac.c......Page 186
Example 7-9. xcbcmac.h......Page 187
Example 7-10. xcbcmac.c......Page 188
7.4 Secure HTTP Cookies......Page 191
Example 7-11. Encrypting data for storage in a cookie......Page 192
Example 7-12. Decrypting data stored in a cookie......Page 194
8.1 When to Use Public Key Cryptography......Page 196
8.2 Diffie-Hellman......Page 197
8.2.2 Generating and Exchanging Parameters......Page 198
8.2.3 Computing Shared Secrets......Page 200
8.2.1 The Basics......Page 202
8.2.2 Generating and Exchanging Parameters......Page 203
8.2.3 Computing Shared Secrets......Page 205
8.2.4 Practical Applications......Page 206
8.3.1 The Basics......Page 207
8.3.2 Generating Parameters and Keys......Page 208
8.3.3 Signing and Verifying......Page 209
8.3.4 Practical Applications......Page 211
8.4.2 Generating Keys......Page 212
8.4.3 Data Encryption, Key Agreement, and Key Transport......Page 213
8.4.4 Signing and Verifying......Page 215
8.5 The EVP Public Key Interface......Page 217
8.5.1 Signing and Verifying......Page 218
8.5.2 Encrypting and Decrypting......Page 221
Example 8-1. Calling EVP_SealInit......Page 222
8.6 Encoding and Decoding Objects......Page 225
Example 8-3. DER-decoding an RSA public key......Page 226
Table 8-1. Functions for reading and writing DER encodings of public key objects......Page 227
8.6.2 Writing and Reading PEM-Encoded Objects......Page 228
Table 8-2. Functions for reading and writing PEM encodings of public key objects......Page 230
9.1 Net::SSLeay for Perl......Page 232
9.1.1 Net::SSLeay Variables......Page 233
9.1.3 Net::SSLeay Utility Functions......Page 234
9.2 M2Crypto for Python......Page 237
9.2.2.1 M2Crypto.SSL......Page 238
9.2.2.2 M2Crypto.BIO......Page 239
Example 9-1. Computing the cryptographic hash of data......Page 240
Example 9-2. Encrypting and decrypting with a symmetric cipher......Page 241
9.2.2.4 Miscellaneous crypto......Page 242
9.2.3.1 Extensions to httplib: httpslib......Page 243
9.2.3.3 Extensions to xmlrpclib: m2xmlrpclib......Page 244
9.3.1 General Functions......Page 245
9.3.2 Certificate Functions......Page 246
Table 9-1. Possible purpose values for openssl_x509_checkpurpose......Page 247
9.3.3 Encryption and Signing Functions......Page 248
Table 9-3. Flags: openssl_pkcs7_encrypt, openssl_pkcs7_sign, openssl_pkcs7_verify......Page 250
Example 10-1. Stack manipulation functions in generic form......Page 253
10.2 Configuration Files......Page 254
Example 10-3. Code to interact with the configuration file......Page 255
10.3 X.509......Page 257
10.3.1.2 X.509 Version 3 extensions......Page 258
Example 10-5. A program to generate a certificate request......Page 259
10.3.2 Making Certificates......Page 262
Example 10-6. Creating a certificate from a request and CA credentials......Page 263
10.3.3 X.509 Certificate Checking......Page 267
Example 10-7. Verifying a client certificate......Page 268
10.4.1 Signing and Verifying......Page 271
Example 10-8. A signing and verifying utility......Page 272
Example 10-9. A utility to encrypt and decrypt S/MIME messages......Page 276
10.4.4 PKCS#7 Flags......Page 279
10.5.1 Wrapping Information into a PKCS#12 Object......Page 280
10.5.2 Importing Objects from PKCS#12 Data......Page 281
Options......Page 282
Options......Page 283
Configuration Options......Page 286
Notes......Page 288
Options......Page 289
Options......Page 291
Options......Page 292
Options......Page 293
Options......Page 294
Options......Page 296
Options......Page 297
Table A-1. Ciphers supported by the enc command......Page 298
Options......Page 299
Notes......Page 300
Options......Page 301
Options......Page 302
Options......Page 303
Options......Page 305
Options......Page 308
Configuration Options......Page 311
Notes......Page 312
Options......Page 313
Notes......Page 314
Options......Page 315
Options......Page 316
Options......Page 318
Options......Page 321
Options......Page 323
Options......Page 324
Notes......Page 327
Options......Page 328
Options......Page 329
Options......Page 330
Display Options......Page 331
Signing Options......Page 333
Name Options......Page 335
Colophon......Page 338