Skip to content

Commit 23fbf77

Browse files
author
android-build-team Robot
committed
Merge cherrypicks of [3365569, 3365570, 3366860, 3366878, 3365571, 3365572, 3366918, 3365573, 3365589, 3365590, 3366938, 3366902, 3365574, 3365575, 3365576, 3365577, 3366958, 3365824, 3365591, 3366959, 3366960, 3366961, 3366962, 3366963, 3366964, 3366965, 3366919, 3366966, 3366967, 3366968, 3366969, 3366970, 3367018, 3367019, 3365592, 3365593, 3366985, 3365825, 3366988, 3366989, 3366990, 3366991, 3366992, 3366993, 3366994, 3367004, 3367005, 3367006, 3367007, 3367008, 3367009, 3367010, 3367011, 3367012, 3367013, 3367014, 3367015, 3367016, 3367017, 3367038, 3367039, 3367040, 3367041, 3367042, 3367044, 3367045, 3367046, 3367049, 3367050, 3367052, 3367053, 3367054, 3367055, 3367056, 3366920, 3366921, 3366922, 3367079] into oc-mr1-release
Change-Id: Iae843903b50e7df11a333cabfff45861e4a17355
2 parents df0b5ee + 926c144 commit 23fbf77

2 files changed

Lines changed: 38 additions & 10 deletions

File tree

services/core/java/com/android/server/locksettings/SyntheticPasswordCrypto.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,28 @@ public static byte[] decrypt(byte[] keyBytes, byte[] personalisation, byte[] cip
112112
}
113113
}
114114

