Skip to content

Commit 31d3cab

Browse files
authored
Merge pull request #130 from Dstack-TEE/refactor-contracts
Refactor contracts
2 parents 2dfe3d0 + 98dd0ab commit 31d3cab

File tree

18 files changed

+899
-1679
lines changed

18 files changed

+899
-1679
lines changed

kms/auth-eth/.openzeppelin/unknown-2035.json

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

kms/auth-eth/contracts/AppAuth.sol

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,19 @@ contract AppAuth is
2121
// State variable to track if upgrades are disabled
2222
bool private _upgradesDisabled;
2323

24+
// Whether allow any device to boot this app or only allow devices
25+
bool public allowAnyDevice;
26+
27+
// Mapping of allowed device IDs for this app
28+
mapping(bytes32 => bool) public allowedDeviceIds;
29+
2430
// Events
2531
event ComposeHashAdded(bytes32 composeHash);
2632
event ComposeHashRemoved(bytes32 composeHash);
2733
event UpgradesDisabled();
34+
event DeviceAdded(bytes32 deviceId);
35+
event DeviceRemoved(bytes32 deviceId);
36+
event AllowAnyDeviceSet(bool allowAny);
2837

2938
/// @custom:oz-upgrades-unsafe-allow constructor
3039
constructor() {
@@ -35,12 +44,14 @@ contract AppAuth is
3544
function initialize(
3645
address initialOwner,
3746
address _appId,
38-
bool _disableUpgrades
47+
bool _disableUpgrades,
48+
bool _allowAnyDevice
3949
) public initializer {
4050
require(initialOwner != address(0), "Invalid owner address");
4151
require(_appId != address(0), "Invalid app ID");
4252
appId = _appId;
4353
_upgradesDisabled = _disableUpgrades;
54+
allowAnyDevice = _allowAnyDevice;
4455
__Ownable_init(initialOwner);
4556
__UUPSUpgradeable_init();
4657
}
@@ -62,6 +73,24 @@ contract AppAuth is
6273
emit ComposeHashRemoved(composeHash);
6374
}
6475

76+
// Set whether any device is allowed to boot this app
77+
function setAllowAnyDevice(bool _allowAnyDevice) external onlyOwner {
78+
allowAnyDevice = _allowAnyDevice;
79+
emit AllowAnyDeviceSet(_allowAnyDevice);
80+
}
81+
82+
// Add a device ID to allowed list
83+
function addDevice(bytes32 deviceId) external onlyOwner {
84+
allowedDeviceIds[deviceId] = true;
85+
emit DeviceAdded(deviceId);
86+
}
87+
88+
// Remove a device ID from allowed list
89+
function removeDevice(bytes32 deviceId) external onlyOwner {
90+
allowedDeviceIds[deviceId] = false;
91+
emit DeviceRemoved(deviceId);
92+
}
93+
6594
// Check if an app is allowed to boot
6695
function isAppAllowed(
6796
IAppAuth.AppBootInfo calldata bootInfo
@@ -76,6 +105,11 @@ contract AppAuth is
76105
return (false, "Compose hash not allowed");
77106
}
78107

108+
// Check if device is allowed (when device restriction is enabled)
109+
if (!allowAnyDevice && !allowedDeviceIds[bootInfo.deviceId]) {
110+
return (false, "Device not allowed");
111+
}
112+
79113
return (true, "");
80114
}
81115

kms/auth-eth/contracts/IAppAuth.sol

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ interface IAppAuth {
88
address instanceId;
99
bytes32 deviceId;
1010
bytes32 mrAggregated;
11+
bytes32 mrSystem;
1112
bytes32 mrImage;
1213
}
1314

14-
function isAppAllowed(AppBootInfo calldata bootInfo) external view returns (bool isAllowed, string memory reason);
15+
function isAppAllowed(
16+
AppBootInfo calldata bootInfo
17+
) external view returns (bool isAllowed, string memory reason);
1518
}

kms/auth-eth/contracts/KmsAuth.sol

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,32 @@ contract KmsAuth is
3535
// Mapping of registered apps
3636
mapping(address => AppConfig) public apps;
3737

38-
// Mapping of allowed aggregated MR measurements
39-
mapping(bytes32 => bool) public allowedAggregatedMrs;
38+
// Mapping of allowed aggregated MR measurements for running KMS
39+
mapping(bytes32 => bool) public kmsAllowedAggregatedMrs;
40+
41+
// Mapping of allowed KMS device IDs
42+
mapping(bytes32 => bool) public kmsAllowedDeviceIds;
4043

