Using OpenSSL in UE4
A few days ago I’ve found myself integrating Kraken’s (Crypto Exchange Service) REST API into UE4.
It’s all pretty straightforward if we ignore the fact they don’t provide a sandbox environment and some of the calls are lacking the response’s format (that is, the actual fields and their type).
Submitting requests and parsing their response is easy, UE4 comes with everything you might need:
An HTTP client served with delicious smart pointers and integrated in the game thread. The only annoyingness required is to bring your own “request content” builder. That is, there’s no helper to set your POST or GET data, you’ll have to do it yourself with some help from FGenericPlatformHttp::UrlEncode.
You are not done yet. Apart from the pain that comes with something that you cannot automate (making CPP classes out of an unformatted documentation), the biggest pain in the butt came directly from the signature requirement from Kraken. They say the want those headers in their private requests:
API-Key = API key
API-Sign = Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key
Oof.
Now on top of making sure your manually built POST data is well formatted and supports all different types of data that You have to deal with, so that you don’t break the signature that is dependant on that, guess how many of those hashing functions UE4 gives to you? You’re right, and our friends at Epic are right as well, why bloat the engine furthermore for something that basically no one needs.
Now HMAC and SHA is something that OpenSSL surely offers but i remember the last time I had dealt with this (don’t recall the reason tho) and sure as hell i did not want to repeat that, did I mention this thing had to be cross-platform as well? While messing around in Visual Assist’s symbol search I realised some of OpenSSL symbols would pop up (and rightfully so, I imagine it has its uses in the low level networking or in the HTTP module itself) so, crossed my fingers and hoped to find it as a module.
Turns out we have a module called (you guessed it) “OpenSSL” that comes with libraries for any platform you can dream of, you just add it to the dependencies list in your build file:
PrivateDependencyModuleNames.AddRange(new string[]
{
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
"HTTP",
"Json",
"OpenSSL",
"JsonUtilities",
});
And you are done… Or you’d think so.
There’s one last thing you should sort out first:
When you finally include OpenSSL’s header you’ll find that some macros are being redefined, funny enough this Third-Party integration guide from Epic uses openssl specifically as an example and shows that you can get away with wrapping that include like this:
THIRD_PARTY_INCLUDES_START
#include <openssl.h>
THIRD_PARTY_INCLUDES_END
And that’s it really. The rest is standard OpenSSL and C++, you can be nerd enough to know how to get stuff done in openssl by yourself or google your way out. Have fun with it!
Filed under: Uncategorized - @ March 18, 2021 4:26 pm