OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 : BufferedReadAdapter(socket, buffer_size) { | 130 : BufferedReadAdapter(socket, buffer_size) { |
131 } | 131 } |
132 | 132 |
133 AsyncProxyServerSocket::~AsyncProxyServerSocket() = default; | 133 AsyncProxyServerSocket::~AsyncProxyServerSocket() = default; |
134 | 134 |
135 /////////////////////////////////////////////////////////////////////////////// | 135 /////////////////////////////////////////////////////////////////////////////// |
136 | 136 |
137 // This is a SSL v2 CLIENT_HELLO message. | 137 // This is a SSL v2 CLIENT_HELLO message. |
138 // TODO: Should this have a session id? The response doesn't have a | 138 // TODO: Should this have a session id? The response doesn't have a |
139 // certificate, so the hello should have a session id. | 139 // certificate, so the hello should have a session id. |
140 static const uint8 kSslClientHello[] = { | 140 static const uint8_t kSslClientHello[] = { |
141 0x80, 0x46, // msg len | 141 0x80, 0x46, // msg len |
142 0x01, // CLIENT_HELLO | 142 0x01, // CLIENT_HELLO |
143 0x03, 0x01, // SSL 3.1 | 143 0x03, 0x01, // SSL 3.1 |
144 0x00, 0x2d, // ciphersuite len | 144 0x00, 0x2d, // ciphersuite len |
145 0x00, 0x00, // session id len | 145 0x00, 0x00, // session id len |
146 0x00, 0x10, // challenge len | 146 0x00, 0x10, // challenge len |
147 0x01, 0x00, 0x80, 0x03, 0x00, 0x80, 0x07, 0x00, 0xc0, // ciphersuites | 147 0x01, 0x00, 0x80, 0x03, 0x00, 0x80, 0x07, 0x00, 0xc0, // ciphersuites |
148 0x06, 0x00, 0x40, 0x02, 0x00, 0x80, 0x04, 0x00, 0x80, // | 148 0x06, 0x00, 0x40, 0x02, 0x00, 0x80, 0x04, 0x00, 0x80, // |
149 0x00, 0x00, 0x04, 0x00, 0xfe, 0xff, 0x00, 0x00, 0x0a, // | 149 0x00, 0x00, 0x04, 0x00, 0xfe, 0xff, 0x00, 0x00, 0x0a, // |
150 0x00, 0xfe, 0xfe, 0x00, 0x00, 0x09, 0x00, 0x00, 0x64, // | 150 0x00, 0xfe, 0xfe, 0x00, 0x00, 0x09, 0x00, 0x00, 0x64, // |
151 0x00, 0x00, 0x62, 0x00, 0x00, 0x03, 0x00, 0x00, 0x06, // | 151 0x00, 0x00, 0x62, 0x00, 0x00, 0x03, 0x00, 0x00, 0x06, // |
152 0x1f, 0x17, 0x0c, 0xa6, 0x2f, 0x00, 0x78, 0xfc, // challenge | 152 0x1f, 0x17, 0x0c, 0xa6, 0x2f, 0x00, 0x78, 0xfc, // challenge |
153 0x46, 0x55, 0x2e, 0xb1, 0x83, 0x39, 0xf1, 0xea // | 153 0x46, 0x55, 0x2e, 0xb1, 0x83, 0x39, 0xf1, 0xea // |
154 }; | 154 }; |
155 | 155 |
156 // This is a TLSv1 SERVER_HELLO message. | 156 // This is a TLSv1 SERVER_HELLO message. |
157 static const uint8 kSslServerHello[] = { | 157 static const uint8_t kSslServerHello[] = { |
158 0x16, // handshake message | 158 0x16, // handshake message |
159 0x03, 0x01, // SSL 3.1 | 159 0x03, 0x01, // SSL 3.1 |
160 0x00, 0x4a, // message len | 160 0x00, 0x4a, // message len |
161 0x02, // SERVER_HELLO | 161 0x02, // SERVER_HELLO |
162 0x00, 0x00, 0x46, // handshake len | 162 0x00, 0x00, 0x46, // handshake len |
163 0x03, 0x01, // SSL 3.1 | 163 0x03, 0x01, // SSL 3.1 |
164 0x42, 0x85, 0x45, 0xa7, 0x27, 0xa9, 0x5d, 0xa0, // server random | 164 0x42, 0x85, 0x45, 0xa7, 0x27, 0xa9, 0x5d, 0xa0, // server random |
165 0xb3, 0xc5, 0xe7, 0x53, 0xda, 0x48, 0x2b, 0x3f, // | 165 0xb3, 0xc5, 0xe7, 0x53, 0xda, 0x48, 0x2b, 0x3f, // |
166 0xc6, 0x5a, 0xca, 0x89, 0xc1, 0x58, 0x52, 0xa1, // | 166 0xc6, 0x5a, 0xca, 0x89, 0xc1, 0x58, 0x52, 0xa1, // |
167 0x78, 0x3c, 0x5b, 0x17, 0x46, 0x00, 0x85, 0x3f, // | 167 0x78, 0x3c, 0x5b, 0x17, 0x46, 0x00, 0x85, 0x3f, // |
168 0x20, // session id len | 168 0x20, // session id len |
169 0x0e, 0xd3, 0x06, 0x72, 0x5b, 0x5b, 0x1b, 0x5f, // session id | 169 0x0e, 0xd3, 0x06, 0x72, 0x5b, 0x5b, 0x1b, 0x5f, // session id |
170 0x15, 0xac, 0x13, 0xf9, 0x88, 0x53, 0x9d, 0x9b, // | 170 0x15, 0xac, 0x13, 0xf9, 0x88, 0x53, 0x9d, 0x9b, // |
171 0xe8, 0x3d, 0x7b, 0x0c, 0x30, 0x32, 0x6e, 0x38, // | 171 0xe8, 0x3d, 0x7b, 0x0c, 0x30, 0x32, 0x6e, 0x38, // |
172 0x4d, 0xa2, 0x75, 0x57, 0x41, 0x6c, 0x34, 0x5c, // | 172 0x4d, 0xa2, 0x75, 0x57, 0x41, 0x6c, 0x34, 0x5c, // |
173 0x00, 0x04, // RSA/RC4-128/MD5 | 173 0x00, 0x04, // RSA/RC4-128/MD5 |
174 0x00 // null compression | 174 0x00 // null compression |
175 }; | 175 }; |
176 | 176 |
177 AsyncSSLSocket::AsyncSSLSocket(AsyncSocket* socket) | 177 AsyncSSLSocket::AsyncSSLSocket(AsyncSocket* socket) |
178 : BufferedReadAdapter(socket, 1024) { | 178 : BufferedReadAdapter(socket, 1024) { |
179 } | 179 } |
180 | 180 |
181 int AsyncSSLSocket::Connect(const SocketAddress& addr) { | 181 int AsyncSSLSocket::Connect(const SocketAddress& addr) { |
182 // Begin buffering before we connect, so that there isn't a race condition | 182 // Begin buffering before we connect, so that there isn't a race condition |
183 // between potential senders and receiving the OnConnectEvent signal | 183 // between potential senders and receiving the OnConnectEvent signal |
184 BufferInput(true); | 184 BufferInput(true); |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 void AsyncSocksProxySocket::OnConnectEvent(AsyncSocket* socket) { | 557 void AsyncSocksProxySocket::OnConnectEvent(AsyncSocket* socket) { |
558 SendHello(); | 558 SendHello(); |
559 } | 559 } |
560 | 560 |
561 void AsyncSocksProxySocket::ProcessInput(char* data, size_t* len) { | 561 void AsyncSocksProxySocket::ProcessInput(char* data, size_t* len) { |
562 ASSERT(state_ < SS_TUNNEL); | 562 ASSERT(state_ < SS_TUNNEL); |
563 | 563 |
564 ByteBuffer response(data, *len); | 564 ByteBuffer response(data, *len); |
565 | 565 |
566 if (state_ == SS_HELLO) { | 566 if (state_ == SS_HELLO) { |
567 uint8 ver, method; | 567 uint8_t ver, method; |
568 if (!response.ReadUInt8(&ver) || | 568 if (!response.ReadUInt8(&ver) || |
569 !response.ReadUInt8(&method)) | 569 !response.ReadUInt8(&method)) |
570 return; | 570 return; |
571 | 571 |
572 if (ver != 5) { | 572 if (ver != 5) { |
573 Error(0); | 573 Error(0); |
574 return; | 574 return; |
575 } | 575 } |
576 | 576 |
577 if (method == 0) { | 577 if (method == 0) { |
578 SendConnect(); | 578 SendConnect(); |
579 } else if (method == 2) { | 579 } else if (method == 2) { |
580 SendAuth(); | 580 SendAuth(); |
581 } else { | 581 } else { |
582 Error(0); | 582 Error(0); |
583 return; | 583 return; |
584 } | 584 } |
585 } else if (state_ == SS_AUTH) { | 585 } else if (state_ == SS_AUTH) { |
586 uint8 ver, status; | 586 uint8_t ver, status; |
587 if (!response.ReadUInt8(&ver) || | 587 if (!response.ReadUInt8(&ver) || |
588 !response.ReadUInt8(&status)) | 588 !response.ReadUInt8(&status)) |
589 return; | 589 return; |
590 | 590 |
591 if ((ver != 1) || (status != 0)) { | 591 if ((ver != 1) || (status != 0)) { |
592 Error(SOCKET_EACCES); | 592 Error(SOCKET_EACCES); |
593 return; | 593 return; |
594 } | 594 } |
595 | 595 |
596 SendConnect(); | 596 SendConnect(); |
597 } else if (state_ == SS_CONNECT) { | 597 } else if (state_ == SS_CONNECT) { |
598 uint8 ver, rep, rsv, atyp; | 598 uint8_t ver, rep, rsv, atyp; |
599 if (!response.ReadUInt8(&ver) || | 599 if (!response.ReadUInt8(&ver) || |
600 !response.ReadUInt8(&rep) || | 600 !response.ReadUInt8(&rep) || |
601 !response.ReadUInt8(&rsv) || | 601 !response.ReadUInt8(&rsv) || |
602 !response.ReadUInt8(&atyp)) | 602 !response.ReadUInt8(&atyp)) |
603 return; | 603 return; |
604 | 604 |
605 if ((ver != 5) || (rep != 0)) { | 605 if ((ver != 5) || (rep != 0)) { |
606 Error(0); | 606 Error(0); |
607 return; | 607 return; |
608 } | 608 } |
609 | 609 |
610 uint16 port; | 610 uint16_t port; |
611 if (atyp == 1) { | 611 if (atyp == 1) { |
612 uint32 addr; | 612 uint32_t addr; |
613 if (!response.ReadUInt32(&addr) || | 613 if (!response.ReadUInt32(&addr) || |
614 !response.ReadUInt16(&port)) | 614 !response.ReadUInt16(&port)) |
615 return; | 615 return; |
616 LOG(LS_VERBOSE) << "Bound on " << addr << ":" << port; | 616 LOG(LS_VERBOSE) << "Bound on " << addr << ":" << port; |
617 } else if (atyp == 3) { | 617 } else if (atyp == 3) { |
618 uint8 len; | 618 uint8_t len; |
619 std::string addr; | 619 std::string addr; |
620 if (!response.ReadUInt8(&len) || | 620 if (!response.ReadUInt8(&len) || |
621 !response.ReadString(&addr, len) || | 621 !response.ReadString(&addr, len) || |
622 !response.ReadUInt16(&port)) | 622 !response.ReadUInt16(&port)) |
623 return; | 623 return; |
624 LOG(LS_VERBOSE) << "Bound on " << addr << ":" << port; | 624 LOG(LS_VERBOSE) << "Bound on " << addr << ":" << port; |
625 } else if (atyp == 4) { | 625 } else if (atyp == 4) { |
626 std::string addr; | 626 std::string addr; |
627 if (!response.ReadString(&addr, 16) || | 627 if (!response.ReadString(&addr, 16) || |
628 !response.ReadUInt16(&port)) | 628 !response.ReadUInt16(&port)) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 request.WriteUInt8(0); // No authentication | 663 request.WriteUInt8(0); // No authentication |
664 request.WriteUInt8(2); // Username/Password | 664 request.WriteUInt8(2); // Username/Password |
665 } | 665 } |
666 DirectSend(request.Data(), request.Length()); | 666 DirectSend(request.Data(), request.Length()); |
667 state_ = SS_HELLO; | 667 state_ = SS_HELLO; |
668 } | 668 } |
669 | 669 |
670 void AsyncSocksProxySocket::SendAuth() { | 670 void AsyncSocksProxySocket::SendAuth() { |
671 ByteBuffer request; | 671 ByteBuffer request; |
672 request.WriteUInt8(1); // Negotiation Version | 672 request.WriteUInt8(1); // Negotiation Version |
673 request.WriteUInt8(static_cast<uint8>(user_.size())); | 673 request.WriteUInt8(static_cast<uint8_t>(user_.size())); |
674 request.WriteString(user_); // Username | 674 request.WriteString(user_); // Username |
675 request.WriteUInt8(static_cast<uint8>(pass_.GetLength())); | 675 request.WriteUInt8(static_cast<uint8_t>(pass_.GetLength())); |
676 size_t len = pass_.GetLength() + 1; | 676 size_t len = pass_.GetLength() + 1; |
677 char * sensitive = new char[len]; | 677 char * sensitive = new char[len]; |
678 pass_.CopyTo(sensitive, true); | 678 pass_.CopyTo(sensitive, true); |
679 request.WriteString(sensitive); // Password | 679 request.WriteString(sensitive); // Password |
680 memset(sensitive, 0, len); | 680 memset(sensitive, 0, len); |
681 delete [] sensitive; | 681 delete [] sensitive; |
682 DirectSend(request.Data(), request.Length()); | 682 DirectSend(request.Data(), request.Length()); |
683 state_ = SS_AUTH; | 683 state_ = SS_AUTH; |
684 } | 684 } |
685 | 685 |
686 void AsyncSocksProxySocket::SendConnect() { | 686 void AsyncSocksProxySocket::SendConnect() { |
687 ByteBuffer request; | 687 ByteBuffer request; |
688 request.WriteUInt8(5); // Socks Version | 688 request.WriteUInt8(5); // Socks Version |
689 request.WriteUInt8(1); // CONNECT | 689 request.WriteUInt8(1); // CONNECT |
690 request.WriteUInt8(0); // Reserved | 690 request.WriteUInt8(0); // Reserved |
691 if (dest_.IsUnresolved()) { | 691 if (dest_.IsUnresolved()) { |
692 std::string hostname = dest_.hostname(); | 692 std::string hostname = dest_.hostname(); |
693 request.WriteUInt8(3); // DOMAINNAME | 693 request.WriteUInt8(3); // DOMAINNAME |
694 request.WriteUInt8(static_cast<uint8>(hostname.size())); | 694 request.WriteUInt8(static_cast<uint8_t>(hostname.size())); |
695 request.WriteString(hostname); // Destination Hostname | 695 request.WriteString(hostname); // Destination Hostname |
696 } else { | 696 } else { |
697 request.WriteUInt8(1); // IPV4 | 697 request.WriteUInt8(1); // IPV4 |
698 request.WriteUInt32(dest_.ip()); // Destination IP | 698 request.WriteUInt32(dest_.ip()); // Destination IP |
699 } | 699 } |
700 request.WriteUInt16(dest_.port()); // Destination Port | 700 request.WriteUInt16(dest_.port()); // Destination Port |
701 DirectSend(request.Data(), request.Length()); | 701 DirectSend(request.Data(), request.Length()); |
702 state_ = SS_CONNECT; | 702 state_ = SS_CONNECT; |
703 } | 703 } |
704 | 704 |
(...skipping 26 matching lines...) Expand all Loading... |
731 // Consume parsed data | 731 // Consume parsed data |
732 *len = response.Length(); | 732 *len = response.Length(); |
733 memcpy(data, response.Data(), *len); | 733 memcpy(data, response.Data(), *len); |
734 } | 734 } |
735 | 735 |
736 void AsyncSocksProxyServerSocket::DirectSend(const ByteBuffer& buf) { | 736 void AsyncSocksProxyServerSocket::DirectSend(const ByteBuffer& buf) { |
737 BufferedReadAdapter::DirectSend(buf.Data(), buf.Length()); | 737 BufferedReadAdapter::DirectSend(buf.Data(), buf.Length()); |
738 } | 738 } |
739 | 739 |
740 void AsyncSocksProxyServerSocket::HandleHello(ByteBuffer* request) { | 740 void AsyncSocksProxyServerSocket::HandleHello(ByteBuffer* request) { |
741 uint8 ver, num_methods; | 741 uint8_t ver, num_methods; |
742 if (!request->ReadUInt8(&ver) || | 742 if (!request->ReadUInt8(&ver) || |
743 !request->ReadUInt8(&num_methods)) { | 743 !request->ReadUInt8(&num_methods)) { |
744 Error(0); | 744 Error(0); |
745 return; | 745 return; |
746 } | 746 } |
747 | 747 |
748 if (ver != 5) { | 748 if (ver != 5) { |
749 Error(0); | 749 Error(0); |
750 return; | 750 return; |
751 } | 751 } |
752 | 752 |
753 // Handle either no-auth (0) or user/pass auth (2) | 753 // Handle either no-auth (0) or user/pass auth (2) |
754 uint8 method = 0xFF; | 754 uint8_t method = 0xFF; |
755 if (num_methods > 0 && !request->ReadUInt8(&method)) { | 755 if (num_methods > 0 && !request->ReadUInt8(&method)) { |
756 Error(0); | 756 Error(0); |
757 return; | 757 return; |
758 } | 758 } |
759 | 759 |
760 // TODO: Ask the server which method to use. | 760 // TODO: Ask the server which method to use. |
761 SendHelloReply(method); | 761 SendHelloReply(method); |
762 if (method == 0) { | 762 if (method == 0) { |
763 state_ = SS_CONNECT; | 763 state_ = SS_CONNECT; |
764 } else if (method == 2) { | 764 } else if (method == 2) { |
765 state_ = SS_AUTH; | 765 state_ = SS_AUTH; |
766 } else { | 766 } else { |
767 state_ = SS_ERROR; | 767 state_ = SS_ERROR; |
768 } | 768 } |
769 } | 769 } |
770 | 770 |
771 void AsyncSocksProxyServerSocket::SendHelloReply(uint8 method) { | 771 void AsyncSocksProxyServerSocket::SendHelloReply(uint8_t method) { |
772 ByteBuffer response; | 772 ByteBuffer response; |
773 response.WriteUInt8(5); // Socks Version | 773 response.WriteUInt8(5); // Socks Version |
774 response.WriteUInt8(method); // Auth method | 774 response.WriteUInt8(method); // Auth method |
775 DirectSend(response); | 775 DirectSend(response); |
776 } | 776 } |
777 | 777 |
778 void AsyncSocksProxyServerSocket::HandleAuth(ByteBuffer* request) { | 778 void AsyncSocksProxyServerSocket::HandleAuth(ByteBuffer* request) { |
779 uint8 ver, user_len, pass_len; | 779 uint8_t ver, user_len, pass_len; |
780 std::string user, pass; | 780 std::string user, pass; |
781 if (!request->ReadUInt8(&ver) || | 781 if (!request->ReadUInt8(&ver) || |
782 !request->ReadUInt8(&user_len) || | 782 !request->ReadUInt8(&user_len) || |
783 !request->ReadString(&user, user_len) || | 783 !request->ReadString(&user, user_len) || |
784 !request->ReadUInt8(&pass_len) || | 784 !request->ReadUInt8(&pass_len) || |
785 !request->ReadString(&pass, pass_len)) { | 785 !request->ReadString(&pass, pass_len)) { |
786 Error(0); | 786 Error(0); |
787 return; | 787 return; |
788 } | 788 } |
789 | 789 |
790 // TODO: Allow for checking of credentials. | 790 // TODO: Allow for checking of credentials. |
791 SendAuthReply(0); | 791 SendAuthReply(0); |
792 state_ = SS_CONNECT; | 792 state_ = SS_CONNECT; |
793 } | 793 } |
794 | 794 |
795 void AsyncSocksProxyServerSocket::SendAuthReply(uint8 result) { | 795 void AsyncSocksProxyServerSocket::SendAuthReply(uint8_t result) { |
796 ByteBuffer response; | 796 ByteBuffer response; |
797 response.WriteUInt8(1); // Negotiation Version | 797 response.WriteUInt8(1); // Negotiation Version |
798 response.WriteUInt8(result); | 798 response.WriteUInt8(result); |
799 DirectSend(response); | 799 DirectSend(response); |
800 } | 800 } |
801 | 801 |
802 void AsyncSocksProxyServerSocket::HandleConnect(ByteBuffer* request) { | 802 void AsyncSocksProxyServerSocket::HandleConnect(ByteBuffer* request) { |
803 uint8 ver, command, reserved, addr_type; | 803 uint8_t ver, command, reserved, addr_type; |
804 uint32 ip; | 804 uint32_t ip; |
805 uint16 port; | 805 uint16_t port; |
806 if (!request->ReadUInt8(&ver) || | 806 if (!request->ReadUInt8(&ver) || |
807 !request->ReadUInt8(&command) || | 807 !request->ReadUInt8(&command) || |
808 !request->ReadUInt8(&reserved) || | 808 !request->ReadUInt8(&reserved) || |
809 !request->ReadUInt8(&addr_type) || | 809 !request->ReadUInt8(&addr_type) || |
810 !request->ReadUInt32(&ip) || | 810 !request->ReadUInt32(&ip) || |
811 !request->ReadUInt16(&port)) { | 811 !request->ReadUInt16(&port)) { |
812 Error(0); | 812 Error(0); |
813 return; | 813 return; |
814 } | 814 } |
815 | 815 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
903 void LoggingSocketAdapter::OnCloseEvent(AsyncSocket * socket, int err) { | 903 void LoggingSocketAdapter::OnCloseEvent(AsyncSocket * socket, int err) { |
904 LogMultiline(level_, label_.c_str(), false, NULL, 0, hex_mode_, &lms_); | 904 LogMultiline(level_, label_.c_str(), false, NULL, 0, hex_mode_, &lms_); |
905 LogMultiline(level_, label_.c_str(), true, NULL, 0, hex_mode_, &lms_); | 905 LogMultiline(level_, label_.c_str(), true, NULL, 0, hex_mode_, &lms_); |
906 LOG_V(level_) << label_ << " Closed with error: " << err; | 906 LOG_V(level_) << label_ << " Closed with error: " << err; |
907 AsyncSocketAdapter::OnCloseEvent(socket, err); | 907 AsyncSocketAdapter::OnCloseEvent(socket, err); |
908 } | 908 } |
909 | 909 |
910 /////////////////////////////////////////////////////////////////////////////// | 910 /////////////////////////////////////////////////////////////////////////////// |
911 | 911 |
912 } // namespace rtc | 912 } // namespace rtc |
OLD | NEW |