File: ListKeyChainStore.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (202 lines) | stat: -rw-r--r-- 9,688 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 7133495 8062264 8046777 8153005
 * @summary KeyChain KeyStore implementation retrieves only one private key entry
 * @requires (os.family == "mac")
 * @library /test/lib
 * @run main/othervm/manual ListKeyChainStore
 */

import jdk.test.lib.SecurityTools;
import jdk.test.lib.process.ProcessTools;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.Key;
import java.security.KeyStore;
import java.security.PrivateKey;

public class ListKeyChainStore {
    private static final String PWD = "xxxxxx";
    private static final String DEFAULT_KEYTOOL = "-list -storetype KeychainStore " +
            "-keystore NONE -storepass " + PWD;
    private static final String USER_DIR = System.getProperty("user.dir", ".");
    private static final String FS = System.getProperty("file.separator");
    private static final String PKCS12_KEYSTORE = USER_DIR + FS + "7133495.p12";
    private static final String KEYCHAIN_FILE = USER_DIR + FS + "7133495.keychain";
    private static final String TEMPORARY_FILE = USER_DIR + FS + "7133495.tmp";
    private static final String USER_KEYCHAIN_LIST = USER_DIR + FS + "user.keychain.list";
    private static final String PRIVATE_KEY_ENTRY = "PrivateKeyEntry";

