Changeset 1722
- Timestamp:
- Jan 21, 2008 6:08:47 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/branches/users/nanang/pjmedia/src/pjmedia/transport_srtp.c
r1721 r1722 42 42 static const pj_str_t ID_RTP_SAVP = { "RTP/SAVP", 8 }; 43 43 static const pj_str_t ID_INACTIVE = { "inactive", 8 }; 44 static const pj_str_t ID_CRYPTO = { "crypto", 6 }; 44 45 45 46 typedef struct crypto_suite … … 243 244 int cs_cnt = sizeof(crypto_suites)/sizeof(crypto_suites[0]); 244 245 246 /* treat unspecified crypto_name as crypto 'NULL' */ 245 247 if (crypto_name->slen == 0) 246 248 return 0; … … 301 303 /* check key length */ 302 304 if (opt->crypto[i].key.slen && 303 opt->crypto[i].key.slen < crypto_suites[cs_idx].cipher_key_len) 305 opt->crypto[i].key.slen < 306 (pj_ssize_t)crypto_suites[cs_idx].cipher_key_len) 304 307 return PJMEDIA_SRTP_EINKEYLEN; 305 308 } … … 384 387 crypto_suites_cnt = sizeof(crypto_suites)/sizeof(crypto_suites[0]); 385 388 386 /* Check whether the crypto-suite requested is supported */389 /* Get encryption and authentication method */ 387 390 cr_tx_idx = au_tx_idx = get_crypto_idx(&tx->name); 388 391 if (tx->flags && PJMEDIA_SRTP_NO_ENCRYPTION) … … 391 394 au_tx_idx = 0; 392 395 393 /* Check whether the crypto-suite requested is supported */394 396 cr_rx_idx = au_rx_idx = get_crypto_idx(&rx->name); 395 397 if (rx->flags && PJMEDIA_SRTP_NO_ENCRYPTION) … … 398 400 au_rx_idx = 0; 399 401 400 if (cr_tx_idx == -1 || cr_rx_idx == -1 || au_tx_idx == -1 || au_rx_idx == -1) 402 /* Check whether the crypto-suite requested is supported */ 403 if (cr_tx_idx == -1 || cr_rx_idx == -1 || au_tx_idx == -1 || 404 au_rx_idx == -1) 401 405 return PJMEDIA_SRTP_ENOTSUPCRYPTO; 402 406 407 /* If all options points to 'NULL' method, just bypass SRTP */ 403 408 if (cr_tx_idx == 0 && cr_rx_idx == 0 && au_tx_idx == 0 && au_rx_idx == 0) { 404 409 srtp->bypass_srtp = PJ_TRUE; … … 406 411 } 407 412 413 /* Check key length */ 414 if (tx->key.slen != (pj_ssize_t)crypto_suites[cr_tx_idx].cipher_key_len || 415 rx->key.slen != (pj_ssize_t)crypto_suites[cr_rx_idx].cipher_key_len) 416 return PJMEDIA_SRTP_EINKEYLEN; 417 408 418 /* Init transmit direction */ 409 419 pj_bzero(&tx_, sizeof(srtp_policy_t)); 410 420 pj_memmove(srtp->tx_key, tx->key.ptr, tx->key.slen); 421 if (cr_tx_idx && au_tx_idx) 422 tx_.rtp.sec_serv = sec_serv_conf_and_auth; 423 else if (cr_tx_idx) 424 tx_.rtp.sec_serv = sec_serv_conf; 425 else if (au_tx_idx) 426 tx_.rtp.sec_serv = sec_serv_auth; 427 else 428 tx_.rtp.sec_serv = sec_serv_none; 411 429 tx_.key = (uint8_t*)srtp->tx_key; 412 430 tx_.ssrc.type = ssrc_any_outbound; 413 431 tx_.ssrc.value = 0; 414 tx_.rtp.sec_serv = crypto_suites[cr_tx_idx].service;415 432 tx_.rtp.cipher_type = crypto_suites[cr_tx_idx].cipher_type; 416 433 tx_.rtp.cipher_key_len = crypto_suites[cr_tx_idx].cipher_key_len; … … 434 451 pj_bzero(&rx_, sizeof(srtp_policy_t)); 435 452 pj_memmove(srtp->rx_key, rx->key.ptr, rx->key.slen); 453 if (cr_rx_idx && au_rx_idx) 454 rx_.rtp.sec_serv = sec_serv_conf_and_auth; 455 else if (cr_rx_idx) 456 rx_.rtp.sec_serv = sec_serv_conf; 457 else if (au_rx_idx) 458 rx_.rtp.sec_serv = sec_serv_auth; 459 else 460 rx_.rtp.sec_serv = sec_serv_none; 436 461 rx_.key = (uint8_t*)srtp->rx_key; 437 462 rx_.ssrc.type = ssrc_any_inbound; … … 774 799 } 775 800 776 if (crypto->key.slen != ( int)crypto_suites[cs_idx].cipher_key_len)801 if (crypto->key.slen != (pj_ssize_t)crypto_suites[cs_idx].cipher_key_len) 777 802 return PJMEDIA_SRTP_EINKEYLEN; 778 803 … … 817 842 token = strtok(input.ptr, " "); 818 843 if (!token) { 819 PJ_LOG( 5,(THIS_FILE, "Expecting tag"));844 PJ_LOG(4,(THIS_FILE, "Attribute crypto expecting tag")); 820 845 return PJMEDIA_SDP_EINATTR; 821 846 } … … 827 852 token = strtok(NULL, " "); 828 853 if (!token) { 829 PJ_LOG( 5,(THIS_FILE, "Expecting crypto suite"));854 PJ_LOG(4,(THIS_FILE, "Attribute crypto expecting crypto suite")); 830 855 return PJMEDIA_SDP_EINATTR; 831 856 } … … 835 860 token = strtok(NULL, ":"); 836 861 if (!token) { 837 PJ_LOG( 5,(THIS_FILE, "Expecting key method"));862 PJ_LOG(4,(THIS_FILE, "Attribute crypto expecting key method")); 838 863 return PJMEDIA_SDP_EINATTR; 839 864 } 840 865 if (pj_ansi_stricmp(token, "inline")) { 841 PJ_LOG(5,(THIS_FILE, "Key method %s not supported!", token)); 866 PJ_LOG(4,(THIS_FILE, "Attribute crypto key method '%s' not supported!", 867 token)); 842 868 return PJMEDIA_SDP_EINATTR; 843 869 } … … 846 872 token = strtok(NULL, "| "); 847 873 if (!token) { 848 PJ_LOG( 5,(THIS_FILE, "Expecting key"));874 PJ_LOG(4,(THIS_FILE, "Attribute crypto expecting key")); 849 875 return PJMEDIA_SDP_EINATTR; 850 876 } … … 857 883 &itmp); 858 884 if (status != PJ_SUCCESS) { 859 PJ_LOG( 5,(THIS_FILE, "Failed decodingkey from base64"));885 PJ_LOG(4,(THIS_FILE, "Failed decoding crypto key from base64")); 860 886 return status; 861 887 } … … 927 953 return PJMEDIA_SRTP_ESDPINTRANSPORT; 928 954 } 955 m_loc->desc.transport = ID_RTP_SAVP; 929 956 } 930 957 } … … 944 971 if (buffer_len) { 945 972 pj_strset(&attr_value, buffer, buffer_len); 946 attr = pjmedia_sdp_attr_create(pool, "crypto", &attr_value); 973 attr = pjmedia_sdp_attr_create(pool, ID_CRYPTO.ptr, 974 &attr_value); 947 975 m_loc->attr[m_loc->attr_count++] = attr; 948 976 } … … 954 982 pj_bool_t has_match = PJ_FALSE; 955 983 int chosen_tag = 0; 956 int tags[ 8];957 int cr_ count = 0;984 int tags[64]; /* assume no more than 64 crypto attrs in a media */ 985 int cr_attr_count = 0; 958 986 int k; 959 987 960 988 for (i=0; i<m_rem->attr_count; ++i) { 961 if (pj_stricmp 2(&m_rem->attr[i]->name, "crypto") != 0)989 if (pj_stricmp(&m_rem->attr[i]->name, &ID_CRYPTO) != 0) 962 990 continue; 963 991 964 992 /* SRTP is disabled but there is crypto attr in remote media */ 993 /* Put the checking here to save a bit memory for parsing */ 965 994 if (srtp->setting.use == PJMEDIA_SRTP_DISABLED) { 966 995 DEACTIVATE_MEDIA(pool, m_loc); … … 971 1000 972 1001 status = parse_attr_crypto(pool, m_rem->attr[i], 973 &tmp_rx_crypto, &tags[cr_ count]);1002 &tmp_rx_crypto, &tags[cr_attr_count]); 974 1003 if (status != PJ_SUCCESS) 975 1004 return status; 976 1005 977 1006 /* Check duplicated tag */ 978 for (k=0; k<cr_ count; ++k) {979 if (tags[k] == tags[cr_ count]) {1007 for (k=0; k<cr_attr_count; ++k) { 1008 if (tags[k] == tags[cr_attr_count]) { 980 1009 DEACTIVATE_MEDIA(pool, m_loc); 981 1010 return PJMEDIA_SRTP_ESDPDUPCRYPTOTAG; … … 997 1026 srtp->tx_policy = srtp->setting.crypto[j]; 998 1027 srtp->rx_policy = tmp_rx_crypto; 999 chosen_tag = tags[cr_ count];1028 chosen_tag = tags[cr_attr_count]; 1000 1029 has_match = PJ_TRUE; 1001 1030 break; 1002 1031 } 1003 1032 } 1004 cr_ count++;1033 cr_attr_count++; 1005 1034 } 1006 1035 … … 1018 1047 goto BYPASS_SRTP; 1019 1048 } else if (srtp->setting.use == PJMEDIA_SRTP_MANDATORY) { 1020 if (!has_crypto_attr) { 1021 DEACTIVATE_MEDIA(pool, m_loc); 1022 return PJMEDIA_SRTP_ESDPREQCRYPTO; 1023 } 1024 } 1025 1026 /* No crypto match? */ 1049 /* do nothing, this is intended */ 1050 } 1051 1052 /* No crypto attr */ 1053 if (!has_crypto_attr) { 1054 DEACTIVATE_MEDIA(pool, m_loc); 1055 return PJMEDIA_SRTP_ESDPREQCRYPTO; 1056 } 1057 1058 /* No crypto match */ 1027 1059 if (!has_match) { 1028 1060 DEACTIVATE_MEDIA(pool, m_loc); … … 1044 1076 if (buffer_len) { 1045 1077 pj_strset(&attr_value, buffer, buffer_len); 1046 attr = pjmedia_sdp_attr_create(pool, "crypto", &attr_value);1078 attr = pjmedia_sdp_attr_create(pool, ID_CRYPTO.ptr, &attr_value); 1047 1079 m_loc->attr[m_loc->attr_count++] = attr; 1048 1080 } … … 1091 1123 if (srtp->offerer_side) { 1092 1124 if (srtp->setting.use == PJMEDIA_SRTP_DISABLED) { 1093 if (pjmedia_sdp_media_find_attr 2(m_rem, "crypto", NULL)) {1125 if (pjmedia_sdp_media_find_attr(m_rem, &ID_CRYPTO, NULL)) { 1094 1126 DEACTIVATE_MEDIA(pool, m_loc); 1095 1127 return PJMEDIA_SRTP_ESDPINCRYPTO; … … 1116 1148 1117 1149 for (i=0; i<m_rem->attr_count; ++i) { 1118 if (pj_stricmp 2(&m_rem->attr[i]->name, "crypto") != 0)1150 if (pj_stricmp(&m_rem->attr[i]->name, &ID_CRYPTO) != 0) 1119 1151 continue; 1120 1152
Note: See TracChangeset
for help on using the changeset viewer.