Write signed variable
| 369 | |
| 370 | // Write signed variable |
| 371 | EFI_STATUS SetSignedVariable(IN CHAR16 *DatabaseName, |
| 372 | IN EFI_GUID *DatabaseGuid, |
| 373 | IN UINT32 Attributes, |
| 374 | IN VOID *Database, |
| 375 | IN UINTN DatabaseSize) |
| 376 | { |
| 377 | EFI_STATUS Status; |
| 378 | EFI_VARIABLE_AUTHENTICATION_2 *Authentication; |
| 379 | UINTN Size, NameLen; |
| 380 | UINTN DataSize = 0; |
| 381 | EFI_TIME Timestamp; |
| 382 | VOID *Data = NULL; |
| 383 | BIO *BioData = NULL; |
| 384 | PKCS7 *p7; |
| 385 | X509 *Certificate = NULL; |
| 386 | EVP_PKEY *PrivateKey = NULL; |
| 387 | const EVP_MD *md; |
| 388 | // Check parameters |
| 389 | if ((DatabaseName == NULL) || (DatabaseGuid == NULL)) { |
| 390 | return EFI_INVALID_PARAMETER; |
| 391 | } |
| 392 | DBG("Setting secure variable: %s %ls 0x%hhX (0x%hhX)\n", strguid(DatabaseGuid), DatabaseName, Database, DatabaseSize); |
| 393 | NameLen = StrLen(DatabaseName); |
| 394 | if (NameLen == 0) { |
| 395 | return EFI_INVALID_PARAMETER; |
| 396 | } |
| 397 | // Get the current time |
| 398 | DBG("Getting timestamp ...\n"); |
| 399 | Status = GetUTCTime(&Timestamp); |
| 400 | if (EFI_ERROR(Status)) { |
| 401 | return Status; |
| 402 | } |
| 403 | DBG("Timestamp: %t\n", Timestamp); |
| 404 | // In user mode we need to sign the database with exchange key |
| 405 | if (!gSettings.SecureBootSetupMode) { |
| 406 | // Initialize the cyphers and digests |
| 407 | ERR_load_crypto_strings(); |
| 408 | OpenSSL_add_all_digests(); |
| 409 | OpenSSL_add_all_ciphers(); |
| 410 | // Create signing certificate |
| 411 | BioData = BIO_new_mem_buf((void *)gSecureBootExchangeKey, sizeof(gSecureBootExchangeKey)); |
| 412 | if (BioData == NULL) { |
| 413 | return EFI_OUT_OF_RESOURCES; |
| 414 | } |
| 415 | Certificate = PEM_read_bio_X509(BioData, NULL, NULL, NULL); |
| 416 | BIO_free(BioData); |
| 417 | if (Certificate == NULL) { |
| 418 | return EFI_OUT_OF_RESOURCES; |
| 419 | } |
| 420 | // Create signing private key |
| 421 | BioData = BIO_new_mem_buf((void *)gSecureBootExchangePrivateKey, sizeof(gSecureBootExchangePrivateKey)); |
| 422 | if (BioData == NULL) { |
| 423 | return EFI_OUT_OF_RESOURCES; |
| 424 | } |
| 425 | PrivateKey = PEM_read_bio_PrivateKey(BioData, NULL, NULL, NULL); |
| 426 | BIO_free(BioData); |
| 427 | if (PrivateKey == NULL) { |
| 428 | X509_free(Certificate); |
no test coverage detected