Changeset 1713


Ignore:
Timestamp:
Jan 19, 2008 1:42:40 AM (16 years ago)
Author:
nanang
Message:

Ticket #452:
changed on transport_srtp.c: (re)applied setting close member transport & added more validation checks

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/branches/users/nanang/pjmedia/src/pjmedia/transport_srtp.c

    r1711 r1713  
    181181 
    182182 
    183 pj_str_t srtp_transport_getliberrstr(int err) 
    184 { 
    185     pj_str_t msg = {NULL, 0}; 
    186  
     183const char* get_libsrtp_errstr(int err) 
     184{ 
    187185#if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING != 0) 
    188186    static char *liberr[] = { 
    189         "nothing to report                       ", /* err_status_ok           = 0,   */ 
    190         "unspecified failure                     ", /* err_status_fail         = 1,   */ 
    191         "unsupported parameter                   ", /* err_status_bad_param    = 2,   */ 
    192         "couldn't allocate memory                ", /* err_status_alloc_fail   = 3,   */ 
    193         "couldn't deallocate properly            ", /* err_status_dealloc_fail = 4,   */ 
    194         "couldn't initialize                     ", /* err_status_init_fail    = 5,   */ 
    195         "can't process as much data as requested ", /* err_status_terminus     = 6,   */ 
    196         "authentication failure                  ", /* err_status_auth_fail    = 7,   */ 
    197         "cipher failure                          ", /* err_status_cipher_fail  = 8,   */ 
    198         "replay check failed (bad index)         ", /* err_status_replay_fail  = 9,   */ 
    199         "replay check failed (index too old)     ", /* err_status_replay_old   = 10,  */ 
    200         "algorithm failed test routine           ", /* err_status_algo_fail    = 11,  */ 
    201         "unsupported operation                   ", /* err_status_no_such_op   = 12,  */ 
    202         "no appropriate context found            ", /* err_status_no_ctx       = 13,  */ 
    203         "unable to perform desired validation    ", /* err_status_cant_check   = 14,  */ 
    204         "can't use key any more                  ", /* err_status_key_expired  = 15,  */ 
    205         "error in use of socket                  ", /* err_status_socket_err   = 16,  */ 
    206         "error in use POSIX signals              ", /* err_status_signal_err   = 17,  */ 
    207         "nonce check failed                      ", /* err_status_nonce_bad    = 18,  */ 
    208         "couldn't read data                      ", /* err_status_read_fail    = 19,  */ 
    209         "couldn't write data                     ", /* err_status_write_fail   = 20,  */ 
    210         "error pasring data                      ", /* err_status_parse_err    = 21,  */ 
    211         "error encoding data                     ", /* err_status_encode_err   = 22,  */ 
    212         "error while using semaphores            ", /* err_status_semaphore_err = 23,  */ 
    213         "error while using pfkey                 " /* err_status_pfkey_err    = 24,   */ 
     187        "ok",                               /* err_status_ok            = 0  */ 
     188        "unspecified failure",              /* err_status_fail          = 1  */ 
     189        "unsupported parameter",            /* err_status_bad_param     = 2  */ 
     190        "couldn't allocate memory",         /* err_status_alloc_fail    = 3  */ 
     191        "couldn't deallocate properly",     /* err_status_dealloc_fail  = 4  */ 
     192        "couldn't initialize",              /* err_status_init_fail     = 5  */ 
     193        "can't process as much data as requested",  
     194                                            /* err_status_terminus      = 6  */ 
     195        "authentication failure",           /* err_status_auth_fail     = 7  */ 
     196        "cipher failure",                   /* err_status_cipher_fail   = 8  */ 
     197        "replay check failed (bad index)",  /* err_status_replay_fail   = 9  */ 
     198        "replay check failed (index too old)",  
     199                                            /* err_status_replay_old    = 10 */ 
     200        "algorithm failed test routine",    /* err_status_algo_fail     = 11 */ 
     201        "unsupported operation",            /* err_status_no_such_op    = 12 */ 
     202        "no appropriate context found",     /* err_status_no_ctx        = 13 */ 
     203        "unable to perform desired validation",  
     204                                            /* err_status_cant_check    = 14 */ 
     205        "can't use key any more",           /* err_status_key_expired   = 15 */ 
     206        "error in use of socket",           /* err_status_socket_err    = 16 */ 
     207        "error in use POSIX signals",       /* err_status_signal_err    = 17 */ 
     208        "nonce check failed",               /* err_status_nonce_bad     = 18 */ 
     209        "couldn't read data",               /* err_status_read_fail     = 19 */ 
     210        "couldn't write data",              /* err_status_write_fail    = 20 */ 
     211        "error pasring data",               /* err_status_parse_err     = 21 */ 
     212        "error encoding data",              /* err_status_encode_err    = 22 */ 
     213        "error while using semaphores",     /* err_status_semaphore_err = 23 */ 
     214        "error while using pfkey"           /* err_status_pfkey_err     = 24 */ 
    214215    }; 
    215     if (err >= 0 && err <= 24) { 
    216         msg.ptr = liberr[err]; 
    217         msg.slen = 40; 
    218     } 
     216    return liberr[err]; 
     217#else 
     218    return NULL; 
    219219#endif 
    220  
    221     return msg; 
    222220} 
    223221 
     
    235233 
    236234        initialized = PJ_TRUE; 
    237  
    238         PJ_TODO(REGISTER_LIBSRTP_ERROR_CODES_AND_MESSAGES); 
    239235    } 
    240236     
     
    247243    int cs_cnt = sizeof(crypto_suites)/sizeof(crypto_suites[0]); 
    248244     
     245    if (crypto_name->slen == 0) 
     246        return 0; 
     247 
    249248    for (i=0; i<cs_cnt; ++i) { 
    250249        if (!pj_stricmp2(crypto_name, crypto_suites[i].name)) 
     
    284283    PJ_ASSERT_RETURN(endpt && p_tp, PJ_EINVAL); 
    285284 
     285    /* Check crypto availability */ 
     286    if (opt->crypto_count == 0 &&  
     287        opt->use == PJMEDIA_SRTP_MANDATORY) 
     288        return PJMEDIA_SRTP_ESDPREQCRYPTO; 
     289 
     290    /* Check crypto */ 
     291    if (opt->use != PJMEDIA_SRTP_DISABLED) { 
     292        for (i=0; i < opt->crypto_count; ++i) { 
     293            int cs_idx = get_crypto_idx(&opt->crypto[i].name); 
     294 
     295            /* check crypto name */ 
     296            if (cs_idx == -1) 
     297                return PJMEDIA_SRTP_ENOTSUPCRYPTO; 
     298 
     299            /* check key length */ 
     300            if (opt->crypto[i].key.slen &&  
     301                opt->crypto[i].key.slen < crypto_suites[cs_idx].cipher_key_len) 
     302                return PJMEDIA_SRTP_EINKEYLEN; 
     303        } 
     304    } 
     305 
    286306    /* Init libsrtp. */ 
    287307    status = pjmedia_srtp_init_lib(); 
     
    294314    srtp->pool = pool; 
    295315    srtp->session_inited = PJ_FALSE; 
    296     srtp->bypass_srtp = PJ_TRUE; 
     316    srtp->bypass_srtp = PJ_FALSE; 
    297317 
    298318    if (opt) { 
    299319        srtp->setting = *opt; 
    300         for (i=0; i < opt->crypto_count; ++i) { 
     320        if (opt->use == PJMEDIA_SRTP_DISABLED) 
     321            srtp->setting.crypto_count = 0; 
     322 
     323        for (i=0; i < srtp->setting.crypto_count; ++i) { 
    301324            int cs_idx = get_crypto_idx(&opt->crypto[i].name); 
    302             if (cs_idx == -1) 
    303                 return PJMEDIA_SRTP_ENOTSUPCRYPTO; 
    304  
     325            pj_str_t tmp_key = opt->crypto[i].key; 
     326 
     327            /* re-set crypto */ 
    305328            srtp->setting.crypto[i].name = pj_str(crypto_suites[cs_idx].name); 
    306             pj_strdup(pool, &srtp->setting.crypto[i].key, &opt->crypto[i].key); 
     329            /* cut key length */ 
     330            tmp_key.slen = crypto_suites[cs_idx].cipher_key_len; 
     331            pj_strdup(pool, &srtp->setting.crypto[i].key, &tmp_key); 
    307332        } 
    308333    } else { 
    309334        pjmedia_srtp_setting_default(&srtp->setting); 
    310335    } 
    311  
    312     if (srtp->setting.crypto_count==0 &&  
    313         srtp->setting.use == PJMEDIA_SRTP_MANDATORY) 
    314         return PJMEDIA_SRTP_ESDPREQCRYPTO; 
    315336 
    316337    status = pj_lock_create_null_mutex(pool, pool->obj_name, &srtp->mutex); 
     
    376397    if (cr_tx_idx == -1 || cr_rx_idx == -1 || au_tx_idx == -1 || au_rx_idx == -1) 
    377398        return PJMEDIA_SRTP_ENOTSUPCRYPTO; 
     399 
     400    if (cr_tx_idx == 0 && cr_rx_idx == 0 && au_tx_idx == 0 && au_rx_idx == 0) { 
     401        srtp->bypass_srtp = PJ_TRUE; 
     402        return PJ_SUCCESS; 
     403    } 
    378404 
    379405    /* Init transmit direction */ 
     
    429455    /* Declare SRTP session initialized */ 
    430456    srtp->session_inited = PJ_TRUE; 
    431     srtp->bypass_srtp = PJ_FALSE; 
    432457 
    433458    PJ_LOG(5, (THIS_FILE, "TX: %s key=%s", srtp->tx_policy.name.ptr, 
     
    545570 
    546571    if (srtp->bypass_srtp) 
    547         return pjmedia_transport_send_rtp(srtp->real_tp, srtp->tx_buffer, len); 
     572        return pjmedia_transport_send_rtp(srtp->real_tp, pkt, size); 
    548573 
    549574    if (!srtp->session_inited) 
     
    578603 
    579604    if (srtp->bypass_srtp) 
    580         return pjmedia_transport_send_rtp(srtp->real_tp, srtp->tx_buffer, len); 
     605        return pjmedia_transport_send_rtp(srtp->real_tp, pkt, size); 
    581606 
    582607    if (!srtp->session_inited) 
     
    642667 
    643668    if (srtp->bypass_srtp) { 
    644         srtp->rtp_cb(srtp->user_data, srtp->rx_buffer, len); 
     669        srtp->rtp_cb(srtp->user_data, pkt, size); 
    645670        return; 
    646671    } 
     
    674699 
    675700    if (srtp->bypass_srtp) { 
    676         srtp->rtcp_cb(srtp->user_data, srtp->rx_buffer, len); 
     701        srtp->rtcp_cb(srtp->user_data, pkt, size); 
    677702        return; 
    678703    } 
     
    746771    } 
    747772 
    748     if ((unsigned)crypto->key.slen != crypto_suites[cs_idx].cipher_key_len) 
     773    if (crypto->key.slen != (int)crypto_suites[cs_idx].cipher_key_len) 
    749774        return PJMEDIA_SRTP_EINKEYLEN; 
    750775 
     
    759784    b64_key[b64_key_len] = '\0'; 
    760785     
    761     PJ_ASSERT_RETURN((unsigned)*buffer_len >= (crypto->name.slen + \ 
     786    PJ_ASSERT_RETURN(*buffer_len >= (crypto->name.slen + \ 
    762787                     b64_key_len + 16), PJ_ETOOSMALL); 
    763788 
     
    959984                                   &srtp->setting.crypto[j].name) == 0) 
    960985                    { 
     986                        int cs_idx = get_crypto_idx(&tmp_rx_crypto.name); 
     987 
     988                        if (tmp_rx_crypto.key.slen !=  
     989                            (int)crypto_suites[cs_idx].cipher_key_len) 
     990                            return PJMEDIA_SRTP_EINKEYLEN; 
     991 
    961992                        srtp->tx_policy = srtp->setting.crypto[j]; 
    962993                        srtp->rx_policy = tmp_rx_crypto; 
     
    10361067    /* bypass if media transport is not RTP/AVP or RTP/SAVP */ 
    10371068    if (pj_stricmp(&m_loc->desc.transport, &ID_RTP_AVP)  != 0 &&  
    1038         pj_stricmp(&m_loc->desc.transport, &ID_RTP_SAVP) != 0) 
     1069        pj_stricmp(&m_loc->desc.transport, &ID_RTP_SAVP) != 0)  
     1070    { 
     1071        srtp->bypass_srtp = PJ_TRUE; 
    10391072        goto PROPAGATE_MEDIA_START; 
     1073    } 
    10401074 
    10411075    /* If the media is inactive, do nothing. */ 
     
    10431077        (m_rem && pjmedia_sdp_media_find_attr(m_rem, &ID_INACTIVE, NULL))) 
    10441078    { 
     1079        srtp->bypass_srtp = PJ_TRUE; 
    10451080        goto PROPAGATE_MEDIA_START; 
    10461081    } 
     
    10571092                return PJMEDIA_SRTP_ESDPINCRYPTO; 
    10581093            } 
    1059             goto PROPAGATE_MEDIA_START;; 
     1094            srtp->bypass_srtp = PJ_TRUE; 
     1095            goto PROPAGATE_MEDIA_START; 
    10601096        } else if (srtp->setting.use == PJMEDIA_SRTP_OPTIONAL) { 
    10611097            if (pj_stricmp(&m_rem->desc.transport, &m_loc->desc.transport)) { 
     
    10731109    if (srtp->offerer_side) { 
    10741110        /* find supported crypto-suite, get the tag, and assign policy_local */ 
     1111        pjmedia_srtp_crypto tmp_tx_crypto; 
    10751112        pj_bool_t has_crypto_attr = PJ_FALSE; 
    10761113        int rem_tag; 
     
    10891126 
    10901127            status = parse_attr_crypto(pool, m_rem->attr[i],  
    1091                                        &srtp->rx_policy, &rem_tag); 
     1128                                       &tmp_tx_crypto, &rem_tag); 
    10921129            if (status != PJ_SUCCESS) 
    10931130                return status; 
     
    10951132 
    10961133            /* our offer tag is always ordered by setting */ 
    1097             if (rem_tag<1 || rem_tag>srtp->setting.crypto_count) { 
     1134            if (rem_tag < 1 || rem_tag > srtp->setting.crypto_count) { 
    10981135                DEACTIVATE_MEDIA(pool, m_loc); 
    10991136                return PJMEDIA_SRTP_ESDPINCRYPTOTAG; 
     
    11091146 
    11101147            srtp->tx_policy = srtp->setting.crypto[rem_tag-1]; 
     1148            srtp->rx_policy = tmp_tx_crypto; 
    11111149        } 
    11121150 
    11131151        if (srtp->setting.use == PJMEDIA_SRTP_DISABLED) { 
    11141152            /* should never reach here */ 
     1153            srtp->bypass_srtp = PJ_TRUE; 
    11151154            goto PROPAGATE_MEDIA_START; 
    11161155        } else if (srtp->setting.use == PJMEDIA_SRTP_OPTIONAL) { 
    1117             if (!has_crypto_attr) 
     1156            if (!has_crypto_attr) { 
     1157                srtp->bypass_srtp = PJ_TRUE; 
    11181158                goto PROPAGATE_MEDIA_START; 
     1159            } 
    11191160        } else if (srtp->setting.use == PJMEDIA_SRTP_MANDATORY) { 
    11201161            if (!has_crypto_attr) { 
     
    11451186    pj_status_t status; 
    11461187 
    1147     status = pjmedia_transport_srtp_stop(tp); 
    1148     if (status != PJ_SUCCESS) 
    1149         PJ_LOG(4, (THIS_FILE, "Failed deinit session.")); 
    1150  
    1151     return pjmedia_transport_media_stop(srtp->real_tp); 
    1152 } 
     1188    if (srtp->setting.close_member_tp) { 
     1189        status = pjmedia_transport_media_stop(srtp->real_tp); 
     1190        if (status != PJ_SUCCESS) 
     1191            PJ_LOG(4, (THIS_FILE, "Failed deinit session.")); 
     1192    } 
     1193 
     1194    return pjmedia_transport_srtp_stop(tp); 
     1195} 
Note: See TracChangeset for help on using the changeset viewer.