O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Secure Programming Cookbook for C and C++

Book Description

Password sniffing, spoofing, buffer overflows, and denial of service: these are only a few of the attacks on today's computer systems and networks. At the root of this epidemic is poorly written, poorly tested, and insecure code that puts everyone at risk. Clearly, today's developers need help figuring out how to write code that attackers won't be able to exploit. But writing such code is surprisingly difficult.

Secure Programming Cookbook for C and C++ is an important new resource for developers serious about writing secure code. It contains a wealth of solutions to problems faced by those who care about the security of their applications. It covers a wide range of topics, including safe initialization, access control, input validation, symmetric and public key cryptography, cryptographic hashes and MACs, authentication and key exchange, PKI, random numbers, and anti-tampering. The rich set of code samples provided in the book's more than 200 recipes will help programmers secure the C and C++ programs they write for both Unix® (including Linux®) and Windows® environments. Readers will learn:

  • How to avoid common programming errors, such as buffer overflows, race conditions, and format string problems
  • How to properly SSL-enable applications
  • How to create secure channels for client-server communication without SSL
  • How to integrate Public Key Infrastructure (PKI) into applications
  • Best practices for using cryptography properly
  • Techniques and strategies for properly validating input to programs
  • How to launch programs securely
  • How to use file access mechanisms properly
  • Techniques for protecting applications from reverse engineering
The book's web site supplements the book by providing a place to post new recipes, including those written in additional languages like Perl, Java, and Python. Monthly prizes will reward the best recipes submitted by readers.

Secure Programming Cookbook for C and C++ is destined to become an essential part of any developer's library, a code companion developers will turn to again and again as they seek to protect their systems from attackers and reduce the risks they face in today's dangerous world.

