Token/OpenSC.pm untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 215 CC 18 SCAR 31.7
CC
18
Cov
0%
genKey
72.0
new
20.0
setError
12.0
CRAP 23.7
1 ## OpenCA::Token::OpenSC.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 OpenSC token too
29 ## errorcodes 714* 71 -> token ; 4 -> fourth implemented token
30  
31 package OpenCA::Token::OpenSC;
32  
33 use OpenCA::OpenSSL;
34  
35 our ($errno, $errval);
36 our $AUTOLOAD;
37  
38 ($OpenCA::Token::OpenSC::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 OpenSC token
43 sub new {
44 my $that = shift;
45 my $class = ref($that) || $that;
46  
47 my $self = {
48 DEBUG => 0,
49 ## debug_msg => ()
50 };
51  
52 bless $self, $class;
53 $self->{ONLINE} = 0;
54  
55 my $keys = { @_ };
56 $self->{DEBUG} = $keys->{DEBUG};
57 $self->{gettext} = $keys->{GETTEXT};
58 $self->{CRYPTO} = $keys->{OPENCA_CRYPTO};
59 $self->{NAME} = $keys->{OPENCA_TOKEN};
60 $self->{PASSWD_PARTS} = $keys->{PASSWD_PARTS};
61 delete $keys->{OPENCA_CRYPTO};
62 delete $keys->{OPENCA_TOKEN};
63 delete $keys->{PASSWD_PARTS};
64  
65 ## daemon mode etc. is not supported for OpenSC until now
66 #$self->{MODE} = $keys->{TOKEN_MODE};
67  
68 ## this is the direct OpenSC stuff
69 ## FIXME: pkcs11-tool is unusable because the keylength is hardcoded
70 $self->{CARDDRIVER} = $keys->{CARDDRIVER};
71 $self->{CARDREADER} = $keys->{CARDREADER};
72 $self->{PKCS15_INIT} = $keys->{PKCS15_INIT};
73 $self->{PKCS15_TOOL} = $keys->{PKCS15_TOOL};
74 $self->{OPENSC_TOOL} = $keys->{OPENSC_TOOL};
75  
76 return undef if (not $self->{CRYPTO});
77 return undef if (not $self->{NAME});
78  
79 $keys->{DYNAMIC_ENGINE} = 1;
80 $keys->{GET_PIN_CALLBACK} = \&_get_pin_callback;
81 $keys->{STDOUT_CALLBACK} = \&_stdout_callback;
82 $keys->{CALLBACK_HANDLER} = $self;
83  
84 ## create openssl object
85 $self->debug ("new: initializing OpenCA::OpenSSL");
86 $self->{OPENSSL} = OpenCA::OpenSSL->new ( %{$keys} );
87 if (not $self->{OPENSSL})
88 {
89 $self->setError ($OpenCA::OpenSSL::errno, $OpenCA::OpenSSL::errval);
90 return undef;
91 }
92 $self->debug ("new: NAME ".$self->{NAME});
93 $self->debug ("new: PASSWD_PARTS ".$self->{PASSWD_PARTS});
94 $self->debug ("new: initialized OpenCA::OpenSSL");
95  
96 return $self;
97 }
98  
99 sub setError {
100 my $self = shift;
101  
102 if (scalar (@_) == 4) {
103 my $keys = { @_ };
104 $self->{errno} = $keys->{ERRNO};
105 $self->{errval} = $keys->{ERRVAL};
106 } else {
107 $self->{errno} = $_[0];
108 $self->{errval} = $_[1];
109 }
110  
111 $errno = $self->{errno};
112 $errval = $self->{errval};
113  
114 return undef if (not $errno);
115  
116 print STDERR "PKI Master Alert: OpenCA::Token::OpenSC error\n";
117 print STDERR "PKI Master Alert: Aborting all operations\n";
118 print STDERR "PKI Master Alert: Error: $errno\n";
119 print STDERR "PKI Master Alert: Message: $errval\n";
120 print STDERR "PKI Master Alert: debugging messages of empty token follow\n";
121  
122 ## support for: return $self->setError (1234, "Something fails.") if (not $xyz);
123 return undef;
124 }
125  
126 sub errno
127 {
128 my $self = shift;
129 return $self->{errno};
130 }
131  
132 sub errval
133 {
134 my $self = shift;
135 return $self->{errval};
136 }
137  
138 sub debug {
139 my $self = shift;
140 return 1 if (not $self->{DEBUG});
141 print STDERR "OpenCA::Token::OpenSC->".$_[0]."\n";
142 return 1;
143 }
144  
145 ## failover to default token OpenSSL which uses -engine
146 ## see new to get an idea what is going on
147 sub AUTOLOAD {
148 my $self = shift;
149  
150 if ($AUTOLOAD =~ /OpenCA::OpenSSL/)
151 {
152 print STDERR "PKI Master Alert: OpenCA::OpenSSL is missing a function\n";
153 print STDERR "PKI Master Alert: $AUTOLOAD\n";
154 $self->setError (666,
155 $self->{gettext} ("OpenCA::OpenSSL is missing a function. __FUNCTION__",
156 "__FUNCTION__", $AUTOLOAD));
157 return undef;
158 }
159 $self->debug ("OpenCA::Token::OpenSC: AUTOLOAD => $AUTOLOAD");
160  
161 return 1 if ($AUTOLOAD eq 'OpenCA::Token::OpenSC::DESTROY');
162  
163 my $function = $AUTOLOAD;
164 $function =~ s/.*:://g;
165 my $ret = $self->{OPENSSL}->$function ( @_ );
166 $self->setError ($OpenCA::OpenSSL::errno, $OpenCA::OpenSSL::errval);
167 return $ret;
168 }
169  
170 sub login {
171 my $self = shift;
172 my @result = ($self->{CRYPTO}->getAccessControl())->getTokenParam (
173 $self->{NAME},
174 $self->{PASSWD_PARTS});
175 $self->{PASSWD} = join '', @result;
176 $self->{OPENSSL}->{PASSWD} = $self->{PASSWD};
177  
178 ## FIXME: I do not know how to verify a PIN with OpenSC :(
179  
180 $self->{ONLINE} = 1;
181 return 1;
182 }
183  
184 sub logout {
185 my $self = shift;
186 undef $self->{PASSWD};
187 undef $self->{OPENSSL}->{PASSWD};
188 $self->{ONLINE} = 0;
189 return 1;
190 }
191  
192 sub online {
193 return 1;
194 }
195  
196 sub keyOnline {
197 my $self = shift;
198 return undef if (not $self->{ONLINE});
199 return 1;
200 }
201  
202 sub getMode {
203 return "standby";
204 }
205  
206 sub genKey {
207 my $self = shift;
208  
209 my $keys = { @_ };
210 my $command;
211 my $ret;
212  
213 return $self->setError (7144012,
214 $self->{gettext} ("You try to generate a key for an OpenSC token but you don't specify the number of bits."))
215 if (not $keys->{BITS});
216 return $self->setError (7144013,
217 $self->{gettext} ("You must specify a passphrase."))
218 if (not $self->{PASSWD});
219  
220 ## FIXME
221 ##
222 ## actually we cannot use PKCS#11 here because pkcs11-tool uses a fixed
223 ## keylength what is inacceptable for OpenCA
224  
225 ## Let's describe what we are doing:
226 ## 1. reader present
227 ## 2. card ready and empty
228 ## 3. erase card
229 ## 4. init pkcs#15 stuff
230 ## 5. generate private key
231  
232 ## 1. reader present
233  
234 $command = $self->{OPENSC_TOOL}." -l";
235 $self->debug ("genReq: 1. step: $command");
236 $ret = `$command`;
237 if ($ret !~ /\n0\s+pcsc\s/)
238 {
239 $self->setError (7144020, $self->{gettext} ("No PCSC reader found."));
240 return undef;
241 }
242  
243 ## 2. card ready and empty
244  
245 $command = "$self->{PKCS15_TOOL} --list-keys --reader $self->{CARDREADER}";
246 $self->debug ("genReq: 2. step: $command");
247 $ret = `$command`;
248 if ($ret =~ /Private RSA Key \[Private Key\]/)
249 {
250 $self->setError (7144030, $self->{gettext} ("Card is not empty."));
251 return undef;
252 }
253  
254 ## 3. erase card
255  
256 $command = "$self->{PKCS15_INIT} --erase-card -r $self->{CARDREADER} --use-default-transport-keys";
257 $self->debug ("genReq: 3. step: $command");
258 $ret = `$command`;
259 if (not $ret)
260 {
261 $self->setError (7144035, $self->{gettext} ("Cannot erase card."));
262 return undef;
263 }
264
265 ## 4. init pkcs#15 stuff
266  
267 $command = "$self->{PKCS15_INIT} --pin $self->{PASSWD} ".
268 "--puk $self->{PASSWD} ".
269 "--no-so-pin ".
270 "--store-pin ".
271 "--create-pkcs15 ".
272 "--auth-id 0 ".
273 "-r $self->{CARDREADER} ".
274 "--use-default-transport-keys";
275 $self->debug ("genReq: 4. step: $command");
276 $ret = `$command`;
277 if (not $ret)
278 {
279 $self->setError (7144040, $self->{gettext} ("Cannot initialize PKCS#15 struicture on card."));
280 return undef;
281 }
282  
283 ## 5. generate private key
284  
285 $command = "$self->{PKCS15_INIT} --generate-key rsa$keys->{BITS} ".
286 "--auth-id 0 ".
287 "-r $self->{CARDREADER} ".
288 "--pin $self->{PASSWD} ".
289 "--key-usage sign,decrypt";
290 $self->debug ("genReq: 5. step: $command");
291 $ret = `$command`;
292 if (not $ret)
293 {
294 $self->setError (7144045, $self->{gettext} ("Keygeneration on card failed."));
295 return undef;
296 }
297  
298 return 1;
299 }
300  
301 sub _get_pin_callback
302 {
303 my $self = shift;
304  
305 ## if the user forget to ask for the PIN via login
306 ## then we cannot handle it here
307  
308 if ($self->{PASSWD})
309 {
310 return "-pre PIN:".$self->{PASSWD};
311 } else {
312 return "";
313 }
314 }
315  
316 sub _stdout_callback
317 {
318 my $self = shift;
319 my $result = shift;
320  
321 ## remove leading OpenSSL> from engine call
322 $result =~ s/^OpenSSL>\s//;
323 ## remove all until OpenSSL>
324 $result = substr $result, index $result, "OpenSSL> ";
325  
326 return $result
327 }
328  
329 1;