/[resiprocate]/main/resip/stack/test/digcalc.hxx
ViewVC logotype

Contents of /main/resip/stack/test/digcalc.hxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8431 - (show annotations) (download)
Wed Mar 4 21:58:34 2009 UTC (10 years, 8 months ago) by adam
File MIME type: text/plain
File size: 7059 byte(s)
Fix for digest test driver

1 #ifndef digcalc_hxx
2 #define digcalc_hxx
3
4
5 #include "rutil/vmd5.hxx"
6 #include <string.h>
7 #include <iostream>
8 #include "rutil/Data.hxx"
9
10 #define HASHLEN 16
11 typedef char HASH[HASHLEN];
12 #define HASHHEXLEN 32
13 typedef char HASHHEX[HASHHEXLEN+1];
14 #define IN const
15 #define OUT
16
17
18 using namespace std;
19
20 namespace resip
21 {
22
23 typedef MD5Context MD5_CTX;
24
25
26 void
27 MD5_Update(struct MD5Context *ctx, char const *buf, unsigned len)
28 {
29 MD5Update(ctx, (unsigned char*)(buf), len);
30 }
31
32 void
33 MD5_Final(char digest[16], struct MD5Context *ctx)
34 {
35 MD5Final((unsigned char*)digest, ctx);
36 }
37
38 /* calculate H(A1) as per HTTP Digest spec */
39 void DigestCalcHA1(
40 IN char * pszAlg,
41 IN char * pszUserName,
42 IN char * pszRealm,
43 IN char * pszPassword,
44 IN char * pszNonce,
45 IN char * pszCNonce,
46 OUT HASHHEX SessionKey
47 );
48
49 /* calculate request-digest/response-digest as per HTTP Digest spec */
50 void DigestCalcResponse(
51 IN HASHHEX HA1, /* H(A1) */
52 IN char * pszNonce, /* nonce from server */
53 IN char * pszNonceCount, /* 8 hex digits */
54 IN char * pszCNonce, /* client nonce */
55 IN char * pszQop, /* qop-value: "", "auth", "auth-int" */
56 IN char * pszMethod, /* method from the request */
57 IN char * pszDigestUri, /* requested URL */
58 IN HASHHEX HEntity, /* H(entity body) if qop="auth-int" */
59 OUT HASHHEX Response /* request-digest or response-digest */
60 );
61
62 void CvtHex(
63 IN HASH Bin,
64 OUT HASHHEX Hex
65 )
66 {
67 unsigned short i;
68 unsigned char j;
69
70 for (i = 0; i < HASHLEN; i++) {
71 j = (Bin[i] >> 4) & 0xf;
72 if (j <= 9)
73 Hex[i*2] = (j + '0');
74 else
75 Hex[i*2] = (j + 'a' - 10);
76 j = Bin[i] & 0xf;
77 if (j <= 9)
78 Hex[i*2+1] = (j + '0');
79 else
80 Hex[i*2+1] = (j + 'a' - 10);
81 };
82 Hex[HASHHEXLEN] = '\0';
83 };
84
85 /* calculate H(A1) as per spec */
86 void DigestCalcHA1(
87 IN char * pszAlg,
88 IN char * pszUserName,
89 IN char * pszRealm,
90 IN char * pszPassword,
91 IN char * pszNonce,
92 IN char * pszCNonce,
93 OUT HASHHEX SessionKey
94 )
95 {
96 MD5_CTX Md5Ctx;
97 HASH HA1;
98
99 MD5Init(&Md5Ctx);
100 MD5_Update(&Md5Ctx, pszUserName, strlen(pszUserName));
101 MD5_Update(&Md5Ctx, ":", 1);
102 MD5_Update(&Md5Ctx, pszRealm, strlen(pszRealm));
103 MD5_Update(&Md5Ctx, ":", 1);
104 MD5_Update(&Md5Ctx, pszPassword, strlen(pszPassword));
105 MD5_Final(HA1, &Md5Ctx);
106 if (strcasecmp(pszAlg, "md5-sess") == 0) {
107 MD5Init(&Md5Ctx);
108 MD5_Update(&Md5Ctx, HA1, HASHLEN);
109 MD5_Update(&Md5Ctx, ":", 1);
110 MD5_Update(&Md5Ctx, pszNonce, strlen(pszNonce));
111 MD5_Update(&Md5Ctx, ":", 1);
112 MD5_Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
113 MD5_Final(HA1, &Md5Ctx);
114 };
115 CvtHex(HA1, SessionKey);
116 };
117
118 /* calculate request-digest/response-digest as per HTTP Digest spec */
119 void DigestCalcResponse(
120 IN HASHHEX HA1, /* H(A1) */
121 IN char * pszNonce, /* nonce from server */
122 IN char * pszNonceCount, /* 8 hex digits */
123 IN char * pszCNonce, /* client nonce */
124 IN char * pszQop, /* qop-value: "", "auth", "auth-int" */
125 IN char * pszMethod, /* method from the request */
126 IN char * pszDigestUri, /* requested URL */
127 IN HASHHEX HEntity, /* H(entity body) if qop="auth-int" */
128 OUT HASHHEX Response /* request-digest or response-digest */
129 )
130 {
131 MD5_CTX Md5Ctx;
132 HASH HA2;
133 HASH RespHash;
134 HASHHEX HA2Hex;
135
136 // calculate H(A2)
137 MD5Init(&Md5Ctx);
138 MD5_Update(&Md5Ctx, pszMethod, strlen(pszMethod));
139 MD5_Update(&Md5Ctx, ":", 1);
140 MD5_Update(&Md5Ctx, pszDigestUri, strlen(pszDigestUri));
141 if (strcasecmp(pszQop, "auth-int") == 0) {
142 MD5_Update(&Md5Ctx, ":", 1);
143 MD5_Update(&Md5Ctx, HEntity, HASHHEXLEN);
144 };
145 MD5_Final(HA2, &Md5Ctx);
146 CvtHex(HA2, HA2Hex);
147
148
149 // calculate response
150 MD5Init(&Md5Ctx);
151 MD5_Update(&Md5Ctx, HA1, HASHHEXLEN);
152 MD5_Update(&Md5Ctx, ":", 1);
153 MD5_Update(&Md5Ctx, pszNonce, strlen(pszNonce));
154 MD5_Update(&Md5Ctx, ":", 1);
155 if (*pszQop) {
156 MD5_Update(&Md5Ctx, pszNonceCount, strlen(pszNonceCount));
157 MD5_Update(&Md5Ctx, ":", 1);
158 MD5_Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
159 MD5_Update(&Md5Ctx, ":", 1);
160 MD5_Update(&Md5Ctx, pszQop, strlen(pszQop));
161 MD5_Update(&Md5Ctx, ":", 1);
162 };
163 MD5_Update(&Md5Ctx, HA2Hex, HASHHEXLEN);
164 MD5_Final(RespHash, &Md5Ctx);
165 CvtHex(RespHash, Response);
166 };
167
168
169 }
170
171
172 #endif
173 /* ====================================================================
174 * The Vovida Software License, Version 1.0
175 *
176 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
177 *
178 * Redistribution and use in source and binary forms, with or without
179 * modification, are permitted provided that the following conditions
180 * are met:
181 *
182 * 1. Redistributions of source code must retain the above copyright
183 * notice, this list of conditions and the following disclaimer.
184 *
185 * 2. Redistributions in binary form must reproduce the above copyright
186 * notice, this list of conditions and the following disclaimer in
187 * the documentation and/or other materials provided with the
188 * distribution.
189 *
190 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
191 * and "Vovida Open Communication Application Library (VOCAL)" must
192 * not be used to endorse or promote products derived from this
193 * software without prior written permission. For written
194 * permission, please contact vocal@vovida.org.
195 *
196 * 4. Products derived from this software may not be called "VOCAL", nor
197 * may "VOCAL" appear in their name, without prior written
198 * permission of Vovida Networks, Inc.
199 *
200 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
201 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
202 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
203 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
204 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
205 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
206 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
207 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
208 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
209 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
210 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
211 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
212 * DAMAGE.
213 *
214 * ====================================================================
215 *
216 * This software consists of voluntary contributions made by Vovida
217 * Networks, Inc. and many individuals on behalf of Vovida Networks,
218 * Inc. For more information on Vovida Networks, Inc., please see
219 * <http://www.vovida.org/>.
220 *
221 */

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27