Skip to content

Commit a4789f3

Browse files
committed
Implementazione operazione di verifica per singolo file
1 parent 79c008d commit a4789f3

File tree

3 files changed

+194
-43
lines changed

3 files changed

+194
-43
lines changed

src/main/java/it/libersoft/firmapiud/dbusinterface/FirmapiuDImpl.java

Lines changed: 187 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package it.libersoft.firmapiud.dbusinterface;
55

66
import it.libersoft.firmapiu.Data;
7+
import it.libersoft.firmapiu.Report;
78
import it.libersoft.firmapiu.ResultInterface;
89
//import it.libersoft.firmapiu.DataFilePath;
910
//import it.libersoft.firmapiu.GenericArgument;
@@ -13,6 +14,7 @@
1314
import it.libersoft.firmapiu.cades.P7FileCommandInterface;
1415
import it.libersoft.firmapiu.exception.FirmapiuException;
1516
import it.libersoft.firmapiu.cades.CommandProxyInterface;
17+
import it.libersoft.firmapiu.crtoken.KeyStoreToken;
1618
import it.libersoft.firmapiu.crtoken.PKCS11Token;
1719
import it.libersoft.firmapiu.crtoken.TokenFactoryBuilder;
1820
import it.libersoft.firmapiu.data.DataFactoryBuilder;
@@ -23,25 +25,30 @@
2325

2426
import java.io.File;
2527
import java.lang.reflect.Type;
28+
import java.util.ArrayList;
2629
import java.util.Iterator;
30+
import java.util.List;
2731
import java.util.Locale;
2832
import java.util.Map;
2933
import java.util.ResourceBundle;
3034
import java.util.Set;
3135
import java.util.TreeMap;
3236
import java.util.TreeSet;
3337

38+
import org.bouncycastle.cms.SignerInformation;
3439
import org.freedesktop.dbus.Struct;
3540
import org.freedesktop.dbus.Variant;
3641
import org.freedesktop.dbus.exceptions.DBusExecutionException;
3742
import org.freedesktop.dbus.types.DBusStructType;
3843

