[kaffe] CVS kaffe (robilad): Resynced with GNU inetlib: SASL fallbacks and Base64 fixes

Kaffe CVS cvs-commits at kaffe.org
Tue Sep 21 08:32:30 PDT 2004


PatchSet 5199 
Date: 2004/09/21 13:43:44
Author: robilad
Branch: HEAD
Tag: (none) 
Log:
Resynced with GNU inetlib: SASL fallbacks and Base64 fixes

2004-09-21  Dalibor Topic  <robilad at kaffe.org>

        * libraries/javalib/gnu/inet/imap/IMAPConnection.java,
        libraries/javalib/gnu/inet/pop3/POP3Connection.java,
        libraries/javalib/gnu/inet/smtp/SMTPConnection.java,
        libraries/javalib/gnu/inet/util/BASE64.java,
        libraries/javalib/gnu/inet/util/SaslCallbackHandler.java,
        libraries/javalib/gnu/inet/util/SaslCramMD5.java,
        libraries/javalib/gnu/inet/util/SaslLogin.java,
        libraries/javalib/gnu/inet/util/SaslPlain.java:
        Resynced with GNU Inetlib.

        2004-09-20  Chris Burdess  <dog at bluezoo.org>

        * BASE64.java: Padding characters at end of encoded data.

        2004-09-12  Chris Burdess  <dog at bluezoo.org>

        * IMAPConnection.java,POP3Connection.java,SMTPConnection.java,util:
            Fallback SASL clients for LOGIN, PLAIN, and CRAM-MD5.

Members: 
	ChangeLog:1.2754->1.2755 
	libraries/javalib/gnu/inet/imap/IMAPConnection.java:1.3->1.4 
	libraries/javalib/gnu/inet/pop3/POP3Connection.java:1.2->1.3 
	libraries/javalib/gnu/inet/smtp/SMTPConnection.java:1.3->1.4 
	libraries/javalib/gnu/inet/util/BASE64.java:1.3->1.4 
	libraries/javalib/gnu/inet/util/SaslCallbackHandler.java:1.3->1.4 
	libraries/javalib/gnu/inet/util/SaslCramMD5.java:INITIAL->1.1 
	libraries/javalib/gnu/inet/util/SaslLogin.java:INITIAL->1.1 
	libraries/javalib/gnu/inet/util/SaslPlain.java:INITIAL->1.1 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.2754 kaffe/ChangeLog:1.2755
--- kaffe/ChangeLog:1.2754	Tue Sep 21 13:27:41 2004
+++ kaffe/ChangeLog	Tue Sep 21 13:43:44 2004
@@ -1,5 +1,26 @@
 2004-09-21  Dalibor Topic  <robilad at kaffe.org>
 
+	* libraries/javalib/gnu/inet/imap/IMAPConnection.java,
+	libraries/javalib/gnu/inet/pop3/POP3Connection.java,
+	libraries/javalib/gnu/inet/smtp/SMTPConnection.java,
+	libraries/javalib/gnu/inet/util/BASE64.java,
+	libraries/javalib/gnu/inet/util/SaslCallbackHandler.java,
+	libraries/javalib/gnu/inet/util/SaslCramMD5.java,
+	libraries/javalib/gnu/inet/util/SaslLogin.java,
+	libraries/javalib/gnu/inet/util/SaslPlain.java:
+	Resynced with GNU Inetlib.
+
+	2004-09-20  Chris Burdess  <dog at bluezoo.org>
+
+        * BASE64.java: Padding characters at end of encoded data.
+
+	2004-09-12  Chris Burdess  <dog at bluezoo.org>
+
+        * IMAPConnection.java,POP3Connection.java,SMTPConnection.java,util:
+            Fallback SASL clients for LOGIN, PLAIN, and CRAM-MD5.
+
+2004-09-21  Dalibor Topic  <robilad at kaffe.org>
+
 	* libraries/javalib/java/applet/Applet.java,
 	libraries/javalib/java/applet/AppletContext.java,
 	libraries/javalib/java/applet/AppletStub.java,
