While implementing a generic POP3/SMTP Web-Interface (see a German language version at http://webmail.zf2.de) I thought about storing sensible user data (like passwords) over the session. After dropping my first idea to store that data on the server (which is possible but unwanted by our customers), I decided to store those passwords in the Cookie.

As it may be possible (in this case even most probably) that a user will use that interface from a foreign computer (maybe in an internet cafe) I needed to ensure that nobody else could read this information (from the cookie).

The final approach is:

I think this procedure is fine to ensure client security and some server security by not storing the password server-side. I know that the script itself knows the password, but I don't consider this too risky.

I would like to have some comments on this approach. Did I miss something? Did I oversize the problem? Please give me some feedback

alex pleiner <alex@zeitform.de>
zeitform Internet Dienste

use Crypt::CBC; use CGI; my $q = new CGI; my $encryption_method = "Blowfish"; # get cookie my ($login, $password, $pophost, $smtphost, $session) = getcookie(); ## do something here such as fill template et. al. # set cookie my $cookie = setcookie($login, $password, $pophost, $smtphost, $sessio +n); print $q->header(-cookie=>$cookie, -expires => "now"); print $template->output; ############################################ sub setcookie { ############################################ my $login = shift; my $password = shift; my $pophost = shift; my $smtphost = shift; my $session = shift; my $key = $ENV{UNIQUE_ID}; my $ciphertext; my $cipher = new Crypt::CBC($key, $encryption_method); $ciphertext = $cipher->encrypt($password); open ID, ">$Conf::tmp_dir/$session" or print_error("write_error"); print ID $key; close ID; my $cookie = $q->cookie( -name => "zf_webmail", -value => { pop3 => $pophost, smtp => $smtphost, login => $login, password => $ciphertext, id => $session, }, -expires => '+10m'); return $cookie; } ########################################### sub getcookie { ########################################### my %cookies = $q->cookie(-name => "zf_webmail"); my $login = $cookies{login}; unless ($login) { # no cookie -> no session # print error } else { my $ciphertext = $cookies{password} or print_error("no_cook_passwo +rd"); my $pophost = $cookies{pop3} or print_error("no_cook_pop3") +; my $smtphost = $cookies{smtp} or print_error("no_cook_smtp") +; my $session = $cookies{id} or print_error("no_cook_sessio +n"); my $password; open ID, "$Conf::tmp_dir/$session" or print_error("read_error"); my $key = <ID>; close ID; my $cipher = new Crypt::CBC($key, $encryption_method); $password = $cipher->decrypt($ciphertext); return ($login, $password, $pophost, $smtphost, $session); } }

In reply to Encrypted Storage of sensible Data in a Cookie by projekt21

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.