@@ -273,50 +273,61 @@ public void deleteSecretKeyFromStorage() throws ClientException {
273273 /**
274274 * Generate a self-signed cert and derive an AlgorithmParameterSpec from that.
275275 * This is for the key to be generated in {@link KeyStore} via {@link KeyPairGenerator}
276+ * Note : This is now only for API level < 28
276277 *
277278 * @param context an Android {@link Context} object.
278279 * @return a {@link AlgorithmParameterSpec} for the keystore key (that we'll use to wrap the secret key).
279280 */
280- // @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
281- // private static AlgorithmParameterSpec getLegacySpecForKeyStoreKey(@NonNull final Context context,
282- // @NonNull final String alias) {
283- // // Generate a self-signed cert.
284- // final String certInfo = String.format(Locale.ROOT, "CN=%s, OU=%s",
285- // alias,
286- // context.getPackageName());
287- //
288- // final Calendar start = Calendar.getInstance();
289- // final Calendar end = Calendar.getInstance();
290- // final int certValidYears = 100;
291- // end.add(Calendar.YEAR, certValidYears);
292- //
293- // return new KeyPairGeneratorSpec.Builder(context)
294- // .setAlias(alias)
295- // .setSubject(new X500Principal(certInfo))
296- // .setSerialNumber(BigInteger.ONE)
297- // .setStartDate(start.getTime())
298- // .setEndDate(end.getTime())
299- // .build();
300- // }
301-
302-
303- private static AlgorithmParameterSpec getSpecForKeyStoreKey (@ NonNull final Context context , @ NonNull final String alias ) {
281+ @ RequiresApi (api = Build .VERSION_CODES .JELLY_BEAN_MR2 )
282+ private static AlgorithmParameterSpec getLegacySpecForKeyStoreKey (@ NonNull final Context context ,
283+ @ NonNull final String alias ) {
304284 // Generate a self-signed cert.
305285 final String certInfo = String .format (Locale .ROOT , "CN=%s, OU=%s" ,
306286 alias ,
307287 context .getPackageName ());
288+
289+ final Calendar start = Calendar .getInstance ();
290+ final Calendar end = Calendar .getInstance ();
308291 final int certValidYears = 100 ;
309- return new KeyGenParameterSpec . Builder ( alias , KeyProperties . PURPOSE_WRAP_KEY | KeyProperties . PURPOSE_ENCRYPT | KeyProperties . PURPOSE_DECRYPT )
310- . setCertificateSubject ( new X500Principal ( certInfo ))
311- . setCertificateSerialNumber ( BigInteger . ONE )
312- .setCertificateNotBefore ( new Date () )
313- .setCertificateNotAfter (new Date ( System . currentTimeMillis () + TimeUnit . DAYS . toMillis ( 365 * certValidYears ) ))
314- .setKeySize ( 2048 )
315- .setDigests ( KeyProperties . DIGEST_SHA256 , KeyProperties . DIGEST_SHA512 )
316- .setEncryptionPaddings ( KeyProperties . ENCRYPTION_PADDING_RSA_PKCS1 )
292+ end . add ( Calendar . YEAR , certValidYears );
293+
294+ return new KeyPairGeneratorSpec . Builder ( context )
295+ .setAlias ( alias )
296+ .setSubject (new X500Principal ( certInfo ))
297+ .setSerialNumber ( BigInteger . ONE )
298+ .setStartDate ( start . getTime () )
299+ .setEndDate ( end . getTime () )
317300 .build ();
318301 }
319302
303+ /**
304+ * Generate a self-signed cert and derive an AlgorithmParameterSpec from that.
305+ * This is for the key to be generated in {@link KeyStore} via {@link KeyPairGenerator}
306+ *
307+ * @param context an Android {@link Context} object.
308+ * @return a {@link AlgorithmParameterSpec} for the keystore key (that we'll use to wrap the secret key).
309+ */
310+ private static AlgorithmParameterSpec getSpecForKeyStoreKey (@ NonNull final Context context , @ NonNull final String alias ) {
311+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .P ) {
312+ return getLegacySpecForKeyStoreKey (context , alias );
313+ } else {
314+ final String certInfo = String .format (Locale .ROOT , "CN=%s, OU=%s" ,
315+ alias ,
316+ context .getPackageName ());
317+ final int certValidYears = 100 ;
318+ int purposes = KeyProperties .PURPOSE_WRAP_KEY | KeyProperties .PURPOSE_ENCRYPT | KeyProperties .PURPOSE_DECRYPT ;
319+ return new KeyGenParameterSpec .Builder (alias , purposes )
320+ .setCertificateSubject (new X500Principal (certInfo ))
321+ .setCertificateSerialNumber (BigInteger .ONE )
322+ .setCertificateNotBefore (new Date ())
323+ .setCertificateNotAfter (new Date (System .currentTimeMillis () + TimeUnit .DAYS .toMillis (365 * certValidYears )))
324+ .setKeySize (2048 )
325+ .setDigests (KeyProperties .DIGEST_SHA256 , KeyProperties .DIGEST_SHA512 )
326+ .setEncryptionPaddings (KeyProperties .ENCRYPTION_PADDING_RSA_PKCS1 )
327+ .build ();
328+ }
329+ }
330+
320331 /**
321332 * Get the file that stores the wrapped key.
322333 */
0 commit comments