Skip to content
Newer
Older
100644 150 lines (124 sloc) 3.63 KB
77866f1 @dlongley Added socket data presenter class for SSL, not fully implemented.
dlongley authored Jul 23, 2007
1 /*
a6bfe8d @dlongley Removed redundant/unnecessary NULL checks and used initializers.
dlongley authored Feb 2, 2011
2 * Copyright (c) 2007-2011 Digital Bazaar, Inc. All rights reserved.
77866f1 @dlongley Added socket data presenter class for SSL, not fully implemented.
dlongley authored Jul 23, 2007
3 */
2ef08f0 @msporny Replaced all "db/..." includes with "monarch/...".
msporny authored Dec 11, 2009
4 #include "monarch/net/SslSocketDataPresenter.h"
6bf7285 @dlongley Fixed bug in connection service.
dlongley authored May 21, 2009
5
2ef08f0 @msporny Replaced all "db/..." includes with "monarch/...".
msporny authored Dec 12, 2009
6 #include "monarch/net/SslSocket.h"
7 #include "monarch/net/SocketDefinitions.h"
77866f1 @dlongley Added socket data presenter class for SSL, not fully implemented.
dlongley authored Jul 23, 2007
8
cdd4403 @msporny Replaced all "db::" namespace prefixes with "monarch::".
msporny authored Dec 11, 2009
9 using namespace monarch::net;
10 using namespace monarch::rt;
77866f1 @dlongley Added socket data presenter class for SSL, not fully implemented.
dlongley authored Jul 23, 2007
11
a6bfe8d @dlongley Removed redundant/unnecessary NULL checks and used initializers.
dlongley authored Feb 2, 2011
12 SslSocketDataPresenter::SslSocketDataPresenter(SslContext* context) :
13 mContext(context)
77866f1 @dlongley Added socket data presenter class for SSL, not fully implemented.
dlongley authored Jul 23, 2007
14 {
15 }
16
17 SslSocketDataPresenter::~SslSocketDataPresenter()
18 {
19 }
c094ed5 @dlongley Removed std::string's from core class implementations that require
dlongley authored Jul 27, 2007
20
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
21 bool SslSocketDataPresenter::detectSsl(Socket* s)
22 {
23 bool rval = false;
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
24
6bf7285 @dlongley Fixed bug in connection service.
dlongley authored May 22, 2009
25 // save old receive timeout, set new one at 3 seconds
26 uint32_t recvTimeout = s->getReceiveTimeout();
27 s->setReceiveTimeout(1000 * 3);
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
28
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
29 // at least 5 bytes are needed to detect an SSL/TLS packet
30 char b[5];
e122535 @dlongley Fixed bug where numBytes was set to an unsigned int.
dlongley authored May 21, 2009
31 int numBytes = 0;
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
32
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
33 // peek 5 bytes
e122535 @dlongley Fixed bug where numBytes was set to an unsigned int.
dlongley authored May 22, 2009
34 int count = 5;
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
35 while(count > 0 &&
1e2c654 @dlongley Changed input/output stream interfaces so they use signed integers in…
dlongley authored Sep 5, 2007
36 (numBytes = s->getInputStream()->peek(b + (5 - count), count)) > 0)
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
37 {
38 count -= numBytes;
39 }
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
40
6bf7285 @dlongley Fixed bug in connection service.
dlongley authored May 22, 2009
41 if(numBytes < 0)
42 {
6e258a2 @dlongley Fixed accidental return of true when a socket times out.
dlongley authored May 21, 2009
43 // clear socket timeout exceptions
cb51b00 @dlongley Updated instances of deprecated Exception::clearLast.
dlongley authored Jul 15, 2009
44 ExceptionRef e = Exception::get();
acfcfac @dlongley Added convenience methods for checking exception types or
dlongley authored Jul 30, 2009
45 if(e->isType(SOCKET_TIMEOUT_EXCEPTION_TYPE))
6bf7285 @dlongley Fixed bug in connection service.
dlongley authored May 22, 2009
46 {
cb51b00 @dlongley Updated instances of deprecated Exception::clearLast.
dlongley authored Jul 15, 2009
47 Exception::clear();
6bf7285 @dlongley Fixed bug in connection service.
dlongley authored May 22, 2009
48 }
49 }
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
50
6bf7285 @dlongley Fixed bug in connection service.
dlongley authored May 22, 2009
51 // reset old receive timeout
52 s->setReceiveTimeout(recvTimeout);
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
53
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
54 if(count == 0)
55 {
56 // check SSL record type
57 // check for handshake (value of 22 == 0x16)
58 if(b[0] == 0x16)
59 {
60 // offset + 1 is major version
61 // offset + 2 is minor version
62 // offset + 3, 4 is a short that is the length of
63 // data in the record excluding the header (max of 16384)
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
64
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
65 // SSL 3.0 / TLS 1.0 both have major version 3.0
66 if(b[1] == 0x03)
67 {
68 // TLS v1.0 connection detected
69 rval = true;
70 }
71 }
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
72
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
73 // not TLS 1.0, may be SSL 2.0/3.0
74 if(!rval)
75 {
76 // EXPECT SSL 2.0/3.0:
77 // * means optional, escape and padding only exist if
78 // the most significant bit is set for record-length
79 //
80 // FORMAT:
81 // HEADER {record-length, *is-escape-record?, *padding}
82 // MAC-DATA[MAC_SIZE]
83 // ACTUAL-DATA[N]
84 // PADDING-DATA[PADDING]
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
85
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
86 // for storing when the actual data starts
87 unsigned int actualDataOffset = 0;
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
88
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
89 // for storing record length
90 unsigned int recordLength = 0;
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
91
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
92 // see if the most significant bit is set in the first byte
93 if((b[0] & 0x80) != 0)
94 {
95 // total header length is 2 bytes
96 actualDataOffset = 2;
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
97
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
98 // get the record length
99 recordLength = ((b[0] & 0x7F) << 8) | b[1];
100 }
101 else
102 {
103 // total header length is 3 bytes
104 actualDataOffset = 3;
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
105
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
106 // get the record length
107 recordLength = ((b[0] & 0x3F) << 8) | b[1];
108 }
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
109
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
110 // get the client-hello
111 unsigned int clientHello = b[actualDataOffset];
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
112
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
113 // get version
114 unsigned int version = b[actualDataOffset + 1];
115 if(version == 0)
116 {
117 version = b[actualDataOffset + 2];
118 }
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
119
120 // ensure that we have a client hello
f05edb7 @dlongley Ensure record length > 0.
dlongley authored May 16, 2011
121 if(clientHello == 1 && recordLength > 0)
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
122 {
123 // check for version 2 or 3
124 if(version == 2 || version == 3)
125 {
126 rval = true;
127 }
128 }
129 }
130 }
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
131
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
132 return rval;
133 }
77866f1 @dlongley Added socket data presenter class for SSL, not fully implemented.
dlongley authored Jul 23, 2007
134
135 Socket* SslSocketDataPresenter::createPresentationWrapper(
136 Socket* s, bool& secure)
137 {
138 SslSocket* rval = NULL;
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
139
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
140 // detect SSL record
141 if(detectSsl(s))
77866f1 @dlongley Added socket data presenter class for SSL, not fully implemented.
dlongley authored Jul 23, 2007
142 {
143 // create an SSL socket, (false = use server mode, true = cleanup)
144 rval = new SslSocket(mContext, (TcpSocket*)s, false, true);
62ccbc8 @dlongley Added code to detect SSL records for SslSocketDataPresenter.
dlongley authored Jul 23, 2007
145 secure = true;
77866f1 @dlongley Added socket data presenter class for SSL, not fully implemented.
dlongley authored Jul 23, 2007
146 }
7b588f9 @dlongley Removed trailing whitespace from db net.
dlongley authored Aug 14, 2009
147
77866f1 @dlongley Added socket data presenter class for SSL, not fully implemented.
dlongley authored Jul 23, 2007
148 return rval;
149 }
Something went wrong with that request. Please try again.