3 * Copyright 2004--2008, Google Inc.
4 * Copyright 2011, RTFM, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
29 #ifndef TALK_BASE_NSSSTREAMADAPTER_H_
30 #define TALK_BASE_NSSSTREAMADAPTER_H_
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"
54 static PK11SlotInfo *GetSlot() {
55 return Instance() ? Instance()->slot_: NULL;
58 static NSSContext *Instance();
59 static bool InitializeSSL(VerificationCallback callback);
60 static bool InitializeSSLThread();
61 static bool CleanupSSL();
64 PK11SlotInfo *slot_; // The PKCS-11 slot
65 static bool initialized; // Was this initialized?
66 static NSSContext *global_nss_context; // The global context
70 class NSSStreamAdapter : public SSLStreamAdapterHelper {
72 explicit NSSStreamAdapter(StreamInterface* stream);
73 virtual ~NSSStreamAdapter();
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);
82 // Key Extractor interface
83 virtual bool ExportKeyingMaterial(const std::string& label,
90 // DTLS-SRTP interface
91 virtual bool SetDtlsSrtpCiphers(const std::vector<std::string>& ciphers);
92 virtual bool GetDtlsSrtpCipher(std::string* cipher);
94 // Capabilities interfaces
95 static bool HaveDtls();
96 static bool HaveDtlsSrtp();
97 static bool HaveExporter();
100 // Override SSLStreamAdapter
101 virtual void OnEvent(StreamInterface* stream, int events, int err);
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);
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);
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
125 static PRDescIdentity nspr_layer_identity; // The NSPR layer identity
128 } // namespace talk_base
130 #endif // TALK_BASE_NSSSTREAMADAPTER_H_