1 /* 2 * Copyright (C) 2020 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.content.pm; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 import com.android.internal.util.DataClass; 25 26 import java.io.ByteArrayInputStream; 27 import java.io.InputStream; 28 import java.security.cert.Certificate; 29 import java.security.cert.CertificateEncodingException; 30 import java.security.cert.CertificateException; 31 import java.security.cert.CertificateFactory; 32 import java.security.cert.X509Certificate; 33 34 /** 35 * A typed checksum of an APK. 36 * 37 * @see PackageManager#requestChecksums 38 */ 39 @DataClass(genHiddenConstructor = true, genToString = true) 40 @DataClass.Suppress({"getChecksum"}) 41 public final class ApkChecksum implements Parcelable { 42 /** 43 * Checksum for which split. Null indicates base.apk. 44 */ 45 private final @Nullable String mSplitName; 46 /** 47 * Checksum. 48 */ 49 private final @NonNull Checksum mChecksum; 50 /** 51 * For Installer-provided checksums, package name of the Installer. 52 */ 53 private final @Nullable String mInstallerPackageName; 54 /** 55 * For Installer-provided checksums, certificate of the Installer. 56 */ 57 private final @Nullable byte[] mInstallerCertificate; 58 59 /** 60 * Constructor, internal use only. 61 * 62 * @hide 63 */ ApkChecksum(@ullable String splitName, @Checksum.Type int type, @NonNull byte[] value)64 public ApkChecksum(@Nullable String splitName, @Checksum.Type int type, 65 @NonNull byte[] value) { 66 this(splitName, new Checksum(type, value), (String) null, (byte[]) null); 67 } 68 69 /** 70 * Constructor, internal use only. 71 * 72 * @hide 73 */ ApkChecksum(@ullable String splitName, @Checksum.Type int type, @NonNull byte[] value, @Nullable String sourcePackageName, @Nullable Certificate sourceCertificate)74 public ApkChecksum(@Nullable String splitName, @Checksum.Type int type, @NonNull byte[] value, 75 @Nullable String sourcePackageName, @Nullable Certificate sourceCertificate) 76 throws CertificateEncodingException { 77 this(splitName, new Checksum(type, value), sourcePackageName, 78 (sourceCertificate != null) ? sourceCertificate.getEncoded() : null); 79 } 80 81 82 /** 83 * Checksum type. 84 */ getType()85 public @Checksum.Type int getType() { 86 return mChecksum.getType(); 87 } 88 89 /** 90 * Checksum value. 91 */ getValue()92 public @NonNull byte[] getValue() { 93 return mChecksum.getValue(); 94 } 95 96 /** 97 * Returns raw bytes representing encoded certificate of the Installer. 98 * @hide 99 */ getInstallerCertificateBytes()100 public @Nullable byte[] getInstallerCertificateBytes() { 101 return mInstallerCertificate; 102 } 103 104 /** 105 * For Installer-provided checksums, certificate of the Installer. 106 * @throws CertificateException in case when certificate can't be re-created from serialized 107 * data. 108 */ getInstallerCertificate()109 public @Nullable Certificate getInstallerCertificate() throws CertificateException { 110 if (mInstallerCertificate == null) { 111 return null; 112 } 113 final CertificateFactory cf = CertificateFactory.getInstance("X.509"); 114 final InputStream is = new ByteArrayInputStream(mInstallerCertificate); 115 final X509Certificate cert = (X509Certificate) cf.generateCertificate(is); 116 return cert; 117 } 118 119 120 121 // Code below generated by codegen v1.0.23. 122 // 123 // DO NOT MODIFY! 124 // CHECKSTYLE:OFF Generated code 125 // 126 // To regenerate run: 127 // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/content/pm/ApkChecksum.java 128 // 129 // To exclude the generated code from IntelliJ auto-formatting enable (one-time): 130 // Settings > Editor > Code Style > Formatter Control 131 //@formatter:off 132 133 134 /** 135 * Creates a new ApkChecksum. 136 * 137 * @param splitName 138 * Checksum for which split. Null indicates base.apk. 139 * @param checksum 140 * Checksum. 141 * @param installerPackageName 142 * For Installer-provided checksums, package name of the Installer. 143 * @param installerCertificate 144 * For Installer-provided checksums, certificate of the Installer. 145 * @hide 146 */ 147 @DataClass.Generated.Member ApkChecksum( @ullable String splitName, @NonNull Checksum checksum, @Nullable String installerPackageName, @Nullable byte[] installerCertificate)148 public ApkChecksum( 149 @Nullable String splitName, 150 @NonNull Checksum checksum, 151 @Nullable String installerPackageName, 152 @Nullable byte[] installerCertificate) { 153 this.mSplitName = splitName; 154 this.mChecksum = checksum; 155 com.android.internal.util.AnnotationValidations.validate( 156 NonNull.class, null, mChecksum); 157 this.mInstallerPackageName = installerPackageName; 158 this.mInstallerCertificate = installerCertificate; 159 160 // onConstructed(); // You can define this method to get a callback 161 } 162 163 /** 164 * Checksum for which split. Null indicates base.apk. 165 */ 166 @DataClass.Generated.Member getSplitName()167 public @Nullable String getSplitName() { 168 return mSplitName; 169 } 170 171 /** 172 * For Installer-provided checksums, package name of the Installer. 173 */ 174 @DataClass.Generated.Member getInstallerPackageName()175 public @Nullable String getInstallerPackageName() { 176 return mInstallerPackageName; 177 } 178 179 @Override 180 @DataClass.Generated.Member toString()181 public String toString() { 182 // You can override field toString logic by defining methods like: 183 // String fieldNameToString() { ... } 184 185 return "ApkChecksum { " + 186 "splitName = " + mSplitName + ", " + 187 "checksum = " + mChecksum + ", " + 188 "installerPackageName = " + mInstallerPackageName + ", " + 189 "installerCertificate = " + java.util.Arrays.toString(mInstallerCertificate) + 190 " }"; 191 } 192 193 @Override 194 @DataClass.Generated.Member writeToParcel(@onNull Parcel dest, int flags)195 public void writeToParcel(@NonNull Parcel dest, int flags) { 196 // You can override field parcelling by defining methods like: 197 // void parcelFieldName(Parcel dest, int flags) { ... } 198 199 byte flg = 0; 200 if (mSplitName != null) flg |= 0x1; 201 if (mInstallerPackageName != null) flg |= 0x4; 202 if (mInstallerCertificate != null) flg |= 0x8; 203 dest.writeByte(flg); 204 if (mSplitName != null) dest.writeString(mSplitName); 205 dest.writeTypedObject(mChecksum, flags); 206 if (mInstallerPackageName != null) dest.writeString(mInstallerPackageName); 207 if (mInstallerCertificate != null) dest.writeByteArray(mInstallerCertificate); 208 } 209 210 @Override 211 @DataClass.Generated.Member describeContents()212 public int describeContents() { return 0; } 213 214 /** @hide */ 215 @SuppressWarnings({"unchecked", "RedundantCast"}) 216 @DataClass.Generated.Member ApkChecksum(@onNull Parcel in)217 /* package-private */ ApkChecksum(@NonNull Parcel in) { 218 // You can override field unparcelling by defining methods like: 219 // static FieldType unparcelFieldName(Parcel in) { ... } 220 221 byte flg = in.readByte(); 222 String splitName = (flg & 0x1) == 0 ? null : in.readString(); 223 Checksum checksum = (Checksum) in.readTypedObject(Checksum.CREATOR); 224 String installerPackageName = (flg & 0x4) == 0 ? null : in.readString(); 225 byte[] installerCertificate = (flg & 0x8) == 0 ? null : in.createByteArray(); 226 227 this.mSplitName = splitName; 228 this.mChecksum = checksum; 229 com.android.internal.util.AnnotationValidations.validate( 230 NonNull.class, null, mChecksum); 231 this.mInstallerPackageName = installerPackageName; 232 this.mInstallerCertificate = installerCertificate; 233 234 // onConstructed(); // You can define this method to get a callback 235 } 236 237 @DataClass.Generated.Member 238 public static final @NonNull Parcelable.Creator<ApkChecksum> CREATOR 239 = new Parcelable.Creator<ApkChecksum>() { 240 @Override 241 public ApkChecksum[] newArray(int size) { 242 return new ApkChecksum[size]; 243 } 244 245 @Override 246 public ApkChecksum createFromParcel(@NonNull Parcel in) { 247 return new ApkChecksum(in); 248 } 249 }; 250 251 @DataClass.Generated( 252 time = 1674080488372L, 253 codegenVersion = "1.0.23", 254 sourceFile = "frameworks/base/core/java/android/content/pm/ApkChecksum.java", 255 inputSignatures = "private final @android.annotation.Nullable java.lang.String mSplitName\nprivate final @android.annotation.NonNull android.content.pm.Checksum mChecksum\nprivate final @android.annotation.Nullable java.lang.String mInstallerPackageName\nprivate final @android.annotation.Nullable byte[] mInstallerCertificate\npublic @android.content.pm.Checksum.Type int getType()\npublic @android.annotation.NonNull byte[] getValue()\npublic @android.annotation.Nullable byte[] getInstallerCertificateBytes()\npublic @android.annotation.Nullable java.security.cert.Certificate getInstallerCertificate()\nclass ApkChecksum extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genHiddenConstructor=true, genToString=true)") 256 @Deprecated __metadata()257 private void __metadata() {} 258 259 260 //@formatter:on 261 // End of generated code 262 263 } 264