22 #include "runtime.hpp"
23 #include "yassl_int.hpp"
24 #include "handshake.hpp"
35 bool isTLS(ProtocolVersion pv)
37 if (pv.major_ >= 3 && pv.minor_ >= 1)
47 void hashHandShake(SSL&,
const input_buffer&, uint);
50 ProtocolVersion::ProtocolVersion(uint8 maj, uint8 min)
51 : major_(maj), minor_(min)
56 void ClientKeyExchange::createKey(SSL& ssl)
58 const ClientKeyFactory& ckf = ssl.getFactory().getClientKey();
59 client_key_ = ckf.CreateObject(ssl.getSecurity().get_parms().kea_);
62 ssl.SetError(factory_error);
67 void ServerKeyExchange::createKey(SSL& ssl)
69 const ServerKeyFactory& skf = ssl.getFactory().getServerKey();
70 server_key_ = skf.CreateObject(ssl.getSecurity().get_parms().kea_);
73 ssl.SetError(factory_error);
78 void EncryptedPreMasterSecret::build(SSL& ssl)
80 opaque tmp[SECRET_LEN];
81 memset(tmp, 0,
sizeof(tmp));
82 ssl.getCrypto().get_random().Fill(tmp, SECRET_LEN);
83 ProtocolVersion pv = ssl.getSecurity().get_connection().chVersion_;
86 ssl.set_preMaster(tmp, SECRET_LEN);
88 const CertManager& cert = ssl.getCrypto().get_certManager();
89 RSA rsa(cert.get_peerKey(), cert.get_peerKeyLength());
90 bool tls = ssl.isTLS();
91 alloc(rsa.get_cipherLength() + (tls ? 2 : 0));
92 byte* holder = secret_;
95 c16toa(rsa.get_cipherLength(), len);
96 memcpy(secret_, len,
sizeof(len));
99 rsa.encrypt(holder, tmp, SECRET_LEN, ssl.getCrypto().get_random());
104 void ClientDiffieHellmanPublic::build(SSL& ssl)
106 DiffieHellman& dhServer = ssl.useCrypto().use_dh();
107 DiffieHellman dhClient(dhServer);
109 uint keyLength = dhClient.get_agreedKeyLength();
111 alloc(keyLength,
true);
112 dhClient.makeAgreement(dhServer.get_publicKey(), keyLength);
113 c16toa(keyLength, Yc_);
114 memcpy(Yc_ + KEY_OFFSET, dhClient.get_publicKey(), keyLength);
117 if (*dhClient.get_agreedKey() == 0)
118 ssl.set_preMaster(dhClient.get_agreedKey() + 1, keyLength - 1);
120 ssl.set_preMaster(dhClient.get_agreedKey(), keyLength);
125 void DH_Server::build(SSL& ssl)
127 DiffieHellman& dhServer = ssl.useCrypto().use_dh();
130 dhServer.set_sizes(pSz, gSz, pubSz);
131 dhServer.get_parms(parms_.alloc_p(pSz), parms_.alloc_g(gSz),
132 parms_.alloc_pub(pubSz));
136 const CertManager& cert = ssl.getCrypto().get_certManager();
138 if (ssl.getSecurity().get_parms().sig_algo_ == rsa_sa_algo) {
139 if (cert.get_keyType() != rsa_sa_algo) {
140 ssl.SetError(privateKey_error);
143 auth.reset(NEW_YS RSA(cert.get_privateKey(),
144 cert.get_privateKeyLength(),
false));
147 if (cert.get_keyType() != dsa_sa_algo) {
148 ssl.SetError(privateKey_error);
151 auth.reset(NEW_YS DSS(cert.get_privateKey(),
152 cert.get_privateKeyLength(),
false));
153 sigSz += DSS_ENCODED_EXTRA;
156 sigSz += auth->get_signatureLength();
158 ssl.SetError(privateKey_error);
163 length_ += pSz + gSz + pubSz + sigSz;
165 output_buffer tmp(length_);
169 tmp.write(len,
sizeof(len));
170 tmp.write(parms_.get_p(), pSz);
173 tmp.write(len,
sizeof(len));
174 tmp.write(parms_.get_g(), gSz);
177 tmp.write(len,
sizeof(len));
178 tmp.write(parms_.get_pub(), pubSz);
181 byte hash[FINISHED_SZ];
184 signature_ = NEW_YS byte[sigSz];
188 md5.update(conn.client_random_, RAN_LEN);
189 md5.update(conn.server_random_, RAN_LEN);
190 md5.update(tmp.get_buffer(), tmp.get_size());
191 md5.get_digest(hash);
194 sha.update(conn.client_random_, RAN_LEN);
195 sha.update(conn.server_random_, RAN_LEN);
196 sha.update(tmp.get_buffer(), tmp.get_size());
197 sha.get_digest(&hash[MD5_LEN]);
199 if (ssl.getSecurity().get_parms().sig_algo_ == rsa_sa_algo)
200 auth->sign(signature_, hash,
sizeof(hash),
201 ssl.getCrypto().get_random());
203 auth->sign(signature_, &hash[MD5_LEN], SHA_LEN,
204 ssl.getCrypto().get_random());
205 byte encoded[DSS_SIG_SZ + DSS_ENCODED_EXTRA];
206 TaoCrypt::EncodeDSA_Signature(signature_, encoded);
207 memcpy(signature_, encoded,
sizeof(encoded));
211 tmp.write(len,
sizeof(len));
212 tmp.write(signature_, sigSz);
215 keyMessage_ = NEW_YS opaque[length_];
216 memcpy(keyMessage_, tmp.get_buffer(), tmp.get_size());
221 void EncryptedPreMasterSecret::read(SSL& ssl, input_buffer& input)
223 const CertManager& cert = ssl.getCrypto().get_certManager();
224 RSA rsa(cert.get_privateKey(), cert.get_privateKeyLength(),
false);
225 uint16 cipherLen = rsa.get_cipherLength();
228 input.read(len,
sizeof(len));
229 ato16(len, cipherLen);
232 input.read(secret_, length_);
234 opaque preMasterSecret[SECRET_LEN];
235 rsa.decrypt(preMasterSecret, secret_, length_,
236 ssl.getCrypto().get_random());
238 ProtocolVersion pv = ssl.getSecurity().get_connection().chVersion_;
239 if (pv.major_ != preMasterSecret[0] || pv.minor_ != preMasterSecret[1])
240 ssl.SetError(pms_version_error);
242 ssl.set_preMaster(preMasterSecret, SECRET_LEN);
243 ssl.makeMasterSecret();
247 EncryptedPreMasterSecret::EncryptedPreMasterSecret()
248 : secret_(0), length_(0)
252 EncryptedPreMasterSecret::~EncryptedPreMasterSecret()
254 ysArrayDelete(secret_);
258 int EncryptedPreMasterSecret::get_length()
const
264 opaque* EncryptedPreMasterSecret::get_clientKey()
const
270 void EncryptedPreMasterSecret::alloc(
int sz)
273 secret_ = NEW_YS opaque[sz];
278 void ClientDiffieHellmanPublic::read(SSL& ssl, input_buffer& input)
280 DiffieHellman& dh = ssl.useCrypto().use_dh();
284 tmp[0] = input[AUTO];
285 tmp[1] = input[AUTO];
286 ato16(tmp, keyLength);
289 input.read(Yc_, keyLength);
290 dh.makeAgreement(Yc_, keyLength);
293 if (*dh.get_agreedKey() == 0)
294 ssl.set_preMaster(dh.get_agreedKey() + 1, dh.get_agreedKeyLength() - 1);
296 ssl.set_preMaster(dh.get_agreedKey(), dh.get_agreedKeyLength());
297 ssl.makeMasterSecret();
301 ClientDiffieHellmanPublic::ClientDiffieHellmanPublic()
306 ClientDiffieHellmanPublic::~ClientDiffieHellmanPublic()
312 int ClientDiffieHellmanPublic::get_length()
const
318 opaque* ClientDiffieHellmanPublic::get_clientKey()
const
324 void ClientDiffieHellmanPublic::alloc(
int sz,
bool offset)
326 length_ = sz + (offset ? KEY_OFFSET : 0);
327 Yc_ = NEW_YS opaque[length_];
332 void DH_Server::read(SSL& ssl, input_buffer& input)
334 uint16 length, messageTotal = 6;
338 tmp[0] = input[AUTO];
339 tmp[1] = input[AUTO];
341 messageTotal += length;
343 input.read(parms_.alloc_p(length), length);
346 tmp[0] = input[AUTO];
347 tmp[1] = input[AUTO];
349 messageTotal += length;
351 input.read(parms_.alloc_g(length), length);
354 tmp[0] = input[AUTO];
355 tmp[1] = input[AUTO];
357 messageTotal += length;
359 input.read(parms_.alloc_pub(length), length);
362 input_buffer
message(messageTotal);
363 input.set_current(input.get_current() - messageTotal);
364 input.read(
message.get_buffer(), messageTotal);
365 message.add_size(messageTotal);
368 tmp[0] = input[AUTO];
369 tmp[1] = input[AUTO];
372 signature_ = NEW_YS byte[length];
373 input.read(signature_, length);
376 byte hash[FINISHED_SZ];
380 const Connection& conn = ssl.getSecurity().get_connection();
382 md5.update(conn.client_random_, RAN_LEN);
383 md5.update(conn.server_random_, RAN_LEN);
385 md5.get_digest(hash);
388 sha.update(conn.client_random_, RAN_LEN);
389 sha.update(conn.server_random_, RAN_LEN);
391 sha.get_digest(&hash[MD5_LEN]);
393 const CertManager& cert = ssl.getCrypto().get_certManager();
395 if (ssl.getSecurity().get_parms().sig_algo_ == rsa_sa_algo) {
396 RSA rsa(cert.get_peerKey(), cert.get_peerKeyLength());
397 if (!rsa.verify(hash,
sizeof(hash), signature_, length))
398 ssl.SetError(verify_error);
401 byte decodedSig[DSS_SIG_SZ];
402 length = TaoCrypt::DecodeDSA_Signature(decodedSig, signature_, length);
404 DSS dss(cert.get_peerKey(), cert.get_peerKeyLength());
405 if (!dss.verify(&hash[MD5_LEN], SHA_LEN, decodedSig, length))
406 ssl.SetError(verify_error);
410 ssl.useCrypto().SetDH(NEW_YS DiffieHellman(parms_.get_p(),
411 parms_.get_pSize(), parms_.get_g(), parms_.get_gSize(),
412 parms_.get_pub(), parms_.get_pubSize(),
413 ssl.getCrypto().get_random()));
417 DH_Server::DH_Server()
418 : signature_(0), length_(0), keyMessage_(0)
422 DH_Server::~DH_Server()
424 ysArrayDelete(keyMessage_);
425 ysArrayDelete(signature_);
429 int DH_Server::get_length()
const
435 opaque* DH_Server::get_serverKey()
const
442 Parameters::Parameters(ConnectionEnd ce,
const Ciphers& ciphers,
443 ProtocolVersion pv,
bool haveDH) : entity_(ce)
446 strncpy(cipher_name_,
"NONE", 5);
450 if (ciphers.setSuites_) {
451 suites_size_ = ciphers.suiteSz_;
452 memcpy(suites_, ciphers.suites_, ciphers.suiteSz_);
456 SetSuites(pv, ce == server_end && removeDH_);
461 void Parameters::SetSuites(ProtocolVersion pv,
bool removeDH,
bool removeRSA,
473 suites_[i++] = TLS_DHE_RSA_WITH_AES_256_CBC_SHA;
477 suites_[i++] = TLS_DHE_DSS_WITH_AES_256_CBC_SHA;
482 suites_[i++] = TLS_RSA_WITH_AES_256_CBC_SHA;
487 suites_[i++] = TLS_DHE_RSA_WITH_AES_128_CBC_SHA;
491 suites_[i++] = TLS_DHE_DSS_WITH_AES_128_CBC_SHA;
496 suites_[i++] = TLS_RSA_WITH_AES_128_CBC_SHA;
498 suites_[i++] = TLS_RSA_WITH_AES_256_CBC_RMD160;
500 suites_[i++] = TLS_RSA_WITH_AES_128_CBC_RMD160;
502 suites_[i++] = TLS_RSA_WITH_3DES_EDE_CBC_RMD160;
507 suites_[i++] = TLS_DHE_RSA_WITH_AES_256_CBC_RMD160;
509 suites_[i++] = TLS_DHE_RSA_WITH_AES_128_CBC_RMD160;
511 suites_[i++] = TLS_DHE_RSA_WITH_3DES_EDE_CBC_RMD160;
515 suites_[i++] = TLS_DHE_DSS_WITH_AES_256_CBC_RMD160;
517 suites_[i++] = TLS_DHE_DSS_WITH_AES_128_CBC_RMD160;
519 suites_[i++] = TLS_DHE_DSS_WITH_3DES_EDE_CBC_RMD160;
526 suites_[i++] = SSL_RSA_WITH_RC4_128_SHA;
528 suites_[i++] = SSL_RSA_WITH_RC4_128_MD5;
531 suites_[i++] = SSL_RSA_WITH_3DES_EDE_CBC_SHA;
533 suites_[i++] = SSL_RSA_WITH_DES_CBC_SHA;
538 suites_[i++] = SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA;
542 suites_[i++] = SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA;
546 suites_[i++] = SSL_DHE_RSA_WITH_DES_CBC_SHA;
550 suites_[i++] = SSL_DHE_DSS_WITH_DES_CBC_SHA;
560 void Parameters::SetCipherNames()
562 const int suites = suites_size_ / 2;
565 for (
int j = 0; j < suites; j++) {
566 int index = suites_[j*2 + 1];
567 size_t len = strlen(cipher_names[index]) + 1;
568 strncpy(cipher_list_[pos++], cipher_names[index], len);
570 cipher_list_[pos][0] = 0;
575 input_buffer& operator>>(input_buffer& input, RecordLayerHeader& hdr)
577 hdr.type_ = ContentType(input[AUTO]);
578 hdr.version_.major_ = input[AUTO];
579 hdr.version_.minor_ = input[AUTO];
583 tmp[0] = input[AUTO];
584 tmp[1] = input[AUTO];
585 ato16(tmp, hdr.length_);
592 output_buffer& operator<<(output_buffer& output,
const RecordLayerHeader& hdr)
594 output[AUTO] = hdr.type_;
595 output[AUTO] = hdr.version_.major_;
596 output[AUTO] = hdr.version_.minor_;
600 c16toa(hdr.length_, tmp);
601 output[AUTO] = tmp[0];
602 output[AUTO] = tmp[1];
609 input_buffer& operator>>(input_buffer& input, Message&
msg)
611 return msg.set(input);
615 output_buffer& operator<<(output_buffer& output,
const Message& msg)
617 return msg.get(output);
622 input_buffer& operator>>(input_buffer& input, HandShakeHeader& hs)
624 hs.type_ = HandShakeType(input[AUTO]);
626 hs.length_[0] = input[AUTO];
627 hs.length_[1] = input[AUTO];
628 hs.length_[2] = input[AUTO];
635 output_buffer& operator<<(output_buffer& output,
const HandShakeHeader& hdr)
637 output[AUTO] = hdr.type_;
638 output.write(hdr.length_,
sizeof(hdr.length_));
644 void HandShakeHeader::Process(input_buffer& input, SSL& ssl)
646 ssl.verifyState(*
this);
647 if (ssl.GetError())
return;
648 const HandShakeFactory& hsf = ssl.getFactory().getHandShake();
651 ssl.SetError(factory_error);
655 uint len = c24to32(length_);
656 if (len > input.get_remaining()) {
657 ssl.SetError(bad_input);
660 hashHandShake(ssl, input, len);
664 hs->Process(input, ssl);
668 ContentType HandShakeHeader::get_type()
const
674 uint16 HandShakeHeader::get_length()
const
676 return c24to32(length_);
680 HandShakeType HandShakeHeader::get_handshakeType()
const
686 void HandShakeHeader::set_type(HandShakeType hst)
692 void HandShakeHeader::set_length(uint32 u32)
694 c32to24(u32, length_);
698 input_buffer& HandShakeHeader::set(input_buffer& in)
704 output_buffer& HandShakeHeader::get(output_buffer& out)
const
711 int HandShakeBase::get_length()
const
717 void HandShakeBase::set_length(
int l)
724 HandShakeType HandShakeBase::get_type()
const
730 input_buffer& HandShakeBase::set(input_buffer& in)
736 output_buffer& HandShakeBase::get(output_buffer& out)
const
742 void HandShakeBase::Process(input_buffer&, SSL&)
746 input_buffer& HelloRequest::set(input_buffer& in)
752 output_buffer& HelloRequest::get(output_buffer& out)
const
758 void HelloRequest::Process(input_buffer&, SSL&)
762 HandShakeType HelloRequest::get_type()
const
764 return hello_request;
769 input_buffer& operator>>(input_buffer& input, ChangeCipherSpec& cs)
771 cs.type_ = CipherChoice(input[AUTO]);
776 output_buffer& operator<<(output_buffer& output,
const ChangeCipherSpec& cs)
778 output[AUTO] = cs.type_;
783 ChangeCipherSpec::ChangeCipherSpec()
784 : type_(change_cipher_spec_choice)
788 input_buffer& ChangeCipherSpec::set(input_buffer& in)
794 output_buffer& ChangeCipherSpec::get(output_buffer& out)
const
800 ContentType ChangeCipherSpec::get_type()
const
802 return change_cipher_spec;
806 uint16 ChangeCipherSpec::get_length()
const
813 void ChangeCipherSpec::Process(input_buffer&, SSL& ssl)
815 ssl.useSecurity().use_parms().pending_ =
false;
816 if (ssl.getSecurity().get_resuming()) {
817 if (ssl.getSecurity().get_parms().entity_ == client_end)
818 buildFinished(ssl, ssl.useHashes().use_verify(), server);
820 else if (ssl.getSecurity().get_parms().entity_ == server_end)
821 buildFinished(ssl, ssl.useHashes().use_verify(), client);
825 Alert::Alert(AlertLevel al, AlertDescription ad)
826 : level_(al), description_(ad)
830 ContentType Alert::get_type()
const
836 uint16 Alert::get_length()
const
838 return SIZEOF_ENUM * 2;
842 input_buffer& Alert::set(input_buffer& in)
848 output_buffer& Alert::get(output_buffer& out)
const
855 input_buffer& operator>>(input_buffer& input, Alert& a)
857 a.level_ = AlertLevel(input[AUTO]);
858 a.description_ = AlertDescription(input[AUTO]);
865 output_buffer& operator<<(output_buffer& output,
const Alert& a)
867 output[AUTO] = a.level_;
868 output[AUTO] = a.description_;
874 void Alert::Process(input_buffer& input, SSL& ssl)
876 if (ssl.getSecurity().get_parms().pending_ ==
false) {
877 int aSz = get_length();
878 opaque verify[SHA_LEN];
879 const opaque* data = input.get_buffer() + input.get_current() - aSz;
882 TLS_hmac(ssl, verify, data, aSz, alert,
true);
884 hmac(ssl, verify, data, aSz, alert,
true);
887 int digestSz = ssl.getCrypto().get_digest().get_digestSize();
889 input.read(mac, digestSz);
891 if (ssl.getSecurity().get_parms().cipher_type_ ==
block) {
895 ivExtra = ssl.getCrypto().get_cipher().get_blockSize();
896 int padSz = ssl.getSecurity().get_parms().encrypt_size_ - ivExtra -
898 input.set_current(input.get_current() + padSz);
902 if (memcmp(mac, verify, digestSz)) {
903 ssl.SetError(verify_error);
907 if (level_ == fatal) {
908 ssl.useStates().useRecord() = recordNotReady;
909 ssl.useStates().useHandShake() = handShakeNotReady;
910 ssl.SetError(YasslError(description_));
916 : length_(0), buffer_(0), write_buffer_(0)
920 Data::Data(uint16 len, opaque* b)
921 : length_(len), buffer_(b), write_buffer_(0)
925 void Data::SetData(uint16 len,
const opaque* buffer)
928 write_buffer_ = buffer;
931 input_buffer& Data::set(input_buffer& in)
937 output_buffer& Data::get(output_buffer& out)
const
943 ContentType Data::get_type()
const
945 return application_data;
949 uint16 Data::get_length()
const
955 void Data::set_length(uint16 l)
961 opaque* Data::set_buffer()
968 output_buffer& operator<<(output_buffer& output,
const Data& data)
970 output.write(data.write_buffer_, data.length_);
976 static int constant_compare(
const byte* a,
const byte* b,
int len)
981 for (
int i = 0; i < len; i++) {
996 static int pad_check(
const byte* input, byte pad,
int len)
1001 for (
int i = 0; i < len; i++) {
1002 if (input[i] == pad)
1016 static inline int get_rounds(
int pLen,
int padLen,
int t)
1021 int L1 = COMPRESS_CONSTANT + pLen - t;
1022 int L2 = COMPRESS_CONSTANT + pLen - padLen - 1 - t;
1024 L1 -= COMPRESS_UPPER;
1025 L2 -= COMPRESS_UPPER;
1027 if ( (L1 % COMPRESS_LOWER) == 0)
1029 if ( (L2 % COMPRESS_LOWER) == 0)
1032 L1 /= COMPRESS_LOWER;
1033 L2 /= COMPRESS_LOWER;
1043 static inline void compress_rounds(SSL& ssl,
int rounds,
const byte* dummy)
1046 Digest* digest = NULL;
1048 MACAlgorithm ma = ssl.getSecurity().get_parms().mac_algorithm_;
1050 digest = NEW_YS SHA;
1052 digest = NEW_YS MD5;
1054 digest = NEW_YS RMD;
1058 for (
int i = 0; i < rounds; i++)
1059 digest->update(dummy, COMPRESS_LOWER);
1067 static int timing_verify(SSL& ssl,
const byte* input,
int padLen,
int t,
1070 byte verify[SHA_LEN];
1071 byte dummy[MAX_PAD_SIZE];
1073 memset(dummy, 1,
sizeof(dummy));
1075 if ( (t + padLen + 1) > pLen) {
1076 pad_check(dummy, (byte)padLen, MAX_PAD_SIZE);
1078 TLS_hmac(ssl, verify, input, pLen - t, application_data, 1);
1080 hmac(ssl, verify, input, pLen - t, application_data, 1);
1081 constant_compare(verify, input + pLen - t, t);
1086 if (pad_check(input + pLen - (padLen + 1), (byte)padLen, padLen + 1) != 0) {
1087 pad_check(dummy, (byte)padLen, MAX_PAD_SIZE - padLen - 1);
1089 TLS_hmac(ssl, verify, input, pLen - t, application_data, 1);
1091 hmac(ssl, verify, input, pLen - t, application_data, 1);
1092 constant_compare(verify, input + pLen - t, t);
1097 pad_check(dummy, (byte)padLen, MAX_PAD_SIZE - padLen - 1);
1099 TLS_hmac(ssl, verify, input, pLen - padLen - 1 - t, application_data,1);
1101 hmac(ssl, verify, input, pLen - padLen - 1 - t, application_data, 1);
1103 compress_rounds(ssl, get_rounds(pLen, padLen, t), dummy);
1105 if (constant_compare(verify, input + (pLen - padLen - 1 - t), t) != 0)
1113 void Data::Process(input_buffer& input, SSL& ssl)
1115 int msgSz = ssl.getSecurity().get_parms().encrypt_size_;
1116 int pad = 0, padSz = 0;
1118 int digestSz = ssl.getCrypto().get_digest().get_digestSize();
1119 const byte* rawData = input.get_buffer() + input.get_current();
1120 opaque verify[SHA_LEN];
1122 if (ssl.getSecurity().get_parms().cipher_type_ ==
block) {
1123 if (ssl.isTLSv1_1())
1124 ivExtra = ssl.getCrypto().get_cipher().get_blockSize();
1125 pad = *(input.get_buffer() + input.get_current() + msgSz -ivExtra - 1);
1129 if (timing_verify(ssl, rawData, pad,digestSz, msgSz-ivExtra) != 0) {
1130 ssl.SetError(verify_error);
1135 int sz3 = msgSz - digestSz - pad - 1;
1136 hmac(ssl, verify, rawData, sz3, application_data,
true);
1137 if (constant_compare(verify, rawData + sz3, digestSz) != 0) {
1138 ssl.SetError(verify_error);
1144 int streamSz = msgSz - digestSz;
1146 TLS_hmac(ssl, verify, rawData, streamSz, application_data,
true);
1148 hmac(ssl, verify, rawData, streamSz, application_data,
true);
1149 if (constant_compare(verify, rawData + streamSz, digestSz) != 0) {
1150 ssl.SetError(verify_error);
1155 int dataSz = msgSz - ivExtra - digestSz - pad - padSz;
1158 ssl.SetError(bad_input);
1164 if (ssl.CompressionOn()) {
1166 if (DeCompress(input, dataSz, tmp) == -1) {
1167 ssl.SetError(decompress_error);
1170 ssl.addData(NEW_YS input_buffer(tmp.get_size(),
1171 tmp.get_buffer(), tmp.get_size()));
1175 ssl.addData(data = NEW_YS input_buffer(dataSz));
1176 input.read(data->get_buffer(), dataSz);
1177 data->add_size(dataSz);
1182 input.set_current(input.get_current() + digestSz + pad + padSz);
1187 input_buffer& operator>>(input_buffer& input, HandShakeBase& hs)
1189 return hs.set(input);
1194 output_buffer& operator<<(output_buffer& output,
const HandShakeBase& hs)
1196 return hs.get(output);
1200 Certificate::Certificate(
const x509* cert) : cert_(cert)
1202 set_length(cert_->get_length() + 2 * CERT_HEADER);
1206 const opaque* Certificate::get_buffer()
const
1208 return cert_->get_buffer();
1213 output_buffer& operator<<(output_buffer& output,
const Certificate& cert)
1215 uint sz = cert.get_length() - 2 * CERT_HEADER;
1216 opaque tmp[CERT_HEADER];
1218 c32to24(sz + CERT_HEADER, tmp);
1219 output.write(tmp, CERT_HEADER);
1221 output.write(tmp, CERT_HEADER);
1222 output.write(cert.get_buffer(), sz);
1229 void Certificate::Process(input_buffer& input, SSL& ssl)
1231 CertManager& cm = ssl.useCrypto().use_certManager();
1236 if (input.get_remaining() <
sizeof(tmp)) {
1237 ssl.SetError(YasslError(bad_input));
1240 tmp[0] = input[AUTO];
1241 tmp[1] = input[AUTO];
1242 tmp[2] = input[AUTO];
1243 c24to32(tmp, list_sz);
1245 if (list_sz > (uint)MAX_RECORD_SIZE) {
1246 ssl.SetError(YasslError(bad_input));
1254 if (input.get_remaining() <
sizeof(tmp)) {
1255 ssl.SetError(YasslError(bad_input));
1258 tmp[0] = input[AUTO];
1259 tmp[1] = input[AUTO];
1260 tmp[2] = input[AUTO];
1261 c24to32(tmp, cert_sz);
1263 if (cert_sz > (uint)MAX_RECORD_SIZE || input.get_remaining() < cert_sz){
1264 ssl.SetError(YasslError(bad_input));
1268 cm.AddPeerCert(myCert = NEW_YS x509(cert_sz));
1269 input.read(myCert->use_buffer(), myCert->get_length());
1271 list_sz -= cert_sz + CERT_HEADER;
1273 if (
int err = cm.Validate())
1274 ssl.SetError(YasslError(err));
1275 else if (ssl.getSecurity().get_parms().entity_ == client_end)
1276 ssl.useStates().useClient() = serverCertComplete;
1280 Certificate::Certificate()
1285 input_buffer& Certificate::set(input_buffer& in)
1291 output_buffer& Certificate::get(output_buffer& out)
const
1293 return out << *
this;
1297 HandShakeType Certificate::get_type()
const
1303 ServerDHParams::ServerDHParams()
1304 : pSz_(0), gSz_(0), pubSz_(0), p_(0), g_(0), Ys_(0)
1308 ServerDHParams::~ServerDHParams()
1316 int ServerDHParams::get_pSize()
const
1322 int ServerDHParams::get_gSize()
const
1328 int ServerDHParams::get_pubSize()
const
1334 const opaque* ServerDHParams::get_p()
const
1340 const opaque* ServerDHParams::get_g()
const
1346 const opaque* ServerDHParams::get_pub()
const
1352 opaque* ServerDHParams::alloc_p(
int sz)
1354 p_ = NEW_YS opaque[pSz_ = sz];
1359 opaque* ServerDHParams::alloc_g(
int sz)
1361 g_ = NEW_YS opaque[gSz_ = sz];
1366 opaque* ServerDHParams::alloc_pub(
int sz)
1368 Ys_ = NEW_YS opaque[pubSz_ = sz];
1373 int ServerKeyBase::get_length()
const
1379 opaque* ServerKeyBase::get_serverKey()
const
1386 input_buffer& operator>>(input_buffer& input, ServerHello& hello)
1389 hello.server_version_.major_ = input[AUTO];
1390 hello.server_version_.minor_ = input[AUTO];
1393 input.read(hello.random_, RAN_LEN);
1396 hello.id_len_ = input[AUTO];
1398 input.read(hello.session_id_, hello.id_len_);
1401 hello.cipher_suite_[0] = input[AUTO];
1402 hello.cipher_suite_[1] = input[AUTO];
1405 hello.compression_method_ = CompressionMethod(input[AUTO]);
1412 output_buffer& operator<<(output_buffer& output,
const ServerHello& hello)
1415 output[AUTO] = hello.server_version_.major_;
1416 output[AUTO] = hello.server_version_.minor_;
1419 output.write(hello.random_, RAN_LEN);
1422 output[AUTO] = hello.id_len_;
1423 output.write(hello.session_id_, ID_LEN);
1426 output[AUTO] = hello.cipher_suite_[0];
1427 output[AUTO] = hello.cipher_suite_[1];
1430 output[AUTO] = hello.compression_method_;
1437 void ServerHello::Process(input_buffer&, SSL& ssl)
1439 if (ssl.GetMultiProtocol()) {
1440 if (ssl.isTLS() && server_version_.minor_ < 1)
1442 ssl.useSecurity().use_connection().TurnOffTLS();
1443 else if (ssl.isTLSv1_1() && server_version_.minor_ == 1)
1445 ssl.useSecurity().use_connection().TurnOffTLS1_1();
1447 else if (ssl.isTLSv1_1() && server_version_.minor_ < 2) {
1448 ssl.SetError(badVersion_error);
1451 else if (ssl.isTLS() && server_version_.minor_ < 1) {
1452 ssl.SetError(badVersion_error);
1455 else if (!ssl.isTLS() && (server_version_.major_ == 3 &&
1456 server_version_.minor_ >= 1)) {
1457 ssl.SetError(badVersion_error);
1460 ssl.set_pending(cipher_suite_[1]);
1461 ssl.set_random(random_, server_end);
1463 ssl.set_sessionID(session_id_);
1465 ssl.useSecurity().use_connection().sessionID_Set_ =
false;
1467 if (ssl.getSecurity().get_resuming()) {
1468 if (memcmp(session_id_, ssl.getSecurity().get_resume().GetID(),
1470 ssl.set_masterSecret(ssl.getSecurity().get_resume().GetSecret());
1472 ssl.deriveTLSKeys();
1475 ssl.useStates().useClient() = serverHelloDoneComplete;
1479 ssl.useSecurity().set_resuming(
false);
1480 ssl.useLog().Trace(
"server denied resumption");
1484 if (ssl.CompressionOn() && !compression_method_)
1485 ssl.UnSetCompression();
1487 ssl.useStates().useClient() = serverHelloComplete;
1491 ServerHello::ServerHello()
1493 memset(random_, 0, RAN_LEN);
1494 memset(session_id_, 0, ID_LEN);
1498 ServerHello::ServerHello(ProtocolVersion pv,
bool useCompression)
1499 : server_version_(pv),
1500 compression_method_(useCompression ? zlib : no_compression)
1502 memset(random_, 0, RAN_LEN);
1503 memset(session_id_, 0, ID_LEN);
1507 input_buffer& ServerHello::set(input_buffer& in)
1513 output_buffer& ServerHello::get(output_buffer& out)
const
1515 return out << *
this;
1519 HandShakeType ServerHello::get_type()
const
1521 return server_hello;
1525 const opaque* ServerHello::get_random()
const
1532 void ServerHelloDone::Process(input_buffer&, SSL& ssl)
1534 ssl.useStates().useClient() = serverHelloDoneComplete;
1538 ServerHelloDone::ServerHelloDone()
1544 input_buffer& ServerHelloDone::set(input_buffer& in)
1550 output_buffer& ServerHelloDone::get(output_buffer& out)
const
1556 HandShakeType ServerHelloDone::get_type()
const
1558 return server_hello_done;
1562 int ClientKeyBase::get_length()
const
1568 opaque* ClientKeyBase::get_clientKey()
const
1575 input_buffer& operator>>(input_buffer& input, ClientHello& hello)
1577 uint begin = input.get_current();
1580 hello.client_version_.major_ = input[AUTO];
1581 hello.client_version_.minor_ = input[AUTO];
1584 input.read(hello.random_, RAN_LEN);
1587 hello.id_len_ = input[AUTO];
1588 if (hello.id_len_) input.read(hello.session_id_, ID_LEN);
1593 tmp[0] = input[AUTO];
1594 tmp[1] = input[AUTO];
1597 hello.suite_len_ = min(len, static_cast<uint16>(MAX_SUITE_SZ));
1598 input.read(hello.cipher_suites_, hello.suite_len_);
1599 if (len > hello.suite_len_)
1600 input.set_current(input.get_current() + len - hello.suite_len_);
1603 hello.comp_len_ = input[AUTO];
1604 hello.compression_methods_ = no_compression;
1605 while (hello.comp_len_--) {
1606 CompressionMethod cm = CompressionMethod(input[AUTO]);
1608 hello.compression_methods_ = zlib;
1611 uint read = input.get_current() - begin;
1612 uint expected = hello.get_length();
1615 if (read < expected)
1616 input.set_current(input.get_current() + expected - read);
1623 output_buffer& operator<<(output_buffer& output,
const ClientHello& hello)
1626 output[AUTO] = hello.client_version_.major_;
1627 output[AUTO] = hello.client_version_.minor_;
1630 output.write(hello.random_, RAN_LEN);
1633 output[AUTO] = hello.id_len_;
1634 if (hello.id_len_) output.write(hello.session_id_, ID_LEN);
1638 c16toa(hello.suite_len_, tmp);
1639 output[AUTO] = tmp[0];
1640 output[AUTO] = tmp[1];
1641 output.write(hello.cipher_suites_, hello.suite_len_);
1644 output[AUTO] = hello.comp_len_;
1645 output[AUTO] = hello.compression_methods_;
1652 void ClientHello::Process(input_buffer&, SSL& ssl)
1655 ssl.useSecurity().use_connection().chVersion_ = client_version_;
1657 if (client_version_.major_ != 3) {
1658 ssl.SetError(badVersion_error);
1661 if (ssl.GetMultiProtocol()) {
1662 if (ssl.isTLS() && client_version_.minor_ < 1) {
1664 ssl.useSecurity().use_connection().TurnOffTLS();
1666 ProtocolVersion pv = ssl.getSecurity().get_connection().version_;
1667 bool removeDH = ssl.getSecurity().get_parms().removeDH_;
1668 bool removeRSA =
false;
1669 bool removeDSA =
false;
1671 const CertManager& cm = ssl.getCrypto().get_certManager();
1672 if (cm.get_keyType() == rsa_sa_algo)
1678 ssl.useSecurity().use_parms().SetSuites(pv, removeDH, removeRSA,
1681 else if (ssl.isTLSv1_1() && client_version_.minor_ == 1)
1683 ssl.useSecurity().use_connection().TurnOffTLS1_1();
1685 else if (ssl.isTLSv1_1() && client_version_.minor_ < 2) {
1686 ssl.SetError(badVersion_error);
1689 else if (ssl.isTLS() && client_version_.minor_ < 1) {
1690 ssl.SetError(badVersion_error);
1693 else if (!ssl.isTLS() && client_version_.minor_ >= 1) {
1694 ssl.SetError(badVersion_error);
1698 ssl.set_random(random_, client_end);
1701 SSL_SESSION* session = 0;
1702 if (!ssl.getSecurity().GetContext()->GetSessionCacheOff())
1703 session = GetSessions().lookup(session_id_);
1705 ssl.useLog().Trace(
"session lookup failed");
1708 ssl.set_session(session);
1709 ssl.useSecurity().set_resuming(
true);
1710 ssl.matchSuite(session->GetSuite(), SUITE_LEN);
1711 if (ssl.GetError())
return;
1712 ssl.set_pending(ssl.getSecurity().get_parms().suite_[1]);
1713 ssl.set_masterSecret(session->GetSecret());
1715 opaque serverRandom[RAN_LEN];
1716 ssl.getCrypto().get_random().Fill(serverRandom,
sizeof(serverRandom));
1717 ssl.set_random(serverRandom, server_end);
1719 ssl.deriveTLSKeys();
1722 ssl.useStates().useServer() = clientKeyExchangeComplete;
1725 ssl.matchSuite(cipher_suites_, suite_len_);
1726 if (ssl.GetError())
return;
1727 ssl.set_pending(ssl.getSecurity().get_parms().suite_[1]);
1729 if (compression_methods_ == zlib)
1730 ssl.SetCompression();
1732 ssl.useStates().useServer() = clientHelloComplete;
1736 input_buffer& ClientHello::set(input_buffer& in)
1742 output_buffer& ClientHello::get(output_buffer& out)
const
1744 return out << *
this;
1748 HandShakeType ClientHello::get_type()
const
1750 return client_hello;
1754 const opaque* ClientHello::get_random()
const
1760 ClientHello::ClientHello()
1762 memset(random_, 0, RAN_LEN);
1766 ClientHello::ClientHello(ProtocolVersion pv,
bool useCompression)
1767 : client_version_(pv),
1768 compression_methods_(useCompression ? zlib : no_compression)
1770 memset(random_, 0, RAN_LEN);
1775 output_buffer& operator<<(output_buffer& output,
const ServerKeyExchange& sk)
1777 output.write(sk.getKey(), sk.getKeyLength());
1783 void ServerKeyExchange::Process(input_buffer& input, SSL& ssl)
1786 if (ssl.GetError())
return;
1787 server_key_->read(ssl, input);
1789 ssl.useStates().useClient() = serverKeyExchangeComplete;
1793 ServerKeyExchange::ServerKeyExchange(SSL& ssl)
1799 ServerKeyExchange::ServerKeyExchange()
1804 ServerKeyExchange::~ServerKeyExchange()
1806 ysDelete(server_key_);
1810 void ServerKeyExchange::build(SSL& ssl)
1812 server_key_->build(ssl);
1813 set_length(server_key_->get_length());
1817 const opaque* ServerKeyExchange::getKey()
const
1819 return server_key_->get_serverKey();
1823 int ServerKeyExchange::getKeyLength()
const
1825 return server_key_->get_length();
1829 input_buffer& ServerKeyExchange::set(input_buffer& in)
1835 output_buffer& ServerKeyExchange::get(output_buffer& out)
const
1837 return out << *
this;
1841 HandShakeType ServerKeyExchange::get_type()
const
1843 return server_key_exchange;
1848 CertificateRequest::CertificateRequest()
1851 memset(certificate_types_, 0,
sizeof(certificate_types_));
1855 CertificateRequest::~CertificateRequest()
1858 STL::for_each(certificate_authorities_.begin(),
1859 certificate_authorities_.end(),
1864 void CertificateRequest::Build()
1866 certificate_types_[0] = rsa_sign;
1867 certificate_types_[1] = dss_sign;
1871 uint16 authCount = 0;
1874 for (
int j = 0; j < authCount; j++) {
1875 int sz = REQUEST_HEADER + MIN_DIS_SIZE;
1876 DistinguishedName dn;
1877 certificate_authorities_.push_back(dn = NEW_YS byte[sz]);
1879 opaque tmp[REQUEST_HEADER];
1880 c16toa(MIN_DIS_SIZE, tmp);
1881 memcpy(dn, tmp,
sizeof(tmp));
1884 memcpy(dn, tmp, MIN_DIS_SIZE);
1888 set_length(SIZEOF_ENUM + typeTotal_ + REQUEST_HEADER + authSz);
1892 input_buffer& CertificateRequest::set(input_buffer& in)
1898 output_buffer& CertificateRequest::get(output_buffer& out)
const
1900 return out << *
this;
1905 input_buffer& operator>>(input_buffer& input, CertificateRequest&
request)
1908 request.typeTotal_ = input[AUTO];
1909 for (
int i = 0; i < request.typeTotal_; i++)
1910 request.certificate_types_[i] = ClientCertificateType(input[AUTO]);
1912 byte tmp[REQUEST_HEADER];
1913 input.read(tmp,
sizeof(tmp));
1920 input.read(tmp,
sizeof(tmp));
1923 DistinguishedName dn;
1924 request.certificate_authorities_.push_back(dn = NEW_YS
1925 byte[REQUEST_HEADER + dnSz]);
1926 memcpy(dn, tmp, REQUEST_HEADER);
1927 input.read(&dn[REQUEST_HEADER], dnSz);
1929 sz -= dnSz + REQUEST_HEADER;
1937 output_buffer& operator<<(output_buffer& output,
1938 const CertificateRequest& request)
1941 output[AUTO] = request.typeTotal_;
1942 for (
int i = 0; i < request.typeTotal_; i++)
1943 output[AUTO] = request.certificate_types_[i];
1946 opaque tmp[REQUEST_HEADER];
1947 c16toa(request.get_length() - SIZEOF_ENUM -
1948 request.typeTotal_ - REQUEST_HEADER, tmp);
1949 output.write(tmp,
sizeof(tmp));
1951 STL::list<DistinguishedName>::const_iterator first =
1952 request.certificate_authorities_.begin();
1953 STL::list<DistinguishedName>::const_iterator last =
1954 request.certificate_authorities_.end();
1955 while (first != last) {
1958 output.write(*first, sz + REQUEST_HEADER);
1968 void CertificateRequest::Process(input_buffer&, SSL& ssl)
1970 CertManager& cm = ssl.useCrypto().use_certManager();
1973 if (cm.get_cert() && cm.get_privateKey())
1978 HandShakeType CertificateRequest::get_type()
const
1980 return certificate_request;
1985 CertificateVerify::CertificateVerify() : signature_(0)
1989 CertificateVerify::~CertificateVerify()
1991 ysArrayDelete(signature_);
1995 void CertificateVerify::Build(SSL& ssl)
1997 build_certHashes(ssl, hashes_);
2000 byte len[VERIFY_HEADER];
2004 const CertManager& cert = ssl.getCrypto().get_certManager();
2005 if (cert.get_keyType() == rsa_sa_algo) {
2006 RSA rsa(cert.get_privateKey(), cert.get_privateKeyLength(),
false);
2008 sz = rsa.get_cipherLength() + VERIFY_HEADER;
2009 sig.reset(NEW_YS byte[sz]);
2011 c16toa(sz - VERIFY_HEADER, len);
2012 memcpy(sig.get(), len, VERIFY_HEADER);
2013 rsa.sign(sig.get() + VERIFY_HEADER, hashes_.md5_,
sizeof(Hashes),
2014 ssl.getCrypto().get_random());
2017 DSS dss(cert.get_privateKey(), cert.get_privateKeyLength(),
false);
2019 sz = DSS_SIG_SZ + DSS_ENCODED_EXTRA + VERIFY_HEADER;
2020 sig.reset(NEW_YS byte[sz]);
2022 c16toa(sz - VERIFY_HEADER, len);
2023 memcpy(sig.get(), len, VERIFY_HEADER);
2024 dss.sign(sig.get() + VERIFY_HEADER, hashes_.sha_, SHA_LEN,
2025 ssl.getCrypto().get_random());
2027 byte encoded[DSS_SIG_SZ + DSS_ENCODED_EXTRA];
2028 TaoCrypt::EncodeDSA_Signature(sig.get() + VERIFY_HEADER, encoded);
2029 memcpy(sig.get() + VERIFY_HEADER, encoded,
sizeof(encoded));
2032 signature_ = sig.release();
2036 input_buffer& CertificateVerify::set(input_buffer& in)
2042 output_buffer& CertificateVerify::get(output_buffer& out)
const
2044 return out << *
this;
2049 input_buffer& operator>>(input_buffer& input, CertificateVerify& request)
2051 byte tmp[VERIFY_HEADER];
2052 input.read(tmp,
sizeof(tmp));
2056 request.set_length(sz);
2058 request.signature_ = NEW_YS byte[sz];
2059 input.read(request.signature_, sz);
2066 output_buffer& operator<<(output_buffer& output,
2067 const CertificateVerify& verify)
2069 output.write(verify.signature_, verify.get_length());
2076 void CertificateVerify::Process(input_buffer&, SSL& ssl)
2078 const Hashes& hashVerify = ssl.getHashes().get_certVerify();
2079 const CertManager& cert = ssl.getCrypto().get_certManager();
2081 if (cert.get_peerKeyType() == rsa_sa_algo) {
2082 RSA rsa(cert.get_peerKey(), cert.get_peerKeyLength());
2084 if (!rsa.verify(hashVerify.md5_,
sizeof(hashVerify), signature_,
2086 ssl.SetError(verify_error);
2089 byte decodedSig[DSS_SIG_SZ];
2090 TaoCrypt::DecodeDSA_Signature(decodedSig, signature_, get_length());
2092 DSS dss(cert.get_peerKey(), cert.get_peerKeyLength());
2093 if (!dss.verify(hashVerify.sha_, SHA_LEN, decodedSig, get_length()))
2094 ssl.SetError(verify_error);
2099 HandShakeType CertificateVerify::get_type()
const
2101 return certificate_verify;
2106 output_buffer& operator<<(output_buffer& output,
const ClientKeyExchange& ck)
2108 output.write(ck.getKey(), ck.getKeyLength());
2114 void ClientKeyExchange::Process(input_buffer& input, SSL& ssl)
2117 if (ssl.GetError())
return;
2118 client_key_->read(ssl, input);
2120 if (ssl.getCrypto().get_certManager().verifyPeer())
2121 build_certHashes(ssl, ssl.useHashes().use_certVerify());
2123 ssl.useStates().useServer() = clientKeyExchangeComplete;
2127 ClientKeyExchange::ClientKeyExchange(SSL& ssl)
2133 ClientKeyExchange::ClientKeyExchange()
2138 ClientKeyExchange::~ClientKeyExchange()
2140 ysDelete(client_key_);
2144 void ClientKeyExchange::build(SSL& ssl)
2146 client_key_->build(ssl);
2147 set_length(client_key_->get_length());
2150 const opaque* ClientKeyExchange::getKey()
const
2152 return client_key_->get_clientKey();
2156 int ClientKeyExchange::getKeyLength()
const
2158 return client_key_->get_length();
2162 input_buffer& ClientKeyExchange::set(input_buffer& in)
2168 output_buffer& ClientKeyExchange::get(output_buffer& out)
const
2170 return out << *
this;
2174 HandShakeType ClientKeyExchange::get_type()
const
2176 return client_key_exchange;
2181 input_buffer& operator>>(input_buffer& input, Finished&)
2189 output_buffer& operator<<(output_buffer& output,
const Finished& fin)
2191 if (fin.get_length() == FINISHED_SZ) {
2192 output.write(fin.hashes_.md5_, MD5_LEN);
2193 output.write(fin.hashes_.sha_, SHA_LEN);
2196 output.write(fin.hashes_.md5_, TLS_FINISHED_SZ);
2203 void Finished::Process(input_buffer& input, SSL& ssl)
2206 const Finished& verify = ssl.getHashes().get_verify();
2207 uint finishedSz = ssl.isTLS() ? TLS_FINISHED_SZ : FINISHED_SZ;
2209 input.read(hashes_.md5_, finishedSz);
2211 if (memcmp(&hashes_, &verify.hashes_, finishedSz)) {
2212 ssl.SetError(verify_error);
2217 opaque verifyMAC[SHA_LEN];
2218 uint macSz = finishedSz + HANDSHAKE_HEADER;
2221 TLS_hmac(ssl, verifyMAC, input.get_buffer() + input.get_current()
2222 - macSz, macSz, handshake,
true);
2224 hmac(ssl, verifyMAC, input.get_buffer() + input.get_current() - macSz,
2225 macSz, handshake,
true);
2228 opaque mac[SHA_LEN];
2229 int digestSz = ssl.getCrypto().get_digest().get_digestSize();
2230 input.read(mac, digestSz);
2233 if (ssl.getSecurity().get_parms().cipher_type_ ==
block)
2234 if (ssl.isTLSv1_1())
2235 ivExtra = ssl.getCrypto().get_cipher().get_blockSize();
2237 int padSz = ssl.getSecurity().get_parms().encrypt_size_ - ivExtra -
2238 HANDSHAKE_HEADER - finishedSz - digestSz;
2239 input.set_current(input.get_current() + padSz);
2242 if (memcmp(mac, verifyMAC, digestSz)) {
2243 ssl.SetError(verify_error);
2248 ssl.useStates().useHandShake() = handShakeReady;
2249 if (ssl.getSecurity().get_parms().entity_ == client_end)
2250 ssl.useStates().useClient() = serverFinishedComplete;
2252 ssl.useStates().useServer() = clientFinishedComplete;
2256 Finished::Finished()
2258 set_length(FINISHED_SZ);
2262 uint8* Finished::set_md5()
2264 return hashes_.md5_;
2268 uint8* Finished::set_sha()
2270 return hashes_.sha_;
2274 input_buffer& Finished::set(input_buffer& in)
2280 output_buffer& Finished::get(output_buffer& out)
const
2282 return out << *
this;
2286 HandShakeType Finished::get_type()
const
2292 void clean(
volatile opaque* p, uint sz, RandomPool& ran)
2296 for (i = 0; i < sz; ++
i)
2299 ran.Fill(const_cast<opaque*>(p), sz);
2301 for (i = 0; i < sz; ++
i)
2308 : pre_master_secret_(0), sequence_number_(0), peer_sequence_number_(0),
2309 pre_secret_len_(0), send_server_key_(false), master_clean_(false),
2310 TLS_(v.major_ >= 3 && v.minor_ >= 1),
2311 TLSv1_1_(v.major_ >= 3 && v.minor_ >= 2), compression_(false),
2312 version_(v), random_(ran)
2314 memset(sessionID_, 0,
sizeof(sessionID_));
2318 Connection::~Connection()
2320 CleanMaster(); CleanPreMaster(); ysArrayDelete(pre_master_secret_);
2324 void Connection::AllocPreSecret(uint sz)
2326 pre_master_secret_ = NEW_YS opaque[pre_secret_len_ = sz];
2330 void Connection::TurnOffTLS()
2333 version_.minor_ = 0;
2337 void Connection::TurnOffTLS1_1()
2340 version_.minor_ = 1;
2345 void Connection::CleanMaster()
2347 if (!master_clean_) {
2348 volatile opaque* p = master_secret_;
2349 clean(p, SECRET_LEN, random_);
2350 master_clean_ =
true;
2356 void Connection::CleanPreMaster()
2358 if (pre_master_secret_) {
2359 volatile opaque* p = pre_master_secret_;
2360 clean(p, pre_secret_len_, random_);
2362 ysArrayDelete(pre_master_secret_);
2363 pre_master_secret_ = 0;
2369 Message* CreateCipherSpec() {
return NEW_YS ChangeCipherSpec; }
2370 Message* CreateAlert() {
return NEW_YS Alert; }
2371 Message* CreateHandShake() {
return NEW_YS HandShakeHeader; }
2372 Message* CreateData() {
return NEW_YS
Data; }
2375 HandShakeBase* CreateHelloRequest() {
return NEW_YS HelloRequest; }
2376 HandShakeBase* CreateClientHello() {
return NEW_YS ClientHello; }
2377 HandShakeBase* CreateServerHello() {
return NEW_YS ServerHello; }
2378 HandShakeBase* CreateCertificate() {
return NEW_YS Certificate; }
2379 HandShakeBase* CreateServerKeyExchange() {
return NEW_YS ServerKeyExchange;}
2380 HandShakeBase* CreateCertificateRequest() {
return NEW_YS
2381 CertificateRequest; }
2382 HandShakeBase* CreateServerHelloDone() {
return NEW_YS ServerHelloDone; }
2383 HandShakeBase* CreateCertificateVerify() {
return NEW_YS CertificateVerify;}
2384 HandShakeBase* CreateClientKeyExchange() {
return NEW_YS ClientKeyExchange;}
2385 HandShakeBase* CreateFinished() {
return NEW_YS Finished; }
2388 ServerKeyBase* CreateRSAServerKEA() {
return NEW_YS RSA_Server; }
2389 ServerKeyBase* CreateDHServerKEA() {
return NEW_YS DH_Server; }
2390 ServerKeyBase* CreateFortezzaServerKEA() {
return NEW_YS Fortezza_Server; }
2393 ClientKeyBase* CreateRSAClient() {
return NEW_YS
2394 EncryptedPreMasterSecret; }
2395 ClientKeyBase* CreateDHClient() {
return NEW_YS
2396 ClientDiffieHellmanPublic; }
2397 ClientKeyBase* CreateFortezzaClient() {
return NEW_YS FortezzaKeys; }
2401 void InitMessageFactory(MessageFactory& mf)
2404 mf.Register(alert, CreateAlert);
2405 mf.Register(change_cipher_spec, CreateCipherSpec);
2406 mf.Register(handshake, CreateHandShake);
2407 mf.Register(application_data, CreateData);
2412 void InitHandShakeFactory(HandShakeFactory& hsf)
2415 hsf.Register(hello_request, CreateHelloRequest);
2416 hsf.Register(client_hello, CreateClientHello);
2417 hsf.Register(server_hello, CreateServerHello);
2418 hsf.Register(certificate, CreateCertificate);
2419 hsf.Register(server_key_exchange, CreateServerKeyExchange);
2420 hsf.Register(certificate_request, CreateCertificateRequest);
2421 hsf.Register(server_hello_done, CreateServerHelloDone);
2422 hsf.Register(certificate_verify, CreateCertificateVerify);
2423 hsf.Register(client_key_exchange, CreateClientKeyExchange);
2424 hsf.Register(finished, CreateFinished);
2429 void InitServerKeyFactory(ServerKeyFactory& skf)
2432 skf.Register(rsa_kea, CreateRSAServerKEA);
2433 skf.Register(diffie_hellman_kea, CreateDHServerKEA);
2434 skf.Register(fortezza_kea, CreateFortezzaServerKEA);
2439 void InitClientKeyFactory(ClientKeyFactory& ckf)
2442 ckf.Register(rsa_kea, CreateRSAClient);
2443 ckf.Register(diffie_hellman_kea, CreateDHClient);
2444 ckf.Register(fortezza_kea, CreateFortezzaClient);