summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/transport/transportlayersrtp.cpp
blob: 25830a2ddeffcab4b9b78788324506fba9b72299 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* -*- 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

#include "transportlayersrtp.h"
#include "transportlayerdtls.h"

#include "logging.h"
#include "nsError.h"
#include "mozilla/Assertions.h"

namespace mozilla {

MOZ_MTLOG_MODULE("mtransport")

static char kDTLSExporterLabel[] = "EXTRACTOR-dtls_srtp";

TransportLayerSrtp::TransportLayerSrtp(TransportLayerDtls& dtls) {
  // We need to connect to the dtls layer, not the ice layer, because even
  // though the packets that DTLS decrypts don't flow through us, we do base our
  // keying information on the keying information established by the DTLS layer.
  dtls.SignalStateChange.connect(this, &TransportLayerSrtp::StateChange);

  TL_SET_STATE(dtls.state());
}

void TransportLayerSrtp::WasInserted() {
  // Connect to the lower layers
  if (!Setup()) {
    TL_SET_STATE(TS_ERROR);
  }
}

bool TransportLayerSrtp::Setup() {
  CheckThread();
  if (!downward_) {
    MOZ_MTLOG(ML_ERROR, "SRTP layer with nothing below. This is useless");
    return false;
  }

  // downward_ is the TransportLayerIce
  downward_->SignalPacketReceived.connect(this,
                                          &TransportLayerSrtp::PacketReceived);

  return true;
}

TransportResult TransportLayerSrtp::SendPacket(MediaPacket& packet) {
  if (state() != TS_OPEN) {
    return TE_ERROR;
  }

  if (packet.len() < 4) {
    MOZ_ASSERT(false);
    return TE_ERROR;
  }

  MOZ_ASSERT(packet.capacity() - packet.len() >= SRTP_MAX_EXPANSION);

  int out_len;
  nsresult res;
  switch (packet.type()) {
    case MediaPacket::RTP:
      res = mSendSrtp->ProtectRtp(packet.data(), packet.len(),
                                  packet.capacity(), &out_len);
      packet.SetType(MediaPacket::SRTP);
      break;
    case MediaPacket::RTCP:
      res = mSendSrtp->ProtectRtcp(packet.data(), packet.len(),
                                   packet.capacity(), &out_len);
      packet.SetType(MediaPacket::SRTCP);
      break;
    default:
      MOZ_CRASH("SRTP layer asked to send packet that is neither RTP or RTCP");
  }

  if (NS_FAILED(res)) {
    MOZ_MTLOG(ML_ERROR,
              "Error protecting "
                  << (packet.type() == MediaPacket::RTP ? "RTP" : "RTCP")
                  << " len=" << packet.len() << "[" << std::hex
                  << packet.data()[0] << " " << packet.data()[1] << " "
                  << packet.data()[2] << " " << packet.data()[3] << "]");
    return TE_ERROR;
  }

  size_t unencrypted_len = packet.len();
  packet.SetLength(out_len);

  TransportResult bytes = downward_->SendPacket(packet);
  if (bytes == out_len) {
    // Whole packet was written, but the encrypted length might be different.
    // Don't confuse the caller.
    return unencrypted_len;
  }

  if (bytes == TE_WOULDBLOCK) {
    return TE_WOULDBLOCK;
  }

  return TE_ERROR;
}

void TransportLayerSrtp::StateChange(TransportLayer* layer, State state) {
  if (state == TS_OPEN && !mSendSrtp) {
    TransportLayerDtls* dtls = static_cast<TransportLayerDtls*>(layer);
    MOZ_ASSERT(dtls);  // DTLS is mandatory

    uint16_t cipher_suite;
    nsresult res = dtls->GetSrtpCipher(&cipher_suite);
    if (NS_FAILED(res)) {
      MOZ_MTLOG(ML_DEBUG, "DTLS-SRTP disabled");
      TL_SET_STATE(TS_ERROR);
      return;
    }

    unsigned int key_size = SrtpFlow::KeySize(cipher_suite);
    unsigned int salt_size = SrtpFlow::SaltSize(cipher_suite);
    unsigned int master_key_size = key_size + salt_size;
    MOZ_ASSERT(master_key_size <= SRTP_MAX_KEY_LENGTH);

    // SRTP Key Exporter as per RFC 5764 S 4.2
    unsigned char srtp_block[SRTP_MAX_KEY_LENGTH * 2];
    res = dtls->ExportKeyingMaterial(kDTLSExporterLabel, false, "", srtp_block,
                                     sizeof(srtp_block));
    if (NS_FAILED(res)) {
      MOZ_MTLOG(ML_ERROR, "Failed to compute DTLS-SRTP keys. This is an error");
      TL_SET_STATE(TS_ERROR);
      return;
    }

    // Slice and dice as per RFC 5764 S 4.2
    unsigned char client_write_key[SRTP_MAX_KEY_LENGTH];
    unsigned char server_write_key[SRTP_MAX_KEY_LENGTH];
    unsigned int offset = 0;
    memcpy(client_write_key, srtp_block + offset, key_size);
    offset += key_size;
    memcpy(server_write_key, srtp_block + offset, key_size);
    offset += key_size;
    memcpy(client_write_key + key_size, srtp_block + offset, salt_size);
    offset += salt_size;
    memcpy(server_write_key + key_size, srtp_block + offset, salt_size);
    MOZ_ASSERT((offset + salt_size) == (2 * master_key_size));

    unsigned char* write_key;
    unsigned char* read_key;

    if (dtls->role() == TransportLayerDtls::CLIENT) {
      write_key = client_write_key;
      read_key = server_write_key;
    } else {
      write_key = server_write_key;
      read_key = client_write_key;
    }

    MOZ_ASSERT(!mSendSrtp && !mRecvSrtp);
    mSendSrtp =
        SrtpFlow::Create(cipher_suite, false, write_key, master_key_size);
    mRecvSrtp = SrtpFlow::Create(cipher_suite, true, read_key, master_key_size);
    if (!mSendSrtp || !mRecvSrtp) {
      MOZ_MTLOG(ML_ERROR, "Couldn't create SRTP flow.");
      TL_SET_STATE(TS_ERROR);
      return;
    }

    MOZ_MTLOG(ML_INFO, "Created SRTP flow!");
  }

  TL_SET_STATE(state);
}

void TransportLayerSrtp::PacketReceived(TransportLayer* layer,
                                        MediaPacket& packet) {
  if (state() != TS_OPEN) {
    return;
  }

  if (!packet.data()) {
    // Something ate this, probably the DTLS layer
    return;
  }

  if (packet.type() != MediaPacket::SRTP &&
      packet.type() != MediaPacket::SRTCP) {
    return;
  }

  // We want to keep the encrypted packet around for packet dumping
  packet.CopyDataToEncrypted();
  int outLen;
  nsresult res;

  if (packet.type() == MediaPacket::SRTP) {
    packet.SetType(MediaPacket::RTP);
    res = mRecvSrtp->UnprotectRtp(packet.data(), packet.len(), packet.len(),
                                  &outLen);
  } else {
    packet.SetType(MediaPacket::RTCP);
    res = mRecvSrtp->UnprotectRtcp(packet.data(), packet.len(), packet.len(),
                                   &outLen);
  }

  if (NS_SUCCEEDED(res)) {
    packet.SetLength(outLen);
    SignalPacketReceived(this, packet);
  } else {
    // TODO: What do we do wrt packet dumping here? Maybe signal an empty
    // packet? Signal the still-encrypted packet?
    MOZ_MTLOG(ML_ERROR,
              "Error unprotecting "
                  << (packet.type() == MediaPacket::RTP ? "RTP" : "RTCP")
                  << " len=" << packet.len() << "[" << std::hex
                  << packet.data()[0] << " " << packet.data()[1] << " "
                  << packet.data()[2] << " " << packet.data()[3] << "]");
  }
}

}  // namespace mozilla