OpenTTD Source 20241224-master-gf74b0cf984
signature.cpp File Reference

Implementation of signature validation routines. More...

#include "stdafx.h"
#include "signature.h"
#include "debug.h"
#include "fileio_func.h"
#include "string_func.h"
#include "3rdparty/monocypher/monocypher.h"
#include "3rdparty/monocypher/monocypher-ed25519.h"
#include "3rdparty/nlohmann/json.hpp"
#include "safeguards.h"

Go to the source code of this file.

Functions

static std::string CalculateHashV1 (const std::string &filename)
 Calculate the 32-byte blake2b hash of a file.
 
static bool ValidateChecksum (const std::string &filename, const std::string &checksum)
 Validate whether the checksum of a file is the same.
 
static bool ValidateSignature (const std::string &signature, const nlohmann::json &files, const std::string &filename)
 Validate whether the signature is valid for this set of files.
 
static bool ValidateSchema (const nlohmann::json &signatures, const std::string &filename)
 Validate the signatures file complies with the JSON schema.
 
static bool _ValidateSignatureFile (const std::string &filename)
 Validate that the signatures mentioned in the signature file are matching the files in question.
 
bool ValidateSignatureFile (const std::string &filename)
 Validate that the signatures mentioned in the signature file are matching the files in question.
 

Variables

static const std::initializer_list< std::array< uint8_t, 32 > > _public_keys_v1
 The public keys used for signature validation.
 

Detailed Description

Implementation of signature validation routines.

Definition in file signature.cpp.

Function Documentation

◆ _ValidateSignatureFile()

static bool _ValidateSignatureFile ( const std::string &  filename)
static

Validate that the signatures mentioned in the signature file are matching the files in question.

Returns
True iff the files in the signature file passed validation.

Definition at line 194 of file signature.cpp.

References Debug, FioFOpenFile(), FS2OTTD(), NO_DIRECTORY, OTTD2FS(), ValidateChecksum(), ValidateSchema(), and ValidateSignature().

Referenced by ValidateSignatureFile().

◆ CalculateHashV1()

static std::string CalculateHashV1 ( const std::string &  filename)
static

Calculate the 32-byte blake2b hash of a file.

Parameters
filenameThe filename to calculate the hash of.
Returns
The 32-byte blake2b hash of the file, hex-encoded.

Definition at line 36 of file signature.cpp.

References FioFOpenFile(), FormatArrayAsHex(), and NO_DIRECTORY.

Referenced by ValidateChecksum().

◆ ValidateChecksum()

static bool ValidateChecksum ( const std::string &  filename,
const std::string &  checksum 
)
static

Validate whether the checksum of a file is the same.

Parameters
filenameThe filename to validate the checksum of.
checksumThe expected checksum.
Returns
True iff the checksum of the file is the same as the expected checksum.

Definition at line 63 of file signature.cpp.

References CalculateHashV1(), and Debug.

Referenced by _ValidateSignatureFile().

◆ ValidateSchema()

static bool ValidateSchema ( const nlohmann::json &  signatures,
const std::string &  filename 
)
static

Validate the signatures file complies with the JSON schema.

Parameters
signaturesThe signatures JSON to validate.
filenameThe filename of the signatures file (for error-reporting).
Returns
True iff the signatures file complies with the JSON schema.

Definition at line 145 of file signature.cpp.

References Debug.

Referenced by _ValidateSignatureFile().

◆ ValidateSignature()

static bool ValidateSignature ( const std::string &  signature,
const nlohmann::json &  files,
const std::string &  filename 
)
static

Validate whether the signature is valid for this set of files.

Parameters
signatureThe signature to validate.
filesThe files to validate the signature against.
filenameThe filename of the signatures file (for error-reporting).
Returns
True iff the signature is valid for this set of files.

Definition at line 101 of file signature.cpp.

References _public_keys_v1, ConvertHexToBytes(), and Debug.

Referenced by _ValidateSignatureFile().

◆ ValidateSignatureFile()

bool ValidateSignatureFile ( const std::string &  filename)

Validate that the signatures mentioned in the signature file are matching the files in question.

Note
if ALLOW_INVALID_SIGNATURE is defined, this function will always return true (but will still report any errors in the console).
Returns
True iff the files in the signature file passed validation.

Definition at line 267 of file signature.cpp.

References _ValidateSignatureFile().

Variable Documentation

◆ _public_keys_v1

const std::initializer_list<std::array<uint8_t, 32> > _public_keys_v1
static
Initial value:
= {
{ 0xed, 0x5d, 0x57, 0x47, 0x21, 0x99, 0x8b, 0x02, 0xdf, 0x6e, 0x3d, 0x69, 0xe1, 0x87, 0xca, 0xd0, 0x0e, 0x88, 0xc3, 0xe2, 0xb2, 0xa6, 0x7b, 0xc0, 0x42, 0xc8, 0xd6, 0x4b, 0x65, 0xe6, 0x48, 0xf7 },
}

The public keys used for signature validation.

Definition at line 25 of file signature.cpp.

Referenced by ValidateSignature().