henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | |
wu@webrtc.org | 62fe97f | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 31 | #include <algorithm> |
| 32 | #include <vector> |
| 33 | |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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: |
wu@webrtc.org | 8a77f5b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 41 | // 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.org | 62fe97f | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 45 | explicit FakeSSLCertificate(const std::vector<std::string>& certs) |
wu@webrtc.org | 8a77f5b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 46 | : data_(certs.front()), digest_algorithm_(DIGEST_SHA_1) { |
wu@webrtc.org | 62fe97f | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 47 | 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.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 53 | virtual FakeSSLCertificate* GetReference() const { |
| 54 | return new FakeSSLCertificate(*this); |
| 55 | } |
| 56 | virtual std::string ToPEMString() const { |
| 57 | return data_; |
| 58 | } |
wu@webrtc.org | 62fe97f | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 59 | 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.org | 8a77f5b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 64 | void set_digest_algorithm(const std::string& algorithm) { |
| 65 | digest_algorithm_ = algorithm; |
| 66 | } |
mallinath@webrtc.org | 8841d7b | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 67 | virtual bool GetSignatureDigestAlgorithm(std::string* algorithm) const { |
wu@webrtc.org | 8a77f5b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 68 | *algorithm = digest_algorithm_; |
mallinath@webrtc.org | 8841d7b | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 69 | return true; |
| 70 | } |
pbos@webrtc.org | b951827 | 2014-03-07 15:22:04 +0000 | [diff] [blame] | 71 | virtual bool ComputeDigest(const std::string& algorithm, |
| 72 | unsigned char* digest, |
| 73 | size_t size, |
| 74 | size_t* length) const { |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | *length = talk_base::ComputeDigest(algorithm, data_.c_str(), data_.size(), |
| 76 | digest, size); |
| 77 | return (*length != 0); |
| 78 | } |
wu@webrtc.org | 62fe97f | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 79 | 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.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | private: |
wu@webrtc.org | 62fe97f | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 89 | static FakeSSLCertificate* DupCert(FakeSSLCertificate cert) { |
| 90 | return cert.GetReference(); |
| 91 | } |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | std::string data_; |
wu@webrtc.org | 62fe97f | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 93 | std::vector<FakeSSLCertificate> certs_; |
wu@webrtc.org | 8a77f5b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 94 | std::string digest_algorithm_; |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | class FakeSSLIdentity : public talk_base::SSLIdentity { |
| 98 | public: |
| 99 | explicit FakeSSLIdentity(const std::string& data) : cert_(data) {} |
wu@webrtc.org | 62fe97f | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 100 | explicit FakeSSLIdentity(const FakeSSLCertificate& cert) : cert_(cert) {} |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 101 | 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_ |