正在显示
2 个修改的文件
包含
148 行增加
和
356 行删除
| @@ -515,184 +515,29 @@ namespace _srs_internal | @@ -515,184 +515,29 @@ namespace _srs_internal | ||
| 515 | } | 515 | } |
| 516 | } | 516 | } |
| 517 | 517 | ||
| 518 | - void __srs_time_copy_to(char*& pp, int32_t time) | ||
| 519 | - { | ||
| 520 | - // 4bytes time | ||
| 521 | - __srs_stream_write_4bytes(pp, time); | ||
| 522 | - pp += 4; | ||
| 523 | - } | ||
| 524 | - void __srs_version_copy_to(char*& pp, int32_t version) | ||
| 525 | - { | ||
| 526 | - // 4bytes version | ||
| 527 | - __srs_stream_write_4bytes(pp, version); | ||
| 528 | - pp += 4; | ||
| 529 | - } | ||
| 530 | - void __srs_key_copy_to(char*& pp, key_block* key) | ||
| 531 | - { | ||
| 532 | - // 764bytes key block | ||
| 533 | - if (key->random0_size > 0) { | ||
| 534 | - memcpy(pp, key->random0, key->random0_size); | ||
| 535 | - } | ||
| 536 | - pp += key->random0_size; | ||
| 537 | - | ||
| 538 | - memcpy(pp, key->key, sizeof(key->key)); | ||
| 539 | - pp += sizeof(key->key); | ||
| 540 | - | ||
| 541 | - if (key->random1_size > 0) { | ||
| 542 | - memcpy(pp, key->random1, key->random1_size); | ||
| 543 | - } | ||
| 544 | - pp += key->random1_size; | ||
| 545 | - | ||
| 546 | - __srs_stream_write_4bytes(pp, key->offset); | ||
| 547 | - pp += 4; | ||
| 548 | - } | ||
| 549 | - void __srs_digest_copy_to(char*& pp, digest_block* digest, bool with_digest) | ||
| 550 | - { | ||
| 551 | - // 732bytes digest block without the 32bytes digest-data | ||
| 552 | - // nbytes digest block part1 | ||
| 553 | - __srs_stream_write_4bytes(pp, digest->offset); | ||
| 554 | - pp += 4; | ||
| 555 | - | ||
| 556 | - // digest random padding. | ||
| 557 | - if (digest->random0_size > 0) { | ||
| 558 | - memcpy(pp, digest->random0, digest->random0_size); | ||
| 559 | - } | ||
| 560 | - pp += digest->random0_size; | ||
| 561 | - | ||
| 562 | - // digest | ||
| 563 | - if (with_digest) { | ||
| 564 | - memcpy(pp, digest->digest, 32); | ||
| 565 | - pp += 32; | ||
| 566 | - } | ||
| 567 | - | ||
| 568 | - // nbytes digest block part2 | ||
| 569 | - if (digest->random1_size > 0) { | ||
| 570 | - memcpy(pp, digest->random1, digest->random1_size); | ||
| 571 | - } | ||
| 572 | - pp += digest->random1_size; | ||
| 573 | - } | ||
| 574 | - | ||
| 575 | - /** | ||
| 576 | - * copy whole c1s1 to bytes. | ||
| 577 | - */ | ||
| 578 | - void srs_schema0_copy_to(char* bytes, bool with_digest, | ||
| 579 | - int32_t time, int32_t version, key_block* key, digest_block* digest) | ||
| 580 | - { | ||
| 581 | - char* pp = bytes; | ||
| 582 | - | ||
| 583 | - __srs_time_copy_to(pp, time); | ||
| 584 | - __srs_version_copy_to(pp, version); | ||
| 585 | - __srs_key_copy_to(pp, key); | ||
| 586 | - __srs_digest_copy_to(pp, digest, with_digest); | ||
| 587 | - | ||
| 588 | - if (with_digest) { | ||
| 589 | - srs_assert(pp - bytes == 1536); | ||
| 590 | - } else { | ||
| 591 | - srs_assert(pp - bytes == 1536 - 32); | ||
| 592 | - } | ||
| 593 | - } | ||
| 594 | - void srs_schema1_copy_to(char* bytes, bool with_digest, | ||
| 595 | - int32_t time, int32_t version, digest_block* digest, key_block* key) | ||
| 596 | - { | ||
| 597 | - char* pp = bytes; | ||
| 598 | - | ||
| 599 | - __srs_time_copy_to(pp, time); | ||
| 600 | - __srs_version_copy_to(pp, version); | ||
| 601 | - __srs_digest_copy_to(pp, digest, with_digest); | ||
| 602 | - __srs_key_copy_to(pp, key); | ||
| 603 | - | ||
| 604 | - if (with_digest) { | ||
| 605 | - srs_assert(pp - bytes == 1536); | ||
| 606 | - } else { | ||
| 607 | - srs_assert(pp - bytes == 1536 - 32); | ||
| 608 | - } | ||
| 609 | - } | ||
| 610 | - | ||
| 611 | - /** | ||
| 612 | - * c1s1 is splited by digest: | ||
| 613 | - * c1s1-part1: n bytes (time, version, key and digest-part1). | ||
| 614 | - * digest-data: 32bytes | ||
| 615 | - * c1s1-part2: (1536-n-32)bytes (digest-part2) | ||
| 616 | - */ | ||
| 617 | - char* srs_bytes_join_schema0(int32_t time, int32_t version, key_block* key, digest_block* digest) | ||
| 618 | - { | ||
| 619 | - char* bytes = new char[1536 -32]; | ||
| 620 | - | ||
| 621 | - srs_schema0_copy_to(bytes, false, time, version, key, digest); | ||
| 622 | - | ||
| 623 | - return bytes; | ||
| 624 | - } | ||
| 625 | - | ||
| 626 | - /** | ||
| 627 | - * c1s1 is splited by digest: | ||
| 628 | - * c1s1-part1: n bytes (time, version and digest-part1). | ||
| 629 | - * digest-data: 32bytes | ||
| 630 | - * c1s1-part2: (1536-n-32)bytes (digest-part2 and key) | ||
| 631 | - */ | ||
| 632 | - char* srs_bytes_join_schema1(int32_t time, int32_t version, digest_block* digest, key_block* key) | ||
| 633 | - { | ||
| 634 | - char* bytes = new char[1536 -32]; | ||
| 635 | - | ||
| 636 | - srs_schema1_copy_to(bytes, false, time, version, digest, key); | ||
| 637 | - | ||
| 638 | - return bytes; | ||
| 639 | - } | ||
| 640 | - | ||
| 641 | c1s1_strategy::c1s1_strategy() | 518 | c1s1_strategy::c1s1_strategy() |
| 642 | { | 519 | { |
| 643 | - } | ||
| 644 | - | ||
| 645 | - c1s1_strategy::~c1s1_strategy() | ||
| 646 | - { | ||
| 647 | - } | ||
| 648 | - | ||
| 649 | - c1s1_strategy_schema0::c1s1_strategy_schema0() | ||
| 650 | - { | ||
| 651 | key.init(); | 520 | key.init(); |
| 652 | digest.init(); | 521 | digest.init(); |
| 653 | } | 522 | } |
| 654 | 523 | ||
| 655 | - c1s1_strategy_schema0::~c1s1_strategy_schema0() | 524 | + c1s1_strategy::~c1s1_strategy() |
| 656 | { | 525 | { |
| 657 | key.free(); | 526 | key.free(); |
| 658 | digest.free(); | 527 | digest.free(); |
| 659 | } | 528 | } |
| 660 | 529 | ||
| 661 | - srs_schema_type c1s1_strategy_schema0::schema() | ||
| 662 | - { | ||
| 663 | - return srs_schema0; | ||
| 664 | - } | ||
| 665 | - | ||
| 666 | - char* c1s1_strategy_schema0::get_digest() | 530 | + char* c1s1_strategy::get_digest() |
| 667 | { | 531 | { |
| 668 | return digest.digest; | 532 | return digest.digest; |
| 669 | } | 533 | } |
| 670 | 534 | ||
| 671 | - void c1s1_strategy_schema0::dump(c1s1* owner, char* _c1s1) | 535 | + void c1s1_strategy::dump(c1s1* owner, char* _c1s1) |
| 672 | { | 536 | { |
| 673 | - srs_schema0_copy_to(_c1s1, true, owner->time, owner->version, &key, &digest); | 537 | + copy_to(owner, _c1s1, true); |
| 674 | } | 538 | } |
| 675 | 539 | ||
| 676 | - int c1s1_strategy_schema0::parse(char* _c1s1) | ||
| 677 | - { | ||
| 678 | - int ret = ERROR_SUCCESS; | ||
| 679 | - | ||
| 680 | - if ((ret = key.parse(_c1s1 + 8)) != ERROR_SUCCESS) { | ||
| 681 | - srs_error("parse the c1 key failed. ret=%d", ret); | ||
| 682 | - return ret; | ||
| 683 | - } | ||
| 684 | - | ||
| 685 | - if ((ret = digest.parse(_c1s1 + 8 + 764)) != ERROR_SUCCESS) { | ||
| 686 | - srs_error("parse the c1 digest failed. ret=%d", ret); | ||
| 687 | - return ret; | ||
| 688 | - } | ||
| 689 | - | ||
| 690 | - srs_verbose("parse c1 key-digest success"); | ||
| 691 | - | ||
| 692 | - return ret; | ||
| 693 | - } | ||
| 694 | - | ||
| 695 | - int c1s1_strategy_schema0::c1_create(c1s1* owner) | 540 | + int c1s1_strategy::c1_create(c1s1* owner) |
| 696 | { | 541 | { |
| 697 | int ret = ERROR_SUCCESS; | 542 | int ret = ERROR_SUCCESS; |
| 698 | 543 | ||
| @@ -712,7 +557,7 @@ namespace _srs_internal | @@ -712,7 +557,7 @@ namespace _srs_internal | ||
| 712 | return ret; | 557 | return ret; |
| 713 | } | 558 | } |
| 714 | 559 | ||
| 715 | - int c1s1_strategy_schema0::c1_validate_digest(c1s1* owner, bool& is_valid) | 560 | + int c1s1_strategy::c1_validate_digest(c1s1* owner, bool& is_valid) |
| 716 | { | 561 | { |
| 717 | int ret = ERROR_SUCCESS; | 562 | int ret = ERROR_SUCCESS; |
| 718 | 563 | ||
| @@ -731,7 +576,7 @@ namespace _srs_internal | @@ -731,7 +576,7 @@ namespace _srs_internal | ||
| 731 | return ret; | 576 | return ret; |
| 732 | } | 577 | } |
| 733 | 578 | ||
| 734 | - int c1s1_strategy_schema0::s1_create(c1s1* owner) | 579 | + int c1s1_strategy::s1_create(c1s1* owner) |
| 735 | { | 580 | { |
| 736 | int ret = ERROR_SUCCESS; | 581 | int ret = ERROR_SUCCESS; |
| 737 | 582 | ||
| @@ -769,7 +614,7 @@ namespace _srs_internal | @@ -769,7 +614,7 @@ namespace _srs_internal | ||
| 769 | return ret; | 614 | return ret; |
| 770 | } | 615 | } |
| 771 | 616 | ||
| 772 | - int c1s1_strategy_schema0::s1_validate_digest(c1s1* owner, bool& is_valid) | 617 | + int c1s1_strategy::s1_validate_digest(c1s1* owner, bool& is_valid) |
| 773 | { | 618 | { |
| 774 | int ret = ERROR_SUCCESS; | 619 | int ret = ERROR_SUCCESS; |
| 775 | 620 | ||
| @@ -788,16 +633,20 @@ namespace _srs_internal | @@ -788,16 +633,20 @@ namespace _srs_internal | ||
| 788 | return ret; | 633 | return ret; |
| 789 | } | 634 | } |
| 790 | 635 | ||
| 791 | - int c1s1_strategy_schema0::calc_c1_digest(c1s1* owner, char*& c1_digest) | 636 | + int c1s1_strategy::calc_c1_digest(c1s1* owner, char*& c1_digest) |
| 792 | { | 637 | { |
| 793 | int ret = ERROR_SUCCESS; | 638 | int ret = ERROR_SUCCESS; |
| 794 | 639 | ||
| 795 | - char* c1s1_joined_bytes = NULL; | ||
| 796 | - | ||
| 797 | - c1s1_joined_bytes = srs_bytes_join_schema0(owner->time, owner->version, &key, &digest); | ||
| 798 | - | ||
| 799 | - srs_assert(c1s1_joined_bytes != NULL); | 640 | + /** |
| 641 | + * c1s1 is splited by digest: | ||
| 642 | + * c1s1-part1: n bytes (time, version, key and digest-part1). | ||
| 643 | + * digest-data: 32bytes | ||
| 644 | + * c1s1-part2: (1536-n-32)bytes (digest-part2) | ||
| 645 | + * @return a new allocated bytes, user must free it. | ||
| 646 | + */ | ||
| 647 | + char* c1s1_joined_bytes = new char[1536 -32]; | ||
| 800 | SrsAutoFree(char, c1s1_joined_bytes); | 648 | SrsAutoFree(char, c1s1_joined_bytes); |
| 649 | + copy_to(owner, c1s1_joined_bytes, false); | ||
| 801 | 650 | ||
| 802 | c1_digest = new char[__SRS_OpensslHashSize]; | 651 | c1_digest = new char[__SRS_OpensslHashSize]; |
| 803 | if ((ret = openssl_HMACsha256(SrsGenuineFPKey, 30, c1s1_joined_bytes, 1536 - 32, c1_digest)) != ERROR_SUCCESS) { | 652 | if ((ret = openssl_HMACsha256(SrsGenuineFPKey, 30, c1s1_joined_bytes, 1536 - 32, c1_digest)) != ERROR_SUCCESS) { |
| @@ -810,16 +659,20 @@ namespace _srs_internal | @@ -810,16 +659,20 @@ namespace _srs_internal | ||
| 810 | return ret; | 659 | return ret; |
| 811 | } | 660 | } |
| 812 | 661 | ||
| 813 | - int c1s1_strategy_schema0::calc_s1_digest(c1s1* owner, char*& s1_digest) | 662 | + int c1s1_strategy::calc_s1_digest(c1s1* owner, char*& s1_digest) |
| 814 | { | 663 | { |
| 815 | int ret = ERROR_SUCCESS; | 664 | int ret = ERROR_SUCCESS; |
| 816 | 665 | ||
| 817 | - char* c1s1_joined_bytes = NULL; | ||
| 818 | - | ||
| 819 | - c1s1_joined_bytes = srs_bytes_join_schema0(owner->time, owner->version, &key, &digest); | ||
| 820 | - | ||
| 821 | - srs_assert(c1s1_joined_bytes != NULL); | 666 | + /** |
| 667 | + * c1s1 is splited by digest: | ||
| 668 | + * c1s1-part1: n bytes (time, version, key and digest-part1). | ||
| 669 | + * digest-data: 32bytes | ||
| 670 | + * c1s1-part2: (1536-n-32)bytes (digest-part2) | ||
| 671 | + * @return a new allocated bytes, user must free it. | ||
| 672 | + */ | ||
| 673 | + char* c1s1_joined_bytes = new char[1536 -32]; | ||
| 822 | SrsAutoFree(char, c1s1_joined_bytes); | 674 | SrsAutoFree(char, c1s1_joined_bytes); |
| 675 | + copy_to(owner, c1s1_joined_bytes, false); | ||
| 823 | 676 | ||
| 824 | s1_digest = new char[__SRS_OpensslHashSize]; | 677 | s1_digest = new char[__SRS_OpensslHashSize]; |
| 825 | if ((ret = openssl_HMACsha256(SrsGenuineFMSKey, 36, c1s1_joined_bytes, 1536 - 32, s1_digest)) != ERROR_SUCCESS) { | 678 | if ((ret = openssl_HMACsha256(SrsGenuineFMSKey, 36, c1s1_joined_bytes, 1536 - 32, s1_digest)) != ERROR_SUCCESS) { |
| @@ -832,189 +685,152 @@ namespace _srs_internal | @@ -832,189 +685,152 @@ namespace _srs_internal | ||
| 832 | return ret; | 685 | return ret; |
| 833 | } | 686 | } |
| 834 | 687 | ||
| 835 | - c1s1_strategy_schema1::c1s1_strategy_schema1() | 688 | + void c1s1_strategy::copy_time_version(char*& pp, c1s1* owner) |
| 836 | { | 689 | { |
| 837 | - key.init(); | ||
| 838 | - digest.init(); | 690 | + // 4bytes time |
| 691 | + __srs_stream_write_4bytes(pp, owner->time); | ||
| 692 | + pp += 4; | ||
| 693 | + // 4bytes version | ||
| 694 | + __srs_stream_write_4bytes(pp, owner->version); | ||
| 695 | + pp += 4; | ||
| 839 | } | 696 | } |
| 840 | - | ||
| 841 | - c1s1_strategy_schema1::~c1s1_strategy_schema1() | 697 | + void c1s1_strategy::copy_key(char*& pp) |
| 842 | { | 698 | { |
| 843 | - key.free(); | ||
| 844 | - digest.free(); | 699 | + // 764bytes key block |
| 700 | + if (key.random0_size > 0) { | ||
| 701 | + memcpy(pp, key.random0, key.random0_size); | ||
| 845 | } | 702 | } |
| 703 | + pp += key.random0_size; | ||
| 846 | 704 | ||
| 847 | - srs_schema_type c1s1_strategy_schema1::schema() | ||
| 848 | - { | ||
| 849 | - return srs_schema1; | ||
| 850 | - } | 705 | + memcpy(pp, key.key, sizeof(key.key)); |
| 706 | + pp += sizeof(key.key); | ||
| 851 | 707 | ||
| 852 | - char* c1s1_strategy_schema1::get_digest() | ||
| 853 | - { | ||
| 854 | - return digest.digest; | 708 | + if (key.random1_size > 0) { |
| 709 | + memcpy(pp, key.random1, key.random1_size); | ||
| 855 | } | 710 | } |
| 711 | + pp += key.random1_size; | ||
| 856 | 712 | ||
| 857 | - void c1s1_strategy_schema1::dump(c1s1* owner, char* _c1s1) | ||
| 858 | - { | ||
| 859 | - srs_schema0_copy_to(_c1s1, true, owner->time, owner->version, &key, &digest); | 713 | + __srs_stream_write_4bytes(pp, key.offset); |
| 714 | + pp += 4; | ||
| 860 | } | 715 | } |
| 861 | - | ||
| 862 | - int c1s1_strategy_schema1::parse(char* _c1s1) | 716 | + void c1s1_strategy::digest_key(char*& pp, bool with_digest) |
| 863 | { | 717 | { |
| 864 | - int ret = ERROR_SUCCESS; | 718 | + // 732bytes digest block without the 32bytes digest-data |
| 719 | + // nbytes digest block part1 | ||
| 720 | + __srs_stream_write_4bytes(pp, digest.offset); | ||
| 721 | + pp += 4; | ||
| 865 | 722 | ||
| 866 | - if ((ret = digest.parse(_c1s1 + 8)) != ERROR_SUCCESS) { | ||
| 867 | - srs_error("parse the c1 digest failed. ret=%d", ret); | ||
| 868 | - return ret; | 723 | + // digest random padding. |
| 724 | + if (digest.random0_size > 0) { | ||
| 725 | + memcpy(pp, digest.random0, digest.random0_size); | ||
| 869 | } | 726 | } |
| 727 | + pp += digest.random0_size; | ||
| 870 | 728 | ||
| 871 | - if ((ret = key.parse(_c1s1 + 8 + 764)) != ERROR_SUCCESS) { | ||
| 872 | - srs_error("parse the c1 key failed. ret=%d", ret); | ||
| 873 | - return ret; | 729 | + // digest |
| 730 | + if (with_digest) { | ||
| 731 | + memcpy(pp, digest.digest, 32); | ||
| 732 | + pp += 32; | ||
| 874 | } | 733 | } |
| 875 | 734 | ||
| 876 | - srs_verbose("parse c1 digest-key success"); | ||
| 877 | - | ||
| 878 | - return ret; | 735 | + // nbytes digest block part2 |
| 736 | + if (digest.random1_size > 0) { | ||
| 737 | + memcpy(pp, digest.random1, digest.random1_size); | ||
| 738 | + } | ||
| 739 | + pp += digest.random1_size; | ||
| 879 | } | 740 | } |
| 880 | 741 | ||
| 881 | - int c1s1_strategy_schema1::c1_create(c1s1* owner) | 742 | + c1s1_strategy_schema0::c1s1_strategy_schema0() |
| 882 | { | 743 | { |
| 883 | - int ret = ERROR_SUCCESS; | ||
| 884 | - | ||
| 885 | - // generate digest | ||
| 886 | - char* c1_digest = NULL; | ||
| 887 | - | ||
| 888 | - if ((ret = calc_c1_digest(owner, c1_digest)) != ERROR_SUCCESS) { | ||
| 889 | - srs_error("sign c1 error, failed to calc digest. ret=%d", ret); | ||
| 890 | - return ret; | ||
| 891 | } | 744 | } |
| 892 | 745 | ||
| 893 | - srs_assert(c1_digest != NULL); | ||
| 894 | - SrsAutoFree(char, c1_digest); | ||
| 895 | - | ||
| 896 | - memcpy(digest.digest, c1_digest, 32); | 746 | + c1s1_strategy_schema0::~c1s1_strategy_schema0() |
| 747 | + { | ||
| 748 | + } | ||
| 897 | 749 | ||
| 898 | - return ret; | 750 | + srs_schema_type c1s1_strategy_schema0::schema() |
| 751 | + { | ||
| 752 | + return srs_schema0; | ||
| 899 | } | 753 | } |
| 900 | 754 | ||
| 901 | - int c1s1_strategy_schema1::c1_validate_digest(c1s1* owner, bool& is_valid) | 755 | + int c1s1_strategy_schema0::parse(char* _c1s1) |
| 902 | { | 756 | { |
| 903 | int ret = ERROR_SUCCESS; | 757 | int ret = ERROR_SUCCESS; |
| 904 | 758 | ||
| 905 | - char* c1_digest = NULL; | ||
| 906 | - | ||
| 907 | - if ((ret = calc_c1_digest(owner, c1_digest)) != ERROR_SUCCESS) { | ||
| 908 | - srs_error("validate c1 error, failed to calc digest. ret=%d", ret); | 759 | + if ((ret = key.parse(_c1s1 + 8)) != ERROR_SUCCESS) { |
| 760 | + srs_error("parse the c1 key failed. ret=%d", ret); | ||
| 909 | return ret; | 761 | return ret; |
| 910 | } | 762 | } |
| 911 | 763 | ||
| 912 | - srs_assert(c1_digest != NULL); | ||
| 913 | - SrsAutoFree(char, c1_digest); | 764 | + if ((ret = digest.parse(_c1s1 + 8 + 764)) != ERROR_SUCCESS) { |
| 765 | + srs_error("parse the c1 digest failed. ret=%d", ret); | ||
| 766 | + return ret; | ||
| 767 | + } | ||
| 914 | 768 | ||
| 915 | - is_valid = srs_bytes_equals(digest.digest, c1_digest, 32); | 769 | + srs_verbose("parse c1 key-digest success"); |
| 916 | 770 | ||
| 917 | return ret; | 771 | return ret; |
| 918 | } | 772 | } |
| 919 | 773 | ||
| 920 | - int c1s1_strategy_schema1::s1_create(c1s1* owner) | 774 | + void c1s1_strategy_schema0::copy_to(c1s1* owner, char* bytes, bool with_digest) |
| 921 | { | 775 | { |
| 922 | - int ret = ERROR_SUCCESS; | 776 | + char* pp = bytes; |
| 923 | 777 | ||
| 924 | - SrsDH dh; | 778 | + copy_time_version(pp, owner); |
| 779 | + copy_key(pp); | ||
| 780 | + digest_key(pp, with_digest); | ||
| 925 | 781 | ||
| 926 | - // ensure generate 128bytes public key. | ||
| 927 | - if ((ret = dh.initialize(true)) != ERROR_SUCCESS) { | ||
| 928 | - return ret; | 782 | + if (with_digest) { |
| 783 | + srs_assert(pp - bytes == 1536); | ||
| 784 | + } else { | ||
| 785 | + srs_assert(pp - bytes == 1536 - 32); | ||
| 929 | } | 786 | } |
| 930 | - | ||
| 931 | - // directly generate the public key. | ||
| 932 | - // @see: https://github.com/winlinvip/simple-rtmp-server/issues/148 | ||
| 933 | - int pkey_size = 128; | ||
| 934 | - if ((ret = dh.copy_public_key(key.key, pkey_size)) != ERROR_SUCCESS) { | ||
| 935 | - srs_error("calc s1 key failed. ret=%d", ret); | ||
| 936 | - return ret; | ||
| 937 | } | 787 | } |
| 938 | - srs_assert(pkey_size == 128); | ||
| 939 | - srs_verbose("calc s1 key success."); | ||
| 940 | 788 | ||
| 941 | - char* s1_digest = NULL; | ||
| 942 | - if ((ret = calc_s1_digest(owner, s1_digest)) != ERROR_SUCCESS) { | ||
| 943 | - srs_error("calc s1 digest failed. ret=%d", ret); | ||
| 944 | - return ret; | 789 | + c1s1_strategy_schema1::c1s1_strategy_schema1() |
| 790 | + { | ||
| 945 | } | 791 | } |
| 946 | - srs_verbose("calc s1 digest success."); | ||
| 947 | 792 | ||
| 948 | - srs_assert(s1_digest != NULL); | ||
| 949 | - SrsAutoFree(char, s1_digest); | ||
| 950 | - | ||
| 951 | - memcpy(digest.digest, s1_digest, 32); | ||
| 952 | - srs_verbose("copy s1 key success."); | 793 | + c1s1_strategy_schema1::~c1s1_strategy_schema1() |
| 794 | + { | ||
| 795 | + } | ||
| 953 | 796 | ||
| 954 | - return ret; | 797 | + srs_schema_type c1s1_strategy_schema1::schema() |
| 798 | + { | ||
| 799 | + return srs_schema1; | ||
| 955 | } | 800 | } |
| 956 | 801 | ||
| 957 | - int c1s1_strategy_schema1::s1_validate_digest(c1s1* owner, bool& is_valid) | 802 | + int c1s1_strategy_schema1::parse(char* _c1s1) |
| 958 | { | 803 | { |
| 959 | int ret = ERROR_SUCCESS; | 804 | int ret = ERROR_SUCCESS; |
| 960 | 805 | ||
| 961 | - char* s1_digest = NULL; | ||
| 962 | - | ||
| 963 | - if ((ret = calc_s1_digest(owner, s1_digest)) != ERROR_SUCCESS) { | ||
| 964 | - srs_error("validate s1 error, failed to calc digest. ret=%d", ret); | 806 | + if ((ret = digest.parse(_c1s1 + 8)) != ERROR_SUCCESS) { |
| 807 | + srs_error("parse the c1 digest failed. ret=%d", ret); | ||
| 965 | return ret; | 808 | return ret; |
| 966 | } | 809 | } |
| 967 | 810 | ||
| 968 | - srs_assert(s1_digest != NULL); | ||
| 969 | - SrsAutoFree(char, s1_digest); | ||
| 970 | - | ||
| 971 | - is_valid = srs_bytes_equals(digest.digest, s1_digest, 32); | ||
| 972 | - | 811 | + if ((ret = key.parse(_c1s1 + 8 + 764)) != ERROR_SUCCESS) { |
| 812 | + srs_error("parse the c1 key failed. ret=%d", ret); | ||
| 973 | return ret; | 813 | return ret; |
| 974 | } | 814 | } |
| 975 | 815 | ||
| 976 | - int c1s1_strategy_schema1::calc_c1_digest(c1s1* owner, char*& c1_digest) | ||
| 977 | - { | ||
| 978 | - int ret = ERROR_SUCCESS; | ||
| 979 | - | ||
| 980 | - char* c1s1_joined_bytes = NULL; | ||
| 981 | - | ||
| 982 | - c1s1_joined_bytes = srs_bytes_join_schema1(owner->time, owner->version, &digest, &key); | ||
| 983 | - | ||
| 984 | - srs_assert(c1s1_joined_bytes != NULL); | ||
| 985 | - SrsAutoFree(char, c1s1_joined_bytes); | ||
| 986 | - | ||
| 987 | - c1_digest = new char[__SRS_OpensslHashSize]; | ||
| 988 | - if ((ret = openssl_HMACsha256(SrsGenuineFPKey, 30, c1s1_joined_bytes, 1536 - 32, c1_digest)) != ERROR_SUCCESS) { | ||
| 989 | - srs_freep(c1_digest); | ||
| 990 | - srs_error("calc digest for c1 failed. ret=%d", ret); | ||
| 991 | - return ret; | ||
| 992 | - } | ||
| 993 | - srs_verbose("digest calculated for c1"); | 816 | + srs_verbose("parse c1 digest-key success"); |
| 994 | 817 | ||
| 995 | return ret; | 818 | return ret; |
| 996 | } | 819 | } |
| 997 | 820 | ||
| 998 | - int c1s1_strategy_schema1::calc_s1_digest(c1s1* owner, char*& s1_digest) | 821 | + void c1s1_strategy_schema1::copy_to(c1s1* owner, char* bytes, bool with_digest) |
| 999 | { | 822 | { |
| 1000 | - int ret = ERROR_SUCCESS; | ||
| 1001 | - | ||
| 1002 | - char* c1s1_joined_bytes = NULL; | 823 | + char* pp = bytes; |
| 1003 | 824 | ||
| 1004 | - c1s1_joined_bytes = srs_bytes_join_schema1(owner->time, owner->version, &digest, &key); | 825 | + copy_time_version(pp, owner); |
| 826 | + digest_key(pp, with_digest); | ||
| 827 | + copy_key(pp); | ||
| 1005 | 828 | ||
| 1006 | - srs_assert(c1s1_joined_bytes != NULL); | ||
| 1007 | - SrsAutoFree(char, c1s1_joined_bytes); | ||
| 1008 | - | ||
| 1009 | - s1_digest = new char[__SRS_OpensslHashSize]; | ||
| 1010 | - if ((ret = openssl_HMACsha256(SrsGenuineFMSKey, 36, c1s1_joined_bytes, 1536 - 32, s1_digest)) != ERROR_SUCCESS) { | ||
| 1011 | - srs_freep(s1_digest); | ||
| 1012 | - srs_error("calc digest for s1 failed. ret=%d", ret); | ||
| 1013 | - return ret; | 829 | + if (with_digest) { |
| 830 | + srs_assert(pp - bytes == 1536); | ||
| 831 | + } else { | ||
| 832 | + srs_assert(pp - bytes == 1536 - 32); | ||
| 1014 | } | 833 | } |
| 1015 | - srs_verbose("digest calculated for s1"); | ||
| 1016 | - | ||
| 1017 | - return ret; | ||
| 1018 | } | 834 | } |
| 1019 | 835 | ||
| 1020 | c2s2::c2s2() | 836 | c2s2::c2s2() |
| @@ -193,32 +193,6 @@ namespace _srs_internal | @@ -193,32 +193,6 @@ namespace _srs_internal | ||
| 193 | void free(); | 193 | void free(); |
| 194 | }; | 194 | }; |
| 195 | 195 | ||
| 196 | - /** | ||
| 197 | - * copy whole c1s1 to bytes. | ||
| 198 | - */ | ||
| 199 | - void srs_schema0_copy_to(char* bytes, bool with_digest, | ||
| 200 | - int32_t time, int32_t version, key_block* key, digest_block* digest); | ||
| 201 | - void srs_schema1_copy_to(char* bytes, bool with_digest, | ||
| 202 | - int32_t time, int32_t version, digest_block* digest, key_block* key); | ||
| 203 | - | ||
| 204 | - /** | ||
| 205 | - * c1s1 is splited by digest: | ||
| 206 | - * c1s1-part1: n bytes (time, version, key and digest-part1). | ||
| 207 | - * digest-data: 32bytes | ||
| 208 | - * c1s1-part2: (1536-n-32)bytes (digest-part2) | ||
| 209 | - * @return a new allocated bytes, user must free it. | ||
| 210 | - */ | ||
| 211 | - char* srs_bytes_join_schema0(int32_t time, int32_t version, key_block* key, digest_block* digest); | ||
| 212 | - | ||
| 213 | - /** | ||
| 214 | - * c1s1 is splited by digest: | ||
| 215 | - * c1s1-part1: n bytes (time, version and digest-part1). | ||
| 216 | - * digest-data: 32bytes | ||
| 217 | - * c1s1-part2: (1536-n-32)bytes (digest-part2 and key) | ||
| 218 | - * @return a new allocated bytes, user must free it. | ||
| 219 | - */ | ||
| 220 | - char* srs_bytes_join_schema1(int32_t time, int32_t version, digest_block* digest, key_block* key); | ||
| 221 | - | ||
| 222 | class c1s1; | 196 | class c1s1; |
| 223 | 197 | ||
| 224 | /** | 198 | /** |
| @@ -228,6 +202,9 @@ namespace _srs_internal | @@ -228,6 +202,9 @@ namespace _srs_internal | ||
| 228 | */ | 202 | */ |
| 229 | class c1s1_strategy | 203 | class c1s1_strategy |
| 230 | { | 204 | { |
| 205 | + protected: | ||
| 206 | + key_block key; | ||
| 207 | + digest_block digest; | ||
| 231 | public: | 208 | public: |
| 232 | c1s1_strategy(); | 209 | c1s1_strategy(); |
| 233 | virtual ~c1s1_strategy(); | 210 | virtual ~c1s1_strategy(); |
| @@ -239,11 +216,11 @@ namespace _srs_internal | @@ -239,11 +216,11 @@ namespace _srs_internal | ||
| 239 | /** | 216 | /** |
| 240 | * get the digest key. | 217 | * get the digest key. |
| 241 | */ | 218 | */ |
| 242 | - virtual char* get_digest() = 0; | 219 | + virtual char* get_digest(); |
| 243 | /** | 220 | /** |
| 244 | * copy to bytes. | 221 | * copy to bytes. |
| 245 | */ | 222 | */ |
| 246 | - virtual void dump(c1s1* owner, char* _c1s1) = 0; | 223 | + virtual void dump(c1s1* owner, char* _c1s1); |
| 247 | /** | 224 | /** |
| 248 | * server: parse the c1s1, discovery the key and digest by schema. | 225 | * server: parse the c1s1, discovery the key and digest by schema. |
| 249 | * use the c1_validate_digest() to valid the digest of c1. | 226 | * use the c1_validate_digest() to valid the digest of c1. |
| @@ -265,19 +242,29 @@ namespace _srs_internal | @@ -265,19 +242,29 @@ namespace _srs_internal | ||
| 265 | * digest-data = calc_c1_digest(c1, schema) | 242 | * digest-data = calc_c1_digest(c1, schema) |
| 266 | * copy digest-data to c1 | 243 | * copy digest-data to c1 |
| 267 | */ | 244 | */ |
| 268 | - virtual int c1_create(c1s1* owner) = 0; | 245 | + virtual int c1_create(c1s1* owner); |
| 269 | /** | 246 | /** |
| 270 | * server: validate the parsed c1 schema | 247 | * server: validate the parsed c1 schema |
| 271 | */ | 248 | */ |
| 272 | - virtual int c1_validate_digest(c1s1* owner, bool& is_valid) = 0; | 249 | + virtual int c1_validate_digest(c1s1* owner, bool& is_valid); |
| 273 | /** | 250 | /** |
| 274 | * server: create and sign the s1 from c1. | 251 | * server: create and sign the s1 from c1. |
| 275 | */ | 252 | */ |
| 276 | - virtual int s1_create(c1s1* owner) = 0; | 253 | + virtual int s1_create(c1s1* owner); |
| 277 | /** | 254 | /** |
| 278 | * server: validate the parsed s1 schema | 255 | * server: validate the parsed s1 schema |
| 279 | */ | 256 | */ |
| 280 | - virtual int s1_validate_digest(c1s1* owner, bool& is_valid) = 0; | 257 | + virtual int s1_validate_digest(c1s1* owner, bool& is_valid); |
| 258 | + protected: | ||
| 259 | + virtual int calc_c1_digest(c1s1* owner, char*& c1_digest); | ||
| 260 | + virtual int calc_s1_digest(c1s1* owner, char*& s1_digest); | ||
| 261 | + /** | ||
| 262 | + * copy whole c1s1 to bytes. | ||
| 263 | + */ | ||
| 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); | ||
| 281 | }; | 268 | }; |
| 282 | 269 | ||
| 283 | /** | 270 | /** |
| @@ -287,24 +274,17 @@ namespace _srs_internal | @@ -287,24 +274,17 @@ namespace _srs_internal | ||
| 287 | */ | 274 | */ |
| 288 | class c1s1_strategy_schema0 : public c1s1_strategy | 275 | class c1s1_strategy_schema0 : public c1s1_strategy |
| 289 | { | 276 | { |
| 290 | - private: | ||
| 291 | - key_block key; | ||
| 292 | - digest_block digest; | ||
| 293 | public: | 277 | public: |
| 294 | c1s1_strategy_schema0(); | 278 | c1s1_strategy_schema0(); |
| 295 | virtual ~c1s1_strategy_schema0(); | 279 | virtual ~c1s1_strategy_schema0(); |
| 296 | public: | 280 | public: |
| 297 | virtual srs_schema_type schema(); | 281 | virtual srs_schema_type schema(); |
| 298 | - virtual char* get_digest(); | ||
| 299 | - virtual void dump(c1s1* owner, char* _c1s1); | ||
| 300 | virtual int parse(char* _c1s1); | 282 | virtual int parse(char* _c1s1); |
| 301 | - virtual int c1_create(c1s1* owner); | ||
| 302 | - virtual int c1_validate_digest(c1s1* owner, bool& is_valid); | ||
| 303 | - virtual int s1_create(c1s1* owner); | ||
| 304 | - virtual int s1_validate_digest(c1s1* owner, bool& is_valid); | ||
| 305 | private: | 283 | private: |
| 306 | - virtual int calc_c1_digest(c1s1* owner, char*& c1_digest); | ||
| 307 | - virtual int calc_s1_digest(c1s1* owner, char*& s1_digest); | 284 | + /** |
| 285 | + * copy whole c1s1 to bytes. | ||
| 286 | + */ | ||
| 287 | + virtual void copy_to(c1s1* owner, char* bytes, bool with_digest); | ||
| 308 | }; | 288 | }; |
| 309 | 289 | ||
| 310 | /** | 290 | /** |
| @@ -314,24 +294,17 @@ namespace _srs_internal | @@ -314,24 +294,17 @@ namespace _srs_internal | ||
| 314 | */ | 294 | */ |
| 315 | class c1s1_strategy_schema1 : public c1s1_strategy | 295 | class c1s1_strategy_schema1 : public c1s1_strategy |
| 316 | { | 296 | { |
| 317 | - private: | ||
| 318 | - digest_block digest; | ||
| 319 | - key_block key; | ||
| 320 | public: | 297 | public: |
| 321 | c1s1_strategy_schema1(); | 298 | c1s1_strategy_schema1(); |
| 322 | virtual ~c1s1_strategy_schema1(); | 299 | virtual ~c1s1_strategy_schema1(); |
| 323 | public: | 300 | public: |
| 324 | virtual srs_schema_type schema(); | 301 | virtual srs_schema_type schema(); |
| 325 | - virtual char* get_digest(); | ||
| 326 | - virtual void dump(c1s1* owner, char* _c1s1); | ||
| 327 | virtual int parse(char* _c1s1); | 302 | virtual int parse(char* _c1s1); |
| 328 | - virtual int c1_create(c1s1* owner); | ||
| 329 | - virtual int c1_validate_digest(c1s1* owner, bool& is_valid); | ||
| 330 | - virtual int s1_create(c1s1* owner); | ||
| 331 | - virtual int s1_validate_digest(c1s1* owner, bool& is_valid); | ||
| 332 | private: | 303 | private: |
| 333 | - virtual int calc_c1_digest(c1s1* owner, char*& c1_digest); | ||
| 334 | - virtual int calc_s1_digest(c1s1* owner, char*& s1_digest); | 304 | + /** |
| 305 | + * copy whole c1s1 to bytes. | ||
| 306 | + */ | ||
| 307 | + virtual void copy_to(c1s1* owner, char* bytes, bool with_digest); | ||
| 335 | }; | 308 | }; |
| 336 | 309 | ||
| 337 | /** | 310 | /** |
| @@ -356,9 +329,10 @@ namespace _srs_internal | @@ -356,9 +329,10 @@ namespace _srs_internal | ||
| 356 | int32_t version; | 329 | int32_t version; |
| 357 | // 764bytes+764bytes | 330 | // 764bytes+764bytes |
| 358 | c1s1_strategy* payload; | 331 | c1s1_strategy* payload; |
| 359 | - | 332 | + public: |
| 360 | c1s1(); | 333 | c1s1(); |
| 361 | virtual ~c1s1(); | 334 | virtual ~c1s1(); |
| 335 | + public: | ||
| 362 | /** | 336 | /** |
| 363 | * get the scema. | 337 | * get the scema. |
| 364 | */ | 338 | */ |
| @@ -367,6 +341,7 @@ namespace _srs_internal | @@ -367,6 +341,7 @@ namespace _srs_internal | ||
| 367 | * get the digest key. | 341 | * get the digest key. |
| 368 | */ | 342 | */ |
| 369 | virtual char* get_digest(); | 343 | virtual char* get_digest(); |
| 344 | + public: | ||
| 370 | /** | 345 | /** |
| 371 | * copy to bytes. | 346 | * copy to bytes. |
| 372 | */ | 347 | */ |
| @@ -377,7 +352,7 @@ namespace _srs_internal | @@ -377,7 +352,7 @@ namespace _srs_internal | ||
| 377 | * use the s1_validate_digest() to valid the digest of s1. | 352 | * use the s1_validate_digest() to valid the digest of s1. |
| 378 | */ | 353 | */ |
| 379 | virtual int parse(char* _c1s1, srs_schema_type _schema); | 354 | virtual int parse(char* _c1s1, srs_schema_type _schema); |
| 380 | - | 355 | + public: |
| 381 | /** | 356 | /** |
| 382 | * client: create and sign c1 by schema. | 357 | * client: create and sign c1 by schema. |
| 383 | * sign the c1, generate the digest. | 358 | * sign the c1, generate the digest. |
| @@ -398,6 +373,7 @@ namespace _srs_internal | @@ -398,6 +373,7 @@ namespace _srs_internal | ||
| 398 | * server: validate the parsed c1 schema | 373 | * server: validate the parsed c1 schema |
| 399 | */ | 374 | */ |
| 400 | virtual int c1_validate_digest(bool& is_valid); | 375 | virtual int c1_validate_digest(bool& is_valid); |
| 376 | + public: | ||
| 401 | /** | 377 | /** |
| 402 | * server: create and sign the s1 from c1. | 378 | * server: create and sign the s1 from c1. |
| 403 | * // decode c1 try schema0 then schema1 | 379 | * // decode c1 try schema0 then schema1 |
| @@ -442,10 +418,10 @@ namespace _srs_internal | @@ -442,10 +418,10 @@ namespace _srs_internal | ||
| 442 | public: | 418 | public: |
| 443 | char random[1504]; | 419 | char random[1504]; |
| 444 | char digest[32]; | 420 | char digest[32]; |
| 445 | - | 421 | + public: |
| 446 | c2s2(); | 422 | c2s2(); |
| 447 | virtual ~c2s2(); | 423 | virtual ~c2s2(); |
| 448 | - | 424 | + public: |
| 449 | /** | 425 | /** |
| 450 | * copy to bytes. | 426 | * copy to bytes. |
| 451 | */ | 427 | */ |
| @@ -454,7 +430,7 @@ namespace _srs_internal | @@ -454,7 +430,7 @@ namespace _srs_internal | ||
| 454 | * parse the c2s2 | 430 | * parse the c2s2 |
| 455 | */ | 431 | */ |
| 456 | virtual void parse(char* _c2s2); | 432 | virtual void parse(char* _c2s2); |
| 457 | - | 433 | + public: |
| 458 | /** | 434 | /** |
| 459 | * create c2. | 435 | * create c2. |
| 460 | * random fill c2s2 1536 bytes | 436 | * random fill c2s2 1536 bytes |
| @@ -469,7 +445,7 @@ namespace _srs_internal | @@ -469,7 +445,7 @@ namespace _srs_internal | ||
| 469 | * validate the c2 from client. | 445 | * validate the c2 from client. |
| 470 | */ | 446 | */ |
| 471 | virtual int c2_validate(c1s1* s1, bool& is_valid); | 447 | virtual int c2_validate(c1s1* s1, bool& is_valid); |
| 472 | - | 448 | + public: |
| 473 | /** | 449 | /** |
| 474 | * create s2. | 450 | * create s2. |
| 475 | * random fill c2s2 1536 bytes | 451 | * random fill c2s2 1536 bytes |
-
请 注册 或 登录 后发表评论