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