scoped_openssl_types.h revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CRYPTO_SCOPED_OPENSSL_TYPES_H_
6#define CRYPTO_SCOPED_OPENSSL_TYPES_H_
7
8#include <openssl/bn.h>
9#include <openssl/dsa.h>
10#include <openssl/ec.h>
11#include <openssl/ecdsa.h>
12#include <openssl/evp.h>
13#include <openssl/rsa.h>
14
15#include "base/memory/scoped_ptr.h"
16
17namespace crypto {
18
19// Simplistic helper that wraps a call to a deleter function. In a C++11 world,
20// this would be std::function<>. An alternative would be to re-use
21// base::internal::RunnableAdapter<>, but that's far too heavy weight.
22template <typename Type, void (*Destroyer)(Type*)>
23struct OpenSSLDestroyer {
24  void operator()(Type* ptr) const { Destroyer(ptr); }
25};
26
27template <typename PointerType, void (*Destroyer)(PointerType*)>
28struct ScopedOpenSSL {
29  typedef scoped_ptr<PointerType, OpenSSLDestroyer<PointerType, Destroyer> >
30      Type;
31};
32
33// Several typedefs are provided for crypto-specific primitives, for
34// short-hand and prevalence. Note that OpenSSL types related to X.509 are
35// intentionally not included, as crypto/ does not generally deal with
36// certificates or PKI.
37typedef ScopedOpenSSL<BIGNUM, BN_free>::Type ScopedBIGNUM;
38typedef ScopedOpenSSL<EC_KEY, EC_KEY_free>::Type ScopedEC_KEY;
39typedef ScopedOpenSSL<BIO, BIO_free_all>::Type ScopedBIO;
40typedef ScopedOpenSSL<DSA, DSA_free>::Type ScopedDSA;
41typedef ScopedOpenSSL<ECDSA_SIG, ECDSA_SIG_free>::Type ScopedECDSA_SIG;
42typedef ScopedOpenSSL<EC_KEY, EC_KEY_free>::Type ScopedEC_KEY;
43typedef ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy>::Type ScopedEVP_MD_CTX;
44typedef ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free>::Type ScopedEVP_PKEY;
45typedef ScopedOpenSSL<RSA, RSA_free>::Type ScopedRSA;
46
47}  // namespace crypto
48
49#endif  // CRYPTO_SCOPED_OPENSSL_TYPES_H_
50