[SIPForum-discussion] help about osip2 develop www_authenticate event

benjiamsh benjiamsh at 163.com
Wed Jun 25 02:25:56 UTC 2008


dear all
     i am developing one sip phone base osip2 lib.  now i got one promble about www_authenticate. can you help me?
  
 when i got 401 unauther event from pbx. i know i should send my www_authenticate data.

  this is one example in my case (it work is right)
////////////////////////////
the x-lite send to pbx

Authorization: Digest username="6001",realm="asterisk",
nonce="46803264",
uri="sip:192.168.0.73",
response="34474aec8a07b1cd12c6025e2d4d48d6",
algorithm=MD5

///////////////////////////


my user name is 6001  and my password is 6001
from the rfc i know 

    //step1:caculate username:realm:password MD5 value to get HA1
    //step2:caculate method:uri MD5 value to get HA2
    //step3:caculate HA1:nonce:HA2 MD5 value, it's the result


from the event , i know 
username is 6001
realm     is asterisk
uri is      sip:192.168.0.73
password   6001      
method      MD5
nonce       46803264
   
void getresponse(
    char *username,      
    char *passwd,     //
    char *realm,        //
    char *method,     //meth
    char *uri,
    char *nonce,
    char *response
);

void getresponse(
    char *username,     //用户名
    char *passwd,       //口令
    char *realm,        //realm
    char *method,       //method
    char *uri,
    char *nonce,
    char *response
)
{
    MD5_CTX Md5Ctx;
    MD5Init(&Md5Ctx);


    #define bufflens 256
    unsigned char ha1buff[bufflens] = {0};
    unsigned char ha2buff[bufflens] = {0};
    unsigned char response1[bufflens] = {0};
    unsigned char response2[bufflens] = {0};
    unsigned char response3[bufflens] = {0};

    char buff[1024] = {0};
    //unsigned char response[1024] = {0};
    //step1:caculate username:realm:password MD5 value to get HA1

  
    MD5Update(&Md5Ctx, (unsigned char *)username, strlen(username));
    MD5Update(&Md5Ctx, (unsigned char *)":", 1);
    MD5Update(&Md5Ctx, (unsigned char *)realm, strlen(realm));
    MD5Update(&Md5Ctx, (unsigned char *)":", 1);
    MD5Update(&Md5Ctx, (unsigned char *)passwd, strlen(passwd));
 
    MD5Final(response1, &Md5Ctx);
    CvtHex((char *)response1, (char *)ha1buff);
    printf("%s\n",ha1buff);
    // step2:caculate method:uri MD5 value to get HA2
    MD5Init(&Md5Ctx);
  
    MD5Update(&Md5Ctx, (unsigned char *)method, strlen(method));
    MD5Update(&Md5Ctx, (unsigned char *)":", 1);
    MD5Update(&Md5Ctx, (unsigned char *)uri, strlen(uri));
 
       MD5Final(response2, &Md5Ctx);
    CvtHex((char *)response2, (char *)ha2buff);
    printf("%s\n",ha2buff);
 

    //step3:caculate HA1:nonce:HA2 MD5 value, it's the result

    MD5Init(&Md5Ctx);

    MD5Update(&Md5Ctx,  &ha1buff[0], 32);
    MD5Update(&Md5Ctx, (unsigned char *)":", 1);
    MD5Update(&Md5Ctx, (unsigned char *)nonce, strlen(nonce));
    MD5Update(&Md5Ctx, (unsigned char *)":", 1);
    MD5Update(&Md5Ctx,  &ha2buff[0], 32);
  

     MD5Final(response3, &Md5Ctx);
    CvtHex((char *)response3, (char *)ha2buff);
    printf("%s\n",ha2buff);

}

void
CvtHex (IN char * Bin, OUT char * Hex)
{
  unsigned short i;
  unsigned char j;

  for (i = 0; i < HASHLEN; i++)
    {
      j = (Bin[i] >> 4) & 0xf;
      if (j <= 9)
        Hex[i * 2] = (j + '0');
      else
        Hex[i * 2] = (j + 'a' - 10);
      j = Bin[i] & 0xf;
      if (j <= 9)
        Hex[i * 2 + 1] = (j + '0');
      else
        Hex[i * 2 + 1] = (j + 'a' - 10);
    };
  Hex[HASHHEXLEN] = '\0';
}

call 
getresponse(        "6001",       //用户名
        "6001",       //口令
        "asterisk",   //realm
        "MD5",        //meth
        "sip:192.168.0.73", 
        "46803264",
        NULL
        );

but i can not get "34474aec8a07b1cd12c6025e2d4d48d6", i think some wrng, can you help me ? thanks

    best regards!
benjiam
2008-06-25 



benjiamsh 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://sipforum.org/pipermail/discussion/attachments/20080625/39c538ec/attachment-0002.html>


More information about the discussion mailing list