blob: 66fee1e90affd6367420a0ed13f80b164c69d751 [file] [log] [blame]
Andres Morales23974272015-05-14 22:42:26 -07001/*
2 * Copyright (C) 2015 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
17package android.service.gatekeeper;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
Rubin Xu0cbc19e2016-12-09 14:00:21 +000022import com.android.internal.annotations.VisibleForTesting;
23
Andres Morales23974272015-05-14 22:42:26 -070024/**
25 * Response object for a GateKeeper verification request.
26 * @hide
27 */
28public final class GateKeeperResponse implements Parcelable {
29
30 public static final int RESPONSE_ERROR = -1;
31 public static final int RESPONSE_OK = 0;
32 public static final int RESPONSE_RETRY = 1;
33
34 private final int mResponseCode;
35
36 private int mTimeout;
37 private byte[] mPayload;
38 private boolean mShouldReEnroll;
39
Rubin Xu0cbc19e2016-12-09 14:00:21 +000040 /** Default constructor for response with generic response code **/
Andres Morales23974272015-05-14 22:42:26 -070041 private GateKeeperResponse(int responseCode) {
42 mResponseCode = responseCode;
43 }
44
Rubin Xu0cbc19e2016-12-09 14:00:21 +000045 @VisibleForTesting
46 public static GateKeeperResponse createGenericResponse(int responseCode) {
47 return new GateKeeperResponse(responseCode);
48 }
49
50 private static GateKeeperResponse createRetryResponse(int timeout) {
51 GateKeeperResponse response = new GateKeeperResponse(RESPONSE_RETRY);
52 response.mTimeout = timeout;
53 return response;
54 }
55
56 @VisibleForTesting
57 public static GateKeeperResponse createOkResponse(byte[] payload, boolean shouldReEnroll) {
58 GateKeeperResponse response = new GateKeeperResponse(RESPONSE_OK);
59 response.mPayload = payload;
60 response.mShouldReEnroll = shouldReEnroll;
61 return response;
Andres Morales23974272015-05-14 22:42:26 -070062 }
63
64 @Override
65 public int describeContents() {
66 return 0;
67 }
68
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -070069 public static final @android.annotation.NonNull Parcelable.Creator<GateKeeperResponse> CREATOR
Andres Morales23974272015-05-14 22:42:26 -070070 = new Parcelable.Creator<GateKeeperResponse>() {
71 @Override
72 public GateKeeperResponse createFromParcel(Parcel source) {
73 int responseCode = source.readInt();
Rubin Xu0cbc19e2016-12-09 14:00:21 +000074 final GateKeeperResponse response;
Andres Morales23974272015-05-14 22:42:26 -070075 if (responseCode == RESPONSE_RETRY) {
Rubin Xu0cbc19e2016-12-09 14:00:21 +000076 response = createRetryResponse(source.readInt());
Andres Morales23974272015-05-14 22:42:26 -070077 } else if (responseCode == RESPONSE_OK) {
Rubin Xu0cbc19e2016-12-09 14:00:21 +000078 final boolean shouldReEnroll = source.readInt() == 1;
79 byte[] payload = null;
Andres Morales23974272015-05-14 22:42:26 -070080 int size = source.readInt();
81 if (size > 0) {
Rubin Xu0cbc19e2016-12-09 14:00:21 +000082 payload = new byte[size];
Andres Morales23974272015-05-14 22:42:26 -070083 source.readByteArray(payload);
Andres Morales23974272015-05-14 22:42:26 -070084 }
Rubin Xu0cbc19e2016-12-09 14:00:21 +000085 response = createOkResponse(payload, shouldReEnroll);
86 } else {
87 response = createGenericResponse(responseCode);
Andres Morales23974272015-05-14 22:42:26 -070088 }
89 return response;
90 }
91
92 @Override
93 public GateKeeperResponse[] newArray(int size) {
94 return new GateKeeperResponse[size];
95 }
96
97 };
98
99 @Override
100 public void writeToParcel(Parcel dest, int flags) {
101 dest.writeInt(mResponseCode);
102 if (mResponseCode == RESPONSE_RETRY) {
103 dest.writeInt(mTimeout);
104 } else if (mResponseCode == RESPONSE_OK) {
105 dest.writeInt(mShouldReEnroll ? 1 : 0);
106 if (mPayload != null) {
107 dest.writeInt(mPayload.length);
108 dest.writeByteArray(mPayload);
Charles Hee74cae82017-07-14 14:41:06 +0100109 } else {
110 dest.writeInt(0);
Andres Morales23974272015-05-14 22:42:26 -0700111 }
112 }
113 }
114
115 public byte[] getPayload() {
116 return mPayload;
117 }
118
119 public int getTimeout() {
120 return mTimeout;
121 }
122
123 public boolean getShouldReEnroll() {
124 return mShouldReEnroll;
125 }
126
127 public int getResponseCode() {
128 return mResponseCode;
129 }
Andres Morales23974272015-05-14 22:42:26 -0700130}