1/*
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 it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation.  Oracle designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 */
24
25/*
26 *
27 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
28 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
29 */
30
31package sun.security.krb5;
32
33import sun.security.krb5.internal.*;
34
35abstract class KrbAppMessage {
36
37    private static boolean DEBUG = Krb5.DEBUG;
38    /**
39     * Common checks for KRB-PRIV and KRB-SAFE
40     */
41    void check(KerberosTime packetTimestamp,
42               Integer packetUsec,
43               Integer packetSeqNumber,
44               HostAddress packetSAddress,
45               HostAddress packetRAddress,
46               SeqNumber seqNumber,
47               HostAddress sAddress,
48               HostAddress rAddress,
49               boolean timestampRequired,
50               boolean seqNumberRequired,
51               PrincipalName packetPrincipal)
52        throws KrbApErrException {
53
54        if (!Krb5.AP_EMPTY_ADDRESSES_ALLOWED || sAddress != null) {
55            if (packetSAddress == null || sAddress == null ||
56                !packetSAddress.equals(sAddress)) {
57                if (DEBUG && packetSAddress == null) {
58                    System.out.println("packetSAddress is null");
59                }
60                if (DEBUG && sAddress == null) {
61                    System.out.println("sAddress is null");
62                }
63                throw new KrbApErrException(Krb5.KRB_AP_ERR_BADADDR);
64            }
65        }
66
67        if (!Krb5.AP_EMPTY_ADDRESSES_ALLOWED || rAddress != null) {
68            if (packetRAddress == null || rAddress == null ||
69                !packetRAddress.equals(rAddress))
70                throw new KrbApErrException(Krb5.KRB_AP_ERR_BADADDR);
71        }
72
73        if (packetTimestamp != null) {
74            if (packetUsec != null) {
75                packetTimestamp =
76                    packetTimestamp.withMicroSeconds(packetUsec.intValue());
77            }
78            if (!packetTimestamp.inClockSkew()) {
79                throw new KrbApErrException(Krb5.KRB_AP_ERR_SKEW);
80            }
81        } else {
82            if (timestampRequired) {
83                throw new KrbApErrException(Krb5.KRB_AP_ERR_SKEW);
84            }
85        }
86
87        // XXX check replay cache
88        // if (rcache.repeated(packetTimestamp, packetUsec, packetSAddress))
89        //      throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
90
91        // XXX consider moving up to api level
92        if (seqNumber == null && seqNumberRequired == true)
93            throw new KrbApErrException(Krb5.API_INVALID_ARG);
94
95        if (packetSeqNumber != null && seqNumber != null) {
96            if (packetSeqNumber.intValue() != seqNumber.current())
97                throw new KrbApErrException(Krb5.KRB_AP_ERR_BADORDER);
98            // should be done only when no more exceptions are possible
99            seqNumber.step();
100        } else {
101            if (seqNumberRequired) {
102                throw new KrbApErrException(Krb5.KRB_AP_ERR_BADORDER);
103            }
104        }
105
106        // Must not be relaxed, per RFC 4120
107        if (packetTimestamp == null && packetSeqNumber == null)
108            throw new KrbApErrException(Krb5.KRB_AP_ERR_MODIFIED);
109
110        // XXX check replay cache
111        // rcache.save_identifier(packetTimestamp, packetUsec, packetSAddress,
112        // packetPrincipal, pcaketRealm);
113    }
114
115}
116