blob: 8b588852af388604ea7525f297a303263a1c06aa [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
27namespace rtc {
28
29// Singleton
30class 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
52class NSSStreamAdapter : public SSLStreamAdapterHelper {
53 public:
54 explicit NSSStreamAdapter(StreamInterface* stream);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000055 ~NSSStreamAdapter() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000056 bool Init();
57
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000058 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.orgf0488722014-05-13 18:00:26 +000067
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000068 bool GetSslCipher(std::string* cipher) override;
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +000069
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070 // Key Extractor interface
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000071 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.orgf0488722014-05-13 18:00:26 +000077
78 // DTLS-SRTP interface
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000079 bool SetDtlsSrtpCiphers(const std::vector<std::string>& ciphers) override;
80 bool GetDtlsSrtpCipher(std::string* cipher) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081
82 // Capabilities interfaces
83 static bool HaveDtls();
84 static bool HaveDtlsSrtp();
85 static bool HaveExporter();
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +000086 static std::string GetDefaultSslCipher();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087
88 protected:
89 // Override SSLStreamAdapter
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000090 void OnEvent(StreamInterface* stream, int events, int err) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091
92 // Override SSLStreamAdapterHelper
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000093 int BeginSSL() override;
94 void Cleanup() override;
95 bool GetDigestLength(const std::string& algorithm, size_t* length) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096
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_