blob: 882c735036fa2eee4505c7522be2fa89ee088a9d [file] [log] [blame]
Henrik Boström41b3a382015-08-20 12:15:54 +02001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_RTC_CERTIFICATE_H_
12#define RTC_BASE_RTC_CERTIFICATE_H_
Henrik Boström41b3a382015-08-20 12:15:54 +020013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <memory>
Yves Gerey988cc082018-10-23 12:03:01 +020017#include <string>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018
Harald Alvestrandc830bd62021-06-24 13:02:09 +000019#include "absl/base/attributes.h"
Tommi86ee89f2021-04-20 16:58:01 +020020#include "api/ref_counted_base.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010021#include "api/scoped_refptr.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020022#include "rtc_base/system/rtc_export.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023
24namespace rtc {
25
Yves Gerey988cc082018-10-23 12:03:01 +020026class SSLCertChain;
27class SSLCertificate;
28class SSLIdentity;
29
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030// This class contains PEM strings of an RTCCertificate's private key and
31// certificate and acts as a text representation of RTCCertificate. Certificates
32// can be serialized and deserialized to and from this format, which allows for
33// cloning and storing of certificates to disk. The PEM format is that of
34// |SSLIdentity::PrivateKeyToPEMString| and |SSLCertificate::ToPEMString|, e.g.
35// the string representations used by OpenSSL.
36class RTCCertificatePEM {
37 public:
Yves Gerey665174f2018-06-19 15:03:05 +020038 RTCCertificatePEM(const std::string& private_key,
39 const std::string& certificate)
40 : private_key_(private_key), certificate_(certificate) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041
42 const std::string& private_key() const { return private_key_; }
43 const std::string& certificate() const { return certificate_; }
44
45 private:
46 std::string private_key_;
47 std::string certificate_;
48};
49
50// A thin abstraction layer between "lower level crypto stuff" like
51// SSLCertificate and WebRTC usage. Takes ownership of some lower level objects,
52// reference counting protects these from premature destruction.
Tommi86ee89f2021-04-20 16:58:01 +020053class RTC_EXPORT RTCCertificate final
54 : public RefCountedNonVirtual<RTCCertificate> {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055 public:
Artem Titov96e3b992021-07-26 16:03:14 +020056 // Takes ownership of `identity`.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057 static scoped_refptr<RTCCertificate> Create(
58 std::unique_ptr<SSLIdentity> identity);
59
60 // Returns the expiration time in ms relative to epoch, 1970-01-01T00:00:00Z.
61 uint64_t Expires() const;
Artem Titov96e3b992021-07-26 16:03:14 +020062 // Checks if the certificate has expired, where `now` is expressed in ms
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020063 // relative to epoch, 1970-01-01T00:00:00Z.
64 bool HasExpired(uint64_t now) const;
Benjamin Wright6c6c9df2018-10-25 01:16:26 -070065
66 const SSLCertificate& GetSSLCertificate() const;
67 const SSLCertChain& GetSSLCertificateChain() const;
68
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069 // TODO(hbos): If possible, remove once RTCCertificate and its
Benjamin Wright6c6c9df2018-10-25 01:16:26 -070070 // GetSSLCertificate() is used in all relevant places. Should not pass around
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020071 // raw SSLIdentity* for the sake of accessing SSLIdentity::certificate().
72 // However, some places might need SSLIdentity* for its public/private key...
73 SSLIdentity* identity() const { return identity_.get(); }
74
75 // To/from PEM, a text representation of the RTCCertificate.
76 RTCCertificatePEM ToPEM() const;
77 // Can return nullptr if the certificate is invalid.
78 static scoped_refptr<RTCCertificate> FromPEM(const RTCCertificatePEM& pem);
79 bool operator==(const RTCCertificate& certificate) const;
80 bool operator!=(const RTCCertificate& certificate) const;
81
82 protected:
83 explicit RTCCertificate(SSLIdentity* identity);
Tommi86ee89f2021-04-20 16:58:01 +020084
85 friend class RefCountedNonVirtual<RTCCertificate>;
86 ~RTCCertificate();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020087
88 private:
89 // The SSLIdentity is the owner of the SSLCertificate. To protect our
Artem Titov96e3b992021-07-26 16:03:14 +020090 // GetSSLCertificate() we take ownership of `identity_`.
Tomas Gunnarsson3cccdb82021-03-29 19:34:00 +020091 const std::unique_ptr<SSLIdentity> identity_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020092};
93
94} // namespace rtc
Henrik Boström41b3a382015-08-20 12:15:54 +020095
Steve Anton10542f22019-01-11 09:11:00 -080096#endif // RTC_BASE_RTC_CERTIFICATE_H_