rfc2617.c

Go to the documentation of this file.
00001 /*
00002  * $Id: rfc2617.c 570 2008-06-25 13:21:32Z vingarzan $
00003  *
00004  * Digest response calculation as per RFC2617
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of ser, a free SIP server.
00009  *
00010  * ser is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * For a license to use the ser software under conditions
00016  * other than those described here, or to purchase support for this
00017  * software, please contact iptel.org by e-mail at the following addresses:
00018  *    info@iptel.org
00019  *
00020  * ser is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License 
00026  * along with this program; if not, write to the Free Software 
00027  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028  */
00029 
00030 
00040 #include <sys/types.h>
00041 #include <stdlib.h>
00042 #include <string.h>
00043 #include <stdio.h>
00044 
00045 #include "rfc2617.h"
00046 #include "../../md5global.h"
00047 #include "../../md5.h"
00048 
00049 
00050 static char hexa_chars[17]="0123456789abcdef";
00051 
00052 inline void cvt_hex(HASH _b, HASHHEX _h)
00053 {
00054     unsigned short i;   
00055     for (i = 0; i < HASHLEN; i++) {
00056         _h[i * 2] = hexa_chars[(_b[i] >> 4) & 0xf];
00057         _h[i * 2 + 1] =  hexa_chars[_b[i] & 0xf];
00058     };
00059     _h[HASHHEXLEN] = '\0';
00060 }
00061 
00062 
00063 /* 
00064  * calculate H(A1) as per spec 
00065  */
00066 void calc_HA1(ha_alg_t _alg, str* _username, str* _realm, str* _password,
00067           str* _nonce, str* _cnonce, HASHHEX _sess_key)
00068 {
00069     MD5_CTX Md5Ctx;
00070     HASH HA1;
00071     MD5Init(&Md5Ctx);
00072     MD5Update(&Md5Ctx, _username->s, _username->len);
00073     MD5Update(&Md5Ctx, ":", 1);
00074     MD5Update(&Md5Ctx, _realm->s, _realm->len);
00075     MD5Update(&Md5Ctx, ":", 1);
00076     MD5Update(&Md5Ctx, _password->s, _password->len);
00077     MD5Final(HA1, &Md5Ctx);
00078 
00079     if (_alg == HA_MD5_SESS) {
00080         MD5Init(&Md5Ctx);
00081         MD5Update(&Md5Ctx, HA1, HASHLEN);
00082         MD5Update(&Md5Ctx, ":", 1);
00083         MD5Update(&Md5Ctx, _nonce->s, _nonce->len);
00084         MD5Update(&Md5Ctx, ":", 1);
00085         MD5Update(&Md5Ctx, _cnonce->s, _cnonce->len);
00086         MD5Final(HA1, &Md5Ctx);
00087     };
00088 
00089     cvt_hex(HA1, _sess_key);
00090 }
00091 
00092 void calc_H(str *ent, HASHHEX hash)
00093 {
00094     MD5_CTX Md5Ctx;
00095     HASH HA1;
00096     MD5Init(&Md5Ctx);
00097     MD5Update(&Md5Ctx, ent->s, ent->len);
00098     MD5Final(HA1, &Md5Ctx);
00099     cvt_hex(HA1, hash);
00100 }
00101 
00102 /* 
00103  * calculate request-digest/response-digest as per HTTP Digest spec 
00104  */
00105 void calc_response(HASHHEX _ha1,      /* H(A1) */
00106            str* _nonce,       /* nonce from server */
00107            str* _nc,          /* 8 hex digits */
00108            str* _cnonce,      /* client nonce */
00109            str* _qop,         /* qop-value: "", "auth", "auth-int" */
00110            int _auth_int,     /* 1 if auth-int is used */
00111            str* _method,      /* method from the request */
00112            str* _uri,         /* requested URL */
00113            HASHHEX _hentity,  /* H(entity body) if qop="auth-int" */
00114            HASHHEX _response) /* request-digest or response-digest */
00115 {
00116     MD5_CTX Md5Ctx;
00117     HASH HA2;
00118     HASH RespHash;
00119     HASHHEX HA2Hex;
00120     
00121          /* calculate H(A2) */
00122     MD5Init(&Md5Ctx);
00123     MD5Update(&Md5Ctx, _method->s, _method->len);
00124     MD5Update(&Md5Ctx, ":", 1);
00125     MD5Update(&Md5Ctx, _uri->s, _uri->len);
00126 
00127     if (_auth_int) {
00128         MD5Update(&Md5Ctx, ":", 1);
00129         MD5Update(&Md5Ctx, _hentity, HASHHEXLEN);
00130     };
00131 
00132     MD5Final(HA2, &Md5Ctx);
00133     cvt_hex(HA2, HA2Hex);
00134     
00135          /* calculate response */
00136     MD5Init(&Md5Ctx);
00137     MD5Update(&Md5Ctx, _ha1, HASHHEXLEN);
00138     MD5Update(&Md5Ctx, ":", 1);
00139     MD5Update(&Md5Ctx, _nonce->s, _nonce->len);
00140     MD5Update(&Md5Ctx, ":", 1);
00141 
00142     if (_qop->len) {
00143         MD5Update(&Md5Ctx, _nc->s, _nc->len);
00144         MD5Update(&Md5Ctx, ":", 1);
00145         MD5Update(&Md5Ctx, _cnonce->s, _cnonce->len);
00146         MD5Update(&Md5Ctx, ":", 1);
00147         MD5Update(&Md5Ctx, _qop->s, _qop->len);
00148         MD5Update(&Md5Ctx, ":", 1);
00149     };
00150     MD5Update(&Md5Ctx, HA2Hex, HASHHEXLEN);
00151     MD5Final(RespHash, &Md5Ctx);
00152     cvt_hex(RespHash, _response);
00153 }

Generated on Thu Oct 23 04:14:39 2008 for Open IMS Core CSCFs by  doxygen 1.5.2