• Home
  • 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.ohttp;
18 
19 import com.google.auto.value.AutoValue;
20 
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.util.Arrays;
24 
25 /** Contains the results of Ohttp encryption and the request context required for decryption */
26 @AutoValue
27 public abstract class ObliviousHttpRequest {
28 
29     @SuppressWarnings("mutable")
plainText()30     abstract byte[] plainText();
31 
32     @SuppressWarnings("mutable")
cipherText()33     abstract byte[] cipherText();
34 
35     /** Returns the Oblivious HTTP request context that should be saved for decryption */
requestContext()36     public abstract ObliviousHttpRequestContext requestContext();
37 
38     /** Create a Oblivious HTTP Request object */
create( byte[] plainText, byte[] cipherText, ObliviousHttpRequestContext requestContext)39     public static ObliviousHttpRequest create(
40             byte[] plainText, byte[] cipherText, ObliviousHttpRequestContext requestContext) {
41         return new AutoValue_ObliviousHttpRequest(plainText, cipherText, requestContext);
42     }
43 
44     /** Get the plain text that is encrypted */
getPlainText()45     public byte[] getPlainText() {
46         return Arrays.copyOf(plainText(), plainText().length);
47     }
48 
49     /** Get the encrypted cipher text */
getCipherText()50     public byte[] getCipherText() {
51         return Arrays.copyOf(cipherText(), cipherText().length);
52     }
53 
54     /**
55      * Serialize according to OHTTP spec
56      *
57      * <p>concat(hdr, enc, ct) per
58      * https://www.ietf.org/archive/id/draft-ietf-ohai-ohttp-03.html#name-encapsulation-of-requests
59      */
serialize()60     public byte[] serialize() throws IOException {
61         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
62         if (requestContext().hasMediaTypeChanged()) {
63             outputStream.write((byte) 0); // version byte
64         }
65         outputStream.write(requestContext().keyConfig().serializeOhttpPayloadHeader());
66         outputStream.write(requestContext().encapsulatedSharedSecret().getBytes());
67         outputStream.write(cipherText());
68         return outputStream.toByteArray();
69     }
70 }
71