Bug 1305422 - part 15 - don't call size_forward in nsPKCS12Blob; r=mgoodwin,nfroyd
authorDavid Keeler <dkeeler@mozilla.com>
Thu, 29 Sep 2016 19:33:57 -0700
changeset 315878 daf738c4a41117ef270fc56dd1ec10121b596802
parent 315877 e8f15fc02cfc3389a47cf5b2ab19bd42aac829f3
child 315879 8ad96d4c9d427e135e51b9dd7af31a9d99782e2c
push id30757
push usercbook@mozilla.com
push dateFri, 30 Sep 2016 10:02:43 +0000
treeherdermozilla-central@5ffed033557e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmgoodwin, nfroyd
bugs1305422
milestone52.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1305422 - part 15 - don't call size_forward in nsPKCS12Blob; r=mgoodwin,nfroyd Before this patch, nsPKCS12Blob::digest_read used size_forward to perform a size check on a buffer. However, the entire set of {digest_open, digest_close, digest_read, digest_write} was unnecessary because NSS provides this functionality by default when using SEC_PKCS12DecoderStart. This patch simplifies things by removing the extraneous implementations.
security/manager/ssl/nsPKCS12Blob.cpp
security/manager/ssl/nsPKCS12Blob.h
--- a/security/manager/ssl/nsPKCS12Blob.cpp
+++ b/security/manager/ssl/nsPKCS12Blob.cpp
@@ -39,29 +39,24 @@ extern LazyLogModule gPIPNSSLog;
 #define PIP_PKCS12_NOSMARTCARD_EXPORT  4
 #define PIP_PKCS12_RESTORE_FAILED      5
 #define PIP_PKCS12_BACKUP_FAILED       6
 #define PIP_PKCS12_NSS_ERROR           7
 
 // constructor
 nsPKCS12Blob::nsPKCS12Blob():mCertArray(0),
                              mTmpFile(nullptr),
-                             mDigest(nullptr),
-                             mDigestIterator(nullptr),
                              mTokenSet(false)
 {
   mUIContext = new PipUIContext();
 }
 
 // destructor
 nsPKCS12Blob::~nsPKCS12Blob()
 {
-  delete mDigestIterator;
-  delete mDigest;
-
   nsNSSShutDownPreventionLock locker;
   if (isAlreadyShutDown()) {
     return;
   }
 
   shutdown(ShutdownCalledFrom::Object);
 }
 
@@ -174,20 +169,18 @@ nsPKCS12Blob::ImportFromFileHelper(nsIFi
     slot = UniquePK11SlotInfo(PK11_FindSlotByName(tokenNameCString.get()));
   }
   if (!slot) {
     srv = SECFailure;
     goto finish;
   }
 
   // initialize the decoder
-  dcx = SEC_PKCS12DecoderStart(&unicodePw, slot.get(), nullptr,
-                               digest_open, digest_close,
-                               digest_read, digest_write,
-                               this);
+  dcx = SEC_PKCS12DecoderStart(&unicodePw, slot.get(), nullptr, nullptr,
+                               nullptr, nullptr, nullptr, nullptr);
   if (!dcx) {
     srv = SECFailure;
     goto finish;
   }
   // read input file and feed it to the decoder
   rv = inputToDecoder(dcx, file);
   if (NS_FAILED(rv)) {
     if (NS_ERROR_ABORT == rv) {
@@ -499,115 +492,16 @@ nsPKCS12Blob::inputToDecoder(SEC_PKCS12D
       return NS_ERROR_ABORT;
     }
     if (amount < PIP_PKCS12_BUFFER_SIZE)
       break;
   }
   return NS_OK;
 }
 
