1 /* 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 17 package android.service.gatekeeper; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 22 import com.android.internal.annotations.VisibleForTesting; 23 24 /** 25 * Response object for a GateKeeper verification request. 26 * @hide 27 */ 28 public 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 public static final GateKeeperResponse ERROR = createGenericResponse(RESPONSE_ERROR); 35 36 private final int mResponseCode; 37 38 private int mTimeout; 39 private byte[] mPayload; 40 private boolean mShouldReEnroll; 41 42 /** Default constructor for response with generic response code **/ GateKeeperResponse(int responseCode)43 private GateKeeperResponse(int responseCode) { 44 mResponseCode = responseCode; 45 } 46 47 @VisibleForTesting createGenericResponse(int responseCode)48 public static GateKeeperResponse createGenericResponse(int responseCode) { 49 return new GateKeeperResponse(responseCode); 50 } 51 createRetryResponse(int timeout)52 private static GateKeeperResponse createRetryResponse(int timeout) { 53 GateKeeperResponse response = new GateKeeperResponse(RESPONSE_RETRY); 54 response.mTimeout = timeout; 55 return response; 56 } 57 58 @VisibleForTesting createOkResponse(byte[] payload, boolean shouldReEnroll)59 public static GateKeeperResponse createOkResponse(byte[] payload, boolean shouldReEnroll) { 60 GateKeeperResponse response = new GateKeeperResponse(RESPONSE_OK); 61 response.mPayload = payload; 62 response.mShouldReEnroll = shouldReEnroll; 63 return response; 64 } 65 66 @Override describeContents()67 public int describeContents() { 68 return 0; 69 } 70 71 public static final @android.annotation.NonNull Parcelable.Creator<GateKeeperResponse> CREATOR 72 = new Parcelable.Creator<GateKeeperResponse>() { 73 @Override 74 public GateKeeperResponse createFromParcel(Parcel source) { 75 int responseCode = source.readInt(); 76 final GateKeeperResponse response; 77 if (responseCode == RESPONSE_RETRY) { 78 response = createRetryResponse(source.readInt()); 79 } else if (responseCode == RESPONSE_OK) { 80 final boolean shouldReEnroll = source.readInt() == 1; 81 byte[] payload = null; 82 int size = source.readInt(); 83 if (size > 0) { 84 payload = new byte[size]; 85 source.readByteArray(payload); 86 } 87 response = createOkResponse(payload, shouldReEnroll); 88 } else { 89 response = createGenericResponse(responseCode); 90 } 91 return response; 92 } 93 94 @Override 95 public GateKeeperResponse[] newArray(int size) { 96 return new GateKeeperResponse[size]; 97 } 98 99 }; 100 101 @Override writeToParcel(Parcel dest, int flags)102 public void writeToParcel(Parcel dest, int flags) { 103 dest.writeInt(mResponseCode); 104 if (mResponseCode == RESPONSE_RETRY) { 105 dest.writeInt(mTimeout); 106 } else if (mResponseCode == RESPONSE_OK) { 107 dest.writeInt(mShouldReEnroll ? 1 : 0); 108 if (mPayload != null) { 109 dest.writeInt(mPayload.length); 110 dest.writeByteArray(mPayload); 111 } else { 112 dest.writeInt(0); 113 } 114 } 115 } 116 getPayload()117 public byte[] getPayload() { 118 return mPayload; 119 } 120 getTimeout()121 public int getTimeout() { 122 return mTimeout; 123 } 124 getShouldReEnroll()125 public boolean getShouldReEnroll() { 126 return mShouldReEnroll; 127 } 128 getResponseCode()129 public int getResponseCode() { 130 return mResponseCode; 131 } 132 } 133