make/jdk/src/classes/build/tools/generatecacerts/GenerateCacerts.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 55412 55a79ffab804
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55215
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
     1
/*
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
     4
 *
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    10
 *
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    15
 * accompanied this code).
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    16
 *
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    20
 *
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    23
 * questions.
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    24
 */
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    25
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    26
package build.tools.generatecacerts;
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    27
55412
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    28
import java.io.DataOutputStream;
55215
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    29
import java.io.FileOutputStream;
55412
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    30
import java.io.IOException;
55215
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    31
import java.io.InputStream;
55412
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    32
import java.io.OutputStream;
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    33
import java.io.UnsupportedEncodingException;
55215
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    34
import java.nio.file.Files;
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    35
import java.nio.file.Path;
55412
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    36
import java.security.DigestOutputStream;
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    37
import java.security.MessageDigest;
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    38
import java.security.NoSuchAlgorithmException;
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    39
import java.security.cert.CertificateException;
55215
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    40
import java.security.cert.CertificateFactory;
55412
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    41
import java.security.cert.X509Certificate;
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    42
import java.util.Arrays;
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    43
import java.util.List;
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    44
import java.util.stream.Collectors;
55215
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    45
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    46
/**
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    47
 * Generate cacerts
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    48
 *    args[0]: Full path string to the directory that contains CA certs
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    49
 *    args[1]: Full path string to the generated cacerts
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    50
 */
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    51
public class GenerateCacerts {
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    52
    public static void main(String[] args) throws Exception {
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    53
        try (FileOutputStream fos = new FileOutputStream(args[1])) {
55412
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    54
            store(args[0], fos, "changeit".toCharArray());
55215
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    55
        }
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
    56
    }
55412
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    57
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    58
    // The following code are copied from JavaKeyStore.java.
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    59
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    60
    private static final int MAGIC = 0xfeedfeed;
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    61
    private static final int VERSION_2 = 0x02;
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    62
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    63
    // This method is a simplified version of JavaKeyStore::engineStore.
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    64
    // A new "dir" argument is added. All cert names in "dir" is collected into
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    65
    // a sorted array. Each cert is stored with a creation date set to its
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    66
    // notBefore value. Thus the output is determined as long as the certs
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    67
    // are the same.
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    68
    public static void store(String dir, OutputStream stream, char[] password)
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    69
            throws IOException, NoSuchAlgorithmException, CertificateException
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    70
    {
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    71
        byte[] encoded; // the certificate encoding
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    72
        CertificateFactory cf = CertificateFactory.getInstance("X509");
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    73
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    74
        MessageDigest md = getPreKeyedHash(password);
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    75
        DataOutputStream dos
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    76
                = new DataOutputStream(new DigestOutputStream(stream, md));
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    77
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    78
        dos.writeInt(MAGIC);
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    79
        // always write the latest version
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    80
        dos.writeInt(VERSION_2);
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    81
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    82
        // All file names in dir sorted.
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    83
        // README is excluded. Name starting with "." excluded.
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    84
        List<String> entries = Files.list(Path.of(dir))
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    85
                .map(p -> p.getFileName().toString())
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    86
                .filter(s -> !s.equals("README") && !s.startsWith("."))
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    87
                .collect(Collectors.toList());
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    88
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    89
        entries.sort(String::compareTo);
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    90
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    91
        dos.writeInt(entries.size());
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    92
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    93
        for (String entry : entries) {
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    94
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    95
            String alias = entry + " [jdk]";
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    96
            X509Certificate cert;
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    97
            try (InputStream fis = Files.newInputStream(Path.of(dir, entry))) {
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    98
                cert = (X509Certificate) cf.generateCertificate(fis);
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
    99
            }
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   100
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   101
            dos.writeInt(2);
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   102
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   103
            // Write the alias
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   104
            dos.writeUTF(alias);
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   105
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   106
            // Write the (entry creation) date, which is notBefore of the cert
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   107
            dos.writeLong(cert.getNotBefore().getTime());
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   108
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   109
            // Write the trusted certificate
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   110
            encoded = cert.getEncoded();
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   111
            dos.writeUTF(cert.getType());
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   112
            dos.writeInt(encoded.length);
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   113
            dos.write(encoded);
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   114
        }
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   115
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   116
        /*
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   117
         * Write the keyed hash which is used to detect tampering with
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   118
         * the keystore (such as deleting or modifying key or
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   119
         * certificate entries).
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   120
         */
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   121
        byte[] digest = md.digest();
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   122
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   123
        dos.write(digest);
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   124
        dos.flush();
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   125
    }
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   126
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   127
    private static MessageDigest getPreKeyedHash(char[] password)
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   128
            throws NoSuchAlgorithmException, UnsupportedEncodingException
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   129
    {
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   130
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   131
        MessageDigest md = MessageDigest.getInstance("SHA");
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   132
        byte[] passwdBytes = convertToBytes(password);
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   133
        md.update(passwdBytes);
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   134
        Arrays.fill(passwdBytes, (byte) 0x00);
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   135
        md.update("Mighty Aphrodite".getBytes("UTF8"));
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   136
        return md;
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   137
    }
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   138
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   139
    private static byte[] convertToBytes(char[] password) {
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   140
        int i, j;
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   141
        byte[] passwdBytes = new byte[password.length * 2];
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   142
        for (i=0, j=0; i<password.length; i++) {
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   143
            passwdBytes[j++] = (byte)(password[i] >> 8);
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   144
            passwdBytes[j++] = (byte)password[i];
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   145
        }
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   146
        return passwdBytes;
55a79ffab804 8225392: Comparison builds are failing due to cacerts file
weijun
parents: 55215
diff changeset
   147
    }
55215
29ab1f3bd353 8193255: Root Certificates should be stored in text format and assembled at build time
weijun
parents:
diff changeset
   148
}