1 /*
2  * Copyright (C) 2024 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.adservices.service.stats;
18 
19 import android.adservices.adselection.SignedContextualAds;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.Nullable;
23 
24 import com.google.auto.value.AutoValue;
25 
26 /** Data class for {@link SignedContextualAds} signature authentication stats */
27 @AutoValue
28 public abstract class SignatureVerificationStats {
29     public static final int UNSET = 0;
30     public static final String EMPTY_STRING = "";
31 
32     /** Returns the latency of fetching the key from key management store */
getKeyFetchLatency()33     public abstract long getKeyFetchLatency();
34 
35     /** Returns the latency of the serialization of a given {@link SignedContextualAds} object */
getSerializationLatency()36     public abstract long getSerializationLatency();
37 
38     /**
39      * Returns the latency of the verifying the signature (only the signature verification, without
40      * including the serialization and key fetch)
41      */
getVerificationLatency()42     public abstract long getVerificationLatency();
43 
44     /** Returns number of keys fetched for a given ad tech */
getNumOfKeysFetched()45     public abstract int getNumOfKeysFetched();
46 
47     /**
48      * Returns the end status of the verification request
49      *
50      * <p>A verification request can end in success or a failure with various reasons
51      */
getSignatureVerificationStatus()52     public abstract VerificationStatus getSignatureVerificationStatus();
53 
54     /** Returns enrollment id of the buyer that has a signature verification failure */
55     @Nullable
getFailedSignatureBuyerEnrollmentId()56     public abstract String getFailedSignatureBuyerEnrollmentId();
57 
58     /** Returns enrollment id of the seller that has a signature verification failure */
59     @Nullable
getFailedSignatureSellerEnrollmentId()60     public abstract String getFailedSignatureSellerEnrollmentId();
61 
62     /** Returns caller package name that has a signature verification failure */
63     @Nullable
getFailedSignatureCallerPackageName()64     public abstract String getFailedSignatureCallerPackageName();
65 
66     /** Returns number of unknown error */
getFailureDetailUnknownError()67     public abstract int getFailureDetailUnknownError();
68 
69     /** Returns number of enrollment data not found for the buyer or was null. */
getFailureDetailNoEnrollmentDataForBuyer()70     public abstract int getFailureDetailNoEnrollmentDataForBuyer();
71 
72     /** Returns zero keys fetched for the buyer */
getFailureDetailNoKeysFetchedForBuyer()73     public abstract int getFailureDetailNoKeysFetchedForBuyer();
74 
75     /** Returns signature is not formatted correctly */
getFailureDetailWrongSignatureFormat()76     public abstract int getFailureDetailWrongSignatureFormat();
77 
78     /** Returns number of keys that has the wrong format */
getFailureDetailCountOfKeysWithWrongFormat()79     public abstract int getFailureDetailCountOfKeysWithWrongFormat();
80 
81     /** Returns number of keys that failed to verify the signature */
getFailureDetailCountOfKeysFailedToVerifySignature()82     public abstract int getFailureDetailCountOfKeysFailedToVerifySignature();
83 
84     public enum VerificationStatus {
85         /** Failure reason is unknown */
86         UNKNOWN(0),
87 
88         /** Signature is verified successfully */
89         VERIFIED(1),
90 
91         /** Verification is failed against all the keys fetched from the key store. */
92         VERIFICATION_FAILED(2);
93 
94         private final int mValue;
95 
VerificationStatus(int value)96         VerificationStatus(int value) {
97             mValue = value;
98         }
99 
getValue()100         public int getValue() {
101             return mValue;
102         }
103     }
104 
105     /** Returns a builder for this instance */
toBuilder()106     public abstract Builder toBuilder();
107 
108     /** Returns a generic builder. */
builder()109     public static Builder builder() {
110         return new AutoValue_SignatureVerificationStats.Builder()
111                 .setKeyFetchLatency(UNSET)
112                 .setSerializationLatency(UNSET)
113                 .setVerificationLatency(UNSET)
114                 .setNumOfKeysFetched(UNSET)
115                 .setSignatureVerificationStatus(VerificationStatus.UNKNOWN)
116                 .setFailedSignatureSellerEnrollmentId(EMPTY_STRING)
117                 .setFailedSignatureBuyerEnrollmentId(EMPTY_STRING)
118                 .setFailedSignatureCallerPackageName(EMPTY_STRING)
119                 .setFailureDetailUnknownError(UNSET)
120                 .setFailureDetailNoEnrollmentDataForBuyer(UNSET)
121                 .setFailureDetailNoKeysFetchedForBuyer(UNSET)
122                 .setFailureDetailWrongSignatureFormat(UNSET)
123                 .setFailureDetailCountOfKeysWithWrongFormat(UNSET)
124                 .setFailureDetailCountOfKeysFailedToVerifySignature(UNSET);
125     }
126 
127     /** Builder class for SignatureVerificationStats */
128     @AutoValue.Builder
129     public abstract static class Builder {
130         /** Sets the latency of fetching the key from key management store */
131         @NonNull
setKeyFetchLatency(long keyFetchLatency)132         public abstract Builder setKeyFetchLatency(long keyFetchLatency);
133 
134         /** Sets the latency of the serialization of a given {@link SignedContextualAds} object */
135         @NonNull
setSerializationLatency(long serializationLatency)136         public abstract Builder setSerializationLatency(long serializationLatency);
137 
138         /**
139          * Sets the latency of the verifying the signature (only the signature verification, without
140          * including the serialization and key fetch)
141          */
142         @NonNull
setVerificationLatency(long verificationLatency)143         public abstract Builder setVerificationLatency(long verificationLatency);
144 
145         /** Sets number of keys fetched for a given ad tech */
146         @NonNull
setNumOfKeysFetched(int value)147         public abstract Builder setNumOfKeysFetched(int value);
148 
149         /**
150          * Sets the end status of the verification request
151          *
152          * <p>A verification request can end in success or a failure with various reasons
153          */
154         @NonNull
setSignatureVerificationStatus( VerificationStatus verificationStatus)155         public abstract Builder setSignatureVerificationStatus(
156                 VerificationStatus verificationStatus);
157 
158         /** Sets enrollment id of the buyer that has a signature verification failure */
159         @NonNull
setFailedSignatureBuyerEnrollmentId(String value)160         public abstract Builder setFailedSignatureBuyerEnrollmentId(String value);
161 
162         /** Sets enrollment id of the seller that has a signature verification failure */
163         @NonNull
setFailedSignatureSellerEnrollmentId(String value)164         public abstract Builder setFailedSignatureSellerEnrollmentId(String value);
165 
166         /** Sets caller package name that has a signature verification failure */
167         @NonNull
setFailedSignatureCallerPackageName(String value)168         public abstract Builder setFailedSignatureCallerPackageName(String value);
169 
170         /** Sets number of unknown error */
171         @NonNull
setFailureDetailUnknownError(int value)172         public abstract Builder setFailureDetailUnknownError(int value);
173 
174         /** Sets number of enrollment data not found for the buyer or was null. */
175         @NonNull
setFailureDetailNoEnrollmentDataForBuyer(int value)176         public abstract Builder setFailureDetailNoEnrollmentDataForBuyer(int value);
177 
178         /** Sets zero keys fetched for the buyer */
179         @NonNull
setFailureDetailNoKeysFetchedForBuyer(int value)180         public abstract Builder setFailureDetailNoKeysFetchedForBuyer(int value);
181 
182         /** Sets signature is not formatted correctly */
183         @NonNull
setFailureDetailWrongSignatureFormat(int value)184         public abstract Builder setFailureDetailWrongSignatureFormat(int value);
185 
186         /** Sets number of keys that has the wrong format */
187         @NonNull
setFailureDetailCountOfKeysWithWrongFormat(int value)188         public abstract Builder setFailureDetailCountOfKeysWithWrongFormat(int value);
189 
190         /** Sets number of keys that failed to verify the signature */
191         @NonNull
setFailureDetailCountOfKeysFailedToVerifySignature(int value)192         public abstract Builder setFailureDetailCountOfKeysFailedToVerifySignature(int value);
193 
194         /** Build the {@link SignatureVerificationStats} */
195         @NonNull
build()196         public abstract SignatureVerificationStats build();
197     }
198 }
199