File: GenKeyPairSigner.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 (309 lines) | stat: -rw-r--r-- 13,579 bytes parent folder | download | duplicates (2)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Copyright (c) 2021, 2022, 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 8260693 8267319
 * @summary Test for keytool -genkeypair with -signer and -signerkeypass options
 * @library /test/lib
 * @modules java.base/sun.security.util
 */

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

import java.io.*;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.security.KeyStore;
import java.security.PublicKey;
import java.util.Arrays;
import sun.security.util.DerValue;
import sun.security.util.KeyUtil;
import sun.security.util.KnownOIDs;
import static sun.security.util.KnownOIDs.*;

public class GenKeyPairSigner {

    static OutputAnalyzer kt(String cmd, String ks) throws Exception {
        return SecurityTools.keytool("-storepass changeit " + cmd +
                " -keystore " + ks);
    }

    static OutputAnalyzer ktjks(String cmd, String ks, String kpass) throws Exception {
        return SecurityTools.keytool("-storepass changeit " + cmd +
                " -keystore " + ks + " -storetype jks" + " -keypass " +
                kpass);
    }

    public static void main(String[] args) throws Exception {
        testSignerPKCS12();
        testSignerJKS();
        testSignerOpt();
    }

    static void testSignerPKCS12() throws Exception {
        KeyStore kstore;
        X509Certificate cert;
        String sigName, pKeyAlg;
        PublicKey pKey;
        int keyLen;

        /*
         * The signer alias is stored in the PKCS12 keystore
         */
        System.out.println("Testing the signer alias that is stored in the PKCS12 keystore");
        System.out.println("Generating a root cert with SubjectKeyIdentifier extension");
        SecurityTools.keytool("-keystore ks -storepass changeit " +
                "-genkeypair -keyalg EdDSA -alias ca -dname CN=CA -ext bc:c " +
                "-ext 2.5.29.14=04:14:00:01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16:17:18:19")
                .shouldContain("Generating 255 bit Ed25519 key pair and self-signed certificate (Ed25519) with a validity of 90 days")
                .shouldContain("for: CN=CA")
                .shouldHaveExitValue(0);

        System.out.println("Generating an XDH cert with -signer option");
        SecurityTools.keytool("-keystore ks -storepass changeit " +
                "-genkeypair -keyalg XDH -alias e1 -dname CN=E1 -signer ca")
                .shouldContain("Generating 255 bit XDH key pair and a certificate (Ed25519) issued by <ca> with a validity of 90 days")
                .shouldContain("for: CN=E1")
                .shouldHaveExitValue(0);

        // examine the resulting cert
        kstore = KeyStore.getInstance(new File("ks"), "changeit".toCharArray());
        cert = (X509Certificate)kstore.getCertificate("e1");

        Certificate[] certChain = kstore.getCertificateChain("e1");
        if (certChain.length != 2) {
            throw new Exception("Generated cert chain is in error");
        }

        sigName = cert.getSigAlgName();
        if (sigName != "Ed25519") {
            throw new Exception("Signature algorithm name is in error");
        }

        pKey = cert.getPublicKey();
        keyLen = KeyUtil.getKeySize(pKey);
        if (keyLen != 255) {
            throw new Exception("Key size is in error");
        }

        pKeyAlg = pKey.getAlgorithm();
        if (pKeyAlg != "XDH") {
            throw new Exception("Subject Public Key Algorithm is in error");
        }

        SecurityTools.keytool("-keystore ks -storepass changeit " +
                "-list -v")
                .shouldContain("Alias name: e1")
                .shouldContain("Certificate chain length: 2")
                .shouldContain("Signature algorithm name: Ed25519")
                .shouldContain("Subject Public Key Algorithm: 255-bit XDH key")
                .shouldHaveExitValue(0);

        // check to make sure that cert's AKID is created from the SKID of the signing cert
        byte[] expectedId = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19};

        byte[] authorityKeyIdExt = cert.getExtensionValue(
                KnownOIDs.AuthorityKeyID.value());

