D:/Storage/CVS_Head/h323plus/src/h235pluginmgr.cxx

00001 /*
00002  * h235pluginmgr.cxx
00003  *
00004  * h235 Implementation for the h323plus library.
00005  *
00006  *
00007  * Copyright (c) 2006 ISVO (Asia) Pte Ltd. All Rights Reserved.
00008  *
00009  * The contents of this file are subject to the Mozilla Public License
00010  * Version 1.1 (the "License"); you may not use this file except in
00011  * compliance with the License. You may obtain a copy of the License at
00012  * http://www.mozilla.org/MPL/
00013  *
00014  * Alternatively, the contents of this file may be used under the terms
00015  * of the General Public License (the  "GNU License"), in which case the
00016  * provisions of GNU License are applicable instead of those
00017  * above. If you wish to allow use of your version of this file only
00018  * under the terms of the GNU License and not to allow others to use
00019  * your version of this file under the MPL, indicate your decision by
00020  * deleting the provisions above and replace them with the notice and
00021  * other provisions required by the GNU License. If you do not delete
00022  * the provisions above, a recipient may use your version of this file
00023  * under either the MPL or the GNU License."
00024  *
00025  * Software distributed under the License is distributed on an "AS IS"
00026  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00027  * the License for the specific language governing rights and limitations
00028  * under the License.
00029  *
00030  *
00031  *
00032  * Contributor(s): ______________________________________.
00033  *
00034  * $Log: h235pluginmgr.cxx,v $
00035  * Revision 1.1  2007/08/06 20:51:05  shorne
00036  * First commit of h323plus
00037  *
00038  *
00039  *
00040 */
00041 
00042 #include <ptlib.h>
00043 #include <ptclib/asner.h>
00044 
00045 // For OpenH323
00046 #include "h235pluginmgr.h"
00047 #include "h235plugin.h"
00048 #include <h225.h>
00049 #include <h235.h>
00050 
00051 #ifdef _MSC_VER
00052 #pragma warning(disable:4700)
00053 #endif
00054 
00055 
00056 static int PluginControl(Pluginh235_Definition * h235, 
00057                                        void * context,
00058                                  const char * name,
00059                                  const char * parm, 
00060                                                              const char * val)
00061 {
00062   Pluginh235_ControlDefn * controls = h235->h235Controls;
00063   if (controls == NULL)
00064     return 0;
00065 
00066   while (controls->name != NULL) {
00067     if (strcmp(controls->name, name) == 0)
00068       return (*controls->control)(h235, context, parm, val);
00069     controls++;
00070   }
00071 
00072   return 0;
00073 }
00074 
00075 H235PluginAuthenticator::H235PluginAuthenticator(Pluginh235_Definition * _def)
00076   : def(_def)
00077 {
00078   switch (def->flags & Pluginh235_TokenTypeMask) {
00079         case Pluginh235_TokenTypecrypto:  
00080             switch (def->flags & Pluginh235_TokenTypeMask) {
00081               case Pluginh235_TokenStyleHash:
00082                                 type = H235_AuthenticationMechanism::e_pwdHash;
00083                 break;
00084               case Pluginh235_TokenStyleSigned:
00085                 type = H235_AuthenticationMechanism::e_certSign;
00086                 break;
00087               case Pluginh235_TokenStyleEncrypted:
00088                 type = H235_AuthenticationMechanism::e_pwdSymEnc;
00089                 break;
00090               default:
00091                 type = H235_AuthenticationMechanism::e_nonStandard;
00092              }
00093             break;
00094         case Pluginh235_TokenTypeclear: 
00095                 type = H235_AuthenticationMechanism::e_authenticationBES;
00096             break;
00097         default:
00098              type = H235_AuthenticationMechanism::e_nonStandard;
00099             break;
00100   }     
00101 
00102    SetTimestampGracePeriod(2*60*60+10);  
00103 }
00104 
00105 H235_ClearToken * H235PluginAuthenticator::CreateClearToken()
00106 {
00107         BYTE * data;
00108         unsigned dataLen;
00109         int ret = (*def->h235function)(def, NULL, H235_BuildClear,
00110                                                                   data, &dataLen,NULL,0);
00111 
00112         if (ret == 0)
00113                 return NULL;
00114 
00115         PPER_Stream raw(data,dataLen);
00116     H235_ClearToken * token = new H235_ClearToken;
00117         token->Decode(raw);
00118     return token;
00119 }
00120 
00121 H225_CryptoH323Token * H235PluginAuthenticator::CreateCryptoToken()
00122 {
00123         BYTE * data;
00124         unsigned dataLen;
00125         int ret = (*def->h235function)(def, NULL, H235_BuildCrypto,
00126                                                                   data, &dataLen,NULL,0);
00127         if (ret == 0)
00128                 return NULL;
00129 
00130         PPER_Stream raw(data,dataLen);
00131     H225_CryptoH323Token * token = new H225_CryptoH323Token;
00132         token->Decode(raw);
00133     return token;
00134 }
00135 
00136 BOOL H235PluginAuthenticator::Finalise(PBYTEArray & rawPDU)
00137 {
00138    BYTE * data = rawPDU.GetPointer();
00139    unsigned dataLen = rawPDU.GetSize();
00140    int ret = (*def->h235function)(def, NULL, H235_FinaliseCrypto, data, &dataLen,NULL,0);
00141    
00142    if (ret == 0)
00143            return FALSE;
00144    
00145    PBYTEArray newPDU(data,dataLen);
00146    rawPDU = newPDU;
00147    return TRUE;
00148 }
00149 
00150 H235Authenticator::ValidationResult H235PluginAuthenticator::ValidateClearToken(const H235_ClearToken & clearToken)
00151 {
00152         PPER_Stream enc;
00153         clearToken.Encode(enc);
00154 
00155         BYTE * data = enc.GetPointer();
00156         unsigned dataLen = enc.GetSize();
00157 
00158         int ret = (*def->h235function)(def, NULL, H235_ValidateClear,
00159                                                                   data, &dataLen,NULL,0);
00160 
00161     return (H235Authenticator::ValidationResult)ret;
00162 }
00163 
00164 H235Authenticator::ValidationResult H235PluginAuthenticator::ValidateCryptoToken(const H225_CryptoH323Token & cryptoToken,
00165                                                                                  const PBYTEArray & rawPDU)
00166 {
00167         PPER_Stream enc;
00168         cryptoToken.Encode(enc);
00169 
00170         BYTE * data = enc.GetPointer();
00171         unsigned dataLen = enc.GetSize();
00172         const BYTE * raw = rawPDU;
00173         unsigned rawLen = rawPDU.GetSize();
00174 
00175         int ret = (*def->h235function)(def, NULL, H235_ValidateClear,
00176                                                                   data, &dataLen, raw, &rawLen);
00177 
00178     return (H235Authenticator::ValidationResult)ret;
00179 }
00180 
00181 BOOL H235PluginAuthenticator::IsCapability(const H235_AuthenticationMechanism & mechanism,
00182                                            const PASN_ObjectId & algorithmOID)
00183 {
00184   return ((mechanism.GetTag() == type) && (algorithmOID.AsString() == def->identifier));
00185 }
00186 
00187 BOOL H235PluginAuthenticator::SetCapability(H225_ArrayOf_AuthenticationMechanism & mechanisms,
00188                                             H225_ArrayOf_PASN_ObjectId & algorithmOIDs)
00189 {
00190   return AddCapability(type, def->identifier,mechanisms, algorithmOIDs);
00191 }
00192 
00193 BOOL H235PluginAuthenticator::UseGkAndEpIdentifiers() const
00194 {
00195    return (PluginControl(def, NULL,GET_PLUGINH235_SETTINGS, Pluginh235_Set_UseGkAndEpIdentifiers, NULL)); 
00196 }
00197 
00198 BOOL H235PluginAuthenticator::IsSecuredPDU(unsigned rasPDU,BOOL received) const
00199 {
00200    return (PluginControl(def, NULL,GET_PLUGINH235_SETTINGS, Pluginh235_Set_IsSecuredPDU, PString(rasPDU))); 
00201 }
00202 
00203 BOOL H235PluginAuthenticator::IsSecuredSignalPDU(unsigned signalPDU,
00204                                                  BOOL received) const
00205 {
00206    return (PluginControl(def, NULL,GET_PLUGINH235_SETTINGS, Pluginh235_Set_IsSecuredSignalPDU, PString(signalPDU)));
00207 }
00208 
00209 BOOL H235PluginAuthenticator::IsActive() const
00210 {
00211    return (PluginControl(def, NULL,GET_PLUGINH235_SETTINGS, Pluginh235_Set_IsActive, NULL));
00212 }
00213 
00214 const PString & H235PluginAuthenticator::GetRemoteId() const
00215 {
00216    return remoteId;
00217 }
00218 
00219 void H235PluginAuthenticator::SetRemoteId(const PString & id)
00220 {
00221    remoteId = id;
00222    PluginControl(def, NULL,SET_PLUGINH235_SETTINGS, Pluginh235_Set_RemoteId, remoteId); 
00223 }
00224 
00225 const PString & H235PluginAuthenticator::GetLocalId() const
00226 {
00227    return localId;
00228 }
00229 
00230 void H235PluginAuthenticator::SetLocalId(const PString & id)
00231 {
00232    localId = id;
00233    PluginControl(def, NULL,SET_PLUGINH235_SETTINGS, Pluginh235_Set_LocalId, id); 
00234 }
00235 
00236 const PString & H235PluginAuthenticator::GetPassword() const
00237 {
00238    return password;
00239 }
00240     
00241 void H235PluginAuthenticator::SetPassword(const PString & pw)
00242 {
00243    password = pw;
00244    PluginControl(def, NULL,SET_PLUGINH235_SETTINGS, Pluginh235_Set_Password, password); 
00245 }
00246 
00247 int H235PluginAuthenticator::GetTimestampGracePeriod() const
00248 {
00249    return timestampGracePeriod; 
00250 }
00251     
00252 void H235PluginAuthenticator::SetTimestampGracePeriod(int grace)
00253 {
00254    timestampGracePeriod = grace;
00255    PluginControl(def, NULL,SET_PLUGINH235_SETTINGS, Pluginh235_Set_TimestampGracePeriod, PString(timestampGracePeriod)); 
00256 }
00257 
00258 H235Authenticator::Application H235PluginAuthenticator::GetApplication()
00259 {
00260     return (H235Authenticator::Application)PluginControl(def, NULL,GET_PLUGINH235_SETTINGS, 
00261                       Pluginh235_Set_Application, NULL) ;
00262 }
00263 
00265 
00266 h235PluginDeviceManager::h235PluginDeviceManager(PPluginManager * _pluginMgr)
00267  : PPluginModuleManager(PLUGIN_H235_GET_DEVICE_FN_STR, _pluginMgr)
00268 {
00269     PTRACE(3, "H323h235\tPlugin loading h235 "); 
00270 
00271     // cause the plugin manager to load all dynamic plugins
00272     pluginMgr->AddNotifier(PCREATE_NOTIFIER(OnLoadModule), TRUE);
00273 }
00274     
00275 h235PluginDeviceManager::~h235PluginDeviceManager()
00276 {
00277 
00278 }
00279 
00280 void h235PluginDeviceManager::OnLoadPlugin(PDynaLink & dll, INT code)
00281 {
00282   Pluginh235_Geth235Function geth235;
00283   if (!dll.GetFunction(PString(signatureFunctionName), (PDynaLink::Function &)geth235)) {
00284     PTRACE(3, "H323h235\tPlugin DLL " << dll.GetName() << " is not a H235 plugin");       
00285     return;
00286   }
00287 
00288   unsigned int count;
00289   Pluginh235_Definition * h235 = (*geth235)(&count, PLUGIN_H235_VERSION);
00290   if (h235 == NULL || count == 0) {
00291     PTRACE(3, "H323PLUGIN\tPlugin DLL " << dll.GetName() << " contains no H235 definitions" );
00292     return;
00293   } 
00294 
00295   PTRACE(3, "H323PLUGIN\tLoading H235 plugin  " << dll.GetName() );
00296 
00297   switch (code) {
00298 
00299     // plugin loaded
00300     case 0:
00301       Registerh235(count, h235);
00302       break;
00303 
00304     // plugin unloaded
00305     case 1:
00306       Unregisterh235(count, h235);
00307       break;
00308 
00309     default:
00310       break;
00311   }
00312 }
00313 
00314 void h235PluginDeviceManager::OnShutdown()
00315 {
00316   // unregister the H235 plugin 
00317     h235Factory::UnregisterAll();
00318 }
00319 
00320 void h235PluginDeviceManager::Bootstrap()
00321 {
00322 
00323 }
00324 
00325 BOOL h235PluginDeviceManager::Registerh235(unsigned int count, void * _h235List)
00326 {
00327   // make sure all non-timestamped codecs have the same concept of "now"
00328   static time_t h235Now = ::time(NULL);
00329 
00330   Pluginh235_Definition * h235List = (Pluginh235_Definition *)_h235List;
00331 
00332   unsigned i;
00333   for (i = 0; i < count; i++) {
00334      CreateH235Authenticator(&h235List[i]);
00335   }
00336 
00337   return TRUE;
00338 }
00339 
00340 BOOL h235PluginDeviceManager::Unregisterh235(unsigned int /*count*/, void * /*_h235List*/)
00341 {
00342 
00343         return FALSE;
00344 }
00345 
00346 static PString Createh235Name(Pluginh235_Definition * h235, unsigned int h235type)
00347 {
00348   PString str;
00349 
00350   switch (h235type) {
00351       case Pluginh235_TokenStyleHash:
00352              str  = h235->desc + PString(" {hash}");
00353          break;
00354       case Pluginh235_TokenStyleSigned:
00355              str  = h235->desc + PString(" {sign}");
00356          break;
00357       case Pluginh235_TokenStyleEncrypted:
00358              str  = h235->desc + PString(" {enc}");
00359          break;
00360       case Pluginh235_TokenTypeclear: 
00361              str  = h235->desc + PString(" {clear}");
00362          break;     
00363       default:
00364          str = h235->desc;
00365   }
00366 
00367   return str;
00368 }
00369 
00370 void h235PluginDeviceManager::CreateH235Authenticator(Pluginh235_Definition * h235)
00371 {
00372   // make sure all non-timestamped codecs have the same concept of "now"
00373   static time_t mediaNow = time(NULL);
00374 
00375   // deal with codec having no info, or timestamp in future
00376   time_t timeStamp = h235->info == NULL ? mediaNow : h235->info->timestamp;
00377   if (timeStamp > mediaNow)
00378     timeStamp = mediaNow;
00379 
00380 // Authenticator Name
00381   PString h235Name = PString(); 
00382   H235PluginAuthenticator * auth = NULL;
00383 
00384 
00385 // Type of h235 Plugin
00386   switch (h235->flags & Pluginh235_TokenTypeMask) {
00387         case Pluginh235_TokenTypecrypto:  
00388             switch (h235->flags & Pluginh235_TokenTypeMask) {
00389               case Pluginh235_TokenStyleHash:
00390                         h235Name = Createh235Name(h235,Pluginh235_TokenStyleHash);
00391                 break;
00392               case Pluginh235_TokenStyleSigned:
00393                         h235Name = Createh235Name(h235,Pluginh235_TokenStyleSigned);
00394                 break;
00395               case Pluginh235_TokenStyleEncrypted:
00396                         h235Name = Createh235Name(h235,Pluginh235_TokenStyleEncrypted);
00397                 break;
00398               default:
00399                 h235Name = h235->desc;
00400              }
00401             break;
00402         case Pluginh235_TokenTypeclear: 
00403             h235Name = Createh235Name(h235, Pluginh235_TokenTypeclear);
00404             break;
00405         default:
00406             h235Name = h235->desc;
00407             break;
00408   }     
00409                 
00410   auth = new H235PluginAuthenticator(h235);
00411   auth->SetName(h235Name);
00412                   
00413 
00414   if (auth != NULL)
00415      h235Factory::Register(h235Name, auth);
00416 }
00417 
00418 #ifdef _MSC_VER
00419 #pragma warning(default:4700)
00420 #endif

Generated on Thu Oct 25 13:42:53 2007 for h323plus by  doxygen 1.5.2