blob: 9fc3f2ffe90aa08c37666624bbfdbe1331ef9f2c [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Portions Copyright 2000-2004 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26/*
27 *
28 * (C) Copyright IBM Corp. 1999 All Rights Reserved.
29 * Copyright 1997 The Open Group Research Institute. All rights reserved.
30 */
31
32package sun.security.krb5;
33
34import sun.security.krb5.EncryptionKey;
35import sun.security.krb5.internal.*;
36import sun.security.krb5.internal.crypto.*;
37import java.io.IOException;
38
39class KrbSafe extends KrbAppMessage {
40
41 private byte[] obuf;
42 private byte[] userData;
43
44 public KrbSafe(byte[] userData,
45 Credentials creds,
46 EncryptionKey subKey,
47 KerberosTime timestamp,
48 SeqNumber seqNumber,
49 HostAddress saddr,
50 HostAddress raddr
51 ) throws KrbException, IOException {
52 EncryptionKey reqKey = null;
53 if (subKey != null)
54 reqKey = subKey;
55 else
56 reqKey = creds.key;
57
58 obuf = mk_safe(userData,
59 reqKey,
60 timestamp,
61 seqNumber,
62 saddr,
63 raddr
64 );
65 }
66
67 public KrbSafe(byte[] msg,
68 Credentials creds,
69 EncryptionKey subKey,
70 SeqNumber seqNumber,
71 HostAddress saddr,
72 HostAddress raddr,
73 boolean timestampRequired,
74 boolean seqNumberRequired
75 ) throws KrbException, IOException {
76
77 KRBSafe krb_safe = new KRBSafe(msg);
78
79 EncryptionKey reqKey = null;
80 if (subKey != null)
81 reqKey = subKey;
82 else
83 reqKey = creds.key;
84
85 userData = rd_safe(
86 krb_safe,
87 reqKey,
88 seqNumber,
89 saddr,
90 raddr,
91 timestampRequired,
92 seqNumberRequired,
93 creds.client,
94 creds.client.getRealm()
95 );
96 }
97
98 public byte[] getMessage() {
99 return obuf;
100 }
101
102 public byte[] getData() {
103 return userData;
104 }
105
106 private byte[] mk_safe(byte[] userData,
107 EncryptionKey key,
108 KerberosTime timestamp,
109 SeqNumber seqNumber,
110 HostAddress sAddress,
111 HostAddress rAddress
112 ) throws Asn1Exception, IOException, KdcErrException,
113 KrbApErrException, KrbCryptoException {
114
115 Integer usec = null;
116 Integer seqno = null;
117
118 if (timestamp != null)
119 usec = new Integer(timestamp.getMicroSeconds());
120
121 if (seqNumber != null) {
122 seqno = new Integer(seqNumber.current());
123 seqNumber.step();
124 }
125
126 KRBSafeBody krb_safeBody =
127 new KRBSafeBody(userData,
128 timestamp,
129 usec,
130 seqno,
131 sAddress,
132 rAddress
133 );
134
135 byte[] temp = krb_safeBody.asn1Encode();
136 Checksum cksum = new Checksum(
137 Checksum.SAFECKSUMTYPE_DEFAULT,
138 temp,
139 key,
140 KeyUsage.KU_KRB_SAFE_CKSUM
141 );
142
143 KRBSafe krb_safe = new KRBSafe(krb_safeBody, cksum);
144
145 temp = krb_safe.asn1Encode();
146
147 return krb_safe.asn1Encode();
148 }
149
150 private byte[] rd_safe(KRBSafe krb_safe,
151 EncryptionKey key,
152 SeqNumber seqNumber,
153 HostAddress sAddress,
154 HostAddress rAddress,
155 boolean timestampRequired,
156 boolean seqNumberRequired,
157 PrincipalName cname,
158 Realm crealm
159 ) throws Asn1Exception, KdcErrException,
160 KrbApErrException, IOException, KrbCryptoException {
161
162 byte[] temp = krb_safe.safeBody.asn1Encode();
163
164 if (!krb_safe.cksum.verifyKeyedChecksum(temp, key,
165 KeyUsage.KU_KRB_SAFE_CKSUM)) {
166 throw new KrbApErrException(
167 Krb5.KRB_AP_ERR_MODIFIED);
168 }
169
170 check(krb_safe.safeBody.timestamp,
171 krb_safe.safeBody.usec,
172 krb_safe.safeBody.seqNumber,
173 krb_safe.safeBody.sAddress,
174 krb_safe.safeBody.rAddress,
175 seqNumber,
176 sAddress,
177 rAddress,
178 timestampRequired,
179 seqNumberRequired,
180 cname,
181 crealm
182 );
183
184 return krb_safe.safeBody.userData;
185 }
186}