1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
25
25
* @test
26
26
* @bug 8149411 8007632
27
27
* @summary Get AES key from keystore (uses SecretKeySpec not SecretKeyFactory)
28
+ * @library /test/lib
28
29
* @run main P12SecretKey pkcs12 AES 128
29
30
* @run main P12SecretKey pkcs12 DES 56
30
31
* @run main P12SecretKey pkcs12 DESede 168
33
34
import java .io .File ;
34
35
import java .io .FileInputStream ;
35
36
import java .io .FileOutputStream ;
36
- import java .nio .file .Files ;
37
37
import java .security .KeyStore ;
38
- import java .security .cert .CertificateException ;
39
38
import java .util .Arrays ;
40
39
41
40
import javax .crypto .KeyGenerator ;
42
41
import javax .crypto .SecretKey ;
43
42
43
+ import static jdk .test .lib .Utils .createTempFile ;
44
+
44
45
public class P12SecretKey {
45
46
46
47
private static final String ALIAS = "alias" ;
@@ -66,30 +67,32 @@ private void run(String keystoreType, String algName, int keySize) throws Except
66
67
KeyStore .ProtectionParameter kspp = new KeyStore .PasswordProtection (pw );
67
68
ks .setEntry (ALIAS , ske , kspp );
68
69
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 ();
70
77
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
+ }
76
82
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" );
90
95
}
91
- } finally {
92
- Files .deleteIfExists (ksFile .toPath ());
93
96
}
94
97
}
95
98
}
0 commit comments