u
This commit is contained in:
@@ -3,8 +3,13 @@
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <cctype>
|
||||
|
||||
#include <uv.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/x509.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/bn.h>
|
||||
|
||||
#include "config.hpp"
|
||||
#include "log.hpp"
|
||||
@@ -29,12 +34,60 @@ static Router router;
|
||||
static PairingStore pairing_store;
|
||||
static RequestLog request_log;
|
||||
|
||||
static json build_config() {
|
||||
static bool read_cert_metadata(const char* cert_path,
|
||||
std::string& fingerprint,
|
||||
std::string& serial,
|
||||
std::string& server_fingerprint) {
|
||||
FILE* fp = std::fopen(cert_path, "r");
|
||||
if (!fp) return false;
|
||||
X509* cert = PEM_read_X509(fp, nullptr, nullptr, nullptr);
|
||||
std::fclose(fp);
|
||||
if (!cert) return false;
|
||||
|
||||
unsigned char md[EVP_MAX_MD_SIZE];
|
||||
unsigned int md_len = 0;
|
||||
if (X509_digest(cert, EVP_sha1(), md, &md_len) != 1) {
|
||||
X509_free(cert);
|
||||
return false;
|
||||
}
|
||||
|
||||
static const char* hex = "0123456789ABCDEF";
|
||||
fingerprint.clear();
|
||||
fingerprint.reserve(md_len * 2);
|
||||
server_fingerprint.clear();
|
||||
server_fingerprint.reserve(md_len * 3 - 1);
|
||||
for (unsigned int i = 0; i < md_len; ++i) {
|
||||
fingerprint.push_back(hex[(md[i] >> 4) & 0xF]);
|
||||
fingerprint.push_back(hex[md[i] & 0xF]);
|
||||
if (i) server_fingerprint.push_back('-');
|
||||
server_fingerprint.push_back(hex[(md[i] >> 4) & 0xF]);
|
||||
server_fingerprint.push_back(hex[md[i] & 0xF]);
|
||||
}
|
||||
|
||||
const ASN1_INTEGER* asn1_serial = X509_get0_serialNumber(cert);
|
||||
BIGNUM* bn = ASN1_INTEGER_to_BN(asn1_serial, nullptr);
|
||||
char* hex_serial = bn ? BN_bn2hex(bn) : nullptr;
|
||||
bool ok = hex_serial != nullptr;
|
||||
if (ok) {
|
||||
serial = hex_serial;
|
||||
for (char& c : serial) c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
|
||||
OPENSSL_free(hex_serial);
|
||||
}
|
||||
BN_free(bn);
|
||||
X509_free(cert);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static json build_config(const char* cert_path) {
|
||||
std::string fingerprint, serial, server_fingerprint;
|
||||
if (!read_cert_metadata(cert_path, fingerprint, serial, server_fingerprint)) {
|
||||
logc::error("failed to read certificate metadata from %s", cert_path);
|
||||
}
|
||||
return {
|
||||
{"authToken", config::get("AUTH_TOKEN", "df40ad2067954646abb0499548a52241")},
|
||||
{"certificateFingerprint", config::get("CERTIFICATE_FINGERPRINT", "BC2114CF407A42724BEEF417960F76DCBF9DE879")},
|
||||
{"certificateSerialNumber", config::get("CERTIFICATE_SERIAL_NUMBER", "00BFC8BEACDB981B165210EF111CB9D3")},
|
||||
{"serverFingerprint", config::get("SERVER_FINGERPRINT", "39-6D-BD-DE-F3-5C-5A-EA-C2-19-CF-EB-A7-A9-58-2F-20-3F-20-F7-3D-E6-CA-8E-AE-FD-28-30-37-A6-45-AE")},
|
||||
{"certificateFingerprint", fingerprint},
|
||||
{"certificateSerialNumber", serial},
|
||||
{"serverFingerprint", server_fingerprint},
|
||||
{"mandantId", config::get("MANDANT_ID", "1")},
|
||||
{"mandantName", config::get("MANDANT_NAME", "eB-Standard")},
|
||||
{"mandantDatabase", config::get("MANDANT_DATABASE", "eazybusiness")},
|
||||
@@ -125,7 +178,7 @@ int main(int /*argc*/, char* argv[]) {
|
||||
|
||||
loop = uv_default_loop();
|
||||
|
||||
server_config = build_config();
|
||||
server_config = build_config(cert_path.c_str());
|
||||
|
||||
// Register routes
|
||||
router.add_route("GET", "/v1/client", handle_client);
|
||||
|
||||
Reference in New Issue
Block a user