blob: b9cb730caae1fb2034ed588af79f12d3af6e0a77 [file] [log] [blame]
Rubin Xu3bf722a2016-12-15 16:07:38 +00001/*
2 * Copyright (C) 2017 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 */
Andrew Scull507d11c2017-05-03 17:19:01 +010016package com.android.server.locksettings;
Rubin Xu3bf722a2016-12-15 16:07:38 +000017
Adrian Roos2adc2632017-09-05 17:01:42 +020018import android.content.Context;
Rubin Xu7b7424b2017-03-31 18:03:20 +010019import android.hardware.weaver.V1_0.IWeaver;
20import android.os.RemoteException;
Adrian Roos7374d3a2017-03-31 14:14:53 -070021import android.os.UserManager;
Rubin Xu3bf722a2016-12-15 16:07:38 +000022import android.util.ArrayMap;
23
24import junit.framework.AssertionFailedError;
25
26import java.nio.ByteBuffer;
27import java.security.NoSuchAlgorithmException;
28import java.security.spec.InvalidKeySpecException;
29import java.util.Arrays;
30
31import javax.crypto.SecretKeyFactory;
32import javax.crypto.spec.PBEKeySpec;
33
34public class MockSyntheticPasswordManager extends SyntheticPasswordManager {
35
Rubin Xu16c823e2017-06-27 14:44:58 +010036 private FakeGateKeeperService mGateKeeper;
Rubin Xu7b7424b2017-03-31 18:03:20 +010037 private IWeaver mWeaverService;
Rubin Xu3bf722a2016-12-15 16:07:38 +000038
Adrian Roos2adc2632017-09-05 17:01:42 +020039 public MockSyntheticPasswordManager(Context context, LockSettingsStorage storage,
Rubin Xu16c823e2017-06-27 14:44:58 +010040 FakeGateKeeperService gatekeeper, UserManager userManager) {
Adrian Roos2adc2632017-09-05 17:01:42 +020041 super(context, storage, userManager);
Rubin Xu3bf722a2016-12-15 16:07:38 +000042 mGateKeeper = gatekeeper;
43 }
44
45 private ArrayMap<String, byte[]> mBlobs = new ArrayMap<>();
46
47 @Override
48 protected byte[] decryptSPBlob(String blobKeyName, byte[] blob, byte[] applicationId) {
49 if (mBlobs.containsKey(blobKeyName) && !Arrays.equals(mBlobs.get(blobKeyName), blob)) {
50 throw new AssertionFailedError("blobKeyName content is overwritten: " + blobKeyName);
51 }
52 ByteBuffer buffer = ByteBuffer.allocate(blob.length);
53 buffer.put(blob, 0, blob.length);
54 buffer.flip();
55 int len;
56 len = buffer.getInt();
57 byte[] data = new byte[len];
58 buffer.get(data);
59 len = buffer.getInt();
60 byte[] appId = new byte[len];
61 buffer.get(appId);
62 long sid = buffer.getLong();
63 if (!Arrays.equals(appId, applicationId)) {
64 throw new AssertionFailedError("Invalid application id");
65 }
66 if (sid != 0 && mGateKeeper.getAuthTokenForSid(sid) == null) {
67 throw new AssertionFailedError("No valid auth token");
68 }
69 return data;
70 }
71
72 @Override
73 protected byte[] createSPBlob(String blobKeyName, byte[] data, byte[] applicationId, long sid) {
74 ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES + data.length + Integer.BYTES
75 + applicationId.length + Long.BYTES);
76 buffer.putInt(data.length);
77 buffer.put(data);
78 buffer.putInt(applicationId.length);
79 buffer.put(applicationId);
80 buffer.putLong(sid);
81 byte[] result = buffer.array();
82 mBlobs.put(blobKeyName, result);
83 return result;
84 }
85
86 @Override
87 protected void destroySPBlobKey(String keyAlias) {
88 }
89
90 @Override
91 protected long sidFromPasswordHandle(byte[] handle) {
Rubin Xu16c823e2017-06-27 14:44:58 +010092 return new FakeGateKeeperService.VerifyHandle(handle).sid;
Rubin Xu3bf722a2016-12-15 16:07:38 +000093 }
94
95 @Override
Rich Canningsf64ec632019-02-21 12:40:36 -080096 protected byte[] scrypt(byte[] password, byte[] salt, int n, int r, int p, int outLen) {
Rubin Xu3bf722a2016-12-15 16:07:38 +000097 try {
Rich Canningsf64ec632019-02-21 12:40:36 -080098 char[] passwordChars = new char[password.length];
99 for (int i = 0; i < password.length; i++) {
100 passwordChars[i] = (char) password[i];
101 }
102 PBEKeySpec spec = new PBEKeySpec(passwordChars, salt, 10, outLen * 8);
Rubin Xu3bf722a2016-12-15 16:07:38 +0000103 SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
104 return f.generateSecret(spec).getEncoded();
105 } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
106 e.printStackTrace();
107 return null;
108 }
109 }
110
Rubin Xu7b7424b2017-03-31 18:03:20 +0100111 @Override
112 protected IWeaver getWeaverService() throws RemoteException {
113 return mWeaverService;
114 }
115
116 public void enableWeaver() {
117 mWeaverService = new MockWeaverService();
118 initWeaverService();
119 }
120
Rubin Xu3bf722a2016-12-15 16:07:38 +0000121}