ssl_connection_status_flags.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_SSL_SSL_CONNECTION_STATUS_FLAGS_H_
  5. #define NET_SSL_SSL_CONNECTION_STATUS_FLAGS_H_
  6. #include <stdint.h>
  7. #include "base/check_op.h"
  8. namespace net {
  9. // Status flags for SSLInfo::connection_status.
  10. enum {
  11. // The lower 16 bits are reserved for the TLS ciphersuite id.
  12. SSL_CONNECTION_CIPHERSUITE_MASK = 0xffff,
  13. // The next two bits are reserved for the compression used.
  14. SSL_CONNECTION_COMPRESSION_SHIFT = 16,
  15. SSL_CONNECTION_COMPRESSION_MASK = 3,
  16. // 1 << 18 was previously used for SSL_CONNECTION_VERSION_FALLBACK.
  17. // 1 << 19 was previously used for SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION.
  18. // The next three bits are reserved for the SSL version.
  19. SSL_CONNECTION_VERSION_SHIFT = 20,
  20. SSL_CONNECTION_VERSION_MASK = 7,
  21. // 1 << 31 (the sign bit) is reserved so that the SSL connection status will
  22. // never be negative.
  23. };
  24. // NOTE: the SSL version enum constants must be between 0 and
  25. // SSL_CONNECTION_VERSION_MASK, inclusive. These values are persisted to disk
  26. // and used in UMA, so they must remain stable.
  27. enum SSLVersion {
  28. SSL_CONNECTION_VERSION_UNKNOWN = 0, // Unknown SSL version.
  29. SSL_CONNECTION_VERSION_SSL2 = 1,
  30. SSL_CONNECTION_VERSION_SSL3 = 2,
  31. SSL_CONNECTION_VERSION_TLS1 = 3,
  32. SSL_CONNECTION_VERSION_TLS1_1 = 4,
  33. SSL_CONNECTION_VERSION_TLS1_2 = 5,
  34. SSL_CONNECTION_VERSION_TLS1_3 = 6,
  35. SSL_CONNECTION_VERSION_QUIC = 7,
  36. SSL_CONNECTION_VERSION_MAX,
  37. };
  38. static_assert(SSL_CONNECTION_VERSION_MAX - 1 <= SSL_CONNECTION_VERSION_MASK,
  39. "SSL_CONNECTION_VERSION_MASK too small");
  40. inline uint16_t SSLConnectionStatusToCipherSuite(int connection_status) {
  41. return static_cast<uint16_t>(connection_status);
  42. }
  43. inline SSLVersion SSLConnectionStatusToVersion(int connection_status) {
  44. return static_cast<SSLVersion>(
  45. (connection_status >> SSL_CONNECTION_VERSION_SHIFT) &
  46. SSL_CONNECTION_VERSION_MASK);
  47. }
  48. inline void SSLConnectionStatusSetCipherSuite(uint16_t cipher_suite,
  49. int* connection_status) {
  50. // Clear out the old ciphersuite.
  51. *connection_status &= ~SSL_CONNECTION_CIPHERSUITE_MASK;
  52. // Set the new ciphersuite.
  53. *connection_status |= cipher_suite;
  54. }
  55. inline void SSLConnectionStatusSetVersion(int version, int* connection_status) {
  56. DCHECK_GT(version, 0);
  57. DCHECK_LT(version, SSL_CONNECTION_VERSION_MAX);
  58. // Clear out the old version.
  59. *connection_status &=
  60. ~(SSL_CONNECTION_VERSION_MASK << SSL_CONNECTION_VERSION_SHIFT);
  61. // Set the new version.
  62. *connection_status |=
  63. ((version & SSL_CONNECTION_VERSION_MASK) << SSL_CONNECTION_VERSION_SHIFT);
  64. }
  65. } // namespace net
  66. #endif // NET_SSL_SSL_CONNECTION_STATUS_FLAGS_H_