2 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
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.
11 #ifndef WEBRTC_BASE_FAKESSLIDENTITY_H_
12 #define WEBRTC_BASE_FAKESSLIDENTITY_H_
17 #include "webrtc/base/messagedigest.h"
18 #include "webrtc/base/sslidentity.h"
22 class FakeSSLCertificate : public rtc::SSLCertificate {
24 // SHA-1 is the default digest algorithm because it is available in all build
25 // configurations used for unit testing.
26 explicit FakeSSLCertificate(const std::string& data)
27 : data_(data), digest_algorithm_(DIGEST_SHA_1) {}
28 explicit FakeSSLCertificate(const std::vector<std::string>& certs)
29 : data_(certs.front()), digest_algorithm_(DIGEST_SHA_1) {
30 std::vector<std::string>::const_iterator it;
32 for (it = certs.begin() + 1; it != certs.end(); ++it) {
33 certs_.push_back(FakeSSLCertificate(*it));
36 virtual FakeSSLCertificate* GetReference() const {
37 return new FakeSSLCertificate(*this);
39 virtual std::string ToPEMString() const {
42 virtual void ToDER(Buffer* der_buffer) const {
43 std::string der_string;
44 VERIFY(SSLIdentity::PemToDer(kPemTypeCertificate, data_, &der_string));
45 der_buffer->SetData(der_string.c_str(), der_string.size());
47 void set_digest_algorithm(const std::string& algorithm) {
48 digest_algorithm_ = algorithm;
50 virtual bool GetSignatureDigestAlgorithm(std::string* algorithm) const {
51 *algorithm = digest_algorithm_;
54 virtual bool ComputeDigest(const std::string& algorithm,
55 unsigned char* digest,
57 size_t* length) const {
58 *length = rtc::ComputeDigest(algorithm, data_.c_str(), data_.size(),
60 return (*length != 0);
62 virtual bool GetChain(SSLCertChain** chain) const {
65 std::vector<SSLCertificate*> new_certs(certs_.size());
66 std::transform(certs_.begin(), certs_.end(), new_certs.begin(), DupCert);
67 *chain = new SSLCertChain(new_certs);
68 std::for_each(new_certs.begin(), new_certs.end(), DeleteCert);
73 static FakeSSLCertificate* DupCert(FakeSSLCertificate cert) {
74 return cert.GetReference();
76 static void DeleteCert(SSLCertificate* cert) { delete cert; }
78 std::vector<FakeSSLCertificate> certs_;
79 std::string digest_algorithm_;
82 class FakeSSLIdentity : public rtc::SSLIdentity {
84 explicit FakeSSLIdentity(const std::string& data) : cert_(data) {}
85 explicit FakeSSLIdentity(const FakeSSLCertificate& cert) : cert_(cert) {}
86 virtual FakeSSLIdentity* GetReference() const {
87 return new FakeSSLIdentity(*this);
89 virtual const FakeSSLCertificate& certificate() const { return cert_; }
91 FakeSSLCertificate cert_;
96 #endif // WEBRTC_BASE_FAKESSLIDENTITY_H_