1 /* 2 * libjingle 3 * Copyright 2004--2008, Google Inc. 4 * Copyright 2011, RTFM, Inc. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef TALK_BASE_NSSSTREAMADAPTER_H_ 30 #define TALK_BASE_NSSSTREAMADAPTER_H_ 31 32 #include <string> 33 #include <vector> 34 35 #include "nspr.h" 36 #include "nss.h" 37 #include "secmodt.h" 38 39 #include "talk/base/buffer.h" 40 #include "talk/base/nssidentity.h" 41 #include "talk/base/ssladapter.h" 42 #include "talk/base/sslstreamadapter.h" 43 #include "talk/base/sslstreamadapterhelper.h" 44 45 namespace talk_base { 46 47 // Singleton 48 class NSSContext { 49 public: 50 NSSContext() {} 51 ~NSSContext() { 52 } 53 54 static PK11SlotInfo *GetSlot() { 55 return Instance() ? Instance()->slot_: NULL; 56 } 57 58 static NSSContext *Instance(); 59 static bool InitializeSSL(VerificationCallback callback); 60 static bool InitializeSSLThread(); 61 static bool CleanupSSL(); 62 63 private: 64 PK11SlotInfo *slot_; // The PKCS-11 slot 65 static bool initialized; // Was this initialized? 66 static NSSContext *global_nss_context; // The global context 67 }; 68 69 70 class NSSStreamAdapter : public SSLStreamAdapterHelper { 71 public: 72 explicit NSSStreamAdapter(StreamInterface* stream); 73 virtual ~NSSStreamAdapter(); 74 bool Init(); 75 76 virtual StreamResult Read(void* data, size_t data_len, 77 size_t* read, int* error); 78 virtual StreamResult Write(const void* data, size_t data_len, 79 size_t* written, int* error); 80 void OnMessage(Message *msg); 81 82 // Key Extractor interface 83 virtual bool ExportKeyingMaterial(const std::string& label, 84 const uint8* context, 85 size_t context_len, 86 bool use_context, 87 uint8* result, 88 size_t result_len); 89 90 // DTLS-SRTP interface 91 virtual bool SetDtlsSrtpCiphers(const std::vector<std::string>& ciphers); 92 virtual bool GetDtlsSrtpCipher(std::string* cipher); 93 94 // Capabilities interfaces 95 static bool HaveDtls(); 96 static bool HaveDtlsSrtp(); 97 static bool HaveExporter(); 98 99 protected: 100 // Override SSLStreamAdapter 101 virtual void OnEvent(StreamInterface* stream, int events, int err); 102 103 // Override SSLStreamAdapterHelper 104 virtual int BeginSSL(); 105 virtual void Cleanup(); 106 virtual bool GetDigestLength(const std::string &algorithm, 107 std::size_t *length) { 108 return NSSCertificate::GetDigestLength(algorithm, length); 109 } 110 111 private: 112 int ContinueSSL(); 113 static SECStatus AuthCertificateHook(void *arg, PRFileDesc *fd, 114 PRBool checksig, PRBool isServer); 115 static SECStatus GetClientAuthDataHook(void *arg, PRFileDesc *fd, 116 CERTDistNames *caNames, 117 CERTCertificate **pRetCert, 118 SECKEYPrivateKey **pRetKey); 119 120 PRFileDesc *ssl_fd_; // NSS's SSL file descriptor 121 static bool initialized; // Was InitializeSSL() called? 122 bool cert_ok_; // Did we get and check a cert 123 std::vector<PRUint16> srtp_ciphers_; // SRTP cipher list 124 125 static PRDescIdentity nspr_layer_identity; // The NSPR layer identity 126 }; 127 128 } // namespace talk_base 129 130 #endif // TALK_BASE_NSSSTREAMADAPTER_H_ 131