henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_BASE_NSSSTREAMADAPTER_H_ |
| 12 | #define WEBRTC_BASE_NSSSTREAMADAPTER_H_ |
| 13 | |
| 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "nspr.h" |
| 18 | #include "nss.h" |
| 19 | #include "secmodt.h" |
| 20 | |
| 21 | #include "webrtc/base/buffer.h" |
| 22 | #include "webrtc/base/nssidentity.h" |
| 23 | #include "webrtc/base/ssladapter.h" |
| 24 | #include "webrtc/base/sslstreamadapter.h" |
| 25 | #include "webrtc/base/sslstreamadapterhelper.h" |
| 26 | |
| 27 | namespace rtc { |
| 28 | |
| 29 | // Singleton |
| 30 | class NSSContext { |
| 31 | public: |
| 32 | NSSContext() {} |
| 33 | ~NSSContext() { |
| 34 | } |
| 35 | |
| 36 | static PK11SlotInfo *GetSlot() { |
| 37 | return Instance() ? Instance()->slot_: NULL; |
| 38 | } |
| 39 | |
| 40 | static NSSContext *Instance(); |
| 41 | static bool InitializeSSL(VerificationCallback callback); |
| 42 | static bool InitializeSSLThread(); |
| 43 | static bool CleanupSSL(); |
| 44 | |
| 45 | private: |
| 46 | PK11SlotInfo *slot_; // The PKCS-11 slot |
| 47 | static bool initialized; // Was this initialized? |
| 48 | static NSSContext *global_nss_context; // The global context |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | class NSSStreamAdapter : public SSLStreamAdapterHelper { |
| 53 | public: |
| 54 | explicit NSSStreamAdapter(StreamInterface* stream); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame^] | 55 | ~NSSStreamAdapter() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | bool Init(); |
| 57 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame^] | 58 | StreamResult Read(void* data, |
| 59 | size_t data_len, |
| 60 | size_t* read, |
| 61 | int* error) override; |
| 62 | StreamResult Write(const void* data, |
| 63 | size_t data_len, |
| 64 | size_t* written, |
| 65 | int* error) override; |
| 66 | void OnMessage(Message* msg) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame^] | 68 | bool GetSslCipher(std::string* cipher) override; |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 69 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | // Key Extractor interface |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame^] | 71 | bool ExportKeyingMaterial(const std::string& label, |
| 72 | const uint8* context, |
| 73 | size_t context_len, |
| 74 | bool use_context, |
| 75 | uint8* result, |
| 76 | size_t result_len) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 77 | |
| 78 | // DTLS-SRTP interface |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame^] | 79 | bool SetDtlsSrtpCiphers(const std::vector<std::string>& ciphers) override; |
| 80 | bool GetDtlsSrtpCipher(std::string* cipher) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 81 | |
| 82 | // Capabilities interfaces |
| 83 | static bool HaveDtls(); |
| 84 | static bool HaveDtlsSrtp(); |
| 85 | static bool HaveExporter(); |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 86 | static std::string GetDefaultSslCipher(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 87 | |
| 88 | protected: |
| 89 | // Override SSLStreamAdapter |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame^] | 90 | void OnEvent(StreamInterface* stream, int events, int err) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 91 | |
| 92 | // Override SSLStreamAdapterHelper |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame^] | 93 | int BeginSSL() override; |
| 94 | void Cleanup() override; |
| 95 | bool GetDigestLength(const std::string& algorithm, size_t* length) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 96 | |
| 97 | private: |
| 98 | int ContinueSSL(); |
| 99 | static SECStatus AuthCertificateHook(void *arg, PRFileDesc *fd, |
| 100 | PRBool checksig, PRBool isServer); |
| 101 | static SECStatus GetClientAuthDataHook(void *arg, PRFileDesc *fd, |
| 102 | CERTDistNames *caNames, |
| 103 | CERTCertificate **pRetCert, |
| 104 | SECKEYPrivateKey **pRetKey); |
| 105 | |
| 106 | PRFileDesc *ssl_fd_; // NSS's SSL file descriptor |
| 107 | static bool initialized; // Was InitializeSSL() called? |
| 108 | bool cert_ok_; // Did we get and check a cert |
| 109 | std::vector<PRUint16> srtp_ciphers_; // SRTP cipher list |
| 110 | |
| 111 | static PRDescIdentity nspr_layer_identity; // The NSPR layer identity |
| 112 | }; |
| 113 | |
| 114 | } // namespace rtc |
| 115 | |
| 116 | #endif // WEBRTC_BASE_NSSSTREAMADAPTER_H_ |