blob: bc675becfe0e18c4a4331c4e8471b4ca28e61cf9 [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 Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_COMMON_CERTIFICATE_CHECKER_H_
18#define UPDATE_ENGINE_COMMON_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,
81};
82
Bruno Rocha7f9aea22011-09-12 14:31:24 -070083// Responsible for checking whether update server certificates change, and
84// reporting to UMA when this happens. Since all state information is persisted,
85// and openssl forces us to use a static callback with no data pointer, this
86// class is entirely static.
87class CertificateChecker {
88 public:
Alex Deymoc1c17b42015-11-23 03:53:15 -030089 class Observer {
90 public:
91 virtual ~Observer() = default;
92
93 // Called whenever a certificate is checked for the server |server_to_check|
94 // passing the result of said certificate check.
95 virtual void CertificateChecked(ServerToCheck server_to_check,
96 CertificateCheckResult result) = 0;
Bruno Rocha7f9aea22011-09-12 14:31:24 -070097 };
98
Alex Deymoc1c17b42015-11-23 03:53:15 -030099 CertificateChecker(PrefsInterface* prefs,
100 OpenSSLWrapper* openssl_wrapper,
101 ServerToCheck server_to_check);
102 virtual ~CertificateChecker() = default;
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700103
104 // This callback is called by libcurl just before the initialization of an
105 // SSL connection after having processed all other SSL related options. Used
Alex Deymoc1c17b42015-11-23 03:53:15 -0300106 // to check if server certificates change. |cert_checker| is expected to be a
107 // pointer to the CertificateChecker instance.
108 static CURLcode ProcessSSLContext(CURL* curl_handle,
109 SSL_CTX* ssl_ctx,
110 void* cert_checker);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700111
Alex Deymoc1c17b42015-11-23 03:53:15 -0300112 // Set the certificate observer to the passed instance. To remove the
113 // observer, pass a nullptr. The |observer| instance must be valid while this
114 // CertificateChecker verifies certificates.
115 void SetObserver(Observer* observer) { observer_ = observer; }
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700116
117 private:
118 FRIEND_TEST(CertificateCheckerTest, NewCertificate);
119 FRIEND_TEST(CertificateCheckerTest, SameCertificate);
120 FRIEND_TEST(CertificateCheckerTest, ChangedCertificate);
121 FRIEND_TEST(CertificateCheckerTest, FailedCertificate);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700122
Alex Deymoc1c17b42015-11-23 03:53:15 -0300123 // This callback is called by openssl after initial SSL verification. It is
124 // used to perform any additional security verification on the connection,
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700125 // but we use them here to get hold of the server certificate, in order to
126 // determine if it has changed since the last connection. Since openssl forces
Alex Deymoc1c17b42015-11-23 03:53:15 -0300127 // us to do this statically, we pass a pointer to this instance via a
128 // thread-safe global pointer. The assigned callback is then called once per
129 // each certificate on the server and returns 1 for success and 0 for failure.
130 static int VerifySSLCallback(int preverify_ok, X509_STORE_CTX* x509_ctx);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700131
Alex Deymoc1c17b42015-11-23 03:53:15 -0300132 // Checks if server certificate stored in |x509_ctx| has changed since last
133 // connection to that same server, specified by the |server_to_check_| member.
134 // This is called by the callbacks defined above. The result of the
135 // certificate check is passed to the observer, if any. Returns true on
136 // success and false otherwise.
137 bool CheckCertificateChange(int preverify_ok, X509_STORE_CTX* x509_ctx);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700138
Alex Deymoc1c17b42015-11-23 03:53:15 -0300139 // Notifies the observer, if any, of a certificate checking.
140 void NotifyCertificateChecked(CertificateCheckResult result);
141
142 // Prefs instance used to store the certificates seen in the past.
143 PrefsInterface* prefs_;
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700144
145 // The wrapper for openssl operations.
Alex Deymoc1c17b42015-11-23 03:53:15 -0300146 OpenSSLWrapper* openssl_wrapper_;
147
148 // The server to check from this certificate checker instance.
149 ServerToCheck server_to_check_;
150
151 // The observer called whenever a certificate is checked, if not null.
152 Observer* observer_{nullptr};
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700153
154 DISALLOW_COPY_AND_ASSIGN(CertificateChecker);
155};
156
157} // namespace chromeos_update_engine
158
Alex Deymo39910dc2015-11-09 17:04:30 -0800159#endif // UPDATE_ENGINE_COMMON_CERTIFICATE_CHECKER_H_