rtpsecuresession.cpp
7.09 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
This file is a part of JRTPLIB
Copyright (c) 1999-2017 Jori Liesenborgs
Contact: jori.liesenborgs@gmail.com
This library was developed at the Expertise Centre for Digital Media
(http://www.edm.uhasselt.be), a research center of the Hasselt University
(http://www.uhasselt.be). The library is based upon work done for
my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include "rtpsecuresession.h"
#if defined(RTP_SUPPORT_SRTP) || defined(RTP_SUPPORT_SRTP2)
#include "rtprawpacket.h"
#ifdef RTP_SUPPORT_THREAD
#include <jthread/jmutexautolock.h>
#endif
#ifdef RTP_SUPPORT_SRTP2
#include <srtp2/srtp.h>
#define err_status_t srtp_err_status_t
#define err_status_ok srtp_err_status_ok
#else
#include <srtp/srtp.h>
#endif
#include <iostream>
#include <vector>
using namespace std;
#ifdef RTP_SUPPORT_THREAD
using namespace jthread;
#endif
namespace jrtplib
{
// SRTP library needs to be initialized already!
RTPSecureSession::RTPSecureSession(RTPRandom *rnd, RTPMemoryManager *mgr) : RTPSession(rnd, mgr)
{
// Make sure the OnChange... functions will be called
SetChangeIncomingData(true);
SetChangeOutgoingData(true);
m_pSRTPContext = 0;
m_lastSRTPError = 0;
}
RTPSecureSession::~RTPSecureSession()
{
if (m_pSRTPContext)
srtp_dealloc(m_pSRTPContext);
}
int RTPSecureSession::InitializeSRTPContext()
{
#ifdef RTP_SUPPORT_THREAD
if (!m_srtpLock.IsInitialized())
{
if (m_srtpLock.Init() < 0)
return ERR_RTP_SECURESESSION_CANTINITMUTEX;
}
JMutexAutoLock l(m_srtpLock);
#endif // RTP_SUPPORT_THREAD
if (m_pSRTPContext)
return ERR_RTP_SECURESESSION_CONTEXTALREADYINITIALIZED;
err_status_t result = srtp_create(&m_pSRTPContext, NULL);
if (result != err_status_ok)
{
m_pSRTPContext = 0;
m_lastSRTPError = (int)result;
return ERR_RTP_SECURESESSION_CANTINITIALIZE_SRTPCONTEXT;
}
return 0;
}
int RTPSecureSession::GetLastLibSRTPError()
{
#ifdef RTP_SUPPORT_THREAD
JMutexAutoLock l(m_srtpLock);
#endif // RTP_SUPPORT_THREAD
int err = m_lastSRTPError;
m_lastSRTPError = 0; // clear it
return err;
}
void RTPSecureSession::SetLastLibSRTPError(int err)
{
#ifdef RTP_SUPPORT_THREAD
JMutexAutoLock l(m_srtpLock);
#endif // RTP_SUPPORT_THREAD
m_lastSRTPError = err;
}
srtp_ctx_t *RTPSecureSession::LockSRTPContext()
{
#ifdef RTP_SUPPORT_THREAD
m_srtpLock.Lock();
#endif // RTP_SUPPORT_THREAD
if (!m_pSRTPContext)
{
#ifdef RTP_SUPPORT_THREAD
m_srtpLock.Unlock(); // Make sure the mutex is not locked on error
#endif // RTP_SUPPORT_THREAD
return 0;
}
return m_pSRTPContext;
}
int RTPSecureSession::UnlockSRTPContext()
{
if (!m_pSRTPContext)
return ERR_RTP_SECURESESSION_CONTEXTNOTINITIALIZED;
#ifdef RTP_SUPPORT_THREAD
m_srtpLock.Unlock();
#endif // RTP_SUPPORT_THREAD
return 0;
}
int RTPSecureSession::encryptData(uint8_t *pData, int &dataLength, bool rtp)
{
int length = dataLength;
if (rtp)
{
if (length < (int)sizeof(uint32_t)*3)
return ERR_RTP_SECURESESSION_NOTENOUGHDATATOENCRYPT ;
err_status_t result = srtp_protect(m_pSRTPContext, (void *)pData, &length);
if (result != err_status_ok)
{
m_lastSRTPError = (int)result;
return ERR_RTP_SECURESESSION_CANTENCRYPTRTPDATA;
}
}
else // rtcp
{
if (length < (int)sizeof(uint32_t)*2)
return ERR_RTP_SECURESESSION_NOTENOUGHDATATOENCRYPT;
err_status_t result = srtp_protect_rtcp(m_pSRTPContext, (void *)pData, &length);
if (result != err_status_ok)
{
m_lastSRTPError = (int)result;
return ERR_RTP_SECURESESSION_CANTENCRYPTRTCPDATA;
}
}
dataLength = length;
return 0;
}
int RTPSecureSession::OnChangeRTPOrRTCPData(const void *origdata, size_t origlen, bool isrtp, void **senddata, size_t *sendlen)
{
*senddata = 0;
if (!origdata || origlen == 0) // Nothing to do in this case, packet can be ignored
return 0;
srtp_ctx_t *pCtx = LockSRTPContext();
if (pCtx == 0)
return ERR_RTP_SECURESESSION_CONTEXTNOTINITIALIZED;
// Need to add some extra bytes, and we'll add a few more to be really safe
uint8_t *pDataCopy = RTPNew(GetMemoryManager(), RTPMEM_TYPE_BUFFER_SRTPDATA) uint8_t [origlen + SRTP_MAX_TRAILER_LEN + 32];
memcpy(pDataCopy, origdata, origlen);
int status = 0;
int dataLength = (int)origlen;
if ((status = encryptData(pDataCopy, dataLength, isrtp)) < 0)
{
UnlockSRTPContext();
RTPDeleteByteArray(pDataCopy, GetMemoryManager());
return status;
}
UnlockSRTPContext();
*senddata = pDataCopy;
*sendlen = dataLength;
return 0;
}
int RTPSecureSession::decryptRawPacket(RTPRawPacket *rawpack, int *srtpError)
{
*srtpError = 0;
uint8_t *pData = rawpack->GetData();
int dataLength = (int)rawpack->GetDataLength();
if (rawpack->IsRTP())
{
if (dataLength < (int)sizeof(uint32_t)*3)
return ERR_RTP_SECURESESSION_NOTENOUGHDATATODECRYPT;
err_status_t result = srtp_unprotect(m_pSRTPContext, (void*)pData, &dataLength);
if (result != err_status_ok)
{
*srtpError = result;
return ERR_RTP_SECURESESSION_CANTDECRYPTRTPDATA;
}
}
else // RTCP
{
if (dataLength < (int)sizeof(uint32_t)*2)
return ERR_RTP_SECURESESSION_NOTENOUGHDATATODECRYPT;
err_status_t result = srtp_unprotect_rtcp(m_pSRTPContext, (void *)pData, &dataLength);
if (result != err_status_ok)
{
*srtpError = result;
return ERR_RTP_SECURESESSION_CANTDECRYPTRTCPDATA;
}
}
rawpack->ZeroData(); // make sure we don't delete the data we're going to store
rawpack->SetData(pData, (size_t)dataLength);
return 0;
}
bool RTPSecureSession::OnChangeIncomingData(RTPRawPacket *rawpack)
{
if (!rawpack)
return false;
srtp_ctx_t *pCtx = LockSRTPContext();
if (pCtx == 0)
{
OnErrorChangeIncomingData(ERR_RTP_SECURESESSION_CONTEXTNOTINITIALIZED, 0);
return false;
}
int srtpErr = 0;
int status = decryptRawPacket(rawpack, &srtpErr);
UnlockSRTPContext();
if (status < 0)
{
OnErrorChangeIncomingData(status, srtpErr);
return false;
}
return true;
}
void RTPSecureSession::OnSentRTPOrRTCPData(void *senddata, size_t sendlen, bool isrtp)
{
JRTPLIB_UNUSED(sendlen);
JRTPLIB_UNUSED(isrtp);
if (senddata)
RTPDeleteByteArray((uint8_t *)senddata, GetMemoryManager());
}
} // end namespace
#endif // RTP_SUPPORT_SRTP || RTP_SUPPORT_SRTP2