jdk/test/sun/security/pkcs12/EmptyAlias.java
author vinnie
Thu, 24 Aug 2017 16:49:56 +0100
changeset 47002 e82e52e3e566
permissions -rw-r--r--
8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry() Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47002
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
     1
/*
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
     4
 *
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
     8
 *
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    13
 * accompanied this code).
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    14
 *
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    18
 *
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    21
 * questions.
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    22
 */
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    23
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    24
/*
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    25
 * @test
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    26
 * @bug 8173181
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    27
 * @summary KeyStore regression due to default keystore being changed to PKCS12
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    28
 */
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    29
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    30
import java.io.*;
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    31
import java.security.KeyStore;
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    32
import java.security.cert.Certificate;
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    33
import java.security.cert.CertificateFactory;
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    34
import java.security.cert.X509Certificate;
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    35
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    36
/**
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    37
 * Test that a PKCS12 keystore entry can be created with an empty alias name.
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    38
 */
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    39
public class EmptyAlias {
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    40
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    41
    private static final String DIR = System.getProperty("test.src", ".");
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    42
    private static final String CERT = DIR + "/trusted.pem";
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    43
    private static final String EMPTY_ALIAS = "";
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    44
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    46
        KeyStore keystore = KeyStore.getInstance("PKCS12");
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    47
        keystore.load(null, null);
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    48
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    49
        keystore.setCertificateEntry(EMPTY_ALIAS, loadCertificate(CERT));
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    50
        KeyStore.Entry entry = keystore.getEntry(EMPTY_ALIAS, null);
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    51
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    52
        if (entry == null) {
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    53
            throw new Exception(
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    54
                "Error retrieving keystore entry using its (empty) alias");
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    55
        }
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    56
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    57
        System.out.println("OK");
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    58
    }
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    59
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    60
    private static Certificate loadCertificate(String certFile)
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    61
            throws Exception {
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    62
        X509Certificate cert = null;
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    63
        try (FileInputStream certStream = new FileInputStream(certFile)) {
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    64
            CertificateFactory factory =
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    65
                CertificateFactory.getInstance("X.509");
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    66
            return factory.generateCertificate(certStream);
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    67
        }
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    68
    }
e82e52e3e566 8173181: Empty string alias in KeyStore throws StringIndexOutOfBoundsException for getEntry()
vinnie
parents:
diff changeset
    69
}