support client complex handshake with server

pull/133/head
winlin 11 years ago
parent 841f0f8899
commit 491ea4b630

@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// current release version
#define VERSION_MAJOR "0"
#define VERSION_MINOR "9"
#define VERSION_REVISION "19"
#define VERSION_REVISION "20"
#define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
// server info.
#define RTMP_SIG_SRS_KEY "srs"

@ -657,6 +657,7 @@ int c2s2::s2_validate(c1s1* c1, bool& is_valid)
return ret;
}
// TODO: FIXME: move to the right position.
c1s1::c1s1()
{
schema = srs_schema_invalid;
@ -688,51 +689,7 @@ void c1s1::dump(char* _c1s1)
}
}
int c1s1::c1_create(srs_schema_type _schema)
{
int ret = ERROR_SUCCESS;
if (_schema == srs_schema_invalid) {
ret = ERROR_RTMP_CH_SCHEMA;
srs_error("create c1 failed. invalid schema=%d, ret=%d", _schema, ret);
return ret;
}
destroy_blocks();
time = ::time(NULL);
version = 0x02070080; // client c1 version
if (_schema == srs_schema0) {
srs_key_block_init(&block0.key);
srs_digest_block_init(&block1.digest);
} else {
srs_digest_block_init(&block0.digest);
srs_key_block_init(&block1.key);
}
schema = _schema;
char* digest = NULL;
if ((ret = calc_c1_digest(digest)) != ERROR_SUCCESS) {
srs_error("sign c1 error, failed to calc digest. ret=%d", ret);
return ret;
}
srs_assert(digest != NULL);
SrsAutoFree(char, digest, true);
if (schema == srs_schema0) {
memcpy(block1.digest.digest, digest, 32);
} else {
memcpy(block0.digest.digest, digest, 32);
}
return ret;
}
int c1s1::c1_parse(char* _c1s1, srs_schema_type _schema)
int c1s1::parse(char* _c1s1, srs_schema_type _schema)
{
int ret = ERROR_SUCCESS;
@ -778,6 +735,50 @@ int c1s1::c1_parse(char* _c1s1, srs_schema_type _schema)
return ret;
}
int c1s1::c1_create(srs_schema_type _schema)
{
int ret = ERROR_SUCCESS;
if (_schema == srs_schema_invalid) {
ret = ERROR_RTMP_CH_SCHEMA;
srs_error("create c1 failed. invalid schema=%d, ret=%d", _schema, ret);
return ret;
}
destroy_blocks();
time = ::time(NULL);
version = 0x02070080; // client c1 version
if (_schema == srs_schema0) {
srs_key_block_init(&block0.key);
srs_digest_block_init(&block1.digest);
} else {
srs_digest_block_init(&block0.digest);
srs_key_block_init(&block1.key);
}
schema = _schema;
char* digest = NULL;
if ((ret = calc_c1_digest(digest)) != ERROR_SUCCESS) {
srs_error("sign c1 error, failed to calc digest. ret=%d", ret);
return ret;
}
srs_assert(digest != NULL);
SrsAutoFree(char, digest, true);
if (schema == srs_schema0) {
memcpy(block1.digest.digest, digest, 32);
} else {
memcpy(block0.digest.digest, digest, 32);
}
return ret;
}
int c1s1::c1_validate_digest(bool& is_valid)
{
is_valid = false;
@ -1073,14 +1074,14 @@ int SrsComplexHandshake::handshake_with_client(SrsHandshakeBytes* hs_bytes, ISrs
// decode c1
c1s1 c1;
// try schema0.
if ((ret = c1.c1_parse(hs_bytes->c0c1 + 1, srs_schema0)) != ERROR_SUCCESS) {
if ((ret = c1.parse(hs_bytes->c0c1 + 1, srs_schema0)) != ERROR_SUCCESS) {
srs_error("parse c1 schema%d error. ret=%d", srs_schema0, ret);
return ret;
}
// try schema1
bool is_valid = false;
if ((ret = c1.c1_validate_digest(is_valid)) != ERROR_SUCCESS || !is_valid) {
if ((ret = c1.c1_parse(hs_bytes->c0c1 + 1, srs_schema1)) != ERROR_SUCCESS) {
if ((ret = c1.parse(hs_bytes->c0c1 + 1, srs_schema1)) != ERROR_SUCCESS) {
srs_error("parse c1 schema%d error. ret=%d", srs_schema1, ret);
return ret;
}
@ -1163,12 +1164,76 @@ int SrsComplexHandshake::handshake_with_server(SrsHandshakeBytes* /*hs_bytes*/,
return ERROR_RTMP_TRY_SIMPLE_HS;
}
#else
int SrsComplexHandshake::handshake_with_server(SrsHandshakeBytes* /*hs_bytes*/, ISrsProtocolReaderWriter* /*io*/)
int SrsComplexHandshake::handshake_with_server(SrsHandshakeBytes* hs_bytes, ISrsProtocolReaderWriter* io)
{
int ret = ERROR_SUCCESS;
// TODO: implements complex handshake.
ret = ERROR_RTMP_TRY_SIMPLE_HS;
ssize_t nsize;
// complex handshake
if ((ret = hs_bytes->create_c0c1()) != ERROR_SUCCESS) {
return ret;
}
// plain text required.
hs_bytes->c0c1[0] = 0x03;
// sign c1
c1s1 c1;
if ((ret = c1.c1_create(srs_schema0)) != ERROR_SUCCESS) {
return ret;
}
c1.dump(hs_bytes->c0c1 + 1);
// verify c1
bool is_valid;
if ((ret = c1.c1_validate_digest(is_valid)) != ERROR_SUCCESS || !is_valid) {
ret = ERROR_RTMP_TRY_SIMPLE_HS;
return ret;
}
if ((ret = io->write(hs_bytes->c0c1, 1537, &nsize)) != ERROR_SUCCESS) {
srs_warn("write c0c1 failed. ret=%d", ret);
return ret;
}
srs_verbose("write c0c1 success.");
// s0s1s2
if ((ret = hs_bytes->read_s0s1s2(io)) != ERROR_SUCCESS) {
return ret;
}
// plain text required.
if (hs_bytes->s0s1s2[0] != 0x03) {
ret = ERROR_RTMP_HANDSHAKE;
srs_warn("handshake failed, plain text required. ret=%d", ret);
return ret;
}
// verify s1s2
c1s1 s1;
if ((ret = s1.parse(hs_bytes->s0s1s2 + 1, c1.schema)) != ERROR_SUCCESS) {
return ret;
}
if ((ret = s1.s1_validate_digest(is_valid)) != ERROR_SUCCESS || !is_valid) {
ret = ERROR_RTMP_TRY_SIMPLE_HS;
return ret;
}
// c2
if ((ret = hs_bytes->create_c2()) != ERROR_SUCCESS) {
return ret;
}
c2s2 c2;
if ((ret = c2.c2_create(&s1)) != ERROR_SUCCESS) {
return ret;
}
c2.dump(hs_bytes->c2);
if ((ret = io->write(hs_bytes->c2, 1536, &nsize)) != ERROR_SUCCESS) {
srs_warn("complex handshake write c2 failed. ret=%d", ret);
return ret;
}
srs_verbose("complex handshake write c2 success.");
srs_trace("complex handshake with server success.");
return ret;
}

@ -84,7 +84,7 @@ namespace srs
};
/**
* 764bytes key
* 764bytes key structure
* random-data: (offset)bytes
* key-data: 128bytes
* random-data: (764-offset-128-4)bytes
@ -109,7 +109,7 @@ namespace srs
};
/**
* 764bytes digest
* 764bytes digest structure
* offset: 4bytes
* random-data: (offset)bytes
* digest-data: 32bytes
@ -179,6 +179,11 @@ namespace srs
* copy to bytes.
*/
virtual void dump(char* _c1s1);
/**
* server: parse the c1s1, discovery the key and digest by schema.
* use the c1_validate_digest() to valid the digest of c1.
*/
virtual int parse(char* _c1s1, srs_schema_type _schema);
/**
* client: create and sign c1 by schema.
@ -197,11 +202,6 @@ namespace srs
*/
virtual int c1_create(srs_schema_type _schema);
/**
* server: parse the c1s1, discovery the key and digest by schema.
* use the c1_validate_digest() to valid the digest of c1.
*/
virtual int c1_parse(char* _c1s1, srs_schema_type _schema);
/**
* server: validate the parsed c1 schema
*/
virtual int c1_validate_digest(bool& is_valid);

@ -35,7 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_kernel_log.hpp>
#include <srs_kernel_error.hpp>
// the following is the timeout for rtmp protocol,
// to avoid death connection.

File diff suppressed because one or more lines are too long

@ -29,7 +29,39 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <srs_utest.hpp>
#include <srs_protocol_io.hpp>
#include <srs_protocol_rtmp.hpp>
#include <srs_protocol_handshake.hpp>
using namespace srs;
class MockEmptyIO : public ISrsProtocolReaderWriter
{
public:
MockEmptyIO();
virtual ~MockEmptyIO();
// for protocol
public:
virtual bool is_never_timeout(int64_t timeout_us);
// for handshake.
public:
virtual int read_fully(const void* buf, size_t size, ssize_t* nread);
virtual int write(const void* buf, size_t size, ssize_t* nwrite);
// for protocol
public:
virtual void set_recv_timeout(int64_t timeout_us);
virtual int64_t get_recv_timeout();
virtual int64_t get_recv_bytes();
virtual int get_recv_kbps();
// for protocol
public:
virtual void set_send_timeout(int64_t timeout_us);
virtual int64_t get_send_timeout();
virtual int64_t get_send_bytes();
virtual int get_send_kbps();
virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite);
// for protocol/amf0/msg-codec
public:
virtual int read(const void* buf, size_t size, ssize_t* nread);
};
#endif

Loading…
Cancel
Save