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.
5 #ifndef CRYPTO_SCOPED_OPENSSL_TYPES_H_
6 #define CRYPTO_SCOPED_OPENSSL_TYPES_H_
8 #include <openssl/bio.h>
9 #include <openssl/bn.h>
10 #include <openssl/dsa.h>
11 #include <openssl/ec.h>
12 #include <openssl/ecdsa.h>
13 #include <openssl/evp.h>
14 #include <openssl/rsa.h>
16 #include "base/memory/scoped_ptr.h"
20 // Simplistic helper that wraps a call to a deleter function. In a C++11 world,
21 // this would be std::function<>. An alternative would be to re-use
22 // base::internal::RunnableAdapter<>, but that's far too heavy weight.
23 template <typename Type
, void (*Destroyer
)(Type
*)>
24 struct OpenSSLDestroyer
{
25 typedef void AllowSelfReset
;
26 void operator()(Type
* ptr
) const { Destroyer(ptr
); }
29 template <typename PointerType
, void (*Destroyer
)(PointerType
*)>
30 struct ScopedOpenSSL
{
31 typedef scoped_ptr
<PointerType
, OpenSSLDestroyer
<PointerType
, Destroyer
> >
36 void operator()(uint8_t* ptr
) const { OPENSSL_free(ptr
); }
39 // Several typedefs are provided for crypto-specific primitives, for
40 // short-hand and prevalence. Note that OpenSSL types related to X.509 are
41 // intentionally not included, as crypto/ does not generally deal with
42 // certificates or PKI.
43 typedef ScopedOpenSSL
<BIGNUM
, BN_free
>::Type ScopedBIGNUM
;
44 typedef ScopedOpenSSL
<EC_KEY
, EC_KEY_free
>::Type ScopedEC_KEY
;
45 typedef ScopedOpenSSL
<BIO
, BIO_free_all
>::Type ScopedBIO
;
46 typedef ScopedOpenSSL
<DSA
, DSA_free
>::Type ScopedDSA
;
47 typedef ScopedOpenSSL
<ECDSA_SIG
, ECDSA_SIG_free
>::Type ScopedECDSA_SIG
;
48 typedef ScopedOpenSSL
<EC_KEY
, EC_KEY_free
>::Type ScopedEC_KEY
;
49 typedef ScopedOpenSSL
<EVP_MD_CTX
, EVP_MD_CTX_destroy
>::Type ScopedEVP_MD_CTX
;
50 typedef ScopedOpenSSL
<EVP_PKEY
, EVP_PKEY_free
>::Type ScopedEVP_PKEY
;
51 typedef ScopedOpenSSL
<EVP_PKEY_CTX
, EVP_PKEY_CTX_free
>::Type ScopedEVP_PKEY_CTX
;
52 typedef ScopedOpenSSL
<RSA
, RSA_free
>::Type ScopedRSA
;
54 // The bytes must have been allocated with OPENSSL_malloc.
55 typedef scoped_ptr
<uint8_t, OpenSSLFree
> ScopedOpenSSLBytes
;
59 #endif // CRYPTO_SCOPED_OPENSSL_TYPES_H_