    public static void main(String[] args) throws Throwable {
        LOG_MSG("WARNING: This test doesn't work on macOS virtualized environment. " +
                "`security list-keychains -s` doesn't update the search order.");

        deleteTestTempFilesIfExists();

        // Get the old security keychain list to restore later
        try (PrintStream printStream = new PrintStream(USER_KEYCHAIN_LIST)) {
            ProcessTools.executeCommand("sh", "-c", "security list-keychains")
                    .shouldHaveExitValue(0).outputTo(printStream);
        }

        try {
            try (PrintStream printStream = new PrintStream(TEMPORARY_FILE)) {
                SecurityTools.keytool(DEFAULT_KEYTOOL).shouldHaveExitValue(0)
                        .outputTo(printStream);
            }
            int oldPrivateKeyCount = countOccurrences(TEMPORARY_FILE, PRIVATE_KEY_ENTRY);
            LOG_MSG("Found " + oldPrivateKeyCount + " private key entries in the " +
                    "Keychain keystore");

            // Create the PKCS12 keystore containing 3 public/private key pairs
            LOG_MSG("Creating PKCS12 keystore: " + PKCS12_KEYSTORE);
            for (int i = 0; i < 3; i++) {
                // Use legacy encryption and MAC algorithms, refer macOS open radar FB8988319
                // macOS security framework doesn't work with the latest algorithms
                SecurityTools.keytool(String.format("-J-Dkeystore.pkcs12.legacy -genkeypair" +
                                " -storetype PKCS12 -keystore %s -storepass %s -keyalg rsa  -dname " +
                                "CN=CN%d,OU=OU%d,O=O%d,ST=ST%d,C=US -alias 7133495-%d",
                        PKCS12_KEYSTORE, PWD, i, i, i, i, i)).shouldHaveExitValue(0);
            }

            // Create the keychain
            LOG_MSG("Creating keychain: " + KEYCHAIN_FILE);
            ProcessTools.executeCommand("sh", "-c", String.format("security create-keychain" +
                    " -p %s %s", PWD, KEYCHAIN_FILE)).shouldHaveExitValue(0);

            // Unlock the keychain
            LOG_MSG("Unlock keychain: " + KEYCHAIN_FILE);
            ProcessTools.executeCommand("sh", "-c", String.format("security unlock-keychain" +
                    " -p %s %s", PWD, KEYCHAIN_FILE)).shouldHaveExitValue(0);

            // Import the key pairs from the PKCS12 keystore into the keychain
            // The '-A' option is used to lower the keychain's access controls
            LOG_MSG("Importing the key pairs from " + PKCS12_KEYSTORE
                    + " to " + KEYCHAIN_FILE);
            ProcessTools.executeCommand("sh", "-c", String.format("security import %s -k %s" +
                    " -f pkcs12 -P %s -A", PKCS12_KEYSTORE, KEYCHAIN_FILE, PWD)).shouldHaveExitValue(0);

            // Generate a 2048-bit RSA keypair and import into the keychain
            // Its private key is configured with non-default key usage settings
            ProcessTools.executeCommand("sh", "-c", String.format("certtool ca k=%s " +
                    "<<EOF\n" +
                    "test\n" +
                    "r\n" +
                    "2048\n" +
                    "y\n" +
                    "b\n" +
                    "s\n" +
                    "y\n" +
                    "A\n" +
                    "US\n" +
                    "A\n" +
                    "A\n" +
                    "\n" +
                    "\n" +
                    "y\n" +
                    "EOF", KEYCHAIN_FILE)).shouldHaveExitValue(0);

            // Adjust the keychain search order to add KEYCHAIN_FILE to top
            try (PrintStream printStream = new PrintStream(TEMPORARY_FILE)) {
                printStream.println("\"" + KEYCHAIN_FILE + "\"");
                printStream.println(ProcessTools.executeCommand("sh", "-c", "security list-keychains")
                        .shouldHaveExitValue(0).getOutput());
            }
            ProcessTools.executeCommand("sh", "-c", String.format("security list-keychains -s %s",
                    ProcessTools.executeCommand("sh", "-c", String.format("xargs < %s",
                            TEMPORARY_FILE)).getOutput()));
            LOG_MSG("Keychain search order:");
            ProcessTools.executeCommand("sh", "-c", "security list-keychains");

            // Recount the number of private key entries in the Keychain keystore
            // 3 private keys imported from PKCS12, 1 private key generated by 'certtool'
            Files.deleteIfExists(Paths.get(TEMPORARY_FILE));
            try (PrintStream printStream = new PrintStream(TEMPORARY_FILE)) {
                SecurityTools.keytool(DEFAULT_KEYTOOL).shouldHaveExitValue(0)
                        .outputTo(printStream);
            }
            int newPrivateKeyCount = countOccurrences(TEMPORARY_FILE, PRIVATE_KEY_ENTRY);
            LOG_MSG("Found " + newPrivateKeyCount + " private key entries in " +
                    "the Keychain keystore");
            if (newPrivateKeyCount < (oldPrivateKeyCount + 4)) {
                throw new RuntimeException("Error: expected more private key entries in the " +
                        "Keychain keystore");
            }

            // Export a private key from the keychain (without supplying a password)
            // Access controls have already been lowered (see 'security import ... -A' above)
            LOG_MSG("Exporting a private key from the keychain");
            KeyStore ks = KeyStore.getInstance("KeychainStore");
            ks.load(null, null);
            Key key = ks.getKey("CN0", null);
            if (key instanceof PrivateKey) {
                LOG_MSG("Exported " + key.getAlgorithm() + " private key from CN0");
            } else {
                throw new RuntimeException("Error exporting private key from keychain");
            }
        } finally {
            // Reset earlier keychain list
            ProcessTools.executeCommand("sh", "-c", String.format("security list-keychains -s %s",
                    ProcessTools.executeCommand("sh", "-c", String.format("xargs < %s",
                            USER_KEYCHAIN_LIST)).getOutput()));

            deleteTestTempFilesIfExists();
        }
    }

    private static void deleteTestTempFilesIfExists() throws Throwable {
        Files.deleteIfExists(Paths.get(USER_KEYCHAIN_LIST));
        Files.deleteIfExists(Paths.get(PKCS12_KEYSTORE));
        if (Files.exists(Paths.get(KEYCHAIN_FILE))) {
            ProcessTools.executeCommand("sh", "-c", String.format("security delete-keychain" +
                    " %s", KEYCHAIN_FILE)).shouldHaveExitValue(0);
        }
        Files.deleteIfExists(Paths.get(TEMPORARY_FILE));
    }

    private static int countOccurrences(String filePath, String word) throws IOException {
        int count = 0;
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.contains(word)) {
                    count++;
                }
            }
        }

        return count;
    }

    private static void LOG_MSG(String msg) {
        // ProcessTools and SecurityTools log a lot of messages so pretty format
        // messages logged from this test
        System.out.println();
        System.out.println("==> " + msg);
    }
}