Source for gnu.javax.crypto.sasl.ServerMechanism

   1: /* ServerMechanism.java --
   2:    Copyright (C) 2003, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2 of the License, or (at
   9: your option) any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: USA
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version.  */
  37: 
  38: 
  39: package gnu.javax.crypto.sasl;
  40: 
  41: import gnu.java.security.Registry;
  42: 
  43: import java.util.HashMap;
  44: import java.util.Map;
  45: 
  46: import javax.security.auth.callback.CallbackHandler;
  47: import javax.security.sasl.Sasl;
  48: import javax.security.sasl.SaslException;
  49: import javax.security.sasl.SaslServer;
  50: 
  51: /**
  52:  * A base class to facilitate implementing SASL server-side mechanisms.
  53:  */
  54: public abstract class ServerMechanism
  55:     implements SaslServer
  56: {
  57:   /** Name of this mechanism. */
  58:   protected String mechanism;
  59:   /** Name of protocol using this mechanism. */
  60:   protected String protocol;
  61:   /** Name of server to authenticate to. */
  62:   protected String serverName;
  63:   /** Properties of qualities desired for this mechanism. */
  64:   protected Map properties;
  65:   /** Callback handler to use with this mechanism instance. */
  66:   protected CallbackHandler handler;
  67:   /** Whether authentication phase is completed (true) or not (false). */
  68:   protected boolean complete = false;
  69:   /** The authorisation identity. */
  70:   protected String authorizationID;
  71:   /** Channel binding data to use with this mechanism instance. */
  72:   protected byte[] channelBinding;
  73:   /** The state of the authentication automaton. -1 means uninitialised. */
  74:   protected int state = -1;
  75:   /** The provider for authentication information. */
  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:   /**
 237:    * Initialises the mechanism with designated attributes. Permissible names and
 238:    * values are mechanism specific.
 239:    *
 240:    * @param attributes a set of name-value pairs that describes the desired
 241:    *          future behaviour of this instance.
 242:    * @throws IllegalMechanismStateException if the instance is already
 243:    *           initialised.
 244:    * @throws SaslException if an exception occurs during the process.
 245:    */
 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:   /**
 278:    * Resets the mechanism instance for re-initialisation and use with other
 279:    * characteristics.
 280:    *
 281:    * @throws SaslException if an exception occurs during the process.
 282:    */
 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: }