Back to index

lightning-sunbird  0.9+nobinonly
nsClientAuthRemember.h
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  * Red Hat, Inc.
00020  * Portions created by the Initial Developer are Copyright (C) 2008
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Kai Engert <kengert@redhat.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef __NSCLIENTAUTHREMEMBER_H__
00041 #define __NSCLIENTAUTHREMEMBER_H__
00042 
00043 #include "nsTHashtable.h"
00044 #include "nsIObserver.h"
00045 #include "nsIX509Cert.h"
00046 #include "nsAutoPtr.h"
00047 #include "nsNSSCertificate.h"
00048 #include "nsString.h"
00049 #include "nsWeakReference.h"
00050 #include "prmon.h"
00051 
00052 class nsClientAuthRemember
00053 {
00054 public:
00055 
00056   nsClientAuthRemember()
00057   {
00058   }
00059 
00060   nsClientAuthRemember(const nsClientAuthRemember &other)
00061   {
00062     this->operator=(other);
00063   }
00064 
00065   nsClientAuthRemember &operator=(const nsClientAuthRemember &other)
00066   {
00067     mAsciiHost = other.mAsciiHost;
00068     mFingerprint = other.mFingerprint;
00069     mClientNickname = other.mClientNickname;
00070     return *this;
00071   }
00072 
00073   nsCString mAsciiHost;
00074   nsCString mFingerprint;
00075   nsCString mClientNickname;
00076 };
00077 
00078 
00079 // hash entry class
00080 class nsClientAuthRememberEntry : public PLDHashEntryHdr
00081 {
00082   public:
00083     // Hash methods
00084     typedef const char* KeyType;
00085     typedef const char* KeyTypePointer;
00086 
00087     // do nothing with aHost - we require mHead to be set before we're live!
00088     nsClientAuthRememberEntry(KeyTypePointer aHostWithCertUTF8)
00089     {
00090     }
00091 
00092     nsClientAuthRememberEntry(const nsClientAuthRememberEntry& toCopy)
00093     {
00094       mSettings = toCopy.mSettings;
00095     }
00096 
00097     ~nsClientAuthRememberEntry()
00098     {
00099     }
00100 
00101     KeyType GetKey() const
00102     {
00103       return HostWithCertPtr();
00104     }
00105 
00106     KeyTypePointer GetKeyPointer() const
00107     {
00108       return HostWithCertPtr();
00109     }
00110 
00111     PRBool KeyEquals(KeyTypePointer aKey) const
00112     {
00113       return !strcmp(HostWithCertPtr(), aKey);
00114     }
00115 
00116     static KeyTypePointer KeyToPointer(KeyType aKey)
00117     {
00118       return aKey;
00119     }
00120 
00121     static PLDHashNumber HashKey(KeyTypePointer aKey)
00122     {
00123       // PL_DHashStringKey doesn't use the table parameter, so we can safely
00124       // pass nsnull
00125       return PL_DHashStringKey(nsnull, aKey);
00126     }
00127 
00128     enum { ALLOW_MEMMOVE = PR_FALSE };
00129 
00130     // get methods
00131     inline const nsCString &HostWithCert() const { return mHostWithCert; }
00132 
00133     inline KeyTypePointer HostWithCertPtr() const
00134     {
00135       return mHostWithCert.get();
00136     }
00137 
00138     nsClientAuthRemember mSettings;
00139     nsCString mHostWithCert;
00140 };
00141 
00142 class nsClientAuthRememberService : public nsIObserver,
00143                                     public nsSupportsWeakReference
00144 {
00145 public:
00146   NS_DECL_ISUPPORTS
00147   NS_DECL_NSIOBSERVER
00148 
00149   nsClientAuthRememberService();
00150   ~nsClientAuthRememberService();
00151 
00152   nsresult Init();
00153 
00154   static void GetHostWithCert(const nsACString & aHostName, 
00155                               const nsACString & nickname, nsACString& _retval);
00156 
00157   nsresult RememberDecision(const nsACString & aHostName, 
00158                             CERTCertificate *aServerCert, CERTCertificate *aClientCert);
00159   nsresult HasRememberedDecision(const nsACString & aHostName, 
00160                                  CERTCertificate *aCert, nsACString & aClientNickname, PRBool *_retval);
00161 
00162   void ClearRememberedDecisions();
00163 
00164 protected:
00165     PRMonitor *monitor;
00166     nsTHashtable<nsClientAuthRememberEntry> mSettingsTable;
00167 
00168     void RemoveAllFromMemory();
00169     nsresult AddEntryToList(const nsACString &host, 
00170                             const nsACString &server_fingerprint,
00171                             const nsACString &client_nickname);
00172 };
00173 
00174 #endif