winlin

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

@@ -312,31 +312,17 @@ namespace _srs_internal @@ -312,31 +312,17 @@ namespace _srs_internal
312 return stream.read_4bytes(); 312 return stream.read_4bytes();
313 } 313 }
314 314
315 - // calc the offset of key,  
316 - // the key->offset cannot be used as the offset of key.  
317 - int srs_key_block_get_offset(key_block* key)  
318 - {  
319 - int max_offset_size = 764 - 128 - 4;  
320 -  
321 - int offset = 0;  
322 - u_int8_t* pp = (u_int8_t*)&key->offset;  
323 - offset += *pp++;  
324 - offset += *pp++;  
325 - offset += *pp++;  
326 - offset += *pp++;  
327 -  
328 - return offset % max_offset_size;  
329 - }  
330 -  
331 // create new key block data. 315 // create new key block data.
332 // if created, user must free it by srs_key_block_free 316 // if created, user must free it by srs_key_block_free
333 - void srs_key_block_init(key_block* key) 317 + void key_block::init()
334 { 318 {
  319 + key_block* key = this;
  320 +
335 key->offset = (int32_t)rand(); 321 key->offset = (int32_t)rand();
336 key->random0 = NULL; 322 key->random0 = NULL;
337 key->random1 = NULL; 323 key->random1 = NULL;
338 324
339 - int offset = srs_key_block_get_offset(key); 325 + int offset = key->offsets();
340 srs_assert(offset >= 0); 326 srs_assert(offset >= 0);
341 327
342 key->random0_size = offset; 328 key->random0_size = offset;
@@ -356,11 +342,31 @@ namespace _srs_internal @@ -356,11 +342,31 @@ namespace _srs_internal
356 } 342 }
357 } 343 }
358 344
  345 + // calc the offset of key,
  346 + // the key->offset cannot be used as the offset of key.
  347 + int key_block::offsets()
  348 + {
  349 + key_block* key = this;
  350 +
  351 + int max_offset_size = 764 - 128 - 4;
  352 +
  353 + int offset = 0;
  354 + u_int8_t* pp = (u_int8_t*)&key->offset;
  355 + offset += *pp++;
  356 + offset += *pp++;
  357 + offset += *pp++;
  358 + offset += *pp++;
  359 +
  360 + return offset % max_offset_size;
  361 + }
  362 +
