summaryrefslogtreecommitdiff
path: root/src/srtp.cc
blob: 72b36512ffdcd9076aa28c6f677575e308f84817 (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
/* -*- 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/crypto/include/datatypes.h>
#include <srtp/include/srtp.h>
#include <srtp/include/rtp_priv.h>
#else
#include <srtp/datatypes.h>
#include <srtp/srtp.h>
#include <srtp/rtp_priv.h>
#endif

#include "asc2bin.h"

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

  unsigned int ssrc;

  srtp_t session;
  srtp_policy_t policy;
};

static bool is_initialised = false;

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

  err_status_t status;

  if(!is_initialised) {
    status = srtp_init();
    if(status != err_status_ok) {
      // TODO: Error handling
      printf("srtp_init failed %d\n", status);
    }
    is_initialised = true;
  }

  prv = new struct prv;

  prv->ssrc = ssrc;

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

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

  status = srtp_add_stream(prv->session, &prv->policy);
  if(status != err_status_ok) {
    // TODO: Error handling
    printf("srtp_add_stream %d\n", 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);
  }

  status = srtp_shutdown();
  if(status != err_status_ok) {
    // TODO: Error handling
    printf("srtp_shutdown failed %d\n", status);
  }

  is_initialised = false;

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

void SRTP::setupKey(const std::string &key)
{
  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)
{
#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)
{
  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)
{
  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;
}