正在显示
2 个修改的文件
包含
174 行增加
和
151 行删除
| @@ -292,28 +292,6 @@ namespace _srs_internal | @@ -292,28 +292,6 @@ namespace _srs_internal | ||
| 292 | return ret; | 292 | return ret; |
| 293 | } | 293 | } |
| 294 | 294 | ||
| 295 | - // read/write stream using SrsStream. | ||
| 296 | - void __srs_stream_write_4bytes(char* pp, int32_t value) | ||
| 297 | - { | ||
| 298 | - static SrsStream stream; | ||
| 299 | - | ||
| 300 | - int ret = stream.initialize(pp, 4); | ||
| 301 | - srs_assert(ret == ERROR_SUCCESS); | ||
| 302 | - | ||
| 303 | - stream.write_4bytes(value); | ||
| 304 | - } | ||
| 305 | - int32_t __srs_stream_read_4bytes(char* pp) | ||
| 306 | - { | ||
| 307 | - static SrsStream stream; | ||
| 308 | - | ||
| 309 | - int ret = stream.initialize(pp, 4); | ||
| 310 | - srs_assert(ret == ERROR_SUCCESS); | ||
| 311 | - | ||
| 312 | - return stream.read_4bytes(); | ||
| 313 | - } | ||
| 314 | - | ||
| 315 | - // create new key block data. | ||
| 316 | - // if created, user must free it by srs_key_block_free | ||
| 317 | void key_block::init() | 295 | void key_block::init() |
| 318 | { | 296 | { |
| 319 | key_block* key = this; | 297 | key_block* key = this; |
| @@ -342,8 +320,6 @@ namespace _srs_internal | @@ -342,8 +320,6 @@ namespace _srs_internal | ||
| 342 | } | 320 | } |
| 343 | } | 321 | } |
| 344 | 322 | ||
| 345 | - // calc the offset of key, | ||
| 346 | - // the key->offset cannot be used as the offset of key. | ||
| 347 | int key_block::offsets() | 323 | int key_block::offsets() |
| 348 | { | 324 | { |
| 349 | key_block* key = this; | 325 | key_block* key = this; |
| @@ -360,48 +336,46 @@ namespace _srs_internal | @@ -360,48 +336,46 @@ namespace _srs_internal | ||
| 360 | return offset % max_offset_size; | 336 | return offset % max_offset_size; |
| 361 | } | 337 | } |
| 362 | 338 | ||
| 363 | - // parse key block from c1s1. | ||
| 364 | - // if created, user must free it by srs_key_block_free | ||
| 365 | - // @c1s1_key_bytes the key start bytes, maybe c1s1 or c1s1+764 | ||
| 366 | - int key_block::parse(char* c1s1_key_bytes) | 339 | + int key_block::parse(SrsStream* stream) |
| 367 | { | 340 | { |
| 368 | key_block* key = this; | 341 | key_block* key = this; |
| 369 | 342 | ||
| 370 | int ret = ERROR_SUCCESS; | 343 | int ret = ERROR_SUCCESS; |
| 344 | + | ||
| 345 | + // the key must be 764 bytes. | ||
| 346 | + srs_assert(stream->require(764)); | ||
| 371 | 347 | ||
| 372 | - char* pp = c1s1_key_bytes + 764; | 348 | + // read the last offset first, 760-763 |
| 349 | + stream->skip(764 - sizeof(int32_t)); | ||
| 350 | + key->offset = stream->read_4bytes(); | ||
| 373 | 351 | ||
| 374 | - pp -= sizeof(int32_t); | ||
| 375 | - key->offset = __srs_stream_read_4bytes(pp); | 352 | + // reset stream to read others. |
| 353 | + stream->skip(-764); | ||
| 376 | 354 | ||
| 355 | + // TODO: FIXME: free it. | ||
| 377 | key->random0 = NULL; | 356 | key->random0 = NULL; |
| 378 | key->random1 = NULL; | 357 | key->random1 = NULL; |
| 379 | 358 | ||
| 380 | int offset = key->offsets(); | 359 | int offset = key->offsets(); |
| 381 | srs_assert(offset >= 0); | 360 | srs_assert(offset >= 0); |
| 382 | 361 | ||
| 383 | - pp = c1s1_key_bytes; | ||
| 384 | key->random0_size = offset; | 362 | key->random0_size = offset; |
| 385 | if (key->random0_size > 0) { | 363 | if (key->random0_size > 0) { |
| 386 | key->random0 = new char[key->random0_size]; | 364 | key->random0 = new char[key->random0_size]; |
| 387 | - memcpy(key->random0, pp, key->random0_size); | 365 | + stream->read_bytes(key->random0, key->random0_size); |
| 388 | } | 366 | } |
| 389 | - pp += key->random0_size; | ||
| 390 | 367 | ||
| 391 | - memcpy(key->key, pp, sizeof(key->key)); | ||
| 392 | - pp += sizeof(key->key); | 368 | + stream->read_bytes(key->key, 128); |
| 393 | 369 | ||
| 394 | key->random1_size = 764 - offset - 128 - 4; | 370 | key->random1_size = 764 - offset - 128 - 4; |
| 395 | if (key->random1_size > 0) { | 371 | if (key->random1_size > 0) { |
| 396 | key->random1 = new char[key->random1_size]; | 372 | key->random1 = new char[key->random1_size]; |
| 397 | - memcpy(key->random1, pp, key->random1_size); | 373 | + stream->read_bytes(key->random1, key->random1_size); |
| 398 | } | 374 | } |
| 399 | 375 | ||
| 400 | return ret; | 376 | return ret; |
| 401 | } | 377 | } |
| 402 | 378 | ||
| 403 | - // free the block data create by | ||
| 404 | - // srs_key_block_init or srs_key_block_parse | ||
| 405 | void key_block::free() | 379 | void key_block::free() |
| 406 | { | 380 | { |
| 407 | key_block* key = this; | 381 | key_block* key = this; |
| @@ -414,8 +388,6 @@ namespace _srs_internal | @@ -414,8 +388,6 @@ namespace _srs_internal | ||
| 414 | } | 388 | } |
| 415 | } | 389 | } |
| 416 | 390 | ||
| 417 | - // create new digest block data. | ||
| 418 | - // if created, user must free it by srs_digest_block_free | ||
| 419 | void digest_block::init() | 391 | void digest_block::init() |
| 420 | { | 392 | { |
| 421 | digest_block* digest = this; | 393 | digest_block* digest = this; |
| @@ -444,8 +416,6 @@ namespace _srs_internal | @@ -444,8 +416,6 @@ namespace _srs_internal | ||
| 444 | } | 416 | } |
| 445 | } | 417 | } |
| 446 | 418 | ||
| 447 | - // calc the offset of digest, | ||
| 448 | - // the key->offset cannot be used as the offset of digest. | ||
| 449 | int digest_block::offsets() | 419 | int digest_block::offsets() |
| 450 | { | 420 | { |
| 451 | digest_block* digest = this; | 421 | digest_block* digest = this; |
| @@ -462,20 +432,18 @@ namespace _srs_internal | @@ -462,20 +432,18 @@ namespace _srs_internal | ||
| 462 | return offset % max_offset_size; | 432 | return offset % max_offset_size; |
| 463 | } | 433 | } |
| 464 | 434 | ||
| 465 | - // parse digest block from c1s1. | ||
| 466 | - // if created, user must free it by srs_digest_block_free | ||
| 467 | - // @c1s1_digest_bytes the digest start bytes, maybe c1s1 or c1s1+764 | ||
| 468 | - int digest_block::parse(char* c1s1_digest_bytes) | 435 | + int digest_block::parse(SrsStream* stream) |
| 469 | { | 436 | { |
| 470 | digest_block* digest = this; | 437 | digest_block* digest = this; |
| 471 | 438 | ||
| 472 | int ret = ERROR_SUCCESS; | 439 | int ret = ERROR_SUCCESS; |
| 473 | - | ||
| 474 | - char* pp = c1s1_digest_bytes; | ||
| 475 | 440 | ||
| 476 | - digest->offset = __srs_stream_read_4bytes(pp); | ||
| 477 | - pp += sizeof(int32_t); | 441 | + // the digest must be 764 bytes. |
| 442 | + srs_assert(stream->require(764)); | ||
| 443 | + | ||
| 444 | + digest->offset = stream->read_4bytes(); | ||
| 478 | 445 | ||
| 446 | + // TODO: FIXME: free it. | ||
| 479 | digest->random0 = NULL; | 447 | digest->random0 = NULL; |
| 480 | digest->random1 = NULL; | 448 | digest->random1 = NULL; |
| 481 | 449 | ||
| @@ -485,24 +453,20 @@ namespace _srs_internal | @@ -485,24 +453,20 @@ namespace _srs_internal | ||
| 485 | digest->random0_size = offset; | 453 | digest->random0_size = offset; |
| 486 | if (digest->random0_size > 0) { | 454 | if (digest->random0_size > 0) { |
| 487 | digest->random0 = new char[digest->random0_size]; | 455 | digest->random0 = new char[digest->random0_size]; |
| 488 | - memcpy(digest->random0, pp, digest->random0_size); | 456 | + stream->read_bytes(digest->random0, digest->random0_size); |
| 489 | } | 457 | } |
| 490 | - pp += digest->random0_size; | ||
| 491 | 458 | ||
| 492 | - memcpy(digest->digest, pp, sizeof(digest->digest)); | ||
| 493 | - pp += sizeof(digest->digest); | 459 | + stream->read_bytes(digest->digest, 32); |
| 494 | 460 | ||
| 495 | digest->random1_size = 764 - 4 - offset - 32; | 461 | digest->random1_size = 764 - 4 - offset - 32; |
| 496 | if (digest->random1_size > 0) { | 462 | if (digest->random1_size > 0) { |
| 497 | digest->random1 = new char[digest->random1_size]; | 463 | digest->random1 = new char[digest->random1_size]; |
| 498 | - memcpy(digest->random1, pp, digest->random1_size); | 464 | + stream->read_bytes(digest->random1, digest->random1_size); |
| 499 | } | 465 | } |
| 500 | 466 | ||
| 501 | return ret; | 467 | return ret; |
| 502 | } | 468 | } |
| 503 | 469 | ||
| 504 | - // free the block data create by | ||
| 505 | - // srs_digest_block_init or srs_digest_block_parse | ||
| 506 | void digest_block::free() | 470 | void digest_block::free() |
| 507 | { | 471 | { |
| 508 | digest_block* digest = this; | 472 | digest_block* digest = this; |
| @@ -532,9 +496,9 @@ namespace _srs_internal | @@ -532,9 +496,9 @@ namespace _srs_internal | ||
| 532 | return digest.digest; | 496 | return digest.digest; |
| 533 | } | 497 | } |
| 534 | 498 | ||
| 535 | - void c1s1_strategy::dump(c1s1* owner, char* _c1s1) | 499 | + int c1s1_strategy::dump(c1s1* owner, char* _c1s1, int size) |
| 536 | { | 500 | { |
| 537 | - copy_to(owner, _c1s1, true); | 501 | + return copy_to(owner, _c1s1, size, true); |
| 538 | } | 502 | } |
| 539 | 503 | ||
| 540 | int c1s1_strategy::c1_create(c1s1* owner) | 504 | int c1s1_strategy::c1_create(c1s1* owner) |
| @@ -646,7 +610,9 @@ namespace _srs_internal | @@ -646,7 +610,9 @@ namespace _srs_internal | ||
| 646 | */ | 610 | */ |
| 647 | char* c1s1_joined_bytes = new char[1536 -32]; | 611 | char* c1s1_joined_bytes = new char[1536 -32]; |
| 648 | SrsAutoFree(char, c1s1_joined_bytes); | 612 | SrsAutoFree(char, c1s1_joined_bytes); |
| 649 | - copy_to(owner, c1s1_joined_bytes, false); | 613 | + if ((ret = copy_to(owner, c1s1_joined_bytes, 1536 - 32, false)) != ERROR_SUCCESS) { |
| 614 | + return ret; | ||
| 615 | + } | ||
| 650 | 616 | ||
| 651 | c1_digest = new char[__SRS_OpensslHashSize]; | 617 | c1_digest = new char[__SRS_OpensslHashSize]; |
| 652 | if ((ret = openssl_HMACsha256(SrsGenuineFPKey, 30, c1s1_joined_bytes, 1536 - 32, c1_digest)) != ERROR_SUCCESS) { | 618 | if ((ret = openssl_HMACsha256(SrsGenuineFPKey, 30, c1s1_joined_bytes, 1536 - 32, c1_digest)) != ERROR_SUCCESS) { |
| @@ -672,7 +638,9 @@ namespace _srs_internal | @@ -672,7 +638,9 @@ namespace _srs_internal | ||
| 672 | */ | 638 | */ |
| 673 | char* c1s1_joined_bytes = new char[1536 -32]; | 639 | char* c1s1_joined_bytes = new char[1536 -32]; |
| 674 | SrsAutoFree(char, c1s1_joined_bytes); | 640 | SrsAutoFree(char, c1s1_joined_bytes); |
| 675 | - copy_to(owner, c1s1_joined_bytes, false); | 641 | + if ((ret = copy_to(owner, c1s1_joined_bytes, 1536 - 32, false)) != ERROR_SUCCESS) { |
| 642 | + return ret; | ||
| 643 | + } | ||
| 676 | 644 | ||
| 677 | s1_digest = new char[__SRS_OpensslHashSize]; | 645 | s1_digest = new char[__SRS_OpensslHashSize]; |
| 678 | if ((ret = openssl_HMACsha256(SrsGenuineFMSKey, 36, c1s1_joined_bytes, 1536 - 32, s1_digest)) != ERROR_SUCCESS) { | 646 | if ((ret = openssl_HMACsha256(SrsGenuineFMSKey, 36, c1s1_joined_bytes, 1536 - 32, s1_digest)) != ERROR_SUCCESS) { |
| @@ -685,58 +653,66 @@ namespace _srs_internal | @@ -685,58 +653,66 @@ namespace _srs_internal | ||
| 685 | return ret; | 653 | return ret; |
| 686 | } | 654 | } |
| 687 | 655 | ||
| 688 | - void c1s1_strategy::copy_time_version(char*& pp, c1s1* owner) | 656 | + void c1s1_strategy::copy_time_version(SrsStream* stream, c1s1* owner) |
| 689 | { | 657 | { |
| 658 | + srs_assert(stream->require(8)); | ||
| 659 | + | ||
| 690 | // 4bytes time | 660 | // 4bytes time |
| 691 | - __srs_stream_write_4bytes(pp, owner->time); | ||
| 692 | - pp += 4; | 661 | + stream->write_4bytes(owner->time); |
| 662 | + | ||
| 693 | // 4bytes version | 663 | // 4bytes version |
| 694 | - __srs_stream_write_4bytes(pp, owner->version); | ||
| 695 | - pp += 4; | 664 | + stream->write_4bytes(owner->version); |
| 696 | } | 665 | } |
| 697 | - void c1s1_strategy::copy_key(char*& pp) | 666 | + void c1s1_strategy::copy_key(SrsStream* stream) |
| 698 | { | 667 | { |
| 668 | + srs_assert(key.random0_size >= 0); | ||
| 669 | + srs_assert(key.random1_size >= 0); | ||
| 670 | + | ||
| 671 | + int total = key.random0_size + 128 + key.random1_size + 4; | ||
| 672 | + srs_assert(stream->require(total)); | ||
| 673 | + | ||
| 699 | // 764bytes key block | 674 | // 764bytes key block |
| 700 | if (key.random0_size > 0) { | 675 | if (key.random0_size > 0) { |
| 701 | - memcpy(pp, key.random0, key.random0_size); | 676 | + stream->write_bytes(key.random0, key.random0_size); |
| 702 | } | 677 | } |
| 703 | - pp += key.random0_size; | ||
| 704 | 678 | ||
| 705 | - memcpy(pp, key.key, sizeof(key.key)); | ||
| 706 | - pp += sizeof(key.key); | 679 | + stream->write_bytes(key.key, 128); |
| 707 | 680 | ||
| 708 | if (key.random1_size > 0) { | 681 | if (key.random1_size > 0) { |
| 709 | - memcpy(pp, key.random1, key.random1_size); | 682 | + stream->write_bytes(key.random1, key.random1_size); |
| 710 | } | 683 | } |
| 711 | - pp += key.random1_size; | ||
| 712 | 684 | ||
| 713 | - __srs_stream_write_4bytes(pp, key.offset); | ||
| 714 | - pp += 4; | 685 | + stream->write_4bytes(key.offset); |
| 715 | } | 686 | } |
| 716 | - void c1s1_strategy::digest_key(char*& pp, bool with_digest) | 687 | + void c1s1_strategy::copy_digest(SrsStream* stream, bool with_digest) |
| 717 | { | 688 | { |
| 689 | + srs_assert(key.random0_size >= 0); | ||
| 690 | + srs_assert(key.random1_size >= 0); | ||
| 691 | + | ||
| 692 | + int total = 4 + digest.random0_size + digest.random1_size; | ||
| 693 | + if (with_digest) { | ||
| 694 | + total += 32; | ||
| 695 | + } | ||
| 696 | + srs_assert(stream->require(total)); | ||
| 697 | + | ||
| 718 | // 732bytes digest block without the 32bytes digest-data | 698 | // 732bytes digest block without the 32bytes digest-data |
| 719 | // nbytes digest block part1 | 699 | // nbytes digest block part1 |
| 720 | - __srs_stream_write_4bytes(pp, digest.offset); | ||
| 721 | - pp += 4; | 700 | + stream->write_4bytes(digest.offset); |
| 722 | 701 | ||
| 723 | // digest random padding. | 702 | // digest random padding. |
| 724 | if (digest.random0_size > 0) { | 703 | if (digest.random0_size > 0) { |
| 725 | - memcpy(pp, digest.random0, digest.random0_size); | 704 | + stream->write_bytes(digest.random0, digest.random0_size); |
| 726 | } | 705 | } |
| 727 | - pp += digest.random0_size; | ||
| 728 | 706 | ||
| 729 | // digest | 707 | // digest |
| 730 | if (with_digest) { | 708 | if (with_digest) { |
| 731 | - memcpy(pp, digest.digest, 32); | ||
| 732 | - pp += 32; | 709 | + stream->write_bytes(digest.digest, 32); |
| 733 | } | 710 | } |
| 734 | 711 | ||
| 735 | // nbytes digest block part2 | 712 | // nbytes digest block part2 |
| 736 | if (digest.random1_size > 0) { | 713 | if (digest.random1_size > 0) { |
| 737 | - memcpy(pp, digest.random1, digest.random1_size); | 714 | + stream->write_bytes(digest.random1, digest.random1_size); |
| 738 | } | 715 | } |
| 739 | - pp += digest.random1_size; | ||
| 740 | } | 716 | } |
| 741 | 717 | ||
| 742 | c1s1_strategy_schema0::c1s1_strategy_schema0() | 718 | c1s1_strategy_schema0::c1s1_strategy_schema0() |
| @@ -752,16 +728,28 @@ namespace _srs_internal | @@ -752,16 +728,28 @@ namespace _srs_internal | ||
| 752 | return srs_schema0; | 728 | return srs_schema0; |
| 753 | } | 729 | } |
| 754 | 730 | ||
| 755 | - int c1s1_strategy_schema0::parse(char* _c1s1) | 731 | + int c1s1_strategy_schema0::parse(char* _c1s1, int size) |
| 756 | { | 732 | { |
| 757 | int ret = ERROR_SUCCESS; | 733 | int ret = ERROR_SUCCESS; |
| 758 | 734 | ||
| 759 | - if ((ret = key.parse(_c1s1 + 8)) != ERROR_SUCCESS) { | 735 | + srs_assert(size == 1536); |
| 736 | + | ||
| 737 | + SrsStream stream; | ||
| 738 | + | ||
| 739 | + if ((ret = stream.initialize(_c1s1 + 8, 764)) != ERROR_SUCCESS) { | ||
| 740 | + return ret; | ||
| 741 | + } | ||
| 742 | + | ||
| 743 | + if ((ret = key.parse(&stream)) != ERROR_SUCCESS) { | ||
| 760 | srs_error("parse the c1 key failed. ret=%d", ret); | 744 | srs_error("parse the c1 key failed. ret=%d", ret); |
| 761 | return ret; | 745 | return ret; |
| 762 | } | 746 | } |
| 763 | 747 | ||
| 764 | - if ((ret = digest.parse(_c1s1 + 8 + 764)) != ERROR_SUCCESS) { | 748 | + if ((ret = stream.initialize(_c1s1 + 8 + 764, 764)) != ERROR_SUCCESS) { |
| 749 | + return ret; | ||
| 750 | + } | ||
| 751 | + | ||
| 752 | + if ((ret = digest.parse(&stream)) != ERROR_SUCCESS) { | ||
| 765 | srs_error("parse the c1 digest failed. ret=%d", ret); | 753 | srs_error("parse the c1 digest failed. ret=%d", ret); |
| 766 | return ret; | 754 | return ret; |
| 767 | } | 755 | } |
| @@ -771,19 +759,20 @@ namespace _srs_internal | @@ -771,19 +759,20 @@ namespace _srs_internal | ||
| 771 | return ret; | 759 | return ret; |
| 772 | } | 760 | } |
| 773 | 761 | ||
| 774 | - void c1s1_strategy_schema0::copy_to(c1s1* owner, char* bytes, bool with_digest) | 762 | + int c1s1_strategy_schema0::copy_to(c1s1* owner, char* bytes, int size, bool with_digest) |
| 775 | { | 763 | { |
| 776 | - char* pp = bytes; | ||
| 777 | - | ||
| 778 | - copy_time_version(pp, owner); | ||
| 779 | - copy_key(pp); | ||
| 780 | - digest_key(pp, with_digest); | 764 | + SrsStream stream; |
| 765 | + int ret = ERROR_SUCCESS; | ||
| 781 | 766 | ||
| 782 | - if (with_digest) { | ||
| 783 | - srs_assert(pp - bytes == 1536); | ||
| 784 | - } else { | ||
| 785 | - srs_assert(pp - bytes == 1536 - 32); | 767 | + if ((ret = stream.initialize(bytes, size)) != ERROR_SUCCESS) { |
| 768 | + return ret; | ||
| 786 | } | 769 | } |
| 770 | + | ||
| 771 | + copy_time_version(&stream, owner); | ||
| 772 | + copy_key(&stream); | ||
| 773 | + copy_digest(&stream, with_digest); | ||
| 774 | + | ||
| 775 | + srs_assert(stream.empty()); | ||
| 787 | } | 776 | } |
| 788 | 777 | ||
| 789 | c1s1_strategy_schema1::c1s1_strategy_schema1() | 778 | c1s1_strategy_schema1::c1s1_strategy_schema1() |
| @@ -799,16 +788,28 @@ namespace _srs_internal | @@ -799,16 +788,28 @@ namespace _srs_internal | ||
| 799 | return srs_schema1; | 788 | return srs_schema1; |
| 800 | } | 789 | } |
| 801 | 790 | ||
| 802 | - int c1s1_strategy_schema1::parse(char* _c1s1) | 791 | + int c1s1_strategy_schema1::parse(char* _c1s1, int size) |
| 803 | { | 792 | { |
| 804 | int ret = ERROR_SUCCESS; | 793 | int ret = ERROR_SUCCESS; |
| 805 | 794 | ||
| 806 | - if ((ret = digest.parse(_c1s1 + 8)) != ERROR_SUCCESS) { | 795 | + srs_assert(size == 1536); |
| 796 | + | ||
| 797 | + SrsStream stream; | ||
| 798 | + | ||
| 799 | + if ((ret = stream.initialize(_c1s1 + 8, 764)) != ERROR_SUCCESS) { | ||
| 800 | + return ret; | ||
| 801 | + } | ||
| 802 | + | ||
| 803 | + if ((ret = digest.parse(&stream)) != ERROR_SUCCESS) { | ||
| 807 | srs_error("parse the c1 digest failed. ret=%d", ret); | 804 | srs_error("parse the c1 digest failed. ret=%d", ret); |
| 808 | return ret; | 805 | return ret; |
| 809 | } | 806 | } |
| 810 | 807 | ||
| 811 | - if ((ret = key.parse(_c1s1 + 8 + 764)) != ERROR_SUCCESS) { | 808 | + if ((ret = stream.initialize(_c1s1 + 8 + 764, 764)) != ERROR_SUCCESS) { |
| 809 | + return ret; | ||
| 810 | + } | ||
| 811 | + | ||
| 812 | + if ((ret = key.parse(&stream)) != ERROR_SUCCESS) { | ||
| 812 | srs_error("parse the c1 key failed. ret=%d", ret); | 813 | srs_error("parse the c1 key failed. ret=%d", ret); |
| 813 | return ret; | 814 | return ret; |
| 814 | } | 815 | } |
| @@ -818,19 +819,20 @@ namespace _srs_internal | @@ -818,19 +819,20 @@ namespace _srs_internal | ||
| 818 | return ret; | 819 | return ret; |
| 819 | } | 820 | } |
| 820 | 821 | ||
| 821 | - void c1s1_strategy_schema1::copy_to(c1s1* owner, char* bytes, bool with_digest) | 822 | + int c1s1_strategy_schema1::copy_to(c1s1* owner, char* bytes, int size, bool with_digest) |
| 822 | { | 823 | { |
| 823 | - char* pp = bytes; | ||
| 824 | - | ||
| 825 | - copy_time_version(pp, owner); | ||
| 826 | - digest_key(pp, with_digest); | ||
| 827 | - copy_key(pp); | 824 | + SrsStream stream; |
| 825 | + int ret = ERROR_SUCCESS; | ||
| 828 | 826 | ||
| 829 | - if (with_digest) { | ||
| 830 | - srs_assert(pp - bytes == 1536); | ||
| 831 | - } else { | ||
| 832 | - srs_assert(pp - bytes == 1536 - 32); | 827 | + if ((ret = stream.initialize(bytes, size)) != ERROR_SUCCESS) { |
| 828 | + return ret; | ||
| 833 | } | 829 | } |
| 830 | + | ||
| 831 | + copy_time_version(&stream, owner); | ||
| 832 | + copy_digest(&stream, with_digest); | ||
| 833 | + copy_key(&stream); | ||
| 834 | + | ||
| 835 | + srs_assert(stream.empty()); | ||
| 834 | } | 836 | } |
| 835 | 837 | ||
| 836 | // TODO: FIXME: move to the right position. | 838 | // TODO: FIXME: move to the right position. |
| @@ -870,24 +872,32 @@ namespace _srs_internal | @@ -870,24 +872,32 @@ namespace _srs_internal | ||
| 870 | return payload->get_digest(); | 872 | return payload->get_digest(); |
| 871 | } | 873 | } |
| 872 | 874 | ||
| 873 | - void c1s1::dump(char* _c1s1) | 875 | + int c1s1::dump(char* _c1s1, int size) |
| 874 | { | 876 | { |
| 875 | srs_assert(payload != NULL); | 877 | srs_assert(payload != NULL); |
| 876 | - return payload->dump(this, _c1s1); | 878 | + return payload->dump(this, _c1s1, size); |
| 877 | } | 879 | } |
| 878 | 880 | ||
| 879 | - int c1s1::parse(char* _c1s1, srs_schema_type schema) | 881 | + int c1s1::parse(char* _c1s1, int size, srs_schema_type schema) |
| 880 | { | 882 | { |
| 881 | int ret = ERROR_SUCCESS; | 883 | int ret = ERROR_SUCCESS; |
| 882 | 884 | ||
| 885 | + srs_assert(size == 1536); | ||
| 886 | + | ||
| 883 | if (schema != srs_schema0 && schema != srs_schema1) { | 887 | if (schema != srs_schema0 && schema != srs_schema1) { |
| 884 | ret = ERROR_RTMP_CH_SCHEMA; | 888 | ret = ERROR_RTMP_CH_SCHEMA; |
| 885 | srs_error("parse c1 failed. invalid schema=%d, ret=%d", schema, ret); | 889 | srs_error("parse c1 failed. invalid schema=%d, ret=%d", schema, ret); |
| 886 | return ret; | 890 | return ret; |
| 887 | } | 891 | } |
| 888 | 892 | ||
| 889 | - time = __srs_stream_read_4bytes(_c1s1); | ||
| 890 | - version = __srs_stream_read_4bytes(_c1s1 + 4); // client c1 version | 893 | + SrsStream stream; |
| 894 | + | ||
| 895 | + if ((ret = stream.initialize(_c1s1, size)) != ERROR_SUCCESS) { | ||
| 896 | + return ret; | ||
| 897 | + } | ||
| 898 | + | ||
| 899 | + time = stream.read_4bytes(); | ||
| 900 | + version = stream.read_4bytes(); // client c1 version | ||
| 891 | 901 | ||
| 892 | srs_freep(payload); | 902 | srs_freep(payload); |
| 893 | if (schema == srs_schema0) { | 903 | if (schema == srs_schema0) { |
| @@ -896,7 +906,7 @@ namespace _srs_internal | @@ -896,7 +906,7 @@ namespace _srs_internal | ||
| 896 | payload = new c1s1_strategy_schema1(); | 906 | payload = new c1s1_strategy_schema1(); |
| 897 | } | 907 | } |
| 898 | 908 | ||
| 899 | - return payload->parse(_c1s1); | 909 | + return payload->parse(_c1s1, size); |
| 900 | } | 910 | } |
| 901 | 911 | ||
| 902 | int c1s1::c1_create(srs_schema_type schema) | 912 | int c1s1::c1_create(srs_schema_type schema) |
| @@ -976,16 +986,24 @@ namespace _srs_internal | @@ -976,16 +986,24 @@ namespace _srs_internal | ||
| 976 | { | 986 | { |
| 977 | } | 987 | } |
| 978 | 988 | ||
| 979 | - void c2s2::dump(char* _c2s2) | 989 | + int c2s2::dump(char* _c2s2, int size) |
| 980 | { | 990 | { |
| 991 | + srs_assert(size == 1536); | ||
| 992 | + | ||
| 981 | memcpy(_c2s2, random, 1504); | 993 | memcpy(_c2s2, random, 1504); |
| 982 | memcpy(_c2s2 + 1504, digest, 32); | 994 | memcpy(_c2s2 + 1504, digest, 32); |
| 995 | + | ||
| 996 | + return ERROR_SUCCESS; | ||
| 983 | } | 997 | } |
| 984 | 998 | ||
| 985 | - void c2s2::parse(char* _c2s2) | 999 | + int c2s2::parse(char* _c2s2, int size) |
| 986 | { | 1000 | { |
| 1001 | + srs_assert(size == 1536); | ||
| 1002 | + | ||
| 987 | memcpy(random, _c2s2, 1504); | 1003 | memcpy(random, _c2s2, 1504); |
| 988 | memcpy(digest, _c2s2 + 1504, 32); | 1004 | memcpy(digest, _c2s2 + 1504, 32); |
| 1005 | + | ||
| 1006 | + return ERROR_SUCCESS; | ||
| 989 | } | 1007 | } |
| 990 | 1008 | ||
| 991 | int c2s2::c2_create(c1s1* s1) | 1009 | int c2s2::c2_create(c1s1* s1) |
| @@ -1201,7 +1219,7 @@ int SrsComplexHandshake::handshake_with_client(SrsHandshakeBytes* hs_bytes, ISrs | @@ -1201,7 +1219,7 @@ int SrsComplexHandshake::handshake_with_client(SrsHandshakeBytes* hs_bytes, ISrs | ||
| 1201 | c1s1 c1; | 1219 | c1s1 c1; |
| 1202 | // try schema0. | 1220 | // try schema0. |
| 1203 | // @remark, use schema0 to make flash player happy. | 1221 | // @remark, use schema0 to make flash player happy. |
| 1204 | - if ((ret = c1.parse(hs_bytes->c0c1 + 1, srs_schema0)) != ERROR_SUCCESS) { | 1222 | + if ((ret = c1.parse(hs_bytes->c0c1 + 1, 1536, srs_schema0)) != ERROR_SUCCESS) { |
| 1205 | srs_error("parse c1 schema%d error. ret=%d", srs_schema0, ret); | 1223 | srs_error("parse c1 schema%d error. ret=%d", srs_schema0, ret); |
| 1206 | return ret; | 1224 | return ret; |
| 1207 | } | 1225 | } |
| @@ -1209,7 +1227,7 @@ int SrsComplexHandshake::handshake_with_client(SrsHandshakeBytes* hs_bytes, ISrs | @@ -1209,7 +1227,7 @@ int SrsComplexHandshake::handshake_with_client(SrsHandshakeBytes* hs_bytes, ISrs | ||
| 1209 | bool is_valid = false; | 1227 | bool is_valid = false; |
| 1210 | if ((ret = c1.c1_validate_digest(is_valid)) != ERROR_SUCCESS || !is_valid) { | 1228 | if ((ret = c1.c1_validate_digest(is_valid)) != ERROR_SUCCESS || !is_valid) { |
| 1211 | srs_info("schema0 failed, try schema1."); | 1229 | srs_info("schema0 failed, try schema1."); |
| 1212 | - if ((ret = c1.parse(hs_bytes->c0c1 + 1, srs_schema1)) != ERROR_SUCCESS) { | 1230 | + if ((ret = c1.parse(hs_bytes->c0c1 + 1, 1536, srs_schema1)) != ERROR_SUCCESS) { |
| 1213 | srs_error("parse c1 schema%d error. ret=%d", srs_schema1, ret); | 1231 | srs_error("parse c1 schema%d error. ret=%d", srs_schema1, ret); |
| 1214 | return ret; | 1232 | return ret; |
| 1215 | } | 1233 | } |
| @@ -1257,8 +1275,12 @@ int SrsComplexHandshake::handshake_with_client(SrsHandshakeBytes* hs_bytes, ISrs | @@ -1257,8 +1275,12 @@ int SrsComplexHandshake::handshake_with_client(SrsHandshakeBytes* hs_bytes, ISrs | ||
| 1257 | if ((ret = hs_bytes->create_s0s1s2()) != ERROR_SUCCESS) { | 1275 | if ((ret = hs_bytes->create_s0s1s2()) != ERROR_SUCCESS) { |
| 1258 | return ret; | 1276 | return ret; |
| 1259 | } | 1277 | } |
| 1260 | - s1.dump(hs_bytes->s0s1s2 + 1); | ||
| 1261 | - s2.dump(hs_bytes->s0s1s2 + 1537); | 1278 | + if ((ret = s1.dump(hs_bytes->s0s1s2 + 1, 1536)) != ERROR_SUCCESS) { |
| 1279 | + return ret; | ||
| 1280 | + } | ||
| 1281 | + if ((ret = s2.dump(hs_bytes->s0s1s2 + 1537, 1536)) != ERROR_SUCCESS) { | ||
| 1282 | + return ret; | ||
| 1283 | + } | ||
| 1262 | if ((ret = io->write(hs_bytes->s0s1s2, 3073, &nsize)) != ERROR_SUCCESS) { | 1284 | if ((ret = io->write(hs_bytes->s0s1s2, 3073, &nsize)) != ERROR_SUCCESS) { |
| 1263 | srs_warn("complex handshake send s0s1s2 failed. ret=%d", ret); | 1285 | srs_warn("complex handshake send s0s1s2 failed. ret=%d", ret); |
| 1264 | return ret; | 1286 | return ret; |
| @@ -1270,7 +1292,9 @@ int SrsComplexHandshake::handshake_with_client(SrsHandshakeBytes* hs_bytes, ISrs | @@ -1270,7 +1292,9 @@ int SrsComplexHandshake::handshake_with_client(SrsHandshakeBytes* hs_bytes, ISrs | ||
| 1270 | return ret; | 1292 | return ret; |
| 1271 | } | 1293 | } |
| 1272 | c2s2 c2; | 1294 | c2s2 c2; |
| 1273 | - c2.parse(hs_bytes->c2); | 1295 | + if ((ret = c2.parse(hs_bytes->c2, 1536)) != ERROR_SUCCESS) { |
| 1296 | + return ret; | ||
| 1297 | + } | ||
| 1274 | srs_verbose("complex handshake read c2 success."); | 1298 | srs_verbose("complex handshake read c2 success."); |
| 1275 | 1299 | ||
| 1276 | // verify c2 | 1300 | // verify c2 |
| @@ -1306,7 +1330,9 @@ int SrsComplexHandshake::handshake_with_server(SrsHandshakeBytes* hs_bytes, ISrs | @@ -1306,7 +1330,9 @@ int SrsComplexHandshake::handshake_with_server(SrsHandshakeBytes* hs_bytes, ISrs | ||
| 1306 | if ((ret = c1.c1_create(srs_schema1)) != ERROR_SUCCESS) { | 1330 | if ((ret = c1.c1_create(srs_schema1)) != ERROR_SUCCESS) { |
| 1307 | return ret; | 1331 | return ret; |
| 1308 | } | 1332 | } |
| 1309 | - c1.dump(hs_bytes->c0c1 + 1); | 1333 | + if ((ret = c1.dump(hs_bytes->c0c1 + 1, 1536)) != ERROR_SUCCESS) { |
| 1334 | + return ret; | ||
| 1335 | + } | ||
| 1310 | 1336 | ||
| 1311 | // verify c1 | 1337 | // verify c1 |
| 1312 | bool is_valid; | 1338 | bool is_valid; |
| @@ -1335,7 +1361,7 @@ int SrsComplexHandshake::handshake_with_server(SrsHandshakeBytes* hs_bytes, ISrs | @@ -1335,7 +1361,7 @@ int SrsComplexHandshake::handshake_with_server(SrsHandshakeBytes* hs_bytes, ISrs | ||
| 1335 | 1361 | ||
| 1336 | // verify s1s2 | 1362 | // verify s1s2 |
| 1337 | c1s1 s1; | 1363 | c1s1 s1; |
| 1338 | - if ((ret = s1.parse(hs_bytes->s0s1s2 + 1, c1.schema())) != ERROR_SUCCESS) { | 1364 | + if ((ret = s1.parse(hs_bytes->s0s1s2 + 1, 1536, c1.schema())) != ERROR_SUCCESS) { |
| 1339 | return ret; | 1365 | return ret; |
| 1340 | } | 1366 | } |
| 1341 | 1367 | ||
| @@ -1353,7 +1379,9 @@ int SrsComplexHandshake::handshake_with_server(SrsHandshakeBytes* hs_bytes, ISrs | @@ -1353,7 +1379,9 @@ int SrsComplexHandshake::handshake_with_server(SrsHandshakeBytes* hs_bytes, ISrs | ||
| 1353 | return ret; | 1379 | return ret; |
| 1354 | } | 1380 | } |
| 1355 | 1381 | ||
| 1356 | - c2.dump(hs_bytes->c2); | 1382 | + if ((ret = c2.dump(hs_bytes->c2, 1536)) != ERROR_SUCCESS) { |
| 1383 | + return ret; | ||
| 1384 | + } | ||
| 1357 | if ((ret = io->write(hs_bytes->c2, 1536, &nsize)) != ERROR_SUCCESS) { | 1385 | if ((ret = io->write(hs_bytes->c2, 1536, &nsize)) != ERROR_SUCCESS) { |
| 1358 | srs_warn("complex handshake write c2 failed. ret=%d", ret); | 1386 | srs_warn("complex handshake write c2 failed. ret=%d", ret); |
| 1359 | return ret; | 1387 | return ret; |
| @@ -33,6 +33,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -33,6 +33,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 33 | class ISrsProtocolReaderWriter; | 33 | class ISrsProtocolReaderWriter; |
| 34 | class SrsComplexHandshake; | 34 | class SrsComplexHandshake; |
| 35 | class SrsHandshakeBytes; | 35 | class SrsHandshakeBytes; |
| 36 | +class SrsStream; | ||
| 36 | 37 | ||
| 37 | #ifdef SRS_AUTO_SSL | 38 | #ifdef SRS_AUTO_SSL |
| 38 | 39 | ||
| @@ -142,8 +143,8 @@ namespace _srs_internal | @@ -142,8 +143,8 @@ namespace _srs_internal | ||
| 142 | 143 | ||
| 143 | // parse key block from c1s1. | 144 | // parse key block from c1s1. |
| 144 | // if created, user must free it by srs_key_block_free | 145 | // 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); | 146 | + // @stream contains c1s1_key_bytes the key start bytes |
| 147 | + int parse(SrsStream* stream); | ||
| 147 | 148 | ||
| 148 | // free the block data create by | 149 | // free the block data create by |
| 149 | // srs_key_block_init or srs_key_block_parse | 150 | // srs_key_block_init or srs_key_block_parse |
| @@ -185,8 +186,8 @@ namespace _srs_internal | @@ -185,8 +186,8 @@ namespace _srs_internal | ||
| 185 | 186 | ||
| 186 | // parse digest block from c1s1. | 187 | // parse digest block from c1s1. |
| 187 | // if created, user must free it by srs_digest_block_free | 188 | // if created, user must free it by srs_digest_block_free |
| 188 | - // @c1s1_digest_bytes the digest start bytes, maybe c1s1 or c1s1+764 | ||
| 189 | - int parse(char* c1s1_digest_bytes); | 189 | + // @stream contains c1s1_digest_bytes the digest start bytes |
| 190 | + int parse(SrsStream* stream); | ||
| 190 | 191 | ||
| 191 | // free the block data create by | 192 | // free the block data create by |
| 192 | // srs_digest_block_init or srs_digest_block_parse | 193 | // srs_digest_block_init or srs_digest_block_parse |
| @@ -220,12 +221,12 @@ namespace _srs_internal | @@ -220,12 +221,12 @@ namespace _srs_internal | ||
| 220 | /** | 221 | /** |
| 221 | * copy to bytes. | 222 | * copy to bytes. |
| 222 | */ | 223 | */ |
| 223 | - virtual void dump(c1s1* owner, char* _c1s1); | 224 | + virtual int dump(c1s1* owner, char* _c1s1, int size); |
| 224 | /** | 225 | /** |
| 225 | * server: parse the c1s1, discovery the key and digest by schema. | 226 | * server: parse the c1s1, discovery the key and digest by schema. |
| 226 | * use the c1_validate_digest() to valid the digest of c1. | 227 | * use the c1_validate_digest() to valid the digest of c1. |
| 227 | */ | 228 | */ |
| 228 | - virtual int parse(char* _c1s1) = 0; | 229 | + virtual int parse(char* _c1s1, int size) = 0; |
| 229 | public: | 230 | public: |
| 230 | /** | 231 | /** |
| 231 | * client: create and sign c1 by schema. | 232 | * client: create and sign c1 by schema. |
| @@ -261,10 +262,10 @@ namespace _srs_internal | @@ -261,10 +262,10 @@ namespace _srs_internal | ||
| 261 | /** | 262 | /** |
| 262 | * copy whole c1s1 to bytes. | 263 | * copy whole c1s1 to bytes. |
| 263 | */ | 264 | */ |
| 264 | - virtual void copy_to(c1s1* owner, char* bytes, bool with_digest) = 0; | ||
| 265 | - virtual void copy_time_version(char*& pp, c1s1* owner); | ||
| 266 | - virtual void copy_key(char*& pp); | ||
| 267 | - virtual void digest_key(char*& pp, bool with_digest); | 265 | + virtual int copy_to(c1s1* owner, char* bytes, int size, bool with_digest) = 0; |
| 266 | + virtual void copy_time_version(SrsStream* stream, c1s1* owner); | ||
| 267 | + virtual void copy_key(SrsStream* stream); | ||
| 268 | + virtual void copy_digest(SrsStream* stream, bool with_digest); | ||
| 268 | }; | 269 | }; |
| 269 | 270 | ||
| 270 | /** | 271 | /** |
| @@ -279,12 +280,9 @@ namespace _srs_internal | @@ -279,12 +280,9 @@ namespace _srs_internal | ||
| 279 | virtual ~c1s1_strategy_schema0(); | 280 | virtual ~c1s1_strategy_schema0(); |
| 280 | public: | 281 | public: |
| 281 | virtual srs_schema_type schema(); | 282 | virtual srs_schema_type schema(); |
| 282 | - virtual int parse(char* _c1s1); | 283 | + virtual int parse(char* _c1s1, int size); |
| 283 | private: | 284 | private: |
| 284 | - /** | ||
| 285 | - * copy whole c1s1 to bytes. | ||
| 286 | - */ | ||
| 287 | - virtual void copy_to(c1s1* owner, char* bytes, bool with_digest); | 285 | + virtual int copy_to(c1s1* owner, char* bytes, int size, bool with_digest); |
| 288 | }; | 286 | }; |
| 289 | 287 | ||
| 290 | /** | 288 | /** |
| @@ -299,12 +297,9 @@ namespace _srs_internal | @@ -299,12 +297,9 @@ namespace _srs_internal | ||
| 299 | virtual ~c1s1_strategy_schema1(); | 297 | virtual ~c1s1_strategy_schema1(); |
| 300 | public: | 298 | public: |
| 301 | virtual srs_schema_type schema(); | 299 | virtual srs_schema_type schema(); |
| 302 | - virtual int parse(char* _c1s1); | 300 | + virtual int parse(char* _c1s1, int size); |
| 303 | private: | 301 | private: |
| 304 | - /** | ||
| 305 | - * copy whole c1s1 to bytes. | ||
| 306 | - */ | ||
| 307 | - virtual void copy_to(c1s1* owner, char* bytes, bool with_digest); | 302 | + virtual int copy_to(c1s1* owner, char* bytes, int size, bool with_digest); |
| 308 | }; | 303 | }; |
| 309 | 304 | ||
| 310 | /** | 305 | /** |
| @@ -345,13 +340,13 @@ namespace _srs_internal | @@ -345,13 +340,13 @@ namespace _srs_internal | ||
| 345 | /** | 340 | /** |
| 346 | * copy to bytes. | 341 | * copy to bytes. |
| 347 | */ | 342 | */ |
| 348 | - virtual void dump(char* _c1s1); | 343 | + virtual int dump(char* _c1s1, int size); |
| 349 | /** | 344 | /** |
| 350 | * server: parse the c1s1, discovery the key and digest by schema. | 345 | * server: parse the c1s1, discovery the key and digest by schema. |
| 351 | * use the c1_validate_digest() to valid the digest of c1. | 346 | * use the c1_validate_digest() to valid the digest of c1. |
| 352 | * use the s1_validate_digest() to valid the digest of s1. | 347 | * use the s1_validate_digest() to valid the digest of s1. |
| 353 | */ | 348 | */ |
| 354 | - virtual int parse(char* _c1s1, srs_schema_type _schema); | 349 | + virtual int parse(char* _c1s1, int size, srs_schema_type _schema); |
| 355 | public: | 350 | public: |
| 356 | /** | 351 | /** |
| 357 | * client: create and sign c1 by schema. | 352 | * client: create and sign c1 by schema. |
| @@ -425,11 +420,11 @@ namespace _srs_internal | @@ -425,11 +420,11 @@ namespace _srs_internal | ||
| 425 | /** | 420 | /** |
| 426 | * copy to bytes. | 421 | * copy to bytes. |
| 427 | */ | 422 | */ |
| 428 | - virtual void dump(char* _c2s2); | 423 | + virtual int dump(char* _c2s2, int size); |
| 429 | /** | 424 | /** |
| 430 | * parse the c2s2 | 425 | * parse the c2s2 |
| 431 | */ | 426 | */ |
| 432 | - virtual void parse(char* _c2s2); | 427 | + virtual int parse(char* _c2s2, int size); |
| 433 | public: | 428 | public: |
| 434 | /** | 429 | /** |
| 435 | * create c2. | 430 | * create c2. |
-
请 注册 或 登录 后发表评论