-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCipherSuite.cpp
248 lines (235 loc) · 6.82 KB
/
CipherSuite.cpp
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
//
// cipher-suite.cpp
// browser-cc
//
// Created by Nissassin Seventeen on 10/2/15.
// Copyright (c) 2015 Nissassin Seventeen. All rights reserved.
//
#include "CipherSuite.hpp"
#include "Cipher.hpp"
#include "MAC.hpp"
#include "Util.hpp"
#include "BitUtil.hpp"
CipherSuite::CipherSuite(CipherSuiteType type) :
suite(TLS_NULL_WITH_NULL_NULL), cipher(NULL), keyExchange(
KEY_EXCHANGE_NULL), mac(NULL) {
setType(type);
}
void CipherSuite::setType(CipherSuiteType type) {
this->suite = type;
switch (this->suite) {
case TLS_NULL_WITH_NULL_NULL:
keyExchange = KEY_EXCHANGE_NULL;
cipher.reset(new Cipher(Cipher::CIPHER_NULL));
mac.reset(new MAC(MAC::MAC_NULL));
break;
case TLS_RSA_WITH_NULL_MD5:
keyExchange = RSA;
cipher.reset(new Cipher(Cipher::CIPHER_NULL));
mac.reset(new MAC(MAC::MD5));
break;
case TLS_RSA_WITH_NULL_SHA:
keyExchange = RSA;
cipher.reset(new Cipher(Cipher::CIPHER_NULL));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_RSA_WITH_NULL_SHA256:
keyExchange = RSA;
cipher.reset(new Cipher(Cipher::CIPHER_NULL));
mac.reset(new MAC(MAC::SHA256));
break;
case TLS_RSA_WITH_RC4_128_MD5:
keyExchange = RSA;
cipher.reset(new Cipher(Cipher::RC4_128));
mac.reset(new MAC(MAC::MD5));
break;
case TLS_RSA_WITH_RC4_128_SHA:
keyExchange = RSA;
cipher.reset(new Cipher(Cipher::RC4_128));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_RSA_WITH_3DES_EDE_CBC_SHA:
keyExchange = RSA;
cipher.reset(new Cipher(Cipher::_3DES_EDE_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_RSA_WITH_AES_128_CBC_SHA:
keyExchange = RSA;
cipher.reset(new Cipher(Cipher::AES_128_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_RSA_WITH_AES_256_CBC_SHA:
keyExchange = RSA;
cipher.reset(new Cipher(Cipher::AES_256_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_RSA_WITH_AES_128_CBC_SHA256:
keyExchange = RSA;
cipher.reset(new Cipher(Cipher::AES_128_CBC));
mac.reset(new MAC(MAC::SHA256));
break;
case TLS_RSA_WITH_AES_256_CBC_SHA256:
keyExchange = RSA;
cipher.reset(new Cipher(Cipher::AES_256_CBC));
mac.reset(new MAC(MAC::SHA256));
break;
case TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA:
keyExchange = DH_DSS;
cipher.reset(new Cipher(Cipher::_3DES_EDE_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA:
keyExchange = DH_RSA;
cipher.reset(new Cipher(Cipher::_3DES_EDE_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA:
keyExchange = DHE_DSS;
cipher.reset(new Cipher(Cipher::_3DES_EDE_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
keyExchange = DHE_RSA;
cipher.reset(new Cipher(Cipher::_3DES_EDE_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_DH_anon_WITH_RC4_128_MD5:
keyExchange = DH_anon;
cipher.reset(new Cipher(Cipher::RC4_128));
mac.reset(new MAC(MAC::MD5));
break;
case TLS_DH_anon_WITH_3DES_EDE_CBC_SHA:
keyExchange = DH_anon;
cipher.reset(new Cipher(Cipher::_3DES_EDE_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_DH_DSS_WITH_AES_128_CBC_SHA:
keyExchange = DH_DSS;
cipher.reset(new Cipher(Cipher::AES_128_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_DH_RSA_WITH_AES_128_CBC_SHA:
keyExchange = DH_RSA;
cipher.reset(new Cipher(Cipher::AES_128_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_DHE_DSS_WITH_AES_128_CBC_SHA:
keyExchange = DHE_DSS;
cipher.reset(new Cipher(Cipher::AES_128_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_DHE_RSA_WITH_AES_128_CBC_SHA:
keyExchange = DHE_RSA;
cipher.reset(new Cipher(Cipher::AES_128_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_DH_anon_WITH_AES_128_CBC_SHA:
keyExchange = DH_anon;
cipher.reset(new Cipher(Cipher::AES_128_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_DH_DSS_WITH_AES_256_CBC_SHA:
keyExchange = DH_DSS;
cipher.reset(new Cipher(Cipher::AES_256_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_DH_RSA_WITH_AES_256_CBC_SHA:
keyExchange = DH_RSA;
cipher.reset(new Cipher(Cipher::AES_256_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_DHE_DSS_WITH_AES_256_CBC_SHA:
keyExchange = DHE_DSS;
cipher.reset(new Cipher(Cipher::AES_256_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_DHE_RSA_WITH_AES_256_CBC_SHA:
keyExchange = DHE_RSA;
cipher.reset(new Cipher(Cipher::AES_256_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_DH_anon_WITH_AES_256_CBC_SHA:
keyExchange = DH_anon;
cipher.reset(new Cipher(Cipher::AES_256_CBC));
mac.reset(new MAC(MAC::SHA));
break;
case TLS_DH_DSS_WITH_AES_128_CBC_SHA256:
keyExchange = DH_DSS;
cipher.reset(new Cipher(Cipher::AES_128_CBC));
mac.reset(new MAC(MAC::SHA256));
break;
case TLS_DH_RSA_WITH_AES_128_CBC_SHA256:
keyExchange = DH_RSA;
cipher.reset(new Cipher(Cipher::AES_128_CBC));
mac.reset(new MAC(MAC::SHA256));
break;
case TLS_DHE_DSS_WITH_AES_128_CBC_SHA256:
keyExchange = DHE_DSS;
cipher.reset(new Cipher(Cipher::AES_128_CBC));
mac.reset(new MAC(MAC::SHA256));
break;
case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256:
keyExchange = DHE_RSA;
cipher.reset(new Cipher(Cipher::AES_128_CBC));
mac.reset(new MAC(MAC::SHA256));
break;
case TLS_DH_anon_WITH_AES_128_CBC_SHA256:
keyExchange = DH_anon;
cipher.reset(new Cipher(Cipher::AES_128_CBC));
mac.reset(new MAC(MAC::SHA256));
break;
case TLS_DH_DSS_WITH_AES_256_CBC_SHA256:
keyExchange = DH_DSS;
cipher.reset(new Cipher(Cipher::AES_256_CBC));
mac.reset(new MAC(MAC::SHA256));
break;
case TLS_DH_RSA_WITH_AES_256_CBC_SHA256:
keyExchange = DH_RSA;
cipher.reset(new Cipher(Cipher::AES_256_CBC));
mac.reset(new MAC(MAC::SHA256));
break;
case TLS_DHE_DSS_WITH_AES_256_CBC_SHA256:
keyExchange = DHE_DSS;
cipher.reset(new Cipher(Cipher::AES_256_CBC));
mac.reset(new MAC(MAC::SHA256));
break;
case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256:
keyExchange = DHE_RSA;
cipher.reset(new Cipher(Cipher::AES_256_CBC));
mac.reset(new MAC(MAC::SHA256));
break;
case TLS_DH_anon_WITH_AES_256_CBC_SHA256:
keyExchange = DH_anon;
cipher.reset(new Cipher(Cipher::AES_256_CBC));
mac.reset(new MAC(MAC::SHA256));
break;
default:
break;
}
}
vector<uint8_t> CipherSuite::toData() const {
uint8_t high(BitUtil::filterByte(this->suite, 8));
uint8_t low(BitUtil::filterByte(this->suite));
vector<uint8_t> data;
data.push_back(high);
data.push_back(low);
return data;
}
size_t CipherSuite::size() const {
return 2;
}
CipherSuite::CipherSuite(const vector<uint8_t> &data, size_t offset) {
setType((CipherSuiteType) Util::takeData16(data, offset));
}
const shared_ptr<Cipher>& CipherSuite::getCipher() const {
return cipher;
}
CipherSuite::KeyExchangeType CipherSuite::getKeyExchange() const {
return keyExchange;
}
const shared_ptr<MAC>& CipherSuite::getMac() const {
return mac;
}
CipherSuite::CipherSuiteType CipherSuite::getSuite() const {
return suite;
}