        byte[] authorityKeyId = null;
        if (authorityKeyIdExt == null) {
            throw new Exception("Failed to get AKID extension from the cert");
        } else {
            try {
                authorityKeyId = new DerValue(authorityKeyIdExt).getOctetString();
            } catch (IOException e) {
                throw new Exception("Failed to get AKID encoded OctetString in the cert");
            }
        }

        authorityKeyId = Arrays.copyOfRange(authorityKeyId, 4, authorityKeyId.length);
        if (!Arrays.equals(authorityKeyId, expectedId)) {
            throw new Exception("Failed due to AKID mismatch");
        }

        kt("-genkeypair -keyalg RSA -alias ca2 -dname CN=CA2 -ext bc:c ",
                "ks");

        System.out.println("Generating an X448 cert with -signer option");
        SecurityTools.keytool("-keystore ks -storepass changeit " +
                "-genkeypair -keyalg X448 -alias e2 -dname CN=E2 -sigalg SHA384withRSA -signer ca2")
                .shouldContain("Generating 448 bit XDH key pair and a certificate (SHA384withRSA) issued by <ca2> with a validity of 90 days")
                .shouldContain("for: CN=E2")
                .shouldHaveExitValue(0);

        // examine the resulting cert
        kstore = KeyStore.getInstance(new File("ks"), "changeit".toCharArray());
        cert = (X509Certificate)kstore.getCertificate("e2");
        sigName = cert.getSigAlgName();
        if (sigName != "SHA384withRSA") {
            throw new Exception("Signature algorithm name is in error");
        }

        pKey = cert.getPublicKey();
        keyLen = KeyUtil.getKeySize(pKey);
        if (keyLen != 448) {
            throw new Exception("Key size is in error");
        }

        pKeyAlg = pKey.getAlgorithm();
        if (pKeyAlg != "XDH") {
            throw new Exception("Subject Public Key Algorithm is in error");
        }

        SecurityTools.keytool("-keystore ks -storepass changeit " +
                "-list -v")
                .shouldContain("Alias name: e2")
                .shouldContain("Signature algorithm name: SHA384withRSA")
                .shouldContain("Subject Public Key Algorithm: 448-bit XDH key")
                .shouldHaveExitValue(0);

        kt("-genkeypair -keyalg DSA -alias ca3 -dname CN=CA3 -ext bc:c ",
                "ks");

        System.out.println("Generating a DH cert with -signer option");
        SecurityTools.keytool("-keystore ks -storepass changeit " +
                "-genkeypair -keyalg DH -alias e3 -dname CN=E3 -signer ca3")
                .shouldContain("Generating 3,072 bit DH key pair and a certificate (SHA256withDSA) issued by <ca3> with a validity of 90 days")
                .shouldContain("for: CN=E3")
                .shouldHaveExitValue(0);

        // examine the resulting cert
        kstore = KeyStore.getInstance(new File("ks"), "changeit".toCharArray());
        cert = (X509Certificate)kstore.getCertificate("e3");
        sigName = cert.getSigAlgName();
        if (sigName != "SHA256withDSA") {
            throw new Exception("Signature algorithm name is in error");
        }

        pKey = cert.getPublicKey();
        keyLen = KeyUtil.getKeySize(pKey);
        if (keyLen != 3072) {
            throw new Exception("Key size is in error");
        }

        pKeyAlg = pKey.getAlgorithm();
        if (pKeyAlg != "DH") {
            throw new Exception("Subject Public Key Algorithm is in error");
        }

