summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/common/efi/efisignaturedb.cpp
blob: b75445f0d3fcb3851d9ff0826234042db3ca3666 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/* $Id: efisignaturedb.cpp $ */
/** @file
 * IPRT - EFI signature database helpers.
 */

/*
 * Copyright (C) 2021-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP RTLOGGROUP_DEFAULT
#include <iprt/efi.h>

#include <iprt/cdefs.h>
#include <iprt/asm.h>
#include <iprt/string.h>
#include <iprt/list.h>
#include <iprt/mem.h>
#include <iprt/sg.h>

#include <iprt/formats/efi-signature.h>


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/

/**
 * EFI signature entry.
 */
typedef struct RTEFISIGNATURE
{
    /** List node. */
    RTLISTNODE              NdLst;
    /** The signature owner. */
    RTUUID                  UuidOwner;
    /** Size of the signature data in bytes. */
    uint32_t                cbSignature;
    /** The signature data (variable in size). */
    RT_FLEXIBLE_ARRAY_EXTENSION
    uint8_t                 abSignature[RT_FLEXIBLE_ARRAY];
} RTEFISIGNATURE;
/** Pointer to a EFI signature entry. */
typedef RTEFISIGNATURE *PRTEFISIGNATURE;
/** Pointer to a const EFI signature entry. */
typedef const RTEFISIGNATURE *PCRTEFISIGNATURE;


/**
 * The EFI signature database instance data.
 */
typedef struct RTEFISIGDBINT
{
    /** List head of the various signature types. */
    RTLISTANCHOR            aLstSigTypes[RTEFISIGTYPE_FIRST_INVALID];
} RTEFISIGDBINT;
/** Pointer to the EFI signature database instance data. */
typedef RTEFISIGDBINT *PRTEFISIGDBINT;


/**
 * Signature type descriptor.
 */
typedef struct RTEFISIGDBDESC
{
    /** The EFI GUID identifying the signature type. */
    EFI_GUID                GuidSignatureType;
    /** The additional signature header for this signature type. */
    uint32_t                cbSigHdr;
    /** Size of the signature data (including EFI_SIGNATURE_DATA),
     * can be 0 size varies with each signature (X.509 for example). */
    uint32_t                cbSig;
    /** The internal signature type enum. */
    RTEFISIGTYPE            enmSigType;
    /** Human readable string of the signature type. */
    const char              *pszName;
} RTEFISIGDBDESC;
/** Pointer to a signature type descriptor. */
typedef RTEFISIGDBDESC *PRTEFISIGDBDESC;
/** Pointer to a const signature type descriptor. */
typedef const RTEFISIGDBDESC *PCRTEFISIGDBDESC;


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/

/**
 * Mapping of EFI signature GUIDs to their IPRT signature type equivalent.
 */
static const RTEFISIGDBDESC g_aGuid2SigTypeMapping[] =
{
    { EFI_NULL_GUID,                          0,                                    0,  RTEFISIGTYPE_INVALID,        "INVALID"          },
    { EFI_SIGNATURE_TYPE_GUID_SHA256,         0, EFI_SIGNATURE_TYPE_SZ_SHA256,          RTEFISIGTYPE_SHA256,         "SHA-256"          },
    { EFI_SIGNATURE_TYPE_GUID_RSA2048,        0, EFI_SIGNATURE_TYPE_SZ_RSA2048,         RTEFISIGTYPE_RSA2048,        "RSA-2048"         },
    { EFI_SIGNATURE_TYPE_GUID_RSA2048_SHA256, 0, EFI_SIGNATURE_TYPE_SZ_RSA2048_SHA256,  RTEFISIGTYPE_RSA2048_SHA256, "RSA-2048/SHA-256" },
    { EFI_SIGNATURE_TYPE_GUID_SHA1,           0, EFI_SIGNATURE_TYPE_SZ_SHA1,            RTEFISIGTYPE_SHA1,           "SHA-1"            },
    { EFI_SIGNATURE_TYPE_GUID_RSA2048_SHA1,   0, EFI_SIGNATURE_TYPE_SZ_RSA2048_SHA1,    RTEFISIGTYPE_RSA2048_SHA1,   "RSA-2048/SHA-1"   },
    { EFI_SIGNATURE_TYPE_GUID_X509,           0,                                    0,  RTEFISIGTYPE_X509,           "X.509"            }
};


