1 /* PlainServer.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.plain;
40 
41 import gnu.java.security.Registry;
42 import gnu.javax.crypto.sasl.NoSuchUserException;
43 import gnu.javax.crypto.sasl.ServerMechanism;
44 
45 import java.io.IOException;
46 import java.io.UnsupportedEncodingException;
47 import java.util.Arrays;
48 import java.util.HashMap;
49 import java.util.Map;
50 import java.util.NoSuchElementException;
51 import java.util.StringTokenizer;
52 
53 import javax.security.sasl.SaslException;
54 import javax.security.sasl.SaslServer;
55 
56 /**
57  * The PLAIN SASL server-side mechanism.
58  */
59 public class PlainServer
60     extends ServerMechanism
61     implements SaslServer
62 {
PlainServer()63   public PlainServer()
64   {
65     super(Registry.SASL_PLAIN_MECHANISM);
66   }
67 
initMechanism()68   protected void initMechanism() throws SaslException
69   {
70   }
71 
resetMechanism()72   protected void resetMechanism() throws SaslException
73   {
74   }
75 
evaluateResponse(final byte[] response)76   public byte[] evaluateResponse(final byte[] response) throws SaslException
77   {
78     if (response == null)
79       return null;
80     try
81       {
82         final String nullStr = new String("\0");
83         final StringTokenizer strtok = new StringTokenizer(new String(response),
84                                                            nullStr, true);
85         authorizationID = strtok.nextToken();
86         if (! authorizationID.equals(nullStr))
87           strtok.nextToken();
88         else
89           authorizationID = null;
90         final String id = strtok.nextToken();
91         if (id.equals(nullStr))
92           throw new SaslException("No identity given");
93         if (authorizationID == null)
94           authorizationID = id;
95         if ((! authorizationID.equals(nullStr)) && (! authorizationID.equals(id)))
96           throw new SaslException("Delegation not supported");
97         strtok.nextToken();
98         final byte[] pwd;
99         try
100           {
101             pwd = strtok.nextToken().getBytes("UTF-8");
102           }
103         catch (UnsupportedEncodingException x)
104           {
105             throw new SaslException("evaluateResponse()", x);
106           }
107         if (pwd == null)
108           throw new SaslException("No password given");
109         final byte[] password;
110         try
111           {
112             password = new String(lookupPassword(id)).getBytes("UTF-8");
113           }
114         catch (UnsupportedEncodingException x)
115           {
116             throw new SaslException("evaluateResponse()", x);
117           }
118         if (! Arrays.equals(pwd, password))
119           throw new SaslException("Password incorrect");
120         this.complete = true;
121         return null;
122       }
123     catch (NoSuchElementException x)
124       {
125         throw new SaslException("evaluateResponse()", x);
126       }
127   }
128 
getNegotiatedQOP()129   protected String getNegotiatedQOP()
130   {
131     return Registry.QOP_AUTH;
132   }
133 
lookupPassword(final String userName)134   private char[] lookupPassword(final String userName) throws SaslException
135   {
136     try
137       {
138         if (! authenticator.contains(userName))
139           throw new NoSuchUserException(userName);
140         final Map userID = new HashMap();
141         userID.put(Registry.SASL_USERNAME, userName);
142         final Map credentials = authenticator.lookup(userID);
143         final String password = (String) credentials.get(Registry.SASL_PASSWORD);
144         if (password == null)
145           throw new SaslException("lookupPassword()", new InternalError());
146         return password.toCharArray();
147       }
148     catch (IOException x)
149       {
150         if (x instanceof SaslException)
151           throw (SaslException) x;
152         throw new SaslException("lookupPassword()", x);
153       }
154   }
155 }
156