1 /* 2 * libjingle 3 * Copyright 2013 Google Inc. 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_APP_WEBRTC_TEST_FAKEDTLSIDENTITYSERVICE_H_ 29 #define TALK_APP_WEBRTC_TEST_FAKEDTLSIDENTITYSERVICE_H_ 30 31 #include <string> 32 #include <utility> 33 34 #include "talk/app/webrtc/dtlsidentitystore.h" 35 #include "talk/app/webrtc/peerconnectioninterface.h" 36 #include "webrtc/base/rtccertificate.h" 37 38 static const struct { 39 const char* rsa_private_key_pem; 40 const char* cert_pem; 41 } kKeysAndCerts[] =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ABo1AwTjAdBgNV\n" 93 "HQ4EFgQUx2tbJdlcSTCepn09UdYORXKuSTAwHwYDVR0jBBgwFoAUx2tbJdlcSTCe\n" 94 "pn09UdYORXKuSTAwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOBgQAmp9Id\n" 95 "E716gHMqeBG4S2FCgVFCr0a0ugkaneQAN/c2L9CbMemEN9W6jvucUIVOtYd90dDW\n" 96 "lXuowWmT/JctPe3D2qt4yvYW3puECHk2tVQmrJOZiZiTRtWm6HxkmoUYHYp/DtaS\n" 97 "1Xe29gSTnZtI5sQCrGMzk3SGRSSs7ejLKiVDBQ==\n" 98 "-----END CERTIFICATE-----\n"}}; 99 100 class FakeDtlsIdentityStore : public webrtc::DtlsIdentityStoreInterface, 101 public rtc::MessageHandler { 102 public: 103 typedef rtc::TypedMessageData<rtc::scoped_refptr< 104 webrtc::DtlsIdentityRequestObserver> > MessageData; 105 106 FakeDtlsIdentityStore() : should_fail_(false) {} 107 108 void set_should_fail(bool should_fail) { 109 should_fail_ = should_fail; 110 } 111 112 void use_original_key() { key_index_ = 0; } 113 void use_alternate_key() { key_index_ = 1; } 114 115 void RequestIdentity( 116 rtc::KeyType key_type, 117 const rtc::scoped_refptr<webrtc::DtlsIdentityRequestObserver>& 118 observer) override { 119 // TODO(hbos): Should be able to generate KT_ECDSA too. 120 RTC_DCHECK(key_type == rtc::KT_RSA || should_fail_); 121 MessageData* msg = new MessageData( 122 rtc::scoped_refptr<webrtc::DtlsIdentityRequestObserver>(observer)); 123 rtc::Thread::Current()->Post( 124 this, should_fail_ ? MSG_FAILURE : MSG_SUCCESS, msg); 125 } 126 127 static rtc::scoped_refptr<rtc::RTCCertificate> GenerateCertificate() { 128 std::string cert; 129 std::string key; 130 rtc::SSLIdentity::PemToDer("CERTIFICATE", kKeysAndCerts[0].cert_pem, &cert); 131 rtc::SSLIdentity::PemToDer("RSA PRIVATE KEY", 132 kKeysAndCerts[0].rsa_private_key_pem, &key); 133 134 std::string pem_cert = rtc::SSLIdentity::DerToPem( 135 rtc::kPemTypeCertificate, 136 reinterpret_cast<const unsigned char*>(cert.data()), 137 cert.length()); 138 std::string pem_key = rtc::SSLIdentity::DerToPem( 139 rtc::kPemTypeRsaPrivateKey, 140 reinterpret_cast<const unsigned char*>(key.data()), 141 key.length()); 142 rtc::scoped_ptr<rtc::SSLIdentity> identity( 143 rtc::SSLIdentity::FromPEMStrings(pem_key, pem_cert)); 144 145 return rtc::RTCCertificate::Create(std::move(identity)); 146 } 147 148 private: 149 enum { 150 MSG_SUCCESS, 151 MSG_FAILURE, 152 }; 153 154 const char* get_key() { 155 return kKeysAndCerts[key_index_].rsa_private_key_pem; 156 } 157 const char* get_cert() { return kKeysAndCerts[key_index_].cert_pem; } 158 159 // rtc::MessageHandler implementation. 160 void OnMessage(rtc::Message* msg) { 161 MessageData* message_data = static_cast<MessageData*>(msg->pdata); 162 rtc::scoped_refptr<webrtc::DtlsIdentityRequestObserver> observer = 163 message_data->data(); 164 switch (msg->message_id) { 165 case MSG_SUCCESS: { 166 std::string cert; 167 std::string key; 168 rtc::SSLIdentity::PemToDer("CERTIFICATE", get_cert(), &cert); 169 rtc::SSLIdentity::PemToDer("RSA PRIVATE KEY", get_key(), &key); 170 observer->OnSuccess(cert, key); 171 break; 172 } 173 case MSG_FAILURE: 174 observer->OnFailure(0); 175 break; 176 } 177 delete message_data; 178 } 179 180 bool should_fail_; 181 int key_index_ = 0; 182 }; 183 184 #endif // TALK_APP_WEBRTC_TEST_FAKEDTLSIDENTITYSERVICE_H_ 185