1 /****************************************************************************** 2 * 3 * Copyright 2021,2024 NXP 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 #ifndef _SBACCESSCONTROLLER_H_ 20 #define _SBACCESSCONTROLLER_H_ 21 #include <IntervalTimer.h> 22 #include <vector> 23 24 #define EARLY_BOOT_ENDED_CMD (0x35) // INS Received from VOLD when earlyboot state ends 25 #define INS_SEND_ROT_DATA_CMD (0x4F) // Google defined RoT cmd 26 #define BEGIN_OPERATION_CMD (0x30) // begin() 27 #define FINISH_OPERATION_CMD (0x32) // finish() 28 #define ABORT_OPERATION_CMD (0x33) // abort() 29 30 // Session timeout values during Applet upgrade 31 #define SMALLEST_SESSION_TIMEOUT (0) // 0 msec, during actual upgrade process 32 #define UPGRADE_SESSION_TIMEOUT (5 * 100) // 500 msecs, teared scenario 33 34 #define SB_ACCESS_BLOCK_TIMER (40 * 1000) // 40 secs,Block access to SB applet during upgrade 35 36 // Other Session timeout 37 #define REGULAR_SESSION_TIMEOUT (3 * 1000) // 3 secs,default value 38 #define CRYPTO_OP_SESSION_TIMEOUT (20 * 1000) // 20 sec,for begin() operation 39 40 enum BOOTSTATE { 41 SB_EARLY_BOOT = 0, 42 SB_EARLY_BOOT_ENDED, 43 }; 44 45 enum OPERATION_STATE { 46 OP_STARTED = 0, 47 OP_FINISHED, 48 }; 49 50 namespace keymint::javacard { 51 class SBAccessController { 52 public: 53 /** 54 * Controls Applet selection 55 * 1) Not allowed when actual upgrade is in progress for 40 secs 56 * 2) Only allowed for allow listed cmds during early boot in upgrade teared case 57 * 3) Allowed in all other cases 58 * Params : void 59 * Returns : true if Applet select is allowed else false 60 */ 61 bool isSelectAllowed(); 62 63 /** 64 * Parses SELECT cmd response to record if Applet upgrade is in progress 65 * Params : R-APDU to SELECT cmd 66 * Returns : void 67 */ 68 void parseResponse(std::vector<uint8_t>& responseApdu); 69 70 /** 71 * Sets the state of crypto operation 72 * Params : crypto operation start/finish 73 * Returns : void 74 */ 75 void setCryptoOperationState(uint8_t opState); 76 77 /** 78 * Determines if current INS is allowed 79 * Params : one bytes INS value 80 * Returns : true if cmd is allowed else false 81 */ 82 bool isOperationAllowed(uint8_t cmdIns); 83 84 /** 85 * Provides session timeout value for Logical channel mgmt 86 * 1) UPGRADE_SESSION_TIMEOUT for upgrade teared scenario during early boot 87 * 2) SMALLEST_SESSION_TIMEOUT during actual upgrade process 88 * 3) CRYPTO_OP_SESSION_TIMEOUT for crypto begin() 89 * 4) REGULAR_SESSION_TIMEOUT for all other operations 90 * Params : void 91 * Returns : Session timeout value in ms 92 */ 93 int getSessionTimeout(); 94 /** 95 * Helper function to check if all allowed cmds 96 * are received to mark mBootState as BOOT_ENDED 97 * Params: void 98 * Returns: void 99 */ 100 void updateBootState(); 101 102 /** 103 * Helper function to get singleton instance 104 * Params: void 105 * Returns: Instance of SBAccessController 106 */ 107 static SBAccessController& getInstance(); 108 SBAccessController(const SBAccessController&) = delete; 109 110 private: 111 // mark constructor private SBAccessController()112 SBAccessController() : mIsUpdateInProgress(false), mBootState(SB_EARLY_BOOT) {} 113 bool mIsUpdateInProgress; // stores Applet upgrade state 114 BOOTSTATE mBootState; 115 116 IntervalTimer mTimer; // track Applet upgrade progress 117 IntervalTimer mTimerCrypto; // track crypto operations 118 void startTimer(bool isStart, IntervalTimer& t, int timeout, 119 void (*timerFunc)(union sigval arg)); 120 }; 121 } // namespace keymint::javacard 122 #endif /* _SBACCESSCONTROLLER_H_ */ 123