blob: ad6020c0846cadddc0fc626a129fdde4cf22de5b [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 com.android.internal.widget;
18
19import android.os.Parcel;
20import android.os.Parcelable;
Adrian Roos7374d3a2017-03-31 14:14:53 -070021import android.service.gatekeeper.GateKeeperResponse;
22import android.util.Slog;
Andres Morales23974272015-05-14 22:42:26 -070023
24/**
25 * Response object for a ILockSettings credential verification request.
26 * @hide
27 */
28public final class VerifyCredentialResponse 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 VerifyCredentialResponse OK = new VerifyCredentialResponse();
35 public static final VerifyCredentialResponse ERROR
36 = new VerifyCredentialResponse(RESPONSE_ERROR, 0, null);
Adrian Roos7374d3a2017-03-31 14:14:53 -070037 private static final String TAG = "VerifyCredentialResponse";
Andres Morales23974272015-05-14 22:42:26 -070038
39 private int mResponseCode;
40 private byte[] mPayload;
41 private int mTimeout;
42
43 public static final Parcelable.Creator<VerifyCredentialResponse> CREATOR
44 = new Parcelable.Creator<VerifyCredentialResponse>() {
45 @Override
46 public VerifyCredentialResponse createFromParcel(Parcel source) {
47 int responseCode = source.readInt();
48 VerifyCredentialResponse response = new VerifyCredentialResponse(responseCode, 0, null);
49 if (responseCode == RESPONSE_RETRY) {
50 response.setTimeout(source.readInt());
51 } else if (responseCode == RESPONSE_OK) {
52 int size = source.readInt();
53 if (size > 0) {
54 byte[] payload = new byte[size];
55 source.readByteArray(payload);
56 response.setPayload(payload);
57 }
58 }
59 return response;
60 }
61
62 @Override
63 public VerifyCredentialResponse[] newArray(int size) {
64 return new VerifyCredentialResponse[size];
65 }
66
67 };
68
69 public VerifyCredentialResponse() {
70 mResponseCode = RESPONSE_OK;
71 mPayload = null;
72 }
73
74
75 public VerifyCredentialResponse(byte[] payload) {
76 mPayload = payload;
77 mResponseCode = RESPONSE_OK;
78 }
79
80 public VerifyCredentialResponse(int timeout) {
81 mTimeout = timeout;
82 mResponseCode = RESPONSE_RETRY;
83 mPayload = null;
84 }
85
86 private VerifyCredentialResponse(int responseCode, int timeout, byte[] payload) {
87 mResponseCode = responseCode;
88 mTimeout = timeout;
89 mPayload = payload;
90 }
91
92 @Override
93 public void writeToParcel(Parcel dest, int flags) {
94 dest.writeInt(mResponseCode);
95 if (mResponseCode == RESPONSE_RETRY) {
96 dest.writeInt(mTimeout);
97 } else if (mResponseCode == RESPONSE_OK) {
98 if (mPayload != null) {
99 dest.writeInt(mPayload.length);
100 dest.writeByteArray(mPayload);
101 }
102 }
103 }
104
105 @Override
106 public int describeContents() {
107 return 0;
108 }
109
110 public byte[] getPayload() {
111 return mPayload;
112 }
113
114 public int getTimeout() {
115 return mTimeout;
116 }
117
118 public int getResponseCode() {
119 return mResponseCode;
120 }
121
122 private void setTimeout(int timeout) {
123 mTimeout = timeout;
124 }
125
126 private void setPayload(byte[] payload) {
127 mPayload = payload;
128 }
Adrian Roos7374d3a2017-03-31 14:14:53 -0700129
130 public VerifyCredentialResponse stripPayload() {
131 return new VerifyCredentialResponse(mResponseCode, mTimeout, new byte[0]);
132 }
133
134 public static VerifyCredentialResponse fromGateKeeperResponse(
135 GateKeeperResponse gateKeeperResponse) {
136 VerifyCredentialResponse response;
137 int responseCode = gateKeeperResponse.getResponseCode();
138 if (responseCode == GateKeeperResponse.RESPONSE_RETRY) {
139 response = new VerifyCredentialResponse(gateKeeperResponse.getTimeout());
140 } else if (responseCode == GateKeeperResponse.RESPONSE_OK) {
141 byte[] token = gateKeeperResponse.getPayload();
142 if (token == null) {
143 // something's wrong if there's no payload with a challenge
144 Slog.e(TAG, "verifyChallenge response had no associated payload");
145 response = VerifyCredentialResponse.ERROR;
146 } else {
147 response = new VerifyCredentialResponse(token);
148 }
149 } else {
150 response = VerifyCredentialResponse.ERROR;
151 }
152 return response;
153 }
Andres Morales23974272015-05-14 22:42:26 -0700154}