        SecurityTools.keytool("-keystore ks -storepass changeit " +
                "-list -v")
                .shouldContain("Alias name: e3")
                .shouldContain("Signature algorithm name: SHA384withRSA")
                .shouldContain("Subject Public Key Algorithm: 3072-bit DH key")
                .shouldHaveExitValue(0);
    }

    static void testSignerJKS() throws Exception {
        KeyStore kstore;
        X509Certificate cert;
        String sigName, pKeyAlg;
        PublicKey pKey;
        int keyLen;

        /*
         * The signer alias is stored in the JKS keystore
         * Using JKS keystore here is to test the scenario when the private key
         * of the signer entry is protected by a password different from the
         * store password, and -signerkeypass option needs to be specified
         * along with -signer option.
         */
        System.out.println("Testing the signer alias that is stored in the JKS keystore");
        ktjks("-genkeypair -keyalg RSA -keysize 1024 -alias ca -dname CN=CA -ext bc:c",
                "ksjks", "cakeypass");

        System.out.println("Generating an DSA cert with -signer and -signerkeypass options");
        SecurityTools.keytool("-keystore ksjks -storepass changeit -storetype jks " +
                "-genkeypair -keyalg DSA -keysize 1024 -alias ca1 -dname CN=CA1 " +
                "-keypass ca1keypass -signer ca -signerkeypass cakeypass")
                .shouldContain("Generating 1,024 bit DSA key pair and a certificate (SHA384withRSA) issued by <ca> with a validity of 90 days")
                .shouldContain("for: CN=CA1")
                .shouldContain("The generated certificate #1 of 2 uses a 1024-bit DSA key which is considered a security risk")
                .shouldContain("The generated certificate #2 of 2 uses a 1024-bit RSA key which is considered a security risk")
                .shouldHaveExitValue(0);

        System.out.println("Generating an XDH cert with -signer and -signerkeypass options");
        SecurityTools.keytool("-keystore ksjks -storepass changeit -storetype jks " +
                "-genkeypair -keyalg XDH -alias e1 -dname CN=E1 " +
                "-keypass e1keypass -signer ca1 -signerkeypass ca1keypass")
                .shouldContain("Generating 255 bit XDH key pair and a certificate (SHA256withDSA) issued by <ca1> with a validity of 90 days")
                .shouldContain("for: CN=E1")
                .shouldContain("The generated certificate #2 of 3 uses a 1024-bit DSA key which is considered a security risk")
                .shouldContain("The generated certificate #3 of 3 uses a 1024-bit RSA key which is considered a security risk")
                .shouldHaveExitValue(0);

        // examine the resulting cert
        kstore = KeyStore.getInstance(new File("ksjks"), "changeit".toCharArray());
        cert = (X509Certificate)kstore.getCertificate("e1");

        Certificate[] certChain = kstore.getCertificateChain("e1");
        if (certChain.length != 3) {
            throw new Exception("Generated cert chain is in error");
        }

        sigName = cert.getSigAlgName();
        if (sigName != "SHA256withDSA") {
            throw new Exception("Signature algorithm name is in error");
        }

        pKey = cert.getPublicKey();
        keyLen = KeyUtil.getKeySize(pKey);
        if (keyLen != 255) {
            throw new Exception("Key size is in error");
        }

        pKeyAlg = pKey.getAlgorithm();
        if (pKeyAlg != "XDH") {
            throw new Exception("Subject Public Key Algorithm is in error");
        }

        SecurityTools.keytool("-keystore ksjks -storepass changeit " +
                "-list -v")
                .shouldContain("Alias name: e1")
                .shouldContain("Certificate chain length: 3")
                .shouldContain("Signature algorithm name: SHA256withDSA")
                .shouldContain("Subject Public Key Algorithm: 255-bit XDH key")
                .shouldHaveExitValue(0);
    }

    static void testSignerOpt() throws Exception {

        SecurityTools.keytool("-keystore ks -storepass changeit " +
                "-genkeypair -keyalg X25519 -alias e4 -dname CN=E4")
                .shouldContain("Cannot derive signature algorithm from XDH")
                .shouldHaveExitValue(1);

        SecurityTools.keytool("-keystore ks -storepass changeit " +
                "-genkeypair -keyalg X448 -alias e4 -dname CN=E4 -signer noca")
                .shouldContain("Alias <noca> does not exist")
                .shouldHaveExitValue(1);

        SecurityTools.keytool("-genkeypair --help")
                .shouldContain("-signer <alias>         signer alias")
                .shouldContain("-signerkeypass <arg>    signer key password")
                .shouldHaveExitValue(0);
    }
}