Ignore:
Timestamp:
Jan 23, 2008 8:39:07 PM (16 years ago)
Author:
bennylp
Message:

Ticket #61: Implement SRTP support in PJMEDIA and PJSUA-LIB, and updated applications because of the changes. This is a major modification back ported from SRTP branch. See ticket #61 for changelog detail of this commit

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip-apps/src/samples/streamutil.c

    r1666 r1735  
    5656 "  --send-only           Set stream direction to send only             \n" 
    5757 "  --recv-only           Set stream direction to recv only (default)   \n" 
     58 
     59#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) 
     60 "  --use-srtp[=NAME]     Enable SRTP with crypto suite NAME            \n" 
     61 "                        e.g: AES_CM_128_HMAC_SHA1_80 (default),       \n" 
     62 "                             AES_CM_128_HMAC_SHA1_32                  \n" 
     63 "                        Use this option along with the TX & RX keys,  \n" 
     64 "                        formated of 60 hex digits (e.g: E148DA..)      \n" 
     65 "  --srtp-tx-key         SRTP key for transmiting                      \n" 
     66 "  --srtp-rx-key         SRTP key for receiving                        \n" 
     67#endif 
     68 
    5869 "\n" 
    5970; 
     
    6576#include <pjmedia.h> 
    6677#include <pjmedia-codec.h> 
     78#include <pjmedia/transport_srtp.h> 
    6779 
    6880#include <stdlib.h>     /* atoi() */ 
     
    7991static void print_stream_stat(pjmedia_stream *stream); 
    8092 
     93/* Prototype for LIBSRTP utility in file datatypes.c */ 
     94int hex_string_to_octet_string(char *raw, char *hex, int len); 
    8195 
    8296/*  
     
    123137                                  pj_uint16_t local_port, 
    124138                                  const pj_sockaddr_in *rem_addr, 
     139#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) 
     140                                  pj_bool_t use_srtp, 
     141                                  const pj_str_t *crypto_suite, 
     142                                  const pj_str_t *srtp_tx_key, 
     143                                  const pj_str_t *srtp_rx_key, 
     144#endif 
    125145                                  pjmedia_stream **p_stream ) 
    126146{ 
    127147    pjmedia_stream_info info; 
    128     pjmedia_transport *transport; 
     148    pjmedia_transport *transport = NULL; 
    129149    pj_status_t status; 
     150#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) 
     151    pjmedia_transport *srtp_tp = NULL; 
     152#endif 
    130153 
    131154 
     
    159182        return status; 
    160183 
     184#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) 
     185    /* Check if SRTP enabled */ 
     186    if (use_srtp) { 
     187        pjmedia_srtp_crypto tx_plc, rx_plc; 
     188 
     189        status = pjmedia_transport_srtp_create(med_endpt, transport,  
     190                                               NULL, &srtp_tp); 
     191        if (status != PJ_SUCCESS) 
     192            return status; 
     193 
     194        pj_bzero(&tx_plc, sizeof(pjmedia_srtp_crypto)); 
     195        pj_bzero(&rx_plc, sizeof(pjmedia_srtp_crypto)); 
     196 
     197        tx_plc.key = *srtp_tx_key; 
     198        tx_plc.name = *crypto_suite; 
     199        rx_plc.key = *srtp_rx_key; 
     200        rx_plc.name = *crypto_suite; 
     201         
     202        status = pjmedia_transport_srtp_start(srtp_tp, &tx_plc, &rx_plc); 
     203        if (status != PJ_SUCCESS) 
     204            return status; 
     205 
     206        transport = srtp_tp; 
     207    } 
     208#endif 
    161209 
    162210    /* Now that the stream info is initialized, we can create the  
     
    165213 
    166214    status = pjmedia_stream_create( med_endpt, pool, &info,  
    167                                     transport, NULL, p_stream); 
     215                                    transport,  
     216                                    NULL, p_stream); 
    168217 
    169218    if (status != PJ_SUCCESS) { 
    170219        app_perror(THIS_FILE, "Error creating stream", status); 
    171         pjmedia_transport_udp_close(transport); 
     220        pjmedia_transport_close(transport); 
    172221        return status; 
    173222    } 
     
    202251    pj_status_t status;  
    203252 
     253#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) 
     254    /* SRTP variables */ 
     255    pj_bool_t use_srtp = PJ_FALSE; 
     256    char tmp_tx_key[64]; 
     257    char tmp_rx_key[64]; 
     258    pj_str_t  srtp_tx_key = {NULL, 0}; 
     259    pj_str_t  srtp_rx_key = {NULL, 0}; 
     260    pj_str_t  srtp_crypto_suite = {NULL, 0}; 
     261    int tmp_key_len; 
     262#endif 
    204263 
    205264    /* Default values */ 
     
    221280        OPT_SEND_ONLY   = 's', 
    222281        OPT_RECV_ONLY   = 'i', 
     282#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) 
     283        OPT_USE_SRTP    = 'S', 
     284#endif 
     285        OPT_SRTP_TX_KEY = 'x', 
     286        OPT_SRTP_RX_KEY = 'y', 
    223287        OPT_HELP        = 'h', 
    224288    }; 
     
    233297        { "send-only",      0, 0, OPT_SEND_ONLY }, 
    234298        { "recv-only",      0, 0, OPT_RECV_ONLY }, 
     299#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) 
     300        { "use-srtp",       2, 0, OPT_USE_SRTP }, 
     301        { "srtp-tx-key",    1, 0, OPT_SRTP_TX_KEY }, 
     302        { "srtp-rx-key",    1, 0, OPT_SRTP_RX_KEY }, 
     303#endif 
    235304        { "help",           0, 0, OPT_HELP }, 
    236305        { NULL, 0, 0, 0 }, 
     
    299368            break; 
    300369 
     370#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) 
     371        case OPT_USE_SRTP: 
     372            use_srtp = PJ_TRUE; 
     373            if (pj_optarg) { 
     374                pj_strset(&srtp_crypto_suite, pj_optarg, strlen(pj_optarg)); 
     375            } else { 
     376                srtp_crypto_suite = pj_str("AES_CM_128_HMAC_SHA1_80"); 
     377            } 
     378            break; 
     379 
     380        case OPT_SRTP_TX_KEY: 
     381            tmp_key_len = hex_string_to_octet_string(tmp_tx_key, pj_optarg, strlen(pj_optarg)); 
     382            pj_strset(&srtp_tx_key, tmp_tx_key, tmp_key_len/2); 
     383            break; 
     384 
     385        case OPT_SRTP_RX_KEY: 
     386            tmp_key_len = hex_string_to_octet_string(tmp_rx_key, pj_optarg, strlen(pj_optarg)); 
     387            pj_strset(&srtp_rx_key, tmp_rx_key, tmp_key_len/2); 
     388            break; 
     389#endif 
     390 
    301391        case OPT_HELP: 
    302392            usage(); 
     
    324414    } 
    325415 
     416#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) 
     417    /* SRTP validation */ 
     418    if (use_srtp) { 
     419        if (!srtp_tx_key.slen || !srtp_rx_key.slen) 
     420        { 
     421            printf("Error: Key for each SRTP stream direction must be set\n"); 
     422            return 1; 
     423        } 
     424    } 
     425#endif 
    326426 
    327427    /* Must create a pool factory before we can allocate any memory. */ 
     
    369469    /* Create stream based on program arguments */ 
    370470    status = create_stream(pool, med_endpt, codec_info, dir, local_port,  
    371                            &remote_addr, &stream); 
     471                           &remote_addr,  
     472#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) 
     473                           use_srtp, &srtp_crypto_suite,  
     474                           &srtp_tx_key, &srtp_rx_key, 
     475#endif 
     476                           &stream); 
    372477    if (status != PJ_SUCCESS) 
    373478        goto on_exit; 
     
    538643        tp = pjmedia_stream_get_transport(stream); 
    539644        pjmedia_stream_destroy(stream); 
    540         pjmedia_transport_udp_close(tp); 
     645         
     646        pjmedia_transport_close(tp); 
    541647    } 
    542648 
Note: See TracChangeset for help on using the changeset viewer.