@@ -30,14 +30,8 @@ contract KmsAuth is
3030 /// @custom:oz-renamed-from tproxyAppId
3131 string public gatewayAppId;
3232
33- // Struct to store App configuration
34- struct AppConfig {
35- bool isRegistered;
36- address controller;
37- }
38-
3933 // Mapping of registered apps
40- mapping (address => AppConfig ) public apps ;
34+ mapping (address => bool ) public registeredApps ;
4135
4236 // Mapping of allowed aggregated MR measurements for running KMS
4337 mapping (bytes32 => bool ) public kmsAllowedAggregatedMrs;
@@ -48,9 +42,6 @@ contract KmsAuth is
4842 // Mapping of allowed image measurements
4943 mapping (bytes32 => bool ) public allowedOsImages;
5044
51- // Sequence number for app IDs - per user
52- mapping (address => uint256 ) public nextAppSequence;
53-
5445 // AppAuth implementation contract address for factory deployment
5546 address public appAuthImplementation;
5647
@@ -65,7 +56,7 @@ contract KmsAuth is
6556 event OsImageHashRemoved (bytes32 osImageHash );
6657 event GatewayAppIdSet (string gatewayAppId );
6758 event AppAuthImplementationSet (address implementation );
68- event AppDeployedViaFactory (address indexed appId , address indexed proxyAddress , address indexed deployer );
59+ event AppDeployedViaFactory (address indexed appId , address indexed deployer );
6960
7061 /// @custom:oz-upgrades-unsafe-allow constructor
7162 constructor () {
@@ -77,7 +68,7 @@ contract KmsAuth is
7768 __Ownable_init (initialOwner);
7869 __UUPSUpgradeable_init ();
7970 __ERC165_init ();
80-
71+
8172 // Set AppAuth implementation if provided
8273 if (_appAuthImplementation != address (0 )) {
8374 appAuthImplementation = _appAuthImplementation;
@@ -130,32 +121,11 @@ contract KmsAuth is
130121 emit GatewayAppIdSet (appId);
131122 }
132123
133- // View next app id
134- function nextAppId () public view returns (address appId ) {
135- bytes32 fullHash = keccak256 (
136- abi.encodePacked (
137- address (this ),
138- msg .sender ,
139- nextAppSequence[msg .sender ]
140- )
141- );
142- return address (uint160 (uint256 (fullHash)));
143- }
144-
145- // Internal function to register an app with the given app ID and controller
146- function _registerAppInternal (address appId , address controller ) private {
147- require (! apps[appId].isRegistered, "App already registered " );
148- apps[appId].isRegistered = true ;
149- apps[appId].controller = controller;
150- nextAppSequence[msg .sender ]++ ;
151- emit AppRegistered (appId);
152- }
153-
154124 // Function to register an app
155- function registerApp (address controller ) external {
156- require (controller != address (0 ), "Invalid controller address " );
157- address appId = nextAppId () ;
158- _registerAppInternal (appId, controller );
125+ function registerApp (address appId ) public {
126+ require (appId != address (0 ), "Invalid app ID " );
127+ registeredApps[ appId] = true ;
128+ emit AppRegistered (appId);
159129 }
160130
161131 // Function to set AppAuth implementation contract address
@@ -172,30 +142,25 @@ contract KmsAuth is
172142 bool allowAnyDevice ,
173143 bytes32 initialDeviceId ,
174144 bytes32 initialComposeHash
175- ) external returns (address appId , address proxyAddress ) {
145+ ) external returns (address appId ) {
176146 require (appAuthImplementation != address (0 ), "AppAuth implementation not set " );
177147 require (initialOwner != address (0 ), "Invalid owner address " );
178-
179- // Calculate app ID
180- appId = nextAppId ();
181-
148+
182149 // Prepare initialization data
183150 bytes memory initData = abi.encodeWithSelector (
184- bytes4 (keccak256 ("initialize(address,address, bool,bool,bytes32,bytes32) " )),
151+ bytes4 (keccak256 ("initialize(address,bool,bool,bytes32,bytes32) " )),
185152 initialOwner,
186- appId,
187153 disableUpgrades,
188154 allowAnyDevice,
189155 initialDeviceId,
190156 initialComposeHash
191157 );
192-
158+
193159 // Deploy proxy contract
194- proxyAddress = address (new ERC1967Proxy (appAuthImplementation, initData));
195-
160+ appId = address (new ERC1967Proxy (appAuthImplementation, initData));
196161 // Register to KMS
197- _registerAppInternal (appId, proxyAddress );
198- emit AppDeployedViaFactory (appId, proxyAddress, msg .sender );
162+ registerApp (appId);
163+ emit AppDeployedViaFactory (appId, msg .sender );
199164 }
200165
201166 // Function to register an aggregated MR measurement
@@ -269,23 +234,31 @@ contract KmsAuth is
269234 AppBootInfo calldata bootInfo
270235 ) external view override returns (bool isAllowed , string memory reason ) {
271236 // Check if app is registered
272- if (! apps [bootInfo.appId].isRegistered ) {
237+ if (! registeredApps [bootInfo.appId]) {
273238 return (false , "App not registered " );
274239 }
275-
276- // Check aggregated MR and image measurements
240+ // Check if the OS image is allowed
277241 if (! allowedOsImages[bootInfo.osImageHash]) {
278242 return (false , "OS image is not allowed " );
279243 }
280244
281- // Ask the app controller if the app is allowed to boot
282- address controller = apps[bootInfo.appId].controller;
283- if (controller == address (0 )) {
284- return (false , "App controller not set " );
245+ // Check if the contract exists at the appId address
246+ if (! isContract (bootInfo.appId)) {
247+ return (false , "App not deployed or invalid address " );
285248 }
286- return IAppAuth (controller).isAppAllowed (bootInfo);
249+
250+ // Call the app's isAppAllowed function
251+ return IAppAuth (bootInfo.appId).isAppAllowed (bootInfo);
287252 }
288253
289254 // Add storage gap for upgradeable contracts
290255 uint256 [50 ] private __gap;
291256}
257+
258+ function isContract (address addr ) view returns (bool ){
259+ uint32 size;
260+ assembly {
261+ size := extcodesize (addr)
262+ }
263+ return (size > 0 );
264+ }
0 commit comments