Skip to content

Commit 71d3e76

Browse files
authored
Merge pull request #16 from rwth-acis/development
Development
2 parents 47dc5ef + e1757f0 commit 71d3e76

File tree

4 files changed

+113
-91
lines changed

4 files changed

+113
-91
lines changed

build.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<property name="ivy.organisation" value="i5" />
66
<property name="ivy.module" value="las2peer" />
77
<property name="ivy.revision" value="0.5" />
8-
<property name="ivy.build.number" value="3" />
8+
<property name="ivy.build.number" value="4" />
99
<property name="ivy.deliver.revision" value="${ivy.revision}" />
1010
<property name="ivy.pom.version" value="${ivy.revision}" />
1111

src/main/java/i5/las2peer/persistency/Envelope.java

+75-62
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
package i5.las2peer.persistency;
22

3-
import java.io.ByteArrayInputStream;
4-
import java.io.IOException;
5-
import java.io.ObjectInputStream;
6-
import java.io.ObjectStreamClass;
7-
import java.io.Serializable;
8-
import java.io.UnsupportedEncodingException;
9-
import java.util.Date;
10-
import java.util.Enumeration;
11-
import java.util.Hashtable;
12-
import java.util.Random;
13-
14-
import javax.crypto.SecretKey;
15-
16-
import org.apache.commons.codec.binary.Base64;
17-
183
import i5.las2peer.execution.L2pThread;
194
import i5.las2peer.p2p.ArtifactNotFoundException;
205
import i5.las2peer.p2p.StorageException;
@@ -32,6 +17,21 @@
3217
import i5.simpleXML.Parser;
3318
import i5.simpleXML.XMLSyntaxException;
3419

