Digital Signatures for Security
Concepts of digital signatures for secure sites
The password of the florist's website is vulnerable in two regards: First, it is stored as plain text in the database. Normally, data sources for the Internet must be given full access because various queries may need to access different tables or views to construct HTML output - we did this ourselves when we assigned the Internet User read and write permissions for the florist.mdb database. In today's Internet age, easy to use wizards and tools may be used to generate HTML, SQL, server side scripts and so on; a user could run his own scripts that access the table and get all the passwords. We have partially protected the database by using a data source. We could easily use a datasource password to prevent such attacks.
More importantly, the application server (such as IIS) should be set up with proper permissions for every web site and should be able to recognize rogue commands and actions (like unauthorized interception of another user's session, for instance, akin to phone tapping). Normal internet users should also be prevented from getting too close to important data by means of a 'firewall' that blocks certain sorts of HTTP requests.
Internet session security has to do with making sure 'packet sniffing' does not occur. Packet sniffing is performed by hackers whose computers are hooked up to an internet backbone. They run programs that access TCP/IP packets that are travelling between an authorized user and a web server and translate bits and bytes to get passwords, credit card numbers and so on. Digital signatures and encryption/decryption are key technologies that are used today to protect information from such hackers.
Secure Socket technology uses 'certificates' and public and private keys to ensure that the person trying to get at the data is the authorized user. Basically, a server generates a public key/private key pair using a certain algorithm. The key pair is made up of strings of tough to crack hexadecimal characters of required length. The server sends the public key to the client computer along with its response. This public key is used by the client computer to encrypt the next request. However, no one other than the holder of the private key can decrypt the request from the client computer - the decryption algorithm requires both keys in order to translate the data. Generating the right private key is computationally impossible; online banks, bill payment areas and so on use such systems.
Digital signatures are one way processes - a digital signature cannot be decrypted. It can simply be compared to another digital signature to make sure that they match. One famous algorithm that takes strings and creates digital signatures out of them is the MD5 'message digest' algorithm. Basically, any string is converted into a 128 bit string of hexadecimal values that constitutes the digital signature and is next to impossible to decipher. Instead of sending a from password over the net unprotected, its digital signature is sent instead. At the other end, the digital signature of the password from the database is extracted. The two are compared. If they match, the user is logged in.
There is however one vulnerability - hackers sometimes maintain dictionaries of digital signatures. If someone uses a simple password that corresponds to a common word or name, the digital signature generated for a password may exist on a hacker's dictionary and he may easily get the original password. To take care of cases where users provide weak passwords, a CHAP or Challenge Handshake Authentication Protocol has been devised by a set of people who required secure login but did not have the resources to implement a Secure Socket Layer for their sites. This protocol involves implementing the following steps every time a request for a login page is fulfilled in order to create a less vulnerable digital signature
Generate a long random number
Calculate its digital signature using the MD5 algorithm (Random DS)
Set a session variable to this 'Random DS'
Also, in the login form, set a hidden variable's value to 'Random DS'
After the user has entered his or her password and has clicked on submit, perform form validation
Calculate another digital signature - that of the password entered by the user (Password DS).
Now, combine the two digital signatures into one long string ("Password DS" + "Random DS")
Calculate the digital signature of this long string (Combined DS)and load its value into the hidden form field that initially contained 'Random DS'
Empty the password field's value and submit the form
The processing script on the server has access to 'Random DS' because of the session variable that was set to equal Random DS before the login form was loaded. (remember, Digital Signatures cannot be decrypted - it is a one way process)
The processing script retrieves the text password corresponding to the username from the database.
DatabasePassword DS is calculated.
The Session variable containing Random DS is appended to DatabasePassword DS
The combined DS is calculated and compared to the 'Combined DS' from the form. The user is logged in if they match.
The session variable is reset and a new random number is generated for each try
Since a random number's digital signature is added to the password's digital signature and the combined digital signature is sent across the network, and since a different random number is used for each try, a CHAP system is quite secure. We will build a CHAP system using 'Freeware' MD5 Digital Signature Calculator programs in VB Script and Javascript. Although we will secure the login page this way, the original registration page is still insecure; the password is sent as plain text. Further, the password field in the database table is not encrypted; it is just masked. You can research ways to secure the database password and the password at the time of registration as an exercise.
An included zip file contains all the material used for the CHAP exercise. Save this file on your computer and extract its contents onto the Inetpub\wwwroot\florist directory on your computer. This file contains the following items:
Login.asp - The Digital Signature ready florist web site login page
CheckloginCHAP.asp - The script used to authenticate CHAP augmented login
md5digsiggenerator.asp - An ASP file that contains a ' DigestStrToMD5Hex()' function that takes a string as input and returns the MD5 hexadecimal hash of the string. We will use this function in login.asp to convert the random integer to a digital signature. We will also use this function in checkloginCHAP.asp to convert the database password to its digital signature and to convert the database password DS + Random DS string to the digital signature that will be compared with the challenge value from the form.
md5.js - A Javascript file that contains a 'hex_md5()' function that takes a string as input and returns the MD5 hexadecimal hash of the string. This will be embedded in the login.asp file and will be used to convert the password entered by the user to a digital signature and to compute the 'challenge' value at the time of form submission.
You may take a look at the md5digsiggenerator.asp and md5.js files to get a better idea about the MD5 algorithm; Comments at the start of each program provide detailed notes and links. A wealth of such free resources are available on the Internet. Let us now look at the code added to login.asp and checklogin.asp to incorporate Digital Signature security. Let us look at the login.asp file first.
<!--#include file="md5digsiggenerator.asp"--> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>
The 'include' line above is called a server side include. The md5digsiggenerator.asp file is a long file; we will just be calling a function it contains. Using an include line will allow us to use all the functions the script mentioned after "file=" contains without copying all the complicated code into the current script. One include statement should be used for each file we wish to access.
<%
Dim challenge
Randomize
challenge = cstr(int((1000000000 * Rnd) + 999999999 ))
challenge = DigestStrToMD5Hex(challenge)
session("challenge") = challenge
%>
The section above uses the 'Randomize' statement to create a random number seed from the server's current time.
Next, a variable called challenge is initialized to contain a ten digit integer random number using VB's 'Rnd' function. Basically, 10000000000 represents the upper boundary of the random number while 999999999 is the lower bound. This formula returns a number with decimal digits; the 'int' function is used to extract just the integer part
The next line of code calls the md5 digital signature generation function to convert the random number to its md5 digital signature.
Finally, the Digital signature of the random variable is stored in the 'challenge' session variable.
In the HTML body, we include a new form field called challenge whose value is set to the Digital Signature of the Random Number we just generated. Also, the action of the form has been set to checkloginCHAP.asp rather than cheklogin.asp.
<td>Password: </td><td><input type=password name=password1 /> <input type="hidden" name="challenge" id="challenge" value="<%= challenge %>" /> </td>
The following additions have been made to the Javascript portion. First, a client side include (<script src=md5.js etc...) allows us to access the Javascript md5 hash generator function without actually putting all the Javascript code in the file.
<script src="md5.js" type="text/javascript"></script>
<SCRIPT LANGUAGE="JavaScript">
<!--
function doCHAP(){
pwdVal = hex_md5(document.forms.loginform.password1.value);
challengeVal = document.forms.loginform.challenge.value;
document.forms.loginform.challenge.value =
hex_md5(pwdVal.toUpperCase() + challengeVal);
document.forms.loginform.password1.value = "";
}
The "doCHAP" Javascript function first converts the value the user entered in the password field to its digital signature using the hex_md5 function in the md5.js file.
Next, it retrieves the Digital Signature of the random number in the challenge hidden field
Finally, it computes the Digital signature of the long string that consists of the Random Number Digital Signature appended to the password Digital Signature (converted to uppercase as the Random Number DS is all uppercase) . The challenge hidden field is set to this value.
Importantly, the password field is reset. This is vital - if we don't do this, the password will travel across the network without any sort of encryption - just the thing we wish to avoid.
All that remains after this is to call the doCHAP function at the vary end of the form validation function right before submitting the form (before the 'return true' statement)
function validate()
{
.
.
.
..LOGIN AND PASSWORD VALIDATION SECTIONS..
.
.
.
.
doCHAP();
return true;
}
The checkloginCHAP script first recovers the md5 hash value that is returned through the challenge variable in the login page form.
set conn=Server.CreateObject("ADODB.Connection")
conn.open "florist","",""
set rs = Server.CreateObject("ADODB.recordset")
dim likesArray, favflower
challengepassword = request.form("challenge")
rs.Open "SELECT * From users where login like '" & request.form("login") & "'" , conn
if rs.eof then
rs.close
set rs = nothing
conn.close
set conn = nothing
response.redirect("login.asp?login=invalid")
The select statement uses just the login the user entered to retrieve a row from the database. An invalid login message is sent to the user if no rows are returned.
else
login = rs("login")
pwdVal = DigestStrToMD5Hex(rs("password"))
challenge = session("challenge")
checkval = DigestStrToMD5Hex(pwdVal & challenge)
session("challenge") = ""
if checkval = challengepassword then
session("login") = login
likesArray = split(rs("likes"),",",-1,1)
favflower = likesArray(0)
set likesArray = nothing
session.Timeout = 30%>
If the login ID exists in the database, we are ready to perform digital signature authentication.
First, we use the DigestStrToMD5Hex function to calculate the md5 digital signature of the password from the database.
Then, we retrieve the challenge session variable. This variable contains the digital signature of the Random Variable generated before the form was sent to the client computer.
The combined digital signature of the password digital signature and the random number digital signature is calculated using DigestStrToMD5Hex just the way we calculated the final value of the challenge hidden form field on the client side before the form was submitted.
The session variable is reset for security
Next, we compare the signature returned from the form to the signature we just computed using the database password. If they match, we authenticate the login and sign in the user. Other wise, we ask the user to try again. Note that the random variable would be generated afresh for every login attempt. The 'back' button cannot be used as the session variable gets reset during authentication
Screenshot 9b: Successful Login Attempt using CHAP
Since this is just an exercise to give you an idea about Internet security, the output pages are designed to show you the MD5 hashes that are calculated on the server and client side. Add new login IDs using the Registration page and play around with the authentication pages by entering correct and invalid passwords.
Screenshot 9c: Unsuccessful Login Attempt using CHAP