@@ -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