/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/


/**
 * Returns the internal siganture type descriptor for the given EFI GUID.
 *
 * @returns Pointer to the descriptor if found or NULL if not.
 * @param   pGuid               The EFI signature type GUID to look for.
 */
static PCRTEFISIGDBDESC rtEfiSigDbGetDescByGuid(PCEFI_GUID pGuid)
{
    for (uint32_t i = 0; i < RT_ELEMENTS(g_aGuid2SigTypeMapping); i++)
        if (!RTEfiGuidCompare(&g_aGuid2SigTypeMapping[i].GuidSignatureType, pGuid))
            return &g_aGuid2SigTypeMapping[i];

    return NULL;
}


/**
 * Validates the given signature lsit header.
 *
 * @returns Flag whether the list header is considered valid.
 * @param   pLstHdr             The list header to validate.
 * @param   pDesc               The descriptor for the signature type of the given list.
 */
static bool rtEfiSigDbSigHdrValidate(PCEFI_SIGNATURE_LIST pLstHdr, PCRTEFISIGDBDESC pDesc)
{
    uint32_t cbSigLst = RT_LE2H_U32(pLstHdr->cbSigLst);
    uint32_t cbSigHdr = RT_LE2H_U32(pLstHdr->cbSigHdr);
    uint32_t cbSig    = RT_LE2H_U32(pLstHdr->cbSig);

    if (cbSigHdr != pDesc->cbSigHdr)
        return false;
    if (cbSig < sizeof(EFI_SIGNATURE_DATA))
        return false;
    if (   pDesc->cbSig
        && pLstHdr->cbSig != pDesc->cbSig)
        return false;
    if (   cbSigLst <= sizeof(*pLstHdr)
        || cbSigLst <= cbSigHdr
        || cbSigLst <= cbSig)
        return false;
    if ((cbSigLst - sizeof(*pLstHdr) - cbSigHdr) % cbSig)
        return false;

    return true;
}


/**
 * Loads a single signature list into the given signature database from the given file.
 *
 * @returns IPRT status code.
 * @param   pThis               The signature database instance.
 * @param   hVfsFileIn          The file to load the signature list from.
 * @param   pcbConsumed         Where to store the number of bytes consumed for this signature list on success.
 */
