-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into peg/attestation-pallet
* master: TSS attestation endpoint (#1001)
- Loading branch information
Showing
12 changed files
with
224 additions
and
2 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,69 @@ | ||
// Copyright (C) 2023 Entropy Cryptography Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// 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 Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
use crate::{attestation::errors::AttestationErr, AppState}; | ||
use axum::{body::Bytes, extract::State, http::StatusCode}; | ||
|
||
/// HTTP POST endpoint to initiate a TDX attestation. | ||
/// Not yet implemented. | ||
#[cfg(not(any(test, feature = "unsafe")))] | ||
pub async fn attest( | ||
State(_app_state): State<AppState>, | ||
_input: Bytes, | ||
) -> Result<StatusCode, AttestationErr> { | ||
// Non-mock attestation (the real thing) will go here | ||
Err(AttestationErr::NotImplemented) | ||
} | ||
|
||
/// HTTP POST endpoint to initiate a mock TDX attestation for testing on non-TDX hardware. | ||
/// The body of the request should be a 32 byte random nonce used to show 'freshness' of the | ||
/// quote. | ||
/// The response body contains a mock TDX v4 quote serialized as described in the | ||
/// [Index TDX DCAP Quoting Library API](https://download.01.org/intel-sgx/latest/dcap-latest/linux/docs/Intel_TDX_DCAP_Quoting_Library_API.pdf). | ||
#[cfg(any(test, feature = "unsafe"))] | ||
pub async fn attest( | ||
State(app_state): State<AppState>, | ||
input: Bytes, | ||
) -> Result<(StatusCode, Bytes), AttestationErr> { | ||
use crate::{chain_api::get_rpc, get_signer_and_x25519_secret}; | ||
use rand_core::OsRng; | ||
use sp_core::Pair; | ||
|
||
// TODO (#982) confirm with the chain that an attestation should be happenning | ||
let nonce = input.as_ref().try_into()?; | ||
|
||
let rpc = get_rpc(&app_state.configuration.endpoint).await?; | ||
|
||
let block_number = | ||
rpc.chain_get_header(None).await?.ok_or_else(|| AttestationErr::BlockNumber)?.number; | ||
|
||
// In the real thing this is the hardware key used in the quoting enclave | ||
let signing_key = tdx_quote::SigningKey::random(&mut OsRng); | ||
|
||
let (signer, x25519_secret) = get_signer_and_x25519_secret(&app_state.kv_store).await?; | ||
let public_key = x25519_dalek::PublicKey::from(&x25519_secret); | ||
|
||
let input_data = entropy_shared::QuoteInputData::new( | ||
signer.signer().public().into(), | ||
*public_key.as_bytes(), | ||
nonce, | ||
block_number, | ||
); | ||
|
||
let quote = tdx_quote::Quote::mock(signing_key.clone(), input_data.0); | ||
// Here we would submit an attest extrinsic to the chain - but for now we just include it in the | ||
// response | ||
Ok((StatusCode::OK, Bytes::from(quote.as_bytes().to_vec()))) | ||
} |
46 changes: 46 additions & 0 deletions
46
crates/threshold-signature-server/src/attestation/errors.rs
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,46 @@ | ||
// Copyright (C) 2023 Entropy Cryptography Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// 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 Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
use std::array::TryFromSliceError; | ||
|
||
use axum::{ | ||
http::StatusCode, | ||
response::{IntoResponse, Response}, | ||
}; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum AttestationErr { | ||
#[error("Generic Substrate error: {0}")] | ||
GenericSubstrate(#[from] subxt::error::Error), | ||
#[error("User Error: {0}")] | ||
UserErr(#[from] crate::user::UserErr), | ||
#[cfg(not(any(test, feature = "unsafe")))] | ||
#[error("Not yet implemented")] | ||
NotImplemented, | ||
#[error("Input must be 32 bytes: {0}")] | ||
TryFromSlice(#[from] TryFromSliceError), | ||
#[cfg(any(test, feature = "unsafe"))] | ||
#[error("Could not get block number")] | ||
BlockNumber, | ||
} | ||
|
||
impl IntoResponse for AttestationErr { | ||
fn into_response(self) -> Response { | ||
tracing::error!("{:?}", format!("{self}")); | ||
let body = format!("{self}").into_bytes(); | ||
(StatusCode::INTERNAL_SERVER_ERROR, body).into_response() | ||
} | ||
} |
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,22 @@ | ||
// Copyright (C) 2023 Entropy Cryptography Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// 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 Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
//! Makes attestations that this program is running on TDX hardware | ||
pub mod api; | ||
pub mod errors; | ||
|
||
#[cfg(test)] | ||
mod tests; |
51 changes: 51 additions & 0 deletions
51
crates/threshold-signature-server/src/attestation/tests.rs
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,51 @@ | ||
// Copyright (C) 2023 Entropy Cryptography Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// 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 Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
use crate::helpers::tests::{initialize_test_logger, spawn_testing_validators}; | ||
use entropy_kvdb::clean_tests; | ||
use entropy_shared::QuoteInputData; | ||
use entropy_testing_utils::{ | ||
constants::{TSS_ACCOUNTS, X25519_PUBLIC_KEYS}, | ||
substrate_context::test_node_process_testing_state, | ||
}; | ||
use serial_test::serial; | ||
|
||
#[tokio::test] | ||
#[serial] | ||
async fn test_attest() { | ||
initialize_test_logger().await; | ||
clean_tests(); | ||
|
||
let _cxt = test_node_process_testing_state(false).await; | ||
let (_validator_ips, _validator_ids) = spawn_testing_validators(false).await; | ||
|
||
let nonce = [0; 32]; | ||
let client = reqwest::Client::new(); | ||
let res = client | ||
.post(format!("http://127.0.0.1:3001/attest")) | ||
.body(nonce.to_vec()) | ||
.send() | ||
.await | ||
.unwrap(); | ||
assert_eq!(res.status(), 200); | ||
let quote = res.bytes().await.unwrap(); | ||
|
||
// This internally verifies the signature in the quote | ||
let quote = tdx_quote::Quote::from_bytes("e).unwrap(); | ||
|
||
// Check the input data of the quote | ||
let expected_input_data = | ||
QuoteInputData::new(TSS_ACCOUNTS[0].0, X25519_PUBLIC_KEYS[0], nonce, 0); | ||
assert_eq!(quote.report_input_data(), expected_input_data.0); | ||
} |
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