Changeset 6118


Ignore:
Timestamp:
Dec 16, 2019 12:37:57 PM (4 years ago)
Author:
riza
Message:

Close #2257: Filter SRTP crypto setting for DTLS-SRTP profile.

Location:
pjproject/trunk/pjmedia
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/include/pjmedia/errno.h

    r5845 r6118  
    657657#define PJMEDIA_SRTP_DTLS_ENOFPRINT (PJMEDIA_ERRNO_START+243)   /* 220243 */ 
    658658 
     659/** 
     660 * @hideinitializer 
     661 * No valid SRTP protection profile for DTLS. 
     662 */ 
     663#define PJMEDIA_SRTP_DTLS_ENOPROFILE (PJMEDIA_ERRNO_START+244)   /* 220244 */ 
     664 
    659665#endif /* PJMEDIA_HAS_SRTP */ 
    660666 
  • pjproject/trunk/pjmedia/src/pjmedia/errno.c

    r5845 r6118  
    171171    PJ_BUILD_ERR( PJMEDIA_SRTP_DTLS_EPEERNOCERT,"No certificate supplied by peer in DTLS nego" ), 
    172172    PJ_BUILD_ERR( PJMEDIA_SRTP_DTLS_EFPNOTMATCH,"Fingerprint from signalling not match to actual fingerprint" ), 
    173     PJ_BUILD_ERR( PJMEDIA_SRTP_DTLS_ENOFPRINT,  "Fingerprint not found" ) 
     173    PJ_BUILD_ERR( PJMEDIA_SRTP_DTLS_ENOFPRINT,  "Fingerprint not found" ), 
     174    PJ_BUILD_ERR( PJMEDIA_SRTP_DTLS_ENOPROFILE, "No valid SRTP protection profile found" ) 
    174175#endif 
    175176 
  • pjproject/trunk/pjmedia/src/pjmedia/transport_srtp_dtls.c

    r6017 r6118  
    136136static const pj_str_t ID_FINGERPRINT  = { "fingerprint", 11 }; 
    137137 
     138/* Map of OpenSSL-pjmedia SRTP cryptos. Currently OpenSSL seems to 
     139 * support few cryptos only (based on ssl/d1_srtp.c of OpenSSL 1.1.0c). 
     140 */ 
     141#define OPENSSL_PROFILE_NUM 4 
     142 
     143static char* ossl_profiles[OPENSSL_PROFILE_NUM] = 
     144{ 
     145     "SRTP_AES128_CM_SHA1_80", 
     146     "SRTP_AES128_CM_SHA1_32", 
     147     "SRTP_AEAD_AES_256_GCM", 
     148     "SRTP_AEAD_AES_128_GCM" 
     149}; 
     150static char* pj_profiles[OPENSSL_PROFILE_NUM] = 
     151{ 
     152    "AES_CM_128_HMAC_SHA1_80", 
     153    "AES_CM_128_HMAC_SHA1_32", 
     154    "AEAD_AES_256_GCM", 
     155    "AEAD_AES_128_GCM" 
     156}; 
     157 
     158/* This will store the valid OpenSSL profiles which is mapped from  
     159 * OpenSSL-pjmedia SRTP cryptos. 
     160 */ 
     161static char *valid_pj_profiles_list[OPENSSL_PROFILE_NUM]; 
     162static char *valid_ossl_profiles_list[OPENSSL_PROFILE_NUM]; 
     163static unsigned valid_profiles_cnt; 
     164 
    138165 
    139166/* Certificate & private key */ 
     
    162189    } 
    163190 
     191    if (valid_profiles_cnt == 0) { 
     192        unsigned n, j; 
     193        int rc; 
     194        char *p, *end, buf[OPENSSL_PROFILE_NUM*25]; 
     195 
     196        /* Create DTLS context */ 
     197        SSL_CTX *ctx = SSL_CTX_new(DTLS_method()); 
     198        if (ctx == NULL) { 
     199            return PJ_ENOMEM; 
     200        } 
     201 
     202        p = buf; 
     203        end = buf + sizeof(buf); 
     204        for (j=0; j<PJ_ARRAY_SIZE(ossl_profiles); ++j) { 
     205            rc = SSL_CTX_set_tlsext_use_srtp(ctx, ossl_profiles[j]); 
     206            if (rc == 0) { 
     207                valid_pj_profiles_list[valid_profiles_cnt] = 
     208                    pj_profiles[j]; 
     209                valid_ossl_profiles_list[valid_profiles_cnt++] = 
     210                    ossl_profiles[j]; 
     211 
     212                n = pj_ansi_snprintf(p, end - p, ":%s", pj_profiles[j]); 
     213                p += n; 
     214            } 
     215        } 
     216        SSL_CTX_free(ctx); 
     217 
     218        if (valid_profiles_cnt > 0) { 
     219            PJ_LOG(4,("DTLS-SRTP", "%s profile is supported", buf)); 
     220        } else { 
     221            PJ_PERROR(4, ("DTLS-SRTP", PJMEDIA_SRTP_DTLS_ENOPROFILE, 
     222                          "Error getting SRTP profile")); 
     223 
     224            return PJMEDIA_SRTP_DTLS_ENOPROFILE; 
     225        } 
     226    } 
     227 
    164228    return PJ_SUCCESS; 
    165229} 
     
    174238        dtls_priv_key = NULL; 
    175239    } 
     240 
     241    valid_profiles_cnt = 0; 
    176242} 
    177243 
     
    352418} 
    353419 
    354  
    355 /* Map of OpenSSL-pjmedia SRTP cryptos. Currently OpenSSL seems to 
    356  * support few cryptos only (based on ssl/d1_srtp.c of OpenSSL 1.1.0c). 
    357  */ 
    358 static char* ossl_profiles[] = 
    359 { 
    360      "SRTP_AES128_CM_SHA1_80", 
    361      "SRTP_AES128_CM_SHA1_32", 
    362      "SRTP_AEAD_AES_256_GCM", 
    363      "SRTP_AEAD_AES_128_GCM" 
    364 }; 
    365 static char* pj_profiles[] = 
    366 { 
    367     "AES_CM_128_HMAC_SHA1_80", 
    368     "AES_CM_128_HMAC_SHA1_32", 
    369     "AEAD_AES_256_GCM", 
    370     "AEAD_AES_128_GCM" 
    371 }; 
    372  
    373  
    374420/* Create and initialize new SSL context and instance */ 
    375421static pj_status_t ssl_create(dtls_srtp *ds) 
     
    387433    if (ctx == NULL) { 
    388434        return GET_SSL_STATUS(ds); 
     435    } 
     436 
     437    if (valid_profiles_cnt == 0) {       
     438        return PJMEDIA_SRTP_DTLS_ENOPROFILE; 
    389439    } 
    390440 
     
    399449            pjmedia_srtp_crypto *crypto = &ds->srtp->setting.crypto[i]; 
    400450            unsigned j; 
    401             for (j=0; j<PJ_ARRAY_SIZE(pj_profiles); ++j) { 
    402                 if (!pj_ansi_strcmp(crypto->name.ptr, pj_profiles[j])) { 
    403                     n = pj_ansi_snprintf(p, end-p, ":%s", ossl_profiles[j]); 
     451            for (j=0; j < valid_profiles_cnt; ++j) { 
     452                if (!pj_ansi_strcmp(crypto->name.ptr, 
     453                                    valid_pj_profiles_list[j])) 
     454                { 
     455                    n = pj_ansi_snprintf(p, end-p, ":%s", 
     456                                         valid_ossl_profiles_list[j]); 
    404457                    p += n; 
    405458                    break; 
     
    410463        rc = SSL_CTX_set_tlsext_use_srtp(ctx, buf+1); 
    411464        PJ_LOG(4,(ds->base.name, "Setting crypto [%s], errcode=%d", buf, rc)); 
    412         pj_assert(rc == 0); 
     465        if (rc != 0) { 
     466            SSL_CTX_free(ctx); 
     467            return GET_SSL_STATUS(ds); 
     468        } 
    413469    } 
    414470 
Note: See TracChangeset for help on using the changeset viewer.