Henrik Boström | 41b3a38 | 2015-08-20 10:15:54 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_RTC_CERTIFICATE_H_ |
| 12 | #define RTC_BASE_RTC_CERTIFICATE_H_ |
Henrik Boström | 41b3a38 | 2015-08-20 10:15:54 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 16 | #include <memory> |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 17 | #include <string> |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 18 | |
Harald Alvestrand | c830bd6 | 2021-06-24 13:02:09 | [diff] [blame] | 19 | #include "absl/base/attributes.h" |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 20 | #include "absl/strings/string_view.h" |
Tommi | 86ee89f | 2021-04-20 14:58:01 | [diff] [blame] | 21 | #include "api/ref_counted_base.h" |
Mirko Bonadei | d970807 | 2019-01-25 19:26:48 | [diff] [blame] | 22 | #include "api/scoped_refptr.h" |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 23 | #include "rtc_base/system/rtc_export.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 24 | |
| 25 | namespace rtc { |
| 26 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 27 | class SSLCertChain; |
| 28 | class SSLCertificate; |
| 29 | class SSLIdentity; |
| 30 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 31 | // This class contains PEM strings of an RTCCertificate's private key and |
| 32 | // certificate and acts as a text representation of RTCCertificate. Certificates |
| 33 | // can be serialized and deserialized to and from this format, which allows for |
| 34 | // cloning and storing of certificates to disk. The PEM format is that of |
Artem Titov | cfea218 | 2021-08-09 23:22:31 | [diff] [blame] | 35 | // `SSLIdentity::PrivateKeyToPEMString` and `SSLCertificate::ToPEMString`, e.g. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 36 | // the string representations used by OpenSSL. |
| 37 | class RTCCertificatePEM { |
| 38 | public: |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 39 | RTCCertificatePEM(absl::string_view private_key, |
| 40 | absl::string_view certificate) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 41 | : private_key_(private_key), certificate_(certificate) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 42 | |
| 43 | const std::string& private_key() const { return private_key_; } |
| 44 | const std::string& certificate() const { return certificate_; } |
| 45 | |
| 46 | private: |
| 47 | std::string private_key_; |
| 48 | std::string certificate_; |
| 49 | }; |
| 50 | |
| 51 | // A thin abstraction layer between "lower level crypto stuff" like |
| 52 | // SSLCertificate and WebRTC usage. Takes ownership of some lower level objects, |
| 53 | // reference counting protects these from premature destruction. |
Tommi | 86ee89f | 2021-04-20 14:58:01 | [diff] [blame] | 54 | class RTC_EXPORT RTCCertificate final |
| 55 | : public RefCountedNonVirtual<RTCCertificate> { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 56 | public: |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 57 | // Takes ownership of `identity`. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 58 | static scoped_refptr<RTCCertificate> Create( |
| 59 | std::unique_ptr<SSLIdentity> identity); |
| 60 | |
| 61 | // Returns the expiration time in ms relative to epoch, 1970-01-01T00:00:00Z. |
| 62 | uint64_t Expires() const; |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 63 | // Checks if the certificate has expired, where `now` is expressed in ms |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 64 | // relative to epoch, 1970-01-01T00:00:00Z. |
| 65 | bool HasExpired(uint64_t now) const; |
Benjamin Wright | 6c6c9df | 2018-10-25 08:16:26 | [diff] [blame] | 66 | |
| 67 | const SSLCertificate& GetSSLCertificate() const; |
| 68 | const SSLCertChain& GetSSLCertificateChain() const; |
| 69 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 70 | // TODO(hbos): If possible, remove once RTCCertificate and its |
Benjamin Wright | 6c6c9df | 2018-10-25 08:16:26 | [diff] [blame] | 71 | // GetSSLCertificate() is used in all relevant places. Should not pass around |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 72 | // raw SSLIdentity* for the sake of accessing SSLIdentity::certificate(). |
| 73 | // However, some places might need SSLIdentity* for its public/private key... |
| 74 | SSLIdentity* identity() const { return identity_.get(); } |
| 75 | |
| 76 | // To/from PEM, a text representation of the RTCCertificate. |
| 77 | RTCCertificatePEM ToPEM() const; |
| 78 | // Can return nullptr if the certificate is invalid. |
| 79 | static scoped_refptr<RTCCertificate> FromPEM(const RTCCertificatePEM& pem); |
| 80 | bool operator==(const RTCCertificate& certificate) const; |
| 81 | bool operator!=(const RTCCertificate& certificate) const; |
| 82 | |
| 83 | protected: |
| 84 | explicit RTCCertificate(SSLIdentity* identity); |
Tommi | 86ee89f | 2021-04-20 14:58:01 | [diff] [blame] | 85 | |
| 86 | friend class RefCountedNonVirtual<RTCCertificate>; |
| 87 | ~RTCCertificate(); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | // The SSLIdentity is the owner of the SSLCertificate. To protect our |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 91 | // GetSSLCertificate() we take ownership of `identity_`. |
Tomas Gunnarsson | 3cccdb8 | 2021-03-29 17:34:00 | [diff] [blame] | 92 | const std::unique_ptr<SSLIdentity> identity_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | } // namespace rtc |
Henrik Boström | 41b3a38 | 2015-08-20 10:15:54 | [diff] [blame] | 96 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 97 | #endif // RTC_BASE_RTC_CERTIFICATE_H_ |