views:

940

answers:

4

I'm a PHP developer who knows almost nothing about .NET. I've been asked by the .NET guys at work to translate .NET code that decrypts an authentication ticket into PHP so that the PHP code can then set the appropriate session variables for my application to run. Is this even possible? I'm staring at the code and it's baffling me. I'll keep trying if someone can tell me it's not a waste of time for some reason I don't even know. Thanks for any help!

Additional info: Can I even grab the ticket with PHP in the first place?

A: 

If you know the decryption algorithm you sure can implement it in PHP.

Gumbo
A: 

As Gumbo said, you need to take into account the algorithms involved. The asp.net authentication ticket uses:

  1. Create a serialized forms authentication ticket. A byte array representation of the ticket is created.
  2. Sign the forms authentication ticket. The message authentication code (MAC) value for the byte array is computed by using the algorithm and key specified by the validation and validationKey attributes of the machineKey element. By default, the SHA1 algorithm is used.
  3. Encrypt forms authentication ticket. The second byte array that has been created is encrypted by using the Encrypt method of the FormsAuthentication class. The Encrypt method internally uses the algorithm and key specified by the decryption and decryptionKey attributes on the machineKey element. ASP.NET version 1.1 uses the 3DES algorithm by default. ASP.NET version 2.0 uses the Rinjdael (AES) algorithm by default.
eglasius
A: 

From Microsoft KB

The forms authentication ticket is used to tell the ASP.NET application who you are. Thus, ticket is building block of Forms Authentication's security.

The ticket is encrypted and signed using the configuration element of the server's Machine.config file. ASP.NET 2.0 uses the decryptionKey and the new decryption attribute of the element to encrypt forms authentication tickets. The decryption attribute lets you specify the encryption algorithm to use. ASP.NET 1.1 and 1.0 use 3DES encryption, which is not configurable. Tampering with the ticket value is determined by a failure to decrypt the ticket on the server. As a result, the user will be redirected to the logon page.

If the application is deployed in a Web farm, you must make sure that the configuration files on each server share the same value for the validationKey and decryptionKey attributes in the tag, which are used for hashing and decryption of the ticket respectively. You must do this because you cannot guarantee which server will handle successive requests. For more information about FormsAuthenticationTicket encryption and Web farm deployment considerations, visit the following MSDN Web site:

So, you can specify what encryption/decryption algorithm to follow and the key. You can use the same decryption logic in PHP.

Ramesh
A: 

First off, open your machine.config and add in a machinekey entry. Set the decryption key and validation key according to a randomly generated ones from a machinekey generator for aspnet 2.0.

Be sure to use the default's, ie. AES and SHA1. Now that you have the AES decrypt key, store it somewhere because you are going to need it on the php side. In your dot net app, go into the web.config and get the forms auth cookie name, usually something like .ASPXAUTH

Now go to the PHP side. Download and set up an AES encryption library, like this one, http://phpseclib.sourceforge.net/documentation/

Then in PHP you can do something like this (this uses the phpsec lib):

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('Crypt/AES.php');

$authCookie = $_COOKIE['_ASPXAUTH'];

echo $authCookie;

$aes = new Crypt_AES();

$aes->setKey('BCDCBE123654F3E365C24E0498346EB95226A307857B9BDE8EBA6198ACF7F03C');

echo $aes->decrypt($authCookie);

Now what ends up coming out is going to first be pm + the SHA1 hash + a byte representation of the auth ticket. You must convert the serialized bytes to a string to make it readable. Can someone else iluminate on that last step?