| 1 | ## OpenCA::Token::OpenSSL.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 | ## ============ OpenSSL Token ============= ## | ||
| 26 | ############################################################### | ||
| 27 | |||
| 28 | package OpenCA::Token::OpenSSL; | ||
| 29 | |||
| 30 | use OpenCA::OpenSSL; | ||
| 31 | |||
| 32 | our ($errno, $errval); | ||
| 33 | |||
| 34 | ($OpenCA::Token::OpenSSL::VERSION = '$Revision: 1.1.1.1 $' )=~ s/(?:^.*: (\d+))|(?:\s+\$$)/defined $1?"0\.9":""/eg; | ||
| 35 | |||
| 36 | # Errorcode prefix: 711* | ||
| 37 | # 71 token modules | ||
| 38 | # 1 first implemented token | ||
| 39 | |||
| 40 | # Preloaded methods go here. | ||
| 41 | |||
| 42 | ## create a new OpenSSL tokens | ||
| 43 | sub new { | ||
| 44 | my $that = shift; | ||
| 45 | my $class = ref($that) || $that; | ||
| 46 | |||
| 47 | my $self = { | ||
| 48 | DEBUG => 0, | ||
| 49 | }; | ||
| 50 | |||
| 51 | bless $self, $class; | ||
| 52 | |||
| 53 | $self->debug ("new: class instantiated"); | ||
| 54 | |||
| 55 | my $keys = { @_ }; | ||
| 56 | $self->{CRYPTO} = $keys->{OPENCA_CRYPTO}; | ||
| 57 | $self->{gettext} = $keys->{GETTEXT}; | ||
| 58 | $self->{NAME} = $keys->{OPENCA_TOKEN}; | ||
| 59 | ## TOKEN_MODE will be ignored | ||
| 60 | $self->{PASSWD_PARTS} = $keys->{PASSWD_PARTS}; | ||
| 61 | ## FIXME: I hope this fixes @_ | ||
| 62 | delete $keys->{OPENCA_CRYPTO}; | ||
| 63 | delete $keys->{OPENCA_TOKEN}; | ||
| 64 | delete $keys->{PASSWD_PARTS}; | ||
| 65 | return $self->setError (7111010, | ||
| 66 | $self->{gettext} ("Crypto layer is not defined.")) | ||
| 67 | if (not $self->{CRYPTO}); | ||
| 68 | return $self->setError (7111012, | ||
| 69 | $self->{gettext} ("The name of the token is not defined.")) | ||
| 70 | if (not $self->{NAME}); | ||
| 71 | |||
| 72 | $self->debug ("new: crypto and name present"); | ||
| 73 | |||
| 74 | $self->{OPENSSL} = OpenCA::OpenSSL->new ( %{$keys} ); | ||
| 75 | return $self->setError ($OpenCA::OpenSSL::errno, $OpenCA::OpenSSL::errval) | ||
| 76 | if (not $self->{OPENSSL}); | ||
| 77 | |||
| 78 | $self->debug ("new: NAME ".$self->{NAME}); | ||
| 79 | $self->debug ("new: PASSWD_PARTS ".$self->{PASSWD_PARTS}); | ||
| 80 | |||
| 81 | return $self; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub setError { | ||
| 85 | my $self = shift; | ||
| 86 | |||
| 87 | if (scalar (@_) == 4) { | ||
| 88 | my $keys = { @_ }; | ||
| 89 | $self->{errval} = $keys->{ERRVAL}; | ||
| 90 | $self->{errno} = $keys->{ERRNO}; | ||
| 91 | } else { | ||
| 92 | $self->{errno} = $_[0]; | ||
| 93 | $self->{errval} = $_[1]; | ||
| 94 | } | ||
| 95 | $errno = $self->{errno}; | ||
| 96 | $errval = $self->{errval}; | ||
| 97 | |||
| 98 | ## FIXME: this is usually a bug | ||
| 99 | return undef if (not $self->{errno}); | ||
| 100 | |||
| 101 | $self->debug ("setError: errno $errno"); | ||
| 102 | $self->debug ("setError: errval $errval"); | ||
| 103 | |||
| 104 | ## support for: return $self->setError (1234, "Something fails.") if (not $xyz); | ||
| 105 | return undef; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub errno { | ||
| 109 | my $self = shift; | ||
| 110 | return $self->{errno}; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub errval { | ||
| 114 | my $self = shift; | ||
| 115 | return $self->{errval}; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub debug { | ||
| 119 | |||
| 120 | my $self = shift; | ||
| 121 | |||
| 122 | return 1 if (not $self->{DEBUG}); | ||
| 123 | |||
| 124 | foreach my $msg (@_) { | ||
| 125 | print STDERR "OpenCA::Token::OpenSSL->$msg\n"; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | sub login { | ||
| 130 | my $self = shift; | ||
| 131 | if ($_[0]) | ||
| 132 | { | ||
| 133 | $self->{PASSWD} = shift; | ||
| 134 | } else { | ||
| 135 | my @result = ($self->{CRYPTO}->getAccessControl())->getTokenParam ( | ||
| 136 | $self->{NAME}, | ||
| 137 | $self->{PASSWD_PARTS}); | ||
| 138 | $self->{PASSWD} = join '', @result; | ||
| 139 | } | ||
| 140 | $self->{OPENSSL}->{PASSWD} = $self->{PASSWD}; | ||
| 141 | |||
| 142 | return $self->setError (7113050, | ||
| 143 | $self->{gettext} ("Wrong passphrase for private key!")) | ||
| 144 | if (not $self->{OPENSSL}->dataConvert (DATATYPE => "KEY", OUTFORM=>"PKCS8")); | ||
| 145 | |||
| 146 | $self->{ONLINE} = 1; | ||
| 147 | return 1; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub logout { | ||
| 151 | my $self = shift; | ||
| 152 | undef $self->{PASSWD}; | ||
| 153 | undef $self->{OPENSSL}->{PASSWD}; | ||
| 154 | $self->{ONLINE} = 0; | ||
| 155 | return 1; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub online { | ||
| 159 | my $self = shift; | ||
| 160 | return 1; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub keyOnline { | ||
| 164 | my $self = shift; | ||
| 165 | return undef if (not $self->{ONLINE}); | ||
| 166 | return 1; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub getMode { | ||
| 170 | return "standby"; | ||
| 171 | } | ||
| 172 | |||
| 173 | ## map functions manually to avoid memeory leaks from Perl | ||
| 174 | |||
| 175 | sub getDigest | ||
| 176 | { | ||
| 177 | my $self = shift; | ||
| 178 | my $ret = return $self->{OPENSSL}->getDigest(@_); | ||
| 179 | $self->setError ($self->{OPENSSL}->errno, $self->{OPENSSL}->errval) | ||
| 180 | if (not defined $ret); | ||
| 181 | return $ret; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub getPIN | ||
| 185 | { | ||
| 186 | my $self = shift; | ||
| 187 | my $ret = return $self->{OPENSSL}->getPIN(@_); | ||
| 188 | $self->setError ($self->{OPENSSL}->errno, $self->{OPENSSL}->errval) | ||
| 189 | if (not defined $ret); | ||
| 190 | return $ret; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub encrypt | ||
| 194 | { | ||
| 195 | my $self = shift; | ||
| 196 | my $ret = return $self->{OPENSSL}->encrypt(@_); | ||
| 197 | $self->setError ($self->{OPENSSL}->errno, $self->{OPENSSL}->errval) | ||
| 198 | if (not defined $ret); | ||
| 199 | return $ret; | ||
| 200 | } | ||
| 201 | |||
| 202 | ## use OpenSSL by default but take care about the errorcodes | ||
| 203 | sub AUTOLOAD { | ||
| 204 | my $self = shift; | ||
| 205 | use vars qw($AUTOLOAD); | ||
| 206 | |||
| 207 | if ($AUTOLOAD =~ /OpenCA::OpenSSL/) | ||
| 208 | { | ||
| 209 | print STDERR "PKI Master Alert: OpenCA::OpenSSL is missing a function\n"; | ||
| 210 | print STDERR "PKI Master Alert: $AUTOLOAD\n"; | ||
| 211 | $self->setError (666, | ||
| 212 | $self->{gettext} ("OpenCA::OpenSSL is missing a function. __FUNCTION__", | ||
| 213 | "__FUNCTION__", $AUTOLOAD)); | ||
| 214 | return undef; | ||
| 215 | } | ||
| 216 | $self->debug ("OpenCA::Token::OpenSSL: AUTOLOAD => $AUTOLOAD"); | ||
| 217 | |||
| 218 | return 1 if ($AUTOLOAD eq 'OpenCA::Token::OpenSSL::DESTROY'); | ||
| 219 | |||
| 220 | my $function = $AUTOLOAD; | ||
| 221 | $function =~ s/.*:://g; | ||
| 222 | my $ret = $self->{OPENSSL}->$function ( @_ ); | ||
| 223 | $self->setError ($OpenCA::OpenSSL::errno, $OpenCA::OpenSSL::errval) if (not defined $ret); | ||
| 224 | return $ret; | ||
| 225 | } | ||
| 226 | |||
| 227 | 1; |