Commit 3f151f1
Add KeyStoreBackedSecretKeyProvider, Fixes AB#3274176 (#2674)
## Summary
This PR has two main goals: to seamlessly update the encryption padding
used in the Android KeyStore, and to lay the groundwork for integrating
future cryptographic requirements with minimal changes.
---
### Phase 1: Replace `AndroidWrappedKeyProvider` with
`KeyStoreBackedSecretKeyProvider`
- **Controller Flag:** `ENABLE_KEYSTORE_BACKED_SECRET_KEY_PROVIDER`
- **Behavior When Enabled:**
- The factory will return `KeyStoreBackedSecretKeyProvider` instead of
`AndroidWrappedKeyProvider`.
- The new class behaves similarly to the previous one but introduces the
ability to:
- Retrieve the paddings supported by the KeyStore `KeyPair`
- Maintain a list of available ciphers (`oaepCipherSpec` and
`pkcs1CipherSpec`)
- Select a compatible cipher for encryption/decryption based on what the
`KeyPair` supports.
- In this case, if the `KeyPair` only supports `PKCS1`, then all keys
will be encrypted and decrypted using `PKCS1`.
- **Goal:**
- Validate that the new provider functions equivalently to the existing
one.
> Note: No encryption changes are introduced in this phase.
### Tests
- `AndroidWrappedKeyProviderTest` has been parameterized to confirm both
providers behave identically.
- `KeyStoreBackedSecretKeyProviderRolloutTest.java` includes test to
validate migration and rollback
---
### Phase 2: Enable New Wrapped Secret Key Format with Metadata Support
Before introducing new key types, we need to ensure that all new keys
include additional metadata that specifies how they were encrypted.
- **Controller Flag:** `ENABLE_NEW_WRAPPED_SECRET_KEY_FORMAT`
- **Behavior When Enabled:**
- `WrappedSecretKey` will use an enhanced binary format with the
following structure:
- MAGIC BYTES (4 bytes)
- SERIALIZER ID (4bytes)
- Metadata length (4 bytes)
- Metadata (format is decided by the serializer)
- Raw encrypted key bytes
- **Goal:**
- Future-proof the wrapped key by including metadata that specifies:
- Key size
- Algorithm
- Transformation used for encryption
- Key version
- This allows the system to handle multiple key types if formats change
in the future.
- Validate `WrappedSecretKey` can read both new and old keys.
### Tests
- `WrappedSecretKeyTest`
---
### Phase 3: Add OAEP encryption padding.
- **Controller Flag:** `ENABLE_OAEP_WITH_SHA_AND_MGF1_PADDING`
- **Behavior When Enabled:**
- `CryptoParameterSpecFactory` will now return two paddings:
`ENCRYPTION_PADDING_RSA_PKCS1` and `ENCRYPTION_PADDING_RSA_OAEP` for the
KeyGen specifications.
- `KeyStoreBackedSecretKeyProvider` will generate a `KeyPair` that
supports `ENCRYPTION_PADDING_RSA_OAEP`.
- As a result, when a `SecretKey` needs to be wrapped, the provider will
select OAEP, since the `KeyPair` now supports this padding and OAEP
cipher support was already introduced in Phase 1.
- **Goal:**
- All new keys will be encrypted /decrypted with OAEP
- Existing keys will still be encrypted /decrypted with PKCS1
### Tests
- `KeyStoreBackedSecretKeyProviderRolloutTest.java` includes test to
validate migration and rollback
---
### Telemetry
| span_name | key_pair_gen_successful_method |
elapsed_time_keypair_generation | secret_key_wrapping_cipher |
secret_key_wrapping_operation |
|-------------------|----------------------------------------------------------------------------------------------|----------------------------------|------------------------------------------------------------|--------------------------------------------------------------|
| SecretKeyWrapping | null | |
CipherSpec(transformation='RSA/ECB/PKCS1Padding') |
KeyStoreBackedSecretKeyProvider:unwrapSecretKey |
| KeyPairGeneration |
KeyGenSpec(description='modern_spec_with_wrap_key', algorithm='RSA',
encryptionPaddings='[PKCS1Padding]') | 581 | | |
| KeyPairGeneration |
KeyGenSpec(description='modern_spec_with_wrap_key', algorithm='RSA',
encryptionPaddings='[PKCS1Padding]') | 294 | | |
[AB#3274176](https://identitydivision.visualstudio.com/fac9d424-53d2-45c0-91b5-ef6ba7a6bf26/_workitems/edit/3274176)
AzureAD/ad-accounts-for-android#3166
---------
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Sowmya Malayanur <[email protected]>1 parent 4bfba98 commit 3f151f1
File tree
28 files changed
+4304
-46
lines changed- common4j/src/main/com/microsoft/identity/common/java
- flighting
- opentelemetry
- common/src
- androidTest/java/com/microsoft/identity/common
- crypto
- internal/util
- main/java/com/microsoft/identity/common
- crypto
- wrappedsecretkey
- internal/util
- test/java/com/microsoft/identity/common
- crypto
- wrappedsecretkey
- internal/util
28 files changed
+4304
-46
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
Lines changed: 91 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| 42 | + | |
| 43 | + | |
41 | 44 | | |
42 | 45 | | |
43 | 46 | | |
44 | 47 | | |
45 | 48 | | |
46 | 49 | | |
| 50 | + | |
47 | 51 | | |
48 | 52 | | |
49 | 53 | | |
50 | 54 | | |
51 | 55 | | |
| 56 | + | |
52 | 57 | | |
53 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
54 | 118 | | |
55 | 119 | | |
56 | 120 | | |
| |||
111 | 175 | | |
112 | 176 | | |
113 | 177 | | |
114 | | - | |
115 | | - | |
| 178 | + | |
| 179 | + | |
116 | 180 | | |
117 | 181 | | |
118 | 182 | | |
119 | 183 | | |
120 | 184 | | |
121 | 185 | | |
122 | | - | |
| 186 | + | |
123 | 187 | | |
124 | | - | |
| 188 | + | |
125 | 189 | | |
126 | 190 | | |
127 | 191 | | |
| |||
139 | 203 | | |
140 | 204 | | |
141 | 205 | | |
142 | | - | |
| 206 | + | |
143 | 207 | | |
144 | | - | |
| 208 | + | |
145 | 209 | | |
146 | 210 | | |
147 | | - | |
| 211 | + | |
148 | 212 | | |
149 | 213 | | |
150 | 214 | | |
| |||
154 | 218 | | |
155 | 219 | | |
156 | 220 | | |
157 | | - | |
158 | | - | |
| 221 | + | |
| 222 | + | |
159 | 223 | | |
160 | | - | |
| 224 | + | |
161 | 225 | | |
162 | 226 | | |
163 | 227 | | |
164 | 228 | | |
165 | 229 | | |
166 | 230 | | |
167 | 231 | | |
168 | | - | |
| 232 | + | |
169 | 233 | | |
170 | 234 | | |
171 | 235 | | |
| |||
175 | 239 | | |
176 | 240 | | |
177 | 241 | | |
178 | | - | |
| 242 | + | |
179 | 243 | | |
180 | 244 | | |
181 | 245 | | |
182 | 246 | | |
183 | 247 | | |
184 | | - | |
185 | | - | |
| 248 | + | |
| 249 | + | |
186 | 250 | | |
187 | | - | |
| 251 | + | |
188 | 252 | | |
189 | 253 | | |
190 | 254 | | |
191 | 255 | | |
192 | 256 | | |
193 | 257 | | |
194 | 258 | | |
195 | | - | |
| 259 | + | |
196 | 260 | | |
197 | 261 | | |
198 | 262 | | |
| |||
202 | 266 | | |
203 | 267 | | |
204 | 268 | | |
205 | | - | |
| 269 | + | |
206 | 270 | | |
207 | 271 | | |
208 | 272 | | |
209 | 273 | | |
210 | 274 | | |
211 | 275 | | |
212 | | - | |
| 276 | + | |
213 | 277 | | |
214 | 278 | | |
215 | 279 | | |
| |||
224 | 288 | | |
225 | 289 | | |
226 | 290 | | |
227 | | - | |
| 291 | + | |
228 | 292 | | |
229 | 293 | | |
230 | 294 | | |
231 | | - | |
| 295 | + | |
232 | 296 | | |
233 | 297 | | |
234 | 298 | | |
| |||
241 | 305 | | |
242 | 306 | | |
243 | 307 | | |
244 | | - | |
| 308 | + | |
245 | 309 | | |
246 | 310 | | |
247 | 311 | | |
248 | 312 | | |
249 | | - | |
| 313 | + | |
250 | 314 | | |
251 | 315 | | |
252 | 316 | | |
253 | 317 | | |
254 | 318 | | |
255 | | - | |
| 319 | + | |
256 | 320 | | |
257 | 321 | | |
258 | 322 | | |
259 | 323 | | |
260 | | - | |
| 324 | + | |
261 | 325 | | |
262 | 326 | | |
263 | 327 | | |
264 | | - | |
265 | | - | |
| 328 | + | |
| 329 | + | |
266 | 330 | | |
267 | 331 | | |
268 | | - | |
| 332 | + | |
269 | 333 | | |
270 | 334 | | |
271 | 335 | | |
0 commit comments