static int rtEfiSigDbLoadSigList(PRTEFISIGDBINT pThis, RTVFSFILE hVfsFileIn, uint64_t *pcbConsumed)
{
    EFI_SIGNATURE_LIST LstHdr;
    int rc = RTVfsFileRead(hVfsFileIn, &LstHdr, sizeof(LstHdr), NULL /*pcbRead*/);
    if (RT_SUCCESS(rc))
    {
        PCRTEFISIGDBDESC pDesc = rtEfiSigDbGetDescByGuid(&LstHdr.GuidSigType);
        if (pDesc)
        {
            if (rtEfiSigDbSigHdrValidate(&LstHdr, pDesc))
            {
                RTLISTANCHOR LstTmp;
                uint32_t cbSig     = RT_LE2H_U32(LstHdr.cbSig);
                uint32_t cbSigData = cbSig - sizeof(EFI_SIGNATURE_DATA);
                uint32_t cSigs     = (RT_LE2H_U32(LstHdr.cbSigLst) - RT_LE2H_U32(LstHdr.cbSigHdr)) / cbSig;

                /** @todo Skip/parse signature header if we have to add a type which has this != 0. */
                RTListInit(&LstTmp);
                for (uint32_t i = 0; i < cSigs && RT_SUCCESS(rc); i++)
                {
                    PRTEFISIGNATURE pSig = (PRTEFISIGNATURE)RTMemAllocZ(RT_UOFFSETOF_DYN(RTEFISIGNATURE, abSignature[cbSigData]));
                    if (pSig)
                    {
                        EFI_SIGNATURE_DATA SigData;
                        rc = RTVfsFileRead(hVfsFileIn, &SigData, sizeof(SigData), NULL /*pcbRead*/);
                        if (RT_SUCCESS(rc))
                            rc = RTVfsFileRead(hVfsFileIn, &pSig->abSignature[0], cbSigData, NULL /*pcbRead*/);
                        if (RT_SUCCESS(rc))
                        {
                            RTEfiGuidToUuid(&pSig->UuidOwner, &SigData.GuidOwner);
                            pSig->cbSignature = cbSigData;
                            RTListAppend(&LstTmp, &pSig->NdLst);
                        }
                        else
                            RTMemFree(pSig);
                    }
                    else
                        rc = VERR_NO_MEMORY;
                }

                if (RT_SUCCESS(rc))
                {
                    /* Add the signatures to the list. */
                    RTListConcatenate(&pThis->aLstSigTypes[pDesc->enmSigType], &LstTmp);
                    *pcbConsumed = sizeof(LstHdr) + RT_LE2H_U32(LstHdr.cbSigHdr) + cSigs * cbSig;
                }
                else
                {
                    /* Destroy the temporary list. */
                    PRTEFISIGNATURE pIt, pItNext;

                    RTListForEachSafe(&LstTmp, pIt, pItNext, RTEFISIGNATURE, NdLst)
                    {
                        RTListNodeRemove(&pIt->NdLst);
                        RTMemFree(pIt);
                    }
                }
            }
            else
                rc = VERR_NOT_SUPPORTED;
        }
        else
            rc = VERR_NOT_SUPPORTED;
    }

    return rc;
}


/**
 * Variant for written a list of signatures where each signature gets its own signature list header
 * (for types where each signature can differ in size like X.509).
 *
 * @returns IPRT status code.
 * @param   pLst                The list of signatures to write.
 * @param   pDesc               The signature type descriptor.
 * @param   hVfsFileOut         The file to write the database to.
 * @param   pcbThisWritten      Where to store the number of bytes written for the given signature list.
 */
static int rtEfiSigDbWriteListSingle(PRTLISTANCHOR pLst, PCRTEFISIGDBDESC pDesc, RTVFSFILE hVfsFileOut, size_t *pcbThisWritten)
{
    int rc = VINF_SUCCESS;
    size_t cbWritten = 0;
    PRTEFISIGNATURE pIt;

    RTListForEach(pLst, pIt, RTEFISIGNATURE, NdLst)
    {
        EFI_SIGNATURE_LIST LstHdr;
        EFI_SIGNATURE_DATA SigData;
        LstHdr.GuidSigType = pDesc->GuidSignatureType;
        LstHdr.cbSigLst    = RT_H2LE_U32(sizeof(LstHdr) + sizeof(SigData) + pDesc->cbSigHdr + pIt->cbSignature);
        LstHdr.cbSigHdr    = RT_H2LE_U32(pDesc->cbSigHdr);
        LstHdr.cbSig       = RT_H2LE_U32(pIt->cbSignature + sizeof(SigData));
        RTEfiGuidFromUuid(&SigData.GuidOwner, &pIt->UuidOwner);

        RTSGSEG aSegs[3];
        RTSGBUF SgBuf;

        Assert(!pDesc->cbSigHdr);
        aSegs[0].pvSeg = &LstHdr;
        aSegs[0].cbSeg = sizeof(LstHdr);
        aSegs[1].pvSeg = &SigData;
        aSegs[1].cbSeg = sizeof(SigData);
        aSegs[2].pvSeg = &pIt->abSignature[0];
        aSegs[2].cbSeg = pIt->cbSignature;
        RTSgBufInit(&SgBuf, &aSegs[0], RT_ELEMENTS(aSegs));
        rc = RTVfsFileSgWrite(hVfsFileOut, -1, &SgBuf, true /*fBlocking*/, NULL /*pcbWritten*/);
        if (RT_FAILURE(rc))
            break;

        cbWritten += sizeof(LstHdr) + sizeof(SigData) + pDesc->cbSigHdr + pIt->cbSignature;
    }

    if (RT_SUCCESS(rc))
        *pcbThisWritten = cbWritten;

    return rc;
}


