Bug 1308890 - Make use of ScopedAutoSECItem in CryptoKey.cpp. r=rbarnes
authorCykesiopka <cykesiopka.bmo@gmail.com>
Fri, 14 Oct 2016 10:36:54 +0800
changeset 360891 32425af1283d
parent 360890 905f1bc96fb6
child 360892 60af9c33d9c1
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-beta@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrbarnes
bugs1308890
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 1308890 - Make use of ScopedAutoSECItem in CryptoKey.cpp. r=rbarnes ScopedAutoSECItem is useful for getting rid of this pattern: > ScopedSECItem item(SECITEM_AllocItem(nullptr, nullptr, 0)); While this pattern works, ScopedAutoSECItem is slightly superior in that it doesn't unnecessarily cause a SECItem to be allocated from the heap. Moreover, this moves us further away from Scoped.h, which is deprecated. MozReview-Commit-ID: B30MNTHzzTc
dom/crypto/CryptoKey.cpp
--- a/dom/crypto/CryptoKey.cpp
+++ b/dom/crypto/CryptoKey.cpp
@@ -325,26 +325,26 @@ CryptoKey::AddPublicKeyData(SECKEYPublic
   // There should be a private NSS key with type 'EC'.
   MOZ_ASSERT(mPrivateKey && mPrivateKey->keyType == ecKey);
   // The given public key should have the same key type.
   MOZ_ASSERT(aPublicKey->keyType == mPrivateKey->keyType);
 
   nsNSSShutDownPreventionLock locker;
 
   // Read EC params.
-  ScopedSECItem params(::SECITEM_AllocItem(nullptr, nullptr, 0));
+  ScopedAutoSECItem params;
   SECStatus rv = PK11_ReadRawAttribute(PK11_TypePrivKey, mPrivateKey,
-                                       CKA_EC_PARAMS, params);
+                                       CKA_EC_PARAMS, &params);
   if (rv != SECSuccess) {
     return NS_ERROR_DOM_OPERATION_ERR;
   }
 
   // Read private value.
-  ScopedSECItem value(::SECITEM_AllocItem(nullptr, nullptr, 0));
-  rv = PK11_ReadRawAttribute(PK11_TypePrivKey, mPrivateKey, CKA_VALUE, value);
+  ScopedAutoSECItem value;
+  rv = PK11_ReadRawAttribute(PK11_TypePrivKey, mPrivateKey, CKA_VALUE, &value);
   if (rv != SECSuccess) {
     return NS_ERROR_DOM_OPERATION_ERR;
   }
 
   SECItem* point = &aPublicKey->u.ec.publicValue;
   CK_OBJECT_CLASS privateKeyValue = CKO_PRIVATE_KEY;
   CK_BBOOL falseValue = CK_FALSE;
   CK_KEY_TYPE ecValue = CKK_EC;
@@ -352,19 +352,19 @@ CryptoKey::AddPublicKeyData(SECKEYPublic
   CK_ATTRIBUTE keyTemplate[9] = {
     { CKA_CLASS,            &privateKeyValue,     sizeof(privateKeyValue) },
     { CKA_KEY_TYPE,         &ecValue,             sizeof(ecValue) },
     { CKA_TOKEN,            &falseValue,          sizeof(falseValue) },
     { CKA_SENSITIVE,        &falseValue,          sizeof(falseValue) },
     { CKA_PRIVATE,          &falseValue,          sizeof(falseValue) },
     // PrivateKeyFromPrivateKeyTemplate sets the ID.
     { CKA_ID,               nullptr,              0 },
-    { CKA_EC_PARAMS,        params->data,         params->len },
+    { CKA_EC_PARAMS,        params.data,          params.len },
     { CKA_EC_POINT,         point->data,          point->len },
-    { CKA_VALUE,            value->data,          value->len },
+    { CKA_VALUE,            value.data,           value.len },
   };
 
   mPrivateKey = PrivateKeyFromPrivateKeyTemplate(keyTemplate,
                                                  ArrayLength(keyTemplate));
   NS_ENSURE_TRUE(mPrivateKey, NS_ERROR_DOM_OPERATION_ERR);
 
   return NS_OK;
 }
@@ -847,28 +847,24 @@ CryptoKey::PrivateKeyFromJwk(const JsonW
 
   return nullptr;
 }
 
 bool ReadAndEncodeAttribute(SECKEYPrivateKey* aKey,
                             CK_ATTRIBUTE_TYPE aAttribute,
                             Optional<nsString>& aDst)
 {
-  ScopedSECItem item(::SECITEM_AllocItem(nullptr, nullptr, 0));
-  if (!item) {
-    return false;
-  }
-
-  if (PK11_ReadRawAttribute(PK11_TypePrivKey, aKey, aAttribute, item)
+  ScopedAutoSECItem item;
+  if (PK11_ReadRawAttribute(PK11_TypePrivKey, aKey, aAttribute, &item)
         != SECSuccess) {
     return false;
   }
 
   CryptoBuffer buffer;
-  if (!buffer.Assign(item)) {
+  if (!buffer.Assign(&item)) {
     return false;
   }
 
   if (NS_FAILED(buffer.ToJwkBase64(aDst.Value()))) {
     return false;
   }
 
   return true;
@@ -966,32 +962,32 @@ CryptoKey::PrivateKeyToJwk(SECKEYPrivate
         return NS_ERROR_DOM_OPERATION_ERR;
       }
 
       aRetVal.mKty = NS_LITERAL_STRING(JWK_TYPE_RSA);
       return NS_OK;
     }
     case ecKey: {
       // Read EC params.
-      ScopedSECItem params(::SECITEM_AllocItem(nullptr, nullptr, 0));
+      ScopedAutoSECItem params;
       SECStatus rv = PK11_ReadRawAttribute(PK11_TypePrivKey, aPrivKey,
-                                           CKA_EC_PARAMS, params);
+                                           CKA_EC_PARAMS, &params);
       if (rv != SECSuccess) {
         return NS_ERROR_DOM_OPERATION_ERR;
       }
 
       // Read public point Q.
-      ScopedSECItem ecPoint(::SECITEM_AllocItem(nullptr, nullptr, 0));
+      ScopedAutoSECItem ecPoint;
       rv = PK11_ReadRawAttribute(PK11_TypePrivKey, aPrivKey, CKA_EC_POINT,
-                                 ecPoint);
+                                 &ecPoint);
       if (rv != SECSuccess) {
         return NS_ERROR_DOM_OPERATION_ERR;
       }
 
-      if (!ECKeyToJwk(PK11_TypePrivKey, aPrivKey, params, ecPoint, aRetVal)) {
+      if (!ECKeyToJwk(PK11_TypePrivKey, aPrivKey, &params, &ecPoint, aRetVal)) {
         return NS_ERROR_DOM_OPERATION_ERR;
       }
 
       aRetVal.mD.Construct();
 
       // Read private value.
       if (!ReadAndEncodeAttribute(aPrivKey, CKA_VALUE, aRetVal.mD)) {
         return NS_ERROR_DOM_OPERATION_ERR;