001: /*
002: * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.security.sasl.gsskerb;
027:
028: import java.io.IOException;
029: import java.util.Map;
030: import java.util.logging.Logger;
031: import java.util.logging.Level;
032: import javax.security.sasl.*;
033: import com.sun.security.sasl.util.AbstractSaslImpl;
034: import org.ietf.jgss.*;
035:
036: abstract class GssKrb5Base extends AbstractSaslImpl {
037:
038: private static final String KRB5_OID_STR = "1.2.840.113554.1.2.2";
039: protected static Oid KRB5_OID;
040: protected static final byte[] EMPTY = new byte[0];
041:
042: static {
043: try {
044: KRB5_OID = new Oid(KRB5_OID_STR);
045: } catch (GSSException ignore) {
046: }
047: }
048:
049: protected GSSContext secCtx = null;
050: protected MessageProp msgProp; // QOP and privacy for unwrap
051: protected static final int JGSS_QOP = 0; // unrelated to SASL QOP mask
052:
053: protected GssKrb5Base(Map props, String className)
054: throws SaslException {
055: super (props, className);
056: }
057:
058: /**
059: * Retrieves this mechanism's name.
060: *
061: * @return The string "GSSAPI".
062: */
063: public String getMechanismName() {
064: return "GSSAPI";
065: }
066:
067: public byte[] unwrap(byte[] incoming, int start, int len)
068: throws SaslException {
069: if (!completed) {
070: throw new IllegalStateException(
071: "GSSAPI authentication not completed");
072: }
073:
074: // integrity will be true if either privacy or integrity negotiated
075: if (!integrity) {
076: throw new IllegalStateException(
077: "No security layer negotiated");
078: }
079:
080: try {
081: byte[] answer = secCtx
082: .unwrap(incoming, start, len, msgProp);
083: if (logger.isLoggable(Level.FINEST)) {
084: traceOutput(myClassName, "KRB501:Unwrap", "incoming: ",
085: incoming, start, len);
086: traceOutput(myClassName, "KRB502:Unwrap",
087: "unwrapped: ", answer, 0, answer.length);
088: }
089: return answer;
090: } catch (GSSException e) {
091: throw new SaslException("Problems unwrapping SASL buffer",
092: e);
093: }
094: }
095:
096: public byte[] wrap(byte[] outgoing, int start, int len)
097: throws SaslException {
098: if (!completed) {
099: throw new IllegalStateException(
100: "GSSAPI authentication not completed");
101: }
102:
103: // integrity will be true if either privacy or integrity negotiated
104: if (!integrity) {
105: throw new IllegalStateException(
106: "No security layer negotiated");
107: }
108:
109: // Generate GSS token
110: try {
111: byte[] answer = secCtx.wrap(outgoing, start, len, msgProp);
112: if (logger.isLoggable(Level.FINEST)) {
113: traceOutput(myClassName, "KRB503:Wrap", "outgoing: ",
114: outgoing, start, len);
115: traceOutput(myClassName, "KRB504:Wrap", "wrapped: ",
116: answer, 0, answer.length);
117: }
118: return answer;
119:
120: } catch (GSSException e) {
121: throw new SaslException("Problem performing GSS wrap", e);
122: }
123: }
124:
125: public void dispose() throws SaslException {
126: if (secCtx != null) {
127: try {
128: secCtx.dispose();
129: } catch (GSSException e) {
130: throw new SaslException(
131: "Problem disposing GSS context", e);
132: }
133: secCtx = null;
134: }
135: }
136:
137: protected void finalize() throws Throwable {
138: dispose();
139: }
140: }
|