3 * Copyright 2012, The Libjingle Authors.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef TALK_BASE_FAKESSLIDENTITY_H_
29 #define TALK_BASE_FAKESSLIDENTITY_H_
34 #include "talk/base/messagedigest.h"
35 #include "talk/base/sslidentity.h"
39 class FakeSSLCertificate : public talk_base::SSLCertificate {
41 explicit FakeSSLCertificate(const std::string& data) : data_(data) {}
42 explicit FakeSSLCertificate(const std::vector<std::string>& certs)
43 : data_(certs.front()) {
44 std::vector<std::string>::const_iterator it;
46 for (it = certs.begin() + 1; it != certs.end(); ++it) {
47 certs_.push_back(FakeSSLCertificate(*it));
50 virtual FakeSSLCertificate* GetReference() const {
51 return new FakeSSLCertificate(*this);
53 virtual std::string ToPEMString() const {
56 virtual void ToDER(Buffer* der_buffer) const {
57 std::string der_string;
58 VERIFY(SSLIdentity::PemToDer(kPemTypeCertificate, data_, &der_string));
59 der_buffer->SetData(der_string.c_str(), der_string.size());
61 virtual bool GetSignatureDigestAlgorithm(std::string* algorithm) const {
62 // SHA-1 is chosen because it is available in all build configurations
63 // used for unit testing.
64 *algorithm = DIGEST_SHA_1;
67 virtual bool ComputeDigest(const std::string &algorithm,
68 unsigned char *digest, std::size_t size,
69 std::size_t *length) const {
70 *length = talk_base::ComputeDigest(algorithm, data_.c_str(), data_.size(),
72 return (*length != 0);
74 virtual bool GetChain(SSLCertChain** chain) const {
77 std::vector<SSLCertificate*> new_certs(certs_.size());
78 std::transform(certs_.begin(), certs_.end(), new_certs.begin(), DupCert);
79 *chain = new SSLCertChain(new_certs);
84 static FakeSSLCertificate* DupCert(FakeSSLCertificate cert) {
85 return cert.GetReference();
88 std::vector<FakeSSLCertificate> certs_;
91 class FakeSSLIdentity : public talk_base::SSLIdentity {
93 explicit FakeSSLIdentity(const std::string& data) : cert_(data) {}
94 explicit FakeSSLIdentity(const FakeSSLCertificate& cert) : cert_(cert) {}
95 virtual FakeSSLIdentity* GetReference() const {
96 return new FakeSSLIdentity(*this);
98 virtual const FakeSSLCertificate& certificate() const { return cert_; }
100 FakeSSLCertificate cert_;
103 } // namespace talk_base
105 #endif // TALK_BASE_FAKESSLIDENTITY_H_