winlin

for bug #235, refine code order of c1s1 and c2s2

@@ -833,127 +833,6 @@ namespace _srs_internal @@ -833,127 +833,6 @@ namespace _srs_internal
833 } 833 }
834 } 834 }
835 835
836 - c2s2::c2s2()  
837 - {  
838 - srs_random_generate(random, 1504);  
839 -  
840 - int size = snprintf(random, 1504, "%s", RTMP_SIG_SRS_HANDSHAKE);  
841 - srs_assert(++size < 1504);  
842 - snprintf(random + 1504 - size, size, "%s", RTMP_SIG_SRS_HANDSHAKE);  
843 -  
844 - srs_random_generate(digest, 32);  
845 - }  
846 -  
847 - c2s2::~c2s2()  
848 - {  
849 - }  
850 -  
851 - void c2s2::dump(char* _c2s2)  
852 - {  
853 - memcpy(_c2s2, random, 1504);  
854 - memcpy(_c2s2 + 1504, digest, 32);  
855 - }  
856 -  
857 - void c2s2::parse(char* _c2s2)  
858 - {  
859 - memcpy(random, _c2s2, 1504);  
860 - memcpy(digest, _c2s2 + 1504, 32);  
861 - }  
862 -  
863 - int c2s2::c2_create(c1s1* s1)  
864 - {  
865 - int ret = ERROR_SUCCESS;  
866 -  
867 - char temp_key[__SRS_OpensslHashSize];  
868 - if ((ret = openssl_HMACsha256(SrsGenuineFPKey, 62, s1->get_digest(), 32, temp_key)) != ERROR_SUCCESS) {  
869 - srs_error("create c2 temp key failed. ret=%d", ret);  
870 - return ret;  
871 - }  
872 - srs_verbose("generate c2 temp key success.");  
873 -  
874 - char _digest[__SRS_OpensslHashSize];  
875 - if ((ret = openssl_HMACsha256(temp_key, 32, random, 1504, _digest)) != ERROR_SUCCESS) {  
876 - srs_error("create c2 digest failed. ret=%d", ret);  
877 - return ret;  
878 - }  
879 - srs_verbose("generate c2 digest success.");  
880 -  
881 - memcpy(digest, _digest, 32);  
882 -  
883 - return ret;  
884 - }  
885 -  
886 - int c2s2::c2_validate(c1s1* s1, bool& is_valid)  
887 - {  
888 - is_valid = false;  
889 - int ret = ERROR_SUCCESS;  
890 -  
891 - char temp_key[__SRS_OpensslHashSize];  
892 - if ((ret = openssl_HMACsha256(SrsGenuineFPKey, 62, s1->get_digest(), 32, temp_key)) != ERROR_SUCCESS) {  
893 - srs_error("create c2 temp key failed. ret=%d", ret);  
894 - return ret;  
895 - }  
896 - srs_verbose("generate c2 temp key success.");  
897 -  
898 - char _digest[__SRS_OpensslHashSize];  
899 - if ((ret = openssl_HMACsha256(temp_key, 32, random, 1504, _digest)) != ERROR_SUCCESS) {  
900 - srs_error("create c2 digest failed. ret=%d", ret);  
901 - return ret;  
902 - }  
903 - srs_verbose("generate c2 digest success.");  
904 -  
905 - is_valid = srs_bytes_equals(digest, _digest, 32);  
906 -  
907 - return ret;  
908 - }  
909 -  
910 - int c2s2::s2_create(c1s1* c1)  
911 - {  
912 - int ret = ERROR_SUCCESS;  
913 -  
914 - char temp_key[__SRS_OpensslHashSize];  
915 - if ((ret = openssl_HMACsha256(SrsGenuineFMSKey, 68, c1->get_digest(), 32, temp_key)) != ERROR_SUCCESS) {  
916 - srs_error("create s2 temp key failed. ret=%d", ret);  
917 - return ret;  
918 - }  
919 - srs_verbose("generate s2 temp key success.");  
920 -  
921 - char _digest[__SRS_OpensslHashSize];  
922 - if ((ret = openssl_HMACsha256(temp_key, 32, random, 1504, _digest)) != ERROR_SUCCESS) {  
923 - srs_error("create s2 digest failed. ret=%d", ret);  
924 - return ret;  
925 - }  
926 - srs_verbose("generate s2 digest success.");  
927 -  
928 - memcpy(digest, _digest, 32);  
929 -  
930 - return ret;  
931 - }  
932 -  
933 - int c2s2::s2_validate(c1s1* c1, bool& is_valid)  
934 - {  
935 - is_valid = false;  
936 - int ret = ERROR_SUCCESS;  
937 -  
938 - char temp_key[__SRS_OpensslHashSize];  
939 - if ((ret = openssl_HMACsha256(SrsGenuineFMSKey, 68, c1->get_digest(), 32, temp_key)) != ERROR_SUCCESS) {  
940 - srs_error("create s2 temp key failed. ret=%d", ret);  
941 - return ret;  
942 - }  
943 - srs_verbose("generate s2 temp key success.");  
944 -  
945 - char _digest[__SRS_OpensslHashSize];  
946 - if ((ret = openssl_HMACsha256(temp_key, 32, random, 1504, _digest)) != ERROR_SUCCESS) {  
947 - srs_error("create s2 digest failed. ret=%d", ret);  
948 - return ret;  
949 - }  
950 - srs_verbose("generate s2 digest success.");  
951 -  
952 - is_valid = srs_bytes_equals(digest, _digest, 32);  
953 -  
954 - return ret;  
955 - }  
956 -  
957 // TODO: FIXME: move to the right position. 836 // TODO: FIXME: move to the right position.
958 c1s1::c1s1() 837 c1s1::c1s1()
959 { 838 {
@@ -1052,13 +931,6 @@ namespace _srs_internal @@ -1052,13 +931,6 @@ namespace _srs_internal
1052 return payload->c1_validate_digest(this, is_valid); 931 return payload->c1_validate_digest(this, is_valid);
1053 } 932 }
1054 933
1055 - int c1s1::s1_validate_digest(bool& is_valid)  
1056 - {  
1057 - is_valid = false;  
1058 - srs_assert(payload);  
1059 - return payload->s1_validate_digest(this, is_valid);  
1060 - }  
1061 -  
1062 int c1s1::s1_create(c1s1* c1) 934 int c1s1::s1_create(c1s1* c1)
1063 { 935 {
1064 int ret = ERROR_SUCCESS; 936 int ret = ERROR_SUCCESS;
@@ -1081,6 +953,134 @@ namespace _srs_internal @@ -1081,6 +953,134 @@ namespace _srs_internal
1081 953
1082 return payload->s1_create(this); 954 return payload->s1_create(this);
1083 } 955 }
  956 +
  957 + int c1s1::s1_validate_digest(bool& is_valid)
  958 + {
  959 + is_valid = false;
  960 + srs_assert(payload);
  961 + return payload->s1_validate_digest(this, is_valid);
  962 + }
  963 +
  964 + c2s2::c2s2()
  965 + {
  966 + srs_random_generate(random, 1504);
  967 +
  968 + int size = snprintf(random, 1504, "%s", RTMP_SIG_SRS_HANDSHAKE);
  969 + srs_assert(++size < 1504);
  970 + snprintf(random + 1504 - size, size, "%s", RTMP_SIG_SRS_HANDSHAKE);
  971 +
  972 + srs_random_generate(digest, 32);
  973 + }
  974 +
  975 + c2s2::~c2s2()
  976 + {
  977 + }
  978 +
  979 + void c2s2::dump(char* _c2s2)
  980 + {
  981 + memcpy(_c2s2, random, 1504);
  982 + memcpy(_c2s2 + 1504, digest, 32);
  983 + }
  984 +
  985 + void c2s2::parse(char* _c2s2)
  986 + {
  987 + memcpy(random, _c2s2, 1504);
  988 + memcpy(digest, _c2s2 + 1504, 32);
  989 + }
  990 +
  991 + int c2s2::c2_create(c1s1* s1)
  992 + {
  993 + int ret = ERROR_SUCCESS;
  994 +
  995 + char temp_key[__SRS_OpensslHashSize];
  996 + if ((ret = openssl_HMACsha256(SrsGenuineFPKey, 62, s1->get_digest(), 32, temp_key)) != ERROR_SUCCESS) {
  997 + srs_error("create c2 temp key failed. ret=%d", ret);
  998 + return ret;
  999 + }
  1000 + srs_verbose("generate c2 temp key success.");
  1001 +
  1002 + char _digest[__SRS_OpensslHashSize];
  1003 + if ((ret = openssl_HMACsha256(temp_key, 32, random, 1504, _digest)) != ERROR_SUCCESS) {
  1004 + srs_error("create c2 digest failed. ret=%d", ret);
  1005 + return ret;
  1006 + }
  1007 + srs_verbose("generate c2 digest success.");
  1008 +
  1009 + memcpy(digest, _digest, 32);
  1010 +
  1011 + return ret;
  1012 + }
  1013 +
  1014 + int c2s2::c2_validate(c1s1* s1, bool& is_valid)
  1015 + {
  1016 + is_valid = false;
  1017 + int ret = ERROR_SUCCESS;
  1018 +
  1019 + char temp_key[__SRS_OpensslHashSize];
  1020 + if ((ret = openssl_HMACsha256(SrsGenuineFPKey, 62, s1->get_digest(), 32, temp_key)) != ERROR_SUCCESS) {
  1021 + srs_error("create c2 temp key failed. ret=%d", ret);
  1022 + return ret;
  1023 + }
  1024 + srs_verbose("generate c2 temp key success.");
  1025 +
  1026 + char _digest[__SRS_OpensslHashSize];
  1027 + if ((ret = openssl_HMACsha256(temp_key, 32, random, 1504, _digest)) != ERROR_SUCCESS) {
  1028 + srs_error("create c2 digest failed. ret=%d", ret);
  1029 + return ret;
  1030 + }
  1031 + srs_verbose("generate c2 digest success.");
  1032 +
  1033 + is_valid = srs_bytes_equals(digest, _digest, 32);
  1034 +
  1035 + return ret;
  1036 + }
  1037 +
  1038 + int c2s2::s2_create(c1s1* c1)
  1039 + {
  1040 + int ret = ERROR_SUCCESS;
  1041 +
  1042 + char temp_key[__SRS_OpensslHashSize];
  1043 + if ((ret = openssl_HMACsha256(SrsGenuineFMSKey, 68, c1->get_digest(), 32, temp_key)) != ERROR_SUCCESS) {
  1044 + srs_error("create s2 temp key failed. ret=%d", ret);
  1045 + return ret;
  1046 + }
  1047 + srs_verbose("generate s2 temp key success.");
  1048 +
  1049 + char _digest[__SRS_OpensslHashSize];
  1050 + if ((ret = openssl_HMACsha256(temp_key, 32, random, 1504, _digest)) != ERROR_SUCCESS) {
  1051 + srs_error("create s2 digest failed. ret=%d", ret);
  1052 + return ret;
  1053 + }
  1054 + srs_verbose("generate s2 digest success.");
  1055 +
  1056 + memcpy(digest, _digest, 32);
  1057 +
  1058 + return ret;
  1059 + }
  1060 +
  1061 + int c2s2::s2_validate(c1s1* c1, bool& is_valid)
  1062 + {
  1063 + is_valid = false;
  1064 + int ret = ERROR_SUCCESS;
  1065 +
  1066 + char temp_key[__SRS_OpensslHashSize];
  1067 + if ((ret = openssl_HMACsha256(SrsGenuineFMSKey, 68, c1->get_digest(), 32, temp_key)) != ERROR_SUCCESS) {
  1068 + srs_error("create s2 temp key failed. ret=%d", ret);
  1069 + return ret;
  1070 + }
  1071 + srs_verbose("generate s2 temp key success.");
  1072 +
  1073 + char _digest[__SRS_OpensslHashSize];
  1074 + if ((ret = openssl_HMACsha256(temp_key, 32, random, 1504, _digest)) != ERROR_SUCCESS) {
  1075 + srs_error("create s2 digest failed. ret=%d", ret);
  1076 + return ret;
  1077 + }
  1078 + srs_verbose("generate s2 digest success.");
  1079 +
  1080 + is_valid = srs_bytes_equals(digest, _digest, 32);
  1081 +
  1082 + return ret;
  1083 + }
1084 } 1084 }
1085 1085
1086 #endif 1086 #endif