1 /******************************************************************************
2  *
3  *  Copyright 2020 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 #pragma once
20 #include <android-base/logging.h>
21 #include <android-base/stringprintf.h>
22 #include <hardware/hardware.h>
23 #include <hidl/MQDescriptor.h>
24 #include <hidl/Status.h>
25 #include <string.h>
26 #include "phNxpEse_Api.h"
27 
28 using ::android::hardware::hidl_vec;
29 #define OSU_PROP_CLA 0x80
30 #define OSU_PROP_INS 0xDF
31 #define OSU_PROP_RST_P1 0xEF
32 
33 #define ISO7816_BASIC_CHANNEL 0x00
34 #define ISO7816_CLA_OFFSET 0
35 #define ISO7816_INS_OFFSET 1
36 #define ISO7816_P1_OFFSET 2
37 #define ISO7816_P2_OFFSET 3
38 #define ISO7816_LC_OFFSET 4
39 #define ISO7816_EXTENDED_LC_VALUE 0
40 #define ISO7816_SHORT_APDU_HEADER 5
41 #define ISO7816_EXTENDED_APDU_HEADER 7
42 #define ISO7816_CLA_CHN_MASK 0x03
43 #define IS_OSU_MODE(...) (OsuHalExtn::getInstance().isOsuMode(__VA_ARGS__))
44 
45 class OsuHalExtn {
46  public:
47   typedef enum {
48     NON_OSU_MODE,      // Normal Mode  : All HAL APIs allowed
49     OSU_PROP_MODE,     // OSU mode     : Only OSU JAR command allowed
50     OSU_GP_MODE,       // OSU Mode     : GP command in the OSU mode
51     OSU_RST_MODE,      // OSU mode     : Proprietary HARD reset
52     OSU_BLOCKED_MODE,  // OSU mode     : Command not allowed return error
53   } OsuApduMode;
54 
55   typedef enum {
56     INIT,
57     OPENBASIC,
58     OPENLOGICAL,
59     TRANSMIT,
60     CLOSE,
61     RESET,
62   } SecureElementAPI;
63 
64   static OsuHalExtn& getInstance();
65 
66   OsuApduMode isOsuMode(const hidl_vec<uint8_t>& evt, uint8_t type,
67                         phNxpEse_data* pCmdData = nullptr);
68   bool isOsuMode(uint8_t type, uint8_t channel = 0xFF);
69   virtual ~OsuHalExtn();
70   OsuHalExtn() noexcept;
71   void checkAndUpdateOsuMode();
72   unsigned long int getOSUMaxWtxCount();
73 
74  private:
75   bool isAppOSUMode;
76   bool isJcopOSUMode;
77   static const hidl_vec<uint8_t> osu_aid[10];
78   OsuApduMode checkTransmit(uint8_t* input, size_t length, uint32_t* outLength);
79   bool isOsuMode();
80 };
81