bool avjackif::async_handshake(boost::asio::yield_context yield_context) { uint32_t hostl, netl; std::string buf; auto random_pub_key = async_client_hello(yield_context); // 接着私钥加密 随机数 auto singned = RSA_private_encrypt(_rsa.get(), random_pub_key); proto::login login_packet; login_packet.set_user_cert(i2d_X509(_x509.get())); login_packet.set_encryped_radom_key(singned); boost::asio::async_write(*m_sock, boost::asio::buffer(av_router::encode(login_packet)), yield_context); // 读取回应并解码 std::unique_ptr<proto::login_result> login_result( (proto::login_result*)async_read_protobuf_message(*m_sock, yield_context)); return login_result.get()->result() == proto::login_result::LOGIN_SUCCEED; }
bool avjackif::async_handshake(boost::asio::yield_context yield_context) { proto::client_hello client_hello; client_hello.set_client("avim"); client_hello.set_version(0001); unsigned char to[512]; auto dh = DH_new(); DH_generate_parameters_ex(dh,64,DH_GENERATOR_5,NULL); DH_generate_key(dh); // 把 g,p, pubkey 传过去 client_hello.set_random_g((const void*)to, BN_bn2bin(dh->g, to)); client_hello.set_random_p((const void*)to, BN_bn2bin(dh->p, to)); client_hello.set_random_pub_key((const void*)to, BN_bn2bin(dh->pub_key, to)); auto tobesend = av_router::encode(client_hello); boost::asio::async_write(*m_sock, boost::asio::buffer(tobesend), yield_context); std::uint32_t l; boost::asio::async_read(*m_sock, boost::asio::buffer(&l, sizeof(l)), boost::asio::transfer_exactly(4), yield_context); auto hostl = htonl(l); std::string buf; buf.resize(hostl + 4); memcpy(&buf[0], &l, 4); hostl = boost::asio::async_read(*m_sock, boost::asio::buffer(&buf[4], hostl), boost::asio::transfer_exactly(hostl), yield_context); // 解码 boost::scoped_ptr<proto::server_hello> server_hello((proto::server_hello*)av_router::decode(buf)); m_remote_addr.reset(new proto::avAddress( av_address_from_string(server_hello->server_av_address()))); auto server_pubkey = BN_bin2bn((const unsigned char *) server_hello->random_pub_key().data(), server_hello->random_pub_key().length(), NULL); m_shared_key.resize(DH_size(dh)); // 密钥就算出来啦! DH_compute_key(&m_shared_key[0], server_pubkey, dh); BN_free(server_pubkey); std::printf("key = 0x"); for (int i=0; i<DH_size(dh); ++i) { std::printf("%x%x", (m_shared_key[i] >> 4) & 0xf, m_shared_key[i] & 0xf); } std::printf("\n"); DH_free(dh); // 接着私钥加密 随机数 auto singned = RSA_private_encrypt(_rsa.get(), server_hello->random_pub_key()); proto::login login_packet; login_packet.set_user_cert( i2d_X509(_x509.get()) ); login_packet.set_encryped_radom_key(singned); boost::asio::async_write(*m_sock, boost::asio::buffer(av_router::encode(login_packet)), yield_context); // 读取回应 boost::asio::async_read(*m_sock, boost::asio::buffer(&l, sizeof(l)), boost::asio::transfer_exactly(hostl), yield_context); hostl = htonl(l); buf.resize(htonl(l) + 4); memcpy(&buf[0], &l, 4); boost::asio::async_read(*m_sock, boost::asio::buffer(&buf[4], htonl(l)), boost::asio::transfer_exactly(hostl), yield_context); // 解码 boost::scoped_ptr<proto::login_result> login_result((proto::login_result*)av_router::decode(buf)); return login_result.get()->result() == proto::login_result_login_result_code_LOGIN_SUCCEED; }