Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2011 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_CERTIFICATE_CHECKER_H_ |
| 18 | #define UPDATE_ENGINE_CERTIFICATE_CHECKER_H_ |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 19 | |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 20 | #include <curl/curl.h> |
| 21 | #include <openssl/ssl.h> |
| 22 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 25 | #include <base/macros.h> |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 26 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 27 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 28 | namespace chromeos_update_engine { |
| 29 | |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 30 | class PrefsInterface; |
| 31 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 32 | // Wrapper for openssl operations with the certificates. |
| 33 | class OpenSSLWrapper { |
| 34 | public: |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 35 | OpenSSLWrapper() = default; |
| 36 | virtual ~OpenSSLWrapper() = default; |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 37 | |
| 38 | // Takes an openssl X509_STORE_CTX, extracts the corresponding certificate |
| 39 | // from it and calculates its fingerprint (SHA256 digest). Returns true on |
| 40 | // success and false otherwise. |
| 41 | // |
| 42 | // |x509_ctx| is the pointer to the openssl object that holds the certificate. |
| 43 | // |out_depth| is the depth of the current certificate, in the certificate |
| 44 | // chain. |
| 45 | // |out_digest_length| is the length of the generated digest. |
| 46 | // |out_digest| is the byte array where the digest itself will be written. |
| 47 | // It should be big enough to hold a SHA1 digest (e.g. EVP_MAX_MD_SIZE). |
| 48 | virtual bool GetCertificateDigest(X509_STORE_CTX* x509_ctx, |
| 49 | int* out_depth, |
| 50 | unsigned int* out_digest_length, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 51 | uint8_t* out_digest) const; |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 52 | |
| 53 | private: |
| 54 | DISALLOW_COPY_AND_ASSIGN(OpenSSLWrapper); |
| 55 | }; |
| 56 | |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 57 | // The values in this enum are replicated in the metrics server. See metrics.h |
| 58 | // for instructions on how to update these values in the server side. |
| 59 | enum class CertificateCheckResult { |
| 60 | // The certificate is valid and the same as seen before or the first time we |
| 61 | // see a certificate. |
| 62 | kValid, |
| 63 | // The certificate is valid, but is different than a previously seen |
| 64 | // certificate for the selected server. |
| 65 | kValidChanged, |
| 66 | // The certificate validation failed. |
| 67 | kFailed, |
| 68 | |
| 69 | // This value must be the last entry. |
| 70 | kNumConstants |
| 71 | }; |
| 72 | |
| 73 | // These values are used to generate the keys of files persisted via prefs. |
| 74 | // This means that changing these will cause loss of information on metrics |
| 75 | // reporting, during the transition. These values are also mapped to a metric |
| 76 | // name in metrics.cc, so adding values here requires to assign a new metric |
| 77 | // name in that file. |
| 78 | enum class ServerToCheck { |
| 79 | kUpdate = 0, |
| 80 | kDownload, |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 81 | |
| 82 | // Ignore this server. |
| 83 | kNone, |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 84 | }; |
| 85 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 86 | // Responsible for checking whether update server certificates change, and |
| 87 | // reporting to UMA when this happens. Since all state information is persisted, |
| 88 | // and openssl forces us to use a static callback with no data pointer, this |
| 89 | // class is entirely static. |
| 90 | class CertificateChecker { |
| 91 | public: |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 92 | class Observer { |
| 93 | public: |
| 94 | virtual ~Observer() = default; |
| 95 | |
| 96 | // Called whenever a certificate is checked for the server |server_to_check| |
| 97 | // passing the result of said certificate check. |
| 98 | virtual void CertificateChecked(ServerToCheck server_to_check, |
| 99 | CertificateCheckResult result) = 0; |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 102 | CertificateChecker(PrefsInterface* prefs, OpenSSLWrapper* openssl_wrapper); |
| 103 | ~CertificateChecker(); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 104 | |
| 105 | // This callback is called by libcurl just before the initialization of an |
| 106 | // SSL connection after having processed all other SSL related options. Used |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 107 | // to check if server certificates change. |cert_checker| is expected to be a |
| 108 | // pointer to the CertificateChecker instance. |
| 109 | static CURLcode ProcessSSLContext(CURL* curl_handle, |
| 110 | SSL_CTX* ssl_ctx, |
| 111 | void* cert_checker); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 112 | |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 113 | // Initialize this class as the singleton instance. Only one instance can be |
| 114 | // initialized at a time and it should be initialized before other methods |
| 115 | // can be used. |
| 116 | void Init(); |
| 117 | |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 118 | // Set the certificate observer to the passed instance. To remove the |
| 119 | // observer, pass a nullptr. The |observer| instance must be valid while this |
| 120 | // CertificateChecker verifies certificates. |
| 121 | void SetObserver(Observer* observer) { observer_ = observer; } |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 122 | |
| 123 | private: |
| 124 | FRIEND_TEST(CertificateCheckerTest, NewCertificate); |
| 125 | FRIEND_TEST(CertificateCheckerTest, SameCertificate); |
| 126 | FRIEND_TEST(CertificateCheckerTest, ChangedCertificate); |
| 127 | FRIEND_TEST(CertificateCheckerTest, FailedCertificate); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 128 | |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 129 | // These callbacks are asynchronously called by openssl after initial SSL |
| 130 | // verification. They are used to perform any additional security verification |
| 131 | // on the connection, but we use them here to get hold of the server |
| 132 | // certificate, in order to determine if it has changed since the last |
| 133 | // connection. Since openssl forces us to do this statically, we define two |
| 134 | // different callbacks for the two different official update servers, and only |
| 135 | // assign the correspondent one. The assigned callback is then called once per |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 136 | // each certificate on the server and returns 1 for success and 0 for failure. |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 137 | static int VerifySSLCallbackDownload(int preverify_ok, |
| 138 | X509_STORE_CTX* x509_ctx); |
| 139 | static int VerifySSLCallbackUpdate(int preverify_ok, |
| 140 | X509_STORE_CTX* x509_ctx); |
| 141 | static int VerifySSLCallback(int preverify_ok, |
| 142 | X509_STORE_CTX* x509_ctx, |
| 143 | ServerToCheck server_to_check); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 144 | |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 145 | // Checks if server certificate stored in |x509_ctx| has changed since last |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 146 | // connection to that same server, specified by |server_to_check|. |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 147 | // This is called by the callbacks defined above. The result of the |
| 148 | // certificate check is passed to the observer, if any. Returns true on |
| 149 | // success and false otherwise. |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 150 | bool CheckCertificateChange(int preverify_ok, |
| 151 | X509_STORE_CTX* x509_ctx, |
| 152 | ServerToCheck server_to_check); |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 153 | |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 154 | // Notifies the observer, if any, of a certificate checking. |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 155 | void NotifyCertificateChecked(ServerToCheck server_to_check, |
| 156 | CertificateCheckResult result); |
| 157 | |
| 158 | // The CertificateChecker singleton instance. |
| 159 | static CertificateChecker* cert_checker_singleton_; |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 160 | |
| 161 | // Prefs instance used to store the certificates seen in the past. |
| 162 | PrefsInterface* prefs_; |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 163 | |
| 164 | // The wrapper for openssl operations. |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 165 | OpenSSLWrapper* openssl_wrapper_; |
| 166 | |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 167 | // The observer called whenever a certificate is checked, if not null. |
| 168 | Observer* observer_{nullptr}; |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 169 | |
| 170 | DISALLOW_COPY_AND_ASSIGN(CertificateChecker); |
| 171 | }; |
| 172 | |
| 173 | } // namespace chromeos_update_engine |
| 174 | |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 175 | #endif // UPDATE_ENGINE_CERTIFICATE_CHECKER_H_ |