Coverage Report

Created: 2025-06-13 06:12

/src/resiprocate/resip/stack/Auth.cxx
Line
Count
Source (jump to first uncovered line)
1
#if defined(HAVE_CONFIG_H)
2
#include "config.h"
3
#endif
4
5
#include "resip/stack/Auth.hxx"
6
#include "resip/stack/UnknownParameter.hxx"
7
#include "rutil/Data.hxx"
8
#include "rutil/DnsUtil.hxx"
9
#include "rutil/Logger.hxx"
10
#include "rutil/ParseBuffer.hxx"
11
//#include "rutil/WinLeakCheck.hxx"  // not compatible with placement new used below
12
13
using namespace resip;
14
using namespace std;
15
16
#define RESIPROCATE_SUBSYSTEM Subsystem::SIP
17
18
//====================
19
// Auth:
20
//====================
21
Auth::Auth() : 
22
0
   ParserCategory() 
23
0
{}
24
25
Auth::Auth(const HeaderFieldValue& hfv, Headers::Type type, PoolBase* pool) 
26
6.29k
   : ParserCategory(hfv, type, pool) 
27
6.29k
{}
28
29
Auth::Auth(const Auth& rhs, PoolBase* pool)
30
0
   : ParserCategory(rhs, pool),
31
0
   mScheme(rhs.mScheme)
