summaryrefslogtreecommitdiffstats
path: root/crypto/scoped_capi_types.h
blob: f565121059da687b3d5c3ac532435cfc9603aafd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2011 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.

#ifndef CRYPTO_SCOPED_CAPI_TYPES_H_
#define CRYPTO_SCOPED_CAPI_TYPES_H_
#pragma once

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

#include <algorithm>

#include "base/logging.h"

namespace crypto {

// 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);
      DCHECK(ok);
    }
  }
};

// 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);
      DCHECK(ok);
    }
  }
};

// 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 {
 public:
  explicit ScopedCAPIHandle(CAPIHandle handle = NULL) : handle_(handle) {}

  ~ScopedCAPIHandle() {
    free_(handle_);
  }

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

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

  CAPIHandle* receive() {
    CHECK(handle_ == NULL);
    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;
  }

 private:
  CAPIHandle handle_;
  static const FreeProc free_;

  DISALLOW_COPY_AND_ASSIGN(ScopedCAPIHandle);
};

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<
    HCRYPTPROV,
    CAPIDestroyerWithFlags<HCRYPTPROV,
                           CryptReleaseContext, 0> > ScopedHCRYPTPROV;

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

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

}  // namespace crypto

#endif  // CRYPTO_SCOPED_CAPI_TYPES_H_