~ubuntu-branches/ubuntu/saucy/resiprocate/saucy-proposed

« back to all changes in this revision

Viewing changes to reflow/FlowDtlsSocketContext.cxx

  • Committer: Package Import Robot
  • Author(s): Daniel Pocock
  • Date: 2012-05-17 19:29:59 UTC
  • Revision ID: package-import@ubuntu.com-20120517192959-vv00m77isztdy64q
Tags: upstream-1.8.2
ImportĀ upstreamĀ versionĀ 1.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifdef HAVE_CONFIG_H
 
2
#include "config.h"
 
3
#endif
 
4
 
 
5
#ifdef USE_SSL
 
6
#include <rutil/Log.hxx>
 
7
#include <rutil/Logger.hxx>
 
8
#include <rutil/Timer.hxx>
 
9
 
 
10
#include <asio.hpp>
 
11
#include <boost/function.hpp>
 
12
#include <iostream>
 
13
 
 
14
#include "FlowDtlsSocketContext.hxx"
 
15
#include "FlowManagerSubsystem.hxx"
 
16
 
 
17
using namespace flowmanager;
 
18
using namespace resip;
 
19
using namespace dtls;
 
20
using namespace std;
 
21
 
 
22
#define RESIPROCATE_SUBSYSTEM FlowManagerSubsystem::FLOWMANAGER
 
23
 
 
24
FlowDtlsSocketContext::FlowDtlsSocketContext(Flow& flow, const asio::ip::address& address, unsigned short port) 
 
25
   : mFlow(flow), mAddress(address), mPort(port), mSrtpInitialized(false)
 
26
{
 
27
}
 
28
 
 
29
FlowDtlsSocketContext::~FlowDtlsSocketContext() 
 
30
{
 
31
   if(mSrtpInitialized)
 
32
   {
 
33
      // Free the master key memory allocated in DtlsSocket::createSrtpSessionPolicies
 
34
      delete mSRTPPolicyIn.key;
 
35
      delete mSRTPPolicyOut.key;
 
36
   }
 
37
}
 
38
 
 
39
void 
 
40
FlowDtlsSocketContext::write(const unsigned char* data, unsigned int len)
 
41
{
 
42
   InfoLog(<< "Dtls write to " << mAddress.to_string() << ":" << mPort << " called.  ComponentId=" << mFlow.getComponentId());
 
43
   mFlow.rawSendTo(mAddress, mPort, (const char*)data, len);
 
44
}
 
45
 
 
46
void 
 
47
FlowDtlsSocketContext::handshakeCompleted()
 
48
{
 
49
   InfoLog(<< "Flow Dtls Handshake Completed!  ComponentId=" << mFlow.getComponentId());
 
50
 
 
51
   char fprint[100];
 
52
   SRTP_PROTECTION_PROFILE *srtp_profile;
 
53
   int r;
 
54
 
 
55
   if(mSocket->getRemoteFingerprint(fprint))
 
56
   {
 
57
      Data remoteSDPFingerprint = mFlow.getRemoteSDPFingerprint();
 
58
      if(!remoteSDPFingerprint.empty())
 
59
      {
 
60
         if(!mSocket->checkFingerprint(remoteSDPFingerprint.c_str(), remoteSDPFingerprint.size()))
 
61
         {
 
62
            InfoLog(<< "Remote fingerprint = " << fprint << " is not valid!  ComponentId=" << mFlow.getComponentId());
 
63
            return;
 
64
         }
 
65
         else
 
66
         {
 
67
            InfoLog(<< "Remote fingerprint = " << fprint << " is valid!  ComponentId=" << mFlow.getComponentId());
 
68
         }
 
69
      }
 
70
      else
 
71
      {
 
72
         InfoLog(<< "Remote fingerprint = " << fprint << "  ComponentId=" << mFlow.getComponentId());
 
73
      }
 
74
   } 
 
75
   else
 
76
   {
 
77
      InfoLog(<< "Remote fingerprint cannot be obtained from Dtls handshake.  ComponentId=" << mFlow.getComponentId());
 
78
      return; 
 
79
   }
 
80
 
 
81
   srtp_profile=mSocket->getSrtpProfile();
 
82
 
 
83
   if(srtp_profile)
 
84
   {
 
85
      InfoLog(<< "SRTP Extension negotiated profile=" << srtp_profile->name << "  ComponentId=" << mFlow.getComponentId());
 
86
   }
 
87
 
 
88
   mSocket->createSrtpSessionPolicies(mSRTPPolicyOut, mSRTPPolicyIn);
 
89
 
 
90
   r=srtp_create(&mSRTPSessionIn, &mSRTPPolicyIn);   
 
91
   assert(r==0);
 
92
   r=srtp_create(&mSRTPSessionOut, &mSRTPPolicyOut);
 
93
   assert(r==0);
 
94
   mSrtpInitialized = true;
 
95
}
 
