1 /* 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 31 #include <algorithm> 32 #include <vector> 33 34 #include "talk/base/messagedigest.h" 35 #include "talk/base/sslidentity.h" 36 37 namespace talk_base { 38 39 class FakeSSLCertificate : public talk_base::SSLCertificate { 40 public: 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; 45 // Skip certs[0]. 46 for (it = certs.begin() + 1; it != certs.end(); ++it) { 47 certs_.push_back(FakeSSLCertificate(*it)); 48 } 49 } 50 virtual FakeSSLCertificate* GetReference() const { 51 return new FakeSSLCertificate(*this); 52 } 53 virtual std::string ToPEMString() const { 54 return data_; 55 } 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()); 60 } 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; 65 return true; 66 } 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(), 71 digest, size); 72 return (*length != 0); 73 } 74 virtual bool GetChain(SSLCertChain** chain) const { 75 if (certs_.empty()) 76 return false; 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); 80 return true; 81 } 82 83 private: 84 static FakeSSLCertificate* DupCert(FakeSSLCertificate cert) { 85 return cert.GetReference(); 86 } 87 std::string data_; 88 std::vector<FakeSSLCertificate> certs_; 89 }; 90 91 class FakeSSLIdentity : public talk_base::SSLIdentity { 92 public: 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); 97 } 98 virtual const FakeSSLCertificate& certificate() const { return cert_; } 99 private: 100 FakeSSLCertificate cert_; 101 }; 102 103 } // namespace talk_base 104 105 #endif // TALK_BASE_FAKESSLIDENTITY_H_ 106