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.lang.reflect.Array;
17  import java.security.MessageDigest;
18  
19  import org.eclipse.jetty.util.StringUtil;
20  import org.eclipse.jetty.util.TypeUtil;
21  import org.eclipse.jetty.util.log.Log;
22  
23  /* ------------------------------------------------------------ */
24  /**
25   * Credentials. The Credential class represents an abstract mechanism for
26   * checking authentication credentials. A credential instance either represents
27   * a secret, or some data that could only be derived from knowing the secret.
28   * <p>
29   * Often a Credential is related to a Password via a one way algorithm, so while
30   * a Password itself is a Credential, a UnixCrypt or MD5 digest of a a password
31   * is only a credential that can be checked against the password.
32   * <p>
33   * This class includes an implementation for unix Crypt an MD5 digest.
34   * 
35   * @see Password
36   * 
37   */
38  public abstract class Credential
39  {
40      /* ------------------------------------------------------------ */
41      /**
42       * Check a credential
43       * 
44       * @param credentials The credential to check against. This may either be
45       *                another Credential object, a Password object or a String
46       *                which is interpreted by this credential.
47       * @return True if the credentials indicated that the shared secret is known
48       *         to both this Credential and the passed credential.
49       */
50      public abstract boolean check(Object credentials);
51  
52      /* ------------------------------------------------------------ */
53      /**
54       * Get a credential from a String. If the credential String starts with a
55       * known Credential type (eg "CRYPT:" or "MD5:" ) then a Credential of that
56       * type is returned. Else the credential is assumed to be a Password.
57       * 
58       * @param credential String representation of the credential
59       * @return A Credential or Password instance.
60       */
61      public static Credential getCredential(String credential)
62      {
63          if (credential.startsWith(Crypt.__TYPE)) return new Crypt(credential);
64          if (credential.startsWith(MD5.__TYPE)) return new MD5(credential);
65  
66          return new Password(credential);
67      }
68  
69      /* ------------------------------------------------------------ */
70      /**
71       * Unix Crypt Credentials
72       */
73      public static class Crypt extends Credential
74      {
75          public static final String __TYPE = "CRYPT:";
76  
77          private final String _cooked;
78  
79          Crypt(String cooked)
80          {
81              _cooked = cooked.startsWith(Crypt.__TYPE) ? cooked.substring(__TYPE.length()) : cooked;
82          }
83  
84          public boolean check(Object credentials)
85          {
86              if (credentials instanceof char[])
87                  credentials=new String((char[])credentials);
88              if (!(credentials instanceof String) && !(credentials instanceof Password)) 
89                  Log.warn("Can't check " + credentials.getClass() + " against CRYPT");
90  
91              String passwd = credentials.toString();
92              return _cooked.equals(UnixCrypt.crypt(passwd, _cooked));
93          }
94  
95          public static String crypt(String user, String pw)
96          {
97              return "CRYPT:" + UnixCrypt.crypt(pw, user);
98          }
99      }
100 
101     /* ------------------------------------------------------------ */
102     /**
103      * MD5 Credentials
104      */
105     public static class MD5 extends Credential
106     {
107         public static final String __TYPE = "MD5:";
108 
109         public static final Object __md5Lock = new Object();
110 
111         private static MessageDigest __md;
112 
113         private final byte[] _digest;
114 
115         /* ------------------------------------------------------------ */
116         MD5(String digest)
117         {
118             digest = digest.startsWith(__TYPE) ? digest.substring(__TYPE.length()) : digest;
119             _digest = TypeUtil.parseBytes(digest, 16);
120         }
121 
122         /* ------------------------------------------------------------ */
123         public byte[] getDigest()
124         {
125             return _digest;
126         }
127 
128         /* ------------------------------------------------------------ */
129         public boolean check(Object credentials)
130         {
131             try
132             {
133                 byte[] digest = null;
134 
135                 if (credentials instanceof char[])
136                     credentials=new String((char[])credentials);
137                 if (credentials instanceof Password || credentials instanceof String)
138                 {
139                     synchronized (__md5Lock)
140                     {
141                         if (__md == null) __md = MessageDigest.getInstance("MD5");
142                         __md.reset();
143                         __md.update(credentials.toString().getBytes(StringUtil.__ISO_8859_1));
144                         digest = __md.digest();
145                     }
146                     if (digest == null || digest.length != _digest.length) return false;
147                     for (int i = 0; i < digest.length; i++)
148                         if (digest[i] != _digest[i]) return false;
149                     return true;
150                 }
151                 else if (credentials instanceof MD5)
152                 {
153                     MD5 md5 = (MD5) credentials;
154                     if (_digest.length != md5._digest.length) return false;
155                     for (int i = 0; i < _digest.length; i++)
156                         if (_digest[i] != md5._digest[i]) return false;
157                     return true;
158                 }
159                 else if (credentials instanceof Credential)
160                 {
161                     // Allow credential to attempt check - i.e. this'll work
162                     // for DigestAuthModule$Digest credentials
163                     return ((Credential) credentials).check(this);
164                 }
165                 else
166                 {
167                     Log.warn("Can't check " + credentials.getClass() + " against MD5");
168                     return false;
169                 }
170             }
171             catch (Exception e)
172             {
173                 Log.warn(e);
174                 return false;
175             }
176         }
177 
178         /* ------------------------------------------------------------ */
179         public static String digest(String password)
180         {
181             try
182             {
183                 byte[] digest;
184                 synchronized (__md5Lock)
185                 {
186                     if (__md == null)
187                     {
188                         try
189                         {
190                             __md = MessageDigest.getInstance("MD5");
191                         }
192                         catch (Exception e)
193                         {
194                             Log.warn(e);
195                             return null;
196                         }
197                     }
198 
199                     __md.reset();
200                     __md.update(password.getBytes(StringUtil.__ISO_8859_1));
201                     digest = __md.digest();
202                 }
203 
204                 return __TYPE + TypeUtil.toString(digest, 16);
205             }
206             catch (Exception e)
207             {
208                 Log.warn(e);
209                 return null;
210             }
211         }
212     }
213 }