View Javadoc

1   // ========================================================================
2   // Copyright (c) 1998-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.http.security;
15  
16  import java.io.IOException;
17  import java.util.Arrays;
18  
19  import org.eclipse.jetty.util.log.Log;
20  
21  /* ------------------------------------------------------------ */
22  /**
23   * Password utility class.
24   * 
25   * This utility class gets a password or pass phrase either by:
26   * 
27   * <PRE>
28   *  + Password is set as a system property.
29   *  + The password is prompted for and read from standard input
30   *  + A program is run to get the password.
31   * </pre>
32   * 
33   * Passwords that begin with OBF: are de obfuscated. Passwords can be obfuscated
34   * by run org.eclipse.util.Password as a main class. Obfuscated password are
35   * required if a system needs to recover the full password (eg. so that it may
36   * be passed to another system). They are not secure, but prevent casual
37   * observation.
38   * <p>
39   * Passwords that begin with CRYPT: are oneway encrypted with UnixCrypt. The
40   * real password cannot be retrieved, but comparisons can be made to other
41   * passwords. A Crypt can be generated by running org.eclipse.util.UnixCrypt as
42   * a main class, passing password and then the username. Checksum passwords are
43   * a secure(ish) way to store passwords that only need to be checked rather than
44   * recovered. Note that it is not strong security - specially if simple
45   * passwords are used.
46   * 
47   * 
48   */
49  public class Password extends Credential
50  {
51      public static final String __OBFUSCATE = "OBF:";
52  
53      private String _pw;
54  
55      /* ------------------------------------------------------------ */
56      /**
57       * Constructor.
58       * 
59       * @param password The String password.
60       */
61      public Password(String password)
62      {
63          _pw = password;
64  
65          // expand password
66          while (_pw != null && _pw.startsWith(__OBFUSCATE))
67              _pw = deobfuscate(_pw);
68      }
69  
70      /* ------------------------------------------------------------ */
71      public String toString()
72      {
73          return _pw;
74      }
75  
76      /* ------------------------------------------------------------ */
77      public String toStarString()
78      {
79          return "*****************************************************".substring(0, _pw.length());
80      }
81  
82      /* ------------------------------------------------------------ */
83      public boolean check(Object credentials)
84      {
85          if (this == credentials) return true;
86  
87          if (credentials instanceof Password) return credentials.equals(_pw);
88  
89          if (credentials instanceof String) return credentials.equals(_pw);
90  
91          if (credentials instanceof char[]) return Arrays.equals(_pw.toCharArray(), (char[]) credentials);
92  
93          if (credentials instanceof Credential) return ((Credential) credentials).check(_pw);
94  
95          return false;
96      }
97  
98      /* ------------------------------------------------------------ */
99      public boolean equals(Object o)
100     {
101         if (this == o) 
102             return true;
103 
104         if (null == o) 
105             return false;
106 
107         if (o instanceof Password)
108         {
109             Password p = (Password) o;
110             //noinspection StringEquality
111             return p._pw == _pw || (null != _pw && _pw.equals(p._pw));
112         }
113 
114         if (o instanceof String) 
115             return o.equals(_pw);
116 
117         return false;
118     }
119 
120     /* ------------------------------------------------------------ */
121     public int hashCode()
122     {
123         return null == _pw ? super.hashCode() : _pw.hashCode();
124     }
125 
126     /* ------------------------------------------------------------ */
127     public static String obfuscate(String s)
128     {
129         StringBuilder buf = new StringBuilder();
130         byte[] b = s.getBytes();
131 
132         buf.append(__OBFUSCATE);
133         for (int i = 0; i < b.length; i++)
134         {
135             byte b1 = b[i];
136             byte b2 = b[s.length() - (i + 1)];
137             int i1 = 127 + b1 + b2;
138             int i2 = 127 + b1 - b2;
139             int i0 = i1 * 256 + i2;
140             String x = Integer.toString(i0, 36);
141 
142             switch (x.length())
143             {
144                 case 1:
145                     buf.append('0');
146                 case 2:
147                     buf.append('0');
148                 case 3:
149                     buf.append('0');
150                 default:
151                     buf.append(x);
152             }
153         }
154         return buf.toString();
155 
156     }
157 
158     /* ------------------------------------------------------------ */
159     public static String deobfuscate(String s)
160     {
161         if (s.startsWith(__OBFUSCATE)) s = s.substring(4);
162 
163         byte[] b = new byte[s.length() / 2];
164         int l = 0;
165         for (int i = 0; i < s.length(); i += 4)
166         {
167             String x = s.substring(i, i + 4);
168             int i0 = Integer.parseInt(x, 36);
169             int i1 = (i0 / 256);
170             int i2 = (i0 % 256);
171             b[l++] = (byte) ((i1 + i2 - 254) / 2);
172         }
173 
174         return new String(b, 0, l);
175     }
176 
177     /* ------------------------------------------------------------ */
178     /**
179      * Get a password. A password is obtained by trying
180      * <UL>
181      * <LI>Calling <Code>System.getProperty(realm,dft)</Code>
182      * <LI>Prompting for a password
183      * <LI>Using promptDft if nothing was entered.
184      * </UL>
185      * 
186      * @param realm The realm name for the password, used as a SystemProperty
187      *                name.
188      * @param dft The default password.
189      * @param promptDft The default to use if prompting for the password.
190      * @return Password
191      */
192     public static Password getPassword(String realm, String dft, String promptDft)
193     {
194         String passwd = System.getProperty(realm, dft);
195         if (passwd == null || passwd.length() == 0)
196         {
197             try
198             {
199                 System.out.print(realm + ((promptDft != null && promptDft.length() > 0) ? " [dft]" : "") + " : ");
200                 System.out.flush();
201                 byte[] buf = new byte[512];
202                 int len = System.in.read(buf);
203                 if (len > 0) passwd = new String(buf, 0, len).trim();
204             }
205             catch (IOException e)
206             {
207                 Log.warn(Log.EXCEPTION, e);
208             }
209             if (passwd == null || passwd.length() == 0) passwd = promptDft;
210         }
211         return new Password(passwd);
212     }
213 
214     /* ------------------------------------------------------------ */
215     /**
216      * @param arg
217      */
218     public static void main(String[] arg)
219     {
220         if (arg.length != 1 && arg.length != 2)
221         {
222             System.err.println("Usage - java org.eclipse.jetty.security.Password [<user>] <password>");
223             System.err.println("If the password is ?, the user will be prompted for the password");
224             System.exit(1);
225         }
226         String p = arg[arg.length == 1 ? 0 : 1];
227         Password pw = new Password(p);
228         System.err.println(pw.toString());
229         System.err.println(obfuscate(pw.toString()));
230         System.err.println(Credential.MD5.digest(p));
231         if (arg.length == 2) System.err.println(Credential.Crypt.crypt(arg[0], pw.toString()));
232     }
233 }