Skip to content

Commit

Permalink
Fix compilation with newer openssl
Browse files Browse the repository at this point in the history
  • Loading branch information
tchaloupka committed Jan 10, 2025
1 parent 92f33e5 commit b9a6b67
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/openssl/source/jwtlited/openssl.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public import jwtlited;
version (assert) import core.stdc.stdio;

import deimos.openssl.ec;
import deimos.openssl.ecdsa;
import deimos.openssl.err;
import deimos.openssl.evp;
import deimos.openssl.hmac;
Expand Down Expand Up @@ -38,15 +39,15 @@ private struct HMACImpl(JWTAlgorithm implAlg)
private
{
const(char)[] key;
HMAC_CTX ctx;
HMAC_CTX* ctx;
ubyte[signLen] sigBuf;
}

@disable this(this);

~this() @trusted
{
HMAC_CTX_reset(&ctx);
if (ctx) HMAC_CTX_free(ctx);
}

bool loadKey(K)(K key) if (isToken!K)
Expand All @@ -60,8 +61,13 @@ private struct HMACImpl(JWTAlgorithm implAlg)
else static if (implAlg == JWTAlgorithm.HS384) alias evp = EVP_sha384;
else static if (implAlg == JWTAlgorithm.HS512) alias evp = EVP_sha512;

HMAC_CTX_reset(&ctx);
return HMAC_Init_ex(&ctx, this.key.ptr, cast(int)key.length, evp(), null);
if (ctx is null)
{
ctx = HMAC_CTX_new();
if (ctx is null) onOutOfMemoryError;
}
else HMAC_CTX_reset(ctx);
return HMAC_Init_ex(ctx, this.key.ptr, cast(int)key.length, evp(), null);
}();
if (!ret) return false;
return true;
Expand Down Expand Up @@ -90,13 +96,13 @@ private struct HMACImpl(JWTAlgorithm implAlg)
assert(key.length, "Secret key not set");
if (!key.length || !value.length) return false;

scope (exit) HMAC_Init_ex(&ctx, null, 0, null, null);
scope (exit) HMAC_Init_ex(ctx, null, 0, null, null);

auto ret = HMAC_Update(&ctx, cast(const(ubyte)*)value.ptr, cast(ulong)value.length);
auto ret = HMAC_Update(ctx, cast(const(ubyte)*)value.ptr, cast(ulong)value.length);
if (!ret) return false;

uint slen;
ret = HMAC_Final(&ctx, sigBuf.ptr, &slen);
ret = HMAC_Final(ctx, sigBuf.ptr, &slen);
assert(slen == signLen);
if (!ret) return false;
return true;
Expand Down

0 comments on commit b9a6b67

Please sign in to comment.