/**
 * Writes the given signature list to the database in the given file.
 *
 * @returns IPRT status code.
 * @param   pLst                The list of signatures to write.
 * @param   pDesc               The signature type descriptor.
 * @param   hVfsFileOut         The file to write the database to.
 * @param   pcbThisWritten      Where to store the number of bytes written for the given signature list.
 */
static int rtEfiSigDbWriteList(PRTLISTANCHOR pLst, PCRTEFISIGDBDESC pDesc, RTVFSFILE hVfsFileOut, size_t *pcbThisWritten)
{
    /*
     * For signature lists where each signature can have a different size (X.509 for example)
     * writing a new list for each signature is required which is done by a dedicated method.
     */
    if (!pDesc->cbSig)
        return rtEfiSigDbWriteListSingle(pLst, pDesc, hVfsFileOut, pcbThisWritten);


    /* Count the number of signatures first. */
    uint32_t cSigs = 0;
    PRTEFISIGNATURE pIt;

    RTListForEach(pLst, pIt, RTEFISIGNATURE, NdLst)
    {
        cSigs++;
    }

    EFI_SIGNATURE_LIST LstHdr;
    LstHdr.GuidSigType = pDesc->GuidSignatureType;
    LstHdr.cbSigLst    = RT_H2LE_U32(sizeof(LstHdr) + pDesc->cbSigHdr + cSigs * pDesc->cbSig);
    LstHdr.cbSigHdr    = RT_H2LE_U32(pDesc->cbSigHdr);
    LstHdr.cbSig       = RT_H2LE_U32(pDesc->cbSig);

    int rc = RTVfsFileWrite(hVfsFileOut, &LstHdr, sizeof(LstHdr), NULL /*pcbWritten*/);
    if (RT_SUCCESS(rc))
    {
        RTListForEach(pLst, pIt, RTEFISIGNATURE, NdLst)
        {
            RTSGSEG aSegs[2];
            RTSGBUF SgBuf;
            EFI_SIGNATURE_DATA SigData;
            RTEfiGuidFromUuid(&SigData.GuidOwner, &pIt->UuidOwner);

            Assert(pDesc->cbSig == pIt->cbSignature);
            aSegs[0].pvSeg = &SigData;
            aSegs[0].cbSeg = sizeof(SigData);
            aSegs[1].pvSeg = &pIt->abSignature[0];
            aSegs[1].cbSeg = pIt->cbSignature;
            RTSgBufInit(&SgBuf, &aSegs[0], RT_ELEMENTS(aSegs));
            rc = RTVfsFileSgWrite(hVfsFileOut, -1, &SgBuf, true /*fBlocking*/, NULL /*pcbWritten*/);
            if (RT_FAILURE(rc))
                break;
        }
    }

    if (RT_SUCCESS(rc))
        *pcbThisWritten = sizeof(LstHdr) + pDesc->cbSigHdr + cSigs * pDesc->cbSig;

    return rc;
}


/**
 * Allocate a new signature of the given size.
 *
 * @returns Pointer to the new signature or NULL if out of memory.
 * @param   pUuidOwner          The UUID of the signature owner.
 * @param   cbSig               Size of the signature data in bytes.
 */
static PRTEFISIGNATURE rtEfiSigDbAllocSignature(PCRTUUID pUuidOwner, uint32_t cbSig)
{
    PRTEFISIGNATURE pSig = (PRTEFISIGNATURE)RTMemAllocZ(RT_UOFFSETOF_DYN(RTEFISIGNATURE, abSignature[cbSig]));
    if (pSig)
    {
        pSig->UuidOwner   = *pUuidOwner;
        pSig->cbSignature = cbSig;
    }

    return pSig;
}