20+
import java.io.ByteArrayInputStream;
21+
import java.io.IOException;
22+
import java.io.ObjectInputStream;
23+
import java.io.ObjectStreamClass;
24+
import java.io.Serializable;
25+
import java.nio.charset.StandardCharsets;
26+
import java.util.Date;
27+
import java.util.Enumeration;
28+
import java.util.Hashtable;
29+
import java.util.Random;
30+
31+
import javax.crypto.SecretKey;
32+
33+
import org.apache.commons.codec.binary.Base64;
34+
3535
/**
3636
* An envelope provides a secure storage for any {@link Serializable} content within the LAS2peer network.
3737
*
@@ -211,12 +211,10 @@ private void initReaders(Agent[] readers) throws EncodingFailedException {
211211
* @param content
212212
* @param reader
213213
*
214-
* @throws UnsupportedEncodingException
215214
* @throws EncodingFailedException
216215
* @throws DecodingFailedException
217216
*/
218-
public Envelope(String content, Agent reader)
219-
throws UnsupportedEncodingException, EncodingFailedException, DecodingFailedException {
217+
public Envelope(String content, Agent reader) throws EncodingFailedException, DecodingFailedException {
220218
this(content, new Agent[] { reader });
221219
}
222220

@@ -228,10 +226,9 @@ public Envelope(String content, Agent reader)
228226
* @param content
229227
* @param readers
230228
*
231-
* @throws UnsupportedEncodingException
232229
* @throws EncodingFailedException
233230
*/
234-
public Envelope(String content, Agent[] readers) throws UnsupportedEncodingException, EncodingFailedException {
231+
public Envelope(String content, Agent[] readers) throws EncodingFailedException {
235232
this(content, readers, new Random().nextLong());
236233
}
237234

@@ -243,11 +240,9 @@ public Envelope(String content, Agent[] readers) throws UnsupportedEncodingExcep
243240
* @param content
244241
* @param readers
245242
* @param id
246-
* @throws UnsupportedEncodingException
247243
* @throws EncodingFailedException
248244
*/
249-
private Envelope(String content, Agent[] readers, long id)
250-
throws UnsupportedEncodingException, EncodingFailedException {
245+
private Envelope(String content, Agent[] readers, long id) throws EncodingFailedException {
251246
this.id = id;
252247

253248
initKey();
@@ -270,10 +265,9 @@ private Envelope(String content, Agent[] readers, long id)
270265
* @param content
271266
* @param readers
272267
*
273-
* @throws UnsupportedEncodingException
274268
* @throws EncodingFailedException
275269
*/
276-
public Envelope(XmlAble content, Agent[] readers) throws UnsupportedEncodingException, EncodingFailedException {
270+
public Envelope(XmlAble content, Agent[] readers) throws EncodingFailedException {
277271
this(content, readers, new Random().nextLong());
278272
}
279273

@@ -288,10 +282,8 @@ public Envelope(XmlAble content, Agent[] readers) throws UnsupportedEncodingExce
288282
* @param readers
289283
* @param id
290284
* @throws EncodingFailedException
291-
* @throws UnsupportedEncodingException
292285
*/
293-
private Envelope(XmlAble content, Agent[] readers, long id)
294-
throws UnsupportedEncodingException, EncodingFailedException {
286+
private Envelope(XmlAble content, Agent[] readers, long id) throws EncodingFailedException {
295287
this.id = id;
296288

297289
initKey();
@@ -315,7 +307,7 @@ private Envelope(XmlAble content, Agent[] readers, long id)
315307
*
316308
* @param content
317309
* @param reader
318-
* @throws EnvelopeException
310+
* @throws EnvelopeException
319311
*
320312
* @throws EncodingFailedException
321313
* @throws SerializationException
@@ -353,8 +345,8 @@ public Envelope(Serializable content, Agent[] readers) throws EncodingFailedExce
353345
* @throws EncodingFailedException
354346
* @throws SerializationException
355347
*/
356-
private Envelope(Serializable content, Agent[] readers, long id)
357-
throws EncodingFailedException, SerializationException {
348+
private Envelope(Serializable content, Agent[] readers, long id) throws EncodingFailedException,
349+
SerializationException {
358350
this.id = id;
359351

360352
initKey();
@@ -434,7 +426,7 @@ public void open(Agent agent) throws DecodingFailedException, L2pSecurityExcepti
434426
throw new L2pSecurityException("agent " + agent.getId() + " has no access to this object");
435427
}
436428

437-
symmetricKey = (SecretKey) agent.returnSecretKey(encoded);
429+
symmetricKey = agent.returnSecretKey(encoded);
438430
openedBy = agent;
439431

440432
decryptData();
@@ -561,6 +553,21 @@ public void removeReader(Agent agent) throws L2pSecurityException {
561553
htEncryptedKeys.remove(agent.getId());
562554
}
563555

556+
/**
557+
* checks if an agent is reader
558+
*
559+
* Attention: only direct reading access will be checked, no access gained via group memberships
560+
*
561+
* @param agent agent to check
562+
* @return true if and only if the given agent is a reader
563+
*/
564+
public boolean hasReader(Agent agent) {
565+
if (agent instanceof GroupAgent)
566+
return htEncryptedGroupKeys.containsKey(agent.getId());
567+
else
568+
return htEncryptedKeys.containsKey(agent.getId());
569+
}
570+
564571
/**
565572
* add a signature for the content. only agents that signed the Evnelope have writing access. if no signature is
566573
* given, every reader can write to the envelope.
@@ -695,17 +702,15 @@ public byte[] getContentAsBinary() throws DecodingFailedException {
695702
* returns the contents of this envelope as string
696703
*
697704
* @return content as string
698-
* @throws EnvelopeException
705+
* @throws EnvelopeException
699706
*
700707
* @throws DecodingFailedException
701708
*/
702709
public String getContentAsString() throws EnvelopeException {
703710
byte[] content = null;
704711
try {
705712
content = getContentAsBinary();
706-
return new String(content, "UTF-8");
707-
} catch (UnsupportedEncodingException e) {
708-
return new String(content);
713+
return new String(content, StandardCharsets.UTF_8);
709714
} catch (Exception e) {
710715
throw new EnvelopeException("Coding problems with interpreting the content", e);
711716
}
@@ -743,6 +748,15 @@ public XmlAble getContentAsXmlAble() throws EnvelopeException {
743748
}
744749
}
745750

751+
/**
752+
* get a list with all ids of non-group agents entitled to read this envelope
753+
*
754+
* @return array with all agent ids
755+
*/
756+
public Long[] getReader() {
757+
return htEncryptedKeys.keySet().toArray(new Long[0]);
758+
}
759+
746760
/**
747761
* get a list with all ids of groups entitled to read this envelope
748762
*
@@ -780,7 +794,8 @@ public Serializable getContentAsSerializable() throws EnvelopeException {
780794

781795
/**
782796
* Get the content as deserialized object. This method uses the same class loader as the calling class.
783-
* @param <T>
797+
*
798+
* @param <T>
784799
*
785800
* @param cls
786801
* @return the typed content of this envelope
@@ -799,7 +814,8 @@ public <T extends Serializable> T getContent(Class<T> cls) throws EnvelopeExcept
799814

800815
/**
801816
* Get the content as deserialized object.
802-
* @param <T>
817+
*
818+
* @param <T>
803819
*
804820
* @param cls
805821
* @param classLoader
@@ -837,8 +853,9 @@ protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, Clas
837853

838854
/**
839855
* @return a XML (string) representation of this envelope
840-
* @throws SerializationException
856+
* @throws SerializationException
841857
*/
858+
@Override
842859
public String toXmlString() throws SerializationException {
843860
if (baPlainData != null && baCipherData == null) {
844861
try {
@@ -848,8 +865,8 @@ public String toXmlString() throws SerializationException {
848865
}
849866
}
850867

851-
String encodedKeys = "\t<las2peer:keys encoding=\"base64\" encryption=\"" + CryptoTools.getAsymmetricAlgorithm()
852-
+ "\">\n";
868+
String encodedKeys = "\t<las2peer:keys encoding=\"base64\" encryption=\""
869+
+ CryptoTools.getAsymmetricAlgorithm() + "\">\n";
853870
for (Long id : htEncryptedKeys.keySet()) {
854871
encodedKeys += "\t\t<las2peer:key id=\"" + id + "\">" + Base64.encodeBase64String(htEncryptedKeys.get(id))
855872
+ "</las2peer:key>\n";
@@ -994,11 +1011,11 @@ public static Envelope createFromXml(Element root) throws MalformedXMLException
9941011
if (!keys.getName().equals("keys"))
9951012
throw new MalformedXMLException("not an envelope");
9961013
if (!keys.getAttribute("encoding").equals("base64"))
997-
throw new MalformedXMLException(
998-
"base 64 encoding of the content expected - got: " + keys.getAttribute("encoding"));
1014+
throw new MalformedXMLException("base 64 encoding of the content expected - got: "
1015+
+ keys.getAttribute("encoding"));
9991016
if (!keys.getAttribute("encryption").equals(CryptoTools.getAsymmetricAlgorithm()))
1000-
throw new MalformedXMLException(
1001-
CryptoTools.getAsymmetricAlgorithm() + " encryption of the content expected");
1017+
throw new MalformedXMLException(CryptoTools.getAsymmetricAlgorithm()
1018+
+ " encryption of the content expected");
10021019

10031020
for (Enumeration<Element> enKeys = keys.getChildren(); enKeys.hasMoreElements();) {
10041021
Element key = enKeys.nextElement();
@@ -1019,8 +1036,8 @@ public static Envelope createFromXml(Element root) throws MalformedXMLException
10191036
if (!signatures.getName().equals("signatures"))
10201037
throw new MalformedXMLException("signatures expected");
10211038
if (!signatures.getAttribute("encoding").equals("base64"))
1022-
throw new MalformedXMLException(
1023-
"base 64 encoding of the content expected - got: " + keys.getAttribute("encoding"));
1039+
throw new MalformedXMLException("base 64 encoding of the content expected - got: "
1040+
+ keys.getAttribute("encoding"));
10241041
if (!signatures.getAttribute("method").equals(CryptoTools.getSignatureMethod()))
10251042
throw new MalformedXMLException(CryptoTools.getSignatureMethod() + " expected as signature method");
10261043

@@ -1045,7 +1062,7 @@ public static Envelope createFromXml(Element root) throws MalformedXMLException
10451062
* get a locked copy of this agent
10461063
*
10471064
* @return a locked clone of this envelope
1048-
* @throws EnvelopeException
1065+
* @throws EnvelopeException
10491066
* @throws EncodingFailedException
10501067
*/
10511068
public final Envelope cloneLocked() throws EnvelopeException {
@@ -1129,10 +1146,9 @@ public static long getClassEnvelopeId(String cls, String identifier) {
11291146
*
11301147
* @throws EncodingFailedException
11311148
* @throws SerializationException
1132-
* @throws UnsupportedEncodingException
11331149
*/
11341150
public static Envelope createClassIdEnvelope(Object content, String identifier, Agent[] readers)
1135-
throws EncodingFailedException, SerializationException, UnsupportedEncodingException {
1151+
throws EncodingFailedException, SerializationException {
11361152
if (content instanceof String)
11371153
return new Envelope((String) content, readers, getClassEnvelopeId(content.getClass(), identifier));
11381154
else if (content instanceof XmlAble)
@@ -1156,10 +1172,9 @@ else if (content instanceof byte[])
11561172
*
11571173
* @throws SerializationException
11581174
* @throws EncodingFailedException
1159-
* @throws UnsupportedEncodingException
11601175
*/
11611176
public static Envelope createClassIdEnvelope(Object content, String identifier, Agent reader)
1162-
throws UnsupportedEncodingException, EncodingFailedException, SerializationException {
1177+
throws EncodingFailedException, SerializationException {
11631178
return createClassIdEnvelope(content, identifier, new Agent[] { reader });
11641179
}
11651180

@@ -1188,8 +1203,8 @@ public static Envelope fetch(long id) throws ArtifactNotFoundException, StorageE
11881203
* @throws ArtifactNotFoundException
11891204
* @throws StorageException
11901205
*/
1191-
public static Envelope fetchClassIdEnvelope(Class<?> cls, String identifier)
1192-
throws ArtifactNotFoundException, StorageException {
1206+
public static Envelope fetchClassIdEnvelope(Class<?> cls, String identifier) throws ArtifactNotFoundException,
1207+
StorageException {
11931208
return Context.getCurrent().getStoredObject(cls, identifier);
11941209
}
11951210

@@ -1239,11 +1254,10 @@ public void updateContent(byte[] content) throws L2pSecurityException {
12391254
*
12401255
* @param content
12411256
*
1242-
* @throws UnsupportedEncodingException
12431257
* @throws L2pSecurityException
12441258
*/
1245-
public void updateContent(String content) throws UnsupportedEncodingException, L2pSecurityException {
1246-
updateContent(content.getBytes("UTF-8"));
1259+
public void updateContent(String content) throws L2pSecurityException {
1260+
updateContent(content.getBytes(StandardCharsets.UTF_8));
12471261
contentType = ContentType.String;
12481262
}
12491263

@@ -1265,10 +1279,9 @@ public void updateContent(Serializable content) throws L2pSecurityException, Ser
12651279
*
12661280
* @param content
12671281
* @throws L2pSecurityException
1268-
* @throws UnsupportedEncodingException
1269-
* @throws SerializationException
1282+
* @throws SerializationException
12701283
*/
1271-
public void updateContent(XmlAble content) throws UnsupportedEncodingException, L2pSecurityException, SerializationException {
1284+
public void updateContent(XmlAble content) throws L2pSecurityException, SerializationException {
12721285
updateContent(content.toXmlString());
12731286
contentType = ContentType.XmlAble;
12741287
clContentClass = content.getClass();
@@ -1297,8 +1310,8 @@ public void checkOverwrite(Envelope envelope) throws L2pSecurityException {
12971310
return;
12981311
}
12991312

1300-
throw new L2pSecurityException(
1301-
"Check for Overwriting envelope " + getId() + " failed: No needed signature is provided!");
1313+
throw new L2pSecurityException("Check for Overwriting envelope " + getId()
1314+
+ " failed: No needed signature is provided!");
13021315
}
13031316

13041317
/**

0 commit comments

Comments
 (0)