blob: 0adefbce28205b876a1a26971e419ff7c9713d19 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_RTCCERTIFICATE_H_
12#define RTC_BASE_RTCCERTIFICATE_H_
Henrik Boström41b3a382015-08-20 12:15:54 +020013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <stdint.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <memory>
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <string>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/refcount.h"
19#include "rtc_base/scoped_ref_ptr.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020
21namespace rtc {
22
Yves Gerey988cc082018-10-23 12:03:01 +020023class SSLCertChain;
24class SSLCertificate;
25class SSLIdentity;
26
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027// This class contains PEM strings of an RTCCertificate's private key and
28// certificate and acts as a text representation of RTCCertificate. Certificates
29// can be serialized and deserialized to and from this format, which allows for
30// cloning and storing of certificates to disk. The PEM format is that of
31// |SSLIdentity::PrivateKeyToPEMString| and |SSLCertificate::ToPEMString|, e.g.
32// the string representations used by OpenSSL.
33class RTCCertificatePEM {
34 public:
Yves Gerey665174f2018-06-19 15:03:05 +020035 RTCCertificatePEM(const std::string& private_key,
36 const std::string& certificate)
37 : private_key_(private_key), certificate_(certificate) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038
39 const std::string& private_key() const { return private_key_; }
40 const std::string& certificate() const { return certificate_; }
41
42 private:
43 std::string private_key_;
44 std::string certificate_;
45};
46
47// A thin abstraction layer between "lower level crypto stuff" like
48// SSLCertificate and WebRTC usage. Takes ownership of some lower level objects,
49// reference counting protects these from premature destruction.
50class RTCCertificate : public RefCountInterface {
51 public:
52 // Takes ownership of |identity|.
53 static scoped_refptr<RTCCertificate> Create(
54 std::unique_ptr<SSLIdentity> identity);
55
56 // Returns the expiration time in ms relative to epoch, 1970-01-01T00:00:00Z.
57 uint64_t Expires() const;
58 // Checks if the certificate has expired, where |now| is expressed in ms
59 // relative to epoch, 1970-01-01T00:00:00Z.
60 bool HasExpired(uint64_t now) const;
61 const SSLCertificate& ssl_certificate() const;
Taylor Brandstetterc3928662018-02-23 13:04:51 -080062 const SSLCertChain& ssl_cert_chain() const;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020063
64 // TODO(hbos): If possible, remove once RTCCertificate and its
65 // ssl_certificate() is used in all relevant places. Should not pass around
66 // raw SSLIdentity* for the sake of accessing SSLIdentity::certificate().
67 // However, some places might need SSLIdentity* for its public/private key...
68 SSLIdentity* identity() const { return identity_.get(); }
69
70 // To/from PEM, a text representation of the RTCCertificate.
71 RTCCertificatePEM ToPEM() const;
72 // Can return nullptr if the certificate is invalid.
73 static scoped_refptr<RTCCertificate> FromPEM(const RTCCertificatePEM& pem);
74 bool operator==(const RTCCertificate& certificate) const;
75 bool operator!=(const RTCCertificate& certificate) const;
76
77 protected:
78 explicit RTCCertificate(SSLIdentity* identity);
79 ~RTCCertificate() override;
80
81 private:
82 // The SSLIdentity is the owner of the SSLCertificate. To protect our
83 // ssl_certificate() we take ownership of |identity_|.
84 std::unique_ptr<SSLIdentity> identity_;
85};
86
87} // namespace rtc
Henrik Boström41b3a382015-08-20 12:15:54 +020088
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020089#endif // RTC_BASE_RTCCERTIFICATE_H_