RTDECL(int) RTEfiSigDbCreate(PRTEFISIGDB phEfiSigDb)
{
    AssertPtrReturn(phEfiSigDb, VERR_INVALID_POINTER);

    PRTEFISIGDBINT pThis = (PRTEFISIGDBINT)RTMemAllocZ(sizeof(*pThis));
    if (RT_LIKELY(pThis))
    {
        for (uint32_t i = 0; i < RT_ELEMENTS(pThis->aLstSigTypes); i++)
            RTListInit(&pThis->aLstSigTypes[i]);
        *phEfiSigDb = pThis;
        return VINF_SUCCESS;
    }

    return VERR_NO_MEMORY;
}


RTDECL(int) RTEfiSigDbDestroy(RTEFISIGDB hEfiSigDb)
{
    PRTEFISIGDBINT pThis = hEfiSigDb;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);

    for (uint32_t i = 0; i < RT_ELEMENTS(pThis->aLstSigTypes); i++)
    {
        PRTEFISIGNATURE pIt, pItNext;

        RTListForEachSafe(&pThis->aLstSigTypes[i], pIt, pItNext, RTEFISIGNATURE, NdLst)
        {
            RTListNodeRemove(&pIt->NdLst);
            RTMemFree(pIt);
        }
    }

    RTMemFree(pThis);
    return VINF_SUCCESS;
}


RTDECL(int) RTEfiSigDbAddFromExistingDb(RTEFISIGDB hEfiSigDb, RTVFSFILE hVfsFileIn)
{
    PRTEFISIGDBINT pThis = hEfiSigDb;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);

    uint64_t cbFile;
    int rc = RTVfsFileQuerySize(hVfsFileIn, &cbFile);
    if (   RT_SUCCESS(rc)
        && cbFile)
    {
        do
        {
            uint64_t cbConsumed = 0;
            rc = rtEfiSigDbLoadSigList(pThis, hVfsFileIn, &cbConsumed);
            cbFile -= cbConsumed;
        } while (   RT_SUCCESS(rc)
                 && cbFile);
    }

    return rc;
}


RTDECL(int) RTEfiSigDbAddSignatureFromFile(RTEFISIGDB hEfiSigDb, RTEFISIGTYPE enmSigType, PCRTUUID pUuidOwner, RTVFSFILE hVfsFileIn)
{
    PRTEFISIGDBINT pThis = hEfiSigDb;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(enmSigType >= RTEFISIGTYPE_FIRST_VALID && enmSigType < RTEFISIGTYPE_FIRST_INVALID, VERR_INVALID_PARAMETER);
    AssertPtrReturn(pUuidOwner, VERR_INVALID_POINTER);

    PCRTEFISIGDBDESC pDesc = &g_aGuid2SigTypeMapping[enmSigType];
    uint64_t cbSig = 0;
    int rc = RTVfsFileQuerySize(hVfsFileIn, &cbSig);
    if (RT_SUCCESS(rc))
    {
        if (   (   !pDesc->cbSig
                || pDesc->cbSig - sizeof(EFI_SIGNATURE_DATA) == cbSig)
            && cbSig < UINT32_MAX)
        {
            PRTEFISIGNATURE pSig = rtEfiSigDbAllocSignature(pUuidOwner, (uint32_t)cbSig);
            if (pSig)
            {
                rc = RTVfsFileRead(hVfsFileIn, &pSig->abSignature[0], (size_t)cbSig, NULL /*pcbRead*/);
                if (RT_SUCCESS(rc))
                    RTListAppend(&pThis->aLstSigTypes[enmSigType], &pSig->NdLst);
                else
                    RTMemFree(pSig);
            }
            else
                rc = VERR_NO_MEMORY;
        }
        else
            rc = VERR_INVALID_PARAMETER;
    }

    return rc;
}


