blob: 622fed298ffb1088911dd6f01dcaa90100e81f87 [file] [log] [blame]
Luis Hector Chaveze5b2c6f2017-07-26 17:33:47 +00001// 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/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#ifdef OPENSSL_IS_BORINGSSL
15#include <openssl/mem.h>
16#endif
17#include <openssl/rsa.h>
18#include <stdint.h>
19
20#include <memory>
21
22namespace crypto {
23
24// Simplistic helper that wraps a call to a deleter function. In a C++11 world,
25// this would be std::function<>. An alternative would be to re-use
26// base::internal::RunnableAdapter<>, but that's far too heavy weight.
27template <typename Type, void (*Destroyer)(Type*)>
28struct OpenSSLDestroyer {
29 void operator()(Type* ptr) const { Destroyer(ptr); }
30};
31
32template <typename PointerType, void (*Destroyer)(PointerType*)>
33using ScopedOpenSSL =
34 std::unique_ptr<PointerType, OpenSSLDestroyer<PointerType, Destroyer>>;
35
36struct OpenSSLFree {
37 void operator()(uint8_t* ptr) const { OPENSSL_free(ptr); }
38};
39
40// Several typedefs are provided for crypto-specific primitives, for
41// short-hand and prevalence. Note that OpenSSL types related to X.509 are
42// intentionally not included, as crypto/ does not generally deal with
43// certificates or PKI.
44using ScopedBIGNUM = ScopedOpenSSL<BIGNUM, BN_free>;
45using ScopedEC_Key = ScopedOpenSSL<EC_KEY, EC_KEY_free>;
46using ScopedBIO = ScopedOpenSSL<BIO, BIO_free_all>;
47using ScopedDSA = ScopedOpenSSL<DSA, DSA_free>;
48using ScopedECDSA_SIG = ScopedOpenSSL<ECDSA_SIG, ECDSA_SIG_free>;
49using ScopedEC_GROUP = ScopedOpenSSL<EC_GROUP, EC_GROUP_free>;
50using ScopedEC_KEY = ScopedOpenSSL<EC_KEY, EC_KEY_free>;
51using ScopedEC_POINT = ScopedOpenSSL<EC_POINT, EC_POINT_free>;
52using ScopedEVP_MD_CTX = ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy>;
53using ScopedEVP_PKEY = ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free>;
54using ScopedEVP_PKEY_CTX = ScopedOpenSSL<EVP_PKEY_CTX, EVP_PKEY_CTX_free>;
55using ScopedRSA = ScopedOpenSSL<RSA, RSA_free>;
56
57// The bytes must have been allocated with OPENSSL_malloc.
58using ScopedOpenSSLBytes = std::unique_ptr<uint8_t, OpenSSLFree>;
59
60} // namespace crypto
61
62#endif // CRYPTO_SCOPED_OPENSSL_TYPES_H_