Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

pana_cookie.cxx

00001 /* BEGIN_COPYRIGHT                                                        */
00002 /*                                                                        */
00003 /* Open Diameter: Open-source software for the Diameter and               */
00004 /*                Diameter related protocols                              */
00005 /*                                                                        */
00006 /* Copyright (C) 2002-2004 Open Diameter Project                          */
00007 /*                                                                        */
00008 /* This library is free software; you can redistribute it and/or modify   */
00009 /* it under the terms of the GNU Lesser General Public License as         */
00010 /* published by the Free Software Foundation; either version 2.1 of the   */
00011 /* License, or (at your option) any later version.                        */
00012 /*                                                                        */
00013 /* This library is distributed in the hope that it will be useful,        */
00014 /* but WITHOUT ANY WARRANTY; without even the implied warranty of         */
00015 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      */
00016 /* Lesser General Public License for more details.                        */
00017 /*                                                                        */
00018 /* You should have received a copy of the GNU Lesser General Public       */
00019 /* License along with this library; if not, write to the Free Software    */
00020 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307    */
00021 /* USA.                                                                   */
00022 /*                                                                        */
00023 /* In addition, when you copy and redistribute some or the entire part of */
00024 /* the source code of this software with or without modification, you     */
00025 /* MUST include this copyright notice in each copy.                       */
00026 /*                                                                        */
00027 /* If you make any changes that are appeared to be useful, please send    */
00028 /* sources that include the changed part to                               */
00029 /* diameter-developers@lists.sourceforge.net so that we can reflect your  */
00030 /* changes to one unified version of this software.                       */
00031 /*                                                                        */
00032 /* END_COPYRIGHT                                                          */
00033 
00034 #include "ace/System_Time.h"
00035 #include "pana_sha1.h"
00036 #include "pana_cookie.h"
00037 
00038 PANA_Cookie::PANA_Cookie()
00039 {
00040     char sbuf[32];
00041     ACE_UINT32 ltime;
00042     ACE_System_Time::get_local_system_time(ltime);
00043 
00044     /* --- seed and generate secret table --- */
00045     ACE_OS::srand(ltime);
00046     for (int i = 0; i < default_table_size_; i ++) {
00047         ACE_OS::sprintf(sbuf, "%0.4x", ACE_OS::rand());
00048         s_table_[i] = sbuf;
00049     }
00050 
00051     s_index_ = 0; // initial index
00052 }
00053 
00054 bool PANA_Cookie::Generate(std::string &id, std::string &digest)
00055 {
00056     return Generate(s_index_, id, digest);
00057 }
00058 
00059 bool PANA_Cookie::Generate(ACE_UINT32 ndx, std::string &id, std::string &digest)
00060 {
00061     unsigned char sbuf[32];
00062     ACE_OS::memset(sbuf, 0x0, sizeof(sbuf));
00063     ACE_OS::memcpy(sbuf, &ndx, sizeof(ACE_UINT32));
00064 
00065     PANA_Sha1 sha1;
00066     sha1.Update((unsigned char*)id.data(), id.size());
00067     sha1.Update((unsigned char*)s_table_[ndx].data(), sizeof(ACE_UINT32));
00068     sha1.Final();
00069 
00070     sha1.GetHash(sbuf + sizeof(ACE_UINT32));
00071     digest.assign((char*)sbuf, 20 + 2 * sizeof(ACE_UINT32)); // sha1 hash + index
00072 
00073     return (true);
00074 }
00075 
00076 bool PANA_Cookie::Verify(std::string &id, std::string &digest)
00077 {
00078     std::string testDigest;
00079     for (int i = 0; i < default_table_size_; i ++) {
00080         Generate(i, id, testDigest);
00081         // 24 byte sha1 hash value comparison
00082         if (ACE_OS::memcmp(testDigest.data(), digest.data(), 20 + 2 * sizeof(ACE_UINT32)) == 0) {
00083             return (true);
00084         }
00085     }
00086     return (false);
00087 }
00088 
00089 void PANA_Cookie::Cycle()
00090 {
00091     s_index_ ++;
00092     if (s_index_ > default_table_size_) {
00093         s_index_ = 0;
00094     }
00095 }

Generated on Fri Jun 25 19:18:29 2004 for PANA by doxygen 1.3.5