Skip to content
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

DEV-3547: TCBLevelObj[] Read and Deserialization #9

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 45 additions & 18 deletions contracts/PCCSRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import {FmspcTcbDao} from "@automata-network/on-chain-pccs/bases/FmspcTcbDao.sol";
import {PcsDao} from "@automata-network/on-chain-pccs/bases/PcsDao.sol";
import {PckDao} from "@automata-network/on-chain-pccs/bases/PckDao.sol";
import {FmspcTcbHelper} from "@automata-network/on-chain-pccs/helpers/FmspcTcbHelper.sol";

/**
* @title Automata PCCS Router
Expand All @@ -31,10 +32,19 @@
address public override pckDaoAddr;
address public override pckHelperAddr;
address public override crlHelperAddr;
address public override fmspcTcbHelperAddr;

constructor(address _qeid, address _fmspcTcb, address _pcs, address _pck, address _pckHelper, address _crlHelper) {
constructor(
address _qeid,
address _fmspcTcb,
address _pcs,
address _pck,
address _x509,
address _x509Crl,
address _tcbHelper
) {
_initializeOwner(msg.sender);
_setConfig(_qeid, _fmspcTcb, _pcs, _pck, _pckHelper, _crlHelper);
_setConfig(_qeid, _fmspcTcb, _pcs, _pck, _x509, _x509Crl, _tcbHelper);

// allowing eth_call
_authorized[address(0)] = true;
Expand Down Expand Up @@ -73,30 +83,33 @@
}

function setConfig(
address _qeid,
address _fmspcTcb,
address _pcs,
address _qeid,
Dismissed Show dismissed Hide dismissed
address _fmspcTcb,
Dismissed Show dismissed Hide dismissed
address _pcs,
Dismissed Show dismissed Hide dismissed
address _pck,
address _pckHelper,
address _crlHelper
address _x509,
Dismissed Show dismissed Hide dismissed
address _x509Crl,
Dismissed Show dismissed Hide dismissed
address _tcbHelper
Dismissed Show dismissed Hide dismissed
) external onlyOwner {
_setConfig(_qeid, _fmspcTcb, _pcs, _pck, _pckHelper, _crlHelper);
_setConfig(_qeid, _fmspcTcb, _pcs, _pck, _x509, _x509Crl, _tcbHelper);
}

function _setConfig(
address _qeid,
address _fmspcTcb,
address _pcs,
address _qeid,
address _fmspcTcb,
address _pcs,
address _pck,
address _pckHelper,
address _crlHelper
address _x509,
address _x509Crl,
address _tcbHelper
) private {
qeIdDaoAddr = _qeid;
fmspcTcbDaoAddr = _fmspcTcb;
pcsDaoAddr = _pcs;
pckDaoAddr = _pck;
pckHelperAddr = _pckHelper;
crlHelperAddr = _crlHelper;
pckHelperAddr = _x509;
crlHelperAddr = _x509Crl;
fmspcTcbHelperAddr = _tcbHelper;
}

function getQeIdentity(EnclaveId id, uint256 quoteVersion)
Expand Down Expand Up @@ -130,7 +143,9 @@
bytes memory data = tcbDao.getAttestedData(key);
valid = data.length > 0;
if (valid) {
(tcbInfo, tcbLevelsV2,,) = abi.decode(data, (TcbInfoBasic, TCBLevelsObj[], string, bytes));
bytes memory encodedLevels;
(tcbInfo, encodedLevels,,) = abi.decode(data, (TcbInfoBasic, bytes, string, bytes));
tcbLevelsV2 = _decodeTcbLevels(encodedLevels);
} else {
revert FmspcTcbNotFound(TcbId.SGX, 2);
}
Expand All @@ -154,8 +169,10 @@
bytes memory data = tcbDao.getAttestedData(key);
valid = data.length > 0;
if (valid) {
(tcbInfo, tdxModule, tdxModuleIdentities, tcbLevelsV3,,) =
abi.decode(data, (TcbInfoBasic, TDXModule, TDXModuleIdentity[], TCBLevelsObj[], string, bytes));
bytes memory encodedLevels;
(tcbInfo, tdxModule, tdxModuleIdentities, encodedLevels,,) =
abi.decode(data, (TcbInfoBasic, TDXModule, TDXModuleIdentity[], bytes, string, bytes));
tcbLevelsV3 = _decodeTcbLevels(encodedLevels);
} else {
revert FmspcTcbNotFound(id, 3);
}
Expand Down Expand Up @@ -188,6 +205,16 @@
(success, hash) = _getPcsHash(ca, true);
}

function _decodeTcbLevels(bytes memory encodedTcbLevels) private view returns (TCBLevelsObj[] memory tcbLevels) {
FmspcTcbHelper fmspcTcbHelper = FmspcTcbHelper(fmspcTcbHelperAddr);
bytes[] memory encodedTcbLevelsArr = abi.decode(encodedTcbLevels, (bytes[]));
uint256 n = encodedTcbLevelsArr.length;
tcbLevels = new TCBLevelsObj[](n);
for (uint256 i = 0; i < n; i++) {
tcbLevels[i] = fmspcTcbHelper.tcbLevelsObjFromBytes(encodedTcbLevelsArr[i]);
}
}

function _getPcsAttestationData(CA ca, bool crl) private view returns (bool valid, bytes memory ret) {
PcsDao pcsDao = PcsDao(pcsDaoAddr);
ret = pcsDao.getAttestedData(pcsDao.PCS_KEY(ca, crl));
Expand Down
2 changes: 2 additions & 0 deletions contracts/interfaces/IPCCSRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ interface IPCCSRouter {

function crlHelperAddr() external view returns (address);

function fmspcTcbHelperAddr() external view returns (address);

function getQeIdentity(EnclaveId id, uint256 quoteVersion) external view returns (bool, IdentityObj memory);

function getFmspcTcbV2(bytes6 fmspc) external view returns (bool, TCBLevelsObj[] memory);
Expand Down
20 changes: 18 additions & 2 deletions forge-script/DeployRouter.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ contract DeployRouter is Script {
vm.startBroadcast(deployerKey);

PCCSRouter router =
new PCCSRouter(enclaveIdDaoAddr, tcbDaoAddr, pcsDaoAddr, pckDaoAddr, pckHelperAddr, crlHelperAddr);
new PCCSRouter(
enclaveIdDaoAddr,
tcbDaoAddr,
pcsDaoAddr,
pckDaoAddr,
pckHelperAddr,
crlHelperAddr,
tcbHelperAddr
);
console2.log("Deployed PCCSRouter to", address(router));

vm.stopBroadcast();
Expand All @@ -31,7 +39,15 @@ contract DeployRouter is Script {
vm.startBroadcast(deployerKey);

PCCSRouter router = PCCSRouter(vm.envAddress("PCCS_ROUTER"));
router.setConfig(enclaveIdDaoAddr, tcbDaoAddr, pcsDaoAddr, pckDaoAddr, pckHelperAddr, crlHelperAddr);
router.setConfig(
enclaveIdDaoAddr,
tcbDaoAddr,
pcsDaoAddr,
pckDaoAddr,
pckHelperAddr,
crlHelperAddr,
tcbHelperAddr
);

vm.stopBroadcast();
}
Expand Down
6 changes: 3 additions & 3 deletions forge-test/AutomataDcapAttestationFeeTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ contract AutomataDcapAttestationFeeTest is PCCSSetupBase, RiscZeroSetup {
// verify the quote
uint256 balanceBefore = user.balance;
uint256 expectedFee = GAS_PRICE_WEI * EXPECTED_GAS;
console.log("expected fee: ", expectedFee);
// console.log("expected fee: ", expectedFee);

vm.prank(user);
uint256 a = gasleft();
Expand All @@ -94,11 +94,11 @@ contract AutomataDcapAttestationFeeTest is PCCSSetupBase, RiscZeroSetup {
console.log(string(output));
} else {
uint256 gas = a - b;
console.log("gas: ", gas);
// console.log("gas: ", gas);

uint256 balanceAfter = user.balance;
uint256 paidFee = balanceBefore - balanceAfter;
console.log("paid fee: ", paidFee);
// console.log("paid fee: ", paidFee);
}

assertTrue(success);
Expand Down
5 changes: 3 additions & 2 deletions forge-test/utils/PCCSSetupBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {AutomataPcsDao} from "@automata-network/on-chain-pccs/automata_pccs/Auto
import {AutomataPckDao} from "@automata-network/on-chain-pccs/automata_pccs/AutomataPckDao.sol";
import {AutomataDaoStorage} from "@automata-network/on-chain-pccs/automata_pccs/shared/AutomataDaoStorage.sol";

import {PCCSRouter} from "../../contracts/PCCSRouter.sol";
import "../../contracts/PCCSRouter.sol";

abstract contract PCCSSetupBase is Test {
using JSONParserLib for JSONParserLib.Item;
Expand Down Expand Up @@ -96,7 +96,8 @@ abstract contract PCCSSetupBase is Test {
address(pcsDao),
address(pckDao),
address(x509),
address(x509Crl)
address(x509Crl),
address(tcbHelper)
);

// allow PCCS Router to read collaterals from the storage
Expand Down
2 changes: 1 addition & 1 deletion lib/automata-on-chain-pccs
Submodule automata-on-chain-pccs updated 95 files
+107 −1 README.md
+50 −0 broadcast/ConfigAutomataDao.s.sol/1/setAuthorizedCaller-latest.json
+56 −0 broadcast/ConfigAutomataDao.s.sol/10/setAuthorizedCaller-latest.json
+50 −0 broadcast/ConfigAutomataDao.s.sol/11155111/setAuthorizedCaller-latest.json
+20 −20 broadcast/ConfigAutomataDao.s.sol/11155111/updateStorageDao-latest.json
+56 −0 broadcast/ConfigAutomataDao.s.sol/11155420/setAuthorizedCaller-latest.json
+50 −0 broadcast/ConfigAutomataDao.s.sol/17000/setAuthorizedCaller-latest.json
+52 −0 broadcast/ConfigAutomataDao.s.sol/42161/setAuthorizedCaller-latest.json
+53 −0 broadcast/ConfigAutomataDao.s.sol/421614/setAuthorizedCaller-latest.json
+56 −0 broadcast/ConfigAutomataDao.s.sol/480/setAuthorizedCaller-latest.json
+56 −0 broadcast/ConfigAutomataDao.s.sol/4801/setAuthorizedCaller-latest.json
+56 −0 broadcast/ConfigAutomataDao.s.sol/65536/setAuthorizedCaller-latest.json
+56 −0 broadcast/ConfigAutomataDao.s.sol/8453/setAuthorizedCaller-latest.json
+56 −0 broadcast/ConfigAutomataDao.s.sol/84532/setAuthorizedCaller-latest.json
+261 −0 broadcast/DeployAutomataDao.s.sol/1/deployAll-latest.json
+297 −0 broadcast/DeployAutomataDao.s.sol/10/deployAll-latest.json
+61 −231 broadcast/DeployAutomataDao.s.sol/11155111/deployAll-latest.json
+52 −0 broadcast/DeployAutomataDao.s.sol/11155111/deployEnclaveIdDao-latest.json
+52 −0 broadcast/DeployAutomataDao.s.sol/11155111/deployFmspcTcbDao-latest.json
+52 −0 broadcast/DeployAutomataDao.s.sol/11155111/deployPck-latest.json
+297 −0 broadcast/DeployAutomataDao.s.sol/11155420/deployAll-latest.json
+133 −192 broadcast/DeployAutomataDao.s.sol/17000/deployAll-latest.json
+273 −0 broadcast/DeployAutomataDao.s.sol/42161/deployAll-latest.json
+279 −0 broadcast/DeployAutomataDao.s.sol/421614/deployAll-latest.json
+29 −0 broadcast/DeployAutomataDao.s.sol/421614/deployStorage-latest.json
+297 −0 broadcast/DeployAutomataDao.s.sol/480/deployAll-latest.json
+297 −0 broadcast/DeployAutomataDao.s.sol/4801/deployAll-latest.json
+135 −195 broadcast/DeployAutomataDao.s.sol/65536/deployAll-latest.json
+297 −0 broadcast/DeployAutomataDao.s.sol/8453/deployAll-latest.json
+297 −0 broadcast/DeployAutomataDao.s.sol/84532/deployAll-latest.json
+46 −0 broadcast/DeployHelpers.s.sol/1/deployEnclaveIdentityHelper-latest.json
+46 −0 broadcast/DeployHelpers.s.sol/1/deployFmspcTcbHelper-latest.json
+46 −0 broadcast/DeployHelpers.s.sol/1/deployPckHelper-latest.json
+46 −0 broadcast/DeployHelpers.s.sol/1/deployX509CrlHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/10/deployEnclaveIdentityHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/10/deployFmspcTcbHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/10/deployPckHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/10/deployX509CrlHelper-latest.json
+13 −13 broadcast/DeployHelpers.s.sol/11155111/deployEnclaveIdentityHelper-latest.json
+15 −15 broadcast/DeployHelpers.s.sol/11155111/deployFmspcTcbHelper-latest.json
+13 −13 broadcast/DeployHelpers.s.sol/11155111/deployPckHelper-latest.json
+13 −13 broadcast/DeployHelpers.s.sol/11155111/deployX509CrlHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/11155420/deployEnclaveIdentityHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/11155420/deployFmspcTcbHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/11155420/deployPckHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/11155420/deployX509CrlHelper-latest.json
+15 −15 broadcast/DeployHelpers.s.sol/17000/deployEnclaveIdentityHelper-latest.json
+17 −17 broadcast/DeployHelpers.s.sol/17000/deployFmspcTcbHelper-latest.json
+15 −15 broadcast/DeployHelpers.s.sol/17000/deployPckHelper-latest.json
+15 −15 broadcast/DeployHelpers.s.sol/17000/deployX509CrlHelper-latest.json
+48 −0 broadcast/DeployHelpers.s.sol/42161/deployEnclaveIdentityHelper-latest.json
+48 −0 broadcast/DeployHelpers.s.sol/42161/deployFmspcTcbHelper-latest.json
+48 −0 broadcast/DeployHelpers.s.sol/42161/deployPckHelper-latest.json
+48 −0 broadcast/DeployHelpers.s.sol/42161/deployX509CrlHelper-latest.json
+49 −0 broadcast/DeployHelpers.s.sol/421614/deployEnclaveIdentityHelper-latest.json
+49 −0 broadcast/DeployHelpers.s.sol/421614/deployFmspcTcbHelper-latest.json
+49 −0 broadcast/DeployHelpers.s.sol/421614/deployPckHelper-latest.json
+49 −0 broadcast/DeployHelpers.s.sol/421614/deployX509CrlHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/480/deployEnclaveIdentityHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/480/deployFmspcTcbHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/480/deployPckHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/480/deployX509CrlHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/4801/deployEnclaveIdentityHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/4801/deployFmspcTcbHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/4801/deployPckHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/4801/deployX509CrlHelper-latest.json
+18 −18 broadcast/DeployHelpers.s.sol/65536/deployEnclaveIdentityHelper-latest.json
+18 −18 broadcast/DeployHelpers.s.sol/65536/deployFmspcTcbHelper-latest.json
+18 −18 broadcast/DeployHelpers.s.sol/65536/deployPckHelper-latest.json
+18 −18 broadcast/DeployHelpers.s.sol/65536/deployX509CrlHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/8453/deployEnclaveIdentityHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/8453/deployFmspcTcbHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/8453/deployPckHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/8453/deployX509CrlHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/84532/deployEnclaveIdentityHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/84532/deployFmspcTcbHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/84532/deployPckHelper-latest.json
+52 −0 broadcast/DeployHelpers.s.sol/84532/deployX509CrlHelper-latest.json
+14 −0 env/.arbitrum-one.env.example
+14 −0 env/.arbitrum-sepolia.env.example
+14 −0 env/.base-mainnet.env.example
+14 −0 env/.base-sepolia.env.example
+14 −0 env/.ethereum-mainnet.env.example
+14 −0 env/.holesky.env.example
+17 −0 env/.mainnet.env.example
+14 −0 env/.optimism-mainnet.env.example
+14 −0 env/.optimism-sepolia.env.example
+14 −0 env/.sepolia.env.example
+14 −0 env/.world-mainnet.env.example
+14 −0 env/.worldchain-sepolia.env.example
+15 −0 foundry.toml
+0 −12 script/helper/deploy.sh
+27 −9 src/bases/FmspcTcbDao.sol
+74 −0 src/helpers/FmspcTcbHelper.sol
+30 −0 test/tcb/TCBHelperTest.t.sol
Loading