/[resiprocate]/main/resip/stack/test/testDigestAuthentication2.cxx
ViewVC logotype

Contents of /main/resip/stack/test/testDigestAuthentication2.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5948 - (show annotations) (download)
Fri Feb 17 15:30:16 2006 UTC (13 years, 8 months ago) by dworley
File MIME type: text/plain
File size: 8364 byte(s)
Setting more svn: properties, and adding EOLs to the ends of files
that need it.

1 #include <assert.h>
2 #include <iostream>
3 #include <string.h>
4 #ifdef WIN32
5 #include <io.h>
6 #else
7 #include <unistd.h>
8 #endif
9 #include <memory>
10
11 #ifdef WIN32
12 #define usleep(x) Sleep(x/1000)
13 #define sleep(x) Sleep(x*1000)
14 #endif
15
16 #include "resip/stack/HeaderFieldValue.hxx"
17 #include "resip/stack/HeaderTypes.hxx"
18 #include "resip/stack/ParserCategories.hxx"
19 #include "resip/stack/Uri.hxx"
20 #include "resip/stack/Helper.hxx"
21 #include "resip/stack/test/TestSupport.hxx"
22 #include "rutil/Timer.hxx"
23 #include "rutil/DataStream.hxx"
24 #include "rutil/MD5Stream.hxx"
25 #include "digcalc.hxx"
26 #include "rutil/Logger.hxx"
27
28 #define RESIPROCATE_SUBSYSTEM resip::Subsystem::TEST
29
30 using namespace std;
31 using namespace resip;
32
33 int
34 main(int arc, char** argv)
35 {
36
37 {
38 char* alg = "MD5";
39 char* username = "user";
40 char* password = "secret";
41 char* realm = "localhost";
42 char* method = "REGISTER";
43 char* uri = "user@host.com";
44 char* nonce = "92347fea23";
45
46
47 MD5Stream a1;
48 a1 << username
49 << Symbols::COLON
50 << realm
51 << Symbols::COLON
52 << password;
53 Data passwordHashA1 = a1.getHex();
54
55
56 Data responseMD5withA1 = Helper::makeResponseMD5WithA1(passwordHashA1,
57 method,
58 uri,
59 nonce);
60
61
62 Data responseMD5 = Helper::makeResponseMD5(username,
63 password,
64 realm,
65 method,
66 uri,
67 nonce);
68
69 HASHHEX a1Hash;
70 HASHHEX response;
71
72 DigestCalcHA1(alg,
73 username,
74 realm,
75 password,
76 nonce,
77 (char*)"",
78 a1Hash);
79
80 DigestCalcResponse(a1Hash,
81 nonce,
82 (char*)"",
83 (char*)"",
84 (char*)"",
85 method,
86 uri,
87 (char*)"",
88 response);
89
90 assert(responseMD5 == response);
91 assert(responseMD5withA1 == response);
92 }
93
94
95
96 {
97 Data txt("INVITE sip:bob@biloxi.com SIP/2.0\r\n"
98 "Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKnashds8\r\n"
99 "To: Bob <sip:bob@biloxi.com>\r\n"
100 "From: Alice <sip:alice@atlanta.com>;tag=1928301774\r\n"
101 "Call-ID: a84b4c76e66710\r\n"
102 "CSeq: 314159 INVITE\r\n"
103 "Max-Forwards: 70\r\n"
104 "Contact: <sip:alice@pc33.atlanta.com>\r\n"
105 "Content-Type: application/sdp\r\n"
106 "Content-Length: 150\r\n"
107 "\r\n"
108 "v=0\r\n"
109 "o=alice 53655765 2353687637 IN IP4 pc33.atlanta.com\r\n"
110 "s=-\r\n"
111 "c=IN IP4 pc33.atlanta.com\r\n"
112 "t=0 0\r\n"
113 "m=audio 3456 RTP/AVP 0 1 3 99\r\n"
114 "a=rtpmap:0 PCMU/8000\r\n");
115
116 auto_ptr<SipMessage> request(TestSupport::makeMessage(txt.c_str()));
117
118 Data realm = "localhost";
119 auto_ptr<SipMessage> challenge(Helper::makeProxyChallenge(*request, realm, false));
120
121 assert(challenge->exists(h_ProxyAuthenticates));
122 assert(challenge->header(h_ProxyAuthenticates).size() == 1);
123
124 Data username = "bob";
125 Data password = "secret";
126 Data cnonce;// = "366fead6";
127 unsigned int nc = 0;
128 MD5Stream a1;
129 a1 << username
130 << Symbols::COLON
131 << realm
132 << Symbols::COLON
133 << password;
134 Data passwordHashA1 = a1.getHex();
135 InfoLog (<< "passwordHashA1=" << passwordHashA1);
136 Data cnonceRet;
137 request->header(h_ProxyAuthorizations).push_back( Helper::makeChallengeResponseAuthWithA1(*request,
138 username,
139 passwordHashA1,
140 *(challenge->header(h_ProxyAuthenticates).begin()),
141 cnonce,
142 nc,
143 cnonceRet));
144
145 assert(request->exists(h_ProxyAuthorizations));
146 assert(request->header(h_ProxyAuthorizations).size() == 1);
147 assert(!request->header(h_ProxyAuthorizations).front().exists(p_qop));
148
149 const Auth& auth = request->header(h_ProxyAuthorizations).front();
150
151 assert(auth.param(p_username) == "bob");
152 assert(auth.param(p_uri) == "sip:bob@biloxi.com");
153 assert(auth.param(p_algorithm) == "MD5");
154
155 Helper::AuthResult res = Helper::authenticateRequest(*request,
156 realm,
157 password);
158 assert(res == Helper::Authenticated);
159
160 res = Helper::authenticateRequest(*request,
161 realm,
162 password.md5(),
163 5);
164
165 assert(res == Helper::Authenticated);
166
167 sleep(2);
168 res = Helper::authenticateRequest(*request,
169 realm,
170 password.md5(),
171 1);
172
173 assert(res == Helper::Expired);
174
175 }
176
177 cerr << "ALL OK" << endl;
178 return 0;
179 }
180 /* ====================================================================
181 * The Vovida Software License, Version 1.0
182 *
183 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
184 *
185 * Redistribution and use in source and binary forms, with or without
186 * modification, are permitted provided that the following conditions
187 * are met:
188 *
189 * 1. Redistributions of source code must retain the above copyright
190 * notice, this list of conditions and the following disclaimer.
191 *
192 * 2. Redistributions in binary form must reproduce the above copyright
193 * notice, this list of conditions and the following disclaimer in
194 * the documentation and/or other materials provided with the
195 * distribution.
196 *
197 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
198 * and "Vovida Open Communication Application Library (VOCAL)" must
199 * not be used to endorse or promote products derived from this
200 * software without prior written permission. For written
201 * permission, please contact vocal@vovida.org.
202 *
203 * 4. Products derived from this software may not be called "VOCAL", nor
204 * may "VOCAL" appear in their name, without prior written
205 * permission of Vovida Networks, Inc.
206 *
207 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
208 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
209 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
210 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
211 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
212 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
213 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
214 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
215 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
216 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
217 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
218 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
219 * DAMAGE.
220 *
221 * ====================================================================
222 *
223 * This software consists of voluntary contributions made by Vovida
224 * Networks, Inc. and many individuals on behalf of Vovida Networks,
225 * Inc. For more information on Vovida Networks, Inc., please see
226 * <http://www.vovida.org/>.
227 *
228 */

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