Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [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 | |
| 11 | #ifndef WEBRTC_BASE_RTCCERTIFICATE_H_ |
| 12 | #define WEBRTC_BASE_RTCCERTIFICATE_H_ |
| 13 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 16 | #include "webrtc/base/basictypes.h" |
| 17 | #include "webrtc/base/refcount.h" |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 18 | #include "webrtc/base/scoped_ref_ptr.h" |
| 19 | #include "webrtc/base/sslidentity.h" |
| 20 | |
| 21 | namespace rtc { |
| 22 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 23 | // This class contains PEM strings of an RTCCertificate's private key and |
| 24 | // certificate and acts as a text representation of RTCCertificate. Certificates |
| 25 | // can be serialized and deserialized to and from this format, which allows for |
| 26 | // cloning and storing of certificates to disk. The PEM format is that of |
| 27 | // |SSLIdentity::PrivateKeyToPEMString| and |SSLCertificate::ToPEMString|, e.g. |
| 28 | // the string representations used by OpenSSL. |
| 29 | class RTCCertificatePEM { |
| 30 | public: |
| 31 | RTCCertificatePEM( |
| 32 | const std::string& private_key, |
| 33 | const std::string& certificate) |
| 34 | : private_key_(private_key), |
| 35 | certificate_(certificate) {} |
| 36 | |
| 37 | const std::string& private_key() const { return private_key_; } |
| 38 | const std::string& certificate() const { return certificate_; } |
| 39 | |
| 40 | private: |
| 41 | std::string private_key_; |
| 42 | std::string certificate_; |
| 43 | }; |
| 44 | |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 45 | // A thin abstraction layer between "lower level crypto stuff" like |
| 46 | // SSLCertificate and WebRTC usage. Takes ownership of some lower level objects, |
| 47 | // reference counting protects these from premature destruction. |
| 48 | class RTCCertificate : public RefCountInterface { |
| 49 | public: |
| 50 | // Takes ownership of |identity|. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 51 | static scoped_refptr<RTCCertificate> Create( |
| 52 | std::unique_ptr<SSLIdentity> identity); |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 53 | |
hbos | 3980d46 | 2015-12-09 05:26:49 -0800 | [diff] [blame] | 54 | // Returns the expiration time in ms relative to epoch, 1970-01-01T00:00:00Z. |
| 55 | uint64_t Expires() const; |
| 56 | // Checks if the certificate has expired, where |now| is expressed in ms |
| 57 | // relative to epoch, 1970-01-01T00:00:00Z. |
| 58 | bool HasExpired(uint64_t now) const; |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 59 | const SSLCertificate& ssl_certificate() const; |
| 60 | |
| 61 | // TODO(hbos): If possible, remove once RTCCertificate and its |
| 62 | // ssl_certificate() is used in all relevant places. Should not pass around |
| 63 | // raw SSLIdentity* for the sake of accessing SSLIdentity::certificate(). |
| 64 | // However, some places might need SSLIdentity* for its public/private key... |
| 65 | SSLIdentity* identity() const { return identity_.get(); } |
| 66 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 67 | // To/from PEM, a text representation of the RTCCertificate. |
| 68 | RTCCertificatePEM ToPEM() const; |
| 69 | static scoped_refptr<RTCCertificate> FromPEM(const RTCCertificatePEM& pem); |
| 70 | bool operator==(const RTCCertificate& certificate) const; |
| 71 | bool operator!=(const RTCCertificate& certificate) const; |
| 72 | |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 73 | protected: |
| 74 | explicit RTCCertificate(SSLIdentity* identity); |
| 75 | ~RTCCertificate() override; |
| 76 | |
| 77 | private: |
| 78 | // The SSLIdentity is the owner of the SSLCertificate. To protect our |
| 79 | // ssl_certificate() we take ownership of |identity_|. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 80 | std::unique_ptr<SSLIdentity> identity_; |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | } // namespace rtc |
| 84 | |
| 85 | #endif // WEBRTC_BASE_RTCCERTIFICATE_H_ |