4144
// Mapping of allowed image measurements
42-
mapping(bytes32 => bool) public allowedImages;
45+
mapping(bytes32 => bool) public appAllowedImages;
4346

4447
// Mapping of allowed KMS compose hashes
45-
mapping(bytes32 => bool) public allowedKmsComposeHashes;
46-
47-
// Mapping of allowed KMS device IDs
48-
mapping(bytes32 => bool) public allowedKmsDeviceIds;
48+
mapping(bytes32 => bool) public appAllowedSystemMrs;
4949

5050
// Sequence number for app IDs - per user
5151
mapping(address => uint256) public nextAppSequence;
5252

5353
// Events
5454
event AppRegistered(address appId);
5555
event KmsInfoSet(bytes k256Pubkey);
56-
event AggregatedMrRegistered(bytes32 mrAggregated);
57-
event AggregatedMrDeregistered(bytes32 mrAggregated);
58-
event ImageRegistered(bytes32 mrImage);
59-
event ImageDeregistered(bytes32 mrImage);
60-
event KmsComposeHashRegistered(bytes32 composeHash);
61-
event KmsComposeHashDeregistered(bytes32 composeHash);
62-
event KmsDeviceIdRegistered(bytes32 deviceId);
63-
event KmsDeviceIdDeregistered(bytes32 deviceId);
56+
event KmsAggregatedMrAdded(bytes32 mrAggregated);
57+
event KmsAggregatedMrRemoved(bytes32 mrAggregated);
58+
event KmsDeviceAdded(bytes32 deviceId);
59+
event KmsDeviceRemoved(bytes32 deviceId);
60+
event AppImageMrAdded(bytes32 mrImage);
61+
event AppImageMrRemoved(bytes32 mrImage);
62+
event AppSystemMrAdded(bytes32 mrSystem);
63+
event AppSystemMrRemoved(bytes32 mrSystem);
6464
event TproxyAppIdSet(string tproxyAppId);
6565

6666
/// @custom:oz-upgrades-unsafe-allow constructor
@@ -104,7 +104,11 @@ contract KmsAuth is
104104
// View next app id
105105
function nextAppId() public view returns (address appId) {
106106
bytes32 fullHash = keccak256(
107-
abi.encodePacked(address(this), msg.sender, nextAppSequence[msg.sender])
107+
abi.encodePacked(
108+
address(this),
109+
msg.sender,
110+
nextAppSequence[msg.sender]
111+
)
108112
);
109113
return address(uint160(uint256(fullHash)));
110114
}
@@ -121,69 +125,66 @@ contract KmsAuth is
121125
}
122126

