21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ters typed by the user. Instead, the password character will be displayed for each<br />

character that is typed. By default, the password character is an asterisk (*), but you<br />

can change it by sending the control an EM_SETPASSWORDCHAR message with wParam set<br />

to the character to display.<br />

Unfortunately, there is no way to prevent Windows from displaying something as<br />

the user types. The closest that can be achieved is to set the password character to a<br />

space, which will make it difficult for an onlooker to determine how many characters<br />

have been typed.<br />

To safely retrieve the password stored in the EDIT control’s internal buffer, the control<br />

should first be queried to determine how many characters it holds. Allocate a<br />

buffer to hold the data and query the data from the control. The control will make a<br />

copy of the data but leave the original internal buffer unchanged.<br />

To be safe, it’s a good idea to set the contents of the buffer to clear the password<br />

from internal memory used by the EDIT control. Simply setting the control’s internal<br />

buffer to an empty string is not sufficient. Instead, set a string that is the length of the<br />

string retrieved, then set an empty string if you wish. For example:<br />

#include <br />

BOOL IsPasswordValid(HWND hwndPassword) {<br />

BOOL bValid = FALSE;<br />

DWORD dwTextLength;<br />

LPTSTR lpText;<br />

if (!(dwTextLength = (DWORD)SendMessage(hwndPassword, WM_GETTEXTLENGTH, 0, 0)))<br />

return FALSE;<br />

lpText = (LPTSTR)LocalAlloc(LMEM_FIXED, (dwTextLength + 1) * sizeof(TCHAR));<br />

if (!lpText) return FALSE;<br />

SendMessage(hwndPassword, WM_GETTEXT, dwTextLength + 1, (LPARAM)lpText);<br />

/* Do something to validate the password */<br />

while (dwTextLength--) *(lpText + dwTextLength) = ' ';<br />

SendMessage(hwndPassword, WM_SETTEXT, 0, (LPARAM)lpText);<br />

LocalFree(lpText);<br />

return bValid;<br />

}<br />

Other processes running on the same machine can access the contents<br />

of your edit control. Unfortunately, the best mitigation strategy, at this<br />

time, is to get rid of the edit control as soon as possible.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Prompting for a Password | 397

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!