-
Notifications
You must be signed in to change notification settings - Fork 46
Fix for SDL violation in device pop scenarios #2744
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
somalaya
wants to merge
8
commits into
dev
Choose a base branch
from
somalaya/devicePopSdlFix
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
66a3fc9
initial fix for sdl violation in device pop scenarios
somalaya 16c21b1
added telemetry to track mint signed access token success
somalaya 2ffa9e8
skip encryption padding when no encryption purpose passed
somalaya 895331e
addressed comments
somalaya 8da2828
added param
somalaya d64073d
refactored API 23+ and 28+ methods
somalaya 76549a8
UT added
somalaya 9691151
Merge branch 'dev' into somalaya/devicePopSdlFix
somalaya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,8 @@ | |
|
|
||
| import com.microsoft.identity.common.java.crypto.IKeyStoreKeyManager; | ||
| import com.microsoft.identity.common.java.crypto.SecureHardwareState; | ||
| import com.microsoft.identity.common.java.flighting.CommonFlight; | ||
| import com.microsoft.identity.common.java.flighting.CommonFlightsManager; | ||
| import com.microsoft.identity.common.java.platform.AbstractDevicePopManager; | ||
| import com.microsoft.identity.common.logging.Logger; | ||
| import com.nimbusds.jose.crypto.impl.RSAKeyUtils; | ||
|
|
@@ -348,18 +350,30 @@ private KeyPairGenerator getInitializedRsaKeyPairGenerator(@androidx.annotation. | |
| * @param trySetAttestationChallenge True if we should attempt to generate an attestation challenge cert. | ||
| * @throws InvalidAlgorithmParameterException | ||
| */ | ||
| private void initialize(@androidx.annotation.NonNull final Context context, | ||
| @androidx.annotation.NonNull final KeyPairGenerator keyPairGenerator, | ||
| private void initialize(@NonNull final Context context, | ||
| @NonNull final KeyPairGenerator keyPairGenerator, | ||
| final int keySize, | ||
| final boolean useStrongbox, | ||
| final boolean enableImport, | ||
| final boolean trySetAttestationChallenge) throws InvalidAlgorithmParameterException { | ||
| final boolean unnecessaryCryptoPurposesDisabled = | ||
| CommonFlightsManager.INSTANCE.getFlightsProvider().isFlightEnabled(CommonFlight.DISABLE_UNNECESSARY_CRYPTO_PURPOSES_FROM_DEVICE_POP_MANAGER); | ||
|
|
||
| int purposes = KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Consider moving the definition of purposes inside each initializeX method — I believe it makes things a little easier to read. |
||
| if (!unnecessaryCryptoPurposesDisabled) { | ||
| // If the flight is not enabled, we can use the key for encryption and decryption. | ||
| purposes |= KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT; | ||
| } | ||
|
|
||
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { | ||
| initializePre23(context, keyPairGenerator, keySize); | ||
| } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { | ||
| initialize23(keyPairGenerator, keySize, useStrongbox, trySetAttestationChallenge); | ||
| initialize23(keyPairGenerator, keySize, useStrongbox, trySetAttestationChallenge, purposes, unnecessaryCryptoPurposesDisabled); | ||
| } else { | ||
| initialize28(keyPairGenerator, keySize, useStrongbox, enableImport, trySetAttestationChallenge); | ||
| if (!unnecessaryCryptoPurposesDisabled && enableImport) { | ||
| purposes |= KeyProperties.PURPOSE_WRAP_KEY; | ||
| } | ||
| initialize28(keyPairGenerator, keySize, useStrongbox, trySetAttestationChallenge, purposes, unnecessaryCryptoPurposesDisabled); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -368,28 +382,12 @@ private void initialize(@androidx.annotation.NonNull final Context context, | |
| private void initialize23(@androidx.annotation.NonNull final KeyPairGenerator keyPairGenerator, | ||
| final int keySize, | ||
| final boolean useStrongbox, | ||
| final boolean trySetAttestationChallenge) throws InvalidAlgorithmParameterException { | ||
| KeyGenParameterSpec.Builder builder; | ||
| builder = new KeyGenParameterSpec.Builder( | ||
| mKeyManager.getKeyAlias(), | ||
| KeyProperties.PURPOSE_SIGN | ||
| | KeyProperties.PURPOSE_VERIFY | ||
| | KeyProperties.PURPOSE_ENCRYPT | ||
| | KeyProperties.PURPOSE_DECRYPT | ||
| ) | ||
| .setKeySize(keySize) | ||
| .setSignaturePaddings( | ||
| KeyProperties.SIGNATURE_PADDING_RSA_PKCS1 | ||
| ) | ||
| .setDigests( | ||
| KeyProperties.DIGEST_NONE, | ||
| KeyProperties.DIGEST_SHA1, | ||
| KeyProperties.DIGEST_SHA256 | ||
| ).setEncryptionPaddings( | ||
| KeyProperties.ENCRYPTION_PADDING_RSA_OAEP, | ||
| KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1 | ||
somalaya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| final boolean trySetAttestationChallenge, | ||
| final int purposes, | ||
| final boolean unnecessaryCryptoPurposesDisabled) throws InvalidAlgorithmParameterException { | ||
| KeyGenParameterSpec.Builder builder = buildCommonKeyGenSpec(keySize, purposes, unnecessaryCryptoPurposesDisabled); | ||
|
|
||
| // API 23-specific conditions: attestation requires API 24+, StrongBox requires API 28+ | ||
| if (trySetAttestationChallenge && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
| builder = setAttestationChallenge(builder); | ||
| } | ||
|
|
@@ -444,29 +442,10 @@ private static KeyGenParameterSpec.Builder applyHardwareIsolation( | |
| private void initialize28(@androidx.annotation.NonNull final KeyPairGenerator keyPairGenerator, | ||
| final int keySize, | ||
| final boolean useStrongbox, | ||
| final boolean enableImport, | ||
| final boolean trySetAttestationChallenge) throws InvalidAlgorithmParameterException { | ||
| int purposes = KeyProperties.PURPOSE_SIGN | ||
| | KeyProperties.PURPOSE_VERIFY | ||
| | KeyProperties.PURPOSE_ENCRYPT | ||
| | KeyProperties.PURPOSE_DECRYPT; | ||
| if (enableImport) { | ||
| purposes |= KeyProperties.PURPOSE_WRAP_KEY; | ||
| } | ||
| KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder( | ||
| mKeyManager.getKeyAlias(), purposes) | ||
| .setKeySize(keySize) | ||
| .setSignaturePaddings( | ||
| KeyProperties.SIGNATURE_PADDING_RSA_PKCS1 | ||
| ) | ||
| .setDigests( | ||
| KeyProperties.DIGEST_NONE, | ||
| KeyProperties.DIGEST_SHA1, | ||
| KeyProperties.DIGEST_SHA256 | ||
| ).setEncryptionPaddings( | ||
| KeyProperties.ENCRYPTION_PADDING_RSA_OAEP, | ||
| KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1 | ||
| ); | ||
| final boolean trySetAttestationChallenge, | ||
| final int purposes, | ||
| final boolean unnecessaryCryptoPurposesDisabled) throws InvalidAlgorithmParameterException { | ||
| KeyGenParameterSpec.Builder builder = buildCommonKeyGenSpec(keySize, purposes, unnecessaryCryptoPurposesDisabled); | ||
|
|
||
| if (trySetAttestationChallenge) { | ||
| builder = setAttestationChallenge(builder); | ||
|
|
@@ -484,6 +463,55 @@ private void initialize28(@androidx.annotation.NonNull final KeyPairGenerator ke | |
| keyPairGenerator.initialize(spec); | ||
| } | ||
|
|
||
| /** | ||
| * Sets encryption paddings on the KeyGenParameterSpec.Builder if encryption purposes are enabled. | ||
| * | ||
| * @param builder The KeyGenParameterSpec.Builder to configure | ||
| * @param unnecessaryCryptoPurposesDisabled When true, encryption paddings are not set to address | ||
| * SDL security requirements by limiting key usage to signing operations only | ||
| */ | ||
| private void setEncryptionPaddingsIfNeeded(@NonNull final KeyGenParameterSpec.Builder builder, | ||
| final boolean unnecessaryCryptoPurposesDisabled) { | ||
| final String methodTag = TAG + ":setEncryptionPaddingsIfNeeded"; | ||
| if (!unnecessaryCryptoPurposesDisabled) { | ||
| Logger.verbose(methodTag, "Adding encryption paddings (RSA_OAEP, RSA_PKCS1) to key specification"); | ||
| builder.setEncryptionPaddings( | ||
| KeyProperties.ENCRYPTION_PADDING_RSA_OAEP, | ||
| KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1 | ||
| ); | ||
| } else { | ||
| Logger.verbose(methodTag, "Skipping encryption paddings due to SDL security restrictions"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Builds the common KeyGenParameterSpec configuration shared between API 23+ and 28+ initialization methods. | ||
| * Setting core configuration : Key alias, purposes, size, signature paddings, digests, and encryption paddings. | ||
| * | ||
| * @param keySize The RSA key size | ||
| * @param purposes The key purposes (signing, encryption, etc.) | ||
| * @param unnecessaryCryptoPurposesDisabled Whether to skip encryption paddings for SDL compliance | ||
| * @return Configured KeyGenParameterSpec.Builder ready for API-specific configuration | ||
| */ | ||
| private KeyGenParameterSpec.Builder buildCommonKeyGenSpec(final int keySize, | ||
| final int purposes, | ||
| final boolean unnecessaryCryptoPurposesDisabled) { | ||
| KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder( | ||
| mKeyManager.getKeyAlias(), purposes) | ||
| .setKeySize(keySize) | ||
| .setSignaturePaddings( | ||
| KeyProperties.SIGNATURE_PADDING_RSA_PKCS1 | ||
| ) | ||
| .setDigests( | ||
| KeyProperties.DIGEST_NONE, | ||
| KeyProperties.DIGEST_SHA1, | ||
| KeyProperties.DIGEST_SHA256 | ||
| ); | ||
|
|
||
| setEncryptionPaddingsIfNeeded(builder, unnecessaryCryptoPurposesDisabled); | ||
|
|
||
| return builder; | ||
| } | ||
|
|
||
| @SuppressLint(NewApi) | ||
| @SuppressWarnings("deprecation") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: ask copilot for a better name :p