nsPKCS12Blob.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /* $Id: nsPKCS12Blob.h,v 1.16 2006/04/12 15:43:32 benjamin%smedbergs.us Exp $ */
  5. #ifndef _NS_PKCS12BLOB_H_
  6. #define _NS_PKCS12BLOB_H_
  7. #include "nsCOMPtr.h"
  8. #include "nsString.h"
  9. #include "nsIFile.h"
  10. #include "nsIPK11TokenDB.h"
  11. #include "nsNSSHelper.h"
  12. #include "nsIPK11Token.h"
  13. #include "nsIMutableArray.h"
  14. #include "nss.h"
  15. #include "pkcs12.h"
  16. #include "p12plcy.h"
  17. class nsIX509Cert;
  18. //
  19. // nsPKCS12Blob
  20. //
  21. // Class for importing/exporting PKCS#12 blobs
  22. //
  23. class nsPKCS12Blob : public nsNSSShutDownObject
  24. {
  25. public:
  26. nsPKCS12Blob();
  27. virtual ~nsPKCS12Blob();
  28. // Nothing to release.
  29. virtual void virtualDestroyNSSReference() override {}
  30. // Set the token to use (default is internal)
  31. nsresult SetToken(nsIPK11Token *token);
  32. // PKCS#12 Import
  33. nsresult ImportFromFile(nsIFile *file);
  34. // PKCS#12 Export
  35. nsresult ExportToFile(nsIFile *file, nsIX509Cert **certs, int numCerts);
  36. private:
  37. nsCOMPtr<nsIPK11Token> mToken;
  38. nsCOMPtr<nsIMutableArray> mCertArray;
  39. nsCOMPtr<nsIInterfaceRequestor> mUIContext;
  40. // local helper functions
  41. nsresult getPKCS12FilePassword(SECItem *);
  42. nsresult newPKCS12FilePassword(SECItem *);
  43. nsresult inputToDecoder(SEC_PKCS12DecoderContext *, nsIFile *);
  44. nsresult unicodeToItem(const char16_t *, SECItem *);
  45. void handleError(int myerr = 0);
  46. // RetryReason and ImportMode are used when importing a PKCS12 file.
  47. // There are two reasons that cause us to retry:
  48. // - When the password entered by the user is incorrect.
  49. // The user will be prompted to try again.
  50. // - When the user entered a zero length password.
  51. // An empty password should be represented as an empty
  52. // string (a SECItem that contains a single terminating
  53. // null UTF16 character), but some applications use a
  54. // zero length SECItem.
  55. // We try both variations, zero length item and empty string,
  56. // without giving a user prompt when trying the different empty password flavors.
  57. enum RetryReason { rr_do_not_retry, rr_bad_password, rr_auto_retry_empty_password_flavors };
  58. enum ImportMode { im_standard_prompt, im_try_zero_length_secitem };
  59. nsresult ImportFromFileHelper(nsIFile *file, ImportMode aImportMode, RetryReason &aWantRetry);
  60. // NSPR file I/O for export file
  61. PRFileDesc *mTmpFile;
  62. bool mTokenSet;
  63. static SECItem * nickname_collision(SECItem *, PRBool *, void *);
  64. static void write_export_file(void *arg, const char *buf, unsigned long len);
  65. };
  66. #endif /* _NS_PKCS12BLOB_H_ */