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.util.apk;
18 
19 import android.annotation.Nullable;
20 
21 import java.security.cert.Certificate;
22 import java.util.Collections;
23 import java.util.List;
24 
25 /**
26  * A class encapsulating the result from the source stamp verifier
27  *
28  * <p>It indicates whether the source stamp is verified or not, and the source stamp certificate.
29  *
30  * @hide
31  */
32 public final class SourceStampVerificationResult {
33 
34     private final boolean mPresent;
35     private final boolean mVerified;
36     private final Certificate mCertificate;
37     private final List<? extends Certificate> mCertificateLineage;
38 
SourceStampVerificationResult( boolean present, boolean verified, @Nullable Certificate certificate, List<? extends Certificate> certificateLineage)39     private SourceStampVerificationResult(
40             boolean present, boolean verified, @Nullable Certificate certificate,
41             List<? extends Certificate> certificateLineage) {
42         this.mPresent = present;
43         this.mVerified = verified;
44         this.mCertificate = certificate;
45         this.mCertificateLineage = certificateLineage;
46     }
47 
isPresent()48     public boolean isPresent() {
49         return mPresent;
50     }
51 
isVerified()52     public boolean isVerified() {
53         return mVerified;
54     }
55 
getCertificate()56     public Certificate getCertificate() {
57         return mCertificate;
58     }
59 
getCertificateLineage()60     public List<? extends Certificate> getCertificateLineage() {
61         return mCertificateLineage;
62     }
63 
64     /**
65      * Create a non-present source stamp outcome.
66      *
67      * @return A non-present source stamp result.
68      */
notPresent()69     public static SourceStampVerificationResult notPresent() {
70         return new SourceStampVerificationResult(
71                 /* present= */ false, /* verified= */ false, /* certificate= */
72                 null, /* certificateLineage= */ Collections.emptyList());
73     }
74 
75     /**
76      * Create a verified source stamp outcome.
77      *
78      * @param certificate        The source stamp certificate.
79      * @param certificateLineage The proof-of-rotation lineage for the source stamp.
80      * @return A verified source stamp result, and the source stamp certificate.
81      */
verified(Certificate certificate, List<? extends Certificate> certificateLineage)82     public static SourceStampVerificationResult verified(Certificate certificate,
83             List<? extends Certificate> certificateLineage) {
84         return new SourceStampVerificationResult(
85                 /* present= */ true, /* verified= */ true, certificate, certificateLineage);
86     }
87 
88     /**
89      * Create a non-verified source stamp outcome.
90      *
91      * @return A non-verified source stamp result.
92      */
notVerified()93     public static SourceStampVerificationResult notVerified() {
94         return new SourceStampVerificationResult(
95                 /* present= */ true, /* verified= */ false, /* certificate= */
96                 null, /* certificateLineage= */ Collections.emptyList());
97     }
98 }
99