Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.nfc.cardemulation; |
| 18 | |
Mathew Inwood | 1961e1e | 2018-07-31 16:04:15 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.pm.PackageManager; |
Ruchi Kandoi | 44bb579 | 2018-10-19 13:08:34 -0700 | [diff] [blame] | 22 | import android.content.pm.PackageManager.NameNotFoundException; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 23 | import android.content.pm.ResolveInfo; |
| 24 | import android.content.pm.ServiceInfo; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 25 | import android.content.res.Resources; |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 26 | import android.content.res.Resources.NotFoundException; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 27 | import android.content.res.TypedArray; |
| 28 | import android.content.res.XmlResourceParser; |
| 29 | import android.graphics.drawable.Drawable; |
| 30 | import android.os.Parcel; |
| 31 | import android.os.Parcelable; |
| 32 | import android.util.AttributeSet; |
| 33 | import android.util.Log; |
| 34 | import android.util.Xml; |
| 35 | |
| 36 | import org.xmlpull.v1.XmlPullParser; |
| 37 | import org.xmlpull.v1.XmlPullParserException; |
| 38 | |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 39 | import java.io.FileDescriptor; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 40 | import java.io.IOException; |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 41 | import java.io.PrintWriter; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 42 | import java.util.ArrayList; |
| 43 | import java.util.HashMap; |
Martijn Coenen | ed3a29e | 2015-01-23 11:29:54 -0800 | [diff] [blame] | 44 | import java.util.List; |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 45 | import java.util.Map; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * @hide |
| 49 | */ |
| 50 | public final class ApduServiceInfo implements Parcelable { |
| 51 | static final String TAG = "ApduServiceInfo"; |
| 52 | |
| 53 | /** |
| 54 | * The service that implements this |
| 55 | */ |
Mathew Inwood | 1961e1e | 2018-07-31 16:04:15 +0100 | [diff] [blame] | 56 | @UnsupportedAppUsage |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 57 | final ResolveInfo mService; |
| 58 | |
| 59 | /** |
| 60 | * Description of the service |
| 61 | */ |
| 62 | final String mDescription; |
| 63 | |
| 64 | /** |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 65 | * Whether this service represents AIDs running on the host CPU |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 66 | */ |
| 67 | final boolean mOnHost; |
| 68 | |
| 69 | /** |
Ruchi Kandoi | 44bb579 | 2018-10-19 13:08:34 -0700 | [diff] [blame] | 70 | * Offhost reader name. |
| 71 | * eg: SIM, eSE etc |
| 72 | */ |
| 73 | String mOffHostName; |
| 74 | |
| 75 | /** |
| 76 | * Offhost reader name from manifest file. |
| 77 | * Used for unsetOffHostSecureElement() |
| 78 | */ |
| 79 | final String mStaticOffHostName; |
| 80 | |
| 81 | /** |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 82 | * Mapping from category to static AID group |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 83 | */ |
Mathew Inwood | 1961e1e | 2018-07-31 16:04:15 +0100 | [diff] [blame] | 84 | @UnsupportedAppUsage |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 85 | final HashMap<String, AidGroup> mStaticAidGroups; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 88 | * Mapping from category to dynamic AID group |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 89 | */ |
Mathew Inwood | 1961e1e | 2018-07-31 16:04:15 +0100 | [diff] [blame] | 90 | @UnsupportedAppUsage |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 91 | final HashMap<String, AidGroup> mDynamicAidGroups; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
Martijn Coenen | c3f0044 | 2013-08-28 19:23:41 -0700 | [diff] [blame] | 94 | * Whether this service should only be started when the device is unlocked. |
| 95 | */ |
| 96 | final boolean mRequiresDeviceUnlock; |
| 97 | |
| 98 | /** |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 99 | * The id of the service banner specified in XML. |
| 100 | */ |
| 101 | final int mBannerResourceId; |
| 102 | |
| 103 | /** |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 104 | * The uid of the package the service belongs to |
| 105 | */ |
| 106 | final int mUid; |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 107 | |
| 108 | /** |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 109 | * Settings Activity for this service |
| 110 | */ |
| 111 | final String mSettingsActivityName; |
| 112 | |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 113 | /** |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 114 | * @hide |
| 115 | */ |
Mathew Inwood | 1961e1e | 2018-07-31 16:04:15 +0100 | [diff] [blame] | 116 | @UnsupportedAppUsage |
Ruchi Kandoi | 44bb579 | 2018-10-19 13:08:34 -0700 | [diff] [blame] | 117 | public ApduServiceInfo(ResolveInfo info, String description, |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 118 | ArrayList<AidGroup> staticAidGroups, ArrayList<AidGroup> dynamicAidGroups, |
Martijn Coenen | a2344ea | 2015-06-05 10:45:50 +0200 | [diff] [blame] | 119 | boolean requiresUnlock, int bannerResource, int uid, |
Ruchi Kandoi | 44bb579 | 2018-10-19 13:08:34 -0700 | [diff] [blame] | 120 | String settingsActivityName, String offHost, String staticOffHost) { |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 121 | this.mService = info; |
| 122 | this.mDescription = description; |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 123 | this.mStaticAidGroups = new HashMap<String, AidGroup>(); |
| 124 | this.mDynamicAidGroups = new HashMap<String, AidGroup>(); |
Ruchi Kandoi | 44bb579 | 2018-10-19 13:08:34 -0700 | [diff] [blame] | 125 | this.mOffHostName = offHost; |
| 126 | this.mStaticOffHostName = staticOffHost; |
| 127 | this.mOnHost = (offHost == null); |
Martijn Coenen | c3f0044 | 2013-08-28 19:23:41 -0700 | [diff] [blame] | 128 | this.mRequiresDeviceUnlock = requiresUnlock; |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 129 | for (AidGroup aidGroup : staticAidGroups) { |
| 130 | this.mStaticAidGroups.put(aidGroup.category, aidGroup); |
| 131 | } |
| 132 | for (AidGroup aidGroup : dynamicAidGroups) { |
| 133 | this.mDynamicAidGroups.put(aidGroup.category, aidGroup); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 134 | } |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 135 | this.mBannerResourceId = bannerResource; |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 136 | this.mUid = uid; |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 137 | this.mSettingsActivityName = settingsActivityName; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Mathew Inwood | 1961e1e | 2018-07-31 16:04:15 +0100 | [diff] [blame] | 140 | @UnsupportedAppUsage |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 141 | public ApduServiceInfo(PackageManager pm, ResolveInfo info, boolean onHost) throws |
| 142 | XmlPullParserException, IOException { |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 143 | ServiceInfo si = info.serviceInfo; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 144 | XmlResourceParser parser = null; |
| 145 | try { |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 146 | if (onHost) { |
| 147 | parser = si.loadXmlMetaData(pm, HostApduService.SERVICE_META_DATA); |
| 148 | if (parser == null) { |
Martijn Coenen | 51b6322 | 2013-09-04 14:49:46 -0700 | [diff] [blame] | 149 | throw new XmlPullParserException("No " + HostApduService.SERVICE_META_DATA + |
| 150 | " meta-data"); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 151 | } |
| 152 | } else { |
| 153 | parser = si.loadXmlMetaData(pm, OffHostApduService.SERVICE_META_DATA); |
| 154 | if (parser == null) { |
| 155 | throw new XmlPullParserException("No " + OffHostApduService.SERVICE_META_DATA + |
| 156 | " meta-data"); |
| 157 | } |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | int eventType = parser.getEventType(); |
| 161 | while (eventType != XmlPullParser.START_TAG && eventType != XmlPullParser.END_DOCUMENT) { |
| 162 | eventType = parser.next(); |
| 163 | } |
| 164 | |
| 165 | String tagName = parser.getName(); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 166 | if (onHost && !"host-apdu-service".equals(tagName)) { |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 167 | throw new XmlPullParserException( |
| 168 | "Meta-data does not start with <host-apdu-service> tag"); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 169 | } else if (!onHost && !"offhost-apdu-service".equals(tagName)) { |
| 170 | throw new XmlPullParserException( |
| 171 | "Meta-data does not start with <offhost-apdu-service> tag"); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | Resources res = pm.getResourcesForApplication(si.applicationInfo); |
| 175 | AttributeSet attrs = Xml.asAttributeSet(parser); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 176 | if (onHost) { |
| 177 | TypedArray sa = res.obtainAttributes(attrs, |
| 178 | com.android.internal.R.styleable.HostApduService); |
| 179 | mService = info; |
| 180 | mDescription = sa.getString( |
| 181 | com.android.internal.R.styleable.HostApduService_description); |
Martijn Coenen | c3f0044 | 2013-08-28 19:23:41 -0700 | [diff] [blame] | 182 | mRequiresDeviceUnlock = sa.getBoolean( |
| 183 | com.android.internal.R.styleable.HostApduService_requireDeviceUnlock, |
| 184 | false); |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 185 | mBannerResourceId = sa.getResourceId( |
| 186 | com.android.internal.R.styleable.HostApduService_apduServiceBanner, -1); |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 187 | mSettingsActivityName = sa.getString( |
| 188 | com.android.internal.R.styleable.HostApduService_settingsActivity); |
Ruchi Kandoi | 44bb579 | 2018-10-19 13:08:34 -0700 | [diff] [blame] | 189 | mOffHostName = null; |
| 190 | mStaticOffHostName = mOffHostName; |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 191 | sa.recycle(); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 192 | } else { |
| 193 | TypedArray sa = res.obtainAttributes(attrs, |
| 194 | com.android.internal.R.styleable.OffHostApduService); |
| 195 | mService = info; |
| 196 | mDescription = sa.getString( |
| 197 | com.android.internal.R.styleable.OffHostApduService_description); |
Martijn Coenen | c3f0044 | 2013-08-28 19:23:41 -0700 | [diff] [blame] | 198 | mRequiresDeviceUnlock = false; |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 199 | mBannerResourceId = sa.getResourceId( |
Martijn Coenen | eed4339 | 2013-09-26 11:20:37 -0700 | [diff] [blame] | 200 | com.android.internal.R.styleable.OffHostApduService_apduServiceBanner, -1); |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 201 | mSettingsActivityName = sa.getString( |
| 202 | com.android.internal.R.styleable.HostApduService_settingsActivity); |
Ruchi Kandoi | 44bb579 | 2018-10-19 13:08:34 -0700 | [diff] [blame] | 203 | mOffHostName = sa.getString( |
| 204 | com.android.internal.R.styleable.OffHostApduService_secureElementName); |
| 205 | if (mOffHostName != null) { |
| 206 | if (mOffHostName.equals("eSE")) { |
| 207 | mOffHostName = "eSE1"; |
| 208 | } else if (mOffHostName.equals("SIM")) { |
| 209 | mOffHostName = "SIM1"; |
| 210 | } |
| 211 | } |
| 212 | mStaticOffHostName = mOffHostName; |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 213 | sa.recycle(); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 216 | mStaticAidGroups = new HashMap<String, AidGroup>(); |
| 217 | mDynamicAidGroups = new HashMap<String, AidGroup>(); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 218 | mOnHost = onHost; |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 219 | |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 220 | final int depth = parser.getDepth(); |
| 221 | AidGroup currentGroup = null; |
| 222 | |
| 223 | // Parsed values for the current AID group |
| 224 | while (((eventType = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) |
| 225 | && eventType != XmlPullParser.END_DOCUMENT) { |
| 226 | tagName = parser.getName(); |
| 227 | if (eventType == XmlPullParser.START_TAG && "aid-group".equals(tagName) && |
| 228 | currentGroup == null) { |
| 229 | final TypedArray groupAttrs = res.obtainAttributes(attrs, |
| 230 | com.android.internal.R.styleable.AidGroup); |
| 231 | // Get category of AID group |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 232 | String groupCategory = groupAttrs.getString( |
| 233 | com.android.internal.R.styleable.AidGroup_category); |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 234 | String groupDescription = groupAttrs.getString( |
| 235 | com.android.internal.R.styleable.AidGroup_description); |
Martijn Coenen | 5224608 | 2013-08-30 11:14:46 -0700 | [diff] [blame] | 236 | if (!CardEmulation.CATEGORY_PAYMENT.equals(groupCategory)) { |
| 237 | groupCategory = CardEmulation.CATEGORY_OTHER; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 238 | } |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 239 | currentGroup = mStaticAidGroups.get(groupCategory); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 240 | if (currentGroup != null) { |
Martijn Coenen | 5224608 | 2013-08-30 11:14:46 -0700 | [diff] [blame] | 241 | if (!CardEmulation.CATEGORY_OTHER.equals(groupCategory)) { |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 242 | Log.e(TAG, "Not allowing multiple aid-groups in the " + |
| 243 | groupCategory + " category"); |
| 244 | currentGroup = null; |
| 245 | } |
| 246 | } else { |
| 247 | currentGroup = new AidGroup(groupCategory, groupDescription); |
| 248 | } |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 249 | groupAttrs.recycle(); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 250 | } else if (eventType == XmlPullParser.END_TAG && "aid-group".equals(tagName) && |
| 251 | currentGroup != null) { |
| 252 | if (currentGroup.aids.size() > 0) { |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 253 | if (!mStaticAidGroups.containsKey(currentGroup.category)) { |
| 254 | mStaticAidGroups.put(currentGroup.category, currentGroup); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 255 | } |
| 256 | } else { |
| 257 | Log.e(TAG, "Not adding <aid-group> with empty or invalid AIDs"); |
| 258 | } |
| 259 | currentGroup = null; |
| 260 | } else if (eventType == XmlPullParser.START_TAG && "aid-filter".equals(tagName) && |
| 261 | currentGroup != null) { |
| 262 | final TypedArray a = res.obtainAttributes(attrs, |
| 263 | com.android.internal.R.styleable.AidFilter); |
Martijn Coenen | 62c196a | 2013-08-09 13:43:34 -0700 | [diff] [blame] | 264 | String aid = a.getString(com.android.internal.R.styleable.AidFilter_name). |
| 265 | toUpperCase(); |
Martijn Coenen | b514411 | 2014-07-02 12:44:33 -0700 | [diff] [blame] | 266 | if (CardEmulation.isValidAid(aid) && !currentGroup.aids.contains(aid)) { |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 267 | currentGroup.aids.add(aid); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 268 | } else { |
| 269 | Log.e(TAG, "Ignoring invalid or duplicate aid: " + aid); |
| 270 | } |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 271 | a.recycle(); |
Martijn Coenen | 826a73b | 2014-08-05 07:51:58 -0700 | [diff] [blame] | 272 | } else if (eventType == XmlPullParser.START_TAG && |
| 273 | "aid-prefix-filter".equals(tagName) && currentGroup != null) { |
| 274 | final TypedArray a = res.obtainAttributes(attrs, |
| 275 | com.android.internal.R.styleable.AidFilter); |
| 276 | String aid = a.getString(com.android.internal.R.styleable.AidFilter_name). |
| 277 | toUpperCase(); |
| 278 | // Add wildcard char to indicate prefix |
Martijn Coenen | 0985fb6 | 2014-09-16 10:09:12 -0700 | [diff] [blame] | 279 | aid = aid.concat("*"); |
Martijn Coenen | 826a73b | 2014-08-05 07:51:58 -0700 | [diff] [blame] | 280 | if (CardEmulation.isValidAid(aid) && !currentGroup.aids.contains(aid)) { |
| 281 | currentGroup.aids.add(aid); |
| 282 | } else { |
| 283 | Log.e(TAG, "Ignoring invalid or duplicate aid: " + aid); |
| 284 | } |
| 285 | a.recycle(); |
Love Khanna | e910e8b | 2017-05-12 13:53:42 +0530 | [diff] [blame] | 286 | } else if (eventType == XmlPullParser.START_TAG && |
| 287 | tagName.equals("aid-suffix-filter") && currentGroup != null) { |
| 288 | final TypedArray a = res.obtainAttributes(attrs, |
| 289 | com.android.internal.R.styleable.AidFilter); |
| 290 | String aid = a.getString(com.android.internal.R.styleable.AidFilter_name). |
| 291 | toUpperCase(); |
| 292 | // Add wildcard char to indicate suffix |
| 293 | aid = aid.concat("#"); |
| 294 | if (CardEmulation.isValidAid(aid) && !currentGroup.aids.contains(aid)) { |
| 295 | currentGroup.aids.add(aid); |
| 296 | } else { |
| 297 | Log.e(TAG, "Ignoring invalid or duplicate aid: " + aid); |
| 298 | } |
| 299 | a.recycle(); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | } catch (NameNotFoundException e) { |
| 303 | throw new XmlPullParserException("Unable to create context for: " + si.packageName); |
| 304 | } finally { |
| 305 | if (parser != null) parser.close(); |
| 306 | } |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 307 | // Set uid |
| 308 | mUid = si.applicationInfo.uid; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | public ComponentName getComponent() { |
| 312 | return new ComponentName(mService.serviceInfo.packageName, |
| 313 | mService.serviceInfo.name); |
| 314 | } |
| 315 | |
Ruchi Kandoi | 44bb579 | 2018-10-19 13:08:34 -0700 | [diff] [blame] | 316 | public String getOffHostSecureElement() { |
| 317 | return mOffHostName; |
| 318 | } |
| 319 | |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 320 | /** |
| 321 | * Returns a consolidated list of AIDs from the AID groups |
| 322 | * registered by this service. Note that if a service has both |
| 323 | * a static (manifest-based) AID group for a category and a dynamic |
| 324 | * AID group, only the dynamically registered AIDs will be returned |
| 325 | * for that category. |
| 326 | * @return List of AIDs registered by the service |
| 327 | */ |
Martijn Coenen | ed3a29e | 2015-01-23 11:29:54 -0800 | [diff] [blame] | 328 | public List<String> getAids() { |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 329 | final ArrayList<String> aids = new ArrayList<String>(); |
| 330 | for (AidGroup group : getAidGroups()) { |
| 331 | aids.addAll(group.aids); |
| 332 | } |
| 333 | return aids; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Martijn Coenen | ed3a29e | 2015-01-23 11:29:54 -0800 | [diff] [blame] | 336 | public List<String> getPrefixAids() { |
| 337 | final ArrayList<String> prefixAids = new ArrayList<String>(); |
| 338 | for (AidGroup group : getAidGroups()) { |
| 339 | for (String aid : group.aids) { |
| 340 | if (aid.endsWith("*")) { |
| 341 | prefixAids.add(aid); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | return prefixAids; |
| 346 | } |
| 347 | |
Love Khanna | e910e8b | 2017-05-12 13:53:42 +0530 | [diff] [blame] | 348 | public List<String> getSubsetAids() { |
| 349 | final ArrayList<String> subsetAids = new ArrayList<String>(); |
| 350 | for (AidGroup group : getAidGroups()) { |
| 351 | for (String aid : group.aids) { |
| 352 | if (aid.endsWith("#")) { |
| 353 | subsetAids.add(aid); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | return subsetAids; |
| 358 | } |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 359 | /** |
| 360 | * Returns the registered AID group for this category. |
| 361 | */ |
| 362 | public AidGroup getDynamicAidGroupForCategory(String category) { |
| 363 | return mDynamicAidGroups.get(category); |
| 364 | } |
| 365 | |
| 366 | public boolean removeDynamicAidGroupForCategory(String category) { |
| 367 | return (mDynamicAidGroups.remove(category) != null); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Returns a consolidated list of AID groups |
| 372 | * registered by this service. Note that if a service has both |
| 373 | * a static (manifest-based) AID group for a category and a dynamic |
| 374 | * AID group, only the dynamically registered AID group will be returned |
| 375 | * for that category. |
| 376 | * @return List of AIDs registered by the service |
| 377 | */ |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 378 | public ArrayList<AidGroup> getAidGroups() { |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 379 | final ArrayList<AidGroup> groups = new ArrayList<AidGroup>(); |
| 380 | for (Map.Entry<String, AidGroup> entry : mDynamicAidGroups.entrySet()) { |
| 381 | groups.add(entry.getValue()); |
| 382 | } |
| 383 | for (Map.Entry<String, AidGroup> entry : mStaticAidGroups.entrySet()) { |
| 384 | if (!mDynamicAidGroups.containsKey(entry.getKey())) { |
| 385 | // Consolidate AID groups - don't return static ones |
| 386 | // if a dynamic group exists for the category. |
| 387 | groups.add(entry.getValue()); |
| 388 | } |
| 389 | } |
| 390 | return groups; |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Martijn Coenen | 2f6f3a01 | 2014-04-25 17:00:21 -0700 | [diff] [blame] | 393 | /** |
| 394 | * Returns the category to which this service has attributed the AID that is passed in, |
| 395 | * or null if we don't know this AID. |
| 396 | */ |
| 397 | public String getCategoryForAid(String aid) { |
| 398 | ArrayList<AidGroup> groups = getAidGroups(); |
| 399 | for (AidGroup group : groups) { |
Martijn Coenen | 5e991a1 | 2014-09-16 16:03:28 -0700 | [diff] [blame] | 400 | if (group.aids.contains(aid.toUpperCase())) { |
Martijn Coenen | 2f6f3a01 | 2014-04-25 17:00:21 -0700 | [diff] [blame] | 401 | return group.category; |
| 402 | } |
| 403 | } |
| 404 | return null; |
| 405 | } |
| 406 | |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 407 | public boolean hasCategory(String category) { |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 408 | return (mStaticAidGroups.containsKey(category) || mDynamicAidGroups.containsKey(category)); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Mathew Inwood | 1961e1e | 2018-07-31 16:04:15 +0100 | [diff] [blame] | 411 | @UnsupportedAppUsage |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 412 | public boolean isOnHost() { |
| 413 | return mOnHost; |
| 414 | } |
| 415 | |
Mathew Inwood | 1961e1e | 2018-07-31 16:04:15 +0100 | [diff] [blame] | 416 | @UnsupportedAppUsage |
Martijn Coenen | c3f0044 | 2013-08-28 19:23:41 -0700 | [diff] [blame] | 417 | public boolean requiresUnlock() { |
| 418 | return mRequiresDeviceUnlock; |
| 419 | } |
| 420 | |
Mathew Inwood | 1961e1e | 2018-07-31 16:04:15 +0100 | [diff] [blame] | 421 | @UnsupportedAppUsage |
Martijn Coenen | fca35787 | 2013-09-18 17:26:18 +0200 | [diff] [blame] | 422 | public String getDescription() { |
| 423 | return mDescription; |
| 424 | } |
| 425 | |
Mathew Inwood | 1961e1e | 2018-07-31 16:04:15 +0100 | [diff] [blame] | 426 | @UnsupportedAppUsage |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 427 | public int getUid() { |
| 428 | return mUid; |
| 429 | } |
| 430 | |
| 431 | public void setOrReplaceDynamicAidGroup(AidGroup aidGroup) { |
| 432 | mDynamicAidGroups.put(aidGroup.getCategory(), aidGroup); |
| 433 | } |
| 434 | |
Ruchi Kandoi | 44bb579 | 2018-10-19 13:08:34 -0700 | [diff] [blame] | 435 | @UnsupportedAppUsage |
| 436 | public void setOffHostSecureElement(String offHost) { |
| 437 | mOffHostName = offHost; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Resets the off host Secure Element to statically defined |
| 442 | * by the service in the manifest file. |
| 443 | */ |
| 444 | @UnsupportedAppUsage |
| 445 | public void unsetOffHostSecureElement() { |
| 446 | mOffHostName = mStaticOffHostName; |
| 447 | } |
| 448 | |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 449 | public CharSequence loadLabel(PackageManager pm) { |
| 450 | return mService.loadLabel(pm); |
| 451 | } |
| 452 | |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 453 | public CharSequence loadAppLabel(PackageManager pm) { |
| 454 | try { |
| 455 | return pm.getApplicationLabel(pm.getApplicationInfo( |
| 456 | mService.resolvePackageName, PackageManager.GET_META_DATA)); |
| 457 | } catch (PackageManager.NameNotFoundException e) { |
| 458 | return null; |
| 459 | } |
| 460 | } |
| 461 | |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 462 | public Drawable loadIcon(PackageManager pm) { |
| 463 | return mService.loadIcon(pm); |
| 464 | } |
| 465 | |
Mathew Inwood | 1961e1e | 2018-07-31 16:04:15 +0100 | [diff] [blame] | 466 | @UnsupportedAppUsage |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 467 | public Drawable loadBanner(PackageManager pm) { |
| 468 | Resources res; |
| 469 | try { |
| 470 | res = pm.getResourcesForApplication(mService.serviceInfo.packageName); |
| 471 | Drawable banner = res.getDrawable(mBannerResourceId); |
| 472 | return banner; |
| 473 | } catch (NotFoundException e) { |
| 474 | Log.e(TAG, "Could not load banner."); |
| 475 | return null; |
| 476 | } catch (NameNotFoundException e) { |
| 477 | Log.e(TAG, "Could not load banner."); |
| 478 | return null; |
| 479 | } |
| 480 | } |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 481 | |
Mathew Inwood | 1961e1e | 2018-07-31 16:04:15 +0100 | [diff] [blame] | 482 | @UnsupportedAppUsage |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 483 | public String getSettingsActivityName() { return mSettingsActivityName; } |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 484 | |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 485 | @Override |
| 486 | public String toString() { |
| 487 | StringBuilder out = new StringBuilder("ApduService: "); |
| 488 | out.append(getComponent()); |
| 489 | out.append(", description: " + mDescription); |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 490 | out.append(", Static AID Groups: "); |
| 491 | for (AidGroup aidGroup : mStaticAidGroups.values()) { |
| 492 | out.append(aidGroup.toString()); |
| 493 | } |
| 494 | out.append(", Dynamic AID Groups: "); |
| 495 | for (AidGroup aidGroup : mDynamicAidGroups.values()) { |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 496 | out.append(aidGroup.toString()); |
| 497 | } |
| 498 | return out.toString(); |
| 499 | } |
| 500 | |
| 501 | @Override |
| 502 | public boolean equals(Object o) { |
| 503 | if (this == o) return true; |
| 504 | if (!(o instanceof ApduServiceInfo)) return false; |
| 505 | ApduServiceInfo thatService = (ApduServiceInfo) o; |
| 506 | |
| 507 | return thatService.getComponent().equals(this.getComponent()); |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public int hashCode() { |
| 512 | return getComponent().hashCode(); |
| 513 | } |
| 514 | |
| 515 | |
| 516 | @Override |
| 517 | public int describeContents() { |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | @Override |
| 522 | public void writeToParcel(Parcel dest, int flags) { |
| 523 | mService.writeToParcel(dest, flags); |
| 524 | dest.writeString(mDescription); |
Martijn Coenen | 38d3bb7 | 2013-08-07 20:03:03 -0700 | [diff] [blame] | 525 | dest.writeInt(mOnHost ? 1 : 0); |
Ruchi Kandoi | 44bb579 | 2018-10-19 13:08:34 -0700 | [diff] [blame] | 526 | dest.writeString(mOffHostName); |
| 527 | dest.writeString(mStaticOffHostName); |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 528 | dest.writeInt(mStaticAidGroups.size()); |
| 529 | if (mStaticAidGroups.size() > 0) { |
| 530 | dest.writeTypedList(new ArrayList<AidGroup>(mStaticAidGroups.values())); |
| 531 | } |
| 532 | dest.writeInt(mDynamicAidGroups.size()); |
| 533 | if (mDynamicAidGroups.size() > 0) { |
| 534 | dest.writeTypedList(new ArrayList<AidGroup>(mDynamicAidGroups.values())); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 535 | } |
Martijn Coenen | c3f0044 | 2013-08-28 19:23:41 -0700 | [diff] [blame] | 536 | dest.writeInt(mRequiresDeviceUnlock ? 1 : 0); |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 537 | dest.writeInt(mBannerResourceId); |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 538 | dest.writeInt(mUid); |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 539 | dest.writeString(mSettingsActivityName); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 540 | }; |
| 541 | |
Mathew Inwood | 1961e1e | 2018-07-31 16:04:15 +0100 | [diff] [blame] | 542 | @UnsupportedAppUsage |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame^] | 543 | public static final @android.annotation.NonNull Parcelable.Creator<ApduServiceInfo> CREATOR = |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 544 | new Parcelable.Creator<ApduServiceInfo>() { |
| 545 | @Override |
| 546 | public ApduServiceInfo createFromParcel(Parcel source) { |
| 547 | ResolveInfo info = ResolveInfo.CREATOR.createFromParcel(source); |
| 548 | String description = source.readString(); |
Martijn Coenen | 5e991a1 | 2014-09-16 16:03:28 -0700 | [diff] [blame] | 549 | boolean onHost = source.readInt() != 0; |
Ruchi Kandoi | 44bb579 | 2018-10-19 13:08:34 -0700 | [diff] [blame] | 550 | String offHostName = source.readString(); |
| 551 | String staticOffHostName = source.readString(); |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 552 | ArrayList<AidGroup> staticAidGroups = new ArrayList<AidGroup>(); |
| 553 | int numStaticGroups = source.readInt(); |
| 554 | if (numStaticGroups > 0) { |
| 555 | source.readTypedList(staticAidGroups, AidGroup.CREATOR); |
| 556 | } |
| 557 | ArrayList<AidGroup> dynamicAidGroups = new ArrayList<AidGroup>(); |
| 558 | int numDynamicGroups = source.readInt(); |
| 559 | if (numDynamicGroups > 0) { |
| 560 | source.readTypedList(dynamicAidGroups, AidGroup.CREATOR); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 561 | } |
Martijn Coenen | 5e991a1 | 2014-09-16 16:03:28 -0700 | [diff] [blame] | 562 | boolean requiresUnlock = source.readInt() != 0; |
Martijn Coenen | 58d2065 | 2013-09-05 21:05:53 -0700 | [diff] [blame] | 563 | int bannerResource = source.readInt(); |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 564 | int uid = source.readInt(); |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 565 | String settingsActivityName = source.readString(); |
Ruchi Kandoi | 44bb579 | 2018-10-19 13:08:34 -0700 | [diff] [blame] | 566 | return new ApduServiceInfo(info, description, staticAidGroups, |
Martijn Coenen | a2344ea | 2015-06-05 10:45:50 +0200 | [diff] [blame] | 567 | dynamicAidGroups, requiresUnlock, bannerResource, uid, |
Ruchi Kandoi | 44bb579 | 2018-10-19 13:08:34 -0700 | [diff] [blame] | 568 | settingsActivityName, offHostName, staticOffHostName); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | @Override |
| 572 | public ApduServiceInfo[] newArray(int size) { |
| 573 | return new ApduServiceInfo[size]; |
| 574 | } |
| 575 | }; |
| 576 | |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 577 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 578 | pw.println(" " + getComponent() + |
| 579 | " (Description: " + getDescription() + ")"); |
Ruchi Kandoi | 44bb579 | 2018-10-19 13:08:34 -0700 | [diff] [blame] | 580 | if (mOnHost) { |
| 581 | pw.println(" On Host Service"); |
| 582 | } else { |
| 583 | pw.println(" Off-host Service"); |
| 584 | pw.println(" " + "Current off-host SE" + mOffHostName |
| 585 | + " static off-host: " + mOffHostName); |
| 586 | } |
| 587 | pw.println(" Static off-host Secure Element:"); |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 588 | pw.println(" Static AID groups:"); |
| 589 | for (AidGroup group : mStaticAidGroups.values()) { |
| 590 | pw.println(" Category: " + group.category); |
| 591 | for (String aid : group.aids) { |
| 592 | pw.println(" AID: " + aid); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 593 | } |
| 594 | } |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 595 | pw.println(" Dynamic AID groups:"); |
| 596 | for (AidGroup group : mDynamicAidGroups.values()) { |
| 597 | pw.println(" Category: " + group.category); |
| 598 | for (String aid : group.aids) { |
| 599 | pw.println(" AID: " + aid); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 600 | } |
Martijn Coenen | aa1492d | 2014-04-11 12:54:22 -0700 | [diff] [blame] | 601 | } |
Martijn Coenen | 8abf680 | 2015-04-20 13:44:58 +0200 | [diff] [blame] | 602 | pw.println(" Settings Activity: " + mSettingsActivityName); |
Martijn Coenen | a739788 | 2013-07-30 20:07:47 -0700 | [diff] [blame] | 603 | } |
| 604 | } |