1 /*
2  * Copyright (C) 2021 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.security.attestationverification;
18 
19 import static android.security.attestationverification.AttestationVerificationManager.PROFILE_APP_DEFINED;
20 import static android.security.attestationverification.AttestationVerificationManager.PROFILE_UNKNOWN;
21 
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.os.Parcelable;
25 import android.security.attestationverification.AttestationVerificationManager.AttestationProfileId;
26 import android.util.Log;
27 
28 import com.android.internal.util.DataClass;
29 
30 
31 /**
32  * An attestation profile defining the security requirements for verifying the attestation of a
33  * remote compute environment.
34  *
35  * <p>This class is immutable and thread-safe. When checking this profile against an expected
36  * profile, it is recommended to construct the expected profile and compare them with {@code
37  * equals()}.
38  *
39  * @hide
40  * @see AttestationVerificationManager
41  */
42 @DataClass(
43         genConstructor = false,
44         genEqualsHashCode = true
45 )
46 public final class AttestationProfile implements Parcelable {
47 
48     private static final String TAG = "AVF";
49 
50     /**
51      * The ID of a system-defined attestation profile.
52      *
53      * See constants in {@link AttestationVerificationManager} prefixed with {@code PROFILE_}. If
54      * this has the value of {@link AttestationVerificationManager#PROFILE_APP_DEFINED}, then the
55      * packageName and profileName are non-null.
56      */
57     @AttestationProfileId
58     private final int mAttestationProfileId;
59 
60     /**
61      * The package name of a app-defined attestation profile.
62      *
63      * This value will be null unless the value of attestationProfileId is {@link
64      * AttestationVerificationManager#PROFILE_APP_DEFINED}.
65      */
66     @Nullable
67     private final String mPackageName;
68 
69 
70     /**
71      * The name of an app-defined attestation profile.
72      *
73      * This value will be null unless the value of attestationProfileId is {@link
74      * AttestationVerificationManager#PROFILE_APP_DEFINED}.
75      */
76     @Nullable
77     private final String mProfileName;
78 
AttestationProfile( @ttestationProfileId int attestationProfileId, @Nullable String packageName, @Nullable String profileName)79     private AttestationProfile(
80             @AttestationProfileId int attestationProfileId,
81             @Nullable String packageName,
82             @Nullable String profileName) {
83         mAttestationProfileId = attestationProfileId;
84         mPackageName = packageName;
85         mProfileName = profileName;
86     }
87 
88     /**
89      * Create a profile with the given id.
90      *
91      * <p>This constructor is for specifying a profile which is defined by the system. These are
92      * available as constants in the {@link AttestationVerificationManager} class prefixed with
93      * {@code PROFILE_}.
94      *
95      * @param attestationProfileId the ID of the system-defined profile
96      * @throws IllegalArgumentException when called with
97      * {@link AttestationVerificationManager#PROFILE_APP_DEFINED}
98      *                                  (use {@link #AttestationProfile(String, String)})
99      */
AttestationProfile(@ttestationProfileId int attestationProfileId)100     public AttestationProfile(@AttestationProfileId int attestationProfileId) {
101         this(attestationProfileId, null, null);
102         if (attestationProfileId == PROFILE_APP_DEFINED) {
103             throw new IllegalArgumentException("App-defined profiles must be specified with the "
104                     + "constructor AttestationProfile#constructor(String, String)");
105         }
106     }
107 
108     /**
109      * Create a profile with the given package name and profile name.
110      *
111      * <p>This constructor is for specifying a profile defined by an app. The packageName must
112      * match the package name of the app that defines the profile (as specified in the {@code
113      * package} attribute of the {@code
114      * <manifest>} tag in the app's manifest. The profile name matches the {@code name} attribute
115      * of the {@code <attestation-profile>} tag.
116      *
117      * <p>Apps must declare profiles in their manifest as an {@code <attestation-profile>} element.
118      * However, this constructor does not verify that such a profile exists. If the profile does not
119      * exist, verifications will fail.
120      *
121      * @param packageName the package name of the app defining the profile
122      * @param profileName the name of the profile
123      */
AttestationProfile(@onNull String packageName, @NonNull String profileName)124     public AttestationProfile(@NonNull String packageName, @NonNull String profileName) {
125         this(PROFILE_APP_DEFINED, packageName, profileName);
126         if (packageName == null || profileName == null) {
127             throw new IllegalArgumentException("Both packageName and profileName must be non-null");
128         }
129     }
130 
131     @Override
toString()132     public String toString() {
133         if (mAttestationProfileId == PROFILE_APP_DEFINED) {
134             return "AttestationProfile(package=" + mPackageName + ", name=" + mProfileName + ")";
135         } else {
136             String humanReadableProfileId;
137             switch (mAttestationProfileId) {
138                 case PROFILE_UNKNOWN:
139                     humanReadableProfileId = "PROFILE_UNKNOWN";
140                     break;
141                 default:
142                     Log.e(TAG, "ERROR: Missing case in AttestationProfile#toString");
143                     humanReadableProfileId = "ERROR";
144             }
145             return "AttestationProfile(" + humanReadableProfileId + "/" + mAttestationProfileId
146                     + ")";
147         }
148     }
149 
150 
151     // Code below generated by codegen v1.0.23.
152     //
153     // DO NOT MODIFY!
154     // CHECKSTYLE:OFF Generated code
155     //
156     // To regenerate run:
157     // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/security
158     // /attestationverification/AttestationProfile.java
159     //
160     // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
161     //   Settings > Editor > Code Style > Formatter Control
162     //@formatter:off
163 
164 
165     /**
166      * The ID of a system-defined attestation profile.
167      *
168      * See constants in {@link AttestationVerificationManager} prefixed with {@code PROFILE_}. If
169      * this has the value of {@link AttestationVerificationManager#PROFILE_APP_DEFINED}, then the
170      * packageName and profileName are non-null.
171      */
172     @DataClass.Generated.Member
getAttestationProfileId()173     public @AttestationProfileId int getAttestationProfileId() {
174         return mAttestationProfileId;
175     }
176 
177     /**
178      * The package name of a app-defined attestation profile.
179      *
180      * This value will be null unless the value of attestationProfileId is {@link
181      * AttestationVerificationManager#PROFILE_APP_DEFINED}.
182      */
183     @DataClass.Generated.Member
getPackageName()184     public @Nullable String getPackageName() {
185         return mPackageName;
186     }
187 
188     /**
189      * The name of an app-defined attestation profile.
190      *
191      * This value will be null unless the value of attestationProfileId is {@link
192      * AttestationVerificationManager#PROFILE_APP_DEFINED}.
193      */
194     @DataClass.Generated.Member
getProfileName()195     public @Nullable String getProfileName() {
196         return mProfileName;
197     }
198 
199     @Override
200     @DataClass.Generated.Member
equals(@ullable Object o)201     public boolean equals(@Nullable Object o) {
202         // You can override field equality logic by defining either of the methods like:
203         // boolean fieldNameEquals(AttestationProfile other) { ... }
204         // boolean fieldNameEquals(FieldType otherValue) { ... }
205 
206         if (this == o) return true;
207         if (o == null || getClass() != o.getClass()) return false;
208         @SuppressWarnings("unchecked")
209         AttestationProfile that = (AttestationProfile) o;
210         //noinspection PointlessBooleanExpression
211         return true
212                 && mAttestationProfileId == that.mAttestationProfileId
213                 && java.util.Objects.equals(mPackageName, that.mPackageName)
214                 && java.util.Objects.equals(mProfileName, that.mProfileName);
215     }
216 
217     @Override
218     @DataClass.Generated.Member
hashCode()219     public int hashCode() {
220         // You can override field hashCode logic by defining methods like:
221         // int fieldNameHashCode() { ... }
222 
223         int _hash = 1;
224         _hash = 31 * _hash + mAttestationProfileId;
225         _hash = 31 * _hash + java.util.Objects.hashCode(mPackageName);
226         _hash = 31 * _hash + java.util.Objects.hashCode(mProfileName);
227         return _hash;
228     }
229 
230     @Override
231     @DataClass.Generated.Member
writeToParcel(@onNull android.os.Parcel dest, int flags)232     public void writeToParcel(@NonNull android.os.Parcel dest, int flags) {
233         // You can override field parcelling by defining methods like:
234         // void parcelFieldName(Parcel dest, int flags) { ... }
235 
236         byte flg = 0;
237         if (mPackageName != null) flg |= 0x2;
238         if (mProfileName != null) flg |= 0x4;
239         dest.writeByte(flg);
240         dest.writeInt(mAttestationProfileId);
241         if (mPackageName != null) dest.writeString(mPackageName);
242         if (mProfileName != null) dest.writeString(mProfileName);
243     }
244 
245     @Override
246     @DataClass.Generated.Member
describeContents()247     public int describeContents() { return 0; }
248 
249     /** @hide */
250     @SuppressWarnings({"unchecked", "RedundantCast"})
251     @DataClass.Generated.Member
AttestationProfile(@onNull android.os.Parcel in)252     /* package-private */ AttestationProfile(@NonNull android.os.Parcel in) {
253         // You can override field unparcelling by defining methods like:
254         // static FieldType unparcelFieldName(Parcel in) { ... }
255 
256         byte flg = in.readByte();
257         int attestationProfileId = in.readInt();
258         String packageName = (flg & 0x2) == 0 ? null : in.readString();
259         String profileName = (flg & 0x4) == 0 ? null : in.readString();
260 
261         this.mAttestationProfileId = attestationProfileId;
262         com.android.internal.util.AnnotationValidations.validate(
263                 AttestationProfileId.class, null, mAttestationProfileId);
264         this.mPackageName = packageName;
265         this.mProfileName = profileName;
266 
267         // onConstructed(); // You can define this method to get a callback
268     }
269 
270     @DataClass.Generated.Member
271     public static final @NonNull Parcelable.Creator<AttestationProfile> CREATOR
272             = new Parcelable.Creator<AttestationProfile>() {
273         @Override
274         public AttestationProfile[] newArray(int size) {
275             return new AttestationProfile[size];
276         }
277 
278         @Override
279         public AttestationProfile createFromParcel(@NonNull android.os.Parcel in) {
280             return new AttestationProfile(in);
281         }
282     };
283 
284     @DataClass.Generated(
285             time = 1633629498403L,
286             codegenVersion = "1.0.23",
287             sourceFile = "frameworks/base/core/java/android/security/attestationverification/AttestationProfile.java",
288             inputSignatures = "private static final  java.lang.String TAG\nprivate final @android.security.attestationverification.AttestationVerificationManager.AttestationProfileId int mAttestationProfileId\nprivate final @android.annotation.Nullable java.lang.String mPackageName\nprivate final @android.annotation.Nullable java.lang.String mProfileName\npublic @java.lang.Override java.lang.String toString()\nclass AttestationProfile extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genConstructor=false, genEqualsHashCode=true)")
289     @Deprecated
__metadata()290     private void __metadata() {}
291 
292 
293     //@formatter:on
294     // End of generated code
295 
296 }
297