| 1 | ## OpenCA::Token::LunaCA3.pm | ||
| 2 | ## | ||
| 3 | ## Written by Michael Bell for the OpenCA project 2003 | ||
| 4 | ## Copyright (C) 2003-2004 The OpenCA Project | ||
| 5 | ## All rights reserved. | ||
| 6 | ## | ||
| 7 | ## This library is free software; you can redistribute it and/or | ||
| 8 | ## modify it under the terms of the GNU Lesser General Public | ||
| 9 | ## License as published by the Free Software Foundation; either | ||
| 10 | ## version 2.1 of the License, or (at your option) any later version. | ||
| 11 | ## | ||
| 12 | ## This library is distributed in the hope that it will be useful, | ||
| 13 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | ## Lesser General Public License for more details. | ||
| 16 | ## | ||
| 17 | ## You should have received a copy of the GNU Lesser General Public | ||
| 18 | ## License along with this library; if not, write to the Free Software | ||
| 19 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 20 | ## | ||
| 21 | |||
| 22 | use strict; | ||
| 23 | |||
| 24 | ############################################################### | ||
| 25 | ## ============ LunaCA3 Token ============= ## | ||
| 26 | ############################################################### | ||
| 27 | |||
| 28 | ## OpenCA::OpenSSL includes code to support the Chrysalis-ITS token too | ||
| 29 | ## errorcodes 713* 71 -> token ; 3 -> third implemented token | ||
| 30 | |||
| 31 | package OpenCA::Token::LunaCA3; | ||
| 32 | |||
| 33 | use OpenCA::OpenSSL; | ||
| 34 | |||
| 35 | our ($errno, $errval, $AUTOLOAD, $STDERR); | ||
| 36 | $STDERR = \*STDERR; | ||
| 37 | |||
| 38 | ($OpenCA::Token::LunaCA3::VERSION = '$Revision: 1.1.1.1 $' )=~ s/(?:^.*: (\d+))|(?:\s+\$$)/defined $1?"0\.9":""/eg; | ||
| 39 | |||
| 40 | # Preloaded methods go here. | ||
| 41 | |||
| 42 | ## create a new LunaCA3 token | ||
| 43 | sub new { | ||
| 44 | |||
| 45 | $ENV{'LD_LIBRARY_PATH'}=$ENV{'LD_LIBRARY_PATH'}.":/apps/usr/luna/lib"; | ||
| 46 | my $that = shift; | ||
| 47 | my $class = ref($that) || $that; | ||
| 48 | |||
| 49 | my $self = { | ||
| 50 | DEBUG => 0, | ||
| 51 | debug_fd => $STDOUT, | ||
| 52 | ## debug_msg => () | ||
| 53 | }; | ||
| 54 | |||
| 55 | bless $self, $class; | ||
| 56 | |||
| 57 | my $keys = { @_ }; | ||
| 58 | $self->{DEBUG} = $keys->{DEBUG}; | ||
| 59 | $self->{CRYPTO} = $keys->{OPENCA_CRYPTO}; | ||
| 60 | $self->{gettext} = $keys->{GETTEXT}; | ||
| 61 | $self->{NAME} = $keys->{OPENCA_TOKEN}; | ||
| 62 | $self->{MODE} = $keys->{TOKEN_MODE}; | ||
| 63 | $self->{UTILITY} = $keys->{UTILITY}; | ||
| 64 | $self->{SLOT} = $keys->{SLOT}; | ||
| 65 | $self->{APPID} = $keys->{APPID}; | ||
| 66 | $self->{LOCK_FILE} = $keys->{LOCK_FILE}; | ||
| 67 | $self->{KEY} = $keys->{KEY}; | ||
| 68 | return undef if (not $self->{CRYPTO}); | ||
| 69 | return undef if (not $self->{NAME}); | ||
| 70 | |||
| 71 | $keys->{ENGINE} = "LunaCA3"; | ||
| 72 | |||
| 73 | if ($self->{MODE}=~ /^(SESSION|DAEMON)$/i) | ||
| 74 | { | ||
| 75 | my $lower=1000; | ||
| 76 | my $upper=50000; | ||
| 77 | my $HiRandom = int(rand( $upper-$lower + 10000 ) ) + $lower; | ||
| 78 | my $LoRandom = int(rand ($upper -$lower + 1)) + $lower ; | ||
| 79 | |||
| 80 | my $AppID = "$HiRandom:$LoRandom"; | ||
| 81 | #print "\n AppID = $HiRandom:$LoRandom\n"; | ||
| 82 | #$self->{APPID} = $AppID; | ||
| 83 | $self->{APPID} = $keys->{APPID};; | ||
| 84 | if (not $self->login()){ | ||
| 85 | $errno = 7134014; | ||
| 86 | $errval = i18nGettext ("Cannot use the private key of the CA (__ERRNO__). __ERRVAL__", | ||
| 87 | "__ERRNO__", $self->errno(), | ||
| 88 | "__ERRVAL__", $self->errval()); | ||
| 89 | return undef; | ||
| 90 | } | ||
| 91 | print " OK"; | ||
| 92 | $keys->{PRE_ENGINE} = " ENGINE_INIT:". $self->{SLOT}.":".$self->{APPID}; | ||
| 93 | |||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | ## create openssl object | ||
| 100 | # Chrysalis changed the OpenSSL patch to be more compliant with OpenSSL | ||
| 101 | # we have no longer to send the slot and application ID | ||
| 102 | #$keys->{ENGINE} = "LunaCA3 -enginearg ". | ||
| 103 | # $self->{SLOT}.":".$self->{APPID}; | ||
| 104 | $keys->{KEYFORM} = "PEM"; | ||
| 105 | $self->debug ("initing OpenSSL"); | ||
| 106 | $self->{OPENSSL} = OpenCA::OpenSSL->new ( %{$keys} ); | ||
| 107 | $errno = $OpenCA::OpenSSL::errno; | ||
| 108 | $errval = $OpenCA::OpenSSL::errval; | ||
| 109 | |||
| 110 | return undef if not $self->{OPENSSL}; | ||
| 111 | |||
| 112 | $self->debug ("module ready"); | ||
| 113 | return $self; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub setError { | ||
| 117 | my $self = shift; | ||
| 118 | |||
| 119 | if (scalar (@_) == 4) { | ||
| 120 | my $keys = { @_ }; | ||
| 121 | $errval = $keys->{ERRVAL}; | ||
| 122 | $errno = $keys->{ERRNO}; | ||
| 123 | } else { | ||
| 124 | $errno = $_[0]; | ||
| 125 | $errval = $_[1]; | ||
| 126 | } | ||
| 127 | return undef if (not $errno); | ||
| 128 | |||
| 129 | print $STDERR "PKI Master Alert: OpenCA::Token::LunaCA3 error\n"; | ||
| 130 | print $STDERR "PKI Master Alert: Aborting all operations\n"; | ||
| 131 | print $STDERR "PKI Master Alert: Error: $errno\n"; | ||
| 132 | print $STDERR "PKI Master Alert: Message: $errval\n"; | ||
| 133 | print $STDERR "PKI Master Alert: debugging messages of empty token follow\n"; | ||
| 134 | $self->{debug_fd} = $STDERR; | ||
| 135 | $self->debug (); | ||
| 136 | $self->{debug_fd} = $STDOUT; | ||
| 137 | |||
| 138 | ## support for: return $self->setError (1234, "Something fails.") if (not $xyz); | ||
| 139 | return undef; | ||
| 140 | } | ||
| 141 | |||
| 142 | ## failover to default token OpenSSL which uses -engine | ||
| 143 | ## see new to get an idea what is going on | ||
| 144 | sub AUTOLOAD { | ||
| 145 | my $self = shift; | ||
| 146 | |||
| 147 | $self->debug ("starting autoloader"); | ||
| 148 | if ($AUTOLOAD =~ /OpenCA::OpenSSL/) | ||
| 149 | { | ||
| 150 | $self->debug ("even openssl cannot handle this function"); | ||
| 151 | print STDERR "PKI Master Alert: OpenCA::OpenSSL is missing a function\n"; | ||
| 152 | print STDERR "PKI Master Alert: $AUTOLOAD\n"; | ||
| 153 | # $self->setError (666, | ||
| 154 | # $self->{gettext} ("OpenCA::OpenSSL is missing a function. __FUNCTION__", | ||
| 155 | # "__FUNCTION__", $AUTOLOAD)); | ||
| 156 | return undef; | ||
| 157 | } | ||
| 158 | $self->debug ("OpenCA::Token::LunaCA3: AUTOLOAD => $AUTOLOAD"); | ||
| 159 | |||
| 160 | return 1 if ($AUTOLOAD eq 'OpenCA::Token::LunaCA3::DESTROY'); | ||
| 161 | |||
| 162 | $self->debug ("starting openssl"); | ||
| 163 | my $function = $AUTOLOAD; | ||
| 164 | $function =~ s/.*:://g; | ||
| 165 | my $ret = $self->{OPENSSL}->$function ( @_ ); | ||
| 166 | $self->setError ($OpenCA::OpenSSL::errno, $OpenCA::OpenSSL::errval); | ||
| 167 | $self->debug ("openssl called"); | ||
| 168 | return $ret; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub login { | ||
| 172 | my $self = shift; | ||
| 173 | $self->debug ("entering fucntion"); | ||
| 174 | |||
| 175 | my $keys = { @_ }; | ||
| 176 | |||
| 177 | if ( -e $self->{LOCK_FILE}) { | ||
| 178 | return 1; | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | my $command = $self->{UTILITY}; | ||
| 183 | $command .= " -o "; | ||
| 184 | $command .= " -s ".$self->{SLOT}; | ||
| 185 | $command .= " -i ".$self->{APPID}; | ||
| 186 | |||
| 187 | |||
| 188 | $self->debug ("executing login"); | ||
| 189 | my $ret = `$command`; | ||
| 190 | if ($? != 0) | ||
| 191 | { | ||
| 192 | $self->setError ($?, $ret); | ||
| 193 | $self->debug ("login failed"); | ||
| 194 | return undef; | ||
| 195 | } else { | ||
| 196 | $self->{ONLINE} = 1; | ||
| 197 | $self->debug ("login succeeded"); | ||
| 198 | if ($self->{MODE} =~ /^(SESSION|DAEMON)$/i) | ||
| 199 | { | ||
| 200 | $self->debug ("touching session file"); | ||
| 201 | my $command = "touch ".$self->{LOCK_FILE}; | ||
| 202 | `$command`; | ||
| 203 | } | ||
| 204 | return 1; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | sub logout { | ||
| 209 | my $self = shift; | ||
| 210 | |||
| 211 | my $keys = { @_ }; | ||
| 212 | |||
| 213 | my $command = $self->{UTILITY}; | ||
| 214 | $command .= " -c "; | ||
| 215 | $command .= " -s ".$self->{SLOT}; | ||
| 216 | $command .= " -i ".$self->{APPID}; | ||
| 217 | |||
| 218 | my $ret = `$command`; | ||
| 219 | if ($? != 0) | ||
| 220 | { | ||
| 221 | $self->setError ($?, $ret); | ||
| 222 | return undef; | ||
| 223 | } else { | ||
| 224 | $self->{ONLINE} = 0; | ||
| 225 | unlink $self->{LOCK_FILE} if (-e $self->{LOCK_FILE}); | ||
| 226 | return 1; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | sub online { | ||
| 231 | ## FIXME: how we can test a HSM to be online? | ||
| 232 | ## FIXME: while we cannot test this we have no chance to | ||
| 233 | ## FIXME: run this HSM in mode session or daemon | ||
| 234 | my $self = shift; | ||
| 235 | |||
| 236 | if ($self->{ONLINE} or -e $self->{LOCK_FILE}) { | ||
| 237 | return 1; | ||
| 238 | } else { | ||
| 239 | return undef; | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | sub keyOnline { | ||
| 244 | my $self = shift; | ||
| 245 | return $self->online; | ||
| 246 | } | ||
| 247 | |||
| 248 | sub getMode { | ||
| 249 | my $self = shift; | ||
| 250 | return $self->{MODE}; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub genKey { | ||
| 254 | my $self = shift; | ||
| 255 | |||
| 256 | my $keys = { @_ }; | ||
| 257 | |||
| 258 | return $self->setError (7134012, | ||
| 259 | $self->{gettext} ("You try to generate a key for a Chrysalis-ITS Luna CA3 token but you don't specify the number of bits.")) | ||
| 260 | if (not $keys->{BITS}); | ||
| 261 | |||
| 262 | return $self->setError (7134014, | ||
| 263 | $self->{gettext} ("You try to generate a key for a Chrysalis-ITS Luna CA3 token but you don't specify the filename where to store the keyreference.")) | ||
| 264 | if (not $self->{KEY}); | ||
| 265 | my $command = $self->{UTILITY}; | ||
| 266 | $command .= " -s ".$self->{SLOT}; | ||
| 267 | $command .= " -i ".$self->{APPID}; | ||
| 268 | $command .= " -g ".$keys->{BITS}; | ||
| 269 | $command .= " -f ".$self->{KEY}; | ||
| 270 | my $ret = `$command`; | ||
| 271 | if ($? != 0) | ||
| 272 | { | ||
| 273 | $self->setError ($?, $ret); | ||
| 274 | return undef; | ||
| 275 | } else { | ||
| 276 | return 1; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | sub debug | ||
| 281 | { | ||
| 282 | my $self = shift; | ||
| 283 | return 1 if (not $self->{DEBUG}); | ||
| 284 | my $msg = shift; | ||
| 285 | |||
| 286 | my ($package, $filename, $line, $subroutine, $hasargs, | ||
| 287 | $wantarray, $evaltext, $is_require, $hints, $bitmask) = caller(0); | ||
| 288 | $msg = "(line $line): $msg"; | ||
| 289 | |||
| 290 | ($package, $filename, $line, $subroutine, $hasargs, | ||
| 291 | $wantarray, $evaltext, $is_require, $hints, $bitmask) = caller(1); | ||
| 292 | $msg = "$subroutine $msg\n"; | ||
| 293 | |||
| 294 | print STDERR $msg; | ||
| 295 | } | ||
| 296 | |||
| 297 | 1; |