codice:
public String firmaFile(String pathSource, String pathDest, String code_pin, String driver, String slot)
{
String returnValue = NO_ERROR;
CryptoEngine engine = null;
ISignMessage smsg = null;
String signedFile = pathDest;
BufferedInputStream bis = null;
FileOutputStream fos = null;
CryptoDriver cd = new CryptoDriver();
try {
lh.debugOperazioni(LOGGER_PREFIX, "Inizio procedura di firma", this.getClass().getName(), "firmaFile()", Level.INFO_INT);
// impostazioni della modalita' di firma
cd.setSignatureType(CryptoDriver.LEGAL_SIGNATURE);
// aggiunta della data di firma
cd.setAddSigningTime(true);
engine = new HardwareEngine(code_pin, driver, Integer.parseInt(slot));
// lettura delle chiavi disponibili
engine.open();
lh.debugOperazioni(LOGGER_PREFIX, "Login al dispositivo completato", this.getClass().getName(), "firmaFile()", Level.INFO_INT);
// info sul KeyStore
lh.debugOperazioni(LOGGER_PREFIX, "Il KeyStore contiene i seguenti alias:", this.getClass().getName(), "firmaFile()", Level.INFO_INT);
int m=1;
for (Enumeration<String> labels = engine.getSigningIDs(); labels.hasMoreElements();) {
lh.debugOperazioni(LOGGER_PREFIX, (m++)+" - "+labels.nextElement(), this.getClass().getName(), "firmaFile()", Level.INFO_INT);
}
lh.debugOperazioni(LOGGER_PREFIX, "Il KeyStore contiene i seguenti certificati:", this.getClass().getName(), "firmaFile()", Level.INFO_INT);
int mc=1;
for (String cn : engine.getSigningCNs()) {
lh.debugOperazioni(LOGGER_PREFIX, (mc++)+" - "+cn, this.getClass().getName(), "firmaFile()", Level.INFO_INT);
}
// firma vera e propria
//smsg = SignMessageFactory.getFactoryInstance().createISignMessage(cd, engine);
//smsg = SecurityRegistry.getInstance().getFactory(cd).createISignMessage(cd, engine);
smsg = SignMessageFactory.getInstance().createISignMessage(cd, engine);
// Impostazione input e output streams e creazione del messaggio
bis = new BufferedInputStream(new FileInputStream(pathSource));
smsg.setInputStream(bis);
File fo = new File(signedFile);
if (fo.exists()) fo.delete();
fos = new FileOutputStream(fo);
smsg.setOutputStream(fos);
smsg.encode(cd);
smsg.close();
fos.flush();
lh.debugOperazioni(LOGGER_PREFIX, "Firma del file completata", this.getClass().getName(), "firmaFile()", Level.INFO_INT);
}
catch(Exception e)
{
lh.debugOperazioni(LOGGER_PREFIX, "JAVA Exception : " + e.getMessage(), this.getClass().getName(), "firmaFile()", Level.ERROR_INT);
String msgErr = e.getMessage();
if(msgErr.indexOf(PIN_ERRATO)>-1)
{
returnValue = PIN_ERRATO;
}
else if(msgErr.indexOf(NO_TOKEN)>-1)
{
returnValue = NO_TOKEN;
}
else if(msgErr.indexOf(INITIALIZATION_FAIL)>-1)
{
returnValue = NO_TOKEN;
}
else if(msgErr.indexOf(NO_INSERT)>-1)
{
returnValue = NO_TOKEN;
}
else
{
returnValue = msgErr;
}
}
finally
{
if (bis != null)
{
try {bis.close();} catch (IOException ie) {;}
}
if (fos != null)
{
try {fos.close();} catch (IOException ie) {;}
}
File fo = new File(signedFile);
if (fo.length() == 0) fo.delete();
try { engine.close(); } catch(Exception e) {;}
}
lh.debugOperazioni(LOGGER_PREFIX, "Esito firma : " + returnValue, this.getClass().getName(), "firmaFile()", Level.INFO_INT);
return returnValue;
}