winlin

for bug #235, move functions of block and digest to struct.

... ... @@ -312,31 +312,17 @@ namespace _srs_internal
return stream.read_4bytes();
}
// calc the offset of key,
// the key->offset cannot be used as the offset of key.
int srs_key_block_get_offset(key_block* key)
{
int max_offset_size = 764 - 128 - 4;
int offset = 0;
u_int8_t* pp = (u_int8_t*)&key->offset;
offset += *pp++;
offset += *pp++;
offset += *pp++;
offset += *pp++;
return offset % max_offset_size;
}
// create new key block data.
// if created, user must free it by srs_key_block_free
void srs_key_block_init(key_block* key)
void key_block::init()
{
key_block* key = this;
key->offset = (int32_t)rand();
key->random0 = NULL;
key->random1 = NULL;
int offset = srs_key_block_get_offset(key);
int offset = key->offsets();
srs_assert(offset >= 0);
key->random0_size = offset;
... ... @@ -356,11 +342,31 @@ namespace _srs_internal
}
}
// calc the offset of key,
// the key->offset cannot be used as the offset of key.
int key_block::offsets()
{
key_block* key = this;
int max_offset_size = 764 - 128 - 4;
int offset = 0;
u_int8_t* pp = (u_int8_t*)&key->offset;
offset += *pp++;
offset += *pp++;
offset += *pp++;
offset += *pp++;
return offset % max_offset_size;
}
// parse key block from c1s1.
// if created, user must free it by srs_key_block_free
// @c1s1_key_bytes the key start bytes, maybe c1s1 or c1s1+764
int srs_key_block_parse(key_block* key, char* c1s1_key_bytes)
int key_block::parse(char* c1s1_key_bytes)
{
key_block* key = this;
int ret = ERROR_SUCCESS;
char* pp = c1s1_key_bytes + 764;
... ... @@ -371,7 +377,7 @@ namespace _srs_internal
key->random0 = NULL;
key->random1 = NULL;
int offset = srs_key_block_get_offset(key);
int offset = key->offsets();
srs_assert(offset >= 0);
pp = c1s1_key_bytes;
... ... @@ -396,8 +402,10 @@ namespace _srs_internal
// free the block data create by
// srs_key_block_init or srs_key_block_parse
void srs_key_block_free(key_block* key)
void key_block::free()
{
key_block* key = this;
if (key->random0) {
srs_freep(key->random0);
}
... ... @@ -406,31 +414,17 @@ namespace _srs_internal
}
}
// calc the offset of digest,
// the key->offset cannot be used as the offset of digest.
int srs_digest_block_get_offset(digest_block* digest)
{
int max_offset_size = 764 - 32 - 4;
int offset = 0;
u_int8_t* pp = (u_int8_t*)&digest->offset;
offset += *pp++;
offset += *pp++;
offset += *pp++;
offset += *pp++;
return offset % max_offset_size;
}
// create new digest block data.
// if created, user must free it by srs_digest_block_free
void srs_digest_block_init(digest_block* digest)
void digest_block::init()
{
digest_block* digest = this;
digest->offset = (int32_t)rand();
digest->random0 = NULL;
digest->random1 = NULL;
int offset = srs_digest_block_get_offset(digest);
int offset = digest->offsets();
srs_assert(offset >= 0);
digest->random0_size = offset;
... ... @@ -450,11 +444,31 @@ namespace _srs_internal
}
}
// calc the offset of digest,
// the key->offset cannot be used as the offset of digest.
int digest_block::offsets()
{
digest_block* digest = this;
int max_offset_size = 764 - 32 - 4;
int offset = 0;
u_int8_t* pp = (u_int8_t*)&digest->offset;
offset += *pp++;
offset += *pp++;
offset += *pp++;
offset += *pp++;
return offset % max_offset_size;
}
// parse digest block from c1s1.
// if created, user must free it by srs_digest_block_free
// @c1s1_digest_bytes the digest start bytes, maybe c1s1 or c1s1+764
int srs_digest_block_parse(digest_block* digest, char* c1s1_digest_bytes)
int digest_block::parse(char* c1s1_digest_bytes)
{
digest_block* digest = this;
int ret = ERROR_SUCCESS;
char* pp = c1s1_digest_bytes;
... ... @@ -465,7 +479,7 @@ namespace _srs_internal
digest->random0 = NULL;
digest->random1 = NULL;
int offset = srs_digest_block_get_offset(digest);
int offset = digest->offsets();
srs_assert(offset >= 0);
digest->random0_size = offset;
... ... @@ -489,8 +503,10 @@ namespace _srs_internal
// free the block data create by
// srs_digest_block_init or srs_digest_block_parse
void srs_digest_block_free(digest_block* digest)
void digest_block::free()
{
digest_block* digest = this;
if (digest->random0) {
srs_freep(digest->random0);
}
... ... @@ -792,21 +808,21 @@ namespace _srs_internal
version = __srs_stream_read_4bytes(_c1s1 + 4); // client c1 version
if (_schema == srs_schema0) {
if ((ret = srs_key_block_parse(&block0.key, _c1s1 + 8)) != ERROR_SUCCESS) {
if ((ret = block0.key.parse(_c1s1 + 8)) != ERROR_SUCCESS) {
srs_error("parse the c1 key failed. ret=%d", ret);
return ret;
}
if ((ret = srs_digest_block_parse(&block1.digest, _c1s1 + 8 + 764)) != ERROR_SUCCESS) {
if ((ret = block1.digest.parse(_c1s1 + 8 + 764)) != ERROR_SUCCESS) {
srs_error("parse the c1 digest failed. ret=%d", ret);
return ret;
}
srs_verbose("parse c1 key-digest success");
} else if (_schema == srs_schema1) {
if ((ret = srs_digest_block_parse(&block0.digest, _c1s1 + 8)) != ERROR_SUCCESS) {
if ((ret = block0.digest.parse(_c1s1 + 8)) != ERROR_SUCCESS) {
srs_error("parse the c1 key failed. ret=%d", ret);
return ret;
}
if ((ret = srs_key_block_parse(&block1.key, _c1s1 + 8 + 764)) != ERROR_SUCCESS) {
if ((ret = block1.key.parse(_c1s1 + 8 + 764)) != ERROR_SUCCESS) {
srs_error("parse the c1 digest failed. ret=%d", ret);
return ret;
}
... ... @@ -840,11 +856,11 @@ namespace _srs_internal
// generate signature by schema
if (_schema == srs_schema0) {
srs_key_block_init(&block0.key);
srs_digest_block_init(&block1.digest);
block0.key.init();
block1.digest.init();
} else {
srs_digest_block_init(&block0.digest);
srs_key_block_init(&block1.key);
block0.digest.init();
block1.key.init();
}
schema = _schema;
... ... @@ -941,8 +957,8 @@ namespace _srs_internal
}
if (schema == srs_schema0) {
srs_key_block_init(&block0.key);
srs_digest_block_init(&block1.digest);
block0.key.init();
block1.digest.init();
// directly generate the public key.
// @see: https://github.com/winlinvip/simple-rtmp-server/issues/148
... ... @@ -953,8 +969,8 @@ namespace _srs_internal
}
srs_assert(pkey_size == 128);
} else {
srs_digest_block_init(&block0.digest);
srs_key_block_init(&block1.key);
block0.digest.init();
block1.key.init();
// directly generate the public key.
// @see: https://github.com/winlinvip/simple-rtmp-server/issues/148
... ... @@ -1048,11 +1064,11 @@ namespace _srs_internal
}
if (schema == srs_schema0) {
srs_key_block_free(&block0.key);
srs_digest_block_free(&block1.digest);
block0.key.free();
block1.digest.free();
} else {
srs_digest_block_free(&block0.digest);
srs_key_block_free(&block1.key);
block0.digest.free();
block1.key.free();
}
}
}
... ...
... ... @@ -41,6 +41,52 @@ class SrsHandshakeBytes;
namespace _srs_internal
{
// the digest key generate size.
#define __SRS_OpensslHashSize 512
extern u_int8_t SrsGenuineFMSKey[];
extern u_int8_t SrsGenuineFPKey[];
int openssl_HMACsha256(const void* key, int key_size, const void* data, int data_size, void* digest);
int openssl_generate_key(char* public_key, int32_t size);
/**
* the DH wrapper.
*/
class SrsDH
{
private:
DH* pdh;
public:
SrsDH();
virtual ~SrsDH();
public:
/**
* initialize dh, generate the public and private key.
* @param ensure_128bytes_public_key whether ensure public key is 128bytes,
* sometimes openssl generate 127bytes public key.
* default to false to donot ensure.
*/
virtual int initialize(bool ensure_128bytes_public_key = false);
/**
* copy the public key.
* @param pkey the bytes to copy the public key.
* @param pkey_size the max public key size, output the actual public key size.
* user should never ignore this size.
* @remark, when ensure_128bytes_public_key, the size always 128.
*/
virtual int copy_public_key(char* pkey, int32_t& pkey_size);
/**
* generate and copy the shared key.
* generate the shared key with peer public key.
* @param ppkey peer public key.
* @param ppkey_size the size of ppkey.
* @param skey the computed shared key.
* @param skey_size the max shared key size, output the actual shared key size.
* user should never ignore this size.
*/
virtual int copy_shared_key(const char* ppkey, int32_t ppkey_size, char* skey, int32_t& skey_size);
private:
virtual int do_initialize();
};
/**
* the schema type.
*/
... ... @@ -85,6 +131,23 @@ namespace _srs_internal
// 4bytes
int32_t offset;
public:
// create new key block data.
// if created, user must free it by srs_key_block_free
void init();
// calc the offset of key,
// the key->offset cannot be used as the offset of key.
int offsets();
// parse key block from c1s1.
// if created, user must free it by srs_key_block_free
// @c1s1_key_bytes the key start bytes, maybe c1s1 or c1s1+764
int parse(char* c1s1_key_bytes);
// free the block data create by
// srs_key_block_init or srs_key_block_parse
void free();
};
/**
... ... @@ -111,88 +174,24 @@ namespace _srs_internal
// (764-4-offset-32)bytes
char* random1;
int random1_size;
};
// the digest key generate size.
#define __SRS_OpensslHashSize 512
extern u_int8_t SrsGenuineFMSKey[];
extern u_int8_t SrsGenuineFPKey[];
int openssl_HMACsha256(const void* key, int key_size, const void* data, int data_size, void* digest);
int openssl_generate_key(char* public_key, int32_t size);
/**
* the DH wrapper.
*/
class SrsDH
{
private:
DH* pdh;
public:
SrsDH();
virtual ~SrsDH();
public:
/**
* initialize dh, generate the public and private key.
* @param ensure_128bytes_public_key whether ensure public key is 128bytes,
* sometimes openssl generate 127bytes public key.
* default to false to donot ensure.
*/
virtual int initialize(bool ensure_128bytes_public_key = false);
/**
* copy the public key.
* @param pkey the bytes to copy the public key.
* @param pkey_size the max public key size, output the actual public key size.
* user should never ignore this size.
* @remark, when ensure_128bytes_public_key, the size always 128.
*/
virtual int copy_public_key(char* pkey, int32_t& pkey_size);
/**
* generate and copy the shared key.
* generate the shared key with peer public key.
* @param ppkey peer public key.
* @param ppkey_size the size of ppkey.
* @param skey the computed shared key.
* @param skey_size the max shared key size, output the actual shared key size.
* user should never ignore this size.
*/
virtual int copy_shared_key(const char* ppkey, int32_t ppkey_size, char* skey, int32_t& skey_size);
private:
virtual int do_initialize();
};
// calc the offset of key,
// the key->offset cannot be used as the offset of key.
int srs_key_block_get_offset(key_block* key);
// create new key block data.
// if created, user must free it by srs_key_block_free
void srs_key_block_init(key_block* key);
// parse key block from c1s1.
// if created, user must free it by srs_key_block_free
// @c1s1_key_bytes the key start bytes, maybe c1s1 or c1s1+764
int srs_key_block_parse(key_block* key, char* c1s1_key_bytes);
// free the block data create by
// srs_key_block_init or srs_key_block_parse
void srs_key_block_free(key_block* key);
// create new digest block data.
// if created, user must free it by srs_digest_block_free
void init();
// calc the offset of digest,
// the key->offset cannot be used as the offset of digest.
int srs_digest_block_get_offset(digest_block* digest);
// create new digest block data.
// if created, user must free it by srs_digest_block_free
void srs_digest_block_init(digest_block* digest);
int offsets();
// parse digest block from c1s1.
// if created, user must free it by srs_digest_block_free
// @c1s1_digest_bytes the digest start bytes, maybe c1s1 or c1s1+764
int srs_digest_block_parse(digest_block* digest, char* c1s1_digest_bytes);
int parse(char* c1s1_digest_bytes);
// free the block data create by
// srs_digest_block_init or srs_digest_block_parse
void srs_digest_block_free(digest_block* digest);
void free();
};
/**
* copy whole c1s1 to bytes.
... ...