test/jdk/sun/security/krb5/ktab/BufferBoundary.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 50171 40c98f9b69fa
permissions -rw-r--r--
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException Summary: Change implementation to use iterator instead of plain LinkedList Reviewed-by: dfuchs, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50171
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
     1
/*
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
     4
 *
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
     8
 *
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    13
 * accompanied this code).
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    14
 *
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    18
 *
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    21
 * questions.
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    22
 */
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    23
/*
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    24
 * @test
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    25
 * @bug 8201867
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    26
 * @summary Kerberos keytabs with holes in certain places are parsed incorrectly
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    27
 * @modules java.security.jgss/sun.security.krb5
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    28
 *          java.security.jgss/sun.security.krb5.internal.ktab
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    29
 * @run main/othervm BufferBoundary
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    30
 */
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    31
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    32
import sun.security.krb5.PrincipalName;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    33
import sun.security.krb5.internal.ktab.KeyTab;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    34
import sun.security.krb5.internal.ktab.KeyTabEntry;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    35
import sun.security.krb5.internal.ktab.KeyTabInputStream;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    36
import sun.security.krb5.internal.ktab.KeyTabOutputStream;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    37
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    38
import java.io.FileInputStream;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    39
import java.io.FileOutputStream;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    40
import java.io.IOException;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    41
import java.nio.file.Files;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    42
import java.nio.file.Paths;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    43
import java.util.List;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    44
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    45
public class BufferBoundary {
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    47
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    48
        // We force using one etype so the output is determined
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    49
        Files.write(Paths.get("krb5.conf"), List.of(
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    50
                "[libdefaults]",
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    51
                "default_tkt_enctypes = aes128-cts"
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    52
        ));
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    53
        System.setProperty("java.security.krb5.conf", "krb5.conf");
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    54
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    55
        KeyTab kt = KeyTab.create("ktab");
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    56
        int num = 0;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    57
        while (true) {
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    58
            num++;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    59
            PrincipalName pn = new PrincipalName(
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    60
                    String.format("user%03d@REALM", num));
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    61
            kt.addEntry(pn, "password".toCharArray(), num, true);
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    62
            int length = 2;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    63
            for (KeyTabEntry e : kt.getEntries()) {
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    64
                length += e.entryLength() + 4;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    65
            }
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    66
            // Create enough big keytab
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    67
            if (length > 8400) {
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    68
                break;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    69
            }
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    70
        }
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    71
        kt.save();
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    72
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    73
        int count = 0;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    74
        boolean written = false;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    75
        try (KeyTabInputStream kin
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    76
                     = new KeyTabInputStream(new FileInputStream("ktab"));
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    77
                KeyTabOutputStream kout
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    78
                        = new KeyTabOutputStream(new FileOutputStream("ktab2"))) {
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    79
            kout.write(kin.readNBytes(2)); // version
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    80
            count += 2;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    81
            while (true) {
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    82
                int len;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    83
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    84
                try {
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    85
                    len = kin.read(4);
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    86
                } catch (IOException e) {
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    87
                    break; // reach end
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    88
                }
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    89
                count += 4;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    90
                byte[] data = kin.readNBytes(len);
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    91
                count += len;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    92
                if (count < 8192 || written) {
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    93
                    kout.write32(len);
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    94
                    kout.write(data);
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    95
                } else {
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    96
                    // Create a hole on the 8192 boundary
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    97
                    kout.write32(-len);
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    98
                    kout.write(new byte[len]);
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
    99
                    written = true;
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
   100
                }
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
   101
            }
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
   102
        }
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
   103
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
   104
        kt = KeyTab.getInstance("ktab2");
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
   105
        if (!kt.isValid()) {
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
   106
            throw new Exception("Should be valid");
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
   107
        }
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
   108
        if (kt.getEntries().length != num - 1) {
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
   109
            throw new Exception("Should have " + (num - 1) + " entries");
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
   110
        }
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
   111
    }
40c98f9b69fa 8201867: Kerberos keytabs with holes in certain places are parsed incorrectly
weijun
parents:
diff changeset
   112
}