跳到主要内容

Appendix D. Implementation Notes (实现说明)

本附录为实现 TLS 1.2 提供实践建议和最佳实践.

D.1. Random Number Generation and Seeding (随机数生成和播种)

TLS 的安全性高度依赖 random number generation 的质量. 实现必须使用 cryptographically secure pseudorandom number generator (CSPRNG), 并确保 PRNG 在使用前正确播种, 定期重新播种, 且不把 timestamp 或 process ID 等可预测值作为唯一熵源.

D.2. Certificates and Authentication (证书和认证)

实现必须正确验证 certificate chain:

  1. 验证每个 certificate 的 signature.
  2. 确保 certificate chain 到达可信 root CA.
  3. 检查 certificate validity period.
  4. 验证 certificate 中的 Common Name (CN) 或 Subject Alternative Name (SAN).
  5. 应该检查 certificate revocation status, 并支持 CRL 和/或 OCSP.

server 应支持多个 certificate, 例如 RSA 和 ECDSA, 并根据 client capability 选择合适 certificate.

D.3. Cipher Suites (密码套件)

实现应该默认启用强 cipher suite, 禁用 RC4, DES 和 export-grade cipher 等已知弱点套件, 并按优先级排列 cipher suite list.

推荐优先级通常为:

  1. AEAD cipher suite, 如 GCM mode.
  2. 提供 forward secrecy 的 DHE/ECDHE suite.
  3. AES-256 优先于 AES-128.
  4. SHA-256 或更强 MAC.
  5. 尽量避免 CBC mode.

D.4. Implementation Pitfalls (实现陷阱)

实现者需要特别注意:

  • Timing attack: password comparison 和 padding validation 的时间差可能泄露信息. 使用 constant-time comparison.
  • Padding oracle attack: CBC mode padding validation error 可能被利用. 使用 constant-time padding validation, 并为所有 decryption failure 返回相同错误.
  • Version rollback attack: 攻击者可能试图强制使用较弱 protocol version. 应在 Finished message 中包含 negotiated version, 验证 version field 一致性, 并实现 SCSV protection.
  • Renegotiation attack: 实现 RFC 5746, 并在敏感操作期间禁用 renegotiation.
  • Compression attack: TLS compression 可能泄露 secret information. 应禁用 TLS-level compression.
  • Buffer management: 始终检查 length field, 使用安全内存函数, 并在释放后清零敏感数据.
  • Error handling: 外部返回通用错误, 详细错误仅内部记录, 避免泄露 timing information.

D.5. Performance Optimization (性能优化)

实现可以通过 session caching 或 session ticket 减少完整 handshake 数量, 批量处理 encrypt/decrypt operation, 使用 AES-NI 等硬件加速, 并复用 TLS connection 来改善性能.