11/*
2- * Copyright (c) 2016, 2024 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2016, 2025 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2525 * @test
2626 * @bug 8149411 8007632
2727 * @summary Get AES key from keystore (uses SecretKeySpec not SecretKeyFactory)
28+ * @library /test/lib
2829 * @run main P12SecretKey pkcs12 AES 128
2930 * @run main P12SecretKey pkcs12 DES 56
3031 * @run main P12SecretKey pkcs12 DESede 168
3334import java .io .File ;
3435import java .io .FileInputStream ;
3536import java .io .FileOutputStream ;
36- import java .nio .file .Files ;
3737import java .security .KeyStore ;
38- import java .security .cert .CertificateException ;
3938import java .util .Arrays ;
4039
4140import javax .crypto .KeyGenerator ;
4241import javax .crypto .SecretKey ;
4342
43+ import static jdk .test .lib .Utils .createTempFile ;
44+
4445public class P12SecretKey {
4546
4647 private static final String ALIAS = "alias" ;
@@ -66,30 +67,32 @@ private void run(String keystoreType, String algName, int keySize) throws Except
6667 KeyStore .ProtectionParameter kspp = new KeyStore .PasswordProtection (pw );
6768 ks .setEntry (ALIAS , ske , kspp );
6869
69- File ksFile = File .createTempFile ("test" , ".test" );
70+ // temporary files are created in scratch directory
71+ final File ksFile = createTempFile (
72+ String .format ("%s-%s-%d-" ,
73+ keystoreType ,
74+ algName ,
75+ keySize ),
76+ ".ks" ).toFile ();
7077
71- try {
72- try (FileOutputStream fos = new FileOutputStream (ksFile )) {
73- ks .store (fos , pw );
74- fos .flush ();
75- }
78+ try (FileOutputStream fos = new FileOutputStream (ksFile )) {
79+ ks .store (fos , pw );
80+ fos .flush ();
81+ }
7682
77- // now see if we can get it back
78- try (FileInputStream fis = new FileInputStream (ksFile )) {
79- KeyStore ks2 = KeyStore .getInstance (keystoreType );
80- ks2 .load (fis , pw );
81- KeyStore .Entry entry = ks2 .getEntry (ALIAS , kspp );
82- SecretKey keyIn = ((KeyStore .SecretKeyEntry ) entry ).getSecretKey ();
83- if (Arrays .equals (key .getEncoded (), keyIn .getEncoded ())) {
84- System .err .println ("OK: worked just fine with " + keystoreType +
85- " keystore" );
86- } else {
87- System .err .println ("ERROR: keys are NOT equal after storing in "
88- + keystoreType + " keystore" );
89- }
83+ // now see if we can get it back
84+ try (FileInputStream fis = new FileInputStream (ksFile )) {
85+ KeyStore ks2 = KeyStore .getInstance (keystoreType );
86+ ks2 .load (fis , pw );
87+ KeyStore .Entry entry = ks2 .getEntry (ALIAS , kspp );
88+ SecretKey keyIn = ((KeyStore .SecretKeyEntry ) entry ).getSecretKey ();
89+ if (Arrays .equals (key .getEncoded (), keyIn .getEncoded ())) {
90+ System .err .println ("OK: worked just fine with " + keystoreType +
91+ " keystore" );
92+ } else {
93+ throw new RuntimeException ("ERROR: keys are NOT equal after storing in "
94+ + keystoreType + " keystore" );
9095 }
91- } finally {
92- Files .deleteIfExists (ksFile .toPath ());
9396 }
9497 }
9598}
0 commit comments