Coverage Report

Created: 2024-07-23 06:39

/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
   ParserCategory() 
23
0
{}
24
25
Auth::Auth(const HeaderFieldValue& hfv, Headers::Type type, PoolBase* pool) 
26
   : ParserCategory(hfv, type, pool) 
27
6.46k
{}
28
29
Auth::Auth(const Auth& rhs, PoolBase* pool)
30
   : ParserCategory(rhs, pool),
31
   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.46k
{
62
6.46k
   const char* start;
63
6.46k
   start = pb.skipWhitespace();
64
6.46k
   static const std::bitset<256> schemeDelimiter(Data::toBitset("\r\n\t ="));
65
6.46k
   pb.skipToOneOf(schemeDelimiter);
66
67
6.46k
   if (!pb.eof() && *pb.position() == Symbols::EQUALS[0])
68
4.60k
   {
69
      // Authentication-Info only
70
      // back up, and then parse
71
4.60k
      pb.reset(start);
72
4.60k
      parseAuthParameters(pb);
73
4.60k
   }
74
1.86k
   else
75
1.86k
   {
76
      // everything else
77
1.86k
      pb.data(mScheme, start);
78
79
1.86k
      pb.skipWhitespace();
80
1.86k
      parseAuthParameters(pb);
81
1.86k
   }
82
6.46k
}
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.46k
{
118
47.6k
   while (!pb.eof())
119
46.2k
   {
120
46.2k
      const char* keyStart = pb.position();
121
46.2k
      static std::bitset<256> paramBegin=Data::toBitset(" \t\r\n=");
122
46.2k
      static std::bitset<256> terminators=Data::toBitset(" \t\r\n,");
123
46.2k
      const char* keyEnd = pb.skipToOneOf(paramBegin);
124
46.2k
      if((int)(keyEnd-keyStart) != 0)
125
45.6k
      {
126
45.6k
         ParameterTypes::Type type = ParameterTypes::getType(keyStart, (unsigned int)(keyEnd - keyStart));
127
45.6k
         Parameter* p=createParam(type, pb, terminators, getPool());
128
45.6k
         if (!p)
129
27.0k
         {
130
27.0k
            mUnknownParameters.push_back(new UnknownParameter(keyStart, 
131
27.0k
                                                              int((keyEnd - keyStart)), pb, 
132
27.0k
                                                              terminators));
133
27.0k
         }
134
18.5k
         else
135
18.5k
         {
136
            // invoke the particular factory
137
18.5k
            mParameters.push_back(p);
138
18.5k
         }
139
45.6k
      }
140
643
      else
141
643
      {
142
          // empty parameter name - skip and advance pb to next parameter
143
643
          pb.skipToOneOf(terminators);
144
643
      }
145
146
46.2k
      pb.skipWhitespace();
147
46.2k
      if (pb.eof() || *pb.position() != Symbols::COMMA[0])
148
5.02k
      {
149
5.02k
         break;
150
5.02k
      }
151
41.2k
      pb.skipChar();
152
41.2k
      pb.skipWhitespace();
153
41.2k
   }
154
6.46k
}
155
156
EncodeStream&
157
Auth::encodeAuthParameters(EncodeStream& str) const
158
0
{
159
0
   bool first = true;
160
0
   for (ParameterList::const_iterator it = mParameters.begin();
161
0
        it != mParameters.end(); it++)
162
0
   {
163
0
      if (!first)
164
0
      {
165
0
         str << Symbols::COMMA;
166
0
      }
167
0
      first = false;
168
0
      (*it)->encode(str);
169
0
   }
170
171
0
   for (ParameterList::const_iterator it = mUnknownParameters.begin();
172
0
        it != mUnknownParameters.end(); it++)
173
0
   {
174
0
      if (!first)
175
0
      {
176
0
         str << Symbols::COMMA;
177
0
      }
178
0
      first = false;
179
0
      (*it)->encode(str);
180
0
   }
181
0
   return str;
182
0
}
183
184
ParameterTypes::Factory Auth::ParameterFactories[ParameterTypes::MAX_PARAMETER]={0};
185
186
Parameter* 
187
Auth::createParam(ParameterTypes::Type type, ParseBuffer& pb, const std::bitset<256>& terminators, PoolBase* pool)
188
45.6k
{
189
45.6k
   if(type==ParameterTypes::qop)
190
196
   {
191
196
      DataParameter* qop = 0;
192
196
      switch(mHeaderType)
193
196
      {
194
0
         case Headers::ProxyAuthenticate:
195
0
         case Headers::WWWAuthenticate:
196
0
            qop = new (pool) DataParameter(ParameterTypes::qopOptions,pb,terminators);
197
0
            qop->setQuoted(true);
198
0
            break;
199
0
         case Headers::ProxyAuthorization:
200
0
         case Headers::Authorization:
201
0
         case Headers::AuthenticationInfo:
202
196
         default:
203
196
            qop = new (pool) DataParameter(ParameterTypes::qop,pb,terminators);
204
196
            qop->setQuoted(false);
205
196
      }
206
195
      return qop;
207
196
   }
208
209
45.4k
   if(type > ParameterTypes::UNKNOWN && type < ParameterTypes::MAX_PARAMETER && ParameterFactories[type])
210
18.4k
   {
211
18.4k
      return ParameterFactories[type](type, pb, terminators, pool);
212
18.4k
   }
213
27.0k
   return 0;
214
45.4k
}
215
216
bool 
217
Auth::exists(const Param<Auth>& paramType) const
218
0
{
219
0
    checkParsed();
220
0
    bool ret = getParameterByEnum(paramType.getTypeNum()) != NULL;
221
0
    return ret;
222
0
}
223
224
void 
225
Auth::remove(const Param<Auth>& paramType)
226
0
{
227
0
    checkParsed();
228
0
    removeParameterByEnum(paramType.getTypeNum());
229
0
}
230
231
#define defineParam(_enum, _name, _type, _RFC_ref_ignored)                                                      \
232
_enum##_Param::DType&                                                                                           \
233
0
Auth::param(const _enum##_Param& paramType)                                                                     \
234
0
{                                                                                                               \
235
0
   checkParsed();                                                                                               \
236
0
   _enum##_Param::Type* p = static_cast<_enum##_Param::Type*>(getParameterByEnum(paramType.getTypeNum()));      \
237
0
   if (!p)                                                                                                      \
238
0
   {                                                                                                            \
239
0
      p = new _enum##_Param::Type(paramType.getTypeNum());                                                      \
240
0
      mParameters.push_back(p);                                                                                 \
241
0
   }                                                                                                            \
242
0
   return p->value();                                                                                           \
243
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&)
244
const _enum##_Param::DType&                                                                                     \
245
0
Auth::param(const _enum##_Param& paramType) const                                                               \
246
0
{                                                                                                               \
247
0
   checkParsed();                                                                                               \
248
0
   _enum##_Param::Type* p = static_cast<_enum##_Param::Type*>(getParameterByEnum(paramType.getTypeNum()));      \
249
0
   if (!p)                                                                                                      \
250
0
   {                                                                                                            \
251
0
      InfoLog(<< "Missing parameter " << ParameterTypes::ParameterNames[paramType.getTypeNum()]);               \
252
0
      DebugLog(<< *this);                                                                                       \
253
0
      throw Exception("Missing parameter", __FILE__, __LINE__);                                                 \
254
0
   }                                                                                                            \
255
0
   return p->value();                                                                                           \
256
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
257
258
defineParam(algorithm, "algorithm", DataParameter, "RFC 2617");
259
defineParam(cnonce, "cnonce", QuotedDataParameter, "RFC 2617");
260
defineParam(domain, "domain", QuotedDataParameter, "RFC 3261");
261
defineParam(nc, "nc", DataParameter, "RFC 2617");
262
defineParam(nonce, "nonce", QuotedDataParameter, "RFC 2617");
263
defineParam(opaque, "opaque", QuotedDataParameter, "RFC 2617");
264
defineParam(realm, "realm", QuotedDataParameter, "RFC 2617");
265
defineParam(response, "response", QuotedDataParameter, "RFC 3261");
266
defineParam(stale, "stale", DataParameter, "RFC 2617");
267
defineParam(uri, "uri", QuotedDataParameter, "RFC 3261");
268
defineParam(username, "username", QuotedDataParameter, "RFC 3261");
269
270
DataParameter::Type&
271
Auth::param(const qop_Param& paramType)
272
0
{
273
0
   checkParsed();
274
0
   DataParameter* p = static_cast<DataParameter*>(getParameterByEnum(paramType.getTypeNum()));
275
0
   if (!p)
276
0
   {
277
0
      p = new DataParameter(ParameterTypes::qop);
278
0
      p->setQuoted(false);
279
0
      mParameters.push_back(p);
280
0
   }
281
0
   return p->value();
282
0
}
283
const DataParameter::Type&
284
Auth::param(const qop_Param& paramType) const
285
0
{
286
0
   checkParsed();
287
0
   DataParameter* p = static_cast<DataParameter*>(getParameterByEnum(paramType.getTypeNum()));
288
0
   if (!p)
289
0
   {
290
0
      InfoLog(<< "Missing parameter " << ParameterTypes::ParameterNames[paramType.getTypeNum()]);
291
0
      DebugLog(<< *this);
292
0
      throw Exception("Missing parameter", __FILE__, __LINE__);
293
0
   }
294
0
   return p->value();
295
0
}
296
297
DataParameter::Type&
298
Auth::param(const qopOptions_Param& paramType)
299
0
{
300
0
   checkParsed();
301
0
   DataParameter* p = static_cast<DataParameter*>(getParameterByEnum(paramType.getTypeNum()));
302
0
   if (!p)
303
0
   {
304
0
      p = new DataParameter(ParameterTypes::qopOptions);
305
0
      p->setQuoted(true);
306
0
      mParameters.push_back(p);
307
0
   }
308
0
   return p->value();
309
0
}
310
const DataParameter::Type&
311
Auth::param(const qopOptions_Param& paramType) const
312
0
{
313
0
   checkParsed();
314
0
   DataParameter* p = static_cast<DataParameter*>(getParameterByEnum(paramType.getTypeNum()));
315
0
   if (!p)
316
0
   {
317
0
      InfoLog(<< "Missing parameter " << ParameterTypes::ParameterNames[paramType.getTypeNum()]);
318
0
      DebugLog(<< *this);
319
0
      throw Exception("Missing parameter", __FILE__, __LINE__);
320
0
   }
321
0
   return p->value();
322
0
}
323
324
325
#undef defineParam
326
327
/* ====================================================================
328
 * The Vovida Software License, Version 1.0 
329
 * 
330
 * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
331
 * 
332
 * Redistribution and use in source and binary forms, with or without
333
 * modification, are permitted provided that the following conditions
334
 * are met:
335
 * 
336
 * 1. Redistributions of source code must retain the above copyright
337
 *    notice, this list of conditions and the following disclaimer.
338
 * 
339
 * 2. Redistributions in binary form must reproduce the above copyright
340
 *    notice, this list of conditions and the following disclaimer in
341
 *    the documentation and/or other materials provided with the
342
 *    distribution.
343
 * 
344
 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
345
 *    and "Vovida Open Communication Application Library (VOCAL)" must
346
 *    not be used to endorse or promote products derived from this
347
 *    software without prior written permission. For written
348
 *    permission, please contact vocal@vovida.org.
349
 *
350
 * 4. Products derived from this software may not be called "VOCAL", nor
351
 *    may "VOCAL" appear in their name, without prior written
352
 *    permission of Vovida Networks, Inc.
353
 * 
354
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
355
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
356
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
357
 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
358
 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
359
 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
360
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
361
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
362
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
363
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
364
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
365
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
366
 * DAMAGE.
367
 * 
368
 * ====================================================================
369
 * 
370
 * This software consists of voluntary contributions made by Vovida
371
 * Networks, Inc. and many individuals on behalf of Vovida Networks,
372
 * Inc.  For more information on Vovida Networks, Inc., please see
373
 * <http://www.vovida.org/>.
374
 *
375
 */