/usr/share/perl5/Session/Storage/Secure.pm is in libsession-storage-secure-perl 0.010-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 | use 5.008001;
use strict;
use warnings;
package Session::Storage::Secure;
# ABSTRACT: Encrypted, expiring, compressed, serialized session data with integrity
our $VERSION = '0.010'; # VERSION
use Carp (qw/croak/);
use Crypt::CBC ();
use Crypt::Rijndael ();
use Crypt::URandom (qw/urandom/);
use Digest::SHA (qw/hmac_sha256/);
use Math::Random::ISAAC::XS ();
use MIME::Base64 3.12 ();
use Sereal::Encoder ();
use Sereal::Decoder ();
use String::Compare::ConstantTime qw/equals/;
use namespace::clean;
use Moo;
use MooX::Types::MooseLike::Base 0.16 qw(:all);
#--------------------------------------------------------------------------#
# Attributes
#--------------------------------------------------------------------------#
#pod =attr secret_key (required)
#pod
#pod This is used to secure the session data. The encryption and message
#pod authentication key is derived from this using a one-way function. Changing it
#pod will invalidate all sessions.
#pod
#pod =cut
has secret_key => (
is => 'ro',
isa => Str,
required => 1,
);
#pod =attr default_duration
#pod
#pod Number of seconds for which the session may be considered valid. If an
#pod expiration is not provided to C<encode>, this is used instead to expire the
#pod session after a period of time. It is unset by default, meaning that session
#pod expiration is not capped.
#pod
#pod =cut
has default_duration => (
is => 'ro',
isa => Int,
predicate => 1,
);
#pod =attr old_secrets
#pod
#pod An optional array reference of strings containing old secret keys no longer
#pod used for encryption but still supported for decrypting session data.
#pod
#pod =cut
has old_secrets => (
is => 'ro',
isa => ArrayRef [Str],
);
#pod =attr separator
#pod
#pod A character used to separate fields. It defaults to C<~>.
#pod
#pod =cut
has separator => (
is => 'ro',
isa => Str,
default => '~',
);
#pod =attr sereal_encoder_options
#pod
#pod A hash reference with constructor arguments for L<Sereal::Encoder>. Defaults
#pod to C<< { snappy => 1, croak_on_bless => 1 } >>.
#pod
#pod =cut
has sereal_encoder_options => (
is => 'ro',
isa => HashRef,
default => sub { { snappy => 1, croak_on_bless => 1 } },
);
#pod =attr sereal_decoder_options
#pod
#pod A hash reference with constructor arguments for L<Sereal::Decoder>. Defaults
#pod to C<< { refuse_objects => 1, validate_utf8 => 1 } >>.
#pod
#pod =cut
has sereal_decoder_options => (
is => 'ro',
isa => HashRef,
default => sub { { refuse_objects => 1, validate_utf8 => 1 } },
);
#pod =attr transport_encoder
#pod
#pod A code reference to convert binary data elements (the encrypted data and the
#pod MAC) into a transport-safe form. Defaults to
#pod L<MIME::Base64::encode_base64url|MIME::Base64>. The output must not include
#pod the C<separator> attribute used to delimit fields.
#pod
#pod =cut
has transport_encoder => (
is => 'ro',
isa => CodeRef,
default => sub { \&MIME::Base64::encode_base64url },
);
#pod =attr transport_decoder
#pod
#pod A code reference to extract binary data (the encrypted data and the
#pod MAC) from a transport-safe form. It must be the complement to C<encode>.
#pod Defaults to L<MIME::Base64::decode_base64url|MIME::Base64>.
#pod
#pod =cut
has transport_decoder => (
is => 'ro',
isa => CodeRef,
default => sub { \&MIME::Base64::decode_base64url },
);
has _encoder => (
is => 'lazy',
isa => InstanceOf ['Sereal::Encoder'],
handles => { '_freeze' => 'encode' },
);
sub _build__encoder {
my ($self) = @_;
return Sereal::Encoder->new( $self->sereal_encoder_options );
}
has _decoder => (
is => 'lazy',
isa => InstanceOf ['Sereal::Decoder'],
handles => { '_thaw' => 'decode' },
);
sub _build__decoder {
my ($self) = @_;
return Sereal::Decoder->new( $self->sereal_decoder_options );
}
has _rng => (
is => 'lazy',
isa => InstanceOf ['Math::Random::ISAAC::XS'],
handles => { '_irand' => 'irand' },
);
sub _build__rng {
my ($self) = @_;
return Math::Random::ISAAC::XS->new( map { unpack( "N", urandom(4) ) } 1 .. 256 );
}
#pod =method encode
#pod
#pod my $string = $store->encode( $data, $expires );
#pod
#pod The C<$data> argument should be a reference to a data structure. By default,
#pod it must not contain objects. (See L</Objects not stored by default> for
#pod rationale and alternatives.) If it is undefined, an empty hash reference will
#pod be encoded instead.
#pod
#pod The optional C<$expires> argument should be the session expiration time
#pod expressed as epoch seconds. If the C<$expires> time is in the past, the
#pod C<$data> argument is cleared and an empty hash reference is encoded and returned.
#pod If no C<$expires> is given, then if the C<default_duration> attribute is set, it
#pod will be used to calculate an expiration time.
#pod
#pod The method returns a string that securely encodes the session data. All binary
#pod components are protected via the L</transport_encoder> attribute.
#pod
#pod An exception is thrown on any errors.
#pod
#pod =cut
sub encode {
my ( $self, $data, $expires ) = @_;
$data = {} unless defined $data;
my $sep = $self->separator;
# If expiration is set, we want to check it and possibly clear data;
# if not set, we might add an expiration based on default_duration
if ( defined $expires ) {
$data = {} if $expires < time;
}
else {
$expires = $self->has_default_duration ? time + $self->default_duration : "";
}
# Random salt used to derive unique encryption/MAC key for each cookie
my $salt = $self->_irand;
my $key = hmac_sha256( $salt, $self->secret_key );
my $cbc = Crypt::CBC->new( -key => $key, -cipher => 'Rijndael' );
my ( $ciphertext, $mac );
eval {
$ciphertext = $self->transport_encoder->( $cbc->encrypt( $self->_freeze($data) ) );
$mac = $self->transport_encoder->( hmac_sha256( "$expires$sep$ciphertext", $key ) );
};
croak "Encoding error: $@" if $@;
return join( $sep, $salt, $expires, $ciphertext, $mac );
}
#pod =method decode
#pod
#pod my $data = $store->decode( $string );
#pod
#pod The C<$string> argument must be the output of C<encode>.
#pod
#pod If the message integrity check fails or if expiration exists and is in
#pod the past, the method returns undef or an empty list (depending on context).
#pod
#pod An exception is thrown on any errors.
#pod
#pod =cut
sub decode {
my ( $self, $string ) = @_;
return unless length $string;
# Having a string implies at least salt; expires is optional; rest required
my $sep = $self->separator;
my ( $salt, $expires, $ciphertext, $mac ) = split qr/\Q$sep\E/, $string;
return unless defined($ciphertext) && length($ciphertext);
return unless defined($mac) && length($mac);
# Try to decode against all known secret keys
my @secrets = ( $self->secret_key, @{ $self->old_secrets || [] } );
my $key;
CHECK: foreach my $secret (@secrets) {
$key = hmac_sha256( $salt, $secret );
my $check_mac = eval {
$self->transport_encoder->( hmac_sha256( "$expires$sep$ciphertext", $key ) );
};
last CHECK
if (
defined($check_mac)
&& length($check_mac)
&& equals( $check_mac, $mac ) # constant time comparison
);
undef $key;
}
# Check MAC integrity
return unless defined($key);
# Check expiration
return if length($expires) && $expires < time;
# Decrypt and deserialize the data
my $cbc = Crypt::CBC->new( -key => $key, -cipher => 'Rijndael' );
my $data;
eval {
$self->_thaw( $cbc->decrypt( $self->transport_decoder->($ciphertext) ), $data );
};
croak "Decoding error: $@" if $@;
return $data;
}
1;
# vim: ts=4 sts=4 sw=4 et:
__END__
=pod
=encoding UTF-8
=head1 NAME
Session::Storage::Secure - Encrypted, expiring, compressed, serialized session data with integrity
=head1 VERSION
version 0.010
=head1 SYNOPSIS
my $store = Session::Storage::Secure->new(
secret_key => "your pass phrase here",
default_duration => 86400 * 7,
);
my $encoded = $store->encode( $data, $expires );
my $decoded = $store->decode( $encoded );
=head1 DESCRIPTION
This module implements a secure way to encode session data. It is primarily
intended for storing session data in browser cookies, but could be used with
other backend storage where security of stored session data is important.
Features include:
=over 4
=item *
Data serialization and compression using L<Sereal>
=item *
Data encryption using AES with a unique derived key per encoded session
=item *
Enforced expiration timestamp (optional)
=item *
Integrity protected with a message authentication code (MAC)
=back
The storage protocol used in this module is based heavily on
L<A Secure Cookie Protocol|http://www.cse.msu.edu/~alexliu/publications/Cookie/Cookie_COMNET.pdf>
by Alex Liu and others. Liu proposes a session cookie value as follows:
user|expiration|E(data,k)|HMAC(user|expiration|data|ssl-key,k)
where
| denotes concatenation with a separator character
E(p,q) is a symmetric encryption of p with key q
HMAC(p,q) is a keyed message hash of p with key q
k is HMAC(user|expiration, sk)
sk is a secret key shared by all servers
ssl-key is an SSL session key
Because SSL session keys are not readily available (and SSL termination
may happen prior to the application server), we omit C<ssl-key>. This
weakens protection against replay attacks if an attacker can break
the SSL session key and intercept messages.
Using C<user> and C<expiration> to generate the encryption and MAC keys was a
method proposed to ensure unique keys to defeat volume attacks against the
secret key. Rather than rely on those for uniqueness (with the unfortunate
side effect of revealing user names and prohibiting anonymous sessions), we
replace C<user> with a cryptographically-strong random salt value.
The original proposal also calculates a MAC based on unencrypted data. We
instead calculate the MAC based on the encrypted data. This avoids an extra
step decrypting invalid messages. Because the salt is already encoded into the
key, we omit it from the MAC input.
Therefore, the session storage protocol used by this module is as follows:
salt|expiration|E(data,k)|HMAC(expiration|E(data,k),k)
where
| denotes concatenation with a separator character
E(p,q) is a symmetric encryption of p with key q
HMAC(p,q) is a keyed message hash of p with key q
k is HMAC(salt, sk)
sk is a secret key shared by all servers
The salt value is generated using L<Math::Random::ISAAC::XS>, seeded from
L<Crypt::URandom>.
The HMAC algorithm is C<hmac_sha256> from L<Digest::SHA>. Encryption
is done by L<Crypt::CBC> using L<Crypt::Rijndael> (AES). The ciphertext and
MAC's in the cookie are Base64 encoded by L<MIME::Base64> by default.
During session retrieval, if the MAC does not authenticate or if the expiration
is set and in the past, the session will be discarded.
=head1 ATTRIBUTES
=head2 secret_key (required)
This is used to secure the session data. The encryption and message
authentication key is derived from this using a one-way function. Changing it
will invalidate all sessions.
=head2 default_duration
Number of seconds for which the session may be considered valid. If an
expiration is not provided to C<encode>, this is used instead to expire the
session after a period of time. It is unset by default, meaning that session
expiration is not capped.
=head2 old_secrets
An optional array reference of strings containing old secret keys no longer
used for encryption but still supported for decrypting session data.
=head2 separator
A character used to separate fields. It defaults to C<~>.
=head2 sereal_encoder_options
A hash reference with constructor arguments for L<Sereal::Encoder>. Defaults
to C<< { snappy => 1, croak_on_bless => 1 } >>.
=head2 sereal_decoder_options
A hash reference with constructor arguments for L<Sereal::Decoder>. Defaults
to C<< { refuse_objects => 1, validate_utf8 => 1 } >>.
=head2 transport_encoder
A code reference to convert binary data elements (the encrypted data and the
MAC) into a transport-safe form. Defaults to
L<MIME::Base64::encode_base64url|MIME::Base64>. The output must not include
the C<separator> attribute used to delimit fields.
=head2 transport_decoder
A code reference to extract binary data (the encrypted data and the
MAC) from a transport-safe form. It must be the complement to C<encode>.
Defaults to L<MIME::Base64::decode_base64url|MIME::Base64>.
=head1 METHODS
=head2 encode
my $string = $store->encode( $data, $expires );
The C<$data> argument should be a reference to a data structure. By default,
it must not contain objects. (See L</Objects not stored by default> for
rationale and alternatives.) If it is undefined, an empty hash reference will
be encoded instead.
The optional C<$expires> argument should be the session expiration time
expressed as epoch seconds. If the C<$expires> time is in the past, the
C<$data> argument is cleared and an empty hash reference is encoded and returned.
If no C<$expires> is given, then if the C<default_duration> attribute is set, it
will be used to calculate an expiration time.
The method returns a string that securely encodes the session data. All binary
components are protected via the L</transport_encoder> attribute.
An exception is thrown on any errors.
=head2 decode
my $data = $store->decode( $string );
The C<$string> argument must be the output of C<encode>.
If the message integrity check fails or if expiration exists and is in
the past, the method returns undef or an empty list (depending on context).
An exception is thrown on any errors.
=for Pod::Coverage has_default_duration
=head1 LIMITATIONS
=head2 Secret key
You must protect the secret key, of course. Rekeying periodically would
improve security. Rekeying also invalidates all existing sessions unless the
C<old_secrets> attribute contains old encryption keys still used for
decryption. In a multi-node application, all nodes must share the same secret
key.
=head2 Session size
If storing the encoded session in a cookie, keep in mind that cookies must fit
within 4k, so don't store too much data. This module uses L<Sereal> for
serialization and enables the C<snappy> compression option. Sereal plus Snappy
appears to be one of the fastest and most compact serialization options for
Perl, according to the
L<Sereal benchmarks|https://github.com/Sereal/Sereal/wiki/Sereal-Comparison-Graphs>
page.
However, nothing prevents the encoded output from exceeding 4k. Applications
must check for this condition and handle it appropriately with an error or
by splitting the value across multiple cookies.
=head2 Objects not stored by default
The default Sereal options do not allow storing objects because object
deserialization can have undesirable side effects, including potentially fatal
errors if a class is not available at deserialization time or if internal class
structures changed from when the session data was serialized to when it was
deserialized. Applications should take steps to deflate/inflate objects before
storing them in session data.
Alternatively, applications can change L</sereal_encoder_options> and
L</sereal_decoder_options> to allow object serialization or other object
transformations and accept the risks of doing so.
=head1 SECURITY
Storing encrypted session data within a browser cookie avoids latency and
overhead of backend session storage, but has several additional security
considerations.
=head2 Transport security
If using cookies to store session data, an attacker could intercept cookies and
replay them to impersonate a valid user regardless of encryption. SSL
encryption of the transport channel is strongly recommended.
=head2 Cookie replay
Because all session state is maintained in the session cookie, an attacker
or malicious user could replay an old cookie to return to a previous state.
Cookie-based sessions should not be used for recording incremental steps
in a transaction or to record "negative rights".
Because cookie expiration happens on the client-side, an attacker or malicious
user could replay a cookie after its scheduled expiration date. It is strongly
recommended to set C<cookie_duration> or C<default_duration> to limit the window of
opportunity for such replay attacks.
=head2 Session authentication
A compromised secret key could be used to construct valid messages appearing to
be from any user. Applications should take extra steps in their use of session
data to ensure that sessions are authenticated to the user.
One simple approach could be to store a hash of the user's hashed password
in the session on login and to verify it on each request.
# on login
my $hashed_pw = bcrypt( $password, $salt );
if ( $hashed_pw eq $hashed_pw_from_db ) {
session user => $user;
session auth => bcrypt( $hashed_pw, $salt ) );
}
# on each request
if ( bcrypt( $hashed_pw_from_db, $salt ) ne session("auth") ) {
context->destroy_session;
}
The downside of this is that if there is a read-only attack against the
database (SQL injection or leaked backup dump) and the secret key is compromised,
then an attacker can forge a cookie to impersonate any user.
A more secure approach suggested by Stephen Murdoch in
L<Hardened Stateless Session Cookies|http://www.cl.cam.ac.uk/~sjm217/papers/protocols08cookies.pdf>
is to store an iterated hash of the hashed password in the
database and use the hashed password itself within the session.
# on login
my $hashed_pw = bcrypt( $password, $salt );
if ( bcrypt( $hashed_pw, $salt ) eq $double_hashed_pw_from_db ) {
session user => $user;
session auth => $hashed_pw;
}
# on each request
if ( $double_hashed_pw_from_db ne bcrypt( session("auth"), $salt ) ) {
context->destroy_session;
}
This latter approach means that even a compromise of the secret key and the
database contents can't be used to impersonate a user because doing so would
requiring reversing a one-way hash to determine the correct authenticator to
put into the forged cookie.
Both methods require an additional database read per request. This diminishes
some of the scalability benefits of storing session data in a cookie, but
the read could be cached and there is still no database write needed
to store session data.
=head1 SEE ALSO
Papers on secure cookies and cookie session storage:
=over 4
=item *
Liu, Alex X., et al., L<A Secure Cookie Protocol|http://www.cse.msu.edu/~alexliu/publications/Cookie/Cookie_COMNET.pdf>
=item *
Murdoch, Stephen J., L<Hardened Stateless Session Cookies|http://www.cl.cam.ac.uk/~sjm217/papers/protocols08cookies.pdf>
=item *
Fu, Kevin, et al., L<Dos and Don'ts of Client Authentication on the Web|http://pdos.csail.mit.edu/papers/webauth:sec10.pdf>
=back
CPAN modules implementing cookie session storage:
=over 4
=item *
L<Catalyst::Plugin::CookiedSession> -- encryption only
=item *
L<Dancer::Session::Cookie> -- Dancer 1, encryption only
=item *
L<Dancer::SessionFactory::Cookie> -- Dancer 2, forthcoming, based on this module
=item *
L<HTTP::CryptoCookie> -- encryption only
=item *
L<Mojolicious::Sessions> -- MAC only
=item *
L<Plack::Middleware::Session::Cookie> -- MAC only
=item *
L<Plack::Middleware::Session::SerializedCookie> -- really just a framework and you provide the guts with callbacks
=back
Related CPAN modules that offer frameworks for serializing and encrypting data,
but without features relevant for sessions like expiration and unique keying.
=over 4
=item *
L<Crypt::Util>
=item *
L<Data::Serializer>
=back
=for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
=head1 SUPPORT
=head2 Bugs / Feature Requests
Please report any bugs or feature requests through the issue tracker
at L<https://github.com/dagolden/Session-Storage-Secure/issues>.
You will be notified automatically of any progress on your issue.
=head2 Source Code
This is open source software. The code repository is available for
public review and contribution under the terms of the license.
L<https://github.com/dagolden/Session-Storage-Secure>
git clone https://github.com/dagolden/Session-Storage-Secure.git
=head1 AUTHOR
David Golden <dagolden@cpan.org>
=head1 CONTRIBUTOR
Tom Hukins <tom@eborcom.com>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2013 by David Golden.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
=cut
|