32
0
{}
33
34
Auth&
35
Auth::operator=(const Auth& rhs)
36
0
{
37
0
   if (this != &rhs)
38
0
   {
39
0
      ParserCategory::operator=(rhs);
40
0
      mScheme = rhs.mScheme;
41
0
   }
42
0
   return *this;
43
0
}
44
45
Data& 
46
Auth::scheme()
47
0
{
48
0
   checkParsed(); 
49
0
   return mScheme;
50
0
}
51
52
const Data& 
53
Auth::scheme() const 
54
0
{
55
0
   checkParsed(); 
56
0
   return mScheme;
57
0
}
58
59
void
60
Auth::parse(ParseBuffer& pb)
61
6.29k
{
62
6.29k
   const char* start;
63
6.29k
   start = pb.skipWhitespace();
64
6.29k
   static const std::bitset<256> schemeDelimiter(Data::toBitset("\r\n\t ="));
65
6.29k
   pb.skipToOneOf(schemeDelimiter);
66
67
6.29k
   if (!pb.eof() && *pb.position() == Symbols::EQUALS[0])
68
4.21k
   {
69
      // Authentication-Info only
70
      // back up, and then parse
71
4.21k
      pb.reset(start);
72
4.21k
      parseAuthParameters(pb);
73
4.21k
   }
74
2.08k
   else
75
2.08k
   {
76
      // everything else
77
2.08k
      pb.data(mScheme, start);
78
79
2.08k
      pb.skipWhitespace();
80
2.08k
      parseAuthParameters(pb);
81
2.08k
   }
82
6.29k
}
83
84
EncodeStream& 
85
Auth::encodeParsed(EncodeStream& str) const
86
0
{
87
0
   if (!mScheme.empty())
88
0
   {
89
0
      str << mScheme << Symbols::SPACE;
90
0
   }
91
92
0
   encodeAuthParameters(str);
93
94
0
   return str;
95
0
}
96
97
ParserCategory* 
98
Auth::clone() const
99
0
{
100
0
   return new Auth(*this);
101
0
}
102
103
ParserCategory* 
104
Auth::clone(void* location) const
105
0
{
106
0
   return new (location) Auth(*this);
107
0
}
108
109
ParserCategory* 
110
Auth::clone(PoolBase* pool) const
111
0
{
112
0
   return new (pool) Auth(*this, pool);
113
0
}
114
115
void
116
Auth::parseAuthParameters(ParseBuffer& pb)
117
6.29k
{
118
14.4k
   while (!pb.eof())
119
12.8k
   {
120
12.8k
      const char* keyStart = pb.position();
121
12.8k
      static std::bitset<256> paramBegin=Data::toBitset(" \t\r\n=");
122
12.8k
      static std::bitset<256> terminators=Data::toBitset(" \t\r\n,");
123
12.8k
      const char* keyEnd = pb.skipToOneOf(paramBegin);
124
12.8k
      if((int)(keyEnd-keyStart) != 0)
125
11.8k
      {
126
11.8k
         ParameterTypes::Type type = ParameterTypes::getType(keyStart, (unsigned int)(keyEnd - keyStart));
127
11.8k
         Parameter* p=createParam(type, pb, terminators, getPool());
128
11.8k
         if (!p)
129
10.8k
         {
130
10.8k
            UnknownParameter* unknownParam = new UnknownParameter(keyStart,
131
10.8k
                                                                  int((keyEnd - keyStart)),
132
10.8k
                                                                  pb,
133
10.8k
                                                                  terminators);
134
135
10.8k
            if(!addParameter(unknownParam))
136
5.10k
            {
137
5.10k
               freeParameter(unknownParam);
138
5.10k
            }
139
10.8k
         }
140
932
         else
141
932
         {
142
932
            if(!addParameter(p))
143
825
            {
144
825
               freeParameter(p);
145
825
            }
146
932
         }
147
11.8k
      }
148
997
      else
149
997
      {
150
          // empty parameter name - skip and advance pb to next parameter
151
997
          pb.skipToOneOf(terminators);
152
997
      }
153
154
12.8k
      pb.skipWhitespace();
155
12.8k
      if (pb.eof() || *pb.position() != Symbols::COMMA[0])
156
4.68k
      {
157
4.68k
         break;
158
4.68k
      }
159
8.11k
      pb.skipChar();
160
8.11k
      pb.skipWhitespace();
161
8.11k
   }
162
6.29k
}
163
164
EncodeStream&
165
Auth::encodeAuthParameters(EncodeStream& str) const
166
0
{
167
0
   bool first = true;
168
0
   for (ParameterList::const_iterator it = mParameters.begin();
169
0
        it != mParameters.end(); it++)
170
0
   {
171
0
      if (!first)
172
0
      {
173
0
         str << Symbols::COMMA;
174
0
      }
175
0
      first = false;
176
0
      (*it)->encode(str);
177
0
   }
178
179
0
   for (ParameterList::const_iterator it = mUnknownParameters.begin();
180
0
        it != mUnknownParameters.end(); it++)
181
0
   {
182
0
      if (!first)
183
0
      {
184
0
         str << Symbols::COMMA;
185
0
      }
186
0
      first = false;
187
0
      (*it)->encode(str);
188
0
   }
189
0
   return str;
190
0
}
191
192
ParameterTypes::Factory Auth::ParameterFactories[ParameterTypes::MAX_PARAMETER]={0};
193
194
Parameter* 
195
Auth::createParam(ParameterTypes::Type type, ParseBuffer& pb, const std::bitset<256>& terminators, PoolBase* pool)
196
11.8k
{
197
11.8k
   if(type==ParameterTypes::qop)
198
198
   {
199
198
      DataParameter* qop = 0;
200
198
      switch(mHeaderType)
201
198
      {
202
0
         case Headers::ProxyAuthenticate:
203
0
         case Headers::WWWAuthenticate:
204
0
            qop = new (pool) DataParameter(ParameterTypes::qopOptions,pb,terminators);
205
0
            qop->setQuoted(true);
206
0
            break;
207
0
         case Headers::ProxyAuthorization:
208
0
         case Headers::Authorization:
209
0
         case Headers::AuthenticationInfo:
210
198
         default:
211
198
            qop = new (pool) DataParameter(ParameterTypes::qop,pb,terminators);
212
198
            qop->setQuoted(false);
213
198
      }
214
197
      return qop;
215
198
   }
216
217
11.6k
   if(type > ParameterTypes::UNKNOWN && type < ParameterTypes::MAX_PARAMETER && ParameterFactories[type])
218
734
   {
219
734
      return ParameterFactories[type](type, pb, terminators, pool);
220
734
   }
221
10.8k
   return 0;
222
11.6k
}
223
224
bool 
225
Auth::exists(const Param<Auth>& paramType) const
226
0
{
227
0
    checkParsed();
228
0
    bool ret = getParameterByEnum(paramType.getTypeNum()) != NULL;
229
0
    return ret;
230
0
}
231
232
void 
233
Auth::remove(const Param<Auth>& paramType)
234
0
{
235
0
    checkParsed();
236
0
    removeParameterByEnum(paramType.getTypeNum());
237
0
}
238
239
#define defineParam(_enum, _name, _type, _RFC_ref_ignored)                                                      \
240
_enum##_Param::DType&                                                                                           \
241
0
Auth::param(const _enum##_Param& paramType)                                                                     \
242
0
{                                                                                                               \
243
0
   checkParsed();                                                                                               \
244
0
   _enum##_Param::Type* p = static_cast<_enum##_Param::Type*>(getParameterByEnum(paramType.getTypeNum()));      \
245
0
   if (!p)                                                                                                      \
246
0
   {                                                                                                            \
247
0
      p = new _enum##_Param::Type(paramType.getTypeNum());                                                      \
248
0
      mParameters.push_back(p);                                                                                 \
249
0
   }                                                                                                            \
250
0
   return p->value();                                                                                           \
251
0
}                                                                                                               \
Unexecuted instantiation: resip::Auth::param(resip::algorithm_Param const&)
Unexecuted instantiation: resip::Auth::param(resip::cnonce_Param const&)
Unexecuted instantiation: resip::Auth::param(resip::domain_Param const&)
Unexecuted instantiation: resip::Auth::param(resip::nc_Param const&)
Unexecuted instantiation: resip::Auth::param(resip::nonce_Param const&)
Unexecuted instantiation: resip::Auth::param(resip::opaque_Param const&)
Unexecuted instantiation: resip::Auth::param(resip::realm_Param const&)
Unexecuted instantiation: resip::Auth::param(resip::response_Param const&)
Unexecuted instantiation: resip::Auth::param(resip::stale_Param const&)
Unexecuted instantiation: resip::Auth::param(resip::uri_Param const&)
Unexecuted instantiation: resip::Auth::param(resip::username_Param const&)
252
const _enum##_Param::DType&                                                                                     \
253
0
Auth::param(const _enum##_Param& paramType) const                                                               \
254
0
{                                                                                                               \
255
0
   checkParsed();                                                                                               \
256
0
   _enum##_Param::Type* p = static_cast<_enum##_Param::Type*>(getParameterByEnum(paramType.getTypeNum()));      \
257
0
   if (!p)                                                                                                      \
258
0
   {                                                                                                            \
259
0
      InfoLog(<< "Missing parameter " << ParameterTypes::ParameterNames[paramType.getTypeNum()]);               \
260
0
      DebugLog(<< *this);                                                                                       \
261
0
      throw Exception("Missing parameter", __FILE__, __LINE__);                                                 \
262
0
   }                                                                                                            \
263
0
   return p->value();                                                                                           \
264
0
}
Unexecuted instantiation: resip::Auth::param(resip::algorithm_Param const&) const
Unexecuted instantiation: resip::Auth::param(resip::cnonce_Param const&) const
Unexecuted instantiation: resip::Auth::param(resip::domain_Param const&) const
Unexecuted instantiation: resip::Auth::param(resip::nc_Param const&) const
Unexecuted instantiation: resip::Auth::param(resip::nonce_Param const&) const
Unexecuted instantiation: resip::Auth::param(resip::opaque_Param const&) const
Unexecuted instantiation: resip::Auth::param(resip::realm_Param const&) const
Unexecuted instantiation: resip::Auth::param(resip::response_Param const&) const
Unexecuted instantiation: resip::Auth::param(resip::stale_Param const&) const
Unexecuted instantiation: resip::Auth::param(resip::uri_Param const&) const
Unexecuted instantiation: resip::Auth::param(resip::username_Param const&) const
265
266
defineParam(algorithm, "algorithm", DataParameter, "RFC 2617");
267
defineParam(cnonce, "cnonce", QuotedDataParameter, "RFC 2617");
268
defineParam(domain, "domain", QuotedDataParameter, "RFC 3261");
269
defineParam(nc, "nc", DataParameter, "RFC 2617");
270
defineParam(nonce, "nonce", QuotedDataParameter, "RFC 2617");
271
defineParam(opaque, "opaque", QuotedDataParameter, "RFC 2617");
272
defineParam(realm, "realm", QuotedDataParameter, "RFC 2617");
273
defineParam(response, "response", QuotedDataParameter, "RFC 3261");
274
defineParam(stale, "stale", DataParameter, "RFC 2617");
275
defineParam(uri, "uri", QuotedDataParameter, "RFC 3261");
276
defineParam(username, "username", QuotedDataParameter, "RFC 3261");
277
278
DataParameter::Type&
279
Auth::param(const qop_Param& paramType)
280
0
{
281
0
   checkParsed();
282
0
   DataParameter* p = static_cast<DataParameter*>(getParameterByEnum(paramType.getTypeNum()));
283
0
   if (!p)
284
0
   {
285
0
      p = new DataParameter(ParameterTypes::qop);
286
0
      p->setQuoted(false);
287
0
      mParameters.push_back(p);
288
0
   }
289
0
   return p->value();
290
0
}
291
const DataParameter::Type&
292
Auth::param(const qop_Param& paramType) const
293
0
{
294
0
   checkParsed();
295
0
   DataParameter* p = static_cast<DataParameter*>(getParameterByEnum(paramType.getTypeNum()));
296
0
   if (!p)
297
0
   {
298
0
      InfoLog(<< "Missing parameter " << ParameterTypes::ParameterNames[paramType.getTypeNum()]);
299
0
      DebugLog(<< *this);
300
0
      throw Exception("Missing parameter", __FILE__, __LINE__);
301
0
   }
302
0
   return p->value();
303
0
}
304
305
DataParameter::Type&
306
Auth::param(const qopOptions_Param& paramType)
307
0
{
308
0
   checkParsed();
309
0
   DataParameter* p = static_cast<DataParameter*>(getParameterByEnum(paramType.getTypeNum()));
310
0
   if (!p)
311
0
   {
312
0
      p = new DataParameter(ParameterTypes::qopOptions);
313
0
      p->setQuoted(true);
314
0
      mParameters.push_back(p);
315
0
   }
316
0
   return p->value();
317
0
}
318
const DataParameter::Type&
319
Auth::param(const qopOptions_Param& paramType) const
320
0
{
321
0
   checkParsed();
322
0
   DataParameter* p = static_cast<DataParameter*>(getParameterByEnum(paramType.getTypeNum()));
323
0
   if (!p)
324
0
   {
325
0
      InfoLog(<< "Missing parameter " << ParameterTypes::ParameterNames[paramType.getTypeNum()]);
326
0
      DebugLog(<< *this);
327
0
      throw Exception("Missing parameter", __FILE__, __LINE__);
328
0
   }
329
0
   return p->value();
330
0
}
331
332
333
#undef defineParam
334
335
/* ====================================================================
336
 * The Vovida Software License, Version 1.0 
337
 * 
338
 * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
339
 * 
340
 * Redistribution and use in source and binary forms, with or without
341
 * modification, are permitted provided that the following conditions
342
 * are met:
343
 * 
344
 * 1. Redistributions of source code must retain the above copyright
345
 *    notice, this list of conditions and the following disclaimer.
346
 * 
347
 * 2. Redistributions in binary form must reproduce the above copyright
348
 *    notice, this list of conditions and the following disclaimer in
349
 *    the documentation and/or other materials provided with the
350
 *    distribution.
351
 * 
352
 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
353
 *    and "Vovida Open Communication Application Library (VOCAL)" must
354
 *    not be used to endorse or promote products derived from this
355
 *    software without prior written permission. For written
356
 *    permission, please contact vocal@vovida.org.
357
 *
358
 * 4. Products derived from this software may not be called "VOCAL", nor
359
 *    may "VOCAL" appear in their name, without prior written
360
 *    permission of Vovida Networks, Inc.
361
 * 
362
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
363
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
364
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
365
 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
366
 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
367
 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
368
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
369
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
370
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
371
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
373
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
374
 * DAMAGE.
375
 * 
376
 * ====================================================================
377
 * 
378
 * This software consists of voluntary contributions made by Vovida
379
 * Networks, Inc. and many individuals on behalf of Vovida Networks,
380
 * Inc.  For more information on Vovida Networks, Inc., please see
381
 * <http://www.vovida.org/>.
382
 *
383
 */