Index: kaffe/libraries/javalib/gnu/inet/imap/IMAPConnection.java
diff -u kaffe/libraries/javalib/gnu/inet/imap/IMAPConnection.java:1.3 kaffe/libraries/javalib/gnu/inet/imap/IMAPConnection.java:1.4
--- kaffe/libraries/javalib/gnu/inet/imap/IMAPConnection.java:1.3	Mon Sep 13 11:00:25 2004
+++ kaffe/libraries/javalib/gnu/inet/imap/IMAPConnection.java	Tue Sep 21 13:43:47 2004
@@ -1,5 +1,5 @@
 /*
- * $Id: IMAPConnection.java,v 1.3 2004/09/13 11:00:25 dalibor Exp $
+ * $Id: IMAPConnection.java,v 1.4 2004/09/21 13:43:47 robilad Exp $
  * Copyright (C) 2003 The Free Software Foundation
  * 
  * This file is part of GNU inetlib, a library.
@@ -63,14 +63,17 @@
 import gnu.inet.util.EmptyX509TrustManager;
 import gnu.inet.util.Logger;
 import gnu.inet.util.SaslCallbackHandler;
+import gnu.inet.util.SaslCramMD5;
 import gnu.inet.util.SaslInputStream;
+import gnu.inet.util.SaslLogin;
 import gnu.inet.util.SaslOutputStream;
+import gnu.inet.util.SaslPlain;
 
 /**
  * The protocol class implementing IMAP4rev1.
  *
  * @author <a href='mailto:dog at gnu.org'>Chris Burdess</a>
- * @version $Revision: 1.3 $ $Date: 2004/09/13 11:00:25 $
+ * @version $Revision: 1.4 $ $Date: 2004/09/21 13:43:47 $
  */
 public class IMAPConnection
 implements IMAPConstants
@@ -656,28 +659,38 @@
         Properties p = new Properties ();
         p.put ("gnu.crypto.sasl.username", username);
         p.put ("gnu.crypto.sasl.password", password);
-        SaslClient sasl = Sasl.createSaslClient (m, null, "smtp",
+        SaslClient sasl = Sasl.createSaslClient (m, null, "imap",
                                                  socket.getInetAddress ().
                                                  getHostName (), p, ch);
         if (sasl == null)
           {
-            if (debug)
+            // Fall back to home-grown SASL clients
+            if ("LOGIN".equalsIgnoreCase (mechanism))
               {
-                Logger logger = Logger.getInstance ();
-                logger.log("imap", mechanism + " not available");
+                sasl = new SaslLogin (username, password);
+              }
+            else if ("PLAIN".equalsIgnoreCase (mechanism))
+              {
+                sasl = new SaslPlain (username, password);
+              }
+            else if ("CRAM-MD5".equalsIgnoreCase (mechanism))
+              {
+                sasl = new SaslCramMD5 (username, password);
+              }
+            else
+              {
+                if (debug)
+                  {
+                    Logger logger = Logger.getInstance ();
+                    logger.log("imap", mechanism + " not available");
+                  }
+                return false;
               }
-            return false;
           }
         
         StringBuffer cmd = new StringBuffer (AUTHENTICATE);
         cmd.append (' ');
         cmd.append (mechanism);
-        if (sasl.hasInitialResponse ())
-          {
-            cmd.append (' ');
-            byte[] init = sasl.evaluateChallenge (new byte[0]);
-            cmd.append (new String (init, US_ASCII));
-          }
         String tag = newTag ();
         sendCommand (tag, cmd.toString ());
         while (true)
@@ -725,6 +738,11 @@
                     out.write (r1);
                     out.writeln ();
                     out.flush ();
+                    if (debug)
+                      {
+                        Logger logger = Logger.getInstance ();
+                        logger.log ("imap", "> " + new String (r1, US_ASCII));
+                      }
                   }
                 catch (SaslException e)
                   {
@@ -732,6 +750,11 @@
                     out.write (0x2a);
                     out.writeln ();
                     out.flush ();
+                    if (debug)
+                      {
+                        Logger logger = Logger.getInstance ();
+                        logger.log ("imap", "> *");
+                      }
                   }
               }
             else
