diff options
Diffstat (limited to 'src/common/hmac_openssl.c')
-rw-r--r-- | src/common/hmac_openssl.c | 47 |
1 files changed, 43 insertions, 4 deletions
diff --git a/src/common/hmac_openssl.c b/src/common/hmac_openssl.c index 9164f4fdfee..b12e49ba66c 100644 --- a/src/common/hmac_openssl.c +++ b/src/common/hmac_openssl.c @@ -31,7 +31,6 @@ #ifndef FRONTEND #include "utils/memutils.h" #include "utils/resowner.h" -#include "utils/resowner_private.h" #endif /* @@ -73,6 +72,32 @@ struct pg_hmac_ctx #endif }; +/* ResourceOwner callbacks to hold HMAC contexts */ +#ifndef FRONTEND +static void ResOwnerReleaseHMAC(Datum res); + +static const ResourceOwnerDesc hmac_resowner_desc = +{ + .name = "OpenSSL HMAC context", + .release_phase = RESOURCE_RELEASE_BEFORE_LOCKS, + .release_priority = RELEASE_PRIO_HMAC_CONTEXTS, + .ReleaseResource = ResOwnerReleaseHMAC, + .DebugPrint = NULL /* the default message is fine */ +}; + +/* Convenience wrappers over ResourceOwnerRemember/Forget */ +static inline void +ResourceOwnerRememberHMAC(ResourceOwner owner, pg_hmac_ctx *ctx) +{ + ResourceOwnerRemember(owner, PointerGetDatum(ctx), &hmac_resowner_desc); +} +static inline void +ResourceOwnerForgetHMAC(ResourceOwner owner, pg_hmac_ctx *ctx) +{ + ResourceOwnerForget(owner, PointerGetDatum(ctx), &hmac_resowner_desc); +} +#endif + static const char * SSLerrmessage(unsigned long ecode) { @@ -115,7 +140,7 @@ pg_hmac_create(pg_cryptohash_type type) ERR_clear_error(); #ifdef HAVE_HMAC_CTX_NEW #ifndef FRONTEND - ResourceOwnerEnlargeHMAC(CurrentResourceOwner); + ResourceOwnerEnlarge(CurrentResourceOwner); #endif ctx->hmacctx = HMAC_CTX_new(); #else @@ -137,7 +162,7 @@ pg_hmac_create(pg_cryptohash_type type) #ifdef HAVE_HMAC_CTX_NEW #ifndef FRONTEND ctx->resowner = CurrentResourceOwner; - ResourceOwnerRememberHMAC(CurrentResourceOwner, PointerGetDatum(ctx)); + ResourceOwnerRememberHMAC(CurrentResourceOwner, ctx); #endif #else memset(ctx->hmacctx, 0, sizeof(HMAC_CTX)); @@ -303,7 +328,8 @@ pg_hmac_free(pg_hmac_ctx *ctx) #ifdef HAVE_HMAC_CTX_FREE HMAC_CTX_free(ctx->hmacctx); #ifndef FRONTEND - ResourceOwnerForgetHMAC(ctx->resowner, PointerGetDatum(ctx)); + if (ctx->resowner) + ResourceOwnerForgetHMAC(ctx->resowner, ctx); #endif #else explicit_bzero(ctx->hmacctx, sizeof(HMAC_CTX)); @@ -346,3 +372,16 @@ pg_hmac_error(pg_hmac_ctx *ctx) Assert(false); /* cannot be reached */ return _("success"); } + +/* ResourceOwner callbacks */ + +#ifndef FRONTEND +static void +ResOwnerReleaseHMAC(Datum res) +{ + pg_hmac_ctx *ctx = (pg_hmac_ctx *) DatumGetPointer(res); + + ctx->resowner = NULL; + pg_hmac_free(ctx); +} +#endif |