blob: 0ae5b82bf949dfa483643bc6f65328c11bd1065b [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-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
27package com.sun.security.sasl.gsskerb;
28
29import java.io.IOException;
30import java.util.Map;
31import java.util.logging.Logger;
32import java.util.logging.Level;
33import javax.security.sasl.*;
34import com.sun.security.sasl.util.AbstractSaslImpl;
35import org.ietf.jgss.*;
36
37abstract class GssKrb5Base extends AbstractSaslImpl {
38
39 private static final String KRB5_OID_STR = "1.2.840.113554.1.2.2";
40 protected static Oid KRB5_OID;
41 protected static final byte[] EMPTY = new byte[0];
42
43 static {
44 try {
45 KRB5_OID = new Oid(KRB5_OID_STR);
46 } catch (GSSException ignore) {}
47 }
48
49 protected GSSContext secCtx = null;
50 protected MessageProp msgProp; // QOP and privacy for unwrap
51 protected static final int JGSS_QOP = 0; // unrelated to SASL QOP mask
52
53 protected GssKrb5Base(Map props, String className) throws SaslException {
54 super(props, className);
55 }
56
57 /**
58 * Retrieves this mechanism's name.
59 *
60 * @return The string "GSSAPI".
61 */
62 public String getMechanismName() {
63 return "GSSAPI";
64 }
65
66 public byte[] unwrap(byte[] incoming, int start, int len)
67 throws SaslException {
68 if (!completed) {
69 throw new IllegalStateException("GSSAPI authentication not completed");
70 }
71
72 // integrity will be true if either privacy or integrity negotiated
73 if (!integrity) {
74 throw new IllegalStateException("No security layer negotiated");
75 }
76
77 try {
78 byte[] answer = secCtx.unwrap(incoming, start, len, msgProp);
79 if (logger.isLoggable(Level.FINEST)) {
80 traceOutput(myClassName, "KRB501:Unwrap", "incoming: ",
81 incoming, start, len);
82 traceOutput(myClassName, "KRB502:Unwrap", "unwrapped: ",
83 answer, 0, answer.length);
84 }
85 return answer;
86 } catch (GSSException e) {
87 throw new SaslException("Problems unwrapping SASL buffer", e);
88 }
89 }
90
91 public byte[] wrap(byte[] outgoing, int start, int len) throws SaslException {
92 if (!completed) {
93 throw new IllegalStateException("GSSAPI authentication not completed");
94 }
95
96 // integrity will be true if either privacy or integrity negotiated
97 if (!integrity) {
98 throw new IllegalStateException("No security layer negotiated");
99 }
100
101 // Generate GSS token
102 try {
103 byte[] answer = secCtx.wrap(outgoing, start, len, msgProp);
104 if (logger.isLoggable(Level.FINEST)) {
105 traceOutput(myClassName, "KRB503:Wrap", "outgoing: ",
106 outgoing, start, len);
107 traceOutput(myClassName, "KRB504:Wrap", "wrapped: ",
108 answer, 0, answer.length);
109 }
110 return answer;
111
112 } catch (GSSException e) {
113 throw new SaslException("Problem performing GSS wrap", e);
114 }
115 }
116
117 public void dispose() throws SaslException {
118 if (secCtx != null) {
119 try {
120 secCtx.dispose();
121 } catch (GSSException e) {
122 throw new SaslException("Problem disposing GSS context", e);
123 }
124 secCtx = null;
125 }
126 }
127
128 protected void finalize() throws Throwable {
129 dispose();
130 }
131}