21 #include "my_config.h"
28 #include "runtime.hpp"
29 #include "yassl_int.hpp"
30 #include "handshake.hpp"
40 void*
operator new(
size_t sz, yaSSL::new_t)
42 void* ptr = malloc(sz ? sz : 1);
49 void operator delete(
void* ptr, yaSSL::new_t)
55 void*
operator new[](
size_t sz, yaSSL::new_t nt)
57 return ::operator
new(sz, nt);
61 void operator delete[](
void* ptr, yaSSL::new_t nt)
63 ::operator
delete(ptr, nt);
72 #endif // YASSL_PURE_C
75 #include <mysql/get_password.h>
85 void c32to24(uint32 u32, uint24& u24)
87 u24[0] = (u32 >> 16) & 0xff;
88 u24[1] = (u32 >> 8) & 0xff;
94 void c24to32(
const uint24 u24, uint32& u32)
97 u32 = (u24[0] << 16) | (u24[1] << 8) | u24[2];
102 uint32 c24to32(
const uint24 u24)
115 void ato16(
const opaque* c, uint16& u16)
118 u16 = (c[0] << 8) | (c[1]);
123 void ato24(
const opaque* c, uint24& u24)
132 void c16toa(uint16 u16, opaque* c)
134 c[0] = (u16 >> 8) & 0xff;
140 void c24toa(
const uint24 u24, opaque* c)
149 void c32toa(uint32 u32, opaque* c)
151 c[0] = (u32 >> 24) & 0xff;
152 c[1] = (u32 >> 16) & 0xff;
153 c[2] = (u32 >> 8) & 0xff;
158 States::States() : recordLayer_(recordReady), handshakeLayer_(preHandshake),
159 clientState_(serverNull), serverState_(clientNull),
160 connectState_(CONNECT_BEGIN), acceptState_(ACCEPT_BEGIN),
163 const RecordLayerState& States::getRecord()
const
169 const HandShakeState& States::getHandShake()
const
171 return handshakeLayer_;
175 const ClientState& States::getClient()
const
181 const ServerState& States::getServer()
const
187 const ConnectState& States::GetConnect()
const
189 return connectState_;
193 const AcceptState& States::GetAccept()
const
199 const char* States::getString()
const
205 YasslError States::What()
const
211 RecordLayerState& States::useRecord()
217 HandShakeState& States::useHandShake()
219 return handshakeLayer_;
223 ClientState& States::useClient()
229 ServerState& States::useServer()
235 ConnectState& States::UseConnect()
237 return connectState_;
241 AcceptState& States::UseAccept()
247 char* States::useString()
253 void States::SetError(YasslError ye)
259 sslFactory::sslFactory() :
260 messageFactory_(InitMessageFactory),
261 handShakeFactory_(InitHandShakeFactory),
262 serverKeyFactory_(InitServerKeyFactory),
263 clientKeyFactory_(InitClientKeyFactory)
267 const MessageFactory& sslFactory::getMessage()
const
269 return messageFactory_;
273 const HandShakeFactory& sslFactory::getHandShake()
const
275 return handShakeFactory_;
279 const ServerKeyFactory& sslFactory::getServerKey()
const
281 return serverKeyFactory_;
285 const ClientKeyFactory& sslFactory::getClientKey()
const
287 return clientKeyFactory_;
292 SSL::SSL(SSL_CTX* ctx)
293 : secure_(ctx->getMethod()->getVersion(), crypto_.use_random(),
294 ctx->getMethod()->getSide(), ctx->GetCiphers(), ctx,
295 ctx->GetDH_Parms().set_), quietShutdown_(false), has_data_(false)
297 if (
int err = crypto_.get_random().GetError()) {
298 SetError(YasslError(err));
302 CertManager& cm = crypto_.use_certManager();
303 cm.CopySelfCert(ctx->getCert());
305 bool serverSide = secure_.use_parms().entity_ == server_end;
308 if (
int err = cm.SetPrivateKey(*ctx->getKey())) {
309 SetError(YasslError(err));
312 else if (serverSide && !(ctx->GetCiphers().setSuites_)) {
314 ProtocolVersion pv = secure_.get_connection().version_;
316 bool removeDH = secure_.use_parms().removeDH_;
317 bool removeRSA =
false;
318 bool removeDSA =
false;
320 if (cm.get_keyType() == rsa_sa_algo)
324 secure_.use_parms().SetSuites(pv, removeDH, removeRSA, removeDSA);
327 else if (serverSide) {
328 SetError(no_key_file);
332 if (ctx->getMethod()->verifyPeer())
334 if (ctx->getMethod()->verifyNone())
336 if (ctx->getMethod()->failNoCert())
338 cm.setVerifyCallback(ctx->getVerifyCallback());
341 crypto_.SetDH(ctx->GetDH_Parms());
343 const SSL_CTX::CertList& ca = ctx->GetCA_List();
344 SSL_CTX::CertList::const_iterator first(ca.begin());
345 SSL_CTX::CertList::const_iterator last(ca.end());
347 while (first != last) {
348 if (
int err = cm.CopyCaCert(*first)) {
349 SetError(YasslError(err));
358 void SSL::set_pending(Cipher suite)
360 Parameters& parms = secure_.use_parms();
364 case TLS_RSA_WITH_AES_256_CBC_SHA:
365 parms.bulk_cipher_algorithm_ = aes;
366 parms.mac_algorithm_ = sha;
367 parms.kea_ = rsa_kea;
368 parms.hash_size_ = SHA_LEN;
369 parms.key_size_ = AES_256_KEY_SZ;
370 parms.iv_size_ = AES_BLOCK_SZ;
371 parms.cipher_type_ =
block;
372 crypto_.setDigest(NEW_YS SHA);
373 crypto_.setCipher(NEW_YS AES(AES_256_KEY_SZ));
374 strncpy(parms.cipher_name_, cipher_names[TLS_RSA_WITH_AES_256_CBC_SHA],
378 case TLS_RSA_WITH_AES_128_CBC_SHA:
379 parms.bulk_cipher_algorithm_ = aes;
380 parms.mac_algorithm_ = sha;
381 parms.kea_ = rsa_kea;
382 parms.hash_size_ = SHA_LEN;
383 parms.key_size_ = AES_128_KEY_SZ;
384 parms.iv_size_ = AES_BLOCK_SZ;
385 parms.cipher_type_ =
block;
386 crypto_.setDigest(NEW_YS SHA);
387 crypto_.setCipher(NEW_YS AES);
388 strncpy(parms.cipher_name_, cipher_names[TLS_RSA_WITH_AES_128_CBC_SHA],
392 case SSL_RSA_WITH_3DES_EDE_CBC_SHA:
393 parms.bulk_cipher_algorithm_ = triple_des;
394 parms.mac_algorithm_ = sha;
395 parms.kea_ = rsa_kea;
396 parms.hash_size_ = SHA_LEN;
397 parms.key_size_ = DES_EDE_KEY_SZ;
398 parms.iv_size_ = DES_IV_SZ;
399 parms.cipher_type_ =
block;
400 crypto_.setDigest(NEW_YS SHA);
401 crypto_.setCipher(NEW_YS DES_EDE);
402 strncpy(parms.cipher_name_, cipher_names[SSL_RSA_WITH_3DES_EDE_CBC_SHA]
406 case SSL_RSA_WITH_DES_CBC_SHA:
407 parms.bulk_cipher_algorithm_ = des;
408 parms.mac_algorithm_ = sha;
409 parms.kea_ = rsa_kea;
410 parms.hash_size_ = SHA_LEN;
411 parms.key_size_ = DES_KEY_SZ;
412 parms.iv_size_ = DES_IV_SZ;
413 parms.cipher_type_ =
block;
414 crypto_.setDigest(NEW_YS SHA);
415 crypto_.setCipher(NEW_YS DES);
416 strncpy(parms.cipher_name_, cipher_names[SSL_RSA_WITH_DES_CBC_SHA],
420 case SSL_RSA_WITH_RC4_128_SHA:
421 parms.bulk_cipher_algorithm_ = rc4;
422 parms.mac_algorithm_ = sha;
423 parms.kea_ = rsa_kea;
424 parms.hash_size_ = SHA_LEN;
425 parms.key_size_ = RC4_KEY_SZ;
427 parms.cipher_type_ = stream;
428 crypto_.setDigest(NEW_YS SHA);
429 crypto_.setCipher(NEW_YS RC4);
430 strncpy(parms.cipher_name_, cipher_names[SSL_RSA_WITH_RC4_128_SHA],
434 case SSL_RSA_WITH_RC4_128_MD5:
435 parms.bulk_cipher_algorithm_ = rc4;
436 parms.mac_algorithm_ = md5;
437 parms.kea_ = rsa_kea;
438 parms.hash_size_ = MD5_LEN;
439 parms.key_size_ = RC4_KEY_SZ;
441 parms.cipher_type_ = stream;
442 crypto_.setDigest(NEW_YS MD5);
443 crypto_.setCipher(NEW_YS RC4);
444 strncpy(parms.cipher_name_, cipher_names[SSL_RSA_WITH_RC4_128_MD5],
448 case SSL_DHE_RSA_WITH_DES_CBC_SHA:
449 parms.bulk_cipher_algorithm_ = des;
450 parms.mac_algorithm_ = sha;
451 parms.kea_ = diffie_hellman_kea;
452 parms.sig_algo_ = rsa_sa_algo;
453 parms.hash_size_ = SHA_LEN;
454 parms.key_size_ = DES_KEY_SZ;
455 parms.iv_size_ = DES_IV_SZ;
456 parms.cipher_type_ =
block;
457 secure_.use_connection().send_server_key_ =
true;
458 crypto_.setDigest(NEW_YS SHA);
459 crypto_.setCipher(NEW_YS DES);
460 strncpy(parms.cipher_name_, cipher_names[SSL_DHE_RSA_WITH_DES_CBC_SHA],
464 case SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
465 parms.bulk_cipher_algorithm_ = triple_des;
466 parms.mac_algorithm_ = sha;
467 parms.kea_ = diffie_hellman_kea;
468 parms.sig_algo_ = rsa_sa_algo;
469 parms.hash_size_ = SHA_LEN;
470 parms.key_size_ = DES_EDE_KEY_SZ;
471 parms.iv_size_ = DES_IV_SZ;
472 parms.cipher_type_ =
block;
473 secure_.use_connection().send_server_key_ =
true;
474 crypto_.setDigest(NEW_YS SHA);
475 crypto_.setCipher(NEW_YS DES_EDE);
476 strncpy(parms.cipher_name_,
477 cipher_names[SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA], MAX_SUITE_NAME);
480 case TLS_DHE_RSA_WITH_AES_256_CBC_SHA:
481 parms.bulk_cipher_algorithm_ = aes;
482 parms.mac_algorithm_ = sha;
483 parms.kea_ = diffie_hellman_kea;
484 parms.sig_algo_ = rsa_sa_algo;
485 parms.hash_size_ = SHA_LEN;
486 parms.key_size_ = AES_256_KEY_SZ;
487 parms.iv_size_ = AES_BLOCK_SZ;
488 parms.cipher_type_ =
block;
489 secure_.use_connection().send_server_key_ =
true;
490 crypto_.setDigest(NEW_YS SHA);
491 crypto_.setCipher(NEW_YS AES(AES_256_KEY_SZ));
492 strncpy(parms.cipher_name_,
493 cipher_names[TLS_DHE_RSA_WITH_AES_256_CBC_SHA], MAX_SUITE_NAME);
496 case TLS_DHE_RSA_WITH_AES_128_CBC_SHA:
497 parms.bulk_cipher_algorithm_ = aes;
498 parms.mac_algorithm_ = sha;
499 parms.kea_ = diffie_hellman_kea;
500 parms.sig_algo_ = rsa_sa_algo;
501 parms.hash_size_ = SHA_LEN;
502 parms.key_size_ = AES_128_KEY_SZ;
503 parms.iv_size_ = AES_BLOCK_SZ;
504 parms.cipher_type_ =
block;
505 secure_.use_connection().send_server_key_ =
true;
506 crypto_.setDigest(NEW_YS SHA);
507 crypto_.setCipher(NEW_YS AES);
508 strncpy(parms.cipher_name_,
509 cipher_names[TLS_DHE_RSA_WITH_AES_128_CBC_SHA], MAX_SUITE_NAME);
512 case SSL_DHE_DSS_WITH_DES_CBC_SHA:
513 parms.bulk_cipher_algorithm_ = des;
514 parms.mac_algorithm_ = sha;
515 parms.kea_ = diffie_hellman_kea;
516 parms.sig_algo_ = dsa_sa_algo;
517 parms.hash_size_ = SHA_LEN;
518 parms.key_size_ = DES_KEY_SZ;
519 parms.iv_size_ = DES_IV_SZ;
520 parms.cipher_type_ =
block;
521 secure_.use_connection().send_server_key_ =
true;
522 crypto_.setDigest(NEW_YS SHA);
523 crypto_.setCipher(NEW_YS DES);
524 strncpy(parms.cipher_name_, cipher_names[SSL_DHE_DSS_WITH_DES_CBC_SHA],
528 case SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA:
529 parms.bulk_cipher_algorithm_ = triple_des;
530 parms.mac_algorithm_ = sha;
531 parms.kea_ = diffie_hellman_kea;
532 parms.sig_algo_ = dsa_sa_algo;
533 parms.hash_size_ = SHA_LEN;
534 parms.key_size_ = DES_EDE_KEY_SZ;
535 parms.iv_size_ = DES_IV_SZ;
536 parms.cipher_type_ =
block;
537 secure_.use_connection().send_server_key_ =
true;
538 crypto_.setDigest(NEW_YS SHA);
539 crypto_.setCipher(NEW_YS DES_EDE);
540 strncpy(parms.cipher_name_,
541 cipher_names[SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA], MAX_SUITE_NAME);
544 case TLS_DHE_DSS_WITH_AES_256_CBC_SHA:
545 parms.bulk_cipher_algorithm_ = aes;
546 parms.mac_algorithm_ = sha;
547 parms.kea_ = diffie_hellman_kea;
548 parms.sig_algo_ = dsa_sa_algo;
549 parms.hash_size_ = SHA_LEN;
550 parms.key_size_ = AES_256_KEY_SZ;
551 parms.iv_size_ = AES_BLOCK_SZ;
552 parms.cipher_type_ =
block;
553 secure_.use_connection().send_server_key_ =
true;
554 crypto_.setDigest(NEW_YS SHA);
555 crypto_.setCipher(NEW_YS AES(AES_256_KEY_SZ));
556 strncpy(parms.cipher_name_,
557 cipher_names[TLS_DHE_DSS_WITH_AES_256_CBC_SHA], MAX_SUITE_NAME);
560 case TLS_DHE_DSS_WITH_AES_128_CBC_SHA:
561 parms.bulk_cipher_algorithm_ = aes;
562 parms.mac_algorithm_ = sha;
563 parms.kea_ = diffie_hellman_kea;
564 parms.sig_algo_ = dsa_sa_algo;
565 parms.hash_size_ = SHA_LEN;
566 parms.key_size_ = AES_128_KEY_SZ;
567 parms.iv_size_ = AES_BLOCK_SZ;
568 parms.cipher_type_ =
block;
569 secure_.use_connection().send_server_key_ =
true;
570 crypto_.setDigest(NEW_YS SHA);
571 crypto_.setCipher(NEW_YS AES);
572 strncpy(parms.cipher_name_,
573 cipher_names[TLS_DHE_DSS_WITH_AES_128_CBC_SHA], MAX_SUITE_NAME);
576 case TLS_RSA_WITH_AES_256_CBC_RMD160:
577 parms.bulk_cipher_algorithm_ = aes;
578 parms.mac_algorithm_ = rmd;
579 parms.kea_ = rsa_kea;
580 parms.hash_size_ = RMD_LEN;
581 parms.key_size_ = AES_256_KEY_SZ;
582 parms.iv_size_ = AES_BLOCK_SZ;
583 parms.cipher_type_ =
block;
584 crypto_.setDigest(NEW_YS RMD);
585 crypto_.setCipher(NEW_YS AES(AES_256_KEY_SZ));
586 strncpy(parms.cipher_name_,
587 cipher_names[TLS_RSA_WITH_AES_256_CBC_RMD160], MAX_SUITE_NAME);
590 case TLS_RSA_WITH_AES_128_CBC_RMD160:
591 parms.bulk_cipher_algorithm_ = aes;
592 parms.mac_algorithm_ = rmd;
593 parms.kea_ = rsa_kea;
594 parms.hash_size_ = RMD_LEN;
595 parms.key_size_ = AES_128_KEY_SZ;
596 parms.iv_size_ = AES_BLOCK_SZ;
597 parms.cipher_type_ =
block;
598 crypto_.setDigest(NEW_YS RMD);
599 crypto_.setCipher(NEW_YS AES);
600 strncpy(parms.cipher_name_,
601 cipher_names[TLS_RSA_WITH_AES_128_CBC_RMD160], MAX_SUITE_NAME);
604 case TLS_RSA_WITH_3DES_EDE_CBC_RMD160:
605 parms.bulk_cipher_algorithm_ = triple_des;
606 parms.mac_algorithm_ = rmd;
607 parms.kea_ = rsa_kea;
608 parms.hash_size_ = RMD_LEN;
609 parms.key_size_ = DES_EDE_KEY_SZ;
610 parms.iv_size_ = DES_IV_SZ;
611 parms.cipher_type_ =
block;
612 crypto_.setDigest(NEW_YS RMD);
613 crypto_.setCipher(NEW_YS DES_EDE);
614 strncpy(parms.cipher_name_,
615 cipher_names[TLS_RSA_WITH_3DES_EDE_CBC_RMD160], MAX_SUITE_NAME);
618 case TLS_DHE_RSA_WITH_3DES_EDE_CBC_RMD160:
619 parms.bulk_cipher_algorithm_ = triple_des;
620 parms.mac_algorithm_ = rmd;
621 parms.kea_ = diffie_hellman_kea;
622 parms.sig_algo_ = rsa_sa_algo;
623 parms.hash_size_ = RMD_LEN;
624 parms.key_size_ = DES_EDE_KEY_SZ;
625 parms.iv_size_ = DES_IV_SZ;
626 parms.cipher_type_ =
block;
627 secure_.use_connection().send_server_key_ =
true;
628 crypto_.setDigest(NEW_YS RMD);
629 crypto_.setCipher(NEW_YS DES_EDE);
630 strncpy(parms.cipher_name_,
631 cipher_names[TLS_DHE_RSA_WITH_3DES_EDE_CBC_RMD160],
635 case TLS_DHE_RSA_WITH_AES_256_CBC_RMD160:
636 parms.bulk_cipher_algorithm_ = aes;
637 parms.mac_algorithm_ = rmd;
638 parms.kea_ = diffie_hellman_kea;
639 parms.sig_algo_ = rsa_sa_algo;
640 parms.hash_size_ = RMD_LEN;
641 parms.key_size_ = AES_256_KEY_SZ;
642 parms.iv_size_ = AES_BLOCK_SZ;
643 parms.cipher_type_ =
block;
644 secure_.use_connection().send_server_key_ =
true;
645 crypto_.setDigest(NEW_YS RMD);
646 crypto_.setCipher(NEW_YS AES(AES_256_KEY_SZ));
647 strncpy(parms.cipher_name_,
648 cipher_names[TLS_DHE_RSA_WITH_AES_256_CBC_RMD160],
652 case TLS_DHE_RSA_WITH_AES_128_CBC_RMD160:
653 parms.bulk_cipher_algorithm_ = aes;
654 parms.mac_algorithm_ = rmd;
655 parms.kea_ = diffie_hellman_kea;
656 parms.sig_algo_ = rsa_sa_algo;
657 parms.hash_size_ = RMD_LEN;
658 parms.key_size_ = AES_128_KEY_SZ;
659 parms.iv_size_ = AES_BLOCK_SZ;
660 parms.cipher_type_ =
block;
661 secure_.use_connection().send_server_key_ =
true;
662 crypto_.setDigest(NEW_YS RMD);
663 crypto_.setCipher(NEW_YS AES);
664 strncpy(parms.cipher_name_,
665 cipher_names[TLS_DHE_RSA_WITH_AES_128_CBC_RMD160],
669 case TLS_DHE_DSS_WITH_3DES_EDE_CBC_RMD160:
670 parms.bulk_cipher_algorithm_ = triple_des;
671 parms.mac_algorithm_ = rmd;
672 parms.kea_ = diffie_hellman_kea;
673 parms.sig_algo_ = dsa_sa_algo;
674 parms.hash_size_ = RMD_LEN;
675 parms.key_size_ = DES_EDE_KEY_SZ;
676 parms.iv_size_ = DES_IV_SZ;
677 parms.cipher_type_ =
block;
678 secure_.use_connection().send_server_key_ =
true;
679 crypto_.setDigest(NEW_YS RMD);
680 crypto_.setCipher(NEW_YS DES_EDE);
681 strncpy(parms.cipher_name_,
682 cipher_names[TLS_DHE_DSS_WITH_3DES_EDE_CBC_RMD160],
686 case TLS_DHE_DSS_WITH_AES_256_CBC_RMD160:
687 parms.bulk_cipher_algorithm_ = aes;
688 parms.mac_algorithm_ = rmd;
689 parms.kea_ = diffie_hellman_kea;
690 parms.sig_algo_ = dsa_sa_algo;
691 parms.hash_size_ = RMD_LEN;
692 parms.key_size_ = AES_256_KEY_SZ;
693 parms.iv_size_ = AES_BLOCK_SZ;
694 parms.cipher_type_ =
block;
695 secure_.use_connection().send_server_key_ =
true;
696 crypto_.setDigest(NEW_YS RMD);
697 crypto_.setCipher(NEW_YS AES(AES_256_KEY_SZ));
698 strncpy(parms.cipher_name_,
699 cipher_names[TLS_DHE_DSS_WITH_AES_256_CBC_RMD160],
703 case TLS_DHE_DSS_WITH_AES_128_CBC_RMD160:
704 parms.bulk_cipher_algorithm_ = aes;
705 parms.mac_algorithm_ = rmd;
706 parms.kea_ = diffie_hellman_kea;
707 parms.sig_algo_ = dsa_sa_algo;
708 parms.hash_size_ = RMD_LEN;
709 parms.key_size_ = AES_128_KEY_SZ;
710 parms.iv_size_ = AES_BLOCK_SZ;
711 parms.cipher_type_ =
block;
712 secure_.use_connection().send_server_key_ =
true;
713 crypto_.setDigest(NEW_YS RMD);
714 crypto_.setCipher(NEW_YS AES);
715 strncpy(parms.cipher_name_,
716 cipher_names[TLS_DHE_DSS_WITH_AES_128_CBC_RMD160],
721 SetError(unknown_cipher);
726 typedef volatile LONG yassl_pthread_once_t;
727 #define YASSL_PTHREAD_ONCE_INIT 0
728 #define YASSL_PTHREAD_ONCE_INPROGRESS 1
729 #define YASSL_PTHREAD_ONCE_DONE 2
731 int yassl_pthread_once(yassl_pthread_once_t *once_control,
732 void (*init_routine)(
void))
741 if (*once_control == YASSL_PTHREAD_ONCE_DONE)
744 state= InterlockedCompareExchange(once_control, YASSL_PTHREAD_ONCE_INPROGRESS,
745 YASSL_PTHREAD_ONCE_INIT);
749 case YASSL_PTHREAD_ONCE_INIT:
752 *once_control= YASSL_PTHREAD_ONCE_DONE;
755 case YASSL_PTHREAD_ONCE_INPROGRESS:
757 while(*once_control == YASSL_PTHREAD_ONCE_INPROGRESS)
762 case YASSL_PTHREAD_ONCE_DONE:
769 #define yassl_pthread_once_t pthread_once_t
770 #if defined(PTHREAD_ONCE_INITIALIZER)
771 #define YASSL_PTHREAD_ONCE_INIT PTHREAD_ONCE_INITIALIZER
773 #define YASSL_PTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
775 #define yassl_pthread_once(C,F) pthread_once(C,F)
779 void SSL::set_random(
const opaque* random, ConnectionEnd sender)
781 if (sender == client_end)
782 memcpy(secure_.use_connection().client_random_, random, RAN_LEN);
784 memcpy(secure_.use_connection().server_random_, random, RAN_LEN);
789 void SSL::set_preMaster(
const opaque* pre, uint sz)
791 secure_.use_connection().AllocPreSecret(sz);
792 memcpy(secure_.use_connection().pre_master_secret_, pre, sz);
797 int SSL::SetCompression()
800 secure_.use_connection().compression_ =
true;
809 void SSL::UnSetCompression()
811 secure_.use_connection().compression_ =
false;
816 bool SSL::CompressionOn()
const
818 return secure_.get_connection().compression_;
823 void SSL::set_masterSecret(
const opaque* sec)
825 memcpy(secure_.use_connection().master_secret_, sec, SECRET_LEN);
829 void SSL::set_sessionID(
const opaque* sessionID)
831 memcpy(secure_.use_connection().sessionID_, sessionID, ID_LEN);
832 secure_.use_connection().sessionID_Set_ =
true;
837 void SSL::SetError(YasslError ye)
839 states_.SetError(ye);
846 void SSL::SetQuietShutdown(
bool mode)
848 quietShutdown_ =
mode;
852 Buffers& SSL::useBuffers()
862 static bool setPrefix(opaque* sha_input,
int i)
866 memcpy(sha_input,
"A", 1);
869 memcpy(sha_input,
"BB", 2);
872 memcpy(sha_input,
"CCC", 3);
875 memcpy(sha_input,
"DDDD", 4);
878 memcpy(sha_input,
"EEEEE", 5);
881 memcpy(sha_input,
"FFFFFF", 6);
884 memcpy(sha_input,
"GGGGGGG", 7);
893 const char handshake_order[] =
"Out of order HandShake Message!";
899 void SSL::order_error()
901 SetError(out_of_order);
906 void SSL::makeMasterSecret()
909 makeTLSMasterSecret();
911 opaque sha_output[SHA_LEN];
913 const uint& preSz = secure_.get_connection().pre_secret_len_;
914 output_buffer md5_input(preSz + SHA_LEN);
915 output_buffer sha_input(PREFIX + preSz + 2 * RAN_LEN);
920 md5_input.write(secure_.get_connection().pre_master_secret_, preSz);
922 for (
int i = 0;
i < MASTER_ROUNDS; ++
i) {
923 opaque prefix[PREFIX];
924 if (!setPrefix(prefix,
i)) {
925 SetError(prefix_error);
929 sha_input.set_current(0);
930 sha_input.write(prefix,
i + 1);
932 sha_input.write(secure_.get_connection().pre_master_secret_,preSz);
933 sha_input.write(secure_.get_connection().client_random_, RAN_LEN);
934 sha_input.write(secure_.get_connection().server_random_, RAN_LEN);
935 sha.get_digest(sha_output, sha_input.get_buffer(),
936 sha_input.get_size());
938 md5_input.set_current(preSz);
939 md5_input.write(sha_output, SHA_LEN);
940 md5.get_digest(&secure_.use_connection().master_secret_[
i*MD5_LEN],
941 md5_input.get_buffer(), md5_input.get_size());
945 secure_.use_connection().CleanPreMaster();
950 void SSL::makeTLSMasterSecret()
952 opaque seed[SEED_LEN];
954 memcpy(seed, secure_.get_connection().client_random_, RAN_LEN);
955 memcpy(&seed[RAN_LEN], secure_.get_connection().server_random_, RAN_LEN);
957 PRF(secure_.use_connection().master_secret_, SECRET_LEN,
958 secure_.get_connection().pre_master_secret_,
959 secure_.get_connection().pre_secret_len_,
960 master_label, MASTER_LABEL_SZ,
968 void SSL::deriveKeys()
970 int length = 2 * secure_.get_parms().hash_size_ +
971 2 * secure_.get_parms().key_size_ +
972 2 * secure_.get_parms().iv_size_;
973 int rounds = (length + MD5_LEN - 1 ) / MD5_LEN;
974 input_buffer key_data(rounds * MD5_LEN);
976 opaque sha_output[SHA_LEN];
977 opaque md5_input[SECRET_LEN + SHA_LEN];
978 opaque sha_input[KEY_PREFIX + SECRET_LEN + 2 * RAN_LEN];
983 memcpy(md5_input, secure_.get_connection().master_secret_, SECRET_LEN);
985 for (
int i = 0;
i < rounds; ++
i) {
987 if (!setPrefix(sha_input,
i)) {
988 SetError(prefix_error);
992 memcpy(&sha_input[j], secure_.get_connection().master_secret_,
994 memcpy(&sha_input[j+SECRET_LEN],
995 secure_.get_connection().server_random_, RAN_LEN);
996 memcpy(&sha_input[j + SECRET_LEN + RAN_LEN],
997 secure_.get_connection().client_random_, RAN_LEN);
998 sha.get_digest(sha_output, sha_input,
999 sizeof(sha_input) - KEY_PREFIX + j);
1001 memcpy(&md5_input[SECRET_LEN], sha_output, SHA_LEN);
1002 md5.get_digest(key_data.get_buffer() +
i * MD5_LEN,
1003 md5_input,
sizeof(md5_input));
1005 storeKeys(key_data.get_buffer());
1010 void SSL::deriveTLSKeys()
1012 int length = 2 * secure_.get_parms().hash_size_ +
1013 2 * secure_.get_parms().key_size_ +
1014 2 * secure_.get_parms().iv_size_;
1015 opaque seed[SEED_LEN];
1016 input_buffer key_data(length);
1018 memcpy(seed, secure_.get_connection().server_random_, RAN_LEN);
1019 memcpy(&seed[RAN_LEN], secure_.get_connection().client_random_, RAN_LEN);
1021 PRF(key_data.get_buffer(), length, secure_.get_connection().master_secret_,
1022 SECRET_LEN, key_label, KEY_LABEL_SZ, seed, SEED_LEN);
1024 storeKeys(key_data.get_buffer());
1029 void SSL::storeKeys(
const opaque* key_data)
1031 int sz = secure_.get_parms().hash_size_;
1032 memcpy(secure_.use_connection().client_write_MAC_secret_, key_data, sz);
1034 memcpy(secure_.use_connection().server_write_MAC_secret_,&key_data[
i], sz);
1037 sz = secure_.get_parms().key_size_;
1038 memcpy(secure_.use_connection().client_write_key_, &key_data[
i], sz);
1040 memcpy(secure_.use_connection().server_write_key_, &key_data[
i], sz);
1043 sz = secure_.get_parms().iv_size_;
1044 memcpy(secure_.use_connection().client_write_IV_, &key_data[
i], sz);
1046 memcpy(secure_.use_connection().server_write_IV_, &key_data[
i], sz);
1057 if (secure_.get_parms().entity_ == client_end) {
1058 crypto_.use_cipher().set_encryptKey(conn.client_write_key_,
1059 conn.client_write_IV_);
1060 crypto_.use_cipher().set_decryptKey(conn.server_write_key_,
1061 conn.server_write_IV_);
1064 crypto_.use_cipher().set_encryptKey(conn.server_write_key_,
1065 conn.server_write_IV_);
1066 crypto_.use_cipher().set_decryptKey(conn.client_write_key_,
1067 conn.client_write_IV_);
1074 namespace yassl_int_cpp_local1 {
1078 SumData() : total_(0) {}
1079 void operator()(input_buffer* data) { total_ += data->get_remaining(); }
1086 void operator()(
output_buffer* buffer) { total_ += buffer->get_size(); }
1090 using namespace yassl_int_cpp_local1;
1093 uint SSL::bufferedData()
1095 return STL::for_each(buffers_.getData().begin(),buffers_.getData().end(),
1101 void SSL::fillData(
Data& data)
1103 if (GetError())
return;
1104 uint dataSz = data.get_length();
1105 size_t elements = buffers_.getData().size();
1108 dataSz = min(dataSz, bufferedData());
1110 for (
size_t i = 0; i < elements; i++) {
1111 input_buffer* front = buffers_.getData().front();
1112 uint frontSz = front->get_remaining();
1113 uint readSz = min(dataSz - data.get_length(), frontSz);
1115 front->read(data.set_buffer() + data.get_length(), readSz);
1116 data.set_length(data.get_length() + readSz);
1118 if (readSz == frontSz) {
1119 buffers_.useData().pop_front();
1122 if (data.get_length() == dataSz)
1126 if (buffers_.getData().size() == 0) has_data_ =
false;
1131 void SSL::PeekData(
Data& data)
1133 if (GetError())
return;
1134 uint dataSz = data.get_length();
1135 size_t elements = buffers_.getData().size();
1138 dataSz = min(dataSz, bufferedData());
1140 Buffers::inputList::iterator front = buffers_.useData().begin();
1143 uint frontSz = (*front)->get_remaining();
1144 uint readSz = min(dataSz - data.get_length(), frontSz);
1145 uint before = (*front)->get_current();
1147 (*front)->read(data.set_buffer() + data.get_length(), readSz);
1148 data.set_length(data.get_length() + readSz);
1149 (*front)->set_current(before);
1151 if (data.get_length() == dataSz)
1161 void SSL::flushBuffer()
1163 if (GetError())
return;
1165 uint sz = STL::for_each(buffers_.getHandShake().begin(),
1166 buffers_.getHandShake().end(),
1167 SumBuffer()).total_;
1168 output_buffer out(sz);
1169 size_t elements = buffers_.getHandShake().size();
1171 for (
size_t i = 0; i < elements; i++) {
1172 output_buffer* front = buffers_.getHandShake().front();
1173 out.write(front->get_buffer(), front->get_size());
1175 buffers_.useHandShake().pop_front();
1178 Send(out.get_buffer(), out.get_size());
1182 void SSL::Send(
const byte* buffer, uint sz)
1184 unsigned int sent = 0;
1186 if (socket_.send(buffer, sz, sent) != sz) {
1187 if (socket_.WouldBlock()) {
1188 buffers_.SetOutput(NEW_YS output_buffer(sz - sent, buffer + sent,
1190 SetError(YasslError(SSL_ERROR_WANT_WRITE));
1193 SetError(send_error);
1198 void SSL::SendWriteBuffered()
1200 output_buffer* out = buffers_.TakeOutput();
1204 Send(out->get_buffer(), out->get_size());
1210 uint SSL::get_SEQIncrement(
bool verify)
1213 return secure_.use_connection().peer_sequence_number_++;
1215 return secure_.use_connection().sequence_number_++;
1219 const byte* SSL::get_macSecret(
bool verify)
1221 if ( (secure_.get_parms().entity_ == client_end && !verify) ||
1222 (secure_.get_parms().entity_ == server_end && verify) )
1223 return secure_.get_connection().client_write_MAC_secret_;
1225 return secure_.get_connection().server_write_MAC_secret_;
1229 void SSL::verifyState(
const RecordLayerHeader& rlHeader)
1231 if (GetError())
return;
1233 if (rlHeader.version_.major_ != 3 || rlHeader.version_.minor_ > 2) {
1234 SetError(badVersion_error);
1238 if (states_.getRecord() == recordNotReady ||
1239 (rlHeader.type_ == application_data &&
1240 states_.getHandShake() != handShakeReady) )
1241 SetError(record_layer);
1245 void SSL::verifyState(
const HandShakeHeader& hsHeader)
1247 if (GetError())
return;
1249 if (states_.getHandShake() == handShakeNotReady) {
1250 SetError(handshake_layer);
1254 if (secure_.get_parms().entity_ == client_end)
1255 verifyClientState(hsHeader.get_handshakeType());
1257 verifyServerState(hsHeader.get_handshakeType());
1261 void SSL::verifyState(ClientState cs)
1263 if (GetError())
return;
1264 if (states_.getClient() != cs) order_error();
1268 void SSL::verifyState(ServerState ss)
1270 if (GetError())
return;
1271 if (states_.getServer() != ss) order_error();
1275 void SSL::verfiyHandShakeComplete()
1277 if (GetError())
return;
1278 if (states_.getHandShake() != handShakeReady) order_error();
1282 void SSL::verifyClientState(HandShakeType hsType)
1284 if (GetError())
return;
1288 if (states_.getClient() != serverNull)
1292 if (states_.getClient() != serverHelloComplete)
1295 case server_key_exchange :
1296 if (states_.getClient() != serverCertComplete)
1299 case certificate_request :
1300 if (states_.getClient() != serverCertComplete &&
1301 states_.getClient() != serverKeyExchangeComplete)
1304 case server_hello_done :
1305 if (states_.getClient() != serverCertComplete &&
1306 states_.getClient() != serverKeyExchangeComplete)
1310 if (states_.getClient() != serverHelloDoneComplete ||
1311 secure_.get_parms().pending_)
1320 void SSL::verifyServerState(HandShakeType hsType)
1322 if (GetError())
return;
1326 if (states_.getServer() != clientNull)
1330 if (states_.getServer() != clientHelloComplete)
1333 case client_key_exchange :
1334 if (states_.getServer() != clientHelloComplete)
1337 case certificate_verify :
1338 if (states_.getServer() != clientKeyExchangeComplete)
1342 if (states_.getServer() != clientKeyExchangeComplete ||
1343 secure_.get_parms().pending_)
1353 void SSL::matchSuite(
const opaque* peer, uint length)
1355 if (length == 0 || (length % 2) != 0) {
1356 SetError(bad_input);
1362 for (uint i = 1; i < secure_.get_parms().suites_size_; i += 2)
1363 for (uint j = 1; j < length; j+= 2)
1364 if (secure_.use_parms().suites_[
i] == peer[j]) {
1365 secure_.use_parms().suite_[0] = 0x00;
1366 secure_.use_parms().suite_[1] = peer[j];
1370 SetError(match_error);
1374 void SSL::set_session(SSL_SESSION* s)
1376 if (getSecurity().GetContext()->GetSessionCacheOff())
1379 if (s && GetSessions().
lookup(s->GetID(), &secure_.use_resume())) {
1380 secure_.set_resuming(
true);
1381 crypto_.use_certManager().setPeerX509(s->GetPeerX509());
1386 const Crypto& SSL::getCrypto()
const
1392 const Security& SSL::getSecurity()
const
1398 const States& SSL::getStates()
const
1404 const sslHashes& SSL::getHashes()
const
1410 const sslFactory& SSL::getFactory()
const
1412 return GetSSL_Factory();
1416 const Socket& SSL::getSocket()
const
1422 YasslError SSL::GetError()
const
1424 return states_.What();
1428 bool SSL::GetQuietShutdown()
const
1430 return quietShutdown_;
1434 bool SSL::GetMultiProtocol()
const
1436 return secure_.GetContext()->getMethod()->multipleProtocol();
1440 Crypto& SSL::useCrypto()
1446 Security& SSL::useSecurity()
1452 States& SSL::useStates()
1458 sslHashes& SSL::useHashes()
1464 Socket& SSL::useSocket()
1476 bool SSL::isTLS()
const
1478 return secure_.get_connection().TLS_;
1482 bool SSL::isTLSv1_1()
const
1484 return secure_.get_connection().TLSv1_1_;
1489 bool SSL::HasData()
const
1495 void SSL::addData(input_buffer* data)
1497 buffers_.useData().push_back(data);
1498 if (!has_data_) has_data_ =
true;
1502 void SSL::addBuffer(output_buffer* b)
1504 buffers_.useHandShake().push_back(b);
1508 void SSL_SESSION::CopyX509(X509* x)
1512 X509_NAME* issuer = x->GetIssuer();
1513 X509_NAME* subject = x->GetSubject();
1514 ASN1_TIME* before = x->GetBefore();
1515 ASN1_TIME* after = x->GetAfter();
1517 peerX509_ = NEW_YS X509(issuer->GetName(), issuer->GetLength(),
1518 subject->GetName(), subject->GetLength(),
1524 SSL_SESSION::SSL_SESSION(
const SSL& ssl, RandomPool& ran)
1525 : timeout_(DEFAULT_TIMEOUT), random_(ran), peerX509_(0)
1527 const Connection& conn = ssl.getSecurity().get_connection();
1529 memcpy(sessionID_, conn.sessionID_, ID_LEN);
1530 memcpy(master_secret_, conn.master_secret_, SECRET_LEN);
1531 memcpy(suite_, ssl.getSecurity().get_parms().suite_, SUITE_LEN);
1533 bornOn_ = lowResTimer();
1535 CopyX509(ssl.getCrypto().get_certManager().get_peerX509());
1540 SSL_SESSION::SSL_SESSION(RandomPool& ran)
1541 : bornOn_(0), timeout_(0), random_(ran), peerX509_(0)
1543 memset(sessionID_, 0, ID_LEN);
1544 memset(master_secret_, 0, SECRET_LEN);
1545 memset(suite_, 0, SUITE_LEN);
1549 SSL_SESSION& SSL_SESSION::operator=(
const SSL_SESSION& that)
1551 memcpy(sessionID_, that.sessionID_, ID_LEN);
1552 memcpy(master_secret_, that.master_secret_, SECRET_LEN);
1553 memcpy(suite_, that.suite_, SUITE_LEN);
1555 bornOn_ = that.bornOn_;
1556 timeout_ = that.timeout_;
1559 ysDelete(peerX509_);
1562 CopyX509(that.peerX509_);
1568 const opaque* SSL_SESSION::GetID()
const
1574 const opaque* SSL_SESSION::GetSecret()
const
1576 return master_secret_;
1580 const Cipher* SSL_SESSION::GetSuite()
const
1586 X509* SSL_SESSION::GetPeerX509()
const
1592 uint SSL_SESSION::GetBornOn()
const
1598 uint SSL_SESSION::GetTimeOut()
const
1604 void SSL_SESSION::SetTimeOut(uint t)
1610 extern void clean(
volatile opaque*, uint, RandomPool&);
1614 SSL_SESSION::~SSL_SESSION()
1616 volatile opaque* p = master_secret_;
1617 clean(p, SECRET_LEN, random_);
1619 ysDelete(peerX509_);
1623 static Sessions* sessionsInstance = 0;
1624 static yassl_pthread_once_t session_created= YASSL_PTHREAD_ONCE_INIT;
1626 void Session_initialize()
1628 sessionsInstance = NEW_YS Sessions;
1633 static void c_session_initialize() { Session_initialize(); }
1637 Sessions& GetSessions()
1639 yassl_pthread_once(&session_created, c_session_initialize);
1640 return *sessionsInstance;
1644 static sslFactory* sslFactoryInstance = 0;
1646 sslFactory& GetSSL_Factory()
1648 if (!sslFactoryInstance)
1649 sslFactoryInstance = NEW_YS sslFactory;
1650 return *sslFactoryInstance;
1654 static Errors* errorsInstance = 0;
1658 if (!errorsInstance)
1659 errorsInstance = NEW_YS Errors;
1660 return *errorsInstance;
1664 typedef Mutex::Lock Lock;
1668 void Sessions::add(
const SSL& ssl)
1670 if (ssl.getSecurity().get_connection().sessionID_Set_) {
1672 list_.push_back(NEW_YS SSL_SESSION(ssl, random_));
1676 if (count_ > SESSION_FLUSH_COUNT)
1677 if (!ssl.getSecurity().GetContext()->GetSessionCacheFlushOff())
1682 Sessions::~Sessions()
1684 STL::for_each(list_.begin(), list_.end(), del_ptr_zero());
1689 namespace yassl_int_cpp_local2 {
1691 typedef STL::list<SSL_SESSION*>::iterator sess_iterator;
1692 typedef STL::list<ThreadError>::iterator thr_iterator;
1696 explicit sess_match(
const opaque* p) : id_(p) {}
1700 if ( memcmp(sess->GetID(), id_, ID_LEN) == 0)
1707 THREAD_ID_T GetSelf()
1709 #ifndef _POSIX_THREADS
1710 return GetCurrentThreadId();
1712 return pthread_self();
1718 explicit thr_match() : id_(GetSelf()) {}
1722 if (thr.threadID_ == id_)
1730 using namespace yassl_int_cpp_local2;
1737 sess_iterator find = STL::find_if(list_.begin(), list_.end(),
1739 if (find != list_.end()) {
1740 uint current = lowResTimer();
1741 if ( ((*find)->GetBornOn() + (*find)->GetTimeOut()) < current) {
1755 void Sessions::remove(
const opaque*
id)
1758 sess_iterator find = STL::find_if(list_.begin(), list_.end(),
1760 if (find != list_.end()) {
1761 del_ptr_zero()(*find);
1768 void Sessions::Flush()
1771 sess_iterator next = list_.begin();
1772 uint current = lowResTimer();
1774 while (next != list_.end()) {
1775 sess_iterator si = next;
1777 if ( ((*si)->GetBornOn() + (*si)->GetTimeOut()) < current) {
1778 del_ptr_zero()(*si);
1787 void Errors::Remove()
1790 thr_iterator find = STL::find_if(list_.begin(), list_.end(),
1792 if (find != list_.end())
1798 int Errors::Lookup(
bool peek)
1801 thr_iterator find = STL::find_if(list_.begin(), list_.end(),
1803 if (find != list_.end()) {
1804 int ret = find->errorID_;
1815 void Errors::Add(
int error)
1818 add.errorID_ = error;
1819 add.threadID_ = GetSelf();
1824 list_.push_back(add);
1828 SSL_METHOD::SSL_METHOD(ConnectionEnd ce, ProtocolVersion pv,
bool multiProto)
1829 : version_(pv), side_(ce), verifyPeer_(false), verifyNone_(false),
1830 failNoCert_(false), multipleProtocol_(multiProto)
1834 ProtocolVersion SSL_METHOD::getVersion()
const
1840 ConnectionEnd SSL_METHOD::getSide()
const
1846 void SSL_METHOD::setVerifyPeer()
1852 void SSL_METHOD::setVerifyNone()
1858 void SSL_METHOD::setFailNoCert()
1864 bool SSL_METHOD::verifyPeer()
const
1870 bool SSL_METHOD::verifyNone()
const
1876 bool SSL_METHOD::failNoCert()
const
1882 bool SSL_METHOD::multipleProtocol()
const
1884 return multipleProtocol_;
1889 extern "C" char *yassl_mysql_strdup(
const char *from,
int)
1891 return from ? strdup(from) : NULL;
1898 default_password_callback(
char * buffer,
int size_arg,
int rwflag,
1902 size_t passwd_len,
size= (size_t) size_arg;
1904 passwd= ::yassl_mysql_get_tty_password_ext(
"Enter PEM pass phrase:",
1905 yassl_mysql_strdup);
1910 passwd_len= strlen(passwd);
1917 size_t result_len= size - 1 > passwd_len ?
1918 passwd_len : size - 1;
1919 memcpy(buffer, passwd, result_len);
1920 buffer[result_len]= 0;
1927 SSL_CTX::SSL_CTX(SSL_METHOD* meth)
1928 : method_(meth), certificate_(0), privateKey_(0),
1929 passwordCb_(default_password_callback),
1930 userData_(0), sessionCacheOff_(false), sessionCacheFlushOff_(false),
1938 ysDelete(certificate_);
1939 ysDelete(privateKey_);
1941 STL::for_each(caList_.begin(), caList_.end(), del_ptr_zero());
1945 void SSL_CTX::AddCA(x509* ca)
1947 caList_.push_back(ca);
1951 const SSL_CTX::CertList&
1952 SSL_CTX::GetCA_List()
const
1958 const VerifyCallback SSL_CTX::getVerifyCallback()
const
1960 return verifyCallback_;
1964 const x509* SSL_CTX::getCert()
const
1966 return certificate_;
1970 const x509* SSL_CTX::getKey()
const
1976 const SSL_METHOD* SSL_CTX::getMethod()
const
1982 const Ciphers& SSL_CTX::GetCiphers()
const
1988 const DH_Parms& SSL_CTX::GetDH_Parms()
const
1994 const Stats& SSL_CTX::GetStats()
const
2000 pem_password_cb SSL_CTX::GetPasswordCb()
const
2006 void SSL_CTX::SetPasswordCb(pem_password_cb cb)
2012 void* SSL_CTX::GetUserData()
const
2018 bool SSL_CTX::GetSessionCacheOff()
const
2020 return sessionCacheOff_;
2024 bool SSL_CTX::GetSessionCacheFlushOff()
const
2026 return sessionCacheFlushOff_;
2030 void SSL_CTX::SetUserData(
void* data)
2036 void SSL_CTX::SetSessionCacheOff()
2038 sessionCacheOff_ =
true;
2042 void SSL_CTX::SetSessionCacheFlushOff()
2044 sessionCacheFlushOff_ =
true;
2048 void SSL_CTX::setVerifyPeer()
2050 method_->setVerifyPeer();
2054 void SSL_CTX::setVerifyNone()
2056 method_->setVerifyNone();
2060 void SSL_CTX::setFailNoCert()
2062 method_->setFailNoCert();
2066 void SSL_CTX::setVerifyCallback(VerifyCallback vc)
2068 verifyCallback_ = vc;
2072 bool SSL_CTX::SetDH(
const DH& dh)
2074 dhParms_.p_ = dh.p->int_;
2075 dhParms_.g_ = dh.g->int_;
2077 return dhParms_.set_ =
true;
2081 bool SSL_CTX::SetCipherList(
const char* list)
2087 char name[MAX_SUITE_NAME];
2089 char needle[] =
":";
2090 char* haystack =
const_cast<char*
>(list);
2093 const int suiteSz =
sizeof(cipher_names) /
sizeof(cipher_names[0]);
2099 haystack = strstr(haystack, needle);
2102 len = min(
sizeof(name), strlen(prev));
2104 len = min(
sizeof(name), (
size_t)(haystack - prev));
2106 strncpy(name, prev, len);
2107 name[(len ==
sizeof(
name)) ? len - 1 : len] = 0;
2109 for (
int i = 0; i < suiteSz; i++)
2110 if (strncmp(name, cipher_names[i],
sizeof(name)) == 0) {
2112 ciphers_.suites_[idx++] = 0x00;
2113 ciphers_.suites_[idx++] =
i;
2115 if (!ret) ret =
true;
2118 if (!haystack)
break;
2123 ciphers_.setSuites_ =
true;
2124 ciphers_.suiteSz_ = idx;
2131 void SSL_CTX::IncrementStats(StatsField fd)
2147 ++stats_.acceptGood_;
2151 ++stats_.connectGood_;
2154 case AcceptRenegotiate:
2155 ++stats_.acceptRenegotiate_;
2158 case ConnectRenegotiate:
2159 ++stats_.connectRenegotiate_;
2171 ++stats_.cacheFull_;
2187 ++stats_.getCacheSize_;
2191 ++stats_.verifyMode_;
2195 ++stats_.verifyDepth_;
2205 : digest_(0), cipher_(0), dh_(0)
2217 const Digest& Crypto::get_digest()
const
2223 const BulkCipher& Crypto::get_cipher()
const
2229 const DiffieHellman& Crypto::get_dh()
const
2235 const RandomPool& Crypto::get_random()
const
2241 const CertManager& Crypto::get_certManager()
const
2248 Digest& Crypto::use_digest()
2254 BulkCipher& Crypto::use_cipher()
2260 DiffieHellman& Crypto::use_dh()
2266 RandomPool& Crypto::use_random()
2272 CertManager& Crypto::use_certManager()
2279 void Crypto::SetDH(DiffieHellman* dh)
2285 void Crypto::SetDH(
const DH_Parms& dh)
2288 dh_ = NEW_YS DiffieHellman(dh.p_, dh.g_, random_);
2292 bool Crypto::DhSet()
2298 void Crypto::setDigest(Digest* digest)
2304 void Crypto::setCipher(BulkCipher* c)
2310 const MD5& sslHashes::get_MD5()
const
2312 return md5HandShake_;
2316 const SHA& sslHashes::get_SHA()
const
2318 return shaHandShake_;
2322 const Finished& sslHashes::get_verify()
const
2328 const Hashes& sslHashes::get_certVerify()
const
2334 MD5& sslHashes::use_MD5(){
2335 return md5HandShake_;
2339 SHA& sslHashes::use_SHA()
2341 return shaHandShake_;
2345 Finished& sslHashes::use_verify()
2351 Hashes& sslHashes::use_certVerify()
2357 Buffers::Buffers() : prevSent(0), plainSz(0), rawInput_(0), output_(0)
2363 STL::for_each(handShakeList_.begin(), handShakeList_.end(),
2365 STL::for_each(dataList_.begin(), dataList_.end(),
2367 ysDelete(rawInput_);
2372 void Buffers::SetOutput(output_buffer* ob)
2378 void Buffers::SetRawInput(input_buffer* ib)
2384 input_buffer* Buffers::TakeRawInput()
2386 input_buffer* ret = rawInput_;
2393 output_buffer* Buffers::TakeOutput()
2395 output_buffer* ret = output_;
2402 const Buffers::inputList& Buffers::getData()
const
2408 const Buffers::outputList& Buffers::getHandShake()
const
2410 return handShakeList_;
2414 Buffers::inputList& Buffers::useData()
2420 Buffers::outputList& Buffers::useHandShake()
2422 return handShakeList_;
2426 Security::Security(ProtocolVersion pv, RandomPool& ran, ConnectionEnd ce,
2427 const Ciphers& ciphers, SSL_CTX* ctx,
bool haveDH)
2428 : conn_(pv, ran), parms_(ce, ciphers, pv, haveDH), resumeSession_(ran),
2429 ctx_(ctx), resuming_(false)
2433 const Connection& Security::get_connection()
const
2439 const SSL_CTX* Security::GetContext()
const
2445 const Parameters& Security::get_parms()
const
2451 const SSL_SESSION& Security::get_resume()
const
2453 return resumeSession_;
2457 bool Security::get_resuming()
const
2469 Parameters& Security::use_parms()
2475 SSL_SESSION& Security::use_resume()
2477 return resumeSession_;
2481 void Security::set_resuming(
bool b)
2487 X509_NAME::X509_NAME(
const char*
n,
size_t sz)
2491 name_ = NEW_YS
char[sz];
2492 memcpy(name_, n, sz);
2498 X509_NAME::~X509_NAME()
2500 ysArrayDelete(name_);
2501 ysArrayDelete(entry_.data);
2505 const char* X509_NAME::GetName()
const
2511 size_t X509_NAME::GetLength()
const
2517 X509::X509(
const char* i,
size_t iSz,
const char* s,
size_t sSz,
2519 : issuer_(i, iSz), subject_(s, sSz),
2520 beforeDate_((char *) b->data, b->length, b->
type),
2521 afterDate_((char *) a->data, a->length, a->
type)
2525 X509_NAME* X509::GetIssuer()
2531 X509_NAME* X509::GetSubject()
2537 ASN1_TIME* X509::GetBefore()
2539 return beforeDate_.GetString();
2543 ASN1_TIME* X509::GetAfter()
2545 return afterDate_.GetString();
2551 if (i < 0 || i >=
int(sz_))
2555 ysArrayDelete(entry_.data);
2556 entry_.data = NEW_YS byte[sz_];
2558 memcpy(entry_.data, &name_[i], sz_ - i);
2559 if (entry_.data[sz_ -i - 1]) {
2560 entry_.data[sz_ -
i] = 0;
2561 entry_.length = int(sz_) -
i;
2564 entry_.length = int(sz_) - i - 1;
2571 StringHolder::StringHolder(
const char* str,
int sz, byte
type)
2573 asnString_.length = sz;
2574 asnString_.data = NEW_YS byte[sz + 1];
2575 memcpy(asnString_.data, str, sz);
2576 asnString_.type =
type;
2580 StringHolder::~StringHolder()
2582 ysArrayDelete(asnString_.data);
2594 void* myAlloc(
void* ,
unsigned int item,
unsigned int size)
2596 return NEW_YS
unsigned char[item *
size];
2600 void myFree(
void* ,
void* memory)
2602 unsigned char* ptr =
static_cast<unsigned char*
>(memory);
2603 yaSSL::ysArrayDelete(ptr);
2608 int Compress(
const byte* in,
int sz, input_buffer& buffer)
2610 byte tmp[LENGTH_SZ];
2613 buffer.allocate(sz +
sizeof(uint16) + COMPRESS_EXTRA);
2615 c_stream.zalloc = myAlloc;
2616 c_stream.zfree = myFree;
2617 c_stream.opaque = (voidpf)0;
2619 c_stream.next_in =
const_cast<byte*
>(in);
2620 c_stream.avail_in = sz;
2621 c_stream.next_out = buffer.get_buffer() +
sizeof(tmp);
2622 c_stream.avail_out = buffer.get_capacity() -
sizeof(tmp);
2624 if (deflateInit(&c_stream, 8) != Z_OK)
return -1;
2625 int err = deflate(&c_stream, Z_FINISH);
2626 deflateEnd(&c_stream);
2627 if (err != Z_OK && err != Z_STREAM_END)
return -1;
2630 memcpy(buffer.get_buffer(), tmp,
sizeof(tmp));
2631 buffer.add_size(c_stream.total_out +
sizeof(tmp));
2638 int DeCompress(input_buffer& in,
int sz, input_buffer& out)
2640 byte tmp[LENGTH_SZ];
2642 in.read(tmp,
sizeof(tmp));
2651 d_stream.zalloc = myAlloc;
2652 d_stream.zfree = myFree;
2653 d_stream.opaque = (voidpf)0;
2655 d_stream.next_in = in.get_buffer() + in.get_current();
2656 d_stream.avail_in = sz -
sizeof(tmp);
2657 d_stream.next_out = out.get_buffer();
2658 d_stream.avail_out = out.get_capacity();
2660 if (inflateInit(&d_stream) != Z_OK)
return -1;
2661 int err = inflate(&d_stream, Z_FINISH);
2662 inflateEnd(&d_stream);
2663 if (err != Z_OK && err != Z_STREAM_END)
return -1;
2665 out.add_size(d_stream.total_out);
2666 in.set_current(in.get_current() + sz -
sizeof(tmp));
2675 int Compress(
const byte* in,
int sz, input_buffer& buffer)
2681 int DeCompress(input_buffer& in,
int sz, input_buffer& out)
2694 extern "C" void yaSSL_CleanUp()
2696 TaoCrypt::CleanUp();
2697 yaSSL::ysDelete(yaSSL::sslFactoryInstance);
2698 yaSSL::ysDelete(yaSSL::sessionsInstance);
2699 yaSSL::ysDelete(yaSSL::errorsInstance);
2702 yaSSL::sslFactoryInstance = 0;
2703 yaSSL::sessionsInstance = 0;
2704 yaSSL::errorsInstance = 0;
2708 #ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION