• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.adservices.service.encryptionkey;
18 
19 import android.net.Uri;
20 
21 import java.util.Objects;
22 
23 /**
24  * POJO represents the encryption key JSON response when calling Adtech encryption key url endpoint.
25  */
26 public class EncryptionKey {
27 
28     private String mId;
29     private KeyType mKeyType;
30     private String mEnrollmentId;
31     private Uri mReportingOrigin;
32     private String mEncryptionKeyUrl;
33     private ProtocolType mProtocolType;
34     private int mKeyCommitmentId;
35     private String mBody;
36     private long mExpiration;
37     private long mLastFetchTime;
38 
EncryptionKey()39     public EncryptionKey() {
40         mId = null;
41         mKeyType = KeyType.ENCRYPTION;
42         mEnrollmentId = null;
43         mReportingOrigin = null;
44         mEncryptionKeyUrl = null;
45         mProtocolType = ProtocolType.HPKE;
46         mKeyCommitmentId = 0;
47         mBody = null;
48         mExpiration = 0L;
49         mLastFetchTime = 0L;
50     }
51 
52     @Override
equals(Object obj)53     public boolean equals(Object obj) {
54         if (!(obj instanceof EncryptionKey)) {
55             return false;
56         }
57         EncryptionKey encryptionKey = (EncryptionKey) obj;
58         return Objects.equals(mId, encryptionKey.mId)
59                 && Objects.equals(mKeyType, encryptionKey.mKeyType)
60                 && Objects.equals(mEnrollmentId, encryptionKey.mEnrollmentId)
61                 && Objects.equals(mReportingOrigin, encryptionKey.mReportingOrigin)
62                 && Objects.equals(mEncryptionKeyUrl, encryptionKey.mEncryptionKeyUrl)
63                 && Objects.equals(mProtocolType, encryptionKey.mProtocolType)
64                 && (mKeyCommitmentId == encryptionKey.mKeyCommitmentId)
65                 && Objects.equals(mBody, encryptionKey.mBody)
66                 && (mExpiration == encryptionKey.mExpiration)
67                 && (mLastFetchTime == encryptionKey.mLastFetchTime);
68     }
69 
70     @Override
hashCode()71     public int hashCode() {
72         return Objects.hash(
73                 mId,
74                 mKeyType,
75                 mEnrollmentId,
76                 mReportingOrigin,
77                 mEncryptionKeyUrl,
78                 mProtocolType,
79                 mKeyCommitmentId,
80                 mBody,
81                 mExpiration,
82                 mLastFetchTime);
83     }
84 
85     /** Returns id for this encryption key, this is the UUID for each key in db table. */
getId()86     public String getId() {
87         return mId;
88     }
89 
90     /** Returns key type for this key commitment. */
getKeyType()91     public KeyType getKeyType() {
92         return mKeyType;
93     }
94 
95     /** Returns enrollment id for the Adtech. */
getEnrollmentId()96     public String getEnrollmentId() {
97         return mEnrollmentId;
98     }
99 
100     /** Returns Adtech reporting origin, set as triggerRegistrationUrl during enrollment. */
getReportingOrigin()101     public Uri getReportingOrigin() {
102         return mReportingOrigin;
103     }
104 
105     /**
106      * Returns the encryption key url endpoint provided by Adtech, we use this endpoint to fetch and
107      * update keys.
108      */
getEncryptionKeyUrl()109     public String getEncryptionKeyUrl() {
110         return mEncryptionKeyUrl;
111     }
112 
113     /** Returns protocol type for this key commitment. */
getProtocolType()114     public ProtocolType getProtocolType() {
115         return mProtocolType;
116     }
117 
118     /** Returns id for this key commitment, this id is unique per adtech. */
getKeyCommitmentId()119     public int getKeyCommitmentId() {
120         return mKeyCommitmentId;
121     }
122 
123     /** Returns base64-encoded public key body. */
getBody()124     public String getBody() {
125         return mBody;
126     }
127 
128     /** Returns expiration time of this public key in milliseconds. */
getExpiration()129     public long getExpiration() {
130         return mExpiration;
131     }
132 
133     /** Returns the last fetch time for this encryption key. */
getLastFetchTime()134     public long getLastFetchTime() {
135         return mLastFetchTime;
136     }
137 
138     /** Returns the builder for the instance */
cloneToBuilder()139     public EncryptionKey.Builder cloneToBuilder() {
140         return new EncryptionKey.Builder()
141                 .setId(this.mId)
142                 .setKeyType(this.mKeyType)
143                 .setEnrollmentId(this.mEnrollmentId)
144                 .setReportingOrigin(this.mReportingOrigin)
145                 .setEncryptionKeyUrl(this.mEncryptionKeyUrl)
146                 .setProtocolType(this.mProtocolType)
147                 .setKeyCommitmentId(this.mKeyCommitmentId)
148                 .setBody(this.mBody)
149                 .setExpiration(this.mExpiration)
150                 .setLastFetchTime(this.mLastFetchTime);
151     }
152 
153     /** Builder for {@link EncryptionKey}. */
154     public static final class Builder {
155         private final EncryptionKey mBuilding;
156 
Builder()157         public Builder() {
158             mBuilding = new EncryptionKey();
159         }
160 
161         /** See {@link EncryptionKey#getId()}. */
setId(String id)162         public Builder setId(String id) {
163             mBuilding.mId = id;
164             return this;
165         }
166 
167         /** See {@link EncryptionKey#getKeyType()}. */
setKeyType(KeyType keyType)168         public Builder setKeyType(KeyType keyType) {
169             mBuilding.mKeyType = keyType;
170             return this;
171         }
172 
173         /** See {@link EncryptionKey#getEnrollmentId()}. */
setEnrollmentId(String enrollmentId)174         public Builder setEnrollmentId(String enrollmentId) {
175             mBuilding.mEnrollmentId = enrollmentId;
176             return this;
177         }
178 
179         /** See {@link EncryptionKey#getReportingOrigin()}. */
setReportingOrigin(Uri reportingOrigin)180         public Builder setReportingOrigin(Uri reportingOrigin) {
181             mBuilding.mReportingOrigin = reportingOrigin;
182             return this;
183         }
184 
185         /** See {@link EncryptionKey#getEncryptionKeyUrl()}. */
setEncryptionKeyUrl(String encryptionKeyUrl)186         public Builder setEncryptionKeyUrl(String encryptionKeyUrl) {
187             mBuilding.mEncryptionKeyUrl = encryptionKeyUrl;
188             return this;
189         }
190 
191         /** See {@link EncryptionKey#getProtocolType()}. */
setProtocolType(ProtocolType protocolType)192         public Builder setProtocolType(ProtocolType protocolType) {
193             mBuilding.mProtocolType = protocolType;
194             return this;
195         }
196 
197         /** See {@link EncryptionKey#getKeyCommitmentId()}. */
setKeyCommitmentId(int keyCommitmentId)198         public Builder setKeyCommitmentId(int keyCommitmentId) {
199             mBuilding.mKeyCommitmentId = keyCommitmentId;
200             return this;
201         }
202 
203         /** See {@link EncryptionKey#getBody()}. */
setBody(String body)204         public Builder setBody(String body) {
205             mBuilding.mBody = body;
206             return this;
207         }
208 
209         /** See {@link EncryptionKey#getExpiration()}. */
setExpiration(long expiration)210         public Builder setExpiration(long expiration) {
211             mBuilding.mExpiration = expiration;
212             return this;
213         }
214 
215         /** See {@link EncryptionKey#getLastFetchTime()}. */
setLastFetchTime(long lastFetchTime)216         public Builder setLastFetchTime(long lastFetchTime) {
217             mBuilding.mLastFetchTime = lastFetchTime;
218             return this;
219         }
220 
221         /** Build the {@link EncryptionKey}. */
build()222         public EncryptionKey build() {
223             return mBuilding;
224         }
225     }
226 
227     // The key type for this key, a key can be either an encryption key or a signing key.
228     public enum KeyType {
229         ENCRYPTION("encryption"),
230         SIGNING("signing");
231 
232         private final String mValue;
233 
KeyType(String value)234         KeyType(String value) {
235             mValue = value;
236         }
237 
getValue()238         public String getValue() {
239             return mValue;
240         }
241     }
242 
243     /**
244      * ProtocolType enumerates the algorithm used with the key. Set as HPKE by default, more
245      * algorithms can be supported in the future.
246      */
247     public enum ProtocolType {
248         // Algorithm used by signing key.
249         ECDSA("ecdsa"),
250         // Algorithm used by Topics encryption key.
251         HPKE("hpke");
252         private final String mValue;
253 
ProtocolType(String value)254         ProtocolType(String value) {
255             mValue = value;
256         }
257 
getValue()258         public String getValue() {
259             return mValue;
260         }
261     }
262 }
263