359 // parse key block from c1s1. 363 // parse key block from c1s1.
360 // if created, user must free it by srs_key_block_free 364 // if created, user must free it by srs_key_block_free
361 // @c1s1_key_bytes the key start bytes, maybe c1s1 or c1s1+764 365 // @c1s1_key_bytes the key start bytes, maybe c1s1 or c1s1+764
362 - int srs_key_block_parse(key_block* key, char* c1s1_key_bytes) 366 + int key_block::parse(char* c1s1_key_bytes)
363 { 367 {
  368 + key_block* key = this;
  369 +
364 int ret = ERROR_SUCCESS; 370 int ret = ERROR_SUCCESS;
365 371
366 char* pp = c1s1_key_bytes + 764; 372 char* pp = c1s1_key_bytes + 764;
@@ -371,7 +377,7 @@ namespace _srs_internal @@ -371,7 +377,7 @@ namespace _srs_internal
371 key->random0 = NULL; 377 key->random0 = NULL;
372 key->random1 = NULL; 378 key->random1 = NULL;
373 379
374 - int offset = srs_key_block_get_offset(key); 380 + int offset = key->offsets();
375 srs_assert(offset >= 0); 381 srs_assert(offset >= 0);
376 382
377 pp = c1s1_key_bytes; 383 pp = c1s1_key_bytes;
@@ -396,8 +402,10 @@ namespace _srs_internal @@ -396,8 +402,10 @@ namespace _srs_internal
396 402
397 // free the block data create by 403 // free the block data create by
398 // srs_key_block_init or srs_key_block_parse 404 // srs_key_block_init or srs_key_block_parse
399 - void srs_key_block_free(key_block* key) 405 + void key_block::free()
400 { 406 {
  407 + key_block* key = this;
  408 +
401 if (key->random0) { 409 if (key->random0) {
402 srs_freep(key->random0); 410 srs_freep(key->random0);
403 } 411 }
@@ -406,31 +414,17 @@ namespace _srs_internal @@ -406,31 +414,17 @@ namespace _srs_internal
406 } 414 }
407 } 415 }
408 416
409 - // calc the offset of digest,  
410 - // the key->offset cannot be used as the offset of digest.  
411 - int srs_digest_block_get_offset(digest_block* digest)  
412 - {  
413 - int max_offset_size = 764 - 32 - 4;  
414 -  
415 - int offset = 0;  
416 - u_int8_t* pp = (u_int8_t*)&digest->offset;  
417 - offset += *pp++;  
418 - offset += *pp++;  
419 - offset += *pp++;  
420 - offset += *pp++;  
421 -  
422 - return offset % max_offset_size;  
423 - }  
424 -  
425 // create new digest block data. 417 // create new digest block data.
426 // if created, user must free it by srs_digest_block_free 418 // if created, user must free it by srs_digest_block_free
427 - void srs_digest_block_init(digest_block* digest) 419 + void digest_block::init()
428 { 420 {
  421 + digest_block* digest = this;
  422 +
429 digest->offset = (int32_t)rand(); 423 digest->offset = (int32_t)rand();
430 digest->random0 = NULL; 424 digest->random0 = NULL;
431 digest->random1 = NULL; 425 digest->random1 = NULL;
432 426
433 - int offset = srs_digest_block_get_offset(digest); 427 + int offset = digest->offsets();
434 srs_assert(offset >= 0); 428 srs_assert(offset >= 0);
435 429
436 digest->random0_size = offset; 430 digest->random0_size = offset;
@@ -450,11 +444,31 @@ namespace _srs_internal @@ -450,11 +444,31 @@ namespace _srs_internal
450 } 444 }
451 } 445 }
452 446
  447 + // calc the offset of digest,
  448 + // the key->offset cannot be used as the offset of digest.
  449 + int digest_block::offsets()
  450 + {
  451 + digest_block* digest = this;
  452 +
  453 + int max_offset_size = 764 - 32 - 4;
  454 +
  455 + int offset = 0;
  456 + u_int8_t* pp = (u_int8_t*)&digest->offset;
  457 + offset += *pp++;
  458 + offset += *pp++;
  459 + offset += *pp++;
  460 + offset += *pp++;
  461 +
  462 + return offset % max_offset_size;
  463 + }
  464 +