Table of Contents

  1. Secure Programming Cookbook for C and C++
  2. A Note Regarding Supplemental Files
  3. Foreword
  4. Preface
    1. More Than Just a Book
    2. We Can’t Do It All
    3. Organization of This Book
    4. Recipe Compatibility
    5. Conventions Used in This Book
    6. Comments and Questions
    7. Acknowledgments
  5. 1. Safe Initialization
    1. 1.1. Sanitizing the Environment
      1. 1.1.1. Problem
      2. 1.1.2. Solution
      3. 1.1.3. Discussion
      4. 1.1.4. See Also
    2. 1.2. Restricting Privileges on Windows
      1. 1.2.1. Problem
      2. 1.2.2. Solution
      3. 1.2.3. Discussion
        1. 1.2.3.1. Creating restricted tokens
        2. 1.2.3.2. Modifying a process’s primary token
        3. 1.2.3.3. Working with SID_AND_ATTRIBUTES structures
        4. 1.2.3.4. Working with LUID_AND_ATTRIBUTES structures
      4. 1.2.4. See Also
    3. 1.3. Dropping Privileges in setuid Programs
      1. 1.3.1. Problem
      2. 1.3.2. Solution
      3. 1.3.3. Discussion
      4. 1.3.4. See Also
    4. 1.4. Limiting Risk with Privilege Separation
      1. 1.4.1. Problem
      2. 1.4.2. Solution
      3. 1.4.3. Discussion
        1. 1.4.3.1. Privilege separation
        2. 1.4.3.2. A privilege separation library: privman
      4. 1.4.4. See Also
    5. 1.5. Managing File Descriptors Safely
      1. 1.5.1. Problem
      2. 1.5.2. Solution
      3. 1.5.3. Discussion
    6. 1.6. Creating a Child Process Securely
      1. 1.6.1. Problem
      2. 1.6.2. Solution
      3. 1.6.3. Discussion
      4. 1.6.4. See Also
    7. 1.7. Executing External Programs Securely
      1. 1.7.1. Problem
      2. 1.7.2. Solution
      3. 1.7.3. Discussion
      4. 1.7.4. See Also
    8. 1.8. Executing External Programs Securely
      1. 1.8.1. Problem
      2. 1.8.2. Solution
      3. 1.8.3. Discussion
    9. 1.9. Disabling Memory Dumps in the Event of a Crash
      1. 1.9.1. Problem
      2. 1.9.2. Solution
      3. 1.9.3. Discussion
      4. 1.9.4. See Also
  6. 2. Access Control
    1. 2.1. Understanding the Unix Access Control Model
      1. 2.1.1. Problem
      2. 2.1.2. Solution
      3. 2.1.3. Discussion
        1. 2.1.3.1. The sticky bit
        2. 2.1.3.2. The setuid bit
        3. 2.1.3.3. The setgid bit
      4. 2.1.4. See Also
    2. 2.2. Understanding the Windows Access Control Model
      1. 2.2.1. Problem
      2. 2.2.2. Solution
      3. 2.2.3. Discussion
    3. 2.3. Determining Whether a User Has Access to a File on Unix
      1. 2.3.1. Problem
      2. 2.3.2. Solution
      3. 2.3.3. Discussion
    4. 2.4. Determining Whether a Directory Is Secure
      1. 2.4.1. Problem
      2. 2.4.2. Solution
      3. 2.4.3. Discussion
    5. 2.5. Erasing Files Securely
      1. 2.5.1. Problem
      2. 2.5.2. Solution
      3. 2.5.3. Discussion
      4. 2.5.4. See Also
    6. 2.6. Accessing File Information Securely
      1. 2.6.1. Problem
      2. 2.6.2. Solution
      3. 2.6.3. Discussion
      4. 2.6.4. See Also
    7. 2.7. Restricting Access Permissions for New Files on Unix
      1. 2.7.1. Problem
      2. 2.7.2. Solution
      3. 2.7.3. Discussion
      4. 2.7.4. See Also
    8. 2.8. Locking Files
      1. 2.8.1. Problem
      2. 2.8.2. Solution
      3. 2.8.3. Discussion
        1. 2.8.3.1. Locking files on Unix
        2. 2.8.3.2. Locking files on Windows
    9. 2.9. Synchronizing Resource Access Across Processes on Unix
      1. 2.9.1. Problem
      2. 2.9.2. Solution
      3. 2.9.3. Discussion
      4. 2.9.4. See Also
    10. 2.10. Synchronizing Resource Access Across Processes on Windows
      1. 2.10.1. Problem
      2. 2.10.2. Solution
      3. 2.10.3. Discussion
      4. 2.10.4. See Also
    11. 2.11. Creating Files for Temporary Use
      1. 2.11.1. Problem
      2. 2.11.2. Solution
      3. 2.11.3. Discussion
        1. 2.11.3.1. Temporary files on Unix
        2. 2.11.3.2. Temporary files on Windows
      4. 2.11.4. See Also
    12. 2.12. Restricting Filesystem Access on Unix
      1. 2.12.1. Problem
      2. 2.12.2. Solution
      3. 2.12.3. Discussion
    13. 2.13. Restricting Filesystem and Network Access on FreeBSD
      1. 2.13.1. Problem
      2. 2.13.2. Solution
      3. 2.13.3. Discussion
      4. 2.13.4. See Also
  7. 3. Input Validation
    1. 3.1. Understanding Basic Data Validation Techniques
      1. 3.1.1. Problem
      2. 3.1.2. Solution
      3. 3.1.3. Discussion
      4. 3.1.4. See Also
    2. 3.2. Preventing Attacks on Formatting Functions
      1. 3.2.1. Problem
      2. 3.2.2. Solution
      3. 3.2.3. Discussion
      4. 3.2.4. See Also
    3. 3.3. Preventing Buffer Overflows
      1. 3.3.1. Problem
      2. 3.3.2. Solution
      3. 3.3.3. Discussion
        1. 3.3.3.1. String handling
        2. 3.3.3.2. Using C++
        3. 3.3.3.3. Stack protection technologies
      4. 3.3.4. See Also
    4. 3.4. Using the SafeStr Library
      1. 3.4.1. Problem
      2. 3.4.2. Solution
      3. 3.4.3. Discussion
      4. 3.4.4. See Also
    5. 3.5. Preventing Integer Coercion and Wrap-Around Problems
      1. 3.5.1. Problem
      2. 3.5.2. Solution
      3. 3.5.3. Discussion
        1. 3.5.3.1. Signed-to-unsigned coercion
        2. 3.5.3.2. Unsigned-to-signed coercion
        3. 3.5.3.3. Size mismatches
        4. 3.5.3.4. Wrap-around
      4. 3.5.4. See Also
    6. 3.6. Using Environment Variables Securely
      1. 3.6.1. Problem
      2. 3.6.2. Solution
      3. 3.6.3. Discussion
        1. 3.6.3.1. Obtaining the value of an environment variable
        2. 3.6.3.2. Changing the value of an environment variable
        3. 3.6.3.3. Deleting an environment variable
      4. 3.6.4. See Also
    7. 3.7. Validating Filenames and Paths
      1. 3.7.1. Problem
      2. 3.7.2. Solution
      3. 3.7.3. Discussion
    8. 3.8. Evaluating URL Encodings
      1. 3.8.1. Problem
      2. 3.8.2. Solution
      3. 3.8.3. Discussion
      4. 3.8.4. See Also
    9. 3.9. Validating Email Addresses
      1. 3.9.1. Problem
      2. 3.9.2. Solution
      3. 3.9.3. Discussion
      4. 3.9.4. See Also
    10. 3.10. Preventing Cross-Site Scripting
      1. 3.10.1. Problem
      2. 3.10.2. Solution
      3. 3.10.3. Discussion
    11. 3.11. Preventing SQL Injection Attacks
      1. 3.11.1. Problem
      2. 3.11.2. Solution
      3. 3.11.3. Discussion
    12. 3.12. Detecting Illegal UTF-8 Characters
      1. 3.12.1. Problem
      2. 3.12.2. Solution
      3. 3.12.3. Discussion
    13. 3.13. Preventing File Descriptor Overflows When Using select( )
      1. 3.13.1. Problem
      2. 3.13.2. Solution
      3. 3.13.3. Discussion
      4. 3.13.4. See Also
  8. 4. Symmetric Cryptography Fundamentals
    1. 4.1. Representing Keys for Use in Cryptographic Algorithms
      1. 4.1.1. Problem
      2. 4.1.2. Solution
      3. 4.1.3. Discussion
      4. 4.1.4. See Also
    2. 4.2. Generating Random Symmetric Keys
      1. 4.2.1. Problem
      2. 4.2.2. Solution
      3. 4.2.3. Discussion
      4. 4.2.4. See Also
    3. 4.3. Representing Binary Keys (or Other Raw Data) as Hexadecimal
      1. 4.3.1. Problem
      2. 4.3.2. Solution
      3. 4.3.3. Discussion
    4. 4.4. Turning ASCII Hex Keys (or Other ASCII Hex Data) into Binary
      1. 4.4.1. Problem
      2. 4.4.2. Solution
      3. 4.4.3. Discussion
    5. 4.5. Performing Base64 Encoding
      1. 4.5.1. Problem
      2. 4.5.2. Solution
      3. 4.5.3. Discussion
      4. 4.5.4. See Also
    6. 4.6. Performing Base64 Decoding
      1. 4.6.1. Problem
      2. 4.6.2. Solution
      3. 4.6.3. Discussion
      4. 4.6.4. See Also
    7. 4.7. Representing Keys (or Other Binary Data) as English Text
      1. 4.7.1. Problem
      2. 4.7.2. Solution
      3. 4.7.3. Discussion
      4. 4.7.4. See Also
    8. 4.8. Converting Text Keys to Binary Keys
      1. 4.8.1. Problem
      2. 4.8.2. Solution
      3. 4.8.3. Discussion
      4. 4.8.4. See Also
    9. 4.9. Using Salts, Nonces, and Initialization Vectors
      1. 4.9.1. Problem
      2. 4.9.2. Solution
      3. 4.9.3. Discussion
        1. 4.9.3.1. Salts
        2. 4.9.3.2. Nonces
        3. 4.9.3.3. Initialization vectors (IVs)
      4. 4.9.4. See Also
    10. 4.10. Deriving Symmetric Keys from a Password
      1. 4.10.1. Problem
      2. 4.10.2. Solution
      3. 4.10.3. Discussion
      4. 4.10.4. See Also
    11. 4.11. Algorithmically Generating Symmetric Keys from One Base Secret
      1. 4.11.1. Problem
      2. 4.11.2. Solution
      3. 4.11.3. Discussion
      4. 4.11.4. See Also
    12. 4.12. Encrypting in a Single Reduced Character Set
      1. 4.12.1. Problem
      2. 4.12.2. Solution
      3. 4.12.3. Discussion
      4. 4.12.4. See Also
    13. 4.13. Managing Key Material Securely
      1. 4.13.1. Problem
      2. 4.13.2. Solution
      3. 4.13.3. See Also
    14. 4.14. Timing Cryptographic Primitives
      1. 4.14.1. Problem
      2. 4.14.2. Solution
      3. 4.14.3. Discussion
        1. 4.14.3.1. Timing basics
        2. 4.14.3.2. Timing cryptographic code
  9. 5. Symmetric Encryption
    1. 5.1. Deciding Whether to Use Multiple Encryption Algorithms
      1. 5.1.1. Problem
      2. 5.1.2. Solution
      3. 5.1.3. Discussion
      4. 5.1.4. See Also
    2. 5.2. Figuring Out Which Encryption Algorithm Is Best
      1. 5.2.1. Problem
      2. 5.2.2. Solution
      3. 5.2.3. Discussion
      4. 5.2.4. See Also
    3. 5.3. Selecting an Appropriate Key Length
      1. 5.3.1. Problem
      2. 5.3.2. Solution
      3. 5.3.3. Discussion
      4. 5.3.4. See Also
    4. 5.4. Selecting a Cipher Mode
      1. 5.4.1. Problem
      2. 5.4.2. Solution
      3. 5.4.3. Discussion
        1. 5.4.3.1. Electronic Code Book (ECB) mode
        2. 5.4.3.2. Cipher Block Chaining (CBC) mode
        3. 5.4.3.3. Counter (CTR) mode
        4. 5.4.3.4. Output Feedback (OFB) mode
        5. 5.4.3.5. Cipher Feedback (CFB) mode
        6. 5.4.3.6. Carter-Wegman + CTR (CWC) mode
        7. 5.4.3.7. Offset Codebook (OCB) mode
        8. 5.4.3.8. CTR plus CBC-MAC (CCM) mode
      4. 5.4.4. See Also
    5. 5.5. Using a Raw Block Cipher
      1. 5.5.1. Problem
      2. 5.5.2. Solution
      3. 5.5.3. Discussion
        1. 5.5.3.1. Brian Gladman’s AES implementation
        2. 5.5.3.2. OpenSSL block cipher implementations
      4. 5.5.4. See Also
    6. 5.6. Using a Generic CBC Mode Implementation
      1. 5.6.1. Problem
      2. 5.6.2. Solution
      3. 5.6.3. Discussion
        1. 5.6.3.1. The high-level API
        2. 5.6.3.2. SPC_CBC_CTX data type
        3. 5.6.3.3. Incremental initialization
        4. 5.6.3.4. Incremental encrypting
        5. 5.6.3.5. Incremental decryption
      4. 5.6.4. See Also
    7. 5.7. Using a Generic CFB Mode Implementation
      1. 5.7.1. Problem
      2. 5.7.2. Solution
      3. 5.7.3. Discussion
        1. 5.7.3.1. The high-level API
        2. 5.7.3.2. The incremental API
      4. 5.7.4. See Also
    8. 5.8. Using a Generic OFB Mode Implementation
      1. 5.8.1. Problem
      2. 5.8.2. Solution
      3. 5.8.3. Discussion
        1. 5.8.3.1. The high-level API
        2. 5.8.3.2. The incremental API
      4. 5.8.4. See Also
    9. 5.9. Using a Generic CTR Mode Implementation
      1. 5.9.1. Problem
      2. 5.9.2. Solution
      3. 5.9.3. Discussion
        1. 5.9.3.1. The high-level API
        2. 5.9.3.2. The incremental API
      4. 5.9.4. See Also
    10. 5.10. Using CWC Mode
      1. 5.10.1. Problem
      2. 5.10.2. Solution
      3. 5.10.3. Discussion
      4. 5.10.4. See Also
    11. 5.11. Manually Adding and Checking Cipher Padding
      1. 5.11.1. Problem
      2. 5.11.2. Solution
      3. 5.11.3. Discussion
    12. 5.12. Precomputing Keystream in OFB, CTR, CCM, or CWC Modes (or with Stream Ciphers)
      1. 5.12.1. Problem
      2. 5.12.2. Solution
      3. 5.12.3. Discussion
    13. 5.13. Parallelizing Encryption and Decryption in Modes That Allow It (Without Breaking Compatibility)
      1. 5.13.1. Problem
      2. 5.13.2. Solution
      3. 5.13.3. Discussion
      4. 5.13.4. See Also
    14. 5.14. Parallelizing Encryption and Decryption in Arbitrary Modes (Breaking Compatibility)
      1. 5.14.1. Problem
      2. 5.14.2. Solution
      3. 5.14.3. Discussion
      4. 5.14.4. See Also
    15. 5.15. Performing File or Disk Encryption
      1. 5.15.1. Problem
      2. 5.15.2. Solution
      3. 5.15.3. Discussion
      4. 5.15.4. See Also
    16. 5.16. Using a High-Level, Error-Resistant Encryption and Decryption API
      1. 5.16.1. Problem
      2. 5.16.2. Solution
      3. 5.16.3. Discussion
      4. 5.16.4. See Also
    17. 5.17. Performing Block Cipher Setup (for CBC, CFB, OFB, and ECB Modes) in OpenSSL
      1. 5.17.1. Problem
      2. 5.17.2. Solution
      3. 5.17.3. Discussion
      4. 5.17.4. See Also
    18. 5.18. Using Variable Key-Length Ciphers in OpenSSL
      1. 5.18.1. Problem
      2. 5.18.2. Solution
      3. 5.18.3. Discussion
    19. 5.19. Disabling Cipher Padding in OpenSSL in CBC Mode
      1. 5.19.1. Problem
      2. 5.19.2. Solution
      3. 5.19.3. Discussion
    20. 5.20. Performing Additional Cipher Setup in OpenSSL
      1. 5.20.1. Problem
      2. 5.20.2. Solution
      3. 5.20.3. Discussion
    21. 5.21. Querying Cipher Configuration Properties in OpenSSL
      1. 5.21.1. Problem
      2. 5.21.2. Solution
      3. 5.21.3. Discussion
    22. 5.22. Performing Low-Level Encryption and Decryption with OpenSSL
      1. 5.22.1. Problem
      2. 5.22.2. Solution
      3. 5.22.3. Discussion
      4. 5.22.4. See Also
    23. 5.23. Setting Up and Using RC4
      1. 5.23.1. Problem
      2. 5.23.2. Solution
      3. 5.23.3. Discussion
    24. 5.24. Using One-Time Pads
      1. 5.24.1. Problem
      2. 5.24.2. Solution
      3. 5.24.3. Discussion
      4. 5.24.4. See Also
    25. 5.25. Using Symmetric Encryption with Microsoft’s CryptoAPI
      1. 5.25.1. Problem
      2. 5.25.2. Solution
      3. 5.25.3. Discussion
      4. 5.25.4. See Also
    26. 5.26. Creating a CryptoAPI Key Object from Raw Key Data
      1. 5.26.1. Problem
      2. 5.26.2. Solution
      3. 5.26.3. Discussion
      4. 5.26.4. See Also
    27. 5.27. Extracting Raw Key Data from a CryptoAPI Key Object
      1. 5.27.1. Problem
      2. 5.27.2. Solution
      3. 5.27.3. Discussion
      4. 5.27.4. See Also
  10. 6. Hashes and Message Authentication
    1. 6.1. Understanding the Basics of Hashes and MACs
      1. 6.1.1. Problem
      2. 6.1.2. Solution
      3. 6.1.3. Discussion
        1. 6.1.3.1. Types of primitives
        2. 6.1.3.2. Attacks against one-way constructs
      4. 6.1.4. See Also
    2. 6.2. Deciding Whether to Support Multiple Message Digests or MACs
      1. 6.2.1. Problem
      2. 6.2.2. Solution
      3. 6.2.3. Discussion
      4. 6.2.4. See Also
    3. 6.3. Choosing a Cryptographic Hash Algorithm
      1. 6.3.1. Problem
      2. 6.3.2. Solution
      3. 6.3.3. Discussion
      4. 6.3.4. See Also
    4. 6.4. Choosing a Message Authentication Code
      1. 6.4.1. Problem
      2. 6.4.2. Solution
      3. 6.4.3. Discussion
      4. 6.4.4. See Also
    5. 6.5. Incrementally Hashing Data
      1. 6.5.1. Problem
      2. 6.5.2. Solution
      3. 6.5.3. Discussion
      4. 6.5.4. See Also
    6. 6.6. Hashing a Single String
      1. 6.6.1. Problem
      2. 6.6.2. Solution
      3. 6.6.3. Discussion
      4. 6.6.4. See Also
    7. 6.7. Using a Cryptographic Hash
      1. 6.7.1. Problem
      2. 6.7.2. Solution
      3. 6.7.3. Discussion
      4. 6.7.4. See Also
    8. 6.8. Using a Nonce to Protect Against Birthday Attacks
      1. 6.8.1. Problem
      2. 6.8.2. Solution
      3. 6.8.3. Discussion
      4. 6.8.4. See Also
    9. 6.9. Checking Message Integrity
      1. 6.9.1. Problem
      2. 6.9.2. Solution
      3. 6.9.3. Discussion
      4. 6.9.4. See Also
    10. 6.10. Using HMAC
      1. 6.10.1. Problem
      2. 6.10.2. Solution
      3. 6.10.3. Discussion
      4. 6.10.4. See Also
    11. 6.11. Using OMAC (a Simple Block Cipher-Based MAC)
      1. 6.11.1. Problem
      2. 6.11.2. Solution
      3. 6.11.3. Discussion
      4. 6.11.4. See Also
    12. 6.12. Using HMAC or OMAC with a Nonce
      1. 6.12.1. Problem
      2. 6.12.2. Solution
      3. 6.12.3. Discussion
      4. 6.12.4. See Also
    13. 6.13. Using a MAC That’s Reasonably Fast in Software and Hardware
      1. 6.13.1. Problem
      2. 6.13.2. Solution
      3. 6.13.3. Discussion
      4. 6.13.4. See Also
    14. 6.14. Using a MAC That’s Optimized for Software Speed
      1. 6.14.1. Problem
      2. 6.14.2. Solution
      3. 6.14.3. Discussion
      4. 6.14.4. See Also
    15. 6.15. Constructing a Hash Function from a Block Cipher
      1. 6.15.1. Problem
      2. 6.15.2. Solution
      3. 6.15.3. Discussion
      4. 6.15.4. See Also
    16. 6.16. Using a Block Cipher to Build a Full-Strength Hash Function
      1. 6.16.1. Problem
      2. 6.16.2. Solution
      3. 6.16.3. Discussion
    17. 6.17. Using Smaller MAC Tags
      1. 6.17.1. Problem
      2. 6.17.2. Solution
      3. 6.17.3. Discussion
    18. 6.18. Making Encryption and Message Integrity Work Together
      1. 6.18.1. Problem
      2. 6.18.2. Solution
      3. 6.18.3. Discussion
      4. 6.18.4. See Also
    19. 6.19. Making Your Own MAC
      1. 6.19.1. Problem
      2. 6.19.2. Solution
      3. 6.19.3. Discussion
      4. 6.19.4. See Also
    20. 6.20. Encrypting with a Hash Function
      1. 6.20.1. Problem
      2. 6.20.2. Solution
      3. 6.20.3. Discussion
      4. 6.20.4. See Also
    21. 6.21. Securely Authenticating a MAC (Thwarting Capture Replay Attacks)
      1. 6.21.1. Problem
      2. 6.21.2. Solution
      3. 6.21.3. Discussion
      4. 6.21.4. See Also
    22. 6.22. Parallelizing MACs
      1. 6.22.1. Problem
      2. 6.22.2. Solution
      3. 6.22.3. Discussion
      4. 6.22.4. See Also
  11. 7. Public Key Cryptography
    1. 7.1. Determining When to Use Public Key Cryptography
      1. 7.1.1. Problem
      2. 7.1.2. Solution
      3. 7.1.3. Discussion
      4. 7.1.4. See Also
    2. 7.2. Selecting a Public Key Algorithm
      1. 7.2.1. Problem
      2. 7.2.2. Solution
      3. 7.2.3. Discussion
      4. 7.2.4. See Also
    3. 7.3. Selecting Public Key Sizes
      1. 7.3.1. Problem
      2. 7.3.2. Solution
      3. 7.3.3. Discussion
    4. 7.4. Manipulating Big Numbers
      1. 7.4.1. Problem
      2. 7.4.2. Solution
      3. 7.4.3. Discussion
        1. 7.4.3.1. Initialization and cleanup
        2. 7.4.3.2. Assigning to BIGNUM objects
        3. 7.4.3.3. Getting BIGNUM objects with random values
        4. 7.4.3.4. Outputting BIGNUM objects
        5. 7.4.3.5. Common tests on BIGNUM objects
        6. 7.4.3.6. Math operations on BIGNUM objects
      4. 7.4.4. See Also
    5. 7.5. Generating a Prime Number (Testing for Primality)
      1. 7.5.1. Problem
      2. 7.5.2. Solution
      3. 7.5.3. Discussion
      4. 7.5.4. See Also
    6. 7.6. Generating an RSA Key Pair
      1. 7.6.1. Problem
      2. 7.6.2. Solution
      3. 7.6.3. Discussion
      4. 7.6.4. See Also
    7. 7.7. Disentangling the Public and Private Keys in OpenSSL
      1. 7.7.1. Problem
      2. 7.7.2. Solution
      3. 7.7.3. Discussion
    8. 7.8. Converting Binary Strings to Integers for Use with RSA
      1. 7.8.1. Problem
      2. 7.8.2. Solution
      3. 7.8.3. Discussion
      4. 7.8.4. See Also
    9. 7.9. Converting Integers into Binary Strings for Use with RSA
      1. 7.9.1. Problem
      2. 7.9.2. Solution
      3. 7.9.3. Discussion
    10. 7.10. Performing Raw Encryption with an RSA Public Key
      1. 7.10.1. Problem
      2. 7.10.2. Solution
      3. 7.10.3. Discussion
      4. 7.10.4. See Also
    11. 7.11. Performing Raw Decryption Using an RSA Private Key
      1. 7.11.1. Problem
      2. 7.11.2. Solution
      3. 7.11.3. Discussion
      4. 7.11.4. See Also
    12. 7.12. Signing Data Using an RSA Private Key
      1. 7.12.1. Problem
      2. 7.12.2. Solution
      3. 7.12.3. Discussion
    13. 7.13. Verifying Signed Data Using an RSA Public Key
      1. 7.13.1. Problem
      2. 7.13.2. Solution
      3. 7.13.3. Discussion
      4. 7.13.4. See Also
    14. 7.14. Securely Signing and Encrypting with RSA
      1. 7.14.1. Problem
      2. 7.14.2. Solution
      3. 7.14.3. Discussion
      4. 7.14.4. See Also
    15. 7.15. Using the Digital Signature Algorithm (DSA)
      1. 7.15.1. Problem
      2. 7.15.2. Solution
      3. 7.15.3. Discussion
      4. 7.15.4. See Also
    16. 7.16. Representing Public Keys and Certificates in Binary (DER Encoding)
      1. 7.16.1. Problem
      2. 7.16.2. Solution
      3. 7.16.3. Discussion
      4. 7.16.4. See Also
    17. 7.17. Representing Keys and Certificates in Plaintext (PEM Encoding)
      1. 7.17.1. Problem
      2. 7.17.2. Solution
      3. 7.17.3. Discussion
      4. 7.17.4. See Also
  12. 8. Authentication and Key Exchange
    1. 8.1. Choosing an Authentication Method
      1. 8.1.1. Problem
      2. 8.1.2. Solution
      3. 8.1.3. Discussion
        1. 8.1.3.1. Traditional UNIX crypt( )
        2. 8.1.3.2. MD5 Modular Crypt Format (a.k.a. md5crypt or MD5-MCF)
        3. 8.1.3.3. PBKDF2
        4. 8.1.3.4. S/KEY and OPIE
        5. 8.1.3.5. CRAM
        6. 8.1.3.6. Digest-Auth (RFC 2617)
        7. 8.1.3.7. SRP
        8. 8.1.3.8. Basic public key exchange
        9. 8.1.3.9. SAX
        10. 8.1.3.10. PAX
        11. 8.1.3.11. Kerberos
        12. 8.1.3.12. Windows NT LAN Manager (NTLM)
        13. 8.1.3.13. SSL certificate-based checking
      4. 8.1.4. See Also
    2. 8.2. Getting User and Group Information on Unix
      1. 8.2.1. Problem
      2. 8.2.2. Solution
      3. 8.2.3. Discussion
    3. 8.3. Getting User and Group Information on Windows
      1. 8.3.1. Problem
      2. 8.3.2. Solution
      3. 8.3.3. Discussion
    4. 8.4. Restricting Access Based on Hostname or IP Address
      1. 8.4.1. Problem
      2. 8.4.2. Solution
      3. 8.4.3. Discussion
    5. 8.5. Generating Random Passwords and Passphrases
      1. 8.5.1. Problem
      2. 8.5.2. Solution
      3. 8.5.3. Discussion
      4. 8.5.4. See Also
    6. 8.6. Testing the Strength of Passwords
      1. 8.6.1. Problem
      2. 8.6.2. Solution
      3. 8.6.3. Discussion
      4. 8.6.4. See Also
    7. 8.7. Prompting for a Password
      1. 8.7.1. Problem
      2. 8.7.2. Solution
      3. 8.7.3. Discussion
        1. 8.7.3.1. Prompting for a password on Unix using getpass( ) or readpassphrase( )
        2. 8.7.3.2. Prompting for a password on Unix without getpass( ) or readpassphrase( )
        3. 8.7.3.3. Prompting for a password on Windows
    8. 8.8. Throttling Failed Authentication Attempts
      1. 8.8.1. Problem
      2. 8.8.2. Solution
      3. 8.8.3. Discussion
    9. 8.9. Performing Password-Based Authentication with crypt( )
      1. 8.9.1. Problem
      2. 8.9.2. Solution
      3. 8.9.3. Discussion
      4. 8.9.4. See Also
    10. 8.10. Performing Password-Based Authentication with MD5-MCF
      1. 8.10.1. Problem
      2. 8.10.2. Solution
      3. 8.10.3. Discussion
      4. 8.10.4. See Also
    11. 8.11. Performing Password-Based Authentication with PBKDF2
      1. 8.11.1. Problem
      2. 8.11.2. Solution
      3. 8.11.3. Discussion
      4. 8.11.4. See Also
    12. 8.12. Authenticating with PAM
      1. 8.12.1. Problem
      2. 8.12.2. Solution
      3. 8.12.3. Discussion
      4. 8.12.4. See Also
    13. 8.13. Authenticating with Kerberos
      1. 8.13.1. Problem
      2. 8.13.2. Solution
      3. 8.13.3. Discussion
      4. 8.13.4. See Also
    14. 8.14. Authenticating with HTTP Cookies
      1. 8.14.1. Problem
      2. 8.14.2. Solution
      3. 8.14.3. Discussion
      4. 8.14.4. See Also
    15. 8.15. Performing Password-Based Authentication and Key Exchange
      1. 8.15.1. Problem
      2. 8.15.2. Solution
      3. 8.15.3. Discussion
        1. 8.15.3.1. The server
        2. 8.15.3.2. The client
      4. 8.15.4. See Also
    16. 8.16. Performing Authenticated Key Exchange Using RSA
      1. 8.16.1. Problem
      2. 8.16.2. Solution
      3. 8.16.3. Discussion
      4. 8.16.4. See Also
    17. 8.17. Using Basic Diffie-Hellman Key Agreement
      1. 8.17.1. Problem
      2. 8.17.2. Solution
      3. 8.17.3. Discussion
      4. 8.17.4. See Also
    18. 8.18. Using Diffie-Hellman and DSA Together
      1. 8.18.1. Problem
      2. 8.18.2. Solution
      3. 8.18.3. Discussion
      4. 8.18.4. See Also
    19. 8.19. Minimizing the Window of Vulnerability When Authenticating Without a PKI
      1. 8.19.1. Problem
      2. 8.19.2. Solution
      3. 8.19.3. Discussion
      4. 8.19.4. See Also
    20. 8.20. Providing Forward Secrecy in a Symmetric System
      1. 8.20.1. Problem
      2. 8.20.2. Solution
      3. 8.20.3. Discussion
      4. 8.20.4. See Also
    21. 8.21. Ensuring Forward Secrecy in a Public Key System
      1. 8.21.1. Problem
      2. 8.21.2. Solution
      3. 8.21.3. Discussion
      4. 8.21.4. See Also
    22. 8.22. Confirming Requests via Email
      1. 8.22.1. Problem
      2. 8.22.2. Solution
      3. 8.22.3. Discussion
      4. 8.22.4. See Also
  13. 9. Networking
    1. 9.1. Creating an SSL Client
      1. 9.1.1. Problem
      2. 9.1.2. Solution
      3. 9.1.3. Discussion
      4. 9.1.4. See Also
    2. 9.2. Creating an SSL Server
      1. 9.2.1. Problem
      2. 9.2.2. Solution
      3. 9.2.3. Discussion
      4. 9.2.4. See Also
    3. 9.3. Using Session Caching to Make SSL Servers More Efficient
      1. 9.3.1. Problem
      2. 9.3.2. Solution
      3. 9.3.3. Discussion
      4. 9.3.4. See Also
    4. 9.4. Securing Web Communication on Windows Using the WinInet API
      1. 9.4.1. Problem
      2. 9.4.2. Solution
      3. 9.4.3. Discussion
      4. 9.4.4. See Also
    5. 9.5. Enabling SSL without Modifying Source Code
      1. 9.5.1. Problem
      2. 9.5.2. Solution
      3. 9.5.3. Discussion
      4. 9.5.4. See Also
    6. 9.6. Using Kerberos Encryption
      1. 9.6.1. Problem
      2. 9.6.2. Solution
      3. 9.6.3. Discussion
      4. 9.6.4. See Also
    7. 9.7. Performing Interprocess Communication Using Sockets
      1. 9.7.1. Problem
      2. 9.7.2. Solution
      3. 9.7.3. Discussion
      4. 9.7.4. See Also
    8. 9.8. Performing Authentication with Unix Domain Sockets
      1. 9.8.1. Problem
      2. 9.8.2. Solution
      3. 9.8.3. Discussion
    9. 9.9. Performing Session ID Management
      1. 9.9.1. Problem
      2. 9.9.2. Solution
      3. 9.9.3. Discussion
      4. 9.9.4. See Also
    10. 9.10. Securing Database Connections
      1. 9.10.1. Problem
      2. 9.10.2. Solution
      3. 9.10.3. Discussion
        1. 9.10.3.1. MySQL
        2. 9.10.3.2. PostgreSQL
      4. 9.10.4. See Also
    11. 9.11. Using a Virtual Private Network to Secure Network Connections
      1. 9.11.1. Problem
      2. 9.11.2. Solution
      3. 9.11.3. Discussion
    12. 9.12. Building an Authenticated Secure Channel Without SSL
      1. 9.12.1. Problem
      2. 9.12.2. Solution
      3. 9.12.3. Discussion
      4. 9.12.4. See Also
  14. 10. Public Key Infrastructure
    1. 10.1. Understanding Public Key Infrastructure (PKI)
      1. 10.1.1. Problem
      2. 10.1.2. Solution
      3. 10.1.3. Discussion
        1. 10.1.3.1. Certificates
        2. 10.1.3.2. Certification authorities
        3. 10.1.3.3. Certificate revocation
        4. 10.1.3.4. Online Certificate Status Protocol
        5. 10.1.3.5. Certificate hierarchies
        6. 10.1.3.6. X.509 certificates
      4. 10.1.4. See Also
    2. 10.2. Obtaining a Certificate
      1. 10.2.1. Problem
      2. 10.2.2. Solution
      3. 10.2.3. Discussion
        1. 10.2.3.1. Personal certificates
        2. 10.2.3.2. Code-signing certificates
        3. 10.2.3.3. Web site certificates
      4. 10.2.4. See Also
    3. 10.3. Using Root Certificates
      1. 10.3.1. Problem
      2. 10.3.2. Solution
      3. 10.3.3. Discussion
      4. 10.3.4. See Also
    4. 10.4. Understanding X.509 Certificate Verification Methodology
      1. 10.4.1. Problem
      2. 10.4.2. Solution
      3. 10.4.3. Discussion
      4. 10.4.4. See Also
    5. 10.5. Performing X.509 Certificate Verification with OpenSSL
      1. 10.5.1. Problem
      2. 10.5.2. Solution
      3. 10.5.3. Discussion
      4. 10.5.4. See Also
    6. 10.6. Performing X.509 Certificate Verification with CryptoAPI
      1. 10.6.1. Problem
      2. 10.6.2. Solution
      3. 10.6.3. Discussion
        1. 10.6.3.1. CryptoAPI certificate stores
      4. 10.6.4. See Also
    7. 10.7. Verifying an SSL Peer’s Certificate
      1. 10.7.1. Problem
      2. 10.7.2. Solution
      3. 10.7.3. Discussion
      4. 10.7.4. See Also
    8. 10.8. Adding Hostname Checking to Certificate Verification
      1. 10.8.1. Problem
      2. 10.8.2. Solution
      3. 10.8.3. Discussion
      4. 10.8.4. See Also
    9. 10.9. Using a Whitelist to Verify Certificates
      1. 10.9.1. Problem
      2. 10.9.2. Solution
      3. 10.9.3. Discussion
    10. 10.10. Obtaining Certificate Revocation Lists with OpenSSL
      1. 10.10.1. Problem
      2. 10.10.2. Solution
      3. 10.10.3. Discussion
      4. 10.10.4. See Also
    11. 10.11. Obtaining CRLs with CryptoAPI
      1. 10.11.1. Problem
      2. 10.11.2. Solution
      3. 10.11.3. Discussion
      4. 10.11.4. See Also
    12. 10.12. Checking Revocation Status via OCSP with OpenSSL
      1. 10.12.1. Problem
      2. 10.12.2. Solution
      3. 10.12.3. Discussion
      4. 10.12.4. See Also
  15. 11. Random Numbers
    1. 11.1. Determining What Kind of Random Numbers to Use
      1. 11.1.1. Problem
      2. 11.1.2. Solution
      3. 11.1.3. Discussion
      4. 11.1.4. See Also
    2. 11.2. Using a Generic API for Randomness and Entropy
      1. 11.2.1. Problem
      2. 11.2.2. Solution
      3. 11.2.3. Discussion
      4. 11.2.4. See Also
    3. 11.3. Using the Standard Unix Randomness Infrastructure
      1. 11.3.1. Problem
      2. 11.3.2. Solution
      3. 11.3.3. Discussion
      4. 11.3.4. See Also
    4. 11.4. Using the Standard Windows Randomness Infrastructure
      1. 11.4.1. Problem
      2. 11.4.2. Solution
      3. 11.4.3. Discussion
      4. 11.4.4. See Also
    5. 11.5. Using an Application-Level Generator
      1. 11.5.1. Problem
      2. 11.5.2. Solution
      3. 11.5.3. Discussion
        1. 11.5.3.1. Using generators based on block ciphers
        2. 11.5.3.2. Using a stream cipher as a generator
        3. 11.5.3.3. Using a generator based on a cryptographic hash function
      4. 11.5.4. See Also
    6. 11.6. Reseeding a Pseudo-Random Number Generator
      1. 11.6.1. Problem
      2. 11.6.2. Solution
      3. 11.6.3. Discussion
      4. 11.6.4. See Also
    7. 11.7. Using an Entropy Gathering Daemon-Compatible Solution
      1. 11.7.1. Problem
      2. 11.7.2. Solution
      3. 11.7.3. Discussion
      4. 11.7.4. See Also
    8. 11.8. Getting Entropy or Pseudo-Randomness Using EGADS
      1. 11.8.1. Problem
      2. 11.8.2. Solution
      3. 11.8.3. Discussion
      4. 11.8.4. See Also
    9. 11.9. Using the OpenSSL Random Number API
      1. 11.9.1. Problem
      2. 11.9.2. Solution
      3. 11.9.3. Discussion
      4. 11.9.4. See Also
    10. 11.10. Getting Random Integers
      1. 11.10.1. Problem
      2. 11.10.2. Solution
      3. 11.10.3. Discussion
      4. 11.10.4. See Also
    11. 11.11. Getting a Random Integer in a Range
      1. 11.11.1. Problem
      2. 11.11.2. Solution
      3. 11.11.3. Discussion
      4. 11.11.4. See Also
    12. 11.12. Getting a Random Floating-Point Value with Uniform Distribution
      1. 11.12.1. Problem
      2. 11.12.2. Solution
      3. 11.12.3. Discussion
    13. 11.13. Getting Floating-Point Values with Nonuniform Distributions
      1. 11.13.1. Problem
      2. 11.13.2. Solution
      3. 11.13.3. Discussion
      4. 11.13.4. See Also
    14. 11.14. Getting a Random Printable ASCII String
      1. 11.14.1. Problem
      2. 11.14.2. Solution
      3. 11.14.3. Discussion
    15. 11.15. Shuffling Fairly
      1. 11.15.1. Problem
      2. 11.15.2. Solution
      3. 11.15.3. Discussion
      4. 11.15.4. See Also
    16. 11.16. Compressing Data with Entropy into a Fixed-Size Seed
      1. 11.16.1. Problem
      2. 11.16.2. Solution
      3. 11.16.3. Discussion
      4. 11.16.4. See Also
    17. 11.17. Getting Entropy at Startup
      1. 11.17.1. Problem
      2. 11.17.2. Solution
      3. 11.17.3. Discussion
      4. 11.17.4. See Also
    18. 11.18. Statistically Testing Random Numbers
      1. 11.18.1. Problem
      2. 11.18.2. Solution
      3. 11.18.3. Discussion
        1. 11.18.3.1. FIPS 140-1 power-up and on-demand tests
        2. 11.18.3.2. The FIPS continuous output test
      4. 11.18.4. See Also
    19. 11.19. Performing Entropy Estimation and Management
      1. 11.19.1. Problem
      2. 11.19.2. Solution
      3. 11.19.3. Discussion
        1. 11.19.3.1. Entropy in timestamps
        2. 11.19.3.2. Entropy in a key press
        3. 11.19.3.3. Entropy in mouse movements
        4. 11.19.3.4. Entropy in disk access
        5. 11.19.3.5. Entropy in data from the network
        6. 11.19.3.6. Entropy in the sound device
        7. 11.19.3.7. Entropy from thread timing and other system state
      4. 11.19.4. See Also
    20. 11.20. Gathering Entropy from the Keyboard
      1. 11.20.1. Problem
      2. 11.20.2. Solution
      3. 11.20.3. Discussion
        1. 11.20.3.1. Collecting entropy from the keyboard on Unix
        2. 11.20.3.2. Collecting entropy from the keyboard on Windows
      4. 11.20.4. See Also
    21. 11.21. Gathering Entropy from Mouse Events on Windows
      1. 11.21.1. Problem
      2. 11.21.2. Solution
      3. 11.21.3. Discussion
      4. 11.21.4. See Also
    22. 11.22. Gathering Entropy from Thread Timings
      1. 11.22.1. Problem
      2. 11.22.2. Solution
      3. 11.22.3. See Also
    23. 11.23. Gathering Entropy from System State
      1. 11.23.1. Problem
      2. 11.23.2. Solution
      3. 11.23.3. Discussion
      4. 11.23.4. See Also
  16. 12. Anti-Tampering
    1. 12.1. Understanding the Problem of Software Protection
      1. 12.1.1. Problem
      2. 12.1.2. Solution
      3. 12.1.3. Discussion
        1. 12.1.3.1. The threat of protection crackers
        2. 12.1.3.2. The goal of software protection
        3. 12.1.3.3. The cost of software protection
        4. 12.1.3.4. Anti-tampering techniques
      4. 12.1.4. See Also
    2. 12.2. Detecting Modification
      1. 12.2.1. Problem
      2. 12.2.2. Solution
      3. 12.2.3. Discussion
      4. 12.2.4. See Also
    3. 12.3. Obfuscating Code
      1. 12.3.1. Problem
      2. 12.3.2. Solution
      3. 12.3.3. Discussion
      4. 12.3.4. See Also
    4. 12.4. Performing Bit and Byte Obfuscation
      1. 12.4.1. Problem
      2. 12.4.2. Solution
      3. 12.4.3. Discussion
      4. 12.4.4. See Also
    5. 12.5. Performing Constant Transforms on Variables
      1. 12.5.1. Problem
      2. 12.5.2. Solution
      3. 12.5.3. Discussion
    6. 12.6. Merging Scalar Variables
      1. 12.6.1. Problem
      2. 12.6.2. Solution
      3. 12.6.3. Discussion
    7. 12.7. Splitting Variables
      1. 12.7.1. Problem
      2. 12.7.2. Solution
      3. 12.7.3. Discussion
    8. 12.8. Disguising Boolean Values
      1. 12.8.1. Problem
      2. 12.8.2. Solution
      3. 12.8.3. Discussion
      4. 12.8.4. See Also
    9. 12.9. Using Function Pointers
      1. 12.9.1. Problem
      2. 12.9.2. Solution
      3. 12.9.3. Discussion
    10. 12.10. Restructuring Arrays
      1. 12.10.1. Problem
      2. 12.10.2. Solution
      3. 12.10.3. Discussion
    11. 12.11. Hiding Strings
      1. 12.11.1. Problem
      2. 12.11.2. Solution
      3. 12.11.3. Discussion
    12. 12.12. Detecting Debuggers
      1. 12.12.1. Problem
      2. 12.12.2. Solution
      3. 12.12.3. Discussion
      4. 12.12.4. See Also
    13. 12.13. Detecting Unix Debuggers
      1. 12.13.1. Problem
      2. 12.13.2. Solution
      3. 12.13.3. Discussion
      4. 12.13.4. See Also
    14. 12.14. Detecting Windows Debuggers
      1. 12.14.1. Problem
      2. 12.14.2. Solution
      3. 12.14.3. Discussion
      4. 12.14.4. See Also
    15. 12.15. Detecting SoftICE
      1. 12.15.1. Problem
      2. 12.15.2. Solution
      3. 12.15.3. Discussion
      4. 12.15.4. See Also
    16. 12.16. Countering Disassembly
      1. 12.16.1. Problem
      2. 12.16.2. Solution
      3. 12.16.3. Discussion
    17. 12.17. Using Self-Modifying Code
      1. 12.17.1. Problem
      2. 12.17.2. Solution
      3. 12.17.3. Discussion
      4. 12.17.4. See Also
  17. 13. Other Topics
    1. 13.1. Performing Error Handling
      1. 13.1.1. Problem
      2. 13.1.2. Solution
      3. 13.1.3. Discussion
      4. 13.1.4. See Also
    2. 13.2. Erasing Data from Memory Securely
      1. 13.2.1. Problem
      2. 13.2.2. Solution
      3. 13.2.3. Discussion
    3. 13.3. Preventing Memory from Being Paged to Disk
      1. 13.3.1. Problem
      2. 13.3.2. Solution
      3. 13.3.3. Discussion
    4. 13.4. Using Variable Arguments Properly
      1. 13.4.1. Problem
      2. 13.4.2. Solution
      3. 13.4.3. Discussion
      4. 13.4.4. See Also
    5. 13.5. Performing Proper Signal Handling
      1. 13.5.1. Problem
      2. 13.5.2. Solution
      3. 13.5.3. Discussion
      4. 13.5.4. See Also
    6. 13.6. Protecting against Shatter Attacks on Windows
      1. 13.6.1. Problem
      2. 13.6.2. Solution
      3. 13.6.3. Discussion
      4. 13.6.4. See Also
    7. 13.7. Guarding Against Spawning Too Many Threads
      1. 13.7.1. Problem
      2. 13.7.2. Solution
      3. 13.7.3. Discussion
    8. 13.8. Guarding Against Creating Too Many Network Sockets
      1. 13.8.1. Problem
      2. 13.8.2. Solution
      3. 13.8.3. Discussion
    9. 13.9. Guarding Against Resource Starvation Attacks on Unix
      1. 13.9.1. Problem
      2. 13.9.2. Solution
      3. 13.9.3. Discussion
      4. 13.9.4. See Also
    10. 13.10. Guarding Against Resource Starvation Attacks on Windows
      1. 13.10.1. Problem
      2. 13.10.2. Solution
      3. 13.10.3. Discussion
    11. 13.11. Following Best Practices for Audit Logging
      1. 13.11.1. Problem
      2. 13.11.2. Solution
      3. 13.11.3. Discussion
        1. 13.11.3.1. Network logging
        2. 13.11.3.2. Logging to CD-R
        3. 13.11.3.3. Signing and encrypting log entries
      4. 13.11.4. See Also
  18. Index
  19. About the Authors
  20. Colophon
  21. Copyright