MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
yassl_imp.cpp
1 /*
2  Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; see the file COPYING. If not, write to the
15  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16  MA 02110-1301 USA.
17 */
18 
19 /* yaSSL source implements all SSL.v3 secification structures.
20  */
21 
22 #include "runtime.hpp"
23 #include "yassl_int.hpp"
24 #include "handshake.hpp"
25 
26 #include "asn.hpp" // provide crypto wrapper??
27 
28 
29 
30 namespace yaSSL {
31 
32 
33 namespace { // locals
34 
35 bool isTLS(ProtocolVersion pv)
36 {
37  if (pv.major_ >= 3 && pv.minor_ >= 1)
38  return true;
39 
40  return false;
41 }
42 
43 
44 } // namespace (locals)
45 
46 
47 void hashHandShake(SSL&, const input_buffer&, uint);
48 
49 
50 ProtocolVersion::ProtocolVersion(uint8 maj, uint8 min)
51  : major_(maj), minor_(min)
52 {}
53 
54 
55 // construct key exchange with known ssl parms
56 void ClientKeyExchange::createKey(SSL& ssl)
57 {
58  const ClientKeyFactory& ckf = ssl.getFactory().getClientKey();
59  client_key_ = ckf.CreateObject(ssl.getSecurity().get_parms().kea_);
60 
61  if (!client_key_)
62  ssl.SetError(factory_error);
63 }
64 
65 
66 // construct key exchange with known ssl parms
67 void ServerKeyExchange::createKey(SSL& ssl)
68 {
69  const ServerKeyFactory& skf = ssl.getFactory().getServerKey();
70  server_key_ = skf.CreateObject(ssl.getSecurity().get_parms().kea_);
71 
72  if (!server_key_)
73  ssl.SetError(factory_error);
74 }
75 
76 
77 // build/set PreMaster secret and encrypt, client side
78 void EncryptedPreMasterSecret::build(SSL& ssl)
79 {
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_;
84  tmp[0] = pv.major_;
85  tmp[1] = pv.minor_;
86  ssl.set_preMaster(tmp, SECRET_LEN);
87 
88  const CertManager& cert = ssl.getCrypto().get_certManager();
89  RSA rsa(cert.get_peerKey(), cert.get_peerKeyLength());
90  bool tls = ssl.isTLS(); // if TLS, put length for encrypted data
91  alloc(rsa.get_cipherLength() + (tls ? 2 : 0));
92  byte* holder = secret_;
93  if (tls) {
94  byte len[2];
95  c16toa(rsa.get_cipherLength(), len);
96  memcpy(secret_, len, sizeof(len));
97  holder += 2;
98  }
99  rsa.encrypt(holder, tmp, SECRET_LEN, ssl.getCrypto().get_random());
100 }
101 
102 
103 // build/set premaster and Client Public key, client side
104 void ClientDiffieHellmanPublic::build(SSL& ssl)
105 {
106  DiffieHellman& dhServer = ssl.useCrypto().use_dh();
107  DiffieHellman dhClient(dhServer);
108 
109  uint keyLength = dhClient.get_agreedKeyLength(); // pub and agree same
110 
111  alloc(keyLength, true);
112  dhClient.makeAgreement(dhServer.get_publicKey(), keyLength);
113  c16toa(keyLength, Yc_);
114  memcpy(Yc_ + KEY_OFFSET, dhClient.get_publicKey(), keyLength);
115 
116  // because of encoding first byte might be zero, don't use it for preMaster
117  if (*dhClient.get_agreedKey() == 0)
118  ssl.set_preMaster(dhClient.get_agreedKey() + 1, keyLength - 1);
119  else
120  ssl.set_preMaster(dhClient.get_agreedKey(), keyLength);
121 }
122 
123 
124 // build server exhange, server side
125 void DH_Server::build(SSL& ssl)
126 {
127  DiffieHellman& dhServer = ssl.useCrypto().use_dh();
128 
129  int pSz, gSz, pubSz;
130  dhServer.set_sizes(pSz, gSz, pubSz);
131  dhServer.get_parms(parms_.alloc_p(pSz), parms_.alloc_g(gSz),
132  parms_.alloc_pub(pubSz));
133 
134  short sigSz = 0;
136  const CertManager& cert = ssl.getCrypto().get_certManager();
137 
138  if (ssl.getSecurity().get_parms().sig_algo_ == rsa_sa_algo) {
139  if (cert.get_keyType() != rsa_sa_algo) {
140  ssl.SetError(privateKey_error);
141  return;
142  }
143  auth.reset(NEW_YS RSA(cert.get_privateKey(),
144  cert.get_privateKeyLength(), false));
145  }
146  else {
147  if (cert.get_keyType() != dsa_sa_algo) {
148  ssl.SetError(privateKey_error);
149  return;
150  }
151  auth.reset(NEW_YS DSS(cert.get_privateKey(),
152  cert.get_privateKeyLength(), false));
153  sigSz += DSS_ENCODED_EXTRA;
154  }
155 
156  sigSz += auth->get_signatureLength();
157  if (!sigSz) {
158  ssl.SetError(privateKey_error);
159  return;
160  }
161 
162  length_ = 8; // pLen + gLen + YsLen + SigLen
163  length_ += pSz + gSz + pubSz + sigSz;
164 
165  output_buffer tmp(length_);
166  byte len[2];
167  // P
168  c16toa(pSz, len);
169  tmp.write(len, sizeof(len));
170  tmp.write(parms_.get_p(), pSz);
171  // G
172  c16toa(gSz, len);
173  tmp.write(len, sizeof(len));
174  tmp.write(parms_.get_g(), gSz);
175  // Ys
176  c16toa(pubSz, len);
177  tmp.write(len, sizeof(len));
178  tmp.write(parms_.get_pub(), pubSz);
179 
180  // Sig
181  byte hash[FINISHED_SZ];
182  MD5 md5;
183  SHA sha;
184  signature_ = NEW_YS byte[sigSz];
185 
186  const Connection& conn = ssl.getSecurity().get_connection();
187  // md5
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);
192 
193  // sha
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]);
198 
199  if (ssl.getSecurity().get_parms().sig_algo_ == rsa_sa_algo)
200  auth->sign(signature_, hash, sizeof(hash),
201  ssl.getCrypto().get_random());
202  else {
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));
208  }
209 
210  c16toa(sigSz, len);
211  tmp.write(len, sizeof(len));
212  tmp.write(signature_, sigSz);
213 
214  // key message
215  keyMessage_ = NEW_YS opaque[length_];
216  memcpy(keyMessage_, tmp.get_buffer(), tmp.get_size());
217 }
218 
219 
220 // read PreMaster secret and decrypt, server side
221 void EncryptedPreMasterSecret::read(SSL& ssl, input_buffer& input)
222 {
223  const CertManager& cert = ssl.getCrypto().get_certManager();
224  RSA rsa(cert.get_privateKey(), cert.get_privateKeyLength(), false);
225  uint16 cipherLen = rsa.get_cipherLength();
226  if (ssl.isTLS()) {
227  byte len[2];
228  input.read(len, sizeof(len));
229  ato16(len, cipherLen);
230  }
231  alloc(cipherLen);
232  input.read(secret_, length_);
233 
234  opaque preMasterSecret[SECRET_LEN];
235  rsa.decrypt(preMasterSecret, secret_, length_,
236  ssl.getCrypto().get_random());
237 
238  ProtocolVersion pv = ssl.getSecurity().get_connection().chVersion_;
239  if (pv.major_ != preMasterSecret[0] || pv.minor_ != preMasterSecret[1])
240  ssl.SetError(pms_version_error); // continue deriving for timing attack
241 
242  ssl.set_preMaster(preMasterSecret, SECRET_LEN);
243  ssl.makeMasterSecret();
244 }
245 
246 
247 EncryptedPreMasterSecret::EncryptedPreMasterSecret()
248  : secret_(0), length_(0)
249 {}
250 
251 
252 EncryptedPreMasterSecret::~EncryptedPreMasterSecret()
253 {
254  ysArrayDelete(secret_);
255 }
256 
257 
258 int EncryptedPreMasterSecret::get_length() const
259 {
260  return length_;
261 }
262 
263 
264 opaque* EncryptedPreMasterSecret::get_clientKey() const
265 {
266  return secret_;
267 }
268 
269 
270 void EncryptedPreMasterSecret::alloc(int sz)
271 {
272  length_ = sz;
273  secret_ = NEW_YS opaque[sz];
274 }
275 
276 
277 // read client's public key, server side
278 void ClientDiffieHellmanPublic::read(SSL& ssl, input_buffer& input)
279 {
280  DiffieHellman& dh = ssl.useCrypto().use_dh();
281 
282  uint16 keyLength;
283  byte tmp[2];
284  tmp[0] = input[AUTO];
285  tmp[1] = input[AUTO];
286  ato16(tmp, keyLength);
287 
288  alloc(keyLength);
289  input.read(Yc_, keyLength);
290  dh.makeAgreement(Yc_, keyLength);
291 
292  // because of encoding, first byte might be 0, don't use for preMaster
293  if (*dh.get_agreedKey() == 0)
294  ssl.set_preMaster(dh.get_agreedKey() + 1, dh.get_agreedKeyLength() - 1);
295  else
296  ssl.set_preMaster(dh.get_agreedKey(), dh.get_agreedKeyLength());
297  ssl.makeMasterSecret();
298 }
299 
300 
301 ClientDiffieHellmanPublic::ClientDiffieHellmanPublic()
302  : length_(0), Yc_(0)
303 {}
304 
305 
306 ClientDiffieHellmanPublic::~ClientDiffieHellmanPublic()
307 {
308  ysArrayDelete(Yc_);
309 }
310 
311 
312 int ClientDiffieHellmanPublic::get_length() const
313 {
314  return length_;
315 }
316 
317 
318 opaque* ClientDiffieHellmanPublic::get_clientKey() const
319 {
320  return Yc_;
321 }
322 
323 
324 void ClientDiffieHellmanPublic::alloc(int sz, bool offset)
325 {
326  length_ = sz + (offset ? KEY_OFFSET : 0);
327  Yc_ = NEW_YS opaque[length_];
328 }
329 
330 
331 // read server's p, g, public key and sig, client side
332 void DH_Server::read(SSL& ssl, input_buffer& input)
333 {
334  uint16 length, messageTotal = 6; // pSz + gSz + pubSz
335  byte tmp[2];
336 
337  // p
338  tmp[0] = input[AUTO];
339  tmp[1] = input[AUTO];
340  ato16(tmp, length);
341  messageTotal += length;
342 
343  input.read(parms_.alloc_p(length), length);
344 
345  // g
346  tmp[0] = input[AUTO];
347  tmp[1] = input[AUTO];
348  ato16(tmp, length);
349  messageTotal += length;
350 
351  input.read(parms_.alloc_g(length), length);
352 
353  // pub
354  tmp[0] = input[AUTO];
355  tmp[1] = input[AUTO];
356  ato16(tmp, length);
357  messageTotal += length;
358 
359  input.read(parms_.alloc_pub(length), length);
360 
361  // save message for hash verify
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);
366 
367  // signature
368  tmp[0] = input[AUTO];
369  tmp[1] = input[AUTO];
370  ato16(tmp, length);
371 
372  signature_ = NEW_YS byte[length];
373  input.read(signature_, length);
374 
375  // verify signature
376  byte hash[FINISHED_SZ];
377  MD5 md5;
378  SHA sha;
379 
380  const Connection& conn = ssl.getSecurity().get_connection();
381  // md5
382  md5.update(conn.client_random_, RAN_LEN);
383  md5.update(conn.server_random_, RAN_LEN);
384  md5.update(message.get_buffer(), message.get_size());
385  md5.get_digest(hash);
386 
387  // sha
388  sha.update(conn.client_random_, RAN_LEN);
389  sha.update(conn.server_random_, RAN_LEN);
390  sha.update(message.get_buffer(), message.get_size());
391  sha.get_digest(&hash[MD5_LEN]);
392 
393  const CertManager& cert = ssl.getCrypto().get_certManager();
394 
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);
399  }
400  else {
401  byte decodedSig[DSS_SIG_SZ];
402  length = TaoCrypt::DecodeDSA_Signature(decodedSig, signature_, length);
403 
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);
407  }
408 
409  // save input
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()));
414 }
415 
416 
417 DH_Server::DH_Server()
418  : signature_(0), length_(0), keyMessage_(0)
419 {}
420 
421 
422 DH_Server::~DH_Server()
423 {
424  ysArrayDelete(keyMessage_);
425  ysArrayDelete(signature_);
426 }
427 
428 
429 int DH_Server::get_length() const
430 {
431  return length_;
432 }
433 
434 
435 opaque* DH_Server::get_serverKey() const
436 {
437  return keyMessage_;
438 }
439 
440 
441 // set available suites
442 Parameters::Parameters(ConnectionEnd ce, const Ciphers& ciphers,
443  ProtocolVersion pv, bool haveDH) : entity_(ce)
444 {
445  pending_ = true; // suite not set yet
446  strncpy(cipher_name_, "NONE", 5);
447 
448  removeDH_ = !haveDH; // only use on server side for set suites
449 
450  if (ciphers.setSuites_) { // use user set list
451  suites_size_ = ciphers.suiteSz_;
452  memcpy(suites_, ciphers.suites_, ciphers.suiteSz_);
453  SetCipherNames();
454  }
455  else
456  SetSuites(pv, ce == server_end && removeDH_); // defaults
457 
458 }
459 
460 
461 void Parameters::SetSuites(ProtocolVersion pv, bool removeDH, bool removeRSA,
462  bool removeDSA)
463 {
464  int i = 0;
465  // available suites, best first
466  // when adding more, make sure cipher_names is updated and
467  // MAX_CIPHERS is big enough
468 
469  if (isTLS(pv)) {
470  if (!removeDH) {
471  if (!removeRSA) {
472  suites_[i++] = 0x00;
473  suites_[i++] = TLS_DHE_RSA_WITH_AES_256_CBC_SHA;
474  }
475  if (!removeDSA) {
476  suites_[i++] = 0x00;
477  suites_[i++] = TLS_DHE_DSS_WITH_AES_256_CBC_SHA;
478  }
479  }
480  if (!removeRSA) {
481  suites_[i++] = 0x00;
482  suites_[i++] = TLS_RSA_WITH_AES_256_CBC_SHA;
483  }
484  if (!removeDH) {
485  if (!removeRSA) {
486  suites_[i++] = 0x00;
487  suites_[i++] = TLS_DHE_RSA_WITH_AES_128_CBC_SHA;
488  }
489  if (!removeDSA) {
490  suites_[i++] = 0x00;
491  suites_[i++] = TLS_DHE_DSS_WITH_AES_128_CBC_SHA;
492  }
493  }
494  if (!removeRSA) {
495  suites_[i++] = 0x00;
496  suites_[i++] = TLS_RSA_WITH_AES_128_CBC_SHA;
497  suites_[i++] = 0x00;
498  suites_[i++] = TLS_RSA_WITH_AES_256_CBC_RMD160;
499  suites_[i++] = 0x00;
500  suites_[i++] = TLS_RSA_WITH_AES_128_CBC_RMD160;
501  suites_[i++] = 0x00;
502  suites_[i++] = TLS_RSA_WITH_3DES_EDE_CBC_RMD160;
503  }
504  if (!removeDH) {
505  if (!removeRSA) {
506  suites_[i++] = 0x00;
507  suites_[i++] = TLS_DHE_RSA_WITH_AES_256_CBC_RMD160;
508  suites_[i++] = 0x00;
509  suites_[i++] = TLS_DHE_RSA_WITH_AES_128_CBC_RMD160;
510  suites_[i++] = 0x00;
511  suites_[i++] = TLS_DHE_RSA_WITH_3DES_EDE_CBC_RMD160;
512  }
513  if (!removeDSA) {
514  suites_[i++] = 0x00;
515  suites_[i++] = TLS_DHE_DSS_WITH_AES_256_CBC_RMD160;
516  suites_[i++] = 0x00;
517  suites_[i++] = TLS_DHE_DSS_WITH_AES_128_CBC_RMD160;
518  suites_[i++] = 0x00;
519  suites_[i++] = TLS_DHE_DSS_WITH_3DES_EDE_CBC_RMD160;
520  }
521  }
522  }
523 
524  if (!removeRSA) {
525  suites_[i++] = 0x00;
526  suites_[i++] = SSL_RSA_WITH_RC4_128_SHA;
527  suites_[i++] = 0x00;
528  suites_[i++] = SSL_RSA_WITH_RC4_128_MD5;
529 
530  suites_[i++] = 0x00;
531  suites_[i++] = SSL_RSA_WITH_3DES_EDE_CBC_SHA;
532  suites_[i++] = 0x00;
533  suites_[i++] = SSL_RSA_WITH_DES_CBC_SHA;
534  }
535  if (!removeDH) {
536  if (!removeRSA) {
537  suites_[i++] = 0x00;
538  suites_[i++] = SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA;
539  }
540  if (!removeDSA) {
541  suites_[i++] = 0x00;
542  suites_[i++] = SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA;
543  }
544  if (!removeRSA) {
545  suites_[i++] = 0x00;
546  suites_[i++] = SSL_DHE_RSA_WITH_DES_CBC_SHA;
547  }
548  if (!removeDSA) {
549  suites_[i++] = 0x00;
550  suites_[i++] = SSL_DHE_DSS_WITH_DES_CBC_SHA;
551  }
552  }
553 
554  suites_size_ = i;
555 
556  SetCipherNames();
557 }
558 
559 
560 void Parameters::SetCipherNames()
561 {
562  const int suites = suites_size_ / 2;
563  int pos = 0;
564 
565  for (int j = 0; j < suites; j++) {
566  int index = suites_[j*2 + 1]; // every other suite is suite id
567  size_t len = strlen(cipher_names[index]) + 1;
568  strncpy(cipher_list_[pos++], cipher_names[index], len);
569  }
570  cipher_list_[pos][0] = 0;
571 }
572 
573 
574 // input operator for RecordLayerHeader, adjust stream
575 input_buffer& operator>>(input_buffer& input, RecordLayerHeader& hdr)
576 {
577  hdr.type_ = ContentType(input[AUTO]);
578  hdr.version_.major_ = input[AUTO];
579  hdr.version_.minor_ = input[AUTO];
580 
581  // length
582  byte tmp[2];
583  tmp[0] = input[AUTO];
584  tmp[1] = input[AUTO];
585  ato16(tmp, hdr.length_);
586 
587  return input;
588 }
589 
590 
591 // output operator for RecordLayerHeader
592 output_buffer& operator<<(output_buffer& output, const RecordLayerHeader& hdr)
593 {
594  output[AUTO] = hdr.type_;
595  output[AUTO] = hdr.version_.major_;
596  output[AUTO] = hdr.version_.minor_;
597 
598  // length
599  byte tmp[2];
600  c16toa(hdr.length_, tmp);
601  output[AUTO] = tmp[0];
602  output[AUTO] = tmp[1];
603 
604  return output;
605 }
606 
607 
608 // virtual input operator for Messages
609 input_buffer& operator>>(input_buffer& input, Message& msg)
610 {
611  return msg.set(input);
612 }
613 
614 // virtual output operator for Messages
615 output_buffer& operator<<(output_buffer& output, const Message& msg)
616 {
617  return msg.get(output);
618 }
619 
620 
621 // input operator for HandShakeHeader
622 input_buffer& operator>>(input_buffer& input, HandShakeHeader& hs)
623 {
624  hs.type_ = HandShakeType(input[AUTO]);
625 
626  hs.length_[0] = input[AUTO];
627  hs.length_[1] = input[AUTO];
628  hs.length_[2] = input[AUTO];
629 
630  return input;
631 }
632 
633 
634 // output operator for HandShakeHeader
635 output_buffer& operator<<(output_buffer& output, const HandShakeHeader& hdr)
636 {
637  output[AUTO] = hdr.type_;
638  output.write(hdr.length_, sizeof(hdr.length_));
639  return output;
640 }
641 
642 
643 // HandShake Header Processing function
644 void HandShakeHeader::Process(input_buffer& input, SSL& ssl)
645 {
646  ssl.verifyState(*this);
647  if (ssl.GetError()) return;
648  const HandShakeFactory& hsf = ssl.getFactory().getHandShake();
649  mySTL::auto_ptr<HandShakeBase> hs(hsf.CreateObject(type_));
650  if (!hs.get()) {
651  ssl.SetError(factory_error);
652  return;
653  }
654 
655  uint len = c24to32(length_);
656  if (len > input.get_remaining()) {
657  ssl.SetError(bad_input);
658  return;
659  }
660  hashHandShake(ssl, input, len);
661 
662  hs->set_length(len);
663  input >> *hs;
664  hs->Process(input, ssl);
665 }
666 
667 
668 ContentType HandShakeHeader::get_type() const
669 {
670  return handshake;
671 }
672 
673 
674 uint16 HandShakeHeader::get_length() const
675 {
676  return c24to32(length_);
677 }
678 
679 
680 HandShakeType HandShakeHeader::get_handshakeType() const
681 {
682  return type_;
683 }
684 
685 
686 void HandShakeHeader::set_type(HandShakeType hst)
687 {
688  type_ = hst;
689 }
690 
691 
692 void HandShakeHeader::set_length(uint32 u32)
693 {
694  c32to24(u32, length_);
695 }
696 
697 
698 input_buffer& HandShakeHeader::set(input_buffer& in)
699 {
700  return in >> *this;
701 }
702 
703 
704 output_buffer& HandShakeHeader::get(output_buffer& out) const
705 {
706  return out << *this;
707 }
708 
709 
710 
711 int HandShakeBase::get_length() const
712 {
713  return length_;
714 }
715 
716 
717 void HandShakeBase::set_length(int l)
718 {
719  length_ = l;
720 }
721 
722 
723 // for building buffer's type field
724 HandShakeType HandShakeBase::get_type() const
725 {
726  return no_shake;
727 }
728 
729 
730 input_buffer& HandShakeBase::set(input_buffer& in)
731 {
732  return in;
733 }
734 
735 
736 output_buffer& HandShakeBase::get(output_buffer& out) const
737 {
738  return out;
739 }
740 
741 
742 void HandShakeBase::Process(input_buffer&, SSL&)
743 {}
744 
745 
746 input_buffer& HelloRequest::set(input_buffer& in)
747 {
748  return in;
749 }
750 
751 
752 output_buffer& HelloRequest::get(output_buffer& out) const
753 {
754  return out;
755 }
756 
757 
758 void HelloRequest::Process(input_buffer&, SSL&)
759 {}
760 
761 
762 HandShakeType HelloRequest::get_type() const
763 {
764  return hello_request;
765 }
766 
767 
768 // input operator for CipherSpec
769 input_buffer& operator>>(input_buffer& input, ChangeCipherSpec& cs)
770 {
771  cs.type_ = CipherChoice(input[AUTO]);
772  return input;
773 }
774 
775 // output operator for CipherSpec
776 output_buffer& operator<<(output_buffer& output, const ChangeCipherSpec& cs)
777 {
778  output[AUTO] = cs.type_;
779  return output;
780 }
781 
782 
783 ChangeCipherSpec::ChangeCipherSpec()
784  : type_(change_cipher_spec_choice)
785 {}
786 
787 
788 input_buffer& ChangeCipherSpec::set(input_buffer& in)
789 {
790  return in >> *this;
791 }
792 
793 
794 output_buffer& ChangeCipherSpec::get(output_buffer& out) const
795 {
796  return out << *this;
797 }
798 
799 
800 ContentType ChangeCipherSpec::get_type() const
801 {
802  return change_cipher_spec;
803 }
804 
805 
806 uint16 ChangeCipherSpec::get_length() const
807 {
808  return SIZEOF_ENUM;
809 }
810 
811 
812 // CipherSpec processing handler
813 void ChangeCipherSpec::Process(input_buffer&, SSL& ssl)
814 {
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); // server
819  }
820  else if (ssl.getSecurity().get_parms().entity_ == server_end)
821  buildFinished(ssl, ssl.useHashes().use_verify(), client); // client
822 }
823 
824 
825 Alert::Alert(AlertLevel al, AlertDescription ad)
826  : level_(al), description_(ad)
827 {}
828 
829 
830 ContentType Alert::get_type() const
831 {
832  return alert;
833 }
834 
835 
836 uint16 Alert::get_length() const
837 {
838  return SIZEOF_ENUM * 2;
839 }
840 
841 
842 input_buffer& Alert::set(input_buffer& in)
843 {
844  return in >> *this;
845 }
846 
847 
848 output_buffer& Alert::get(output_buffer& out) const
849 {
850  return out << *this;
851 }
852 
853 
854 // input operator for Alert
855 input_buffer& operator>>(input_buffer& input, Alert& a)
856 {
857  a.level_ = AlertLevel(input[AUTO]);
858  a.description_ = AlertDescription(input[AUTO]);
859 
860  return input;
861 }
862 
863 
864 // output operator for Alert
865 output_buffer& operator<<(output_buffer& output, const Alert& a)
866 {
867  output[AUTO] = a.level_;
868  output[AUTO] = a.description_;
869  return output;
870 }
871 
872 
873 // Alert processing handler
874 void Alert::Process(input_buffer& input, SSL& ssl)
875 {
876  if (ssl.getSecurity().get_parms().pending_ == false) { // encrypted alert
877  int aSz = get_length(); // alert size already read on input
878  opaque verify[SHA_LEN];
879  const opaque* data = input.get_buffer() + input.get_current() - aSz;
880 
881  if (ssl.isTLS())
882  TLS_hmac(ssl, verify, data, aSz, alert, true);
883  else
884  hmac(ssl, verify, data, aSz, alert, true);
885 
886  // read mac and skip fill
887  int digestSz = ssl.getCrypto().get_digest().get_digestSize();
888  opaque mac[SHA_LEN];
889  input.read(mac, digestSz);
890 
891  if (ssl.getSecurity().get_parms().cipher_type_ == block) {
892  int ivExtra = 0;
893 
894  if (ssl.isTLSv1_1())
895  ivExtra = ssl.getCrypto().get_cipher().get_blockSize();
896  int padSz = ssl.getSecurity().get_parms().encrypt_size_ - ivExtra -
897  aSz - digestSz;
898  input.set_current(input.get_current() + padSz);
899  }
900 
901  // verify
902  if (memcmp(mac, verify, digestSz)) {
903  ssl.SetError(verify_error);
904  return;
905  }
906  }
907  if (level_ == fatal) {
908  ssl.useStates().useRecord() = recordNotReady;
909  ssl.useStates().useHandShake() = handShakeNotReady;
910  ssl.SetError(YasslError(description_));
911  }
912 }
913 
914 
915 Data::Data()
916  : length_(0), buffer_(0), write_buffer_(0)
917 {}
918 
919 
920 Data::Data(uint16 len, opaque* b)
921  : length_(len), buffer_(b), write_buffer_(0)
922 {}
923 
924 
925 void Data::SetData(uint16 len, const opaque* buffer)
926 {
927  length_ = len;
928  write_buffer_ = buffer;
929 }
930 
931 input_buffer& Data::set(input_buffer& in)
932 {
933  return in;
934 }
935 
936 
937 output_buffer& Data::get(output_buffer& out) const
938 {
939  return out << *this;
940 }
941 
942 
943 ContentType Data::get_type() const
944 {
945  return application_data;
946 }
947 
948 
949 uint16 Data::get_length() const
950 {
951  return length_;
952 }
953 
954 
955 void Data::set_length(uint16 l)
956 {
957  length_ = l;
958 }
959 
960 
961 opaque* Data::set_buffer()
962 {
963  return buffer_;
964 }
965 
966 
967 // output operator for Data
968 output_buffer& operator<<(output_buffer& output, const Data& data)
969 {
970  output.write(data.write_buffer_, data.length_);
971  return output;
972 }
973 
974 
975 // check all bytes for equality
976 static int constant_compare(const byte* a, const byte* b, int len)
977 {
978  int good = 0;
979  int bad = 0;
980 
981  for (int i = 0; i < len; i++) {
982  if (a[i] == b[i])
983  good++;
984  else
985  bad++;
986  }
987 
988  if (good == len)
989  return 0;
990  else
991  return 0 - bad; // failure
992 }
993 
994 
995 // check bytes for pad value
996 static int pad_check(const byte* input, byte pad, int len)
997 {
998  int good = 0;
999  int bad = 0;
1000 
1001  for (int i = 0; i < len; i++) {
1002  if (input[i] == pad)
1003  good++;
1004  else
1005  bad++;
1006  }
1007 
1008  if (good == len)
1009  return 0;
1010  else
1011  return 0 - bad; // failure
1012 }
1013 
1014 
1015 // get number of compression rounds
1016 static inline int get_rounds(int pLen, int padLen, int t)
1017 {
1018  int roundL1 = 1; // round ups
1019  int roundL2 = 1;
1020 
1021  int L1 = COMPRESS_CONSTANT + pLen - t;
1022  int L2 = COMPRESS_CONSTANT + pLen - padLen - 1 - t;
1023 
1024  L1 -= COMPRESS_UPPER;
1025  L2 -= COMPRESS_UPPER;
1026 
1027  if ( (L1 % COMPRESS_LOWER) == 0)
1028  roundL1 = 0;
1029  if ( (L2 % COMPRESS_LOWER) == 0)
1030  roundL2 = 0;
1031 
1032  L1 /= COMPRESS_LOWER;
1033  L2 /= COMPRESS_LOWER;
1034 
1035  L1 += roundL1;
1036  L2 += roundL2;
1037 
1038  return L1 - L2;
1039 }
1040 
1041 
1042 // do compression rounds on dummy data
1043 static inline void compress_rounds(SSL& ssl, int rounds, const byte* dummy)
1044 {
1045  if (rounds) {
1046  Digest* digest = NULL;
1047 
1048  MACAlgorithm ma = ssl.getSecurity().get_parms().mac_algorithm_;
1049  if (ma == sha)
1050  digest = NEW_YS SHA;
1051  else if (ma == md5)
1052  digest = NEW_YS MD5;
1053  else if (ma == rmd)
1054  digest = NEW_YS RMD;
1055  else
1056  return;
1057 
1058  for (int i = 0; i < rounds; i++)
1059  digest->update(dummy, COMPRESS_LOWER);
1060 
1061  ysDelete(digest);
1062  }
1063 }
1064 
1065 
1066 // timing resistant pad verification
1067 static int timing_verify(SSL& ssl, const byte* input, int padLen, int t,
1068  int pLen)
1069 {
1070  byte verify[SHA_LEN];
1071  byte dummy[MAX_PAD_SIZE];
1072 
1073  memset(dummy, 1, sizeof(dummy));
1074 
1075  if ( (t + padLen + 1) > pLen) {
1076  pad_check(dummy, (byte)padLen, MAX_PAD_SIZE);
1077  if (ssl.isTLS())
1078  TLS_hmac(ssl, verify, input, pLen - t, application_data, 1);
1079  else
1080  hmac(ssl, verify, input, pLen - t, application_data, 1);
1081  constant_compare(verify, input + pLen - t, t);
1082 
1083  return -1;
1084  }
1085 
1086  if (pad_check(input + pLen - (padLen + 1), (byte)padLen, padLen + 1) != 0) {
1087  pad_check(dummy, (byte)padLen, MAX_PAD_SIZE - padLen - 1);
1088  if (ssl.isTLS())
1089  TLS_hmac(ssl, verify, input, pLen - t, application_data, 1);
1090  else
1091  hmac(ssl, verify, input, pLen - t, application_data, 1);
1092  constant_compare(verify, input + pLen - t, t);
1093 
1094  return -1;
1095  }
1096 
1097  pad_check(dummy, (byte)padLen, MAX_PAD_SIZE - padLen - 1);
1098  if (ssl.isTLS())
1099  TLS_hmac(ssl, verify, input, pLen - padLen - 1 - t, application_data,1);
1100  else
1101  hmac(ssl, verify, input, pLen - padLen - 1 - t, application_data, 1);
1102 
1103  compress_rounds(ssl, get_rounds(pLen, padLen, t), dummy);
1104 
1105  if (constant_compare(verify, input + (pLen - padLen - 1 - t), t) != 0)
1106  return -1;
1107 
1108  return 0;
1109 }
1110 
1111 
1112 // Process handler for Data
1113 void Data::Process(input_buffer& input, SSL& ssl)
1114 {
1115  int msgSz = ssl.getSecurity().get_parms().encrypt_size_;
1116  int pad = 0, padSz = 0;
1117  int ivExtra = 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];
1121 
1122  if (ssl.getSecurity().get_parms().cipher_type_ == block) {
1123  if (ssl.isTLSv1_1()) // IV
1124  ivExtra = ssl.getCrypto().get_cipher().get_blockSize();
1125  pad = *(input.get_buffer() + input.get_current() + msgSz -ivExtra - 1);
1126  padSz = 1;
1127 
1128  if (ssl.isTLS()) {
1129  if (timing_verify(ssl, rawData, pad,digestSz, msgSz-ivExtra) != 0) {
1130  ssl.SetError(verify_error);
1131  return;
1132  }
1133  }
1134  else { // SSLv3, some don't do this padding right
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);
1139  return;
1140  }
1141  }
1142  }
1143  else { // stream
1144  int streamSz = msgSz - digestSz;
1145  if (ssl.isTLS())
1146  TLS_hmac(ssl, verify, rawData, streamSz, application_data, true);
1147  else
1148  hmac(ssl, verify, rawData, streamSz, application_data, true);
1149  if (constant_compare(verify, rawData + streamSz, digestSz) != 0) {
1150  ssl.SetError(verify_error);
1151  return;
1152  }
1153  }
1154 
1155  int dataSz = msgSz - ivExtra - digestSz - pad - padSz;
1156 
1157  if (dataSz < 0) {
1158  ssl.SetError(bad_input);
1159  return;
1160  }
1161 
1162  // read data
1163  if (dataSz) { // could be compressed
1164  if (ssl.CompressionOn()) {
1165  input_buffer tmp;
1166  if (DeCompress(input, dataSz, tmp) == -1) {
1167  ssl.SetError(decompress_error);
1168  return;
1169  }
1170  ssl.addData(NEW_YS input_buffer(tmp.get_size(),
1171  tmp.get_buffer(), tmp.get_size()));
1172  }
1173  else {
1174  input_buffer* data;
1175  ssl.addData(data = NEW_YS input_buffer(dataSz));
1176  input.read(data->get_buffer(), dataSz);
1177  data->add_size(dataSz);
1178  }
1179  }
1180 
1181  // advance past mac and fill
1182  input.set_current(input.get_current() + digestSz + pad + padSz);
1183 }
1184 
1185 
1186 // virtual input operator for HandShakes
1187 input_buffer& operator>>(input_buffer& input, HandShakeBase& hs)
1188 {
1189  return hs.set(input);
1190 }
1191 
1192 
1193 // virtual output operator for HandShakes
1194 output_buffer& operator<<(output_buffer& output, const HandShakeBase& hs)
1195 {
1196  return hs.get(output);
1197 }
1198 
1199 
1200 Certificate::Certificate(const x509* cert) : cert_(cert)
1201 {
1202  set_length(cert_->get_length() + 2 * CERT_HEADER); // list and cert size
1203 }
1204 
1205 
1206 const opaque* Certificate::get_buffer() const
1207 {
1208  return cert_->get_buffer();
1209 }
1210 
1211 
1212 // output operator for Certificate
1213 output_buffer& operator<<(output_buffer& output, const Certificate& cert)
1214 {
1215  uint sz = cert.get_length() - 2 * CERT_HEADER;
1216  opaque tmp[CERT_HEADER];
1217 
1218  c32to24(sz + CERT_HEADER, tmp);
1219  output.write(tmp, CERT_HEADER);
1220  c32to24(sz, tmp);
1221  output.write(tmp, CERT_HEADER);
1222  output.write(cert.get_buffer(), sz);
1223 
1224  return output;
1225 }
1226 
1227 
1228 // certificate processing handler
1229 void Certificate::Process(input_buffer& input, SSL& ssl)
1230 {
1231  CertManager& cm = ssl.useCrypto().use_certManager();
1232 
1233  uint32 list_sz;
1234  byte tmp[3];
1235 
1236  if (input.get_remaining() < sizeof(tmp)) {
1237  ssl.SetError(YasslError(bad_input));
1238  return;
1239  }
1240  tmp[0] = input[AUTO];
1241  tmp[1] = input[AUTO];
1242  tmp[2] = input[AUTO];
1243  c24to32(tmp, list_sz);
1244 
1245  if (list_sz > (uint)MAX_RECORD_SIZE) { // sanity check
1246  ssl.SetError(YasslError(bad_input));
1247  return;
1248  }
1249 
1250  while (list_sz) {
1251  // cert size
1252  uint32 cert_sz;
1253 
1254  if (input.get_remaining() < sizeof(tmp)) {
1255  ssl.SetError(YasslError(bad_input));
1256  return;
1257  }
1258  tmp[0] = input[AUTO];
1259  tmp[1] = input[AUTO];
1260  tmp[2] = input[AUTO];
1261  c24to32(tmp, cert_sz);
1262 
1263  if (cert_sz > (uint)MAX_RECORD_SIZE || input.get_remaining() < cert_sz){
1264  ssl.SetError(YasslError(bad_input));
1265  return;
1266  }
1267  x509* myCert;
1268  cm.AddPeerCert(myCert = NEW_YS x509(cert_sz));
1269  input.read(myCert->use_buffer(), myCert->get_length());
1270 
1271  list_sz -= cert_sz + CERT_HEADER;
1272  }
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;
1277 }
1278 
1279 
1280 Certificate::Certificate()
1281  : cert_(0)
1282 {}
1283 
1284 
1285 input_buffer& Certificate::set(input_buffer& in)
1286 {
1287  return in;
1288 }
1289 
1290 
1291 output_buffer& Certificate::get(output_buffer& out) const
1292 {
1293  return out << *this;
1294 }
1295 
1296 
1297 HandShakeType Certificate::get_type() const
1298 {
1299  return certificate;
1300 }
1301 
1302 
1303 ServerDHParams::ServerDHParams()
1304  : pSz_(0), gSz_(0), pubSz_(0), p_(0), g_(0), Ys_(0)
1305 {}
1306 
1307 
1308 ServerDHParams::~ServerDHParams()
1309 {
1310  ysArrayDelete(Ys_);
1311  ysArrayDelete(g_);
1312  ysArrayDelete(p_);
1313 }
1314 
1315 
1316 int ServerDHParams::get_pSize() const
1317 {
1318  return pSz_;
1319 }
1320 
1321 
1322 int ServerDHParams::get_gSize() const
1323 {
1324  return gSz_;
1325 }
1326 
1327 
1328 int ServerDHParams::get_pubSize() const
1329 {
1330  return pubSz_;
1331 }
1332 
1333 
1334 const opaque* ServerDHParams::get_p() const
1335 {
1336  return p_;
1337 }
1338 
1339 
1340 const opaque* ServerDHParams::get_g() const
1341 {
1342  return g_;
1343 }
1344 
1345 
1346 const opaque* ServerDHParams::get_pub() const
1347 {
1348  return Ys_;
1349 }
1350 
1351 
1352 opaque* ServerDHParams::alloc_p(int sz)
1353 {
1354  p_ = NEW_YS opaque[pSz_ = sz];
1355  return p_;
1356 }
1357 
1358 
1359 opaque* ServerDHParams::alloc_g(int sz)
1360 {
1361  g_ = NEW_YS opaque[gSz_ = sz];
1362  return g_;
1363 }
1364 
1365 
1366 opaque* ServerDHParams::alloc_pub(int sz)
1367 {
1368  Ys_ = NEW_YS opaque[pubSz_ = sz];
1369  return Ys_;
1370 }
1371 
1372 
1373 int ServerKeyBase::get_length() const
1374 {
1375  return 0;
1376 }
1377 
1378 
1379 opaque* ServerKeyBase::get_serverKey() const
1380 {
1381  return 0;
1382 }
1383 
1384 
1385 // input operator for ServerHello
1386 input_buffer& operator>>(input_buffer& input, ServerHello& hello)
1387 {
1388  // Protocol
1389  hello.server_version_.major_ = input[AUTO];
1390  hello.server_version_.minor_ = input[AUTO];
1391 
1392  // Random
1393  input.read(hello.random_, RAN_LEN);
1394 
1395  // Session
1396  hello.id_len_ = input[AUTO];
1397  if (hello.id_len_)
1398  input.read(hello.session_id_, hello.id_len_);
1399 
1400  // Suites
1401  hello.cipher_suite_[0] = input[AUTO];
1402  hello.cipher_suite_[1] = input[AUTO];
1403 
1404  // Compression
1405  hello.compression_method_ = CompressionMethod(input[AUTO]);
1406 
1407  return input;
1408 }
1409 
1410 
1411 // output operator for ServerHello
1412 output_buffer& operator<<(output_buffer& output, const ServerHello& hello)
1413 {
1414  // Protocol
1415  output[AUTO] = hello.server_version_.major_;
1416  output[AUTO] = hello.server_version_.minor_;
1417 
1418  // Random
1419  output.write(hello.random_, RAN_LEN);
1420 
1421  // Session
1422  output[AUTO] = hello.id_len_;
1423  output.write(hello.session_id_, ID_LEN);
1424 
1425  // Suites
1426  output[AUTO] = hello.cipher_suite_[0];
1427  output[AUTO] = hello.cipher_suite_[1];
1428 
1429  // Compression
1430  output[AUTO] = hello.compression_method_;
1431 
1432  return output;
1433 }
1434 
1435 
1436 // Server Hello processing handler
1437 void ServerHello::Process(input_buffer&, SSL& ssl)
1438 {
1439  if (ssl.GetMultiProtocol()) { // SSLv23 support
1440  if (ssl.isTLS() && server_version_.minor_ < 1)
1441  // downgrade to SSLv3
1442  ssl.useSecurity().use_connection().TurnOffTLS();
1443  else if (ssl.isTLSv1_1() && server_version_.minor_ == 1)
1444  // downdrage to TLSv1
1445  ssl.useSecurity().use_connection().TurnOffTLS1_1();
1446  }
1447  else if (ssl.isTLSv1_1() && server_version_.minor_ < 2) {
1448  ssl.SetError(badVersion_error);
1449  return;
1450  }
1451  else if (ssl.isTLS() && server_version_.minor_ < 1) {
1452  ssl.SetError(badVersion_error);
1453  return;
1454  }
1455  else if (!ssl.isTLS() && (server_version_.major_ == 3 &&
1456  server_version_.minor_ >= 1)) {
1457  ssl.SetError(badVersion_error);
1458  return;
1459  }
1460  ssl.set_pending(cipher_suite_[1]);
1461  ssl.set_random(random_, server_end);
1462  if (id_len_)
1463  ssl.set_sessionID(session_id_);
1464  else
1465  ssl.useSecurity().use_connection().sessionID_Set_ = false;
1466 
1467  if (ssl.getSecurity().get_resuming()) {
1468  if (memcmp(session_id_, ssl.getSecurity().get_resume().GetID(),
1469  ID_LEN) == 0) {
1470  ssl.set_masterSecret(ssl.getSecurity().get_resume().GetSecret());
1471  if (ssl.isTLS())
1472  ssl.deriveTLSKeys();
1473  else
1474  ssl.deriveKeys();
1475  ssl.useStates().useClient() = serverHelloDoneComplete;
1476  return;
1477  }
1478  else {
1479  ssl.useSecurity().set_resuming(false);
1480  ssl.useLog().Trace("server denied resumption");
1481  }
1482  }
1483 
1484  if (ssl.CompressionOn() && !compression_method_)
1485  ssl.UnSetCompression(); // server isn't supporting yaSSL zlib request
1486 
1487  ssl.useStates().useClient() = serverHelloComplete;
1488 }
1489 
1490 
1491 ServerHello::ServerHello()
1492 {
1493  memset(random_, 0, RAN_LEN);
1494  memset(session_id_, 0, ID_LEN);
1495 }
1496 
1497 
1498 ServerHello::ServerHello(ProtocolVersion pv, bool useCompression)
1499  : server_version_(pv),
1500  compression_method_(useCompression ? zlib : no_compression)
1501 {
1502  memset(random_, 0, RAN_LEN);
1503  memset(session_id_, 0, ID_LEN);
1504 }
1505 
1506 
1507 input_buffer& ServerHello::set(input_buffer& in)
1508 {
1509  return in >> *this;
1510 }
1511 
1512 
1513 output_buffer& ServerHello::get(output_buffer& out) const
1514 {
1515  return out << *this;
1516 }
1517 
1518 
1519 HandShakeType ServerHello::get_type() const
1520 {
1521  return server_hello;
1522 }
1523 
1524 
1525 const opaque* ServerHello::get_random() const
1526 {
1527  return random_;
1528 }
1529 
1530 
1531 // Server Hello Done processing handler
1532 void ServerHelloDone::Process(input_buffer&, SSL& ssl)
1533 {
1534  ssl.useStates().useClient() = serverHelloDoneComplete;
1535 }
1536 
1537 
1538 ServerHelloDone::ServerHelloDone()
1539 {
1540  set_length(0);
1541 }
1542 
1543 
1544 input_buffer& ServerHelloDone::set(input_buffer& in)
1545 {
1546  return in;
1547 }
1548 
1549 
1550 output_buffer& ServerHelloDone::get(output_buffer& out) const
1551 {
1552  return out;
1553 }
1554 
1555 
1556 HandShakeType ServerHelloDone::get_type() const
1557 {
1558  return server_hello_done;
1559 }
1560 
1561 
1562 int ClientKeyBase::get_length() const
1563 {
1564  return 0;
1565 }
1566 
1567 
1568 opaque* ClientKeyBase::get_clientKey() const
1569 {
1570  return 0;
1571 }
1572 
1573 
1574 // input operator for Client Hello
1575 input_buffer& operator>>(input_buffer& input, ClientHello& hello)
1576 {
1577  uint begin = input.get_current(); // could have extensions at end
1578 
1579  // Protocol
1580  hello.client_version_.major_ = input[AUTO];
1581  hello.client_version_.minor_ = input[AUTO];
1582 
1583  // Random
1584  input.read(hello.random_, RAN_LEN);
1585 
1586  // Session
1587  hello.id_len_ = input[AUTO];
1588  if (hello.id_len_) input.read(hello.session_id_, ID_LEN);
1589 
1590  // Suites
1591  byte tmp[2];
1592  uint16 len;
1593  tmp[0] = input[AUTO];
1594  tmp[1] = input[AUTO];
1595  ato16(tmp, len);
1596 
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_) // ignore extra suites
1600  input.set_current(input.get_current() + len - hello.suite_len_);
1601 
1602  // Compression
1603  hello.comp_len_ = input[AUTO];
1604  hello.compression_methods_ = no_compression;
1605  while (hello.comp_len_--) {
1606  CompressionMethod cm = CompressionMethod(input[AUTO]);
1607  if (cm == zlib)
1608  hello.compression_methods_ = zlib;
1609  }
1610 
1611  uint read = input.get_current() - begin;
1612  uint expected = hello.get_length();
1613 
1614  // ignore client hello extensions for now
1615  if (read < expected)
1616  input.set_current(input.get_current() + expected - read);
1617 
1618  return input;
1619 }
1620 
1621 
1622 // output operaotr for Client Hello
1623 output_buffer& operator<<(output_buffer& output, const ClientHello& hello)
1624 {
1625  // Protocol
1626  output[AUTO] = hello.client_version_.major_;
1627  output[AUTO] = hello.client_version_.minor_;
1628 
1629  // Random
1630  output.write(hello.random_, RAN_LEN);
1631 
1632  // Session
1633  output[AUTO] = hello.id_len_;
1634  if (hello.id_len_) output.write(hello.session_id_, ID_LEN);
1635 
1636  // Suites
1637  byte tmp[2];
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_);
1642 
1643  // Compression
1644  output[AUTO] = hello.comp_len_;
1645  output[AUTO] = hello.compression_methods_;
1646 
1647  return output;
1648 }
1649 
1650 
1651 // Client Hello processing handler
1652 void ClientHello::Process(input_buffer&, SSL& ssl)
1653 {
1654  // store version for pre master secret
1655  ssl.useSecurity().use_connection().chVersion_ = client_version_;
1656 
1657  if (client_version_.major_ != 3) {
1658  ssl.SetError(badVersion_error);
1659  return;
1660  }
1661  if (ssl.GetMultiProtocol()) { // SSLv23 support
1662  if (ssl.isTLS() && client_version_.minor_ < 1) {
1663  // downgrade to SSLv3
1664  ssl.useSecurity().use_connection().TurnOffTLS();
1665 
1666  ProtocolVersion pv = ssl.getSecurity().get_connection().version_;
1667  bool removeDH = ssl.getSecurity().get_parms().removeDH_;
1668  bool removeRSA = false;
1669  bool removeDSA = false;
1670 
1671  const CertManager& cm = ssl.getCrypto().get_certManager();
1672  if (cm.get_keyType() == rsa_sa_algo)
1673  removeDSA = true;
1674  else
1675  removeRSA = true;
1676 
1677  // reset w/ SSL suites
1678  ssl.useSecurity().use_parms().SetSuites(pv, removeDH, removeRSA,
1679  removeDSA);
1680  }
1681  else if (ssl.isTLSv1_1() && client_version_.minor_ == 1)
1682  // downgrade to TLSv1, but use same suites
1683  ssl.useSecurity().use_connection().TurnOffTLS1_1();
1684  }
1685  else if (ssl.isTLSv1_1() && client_version_.minor_ < 2) {
1686  ssl.SetError(badVersion_error);
1687  return;
1688  }
1689  else if (ssl.isTLS() && client_version_.minor_ < 1) {
1690  ssl.SetError(badVersion_error);
1691  return;
1692  }
1693  else if (!ssl.isTLS() && client_version_.minor_ >= 1) {
1694  ssl.SetError(badVersion_error);
1695  return;
1696  }
1697 
1698  ssl.set_random(random_, client_end);
1699 
1700  while (id_len_) { // trying to resume
1701  SSL_SESSION* session = 0;
1702  if (!ssl.getSecurity().GetContext()->GetSessionCacheOff())
1703  session = GetSessions().lookup(session_id_);
1704  if (!session) {
1705  ssl.useLog().Trace("session lookup failed");
1706  break;
1707  }
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());
1714 
1715  opaque serverRandom[RAN_LEN];
1716  ssl.getCrypto().get_random().Fill(serverRandom, sizeof(serverRandom));
1717  ssl.set_random(serverRandom, server_end);
1718  if (ssl.isTLS())
1719  ssl.deriveTLSKeys();
1720  else
1721  ssl.deriveKeys();
1722  ssl.useStates().useServer() = clientKeyExchangeComplete;
1723  return;
1724  }
1725  ssl.matchSuite(cipher_suites_, suite_len_);
1726  if (ssl.GetError()) return;
1727  ssl.set_pending(ssl.getSecurity().get_parms().suite_[1]);
1728 
1729  if (compression_methods_ == zlib)
1730  ssl.SetCompression();
1731 
1732  ssl.useStates().useServer() = clientHelloComplete;
1733 }
1734 
1735 
1736 input_buffer& ClientHello::set(input_buffer& in)
1737 {
1738  return in >> *this;
1739 }
1740 
1741 
1742 output_buffer& ClientHello::get(output_buffer& out) const
1743 {
1744  return out << *this;
1745 }
1746 
1747 
1748 HandShakeType ClientHello::get_type() const
1749 {
1750  return client_hello;
1751 }
1752 
1753 
1754 const opaque* ClientHello::get_random() const
1755 {
1756  return random_;
1757 }
1758 
1759 
1760 ClientHello::ClientHello()
1761 {
1762  memset(random_, 0, RAN_LEN);
1763 }
1764 
1765 
1766 ClientHello::ClientHello(ProtocolVersion pv, bool useCompression)
1767  : client_version_(pv),
1768  compression_methods_(useCompression ? zlib : no_compression)
1769 {
1770  memset(random_, 0, RAN_LEN);
1771 }
1772 
1773 
1774 // output operator for ServerKeyExchange
1775 output_buffer& operator<<(output_buffer& output, const ServerKeyExchange& sk)
1776 {
1777  output.write(sk.getKey(), sk.getKeyLength());
1778  return output;
1779 }
1780 
1781 
1782 // Server Key Exchange processing handler
1783 void ServerKeyExchange::Process(input_buffer& input, SSL& ssl)
1784 {
1785  createKey(ssl);
1786  if (ssl.GetError()) return;
1787  server_key_->read(ssl, input);
1788 
1789  ssl.useStates().useClient() = serverKeyExchangeComplete;
1790 }
1791 
1792 
1793 ServerKeyExchange::ServerKeyExchange(SSL& ssl)
1794 {
1795  createKey(ssl);
1796 }
1797 
1798 
1799 ServerKeyExchange::ServerKeyExchange()
1800  : server_key_(0)
1801 {}
1802 
1803 
1804 ServerKeyExchange::~ServerKeyExchange()
1805 {
1806  ysDelete(server_key_);
1807 }
1808 
1809 
1810 void ServerKeyExchange::build(SSL& ssl)
1811 {
1812  server_key_->build(ssl);
1813  set_length(server_key_->get_length());
1814 }
1815 
1816 
1817 const opaque* ServerKeyExchange::getKey() const
1818 {
1819  return server_key_->get_serverKey();
1820 }
1821 
1822 
1823 int ServerKeyExchange::getKeyLength() const
1824 {
1825  return server_key_->get_length();
1826 }
1827 
1828 
1829 input_buffer& ServerKeyExchange::set(input_buffer& in)
1830 {
1831  return in; // process does
1832 }
1833 
1834 
1835 output_buffer& ServerKeyExchange::get(output_buffer& out) const
1836 {
1837  return out << *this;
1838 }
1839 
1840 
1841 HandShakeType ServerKeyExchange::get_type() const
1842 {
1843  return server_key_exchange;
1844 }
1845 
1846 
1847 // CertificateRequest
1848 CertificateRequest::CertificateRequest()
1849  : typeTotal_(0)
1850 {
1851  memset(certificate_types_, 0, sizeof(certificate_types_));
1852 }
1853 
1854 
1855 CertificateRequest::~CertificateRequest()
1856 {
1857 
1858  STL::for_each(certificate_authorities_.begin(),
1859  certificate_authorities_.end(),
1860  del_ptr_zero()) ;
1861 }
1862 
1863 
1864 void CertificateRequest::Build()
1865 {
1866  certificate_types_[0] = rsa_sign;
1867  certificate_types_[1] = dss_sign;
1868 
1869  typeTotal_ = 2;
1870 
1871  uint16 authCount = 0;
1872  uint16 authSz = 0;
1873 
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]);
1878 
1879  opaque tmp[REQUEST_HEADER];
1880  c16toa(MIN_DIS_SIZE, tmp);
1881  memcpy(dn, tmp, sizeof(tmp));
1882 
1883  // fill w/ junk for now
1884  memcpy(dn, tmp, MIN_DIS_SIZE);
1885  authSz += sz;
1886  }
1887 
1888  set_length(SIZEOF_ENUM + typeTotal_ + REQUEST_HEADER + authSz);
1889 }
1890 
1891 
1892 input_buffer& CertificateRequest::set(input_buffer& in)
1893 {
1894  return in >> *this;
1895 }
1896 
1897 
1898 output_buffer& CertificateRequest::get(output_buffer& out) const
1899 {
1900  return out << *this;
1901 }
1902 
1903 
1904 // input operator for CertificateRequest
1905 input_buffer& operator>>(input_buffer& input, CertificateRequest& request)
1906 {
1907  // types
1908  request.typeTotal_ = input[AUTO];
1909  for (int i = 0; i < request.typeTotal_; i++)
1910  request.certificate_types_[i] = ClientCertificateType(input[AUTO]);
1911 
1912  byte tmp[REQUEST_HEADER];
1913  input.read(tmp, sizeof(tmp));
1914  uint16 sz;
1915  ato16(tmp, sz);
1916 
1917  // authorities
1918  while (sz) {
1919  uint16 dnSz;
1920  input.read(tmp, sizeof(tmp));
1921  ato16(tmp, dnSz);
1922 
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);
1928 
1929  sz -= dnSz + REQUEST_HEADER;
1930  }
1931 
1932  return input;
1933 }
1934 
1935 
1936 // output operator for CertificateRequest
1937 output_buffer& operator<<(output_buffer& output,
1938  const CertificateRequest& request)
1939 {
1940  // types
1941  output[AUTO] = request.typeTotal_;
1942  for (int i = 0; i < request.typeTotal_; i++)
1943  output[AUTO] = request.certificate_types_[i];
1944 
1945  // authorities
1946  opaque tmp[REQUEST_HEADER];
1947  c16toa(request.get_length() - SIZEOF_ENUM -
1948  request.typeTotal_ - REQUEST_HEADER, tmp);
1949  output.write(tmp, sizeof(tmp));
1950 
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) {
1956  uint16 sz;
1957  ato16(*first, sz);
1958  output.write(*first, sz + REQUEST_HEADER);
1959 
1960  ++first;
1961  }
1962 
1963  return output;
1964 }
1965 
1966 
1967 // CertificateRequest processing handler
1968 void CertificateRequest::Process(input_buffer&, SSL& ssl)
1969 {
1970  CertManager& cm = ssl.useCrypto().use_certManager();
1971 
1972  // make sure user provided cert and key before sending and using
1973  if (cm.get_cert() && cm.get_privateKey())
1974  cm.setSendVerify();
1975 }
1976 
1977 
1978 HandShakeType CertificateRequest::get_type() const
1979 {
1980  return certificate_request;
1981 }
1982 
1983 
1984 // CertificateVerify
1985 CertificateVerify::CertificateVerify() : signature_(0)
1986 {}
1987 
1988 
1989 CertificateVerify::~CertificateVerify()
1990 {
1991  ysArrayDelete(signature_);
1992 }
1993 
1994 
1995 void CertificateVerify::Build(SSL& ssl)
1996 {
1997  build_certHashes(ssl, hashes_);
1998 
1999  uint16 sz = 0;
2000  byte len[VERIFY_HEADER];
2002 
2003  // sign
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);
2007 
2008  sz = rsa.get_cipherLength() + VERIFY_HEADER;
2009  sig.reset(NEW_YS byte[sz]);
2010 
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());
2015  }
2016  else { // DSA
2017  DSS dss(cert.get_privateKey(), cert.get_privateKeyLength(), false);
2018 
2019  sz = DSS_SIG_SZ + DSS_ENCODED_EXTRA + VERIFY_HEADER;
2020  sig.reset(NEW_YS byte[sz]);
2021 
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());
2026 
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));
2030  }
2031  set_length(sz);
2032  signature_ = sig.release();
2033 }
2034 
2035 
2036 input_buffer& CertificateVerify::set(input_buffer& in)
2037 {
2038  return in >> *this;
2039 }
2040 
2041 
2042 output_buffer& CertificateVerify::get(output_buffer& out) const
2043 {
2044  return out << *this;
2045 }
2046 
2047 
2048 // input operator for CertificateVerify
2049 input_buffer& operator>>(input_buffer& input, CertificateVerify& request)
2050 {
2051  byte tmp[VERIFY_HEADER];
2052  input.read(tmp, sizeof(tmp));
2053 
2054  uint16 sz = 0;
2055  ato16(tmp, sz);
2056  request.set_length(sz);
2057 
2058  request.signature_ = NEW_YS byte[sz];
2059  input.read(request.signature_, sz);
2060 
2061  return input;
2062 }
2063 
2064 
2065 // output operator for CertificateVerify
2066 output_buffer& operator<<(output_buffer& output,
2067  const CertificateVerify& verify)
2068 {
2069  output.write(verify.signature_, verify.get_length());
2070 
2071  return output;
2072 }
2073 
2074 
2075 // CertificateVerify processing handler
2076 void CertificateVerify::Process(input_buffer&, SSL& ssl)
2077 {
2078  const Hashes& hashVerify = ssl.getHashes().get_certVerify();
2079  const CertManager& cert = ssl.getCrypto().get_certManager();
2080 
2081  if (cert.get_peerKeyType() == rsa_sa_algo) {
2082  RSA rsa(cert.get_peerKey(), cert.get_peerKeyLength());
2083 
2084  if (!rsa.verify(hashVerify.md5_, sizeof(hashVerify), signature_,
2085  get_length()))
2086  ssl.SetError(verify_error);
2087  }
2088  else { // DSA
2089  byte decodedSig[DSS_SIG_SZ];
2090  TaoCrypt::DecodeDSA_Signature(decodedSig, signature_, get_length());
2091 
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);
2095  }
2096 }
2097 
2098 
2099 HandShakeType CertificateVerify::get_type() const
2100 {
2101  return certificate_verify;
2102 }
2103 
2104 
2105 // output operator for ClientKeyExchange
2106 output_buffer& operator<<(output_buffer& output, const ClientKeyExchange& ck)
2107 {
2108  output.write(ck.getKey(), ck.getKeyLength());
2109  return output;
2110 }
2111 
2112 
2113 // Client Key Exchange processing handler
2114 void ClientKeyExchange::Process(input_buffer& input, SSL& ssl)
2115 {
2116  createKey(ssl);
2117  if (ssl.GetError()) return;
2118  client_key_->read(ssl, input);
2119 
2120  if (ssl.getCrypto().get_certManager().verifyPeer())
2121  build_certHashes(ssl, ssl.useHashes().use_certVerify());
2122 
2123  ssl.useStates().useServer() = clientKeyExchangeComplete;
2124 }
2125 
2126 
2127 ClientKeyExchange::ClientKeyExchange(SSL& ssl)
2128 {
2129  createKey(ssl);
2130 }
2131 
2132 
2133 ClientKeyExchange::ClientKeyExchange()
2134  : client_key_(0)
2135 {}
2136 
2137 
2138 ClientKeyExchange::~ClientKeyExchange()
2139 {
2140  ysDelete(client_key_);
2141 }
2142 
2143 
2144 void ClientKeyExchange::build(SSL& ssl)
2145 {
2146  client_key_->build(ssl);
2147  set_length(client_key_->get_length());
2148 }
2149 
2150 const opaque* ClientKeyExchange::getKey() const
2151 {
2152  return client_key_->get_clientKey();
2153 }
2154 
2155 
2156 int ClientKeyExchange::getKeyLength() const
2157 {
2158  return client_key_->get_length();
2159 }
2160 
2161 
2162 input_buffer& ClientKeyExchange::set(input_buffer& in)
2163 {
2164  return in;
2165 }
2166 
2167 
2168 output_buffer& ClientKeyExchange::get(output_buffer& out) const
2169 {
2170  return out << *this;
2171 }
2172 
2173 
2174 HandShakeType ClientKeyExchange::get_type() const
2175 {
2176  return client_key_exchange;
2177 }
2178 
2179 
2180 // input operator for Finished
2181 input_buffer& operator>>(input_buffer& input, Finished&)
2182 {
2183  /* do in process */
2184 
2185  return input;
2186 }
2187 
2188 // output operator for Finished
2189 output_buffer& operator<<(output_buffer& output, const Finished& fin)
2190 {
2191  if (fin.get_length() == FINISHED_SZ) {
2192  output.write(fin.hashes_.md5_, MD5_LEN);
2193  output.write(fin.hashes_.sha_, SHA_LEN);
2194  }
2195  else // TLS_FINISHED_SZ
2196  output.write(fin.hashes_.md5_, TLS_FINISHED_SZ);
2197 
2198  return output;
2199 }
2200 
2201 
2202 // Finished processing handler
2203 void Finished::Process(input_buffer& input, SSL& ssl)
2204 {
2205  // verify hashes
2206  const Finished& verify = ssl.getHashes().get_verify();
2207  uint finishedSz = ssl.isTLS() ? TLS_FINISHED_SZ : FINISHED_SZ;
2208 
2209  input.read(hashes_.md5_, finishedSz);
2210 
2211  if (memcmp(&hashes_, &verify.hashes_, finishedSz)) {
2212  ssl.SetError(verify_error);
2213  return;
2214  }
2215 
2216  // read verify mac
2217  opaque verifyMAC[SHA_LEN];
2218  uint macSz = finishedSz + HANDSHAKE_HEADER;
2219 
2220  if (ssl.isTLS())
2221  TLS_hmac(ssl, verifyMAC, input.get_buffer() + input.get_current()
2222  - macSz, macSz, handshake, true);
2223  else
2224  hmac(ssl, verifyMAC, input.get_buffer() + input.get_current() - macSz,
2225  macSz, handshake, true);
2226 
2227  // read mac and fill
2228  opaque mac[SHA_LEN]; // max size
2229  int digestSz = ssl.getCrypto().get_digest().get_digestSize();
2230  input.read(mac, digestSz);
2231 
2232  uint ivExtra = 0;
2233  if (ssl.getSecurity().get_parms().cipher_type_ == block)
2234  if (ssl.isTLSv1_1())
2235  ivExtra = ssl.getCrypto().get_cipher().get_blockSize();
2236 
2237  int padSz = ssl.getSecurity().get_parms().encrypt_size_ - ivExtra -
2238  HANDSHAKE_HEADER - finishedSz - digestSz;
2239  input.set_current(input.get_current() + padSz);
2240 
2241  // verify mac
2242  if (memcmp(mac, verifyMAC, digestSz)) {
2243  ssl.SetError(verify_error);
2244  return;
2245  }
2246 
2247  // update states
2248  ssl.useStates().useHandShake() = handShakeReady;
2249  if (ssl.getSecurity().get_parms().entity_ == client_end)
2250  ssl.useStates().useClient() = serverFinishedComplete;
2251  else
2252  ssl.useStates().useServer() = clientFinishedComplete;
2253 }
2254 
2255 
2256 Finished::Finished()
2257 {
2258  set_length(FINISHED_SZ);
2259 }
2260 
2261 
2262 uint8* Finished::set_md5()
2263 {
2264  return hashes_.md5_;
2265 }
2266 
2267 
2268 uint8* Finished::set_sha()
2269 {
2270  return hashes_.sha_;
2271 }
2272 
2273 
2274 input_buffer& Finished::set(input_buffer& in)
2275 {
2276  return in >> *this;
2277 }
2278 
2279 
2280 output_buffer& Finished::get(output_buffer& out) const
2281 {
2282  return out << *this;
2283 }
2284 
2285 
2286 HandShakeType Finished::get_type() const
2287 {
2288  return finished;
2289 }
2290 
2291 
2292 void clean(volatile opaque* p, uint sz, RandomPool& ran)
2293 {
2294  uint i(0);
2295 
2296  for (i = 0; i < sz; ++i)
2297  p[i] = 0;
2298 
2299  ran.Fill(const_cast<opaque*>(p), sz);
2300 
2301  for (i = 0; i < sz; ++i)
2302  p[i] = 0;
2303 }
2304 
2305 
2306 
2307 Connection::Connection(ProtocolVersion v, RandomPool& ran)
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)
2313 {
2314  memset(sessionID_, 0, sizeof(sessionID_));
2315 }
2316 
2317 
2318 Connection::~Connection()
2319 {
2320  CleanMaster(); CleanPreMaster(); ysArrayDelete(pre_master_secret_);
2321 }
2322 
2323 
2324 void Connection::AllocPreSecret(uint sz)
2325 {
2326  pre_master_secret_ = NEW_YS opaque[pre_secret_len_ = sz];
2327 }
2328 
2329 
2330 void Connection::TurnOffTLS()
2331 {
2332  TLS_ = false;
2333  version_.minor_ = 0;
2334 }
2335 
2336 
2337 void Connection::TurnOffTLS1_1()
2338 {
2339  TLSv1_1_ = false;
2340  version_.minor_ = 1;
2341 }
2342 
2343 
2344 // wipeout master secret
2345 void Connection::CleanMaster()
2346 {
2347  if (!master_clean_) {
2348  volatile opaque* p = master_secret_;
2349  clean(p, SECRET_LEN, random_);
2350  master_clean_ = true;
2351  }
2352 }
2353 
2354 
2355 // wipeout pre master secret
2356 void Connection::CleanPreMaster()
2357 {
2358  if (pre_master_secret_) {
2359  volatile opaque* p = pre_master_secret_;
2360  clean(p, pre_secret_len_, random_);
2361 
2362  ysArrayDelete(pre_master_secret_);
2363  pre_master_secret_ = 0;
2364  }
2365 }
2366 
2367 
2368 // Create functions for message factory
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; }
2373 
2374 // Create functions for handshake factory
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; }
2386 
2387 // Create functions for server key exchange factory
2388 ServerKeyBase* CreateRSAServerKEA() { return NEW_YS RSA_Server; }
2389 ServerKeyBase* CreateDHServerKEA() { return NEW_YS DH_Server; }
2390 ServerKeyBase* CreateFortezzaServerKEA() { return NEW_YS Fortezza_Server; }
2391 
2392 // Create functions for client key exchange factory
2393 ClientKeyBase* CreateRSAClient() { return NEW_YS
2394  EncryptedPreMasterSecret; }
2395 ClientKeyBase* CreateDHClient() { return NEW_YS
2396  ClientDiffieHellmanPublic; }
2397 ClientKeyBase* CreateFortezzaClient() { return NEW_YS FortezzaKeys; }
2398 
2399 
2400 // Constructor calls this to Register compile time callbacks
2401 void InitMessageFactory(MessageFactory& mf)
2402 {
2403  mf.Reserve(4);
2404  mf.Register(alert, CreateAlert);
2405  mf.Register(change_cipher_spec, CreateCipherSpec);
2406  mf.Register(handshake, CreateHandShake);
2407  mf.Register(application_data, CreateData);
2408 }
2409 
2410 
2411 // Constructor calls this to Register compile time callbacks
2412 void InitHandShakeFactory(HandShakeFactory& hsf)
2413 {
2414  hsf.Reserve(10);
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);
2425 }
2426 
2427 
2428 // Constructor calls this to Register compile time callbacks
2429 void InitServerKeyFactory(ServerKeyFactory& skf)
2430 {
2431  skf.Reserve(3);
2432  skf.Register(rsa_kea, CreateRSAServerKEA);
2433  skf.Register(diffie_hellman_kea, CreateDHServerKEA);
2434  skf.Register(fortezza_kea, CreateFortezzaServerKEA);
2435 }
2436 
2437 
2438 // Constructor calls this to Register compile time callbacks
2439 void InitClientKeyFactory(ClientKeyFactory& ckf)
2440 {
2441  ckf.Reserve(3);
2442  ckf.Register(rsa_kea, CreateRSAClient);
2443  ckf.Register(diffie_hellman_kea, CreateDHClient);
2444  ckf.Register(fortezza_kea, CreateFortezzaClient);
2445 }
2446 
2447 
2448 } // namespace