test/jdk/sun/security/pkcs12/StoreTrustedCertTest.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 15532 jdk/test/sun/security/pkcs12/StoreTrustedCertTest.java@859facd70580
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     1
/*
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     4
 *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     8
 *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    13
 * accompanied this code).
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    14
 *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    18
 *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    21
 * questions.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    22
 */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    23
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    24
/*
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    25
 * @test
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    26
 * @bug 8005408
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    27
 * @summary KeyStore API enhancements
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    28
 */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    29
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    30
import java.io.*;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    31
import java.security.*;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    32
import java.security.cert.*;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    33
import java.util.*;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    34
import java.security.cert.Certificate;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    35
import javax.crypto.*;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    36
import javax.crypto.spec.*;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    37
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    38
// Store a trusted certificate in a keystore and retrieve it again.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    39
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    40
public class StoreTrustedCertTest {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    41
    private final static String DIR = System.getProperty("test.src", ".");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    42
    private static final char[] PASSWORD = "passphrase".toCharArray();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    43
    private static final String KEYSTORE = "truststore.p12";
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    44
    private static final String CERT = DIR + "/trusted.pem";
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    45
    private static final String ALIAS = "my trustedcert";
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    46
    private static final String ALIAS2 = "my trustedcert with attributes";
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    47
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    48
    public static void main(String[] args) throws Exception {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    49
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    50
        new File(KEYSTORE).delete();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    51
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    52
        KeyStore keystore = KeyStore.getInstance("PKCS12");
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    53
        keystore.load(null, null);
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    54
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    55
        Certificate cert = loadCertificate(CERT);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    56
        Set<KeyStore.Entry.Attribute> attributes = new HashSet<>();
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    57
        attributes.add(new PKCS12Attribute("1.3.5.7.9", "that's odd"));
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    58
        attributes.add(new PKCS12Attribute("2.4.6.8.10", "that's even"));
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    59
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    60
        // Set trusted certificate entry
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    61
        keystore.setEntry(ALIAS,
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    62
            new KeyStore.TrustedCertificateEntry(cert), null);
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    63
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    64
        // Set trusted certificate entry with attributes
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    65
        keystore.setEntry(ALIAS2,
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    66
            new KeyStore.TrustedCertificateEntry(cert, attributes), null);
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    67
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    68
        try (FileOutputStream outStream = new FileOutputStream(KEYSTORE)) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    69
            System.out.println("Storing keystore to: " + KEYSTORE);
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    70
            keystore.store(outStream, PASSWORD);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    71
        }
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    72
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    73
        try (FileInputStream inStream = new FileInputStream(KEYSTORE)) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    74
            System.out.println("Loading keystore from: " + KEYSTORE);
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    75
            keystore.load(inStream, PASSWORD);
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    76
            System.out.println("Loaded keystore with " + keystore.size() +
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    77
                " entries");
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    78
        }
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    79
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    80
        KeyStore.Entry entry = keystore.getEntry(ALIAS, null);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    81
        if (entry instanceof KeyStore.TrustedCertificateEntry) {
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    82
            System.out.println("Retrieved trusted certificate entry: " + entry);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    83
        } else {
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    84
            throw new Exception("Not a trusted certificate entry");
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    85
        }
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    86
        System.out.println();
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    87
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    88
        entry = keystore.getEntry(ALIAS2, null);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    89
        if (entry instanceof KeyStore.TrustedCertificateEntry) {
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    90
            KeyStore.TrustedCertificateEntry trustedEntry =
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    91
                (KeyStore.TrustedCertificateEntry) entry;
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    92
            Set<KeyStore.Entry.Attribute> entryAttributes =
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    93
                trustedEntry.getAttributes();
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    94
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    95
            if (entryAttributes.containsAll(attributes)) {
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    96
                System.out.println("Retrieved trusted certificate entry " +
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    97
                    "with attributes: " + entry);
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    98
            } else {
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    99
                throw new Exception("Failed to retrieve entry attributes");
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
   100
            }
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
   101
        } else {
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
   102
            throw new Exception("Not a trusted certificate entry");
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
   103
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
   104
    }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
   105
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
   106
    private static Certificate loadCertificate(String certFile)
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
   107
        throws Exception {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
   108
        X509Certificate cert = null;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
   109
        try (FileInputStream certStream = new FileInputStream(certFile)) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
   110
            CertificateFactory factory =
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
   111
                CertificateFactory.getInstance("X.509");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
   112
            return factory.generateCertificate(certStream);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
   113
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
   114
    }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
   115
}