blob: 30507290e0daec85f60cfe304ab54d128c89da0d [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2012 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_FAKESSLIDENTITY_H_
12#define RTC_BASE_FAKESSLIDENTITY_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <algorithm>
15#include <memory>
16#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
19#include "rtc_base/messagedigest.h"
David Benjamin3df76b12017-09-29 12:27:18 -040020#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/sslidentity.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022
23namespace rtc {
24
25class FakeSSLCertificate : public rtc::SSLCertificate {
26 public:
27 // SHA-1 is the default digest algorithm because it is available in all build
28 // configurations used for unit testing.
29 explicit FakeSSLCertificate(const std::string& data)
30 : data_(data), digest_algorithm_(DIGEST_SHA_1), expiration_time_(-1) {}
31 explicit FakeSSLCertificate(const std::vector<std::string>& certs)
32 : data_(certs.front()),
33 digest_algorithm_(DIGEST_SHA_1),
34 expiration_time_(-1) {
35 std::vector<std::string>::const_iterator it;
36 // Skip certs[0].
37 for (it = certs.begin() + 1; it != certs.end(); ++it) {
38 certs_.push_back(FakeSSLCertificate(*it));
39 }
40 }
41 FakeSSLCertificate* GetReference() const override {
42 return new FakeSSLCertificate(*this);
43 }
44 std::string ToPEMString() const override {
45 return data_;
46 }
47 void ToDER(Buffer* der_buffer) const override {
48 std::string der_string;
49 RTC_CHECK(SSLIdentity::PemToDer(kPemTypeCertificate, data_, &der_string));
50 der_buffer->SetData(der_string.c_str(), der_string.size());
51 }
52 int64_t CertificateExpirationTime() const override {
53 return expiration_time_;
54 }
55 void SetCertificateExpirationTime(int64_t expiration_time) {
56 expiration_time_ = expiration_time;
57 }
58 void set_digest_algorithm(const std::string& algorithm) {
59 digest_algorithm_ = algorithm;
60 }
61 bool GetSignatureDigestAlgorithm(std::string* algorithm) const override {
62 *algorithm = digest_algorithm_;
63 return true;
64 }
65 bool ComputeDigest(const std::string& algorithm,
66 unsigned char* digest,
67 size_t size,
68 size_t* length) const override {
69 *length = rtc::ComputeDigest(algorithm, data_.c_str(), data_.size(),
70 digest, size);
71 return (*length != 0);
72 }
73 std::unique_ptr<SSLCertChain> GetChain() const override {
74 if (certs_.empty())
75 return nullptr;
David Benjamin3df76b12017-09-29 12:27:18 -040076 std::vector<std::unique_ptr<SSLCertificate>> new_certs(certs_.size());
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020077 std::transform(certs_.begin(), certs_.end(), new_certs.begin(), DupCert);
David Benjamin3df76b12017-09-29 12:27:18 -040078 return MakeUnique<SSLCertChain>(std::move(new_certs));
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020079 }
80
81 private:
David Benjamin3df76b12017-09-29 12:27:18 -040082 static std::unique_ptr<SSLCertificate> DupCert(FakeSSLCertificate cert) {
83 return cert.GetUniqueReference();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084 }
85 static void DeleteCert(SSLCertificate* cert) { delete cert; }
86 std::string data_;
87 std::vector<FakeSSLCertificate> certs_;
88 std::string digest_algorithm_;
89 // Expiration time in seconds relative to epoch, 1970-01-01T00:00:00Z (UTC).
90 int64_t expiration_time_;
91};
92
93class FakeSSLIdentity : public rtc::SSLIdentity {
94 public:
95 explicit FakeSSLIdentity(const std::string& data) : cert_(data) {}
96 explicit FakeSSLIdentity(const FakeSSLCertificate& cert) : cert_(cert) {}
97 virtual FakeSSLIdentity* GetReference() const {
98 return new FakeSSLIdentity(*this);
99 }
100 virtual const FakeSSLCertificate& certificate() const { return cert_; }
101 virtual std::string PrivateKeyToPEMString() const {
102 RTC_NOTREACHED(); // Not implemented.
103 return "";
104 }
105 virtual std::string PublicKeyToPEMString() const {
106 RTC_NOTREACHED(); // Not implemented.
107 return "";
108 }
109 virtual bool operator==(const SSLIdentity& other) const {
110 RTC_NOTREACHED(); // Not implemented.
111 return false;
112 }
113 private:
114 FakeSSLCertificate cert_;
115};
116
117} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200119#endif // RTC_BASE_FAKESSLIDENTITY_H_