Back to index

lightning-sunbird  0.9+nobinonly
nsPKCS12Blob.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Ian McGreer <mcgreer@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /* $Id: nsPKCS12Blob.h,v 1.13.28.2 2006/01/31 12:50:31 kaie%kuix.de Exp $ */
00038 
00039 #ifndef _NS_PKCS12BLOB_H_
00040 #define _NS_PKCS12BLOB_H_
00041 
00042 #include "nsCOMPtr.h"
00043 #include "nsILocalFile.h"
00044 #include "nsIPK11TokenDB.h"
00045 #include "nsNSSHelper.h"
00046 #include "nsIPK11Token.h"
00047 #include "nsIArray.h"
00048 
00049 #include "nss.h"
00050 
00051 extern "C" {
00052 #include "pkcs12.h"
00053 #include "p12plcy.h"
00054 }
00055 
00056 class nsIX509Cert;
00057 
00058 //
00059 // nsPKCS12Blob
00060 //
00061 // Class for importing/exporting PKCS#12 blobs
00062 //
00063 class nsPKCS12Blob
00064 {
00065 public:
00066   nsPKCS12Blob();
00067   virtual ~nsPKCS12Blob();
00068 
00069   // Set the token to use (default is internal)
00070   nsresult SetToken(nsIPK11Token *token);
00071 
00072   // PKCS#12 Import
00073   nsresult ImportFromFile(nsILocalFile *file);
00074 
00075   // PKCS#12 Export
00076 #if 0
00077   //nsresult LoadCerts(const PRUnichar **certNames, int numCerts);
00078   nsresult LoadCerts(nsIX509Cert **certs, int numCerts);
00079 #endif
00080   nsresult ExportToFile(nsILocalFile *file, nsIX509Cert **certs, int numCerts);
00081 
00082 private:
00083 
00084   nsCOMPtr<nsIPK11Token>          mToken;
00085   nsCOMPtr<nsIMutableArray>       mCertArray;
00086   nsCOMPtr<nsIInterfaceRequestor> mUIContext;
00087 
00088   // local helper functions
00089   nsresult getPKCS12FilePassword(SECItem *);
00090   nsresult newPKCS12FilePassword(SECItem *);
00091   nsresult inputToDecoder(SEC_PKCS12DecoderContext *, nsILocalFile *);
00092   void unicodeToItem(const PRUnichar *, SECItem *);
00093   PRBool handleError(int myerr = 0);
00094 
00095   // RetryReason and ImportMode are used when importing a PKCS12 file.
00096   // There are two reasons that cause us to retry:
00097   // - When the password entered by the user is incorrect.
00098   //   The user will be prompted to try again.
00099   // - When the user entered a zero length password.
00100   //   An empty password should be represented as an empty
00101   //   string (a SECItem that contains a single terminating
00102   //   NULL UTF16 character), but some applications use a
00103   //   zero length SECItem.
00104   //   We try both variations, zero length item and empty string,
00105   //   without giving a user prompt when trying the different empty password flavors.
00106   
00107   enum RetryReason { rr_do_not_retry, rr_bad_password, rr_auto_retry_empty_password_flavors };
00108   enum ImportMode { im_standard_prompt, im_try_zero_length_secitem };
00109   
00110   nsresult ImportFromFileHelper(nsILocalFile *file, ImportMode aImportMode, RetryReason &aWantRetry);
00111 
00112   // NSPR file I/O for export file
00113   PRFileDesc *mTmpFile;
00114   char       *mTmpFilePath;
00115 
00116   // simulated file I/O for "in memory" temporary digest data
00117   nsCString                 *mDigest;
00118   nsCString::const_iterator *mDigestIterator;
00119 
00120   PRBool      mTokenSet;
00121 
00122   // C-style callback functions for the NSS PKCS#12 library
00123   static SECStatus PR_CALLBACK digest_open(void *, PRBool);
00124   static SECStatus PR_CALLBACK digest_close(void *, PRBool);
00125   static int       PR_CALLBACK digest_read(void *, unsigned char *, unsigned long);
00126   static int       PR_CALLBACK digest_write(void *, unsigned char *, unsigned long);
00127   static SECItem * PR_CALLBACK nickname_collision(SECItem *, PRBool *, void *);
00128   static void PR_CALLBACK write_export_file(void *arg, const char *buf, unsigned long len);
00129 
00130 };
00131 
00132 #endif /* _NS_PKCS12BLOB_H_ */
00133