Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #16

Merged
merged 6 commits into from
Jun 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<property name="ivy.organisation" value="i5" />
<property name="ivy.module" value="las2peer" />
<property name="ivy.revision" value="0.5" />
<property name="ivy.build.number" value="3" />
<property name="ivy.build.number" value="4" />
<property name="ivy.deliver.revision" value="${ivy.revision}" />
<property name="ivy.pom.version" value="${ivy.revision}" />

Expand Down
137 changes: 75 additions & 62 deletions src/main/java/i5/las2peer/persistency/Envelope.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
package i5.las2peer.persistency;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Random;

import javax.crypto.SecretKey;

import org.apache.commons.codec.binary.Base64;

import i5.las2peer.execution.L2pThread;
import i5.las2peer.p2p.ArtifactNotFoundException;
import i5.las2peer.p2p.StorageException;
Expand All @@ -32,6 +17,21 @@
import i5.simpleXML.Parser;
import i5.simpleXML.XMLSyntaxException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Random;

import javax.crypto.SecretKey;

import org.apache.commons.codec.binary.Base64;

/**
* An envelope provides a secure storage for any {@link Serializable} content within the LAS2peer network.
*
Expand Down Expand Up @@ -211,12 +211,10 @@ private void initReaders(Agent[] readers) throws EncodingFailedException {
* @param content
* @param reader
*
* @throws UnsupportedEncodingException
* @throws EncodingFailedException
* @throws DecodingFailedException
*/
public Envelope(String content, Agent reader)
throws UnsupportedEncodingException, EncodingFailedException, DecodingFailedException {
public Envelope(String content, Agent reader) throws EncodingFailedException, DecodingFailedException {
this(content, new Agent[] { reader });
}

Expand All @@ -228,10 +226,9 @@ public Envelope(String content, Agent reader)
* @param content
* @param readers
*
* @throws UnsupportedEncodingException
* @throws EncodingFailedException
*/
public Envelope(String content, Agent[] readers) throws UnsupportedEncodingException, EncodingFailedException {
public Envelope(String content, Agent[] readers) throws EncodingFailedException {
this(content, readers, new Random().nextLong());
}

Expand All @@ -243,11 +240,9 @@ public Envelope(String content, Agent[] readers) throws UnsupportedEncodingExcep
* @param content
* @param readers
* @param id
* @throws UnsupportedEncodingException
* @throws EncodingFailedException
*/
private Envelope(String content, Agent[] readers, long id)
throws UnsupportedEncodingException, EncodingFailedException {
private Envelope(String content, Agent[] readers, long id) throws EncodingFailedException {
this.id = id;

initKey();
Expand All @@ -270,10 +265,9 @@ private Envelope(String content, Agent[] readers, long id)
* @param content
* @param readers
*
* @throws UnsupportedEncodingException
* @throws EncodingFailedException
*/
public Envelope(XmlAble content, Agent[] readers) throws UnsupportedEncodingException, EncodingFailedException {
public Envelope(XmlAble content, Agent[] readers) throws EncodingFailedException {
this(content, readers, new Random().nextLong());
}

Expand All @@ -288,10 +282,8 @@ public Envelope(XmlAble content, Agent[] readers) throws UnsupportedEncodingExce
* @param readers
* @param id
* @throws EncodingFailedException
* @throws UnsupportedEncodingException
*/
private Envelope(XmlAble content, Agent[] readers, long id)
throws UnsupportedEncodingException, EncodingFailedException {
private Envelope(XmlAble content, Agent[] readers, long id) throws EncodingFailedException {
this.id = id;

initKey();
Expand All @@ -315,7 +307,7 @@ private Envelope(XmlAble content, Agent[] readers, long id)
*
* @param content
* @param reader
* @throws EnvelopeException
* @throws EnvelopeException
*
* @throws EncodingFailedException
* @throws SerializationException
Expand Down Expand Up @@ -353,8 +345,8 @@ public Envelope(Serializable content, Agent[] readers) throws EncodingFailedExce
* @throws EncodingFailedException
* @throws SerializationException
*/
private Envelope(Serializable content, Agent[] readers, long id)
throws EncodingFailedException, SerializationException {
private Envelope(Serializable content, Agent[] readers, long id) throws EncodingFailedException,
SerializationException {
this.id = id;

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

symmetricKey = (SecretKey) agent.returnSecretKey(encoded);
symmetricKey = agent.returnSecretKey(encoded);
openedBy = agent;

decryptData();
Expand Down Expand Up @@ -561,6 +553,21 @@ public void removeReader(Agent agent) throws L2pSecurityException {
htEncryptedKeys.remove(agent.getId());
}

/**
* checks if an agent is reader
*
* Attention: only direct reading access will be checked, no access gained via group memberships
*
* @param agent agent to check
* @return true if and only if the given agent is a reader
*/
public boolean hasReader(Agent agent) {
if (agent instanceof GroupAgent)
return htEncryptedGroupKeys.containsKey(agent.getId());
else
return htEncryptedKeys.containsKey(agent.getId());
}

/**
* add a signature for the content. only agents that signed the Evnelope have writing access. if no signature is
* given, every reader can write to the envelope.
Expand Down Expand Up @@ -695,17 +702,15 @@ public byte[] getContentAsBinary() throws DecodingFailedException {
* returns the contents of this envelope as string
*
* @return content as string
* @throws EnvelopeException
* @throws EnvelopeException
*
* @throws DecodingFailedException
*/
public String getContentAsString() throws EnvelopeException {
byte[] content = null;
try {
content = getContentAsBinary();
return new String(content, "UTF-8");
} catch (UnsupportedEncodingException e) {
return new String(content);
return new String(content, StandardCharsets.UTF_8);
} catch (Exception e) {
throw new EnvelopeException("Coding problems with interpreting the content", e);
}
Expand Down Expand Up @@ -743,6 +748,15 @@ public XmlAble getContentAsXmlAble() throws EnvelopeException {
}
}

/**
* get a list with all ids of non-group agents entitled to read this envelope
*
* @return array with all agent ids
*/
public Long[] getReader() {
return htEncryptedKeys.keySet().toArray(new Long[0]);
}

/**
* get a list with all ids of groups entitled to read this envelope
*
Expand Down Expand Up @@ -780,7 +794,8 @@ public Serializable getContentAsSerializable() throws EnvelopeException {

/**
* Get the content as deserialized object. This method uses the same class loader as the calling class.
* @param <T>
*
* @param <T>
*
* @param cls
* @return the typed content of this envelope
Expand All @@ -799,7 +814,8 @@ public <T extends Serializable> T getContent(Class<T> cls) throws EnvelopeExcept

/**
* Get the content as deserialized object.
* @param <T>
*
* @param <T>
*
* @param cls
* @param classLoader
Expand Down Expand Up @@ -837,8 +853,9 @@ protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, Clas

/**
* @return a XML (string) representation of this envelope
* @throws SerializationException
* @throws SerializationException
*/
@Override
public String toXmlString() throws SerializationException {
if (baPlainData != null && baCipherData == null) {
try {
Expand All @@ -848,8 +865,8 @@ public String toXmlString() throws SerializationException {
}
}

String encodedKeys = "\t<las2peer:keys encoding=\"base64\" encryption=\"" + CryptoTools.getAsymmetricAlgorithm()
+ "\">\n";
String encodedKeys = "\t<las2peer:keys encoding=\"base64\" encryption=\""
+ CryptoTools.getAsymmetricAlgorithm() + "\">\n";
for (Long id : htEncryptedKeys.keySet()) {
encodedKeys += "\t\t<las2peer:key id=\"" + id + "\">" + Base64.encodeBase64String(htEncryptedKeys.get(id))
+ "</las2peer:key>\n";
Expand Down Expand Up @@ -994,11 +1011,11 @@ public static Envelope createFromXml(Element root) throws MalformedXMLException
if (!keys.getName().equals("keys"))
throw new MalformedXMLException("not an envelope");
if (!keys.getAttribute("encoding").equals("base64"))
throw new MalformedXMLException(
"base 64 encoding of the content expected - got: " + keys.getAttribute("encoding"));
throw new MalformedXMLException("base 64 encoding of the content expected - got: "
+ keys.getAttribute("encoding"));
if (!keys.getAttribute("encryption").equals(CryptoTools.getAsymmetricAlgorithm()))
throw new MalformedXMLException(
CryptoTools.getAsymmetricAlgorithm() + " encryption of the content expected");
throw new MalformedXMLException(CryptoTools.getAsymmetricAlgorithm()
+ " encryption of the content expected");

for (Enumeration<Element> enKeys = keys.getChildren(); enKeys.hasMoreElements();) {
Element key = enKeys.nextElement();
Expand All @@ -1019,8 +1036,8 @@ public static Envelope createFromXml(Element root) throws MalformedXMLException
if (!signatures.getName().equals("signatures"))
throw new MalformedXMLException("signatures expected");
if (!signatures.getAttribute("encoding").equals("base64"))
throw new MalformedXMLException(
"base 64 encoding of the content expected - got: " + keys.getAttribute("encoding"));
throw new MalformedXMLException("base 64 encoding of the content expected - got: "
+ keys.getAttribute("encoding"));
if (!signatures.getAttribute("method").equals(CryptoTools.getSignatureMethod()))
throw new MalformedXMLException(CryptoTools.getSignatureMethod() + " expected as signature method");

Expand All @@ -1045,7 +1062,7 @@ public static Envelope createFromXml(Element root) throws MalformedXMLException
* get a locked copy of this agent
*
* @return a locked clone of this envelope
* @throws EnvelopeException
* @throws EnvelopeException
* @throws EncodingFailedException
*/
public final Envelope cloneLocked() throws EnvelopeException {
Expand Down Expand Up @@ -1129,10 +1146,9 @@ public static long getClassEnvelopeId(String cls, String identifier) {
*
* @throws EncodingFailedException
* @throws SerializationException
* @throws UnsupportedEncodingException
*/
public static Envelope createClassIdEnvelope(Object content, String identifier, Agent[] readers)
throws EncodingFailedException, SerializationException, UnsupportedEncodingException {
throws EncodingFailedException, SerializationException {
if (content instanceof String)
return new Envelope((String) content, readers, getClassEnvelopeId(content.getClass(), identifier));
else if (content instanceof XmlAble)
Expand All @@ -1156,10 +1172,9 @@ else if (content instanceof byte[])
*
* @throws SerializationException
* @throws EncodingFailedException
* @throws UnsupportedEncodingException
*/
public static Envelope createClassIdEnvelope(Object content, String identifier, Agent reader)
throws UnsupportedEncodingException, EncodingFailedException, SerializationException {
throws EncodingFailedException, SerializationException {
return createClassIdEnvelope(content, identifier, new Agent[] { reader });
}

Expand Down Expand Up @@ -1188,8 +1203,8 @@ public static Envelope fetch(long id) throws ArtifactNotFoundException, StorageE
* @throws ArtifactNotFoundException
* @throws StorageException
*/
public static Envelope fetchClassIdEnvelope(Class<?> cls, String identifier)
throws ArtifactNotFoundException, StorageException {
public static Envelope fetchClassIdEnvelope(Class<?> cls, String identifier) throws ArtifactNotFoundException,
StorageException {
return Context.getCurrent().getStoredObject(cls, identifier);
}

Expand Down Expand Up @@ -1239,11 +1254,10 @@ public void updateContent(byte[] content) throws L2pSecurityException {
*
* @param content
*
* @throws UnsupportedEncodingException
* @throws L2pSecurityException
*/
public void updateContent(String content) throws UnsupportedEncodingException, L2pSecurityException {
updateContent(content.getBytes("UTF-8"));
public void updateContent(String content) throws L2pSecurityException {
updateContent(content.getBytes(StandardCharsets.UTF_8));
contentType = ContentType.String;
}

Expand All @@ -1265,10 +1279,9 @@ public void updateContent(Serializable content) throws L2pSecurityException, Ser
*
* @param content
* @throws L2pSecurityException
* @throws UnsupportedEncodingException
* @throws SerializationException
* @throws SerializationException
*/
public void updateContent(XmlAble content) throws UnsupportedEncodingException, L2pSecurityException, SerializationException {
public void updateContent(XmlAble content) throws L2pSecurityException, SerializationException {
updateContent(content.toXmlString());
contentType = ContentType.XmlAble;
clContentClass = content.getClass();
Expand Down Expand Up @@ -1297,8 +1310,8 @@ public void checkOverwrite(Envelope envelope) throws L2pSecurityException {
return;
}

throw new L2pSecurityException(
"Check for Overwriting envelope " + getId() + " failed: No needed signature is provided!");
throw new L2pSecurityException("Check for Overwriting envelope " + getId()
+ " failed: No needed signature is provided!");
}

/**
Expand Down
Loading