blob: 5a3f12c5b4ed9612fb7a07447349cc722b1511e5 [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;
David Anderson28dea682019-02-20 13:37:51 -080038 private PasswordSlotManagerTestable mPasswordSlotManager;
Rubin Xu3bf722a2016-12-15 16:07:38 +000039
Adrian Roos2adc2632017-09-05 17:01:42 +020040 public MockSyntheticPasswordManager(Context context, LockSettingsStorage storage,
David Anderson28dea682019-02-20 13:37:51 -080041 FakeGateKeeperService gatekeeper, UserManager userManager,
42 PasswordSlotManager passwordSlotManager) {
43 super(context, storage, userManager, passwordSlotManager);
Rubin Xu3bf722a2016-12-15 16:07:38 +000044 mGateKeeper = gatekeeper;
45 }
46
47 private ArrayMap<String, byte[]> mBlobs = new ArrayMap<>();
48
49 @Override
50 protected byte[] decryptSPBlob(String blobKeyName, byte[] blob, byte[] applicationId) {
51 if (mBlobs.containsKey(blobKeyName) && !Arrays.equals(mBlobs.get(blobKeyName), blob)) {
52 throw new AssertionFailedError("blobKeyName content is overwritten: " + blobKeyName);
53 }
54 ByteBuffer buffer = ByteBuffer.allocate(blob.length);
55 buffer.put(blob, 0, blob.length);
56 buffer.flip();
57 int len;
58 len = buffer.getInt();
59 byte[] data = new byte[len];
60 buffer.get(data);
61 len = buffer.getInt();
62 byte[] appId = new byte[len];
63 buffer.get(appId);
64 long sid = buffer.getLong();
65 if (!Arrays.equals(appId, applicationId)) {
66 throw new AssertionFailedError("Invalid application id");
67 }
68 if (sid != 0 && mGateKeeper.getAuthTokenForSid(sid) == null) {
69 throw new AssertionFailedError("No valid auth token");
70 }
71 return data;
72 }
73
74 @Override
75 protected byte[] createSPBlob(String blobKeyName, byte[] data, byte[] applicationId, long sid) {
76 ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES + data.length + Integer.BYTES
77 + applicationId.length + Long.BYTES);
78 buffer.putInt(data.length);
79 buffer.put(data);
80 buffer.putInt(applicationId.length);
81 buffer.put(applicationId);
82 buffer.putLong(sid);
83 byte[] result = buffer.array();
84 mBlobs.put(blobKeyName, result);
85 return result;
86 }
87
88 @Override
89 protected void destroySPBlobKey(String keyAlias) {
90 }
91
92 @Override
93 protected long sidFromPasswordHandle(byte[] handle) {
Rubin Xu16c823e2017-06-27 14:44:58 +010094 return new FakeGateKeeperService.VerifyHandle(handle).sid;
Rubin Xu3bf722a2016-12-15 16:07:38 +000095 }
96
97 @Override
Rich Canningsf64ec632019-02-21 12:40:36 -080098 protected byte[] scrypt(byte[] password, byte[] salt, int n, int r, int p, int outLen) {
Rubin Xu3bf722a2016-12-15 16:07:38 +000099 try {
Rich Canningsf64ec632019-02-21 12:40:36 -0800100 char[] passwordChars = new char[password.length];
101 for (int i = 0; i < password.length; i++) {
102 passwordChars[i] = (char) password[i];
103 }
104 PBEKeySpec spec = new PBEKeySpec(passwordChars, salt, 10, outLen * 8);
Rubin Xu3bf722a2016-12-15 16:07:38 +0000105 SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
106 return f.generateSecret(spec).getEncoded();
107 } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
108 e.printStackTrace();
109 return null;
110 }
111 }
112
Rubin Xu7b7424b2017-03-31 18:03:20 +0100113 @Override
114 protected IWeaver getWeaverService() throws RemoteException {
115 return mWeaverService;
116 }
117
118 public void enableWeaver() {
119 mWeaverService = new MockWeaverService();
120 initWeaverService();
121 }
Rubin Xu3bf722a2016-12-15 16:07:38 +0000122}