453 // parse digest block from c1s1. 465 // parse digest block from c1s1.
454 // if created, user must free it by srs_digest_block_free 466 // if created, user must free it by srs_digest_block_free
455 // @c1s1_digest_bytes the digest start bytes, maybe c1s1 or c1s1+764 467 // @c1s1_digest_bytes the digest start bytes, maybe c1s1 or c1s1+764
456 - int srs_digest_block_parse(digest_block* digest, char* c1s1_digest_bytes) 468 + int digest_block::parse(char* c1s1_digest_bytes)
457 { 469 {
  470 + digest_block* digest = this;
  471 +
458 int ret = ERROR_SUCCESS; 472 int ret = ERROR_SUCCESS;
459 473
460 char* pp = c1s1_digest_bytes; 474 char* pp = c1s1_digest_bytes;
@@ -465,7 +479,7 @@ namespace _srs_internal @@ -465,7 +479,7 @@ namespace _srs_internal
465 digest->random0 = NULL; 479 digest->random0 = NULL;
466 digest->random1 = NULL; 480 digest->random1 = NULL;
467 481
468 - int offset = srs_digest_block_get_offset(digest); 482 + int offset = digest->offsets();
469 srs_assert(offset >= 0); 483 srs_assert(offset >= 0);
470 484
471 digest->random0_size = offset; 485 digest->random0_size = offset;
@@ -489,8 +503,10 @@ namespace _srs_internal @@ -489,8 +503,10 @@ namespace _srs_internal
489 503
490 // free the block data create by 504 // free the block data create by
491 // srs_digest_block_init or srs_digest_block_parse 505 // srs_digest_block_init or srs_digest_block_parse
492 - void srs_digest_block_free(digest_block* digest) 506 + void digest_block::free()
493 { 507 {
  508 + digest_block* digest = this;
  509 +
494 if (digest->random0) { 510 if (digest->random0) {
495 srs_freep(digest->random0); 511 srs_freep(digest->random0);
496 } 512 }
@@ -792,21 +808,21 @@ namespace _srs_internal @@ -792,21 +808,21 @@ namespace _srs_internal
792 version = __srs_stream_read_4bytes(_c1s1 + 4); // client c1 version 808 version = __srs_stream_read_4bytes(_c1s1 + 4); // client c1 version
793 809
794 if (_schema == srs_schema0) { 810 if (_schema == srs_schema0) {
795 - if ((ret = srs_key_block_parse(&block0.key, _c1s1 + 8)) != ERROR_SUCCESS) { 811 + if ((ret = block0.key.parse(_c1s1 + 8)) != ERROR_SUCCESS) {
796 srs_error("parse the c1 key failed. ret=%d", ret); 812 srs_error("parse the c1 key failed. ret=%d", ret);
797 return ret; 813 return ret;
798 } 814 }
799 - if ((ret = srs_digest_block_parse(&block1.digest, _c1s1 + 8 + 764)) != ERROR_SUCCESS) { 815 + if ((ret = block1.digest.parse(_c1s1 + 8 + 764)) != ERROR_SUCCESS) {
800 srs_error("parse the c1 digest failed. ret=%d", ret); 816 srs_error("parse the c1 digest failed. ret=%d", ret);
801 return ret; 817 return ret;
802 } 818 }
803 srs_verbose("parse c1 key-digest success"); 819 srs_verbose("parse c1 key-digest success");
804 } else if (_schema == srs_schema1) { 820 } else if (_schema == srs_schema1) {
805 - if ((ret = srs_digest_block_parse(&block0.digest, _c1s1 + 8)) != ERROR_SUCCESS) { 821 + if ((ret = block0.digest.parse(_c1s1 + 8)) != ERROR_SUCCESS) {
806 srs_error("parse the c1 key failed. ret=%d", ret); 822 srs_error("parse the c1 key failed. ret=%d", ret);
807 return ret; 823 return ret;
808 } 824 }
809 - if ((ret = srs_key_block_parse(&block1.key, _c1s1 + 8 + 764)) != ERROR_SUCCESS) { 825 + if ((ret = block1.key.parse(_c1s1 + 8 + 764)) != ERROR_SUCCESS) {
810 srs_error("parse the c1 digest failed. ret=%d", ret); 826 srs_error("parse the c1 digest failed. ret=%d", ret);
811 return ret; 827 return ret;
812 } 828 }
@@ -840,11 +856,11 @@ namespace _srs_internal @@ -840,11 +856,11 @@ namespace _srs_internal
840 856
841 // generate signature by schema 857 // generate signature by schema
842 if (_schema == srs_schema0) { 858 if (_schema == srs_schema0) {
843 - srs_key_block_init(&block0.key);  
844 - srs_digest_block_init(&block1.digest); 859 + block0.key.init();
  860 + block1.digest.init();
845 } else { 861 } else {
846 - srs_digest_block_init(&block0.digest);  
847 - srs_key_block_init(&block1.key); 862 + block0.digest.init();
  863 + block1.key.init();
848 } 864 }
849 865
850 schema = _schema; 866 schema = _schema;
@@ -941,8 +957,8 @@ namespace _srs_internal @@ -941,8 +957,8 @@ namespace _srs_internal
941 } 957 }
942 958
943 if (schema == srs_schema0) { 959 if (schema == srs_schema0) {
944 - srs_key_block_init(&block0.key);  
945 - srs_digest_block_init(&block1.digest); 960 + block0.key.init();
  961 + block1.digest.init();
946 962
947 // directly generate the public key. 963 // directly generate the public key.
948 // @see: https://github.com/winlinvip/simple-rtmp-server/issues/148 964 // @see: https://github.com/winlinvip/simple-rtmp-server/issues/148
@@ -953,8 +969,8 @@ namespace _srs_internal @@ -953,8 +969,8 @@ namespace _srs_internal
953 } 969 }
954 srs_assert(pkey_size == 128); 970 srs_assert(pkey_size == 128);
955 } else { 971 } else {
956 - srs_digest_block_init(&block0.digest);  
957 - srs_key_block_init(&block1.key); 972 + block0.digest.init();
  973 + block1.key.init();
958 974
959 // directly generate the public key. 975 // directly generate the public key.
960 // @see: https://github.com/winlinvip/simple-rtmp-server/issues/148 976 // @see: https://github.com/winlinvip/simple-rtmp-server/issues/148
@@ -1048,11 +1064,11 @@ namespace _srs_internal @@ -1048,11 +1064,11 @@ namespace _srs_internal
1048 } 1064 }
1049 1065
1050 if (schema == srs_schema0) { 1066 if (schema == srs_schema0) {
1051 - srs_key_block_free(&block0.key);  
1052 - srs_digest_block_free(&block1.digest); 1067 + block0.key.free();
  1068 + block1.digest.free();
1053 } else { 1069 } else {
1054 - srs_digest_block_free(&block0.digest);  
1055 - srs_key_block_free(&block1.key); 1070 + block0.digest.free();
  1071 + block1.key.free();
1056 } 1072 }
1057 } 1073 }
1058 } 1074 }
@@ -41,6 +41,52 @@ class SrsHandshakeBytes; @@ -41,6 +41,52 @@ class SrsHandshakeBytes;
41 41
42 namespace _srs_internal 42 namespace _srs_internal
43 { 43 {
  44 + // the digest key generate size.
  45 + #define __SRS_OpensslHashSize 512
  46 + extern u_int8_t SrsGenuineFMSKey[];
  47 + extern u_int8_t SrsGenuineFPKey[];
  48 + int openssl_HMACsha256(const void* key, int key_size, const void* data, int data_size, void* digest);
  49 + int openssl_generate_key(char* public_key, int32_t size);
  50 +
  51 + /**
  52 + * the DH wrapper.
  53 + */
  54 + class SrsDH
  55 + {
  56 + private:
  57 + DH* pdh;
  58 + public:
  59 + SrsDH();
  60 + virtual ~SrsDH();
  61 + public:
  62 + /**
  63 + * initialize dh, generate the public and private key.
  64 + * @param ensure_128bytes_public_key whether ensure public key is 128bytes,
  65 + * sometimes openssl generate 127bytes public key.
  66 + * default to false to donot ensure.
  67 + */
  68 + virtual int initialize(bool ensure_128bytes_public_key = false);
  69 + /**
  70 + * copy the public key.
  71 + * @param pkey the bytes to copy the public key.
  72 + * @param pkey_size the max public key size, output the actual public key size.
  73 + * user should never ignore this size.
  74 + * @remark, when ensure_128bytes_public_key, the size always 128.
  75 + */
  76 + virtual int copy_public_key(char* pkey, int32_t& pkey_size);
  77 + /**
  78 + * generate and copy the shared key.
  79 + * generate the shared key with peer public key.
  80 + * @param ppkey peer public key.
  81 + * @param ppkey_size the size of ppkey.
  82 + * @param skey the computed shared key.
  83 + * @param skey_size the max shared key size, output the actual shared key size.
  84 + * user should never ignore this size.
  85 + */
  86 + virtual int copy_shared_key(const char* ppkey, int32_t ppkey_size, char* skey, int32_t& skey_size);
  87 + private:
  88 + virtual int do_initialize();
  89 + };
44 /** 90 /**
45 * the schema type. 91 * the schema type.
46 */ 92 */
@@ -85,6 +131,23 @@ namespace _srs_internal @@ -85,6 +131,23 @@ namespace _srs_internal
85 131
86 // 4bytes 132 // 4bytes
87 int32_t offset; 133 int32_t offset;
  134 + public:
  135 + // create new key block data.
  136 + // if created, user must free it by srs_key_block_free
  137 + void init();
  138 +
  139 + // calc the offset of key,
  140 + // the key->offset cannot be used as the offset of key.
  141 + int offsets();
  142 +
  143 + // parse key block from c1s1.
  144 + // if created, user must free it by srs_key_block_free
  145 + // @c1s1_key_bytes the key start bytes, maybe c1s1 or c1s1+764
  146 + int parse(char* c1s1_key_bytes);
  147 +
  148 + // free the block data create by
  149 + // srs_key_block_init or srs_key_block_parse
  150 + void free();
88 }; 151 };
89 152
90 /** 153 /**
@@ -111,88 +174,24 @@ namespace _srs_internal @@ -111,88 +174,24 @@ namespace _srs_internal
111 // (764-4-offset-32)bytes 174 // (764-4-offset-32)bytes
112 char* random1; 175 char* random1;
113 int random1_size; 176 int random1_size;
114 - };  
115 -  
116 - // the digest key generate size.  
117 - #define __SRS_OpensslHashSize 512  
118 - extern u_int8_t SrsGenuineFMSKey[];  
119 - extern u_int8_t SrsGenuineFPKey[];  
120 - int openssl_HMACsha256(const void* key, int key_size, const void* data, int data_size, void* digest);  
121 - int openssl_generate_key(char* public_key, int32_t size);  
122 -  
123 - /**  
124 - * the DH wrapper.  
125 - */  
126 - class SrsDH  
127 - {  
128 - private:  
129 - DH* pdh;  
130 public: 177 public:
131 - SrsDH();  
132 - virtual ~SrsDH();  
133 - public:  
134 - /**  
135 - * initialize dh, generate the public and private key.  
136 - * @param ensure_128bytes_public_key whether ensure public key is 128bytes,  
137 - * sometimes openssl generate 127bytes public key.  
138 - * default to false to donot ensure.  
139 - */  
140 - virtual int initialize(bool ensure_128bytes_public_key = false);  
141 - /**  
142 - * copy the public key.  
143 - * @param pkey the bytes to copy the public key.  
144 - * @param pkey_size the max public key size, output the actual public key size.  
145 - * user should never ignore this size.  
146 - * @remark, when ensure_128bytes_public_key, the size always 128.  
147 - */  
148 - virtual int copy_public_key(char* pkey, int32_t& pkey_size);  
149 - /**  
150 - * generate and copy the shared key.  
151 - * generate the shared key with peer public key.  
152 - * @param ppkey peer public key.  
153 - * @param ppkey_size the size of ppkey.  
154 - * @param skey the computed shared key.  
155 - * @param skey_size the max shared key size, output the actual shared key size.  
156 - * user should never ignore this size.  
157 - */  
158 - virtual int copy_shared_key(const char* ppkey, int32_t ppkey_size, char* skey, int32_t& skey_size);  
159 - private:  
160 - virtual int do_initialize();  
161 - };  
162 -  
163 - // calc the offset of key,  
164 - // the key->offset cannot be used as the offset of key.  
165 - int srs_key_block_get_offset(key_block* key);  
166 -  
167 - // create new key block data.  
168 - // if created, user must free it by srs_key_block_free  
169 - void srs_key_block_init(key_block* key);  
170 -  
171 - // parse key block from c1s1.  
172 - // if created, user must free it by srs_key_block_free  
173 - // @c1s1_key_bytes the key start bytes, maybe c1s1 or c1s1+764  
174 - int srs_key_block_parse(key_block* key, char* c1s1_key_bytes);  
175 -  
176 - // free the block data create by  
177 - // srs_key_block_init or srs_key_block_parse  
178 - void srs_key_block_free(key_block* key); 178 + // create new digest block data.
  179 + // if created, user must free it by srs_digest_block_free
  180 + void init();
