blob: a506394703bd9996ebc2cc0f5a1d8353a28bc527 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2006 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
26package sun.security.jgss;
27
28import org.ietf.jgss.*;
29import sun.security.jgss.spi.*;
30import sun.security.jgss.*;
31import sun.security.util.ObjectIdentifier;
32import java.io.InputStream;
33import java.io.OutputStream;
34import java.io.ByteArrayInputStream;
35import java.io.ByteArrayOutputStream;
36import java.io.IOException;
37
38
39/**
40 * This class represents the JGSS security context and its associated
41 * operations. JGSS security contexts are established between
42 * peers using locally established credentials. Multiple contexts
43 * may exist simultaneously between a pair of peers, using the same
44 * or different set of credentials. The JGSS is independent of
45 * the underlying transport protocols and depends on its callers to
46 * transport the tokens between peers.
47 * <p>
48 * The context object can be thought of as having 3 implicit states:
49 * before it is established, during its context establishment, and
50 * after a fully established context exists.
51 * <p>
52 * Before the context establishment phase is initiated, the context
53 * initiator may request specific characteristics desired of the
54 * established context. These can be set using the set methods. After the
55 * context is established, the caller can check the actual characteristic
56 * and services offered by the context using the query methods.
57 * <p>
58 * The context establishment phase begins with the first call to the
59 * initSecContext method by the context initiator. During this phase the
60 * initSecContext and acceptSecContext methods will produce GSS-API
61 * authentication tokens which the calling application needs to send to its
62 * peer. The initSecContext and acceptSecContext methods may
63 * return a CONTINUE_NEEDED code which indicates that a token is needed
64 * from its peer in order to continue the context establishment phase. A
65 * return code of COMPLETE signals that the local end of the context is
66 * established. This may still require that a token be sent to the peer,
67 * depending if one is produced by GSS-API. The isEstablished method can
68 * also be used to determine if the local end of the context has been
69 * fully established. During the context establishment phase, the
70 * isProtReady method may be called to determine if the context can be
71 * used for the per-message operations. This allows implementation to
72 * use per-message operations on contexts which aren't fully established.
73 * <p>
74 * After the context has been established or the isProtReady method
75 * returns "true", the query routines can be invoked to determine the actual
76 * characteristics and services of the established context. The
77 * application can also start using the per-message methods of wrap and
78 * getMIC to obtain cryptographic operations on application supplied data.
79 * <p>
80 * When the context is no longer needed, the application should call
81 * dispose to release any system resources the context may be using.
82 * <DL><DT><B>RFC 2078</b>
83 * <DD>This class corresponds to the context level calls together with
84 * the per message calls of RFC 2078. The gss_init_sec_context and
85 * gss_accept_sec_context calls have been made simpler by only taking
86 * required parameters. The context can have its properties set before
87 * the first call to initSecContext. The supplementary status codes for the
88 * per-message operations are returned in an instance of the MessageProp
89 * class, which is used as an argument in these calls.</dl>
90 */
91class GSSContextImpl implements GSSContext {
92
93 private GSSManagerImpl gssManager = null;
94
95 // private flags for the context state
96 private static final int PRE_INIT = 1;
97 private static final int IN_PROGRESS = 2;
98 private static final int READY = 3;
99 private static final int DELETED = 4;
100
101 // instance variables
102 private int currentState = PRE_INIT;
103 private boolean initiator;
104
105 private GSSContextSpi mechCtxt = null;
106 private Oid mechOid = null;
107 private ObjectIdentifier objId = null;
108
109 private GSSCredentialImpl myCred = null;
110 private GSSCredentialImpl delegCred = null;
111
112 private GSSNameImpl srcName = null;
113 private GSSNameImpl targName = null;
114
115 private int reqLifetime = INDEFINITE_LIFETIME;
116 private ChannelBinding channelBindings = null;
117
118 private boolean reqConfState = true;
119 private boolean reqIntegState = true;
120 private boolean reqMutualAuthState = true;
121 private boolean reqReplayDetState = true;
122 private boolean reqSequenceDetState = true;
123 private boolean reqCredDelegState = false;
124 private boolean reqAnonState = false;
125
126 /**
127 * Creates a GSSContextImp on the context initiator's side.
128 */
129 public GSSContextImpl(GSSManagerImpl gssManager, GSSName peer, Oid mech,
130 GSSCredential myCred, int lifetime)
131 throws GSSException {
132 if ((peer == null) || !(peer instanceof GSSNameImpl)) {
133 throw new GSSException(GSSException.BAD_NAME);
134 }
135 if (mech == null) mech = ProviderList.DEFAULT_MECH_OID;
136
137 this.gssManager = gssManager;
138 this.myCred = (GSSCredentialImpl) myCred; // XXX Check first
139 reqLifetime = lifetime;
140 targName = (GSSNameImpl)peer;
141 this.mechOid = mech;
142 initiator = true;
143 }
144
145 /**
146 * Creates a GSSContextImpl on the context acceptor's side.
147 */
148 public GSSContextImpl(GSSManagerImpl gssManager, GSSCredential myCred)
149 throws GSSException {
150 this.gssManager = gssManager;
151 this.myCred = (GSSCredentialImpl) myCred; // XXX Check first
152 initiator = false;
153 }
154
155 /**
156 * Creates a GSSContextImpl out of a previously exported
157 * GSSContext.
158 *
159 * @see #isTransferable
160 */
161 public GSSContextImpl(GSSManagerImpl gssManager, byte[] interProcessToken)
162 throws GSSException {
163 this.gssManager = gssManager;
164 mechCtxt = gssManager.getMechanismContext(interProcessToken);
165 initiator = mechCtxt.isInitiator();
166 this.mechOid = mechCtxt.getMech();
167 }
168
169 public byte[] initSecContext(byte inputBuf[], int offset, int len)
170 throws GSSException {
171 /*
172 * Size of ByteArrayOutputStream will double each time that extra
173 * bytes are to be written. Usually, without delegation, a GSS
174 * initial token containing the Kerberos AP-REQ is between 400 and
175 * 600 bytes.
176 */
177 ByteArrayOutputStream bos = new ByteArrayOutputStream(600);
178 ByteArrayInputStream bin =
179 new ByteArrayInputStream(inputBuf, offset, len);
180 int size = initSecContext(bin, bos);
181 return (size == 0? null : bos.toByteArray());
182 }
183
184 public int initSecContext(InputStream inStream,
185 OutputStream outStream) throws GSSException {
186
187 if (mechCtxt != null && currentState != IN_PROGRESS) {
188 throw new GSSExceptionImpl(GSSException.FAILURE,
189 "Illegal call to initSecContext");
190 }
191
192 GSSHeader gssHeader = null;
193 int inTokenLen = -1;
194 GSSCredentialSpi credElement = null;
195 boolean firstToken = false;
196
197 try {
198 if (mechCtxt == null) {
199 if (myCred != null) {
200 try {
201 credElement = myCred.getElement(mechOid, true);
202 } catch (GSSException ge) {
203 if (GSSUtil.isSpNegoMech(mechOid) &&
204 ge.getMajor() == GSSException.NO_CRED) {
205 credElement = myCred.getElement
206 (myCred.getMechs()[0], true);
207 } else {
208 throw ge;
209 }
210 }
211 }
212 GSSNameSpi nameElement = targName.getElement(mechOid);
213 mechCtxt = gssManager.getMechanismContext(nameElement,
214 credElement,
215 reqLifetime,
216 mechOid);
217 mechCtxt.requestConf(reqConfState);
218 mechCtxt.requestInteg(reqIntegState);
219 mechCtxt.requestCredDeleg(reqCredDelegState);
220 mechCtxt.requestMutualAuth(reqMutualAuthState);
221 mechCtxt.requestReplayDet(reqReplayDetState);
222 mechCtxt.requestSequenceDet(reqSequenceDetState);
223 mechCtxt.requestAnonymity(reqAnonState);
224 mechCtxt.setChannelBinding(channelBindings);
225
226 objId = new ObjectIdentifier(mechOid.toString());
227
228 currentState = IN_PROGRESS;
229 firstToken = true;
230 } else {
231 if (mechCtxt.getProvider().getName().equals("SunNativeGSS") ||
232 GSSUtil.isSpNegoMech(mechOid)) {
233 // do not parse GSS header for native provider or SPNEGO
234 // mech
235 } else {
236 // parse GSS header
237 gssHeader = new GSSHeader(inStream);
238 if (!gssHeader.getOid().equals((Object) objId))
239 throw new GSSExceptionImpl
240 (GSSException.DEFECTIVE_TOKEN,
241 "Mechanism not equal to " +
242 mechOid.toString() +
243 " in initSecContext token");
244 inTokenLen = gssHeader.getMechTokenLength();
245 }
246 }
247
248 byte[] obuf = mechCtxt.initSecContext(inStream, inTokenLen);
249
250 int retVal = 0;
251
252 if (obuf != null) {
253 retVal = obuf.length;
254 if (mechCtxt.getProvider().getName().equals("SunNativeGSS") ||
255 (!firstToken && GSSUtil.isSpNegoMech(mechOid))) {
256 // do not add GSS header for native provider or SPNEGO
257 // except for the first SPNEGO token
258 } else {
259 // add GSS header
260 gssHeader = new GSSHeader(objId, obuf.length);
261 retVal += gssHeader.encode(outStream);
262 }
263 outStream.write(obuf);
264 }
265
266 if (mechCtxt.isEstablished())
267 currentState = READY;
268
269 return retVal;
270
271 } catch (IOException e) {
272 throw new GSSExceptionImpl(GSSException.DEFECTIVE_TOKEN,
273 e.getMessage());
274 }
275 }
276
277 public byte[] acceptSecContext(byte inTok[], int offset, int len)
278 throws GSSException {
279
280 /*
281 * Usually initial GSS token containing a Kerberos AP-REP is less
282 * than 100 bytes.
283 */
284 ByteArrayOutputStream bos = new ByteArrayOutputStream(100);
285 acceptSecContext(new ByteArrayInputStream(inTok, offset, len),
286 bos);
287 return bos.toByteArray();
288 }
289
290 public void acceptSecContext(InputStream inStream,
291 OutputStream outStream) throws GSSException {
292
293 if (mechCtxt != null && currentState != IN_PROGRESS) {
294 throw new GSSExceptionImpl(GSSException.FAILURE,
295 "Illegal call to acceptSecContext");
296 }
297
298 GSSHeader gssHeader = null;
299 int inTokenLen = -1;
300 GSSCredentialSpi credElement = null;
301
302 try {
303 if (mechCtxt == null) {
304 // mechOid will be null for an acceptor's context
305 gssHeader = new GSSHeader(inStream);
306 inTokenLen = gssHeader.getMechTokenLength();
307
308 /*
309 * Convert ObjectIdentifier to Oid
310 */
311 objId = gssHeader.getOid();
312 mechOid = new Oid(objId.toString());
313 // System.out.println("Entered GSSContextImpl.acceptSecContext"
314 // + " with mechanism = " + mechOid);
315 if (myCred != null) {
316 credElement = myCred.getElement(mechOid, false);
317 }
318
319 mechCtxt = gssManager.getMechanismContext(credElement,
320 mechOid);
321 mechCtxt.setChannelBinding(channelBindings);
322
323 currentState = IN_PROGRESS;
324 } else {
325 if (mechCtxt.getProvider().getName().equals("SunNativeGSS") ||
326 (GSSUtil.isSpNegoMech(mechOid))) {
327 // do not parse GSS header for native provider and SPNEGO
328 } else {
329 // parse GSS Header
330 gssHeader = new GSSHeader(inStream);
331 if (!gssHeader.getOid().equals((Object) objId))
332 throw new GSSExceptionImpl
333 (GSSException.DEFECTIVE_TOKEN,
334 "Mechanism not equal to " +
335 mechOid.toString() +
336 " in acceptSecContext token");
337 inTokenLen = gssHeader.getMechTokenLength();
338 }
339 }
340
341 byte[] obuf = mechCtxt.acceptSecContext(inStream, inTokenLen);
342
343 if (obuf != null) {
344 int retVal = obuf.length;
345 if (mechCtxt.getProvider().getName().equals("SunNativeGSS") ||
346 (GSSUtil.isSpNegoMech(mechOid))) {
347 // do not add GSS header for native provider and SPNEGO
348 } else {
349 // add GSS header
350 gssHeader = new GSSHeader(objId, obuf.length);
351 retVal += gssHeader.encode(outStream);
352 }
353 outStream.write(obuf);
354 }
355
356 if (mechCtxt.isEstablished()) {
357 currentState = READY;
358 }
359 } catch (IOException e) {
360 throw new GSSExceptionImpl(GSSException.DEFECTIVE_TOKEN,
361 e.getMessage());
362 }
363 }
364
365 public boolean isEstablished() {
366 if (mechCtxt == null)
367 return false;
368 else
369 return (currentState == READY);
370 }
371
372 public int getWrapSizeLimit(int qop, boolean confReq,
373 int maxTokenSize) throws GSSException {
374 if (mechCtxt != null)
375 return mechCtxt.getWrapSizeLimit(qop, confReq, maxTokenSize);
376 else
377 throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
378 "No mechanism context yet!");
379 }
380
381 public byte[] wrap(byte inBuf[], int offset, int len,
382 MessageProp msgProp) throws GSSException {
383 if (mechCtxt != null)
384 return mechCtxt.wrap(inBuf, offset, len, msgProp);
385 else
386 throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
387 "No mechanism context yet!");
388 }
389
390 public void wrap(InputStream inStream, OutputStream outStream,
391 MessageProp msgProp) throws GSSException {
392 if (mechCtxt != null)
393 mechCtxt.wrap(inStream, outStream, msgProp);
394 else
395 throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
396 "No mechanism context yet!");
397 }
398
399 public byte [] unwrap(byte[] inBuf, int offset, int len,
400 MessageProp msgProp) throws GSSException {
401 if (mechCtxt != null)
402 return mechCtxt.unwrap(inBuf, offset, len, msgProp);
403 else
404 throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
405 "No mechanism context yet!");
406 }
407
408 public void unwrap(InputStream inStream, OutputStream outStream,
409 MessageProp msgProp) throws GSSException {
410 if (mechCtxt != null)
411 mechCtxt.unwrap(inStream, outStream, msgProp);
412 else
413 throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
414 "No mechanism context yet!");
415 }
416
417 public byte[] getMIC(byte []inMsg, int offset, int len,
418 MessageProp msgProp) throws GSSException {
419 if (mechCtxt != null)
420 return mechCtxt.getMIC(inMsg, offset, len, msgProp);
421 else
422 throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
423 "No mechanism context yet!");
424 }
425
426 public void getMIC(InputStream inStream, OutputStream outStream,
427 MessageProp msgProp) throws GSSException {
428 if (mechCtxt != null)
429 mechCtxt.getMIC(inStream, outStream, msgProp);
430 else
431 throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
432 "No mechanism context yet!");
433 }
434
435 public void verifyMIC(byte[] inTok, int tokOffset, int tokLen,
436 byte[] inMsg, int msgOffset, int msgLen,
437 MessageProp msgProp) throws GSSException {
438 if (mechCtxt != null)
439 mechCtxt.verifyMIC(inTok, tokOffset, tokLen,
440 inMsg, msgOffset, msgLen, msgProp);
441 else
442 throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
443 "No mechanism context yet!");
444 }
445
446 public void verifyMIC(InputStream tokStream, InputStream msgStream,
447 MessageProp msgProp) throws GSSException {
448 if (mechCtxt != null)
449 mechCtxt.verifyMIC(tokStream, msgStream, msgProp);
450 else
451 throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
452 "No mechanism context yet!");
453 }
454
455 public byte[] export() throws GSSException {
456 // Defaults to null to match old behavior
457 byte[] result = null;
458 // Only allow context export from native provider since JGSS
459 // still has not defined its own interprocess token format
460 if (mechCtxt.isTransferable() &&
461 mechCtxt.getProvider().getName().equals("SunNativeGSS")) {
462 result = mechCtxt.export();
463 }
464 return result;
465 }
466
467 public void requestMutualAuth(boolean state) throws GSSException {
468 if (mechCtxt == null)
469 reqMutualAuthState = state;
470 }
471
472 public void requestReplayDet(boolean state) throws GSSException {
473 if (mechCtxt == null)
474 reqReplayDetState = state;
475 }
476
477 public void requestSequenceDet(boolean state) throws GSSException {
478 if (mechCtxt == null)
479 reqSequenceDetState = state;
480 }
481
482 public void requestCredDeleg(boolean state) throws GSSException {
483 if (mechCtxt == null)
484 reqCredDelegState = state;
485 }
486
487 public void requestAnonymity(boolean state) throws GSSException {
488 if (mechCtxt == null)
489 reqAnonState = state;
490 }
491
492 public void requestConf(boolean state) throws GSSException {
493 if (mechCtxt == null)
494 reqConfState = state;
495 }
496
497 public void requestInteg(boolean state) throws GSSException {
498 if (mechCtxt == null)
499 reqIntegState = state;
500 }
501
502 public void requestLifetime(int lifetime) throws GSSException {
503 if (mechCtxt == null)
504 reqLifetime = lifetime;
505 }
506
507 public void setChannelBinding(ChannelBinding channelBindings)
508 throws GSSException {
509
510 if (mechCtxt == null)
511 this.channelBindings = channelBindings;
512
513 }
514
515 public boolean getCredDelegState() {
516 if (mechCtxt != null)
517 return mechCtxt.getCredDelegState();
518 else
519 return reqCredDelegState;
520 }
521
522 public boolean getMutualAuthState() {
523 if (mechCtxt != null)
524 return mechCtxt.getMutualAuthState();
525 else
526 return reqMutualAuthState;
527 }
528
529 public boolean getReplayDetState() {
530 if (mechCtxt != null)
531 return mechCtxt.getReplayDetState();
532 else
533 return reqReplayDetState;
534 }
535
536 public boolean getSequenceDetState() {
537 if (mechCtxt != null)
538 return mechCtxt.getSequenceDetState();
539 else
540 return reqSequenceDetState;
541 }
542
543 public boolean getAnonymityState() {
544 if (mechCtxt != null)
545 return mechCtxt.getAnonymityState();
546 else
547 return reqAnonState;
548 }
549
550 public boolean isTransferable() throws GSSException {
551 if (mechCtxt != null)
552 return mechCtxt.isTransferable();
553 else
554 return false;
555 }
556
557 public boolean isProtReady() {
558 if (mechCtxt != null)
559 return mechCtxt.isProtReady();
560 else
561 return false;
562 }
563
564 public boolean getConfState() {
565 if (mechCtxt != null)
566 return mechCtxt.getConfState();
567 else
568 return reqConfState;
569 }
570
571 public boolean getIntegState() {
572 if (mechCtxt != null)
573 return mechCtxt.getIntegState();
574 else
575 return reqIntegState;
576 }
577
578 public int getLifetime() {
579 if (mechCtxt != null)
580 return mechCtxt.getLifetime();
581 else
582 return reqLifetime;
583 }
584
585 public GSSName getSrcName() throws GSSException {
586 if (srcName == null) {
587 srcName = GSSNameImpl.wrapElement
588 (gssManager, mechCtxt.getSrcName());
589 }
590 return srcName;
591 }
592
593 public GSSName getTargName() throws GSSException {
594 if (targName == null) {
595 targName = GSSNameImpl.wrapElement
596 (gssManager, mechCtxt.getTargName());
597 }
598 return targName;
599 }
600
601 public Oid getMech() throws GSSException {
602 if (mechCtxt != null) {
603 return mechCtxt.getMech();
604 }
605 return mechOid;
606 }
607
608 public GSSCredential getDelegCred() throws GSSException {
609
610 if (mechCtxt == null)
611 throw new GSSExceptionImpl(GSSException.NO_CONTEXT,
612 "No mechanism context yet!");
613 GSSCredentialSpi delCredElement = mechCtxt.getDelegCred();
614 return (delCredElement == null ?
615 null : new GSSCredentialImpl(gssManager, delCredElement));
616 }
617
618 public boolean isInitiator() throws GSSException {
619 return initiator;
620 }
621
622 public void dispose() throws GSSException {
623 currentState = DELETED;
624 if (mechCtxt != null) {
625 mechCtxt.dispose();
626 mechCtxt = null;
627 }
628 myCred = null;
629 srcName = null;
630 targName = null;
631 }
632}