71 lines
2.7 KiB
C++
71 lines
2.7 KiB
C++
////////////////////////////////////////////////////////////////////////////////
|
|
// Copyright 2016 Google Inc.
|
|
//
|
|
// This software is licensed under the terms defined in the Widevine Master
|
|
// License Agreement. For a copy of this agreement, please contact
|
|
// widevine-licensing@google.com.
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
#ifndef COMMON_OPENSSL_UTIL_H__
|
|
#define COMMON_OPENSSL_UTIL_H__
|
|
|
|
#include "openssl/bio.h"
|
|
#include "openssl/evp.h"
|
|
#include "openssl/rsa.h"
|
|
#include "openssl/x509v3.h"
|
|
|
|
template <typename T, void (*func)(T *)>
|
|
struct OpenSSLDeleter {
|
|
void operator()(T *obj) { func(obj); }
|
|
};
|
|
|
|
template <typename StackType, typename T, void (*func)(T *)>
|
|
struct OpenSSLStackDeleter {
|
|
void operator()(StackType *obj) {
|
|
sk_pop_free(reinterpret_cast<_STACK *>(obj),
|
|
reinterpret_cast<void (*)(void *)>(func));
|
|
}
|
|
};
|
|
|
|
template <typename StackType>
|
|
struct OpenSSLStackOnlyDeleter {
|
|
void operator()(StackType *obj) { sk_free(reinterpret_cast<_STACK *>(obj)); }
|
|
};
|
|
|
|
template <typename T, void (*func)(T *)>
|
|
using ScopedOpenSSLType = std::unique_ptr<T, OpenSSLDeleter<T, func>>;
|
|
template <typename StackType, typename T, void (*func)(T *)>
|
|
using ScopedOpenSSLStack =
|
|
std::unique_ptr<StackType, OpenSSLStackDeleter<StackType, T, func>>;
|
|
template <typename StackType>
|
|
using ScopedOpenSSLStackOnly =
|
|
std::unique_ptr<StackType, OpenSSLStackOnlyDeleter<StackType>>;
|
|
|
|
using ScopedBIO = ScopedOpenSSLType<BIO, BIO_vfree>;
|
|
using ScopedPKEY = ScopedOpenSSLType<EVP_PKEY, EVP_PKEY_free>;
|
|
using ScopedRSA = ScopedOpenSSLType<RSA, RSA_free>;
|
|
using ScopedX509 = ScopedOpenSSLType<X509, X509_free>;
|
|
using ScopedX509Extension =
|
|
ScopedOpenSSLType<X509_EXTENSION, X509_EXTENSION_free>;
|
|
using ScopedX509Name = ScopedOpenSSLType<X509_NAME, X509_NAME_free>;
|
|
using ScopedX509NameEntry =
|
|
ScopedOpenSSLType<X509_NAME_ENTRY, X509_NAME_ENTRY_free>;
|
|
using ScopedX509Store = ScopedOpenSSLType<X509_STORE, X509_STORE_free>;
|
|
using ScopedX509StoreCtx =
|
|
ScopedOpenSSLType<X509_STORE_CTX, X509_STORE_CTX_free>;
|
|
using ScopedAsn1UtcTime = ScopedOpenSSLType<ASN1_UTCTIME, ASN1_UTCTIME_free>;
|
|
using ScopedAsn1Utc8String =
|
|
ScopedOpenSSLType<ASN1_UTF8STRING, ASN1_UTF8STRING_free>;
|
|
using ScopedAsn1Integer = ScopedOpenSSLType<ASN1_INTEGER, ASN1_INTEGER_free>;
|
|
|
|
// XxxStack deallocates the stack and its members while XxxStackOnly deallocates
|
|
// the stack only.
|
|
using ScopedX509Stack = ScopedOpenSSLStack<STACK_OF(X509), X509, X509_free>;
|
|
using ScopedX509StackOnly = ScopedOpenSSLStackOnly<STACK_OF(X509)>;
|
|
using ScopedX509InfoStack =
|
|
ScopedOpenSSLStack<STACK_OF(X509_INFO), X509_INFO, X509_INFO_free>;
|
|
using ScopedX509InfoStackOnly = ScopedOpenSSLStackOnly<STACK_OF(X509_INFO)>;
|
|
|
|
#endif // COMMON_OPENSSL_UTIL_H__
|