blob: 67c5c29a899e1c7bbe0bb3b4630062c7d283d6ef [file] [log] [blame]
Henrik Boström41b3a382015-08-20 10:15:541/*
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 17:11:0011#ifndef RTC_BASE_RTC_CERTIFICATE_H_
12#define RTC_BASE_RTC_CERTIFICATE_H_
Henrik Boström41b3a382015-08-20 10:15:5413
Henrik Kjellanderec78f1c2017-06-29 05:52:5014#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3315
Henrik Kjellanderec78f1c2017-06-29 05:52:5016#include <memory>
Yves Gerey988cc082018-10-23 10:03:0117#include <string>
Henrik Kjellanderec78f1c2017-06-29 05:52:5018
Harald Alvestrandc830bd62021-06-24 13:02:0919#include "absl/base/attributes.h"
Ali Tofigh7fa90572022-03-17 14:47:4920#include "absl/strings/string_view.h"
Tommi86ee89f2021-04-20 14:58:0121#include "api/ref_counted_base.h"
Mirko Bonadeid9708072019-01-25 19:26:4822#include "api/scoped_refptr.h"
Mirko Bonadei35214fc2019-09-23 12:54:2823#include "rtc_base/system/rtc_export.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5024
25namespace rtc {
26
Yves Gerey988cc082018-10-23 10:03:0127class SSLCertChain;
28class SSLCertificate;
29class SSLIdentity;
30
Henrik Kjellanderec78f1c2017-06-29 05:52:5031// 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 Titovcfea2182021-08-09 23:22:3135// `SSLIdentity::PrivateKeyToPEMString` and `SSLCertificate::ToPEMString`, e.g.
Henrik Kjellanderec78f1c2017-06-29 05:52:5036// the string representations used by OpenSSL.
37class RTCCertificatePEM {
38 public:
Ali Tofigh7fa90572022-03-17 14:47:4939 RTCCertificatePEM(absl::string_view private_key,
40 absl::string_view certificate)
Yves Gerey665174f2018-06-19 13:03:0541 : private_key_(private_key), certificate_(certificate) {}
Henrik Kjellanderec78f1c2017-06-29 05:52:5042
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.
Tommi86ee89f2021-04-20 14:58:0154class RTC_EXPORT RTCCertificate final
55 : public RefCountedNonVirtual<RTCCertificate> {
Henrik Kjellanderec78f1c2017-06-29 05:52:5056 public:
Artem Titov96e3b992021-07-26 14:03:1457 // Takes ownership of `identity`.
Henrik Kjellanderec78f1c2017-06-29 05:52:5058 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 Titov96e3b992021-07-26 14:03:1463 // Checks if the certificate has expired, where `now` is expressed in ms
Henrik Kjellanderec78f1c2017-06-29 05:52:5064 // relative to epoch, 1970-01-01T00:00:00Z.
65 bool HasExpired(uint64_t now) const;
Benjamin Wright6c6c9df2018-10-25 08:16:2666
67 const SSLCertificate& GetSSLCertificate() const;
68 const SSLCertChain& GetSSLCertificateChain() const;
69
Henrik Kjellanderec78f1c2017-06-29 05:52:5070 // TODO(hbos): If possible, remove once RTCCertificate and its
Benjamin Wright6c6c9df2018-10-25 08:16:2671 // GetSSLCertificate() is used in all relevant places. Should not pass around
Henrik Kjellanderec78f1c2017-06-29 05:52:5072 // 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);
Tommi86ee89f2021-04-20 14:58:0185
86 friend class RefCountedNonVirtual<RTCCertificate>;
87 ~RTCCertificate();
Henrik Kjellanderec78f1c2017-06-29 05:52:5088
89 private:
90 // The SSLIdentity is the owner of the SSLCertificate. To protect our
Artem Titov96e3b992021-07-26 14:03:1491 // GetSSLCertificate() we take ownership of `identity_`.
Tomas Gunnarsson3cccdb82021-03-29 17:34:0092 const std::unique_ptr<SSLIdentity> identity_;
Henrik Kjellanderec78f1c2017-06-29 05:52:5093};
94
95} // namespace rtc
Henrik Boström41b3a382015-08-20 10:15:5496
Steve Anton10542f22019-01-11 17:11:0097#endif // RTC_BASE_RTC_CERTIFICATE_H_