## OpenCA - CA Command
## (c) 1998-2001 by Massimiliano Pala and OpenCA Group
##
##   File Name: bpRecoverKey
##     Version: $Revision: 1.5 $
##       Brief: recover Keypairs for users
## Description: 
##  Parameters:

sub cmdBpRecoverKey {

print startLogPage (gettext ("Keyrecovery for Batchprocessor's User"));

## datadirectory for the batchprocessor
my $batch_dir  = getRequired ("BP_DIR");

## build the directory path for the ID
my $id_dir = $batch_dir;
my $h_id   = $query->param ('id');
while ($h_id !~ /^$/) {
    $id_dir .= "/".substr $h_id, 0, 1;
    $h_id =~ s/^.//;
}

## decrypt the key
my $msg = $cryptoShell->getSMIME (
                                  DECRYPT    => 1,
                                  CERT       => getRequired ("KEY_BACKUP_CERTIFICATE"),
                                  KEY        => getRequired ("KEY_BACKUP_KEY"),
                                  USE_ENGINE => 1,
                                  PASSWD     => $query->param ('passwd'),
                                  INFILE     => $id_dir."/keybackup/".$query->param ('key_digest').".msg",
                                  );
if (not $msg) {
    print addPreLogLine ("<FONT COLOR=#FF0000>".
            i18nGettext (
              "Cannot recover key because the decryption of the private key failed - ID __ID__ ignored.",
              "__ID__", $tools->getFile ($dir."/ID")).
        "</FONT>");
} else {
    print addPreLogLine ($msg);
}

print addPreLogLine ("");
print addLogLine (gettext ("Keyreovery finished"));
print closeLogSection ();

}

1;