115-
public static byte[] decryptBlob(String keyAlias, byte[] blob, byte[] applicationId) {
115+
public static byte[] decryptBlobV1(String keyAlias, byte[] blob, byte[] applicationId) {
116116
try {
117117
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
118118
keyStore.load(null);
119119

120120
SecretKey decryptionKey = (SecretKey) keyStore.getKey(keyAlias, null);
121121
byte[] intermediate = decrypt(applicationId, APPLICATION_ID_PERSONALIZATION, blob);
122122
return decrypt(decryptionKey, intermediate);
123+
} catch (Exception e) {
124+
e.printStackTrace();
125+
throw new RuntimeException("Failed to decrypt blob", e);
126+
}
127+
}
128+
129+
public static byte[] decryptBlob(String keyAlias, byte[] blob, byte[] applicationId) {
130+
try {
131+
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
132+
keyStore.load(null);
133+
134+
SecretKey decryptionKey = (SecretKey) keyStore.getKey(keyAlias, null);
135+
byte[] intermediate = decrypt(decryptionKey, blob);
136+
return decrypt(applicationId, APPLICATION_ID_PERSONALIZATION, intermediate);
123137
} catch (CertificateException | IOException | BadPaddingException
124138
| IllegalBlockSizeException
125139
| KeyStoreException | NoSuchPaddingException | NoSuchAlgorithmException
@@ -150,9 +164,8 @@ public static byte[] createBlob(String keyAlias, byte[] data, byte[] application
150164
keyStore.setEntry(keyAlias,
151165
new KeyStore.SecretKeyEntry(secretKey),
152166
builder.build());
153-
byte[] intermediate = encrypt(secretKey, data);
154-
return encrypt(applicationId, APPLICATION_ID_PERSONALIZATION, intermediate);
155-
167+
byte[] intermediate = encrypt(applicationId, APPLICATION_ID_PERSONALIZATION, data);
168+
return encrypt(secretKey, intermediate);
156169
} catch (CertificateException | IOException | BadPaddingException
157170
| IllegalBlockSizeException
158171
| KeyStoreException | NoSuchPaddingException | NoSuchAlgorithmException

services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ public class SyntheticPasswordManager {
101101
private static final byte WEAVER_VERSION = 1;
102102
private static final int INVALID_WEAVER_SLOT = -1;
103103

104-
private static final byte SYNTHETIC_PASSWORD_VERSION = 1;
104+
private static final byte SYNTHETIC_PASSWORD_VERSION_V1 = 1;
105+
private static final byte SYNTHETIC_PASSWORD_VERSION = 2;
105106
private static final byte SYNTHETIC_PASSWORD_PASSWORD_BASED = 0;
106107
private static final byte SYNTHETIC_PASSWORD_TOKEN_BASED = 1;
107108

@@ -792,6 +793,7 @@ public AuthenticationResult unwrapPasswordBasedSyntheticPassword(IGateKeeperServ
792793
byte[] pwdToken = computePasswordToken(credential, pwd);
793794

794795
final byte[] applicationId;
796+
final long sid;
795797
int weaverSlot = loadWeaverSlot(handle, userId);
796798
if (weaverSlot != INVALID_WEAVER_SLOT) {
797799
// Weaver based user password
@@ -804,6 +806,7 @@ public AuthenticationResult unwrapPasswordBasedSyntheticPassword(IGateKeeperServ
804806
if (result.gkResponse.getResponseCode() != VerifyCredentialResponse.RESPONSE_OK) {
805807
return result;
806808
}
809+
sid = GateKeeper.INVALID_SECURE_USER_ID;
807810
applicationId = transformUnderWeaverSecret(pwdToken, result.gkResponse.getPayload());
808811
} else {
809812
byte[] gkPwdToken = passwordTokenToGkInput(pwdToken);
@@ -836,12 +839,13 @@ public AuthenticationResult unwrapPasswordBasedSyntheticPassword(IGateKeeperServ
836839
result.gkResponse = VerifyCredentialResponse.ERROR;
837840
return result;
838841
}
842+
sid = sidFromPasswordHandle(pwd.passwordHandle);
839843
applicationId = transformUnderSecdiscardable(pwdToken,
840844
loadSecdiscardable(handle, userId));
841845
}
842846

843847
result.authToken = unwrapSyntheticPasswordBlob(handle, SYNTHETIC_PASSWORD_PASSWORD_BASED,
844-
applicationId, userId);
848+
applicationId, sid, userId);
845849

846850
// Perform verifyChallenge to refresh auth tokens for GK if user password exists.
847851
result.gkResponse = verifyChallenge(gatekeeper, result.authToken, 0L, userId);
@@ -877,7 +881,7 @@ public AuthenticationResult unwrapPasswordBasedSyntheticPassword(IGateKeeperServ
877881
}
878882
byte[] applicationId = transformUnderSecdiscardable(token, secdiscardable);
879883
result.authToken = unwrapSyntheticPasswordBlob(handle, SYNTHETIC_PASSWORD_TOKEN_BASED,
880-
applicationId, userId);
884+
applicationId, 0L, userId);
881885
if (result.authToken != null) {
882886
result.gkResponse = verifyChallenge(gatekeeper, result.authToken, 0L, userId);
883887
if (result.gkResponse == null) {
@@ -892,19 +896,26 @@ public AuthenticationResult unwrapPasswordBasedSyntheticPassword(IGateKeeperServ
892896
}
893897

894898
private AuthenticationToken unwrapSyntheticPasswordBlob(long handle, byte type,
895-
byte[] applicationId, int userId) {
899+
byte[] applicationId, long sid, int userId) {
896900
byte[] blob = loadState(SP_BLOB_NAME, handle, userId);
897901
if (blob == null) {
898902
return null;
899903
}
900-
if (blob[0] != SYNTHETIC_PASSWORD_VERSION) {
904+
final byte version = blob[0];
905+
if (version != SYNTHETIC_PASSWORD_VERSION && version != SYNTHETIC_PASSWORD_VERSION_V1) {
901906
throw new RuntimeException("Unknown blob version");
902907
}
903908
if (blob[1] != type) {
904909
throw new RuntimeException("Invalid blob type");
905910
}
906-
byte[] secret = decryptSPBlob(getHandleName(handle),
911+
final byte[] secret;
912+
if (version == SYNTHETIC_PASSWORD_VERSION_V1) {
913+
secret = SyntheticPasswordCrypto.decryptBlobV1(getHandleName(handle),
914+
Arrays.copyOfRange(blob, 2, blob.length), applicationId);
915+
} else {
916+
secret = decryptSPBlob(getHandleName(handle),
907917
Arrays.copyOfRange(blob, 2, blob.length), applicationId);
918+
}
908919
if (secret == null) {
909920
Log.e(TAG, "Fail to decrypt SP for user " + userId);
910921
return null;
@@ -919,6 +930,10 @@ private AuthenticationToken unwrapSyntheticPasswordBlob(long handle, byte type,
919930
} else {
920931
result.syntheticPassword = new String(secret);
921932
}
933+
if (version == SYNTHETIC_PASSWORD_VERSION_V1) {
934+
Log.i(TAG, "Upgrade v1 SP blob for user " + userId + ", type = " + type);
935+
createSyntheticPasswordBlob(handle, type, result, applicationId, sid, userId);
936+
}
922937
return result;
923938
}
924939

0 commit comments

Comments
 (0)