Path: blob/master/test/jdk/sun/security/krb5/ktab/BufferBoundary.java
41155 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @bug 820186725* @summary Kerberos keytabs with holes in certain places are parsed incorrectly26* @modules java.security.jgss/sun.security.krb527* java.security.jgss/sun.security.krb5.internal.ktab28* @run main/othervm BufferBoundary29*/3031import sun.security.krb5.PrincipalName;32import sun.security.krb5.internal.ktab.KeyTab;33import sun.security.krb5.internal.ktab.KeyTabEntry;34import sun.security.krb5.internal.ktab.KeyTabInputStream;35import sun.security.krb5.internal.ktab.KeyTabOutputStream;3637import java.io.FileInputStream;38import java.io.FileOutputStream;39import java.io.IOException;40import java.nio.file.Files;41import java.nio.file.Paths;42import java.util.List;4344public class BufferBoundary {45public static void main(String[] args) throws Exception {4647// We force using one etype so the output is determined48Files.write(Paths.get("krb5.conf"), List.of(49"[libdefaults]",50"default_tkt_enctypes = aes128-cts"51));52System.setProperty("java.security.krb5.conf", "krb5.conf");5354KeyTab kt = KeyTab.create("ktab");55int num = 0;56while (true) {57num++;58PrincipalName pn = new PrincipalName(59String.format("user%03d@REALM", num));60kt.addEntry(pn, "password".toCharArray(), num, true);61int length = 2;62for (KeyTabEntry e : kt.getEntries()) {63length += e.entryLength() + 4;64}65// Create enough big keytab66if (length > 8400) {67break;68}69}70kt.save();7172int count = 0;73boolean written = false;74try (KeyTabInputStream kin75= new KeyTabInputStream(new FileInputStream("ktab"));76KeyTabOutputStream kout77= new KeyTabOutputStream(new FileOutputStream("ktab2"))) {78kout.write(kin.readNBytes(2)); // version79count += 2;80while (true) {81int len;8283try {84len = kin.read(4);85} catch (IOException e) {86break; // reach end87}88count += 4;89byte[] data = kin.readNBytes(len);90count += len;91if (count < 8192 || written) {92kout.write32(len);93kout.write(data);94} else {95// Create a hole on the 8192 boundary96kout.write32(-len);97kout.write(new byte[len]);98written = true;99}100}101}102103kt = KeyTab.getInstance("ktab2");104if (!kt.isValid()) {105throw new Exception("Should be valid");106}107if (kt.getEntries().length != num - 1) {108throw new Exception("Should have " + (num - 1) + " entries");109}110}111}112113114