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.ohttp;
18 
19 import com.google.auto.value.AutoValue;
20 import com.google.common.io.BaseEncoding;
21 
22 /**
23  * Holds the OHTTP request context which contains the key config, the shared secret and the hpke
24  * context
25  *
26  * <p>Clients need to save this request context to be able to decrypt the response from the server
27  */
28 @AutoValue
29 public abstract class ObliviousHttpRequestContext {
30 
31     /** Returns the Oblivious HTTP key config */
keyConfig()32     public abstract ObliviousHttpKeyConfig keyConfig();
33 
34     /** Returns the encapsulated shared secret generated by the HPKE setup base operation */
encapsulatedSharedSecret()35     public abstract EncapsulatedSharedSecret encapsulatedSharedSecret();
36 
37     /** The random seed that was used to construct the Hpke context */
seed()38     public abstract byte[] seed();
39 
40     /** Returns whether it uses the changed media type */
hasMediaTypeChanged()41     public abstract boolean hasMediaTypeChanged();
42 
43     /** Create a OHTTP request context object */
create( ObliviousHttpKeyConfig keyConfig, EncapsulatedSharedSecret enc, byte[] seed, boolean hasMediaTypeChanged)44     public static ObliviousHttpRequestContext create(
45             ObliviousHttpKeyConfig keyConfig,
46             EncapsulatedSharedSecret enc,
47             byte[] seed,
48             boolean hasMediaTypeChanged) {
49         return new AutoValue_ObliviousHttpRequestContext(keyConfig, enc, seed, hasMediaTypeChanged);
50     }
51 
52     /** Serialize the byte array seed into a tring */
serializeSeed(byte[] seed)53     public static String serializeSeed(byte[] seed) {
54         return BaseEncoding.base16().lowerCase().encode(seed);
55     }
56 
57     /** Deserialize the seed string into its byte array */
deserializeSeed(String serializedSeed)58     public static byte[] deserializeSeed(String serializedSeed) {
59         return BaseEncoding.base16().lowerCase().decode(serializedSeed);
60     }
61 }
62