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 29 #ifndef TALK_APP_WEBRTC_TEST_FAKEDTLSIDENTITYSERVICE_H_ 30 #define TALK_APP_WEBRTC_TEST_FAKEDTLSIDENTITYSERVICE_H_ 31 32 #include "talk/app/webrtc/peerconnectioninterface.h" 33 34 static const char kRSA_PRIVATE_KEY_PEM[] = 35 "-----BEGIN RSA PRIVATE KEY-----\n" 36 "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMYRkbhmI7kVA/r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NDQ3WjARMQ8wDQYDVQQD\n" 56 "EwZXZWJSVEMwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMYRkbhmI7kVA/rM\n" 57 "czsZ+6JDhDvnkF+vn6yCAGuRPV03zuRqZtDy4N4to7PZu9PjqrRl7nDMXrG3YG9y\n" 58 "rlIAZ72KjcKKFAJxQyAKLCIdawKRyp8RdK3LEySWEZb0AV58IadqPZDTNHHRX8dz\n" 59 "5aTSMsbbkZ+C/OzTnbiMqLL/vg6jAgMBAAEwDQYJKoZIhvcNAQELBQADgYEAUflI\n" 60 "VUe5Krqf5RVa5C3u/UTAOAUJBiDS3VANTCLBxjuMsvqOG0WvaYWP3HYPgrz0jXK2\n" 61 "LJE/mGw3MyFHEqi81jh95J+ypl6xKW6Rm8jKLR87gUvCaVYn/Z4/P3AqcQTB7wOv\n" 62 "UD0A8qfhfDM+LK6rPAnCsVN0NRDY3jvd6rzix9M=\n" 63 "-----END CERTIFICATE-----\n"; 64 65 using webrtc::DTLSIdentityRequestObserver; 66 67 class FakeIdentityService : public webrtc::DTLSIdentityServiceInterface, 68 public rtc::MessageHandler { 69 public: 70 struct Request { 71 Request(const std::string& common_name, 72 DTLSIdentityRequestObserver* observer) 73 : common_name(common_name), observer(observer) {} 74 75 std::string common_name; 76 rtc::scoped_refptr<DTLSIdentityRequestObserver> observer; 77 }; 78 typedef rtc::TypedMessageData<Request> MessageData; 79 80 FakeIdentityService() : should_fail_(false) {} 81 82 void set_should_fail(bool should_fail) { 83 should_fail_ = should_fail; 84 } 85 86 // DTLSIdentityServiceInterface implemenation. 87 virtual bool RequestIdentity(const std::string& identity_name, 88 const std::string& common_name, 89 DTLSIdentityRequestObserver* observer) { 90 MessageData* msg = new MessageData(Request(common_name, observer)); 91 if (should_fail_) { 92 rtc::Thread::Current()->Post(this, MSG_FAILURE, msg); 93 } else { 94 rtc::Thread::Current()->Post(this, MSG_SUCCESS, msg); 95 } 96 return true; 97 } 98 99 private: 100 enum { 101 MSG_SUCCESS, 102 MSG_FAILURE, 103 }; 104 105 // rtc::MessageHandler implementation. 106 void OnMessage(rtc::Message* msg) { 107 FakeIdentityService::MessageData* message_data = 108 static_cast<FakeIdentityService::MessageData*>(msg->pdata); 109 DTLSIdentityRequestObserver* observer = message_data->data().observer.get(); 110 switch (msg->message_id) { 111 case MSG_SUCCESS: { 112 std::string cert, key; 113 GenerateIdentity(message_data->data().common_name, &cert, &key); 114 observer->OnSuccess(cert, key); 115 break; 116 } 117 case MSG_FAILURE: 118 observer->OnFailure(0); 119 break; 120 } 121 delete message_data; 122 } 123 124 void GenerateIdentity( 125 const std::string& common_name, 126 std::string* der_cert, 127 std::string* der_key) { 128 rtc::SSLIdentity::PemToDer("CERTIFICATE", kCERT_PEM, der_cert); 129 rtc::SSLIdentity::PemToDer("RSA PRIVATE KEY", 130 kRSA_PRIVATE_KEY_PEM, 131 der_key); 132 } 133 134 bool should_fail_; 135 }; 136 137 #endif // TALK_APP_WEBRTC_TEST_FAKEDTLSIDENTITYSERVICE_H_ 138