-//
-// C callback methods
-//
-
-// digest_open
-// prepare a memory buffer for reading/writing digests
-SECStatus
-nsPKCS12Blob::digest_open(void *arg, PRBool reading)
-{
-  auto cx = static_cast<nsPKCS12Blob*>(arg);
-  NS_ENSURE_TRUE(cx, SECFailure);
-
-  if (reading) {
-    NS_ENSURE_TRUE(cx->mDigest, SECFailure);
-
-    delete cx->mDigestIterator;
-    cx->mDigestIterator = new nsCString::const_iterator;
-
-    if (!cx->mDigestIterator) {
-      PORT_SetError(SEC_ERROR_NO_MEMORY);
-      return SECFailure;
-    }
-
-    cx->mDigest->BeginReading(*cx->mDigestIterator);
-  }
-  else {
-    delete cx->mDigest;
-    cx->mDigest = new nsCString;
-
-    if (!cx->mDigest) {
-      PORT_SetError(SEC_ERROR_NO_MEMORY);
-      return SECFailure;
-    }
-  }
-
-  return SECSuccess;
-}
-
-// digest_close
-// destroy a possibly active iterator
-// remove the data buffer if requested
-SECStatus
-nsPKCS12Blob::digest_close(void *arg, PRBool remove_it)
-{
-  auto cx = static_cast<nsPKCS12Blob*>(arg);
-  NS_ENSURE_TRUE(cx, SECFailure);
-
-  delete cx->mDigestIterator;
-  cx->mDigestIterator = nullptr;
-
-  if (remove_it) {
-    delete cx->mDigest;
-    cx->mDigest = nullptr;
-  }
-
-  return SECSuccess;
-}
-
-// digest_read
-// read bytes from the memory buffer
-int
-nsPKCS12Blob::digest_read(void *arg, unsigned char *buf, unsigned long len)
-{
-  auto cx = static_cast<nsPKCS12Blob*>(arg);
-  NS_ENSURE_TRUE(cx, SECFailure);
-  NS_ENSURE_TRUE(cx->mDigest, SECFailure);
-
-  // iterator object must exist when digest has been opened in read mode
-  NS_ENSURE_TRUE(cx->mDigestIterator, SECFailure);
-
-  unsigned long available = cx->mDigestIterator->size_forward();
-
-  if (len > available)
-    len = available;
-
-  memcpy(buf, cx->mDigestIterator->get(), len);
-  cx->mDigestIterator->advance(len);
-
-  return len;
-}
-
-// digest_write
-// append bytes to the memory buffer
-int
-nsPKCS12Blob::digest_write(void *arg, unsigned char *buf, unsigned long len)
-{
-  auto cx = static_cast<nsPKCS12Blob*>(arg);
-  NS_ENSURE_TRUE(cx, SECFailure);
-  NS_ENSURE_TRUE(cx->mDigest, SECFailure);
-
-  // make sure we are in write mode, read iterator has not yet been allocated
-  NS_ENSURE_FALSE(cx->mDigestIterator, SECFailure);
-
-  cx->mDigest->Append(BitwiseCast<char*, unsigned char*>(buf),
-                      static_cast<uint32_t>(len));
-
-  return len;
-}
-
 // nickname_collision
 // what to do when the nickname collides with one already in the db.
 // TODO: not handled, throw a dialog allowing the nick to be changed?
 SECItem *
 nsPKCS12Blob::nickname_collision(SECItem *oldNick, PRBool *cancel, void *wincx)
 {
   static NS_DEFINE_CID(kNSSComponentCID, NS_NSSCOMPONENT_CID);
 
--- a/security/manager/ssl/nsPKCS12Blob.h
+++ b/security/manager/ssl/nsPKCS12Blob.h
@@ -72,25 +72,16 @@ private:
   enum RetryReason { rr_do_not_retry, rr_bad_password, rr_auto_retry_empty_password_flavors };
   enum ImportMode { im_standard_prompt, im_try_zero_length_secitem };
   
   nsresult ImportFromFileHelper(nsIFile *file, ImportMode aImportMode, RetryReason &aWantRetry);
 
   // NSPR file I/O for export file
   PRFileDesc *mTmpFile;
 
-  // simulated file I/O for "in memory" temporary digest data
-  nsCString                 *mDigest;
-  nsCString::const_iterator *mDigestIterator;
-
   bool        mTokenSet;
 
-  // C-style callback functions for the NSS PKCS#12 library
-  static SECStatus digest_open(void *, PRBool);
-  static SECStatus digest_close(void *, PRBool);
-  static int       digest_read(void *, unsigned char *, unsigned long);
-  static int       digest_write(void *, unsigned char *, unsigned long);
   static SECItem * nickname_collision(SECItem *, PRBool *, void *);
   static void write_export_file(void *arg, const char *buf, unsigned long len);
 
 };
 
 #endif /* _NS_PKCS12BLOB_H_ */