Re: Realm SSHA Code

2016-08-17 Thread George Sexton

Chris,


On 7/31/2016 6:56 AM, Christopher Schultz wrote:

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

George,

On 7/29/16 1:46 PM, George Sexton wrote:

I was looking at the source code for
org.apache.catalina.realm.RealmBase and see that it can handle
salted SHA for passwords. Does anyone have some example code that
demonstrates generating the SSHA value: generating the salt, doing
the digest, and outputting the value so that I could put it in my
Tomcat-Users.xml file? I'm using Tomcat 7, so it looks like the
CredentialHandler which provides a mutate() method wouldn't be
available.

Do you mean a salted digest in general, or specifically the
{SSHA}-prefixed variant?

For the former, just use $CATALINA_HOME/bin/digest.sh to launch
RealmBase's main method from the command-line with the right options.
You can always use a later version of Tomcat just for that purpose,
and use the output with the older versions.

For the latter, you'll have to write some code. The format is fairly
straightforward:

  "{SSHA}" + base64 ( 20 salt bytes + SHA1 ( cleartext ) )


That's not actually how RealmBase is doing it, and that's a big problem 
(I think). Let me explain. From the code, it looks like:


"{SSHA}" + base64 ( SHA1 ( cleartext, 20 salt bytes ) + 20 salt bytes )

Here's the code:

// Need to convert the salt to bytes to apply it to the user's
// digested password.
byte[] serverDigestPlusSaltBytes=
Base64.decodeBase64(serverDigestPlusSalt);
finalintsaltPos=20;
byte[] serverDigestBytes=newbyte[saltPos];
System.arraycopy(serverDigestPlusSaltBytes, 0,
serverDigestBytes, 0, saltPos);

// Generate the digested form of the user provided password
// using the salt
byte[] userDigestBytes;
synchronized(this) {
md.reset();
// User provided password
md.update(userCredentials.getBytes(B2CConverter.ISO_8859_1));
// Add the salt
md.update(serverDigestPlusSaltBytes, saltPos,
serverDigestPlusSaltBytes.length-saltPos);
userDigestBytes=md.digest();
}

If my understanding of the code is correct, it's actually 20 bytes of 
digest followed by 20 bytes of salt. That only works if the algorithm 
produces 20 bytes of digest content. SHA-1 does. SHA-256 produces 32 bytes.


My read of the tomcat realm configuration docs talk about the digest 
attribute in a very non-specific way. My read is that you can specify 
whatever algorithm you want in the digest attribute of the Realm 
configuration entry. Again, if I'm reading this code correctly, that's 
not true. SSHA handling will work ONLY if the algorithm specified is SHA-1.


The implementation should really have put the salt bytes first, and then 
had the digest following it so that the digest length could vary.


I guess this is kind of a moot point since the 8.x CredentialHandlers 
are better implementations anyhow and SSHA is not documented anywhere.




In later versions of Tomcat, the MessageDigestCredentialHandler can
*read* the {SSHA}-formatted output, but it can't generate it directly.

I made a patch a while back that gives web applications access to
their CredentialHandlers, so that applications could call matches()
and mutate() without having to know exactly how the passwords were
being stored. If SSHA is in use, then the output won't match the input.

I see an opportunity for improvement of the
MessageDigestCredentialHandler.

Would you care to try your hand at a patch?


digest.sh in the 8.x series handles generating salted code with 
iterations. I think it's OK.


The one patch I would make is that since (and again, I think) the SSHA 
handling in RealmBase is just fatally flawed, it should be removed.




I think you'd need to add a new option - "output flavor" or something
like that - and then the mutate() method would check that setting
before encoding the result.

- -chris
-BEGIN PGP SIGNATURE-
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAled9ZcACgkQ9CaO5/Lv0PDxwACcD4c2O5R/ujXq/R5A3rxv8Rry
0ZUAn1r86Bj7bGB/+D54ZXxz42svD4QW
=BfHp
-END PGP SIGNATURE-

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



--
George Sexton
*MH Software, Inc.*
Voice: 303 438 9585
http://www.connectdaily.com


Re: Realm SSHA Code

2016-07-31 Thread Christopher Schultz
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

George,

On 7/29/16 1:46 PM, George Sexton wrote:
> I was looking at the source code for
> org.apache.catalina.realm.RealmBase and see that it can handle
> salted SHA for passwords. Does anyone have some example code that
> demonstrates generating the SSHA value: generating the salt, doing
> the digest, and outputting the value so that I could put it in my
> Tomcat-Users.xml file? I'm using Tomcat 7, so it looks like the
> CredentialHandler which provides a mutate() method wouldn't be
> available.

Do you mean a salted digest in general, or specifically the
{SSHA}-prefixed variant?

For the former, just use $CATALINA_HOME/bin/digest.sh to launch
RealmBase's main method from the command-line with the right options.
You can always use a later version of Tomcat just for that purpose,
and use the output with the older versions.

For the latter, you'll have to write some code. The format is fairly
straightforward:

 "{SSHA}" + base64 ( 20 salt bytes + SHA1 ( cleartext ) )

In later versions of Tomcat, the MessageDigestCredentialHandler can
*read* the {SSHA}-formatted output, but it can't generate it directly.

I made a patch a while back that gives web applications access to
their CredentialHandlers, so that applications could call matches()
and mutate() without having to know exactly how the passwords were
being stored. If SSHA is in use, then the output won't match the input.

I see an opportunity for improvement of the
MessageDigestCredentialHandler.

Would you care to try your hand at a patch?

I think you'd need to add a new option - "output flavor" or something
like that - and then the mutate() method would check that setting
before encoding the result.

- -chris
-BEGIN PGP SIGNATURE-
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAled9ZcACgkQ9CaO5/Lv0PDxwACcD4c2O5R/ujXq/R5A3rxv8Rry
0ZUAn1r86Bj7bGB/+D54ZXxz42svD4QW
=BfHp
-END PGP SIGNATURE-

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Realm SSHA Code

2016-07-29 Thread George Sexton
I was looking at the source code for org.apache.catalina.realm.RealmBase 
and see that it can handle salted SHA for passwords. Does anyone have 
some example code that demonstrates generating the SSHA value: 
generating the salt, doing the digest, and outputting the value so that 
I could put it in my Tomcat-Users.xml file? I'm using Tomcat 7, so it 
looks like the CredentialHandler which provides a mutate() method 
wouldn't be available.



--
George Sexton
*MH Software, Inc.*
Voice: 303 438 9585
http://www.connectdaily.com