1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45:
46: import ;
47: import ;
48: import ;
49: import ;
50:
51:
54: public abstract class ServerMechanism
55: implements SaslServer
56: {
57:
58: protected String mechanism;
59:
60: protected String protocol;
61:
62: protected String serverName;
63:
64: protected Map properties;
65:
66: protected CallbackHandler handler;
67:
68: protected boolean complete = false;
69:
70: protected String authorizationID;
71:
72: protected byte[] channelBinding;
73:
74: protected int state = -1;
75:
76: protected IAuthInfoProvider authenticator;
77:
78: protected ServerMechanism(final String mechanism)
79: {
80: super();
81:
82: this.mechanism = mechanism;
83: this.authenticator = AuthInfo.getProvider(mechanism);
84: this.state = -1;
85: }
86:
87: protected abstract void initMechanism() throws SaslException;
88:
89: protected abstract void resetMechanism() throws SaslException;
90:
91: public abstract byte[] evaluateResponse(byte[] response) throws SaslException;
92:
93: public boolean isComplete()
94: {
95: return complete;
96: }
97:
98: public byte[] unwrap(final byte[] incoming, final int offset, final int len)
99: throws SaslException
100: {
101: if (! isComplete())
102: throw new IllegalMechanismStateException();
103: return this.engineUnwrap(incoming, offset, len);
104: }
105:
106: public byte[] wrap(final byte[] outgoing, final int offset, final int len)
107: throws SaslException
108: {
109: if (! isComplete())
110: throw new IllegalMechanismStateException();
111: return this.engineWrap(outgoing, offset, len);
112: }
113:
114: public String getMechanismName()
115: {
116: return this.mechanism;
117: }
118:
119: public String getAuthorizationID()
120: {
121: return this.authorizationID;
122: }
123:
124: public Object getNegotiatedProperty(final String propName)
125: {
126: if (! isComplete())
127: throw new IllegalStateException();
128: if (Sasl.QOP.equals(propName))
129: return getNegotiatedQOP();
130: if (Sasl.STRENGTH.equals(propName))
131: return getNegotiatedStrength();
132: if (Sasl.SERVER_AUTH.equals(propName))
133: return getNegotiatedServerAuth();
134: if (Sasl.MAX_BUFFER.equals(propName))
135: return getNegotiatedMaxBuffer();
136: if (Sasl.RAW_SEND_SIZE.equals(propName))
137: return getNegotiatedRawSendSize();
138: if (Sasl.POLICY_NOPLAINTEXT.equals(propName))
139: return getNegotiatedPolicyNoPlainText();
140: if (Sasl.POLICY_NOACTIVE.equals(propName))
141: return getNegotiatedPolicyNoActive();
142: if (Sasl.POLICY_NODICTIONARY.equals(propName))
143: return getNegotiatedPolicyNoDictionary();
144: if (Sasl.POLICY_NOANONYMOUS.equals(propName))
145: return getNegotiatedPolicyNoAnonymous();
146: if (Sasl.POLICY_FORWARD_SECRECY.equals(propName))
147: return getNegotiatedPolicyForwardSecrecy();
148: if (Sasl.POLICY_PASS_CREDENTIALS.equals(propName))
149: return getNegotiatedPolicyPassCredentials();
150: if (Sasl.REUSE.equals(propName))
151: return getReuse();
152: return null;
153: }
154:
155: public void dispose() throws SaslException
156: {
157: reset();
158: }
159:
160: protected String getNegotiatedQOP()
161: {
162: return Registry.QOP_AUTH;
163: }
164:
165: protected String getNegotiatedStrength()
166: {
167: return Registry.STRENGTH_LOW;
168: }
169:
170: protected String getNegotiatedServerAuth()
171: {
172: return Registry.SERVER_AUTH_FALSE;
173: }
174:
175: protected String getNegotiatedMaxBuffer()
176: {
177: return null;
178: }
179:
180: protected String getNegotiatedPolicyNoPlainText()
181: {
182: return null;
183: }
184:
185: protected String getNegotiatedPolicyNoActive()
186: {
187: return null;
188: }
189:
190: protected String getNegotiatedPolicyNoDictionary()
191: {
192: return null;
193: }
194:
195: protected String getNegotiatedPolicyNoAnonymous()
196: {
197: return null;
198: }
199:
200: protected String getNegotiatedPolicyForwardSecrecy()
201: {
202: return null;
203: }
204:
205: protected String getNegotiatedPolicyPassCredentials()
206: {
207: return null;
208: }
209:
210: protected String getNegotiatedRawSendSize()
211: {
212: return String.valueOf(Registry.SASL_BUFFER_MAX_LIMIT);
213: }
214:
215: protected String getReuse()
216: {
217: return Registry.REUSE_FALSE;
218: }
219:
220: protected byte[] engineUnwrap(final byte[] incoming, final int offset,
221: final int len) throws SaslException
222: {
223: final byte[] result = new byte[len];
224: System.arraycopy(incoming, offset, result, 0, len);
225: return result;
226: }
227:
228: protected byte[] engineWrap(final byte[] outgoing, final int offset,
229: final int len) throws SaslException
230: {
231: final byte[] result = new byte[len];
232: System.arraycopy(outgoing, offset, result, 0, len);
233: return result;
234: }
235:
236:
246: public void init(final Map attributes) throws SaslException
247: {
248: if (state != -1)
249: throw new IllegalMechanismStateException("init()");
250: if (properties == null)
251: properties = new HashMap();
252: else
253: properties.clear();
254: if (attributes != null)
255: {
256: protocol = (String) attributes.get(Registry.SASL_PROTOCOL);
257: serverName = (String) attributes.get(Registry.SASL_SERVER_NAME);
258: handler = (CallbackHandler) attributes.get(Registry.SASL_CALLBACK_HANDLER);
259: channelBinding = (byte[]) attributes.get(Registry.SASL_CHANNEL_BINDING);
260: properties.putAll(attributes);
261: }
262: else
263: handler = null;
264: if (protocol == null)
265: protocol = "";
266: if (serverName == null)
267: serverName = "";
268: if (authenticator != null)
269: authenticator.activate(properties);
270: if (channelBinding == null)
271: channelBinding = new byte[0];
272: initMechanism();
273: complete = false;
274: state = 0;
275: }
276:
277:
283: public void reset() throws SaslException
284: {
285: resetMechanism();
286: properties.clear();
287: if (authenticator != null)
288: authenticator.passivate();
289: protocol = serverName = null;
290: channelBinding = null;
291: complete = false;
292: state = -1;
293: }
294: }