Use a global (per engine) nonce counter. Increase it each time we need it.

git-svn-id: http://voip.null.ro/svn/yate@4378 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
marian 2011-05-16 08:18:56 +00:00
parent cca56df180
commit 311636496a
4 changed files with 31 additions and 7 deletions

View File

@ -151,7 +151,7 @@ SIPEngine::SIPEngine(const char* userAgent)
: Mutex(true,"SIPEngine"),
m_t1(500000), m_t4(5000000), m_maxForwards(70),
m_cseq(0), m_flags(0), m_lazyTrying(false),
m_userAgent(userAgent), m_nonce_time(0),
m_userAgent(userAgent), m_nc(0), m_nonce_time(0),
m_nonce_mutex(false,"SIPEngine::nonce")
{
debugName("sipengine");
@ -423,6 +423,19 @@ long SIPEngine::nonceAge(const String& nonce)
return Time::secNow() - t;
}
// Get a nonce count
void SIPEngine::ncGet(String& nc)
{
m_nonce_mutex.lock();
if (!(++m_nc))
++m_nc;
u_int32_t val = m_nc;
m_nonce_mutex.unlock();
char tmp[9];
::sprintf(tmp,"%08x",val);
nc = tmp;
}
bool SIPEngine::checkUser(const String& username, const String& realm, const String& nonce,
const String& method, const String& uri, const String& response,
const SIPMessage* message, GenObject* userData)

View File

@ -620,7 +620,7 @@ void SIPMessage::setParty(SIPParty* ep)
}
MimeAuthLine* SIPMessage::buildAuth(const String& username, const String& password,
const String& meth, const String& uri, bool proxy) const
const String& meth, const String& uri, bool proxy, SIPEngine* engine) const
{
const char* hdr = proxy ? "Proxy-Authenticate" : "WWW-Authenticate";
const ObjList* l = &header;
@ -640,6 +640,8 @@ MimeAuthLine* SIPMessage::buildAuth(const String& username, const String& passwo
MimeHeaderLine::delQuotes(qop);
if (qop == "auth") {
String nc("00000001");
if (engine)
engine->ncGet(nc);
qop.addParam("nc",nc);
MD5 md5;
md5 << String(::rand()) << nc << String(Time::secNow());
@ -673,12 +675,12 @@ MimeAuthLine* SIPMessage::buildAuth(const String& username, const String& passwo
return 0;
}
MimeAuthLine* SIPMessage::buildAuth(const SIPMessage& original) const
MimeAuthLine* SIPMessage::buildAuth(const SIPMessage& original, SIPEngine* engine) const
{
if (original.getAuthUsername().null())
return 0;
return buildAuth(original.getAuthUsername(),original.getAuthPassword(),
original.method,original.uri,(code == 407));
original.method,original.uri,(code == 407),engine);
}
ObjList* SIPMessage::getRoutes() const

View File

@ -83,7 +83,7 @@ SIPTransaction::SIPTransaction(SIPTransaction& original, SIPMessage* answer)
&original,answer,this);
SIPMessage* msg = new SIPMessage(*original.m_firstMessage);
MimeAuthLine* auth = answer->buildAuth(*original.m_firstMessage);
MimeAuthLine* auth = answer->buildAuth(*original.m_firstMessage,m_engine);
m_firstMessage->setAutoAuth();
msg->complete(m_engine);
msg->addHeader(auth);

View File

@ -311,17 +311,19 @@ public:
* @param meth Method to include in the authorization digest
* @param uri URI to include in the authorization digest
* @param proxy Set to true to authenticate to a proxy, false to a server
* @param engine Optional engine processing this message
* @return A new authorization line to be used in a new transaction
*/
MimeAuthLine* buildAuth(const String& username, const String& password,
const String& meth, const String& uri, bool proxy = false) const;
const String& meth, const String& uri, bool proxy = false, SIPEngine* engine = 0) const;
/**
* Construct a new authorization line based on this answer and original message
* @param original Origianl outgoing message
* @param engine Optional engine processing this message
* @return A new authorization line to be used in a new transaction
*/
MimeAuthLine* buildAuth(const SIPMessage& original) const;
MimeAuthLine* buildAuth(const SIPMessage& original, SIPEngine* engine = 0) const;
/**
* Prepare the message for automatic client transaction authentication.
@ -1210,6 +1212,12 @@ public:
*/
long nonceAge(const String& nonce);
/**
* Get a nonce count
* @param nc String reference to fill with new nonce count
*/
void ncGet(String& nc);
/**
* Build an authentication response
* @param username User account name
@ -1290,6 +1298,7 @@ protected:
bool m_lazyTrying;
String m_userAgent;
String m_allowed;
u_int32_t m_nc;
String m_nonce;
String m_nonce_secret;
u_int32_t m_nonce_time;