现在的位置: 首页 > 综合 > 正文

qutecom注册到asterisk上,sip信令使用rc4加密方法

2018年02月11日 ⁄ 综合 ⁄ 共 5965字 ⁄ 字号 评论关闭

qutecom 一个开源的voip客户端

asterisk 开源的ippbx

rc4加密算法简单,快速,据说是比DES算法快10倍。sip 信令本身就属于明文方式传输的,之所以要加密,是为了防止运营商的干扰,使用一个弱的加密算法,是要能防止串改就满足要求了。

rc4 算法可以google原来,用密钥来生成一个256长度的box, 然后box与明文异或操作得到密文,密文再次异或就恢复明文。

下面实现了 qutecom 到asterisk 信令的当向加密,反向的目前还没弄完,等完工了在来补充。

 

rc4.h

/*
 *RC4 functions for HTMLDOC.
 *
 *   Original code by Rob Earhart
 *   Copyright 1999 by Carnegie Mellon University, All Rights Reserved
 *
 *   Permission to use, copy, modify, and distribute this software and its
 *   documentation for any purpose and without fee is hereby granted,
 *   provided that the above copyright notice appear in all copies and that
 *   both that copyright notice and this permission notice appear in
 *   supporting documentation, and that the name of Carnegie Mellon
 *   University not be used in advertising or publicity pertaining to
 *   distribution of the software without specific, written prior
 *   permission.
 *
 *   CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 *   THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 *   FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR
 *   ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 *   OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _RC4_H_
#  define _RC4_H_

#  ifdef __cplusplus
extern "C" {
#  endif /* __cplusplus */

/*
 * RC4 context...
 */

typedef struct
{
  unsigned char    sbox[256];    /* S boxes for encryption */
  int        i, j;        /* Current indices into S boxes */
} rc4_context_t;

/*
 * Prototypes...
 */

extern void    rc4_init(rc4_context_t *context, const unsigned char *key,
             unsigned keylen);
extern void    rc4_encrypt(rc4_context_t *context, const unsigned char *input,
                unsigned char *output, unsigned len);

#  ifdef __cplusplus
}
#  endif /* __cplusplus */

#endif /* !_RC4_H_ */

rc4.c

/*
 * RC4 functions for HTMLDOC.
 *
 *   Original code by Tim Martin
 *   Copyright 1999 by Carnegie Mellon University, All Rights Reserved
 *
 *   Permission to use, copy, modify, and distribute this software and its
 *   documentation for any purpose and without fee is hereby granted,
 *   provided that the above copyright notice appear in all copies and that
 *   both that copyright notice and this permission notice appear in
 *   supporting documentation, and that the name of Carnegie Mellon
 *   University not be used in advertising or publicity pertaining to
 *   distribution of the software without specific, written prior
 *   permission.
 *
 *   CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 *   THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 *   FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR
 *   ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 *   OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Contents:
 *
 *   rc4_init()    - Initialize an RC4 context with the specified key.
 *   rc4_encrypt() - Encrypt the given buffer.
 */

#include "rc4.h"

/*
 * 'rc4_init()' - Initialize an RC4 context with the specified key.
 */

void
rc4_init(rc4_context_t       *text,    /* IO - Context */
         const unsigned char *key,    /* I - Key */
         unsigned            keylen)    /* I - Length of key */
{
  int        i, j;            /* Looping vars */
  unsigned char    tmp;            /* Temporary variable */

 /*
  * Fill in linearly s0=0, s1=1, ...
  */

  for (i = 0; i < 256; i ++)
    text->sbox[i] = i;

  for (i = 0, j = 0; i < 256; i ++)
  {
   /*
    * j = (j + Si + Ki) mod 256
    */

    j = (j + text->sbox[i] + key[i % keylen]) & 255;

   /*
    * Swap Si and Sj...
    */

    tmp           = text->sbox[i];
    text->sbox[i] = text->sbox[j];
    text->sbox[j] = tmp;
  }

 /*
  * Initialized counters to 0 and return...
  */

  text->i = 0;
  text->j = 0;
}

/*
 * 'rc4_encrypt()' - Encrypt the given buffer.
 */

void
rc4_encrypt(rc4_context_t       *text,        /* I - Context */
        const unsigned char *input,        /* I - Input buffer */
        unsigned char       *output,    /* O - Output buffer */
        unsigned            len)        /* I - Size of buffers */
{
  unsigned char        tmp;            /* Swap variable */
  int            i, j;            /* Looping vars */
  int            t;            /* Current S box */

 /*
  * Loop through the entire buffer...
  */

  i = text->i;
  j = text->j;

  while (len > 0)
  {
   /*
    * Get the next S box indices...
    */

    i = (i + 1) & 255;
    j = (j + text->sbox[i]) & 255;

   /*
    * Swap Si and Sj...
    */

    tmp           = text->sbox[i];
    text->sbox[i] = text->sbox[j];
    text->sbox[j] = tmp;

   /*
    * Get the S box index for this byte...
    */

    t = (text->sbox[i] + text->sbox[j]) & 255;

   /*
    * Encrypt using the S box...
    */

    *output++ = *input++ ^ text->sbox[t];
    len --;
  }

 /*
  * Copy current S box indices back to context...
  */

  text->i = i;
  text->j = j;
}

 

 

修改exosip项目中的 jcallback.c 在函数cb_udp_snd_message 中修改,加入rc4加密部分

....

    if( 1 )
    {
        rc4_context_t context;
        char * key = "*****";
        unsigned char * out = NULL;
        int i=0;
        out = osip_malloc (length);
        if (out == NULL)
            return -1;
        rc4_init(&context,key,16);
        rc4_encrypt(&context,message,out,length);
       
        rc4_message = osip_malloc(length+4);
        if(rc4_message != NULL)
        {
            rc4_message[0] = 'R';
            rc4_message[1] = 'C';
            rc4_message[2] = '4';
            rc4_message[3] = ':';
            for(i=0;i<length;i++)
            {
                rc4_message[i+4] = out[i];
            }
        }
        osip_free(out);
    }

    // Really send the packet over network
    if(rc4_message == NULL)
    {
        i = owsip_send (account, (const void*) message, length, 0, address, OWSL_ADDRESS_SIZE);
    }
    else
    {
        i = owsip_send (account, (const void*) rc4_message, length+4, 0, address, OWSL_ADDRESS_SIZE);
        osip_free(rc4_message);
    }

....

 

 

 

在asterisk 中的chan_sip.c 修改函数 sipsock_read, 添加 接受信令rc4解密代码

.....
        if(res>4 && req.data[0]=='R' && req.data[1]=='C' && req.data[2]=='4' && req.data[3]==':')
        {
                rc4_context_t context;
                char * key = "********";
                unsigned char * out = NULL;
                int i=0;
                out = malloc(res-4);
                rc4_init(&context,key,16);
                rc4_encrypt(&context,req.data+4,out,res-4);
                for(i=0;i<res-4;i++)
                {
                        req.data[i] = out[i];
                }
                free(out);
                req.data[res-4] = '/0';
                res = res-4;
                req.len = res;
        }

.....

 

 

 

 

抱歉!评论已关闭.