1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22 */
  23 
  24 /*
  25  * @test
  26  * @bug 8001326
  27  * @run main/othervm ReplayCachePrecise
  28  * @summary when there are 2 two AuthTime with the same time but different hash,
  29  * it's not a replay.
  30 */
  31 
  32 import java.nio.ByteBuffer;
  33 import java.nio.channels.SeekableByteChannel;
  34 import java.nio.file.Files;
  35 import java.nio.file.Paths;
  36 import java.nio.file.StandardOpenOption;
  37 import java.util.Random;
  38 import sun.security.krb5.KrbException;
  39 import sun.security.krb5.internal.KerberosTime;
  40 import sun.security.krb5.internal.ReplayCache;
  41 import sun.security.krb5.internal.rcache.AuthTimeWithHash;
  42 
  43 public class ReplayCachePrecise {
  44     static final String client = "dummy@REALM";
  45     static final String server = "server/localhost@REALM";
  46     static final Random rand = new Random();
  47 
  48     public static void main(String[] args) throws Exception {
  49 
  50         AuthTimeWithHash a1 = new AuthTimeWithHash(client, server, time(0), 0,
  51                 "HASH", "1111111111111111");
  52         AuthTimeWithHash a2 = new AuthTimeWithHash(client, server, time(0), 0,
  53                 "HASH", "2222222222222222");
  54         KerberosTime now = new KerberosTime(time(0)*1000L);
  55 
  56         // When all new styles, must exact match
  57         ReplayCache cache = ReplayCache.getInstance("dfl:./c1");
  58         cache.checkAndStore(now, a1);
  59         cache.checkAndStore(now, a2);
  60 
  61         // When only old style in cache, partial match
  62         cache = ReplayCache.getInstance("dfl:./c2");
  63         cache.checkAndStore(now, a1);
  64         // A small surgery to remove the new style from the cache file
  65         SeekableByteChannel ch = Files.newByteChannel(Paths.get("c2"),
  66                 StandardOpenOption.WRITE,
  67                 StandardOpenOption.READ);
  68         ch.position(6);
  69         ch.write(ByteBuffer.wrap(a1.encode(false)));
  70         ch.truncate(ch.position());
  71         ch.close();
  72         try {
  73             cache.checkAndStore(now, a2);
  74             throw new Exception();
  75         } catch (KrbException ke) {
  76             // Correct
  77             System.out.println(ke);
  78         }
  79     }
  80 
  81     private static int time(int x) {
  82         return (int)(System.currentTimeMillis()/1000) + x;
  83     }
  84 }