Changeset 90341 in vbox for trunk/src/VBox/Runtime
- Timestamp:
- Jul 26, 2021 4:11:46 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/tools/RTEfiSigDb.cpp
r90336 r90341 428 428 429 429 /** 430 * Adds the given signature to the given signature database of the given EFIvariable store.430 * Adds the given variable to the variable store. 431 431 * 432 432 * @returns IPRT status code. 433 433 * @param hVfsVarStore Handle of the EFI variable store VFS. 434 * @param pszDb The signature database to update. 435 * @param fWipeDbBefore Flag whether to wipe the database before adding the signature. 436 * @param cSigs Number of signatures following. 437 * @param ... A triple of signature path, signature type and owner uuid string pointers for each 438 * signature. 439 */ 440 static int rtEfiSigDbVarStoreAddToDb(RTVFS hVfsVarStore, const char *pszDb, bool fWipeDbBefore, uint32_t cSigs, 441 ... /*const char *pszSigPath, const char *pszSigType, const char *pszUuidOwner*/) 442 { 443 EFI_GUID GuidSecurityDb = EFI_IMAGE_SECURITY_DATABASE_GUID; 444 RTUUID UuidSecurityDb; 445 RTEfiGuidToUuid(&UuidSecurityDb, &GuidSecurityDb); 446 447 char szDbPath[_1K]; 448 ssize_t cch = RTStrPrintf2(szDbPath, sizeof(szDbPath), "/by-uuid/%RTuuid/%s", &UuidSecurityDb, pszDb); 434 * @param pGuid The EFI GUID of the variable. 435 * @param pszVar The variable name. 436 * @param fAttr Attributes for the variable. 437 * @param phVfsFile Where to return the VFS file handle to the created variable on success. 438 */ 439 static int rtEfiSigDbVarStoreAddVar(RTVFS hVfsVarStore, PCEFI_GUID pGuid, const char *pszVar, uint32_t fAttr, PRTVFSFILE phVfsFile) 440 { 441 RTUUID UuidVar; 442 RTEfiGuidToUuid(&UuidVar, pGuid); 443 444 char szVarPath[_1K]; 445 ssize_t cch = RTStrPrintf2(szVarPath, sizeof(szVarPath), "/by-uuid/%RTuuid/%s", &UuidVar, pszVar); 449 446 Assert(cch > 0); RT_NOREF(cch); 450 447 451 RTVFSFILE hVfsFileSigDb = NIL_RTVFSFILE; 452 int rc = RTVfsFileOpen(hVfsVarStore, szDbPath, 448 int rc = RTVfsFileOpen(hVfsVarStore, szVarPath, 453 449 RTFILE_O_READWRITE | RTFILE_O_DENY_NONE | RTFILE_O_OPEN, 454 &hVfsFileSigDb);450 phVfsFile); 455 451 if ( rc == VERR_PATH_NOT_FOUND 456 452 || rc == VERR_FILE_NOT_FOUND) … … 465 461 { 466 462 char szGuidPath[_1K]; 467 cch = RTStrPrintf2(szGuidPath, sizeof(szGuidPath), "by-uuid/%RTuuid", &Uuid SecurityDb);463 cch = RTStrPrintf2(szGuidPath, sizeof(szGuidPath), "by-uuid/%RTuuid", &UuidVar); 468 464 Assert(cch > 0); 469 465 … … 482 478 if (RT_SUCCESS(rc)) 483 479 { 484 rc = RTVfsFileOpen(hVfsVarStore, sz DbPath,480 rc = RTVfsFileOpen(hVfsVarStore, szVarPath, 485 481 RTFILE_O_READWRITE | RTFILE_O_DENY_NONE | RTFILE_O_CREATE, 486 &hVfsFileSigDb);482 phVfsFile); 487 483 if (RT_SUCCESS(rc)) 488 rc = rtEfiSigDbSetVarAttr(hVfsVarStore, pszDb, 489 EFI_VAR_HEADER_ATTR_NON_VOLATILE 490 | EFI_VAR_HEADER_ATTR_BOOTSERVICE_ACCESS 491 | EFI_VAR_HEADER_ATTR_RUNTIME_ACCESS 492 | EFI_AUTH_VAR_HEADER_ATTR_TIME_BASED_AUTH_WRITE_ACCESS); 484 rc = rtEfiSigDbSetVarAttr(hVfsVarStore, pszVar, fAttr); 493 485 } 494 486 495 487 if (RT_FAILURE(rc)) 496 rc = RTMsgErrorRc(rc, "Creating the signature database '%s' failed: %Rrc", pszDb, rc); 497 } 498 488 rc = RTMsgErrorRc(rc, "Creating the variable '%s' failed: %Rrc", pszVar, rc); 489 } 490 491 return rc; 492 } 493 494 495 /** 496 * Creates the given variable and sets the data. 497 * 498 * @returns IPRT status code. 499 * @param hVfsVarStore Handle of the EFI variable store VFS. 500 * @param pGuid The EFI GUID of the variable. 501 * @param pszVar The variable name. 502 * @param fAttr Attributes for the variable. 503 * @param pvBuf The data to write. 504 * @param cbBuf Number of bytes of data. 505 */ 506 static int rtEfiSigDbVarStoreSetVar(RTVFS hVfsVarStore, PCEFI_GUID pGuid, const char *pszVar, uint32_t fAttr, 507 const void *pvBuf, size_t cbBuf) 508 { 509 RTVFSFILE hVfsFileVar = NIL_RTVFSFILE; 510 int rc = rtEfiSigDbVarStoreAddVar(hVfsVarStore, pGuid, pszVar, fAttr, &hVfsFileVar); 511 if (RT_SUCCESS(rc)) 512 { 513 rc = RTVfsFileWrite(hVfsFileVar, pvBuf, cbBuf, NULL /*pcbWritten*/); 514 if (RT_FAILURE(rc)) 515 rc = RTMsgErrorRc(rc, "Writing variable '%s' failed: %Rrc", pszVar, rc); 516 RTVfsFileRelease(hVfsFileVar); 517 } 518 else 519 rc = RTMsgErrorRc(rc, "Creating variable '%s' failed: %Rrc", pszVar, rc); 520 521 return rc; 522 } 523 524 525 /** 526 * Adds the given signature to the given signature database of the given EFI variable store. 527 * 528 * @returns IPRT status code. 529 * @param hVfsVarStore Handle of the EFI variable store VFS. 530 * @param pGuid The EFI GUID of the variable. 531 * @param pszDb The signature database to update. 532 * @param fWipeDbBefore Flag whether to wipe the database before adding the signature. 533 * @param cSigs Number of signatures following. 534 * @param ... A triple of signature path, signature type and owner uuid string pointers for each 535 * signature. 536 */ 537 static int rtEfiSigDbVarStoreAddToDb(RTVFS hVfsVarStore, PCEFI_GUID pGuid, const char *pszDb, bool fWipeDbBefore, uint32_t cSigs, 538 ... /*const char *pszSigPath, const char *pszSigType, const char *pszUuidOwner*/) 539 { 540 RTVFSFILE hVfsFileSigDb = NIL_RTVFSFILE; 541 int rc = rtEfiSigDbVarStoreAddVar(hVfsVarStore, pGuid, pszDb, 542 EFI_VAR_HEADER_ATTR_NON_VOLATILE 543 | EFI_VAR_HEADER_ATTR_BOOTSERVICE_ACCESS 544 | EFI_VAR_HEADER_ATTR_RUNTIME_ACCESS 545 | EFI_AUTH_VAR_HEADER_ATTR_TIME_BASED_AUTH_WRITE_ACCESS, 546 &hVfsFileSigDb); 499 547 if (RT_SUCCESS(rc)) 500 548 { … … 542 590 } 543 591 else 544 rc = RTMsgErrorRc(rc, "Opening signature database '%s' failed: %Rrc", szDbPath, rc);592 rc = RTMsgErrorRc(rc, "Opening signature database '%s' failed: %Rrc", pszDb, rc); 545 593 546 594 return rc; … … 666 714 if (RT_SUCCESS(rc)) 667 715 { 716 EFI_GUID GuidSecurityDb = EFI_IMAGE_SECURITY_DATABASE_GUID; 717 EFI_GUID GuidGlobalVar = EFI_GLOBAL_VARIABLE_GUID; 718 668 719 if (pszPkPath) 669 rc = rtEfiSigDbVarStoreAddToDb(hVfsEfiVarStore, "PK", true /*fWipeDbBefore*/, 1 /*cSigs*/, pszPkPath, "x509", pszUuidPkOwner);720 rc = rtEfiSigDbVarStoreAddToDb(hVfsEfiVarStore, &GuidGlobalVar, "PK", true /*fWipeDbBefore*/, 1 /*cSigs*/, pszPkPath, "x509", pszUuidPkOwner); 670 721 if ( RT_SUCCESS(rc) 671 722 && pszKekPath) 672 rc = rtEfiSigDbVarStoreAddToDb(hVfsEfiVarStore, "KEK", true /*fWipeDbBefore*/, 1 /*cSigs*/, pszKekPath, "x509", pszUuidKekOwner);723 rc = rtEfiSigDbVarStoreAddToDb(hVfsEfiVarStore, &GuidGlobalVar ,"KEK", true /*fWipeDbBefore*/, 1 /*cSigs*/, pszKekPath, "x509", pszUuidKekOwner); 673 724 674 725 if ( RT_SUCCESS(rc) … … 696 747 if ( pszSigTypeFree 697 748 && pszUuidOwnerFree) 698 rc = rtEfiSigDbVarStoreAddToDb(hVfsEfiVarStore, "db",749 rc = rtEfiSigDbVarStoreAddToDb(hVfsEfiVarStore, &GuidSecurityDb, "db", 699 750 i == 0 ? true : false /*fWipeDbBefore*/, 700 751 1 /*cSigs*/, … … 716 767 } 717 768 769 if ( RT_SUCCESS(rc) 770 && fSetSecureBoot) 771 { 772 EFI_GUID GuidSecureBootEnable = EFI_SECURE_BOOT_ENABLE_DISABLE_GUID; 773 uint8_t bVar = fSecureBoot ? 0x1 : 0x0; 774 rtEfiSigDbVarStoreSetVar(hVfsEfiVarStore, &GuidSecureBootEnable, "SecureBootEnable", 775 EFI_VAR_HEADER_ATTR_NON_VOLATILE 776 | EFI_VAR_HEADER_ATTR_BOOTSERVICE_ACCESS 777 | EFI_VAR_HEADER_ATTR_RUNTIME_ACCESS, 778 &bVar, sizeof(bVar)); 779 } 780 718 781 RTVfsRelease(hVfsEfiVarStore); 719 782 } … … 726 789 return rcExit; 727 790 } 791 728 792 729 793 int main(int argc, char **argv)
Note:
See TracChangeset
for help on using the changeset viewer.