96
 
 
97
void 
 
98
FlowDtlsSocketContext::handshakeFailed(const char *err)
 
99
{
 
100
   ErrLog(<< "Flow Dtls Handshake failed!  ComponentId=" << mFlow.getComponentId());
 
101
}
 
102
 
 
103
void FlowDtlsSocketContext::fingerprintMismatch()
 
104
{
 
105
   // Ensure Srtp is not initalized, so the will not process media packets from this endpoint
 
106
   if(mSrtpInitialized)
 
107
   {
 
108
      // Free the master key memory allocated in DtlsSocket::createSrtpSessionPolicies
 
109
      delete mSRTPPolicyIn.key;
 
110
      delete mSRTPPolicyOut.key;
 
111
   }
 
112
   mSrtpInitialized = false;
 
113
}
 
114
 
 
115
err_status_t 
 
116
FlowDtlsSocketContext::srtpProtect(void* data, int* size, bool rtcp)
 
117
{
 
118
   err_status_t status = err_status_no_ctx;
 
119
   if(mSrtpInitialized)
 
120
   {
 
121
      if(rtcp)
 
122
      {
 
123
         status = srtp_protect_rtcp(mSRTPSessionOut, data, size);  
 
124
      }
 
125
      else
 
126
      {
 
127
         status = srtp_protect(mSRTPSessionOut, data, size);  
 
128
      }
 
129
   }
 
130
   return status;
 
131
}
 
132
 
 
133
err_status_t 
 
134
FlowDtlsSocketContext::srtpUnprotect(void* data, int* size, bool rtcp)
 
135
{
 
136
   err_status_t status = err_status_no_ctx;
 
137
   if(mSrtpInitialized)
 
138
   {
 
139
      if(rtcp)
 
140
      {
 
141
         status = srtp_unprotect_rtcp(mSRTPSessionIn, data, size);  
 
142
      }
 
143
      else
 
144
      {
 
145
         status = srtp_unprotect(mSRTPSessionIn, data, size);  
 
146
      }
 
147
   }
 
148
   return status;
 
149
}
 
150
 
 
151
#endif 
 
152
/* ====================================================================
 
153
 
 
154
 Copyright (c) 2007-2008, Plantronics, Inc.
 
155
 All rights reserved.
 
156
 
 
157
 Redistribution and use in source and binary forms, with or without
 
158
 modification, are permitted provided that the following conditions are 
 
159
 met:
 
160
 
 
161
 1. Redistributions of source code must retain the above copyright 
 
162
    notice, this list of conditions and the following disclaimer. 
 
163
 
 
164
 2. Redistributions in binary form must reproduce the above copyright
 
165
    notice, this list of conditions and the following disclaimer in the
 
166
    documentation and/or other materials provided with the distribution. 
 
167
 
 
168
 3. Neither the name of Plantronics nor the names of its contributors 
 
169
    may be used to endorse or promote products derived from this 
 
170
    software without specific prior written permission. 
 
171
 
 
172
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 
173
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 
174
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 
175
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 
176
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 
177
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 
178
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 
179
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 
180
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 
181
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 
182
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
183
 
 
184
 ==================================================================== */