Mon Jun 18 2012 03:55:40

Asterisk developer's documentation


sdp_crypto.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2006 - 2007, Mikael Magnusson
00005  *
00006  * Mikael Magnusson <mikma@users.sourceforge.net>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file sdp_crypto.c
00020  *
00021  * \brief SDP Security descriptions
00022  *
00023  * Specified in RFC 4568
00024  *
00025  * \author Mikael Magnusson <mikma@users.sourceforge.net>
00026  */
00027 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 356604 $")
00031 
00032 #include "asterisk/options.h"
00033 #include "asterisk/utils.h"
00034 #include "include/sdp_crypto.h"
00035 
00036 #define SRTP_MASTER_LEN 30
00037 #define SRTP_MASTERKEY_LEN 16
00038 #define SRTP_MASTERSALT_LEN ((SRTP_MASTER_LEN) - (SRTP_MASTERKEY_LEN))
00039 #define SRTP_MASTER_LEN64 (((SRTP_MASTER_LEN) * 8 + 5) / 6 + 1)
00040 
00041 extern struct ast_srtp_res *res_srtp;
00042 extern struct ast_srtp_policy_res *res_srtp_policy;
00043 
00044 struct sdp_crypto {
00045    char *a_crypto;
00046    unsigned char local_key[SRTP_MASTER_LEN];
00047    char local_key64[SRTP_MASTER_LEN64];
00048 };
00049 
00050 static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, unsigned long ssrc, int inbound);
00051 
00052 static struct sdp_crypto *sdp_crypto_alloc(void)
00053 {
00054    struct sdp_crypto *crypto;
00055 
00056    return crypto = ast_calloc(1, sizeof(*crypto));
00057 }
00058 
00059 void sdp_crypto_destroy(struct sdp_crypto *crypto)
00060 {
00061    ast_free(crypto->a_crypto);
00062    crypto->a_crypto = NULL;
00063    ast_free(crypto);
00064 }
00065 
00066 struct sdp_crypto *sdp_crypto_setup(void)
00067 {
00068    struct sdp_crypto *p;
00069    int key_len;
00070    unsigned char remote_key[SRTP_MASTER_LEN];
00071 
00072    if (!ast_rtp_engine_srtp_is_registered()) {
00073       return NULL;
00074    }
00075 
00076    if (!(p = sdp_crypto_alloc())) {
00077       return NULL;
00078    }
00079 
00080    if (res_srtp->get_random(p->local_key, sizeof(p->local_key)) < 0) {
00081       sdp_crypto_destroy(p);
00082       return NULL;
00083    }
00084 
00085    ast_base64encode(p->local_key64, p->local_key, SRTP_MASTER_LEN, sizeof(p->local_key64));
00086 
00087    key_len = ast_base64decode(remote_key, p->local_key64, sizeof(remote_key));
00088 
00089    if (key_len != SRTP_MASTER_LEN) {
00090       ast_log(LOG_ERROR, "base64 encode/decode bad len %d != %d\n", key_len, SRTP_MASTER_LEN);
00091       ast_free(p);
00092       return NULL;
00093    }
00094 
00095    if (memcmp(remote_key, p->local_key, SRTP_MASTER_LEN)) {
00096       ast_log(LOG_ERROR, "base64 encode/decode bad key\n");
00097       ast_free(p);
00098       return NULL;
00099    }
00100 
00101    ast_debug(1 , "local_key64 %s len %zu\n", p->local_key64, strlen(p->local_key64));
00102 
00103    return p;
00104 }
00105 
00106 static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, unsigned long ssrc, int inbound)
00107 {
00108    const unsigned char *master_salt = NULL;
00109 
00110    if (!ast_rtp_engine_srtp_is_registered()) {
00111       return -1;
00112    }
00113 
00114    master_salt = master_key + SRTP_MASTERKEY_LEN;
00115    if (res_srtp_policy->set_master_key(policy, master_key, SRTP_MASTERKEY_LEN, master_salt, SRTP_MASTERSALT_LEN) < 0) {
00116       return -1;
00117    }
00118 
00119    if (res_srtp_policy->set_suite(policy, suite_val)) {
00120       ast_log(LOG_WARNING, "Could not set remote SRTP suite\n");
00121       return -1;
00122    }
00123 
00124    res_srtp_policy->set_ssrc(policy, ssrc, inbound);
00125 
00126    return 0;
00127 }
00128 
00129 static int sdp_crypto_activate(struct sdp_crypto *p, int suite_val, unsigned char *remote_key, struct ast_rtp_instance *rtp)
00130 {
00131    struct ast_srtp_policy *local_policy = NULL;
00132    struct ast_srtp_policy *remote_policy = NULL;
00133    struct ast_rtp_instance_stats stats = {0,};
00134    int res = -1;
00135 
00136    if (!ast_rtp_engine_srtp_is_registered()) {
00137       return -1;
00138    }
00139 
00140    if (!p) {
00141       return -1;
00142    }
00143 
00144    if (!(local_policy = res_srtp_policy->alloc())) {
00145       return -1;
00146    }
00147 
00148    if (!(remote_policy = res_srtp_policy->alloc())) {
00149       goto err;
00150    }
00151 
00152    if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_LOCAL_SSRC)) {
00153       goto err;
00154    }
00155 
00156    if (set_crypto_policy(local_policy, suite_val, p->local_key, stats.local_ssrc, 0) < 0) {
00157       goto err;
00158    }
00159 
00160    if (set_crypto_policy(remote_policy, suite_val, remote_key, 0, 1) < 0) {
00161       goto err;
00162    }
00163 
00164    /* Add the SRTP policies */
00165    if (ast_rtp_instance_add_srtp_policy(rtp, remote_policy, local_policy)) {
00166       ast_log(LOG_WARNING, "Could not set SRTP policies\n");
00167       goto err;
00168    }
00169 
00170    ast_debug(1 , "SRTP policy activated\n");
00171    res = 0;
00172 
00173 err:
00174    if (local_policy) {
00175       res_srtp_policy->destroy(local_policy);
00176    }
00177 
00178    if (remote_policy) {
00179       res_srtp_policy->destroy(remote_policy);
00180    }
00181 
00182    return res;
00183 }
00184 
00185 int sdp_crypto_process(struct sdp_crypto *p, const char *attr, struct ast_rtp_instance *rtp)
00186 {
00187    char *str = NULL;
00188    char *tag = NULL;
00189    char *suite = NULL;
00190    char *key_params = NULL;
00191    char *key_param = NULL;
00192    char *session_params = NULL;
00193    char *key_salt = NULL;
00194    char *lifetime = NULL;
00195    int found = 0;
00196    int attr_len = strlen(attr);
00197    int key_len = 0;
00198    int suite_val = 0;
00199    unsigned char remote_key[SRTP_MASTER_LEN];
00200 
00201    if (!ast_rtp_engine_srtp_is_registered()) {
00202       return -1;
00203    }
00204 
00205    str = ast_strdupa(attr);
00206 
00207    strsep(&str, ":");
00208    tag = strsep(&str, " ");
00209    suite = strsep(&str, " ");
00210    key_params = strsep(&str, " ");
00211    session_params = strsep(&str, " ");
00212 
00213    if (!tag || !suite) {
00214       ast_log(LOG_WARNING, "Unrecognized a=%s", attr);
00215       return -1;
00216    }
00217 
00218    if (session_params) {
00219       ast_log(LOG_WARNING, "Unsupported crypto parameters: %s", session_params);
00220       return -1;
00221    }
00222 
00223    if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_80")) {
00224       suite_val = AST_AES_CM_128_HMAC_SHA1_80;
00225    } else if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_32")) {
00226       suite_val = AST_AES_CM_128_HMAC_SHA1_32;
00227    } else {
00228       ast_log(LOG_WARNING, "Unsupported crypto suite: %s\n", suite);
00229       return -1;
00230    }
00231 
00232    while ((key_param = strsep(&key_params, ";"))) {
00233       char *method = NULL;
00234       char *info = NULL;
00235 
00236       method = strsep(&key_param, ":");
00237       info = strsep(&key_param, ";");
00238 
00239       if (!strcmp(method, "inline")) {
00240          key_salt = strsep(&info, "|");
00241          lifetime = strsep(&info, "|");
00242 
00243          if (lifetime) {
00244             ast_log(LOG_NOTICE, "Crypto life time unsupported: %s\n", attr);
00245             continue;
00246          }
00247 
00248          found = 1;
00249          break;
00250       }
00251    }
00252 
00253    if (!found) {
00254       ast_log(LOG_NOTICE, "SRTP crypto offer not acceptable\n");
00255       return -1;
00256    }
00257 
00258 
00259    if ((key_len = ast_base64decode(remote_key, key_salt, sizeof(remote_key))) != SRTP_MASTER_LEN) {
00260       ast_log(LOG_WARNING, "SRTP sdescriptions key %d != %d\n", key_len, SRTP_MASTER_LEN);
00261       return -1;
00262    }
00263 
00264    if (sdp_crypto_activate(p, suite_val, remote_key, rtp) < 0) {
00265       return -1;
00266    }
00267 
00268    if (!p->a_crypto) {
00269       if (!(p->a_crypto = ast_calloc(1, attr_len + 11))) {
00270          ast_log(LOG_ERROR, "Could not allocate memory for a_crypto\n");
00271          return -1;
00272       }
00273       snprintf(p->a_crypto, attr_len + 10, "a=crypto:%s %s inline:%s\r\n", tag, suite, p->local_key64);
00274    }
00275    return 0;
00276 }
00277 
00278 int sdp_crypto_offer(struct sdp_crypto *p)
00279 {
00280    char crypto_buf[128];
00281    const char *crypto_suite = "AES_CM_128_HMAC_SHA1_80"; /* Crypto offer */
00282 
00283    if (p->a_crypto) {
00284       ast_free(p->a_crypto);
00285    }
00286 
00287    if (snprintf(crypto_buf, sizeof(crypto_buf), "a=crypto:1 %s inline:%s\r\n",  crypto_suite, p->local_key64) < 1) {
00288       return -1;
00289    }
00290 
00291    if (!(p->a_crypto = ast_strdup(crypto_buf))) {
00292       return -1;
00293    }
00294 
00295    return 0;
00296 }
00297 
00298 const char *sdp_crypto_attrib(struct sdp_crypto *p)
00299 {
00300    return p->a_crypto;
00301 }