/
testDtlsWrapper.cxx
98 lines (72 loc) · 3.01 KB
/
testDtlsWrapper.cxx
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
#include <iostream>
#include "DtlsFactory.hxx"
#include "DtlsSocket.hxx"
#include "rutil/Data.hxx"
#include "CreateCert.hxx"
#include "TestTimerContext.hxx"
#include <openssl/srtp.h>
using namespace std;
using namespace dtls;
using namespace resip;
class TestDtlsSocketContext : public DtlsSocketContext
{
public:
DtlsSocket *mOtherSocket;
const char *mName;
//memory is only valid for duration of callback; must be copied if queueing
//is required
TestDtlsSocketContext(const char *name):
mName(name){}
virtual ~TestDtlsSocketContext(){}
virtual void write(const unsigned char* data, unsigned int len)
{
cout << mName << ": DTLS Wrapper called write...len = " << len << endl;
// Discard data and force retransmit
// mSocket->forceRetransmit();
mOtherSocket->handlePacketMaybe(data, len);
}
virtual void handshakeCompleted()
{
char fprint[100];
SRTP_PROTECTION_PROFILE *srtp_profile;
cout << mName<< ": Hey, amazing, it worked\n";
if(mSocket->getRemoteFingerprint(fprint)){
cout << mName << ": Remote fingerprint == " << fprint << endl;
mOtherSocket->getMyCertFingerprint(fprint);
bool check=mSocket->checkFingerprint(fprint,strlen(fprint));
cout << mName << ": Fingerprint check == " << check << endl;
}
else {
cout << mName << ": Peer did not authenticate" << endl;
}
srtp_profile=mSocket->getSrtpProfile();
if(srtp_profile){
cout << mName << ": SRTP Extension negotiated profile="<<srtp_profile->name << endl;
}
}
virtual void handshakeFailed(const char *err)
{
cout << mName << ": Bummer, handshake failure "<<err<<endl;
}
};
int main(int argc,char **argv)
{
SSL_library_init();
SSL_load_error_strings();
ERR_load_crypto_strings();
srtp_init();
X509 *clientCert,*serverCert;
EVP_PKEY *clientKey,*serverKey;
createCert(resip::Data("sip:client@example.com"),365,1024,clientCert,clientKey);
createCert(resip::Data("sip:server@example.com"),365,1024,serverCert,serverKey);
auto_ptr<DtlsFactory> clientFactory(new DtlsFactory(std::auto_ptr<DtlsTimerContext>(new TestTimerContext()),clientCert,clientKey));
auto_ptr<DtlsFactory> serverFactory(new DtlsFactory(std::auto_ptr<DtlsTimerContext>(new TestTimerContext()),serverCert,serverKey));
cout << "Created the factories\n";
TestDtlsSocketContext *clientContext=new TestDtlsSocketContext("Client");
TestDtlsSocketContext *serverContext=new TestDtlsSocketContext("Server");
auto_ptr<DtlsSocket> clientSocket(clientFactory->createClient(std::auto_ptr<DtlsSocketContext>(clientContext)));
auto_ptr<DtlsSocket> serverSocket(serverFactory->createServer(std::auto_ptr<DtlsSocketContext>(serverContext)));
clientContext->mOtherSocket=serverSocket.get();
serverContext->mOtherSocket=clientSocket.get();
clientSocket->startClient();
}