Back to index

lightning-sunbird  0.9+nobinonly
nsNSSModule.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Hubbie Shaw
00025  *   Doug Turner <dougt@netscape.com>
00026  *   Brian Ryner <bryner@brianryner.com>
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 #include "nsIModule.h"
00043 #include "nsIGenericFactory.h"
00044 
00045 #include "nsNSSComponent.h"
00046 #include "nsSSLSocketProvider.h"
00047 #include "nsTLSSocketProvider.h"
00048 #include "nsKeygenHandler.h"
00049 
00050 #include "nsSDR.h"
00051 
00052 #include "nsPK11TokenDB.h"
00053 #include "nsPKCS11Slot.h"
00054 #include "nsNSSCertificate.h"
00055 #include "nsNSSCertificateDB.h"
00056 #include "nsNSSCertCache.h"
00057 #include "nsCMS.h"
00058 #ifdef MOZ_XUL
00059 #include "nsCertTree.h"
00060 #endif
00061 #include "nsCrypto.h"
00062 //For the NS_CRYPTO_CONTRACTID define
00063 #include "nsDOMCID.h"
00064 
00065 #include "nsCMSSecureMessage.h"
00066 #include "nsCertPicker.h"
00067 #include "nsCURILoader.h"
00068 #include "nsICategoryManager.h"
00069 #include "nsCRLManager.h"
00070 #include "nsCipherInfo.h"
00071 #include "nsNTLMAuthModule.h"
00072 #include "nsStreamCipher.h"
00073 #include "nsKeyModule.h"
00074 
00075 // We must ensure that the nsNSSComponent has been loaded before
00076 // creating any other components.
00077 static void EnsureNSSInitialized(PRBool triggeredByNSSComponent)
00078 {
00079   static PRBool haveLoaded = PR_FALSE;
00080   if (haveLoaded)
00081     return;
00082 
00083   haveLoaded = PR_TRUE;
00084   
00085   if (triggeredByNSSComponent) {
00086     // We must prevent a recursion, as nsNSSComponent creates
00087     // additional instances
00088     return;
00089   }
00090   
00091   nsCOMPtr<nsISupports> nssComponent 
00092     = do_GetService(PSM_COMPONENT_CONTRACTID);
00093 }
00094 
00095 // These two macros are ripped off from nsIGenericFactory.h and slightly
00096 // modified.
00097 #define NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(triggeredByNSSComponent,           \
00098                                                       _InstanceClass)         \
00099 static NS_IMETHODIMP                                                          \
00100 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID,               \
00101                             void **aResult)                                   \
00102 {                                                                             \
00103     nsresult rv;                                                              \
00104     _InstanceClass * inst;                                                    \
00105                                                                               \
00106     EnsureNSSInitialized(triggeredByNSSComponent);                            \
00107                                                                               \
00108     *aResult = NULL;                                                          \
00109     if (NULL != aOuter) {                                                     \
00110         rv = NS_ERROR_NO_AGGREGATION;                                         \
00111         return rv;                                                            \
00112     }                                                                         \
00113                                                                               \
00114     NS_NEWXPCOM(inst, _InstanceClass);                                        \
00115     if (NULL == inst) {                                                       \
00116         rv = NS_ERROR_OUT_OF_MEMORY;                                          \
00117         return rv;                                                            \
00118     }                                                                         \
00119     NS_ADDREF(inst);                                                          \
00120     rv = inst->QueryInterface(aIID, aResult);                                 \
00121     NS_RELEASE(inst);                                                         \
00122                                                                               \
00123     return rv;                                                                \
00124 }                                                                             \
00125 
00126  
00127 #define NS_NSS_GENERIC_FACTORY_CONSTRUCTOR_INIT(triggeredByNSSComponent,      \
00128                                                 _InstanceClass, _InitMethod)  \
00129 static NS_IMETHODIMP                                                          \
00130 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID,               \
00131                             void **aResult)                                   \
00132 {                                                                             \
00133     nsresult rv;                                                              \
00134     _InstanceClass * inst;                                                    \
00135                                                                               \
00136     EnsureNSSInitialized(triggeredByNSSComponent);                            \
00137                                                                               \
00138     *aResult = NULL;                                                          \
00139     if (NULL != aOuter) {                                                     \
00140         rv = NS_ERROR_NO_AGGREGATION;                                         \
00141         return rv;                                                            \
00142     }                                                                         \
00143                                                                               \
00144     NS_NEWXPCOM(inst, _InstanceClass);                                        \
00145     if (NULL == inst) {                                                       \
00146         rv = NS_ERROR_OUT_OF_MEMORY;                                          \
00147         return rv;                                                            \
00148     }                                                                         \
00149     NS_ADDREF(inst);                                                          \
00150     rv = inst->_InitMethod();                                                 \
00151     if(NS_SUCCEEDED(rv)) {                                                    \
00152         rv = inst->QueryInterface(aIID, aResult);                             \
00153     }                                                                         \
00154     NS_RELEASE(inst);                                                         \
00155                                                                               \
00156     return rv;                                                                \
00157 }                                                                             \
00158 
00159 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR_INIT(PR_TRUE, nsNSSComponent, Init)
00160 
00161 // Use the special factory constructor for everything this module implements,
00162 // because all code could potentially require the NSS library.
00163 // Our factory constructor takes an additional boolean parameter.
00164 // Only for the nsNSSComponent, set this to PR_TRUE.
00165 // All other classes must have this set to PR_FALSE.
00166 
00167 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsSSLSocketProvider)
00168 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsTLSSocketProvider)
00169 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsSecretDecoderRing)
00170 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsPK11TokenDB)
00171 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsPKCS11ModuleDB)
00172 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR_INIT(PR_FALSE, PSMContentListener, init)
00173 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsNSSCertificateDB)
00174 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsNSSCertCache)
00175 #ifdef MOZ_XUL
00176 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsCertTree)
00177 #endif
00178 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsCrypto)
00179 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsPkcs11)
00180 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsCMSSecureMessage)
00181 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsCMSDecoder)
00182 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsCMSEncoder)
00183 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsCMSMessage)
00184 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsCertPicker)
00185 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsCRLManager)
00186 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsCipherInfoService)
00187 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR_INIT(PR_FALSE, nsNTLMAuthModule, InitTest)
00188 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsCryptoHash)
00189 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsStreamCipher)
00190 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsKeyObject)
00191 NS_NSS_GENERIC_FACTORY_CONSTRUCTOR(PR_FALSE, nsKeyObjectFactory)
00192 
00193 static NS_METHOD RegisterPSMContentListeners(
00194                       nsIComponentManager *aCompMgr,
00195                       nsIFile *aPath, const char *registryLocation, 
00196                       const char *componentType, const nsModuleComponentInfo *info)
00197 {
00198   nsresult rv;
00199   nsCOMPtr<nsICategoryManager> catman = 
00200     do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
00201   if (NS_FAILED(rv)) return rv;
00202 
00203   nsXPIDLCString previous;
00204 
00205   catman->AddCategoryEntry(
00206     NS_CONTENT_LISTENER_CATEGORYMANAGER_ENTRY,
00207     "application/x-x509-ca-cert",
00208     info->mContractID, PR_TRUE, PR_TRUE, getter_Copies(previous));
00209 
00210   catman->AddCategoryEntry(
00211     NS_CONTENT_LISTENER_CATEGORYMANAGER_ENTRY,
00212     "application/x-x509-server-cert",
00213     info->mContractID, PR_TRUE, PR_TRUE, getter_Copies(previous));
00214 
00215   catman->AddCategoryEntry(
00216     NS_CONTENT_LISTENER_CATEGORYMANAGER_ENTRY,
00217     "application/x-x509-user-cert",
00218     info->mContractID, PR_TRUE, PR_TRUE, getter_Copies(previous));
00219 
00220   catman->AddCategoryEntry(
00221     NS_CONTENT_LISTENER_CATEGORYMANAGER_ENTRY,
00222     "application/x-x509-email-cert",
00223     info->mContractID, PR_TRUE, PR_TRUE, getter_Copies(previous));
00224 
00225   catman->AddCategoryEntry(
00226     NS_CONTENT_LISTENER_CATEGORYMANAGER_ENTRY,
00227     "application/x-pkcs7-crl",
00228     info->mContractID, PR_TRUE, PR_TRUE, getter_Copies(previous));
00229 
00230   catman->AddCategoryEntry(
00231     NS_CONTENT_LISTENER_CATEGORYMANAGER_ENTRY,
00232     "application/x-x509-crl",
00233     info->mContractID, PR_TRUE, PR_TRUE, getter_Copies(previous));
00234 
00235   catman->AddCategoryEntry(
00236     NS_CONTENT_LISTENER_CATEGORYMANAGER_ENTRY,
00237     "application/pkix-crl",
00238     info->mContractID, PR_TRUE, PR_TRUE, getter_Copies(previous));
00239 
00240   return NS_OK;
00241 }
00242 
00243 static const nsModuleComponentInfo components[] =
00244 {
00245   {
00246     PSM_COMPONENT_CLASSNAME,
00247     NS_NSSCOMPONENT_CID,
00248     PSM_COMPONENT_CONTRACTID,
00249     nsNSSComponentConstructor
00250   },
00251   
00252   {
00253     NS_SSLSOCKETPROVIDER_CLASSNAME,
00254     NS_SSLSOCKETPROVIDER_CID,
00255     NS_SSLSOCKETPROVIDER_CONTRACTID,
00256     nsSSLSocketProviderConstructor
00257   },
00258   
00259   {
00260     NS_STARTTLSSOCKETPROVIDER_CLASSNAME,
00261     NS_STARTTLSSOCKETPROVIDER_CID,
00262     NS_STARTTLSSOCKETPROVIDER_CONTRACTID,
00263     nsTLSSocketProviderConstructor
00264   },
00265   
00266   {
00267     NS_SDR_CLASSNAME,
00268     NS_SDR_CID,
00269     NS_SDR_CONTRACTID,
00270     nsSecretDecoderRingConstructor
00271   },
00272 
00273   {
00274     "PK11 Token Database",
00275     NS_PK11TOKENDB_CID,
00276     NS_PK11TOKENDB_CONTRACTID,
00277     nsPK11TokenDBConstructor
00278   },
00279 
00280   {
00281     "PKCS11 Module Database",
00282     NS_PKCS11MODULEDB_CID,
00283     NS_PKCS11MODULEDB_CONTRACTID,
00284     nsPKCS11ModuleDBConstructor
00285   },
00286 
00287   {
00288     "Generic Certificate Content Handler",
00289     NS_PSMCONTENTLISTEN_CID,
00290     NS_PSMCONTENTLISTEN_CONTRACTID,
00291     PSMContentListenerConstructor
00292   },
00293 
00294   {
00295     "X509 Certificate Database",
00296     NS_X509CERTDB_CID,
00297     NS_X509CERTDB_CONTRACTID,
00298     nsNSSCertificateDBConstructor
00299   },
00300 
00301   {
00302     "NSS Certificate Cache",
00303     NS_NSSCERTCACHE_CID,
00304     NS_NSSCERTCACHE_CONTRACTID,
00305     nsNSSCertCacheConstructor
00306   },
00307 
00308   {
00309     "Form Processor",
00310     NS_FORMPROCESSOR_CID,
00311     NS_FORMPROCESSOR_CONTRACTID,
00312     nsKeygenFormProcessor::Create
00313   },
00314 #ifdef MOZ_XUL
00315   {
00316     "Certificate Tree",
00317     NS_CERTTREE_CID,
00318     NS_CERTTREE_CONTRACTID,
00319     nsCertTreeConstructor
00320   },
00321 #endif
00322   {
00323     NS_PKCS11_CLASSNAME,
00324     NS_PKCS11_CID,
00325     NS_PKCS11_CONTRACTID,
00326     nsPkcs11Constructor
00327   },
00328 
00329   {
00330     NS_CRYPTO_CLASSNAME,
00331     NS_CRYPTO_CID,
00332     NS_CRYPTO_CONTRACTID,
00333     nsCryptoConstructor
00334   },
00335 
00336   {
00337     NS_CMSSECUREMESSAGE_CLASSNAME,
00338     NS_CMSSECUREMESSAGE_CID,
00339     NS_CMSSECUREMESSAGE_CONTRACTID,
00340     nsCMSSecureMessageConstructor
00341   },
00342 
00343   {
00344     NS_CMSDECODER_CLASSNAME,
00345     NS_CMSDECODER_CID,
00346     NS_CMSDECODER_CONTRACTID,
00347     nsCMSDecoderConstructor
00348   },
00349 
00350   {
00351     NS_CMSENCODER_CLASSNAME,
00352     NS_CMSENCODER_CID,
00353     NS_CMSENCODER_CONTRACTID,
00354     nsCMSEncoderConstructor
00355   },
00356 
00357   {
00358     NS_CMSMESSAGE_CLASSNAME,
00359     NS_CMSMESSAGE_CID,
00360     NS_CMSMESSAGE_CONTRACTID,
00361     nsCMSMessageConstructor
00362   },
00363 
00364   {
00365     NS_CRYPTO_HASH_CLASSNAME,
00366     NS_CRYPTO_HASH_CID,
00367     NS_CRYPTO_HASH_CONTRACTID,
00368     nsCryptoHashConstructor
00369   },
00370 
00371   {
00372     NS_CERT_PICKER_CLASSNAME,
00373     NS_CERT_PICKER_CID,
00374     NS_CERT_PICKER_CONTRACTID,
00375     nsCertPickerConstructor
00376   },
00377 
00378   {
00379     "PSM Content Listeners",
00380     NS_PSMCONTENTLISTEN_CID,
00381     "@mozilla.org/uriloader/psm-external-content-listener;1",
00382     PSMContentListenerConstructor,
00383     RegisterPSMContentListeners
00384   },
00385 
00386   {
00387     "PSM CRL Manager",
00388     NS_CRLMANAGER_CID,
00389     NS_CRLMANAGER_CONTRACTID,
00390     nsCRLManagerConstructor
00391   },
00392   
00393   {
00394     "PSM Cipher Info",
00395     NS_CIPHERINFOSERVICE_CID,
00396     NS_CIPHERINFOSERVICE_CONTRACTID,
00397     nsCipherInfoServiceConstructor
00398   },
00399   
00400   {
00401     NS_CRYPTO_FIPSINFO_SERVICE_CLASSNAME,
00402     NS_PKCS11MODULEDB_CID,
00403     NS_CRYPTO_FIPSINFO_SERVICE_CONTRACTID,
00404     nsPKCS11ModuleDBConstructor
00405   },
00406 
00407   {
00408     NS_NTLMAUTHMODULE_CLASSNAME,
00409     NS_NTLMAUTHMODULE_CID,
00410     NS_NTLMAUTHMODULE_CONTRACTID,
00411     nsNTLMAuthModuleConstructor
00412   },
00413 
00414   {
00415     NS_STREAMCIPHER_CLASSNAME,
00416     NS_STREAMCIPHER_CID,
00417     NS_STREAMCIPHER_CONTRACTID,
00418     nsStreamCipherConstructor
00419   },
00420 
00421   {
00422     NS_KEYMODULEOBJECT_CLASSNAME,
00423     NS_KEYMODULEOBJECT_CID,
00424     NS_KEYMODULEOBJECT_CONTRACTID,
00425     nsKeyObjectConstructor
00426   },
00427 
00428   {
00429     NS_KEYMODULEOBJECTFACTORY_CLASSNAME,
00430     NS_KEYMODULEOBJECTFACTORY_CID,
00431     NS_KEYMODULEOBJECTFACTORY_CONTRACTID,
00432     nsKeyObjectFactoryConstructor
00433   }
00434 };
00435 
00436 NS_IMPL_NSGETMODULE(NSS, components)