Add reusable certificate and file validation helpers
Fix process-name ownership and module lookup outputs, add explicit SPKI hashing and bounded chain access, and improve WDM integration. Deprecate superseded entry points with migration guidance and align branch code and tests with the library style. Add CI validation helpers with explicit signature selection and owned results Document runtime-confirmed CI signer selection and crypto reuse Expand CI compatibility matrix to twenty verified samples Extend CI export matrix with Windows 11 x64 and ARM64 samples Document CI API availability and historical mincrypt layouts Add explicit Authenticode workflows and correct CI helper contracts Document CI validation flag 0x20 and add kernel probe Harden CI digest parsing and failure cleanup
This commit is contained in:
1 parent
7985b23be5
commit
3fbfe31d15
38 files changed
+9201
-751
No files matched your search
@@ -0,0 +1,396 @@
|
||||
#include "Domito.Internal.h"
|
||||
|
||||
typedef struct _DOMITO_CERTIFICATE_DER_VIEW
|
||||
{
|
||||
const UCHAR* Data;
|
||||
ULONG Size;
|
||||
} DOMITO_CERTIFICATE_DER_VIEW;
|
||||
|
||||
static BOOLEAN
|
||||
DomitoCertificateDerTake(
|
||||
DOMITO_CERTIFICATE_DER_VIEW* Input,
|
||||
UCHAR Tag,
|
||||
DOMITO_CERTIFICATE_DER_VIEW* Content,
|
||||
DOMITO_CERTIFICATE_DER_VIEW* Encoded = nullptr
|
||||
)
|
||||
{
|
||||
if (Input->Size < 2 || Input->Data[0] != Tag)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ULONG length = Input->Data[1];
|
||||
ULONG header = 2;
|
||||
|
||||
if (length & 0x80)
|
||||
{
|
||||
const ULONG count = length & 0x7f;
|
||||
|
||||
if (!count || count > 4 || count > Input->Size - 2 || !Input->Data[2])
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
length = 0;
|
||||
|
||||
for (ULONG i = 0; i < count; ++i)
|
||||
{
|
||||
length = (length << 8) | Input->Data[header++];
|
||||
}
|
||||
|
||||
if (length < 128)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
if (length > Input->Size - header)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
*Content = {Input->Data + header, length};
|
||||
|
||||
if (Encoded)
|
||||
{
|
||||
*Encoded = {Input->Data, header + length};
|
||||
}
|
||||
|
||||
Input->Data += header + length;
|
||||
Input->Size -= header + length;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
_Use_decl_annotations_
|
||||
NTSTATUS
|
||||
DomitoGetCertificateSpki(
|
||||
const UCHAR* CertData,
|
||||
ULONG CertSize,
|
||||
DOMITO_CERTIFICATE_SPKI* Spki
|
||||
)
|
||||
{
|
||||
PAGED_CODE();
|
||||
|
||||
if (!Spki)
|
||||
{
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
*Spki = {};
|
||||
|
||||
if (!CertData || !CertSize)
|
||||
{
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
DOMITO_CERTIFICATE_DER_VIEW certificateInput = {CertData, CertSize};
|
||||
DOMITO_CERTIFICATE_DER_VIEW certificate;
|
||||
DOMITO_CERTIFICATE_DER_VIEW tbsCertificate;
|
||||
DOMITO_CERTIFICATE_DER_VIEW fieldContent;
|
||||
DOMITO_CERTIFICATE_DER_VIEW encodedSpki;
|
||||
DOMITO_CERTIFICATE_DER_VIEW spkiContent;
|
||||
// Validate complete certificate framing, without interpreting trust/algorithms.
|
||||
if (!DomitoCertificateDerTake(&certificateInput, 0x30, &certificate) || certificateInput.Size ||
|
||||
!DomitoCertificateDerTake(&certificate, 0x30, &tbsCertificate) ||
|
||||
!DomitoCertificateDerTake(&certificate, 0x30, &fieldContent) ||
|
||||
!DomitoCertificateDerTake(&certificate, 0x03, &fieldContent) || !fieldContent.Size ||
|
||||
fieldContent.Data[0] > 7 || certificate.Size)
|
||||
{
|
||||
return STATUS_INVALID_IMAGE_FORMAT;
|
||||
}
|
||||
|
||||
if (tbsCertificate.Size && tbsCertificate.Data[0] == 0xa0)
|
||||
{
|
||||
DOMITO_CERTIFICATE_DER_VIEW certificateVersion;
|
||||
|
||||
if (!DomitoCertificateDerTake(&tbsCertificate, 0xa0, &fieldContent) ||
|
||||
!DomitoCertificateDerTake(&fieldContent, 0x02, &certificateVersion) || fieldContent.Size ||
|
||||
certificateVersion.Size != 1 || certificateVersion.Data[0] > 2)
|
||||
{
|
||||
return STATUS_INVALID_IMAGE_FORMAT;
|
||||
}
|
||||
}
|
||||
|
||||
const UCHAR tbsFieldTags[] = {0x02, 0x30, 0x30, 0x30, 0x30};
|
||||
|
||||
for (ULONG i = 0; i < RTL_NUMBER_OF(tbsFieldTags); ++i)
|
||||
{
|
||||
if (!DomitoCertificateDerTake(&tbsCertificate, tbsFieldTags[i], &fieldContent) ||
|
||||
(i == 0 && !fieldContent.Size))
|
||||
{
|
||||
return STATUS_INVALID_IMAGE_FORMAT;
|
||||
}
|
||||
}
|
||||
|
||||
if (!DomitoCertificateDerTake(&tbsCertificate, 0x30, &spkiContent, &encodedSpki))
|
||||
{
|
||||
return STATUS_INVALID_IMAGE_FORMAT;
|
||||
}
|
||||
|
||||
DOMITO_CERTIFICATE_DER_VIEW spkiFields = spkiContent;
|
||||
DOMITO_CERTIFICATE_DER_VIEW keyAlgorithm;
|
||||
DOMITO_CERTIFICATE_DER_VIEW algorithmOid;
|
||||
DOMITO_CERTIFICATE_DER_VIEW publicKeyBits;
|
||||
|
||||
if (!DomitoCertificateDerTake(&spkiFields, 0x30, &keyAlgorithm) ||
|
||||
!DomitoCertificateDerTake(&keyAlgorithm, 0x06, &algorithmOid) || !algorithmOid.Size ||
|
||||
!DomitoCertificateDerTake(&spkiFields, 0x03, &publicKeyBits) || publicKeyBits.Size < 2 ||
|
||||
publicKeyBits.Data[0] > 7 || spkiFields.Size)
|
||||
{
|
||||
return STATUS_INVALID_IMAGE_FORMAT;
|
||||
}
|
||||
|
||||
Spki->Encoded = {encodedSpki.Data, encodedSpki.Size};
|
||||
Spki->Content = {spkiContent.Data, spkiContent.Size};
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
_Use_decl_annotations_
|
||||
NTSTATUS
|
||||
DomitoHashBuffer(
|
||||
const UCHAR* Bytes,
|
||||
ULONG Size,
|
||||
ALG_ID Algorithm,
|
||||
UCHAR* Hash,
|
||||
ULONG* HashSize
|
||||
)
|
||||
{
|
||||
PAGED_CODE();
|
||||
|
||||
if (!HashSize)
|
||||
{
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
const ULONG capacity = *HashSize;
|
||||
ULONG required;
|
||||
|
||||
switch (Algorithm)
|
||||
{
|
||||
case CALG_SHA1:
|
||||
required = 20;
|
||||
break;
|
||||
case CALG_SHA_256:
|
||||
required = 32;
|
||||
break;
|
||||
case CALG_SHA_384:
|
||||
required = 48;
|
||||
break;
|
||||
case CALG_SHA_512:
|
||||
required = 64;
|
||||
break;
|
||||
default:
|
||||
*HashSize = 0;
|
||||
return STATUS_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
if (!Bytes && Size)
|
||||
{
|
||||
*HashSize = 0;
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
if (!Hash || capacity < required)
|
||||
{
|
||||
*HashSize = required;
|
||||
return STATUS_BUFFER_TOO_SMALL;
|
||||
}
|
||||
|
||||
RtlZeroMemory(Hash, required);
|
||||
BCRYPT_ALG_HANDLE algorithm = nullptr;
|
||||
BCRYPT_HASH_HANDLE hash = nullptr;
|
||||
NTSTATUS status =
|
||||
BCryptOpenAlgorithmProvider(&algorithm, DOMITO_CALG_TO_BCRYPT_ALGORITHM(Algorithm), MS_PRIMITIVE_PROVIDER, 0);
|
||||
|
||||
if (NT_SUCCESS(status))
|
||||
{
|
||||
status = BCryptCreateHash(algorithm, &hash, nullptr, 0, nullptr, 0, 0);
|
||||
}
|
||||
|
||||
if (NT_SUCCESS(status) && Size)
|
||||
{
|
||||
status = BCryptHashData(hash, const_cast<PUCHAR>(Bytes), Size, 0);
|
||||
}
|
||||
|
||||
if (NT_SUCCESS(status))
|
||||
{
|
||||
status = BCryptFinishHash(hash, Hash, required, 0);
|
||||
}
|
||||
|
||||
if (hash)
|
||||
{
|
||||
BCryptDestroyHash(hash);
|
||||
}
|
||||
|
||||
if (algorithm)
|
||||
{
|
||||
BCryptCloseAlgorithmProvider(algorithm, 0);
|
||||
}
|
||||
|
||||
if (NT_SUCCESS(status))
|
||||
{
|
||||
*HashSize = required;
|
||||
}
|
||||
else
|
||||
{
|
||||
RtlZeroMemory(Hash, required);
|
||||
*HashSize = 0;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
_Use_decl_annotations_
|
||||
NTSTATUS
|
||||
DomitoHashCertificate(
|
||||
const UCHAR* CertData,
|
||||
ULONG CertSize,
|
||||
DOMITO_CERTIFICATE_HASH_KIND Kind,
|
||||
ALG_ID Algorithm,
|
||||
UCHAR* Hash,
|
||||
ULONG* HashSize
|
||||
)
|
||||
{
|
||||
PAGED_CODE();
|
||||
|
||||
if (!HashSize)
|
||||
{
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
if (!CertData || !CertSize)
|
||||
{
|
||||
*HashSize = 0;
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
DOMITO_CERTIFICATE_SPKI spki = {};
|
||||
|
||||
if (Kind == DomitoCertificateHashSpkiDer || Kind == DomitoCertificateHashSpkiContentLegacy)
|
||||
{
|
||||
NTSTATUS status = DomitoGetCertificateSpki(CertData, CertSize, &spki);
|
||||
|
||||
if (!NT_SUCCESS(status))
|
||||
{
|
||||
*HashSize = 0;
|
||||
return status;
|
||||
}
|
||||
|
||||
const DOMITO_BYTE_VIEW selected = Kind == DomitoCertificateHashSpkiDer ? spki.Encoded : spki.Content;
|
||||
return DomitoHashBuffer(selected.Data, selected.Size, Algorithm, Hash, HashSize);
|
||||
}
|
||||
|
||||
if (Kind != DomitoCertificateHashDer)
|
||||
{
|
||||
*HashSize = 0;
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
// Whole-certificate fingerprint: hashes exact input bytes, no trust claim.
|
||||
return DomitoHashBuffer(CertData, CertSize, Algorithm, Hash, HashSize);
|
||||
}
|
||||
|
||||
static int
|
||||
DomitoHexDigit(
|
||||
WCHAR Value
|
||||
)
|
||||
{
|
||||
if (Value >= L'0' && Value <= L'9')
|
||||
{
|
||||
return Value - L'0';
|
||||
}
|
||||
|
||||
if (Value >= L'A' && Value <= L'F')
|
||||
{
|
||||
return Value - L'A' + 10;
|
||||
}
|
||||
|
||||
if (Value >= L'a' && Value <= L'f')
|
||||
{
|
||||
return Value - L'a' + 10;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
_Use_decl_annotations_
|
||||
NTSTATUS
|
||||
DomitoDecodeHex(
|
||||
const UNICODE_STRING* Hex,
|
||||
UCHAR* Bytes,
|
||||
ULONG ByteCount
|
||||
)
|
||||
{
|
||||
PAGED_CODE();
|
||||
|
||||
if (!Bytes && ByteCount)
|
||||
{
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
if (Bytes && ByteCount)
|
||||
{
|
||||
RtlZeroMemory(Bytes, ByteCount);
|
||||
}
|
||||
|
||||
if (!Hex || Hex->Length > Hex->MaximumLength || (Hex->Length && !Hex->Buffer) ||
|
||||
ByteCount > MAXUSHORT / (2 * sizeof(WCHAR)) || Hex->Length != ByteCount * 2 * sizeof(WCHAR))
|
||||
{
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
for (ULONG i = 0; i < ByteCount * 2; ++i)
|
||||
{
|
||||
if (DomitoHexDigit(Hex->Buffer[i]) < 0)
|
||||
{
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
}
|
||||
|
||||
for (ULONG i = 0; i < ByteCount; ++i)
|
||||
{
|
||||
Bytes[i] =
|
||||
static_cast<UCHAR>((DomitoHexDigit(Hex->Buffer[2 * i]) << 4) | DomitoHexDigit(Hex->Buffer[2 * i + 1]));
|
||||
}
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
_Use_decl_annotations_
|
||||
NTSTATUS
|
||||
DomitoEncodeHex(
|
||||
const UCHAR* Bytes,
|
||||
ULONG ByteCount,
|
||||
WCHAR* Hex,
|
||||
ULONG HexChars
|
||||
)
|
||||
{
|
||||
PAGED_CODE();
|
||||
|
||||
if (Hex && HexChars)
|
||||
{
|
||||
Hex[0] = 0;
|
||||
}
|
||||
|
||||
if ((!Bytes && ByteCount) || !Hex)
|
||||
{
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
if (!HexChars || ByteCount > (HexChars - 1) / 2)
|
||||
{
|
||||
return STATUS_BUFFER_TOO_SMALL;
|
||||
}
|
||||
|
||||
static const WCHAR digits[] = L"0123456789ABCDEF";
|
||||
WCHAR* destination = Hex;
|
||||
|
||||
for (ULONG i = 0; i < ByteCount; ++i)
|
||||
{
|
||||
*destination++ = digits[Bytes[i] >> 4];
|
||||
*destination++ = digits[Bytes[i] & 15];
|
||||
}
|
||||
|
||||
*destination = 0;
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
Reference in new issue
Block a user