320457 Added rfc2045 support to B64Code

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2164 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2010-07-21 04:09:12 +00:00
parent c5583b9ac2
commit 526cef3ab3
3 changed files with 132 additions and 60 deletions

View File

@ -7,6 +7,7 @@ jetty-7.2-SNAPSHOT
+ 320073 Reconsile configuration mechanism
+ 320112 Websocket in aggregate jars
+ 320264 Removed duplicate mime.property entries
+ 320457 Added rfc2045 support to B64Code
+ JETTY-1245 Do not use direct buffers with NIO SSL
+ JETTY-1249 Apply max idle time to all connectors
+ Added ignore to Logger interface

View File

@ -13,8 +13,10 @@
package org.eclipse.jetty.http.security;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import org.eclipse.jetty.util.ByteArrayOutputStream2;
import org.eclipse.jetty.util.StringUtil;
/* ------------------------------------------------------------ */
@ -32,7 +34,7 @@ public class B64Code
{
// ------------------------------------------------------------------
static final char pad='=';
static final char[] nibble2code=
static final char[] rfc1421alphabet=
{
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
@ -40,16 +42,16 @@ public class B64Code
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
};
static final byte[] code2nibble;
static final byte[] rfc1421nibbles;
static
{
code2nibble=new byte[256];
rfc1421nibbles=new byte[256];
for (int i=0;i<256;i++)
code2nibble[i]=-1;
rfc1421nibbles[i]=-1;
for (byte b=0;b<64;b++)
code2nibble[(byte)nibble2code[b]]=b;
code2nibble[(byte)pad]=0;
rfc1421nibbles[(byte)rfc1421alphabet[b]]=b;
rfc1421nibbles[(byte)pad]=0;
}
// ------------------------------------------------------------------
@ -91,7 +93,7 @@ public class B64Code
return new String(encode(bytes));
}
// ------------------------------------------------------------------
/**
* Fast Base 64 encode as described in RFC 1421.
@ -101,25 +103,49 @@ public class B64Code
* @return char array containing the encoded form of the input.
*/
static public char[] encode(byte[] b)
{
return encode(b,false);
}
// ------------------------------------------------------------------
/**
* Fast Base 64 encode as described in RFC 1421 and RFC2045
* <p>Does not insert whitespace as described in RFC 1521, unless rfc2045 is passed as true.
* <p> Avoids creating extra copies of the input/output.
* @param b byte array to encode.
* @param rfc2045 If true, break lines at 76 characters with CRLF
* @return char array containing the encoded form of the input.
*/
static public char[] encode(byte[] b, boolean rfc2045)
{
if (b==null)
return null;
int bLen=b.length;
char r[]=new char[((bLen+2)/3)*4];
int ri=0;
int cLen=((bLen+2)/3)*4;
if (rfc2045)
cLen+=2+2*cLen/76;
char c[]=new char[cLen];
int ci=0;
int bi=0;
byte b0, b1, b2;
int stop=(bLen/3)*3;
int l=0;
while (bi<stop)
{
b0=b[bi++];
b1=b[bi++];
b2=b[bi++];
r[ri++]=nibble2code[(b0>>>2)&0x3f];
r[ri++]=nibble2code[(b0<<4)&0x3f|(b1>>>4)&0x0f];
r[ri++]=nibble2code[(b1<<2)&0x3f|(b2>>>6)&0x03];
r[ri++]=nibble2code[b2&077];
c[ci++]=rfc1421alphabet[(b0>>>2)&0x3f];
c[ci++]=rfc1421alphabet[(b0<<4)&0x3f|(b1>>>4)&0x0f];
c[ci++]=rfc1421alphabet[(b1<<2)&0x3f|(b2>>>6)&0x03];
c[ci++]=rfc1421alphabet[b2&077];
l+=4;
if (rfc2045 && l%76==0)
{
c[ci++]=13;
c[ci++]=10;
}
}
if (bLen!=bi)
@ -129,18 +155,18 @@ public class B64Code
case 2:
b0=b[bi++];
b1=b[bi++];
r[ri++]=nibble2code[(b0>>>2)&0x3f];
r[ri++]=nibble2code[(b0<<4)&0x3f|(b1>>>4)&0x0f];
r[ri++]=nibble2code[(b1<<2)&0x3f];
r[ri++]=pad;
c[ci++]=rfc1421alphabet[(b0>>>2)&0x3f];
c[ci++]=rfc1421alphabet[(b0<<4)&0x3f|(b1>>>4)&0x0f];
c[ci++]=rfc1421alphabet[(b1<<2)&0x3f];
c[ci++]=pad;
break;
case 1:
b0=b[bi++];
r[ri++]=nibble2code[(b0>>>2)&0x3f];
r[ri++]=nibble2code[(b0<<4)&0x3f];
r[ri++]=pad;
r[ri++]=pad;
c[ci++]=rfc1421alphabet[(b0>>>2)&0x3f];
c[ci++]=rfc1421alphabet[(b0<<4)&0x3f];
c[ci++]=pad;
c[ci++]=pad;
break;
default:
@ -148,44 +174,30 @@ public class B64Code
}
}
return r;
if (rfc2045)
{
c[ci++]=13;
c[ci++]=10;
}
return c;
}
// ------------------------------------------------------------------
/**
* Base 64 decode as described in RFC 1421.
* <p>Does not attempt to cope with extra whitespace
* as described in RFC 1521.
* @param s String to decode
* @return String decoded byte array.
*/
static public String decode(String s)
{
try
{
return decode(s,StringUtil.__ISO_8859_1);
}
catch (UnsupportedEncodingException e)
{
throw new IllegalArgumentException(e.toString());
}
}
// ------------------------------------------------------------------
/**
* Base 64 decode as described in RFC 1421.
* <p>Does not attempt to cope with extra whitespace
* as described in RFC 1521.
* @param s String to decode
* Base 64 decode as described in RFC 2045.
* <p>Unlike {@link #decode(char[])}, extra whitespace is ignored.
* @param encoded String to decode.
* @param charEncoding String representing the character encoding
* used to map the decoded bytes into a String.
* @return String decoded byte array.
* @throws UnsupportedEncodingException if the encoding is not supported
* @throws IllegalArgumentException if the input is not a valid
* B64 encoding.
*/
static public String decode(String s,String charEncoding)
static public String decode(String encoded,String charEncoding)
throws UnsupportedEncodingException
{
byte[] decoded=decode(s.toCharArray());
byte[] decoded=decode(encoded);
if (charEncoding==null)
return new String(decoded);
return new String(decoded,charEncoding);
@ -194,8 +206,9 @@ public class B64Code
/* ------------------------------------------------------------ */
/**
* Fast Base 64 decode as described in RFC 1421.
* <p>Does not attempt to cope with extra whitespace
* as described in RFC 1521.
*
* <p>Unlike other decode methods, this does not attempt to
* cope with extra whitespace as described in RFC 1521/2045.
* <p> Avoids creating extra copies of the input/output.
* <p> Note this code has been flattened for performance.
* @param b char array to decode.
@ -230,10 +243,10 @@ public class B64Code
{
while (ri<stop)
{
b0=code2nibble[b[bi++]];
b1=code2nibble[b[bi++]];
b2=code2nibble[b[bi++]];
b3=code2nibble[b[bi++]];
b0=rfc1421nibbles[b[bi++]];
b1=rfc1421nibbles[b[bi++]];
b2=rfc1421nibbles[b[bi++]];
b3=rfc1421nibbles[b[bi++]];
if (b0<0 || b1<0 || b2<0 || b3<0)
throw new IllegalArgumentException("Not B64 encoded");
@ -247,9 +260,9 @@ public class B64Code
switch (rLen%3)
{
case 2:
b0=code2nibble[b[bi++]];
b1=code2nibble[b[bi++]];
b2=code2nibble[b[bi++]];
b0=rfc1421nibbles[b[bi++]];
b1=rfc1421nibbles[b[bi++]];
b2=rfc1421nibbles[b[bi++]];
if (b0<0 || b1<0 || b2<0)
throw new IllegalArgumentException("Not B64 encoded");
r[ri++]=(byte)(b0<<2|b1>>>4);
@ -257,8 +270,8 @@ public class B64Code
break;
case 1:
b0=code2nibble[b[bi++]];
b1=code2nibble[b[bi++]];
b0=rfc1421nibbles[b[bi++]];
b1=rfc1421nibbles[b[bi++]];
if (b0<0 || b1<0)
throw new IllegalArgumentException("Not B64 encoded");
r[ri++]=(byte)(b0<<2|b1>>>4);
@ -277,4 +290,60 @@ public class B64Code
return r;
}
/* ------------------------------------------------------------ */
/**
* Base 64 decode as described in RFC 2045.
* <p>Unlike {@link #decode(char[])}, extra whitespace is ignored.
* @param encoded String to decode.
* @return byte array containing the decoded form of the input.
* @throws IllegalArgumentException if the input is not a valid
* B64 encoding.
*/
static public byte[] decode(String encoded)
{
if (encoded==null)
return null;
int ci=0;
byte nibbles[] = new byte[4];
int s=0;
ByteArrayOutputStream bout = new ByteArrayOutputStream(4*encoded.length()/3);
while (ci<encoded.length())
{
char c=encoded.charAt(ci++);
if (c==pad)
break;
if (Character.isWhitespace(c))
continue;
byte nibble=rfc1421nibbles[c];
if (nibble<0)
throw new IllegalArgumentException("Not B64 encoded");
nibbles[s++]=rfc1421nibbles[c];
switch(s)
{
case 1:
break;
case 2:
bout.write(nibbles[0]<<2|nibbles[1]>>>4);
break;
case 3:
bout.write(nibbles[1]<<4|nibbles[2]>>>2);
break;
case 4:
bout.write(nibbles[2]<<6|nibbles[3]);
s=0;
break;
}
}
return bout.toByteArray();
}
}

View File

@ -34,6 +34,8 @@ public class Constraint implements Cloneable, Serializable
public final static String __CERT_AUTH2 = "CLIENT-CERT";
public final static String __SPNEGO_AUTH = "SPNEGO-AUTH";
public static boolean validateMethod (String method)
{
if (method == null)