1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#ifndef CRYPTO_SCOPED_OPENSSL_TYPES_H_
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#define CRYPTO_SCOPED_OPENSSL_TYPES_H_
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include <openssl/bio.h>
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <openssl/bn.h>
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <openssl/dsa.h>
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <openssl/ec.h>
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <openssl/ecdsa.h>
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <openssl/evp.h>
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <openssl/rsa.h>
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/memory/scoped_ptr.h"
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace crypto {
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Simplistic helper that wraps a call to a deleter function. In a C++11 world,
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// this would be std::function<>. An alternative would be to re-use
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// base::internal::RunnableAdapter<>, but that's far too heavy weight.
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdochtemplate <typename Type, void (*Destroyer)(Type*)>
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstruct OpenSSLDestroyer {
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void operator()(Type* ptr) const { Destroyer(ptr); }
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch};
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdochtemplate <typename PointerType, void (*Destroyer)(PointerType*)>
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstruct ScopedOpenSSL {
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  typedef scoped_ptr<PointerType, OpenSSLDestroyer<PointerType, Destroyer> >
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      Type;
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch};
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)struct OpenSSLFree {
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void operator()(uint8_t* ptr) const { OPENSSL_free(ptr); }
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)};
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Several typedefs are provided for crypto-specific primitives, for
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// short-hand and prevalence. Note that OpenSSL types related to X.509 are
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// intentionally not included, as crypto/ does not generally deal with
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// certificates or PKI.
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdochtypedef ScopedOpenSSL<BIGNUM, BN_free>::Type ScopedBIGNUM;
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdochtypedef ScopedOpenSSL<EC_KEY, EC_KEY_free>::Type ScopedEC_KEY;
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdochtypedef ScopedOpenSSL<BIO, BIO_free_all>::Type ScopedBIO;
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdochtypedef ScopedOpenSSL<DSA, DSA_free>::Type ScopedDSA;
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdochtypedef ScopedOpenSSL<ECDSA_SIG, ECDSA_SIG_free>::Type ScopedECDSA_SIG;
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdochtypedef ScopedOpenSSL<EC_KEY, EC_KEY_free>::Type ScopedEC_KEY;
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdochtypedef ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy>::Type ScopedEVP_MD_CTX;
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdochtypedef ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free>::Type ScopedEVP_PKEY;
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)typedef ScopedOpenSSL<EVP_PKEY_CTX, EVP_PKEY_CTX_free>::Type ScopedEVP_PKEY_CTX;
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdochtypedef ScopedOpenSSL<RSA, RSA_free>::Type ScopedRSA;
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// The bytes must have been allocated with OPENSSL_malloc.
545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)typedef scoped_ptr<uint8_t, OpenSSLFree> ScopedOpenSSLBytes;
555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace crypto
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif  // CRYPTO_SCOPED_OPENSSL_TYPES_H_
59