3944
/**
45+
*
4046
* @author dellanna
4147
*
4248
*/
4349
public final class FirmapiuDImpl implements FirmapiuDInterface {
44-
50+
//TODO ricorda di definire nella documentazione le opzioni messe a disposizione e i tipi che dbus associa a queste opzioni
51+
4552
//FIXME da cambiare nel momento in cui si riscrive libreria
4653
private final CommandProxyInterface cmdInterface;
4754
//interfaccia di comandi specializzata per la gestione di file .p7m .p7s
@@ -54,6 +61,9 @@ public final class FirmapiuDImpl implements FirmapiuDInterface {
5461

5562
private int prova;
5663

64+
//keystore Token contente le CA usate come trust anchor
65+
private KeyStoreToken tslKeystoreToken;
66+
5767
public FirmapiuDImpl() {
5868
super();
5969
ResourceBundle rb = ResourceBundle.getBundle("it.libersoft.firmapiud.lang.locale",Locale.getDefault());
@@ -62,6 +72,12 @@ public FirmapiuDImpl() {
6272
this.cmdInterface=new CommandProxyInterface(rb);
6373
//this.p7CommandInterface = CadesBESFactory.getFactory().getP7FileCommandInterface();
6474
this.prova=0;
75+
//keystore token di una lista di CA fidate definite in una Trusted Store List.
76+
try {
77+
this.tslKeystoreToken=TokenFactoryBuilder.getFactory(KEYSTORETOKENFACTORY).getKeyStoreToken(TSLXMLKEYSTORE);
78+
} catch (FirmapiuException e) {
79+
e.printStackTrace();
80+
}
6581
}
6682

6783
/* (non-Javadoc)
@@ -142,38 +158,93 @@ public Map<String, Variant<?>> sign(Variant<?>[] args,
142158
*/
143159
@Override
144160
public Map<String,String> verify(Variant<?>[] args) {
145-
//FIXME da cambiare quando si cambia libreria
146-
//prepara i parametri da passare a firmapiulib
147-
if (args==null || args.length==0)
148-
throw new DBusExecutionException(localrb.getString("error0"));
149-
Set<String> commandargs=new TreeSet<String>();
150-
for(Variant<?> arg : args)
151-
commandargs.add((String)arg.getValue());
161+
// //FIXME da cambiare quando si cambia libreria
162+
// //prepara i parametri da passare a firmapiulib
163+
// if (args==null || args.length==0)
164+
// throw new DBusExecutionException(localrb.getString("error0"));
165+
// Set<String> commandargs=new TreeSet<String>();
166+
// for(Variant<?> arg : args)
167+
// commandargs.add((String)arg.getValue());
168+
//
169+
// Map<String,?> result=this.cmdInterface.verify(commandargs, null);
170+
//
171+
// //effettua il marshalling dei risultati da inviare a dbus
172+
// Map<String,String> dbusResult = new TreeMap<String,String>();
173+
// Iterator<String> itr=result.keySet().iterator();
174+
// while(itr.hasNext()){
175+
// String key=itr.next();
176+
// Object oldValue=result.get(key);
177+
// String newValue;
178+
// if(oldValue instanceof Boolean)
179+
// {
180+
// newValue=new String(localrb.getString("verify0")+" : "+(Boolean)oldValue);
181+
// }
182+
// else if(oldValue instanceof Exception)
183+
// {
184+
// //FIXME da fissare con struct
185+
// String str="666 : "+oldValue.getClass().getCanonicalName()+" : "+((Exception)oldValue).getLocalizedMessage();
186+
// newValue=str;
187+
// }else
188+
// throw new DBusExecutionException(localrb.getString("error3v")+" : "+localrb.getString("error4v"));
189+
// dbusResult.put(key, newValue);
190+
// }//fine while
191+
// return dbusResult;
192+
// TODO funzionalità di verifica di piu di un file non supportata: E' possibile che la funzionalità venga supportata in futuro
193+
throw new DBusExecutionException(localrb.getString("error5"));
194+
}
195+
196+
@Override
197+
public Map<String, Variant<?>>[] verifySingle(Variant<?> arg,
198+
Map<String, Variant<?>> options) {
199+
//TODO implementare un controllo meno rigido sul token usato per la verifica
200+
//recupera il token contenente il keystore delle "trust anchor" utilizzate per controllare affidabilità del firmatario
201+
if(this.tslKeystoreToken==null)
202+
throw new DBusExecutionException(localrb.getString("error3vt"));
152203

153-
Map<String,?> result=this.cmdInterface.verify(commandargs, null);
204+
//fa l'unmarshalling dei parametri di ingresso
205+
DataFile dataFile= DataFactoryBuilder.getFactory(DATAFILEFACTORY).getDataFile();
206+
unmarshallOptions(dataFile, options);
207+
//se è definita l'opzione detached ed è true deve verificare un p7s
208+
boolean detached=false;
209+
if(options!=null && options.containsKey(DETACHED)){
210+
Variant<?> val=options.get(DETACHED);
211+
if (val.getSig().equals("b")){
212+
detached=(Boolean)val.getValue();
213+
}
214+
}
215+
if(detached){
216+
//verifica un p7s: la variant passata come argomento deve essere ss
217+
//TODO da supportare
218+
throw new DBusExecutionException(localrb.getString("error5"));
219+
}//fine ramo p7s
220+
else{
221+
//verifica un p7m: la variant passata come argomento deve essere s
222+
//unmashall file da firmare
223+
try {
224+
if (arg.getSig().equals("s")){
225+
File file = new File((String)arg.getValue());
226+
dataFile.setData(file);
227+
} else
228+
throw new DBusExecutionException(localrb.getString("error0v")+" : <"+arg.getSig()+">");
229+
} catch (FirmapiuException e) {
230+
e.printStackTrace();
231+
throw new DBusExecutionException(localrb.getString("error0")+" : <"+e.errorCode+"> "+e.getLocalizedMessage());
232+
}
154233

155-
//effettua il marshalling dei risultati da inviare a dbus
156-
Map<String,String> dbusResult = new TreeMap<String,String>();
157-
Iterator<String> itr=result.keySet().iterator();
158-
while(itr.hasNext()){
159-
String key=itr.next();
160-
Object oldValue=result.get(key);
161-
String newValue;
162-
if(oldValue instanceof Boolean)
163-
{
164-
newValue=new String(localrb.getString("verify0")+" : "+(Boolean)oldValue);
234+
//crea l'interfaccia di comando e verifica la busta crittografica passata come parametro
235+
ResultInterface<File,Report> result=null;
236+
237+
P7FileCommandInterface p7CommandInterface=CadesBESFactory.getFactory().getP7FileCommandInterface(null,this.tslKeystoreToken);
238+
try {
239+
result=p7CommandInterface.verify(dataFile);
240+
} catch (FirmapiuException e) {
241+
e.printStackTrace();
242+
throw new DBusExecutionException(localrb.getString("error3v")+" : "+e.getLocalizedMessage());
165243
}
166-
else if(oldValue instanceof Exception)
167-
{
168-
//FIXME da fissare con struct
169-
String str="666 : "+oldValue.getClass().getCanonicalName()+" : "+((Exception)oldValue).getLocalizedMessage();
170-
newValue=str;
171-
}else
172-
throw new DBusExecutionException(localrb.getString("error3v")+" : "+localrb.getString("error4v"));
173-
dbusResult.put(key, newValue);
174-
}//fine while
175-
return dbusResult;
176-
}
244+
245+
return marshallVerifyResult(result);
246+
}//fine ramo p7m
247+
}//fine metodo
177248

178249
@Override
179250
public Map<String, Variant<?>> getContentSignedData(Variant<?>[] args,
@@ -197,7 +268,6 @@ public Map<String, Variant<?>> getContentSignedData(Variant<?>[] args,
197268
}
198269

199270
//PROCEDURE PRIVATE
200-
//TODO ricorda di definire nella documentazione le opzioni messe a disposizione e i tipi che dbus associa a queste opzioni
201271

202272
//effettua l'unmarshalling delle opzioni ricevute in maniera tale da convertire il formato del valore delle opzioni
203273
//ricevute da dbus in quello da passare alla libreria
@@ -222,22 +292,27 @@ public Map<String, Variant<?>> getContentSignedData(Variant<?>[] args,
222292

223293
//fa l'unmashalling dei parametri riveuti in ingresso
224294
//restituisce il pin (può essere uguale a null)
225-
private String unmarshallDataFile(DataFile dataFile, Variant<?>[] args,Map<String, Variant<?>> options ){
226-
//FIXME da cambiare nel momento in cui si riscrive libreria
227-
//linka l'implementazione concreta del demone alla libreria firmapiulib
228-
//prepara i parametri da passare a firmapiulib
295+
private String unmarshallDataFile(DataFile dataFile, Variant<?>[] args,Map<String, Variant<?>> options){
229296
if (args==null || args.length==0)
230297
throw new DBusExecutionException(localrb.getString("error0"));
231-
//DataFile dataFile= DataFactoryBuilder.getFactory(DATAFILEFACTORY).getDataFile();
232298
for(Variant<?> arg : args){
233299
try {
234-
File file = new File((String)arg.getValue());
235-
dataFile.setData(file);
300+
if (arg.getSig().equals("s")){
301+
File file = new File((String)arg.getValue());
302+
dataFile.setData(file);
303+
} else
304+
throw new DBusExecutionException(localrb.getString("error0v")+" : <"+arg.getSig()+">");
236305
} catch (FirmapiuException e) {
237306
e.printStackTrace();
238307
throw new DBusExecutionException(localrb.getString("error0")+" : <"+e.errorCode+"> "+e.getLocalizedMessage());
239308
}
240309
}
310+
311+
return unmarshallOptions(dataFile, options);
312+
}//fine metodo
313+
314+
//fa l'unmarshalling delle opzioni associate al comando
315+
private String unmarshallOptions(DataFile dataFile,Map<String, Variant<?>> options){
241316
//setta le opzioni
242317
//GenericArgument arguments=null;
243318
String pin=null;
@@ -248,7 +323,7 @@ private String unmarshallDataFile(DataFile dataFile, Variant<?>[] args,Map<Strin
248323
//deve fare l'unmarshalling delle opzioni da quelle ricevute in ingresso a quelle richieste da sign
249324
String key=itr.next();
250325
//il pin deve essere salvato a parte
251-
String value = unmarshallOptions(options.get(key));
326+
String value = unmarshallOptionsProcedure(options.get(key));
252327
if (key.equals(TOKENPIN))
253328
pin=value;
254329
else
@@ -260,11 +335,11 @@ private String unmarshallDataFile(DataFile dataFile, Variant<?>[] args,Map<Strin
260335
}
261336
}
262337
}
263-
264338
return pin;
265-
}//fine metodo
339+
}
266340

267-
private String unmarshallOptions(Variant<?> value){
341+
//procedura privata eseguita durante l'unmarshalling delle opzioni
342+
private String unmarshallOptionsProcedure(Variant<?> value){
268343
//i tipi dei valori possono essere solo basic types (Stringhe e Boolean)
269344
if(value.getSig().equals("s")){
270345
return (String)(value.getValue());
@@ -275,6 +350,65 @@ private String unmarshallOptions(Variant<?> value){
275350
throw new DBusExecutionException(localrb.getString("error0"));
276351
}
277352

353+
//fa il mashalling del risultato ottenuto dall'operazione di verifica
354+
public Map<String, Variant<?>>[] marshallVerifyResult(ResultInterface<File, Report> result){
355+
//recupera il report associato all'unico risultato che dovrebbe essere stato ottenuto
356+
Set<File> resultSet;
357+
try {
358+
resultSet = result.getResultDataSet();
359+
} catch (FirmapiuException e3) {
360+
e3.printStackTrace();
361+
throw new DBusExecutionException(localrb.getString("error4v")+" : "+e3.getLocalizedMessage());
362+
}
363+
if(resultSet.size()!=1)
364+
throw new IllegalArgumentException("Abbiamo un problema huston");
365+
File keyFile=resultSet.iterator().next();
366+
Report verifyReport;
367+
try {
368+
verifyReport = result.getResult(keyFile);
369+
} catch (FirmapiuException e3) {
370+
e3.printStackTrace();
371+
throw new DBusExecutionException(localrb.getString("error3v")+" : "+e3.getLocalizedMessage());
372+
}
373+
//recupera la lista dei firmatari e crea la struttura dati in risposta da inviare a Dbus
374+
List<SignerInformation> signerList;
375+
try {
376+
signerList = verifyReport.getSigners();
377+
} catch (FirmapiuException e2) {
378+
e2.printStackTrace();
379+
throw new DBusExecutionException(localrb.getString("error4v")+" : "+e2.getLocalizedMessage());
380+
}
381+
ArrayList<TreeMap<String, Variant<?>>> dbusResultList = new ArrayList<TreeMap<String, Variant<?>>>();
382+
Iterator<SignerInformation> signerListItr = signerList.iterator();
383+
while(signerListItr.hasNext()){
384+
SignerInformation signer=signerListItr.next();
385+
Set<String> signerRecordFields;
386+
try {
387+
signerRecordFields = verifyReport.getSignerRecordFields(signer);
388+
} catch (FirmapiuException e1) {
389+
e1.printStackTrace();
390+
throw new DBusExecutionException(localrb.getString("error4v")+" : "+e1.getLocalizedMessage());
391+
}
392+
TreeMap<String, Variant<?>> dbusRecord= new TreeMap<String, Variant<?>>();
393+
Iterator<String> fieldItr= signerRecordFields.iterator();
394+
while(fieldItr.hasNext()){
395+
String field=fieldItr.next();
396+
try {
397+
Object fieldValue = verifyReport.getSignerField(signer, field);
398+
dbusRecord.put(field,obj2Variant(fieldValue));
399+
} catch (FirmapiuException e) {
400+
// in caso di eccezione aggiunge l'errore come Variant (is)
401+
e.printStackTrace();
402+
FirmapiuExceptionStruct struct = new FirmapiuExceptionStruct(e.errorCode,e.getLocalizedMessage());
403+
Variant<?> newValue=new Variant<FirmapiuExceptionStruct>(struct,FirmapiuExceptionStruct.class);
404+
dbusRecord.put(field,obj2Variant(newValue));
405+
}
406+
}
407+
dbusResultList.add(dbusRecord);
408+
}
409+
return (Map<String, Variant<?>>[])dbusResultList.toArray();
410+
}
411+
278412
//fa il marshalling dei risultati ottenuti in uscita
279413
private Map<String,Variant<?>> marshallFile(ResultInterface<File, File>result){
280414
//effettua il marshalling dei risultati da inviare a dbus
@@ -284,7 +418,7 @@ private Map<String,Variant<?>> marshallFile(ResultInterface<File, File>result){
284418
itr = result.getResultDataSet().iterator();
285419
} catch (FirmapiuException e) {
286420
e.printStackTrace();
287-
throw new DBusExecutionException(localrb.getString("error3f")+" : "+e.getLocalizedMessage());
421+
throw new DBusExecutionException(localrb.getString("error3m")+" : "+e.getLocalizedMessage());
288422
}
289423
while(itr.hasNext()){
290424
File keyFile=itr.next();
@@ -303,4 +437,14 @@ private Map<String,Variant<?>> marshallFile(ResultInterface<File, File>result){
303437

304438
return dbusResult;
305439
}
440+
441+
//trasforma un oggetto in una variant
442+
private static Variant<?> obj2Variant(Object obj){
443+
//per il momento gestisce solo boolean e string
444+
if(obj instanceof String)
445+
return new Variant<String>((String)obj,"s");
446+
if(obj instanceof Boolean)
447+
return new Variant<Boolean>((Boolean)obj,"b");
448+
throw new IllegalArgumentException("Cannot tranform Object to Variant!");
449+
}
306450
}

src/main/java/it/libersoft/firmapiud/dbusinterface/FirmapiuDInterface.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public interface FirmapiuDInterface extends DBusInterface {
2424

2525
//verifica la firma dei file passati come parametro
2626
public Map<String,String> verify (Variant<?>[] args);
27+
28+
//verifica un singolo file passato come parametro: restituisce un array di dictionaries
29+
//contenente l'esito dell'operazione di verifica per ogni firmatario
30+
public Map<String,Variant<?>>[] verifySingle (Variant<?> arg, Map<String,Variant<?>> options);
2731

2832
//restituisce l'originale di un file contenuto in una busta crittografica p7m
2933
public Map<String,Variant<?>> getContentSignedData(Variant<?>[] args,Map<String,Variant<?>> options);

src/main/resources/it/libersoft/firmapiud/lang/locale.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ export3=Errore in fase di esportazione dell'oggetto
1717

1818
#FirmapiuImpl
1919
error0=I valori passati come parametro sono nulli o illegali: non posso procedere nell'operazione
20+
error0v=I valori passati come parametro sono nulli o illegali: firma Variant non riconosciuta
2021
error0p=I valori passati come parametro sono nulli o illegali: non posso procedere nell'operazione. Opzione non trovata
2122
error1=Opzione sconosciuta o valore dell'opzione illegale: non posso procedere nell'operazione
2223
error3f=E' stato rilevato un errore durante l'operazione di firma
2324
error3v=E' stato rilevato un errore durante l'operazione di verifica
25+
error3vt=E' stato rilevato un errore durante l'operazione di verifica: lista delle CA fidate non trovata.
2426
error3c=E' stato rilevato un errore durante l'operazione di recupero del contenuto originale dei dati presenti nella busta crittografica
27+
error3m=E' stato rilevato un errore durante il marshalling
2528
error4f=Mashalling dei risultati dell'operazione di firma fallito
2629
error4v=Mashalling dei risultati dell'operazione di verifica fallito
2730
verify0=L'esito della verifica è

0 commit comments

Comments
 (0)