blob: afa2eb690fa661f85c096a6b89849c3b3919b5ea [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);
55 virtual ~NSSStreamAdapter();
56 bool Init();
57
58 virtual StreamResult Read(void* data, size_t data_len,
59 size_t* read, int* error);
60 virtual StreamResult Write(const void* data, size_t data_len,
61 size_t* written, int* error);
62 void OnMessage(Message *msg);
63
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +000064 virtual bool GetSslCipher(std::string* cipher);
65
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066 // Key Extractor interface
67 virtual bool ExportKeyingMaterial(const std::string& label,
68 const uint8* context,
69 size_t context_len,
70 bool use_context,
71 uint8* result,
72 size_t result_len);
73
74 // DTLS-SRTP interface
75 virtual bool SetDtlsSrtpCiphers(const std::vector<std::string>& ciphers);
76 virtual bool GetDtlsSrtpCipher(std::string* cipher);
77
78 // Capabilities interfaces
79 static bool HaveDtls();
80 static bool HaveDtlsSrtp();
81 static bool HaveExporter();
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +000082 static std::string GetDefaultSslCipher();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083
84 protected:
85 // Override SSLStreamAdapter
86 virtual void OnEvent(StreamInterface* stream, int events, int err);
87
88 // Override SSLStreamAdapterHelper
89 virtual int BeginSSL();
90 virtual void Cleanup();
91 virtual bool GetDigestLength(const std::string& algorithm, size_t* length) {
92 return NSSCertificate::GetDigestLength(algorithm, length);
93 }
94
95 private:
96 int ContinueSSL();
97 static SECStatus AuthCertificateHook(void *arg, PRFileDesc *fd,
98 PRBool checksig, PRBool isServer);
99 static SECStatus GetClientAuthDataHook(void *arg, PRFileDesc *fd,
100 CERTDistNames *caNames,
101 CERTCertificate **pRetCert,
102 SECKEYPrivateKey **pRetKey);
103
104 PRFileDesc *ssl_fd_; // NSS's SSL file descriptor
105 static bool initialized; // Was InitializeSSL() called?
106 bool cert_ok_; // Did we get and check a cert
107 std::vector<PRUint16> srtp_ciphers_; // SRTP cipher list
108
109 static PRDescIdentity nspr_layer_identity; // The NSPR layer identity
110};
111
112} // namespace rtc
113
114#endif // WEBRTC_BASE_NSSSTREAMADAPTER_H_