2012-10-02 20:04:58 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
// Original author: ekr@rtfm.com
|
|
|
|
|
|
|
|
#ifndef transportlayerdtls_h__
|
|
|
|
#define transportlayerdtls_h__
|
|
|
|
|
|
|
|
#include <queue>
|
2015-04-01 18:21:06 +00:00
|
|
|
#include <set>
|
2012-10-02 20:04:58 +00:00
|
|
|
|
|
|
|
#include "sigslot.h"
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
#include "mozilla/RefPtr.h"
|
2016-03-02 20:28:27 +00:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2012-10-02 20:04:58 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIEventTarget.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "ScopedNSSTypes.h"
|
|
|
|
#include "m_cpp_utils.h"
|
|
|
|
#include "dtlsidentity.h"
|
|
|
|
#include "transportflow.h"
|
|
|
|
#include "transportlayer.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
struct Packet;
|
|
|
|
|
|
|
|
class TransportLayerNSPRAdapter {
|
|
|
|
public:
|
2014-08-15 21:41:29 +00:00
|
|
|
explicit TransportLayerNSPRAdapter(TransportLayer *output) :
|
2012-10-02 20:04:58 +00:00
|
|
|
output_(output),
|
2015-07-15 19:23:10 +00:00
|
|
|
input_(),
|
|
|
|
enabled_(true) {}
|
2012-10-02 20:04:58 +00:00
|
|
|
|
|
|
|
void PacketReceived(const void *data, int32_t len);
|
2014-08-04 19:35:00 +00:00
|
|
|
int32_t Recv(void *buf, int32_t buflen);
|
2012-10-02 20:04:58 +00:00
|
|
|
int32_t Write(const void *buf, int32_t length);
|
2015-07-15 19:23:10 +00:00
|
|
|
void SetEnabled(bool enabled) { enabled_ = enabled; }
|
2012-10-02 20:04:58 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_ASSIGN(TransportLayerNSPRAdapter);
|
|
|
|
|
|
|
|
TransportLayer *output_;
|
|
|
|
std::queue<Packet *> input_;
|
2015-07-15 19:23:10 +00:00
|
|
|
bool enabled_;
|
2012-10-02 20:04:58 +00:00
|
|
|
};
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class TransportLayerDtls final : public TransportLayer {
|
2012-10-02 20:04:58 +00:00
|
|
|
public:
|
|
|
|
TransportLayerDtls() :
|
|
|
|
role_(CLIENT),
|
|
|
|
verification_mode_(VERIFY_UNSET),
|
|
|
|
ssl_fd_(nullptr),
|
|
|
|
auth_hook_called_(false),
|
|
|
|
cert_ok_(false) {}
|
|
|
|
|
|
|
|
virtual ~TransportLayerDtls();
|
|
|
|
|
|
|
|
enum Role { CLIENT, SERVER};
|
|
|
|
enum Verification { VERIFY_UNSET, VERIFY_ALLOW_ALL, VERIFY_DIGEST};
|
2013-01-29 17:01:10 +00:00
|
|
|
const static size_t kMaxDigestLength = HASH_LENGTH_MAX;
|
2012-10-02 20:04:58 +00:00
|
|
|
|
|
|
|
// DTLS-specific operations
|
|
|
|
void SetRole(Role role) { role_ = role;}
|
|
|
|
Role role() { return role_; }
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
void SetIdentity(const RefPtr<DtlsIdentity>& identity) {
|
2012-10-02 20:04:58 +00:00
|
|
|
identity_ = identity;
|
|
|
|
}
|
2015-04-01 18:21:06 +00:00
|
|
|
nsresult SetAlpn(const std::set<std::string>& allowedAlpn,
|
|
|
|
const std::string& alpnDefault);
|
|
|
|
const std::string& GetNegotiatedAlpn() const { return alpn_; }
|
|
|
|
|
2012-10-02 20:04:58 +00:00
|
|
|
nsresult SetVerificationAllowAll();
|
|
|
|
nsresult SetVerificationDigest(const std::string digest_algorithm,
|
|
|
|
const unsigned char *digest_value,
|
|
|
|
size_t digest_len);
|
|
|
|
|
2014-08-04 15:49:00 +00:00
|
|
|
nsresult GetCipherSuite(uint16_t* cipherSuite) const;
|
|
|
|
|
2012-10-02 20:04:58 +00:00
|
|
|
nsresult SetSrtpCiphers(std::vector<uint16_t> ciphers);
|
2014-08-04 15:49:00 +00:00
|
|
|
nsresult GetSrtpCipher(uint16_t *cipher) const;
|
2012-10-02 20:04:58 +00:00
|
|
|
|
|
|
|
nsresult ExportKeyingMaterial(const std::string& label,
|
|
|
|
bool use_context,
|
|
|
|
const std::string& context,
|
|
|
|
unsigned char *out,
|
|
|
|
unsigned int outlen);
|
|
|
|
|
|
|
|
const CERTCertificate *GetPeerCert() const {
|
|
|
|
return peer_cert_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transport layer overrides.
|
|
|
|
virtual nsresult InitInternal();
|
|
|
|
virtual void WasInserted();
|
|
|
|
virtual TransportResult SendPacket(const unsigned char *data, size_t len);
|
|
|
|
|
|
|
|
// Signals
|
|
|
|
void StateChange(TransportLayer *layer, State state);
|
|
|
|
void PacketReceived(TransportLayer* layer, const unsigned char *data,
|
|
|
|
size_t len);
|
|
|
|
|
2014-08-04 15:50:00 +00:00
|
|
|
// For testing use only. Returns the fd.
|
|
|
|
PRFileDesc* internal_fd() { CheckThread(); return ssl_fd_.rwget(); }
|
|
|
|
|
2012-10-02 20:04:58 +00:00
|
|
|
TRANSPORT_LAYER_ID("dtls")
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_ASSIGN(TransportLayerDtls);
|
|
|
|
|
|
|
|
// A single digest to check
|
|
|
|
class VerificationDigest {
|
|
|
|
public:
|
|
|
|
VerificationDigest(std::string algorithm,
|
|
|
|
const unsigned char *value, size_t len) {
|
|
|
|
MOZ_ASSERT(len <= sizeof(value_));
|
|
|
|
|
|
|
|
algorithm_ = algorithm;
|
|
|
|
memcpy(value_, value, len);
|
|
|
|
len_ = len;
|
|
|
|
}
|
|
|
|
|
2012-10-26 17:49:57 +00:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VerificationDigest)
|
2012-10-02 20:04:58 +00:00
|
|
|
|
|
|
|
std::string algorithm_;
|
|
|
|
size_t len_;
|
|
|
|
unsigned char value_[kMaxDigestLength];
|
|
|
|
|
|
|
|
private:
|
2014-06-20 11:08:24 +00:00
|
|
|
~VerificationDigest() {}
|
2012-10-02 20:04:58 +00:00
|
|
|
DISALLOW_COPY_ASSIGN(VerificationDigest);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool Setup();
|
2014-08-04 15:49:00 +00:00
|
|
|
bool SetupCipherSuites(PRFileDesc* ssl_fd) const;
|
2015-04-01 18:21:06 +00:00
|
|
|
bool SetupAlpn(PRFileDesc* ssl_fd) const;
|
2012-10-02 20:04:58 +00:00
|
|
|
void Handshake();
|
|
|
|
|
2015-04-01 18:21:06 +00:00
|
|
|
bool CheckAlpn();
|
|
|
|
|
2012-10-02 20:04:58 +00:00
|
|
|
static SECStatus GetClientAuthDataHook(void *arg, PRFileDesc *fd,
|
|
|
|
CERTDistNames *caNames,
|
|
|
|
CERTCertificate **pRetCert,
|
|
|
|
SECKEYPrivateKey **pRetKey);
|
|
|
|
static SECStatus AuthCertificateHook(void *arg,
|
|
|
|
PRFileDesc *fd,
|
|
|
|
PRBool checksig,
|
|
|
|
PRBool isServer);
|
|
|
|
SECStatus AuthCertificateHook(PRFileDesc *fd,
|
|
|
|
PRBool checksig,
|
|
|
|
PRBool isServer);
|
|
|
|
|
|
|
|
static void TimerCallback(nsITimer *timer, void *arg);
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
SECStatus CheckDigest(const RefPtr<VerificationDigest>& digest,
|
2012-10-02 20:04:58 +00:00
|
|
|
CERTCertificate *cert);
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DtlsIdentity> identity_;
|
2015-04-01 18:21:06 +00:00
|
|
|
// What ALPN identifiers are permitted.
|
|
|
|
std::set<std::string> alpn_allowed_;
|
|
|
|
// What ALPN identifier is used if ALPN is not supported.
|
|
|
|
// The empty string indicates that ALPN is required.
|
|
|
|
std::string alpn_default_;
|
|
|
|
// What ALPN string was negotiated.
|
|
|
|
std::string alpn_;
|
2012-10-02 20:04:58 +00:00
|
|
|
std::vector<uint16_t> srtp_ciphers_;
|
|
|
|
|
|
|
|
Role role_;
|
|
|
|
Verification verification_mode_;
|
2015-10-18 05:24:48 +00:00
|
|
|
std::vector<RefPtr<VerificationDigest> > digests_;
|
2012-10-02 20:04:58 +00:00
|
|
|
|
|
|
|
// Must delete nspr_io_adapter after ssl_fd_ b/c ssl_fd_ causes an alert
|
|
|
|
// (ssl_fd_ contains an un-owning pointer to nspr_io_adapter_)
|
2016-03-02 20:28:27 +00:00
|
|
|
UniquePtr<TransportLayerNSPRAdapter> nspr_io_adapter_;
|
2012-10-02 20:04:58 +00:00
|
|
|
ScopedPRFileDesc ssl_fd_;
|
|
|
|
|
|
|
|
ScopedCERTCertificate peer_cert_;
|
|
|
|
nsCOMPtr<nsITimer> timer_;
|
|
|
|
bool auth_hook_called_;
|
|
|
|
bool cert_ok_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // close namespace
|
|
|
|
#endif
|