winlin

fix #235, refine handshake, replace union with template method. 2.0.38.

@@ -485,7 +485,8 @@ Supported operating systems and hardware: @@ -485,7 +485,8 @@ Supported operating systems and hardware:
485 * 2013-10-17, Created.<br/> 485 * 2013-10-17, Created.<br/>
486 486
487 ## History 487 ## History
488 -* v2.0, 2014-11-28, fix [#215](https://github.com/winlinvip/simple-rtmp-server/issues/215), for bug #215, add srs_rtmp_dump tool. 2.0.37. 488 +* v2.0, 2014-11-29, fix [#235](https://github.com/winlinvip/simple-rtmp-server/issues/235), refine handshake, replace union with template method. 2.0.38.
  489 +* v2.0, 2014-11-28, fix [#215](https://github.com/winlinvip/simple-rtmp-server/issues/215), add srs_rtmp_dump tool. 2.0.37.
489 * v2.0, 2014-11-25, update PRIMARY, AUTHORS, CONTRIBUTORS rule. 2.0.32. 490 * v2.0, 2014-11-25, update PRIMARY, AUTHORS, CONTRIBUTORS rule. 2.0.32.
490 * v2.0, 2014-11-24, fix [#212](https://github.com/winlinvip/simple-rtmp-server/issues/212), support publish aac adts raw stream. 2.0.31. 491 * v2.0, 2014-11-24, fix [#212](https://github.com/winlinvip/simple-rtmp-server/issues/212), support publish aac adts raw stream. 2.0.31.
491 * v2.0, 2014-11-22, fix [#217](https://github.com/winlinvip/simple-rtmp-server/issues/217), remove timeout recv, support 7.5k+ 250kbps clients. 2.0.30. 492 * v2.0, 2014-11-22, fix [#217](https://github.com/winlinvip/simple-rtmp-server/issues/217), remove timeout recv, support 7.5k+ 250kbps clients. 2.0.30.
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR 2 32 #define VERSION_MAJOR 2
33 #define VERSION_MINOR 0 33 #define VERSION_MINOR 0
34 -#define VERSION_REVISION 37 34 +#define VERSION_REVISION 38
35 // server info. 35 // server info.
36 #define RTMP_SIG_SRS_KEY "SRS" 36 #define RTMP_SIG_SRS_KEY "SRS"
37 #define RTMP_SIG_SRS_ROLE "origin/edge server" 37 #define RTMP_SIG_SRS_ROLE "origin/edge server"
@@ -514,7 +514,7 @@ namespace _srs_internal @@ -514,7 +514,7 @@ namespace _srs_internal
514 return ret; 514 return ret;
515 } 515 }
516 516
517 - int c1s1_strategy::s1_create(c1s1* owner) 517 + int c1s1_strategy::s1_create(c1s1* owner, c1s1* c1)
518 { 518 {
519 int ret = ERROR_SUCCESS; 519 int ret = ERROR_SUCCESS;
520 520
@@ -528,8 +528,7 @@ namespace _srs_internal @@ -528,8 +528,7 @@ namespace _srs_internal
528 // directly generate the public key. 528 // directly generate the public key.
529 // @see: https://github.com/winlinvip/simple-rtmp-server/issues/148 529 // @see: https://github.com/winlinvip/simple-rtmp-server/issues/148
530 int pkey_size = 128; 530 int pkey_size = 128;
531 - // TODO: FIXME: use c1 public key to calc the shared key.  
532 - if ((ret = dh.copy_public_key(key.key, pkey_size)) != ERROR_SUCCESS) { 531 + if ((ret = dh.copy_shared_key(c1->get_key(), 128, key.key, pkey_size)) != ERROR_SUCCESS) {
533 srs_error("calc s1 key failed. ret=%d", ret); 532 srs_error("calc s1 key failed. ret=%d", ret);
534 return ret; 533 return ret;
535 } 534 }
@@ -827,7 +826,6 @@ namespace _srs_internal @@ -827,7 +826,6 @@ namespace _srs_internal
827 return ret; 826 return ret;
828 } 827 }
829 828
830 - // TODO: FIXME: move to the right position.  
831 c1s1::c1s1() 829 c1s1::c1s1()
832 { 830 {
833 payload = NULL; 831 payload = NULL;
@@ -835,21 +833,6 @@ namespace _srs_internal @@ -835,21 +833,6 @@ namespace _srs_internal
835 c1s1::~c1s1() 833 c1s1::~c1s1()
836 { 834 {
837 srs_freep(payload); 835 srs_freep(payload);
838 - /*  
839 - void c1s1::destroy_blocks()  
840 - {  
841 - if (schema == srs_schema_invalid) {  
842 - return;  
843 - }  
844 -  
845 - if (schema == srs_schema0) {  
846 - block0.key.free();  
847 - block1.digest.free();  
848 - } else {  
849 - block0.digest.free();  
850 - block1.key.free();  
851 - }  
852 - }*/  
853 } 836 }
854 837
855 srs_schema_type c1s1::schema() 838 srs_schema_type c1s1::schema()
@@ -960,7 +943,7 @@ namespace _srs_internal @@ -960,7 +943,7 @@ namespace _srs_internal
960 payload = new c1s1_strategy_schema1(); 943 payload = new c1s1_strategy_schema1();
961 } 944 }
962 945
963 - return payload->s1_create(this); 946 + return payload->s1_create(this, c1);
964 } 947 }
965 948
966 int c1s1::s1_validate_digest(bool& is_valid) 949 int c1s1::s1_validate_digest(bool& is_valid)
@@ -245,8 +245,32 @@ namespace _srs_internal @@ -245,8 +245,32 @@ namespace _srs_internal
245 virtual int c1_validate_digest(c1s1* owner, bool& is_valid); 245 virtual int c1_validate_digest(c1s1* owner, bool& is_valid);
246 /** 246 /**
247 * server: create and sign the s1 from c1. 247 * server: create and sign the s1 from c1.
  248 + * // decode c1 try schema0 then schema1
  249 + * c1-digest-data = get-c1-digest-data(schema0)
  250 + * if c1-digest-data equals to calc_c1_digest(c1, schema0) {
  251 + * c1-key-data = get-c1-key-data(schema0)
  252 + * schema = schema0
  253 + * } else {
  254 + * c1-digest-data = get-c1-digest-data(schema1)
  255 + * if c1-digest-data not equals to calc_c1_digest(c1, schema1) {
  256 + * switch to simple handshake.
  257 + * return
  258 + * }
  259 + * c1-key-data = get-c1-key-data(schema1)
  260 + * schema = schema1
  261 + * }
  262 + *
  263 + * // generate s1
  264 + * random fill 1536bytes s1
  265 + * time = time() // c1[0-3]
  266 + * version = [0x04, 0x05, 0x00, 0x01] // s1[4-7]
  267 + * s1-key-data=shared_key=DH_compute_key(peer_pub_key=c1-key-data)
  268 + * get c1s1-joined by specified schema
  269 + * s1-digest-data = HMACsha256(c1s1-joined, FMSKey, 36)
  270 + * copy s1-digest-data and s1-key-data to s1.
  271 + * @param c1, to get the peer_pub_key of client.
248 */ 272 */
249 - virtual int s1_create(c1s1* owner); 273 + virtual int s1_create(c1s1* owner, c1s1* c1);
250 /** 274 /**
251 * server: validate the parsed s1 schema 275 * server: validate the parsed s1 schema
252 */ 276 */