blob: 5d0b5ba37c120f19233ade5e3ec96bb794fa6982 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Rocha7f9aea22011-09-12 14:31:24 -070016
Alex Deymo14c0da82016-07-20 16:45:45 -070017#ifndef UPDATE_ENGINE_CERTIFICATE_CHECKER_H_
18#define UPDATE_ENGINE_CERTIFICATE_CHECKER_H_
Bruno Rocha7f9aea22011-09-12 14:31:24 -070019
Ben Chan05735a12014-09-03 07:48:22 -070020#include <curl/curl.h>
21#include <openssl/ssl.h>
22
Bruno Rocha7f9aea22011-09-12 14:31:24 -070023#include <string>
24
Ben Chan05735a12014-09-03 07:48:22 -070025#include <base/macros.h>
Bruno Rocha7f9aea22011-09-12 14:31:24 -070026#include <gtest/gtest_prod.h> // for FRIEND_TEST
Bruno Rocha7f9aea22011-09-12 14:31:24 -070027
Bruno Rocha7f9aea22011-09-12 14:31:24 -070028namespace chromeos_update_engine {
29
Alex Deymoc1c17b42015-11-23 03:53:15 -030030class PrefsInterface;
31
Bruno Rocha7f9aea22011-09-12 14:31:24 -070032// Wrapper for openssl operations with the certificates.
33class OpenSSLWrapper {
34 public:
Alex Deymoc1c17b42015-11-23 03:53:15 -030035 OpenSSLWrapper() = default;
36 virtual ~OpenSSLWrapper() = default;
Bruno Rocha7f9aea22011-09-12 14:31:24 -070037
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 Vakulenkof68bbbc2015-02-09 12:53:18 -080051 uint8_t* out_digest) const;
Bruno Rocha7f9aea22011-09-12 14:31:24 -070052
53 private:
54 DISALLOW_COPY_AND_ASSIGN(OpenSSLWrapper);
55};
56
Alex Deymoc1c17b42015-11-23 03:53:15 -030057// 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.
59enum 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.
78enum class ServerToCheck {
79 kUpdate = 0,
80 kDownload,
Alex Deymo33e91e72015-12-01 18:26:08 -030081
82 // Ignore this server.
83 kNone,
Alex Deymoc1c17b42015-11-23 03:53:15 -030084};
85
Bruno Rocha7f9aea22011-09-12 14:31:24 -070086// 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.
90class CertificateChecker {
91 public:
Alex Deymoc1c17b42015-11-23 03:53:15 -030092 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 Rocha7f9aea22011-09-12 14:31:24 -0700100 };
101
Alex Deymo33e91e72015-12-01 18:26:08 -0300102 CertificateChecker(PrefsInterface* prefs, OpenSSLWrapper* openssl_wrapper);
103 ~CertificateChecker();
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700104
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 Deymoc1c17b42015-11-23 03:53:15 -0300107 // 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 Rocha7f9aea22011-09-12 14:31:24 -0700112
Alex Deymo33e91e72015-12-01 18:26:08 -0300113 // 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 Deymoc1c17b42015-11-23 03:53:15 -0300118 // 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 Rocha7f9aea22011-09-12 14:31:24 -0700122
123 private:
124 FRIEND_TEST(CertificateCheckerTest, NewCertificate);
125 FRIEND_TEST(CertificateCheckerTest, SameCertificate);
126 FRIEND_TEST(CertificateCheckerTest, ChangedCertificate);
127 FRIEND_TEST(CertificateCheckerTest, FailedCertificate);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700128
Alex Deymo33e91e72015-12-01 18:26:08 -0300129 // 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 Deymoc1c17b42015-11-23 03:53:15 -0300136 // each certificate on the server and returns 1 for success and 0 for failure.
Alex Deymo33e91e72015-12-01 18:26:08 -0300137 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 Rocha7f9aea22011-09-12 14:31:24 -0700144
Alex Deymoc1c17b42015-11-23 03:53:15 -0300145 // Checks if server certificate stored in |x509_ctx| has changed since last
Alex Deymo33e91e72015-12-01 18:26:08 -0300146 // connection to that same server, specified by |server_to_check|.
Alex Deymoc1c17b42015-11-23 03:53:15 -0300147 // 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 Deymo33e91e72015-12-01 18:26:08 -0300150 bool CheckCertificateChange(int preverify_ok,
151 X509_STORE_CTX* x509_ctx,
152 ServerToCheck server_to_check);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700153
Alex Deymoc1c17b42015-11-23 03:53:15 -0300154 // Notifies the observer, if any, of a certificate checking.
Alex Deymo33e91e72015-12-01 18:26:08 -0300155 void NotifyCertificateChecked(ServerToCheck server_to_check,
156 CertificateCheckResult result);
157
158 // The CertificateChecker singleton instance.
159 static CertificateChecker* cert_checker_singleton_;
Alex Deymoc1c17b42015-11-23 03:53:15 -0300160
161 // Prefs instance used to store the certificates seen in the past.
162 PrefsInterface* prefs_;
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700163
164 // The wrapper for openssl operations.
Alex Deymoc1c17b42015-11-23 03:53:15 -0300165 OpenSSLWrapper* openssl_wrapper_;
166
Alex Deymoc1c17b42015-11-23 03:53:15 -0300167 // The observer called whenever a certificate is checked, if not null.
168 Observer* observer_{nullptr};
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700169
170 DISALLOW_COPY_AND_ASSIGN(CertificateChecker);
171};
172
173} // namespace chromeos_update_engine
174
Alex Deymo14c0da82016-07-20 16:45:45 -0700175#endif // UPDATE_ENGINE_CERTIFICATE_CHECKER_H_