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
|
package Crypt::U2F::Server::Simple;
use 5.018001;
use strict;
use warnings;
use Carp;
require Exporter;
our @ISA = qw(Exporter);
our $VERSION = '0.47';
use Crypt::U2F::Server;
my $refCount = 0;
my $errstr = '';
our %EXPORT_TAGS = ( 'all' => [qw()] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw();
my $maxStringLength = 10_000;
sub new {
my ( $proto, %config ) = @_;
my $class = ref($proto) || $proto;
foreach my $key (qw[appId origin]) {
if ( !defined( $config{$key} ) ) {
croak("$key not defined!");
}
if ( !length( $config{$key} ) ) {
croak("$key must not be empty!");
}
}
my $self = bless \%config, $class;
$config{debug} //= 0;
if ( !$refCount ) {
my $rc = Crypt::U2F::Server::u2fclib_init( $config{debug} );
if ( !$rc ) {
$errstr = Crypt::U2F::Server::u2fclib_getError();
return;
}
}
$refCount++;
my $ctx = Crypt::U2F::Server::u2fclib_get_context();
if ( !defined($ctx) || !$ctx ) {
$errstr = Crypt::U2F::Server::u2fclib_getError();
return;
}
$self->{ctx} = $ctx;
{
my $rc =
Crypt::U2F::Server::u2fclib_setAppID( $self->{ctx}, $self->{appId} );
if ( !$rc ) {
$errstr = Crypt::U2F::Server::u2fclib_getError();
return;
}
}
{
my $rc = Crypt::U2F::Server::u2fclib_setOrigin( $self->{ctx},
$self->{origin} );
if ( !$rc ) {
$errstr = Crypt::U2F::Server::u2fclib_getError();
return;
}
}
if ( defined( $self->{keyHandle} ) ) {
my $rc = $self->setKeyHandle;
if ( !$rc ) {
$errstr = Crypt::U2F::Server::u2fclib_getError();
return;
}
}
if ( defined( $self->{publicKey} ) ) {
my $rc = $self->setPublicKey;
if ( !$rc ) {
$errstr = Crypt::U2F::Server::u2fclib_getError();
return;
}
}
return $self;
}
sub lastError {
return $errstr;
}
sub registrationChallenge {
my ($self) = @_;
my $rc =
Crypt::U2F::Server::u2fclib_calcRegistrationChallenge( $self->{ctx} );
if ( !defined($rc) || !length($rc) ) {
$errstr = Crypt::U2F::Server::u2fclib_getError();
return;
}
return $rc;
}
sub registrationVerify {
my ( $self, $registration ) = @_;
if ( length($registration) >= $maxStringLength ) {
$errstr = "Registration string too long!";
return;
}
( $self->{publicKey}, $self->{keyHandle} ) =
Crypt::U2F::Server::u2fclib_verifyRegistration( $self->{ctx},
$registration );
if (
not( defined( $self->{publicKey} ) and defined( $self->{keyHandle} ) ) )
{
$errstr = Crypt::U2F::Server::u2fclib_getError();
return;
}
return ( $self->{keyHandle}, $self->{publicKey} );
}
sub setKeyHandle {
my ( $self, $keyHandle ) = @_;
if ( !defined($keyHandle) && !defined( $self->{keyHandle} ) ) {
$errstr = "No keyHandle given!";
return 0;
}
if ( defined($keyHandle) ) {
$self->{keyHandle} = $keyHandle;
}
if ( length( $self->{keyHandle} ) >= $maxStringLength ) {
$errstr = "keyHandle string too long!";
return 0;
}
my $rc = Crypt::U2F::Server::u2fclib_setKeyHandle( $self->{ctx},
$self->{keyHandle} );
if ( !defined($rc) || !$rc ) {
$errstr = Crypt::U2F::Server::u2fclib_getError();
return;
}
return 1;
}
sub setPublicKey {
my ( $self, $publicKey ) = @_;
if ( !defined($publicKey) && !defined( $self->{publicKey} ) ) {
$errstr = "No publicKey given!";
return 0;
}
if ( defined($publicKey) ) {
$self->{publicKey} = $publicKey;
}
if ( length( $self->{publicKey} ) >= $maxStringLength ) {
$errstr = "publicKey string too long!";
return 0;
}
my $rc = Crypt::U2F::Server::u2fclib_setPublicKey( $self->{ctx},
$self->{publicKey} );
if ( !defined($rc) || !$rc ) {
$errstr = Crypt::U2F::Server::u2fclib_getError();
return;
}
return 1;
}
sub setChallenge {
my ( $self, $challenge ) = @_;
if ( !defined($challenge) && !defined( $self->{challenge} ) ) {
$errstr = "No challenge given!";
return 0;
}
if ( defined($challenge) ) {
$self->{challenge} = $challenge;
}
if ( length( $self->{challenge} ) >= $maxStringLength ) {
$errstr = "challenge string too long!";
return 0;
}
my $rc = Crypt::U2F::Server::u2fclib_setChallenge( $self->{ctx},
$self->{challenge} );
if ( !defined($rc) || !$rc ) {
$errstr = Crypt::U2F::Server::u2fclib_getError();
return;
}
return 1;
}
sub authenticationChallenge {
my ($self) = @_;
if ( !$self->setKeyHandle || !$self->setPublicKey ) {
return;
}
my $rc =
Crypt::U2F::Server::u2fclib_calcAuthenticationChallenge( $self->{ctx} );
if ( !defined($rc) || !length($rc) ) {
$errstr = Crypt::U2F::Server::u2fclib_getError();
return;
}
return $rc;
}
sub authenticationVerify {
my ( $self, $authentication ) = @_;
if ( !$self->setKeyHandle || !$self->setPublicKey ) {
return 0;
}
my $rc = Crypt::U2F::Server::u2fclib_verifyAuthentication( $self->{ctx},
$authentication );
if ( !defined($rc) || !$rc ) {
$errstr = Crypt::U2F::Server::u2fclib_getError();
return 0;
}
return $rc;
}
sub DESTROY {
my ($self) = @_;
Crypt::U2F::Server::u2fclib_free_context( $self->{ctx} );
$refCount--;
if ( $refCount < 0 ) {
croak("refCount error! Aborting everything!");
}
if ( !$refCount ) {
Crypt::U2F::Server::u2fclib_deInit();
}
return;
}
1;
__END__
=head1 NAME
Crypt::U2F::Server::Simple - Register and Authenticate U2F compatible security devices
=head1 SYNOPSIS
use Crypt::U2F::Server:Simple;
my $crypter = Crypt::U2F::Server::Simple->new(
appId => 'Perl',
origin => 'http://search.cpan.org'
);
# Generate a registration request
my $registerRequest = $crypter->registrationChallenge();
# Give $registerRequest to client, receive $registrationData from client
# NB: if Crypt::U2F::Server::Simple has been recreated (web process for example), challenge
# value must be restored (value only, not JSON blob):
#$crypter->setChallenge($challenge)
my ($keyHandle, $userKey) = $crypter->registrationVerify($registrationData)
# Generate an authentication request (using the previously generated key handle and user key)
my $authrequest = $crypter->authenticationChallenge();
# Send $authrequest to client, receive $authSignature
# NB: if Crypt::U2F::Server::Simple has been recreated (web process for example), challenge
# value must be restored (value only, not JSON blob):
#$crypter->setChallenge($challenge)
my $authok = $crypter->authenticationVerify($authSignature);
=head1 DESCRIPTION
This module implements the server side of U2F authentication through Yubico's C library.
Both registration and authentication are two step processes that each must be run in the same instance
of this perl module. To clarify You can run registration from another instance than authentication, or even
in another program on another server. But, as far as it is currently implemented, you must run both registration
steps in the same instance of this module, the same goes for authentication. Needs more testing, really.
A successful registration of a key yields to two scalars, a key handle and a public key. It is B<your>
responsibility to keep them safe somewhere and reload them into this module whenever you want to do
authentication.
=head1 INSTALLATION
This module requires the Yubico u2f-server shared library installed, please see the official
project page at L<https://developers.yubico.com/libu2f-server/> on how to do that.
=head1 NO MULTITHREADING
The way this is currently implemented, i doubt very much that multithreadingm will work.
Multi-Forking should be OK as long as you only call new() B<after> forking, though. Also
using more than one instance of this module in your program. This isn't really tested
at the moment, though...
=head1 ALPHA WARNING
As already stated above, at this time L<Crypt::U2F::Server> and L<Crypt::U2F::Server::Simple> have seen only
very limited testing and the modules are still subject to change.
That isn't to say that you shouldn't use this at all. Rather, if you are interested, you should test this
a lot and report any bugs you find!
=head1 FUNCTION DESCRIPTION
=head2 lastError()
Probably the most important function of all, therefore mention first. If something goes wrong
(but not HorriblyWrong[tm]), you'll get the last error description of whatever happened.
If something fails during new(), call it with the full name:
my $oooops = Crypt::U2F::Server::Simple::lastError();
If you already got an instance, you can use that as well:
my $oooops = $auth->lastError();
Errors are global over all instances of this module.
If things go HorriblyWrong[tm], your program might crash. Or get remote-buffer-overflow-exploited
or something. In these case, lastError() might not work reliably. You know, just the usual crypto
stuff...
=head2 new()
This comes in two forms, depending if you do authentication in the same instance as the registration steps.
The simple form (only registration or registration+authentication) only requires the arguments appId and origin:
my $auth = Crypt::U2F::Server::Simple->new(
appId => 'Perl',
origin => 'http://search.cpan.org'
);
If you only do authentication, you have to supply the keyHandle and publicKey data as well:
my $auth = Crypt::U2F::Server::Simple->new(
appId => 'Perl',
origin => 'http://search.cpan.org',
keyHandle => $keyHandleData
publicKey => $publicKeyData
);
For security, i would recommend creating a new instance for each and every authentication request.
If something goes wrong during initialization, $auth will be I<undef>.
To enable Yubico library debug, set debug to 1:
my $auth = Crypt::U2F::Server::Simple->new(
appId => 'Perl',
origin => 'http://search.cpan.org',
debug => 1
);
=head2 registrationChallenge()
my $challenge = $auth->registrationChallenge();
Gives you a unique registration challenge on every call. This is a JSON string and should be send to the client
(called a "host" for whatever reason) as is.
If something goes wrong, $challenge will be I<undef>.
$challenge is a JSON blob that contains a hash. Here are the main keys
=over
=item version: the protocol version
=item appId: the appId given in new()
=item challenge: the challenge value
=back
=head2 registrationVerify()
my ($keyHandle, $publicKey) = $auth->registrationVerify($reply);
If the client (the "host") accepts the challenge, it will send you another JSON blob ($reply).
If everything goes well and registration succeeds, you will get the key handle and public key of, well
client key. If it fails, you will get I<undef>.
$keyHandle and $publicKey will get set internally for direct following authentication in the same instance,
you need to store it in some persistent way yourself for future authentication.
As an added bonus, $publicKey will be a binary blob, so you may have to convert it to something like Base64 for
easier handling. See L<MIME::Base64> on how to do that. Make sure you un-encode before loading it into this
module!
=head2 authenticationChallenge()
This function generates an authentication challenge. To do that, it needs keyHandle and publicKey, since this
is key dependent.
my $challenge = $auth->authenticationChallenge();
Otherwise, this works the same as the registration challenge. You get a JSON blob, send that to the client and
get an answer.
The JSON blob structure is described in registrationChallenge() doc.
=head2 authenticationVerify()
After you get the authentication answer, you need to verify it:
my $isValid = $auth->authenticationVerify($reply);
$isValid is true if authentication succeedsr. If something went wrong (library error, fake user), $isValid is false, in which
case you can look into lastError() to see what went wrong.
=head2 setChallenge()
If Crypt::U2F::Server::Simple has been recreated since registrationChallenge()
or authenticationChallenge() usage, challenge value must be restored:
$auth->setChallenge($challenge)
Note that $challenge must be the string value of challenge, not the JSON blob.
See registrationChallenge() doc to get challenge description.
=head1 SEE ALSO
See L<Crypt::U2F::Server> for the low level library if you want better headaches.
There are two examples in the tarball for registration and authentication.
=head1 BUGS
Yes, there should be some in there. First of all, this is crypto stuff, so
it's broken by default (it only depends on the time it takes to happen).
Also, at the moment, this module has seen only very limited testing.
=head1 AUTHOR
=over
=item Rene Schickbauer, E<lt>rene.schickbauer@magnapowertrain.comE<gt>
=item Xavier Guimard, E<lt>x.guimard@free.frE<gt>
=back
=head1 COPYRIGHT AND LICENSE
Adapted as a Perl library by Rene 'cavac' Schickbauer
This roughly based on u2f-server.c from Yubico's
C library, see L<https://developers.yubico.com/libu2f-server/>
In order for this to work, you need to install that
library.
This adaption is (C) 2014-2018 Rene 'cavac' Schickbauer and 2018 Xavier
Guimard, but as it is based on Yubico's code, the licence below applies!
I<We, the community, would hereby thank Yubico for open sourcing their code!>
/*
* Copyright (c) 2014 Yubico AB
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
=cut
|