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