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.adselection.encryption; 18 19 import android.annotation.IntDef; 20 21 import com.google.auto.value.AutoValue; 22 23 import java.lang.annotation.Retention; 24 import java.lang.annotation.RetentionPolicy; 25 import java.util.Set; 26 27 /** Represents the encryption key returned by the key fetch server and used during ad selection. */ 28 @AutoValue 29 public abstract class AdSelectionEncryptionKey { 30 @IntDef( 31 value = { 32 AdSelectionEncryptionKeyType.UNASSIGNED, 33 AdSelectionEncryptionKeyType.AUCTION, 34 AdSelectionEncryptionKeyType.JOIN, 35 }) 36 @Retention(RetentionPolicy.SOURCE) 37 public @interface AdSelectionEncryptionKeyType { 38 int UNASSIGNED = 0; 39 int AUCTION = 1; 40 int JOIN = 3; 41 } 42 43 public static final Set<Integer> VALID_AD_SELECTION_ENCRYPTION_KEY_TYPES = 44 Set.of(AdSelectionEncryptionKeyType.AUCTION, AdSelectionEncryptionKeyType.JOIN); 45 46 /** Encryption key type of this key. */ 47 @AdSelectionEncryptionKeyType keyType()48 public abstract int keyType(); 49 /** Key identifier used to uniquely identify key of this key type. */ keyIdentifier()50 public abstract String keyIdentifier(); 51 52 /** The public key of the asymmetric key pair as bytes sent by the key server. */ 53 @SuppressWarnings("mutable") publicKey()54 public abstract byte[] publicKey(); 55 56 /** Method to create a builder for this key. */ builder()57 public static Builder builder() { 58 return new AutoValue_AdSelectionEncryptionKey.Builder(); 59 } 60 61 /** Builder for AdSelectionEncryptionKey. */ 62 @AutoValue.Builder 63 public abstract static class Builder { 64 /** Sets the key type for this encryption key. */ setKeyType(@dSelectionEncryptionKeyType int keyType)65 public abstract Builder setKeyType(@AdSelectionEncryptionKeyType int keyType); 66 67 /** Set the identifier for this encryption key. */ setKeyIdentifier(String keyIdentifier)68 public abstract Builder setKeyIdentifier(String keyIdentifier); 69 70 /** Set the public key for this encryption key. */ setPublicKey(byte[] publicKey)71 public abstract Builder setPublicKey(byte[] publicKey); 72 73 /** Builds the key. */ build()74 public abstract AdSelectionEncryptionKey build(); 75 } 76 } 77