reSIProcate/stack  9694
SERNonceHelper.cxx
Go to the documentation of this file.
00001 
00002 #include "resip/stack/SERNonceHelper.hxx"
00003 #include "rutil/Random.hxx"
00004 #include "resip/stack/Helper.hxx"
00005 
00006 using namespace resip;
00007 
00008 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
00009 
00010 
00024 SERNonceHelper::SERNonceHelper(int serOffset) : serOffset(serOffset)
00025 {
00026    //privateKey = Data("asdfklsadflkj");
00027    privateKey = Random::getRandomHex(24);
00028 }
00029 
00030 SERNonceHelper::~SERNonceHelper() 
00031 {
00032 }
00033 
00034 void
00035 SERNonceHelper::setPrivateKey(const Data& pprivateKey)
00036 {
00037    this->privateKey = pprivateKey;
00038 }
00039 
00040 Data 
00041 SERNonceHelper::makeNonce(const SipMessage& request, const Data& timestamp) 
00042 {
00043    char buf[8];
00044    Data md5buf(8, Data::Preallocate);
00045    Data nonce(40, Data::Preallocate);
00046    int ts = timestamp.convertInt() + serOffset;
00047    Helper::integer2hex(buf, ts);
00048    md5buf.append(buf, 8);
00049    nonce.append(buf, 8);
00050    md5buf += privateKey;
00051    nonce += md5buf.md5(); 
00052    return nonce;
00053 }
00054 
00055 NonceHelper::Nonce
00056 SERNonceHelper::parseNonce(const Data& nonce) 
00057 {
00058    if(nonce.size() != 40)
00059    {
00060       return SERNonceHelper::Nonce(0);
00061    }
00062    const char *s = nonce.data();
00063    unsigned int ts = Helper::hex2integer(s) - serOffset;
00064    return SERNonceHelper::Nonce(ts); 
00065 }
00066 
00067 
00068 /* ====================================================================
00069  * The Vovida Software License, Version 1.0
00070  *
00071  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00072  *
00073  * Redistribution and use in source and binary forms, with or without
00074  * modification, are permitted provided that the following conditions
00075  * are met:
00076  *
00077  * 1. Redistributions of source code must retain the above copyright
00078  *    notice, this list of conditions and the following disclaimer.
00079  *
00080  * 2. Redistributions in binary form must reproduce the above copyright
00081  *    notice, this list of conditions and the following disclaimer in
00082  *    the documentation and/or other materials provided with the
00083  *    distribution.
00084  *
00085  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00086  *    and "Vovida Open Communication Application Library (VOCAL)" must
00087  *    not be used to endorse or promote products derived from this
00088  *    software without prior written permission. For written
00089  *    permission, please contact vocal@vovida.org.
00090  *
00091  * 4. Products derived from this software may not be called "VOCAL", nor
00092  *    may "VOCAL" appear in their name, without prior written
00093  *    permission of Vovida Networks, Inc.
00094  *
00095  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00096  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00097  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00098  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00099  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00100  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00101  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00102  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00103  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00104  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00105  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00106  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00107  * DAMAGE.
00108  *
00109  * ====================================================================
00110  *
00111  * This software consists of voluntary contributions made by Vovida
00112  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00113  * Inc.  For more information on Vovida Networks, Inc., please see
00114  * <http://www.vovida.org/>.
00115  *
00116  */
00117