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.data.adselection; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 22 import androidx.room.ColumnInfo; 23 import androidx.room.Entity; 24 import androidx.room.Index; 25 import androidx.room.TypeConverters; 26 27 import com.android.adservices.data.common.FledgeRoomConverters; 28 29 import com.google.auto.value.AutoValue; 30 31 import java.time.Instant; 32 33 /** Table representing Encryption Context. */ 34 @AutoValue 35 @AutoValue.CopyAnnotations 36 @Entity( 37 tableName = "encryption_context", 38 indices = {@Index(value = {"creation_instant"})}, 39 primaryKeys = {"context_id", "encryption_key_type"}) 40 @TypeConverters({FledgeRoomConverters.class}) 41 public abstract class DBEncryptionContext { 42 43 /** The id associated with this encryption context. */ 44 @AutoValue.CopyAnnotations 45 @ColumnInfo(name = "context_id") getContextId()46 public abstract long getContextId(); 47 48 /** The key type associated with this encryption context. */ 49 @AutoValue.CopyAnnotations 50 @ColumnInfo(name = "encryption_key_type") 51 @EncryptionKeyConstants.EncryptionKeyType getEncryptionKeyType()52 public abstract int getEncryptionKeyType(); 53 54 /** The key config associated with this encryption context. */ 55 @AutoValue.CopyAnnotations 56 @ColumnInfo(name = "key_config", typeAffinity = ColumnInfo.BLOB) getKeyConfig()57 public abstract byte[] getKeyConfig(); 58 59 /** The encapsulated shared secret generated for this context. */ 60 @AutoValue.CopyAnnotations 61 @ColumnInfo(name = "shared_secret", typeAffinity = ColumnInfo.BLOB) getSharedSecret()62 public abstract byte[] getSharedSecret(); 63 64 /** The seed bytes that will be used to regenerate HPKE context. */ 65 @AutoValue.CopyAnnotations 66 @ColumnInfo(name = "seed", typeAffinity = ColumnInfo.BLOB) getSeed()67 public abstract byte[] getSeed(); 68 69 /** The creation time for this ad selection run. */ 70 @AutoValue.CopyAnnotations 71 @Nullable 72 @ColumnInfo(name = "creation_instant", defaultValue = "CURRENT_TIMESTAMP") getCreationInstant()73 public abstract Instant getCreationInstant(); 74 75 /** The id associated with this encryption context. */ 76 @AutoValue.CopyAnnotations 77 @ColumnInfo(name = "has_media_type_changed", defaultValue = "false") getHasMediaTypeChanged()78 public abstract boolean getHasMediaTypeChanged(); 79 80 /** 81 * Returns an AutoValue builder for a {@link 82 * com.android.adservices.data.adselection.DBEncryptionContext} entity. 83 */ builder()84 public static DBEncryptionContext.Builder builder() { 85 return new AutoValue_DBEncryptionContext.Builder(); 86 } 87 88 /** Creates a {@link DBEncryptionContext} using the builder. */ create( long contextId, @EncryptionKeyConstants.EncryptionKeyType int encryptionKeyType, @NonNull Instant creationInstant, byte[] keyConfig, byte[] sharedSecret, byte[] seed, boolean hasMediaTypeChanged)89 public static DBEncryptionContext create( 90 long contextId, 91 @EncryptionKeyConstants.EncryptionKeyType int encryptionKeyType, 92 @NonNull Instant creationInstant, 93 byte[] keyConfig, 94 byte[] sharedSecret, 95 byte[] seed, 96 boolean hasMediaTypeChanged) { 97 return builder() 98 .setContextId(contextId) 99 .setEncryptionKeyType(encryptionKeyType) 100 .setCreationInstant(creationInstant) 101 .setKeyConfig(keyConfig) 102 .setSharedSecret(sharedSecret) 103 .setSeed(seed) 104 .setHasMediaTypeChanged(hasMediaTypeChanged) 105 .build(); 106 } 107 108 /** Builder class for a {@link com.android.adservices.data.adselection.DBEncryptionContext}. */ 109 @AutoValue.Builder 110 public abstract static class Builder { 111 112 /** Sets ad selection id. */ setContextId(long contextId)113 public abstract Builder setContextId(long contextId); 114 115 /** Sets encryption key type. */ setEncryptionKeyType( @ncryptionKeyConstants.EncryptionKeyType int encryptionKeyType)116 public abstract Builder setEncryptionKeyType( 117 @EncryptionKeyConstants.EncryptionKeyType int encryptionKeyType); 118 119 /** Key Config associated with this encryption context. */ setKeyConfig(byte[] keyConfig)120 public abstract Builder setKeyConfig(byte[] keyConfig); 121 122 /** Encapsulated shared secret for this encryption context. */ setSharedSecret(byte[] sharedSecret)123 public abstract Builder setSharedSecret(byte[] sharedSecret); 124 125 /** The seed required to regenerate HPKE context. */ setSeed(byte[] seed)126 public abstract Builder setSeed(byte[] seed); 127 128 /** Sets the creation instant. */ setCreationInstant(@onNull Instant value)129 public abstract Builder setCreationInstant(@NonNull Instant value); 130 131 /** Set whether is uses media type changed. */ setHasMediaTypeChanged(boolean hasMediaTypeChanged)132 public abstract Builder setHasMediaTypeChanged(boolean hasMediaTypeChanged); 133 134 /** Builds the DBEncryptionContext. */ build()135 public abstract DBEncryptionContext build(); 136 } 137 } 138