|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.25; |
| 3 | + |
| 4 | +import {EnclaveReport} from "@automata-network/dcap-attestation/contracts/types/V3Structs.sol"; |
| 5 | +import {BELE} from "@automata-network/dcap-attestation/contracts/utils/BELE.sol"; |
| 6 | +import {Header} from "@automata-network/dcap-attestation/contracts/types/CommonStruct.sol"; |
| 7 | +import { |
| 8 | + IQuoteVerifier |
| 9 | +} from "@automata-network/dcap-attestation/contracts/interfaces/IQuoteVerifier.sol"; |
| 10 | +import { |
| 11 | + HEADER_LENGTH, |
| 12 | + ENCLAVE_REPORT_LENGTH |
| 13 | +} from "@automata-network/dcap-attestation/contracts/types/Constants.sol"; |
| 14 | +import {EnclaveReport} from "@automata-network/dcap-attestation/contracts/types/V3Structs.sol"; |
| 15 | +import {BytesUtils} from "@automata-network/dcap-attestation/contracts/utils/BytesUtils.sol"; |
| 16 | +import {IEspressoTEEVerifier} from "../bridge/IEspressoTEEVerifier.sol"; |
| 17 | +import { |
| 18 | + V3QuoteVerifier |
| 19 | +} from "@automata-network/dcap-attestation/contracts/verifiers/V3QuoteVerifier.sol"; |
| 20 | +import "@openzeppelin/contracts/access/Ownable2Step.sol"; |
| 21 | + |
| 22 | +/** |
| 23 | + * |
| 24 | + * @title Verifies quotes from the TEE and attests on-chain |
| 25 | + * @notice Contains the logic to verify a quote from the TEE and attest on-chain. It uses the V3QuoteVerifier contract |
| 26 | + * to verify the quote. Along with some additional verification logic. |
| 27 | + */ |
| 28 | + |
| 29 | +contract EspressoTEEVerifierBlobsMock is IEspressoTEEVerifier { |
| 30 | + using BytesUtils for bytes; |
| 31 | + |
| 32 | + // V3QuoteVerififer contract from automata to verify the quote |
| 33 | + V3QuoteVerifier public quoteVerifier; |
| 34 | + |
| 35 | + mapping(bytes32 => bool) public mrEnclaves; |
| 36 | + mapping(bytes32 => bool) public mrSigners; |
| 37 | + |
| 38 | + constructor() {} |
| 39 | + |
| 40 | + function verify(bytes calldata rawQuote, bytes32 reportDataHash) external view { |
| 41 | + // Parse the header |
| 42 | + Header memory header = parseQuoteHeader(rawQuote); |
| 43 | + |
| 44 | + // Currently only version 3 is supported |
| 45 | + if (header.version != 3) { |
| 46 | + revert InvalidHeaderVersion(); |
| 47 | + } |
| 48 | + |
| 49 | + // Parse enclave quote |
| 50 | + uint256 lastIndex = HEADER_LENGTH + ENCLAVE_REPORT_LENGTH; |
| 51 | + EnclaveReport memory localReport; |
| 52 | + bool success; |
| 53 | + (success, localReport) = parseEnclaveReport(rawQuote[HEADER_LENGTH:lastIndex]); |
| 54 | + if (!success) { |
| 55 | + revert FailedToParseEnclaveReport(); |
| 56 | + } |
| 57 | + |
| 58 | + // Verify that the reportDataHash if the hash signed by the TEE |
| 59 | + // We do not check the signature because `quoteVerifier.verifyQuote` already does that |
| 60 | + if (reportDataHash != bytes32(localReport.reportData.substring(0, 32))) { |
| 61 | + revert InvalidReportDataHash(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /* |
| 66 | + @notice Parses the enclave report from the quote |
| 67 | + @param rawEnclaveReport The raw enclave report from the quote in bytes |
| 68 | + @return success True if the enclave report was parsed successfully |
| 69 | + @return enclaveReport The parsed enclave report |
| 70 | + */ |
| 71 | + function parseEnclaveReport( |
| 72 | + bytes memory rawEnclaveReport |
| 73 | + ) public pure returns (bool success, EnclaveReport memory enclaveReport) { |
| 74 | + if (rawEnclaveReport.length != ENCLAVE_REPORT_LENGTH) { |
| 75 | + return (false, enclaveReport); |
| 76 | + } |
| 77 | + enclaveReport.cpuSvn = bytes16(rawEnclaveReport.substring(0, 16)); |
| 78 | + enclaveReport.miscSelect = bytes4(rawEnclaveReport.substring(16, 4)); |
| 79 | + enclaveReport.reserved1 = bytes28(rawEnclaveReport.substring(20, 28)); |
| 80 | + enclaveReport.attributes = bytes16(rawEnclaveReport.substring(48, 16)); |
| 81 | + enclaveReport.mrEnclave = bytes32(rawEnclaveReport.substring(64, 32)); |
| 82 | + enclaveReport.reserved2 = bytes32(rawEnclaveReport.substring(96, 32)); |
| 83 | + enclaveReport.mrSigner = bytes32(rawEnclaveReport.substring(128, 32)); |
| 84 | + enclaveReport.reserved3 = rawEnclaveReport.substring(160, 96); |
| 85 | + enclaveReport.isvProdId = uint16(BELE.leBytesToBeUint(rawEnclaveReport.substring(256, 2))); |
| 86 | + enclaveReport.isvSvn = uint16(BELE.leBytesToBeUint(rawEnclaveReport.substring(258, 2))); |
| 87 | + enclaveReport.reserved4 = rawEnclaveReport.substring(260, 60); |
| 88 | + enclaveReport.reportData = rawEnclaveReport.substring(320, 64); |
| 89 | + success = true; |
| 90 | + } |
| 91 | + |
| 92 | + function parseQuoteHeader(bytes calldata rawQuote) public pure returns (Header memory header) { |
| 93 | + header = Header({ |
| 94 | + version: uint16(BELE.leBytesToBeUint(rawQuote[0:2])), |
| 95 | + attestationKeyType: bytes2(rawQuote[2:4]), |
| 96 | + teeType: bytes4(uint32(BELE.leBytesToBeUint(rawQuote[4:8]))), |
| 97 | + qeSvn: bytes2(rawQuote[8:10]), |
| 98 | + pceSvn: bytes2(rawQuote[10:12]), |
| 99 | + qeVendorId: bytes16(rawQuote[12:28]), |
| 100 | + userData: bytes20(rawQuote[28:48]) |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + /* |
| 105 | + * @dev Set the mrEnclave of the contract |
| 106 | + */ |
| 107 | + function setMrEnclave(bytes32 _mrEnclave, bool _isValid) external { |
| 108 | + if (_isValid) { |
| 109 | + mrEnclaves[_mrEnclave] = true; |
| 110 | + } else { |
| 111 | + delete mrEnclaves[_mrEnclave]; |
| 112 | + } |
| 113 | + emit MREnclaveSet(_mrEnclave, _isValid); |
| 114 | + } |
| 115 | + |
| 116 | + /* |
| 117 | + * @dev Set the mrSigner of the contract |
| 118 | + */ |
| 119 | + function setMrSigner(bytes32 _mrSigner, bool _isValid) external { |
| 120 | + if (_isValid) { |
| 121 | + mrSigners[_mrSigner] = true; |
| 122 | + } else { |
| 123 | + delete mrSigners[_mrSigner]; |
| 124 | + } |
| 125 | + emit MRSignerSet(_mrSigner, _isValid); |
| 126 | + } |
| 127 | +} |
0 commit comments