Bug 1271495 - Replace uses of ScopedPK11Context with UniquePK11Context. r=keeler,mcmanus

ScopedPK11Context is based on Scoped.h, which is deprecated in favour of the
standardised UniquePtr.

MozReview-Commit-ID: HE8UY1hOuph
This commit is contained in:
Cykesiopka
2016-05-13 05:53:57 -07:00
parent 27211263c3
commit e6204da51e
6 changed files with 33 additions and 34 deletions

View File

@@ -1003,7 +1003,7 @@ private:
// Compute the MAC
SECItem param = { siBuffer, nullptr, 0 };
ScopedPK11Context ctx(PK11_CreateContextBySymKey(mMechanism, CKA_SIGN,
UniquePK11Context ctx(PK11_CreateContextBySymKey(mMechanism, CKA_SIGN,
symKey.get(), &param));
if (!ctx.get()) {
return NS_ERROR_DOM_OPERATION_ERR;

View File

@@ -127,10 +127,7 @@ BackgroundFileSaver::~BackgroundFileSaver()
void
BackgroundFileSaver::destructorSafeDestroyNSSReference()
{
if (mDigestContext) {
mozilla::psm::PK11_DestroyContext_true(mDigestContext.forget());
mDigestContext = nullptr;
}
mDigestContext = nullptr;
}
void
@@ -554,8 +551,8 @@ BackgroundFileSaver::ProcessStateChange()
if (sha256Enabled && !mDigestContext) {
nsNSSShutDownPreventionLock lock;
if (!isAlreadyShutDown()) {
mDigestContext =
PK11_CreateDigestContext(static_cast<SECOidTag>(SEC_OID_SHA256));
mDigestContext = UniquePK11Context(
PK11_CreateDigestContext(SEC_OID_SHA256));
NS_ENSURE_TRUE(mDigestContext, NS_ERROR_OUT_OF_MEMORY);
}
}
@@ -586,7 +583,7 @@ BackgroundFileSaver::ProcessStateChange()
return NS_ERROR_NOT_AVAILABLE;
}
nsresult rv = MapSECStatus(PK11_DigestOp(mDigestContext,
nsresult rv = MapSECStatus(PK11_DigestOp(mDigestContext.get(),
uint8_t_ptr_cast(buffer),
count));
NS_ENSURE_SUCCESS(rv, rv);
@@ -632,7 +629,7 @@ BackgroundFileSaver::ProcessStateChange()
// the outputStream. BackgroundFileSaver is reference-counted before the
// call to AsyncCopy, and mDigestContext is never destroyed before
// AsyncCopyCallback.
outputStream = new DigestOutputStream(outputStream, mDigestContext);
outputStream = new DigestOutputStream(outputStream, mDigestContext.get());
}
// Start copying our input to the target file. No errors can be raised past

View File

@@ -242,7 +242,7 @@ private:
* Used to calculate the file hash. This keeps state across file renames and
* is lazily initialized in ProcessStateChange.
*/
ScopedPK11Context mDigestContext;
UniquePK11Context mDigestContext;
//////////////////////////////////////////////////////////////////////////////
//// Private methods

View File

@@ -176,12 +176,12 @@ VerifyStreamContentDigest(nsIInputStream* stream,
return NS_ERROR_SIGNED_JAR_ENTRY_TOO_LARGE;
}
ScopedPK11Context digestContext(PK11_CreateDigestContext(SEC_OID_SHA1));
UniquePK11Context digestContext(PK11_CreateDigestContext(SEC_OID_SHA1));
if (!digestContext) {
return mozilla::psm::GetXPCOMFromNSSError(PR_GetError());
}
rv = MapSECStatus(PK11_DigestBegin(digestContext));
rv = MapSECStatus(PK11_DigestBegin(digestContext.get()));
NS_ENSURE_SUCCESS(rv, rv);
uint64_t totalBytesRead = 0;
@@ -199,7 +199,7 @@ VerifyStreamContentDigest(nsIInputStream* stream,
return NS_ERROR_SIGNED_JAR_ENTRY_TOO_LARGE;
}
rv = MapSECStatus(PK11_DigestOp(digestContext, buf.data, bytesRead));
rv = MapSECStatus(PK11_DigestOp(digestContext.get(), buf.data, bytesRead));
NS_ENSURE_SUCCESS(rv, rv);
}

View File

@@ -52,7 +52,7 @@ namespace mozilla { namespace psm {
static SECStatus
CertIDHash(SHA384Buffer& buf, const CertID& certID)
{
ScopedPK11Context context(PK11_CreateDigestContext(SEC_OID_SHA384));
UniquePK11Context context(PK11_CreateDigestContext(SEC_OID_SHA384));
if (!context) {
return SECFailure;
}

View File

@@ -7,8 +7,8 @@
// This header provides smart pointers and various helpers for code that needs
// to interact with NSS.
#ifndef mozilla_ScopedNSSTypes_h
#define mozilla_ScopedNSSTypes_h
#ifndef ScopedNSSTypes_h
#define ScopedNSSTypes_h
#include <limits>
@@ -107,13 +107,22 @@ PK11_DestroyContext_true(PK11Context * ctx) {
} // namespace mozilla::psm
// Deprecated: use the equivalent UniquePtr templates instead.
MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE(ScopedPK11Context,
PK11Context,
mozilla::psm::PK11_DestroyContext_true)
MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE(ScopedSGNDigestInfo,
SGNDigestInfo,
SGN_DestroyDigestInfo)
// Emulates MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE, but for UniquePtrs.
#define MOZ_TYPE_SPECIFIC_UNIQUE_PTR_TEMPLATE(name, Type, Deleter) \
struct name##DeletePolicy \
{ \
void operator()(Type* aValue) { Deleter(aValue); } \
}; \
typedef UniquePtr<Type, name##DeletePolicy> name;
MOZ_TYPE_SPECIFIC_UNIQUE_PTR_TEMPLATE(UniquePK11Context,
PK11Context,
mozilla::psm::PK11_DestroyContext_true)
/** A more convenient way of dealing with digests calculated into
* stack-allocated buffers. NSS must be initialized on the main thread before
* use, and the caller must ensure NSS isn't shut down, typically by
@@ -130,15 +139,15 @@ MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE(ScopedSGNDigestInfo,
* Less typical usage, for digesting while doing streaming I/O and similar:
*
* Digest digest;
* ScopedPK11Context digestContext(PK11_CreateDigestContext(SEC_OID_SHA1));
* UniquePK11Context digestContext(PK11_CreateDigestContext(SEC_OID_SHA256));
* NS_ENSURE_TRUE(digestContext, NS_ERROR_OUT_OF_MEMORY);
* rv = MapSECStatus(PK11_DigestBegin(digestContext));
* rv = MapSECStatus(PK11_DigestBegin(digestContext.get()));
* NS_ENSURE_SUCCESS(rv, rv);
* for (...) {
* rv = MapSECStatus(PK11_DigestOp(digestContext, ...));
* rv = MapSECStatus(PK11_DigestOp(digestContext.get(), ...));
* NS_ENSURE_SUCCESS(rv, rv);
* }
* rv = digest.End(SEC_OID_SHA1, digestContext);
* rv = digest.End(SEC_OID_SHA256, digestContext);
* NS_ENSURE_SUCCESS(rv, rv)
*/
class Digest
@@ -162,12 +171,13 @@ public:
static_cast<int32_t>(len)));
}
nsresult End(SECOidTag hashAlg, ScopedPK11Context & context)
nsresult End(SECOidTag hashAlg, UniquePK11Context& context)
{
nsresult rv = SetLength(hashAlg);
NS_ENSURE_SUCCESS(rv, rv);
uint32_t len;
rv = MapSECStatus(PK11_DigestFinal(context, mItem.data, &len, mItem.len));
rv = MapSECStatus(PK11_DigestFinal(context.get(), mItem.data, &len,
mItem.len));
NS_ENSURE_SUCCESS(rv, rv);
context = nullptr;
NS_ENSURE_TRUE(len == mItem.len, NS_ERROR_UNEXPECTED);
@@ -315,14 +325,6 @@ MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE(ScopedSECAlgorithmID,
SECAlgorithmID,
internal::SECOID_DestroyAlgorithmID_true)
// Emulates MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE, but for UniquePtrs.
#define MOZ_TYPE_SPECIFIC_UNIQUE_PTR_TEMPLATE(name, Type, Deleter) \
struct name##DeletePolicy \
{ \
void operator()(Type* aValue) { Deleter(aValue); } \
}; \
typedef UniquePtr<Type, name##DeletePolicy> name;
MOZ_TYPE_SPECIFIC_UNIQUE_PTR_TEMPLATE(UniqueCERTCertificate,
CERTCertificate,
CERT_DestroyCertificate)
@@ -393,4 +395,4 @@ MOZ_TYPE_SPECIFIC_UNIQUE_PTR_TEMPLATE(UniqueVFYContext,
internal::VFY_DestroyContext_true)
} // namespace mozilla
#endif // mozilla_ScopedNSSTypes_h
#endif // ScopedNSSTypes_h