blob: 5efa268f8fdc73cfd86cd5440da60554876a243a [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012, The Libjingle Authors.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
28#ifndef TALK_BASE_FAKESSLIDENTITY_H_
29#define TALK_BASE_FAKESSLIDENTITY_H_
30
wu@webrtc.org4551b792013-10-09 15:37:36 +000031#include <algorithm>
32#include <vector>
33
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include "talk/base/messagedigest.h"
35#include "talk/base/sslidentity.h"
36
37namespace talk_base {
38
39class FakeSSLCertificate : public talk_base::SSLCertificate {
40 public:
41 explicit FakeSSLCertificate(const std::string& data) : data_(data) {}
wu@webrtc.org4551b792013-10-09 15:37:36 +000042 explicit FakeSSLCertificate(const std::vector<std::string>& certs)
43 : data_(certs.front()) {
44 std::vector<std::string>::const_iterator it;
45 // Skip certs[0].
46 for (it = certs.begin() + 1; it != certs.end(); ++it) {
47 certs_.push_back(FakeSSLCertificate(*it));
48 }
49 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050 virtual FakeSSLCertificate* GetReference() const {
51 return new FakeSSLCertificate(*this);
52 }
53 virtual std::string ToPEMString() const {
54 return data_;
55 }
wu@webrtc.org4551b792013-10-09 15:37:36 +000056 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());
60 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 virtual bool ComputeDigest(const std::string &algorithm,
62 unsigned char *digest, std::size_t size,
63 std::size_t *length) const {
64 *length = talk_base::ComputeDigest(algorithm, data_.c_str(), data_.size(),
65 digest, size);
66 return (*length != 0);
67 }
wu@webrtc.org4551b792013-10-09 15:37:36 +000068 virtual bool GetChain(SSLCertChain** chain) const {
69 if (certs_.empty())
70 return false;
71 std::vector<SSLCertificate*> new_certs(certs_.size());
72 std::transform(certs_.begin(), certs_.end(), new_certs.begin(), DupCert);
73 *chain = new SSLCertChain(new_certs);
74 return true;
75 }
76
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 private:
wu@webrtc.org4551b792013-10-09 15:37:36 +000078 static FakeSSLCertificate* DupCert(FakeSSLCertificate cert) {
79 return cert.GetReference();
80 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 std::string data_;
wu@webrtc.org4551b792013-10-09 15:37:36 +000082 std::vector<FakeSSLCertificate> certs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083};
84
85class FakeSSLIdentity : public talk_base::SSLIdentity {
86 public:
87 explicit FakeSSLIdentity(const std::string& data) : cert_(data) {}
wu@webrtc.org4551b792013-10-09 15:37:36 +000088 explicit FakeSSLIdentity(const FakeSSLCertificate& cert) : cert_(cert) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 virtual FakeSSLIdentity* GetReference() const {
90 return new FakeSSLIdentity(*this);
91 }
92 virtual const FakeSSLCertificate& certificate() const { return cert_; }
93 private:
94 FakeSSLCertificate cert_;
95};
96
97} // namespace talk_base
98
99#endif // TALK_BASE_FAKESSLIDENTITY_H_