1 /* 2 * Copyright (C) 2023 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 com.android.server.sdksandbox.verifier; 18 19 import android.annotation.NonNull; 20 21 import com.android.internal.annotations.VisibleForTesting; 22 import com.android.server.sdksandbox.proto.Verifier.AllowedApisList; 23 import com.android.server.sdksandbox.proto.Verifier.AllowedApisPerTargetSdk; 24 25 import com.google.protobuf.InvalidProtocolBufferException; 26 27 import java.io.FileNotFoundException; 28 import java.io.IOException; 29 import java.net.URL; 30 import java.util.Map; 31 32 /** 33 * Loads API allowlists that are used by the SdkDexVerifier to scan for disallowed API usages. 34 * 35 * @hide 36 */ 37 public class ApiAllowlistProvider { 38 39 private String mAllowlistResource; 40 ApiAllowlistProvider()41 public ApiAllowlistProvider() { 42 mAllowlistResource = "platform_api_allowlist_per_target_sdk_version_current.binarypb"; 43 } 44 45 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE) ApiAllowlistProvider(String allowlistResource)46 public ApiAllowlistProvider(String allowlistResource) { 47 mAllowlistResource = allowlistResource; 48 } 49 50 /** Loads current allowlists from pre bundled proto. */ 51 @NonNull loadPlatformApiAllowlist()52 public Map<Long, AllowedApisList> loadPlatformApiAllowlist() 53 throws FileNotFoundException, InvalidProtocolBufferException, IOException { 54 URL resourceURL = this.getClass().getClassLoader().getResource(mAllowlistResource); 55 if (resourceURL == null) { 56 throw new FileNotFoundException(mAllowlistResource + " not found."); 57 } 58 59 byte[] allowlistBytes = resourceURL.openStream().readAllBytes(); 60 61 AllowedApisPerTargetSdk allowedApisPerTargetSdk = 62 AllowedApisPerTargetSdk.parseFrom(allowlistBytes); 63 64 return allowedApisPerTargetSdk.getAllowlistPerTargetSdk(); 65 } 66 } 67