blob: fbe5e648425c9c4cdc9f1dce46265938bafe651c [file] [log] [blame]
henrike@webrtc.org0e118e72013-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.org62fe97f2013-10-09 15:37:36 +000031#include <algorithm>
32#include <vector>
33
henrike@webrtc.org0e118e72013-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:
wu@webrtc.org8a77f5b2014-02-13 23:18:49 +000041 // SHA-1 is the default digest algorithm because it is available in all build
42 // configurations used for unit testing.
43 explicit FakeSSLCertificate(const std::string& data)
44 : data_(data), digest_algorithm_(DIGEST_SHA_1) {}
wu@webrtc.org62fe97f2013-10-09 15:37:36 +000045 explicit FakeSSLCertificate(const std::vector<std::string>& certs)
wu@webrtc.org8a77f5b2014-02-13 23:18:49 +000046 : data_(certs.front()), digest_algorithm_(DIGEST_SHA_1) {
wu@webrtc.org62fe97f2013-10-09 15:37:36 +000047 std::vector<std::string>::const_iterator it;
48 // Skip certs[0].
49 for (it = certs.begin() + 1; it != certs.end(); ++it) {
50 certs_.push_back(FakeSSLCertificate(*it));
51 }
52 }
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000053 virtual FakeSSLCertificate* GetReference() const {
54 return new FakeSSLCertificate(*this);
55 }
56 virtual std::string ToPEMString() const {
57 return data_;
58 }
wu@webrtc.org62fe97f2013-10-09 15:37:36 +000059 virtual void ToDER(Buffer* der_buffer) const {
60 std::string der_string;
61 VERIFY(SSLIdentity::PemToDer(kPemTypeCertificate, data_, &der_string));
62 der_buffer->SetData(der_string.c_str(), der_string.size());
63 }
wu@webrtc.org8a77f5b2014-02-13 23:18:49 +000064 void set_digest_algorithm(const std::string& algorithm) {
65 digest_algorithm_ = algorithm;
66 }
mallinath@webrtc.org8841d7b2013-10-13 17:18:27 +000067 virtual bool GetSignatureDigestAlgorithm(std::string* algorithm) const {
wu@webrtc.org8a77f5b2014-02-13 23:18:49 +000068 *algorithm = digest_algorithm_;
mallinath@webrtc.org8841d7b2013-10-13 17:18:27 +000069 return true;
70 }
pbos@webrtc.orgb9518272014-03-07 15:22:04 +000071 virtual bool ComputeDigest(const std::string& algorithm,
72 unsigned char* digest,
73 size_t size,
74 size_t* length) const {
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000075 *length = talk_base::ComputeDigest(algorithm, data_.c_str(), data_.size(),
76 digest, size);
77 return (*length != 0);
78 }
wu@webrtc.org62fe97f2013-10-09 15:37:36 +000079 virtual bool GetChain(SSLCertChain** chain) const {
80 if (certs_.empty())
81 return false;
82 std::vector<SSLCertificate*> new_certs(certs_.size());
83 std::transform(certs_.begin(), certs_.end(), new_certs.begin(), DupCert);
84 *chain = new SSLCertChain(new_certs);
85 return true;
86 }
87
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000088 private:
wu@webrtc.org62fe97f2013-10-09 15:37:36 +000089 static FakeSSLCertificate* DupCert(FakeSSLCertificate cert) {
90 return cert.GetReference();
91 }
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000092 std::string data_;
wu@webrtc.org62fe97f2013-10-09 15:37:36 +000093 std::vector<FakeSSLCertificate> certs_;
wu@webrtc.org8a77f5b2014-02-13 23:18:49 +000094 std::string digest_algorithm_;
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000095};
96
97class FakeSSLIdentity : public talk_base::SSLIdentity {
98 public:
99 explicit FakeSSLIdentity(const std::string& data) : cert_(data) {}
wu@webrtc.org62fe97f2013-10-09 15:37:36 +0000100 explicit FakeSSLIdentity(const FakeSSLCertificate& cert) : cert_(cert) {}
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000101 virtual FakeSSLIdentity* GetReference() const {
102 return new FakeSSLIdentity(*this);
103 }
104 virtual const FakeSSLCertificate& certificate() const { return cert_; }
105 private:
106 FakeSSLCertificate cert_;
107};
108
109} // namespace talk_base
110
111#endif // TALK_BASE_FAKESSLIDENTITY_H_