| 1 | ## OpenCA::Token::nCipher.pm | ||
| 2 | ## | ||
| 3 | ## Written by Michael Bell for the OpenCA project 2003 | ||
| 4 | ## Adapted by Martin Bartosch for the OpenCA project 2004 | ||
| 5 | ## Copyright (C) 2003-2004 The OpenCA Project | ||
| 6 | ## All rights reserved. | ||
| 7 | ## | ||
| 8 | ## Adapted to nCipher HSM: 2004-05-25 Martin Bartosch <m.bartosch@cynops.de> | ||
| 9 | ## code based on OpenCA::Token::OpenSSL and others | ||
| 10 | ## | ||
| 11 | ## 2004-08-13 Martin Bartosch | ||
| 12 | ## - added HSM and key online tests | ||
| 13 | ## 2004-08-16 Martin Bartosch | ||
| 14 | ## - added timeout for external programs | ||
| 15 | ## - added extensive error checking | ||
| 16 | ## - documentation and sample configuration | ||
| 17 | ## 2005-05-10 Martin Bartosch | ||
| 18 | ## - added dynamic engine support (for OpenSSL 0.9.8): | ||
| 19 | ## If the token configuration includes at least one PRE_ENGINE key | ||
| 20 | ## then dynamic engine is used, otherwise this module falls back to | ||
| 21 | ## static engine support. | ||
| 22 | ## For dynamic engine support the following option must be added as | ||
| 23 | ## a minimum: | ||
| 24 | ## <option> | ||
| 25 | ## <name>PRE_ENGINE</name> | ||
| 26 | ## <value>SO_PATH:/usr/local/openssl/lib/engines/libncipher.so</value> | ||
| 27 | ## </option> | ||
| 28 | ## The following defaults will be added if not explicitly overridden in | ||
| 29 | ## the token configuration file: | ||
| 30 | ## ID:chil, LIST_ADD:1, LOAD, THREAD_LOCKING:1 | ||
| 31 | ## 2005-07-12 Martin Bartosch | ||
| 32 | ## - added persistant caching to infrastructure and key online tests | ||
| 33 | ## 2005-08-10 Martin Bartosch | ||
| 34 | ## - added missing alarm() calls | ||
| 35 | ## - autoloaded calls to OpenSSL are timed out via alarm() | ||
| 36 | ## | ||
| 37 | ## This library is free software; you can redistribute it and/or | ||
| 38 | ## modify it under the terms of the GNU Lesser General Public | ||
| 39 | ## License as published by the Free Software Foundation; either | ||
| 40 | ## version 2.1 of the License, or (at your option) any later version. | ||
| 41 | ## | ||
| 42 | ## This library is distributed in the hope that it will be useful, | ||
| 43 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 44 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 45 | ## Lesser General Public License for more details. | ||
| 46 | ## | ||
| 47 | ## You should have received a copy of the GNU Lesser General Public | ||
| 48 | ## License along with this library; if not, write to the Free Software | ||
| 49 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 50 | ## | ||
| 51 | ## | ||
| 52 | ## | ||
| 53 | ## ######################################################################### | ||
| 54 | ## | ||
| 55 | ## Error codes: | ||
| 56 | ## 7151010 Crypto Layer not defined | ||
| 57 | ## 7151012 Token name not defined | ||
| 58 | ## 7151013 NFAST_HOME not defined (configuration problem) | ||
| 59 | ## 7151014 NFAST_HOME not accessible (directory does not exist or permission | ||
| 60 | ## denied) | ||
| 61 | ## 7151015 Unexpected exception during program execution | ||
| 62 | ## 7151016 PRE_ENGINE: SO_PATH not defined (configuration problem) | ||
| 63 | ## | ||
| 64 | ## 7153050 Key is not preloaded/usable | ||
| 65 | ## 7153051 nCipher 'hardserver' process is not running | ||
| 66 | ## 7153052 nCipher 'hardserver' process is not operational | ||
| 67 | ## 7153053 Could not execute 'enquiry' program | ||
| 68 | ## 7153054 Could not execute 'nfkminfo' program | ||
| 69 | ## 7153055 Could not execute 'nfkmverify' program | ||
| 70 | ## 7153056 No operational nCipher modules online | ||
| 71 | ## 7153057 nCipher security world is not initialized / is not usable | ||
| 72 | ## 7153058 No preloaded objects found | ||
| 73 | ## 7153059 External program call timed out | ||
| 74 | ## | ||
| 75 | ## 7154001 Key generation not supported | ||
| 76 | |||
| 77 | use strict; | ||
| 78 | |||
| 79 | ############################################################### | ||
| 80 | ## ============ nCipher Token ============= ## | ||
| 81 | ############################################################### | ||
| 82 | |||
| 83 | package OpenCA::Token::nCipher; | ||
| 84 | |||
| 85 | use OpenCA::OpenSSL; | ||
| 86 | |||
| 87 | use FileHandle; | ||
| 88 | our ($STDERR, $STDOUT); | ||
| 89 | $STDOUT = \*STDOUT; | ||
| 90 | $STDERR = \*STDERR; | ||
| 91 | |||
| 92 | our ($errno, $errval); | ||
| 93 | our $AUTOLOAD; | ||
| 94 | |||
| 95 | ($OpenCA::Token::nCipher::VERSION = '$Revision: 1.1.1.1 $' )=~ s/(?:^.*: (\d+))|(?:\s+\$$)/defined $1?"0\.9":""/eg; | ||
| 96 | |||
| 97 | # Errorcode prefix: 715* | ||
| 98 | # 71 token modules | ||
| 99 | # 5 nCipher module | ||
| 100 | |||
| 101 | # Preloaded methods go here. | ||
| 102 | |||
| 103 | ## create a new OpenSSL tokens | ||
| 104 | sub new { | ||
| 105 | my $that = shift; | ||
| 106 | my $class = ref($that) || $that; | ||
| 107 | |||
| 108 | my $self = { | ||
| 109 | DEBUG => 0, | ||
| 110 | debug_fd => $STDERR, | ||
| 111 | ## debug_msg => (), | ||
| 112 | # if online() is called within the grace period return | ||
| 113 | # the cached result if it was successful last time | ||
| 114 | ONLINECHECKGRACEPERIOD => 60, | ||
| 115 | |||
| 116 | # timeout for external nCipher utilities. | ||
| 117 | # there are several error conditions that may lead to | ||
| 118 | # nCipher tools not terminating (such as switching off | ||
| 119 | # a SCSI attached module). in order to gracefully handle | ||
| 120 | # this we introduce a sensible timeout after which the | ||
| 121 | # command will be terminated. | ||
| 122 | CHECKCMDTIMEOUT => 15, | ||
| 123 | |||
| 124 | # temporary directory | ||
| 125 | TMPDIR => "/tmp", | ||
| 126 | }; | ||
| 127 | |||
| 128 | bless $self, $class; | ||
| 129 | |||
| 130 | my $keys = { @_ }; | ||
| 131 | $self->{CRYPTO} = $keys->{OPENCA_CRYPTO}; | ||
| 132 | $self->{gettext} = $keys->{GETTEXT}; | ||
| 133 | $self->{NAME} = $keys->{OPENCA_TOKEN}; | ||
| 134 | $self->{MODE} = $keys->{TOKEN_MODE}; | ||
| 135 | $self->{DEBUG} = $keys->{DEBUG}; | ||
| 136 | $self->{KEY} = $keys->{KEY}; | ||
| 137 | |||
| 138 | foreach (qw(CHECKCMDTIMEOUT ONLINECHECKGRACEPERIOD TMPDIR)) { | ||
| 139 | if (exists $keys->{$_} and ($keys->{$_} ne "")) { | ||
| 140 | $self->{$_} = $keys->{$_}; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | return $self->setError (7151013, | ||
| 145 | $self->{gettext} ("NFAST_HOME not defined.")) | ||
| 146 | if (not $keys->{NFAST_HOME}); | ||
| 147 | return $self->setError (7151014, | ||
| 148 | $self->{gettext} ("NFAST_HOME not accessible.")) | ||
| 149 | if (! (-d $keys->{NFAST_HOME} and -x $keys->{NFAST_HOME})); | ||
| 150 | |||
| 151 | $self->{NFAST_HOME} = $keys->{NFAST_HOME}; | ||
| 152 | |||
| 153 | # provide sensible default for wrapper if not explicitly defined | ||
| 154 | $keys->{WRAPPER} = $keys->{NFAST_HOME} . "/bin/with-nfast -M" | ||
| 155 | unless (exists $keys->{WRAPPER} and ($keys->{WRAPPER} ne "")); | ||
| 156 | |||
| 157 | $self->{WRAPPER} = $keys->{WRAPPER}; | ||
| 158 | |||
| 159 | return $self->setError (7151010, | ||
| 160 | $self->{gettext} ("Crypto layer is not defined.")) | ||
| 161 | if (not $self->{CRYPTO}); | ||
| 162 | return $self->setError (7151012, | ||
| 163 | $self->{gettext} ("The name of the token is not defined.")) | ||
| 164 | if (not $self->{NAME}); | ||
| 165 | |||
| 166 | $keys->{ENGINE} = "chil"; | ||
| 167 | $keys->{CERT} = $keys->{PEM_CERT}; | ||
| 168 | |||
| 169 | # check if we are configured as a dynamic engine | ||
| 170 | if (exists $keys->{PRE_ENGINE}) { | ||
| 171 | $keys->{DYNAMIC_ENGINE} = 1; | ||
| 172 | # $keys->{GET_PIN_CALLBACK} = \&_get_pin_callback; | ||
| 173 | $keys->{STDOUT_CALLBACK} = \&_stdout_callback; | ||
| 174 | $keys->{CALLBACK_HANDLER} = $self; | ||
| 175 | |||
| 176 | # convert scalar to arrayref | ||
| 177 | my $pre_ref; | ||
| 178 | if (ref $keys->{PRE_ENGINE}) { | ||
| 179 | $pre_ref = $keys->{PRE_ENGINE}; | ||
| 180 | } else { | ||
| 181 | push(@{$pre_ref}, $keys->{PRE_ENGINE}); | ||
| 182 | } | ||
| 183 | $keys->{PRE_ENGINE} = $pre_ref; | ||
| 184 | |||
| 185 | return $self->setError (7151016, | ||
| 186 | $self->{gettext} ("7151016 PRE_ENGINE: SO_PATH not defined (configuration problem)")) | ||
| 187 | unless (grep(/SO_PATH/, @{$keys->{PRE_ENGINE}})); | ||
| 188 | |||
| 189 | |||
| 190 | # check if we have to provide PRE_ENGINE defaults | ||
| 191 | # unshift -> add to the beginning of the list | ||
| 192 | unshift @{$keys->{PRE_ENGINE}}, "LIST_ADD:1" | ||
| 193 | unless (grep(/^LIST_ADD:/, @{$keys->{PRE_ENGINE}})); | ||
| 194 | |||
| 195 | unshift @{$keys->{PRE_ENGINE}}, "ID:chil" | ||
| 196 | unless (grep(/^ID:/, @{$keys->{PRE_ENGINE}})); | ||
| 197 | |||
| 198 | # push -> add to the end of the list | ||
| 199 | push @{$keys->{PRE_ENGINE}}, "LOAD" | ||
| 200 | unless (grep(/^LOAD/, @{$keys->{PRE_ENGINE}})); | ||
| 201 | |||
| 202 | push @{$keys->{PRE_ENGINE}}, "THREAD_LOCKING:1" | ||
| 203 | unless (grep(/^THREAD_LOCKING:/, @{$keys->{PRE_ENGINE}})); | ||
| 204 | } | ||
| 205 | |||
| 206 | $self->{OPENSSL} = OpenCA::OpenSSL->new ( %{$keys} ); | ||
| 207 | |||
| 208 | return $self->setError ($OpenCA::OpenSSL::errno, $OpenCA::OpenSSL::errval) | ||
| 209 | if (not $self->{OPENSSL}); | ||
| 210 | |||
| 211 | $self->debug("new: KEY: " . $self->{KEY}); | ||
| 212 | $self->debug("new: NFAST_HOME: " . $self->{NFAST_HOME}); | ||
| 213 | $self->debug("new: WRAPPER: " . $self->{WRAPPER}); | ||
| 214 | $self->debug("new: TMPDIR: " . $self->{TMPDIR}); | ||
| 215 | |||
| 216 | return $self; | ||
| 217 | } | ||
| 218 | |||
| 219 | # Handle persistant caching of status information. | ||
| 220 | # If TIMEOUT is not specified, records the current timestamp in the | ||
| 221 | # state file. | ||
| 222 | # If TIMEOUT is specified the method checks if the state file exists. | ||
| 223 | # If the state file as a mtime that is younger than TIMEOUT (in seconds), | ||
| 224 | # the actual age (seconds) of the last write operation is returned (1 or | ||
| 225 | # higher) | ||
| 226 | # If the state file is older than TIMEOUT, 0 is returned. | ||
| 227 | # | ||
| 228 | # arguments: | ||
| 229 | # ID => name of the cache item, required | ||
| 230 | # TIMEOUT => number of seconds to accept cached status, optional | ||
| 231 | # RETRIGGER => update cached result on success | ||
| 232 | # return: | ||
| 233 | # undef: error or no previous status present | ||
| 234 | # 0: timeout expired | ||
| 235 | # positive number: timeout not expired (age of last status write) | ||
| 236 | sub cachestatus { | ||
| 237 | my $self = shift; | ||
| 238 | my $keys = { @_ }; | ||
| 239 | |||
| 240 | return undef unless defined $keys->{ID}; | ||
| 241 | |||
| 242 | my $filename = $self->{TMPDIR} . "/openca_cache_nCipher_" . $keys->{ID}; | ||
| 243 | |||
| 244 | if (defined $keys->{RESET}) { | ||
| 245 | # reset cache for this id | ||
| 246 | unlink $filename; | ||
| 247 | return 1; | ||
| 248 | } elsif (defined $keys->{TIMEOUT}) { | ||
| 249 | return undef unless (-e $filename); | ||
| 250 | my $age = time - (stat($filename))[9]; | ||
| 251 | $self->debug("cachestatus: state file $filename age: $age"); | ||
| 252 | return undef if ($age < 0); | ||
| 253 | $age = 1 if ($age == 0); | ||
| 254 | if ($age < $keys->{TIMEOUT}) { | ||
| 255 | $self->debug("cachestatus: status was updated within configured timeout of " . $keys->{TIMEOUT} . " seconds ($age seconds ago)"); | ||
| 256 | $self->cachestatus(ID => $keys->{ID}) if (exists $keys->{RETRIGGER} and $keys->{RETRIGGER}); | ||
| 257 | return $age; | ||
| 258 | } | ||
| 259 | $self->debug("cachestatus: timeout expired for " . $keys->{ID}); | ||
| 260 | unlink $filename; | ||
| 261 | return 0; | ||
| 262 | } else { | ||
| 263 | $self->debug("cachestatus: caching online status in $filename"); | ||
| 264 | local *HANDLE; | ||
| 265 | open HANDLE, ">$filename"; | ||
| 266 | close HANDLE; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | |||
| 271 | |||
| 272 | # get object hash for our private key and store it in an internal | ||
| 273 | # data structure for later reference. returns cached value if it has | ||
| 274 | # been called before. | ||
| 275 | # ret: object hash value of the private key for this token | ||
| 276 | # undef on error | ||
| 277 | sub getKeyHash { | ||
| 278 | my $self = shift; | ||
| 279 | |||
| 280 | return $self->{KEYINFO}->{$self->{KEY}}->{OCS}->{HASH} | ||
| 281 | if (exists $self->{KEYINFO}->{$self->{KEY}}); | ||
| 282 | |||
| 283 | $self->debug("getKeyHash: Getting object hash for key " . $self->{KEY}); | ||
| 284 | |||
| 285 | # get object hash for private key | ||
| 286 | my @cmd = (qq("$self->{NFAST_HOME}/bin/nfkmverify"), | ||
| 287 | "hwcrhk", | ||
| 288 | qq("$self->{KEY}")); | ||
| 289 | |||
| 290 | my $keyid = ""; | ||
| 291 | my $keyinfo; | ||
| 292 | my @keys; | ||
| 293 | eval { | ||
| 294 | local $SIG{ALRM} = sub { die "alarm\n" }; | ||
| 295 | alarm $self->{CHECKCMDTIMEOUT}; | ||
| 296 | |||
| 297 | # call nfkmverify to get object hash for key | ||
| 298 | $self->debug("getKeyHash: exec: " . join (' ', @cmd)); | ||
| 299 | if (! open HANDLE, join (' ', @cmd) . "|") { | ||
| 300 | $self->debug("getKeyHash: nCipher nfkmverify: could not run command '" . join (' ', @cmd) . "'"); | ||
| 301 | $self->setError (7153055, | ||
| 302 | $self->{gettext} ("Could not execute nCipher nfkmverify command")); | ||
| 303 | alarm 0; | ||
| 304 | return undef; | ||
| 305 | } | ||
| 306 | |||
| 307 | # parse nfkmverify output | ||
| 308 | while (<HANDLE>) { | ||
| 309 | chomp; | ||
| 310 | if (/^\*\* \[Application key hwcrhk (.*)\]/) { | ||
| 311 | $keyid = $1; | ||
| 312 | # $self->debug("getKeyHash: key id: $keyid"); | ||
| 313 | push (@keys, $keyid); | ||
| 314 | } else { | ||
| 315 | if ($keyid ne "") { | ||
| 316 | if (/^\s*Cardset protected:\s+(\d+)\/(\d+)\s*(.*?)\s*\[(\d+)s\s+`(.*?)'\]/) { # ` make emacs happy | ||
| 317 | $keyinfo->{$keyid}->{OCS}->{QUORUM}->{K} = $1; | ||
| 318 | $keyinfo->{$keyid}->{OCS}->{QUORUM}->{N} = $2; | ||
| 319 | $keyinfo->{$keyid}->{OCS}->{TYPE} = $3; | ||
| 320 | $keyinfo->{$keyid}->{OCS}->{TIMEOUT} = $4; | ||
| 321 | $keyinfo->{$keyid}->{OCS}->{NAME} = $5; | ||
| 322 | } | ||
| 323 | if (/^\s*Cardset hash\s+(.*)/) { | ||
| 324 | $keyinfo->{$keyid}->{OCS}->{HASH} = $1; | ||
| 325 | } | ||
| 326 | if (/^\s*Type\s+(.*)\s+(\d+)/) { | ||
| 327 | $keyinfo->{$keyid}->{TYPE} = $1; | ||
| 328 | $keyinfo->{$keyid}->{BIT} = $2; | ||
| 329 | } | ||
| 330 | } | ||
| 331 | } | ||
| 332 | } | ||
| 333 | close HANDLE; | ||
| 334 | |||
| 335 | alarm 0; | ||
| 336 | }; | ||
| 337 | # handle exceptions | ||
| 338 | if ($@) { | ||
| 339 | if ($@ ne "alarm\n") { | ||
| 340 | $self->setError(7151015, | ||
| 341 | $self->{gettext} ("Unexpected exception during program execution")); | ||
| 342 | return undef; | ||
| 343 | } | ||
| 344 | $self->debug("getKeyHash: nCipher nfkmverify did not terminate within timeout and was interrupted administratively"); | ||
| 345 | $self->setError (7153059, | ||
| 346 | $self->{gettext} ("External program call timed out")); | ||
| 347 | return undef; | ||
| 348 | } | ||
| 349 | |||
| 350 | if ($? != 0) { | ||
| 351 | $self->debug("getKeyHash: nCipher nfkmverify returned error code $?"); | ||
| 352 | $self->setError (7153055, | ||
| 353 | $self->{gettext} ("Could not execute nCipher nfkmverify command")); | ||
| 354 | return undef; | ||
| 355 | } | ||
| 356 | |||
| 357 | $self->debug("getKeyHash: Key information summary"); | ||
| 358 | foreach (@keys) { | ||
| 359 | my $k = \$keyinfo->{$_}; | ||
| 360 | $self->debug("getKeyHash: Key $_:"); | ||
| 361 | $self->debug("getKeyHash: Type: $$k->{TYPE} ($$k->{BIT} bit)"); | ||
| 362 | $self->debug("getKeyHash: OCS name: $$k->{OCS}->{NAME}"); | ||
| 363 | $self->debug("getKeyHash: OCS hash: $$k->{OCS}->{HASH}"); | ||
| 364 | $self->debug("getKeyHash: OCS type: $$k->{OCS}->{TYPE}"); | ||
| 365 | $self->debug("getKeyHash: OCS quorum: $$k->{OCS}->{QUORUM}->{K}/$$k->{OCS}->{QUORUM}->{N}"); | ||
| 366 | $self->debug("getKeyHash: OCS timeout: $$k->{OCS}->{TIMEOUT}"); | ||
| 367 | } | ||
| 368 | |||
| 369 | $self->{KEYINFO} = $keyinfo; | ||
| 370 | return $keyinfo->{$keyid}->{OCS}->{HASH}; | ||
| 371 | } | ||
| 372 | |||
| 373 | |||
| 374 | sub setError { | ||
| 375 | my $self = shift; | ||
| 376 | |||
| 377 | if (scalar (@_) == 4) { | ||
| 378 | my $keys = { @_ }; | ||
| 379 | $errval = $keys->{ERRVAL}; | ||
| 380 | $errno = $keys->{ERRNO}; | ||
| 381 | } else { | ||
| 382 | $errno = $_[0]; | ||
| 383 | $errval = $_[1]; | ||
| 384 | } | ||
| 385 | |||
| 386 | return undef if (not $errno); | ||
| 387 | |||
| 388 | print $STDERR "PKI Master Alert: OpenCA::Token::nCipher error\n"; | ||
| 389 | print $STDERR "PKI Master Alert: Aborting all operations\n"; | ||
| 390 | print $STDERR "PKI Master Alert: Error: $errno\n"; | ||
| 391 | print $STDERR "PKI Master Alert: Message: $errval\n"; | ||
| 392 | print $STDERR "PKI Master Alert: debugging messages of empty token follow\n" | ||
| 393 | ; | ||
| 394 | $self->{debug_fd} = $STDERR; | ||
| 395 | $self->debug (); | ||
| 396 | $self->{debug_fd} = $STDOUT; | ||
| 397 | |||
| 398 | # clear cached key status information | ||
| 399 | $self->cachestatus(ID => 'key_' . $self->{KEY}, | ||
| 400 | RESET => 1); | ||
| 401 | |||
| 402 | ## support for: return $self->setError (1234, "Something fails.") if (not $xyz); | ||
| 403 | return undef; | ||
| 404 | } | ||
| 405 | |||
| 406 | sub errno { | ||
| 407 | my $self = shift; | ||
| 408 | return $self->{errno}; | ||
| 409 | } | ||
| 410 | |||
| 411 | sub errval { | ||
| 412 | my $self = shift; | ||
| 413 | return $self->{errval}; | ||
| 414 | } | ||
| 415 | |||
| 416 | sub debug { | ||
| 417 | |||
| 418 | my $self = shift; | ||
| 419 | if ($_[0]) { | ||
| 420 | $self->{debug_msg}[scalar @{$self->{debug_msg}}] = $_[0]; | ||
| 421 | $self->debug () if ($self->{DEBUG}); | ||
| 422 | } else { | ||
| 423 | my $oldfh; | ||
| 424 | if ($self->{errno}) | ||
| 425 | { | ||
| 426 | $oldfh = select $self->{debug_fd}; | ||
| 427 | print "PKI Debugging: OpenCA::Token::nCipher error\n"; | ||
| 428 | print "PKI Debugging: Aborting all operations\n"; | ||
| 429 | print "PKI Debugging: Error: ".$self->{errno}."\n"; | ||
| 430 | print "PKI Debugging: Message: ".$self->{errval}."\n"; | ||
| 431 | print "PKI Debugging: debugging messages of OpenSSL token follow\n"; | ||
| 432 | select $oldfh; | ||
| 433 | } | ||
| 434 | my $msg; | ||
| 435 | foreach $msg (@{$self->{debug_msg}}) { | ||
| 436 | $msg =~ s/ / /g if ($self->{debug_fd} eq $STDOUT); | ||
| 437 | my $oldfh = select $self->{debug_fd}; | ||
| 438 | print "OpenCA::Token::nCipher->$msg<br>\n"; | ||
| 439 | select $oldfh; | ||
| 440 | } | ||
| 441 | $self->{debug_msg} = (); | ||
| 442 | } | ||
| 443 | } | ||
| 444 | |||
| 445 | sub login { | ||
| 446 | my $self = shift; | ||
| 447 | |||
| 448 | # check if key is online | ||
| 449 | if ($self->online() and $self->keyOnline()) { | ||
| 450 | $self->{ONLINE} = 1; | ||
| 451 | return 1; | ||
| 452 | } | ||
| 453 | return undef; | ||
| 454 | } | ||
| 455 | |||
| 456 | # fake logout function | ||
| 457 | sub logout { | ||
| 458 | my $self = shift; | ||
| 459 | $self->{ONLINE} = 0; | ||
| 460 | return 1; | ||
| 461 | } | ||
| 462 | |||
| 463 | # check if HSM is attached, online and hardserver process is running | ||
| 464 | # the following tests are performed: | ||
| 465 | # - hardserver daemon is running and reports that nCipher is operational | ||
| 466 | # - at least one nCipher module is online | ||
| 467 | sub online { | ||
| 468 | my $self = shift; | ||
| 469 | $self->debug("online: nCipher HSM online check"); | ||
| 470 | |||
| 471 | # if the last check was performed successfully within our grace period | ||
| 472 | # simply return the cached result | ||
| 473 | # if (defined $self->{HSMONLINE} and (time - $self->{HSMONLINE} < $self->{ONLINECHECKGRACEPERIOD})) { | ||
| 474 | if ($self->cachestatus(ID => 'ncipher_module', | ||
| 475 | TIMEOUT => $self->{ONLINECHECKGRACEPERIOD}, | ||
| 476 | RETRIGGER => 1)) { | ||
| 477 | $self->debug("online: Last HSM online check was performed less than " . $self->{ONLINECHECKGRACEPERIOD} . " seconds ago. Returning cached result."); | ||
| 478 | return 1; | ||
| 479 | } | ||
| 480 | |||
| 481 | $self->debug("online: Checking nCipher infrastructure"); | ||
| 482 | # call enquiry to collect information for hardserver and attached modules | ||
| 483 | |||
| 484 | |||
| 485 | my $section = ""; | ||
| 486 | my $enquiry; | ||
| 487 | my @modules; | ||
| 488 | eval { | ||
| 489 | local $SIG{ALRM} = sub { die "alarm\n" }; | ||
| 490 | alarm $self->{CHECKCMDTIMEOUT}; | ||
| 491 | |||
| 492 | my @cmd = (qq("$self->{NFAST_HOME}/bin/enquiry")); | ||
| 493 | $self->debug("online: exec: " . join (' ', @cmd)); | ||
| 494 | if (! open HANDLE, join (' ', @cmd) . "|") { | ||
| 495 | $self->debug("online: nCipher enquiry: could not run command '" . join (' ', @cmd) . "'"); | ||
| 496 | $self->setError (7153053, | ||
| 497 | $self->{gettext} ("Could not execute nCipher enquiry command")); | ||
| 498 | alarm 0; | ||
| 499 | return undef; | ||
| 500 | } | ||
| 501 | |||
| 502 | # parse enquiry output | ||
| 503 | while (<HANDLE>) { | ||
| 504 | chomp; | ||
| 505 | if (/^\S/) { | ||
| 506 | s/[: \#]//g; | ||
| 507 | $section = lc($_); | ||
| 508 | # $self->debug("online: section: " . lc($_)); | ||
| 509 | push (@modules, $section) if ($section =~ /^module/); | ||
| 510 | } else { | ||
| 511 | if (($section ne "") and | ||
| 512 | (/^\s+(mode|version)\s\s+(\S+)/)) { | ||
| 513 | # $self->debug("online: property: $1, value: $2"); | ||
| 514 | $enquiry->{$section}->{lc($1)} = $2; | ||
| 515 | } | ||
| 516 | } | ||
| 517 | } | ||
| 518 | close HANDLE; | ||
| 519 | alarm 0; | ||
| 520 | }; | ||
| 521 | |||
| 522 | # handle exceptions | ||
| 523 | if ($@) { | ||
| 524 | if ($@ ne "alarm\n") { | ||
| 525 | $self->setError(7151015, | ||
| 526 | $self->{gettext} ("Unexpected exception during program execution")); | ||
| 527 | return undef; | ||
| 528 | } | ||
| 529 | $self->debug("online: nCipher enquiry did not terminate within timeout and was interrupted administratively"); | ||
| 530 | $self->setError (7153059, | ||
| 531 | $self->{gettext} ("External program call timed out")); | ||
| 532 | return undef; | ||
| 533 | } | ||
| 534 | |||
| 535 | if ($? != 0) { | ||
| 536 | $self->debug("online: nCipher enquiry: hardserver is not running (error code $?)"); | ||
| 537 | $self->setError (7153051, | ||
| 538 | $self->{gettext} ("nCipher hardserver process is not running")); | ||
| 539 | return undef; | ||
| 540 | } | ||
| 541 | |||
| 542 | $self->debug("online: nCipher hardserver information"); | ||
| 543 | my $operational_modules = 0; | ||
| 544 | foreach (('server', @modules)) { | ||
| 545 | $self->debug("online: '$_' (version: $enquiry->{$_}->{version}) is $enquiry->{server}->{mode}"); | ||
| 546 | $operational_modules++ if (($_ ne "server") and ($enquiry->{$_}->{mode} eq "operational")); | ||
| 547 | } | ||
| 548 | |||
| 549 | if ($enquiry->{server}->{mode} ne "operational") { | ||
| 550 | $self->setError (7153052, | ||
| 551 | $self->{gettext} ("nCipher hardserver process is not operational")); | ||
| 552 | return undef; | ||
| 553 | } | ||
| 554 | |||
| 555 | if ($operational_modules < 1) { | ||
| 556 | $self->debug("online: No operational nCipher modules are online."); | ||
| 557 | $self->setError (7153056, | ||
| 558 | $self->{gettext} ("No operational nCipher modules online")); | ||
| 559 | return undef; | ||
| 560 | } | ||
| 561 | |||
| 562 | $self->cachestatus(ID => 'ncipher_module'); | ||
| 563 | |||
| 564 | return 1; | ||
| 565 | } | ||
| 566 | |||
| 567 | |||
| 568 | # check if our private key is currently preloaded and usable | ||
| 569 | sub keyOnline { | ||
| 570 | my $self = shift; | ||
| 571 | $self->debug("keyOnline: nCipher key online check"); | ||
| 572 | |||
| 573 | # if the last check was performed successfully within our grace period | ||
| 574 | # simply return the cached result | ||
| 575 | # if (defined $self->{KEYONLINE} and (time - $self->{KEYONLINE} < $self->{ONLINECHECKGRACEPERIOD})) { | ||
| 576 | if ($self->cachestatus(ID => 'key_' . $self->{KEY}, | ||
| 577 | TIMEOUT => $self->{ONLINECHECKGRACEPERIOD}, | ||
| 578 | RETRIGGER => 1)) { | ||
| 579 | $self->debug("keyOnline: Last key online check was performed less than " . $self->{ONLINECHECKGRACEPERIOD} . " seconds ago. Returning cached result."); | ||
| 580 | return 1; | ||
| 581 | } | ||
| 582 | |||
| 583 | # check security world and get information about preloaded objects | ||
| 584 | my @cmd = ($self->{WRAPPER}, | ||
| 585 | "$self->{NFAST_HOME}/bin/nfkminfo"); | ||
| 586 | |||
| 587 | my $section = ""; | ||
| 588 | my $worldinfo; | ||
| 589 | eval { | ||
| 590 | local $SIG{ALRM} = sub { die "alarm\n" }; | ||
| 591 | alarm $self->{CHECKCMDTIMEOUT}; | ||
| 592 | |||
| 593 | $self->debug("keyOnline: exec: " . join (' ', @cmd)); | ||
| 594 | if (! open HANDLE, join (' ', @cmd) . "|") { | ||
| 595 | $self->debug("keyOnline: nCipher nfkminfo: could not run command '" . join (' ', @cmd) . "'"); | ||
| 596 | $self->setError (7153054, | ||
| 597 | $self->{gettext} ("Could not execute nCipher nfkminfo command")); | ||
| 598 | alarm 0; | ||
| 599 | return undef; | ||
| 600 | } | ||
| 601 | |||
| 602 | # parse nfkminfo output | ||
| 603 | while (<HANDLE>) { | ||
| 604 | chomp; | ||
| 605 | if (/^\S/) { | ||
| 606 | s/[: \#\-]//g; | ||
| 607 | $section = lc($_); | ||
| 608 | # $self->debug("keyOnline: section: " . lc($_)); | ||
| 609 | } else { | ||
| 610 | if (($section ne "") and | ||
| 611 | (/^\s+(state)\s\s+(.*)/)) { | ||
| 612 | # $self->debug("keyOnline: property: $1, value: $2"); | ||
| 613 | $worldinfo->{$section}->{lc($1)} = $2; | ||
| 614 | } | ||
| 615 | if ($section =~ /preloadedobjects/) { | ||
| 616 | /\s+([0-9A-Fa-f]+)/; | ||
| 617 | # $self->debug("keyOnline: hash: $1"); | ||
| 618 | $worldinfo->{$section}->{preloaded}->{$1}++; | ||
| 619 | } | ||
| 620 | } | ||
| 621 | } | ||
| 622 | close HANDLE; | ||
| 623 | alarm 0; | ||
| 624 | }; | ||
| 625 | |||
| 626 | # handle exceptions | ||
| 627 | if ($@) { | ||
| 628 | if ($@ ne "alarm\n") { | ||
| 629 | $self->setError(7151015, | ||
| 630 | $self->{gettext} ("Unexpected exception during program execution")); | ||
| 631 | return undef; | ||
| 632 | } | ||
| 633 | $self->debug("keyOnline: nCipher nfkminfo did not terminate within timeout and was interrupted administratively"); | ||
| 634 | $self->setError (7153059, | ||
| 635 | $self->{gettext} ("External program call timed out")); | ||
| 636 | return undef; | ||
| 637 | } | ||
| 638 | |||
| 639 | if ($? != 0) { | ||
| 640 | $self->debug("keyOnline: nCipher nfkminfo returned error code $?"); | ||
| 641 | $self->setError (7153054, | ||
| 642 | $self->{gettext} ("Could not execute nCipher nfkminfo command")); | ||
| 643 | return undef; | ||
| 644 | } | ||
| 645 | |||
| 646 | $self->debug("keyOnline: nCipher security world information"); | ||
| 647 | $self->debug("keyOnline: state:" . $worldinfo->{world}->{state}); | ||
| 648 | my $initialized = 0; | ||
| 649 | my $usable = 0; | ||
| 650 | foreach (split(/\s+/, $worldinfo->{world}->{state})) { | ||
| 651 | $initialized++ if ($_ eq "Initialised"); | ||
| 652 | $usable++ if ($_ eq "Usable"); | ||
| 653 | } | ||
| 654 | |||
| 655 | if (! $initialized) { | ||
| 656 | $self->debug("keyOnline: Security world is not initialized."); | ||
| 657 | $self->setError (7153057, | ||
| 658 | $self->{gettext} ("Security world not initialized/usable")); | ||
| 659 | return undef; | ||
| 660 | } | ||
| 661 | if (! $usable) { | ||
| 662 | $self->debug("keyOnline: Security world is not usable."); | ||
| 663 | $self->setError (7153057, | ||
| 664 | $self->{gettext} ("Security world not initialized/usable")); | ||
| 665 | return undef; | ||
| 666 | } | ||
| 667 | |||
| 668 | if (! exists $worldinfo->{$section}->{preloaded}) { | ||
| 669 | $self->debug("keyOnline: No preloaded objects found"); | ||
| 670 | $self->setError (7153058, | ||
| 671 | $self->{gettext} ("No preloaded objects found")); | ||
| 672 | return undef; | ||
| 673 | } | ||
| 674 | |||
| 675 | $self->debug("keyOnline: Preloaded objects:"); | ||
| 676 | foreach (keys %{$worldinfo->{$section}->{preloaded}}) { | ||
| 677 | $self->debug("keyOnline: $_"); | ||
| 678 | } | ||
| 679 | |||
| 680 | # now we have got a list of preloaded objects. verify it against | ||
| 681 | # the object hash of the desired private key. | ||
| 682 | # so first find out what the hash of the key is. | ||
| 683 | |||
| 684 | |||
| 685 | my $ocshash = $self->getKeyHash(); | ||
| 686 | $self->debug("keyOnline: Verify if key ocs object hash $ocshash is preloaded"); | ||
| 687 | if (! exists $worldinfo->{$section}->{preloaded}->{$ocshash}) { | ||
| 688 | $self->debug("keyOnline: Object is not preloaded, key is not usable"); | ||
| 689 | $self->setError (7153050, | ||
| 690 | $self->{gettext} ("Required object is not preloaded, key is not usable")); | ||
| 691 | return undef; | ||
| 692 | } | ||
| 693 | $self->debug("keyOnline: Key seems to be usable"); | ||
| 694 | |||
| 695 | # remember key online status | ||
| 696 | $self->cachestatus(ID => 'key_' . $self->{KEY}); | ||
| 697 | |||
| 698 | return 1; | ||
| 699 | } | ||
| 700 | |||
| 701 | sub getMode { | ||
| 702 | return "daemon"; | ||
| 703 | } | ||
| 704 | |||
| 705 | # # failover to default token OpenSSL which uses -engine | ||
| 706 | # # see new to get an idea what is going on | ||
| 707 | sub AUTOLOAD { | ||
| 708 | my $self = shift; | ||
| 709 | if ($AUTOLOAD =~ /OpenCA::OpenSSL/) | ||
| 710 | { | ||
| 711 | print STDERR "PKI Master Alert: OpenCA::OpenSSL is missing a function\n"; | ||
| 712 | print STDERR "PKI Master Alert: $AUTOLOAD\n"; | ||
| 713 | $self->setError (666, | ||
| 714 | $self->{gettext} ("OpenCA::OpenSSL is missing a function. __FUNCTION | ||
| 715 | __", | ||
| 716 | "__FUNCTION__", $AUTOLOAD)); | ||
| 717 | return undef; | ||
| 718 | } | ||
| 719 | $self->debug ("OpenCA::Token::nCipher: AUTOLOAD => $AUTOLOAD"); | ||
| 720 | return 1 if ($AUTOLOAD eq 'OpenCA::Token::nCipher::DESTROY'); | ||
| 721 | |||
| 722 | my $function = $AUTOLOAD; | ||
| 723 | $function =~ s/.*:://g; | ||
| 724 | |||
| 725 | $self->debug ("nCipher AUTOLOAD function $function"); | ||
| 726 | |||
| 727 | my $ret; | ||
| 728 | eval { | ||
| 729 | local $SIG{ALRM} = sub { die "alarm\n" }; | ||
| 730 | alarm $self->{CHECKCMDTIMEOUT}; | ||
| 731 | |||
| 732 | $ret = $self->{OPENSSL}->$function ( @_ ); | ||
| 733 | alarm 0; | ||
| 734 | }; | ||
| 735 | # handle exceptions | ||
| 736 | if ($@) { | ||
| 737 | if ($@ ne "alarm\n") { | ||
| 738 | $self->setError(7151015, | ||
| 739 | $self->{gettext} ("Unexpected exception during program execution")); | ||
| 740 | return undef; | ||
| 741 | } | ||
| 742 | $self->debug("AUTOLOAD: OpenCA::OpenSSL::$function did not terminate within timeout and was interrupted administratively"); | ||
| 743 | $self->setError (7153059, | ||
| 744 | $self->{gettext} ("External program call timed out")); | ||
| 745 | return undef; | ||
| 746 | } | ||
| 747 | |||
| 748 | $self->setError ($OpenCA::OpenSSL::errno, $OpenCA::OpenSSL::errval); | ||
| 749 | return $ret; | ||
| 750 | } | ||
| 751 | |||
| 752 | |||
| 753 | sub genKey { | ||
| 754 | my $self = shift; | ||
| 755 | |||
| 756 | my $keys = { @_ }; | ||
| 757 | |||
| 758 | return $self->setError (7154001, | ||
| 759 | $self->{gettext} ("Key generation not supported.")); | ||
| 760 | } | ||
| 761 | |||
| 762 | |||
| 763 | sub _stdout_callback | ||
| 764 | { | ||
| 765 | my $self = shift; | ||
| 766 | my $result = shift; | ||
| 767 | |||
| 768 | ## remove leading OpenSSL> from engine call | ||
| 769 | $result =~ s/^OpenSSL>\s//; | ||
| 770 | ## remove all until OpenSSL> | ||
| 771 | $result = substr $result, index $result, "OpenSSL> "; | ||
| 772 | |||
| 773 | return $result | ||
| 774 | } | ||
| 775 | |||
| 776 | |||
| 777 | |||
| 778 | 1; |