RTDECL(int) RTEfiSigDbAddSignatureFromBuf(RTEFISIGDB hEfiSigDb, RTEFISIGTYPE enmSigType, PCRTUUID pUuidOwner,
                                          const void *pvBuf, size_t cbBuf)
{
    PRTEFISIGDBINT pThis = hEfiSigDb;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(enmSigType >= RTEFISIGTYPE_FIRST_VALID && enmSigType < RTEFISIGTYPE_FIRST_INVALID, VERR_INVALID_PARAMETER);
    AssertPtrReturn(pUuidOwner, VERR_INVALID_POINTER);
    AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
    AssertReturn(cbBuf && cbBuf < UINT32_MAX, VERR_INVALID_PARAMETER);

    int rc = VINF_SUCCESS;
    PCRTEFISIGDBDESC pDesc = &g_aGuid2SigTypeMapping[enmSigType];
    if (   !pDesc->cbSig
        || pDesc->cbSig - sizeof(EFI_SIGNATURE_DATA) == cbBuf)
    {
        PRTEFISIGNATURE pSig = rtEfiSigDbAllocSignature(pUuidOwner, (uint32_t)cbBuf);
        if (pSig)
        {
            memcpy(&pSig->abSignature[0], pvBuf, cbBuf);
            RTListAppend(&pThis->aLstSigTypes[enmSigType], &pSig->NdLst);
        }
        else
            rc = VERR_NO_MEMORY;
    }
    else
        rc = VERR_INVALID_PARAMETER;

    return rc;
}


RTDECL(int) RTEfiSigDbWriteToFile(RTEFISIGDB hEfiSigDb, RTVFSFILE hVfsFileOut)
{
    PRTEFISIGDBINT pThis = hEfiSigDb;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);

    int rc = VINF_SUCCESS;
    size_t cbSigDb = 0;
    for (uint32_t i = RTEFISIGTYPE_FIRST_VALID; i < RT_ELEMENTS(pThis->aLstSigTypes) && RT_SUCCESS(rc); i++)
    {
        if (!RTListIsEmpty(&pThis->aLstSigTypes[i]))
        {
            size_t cbThisWritten = 0;
            rc = rtEfiSigDbWriteList(&pThis->aLstSigTypes[i], &g_aGuid2SigTypeMapping[i], hVfsFileOut, &cbThisWritten);
            if (RT_SUCCESS(rc))
                cbSigDb += cbThisWritten;
        }
    }

    if (RT_SUCCESS(rc))
        rc = RTVfsFileSetSize(hVfsFileOut, cbSigDb, RTVFSFILE_SIZE_F_NORMAL);

    return rc;
}


RTDECL(int) RTEfiSigDbEnum(RTEFISIGDB hEfiSigDb, PFNRTEFISIGDBENUMSIG pfnEnumSig, void *pvUser)
{
    PRTEFISIGDBINT pThis = hEfiSigDb;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);

    for (uint32_t i = RTEFISIGTYPE_FIRST_VALID; i < RT_ELEMENTS(pThis->aLstSigTypes); i++)
    {
        PRTEFISIGNATURE pIt;

        RTListForEach(&pThis->aLstSigTypes[i], pIt, RTEFISIGNATURE, NdLst)
        {
            int rc = pfnEnumSig(pThis, (RTEFISIGTYPE)i, &pIt->UuidOwner, &pIt->abSignature[0], pIt->cbSignature, pvUser);
            if (rc != VINF_SUCCESS)
                return rc;
        }
    }

    return VINF_SUCCESS;
}


RTDECL(const char *) RTEfiSigDbTypeStringify(RTEFISIGTYPE enmSigType)
{
    AssertReturn(enmSigType < RTEFISIGTYPE_FIRST_INVALID, NULL);
    return g_aGuid2SigTypeMapping[enmSigType].pszName;
}


RTDECL(PCEFI_GUID) RTEfiSigDbTypeGetGuid(RTEFISIGTYPE enmSigType)
{
    AssertReturn(enmSigType < RTEFISIGTYPE_FIRST_INVALID, NULL);
    return &g_aGuid2SigTypeMapping[enmSigType].GuidSignatureType;
}