reSIProcate/DialogUsageManager  9694
testSMIMEMessage.cxx
Go to the documentation of this file.
00001 #include "resip/dum/DialogUsageManager.hxx"
00002 #include "resip/dum/MasterProfile.hxx"
00003 #include "resip/dum/UserProfile.hxx"
00004 #include "resip/dum/ClientAuthManager.hxx"
00005 #include "resip/dum/ClientRegistration.hxx"
00006 #include "resip/dum/ClientPagerMessage.hxx"
00007 #include "resip/dum/ServerPagerMessage.hxx"
00008 #include "resip/dum/RegistrationHandler.hxx"
00009 #include "resip/dum/PagerMessageHandler.hxx"
00010 
00011 #include "resip/stack/PlainContents.hxx"
00012 #include "resip/stack/Pkcs7Contents.hxx"
00013 #include "resip/stack/MultipartSignedContents.hxx"
00014 #include "resip/stack/Mime.hxx"
00015 
00016 #include "resip/stack/SecurityAttributes.hxx"
00017 #include "resip/stack/Helper.hxx"
00018 
00019 #include "rutil/Log.hxx"
00020 #include "rutil/Logger.hxx"
00021 
00022 #ifdef WIN32
00023 #include "resip/stack/ssl/WinSecurity.hxx"
00024 #else
00025 #include "resip/stack/ssl/Security.hxx"
00026 #endif
00027 
00028 #include <iostream>
00029 #include <string>
00030 #include <sstream>
00031 
00032 using namespace std;
00033 using namespace resip;
00034 
00035 #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
00036 
00037 class TestSMIMEMessageHandler : public ClientPagerMessageHandler,
00038                             public ServerPagerMessageHandler,
00039                             public ClientRegistrationHandler
00040 {
00041    public:
00042       TestSMIMEMessageHandler(Security *security) : security(security), _registered(false), _ended(false), _rcvd(false)
00043       {}
00044 
00045       virtual ~TestSMIMEMessageHandler() 
00046       {
00047       }
00048 
00049       void reset()
00050       {
00051          _ended = false;
00052          _rcvd = false;
00053       }
00054 
00055       bool isRegistered()
00056       {
00057          return _registered;
00058       }
00059 
00060       bool isEnded()
00061       {
00062          return _ended;
00063       }
00064 
00065       bool isRcvd()
00066       {
00067          return _rcvd;
00068       }
00069 
00070       virtual void onMessageArrived(ServerPagerMessageHandle handle,
00071                                     const SipMessage& message)
00072       {
00073          
00074          InfoLog( << "ServerPagerMessageHandler::onMessageArrived: " );
00075 
00076          SharedPtr<SipMessage> ok = handle->accept();
00077          handle->send(ok);
00078 
00079          _rcvd = true;
00080 
00081          InfoLog( << "received type " << message.header(h_ContentType) );
00082 
00083          Helper::ContentsSecAttrs csa(Helper::extractFromPkcs7(message, *security));
00084          const_cast<SipMessage&>(message).setSecurityAttributes(csa.mAttributes);
00085 
00086          InfoLog( << "Body: " << *csa.mContents << "\n" );
00087 
00088          const SecurityAttributes *attr = message.getSecurityAttributes();
00089          InfoLog( << *attr );
00090          
00091       }
00092 
00093       virtual void onSuccess(ClientPagerMessageHandle,
00094                              const SipMessage& status)
00095       {
00096          InfoLog( << "ClientMessageHandler::onSuccess\n" );
00097          _ended = true;
00098       }
00099 
00100       virtual void onFailure(ClientPagerMessageHandle,
00101                              const SipMessage& status,
00102                              std::auto_ptr<Contents> contents)
00103       {
00104          InfoLog( << "ClientMessageHandler::onFailure\n" );
00105          _ended = true;
00106       }
00107 
00108       virtual void onSuccess(ClientRegistrationHandle,
00109                              const SipMessage& response)
00110       {
00111          InfoLog( << "ClientRegistrationHandler::onSuccess\n" );
00112          _registered = true;
00113       }
00114 
00115       virtual void onRemoved(ClientRegistrationHandle, const SipMessage&)
00116       {
00117          InfoLog( << "ClientRegistrationHander::onRemoved\n" );
00118          exit(-1);
00119       }
00120       
00121       virtual void onFailure(ClientRegistrationHandle,
00122                              const SipMessage& response)
00123       {
00124          InfoLog( << "ClientRegistrationHandler::onFailure\n" );
00125          exit(-1);
00126       }
00127 
00128       virtual int onRequestRetry(ClientRegistrationHandle,
00129                                  int retrySeconds, const SipMessage& response)
00130       {
00131          InfoLog( << "ClientRegistrationHandler::onRequestRetry\n" );
00132          exit(-1);
00133          return -1;
00134       }
00135 
00136    protected:
00137       Security *security;
00138       bool _registered;
00139       bool _ended;
00140       bool _rcvd;
00141       
00142 };
00143 
00144 
00145 /*****************************************************************************/
00146 
00147 int main(int argc, char *argv[])
00148 {
00149 
00150    if ( argc < 3 ) {
00151       cout << "usage: " << argv[0] << " sip:user passwd\n";
00152       return 0;
00153    }
00154 
00155    Log::initialize(Log::Cout, Log::Debug, argv[0]);
00156 
00157    NameAddr userAor(argv[1]);
00158    Data passwd(argv[2]);
00159 
00160    InfoLog(<< "user: " << userAor << ", passwd: " << passwd << "\n");
00161 
00162 #ifdef WIN32
00163    Security* security = new WinSecurity;
00164 #else
00165    Security* security = new Security;
00166 #endif
00167 
00168    assert(security);
00169 
00170    SipStack clientStack(security);
00171    DialogUsageManager clientDum(clientStack);
00172    clientDum.addTransport(UDP, 0, V4);
00173    //clientDum.addTransport(TCP, 0, V4);
00174    //clientDum.addTransport(TLS, 0, V4);
00175    // clientDum.addTransport(UDP, 0, V6);
00176    // clientDum.addTransport(TCP, 0, V6);
00177    // clientDum.addTransport(TLS, 0, V6);
00178 
00179    SharedPtr<MasterProfile> clientProfile(new MasterProfile);   
00180    auto_ptr<ClientAuthManager> clientAuth(new ClientAuthManager());   
00181    TestSMIMEMessageHandler clientHandler(security);
00182 
00183    clientDum.setClientAuthManager(clientAuth);
00184    clientDum.setClientRegistrationHandler(&clientHandler);
00185    clientDum.setClientPagerMessageHandler(&clientHandler);
00186    clientDum.setServerPagerMessageHandler(&clientHandler);
00187 
00188    clientProfile->setDefaultFrom(userAor);
00189    clientProfile->setDigestCredential(userAor.uri().host(), userAor.uri().user(), passwd);
00190    clientProfile->setDefaultRegistrationTime(70);               
00191    clientProfile->addSupportedMethod(MESSAGE);
00192    clientProfile->addSupportedMimeType(MESSAGE, Mime("text", "plain"));
00193    clientProfile->addSupportedMimeType(MESSAGE, Mime("application", "pkcs7-mime"));
00194    clientProfile->addSupportedMimeType(MESSAGE, Mime("multipart", "signed"));
00195    clientDum.setMasterProfile(clientProfile);
00196 
00197    SharedPtr<SipMessage> regMessage = clientDum.makeRegistration(userAor);
00198         
00199    InfoLog( << *regMessage << "Generated register: " << endl << *regMessage );
00200    clientDum.send(regMessage);
00201 
00202    int iteration = 0;
00203    bool finished = false;
00204    while (!finished) 
00205    {
00206 
00207       clientHandler.reset();
00208       
00209       bool sent = false;
00210       while (!clientHandler.isEnded() || !clientHandler.isRcvd() )
00211       {
00212          clientStack.process(100);
00213          while (clientDum.process());
00214 
00215          if (!sent && clientHandler.isRegistered())
00216          {
00217             try 
00218             {
00219                ClientPagerMessageHandle cpmh = clientDum.makePagerMessage(userAor);                     
00220                Contents* mcontent = new PlainContents(Data("message"));
00221                auto_ptr<Contents> content;
00222 
00223                switch (iteration) 
00224                {
00225                   case 0:
00226                   {
00227                      InfoLog( << "Sending PKCS7 encrypted data" );
00228                      Pkcs7Contents* pkcs7 = security->encrypt(mcontent, userAor.uri().getAor());
00229                      content = auto_ptr<Contents>(static_cast<Contents *>(pkcs7));
00230                      break;
00231                   }
00232                   case 1:
00233                   {
00234                      InfoLog( << "Sending PKCS7 signed data" );
00235                      MultipartSignedContents* mpc = security->sign(userAor.uri().getAor(), mcontent);
00236                      content = auto_ptr<Contents>(static_cast<Contents *>(mpc));
00237                      break;
00238                   }
00239                   default:
00240                   {
00241                      InfoLog( << "Finished!" );
00242                      finished = true;
00243                      break;
00244                   }
00245                }
00246                
00247                if (finished)
00248                   break;
00249                
00250                cpmh.get()->page(content);
00251                sent = true;
00252                
00253             }
00254                
00255             catch (...)
00256             {
00257                InfoLog( << "failure to send message at iteration " << iteration );
00258                break;
00259             }
00260 
00261          }
00262 
00263       }
00264       ++iteration;
00265    }
00266 
00267    return 0;
00268 }
00269 
00270 /* ====================================================================
00271  * The Vovida Software License, Version 1.0 
00272  * 
00273  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00274  * 
00275  * Redistribution and use in source and binary forms, with or without
00276  * modification, are permitted provided that the following conditions
00277  * are met:
00278  * 
00279  * 1. Redistributions of source code must retain the above copyright
00280  *    notice, this list of conditions and the following disclaimer.
00281  * 
00282  * 2. Redistributions in binary form must reproduce the above copyright
00283  *    notice, this list of conditions and the following disclaimer in
00284  *    the documentation and/or other materials provided with the
00285  *    distribution.
00286  * 
00287  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00288  *    and "Vovida Open Communication Application Library (VOCAL)" must
00289  *    not be used to endorse or promote products derived from this
00290  *    software without prior written permission. For written
00291  *    permission, please contact vocal@vovida.org.
00292  *
00293  * 4. Products derived from this software may not be called "VOCAL", nor
00294  *    may "VOCAL" appear in their name, without prior written
00295  *    permission of Vovida Networks, Inc.
00296  * 
00297  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00298  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00299  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00300  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00301  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00302  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00303  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00304  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00305  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00306  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00307  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00308  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00309  * DAMAGE.
00310  * 
00311  * ====================================================================
00312  * 
00313  * This software consists of voluntary contributions made by Vovida
00314  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00315  * Inc.  For more information on Vovida Networks, Inc., please see
00316  * <http://www.vovida.org/>.
00317  *
00318  */