-
Notifications
You must be signed in to change notification settings - Fork 771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[rom_ext_e2e] Check the attestation certificates #24793
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright lowRISC contributors (OpenTitan project). | ||
# Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
load( | ||
"//rules/opentitan:defs.bzl", | ||
"fpga_params", | ||
"opentitan_test", | ||
) | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
opentitan_test( | ||
name = "print_certs_test", | ||
srcs = ["print_certs.c"], | ||
exec_env = { | ||
"//hw/top_earlgrey:fpga_hyper310_rom_ext": None, | ||
}, | ||
fpga = fpga_params( | ||
test_cmd = """ | ||
--bootstrap={firmware} | ||
""", | ||
test_harness = "//sw/host/tests/attestation:attestation_test", | ||
), | ||
deps = [ | ||
"//sw/device/lib/base:status", | ||
"//sw/device/lib/runtime:log", | ||
"//sw/device/lib/runtime:print", | ||
"//sw/device/lib/testing/test_framework:ottf_main", | ||
"//sw/device/silicon_creator/lib/drivers:flash_ctrl", | ||
], | ||
) |
87 changes: 87 additions & 0 deletions
87
sw/device/silicon_creator/rom_ext/e2e/attestation/print_certs.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright lowRISC contributors (OpenTitan project). | ||
// Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "sw/device/lib/base/status.h" | ||
#include "sw/device/lib/runtime/log.h" | ||
#include "sw/device/lib/runtime/print.h" | ||
#include "sw/device/lib/testing/test_framework/ottf_main.h" | ||
#include "sw/device/silicon_creator/lib/drivers/flash_ctrl.h" | ||
|
||
OTTF_DEFINE_TEST_CONFIG(); | ||
|
||
const char kBase64[] = | ||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
|
||
static void base64_encode(char *dest, const uint8_t *data, int32_t len) { | ||
for (int32_t i = 0; len > 0; i += 3, len -= 3) { | ||
// clang-format off | ||
uint32_t val = (uint32_t)(data[i] << 16 | | ||
(len > 1 ? data[i + 1] << 8 : 0) | | ||
(len > 2 ? data[i + 2] : 0)); | ||
// clang-format on | ||
*dest++ = kBase64[(val >> 18) & 0x3f]; | ||
*dest++ = kBase64[(val >> 12) & 0x3f]; | ||
*dest++ = len > 1 ? kBase64[(val >> 6) & 0x3f] : '='; | ||
*dest++ = len > 2 ? kBase64[(val >> 0) & 0x3f] : '='; | ||
} | ||
*dest = '\0'; | ||
} | ||
|
||
// TODO: when merging to master, use this function from | ||
// //sw/device/silicon_creator/lib:cert. It does not exist | ||
// on `earlgrey_es_sival`. | ||
static uint32_t cert_x509_asn1_decode_size_header(const uint8_t *header) { | ||
if (header[0] != 0x30 || header[1] != 0x82) { | ||
return 0; | ||
} | ||
return (((uint32_t)header[2]) << 8) + header[3] + 4 /* size of the header */; | ||
} | ||
|
||
static status_t print_cert(char *dest, | ||
const flash_ctrl_info_page_t *info_page) { | ||
uint8_t data[2048]; | ||
TRY(flash_ctrl_info_read(info_page, 0, sizeof(data) / sizeof(uint32_t), | ||
data)); | ||
|
||
uint32_t len = cert_x509_asn1_decode_size_header(data); | ||
base64_encode(dest, data, (int32_t)len); | ||
return OK_STATUS(); | ||
} | ||
|
||
static status_t print_owner_block(char *dest, | ||
const flash_ctrl_info_page_t *info_page) { | ||
uint8_t data[2048]; | ||
TRY(flash_ctrl_info_read(info_page, 0, sizeof(data) / sizeof(uint32_t), | ||
data)); | ||
base64_encode(dest, data, sizeof(data)); | ||
return OK_STATUS(); | ||
} | ||
|
||
static status_t print_certs(void) { | ||
char buf[3072]; | ||
TRY(print_cert(buf, &kFlashCtrlInfoPageUdsCertificate)); | ||
LOG_INFO("UDS: %s", buf); | ||
|
||
TRY(print_cert(buf, &kFlashCtrlInfoPageCdi0Certificate)); | ||
LOG_INFO("CDI_0: %s", buf); | ||
|
||
TRY(print_cert(buf, &kFlashCtrlInfoPageCdi1Certificate)); | ||
LOG_INFO("CDI_1: %s", buf); | ||
|
||
TRY(print_owner_block(buf, &kFlashCtrlInfoPageOwnerSlot0)); | ||
LOG_INFO("OWNER_PAGE_0: %s", buf); | ||
|
||
TRY(print_owner_block(buf, &kFlashCtrlInfoPageOwnerSlot1)); | ||
LOG_INFO("OWNER_PAGE_1: %s", buf); | ||
|
||
return OK_STATUS(); | ||
} | ||
|
||
bool test_main(void) { | ||
status_t sts = print_certs(); | ||
if (status_err(sts)) { | ||
LOG_ERROR("print_certs: %r", sts); | ||
} | ||
return status_ok(sts); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright lowRISC contributors (OpenTitan project). | ||
# Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
load("@rules_rust//rust:defs.bzl", "rust_binary") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
rust_binary( | ||
name = "attestation_test", | ||
srcs = ["attestation_test.rs"], | ||
deps = [ | ||
"//sw/host/opentitanlib", | ||
"//sw/host/ot_certs", | ||
"@crate_index//:anyhow", | ||
"@crate_index//:base64ct", | ||
"@crate_index//:clap", | ||
"@crate_index//:humantime", | ||
"@crate_index//:log", | ||
"@crate_index//:num-bigint-dig", | ||
"@crate_index//:regex", | ||
"@crate_index//:serde_json", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright lowRISC contributors (OpenTitan project). | ||
// Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#![allow(clippy::bool_assert_comparison)] | ||
use anyhow::{anyhow, Result}; | ||
use base64ct::{Base64, Decoder}; | ||
use clap::Parser; | ||
use num_bigint_dig::BigUint; | ||
use regex::Regex; | ||
use std::time::Duration; | ||
|
||
use opentitanlib::app::TransportWrapper; | ||
use opentitanlib::crypto::sha256; | ||
use opentitanlib::image::image::Image; | ||
use opentitanlib::ownership::OwnerBlock; | ||
use opentitanlib::test_utils::init::InitializeTest; | ||
use opentitanlib::uart::console::UartConsole; | ||
use opentitanlib::util::file::FromReader; | ||
|
||
use ot_certs::template::{CertificateExtension, Value}; | ||
use ot_certs::x509; | ||
|
||
#[derive(Debug, Parser)] | ||
struct Opts { | ||
#[command(flatten)] | ||
init: InitializeTest, | ||
|
||
/// Console receive timeout. | ||
#[arg(long, value_parser = humantime::parse_duration, default_value = "10s")] | ||
timeout: Duration, | ||
} | ||
|
||
// A helper trait for extracting data out of the `Value` type. | ||
trait GetValue<T> { | ||
fn get_value(&self) -> &T; | ||
} | ||
|
||
impl<T: std::fmt::Debug> GetValue<T> for Value<T> { | ||
fn get_value(&self) -> &T { | ||
match self { | ||
Value::Variable(_) => panic!("Not expecting a Variable: {self:?}"), | ||
Value::Literal(x) => x, | ||
} | ||
} | ||
} | ||
|
||
impl<T: std::fmt::Debug> GetValue<T> for Option<Value<T>> { | ||
fn get_value(&self) -> &T { | ||
match self { | ||
None => panic!("Not expecting Option::None"), | ||
Some(Value::Variable(_)) => panic!("Not expecting a Variable: {self:?}"), | ||
Some(Value::Literal(x)) => x, | ||
} | ||
} | ||
} | ||
|
||
fn get_base64_blob(haystack: &str, rx: &str) -> Result<Vec<u8>> { | ||
let rx = Regex::new(rx)?; | ||
let encoded = rx | ||
.captures(haystack) | ||
.ok_or(anyhow!("Encoded certificate not found"))?; | ||
let mut bin = Vec::new(); | ||
if !encoded[1].is_empty() { | ||
let mut decoder = Decoder::<Base64>::new(encoded[1].as_bytes())?; | ||
decoder.decode_to_end(&mut bin)?; | ||
} | ||
Ok(bin) | ||
} | ||
|
||
fn attestation_test(opts: &Opts, transport: &TransportWrapper) -> Result<()> { | ||
let uart = transport.uart("console")?; | ||
let capture = UartConsole::wait_for( | ||
&*uart, | ||
r"(?msR)Running.*PASS!$|FAIL!$|BFV:([0-9A-Fa-f]{8})$", | ||
opts.timeout, | ||
)?; | ||
|
||
let _uds_bin = get_base64_blob(&capture[0], r"(?msR)UDS: (.*?)$")?; | ||
let cdi0_bin = get_base64_blob(&capture[0], r"(?msR)CDI_0: (.*?)$")?; | ||
let cdi1_bin = get_base64_blob(&capture[0], r"(?msR)CDI_1: (.*?)$")?; | ||
let owner_page_0 = get_base64_blob(&capture[0], r"(?msR)OWNER_PAGE_0: (.*?)$")?; | ||
let owner_page_1 = get_base64_blob(&capture[0], r"(?msR)OWNER_PAGE_1: (.*?)$")?; | ||
|
||
// TODO: check UDS certificate. | ||
let cdi0 = x509::parse_certificate(&cdi0_bin)?; | ||
let cdi1 = x509::parse_certificate(&cdi1_bin)?; | ||
|
||
// TODO: verify signature chain from CDI_1 to CDI_0 to UDS. | ||
|
||
let image = Image::read_from_file(opts.init.bootstrap.bootstrap.as_deref().unwrap())?; | ||
|
||
// TODO: determine the correct endianness for expressing these measurements. | ||
let measurements = image | ||
.subimages()? | ||
.iter() | ||
.map(|s| s.compute_digest().unwrap().to_le_bytes()) | ||
.collect::<Vec<_>>(); | ||
let owner_measurements = [ | ||
// The owner page digests should not include the signature or seal fields. | ||
sha256::sha256(&owner_page_0[0..OwnerBlock::SIGNATURE_OFFSET]).to_le_bytes(), | ||
sha256::sha256(&owner_page_1[0..OwnerBlock::SIGNATURE_OFFSET]).to_le_bytes(), | ||
]; | ||
|
||
let CertificateExtension::DiceTcbInfo(dice) = &cdi0.private_extensions[0]; | ||
log::info!("Checking CDI_0 (ROM_EXT) DICE Extension: {dice:#?}"); | ||
assert_eq!(dice.model.get_value(), "ROM_EXT"); | ||
assert_eq!(dice.vendor.get_value(), "OpenTitan"); | ||
assert_eq!(dice.layer.get_value(), &BigUint::from(1u8)); | ||
let fw_ids = dice.fw_ids.as_ref().expect("list of fw_ids"); | ||
assert_eq!(fw_ids.len(), 1); | ||
assert_eq!(fw_ids[0].digest.get_value(), &measurements[0]); | ||
|
||
let CertificateExtension::DiceTcbInfo(dice) = &cdi1.private_extensions[0]; | ||
log::info!("Checking CDI_1 (Owner) DICE Extension: {dice:#?}"); | ||
assert_eq!(dice.model.get_value(), "Owner"); | ||
assert_eq!(dice.vendor.get_value(), "OpenTitan"); | ||
assert_eq!(dice.layer.get_value(), &BigUint::from(2u8)); | ||
let fw_ids = dice.fw_ids.as_ref().expect("list of fw_ids"); | ||
assert_eq!(fw_ids.len(), 2); | ||
assert_eq!(fw_ids[0].digest.get_value(), &measurements[1]); | ||
assert_eq!(fw_ids[1].digest.get_value(), &owner_measurements[0]); | ||
Ok(()) | ||
} | ||
|
||
fn main() -> Result<()> { | ||
let opts = Opts::parse(); | ||
opts.init.init_logging(); | ||
let transport = opts.init.init_target()?; | ||
attestation_test(&opts, &transport)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be a good starting point: https://cs.opensource.google/opentitan/opentitan/+/master:sw/host/provisioning/cert_lib/src/lib.rs;drc=96ea5f430320b411facab840ebe7f785509c4a2a;l=160 but heads up I have not gotten it to work yet with the DICE certs since they have a custom extension that openssl can't seem to parse. Needs further investigation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So openssl won't verify a certificate that it can't fully parse?
One option might be to use the openssl library (not tool) to verify the certificate. From what I remember, it's not that straightforward unfortunately, and it might not have rust bindings, but at least with the library you can parse a certificate and extract extensions and decode manually (which is what I did in the
ot_cert
crate).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to perform the TODOs in follow-on PRs: