summaryrefslogtreecommitdiff
path: root/src/srtp.cc
blob: 36f31484b156c62e756beaf3c971c2528fd06f2d (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
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
 *            srtp.cc
 *
 *  Thu Sep  5 10:46:10 CEST 2013
 *  Copyright 2013 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of lrtp.
 *
 * lrtp is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * lrtp is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with lrtp; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include "srtp.h"

#ifdef WIN32
#include <srtp.h>
#else
#include <srtp/srtp.h>
#endif

#include "asc2bin.h"

// This macro translates srtp status codes into exceptions:
#define SRTP_THROW(s)                                                   \
  do {                                                                  \
    switch(s) {                                                         \
    case err_status_fail: throw LRTP_SRTP_FAIL;                         \
    case err_status_bad_param: throw LRTP_SRTP_BAD_PARAM;               \
    case err_status_alloc_fail: throw LRTP_SRTP_ALLOC_FAIL;             \
    case err_status_dealloc_fail: throw LRTP_SRTP_DEALLOC_FAIL;         \
    case err_status_init_fail: throw LRTP_SRTP_INIT_FAIL;               \
    case err_status_terminus: throw LRTP_SRTP_TERMINUS;                 \
    case err_status_auth_fail: throw LRTP_SRTP_AUTH_FAIL;               \
    case err_status_cipher_fail: throw LRTP_SRTP_CIPHER_FAIL;           \
    case err_status_replay_fail: throw LRTP_SRTP_REPLAY_FAIL;           \
    case err_status_replay_old: throw LRTP_SRTP_REPLAY_OLD;             \
    case err_status_algo_fail: throw LRTP_SRTP_ALGO_FAIL;               \
    case err_status_no_such_op: throw LRTP_SRTP_NO_SUCH_OP;             \
    case err_status_no_ctx: throw LRTP_SRTP_NO_CTX;                     \
    case err_status_cant_check: throw LRTP_SRTP_CANT_CHECK;             \
    case err_status_key_expired: throw LRTP_SRTP_KEY_EXPIRED;           \
    case err_status_socket_err: throw LRTP_SRTP_SOCKET_ERR;             \
    case err_status_signal_err: throw LRTP_SRTP_SIGNAL_ERR;             \
    case err_status_nonce_bad: throw LRTP_SRTP_NONCE_BAD;               \
    case err_status_read_fail: throw LRTP_SRTP_READ_FAIL;               \
    case err_status_write_fail: throw LRTP_SRTP_WRITE_FAIL;             \
    case err_status_parse_err: throw LRTP_SRTP_PARSE_ERR;               \
    case err_status_encode_err: throw LRTP_SRTP_ENCODE_ERR;             \
    case err_status_semaphore_err: throw LRTP_SRTP_SEMAPHORE_ERR;       \
    case err_status_pfkey_err: throw LRTP_SRTP_PFKEY_ERR;               \
    default: throw LRTP_SRTP_FAIL;                                      \
    }                                                                   \
  } while(0)

struct SRTP::prv {
  char *key;
  size_t key_len;

  unsigned int ssrc;

  srtp_t session;
  srtp_policy_t policy;
};

SRTP::SRTP(std::string key, unsigned int ssrc)
  _throw(enum lrtp_status_t)
{
  prv = NULL;

  err_status_t status;

  prv = new struct prv;
  if(!prv) throw LRTP_OUT_OF_MEMORY;

  prv->ssrc = ssrc;

  setupKey(key);
  setupPolicy(true, true);

  status = srtp_create(&prv->session, &prv->policy);
  if(status != err_status_ok) SRTP_THROW(status);

  status = srtp_add_stream(prv->session, &prv->policy);
  if(status != err_status_ok) SRTP_THROW(status);
}

SRTP::~SRTP()
{
  err_status_t status = srtp_remove_stream(prv->session, htonl(prv->ssrc));
  if(status != err_status_ok) {
    // TODO: Error handling
    printf("srtp_remove_stream failed %d\n", status);
  }

  status = srtp_dealloc(prv->session);
  if(status != err_status_ok) {
    // TODO: Error handling
    printf("srtp_dealloc failed %d\n", status);
  }

  if(prv) {
    free(prv->key);
    delete prv;
  }
}

void SRTP::setupKey(const std::string &key)
  _throw(enum lrtp_status_t)
{
  prv->key = (char *)calloc(MASTER_KEY_LEN, 1);
  prv->key_len = MASTER_KEY_LEN;

  if(key.length() > MASTER_KEY_LEN * 2) printf("KeyTooLong\n"); // TODO

  // Read key from hexadecimal on command line into an octet string
  ssize_t len = asc2bin(prv->key, prv->key_len, key.c_str(), key.size());

  if(len == -1) printf("InvalidHexKeyString\n"); // TODO
  prv->key_len = len;

  // check that hex string is the right length.
  if(len < MASTER_KEY_LEN) printf("KeyTooShort\n"); // TODO
}

void SRTP::setupPolicy(bool confidentiality, bool authentication)
  _throw(enum lrtp_status_t)
{
#ifndef USE_CRYPTO
  confidentiality = authentication = false;
  printf("No crypto!\n");
#endif  

  /* 
   * create policy structure, using the default mechanisms but 
   * with only the security services requested on the command line,
   * using the right SSRC value
   */
  if(confidentiality && authentication) {
    crypto_policy_set_aes_cm_128_hmac_sha1_80(&prv->policy.rtp);
    prv->policy.rtp.sec_serv = sec_serv_conf_and_auth;
  } else if(confidentiality && !authentication) {
    crypto_policy_set_aes_cm_128_null_auth(&prv->policy.rtp);
    prv->policy.rtp.sec_serv = sec_serv_conf;
  } else if(!confidentiality && authentication) {
    crypto_policy_set_null_cipher_hmac_sha1_80(&prv->policy.rtp);
    prv->policy.rtp.sec_serv = sec_serv_auth;
  } else {
    /*
     * we're not providing security services, so set the policy to the
     * null policy
     *
     * Note that this policy does not conform to the SRTP
     * specification, since RTCP authentication is required.  However,
     * the effect of this policy is to turn off SRTP, so that this
     * application is now a vanilla-flavored RTP application.
     */
    prv->policy.rtp.cipher_type = NULL_CIPHER;
    prv->policy.rtp.cipher_key_len = 0; 
    prv->policy.rtp.auth_type = NULL_AUTH;
    prv->policy.rtp.auth_key_len = 0;
    prv->policy.rtp.auth_tag_len = 0;
    prv->policy.rtp.sec_serv = sec_serv_none;   
  }

  crypto_policy_set_rtcp_default(&prv->policy.rtcp);
  prv->policy.rtcp.sec_serv = sec_serv_none;  // No need for RTCP
  
  prv->policy.key = (uint8_t *)prv->key;
  prv->policy.ssrc.type = ssrc_specific;
  prv->policy.ssrc.value = prv->ssrc;
  prv->policy.next = NULL;
}

int SRTP::encrypt(char *packet, size_t size)
  _throw(enum lrtp_status_t)
{
  int sz = size;
  err_status_t status = srtp_protect(prv->session, packet, &sz);
  if(status != err_status_ok) {
    // TODO: throw SRTP::UnprotectException();
    printf("srtp_protect failed %d\n", status);
  }

  return sz;
}

int SRTP::decrypt(char *packet, size_t size)
  _throw(enum lrtp_status_t)
{
  int sz = size;
  err_status_t status = srtp_unprotect(prv->session, packet, &sz);
  switch(status) {
  case err_status_ok:
    // No errors.
    break;
  case err_status_replay_fail:
    // TODO: throw SRTP::ReplayException();// (replay check failed)
    printf("srtp_unprotect failed replay %d\n", status);
    sz = -1;
    break;
  case err_status_replay_old:
    // TODO: throw SRTP::ReplayOldException();// (replay check failed)
    printf("srtp_unprotect failed replay_old %d\n", status);
    sz = -1;
    break;
  case err_status_auth_fail:
    // TODO: throw SRTP::AuthCheckException();// (auth check failed)
    printf("srtp_unprotect failed auth %d\n", status);
    sz = -1;
    break;
  default:
    // TODO: throw SRTP::UnprotectException();
    printf("srtp_unprotect failed %d\n", status);
    sz = -1;
    break;
  }

  /*
	if(octets_recvd - RTP_HEADER_LEN > (ssize_t)size) {
    printf("BufferSize %d\n", status);
  }
  */

  // TODO: rtp.fromBuffer(packet, size);
  //memcpy(buf, prv->receiver->message.body, octets_recvd - RTP_HEADER_LEN);

  return sz;
}

// Global SRTP instance reference counter
static int active_srtp_instances = 0;

SRTP::SRTPInstance::SRTPInstance()
  _throw(enum lrtp_status_t)
{
  err_status_t status;

  if(active_srtp_instances == 0) {
    status = srtp_init();
    active_srtp_instances++;
    if(status != err_status_ok) SRTP_THROW(status);
  }
}

SRTP::SRTPInstance::~SRTPInstance()
  _throw(enum lrtp_status_t)
{
  err_status_t status;

  active_srtp_instances--;

  if(active_srtp_instances == 0) {
    status = srtp_shutdown();
    if(status != err_status_ok) SRTP_THROW(status);
  }
}