Skip to content

Commit c13f84b

Browse files
committed
[ot_certs] Add subtitution integration test
This test verifies that the generic.hjson template, substituted with a json data files, yields example.hjson. This verifies that the whole substitution chains is correct. Signed-off-by: Amaury Pouly <[email protected]>
1 parent d014a69 commit c13f84b

File tree

6 files changed

+80
-16
lines changed

6 files changed

+80
-16
lines changed

sw/device/silicon_creator/lib/cert/uds_example_data.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

sw/host/ot_certs/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ filegroup(
1616
srcs = ["tests/example.hjson"],
1717
)
1818

19+
filegroup(
20+
name = "example_data",
21+
srcs = ["tests/example_data.json"],
22+
)
23+
1924
rust_library(
2025
name = "ot_certs",
2126
srcs = [
@@ -75,6 +80,8 @@ rust_test_suite(
7580
srcs = glob(["tests/*.rs"]),
7681
compile_data = [
7782
":generic_cert",
83+
":example_cert",
84+
":example_data",
7885
],
7986
deps = [
8087
":ot_certs",

sw/host/ot_certs/tests/example.hjson

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright lowRISC contributors.
22
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
33
// SPDX-License-Identifier: Apache-2.0
4+
5+
// This example must correspond to taking the generic.hjson template and substituting the variables
6+
// using example_data.json
47
{
58
name: "example",
69

@@ -25,8 +28,8 @@
2528
y: "0x0a0636f5073209440adb17dd8b102bc1154dc95394abfaeecd89852e1d622be1",
2629
},
2730
},
28-
authority_key_identifier: "94589abcd87444",
29-
subject_key_identifier: "04897afec876db",
31+
authority_key_identifier: "94589abcd8744445ef329bc4186a11ff9a74bc4d",
32+
subject_key_identifier: "04897afec876db876d4efbc6a3dd9f164965dfac",
3033
extensions: [
3134
{
3235
type: "dice_tcb_info",
@@ -40,16 +43,19 @@
4043
],
4144
flags: {
4245
not_configured: false,
43-
not_secure: false,
46+
not_secure: true,
4447
recovery: false,
45-
debug: false,
48+
debug: true,
4649
}
4750
}
4851
],
4952
signature: {
5053
algorithm: "ecdsa-with-sha256",
5154
// The value field is optional: if not present, the signature will be cleared.
52-
// Otherwise, we can reference the various fields of the signature.
55+
value: {
56+
r: "0",
57+
s: "0",
58+
}
5359
}
5460
}
5561
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"serial_number": "0xc32a9847abefc5074ad",
3+
"issuer_c": "UK",
4+
"issuer_cn": "lowRISC",
5+
"subject_serial_number": "5498694878674878747894768746897849",
6+
"pub_key_ec_x": "0x6d13d8dca1d8211298d41abd8f7ac38c07333c78e652b44c5b425fce61184a",
7+
"pub_key_ec_y": "4534108868468764070259928525122280184028285285512090975127203821338071542753",
8+
"auth_key_id": "94589abcd8744445ef329bc4186a11ff9a74bc4d",
9+
"pub_key_id": [4, 137, 122, 254, 200, 118, 219, 135, 109, 78, 251, 198, 163, 221, 159, 22, 73, 101, 223, 172],
10+
"vendor": "lowRISC",
11+
"model": "OpenTitan",
12+
"security_version": 0,
13+
"layer": [42],
14+
"hash_1": "465644d935385783658357583758c593583b6537",
15+
"hash_2": "009e9809f85978327592857a093f539078626589",
16+
"not_configured": false,
17+
"not_secure": true,
18+
"recovery": "false",
19+
"debug": "true",
20+
"cert_signature_r": "0",
21+
"cert_signature_s": "0"
22+
}

sw/host/ot_certs/tests/generic.hjson

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@
5151
},
5252
hash_1: {
5353
type: "byte-array",
54-
size: 32,
54+
size: 20,
5555
},
5656
hash_2: {
5757
type: "byte-array",
58-
size: 32,
58+
size: 20,
5959
},
6060
security_version: {
6161
type: "integer",

sw/host/ot_certs/tests/subst_test.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright lowRISC contributors.
2+
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
use anyhow::{bail, Result};
6+
7+
use ot_certs::template::subst::{Subst, SubstData};
8+
use ot_certs::template::Template;
9+
10+
const GENERIC_CERT: &str = include_str!("generic.hjson");
11+
const EXAMPLE_DATA: &str = include_str!("example_data.json");
12+
const EXAMPLE_CERT: &str = include_str!("example.hjson");
13+
14+
#[test]
15+
fn main() -> Result<()> {
16+
// Parse generic certificate.
17+
let generic_tmpl =
18+
Template::from_hjson_str(GENERIC_CERT).expect("failed to parse generic template");
19+
// Parse example certificate.
20+
let example_tmpl =
21+
Template::from_hjson_str(EXAMPLE_CERT).expect("failed to parse example template");
22+
// Load data.
23+
let test_data = SubstData::from_json(EXAMPLE_DATA).expect("failed to parse example data");
24+
// Substitute data into the template.
25+
let mut cert = generic_tmpl.subst(&test_data)?;
26+
// We need to change the name to make sure that it matches.
27+
cert.name = "example".to_string();
28+
// Check that we obtain the example certificate template.
29+
if example_tmpl != cert {
30+
println!("expected: {:#?}", example_tmpl);
31+
println!("got: {cert:#?}");
32+
bail!(
33+
"example.hjson does not correspond to substituting example_data.json in generic.hjson"
34+
);
35+
}
36+
37+
Ok(())
38+
}

0 commit comments

Comments
 (0)