179 181
180 // calc the offset of digest, 182 // calc the offset of digest,
181 // the key->offset cannot be used as the offset of digest. 183 // the key->offset cannot be used as the offset of digest.
182 - int srs_digest_block_get_offset(digest_block* digest);  
183 -  
184 - // create new digest block data.  
185 - // if created, user must free it by srs_digest_block_free  
186 - void srs_digest_block_init(digest_block* digest); 184 + int offsets();
187 185
188 // parse digest block from c1s1. 186 // parse digest block from c1s1.
189 // if created, user must free it by srs_digest_block_free 187 // if created, user must free it by srs_digest_block_free
190 // @c1s1_digest_bytes the digest start bytes, maybe c1s1 or c1s1+764 188 // @c1s1_digest_bytes the digest start bytes, maybe c1s1 or c1s1+764
191 - int srs_digest_block_parse(digest_block* digest, char* c1s1_digest_bytes); 189 + int parse(char* c1s1_digest_bytes);
192 190
193 // free the block data create by 191 // free the block data create by
194 // srs_digest_block_init or srs_digest_block_parse 192 // srs_digest_block_init or srs_digest_block_parse
195 - void srs_digest_block_free(digest_block* digest); 193 + void free();
  194 + };
196 195
197 /** 196 /**
198 * copy whole c1s1 to bytes. 197 * copy whole c1s1 to bytes.