-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecureDataContainer_Impl2.java
400 lines (314 loc) · 13.3 KB
/
SecureDataContainer_Impl2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import MyExceptions.*;
import java.util.*;
import static java.lang.System.exit;
@SuppressWarnings("unchecked") public class SecureDataContainer_Impl2<E extends MyCloneable>
implements SecureDataContainer<E>{
/**IR: Collection!=null
* && for all i.(0<=i<Collection.size()
* ==> (Collection.containsKey(User_i) <==> Collection.get(User_i) != null))
*
* FA: {<<getKey(User_i, XXX).getID(), XXX>,
* {<Encrypt(Collection.get(getKey(User_i, XXX)).get(j).getInfo(), 'D'),
* Collection.get(getKey(User_i, XXX)).get(j).getAuthorized()>}>}
*
* for all i.(0<=i<Collection.size()
* ==> User_i is the username of the i-th user of the collection
* && Collection.getKey(User_i, XXX).check(User_i, XXX)==true
**/
private HashMap<User, Vector<DataInformation>> Collection;
//Associa ad ogni utente una lista di dati
//Costruttore
public SecureDataContainer_Impl2(){
Collection=new HashMap<>();
}
//Restituisce il numero degli elementi di un utente presenti nella
//collezione
public int getSize(String Owner, String passw)
throws NullPointerException, IllegalArgumentException,
UserNotExists, WrongPassword{
//getKey effettua i controlli, occupandosi di sollevare le dovute eccezioni
//Questo metodo li solleva al sottoprogramma chiamante, non occupandosi della gestione
User u = getKey(Owner,passw);
return Collection.get(u).size();
}
//Restituisce il numero degli utenti autorizzati ad accedere ad un determinato dato
//Metodo aggiuntivo non richiesto
public int AuthorizedSize(String Owner, String passw, E data)
throws NullPointerException, IllegalArgumentException,
UserNotExists, WrongPassword, DataNotFound {
//getKey effettua i controlli, occupandosi di sollevare le dovute eccezioni
//Questo metodo li solleva al sottoprogramma chiamante, non occupandosi della gestione
User u = getKey(Owner,passw);
//Dato cifrato
E d = (E) Encrypt(data, 'E');
//Controllo esistenza del dato
int indiceDato = Collection.get(u).indexOf(new DataInformation(d));
if (indiceDato>=0)
return Collection.get(u).get(indiceDato).AuthorizedSize();
throw new DataNotFound("Dato non trovato in AuthorizedSize");
}
//Restituisce l'insieme di dati a cui un utente ha accesso in lettura
//Metodo aggiuntivo non richiesto
public Set<E> PrintAuthorizedData(String Owner, String passw)
throws NullPointerException, IllegalArgumentException,
UserNotExists, WrongPassword{
//getKey effettua i controlli, occupandosi di sollevare le dovute eccezioni
//Questo metodo li solleva al sottoprogramma chiamante, non occupandosi della gestione
User u = getKey(Owner,passw);
Set<E> set = new HashSet<>();
//Scansione lista di liste
for (Vector<DataInformation> i:
Collection.values()) {
//Scansione lista dati corrente
for (DataInformation d:
i) {
//Se Owner è autorizzato, aggiungi il dato decifrato all'insieme
if (d.getAuthorized().contains(u))
set.add((E) Encrypt((E) d.getInfo(), 'D'));
}
}
return set;
};
//Restituisce il numero di utenti nella struttura
//Metodo aggiuntivo non richiesto
public int UsersSize() throws NullPointerException{
if (Collection==null) throw new NullPointerException(
"*******************STATO INCONSISTENTE!!!*******************\n" +
"*******************TROVATA FALLA!!!*******************");
return Collection.size();
}
//Ottiene una copia del valore del dato nella collezione
//se vengono rispettati i controlli di identità
public E get(String Owner, String passw, E data)
throws NullPointerException, IllegalArgumentException,
NotAuthorized, WrongPassword, DataNotFound, UserNotExists{
//Controlla esistenza dato
boolean check=false;
for (Vector<DataInformation> v:
Collection.values()) {
if (v.contains(new DataInformation(Encrypt(data, 'e'))))
check=true;
}
if (check==false) throw new DataNotFound("Dato non trovato, impossibile restituirlo");
//getKey effettua i controlli, occupandosi di sollevare le dovute eccezioni
//Questo metodo li solleva al sottoprogramma chiamante, non occupandosi della gestione
User u = getKey(Owner,passw);
int indicedato;
Set s; //set d'appoggio
Iterator<E> it;
E tmp=null; //Valore da ritornare
E d = Encrypt(data, 'E'); //Dato cifrato per i confronti
if ((indicedato=Collection.get(u).indexOf(new DataInformation(d)))>=0) //Dato di owner
tmp= (E) Collection.get(u).get(indicedato).getInfo();
else if ((s=PrintAuthorizedData(Owner,passw)).contains(d)){ //Owner autorizzato
it = s.iterator();
while (it.hasNext()){
if ((tmp=it.next()).equals(d))
break;
}
}
else throw new NotAuthorized("Utente non autorizzato in metodo get");
return Encrypt(tmp, 'D');
};
//Crea l’identità un nuovo utente della collezione
public void createUser(String Id, String passw)
throws IllegalArgumentException, NullPointerException, UserAlreadyExists{
//Controlli di consistenza
if (this.Collection==null) throw new NullPointerException(
"*******************STATO INCONSISTENTE!!!*******************\n" +
"*******************TROVATA FALLA!!!*******************");
if (Id==null || passw==null) throw new IllegalArgumentException("Input null alla creazione dell'utente.");
//Unicità degli utenti
if (Collection.containsKey(new User(Id,passw))) throw new UserAlreadyExists("Utente già esistente.");
Collection.put(new User(Id,passw), new Vector<>() );
};
//Rimuove l’utente dalla collezione
public void RemoveUser(String Id, String passw)
throws NullPointerException, IllegalArgumentException,
UserNotExists, WrongPassword{
//getKey effettua i controlli, occupandosi di sollevare le dovute eccezioni
//Questo metodo li solleva al sottoprogramma chiamante, non occupandosi della gestione
User u = getKey(Id, passw);
Collection.remove(u);
};
//Inserisce il valore del dato nella collezione
//se vengono rispettati i controlli di identità
public boolean put(String Owner, String passw, E data)
throws NullPointerException, IllegalArgumentException,
UserNotExists, WrongPassword{
//getKey effettua i controlli, occupandosi di sollevare le dovute eccezioni
//Questo metodo li solleva al sottoprogramma chiamante, non occupandosi della gestione
User u = getKey(Owner,passw);
Collection.get(u).add(new DataInformation(Encrypt(data, 'E')));
if (Collection.get(u).contains(Encrypt(data, 'E'))) return true;
else return false;
};
//Rimuove il dato nella collezione
//se vengono rispettati i controlli di identità
public E remove(String Owner, String passw, E data)
throws NullPointerException, IllegalArgumentException,
UserNotExists, WrongPassword, DataNotFound{
//getKey effettua i controlli, occupandosi di sollevare le dovute eccezioni
//Questo metodo li solleva al sottoprogramma chiamante, non occupandosi della gestione
User u = getKey(Owner,passw);
//Controlla esistenza dato
if (!(Collection.get(u).contains(new DataInformation(Encrypt(data, 'E')))))
throw new DataNotFound("Dato non trovato, errore all'eliminazione.");
E tmp = (E) Collection.get(u).get(
Collection.get(u).indexOf(new DataInformation(Encrypt(data, 'e'))) //Indice dato
).getInfo();
int indice = Collection.get(u).indexOf(new DataInformation(Encrypt(data, 'e')));
return Encrypt((E) Collection.get(u).remove(indice).getInfo(), 'd');
};
//Crea una copia del dato nella collezione
//se vengono rispettati i controlli di identità
public void copy(String Owner, String passw, E data)
throws NullPointerException, IllegalArgumentException,
WrongPassword, UserNotExists, DataNotFound, NotAuthorized{
//getKey effettua i controlli, occupandosi di sollevare le dovute eccezioni
//Questo metodo li solleva al sottoprogramma chiamante, non occupandosi della gestione
User u = getKey(Owner, passw);
//Controlla esistenza dato
boolean contenuto = false;
int i;
E d=null;
for (Vector<DataInformation> v:
Collection.values()) {
if ((i=v.indexOf(new DataInformation(Encrypt(data, 'E'))))>=0) {
contenuto = true;
//Controlla autorizzazione
if (v.get(i).getAuthorized().contains(u.getID()))
//Aggiunge il dato già cifrato
// ed esce
Collection.get(u).add(new DataInformation(v.get(i).getInfo()));
return;
}
}
if (contenuto==false)
throw new DataNotFound("Dato non trovato, impossibile copiarlo.");
else throw new NotAuthorized("Utente non autorizzato all'operazione.");
};
//Condivide il dato nella collezione con un altro utente
//se vengono rispettati i controlli di identità
public void share(String Owner, String passw, String Other, E data)
throws NullPointerException, IllegalArgumentException,
WrongPassword, DataNotFound, UserNotExists, UserAlreadyExists{
//getKey effettua i controlli, occupandosi di sollevare le dovute eccezioni
//Questo metodo li solleva al sottoprogramma chiamante, non occupandosi della gestione
User u = getKey(Owner, passw);
//Esistenza utente other
if (!Collection.containsKey(new User(Other,"nonrilevante")))
throw new UserNotExists(Other + " non trovato.");
//Esistenza dato
if (Collection.get(u).contains(new DataInformation(Encrypt(data, 'E')))) {
//Presenza dell'utente tra gli autorizzati
boolean contenuto =
Collection.get(u).get(
Collection.get(u).indexOf(new DataInformation(Encrypt(data, 'e'))) //Indice dato
).getAuthorized().contains(Other);
if (!contenuto)
Collection.get(u).get(
Collection.get(u).indexOf(new DataInformation(Encrypt(data, 'E'))) //Indice dato
).Addlicense(Other);
else throw new UserAlreadyExists(Other+" già autorizzato a "+ data.toString());
}
else throw new DataNotFound(data.toString()+" non trovato, impossibile condividerlo.");
};
//Modifica il contenuto di un dato, se sono rispettati i controlli d'identità
//Metodo aggiuntivo non richiesto
public void modify(String Owner, String passw, E OldData, E NewData)
throws NullPointerException, IllegalArgumentException,
WrongPassword, UserNotExists, DataNotFound{
//getKey effettua i controlli, occupandosi di sollevare le dovute eccezioni
//Questo metodo li solleva al sottoprogramma chiamante, non occupandosi della gestione
User u = getKey(Owner, passw);
//Controlli sui dati
if (OldData==null || NewData == null)
throw new IllegalArgumentException("Dati input null, impossibile da modificare");
//Controlla esistenza dato
int indice;
try {
indice = Collection.get(u).indexOf(new DataInformation(Encrypt(OldData, 'E')));
}
catch (ArrayIndexOutOfBoundsException e) {throw new DataNotFound("Dato non esistente, impossibile modificarlo.");}
Collection.get(u).get(indice).setInfo(Encrypt(NewData, 'E'));
};
//Meccanismo di cifratura dati
public E Encrypt(E data, char op)
throws IllegalArgumentException, NullPointerException{
if (data==null) throw new IllegalArgumentException("dato null, non cifrabile");
switch (op) {
case ('d'):
case ('D'):
return (E) data.decrypt();
case ('e'):
case ('E'):
return (E) data.encrypt();
default:
throw new IllegalArgumentException("Operazione " + op + " non programmata");
}
};
//Metodo che permette di ricavare una copia dell'ID di un utente
//Metodo aggiuntivo non richiesto, implementato per l'AF
public User getKey(String name, String pass)
throws IllegalArgumentException, NullPointerException, UserNotExists, WrongPassword {
//Controlli di consistenza
if (this.Collection==null) throw new NullPointerException(
"*******************STATO INCONSISTENTE!!!*******************\n" +
"*******************TROVATA FALLA!!!*******************");
if (name==null|| pass==null) throw new IllegalArgumentException("Input null in getKey\n.");
//controllo esistenza chiave
if (Collection.containsKey(new User(name,pass))){
Set<User> s = Collection.keySet();
for (User u:
s) {
if (u.getID().equals(name)) {
//Controllo identità
if (u.check(name, pass)) {
//Restituisci chiave
return u;
} else
throw new WrongPassword("Password errata in getKey");
}
}
}
else throw new UserNotExists("Chiave non trovata in getKey.");
return null; //Sintatticamente necessario
};
//Restituisce un iteratore (senza remove) che genera tutti i dati
//dell’utente in ordine arbitrario
//se vengono rispettati i controlli di identità
public Iterator<E> getIterator(String Owner, String passw)
throws NullPointerException, IllegalArgumentException,
UserNotExists, WrongPassword{
//getKey effettua i controlli, occupandosi di sollevare le dovute eccezioni
//Questo metodo li solleva al sottoprogramma chiamante, non occupandosi della gestione
User u = getKey(Owner, passw);
return new SDCIterator(u);
}
//Definizione classe iteratore
public class SDCIterator
implements Iterator<E>{
int last; //Ultimo elemento iterato
Vector<DataInformation> v; //Elementi su cui iterare
public SDCIterator(User u){
last=-1;
v=Collection.get(u);
}
@Override
public boolean hasNext() {
if (v.size()-1==last) return false;
else return true;
}
@Override
public E next() throws NoSuchElementException {
if (hasNext()){
last++;
E tmp;
tmp = (E) v.get(last).getInfo();
return Encrypt(tmp, 'D');
}
else throw new NoSuchElementException();
}
}
}