Index: kaffe/libraries/javalib/gnu/inet/pop3/POP3Connection.java
diff -u kaffe/libraries/javalib/gnu/inet/pop3/POP3Connection.java:1.2 kaffe/libraries/javalib/gnu/inet/pop3/POP3Connection.java:1.3
--- kaffe/libraries/javalib/gnu/inet/pop3/POP3Connection.java:1.2	Mon Aug  9 14:38:09 2004
+++ kaffe/libraries/javalib/gnu/inet/pop3/POP3Connection.java	Tue Sep 21 13:43:47 2004
@@ -1,5 +1,5 @@
 /*
- * $Id: POP3Connection.java,v 1.2 2004/08/09 14:38:09 dalibor Exp $
+ * $Id: POP3Connection.java,v 1.3 2004/09/21 13:43:47 robilad Exp $
  * Copyright (C) 2003 The Free Software Foundation
  * 
  * This file is part of GNU inetlib, a library.
@@ -64,8 +64,11 @@
 import gnu.inet.util.Logger;
 import gnu.inet.util.MessageInputStream;
 import gnu.inet.util.SaslCallbackHandler;
+import gnu.inet.util.SaslCramMD5;
 import gnu.inet.util.SaslInputStream;
+import gnu.inet.util.SaslLogin;
 import gnu.inet.util.SaslOutputStream;
+import gnu.inet.util.SaslPlain;
 
 /**
  * A POP3 client connection.
@@ -77,7 +80,7 @@
  * over POP3 documented in RFC 2595 and the AUTH command in RFC 1734.
  *
  * @author <a href='mailto:dog at gnu.org'>Chris Burdess</a>
- * @version $Revision: 1.2 $ $Date: 2004/08/09 14:38:09 $
+ * @version $Revision: 1.3 $ $Date: 2004/09/21 13:43:47 $
  */
 public class POP3Connection
 {
@@ -236,23 +239,33 @@
         p.put ("gnu.crypto.sasl.username", username);
         p.put ("gnu.crypto.sasl.password", password);
         SaslClient sasl =
-          Sasl.createSaslClient (m, null, "smtp",
+          Sasl.createSaslClient (m, null, "pop3",
                                  socket.getInetAddress ().getHostName (),
                                  p, ch);
         if (sasl == null)
           {
-            return false;
+            // Fall back to home-grown SASL clients
+            if ("LOGIN".equalsIgnoreCase (mechanism))
+              {
+                sasl = new SaslLogin (username, password);
+              }
+            else if ("PLAIN".equalsIgnoreCase (mechanism))
+              {
+                sasl = new SaslPlain (username, password);
+              }
+            else if ("CRAM-MD5".equalsIgnoreCase (mechanism))
+              {
+                sasl = new SaslCramMD5 (username, password);
+              }
+            else
+              {
+                return false;
+              }
           }
         
         StringBuffer cmd = new StringBuffer (AUTH);
         cmd.append (' ');
         cmd.append (mechanism);
-        if (sasl.hasInitialResponse ())
-          {
-            cmd.append (' ');
-            byte[] init = sasl.evaluateChallenge (new byte[0]);
-            cmd.append (new String (init, "US-ASCII"));
-          }
         send (cmd.toString ());
         while (true)
           {
@@ -284,6 +297,12 @@
                     out.write (r1);
                     out.write (0x0d);
                     out.flush ();
+                    if (debug)
+                      {
+                        Logger logger = Logger.getInstance ();
+                        logger.log ("pop3", "> " +
+                                    new String (r1, "US-ASCII"));
+                      }
                   }
                 catch (SaslException e)
                   {
@@ -291,6 +310,11 @@
                     out.write (0x2a);
                     out.write (0x0d);
                     out.flush ();
+                    if (debug)
+                      {
+                        Logger logger = Logger.getInstance ();
+                        logger.log ("pop3", "> *");
+                      }
                   }
               default:
                 return false;
Index: kaffe/libraries/javalib/gnu/inet/smtp/SMTPConnection.java
diff -u kaffe/libraries/javalib/gnu/inet/smtp/SMTPConnection.java:1.3 kaffe/libraries/javalib/gnu/inet/smtp/SMTPConnection.java:1.4
--- kaffe/libraries/javalib/gnu/inet/smtp/SMTPConnection.java:1.3	Mon Sep 13 11:00:28 2004
+++ kaffe/libraries/javalib/gnu/inet/smtp/SMTPConnection.java	Tue Sep 21 13:43:47 2004
@@ -1,5 +1,5 @@
 /*
- * $Id: SMTPConnection.java,v 1.3 2004/09/13 11:00:28 dalibor Exp $
+ * $Id: SMTPConnection.java,v 1.4 2004/09/21 13:43:47 robilad Exp $
  * Copyright (C) 2003 Chris Burdess <dog at gnu.org>
  * 
  * This file is part of GNU inetlib, a library.
@@ -59,15 +59,18 @@
 import gnu.inet.util.Logger;
 import gnu.inet.util.MessageOutputStream;
 import gnu.inet.util.SaslCallbackHandler;
+import gnu.inet.util.SaslCramMD5;
 import gnu.inet.util.SaslInputStream;
+import gnu.inet.util.SaslLogin;
 import gnu.inet.util.SaslOutputStream;
+import gnu.inet.util.SaslPlain;
 
 /**
  * An SMTP client.
  * This implements RFC 2821.
  *
  * @author <a href="mailto:dog at gnu.org">Chris Burdess</a>
- * @version $Revision: 1.3 $ $Date: 2004/09/13 11:00:28 $
+ * @version $Revision: 1.4 $ $Date: 2004/09/21 13:43:47 $
  */
 public class SMTPConnection
 {
@@ -607,7 +610,23 @@
                                  p, ch);
         if (sasl == null)
           {
-            return false;
+            // Fall back to home-grown SASL clients
+            if ("LOGIN".equalsIgnoreCase (mechanism))
+              {
+                sasl = new SaslLogin (username, password);
+              }
+            else if ("PLAIN".equalsIgnoreCase (mechanism))
+              {
+                sasl = new SaslPlain (username, password);
+              }
+            else if ("CRAM-MD5".equalsIgnoreCase (mechanism))
+              {
+                sasl = new SaslCramMD5 (username, password);
+              }
+            else
+              {
+                return false;
+              }
           }
         
         StringBuffer cmd = new StringBuffer (AUTH);
@@ -617,7 +636,14 @@
           {
             cmd.append (' ');
             byte[] init = sasl.evaluateChallenge (new byte[0]);
-            cmd.append (new String (init, "US-ASCII"));
+            if (init.length == 0)
+              {
+                cmd.append ('=');
+              }
+            else
+              {
+                cmd.append (new String (BASE64.encode (init), "US-ASCII"));
+              }
           }
         send (cmd.toString ());
         while (true)
@@ -634,6 +660,12 @@
                     out.write (r1);
                     out.write (0x0d);
                     out.flush ();
+                    if (debug)
+                      {
+                        Logger logger = Logger.getInstance ();
+                        logger.log ("smtp", "> " +
+                                    new String (r1, "US-ASCII"));
+                      }
                   }
                 catch (SaslException e)
                   {
@@ -641,6 +673,11 @@
                     out.write (0x2a);
                     out.write (0x0d);
                     out.flush ();
+                    if (debug)
+                      {
+                        Logger logger = Logger.getInstance ();
+                        logger.log ("smtp", "> *");
+                      }
                   }
                 break;
               case 235:
Index: kaffe/libraries/javalib/gnu/inet/util/BASE64.java
diff -u kaffe/libraries/javalib/gnu/inet/util/BASE64.java:1.3 kaffe/libraries/javalib/gnu/inet/util/BASE64.java:1.4
--- kaffe/libraries/javalib/gnu/inet/util/BASE64.java:1.3	Mon Sep 13 11:00:28 2004
+++ kaffe/libraries/javalib/gnu/inet/util/BASE64.java	Tue Sep 21 13:43:48 2004
@@ -1,5 +1,5 @@
 /*
- * $Id: BASE64.java,v 1.3 2004/09/13 11:00:28 dalibor Exp $
+ * $Id: BASE64.java,v 1.4 2004/09/21 13:43:48 robilad Exp $
  * Copyright (C) 2003 The Free Software Foundation
  * 
  * This file is part of GNU inetlib, a library.
@@ -31,7 +31,7 @@
  * Encodes and decodes text according to the BASE64 encoding.
  *
  * @author <a href="mailto:dog at gnu.org">Chris Burdess</a>
- * @version $Revision: 1.3 $ $Date: 2004/09/13 11:00:28 $
+ * @version $Revision: 1.4 $ $Date: 2004/09/21 13:43:48 $
  */
 public final class BASE64
 {
@@ -48,21 +48,21 @@
 
   private static final byte[] dst;
   static
-    {
-      dst = new byte[0x100];
-      for (int i = 0x0; i < 0xff; i++)
-        {
-          dst[i] = -1;
-        }
-      for (int i = 0; i < src.length; i++)
-        {
-          dst[src[i]] = (byte) i;
-        }
-    }
+  {
+    dst = new byte[0x100];
+    for (int i = 0x0; i < 0xff; i++)
+      {
+        dst[i] = -1;
+      }
+    for (int i = 0; i < src.length; i++)
+      {
+        dst[src[i]] = (byte) i;
+      }
+  }
 
   private BASE64 ()
-    {
-    }
+  {
+  }
 
   /**
    * Encode the specified byte array using the BASE64 algorithm.
@@ -70,39 +70,43 @@
    * @param bs the source byte array
    */
   public static byte[] encode (byte[] bs)
-    {
-      int si = 0, ti = 0;         // source/target array indices
-      byte[] bt = new byte[((bs.length + 2) * 4) / 3];     // target byte array
-      for (; si < bs.length; si += 3)
-        {
-          int buflen = bs.length - si;
-          if (buflen == 1)
-            {
-              byte b = bs[si];
-              int i = 0;
-              boolean flag = false;
-              bt[ti++] = src[b >>> 2 & 0x3f];
-              bt[ti++] = src[(b << 4 & 0x30) + (i >>> 4 & 0xf)];
-            }
-          else if (buflen == 2)
-            {
-              byte b1 = bs[si], b2 = bs[si + 1];
-              int i = 0;
-              bt[ti++] = src[b1 >>> 2 & 0x3f];
-              bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
-              bt[ti++] = src[(b2 << 2 & 0x3c) + (i >>> 6 & 0x3)];
-            }
-          else
-            {
-              byte b1 = bs[si], b2 = bs[si + 1], b3 = bs[si + 2];
-              bt[ti++] = src[b1 >>> 2 & 0x3f];
-              bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
-              bt[ti++] = src[(b2 << 2 & 0x3c) + (b3 >>> 6 & 0x3)];
-              bt[ti++] = src[b3 & 0x3f];
-            }
-        }
-      return bt;
-    }
+  {
+    int si = 0, ti = 0;         // source/target array indices
+    byte[] bt = new byte[((bs.length + 2) * 4) / 3];     // target byte array
+    for (; si < bs.length; si += 3)
+      {
+        int buflen = bs.length - si;
+        if (buflen == 1)
+          {
+            byte b = bs[si];
+            int i = 0;
+            boolean flag = false;
+            bt[ti++] = src[b >>> 2 & 0x3f];
+            bt[ti++] = src[(b << 4 & 0x30) + (i >>> 4 & 0xf)];
+          }
+        else if (buflen == 2)
+          {
+            byte b1 = bs[si], b2 = bs[si + 1];
+            int i = 0;
+            bt[ti++] = src[b1 >>> 2 & 0x3f];
+            bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
+            bt[ti++] = src[(b2 << 2 & 0x3c) + (i >>> 6 & 0x3)];
+          }
+        else
+          {
+            byte b1 = bs[si], b2 = bs[si + 1], b3 = bs[si + 2];
+            bt[ti++] = src[b1 >>> 2 & 0x3f];
+            bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
+            bt[ti++] = src[(b2 << 2 & 0x3c) + (b3 >>> 6 & 0x3)];
+            bt[ti++] = src[b3 & 0x3f];
+          }
+      }
+    while (ti < bt.length)
+      {
+        bt[ti++] = 0x3d;
+      }
+    return bt;
+  }
 
   /**
    * Decode the specified byte array using the BASE64 algorithm.
@@ -110,64 +114,64 @@
    * @param bs the source byte array
    */
   public static byte[] decode(byte[] bs)
-    {
-      int srclen = bs.length;
-      while (srclen > 0 && bs[srclen - 1] == 0x3d)
-        {
-          srclen--; /* strip padding character */
-        }
-      byte[] buffer = new byte[srclen];
-      int buflen = 0;
-      int si = 0;
-      int len = srclen - si;
-      while (len > 0)
-        {
-          byte b0 = dst[bs[si++] & 0xff];
-          byte b2 = dst[bs[si++] & 0xff];
-          buffer[buflen++] = (byte) (b0 << 2 & 0xfc | b2 >>> 4 & 0x3);
-          if (len > 2)
-            {
-              b0 = b2;
-              b2 = dst[bs[si++] & 0xff];
-              buffer[buflen++] = (byte) (b0 << 4 & 0xf0 | b2 >>> 2 & 0xf);
-              if (len > 3)
-                {
-                  b0 = b2;
-                  b2 = dst[bs[si++] & 0xff];
-                  buffer[buflen++] = (byte) (b0 << 6 & 0xc0 | b2 & 0x3f);
-                }
-            }
-          len = srclen - si;
-        }
-      byte[] bt = new byte[buflen];
-      System.arraycopy (buffer, 0, bt, 0, buflen);
-      return bt;
-    }
-
+  {
+    int srclen = bs.length;
+    while (srclen > 0 && bs[srclen - 1] == 0x3d)
+      {
+        srclen--; /* strip padding character */
+      }
+    byte[] buffer = new byte[srclen];
+    int buflen = 0;
+    int si = 0;
+    int len = srclen - si;
+    while (len > 0)
+      {
+        byte b0 = dst[bs[si++] & 0xff];
+        byte b2 = dst[bs[si++] & 0xff];
+        buffer[buflen++] = (byte) (b0 << 2 & 0xfc | b2 >>> 4 & 0x3);
+        if (len > 2)
+          {
+            b0 = b2;
+            b2 = dst[bs[si++] & 0xff];
+            buffer[buflen++] = (byte) (b0 << 4 & 0xf0 | b2 >>> 2 & 0xf);
+            if (len > 3)
+              {
+                b0 = b2;
+                b2 = dst[bs[si++] & 0xff];
+                buffer[buflen++] = (byte) (b0 << 6 & 0xc0 | b2 & 0x3f);
+              }
+          }
+        len = srclen - si;
+      }
+    byte[] bt = new byte[buflen];
+    System.arraycopy (buffer, 0, bt, 0, buflen);
+    return bt;
+  }
+  
   public static void main (String[] args)
-    {
-      boolean decode = false;
-      for (int i = 0; i < args.length; i++)
-        {
-          if (args[i].equals ("-d"))
-            {
-              decode = true;
-            }
-          else
-            {
-              try
-                {
-                  byte[] in = args[i].getBytes ("US-ASCII");
-                  byte[] out = decode ? decode (in) : encode (in);
-                  System.out.println (args[i] + " = " +
-                                      new String (out, "US-ASCII"));
-                }
-              catch (java.io.UnsupportedEncodingException e)
-                {
-                  e.printStackTrace (System.err);
-                }
-            }
-        }
-    }
-
+  {
+    boolean decode = false;
+    for (int i = 0; i < args.length; i++)
+      {
+        if (args[i].equals ("-d"))
+          {
+            decode = true;
+          }
+        else
+          {
+            try
+              {
+                byte[] in = args[i].getBytes ("US-ASCII");
+                byte[] out = decode ? decode (in) : encode (in);
+                System.out.println (args[i] + " = " +
+                                    new String (out, "US-ASCII"));
+              }
+            catch (java.io.UnsupportedEncodingException e)
+              {
+                e.printStackTrace (System.err);
+              }
+          }
+      }
+  }
+  
 }
Index: kaffe/libraries/javalib/gnu/inet/util/SaslCallbackHandler.java
diff -u kaffe/libraries/javalib/gnu/inet/util/SaslCallbackHandler.java:1.3 kaffe/libraries/javalib/gnu/inet/util/SaslCallbackHandler.java:1.4
--- kaffe/libraries/javalib/gnu/inet/util/SaslCallbackHandler.java:1.3	Mon Sep 13 11:00:28 2004
+++ kaffe/libraries/javalib/gnu/inet/util/SaslCallbackHandler.java	Tue Sep 21 13:43:48 2004
@@ -1,5 +1,5 @@
 /*
- * $Id: SaslCallbackHandler.java,v 1.3 2004/09/13 11:00:28 dalibor Exp $
+ * $Id: SaslCallbackHandler.java,v 1.4 2004/09/21 13:43:48 robilad Exp $
  * Copyright (C) 2002 The Free Software Foundation
  * 
  * This file is part of GNU inetlib, a library.
@@ -39,7 +39,7 @@
  * A callback handler that can manage username and password callbacks.
  *
  * @author <a href="mailto:dog at gnu.org">Chris Burdess</a>
- * @version $Revision: 1.3 $ $Date: 2004/09/13 11:00:28 $
+ * @version $Revision: 1.4 $ $Date: 2004/09/21 13:43:48 $
  */
 public final class SaslCallbackHandler implements CallbackHandler
 {
@@ -68,7 +68,7 @@
   /**
    * Handle callbacks.
    */
-  public void handle (Callback[]callbacks)
+  public void handle (Callback[] callbacks)
     throws IOException, UnsupportedCallbackException
     {
       for (int i = 0; i < callbacks.length; i++)
===================================================================
Checking out kaffe/libraries/javalib/gnu/inet/util/SaslCramMD5.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/gnu/inet/util/SaslCramMD5.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/gnu/inet/util/SaslCramMD5.java	Tue Sep 21 15:32:29 2004
@@ -0,0 +1,167 @@
+/*
+ * SaslCramMD5.java
+ * Copyright (C) 2004 The Free Software Foundation
+ * 
+ * This file is part of GNU inetlib, a library.
+ * 
+ * GNU inetlib is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * GNU inetlib is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * As a special exception, if you link this library with other files to
+ * produce an executable, this library does not by itself cause the
+ * resulting executable to be covered by the GNU General Public License.
+ * This exception does not however invalidate any other reasons why the
+ * executable file might be covered by the GNU General Public License.
+ */
+
+package gnu.inet.util;
+
+import java.io.UnsupportedEncodingException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import javax.security.sasl.SaslClient;
+import javax.security.sasl.SaslException;
+
+/**
+ * SASL mechanism for CRAM-MD5.
+ *
+ * @author <a href='mailto:dog at gnu.org'>Chris Burdess</a>
+ */
+public class SaslCramMD5
+implements SaslClient
+{
+
+  private String username;
+  private String password;
+  private boolean complete;
+
+  public SaslCramMD5 (String username, String password)
+  {
+    this.username = username;
+    this.password = password;
+  }
+
+  public String getMechanismName ()
+  {
+    return "CRAM-MD5";
+  }
+
+  public boolean hasInitialResponse ()
+  {
+    return false;
+  }
+
+  public byte[] evaluateChallenge (byte[] challenge)
+    throws SaslException
+  {
+    try
+      {
+        byte[] s = password.getBytes ("US-ASCII");
+        byte[] digest = hmac_md5 (s, challenge);
+        byte[] r0 = username.getBytes ("US-ASCII");
+        byte[] r1 = new byte[r0.length + digest.length + 1];
+        System.arraycopy(r0, 0, r1, 0, r0.length); // add username
+        r1[r0.length] = 0x20; // SPACE
+        System.arraycopy(digest, 0, r1, r0.length+1, digest.length);
+        complete = true;
+        return r1;
+      }
+    catch (UnsupportedEncodingException e)
+      {
+        String msg = "Username or password contains non-ASCII characters";
+        throw new SaslException (msg, e);
+      }
+    catch (NoSuchAlgorithmException e)
+      {
+        String msg = "MD5 algorithm not available";
+        throw new SaslException (msg, e);
+      }
+  }
+
+  public boolean isComplete ()
+  {
+    return complete;
+  }
+
+  public byte[] unwrap (byte[] incoming, int off, int len)
+    throws SaslException
+  {
+    byte[] ret = new byte[len - off];
+    System.arraycopy (incoming, off, ret, 0, len);
+    return ret;
+  }
+
+  public byte[] wrap (byte[] outgoing, int off, int len)
+    throws SaslException
+  {
+    byte[] ret = new byte[len - off];
+    System.arraycopy (outgoing, off, ret, 0, len);
+    return ret;
+  }
+
+  public Object getNegotiatedProperty (String name)
+  {
+    return null;
+  }
+
+  public void dispose ()
+  {
+  }
+
+  /**
+   * Computes a CRAM digest using the HMAC algorithm:
+   * <pre>
+   * MD5(key XOR opad, MD5(key XOR ipad, text))
+   * </pre>.
+   * <code>secret</code> is null-padded to a length of 64 bytes.
+   * If the shared secret is longer than 64 bytes, the MD5 digest of the
+   * shared secret is used as a 16 byte input to the keyed MD5 calculation.
+   * See RFC 2104 for details.
+   */
+  private static byte[] hmac_md5(byte[] key, byte[] text)
+    throws NoSuchAlgorithmException
+  {
+    byte[] k_ipad = new byte[64];
+    byte[] k_opad = new byte[64];
+    byte[] digest;
+    MessageDigest md5 = MessageDigest.getInstance("MD5");
+    // if key is longer than 64 bytes reset it to key=MD5(key)
+    if (key.length>64)
+    {
+      md5.update(key);
+      key = md5.digest();
+    }
+    // start out by storing key in pads
+    System.arraycopy(key, 0, k_ipad, 0, key.length);
+    System.arraycopy(key, 0, k_opad, 0, key.length);
+    // XOR key with ipad and opad values
+    for (int i=0; i<64; i++)
+    {
+      k_ipad[i] ^= 0x36;
+      k_opad[i] ^= 0x5c;
+    }
+    // perform inner MD5
+    md5.reset();
+    md5.update(k_ipad);
+    md5.update(text);
+    digest = md5.digest();
+    // perform outer MD5
+    md5.reset();
+    md5.update(k_opad);
+    md5.update(digest);
+    digest = md5.digest();
+    return digest;
+  }
+  
+}
===================================================================
Checking out kaffe/libraries/javalib/gnu/inet/util/SaslLogin.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/gnu/inet/util/SaslLogin.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/gnu/inet/util/SaslLogin.java	Tue Sep 21 15:32:30 2004
@@ -0,0 +1,122 @@
+/*
+ * SaslLogin.java
+ * Copyright (C) 2004 The Free Software Foundation
+ * 
+ * This file is part of GNU inetlib, a library.
+ * 
+ * GNU inetlib is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * GNU inetlib is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * As a special exception, if you link this library with other files to
+ * produce an executable, this library does not by itself cause the
+ * resulting executable to be covered by the GNU General Public License.
+ * This exception does not however invalidate any other reasons why the
+ * executable file might be covered by the GNU General Public License.
+ */
+
+package gnu.inet.util;
+
+import java.io.UnsupportedEncodingException;
+import javax.security.sasl.SaslClient;
+import javax.security.sasl.SaslException;
+
+/**
+ * SASL mechanism for LOGIN.
+ *
+ * @author <a href='mailto:dog at gnu.org'>Chris Burdess</a>
+ */
+public class SaslLogin
+implements SaslClient
+{
+
+  private static final int STATE_USERNAME = 0;
+  private static final int STATE_PASSWORD = 1;
+  private static final int STATE_COMPLETE = 2;
+
+  private String username;
+  private String password;
+  private int state;
+
+  public SaslLogin (String username, String password)
+  {
+    this.username = username;
+    this.password = password;
+    state = STATE_USERNAME;
+  }
+
+  public String getMechanismName ()
+  {
+    return "LOGIN";
+  }
+
+  public boolean hasInitialResponse ()
+  {
+    return false;
+  }
+
+  public byte[] evaluateChallenge (byte[] challenge)
+    throws SaslException
+  {
+    try
+      {
+        switch (state)
+          {
+          case STATE_USERNAME:
+            state = STATE_PASSWORD;
+            return username.getBytes ("US-ASCII");
+          case STATE_PASSWORD:
+            state = STATE_COMPLETE;
+            return password.getBytes ("US-ASCII");
+          default:
+            return new byte[0];
+          }
+      }
+    catch (UnsupportedEncodingException e)
+      {
+        String msg = "Username or password contains non-ASCII characters";
+        throw new SaslException (msg, e);
+      }
+  }
+
+  public boolean isComplete ()
+  {
+    return (state == STATE_COMPLETE);
+  }
+
+  public byte[] unwrap (byte[] incoming, int off, int len)
+    throws SaslException
+  {
+    byte[] ret = new byte[len - off];
+    System.arraycopy (incoming, off, ret, 0, len);
+    return ret;
+  }
+
+  public byte[] wrap (byte[] outgoing, int off, int len)
+    throws SaslException
+  {
+    byte[] ret = new byte[len - off];
+    System.arraycopy (outgoing, off, ret, 0, len);
+    return ret;
+  }
+
+  public Object getNegotiatedProperty (String name)
+  {
+    return null;
+  }
+
+  public void dispose ()
+  {
+  }
+  
+}
===================================================================
Checking out kaffe/libraries/javalib/gnu/inet/util/SaslPlain.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/gnu/inet/util/SaslPlain.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/gnu/inet/util/SaslPlain.java	Tue Sep 21 15:32:30 2004
@@ -0,0 +1,113 @@
+/*
+ * SaslPlain.java
+ * Copyright (C) 2004 The Free Software Foundation
+ * 
+ * This file is part of GNU inetlib, a library.
+ * 
+ * GNU inetlib is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * GNU inetlib is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * As a special exception, if you link this library with other files to
+ * produce an executable, this library does not by itself cause the
+ * resulting executable to be covered by the GNU General Public License.
+ * This exception does not however invalidate any other reasons why the
+ * executable file might be covered by the GNU General Public License.
+ */
+
+package gnu.inet.util;
+
+import java.io.UnsupportedEncodingException;
+import javax.security.sasl.SaslClient;

*** Patch too long, truncated ***




More information about the kaffe mailing list