Logo Search packages:      
Sourcecode: chromium-browser version File versions  Download package


// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


#include <windows.h>
#include <wincrypt.h>

#include <algorithm>

#include "base/logging.h"

namespace base {

// Simple destructor for the Free family of CryptoAPI functions, such as
// CryptDestroyHash, which take only a single argument to release.
template <typename CAPIHandle, BOOL (WINAPI *Destroyer)(CAPIHandle)>
struct CAPIDestroyer {
  void operator()(CAPIHandle handle) const {
    if (handle) {
      BOOL ok = Destroyer(handle);

// Destructor for the Close/Release family of CryptoAPI functions, which take
// a second DWORD parameter indicating flags to use when closing or releasing.
// This includes functions like CertCloseStore or CryptReleaseContext.
template <typename CAPIHandle, BOOL (WINAPI *Destroyer)(CAPIHandle, DWORD),
          DWORD flags>
struct CAPIDestroyerWithFlags {
  void operator()(CAPIHandle handle) const {
    if (handle) {
      BOOL ok = Destroyer(handle, flags);

// scoped_ptr-like class for the CryptoAPI cryptography and certificate
// handles. Because these handles are defined as integer types, and not
// pointers, the existing scoped classes, such as scoped_ptr_malloc, are
// insufficient. The semantics are the same as scoped_ptr.
template <class CAPIHandle, typename FreeProc>
class ScopedCAPIHandle {
  explicit ScopedCAPIHandle(CAPIHandle handle = NULL) : handle_(handle) {}

  ~ScopedCAPIHandle() {

  void reset(CAPIHandle handle = NULL) {
    if (handle_ != handle) {
      handle_ = handle;

  operator CAPIHandle() const { return handle_; }
  CAPIHandle get() const { return handle_; }

  CAPIHandle* receive() {
    CHECK_EQ(NULL, handle_);
    return &handle_;

  bool operator==(CAPIHandle handle) const {
    return handle_ == handle;

  bool operator!=(CAPIHandle handle) const {
    return handle_ != handle;

  void swap(ScopedCAPIHandle& b) {
    CAPIHandle tmp = b.handle_;
    b.handle_ = handle_;
    handle_ = tmp;

  CAPIHandle release() {
    CAPIHandle tmp = handle_;
    handle_ = NULL;
    return tmp;

  CAPIHandle handle_;
  static const FreeProc free_;


template<class CH, typename FP>
const FP ScopedCAPIHandle<CH, FP>::free_ = FP();

template<class CH, typename FP> inline
bool operator==(CH h, const ScopedCAPIHandle<CH, FP>& b) {
  return h == b.get();

template<class CH, typename FP> inline
bool operator!=(CH h, const ScopedCAPIHandle<CH, FP>& b) {
  return h != b.get();

typedef ScopedCAPIHandle<
                           CryptReleaseContext, 0> > ScopedHCRYPTPROV;

typedef ScopedCAPIHandle<
    HCRYPTKEY, CAPIDestroyer<HCRYPTKEY, CryptDestroyKey> > ScopedHCRYPTKEY;

typedef ScopedCAPIHandle<
    HCRYPTHASH, CAPIDestroyer<HCRYPTHASH, CryptDestroyHash> > ScopedHCRYPTHASH;

}  // namespace base


Generated by  Doxygen 1.6.0   Back to index