123127
// Function to register an aggregated MR measurement
124-
function registerAggregatedMr(bytes32 mrAggregated) external onlyOwner {
125-
allowedAggregatedMrs[mrAggregated] = true;
126-
emit AggregatedMrRegistered(mrAggregated);
128+
function addKmsAggregatedMr(bytes32 mrAggregated) external onlyOwner {
129+
kmsAllowedAggregatedMrs[mrAggregated] = true;
130+
emit KmsAggregatedMrAdded(mrAggregated);
127131
}
128132

129133
// Function to deregister an aggregated MR measurement
130-
function deregisterAggregatedMr(bytes32 mrAggregated) external onlyOwner {
131-
allowedAggregatedMrs[mrAggregated] = false;
132-
emit AggregatedMrDeregistered(mrAggregated);
134+
function removeKmsAggregatedMr(
135+
bytes32 mrAggregated
136+
) external onlyOwner {
137+
kmsAllowedAggregatedMrs[mrAggregated] = false;
138+
emit KmsAggregatedMrRemoved(mrAggregated);
133139
}
134140

135-
// Function to register an image measurement
136-
function registerImage(bytes32 mrImage) external onlyOwner {
137-
allowedImages[mrImage] = true;
138-
emit ImageRegistered(mrImage);
141+
// Function to register a KMS device ID
142+
function addKmsDevice(bytes32 deviceId) external onlyOwner {
143+
kmsAllowedDeviceIds[deviceId] = true;
144+
emit KmsDeviceAdded(deviceId);
139145
}
140146

141-
// Function to deregister an image measurement
142-
function deregisterImage(bytes32 mrImage) external onlyOwner {
143-
allowedImages[mrImage] = false;
144-
emit ImageDeregistered(mrImage);
147+
// Function to deregister a KMS device ID
148+
function removeKmsDevice(bytes32 deviceId) external onlyOwner {
149+
kmsAllowedDeviceIds[deviceId] = false;
150+
emit KmsDeviceRemoved(deviceId);
145151
}
146152

147-
// Function to register a KMS compose hash
148-
function registerKmsComposeHash(bytes32 composeHash) external onlyOwner {
149-
allowedKmsComposeHashes[composeHash] = true;
150-
emit KmsComposeHashRegistered(composeHash);
153+
// Function to register an image measurement
154+
function addAppImageMr(bytes32 mrImage) external onlyOwner {
155+
appAllowedImages[mrImage] = true;
156+
emit AppImageMrAdded(mrImage);
151157
}
152158

153-
// Function to deregister a KMS compose hash
154-
function deregisterKmsComposeHash(bytes32 composeHash) external onlyOwner {
155-
allowedKmsComposeHashes[composeHash] = false;
156-
emit KmsComposeHashDeregistered(composeHash);
159+
// Function to deregister an image measurement
160+
function removeAppImageMr(bytes32 mrImage) external onlyOwner {
161+
appAllowedImages[mrImage] = false;
162+
emit AppImageMrRemoved(mrImage);
157163
}
158164

159-
// Function to register a KMS device ID
160-
function registerKmsDeviceId(bytes32 deviceId) external onlyOwner {
161-
allowedKmsDeviceIds[deviceId] = true;
162-
emit KmsDeviceIdRegistered(deviceId);
165+
// Function to register a system MR measurement
166+
function addAppSystemMr(bytes32 mrSystem) external onlyOwner {
167+
appAllowedSystemMrs[mrSystem] = true;
168+
emit AppSystemMrAdded(mrSystem);
163169
}
164170

165-
// Function to deregister a KMS device ID
166-
function deregisterKmsDeviceId(bytes32 deviceId) external onlyOwner {
167-
allowedKmsDeviceIds[deviceId] = false;
168-
emit KmsDeviceIdDeregistered(deviceId);
171+
// Function to deregister a system MR measurement
172+
function removeAppSystemMr(bytes32 mrSystem) external onlyOwner {
173+
appAllowedSystemMrs[mrSystem] = false;
174+
emit AppSystemMrRemoved(mrSystem);
169175
}
170176

171177
// Function to check if KMS is allowed to boot
172178
function isKmsAllowed(
173179
AppBootInfo calldata bootInfo
174180
) external view returns (bool isAllowed, string memory reason) {
175181
// Check if the aggregated MR is allowed
176-
if (!allowedAggregatedMrs[bootInfo.mrAggregated]) {
182+
if (!kmsAllowedAggregatedMrs[bootInfo.mrAggregated]) {
177183
return (false, "Aggregated MR not allowed");
178184
}
179185

180-
// Check if the KMS compose hash is allowed
181-
if (!allowedKmsComposeHashes[bootInfo.composeHash]) {
182-
return (false, "KMS compose hash not allowed");
183-
}
184-
185186
// Check if the KMS device ID is allowed
186-
if (!allowedKmsDeviceIds[bootInfo.deviceId]) {
187+
if (!kmsAllowedDeviceIds[bootInfo.deviceId]) {
187188
return (false, "KMS is not allowed to boot on this device");
188189
}
189190

@@ -201,10 +202,10 @@ contract KmsAuth is
201202

202203
// Check aggregated MR and image measurements
203204
if (
204-
!allowedAggregatedMrs[bootInfo.mrAggregated] &&
205-
!allowedImages[bootInfo.mrImage]
205+
!appAllowedSystemMrs[bootInfo.mrSystem] &&
206+
!appAllowedImages[bootInfo.mrImage]
206207
) {
207-
return (false, "Neither aggregated MR nor image is allowed");
208+
return (false, "Neither system MR nor image is allowed");
208209
}
209210

210211
// Ask the app controller if the app is allowed to boot
@@ -216,5 +217,5 @@ contract KmsAuth is
216217
}
217218

218219
// Add storage gap for upgradeable contracts
219-
uint256[49] private __gap;
220+
uint256[50] private __gap;
220221
}

0 commit comments

Comments
 (0)