blob: 3b0df2984120a7d501b04ac5698fd43d1d8323bb [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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
11#ifndef WEBRTC_BASE_FAKESSLIDENTITY_H_
12#define WEBRTC_BASE_FAKESSLIDENTITY_H_
13
14#include <algorithm>
jbauch555604a2016-04-26 10:13:2215#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:2616#include <vector>
17
Karl Wiberg94784372015-04-20 12:03:0718#include "webrtc/base/common.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2619#include "webrtc/base/messagedigest.h"
20#include "webrtc/base/sslidentity.h"
21
22namespace rtc {
23
24class FakeSSLCertificate : public rtc::SSLCertificate {
25 public:
26 // SHA-1 is the default digest algorithm because it is available in all build
27 // configurations used for unit testing.
28 explicit FakeSSLCertificate(const std::string& data)
hbos3980d462015-12-09 13:26:4929 : data_(data), digest_algorithm_(DIGEST_SHA_1), expiration_time_(-1) {}
henrike@webrtc.orgf0488722014-05-13 18:00:2630 explicit FakeSSLCertificate(const std::vector<std::string>& certs)
hbos3980d462015-12-09 13:26:4931 : data_(certs.front()),
32 digest_algorithm_(DIGEST_SHA_1),
33 expiration_time_(-1) {
henrike@webrtc.orgf0488722014-05-13 18:00:2634 std::vector<std::string>::const_iterator it;
35 // Skip certs[0].
36 for (it = certs.begin() + 1; it != certs.end(); ++it) {
37 certs_.push_back(FakeSSLCertificate(*it));
38 }
39 }
nisseef8b61e2016-04-29 13:09:1540 FakeSSLCertificate* GetReference() const override {
henrike@webrtc.orgf0488722014-05-13 18:00:2641 return new FakeSSLCertificate(*this);
42 }
nisseef8b61e2016-04-29 13:09:1543 std::string ToPEMString() const override {
henrike@webrtc.orgf0488722014-05-13 18:00:2644 return data_;
45 }
nisseef8b61e2016-04-29 13:09:1546 void ToDER(Buffer* der_buffer) const override {
henrike@webrtc.orgf0488722014-05-13 18:00:2647 std::string der_string;
48 VERIFY(SSLIdentity::PemToDer(kPemTypeCertificate, data_, &der_string));
49 der_buffer->SetData(der_string.c_str(), der_string.size());
50 }
hbos3980d462015-12-09 13:26:4951 int64_t CertificateExpirationTime() const override {
52 return expiration_time_;
53 }
54 void SetCertificateExpirationTime(int64_t expiration_time) {
55 expiration_time_ = expiration_time;
56 }
henrike@webrtc.orgf0488722014-05-13 18:00:2657 void set_digest_algorithm(const std::string& algorithm) {
58 digest_algorithm_ = algorithm;
59 }
nisseef8b61e2016-04-29 13:09:1560 bool GetSignatureDigestAlgorithm(std::string* algorithm) const override {
henrike@webrtc.orgf0488722014-05-13 18:00:2661 *algorithm = digest_algorithm_;
62 return true;
63 }
nisseef8b61e2016-04-29 13:09:1564 bool ComputeDigest(const std::string& algorithm,
65 unsigned char* digest,
66 size_t size,
67 size_t* length) const override {
henrike@webrtc.orgf0488722014-05-13 18:00:2668 *length = rtc::ComputeDigest(algorithm, data_.c_str(), data_.size(),
69 digest, size);
70 return (*length != 0);
71 }
nisseef8b61e2016-04-29 13:09:1572 std::unique_ptr<SSLCertChain> GetChain() const override {
henrike@webrtc.orgf0488722014-05-13 18:00:2673 if (certs_.empty())
kwibergf5d47862016-03-15 19:53:2474 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:2675 std::vector<SSLCertificate*> new_certs(certs_.size());
76 std::transform(certs_.begin(), certs_.end(), new_certs.begin(), DupCert);
jbauch555604a2016-04-26 10:13:2277 std::unique_ptr<SSLCertChain> chain(new SSLCertChain(new_certs));
henrike@webrtc.org065247b2014-08-11 14:32:1378 std::for_each(new_certs.begin(), new_certs.end(), DeleteCert);
kwibergf5d47862016-03-15 19:53:2479 return chain;
henrike@webrtc.orgf0488722014-05-13 18:00:2680 }
81
82 private:
83 static FakeSSLCertificate* DupCert(FakeSSLCertificate cert) {
84 return cert.GetReference();
85 }
henrike@webrtc.org065247b2014-08-11 14:32:1386 static void DeleteCert(SSLCertificate* cert) { delete cert; }
henrike@webrtc.orgf0488722014-05-13 18:00:2687 std::string data_;
88 std::vector<FakeSSLCertificate> certs_;
89 std::string digest_algorithm_;
hbos3980d462015-12-09 13:26:4990 // Expiration time in seconds relative to epoch, 1970-01-01T00:00:00Z (UTC).
91 int64_t expiration_time_;
henrike@webrtc.orgf0488722014-05-13 18:00:2692};
93
94class FakeSSLIdentity : public rtc::SSLIdentity {
95 public:
96 explicit FakeSSLIdentity(const std::string& data) : cert_(data) {}
97 explicit FakeSSLIdentity(const FakeSSLCertificate& cert) : cert_(cert) {}
98 virtual FakeSSLIdentity* GetReference() const {
99 return new FakeSSLIdentity(*this);
100 }
101 virtual const FakeSSLCertificate& certificate() const { return cert_; }
hbos6b470a92016-04-28 12:14:21102 virtual std::string PrivateKeyToPEMString() const {
103 RTC_NOTREACHED(); // Not implemented.
104 return "";
105 }
106 virtual std::string PublicKeyToPEMString() const {
107 RTC_NOTREACHED(); // Not implemented.
108 return "";
109 }
110 virtual bool operator==(const SSLIdentity& other) const {
111 RTC_NOTREACHED(); // Not implemented.
112 return false;
113 }
henrike@webrtc.orgf0488722014-05-13 18:00:26114 private:
115 FakeSSLCertificate cert_;
116};
117
118} // namespace rtc
119
120#endif // WEBRTC_BASE_FAKESSLIDENTITY_H_