4
4
package it .libersoft .firmapiud .dbusinterface ;
5
5
6
6
import it .libersoft .firmapiu .Data ;
7
+ import it .libersoft .firmapiu .Report ;
7
8
import it .libersoft .firmapiu .ResultInterface ;
8
9
//import it.libersoft.firmapiu.DataFilePath;
9
10
//import it.libersoft.firmapiu.GenericArgument;
13
14
import it .libersoft .firmapiu .cades .P7FileCommandInterface ;
14
15
import it .libersoft .firmapiu .exception .FirmapiuException ;
15
16
import it .libersoft .firmapiu .cades .CommandProxyInterface ;
17
+ import it .libersoft .firmapiu .crtoken .KeyStoreToken ;
16
18
import it .libersoft .firmapiu .crtoken .PKCS11Token ;
17
19
import it .libersoft .firmapiu .crtoken .TokenFactoryBuilder ;
18
20
import it .libersoft .firmapiu .data .DataFactoryBuilder ;
23
25
24
26
import java .io .File ;
25
27
import java .lang .reflect .Type ;
28
+ import java .util .ArrayList ;
26
29
import java .util .Iterator ;
30
+ import java .util .List ;
27
31
import java .util .Locale ;
28
32
import java .util .Map ;
29
33
import java .util .ResourceBundle ;
30
34
import java .util .Set ;
31
35
import java .util .TreeMap ;
32
36
import java .util .TreeSet ;
33
37
38
+ import org .bouncycastle .cms .SignerInformation ;
34
39
import org .freedesktop .dbus .Struct ;
35
40
import org .freedesktop .dbus .Variant ;
36
41
import org .freedesktop .dbus .exceptions .DBusExecutionException ;
37
42
import org .freedesktop .dbus .types .DBusStructType ;
38
43
39
44
/**
45
+ *
40
46
* @author dellanna
41
47
*
42
48
*/
43
49
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
+
45
52
//FIXME da cambiare nel momento in cui si riscrive libreria
46
53
private final CommandProxyInterface cmdInterface ;
47
54
//interfaccia di comandi specializzata per la gestione di file .p7m .p7s
@@ -54,6 +61,9 @@ public final class FirmapiuDImpl implements FirmapiuDInterface {
54
61
55
62
private int prova ;
56
63
64
+ //keystore Token contente le CA usate come trust anchor
65
+ private KeyStoreToken tslKeystoreToken ;
66
+
57
67
public FirmapiuDImpl () {
58
68
super ();
59
69
ResourceBundle rb = ResourceBundle .getBundle ("it.libersoft.firmapiud.lang.locale" ,Locale .getDefault ());
@@ -62,6 +72,12 @@ public FirmapiuDImpl() {
62
72
this .cmdInterface =new CommandProxyInterface (rb );
63
73
//this.p7CommandInterface = CadesBESFactory.getFactory().getP7FileCommandInterface();
64
74
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
+ }
65
81
}
66
82
67
83
/* (non-Javadoc)
@@ -142,38 +158,93 @@ public Map<String, Variant<?>> sign(Variant<?>[] args,
142
158
*/
143
159
@ Override
144
160
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" ));
152
203
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
+ }
154
233
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 ());
165
243
}
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
177
248
178
249
@ Override
179
250
public Map <String , Variant <?>> getContentSignedData (Variant <?>[] args ,
@@ -197,7 +268,6 @@ public Map<String, Variant<?>> getContentSignedData(Variant<?>[] args,
197
268
}
198
269
199
270
//PROCEDURE PRIVATE
200
- //TODO ricorda di definire nella documentazione le opzioni messe a disposizione e i tipi che dbus associa a queste opzioni
201
271
202
272
//effettua l'unmarshalling delle opzioni ricevute in maniera tale da convertire il formato del valore delle opzioni
203
273
//ricevute da dbus in quello da passare alla libreria
@@ -222,22 +292,27 @@ public Map<String, Variant<?>> getContentSignedData(Variant<?>[] args,
222
292
223
293
//fa l'unmashalling dei parametri riveuti in ingresso
224
294
//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 ){
229
296
if (args ==null || args .length ==0 )
230
297
throw new DBusExecutionException (localrb .getString ("error0" ));
231
- //DataFile dataFile= DataFactoryBuilder.getFactory(DATAFILEFACTORY).getDataFile();
232
298
for (Variant <?> arg : args ){
233
299
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 ()+">" );
236
305
} catch (FirmapiuException e ) {
237
306
e .printStackTrace ();
238
307
throw new DBusExecutionException (localrb .getString ("error0" )+" : <" +e .errorCode +"> " +e .getLocalizedMessage ());
239
308
}
240
309
}
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 ){
241
316
//setta le opzioni
242
317
//GenericArgument arguments=null;
243
318
String pin =null ;
@@ -248,7 +323,7 @@ private String unmarshallDataFile(DataFile dataFile, Variant<?>[] args,Map<Strin
248
323
//deve fare l'unmarshalling delle opzioni da quelle ricevute in ingresso a quelle richieste da sign
249
324
String key =itr .next ();
250
325
//il pin deve essere salvato a parte
251
- String value = unmarshallOptions (options .get (key ));
326
+ String value = unmarshallOptionsProcedure (options .get (key ));
252
327
if (key .equals (TOKENPIN ))
253
328
pin =value ;
254
329
else
@@ -260,11 +335,11 @@ private String unmarshallDataFile(DataFile dataFile, Variant<?>[] args,Map<Strin
260
335
}
261
336
}
262
337
}
263
-
264
338
return pin ;
265
- }//fine metodo
339
+ }
266
340
267
- private String unmarshallOptions (Variant <?> value ){
341
+ //procedura privata eseguita durante l'unmarshalling delle opzioni
342
+ private String unmarshallOptionsProcedure (Variant <?> value ){
268
343
//i tipi dei valori possono essere solo basic types (Stringhe e Boolean)
269
344
if (value .getSig ().equals ("s" )){
270
345
return (String )(value .getValue ());
@@ -275,6 +350,65 @@ private String unmarshallOptions(Variant<?> value){
275
350
throw new DBusExecutionException (localrb .getString ("error0" ));
276
351
}
277
352
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
+
278
412
//fa il marshalling dei risultati ottenuti in uscita
279
413
private Map <String ,Variant <?>> marshallFile (ResultInterface <File , File >result ){
280
414
//effettua il marshalling dei risultati da inviare a dbus
@@ -284,7 +418,7 @@ private Map<String,Variant<?>> marshallFile(ResultInterface<File, File>result){
284
418
itr = result .getResultDataSet ().iterator ();
285
419
} catch (FirmapiuException e ) {
286
420
e .printStackTrace ();
287
- throw new DBusExecutionException (localrb .getString ("error3f " )+" : " +e .getLocalizedMessage ());
421
+ throw new DBusExecutionException (localrb .getString ("error3m " )+" : " +e .getLocalizedMessage ());
288
422
}
289
423
while (itr .hasNext ()){
290
424
File keyFile =itr .next ();
@@ -303,4 +437,14 @@ private Map<String,Variant<?>> marshallFile(ResultInterface<File, File>result){
303
437
304
438
return dbusResult ;
305
439
}
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
+ }
306
450
}
0 commit comments