Skip to content

Commit 9380b65

Browse files
committed
Prepare SNMessage to be used in SN2 Android App
1 parent a7eb1bb commit 9380b65

File tree

2 files changed

+71
-13
lines changed

2 files changed

+71
-13
lines changed

src/net/sharksystem/asap/sharknet/InMemoSNMessage.java

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
* can be encrypted.
2121
*/
2222
public class InMemoSNMessage implements SNMessage {
23+
private ASAPCryptoAlgorithms.EncryptedMessagePackage encryptedMessagePackage;
2324
private byte[] snContent;
24-
private final CharSequence snSender;
25+
private CharSequence snSender;
2526
private boolean verified;
2627
private boolean encrypted;
2728
private Set<CharSequence> snRecipients;
@@ -45,6 +46,12 @@ private InMemoSNMessage(byte[] message, CharSequence sender,
4546
this.creationTime = creationTime;
4647
}
4748

49+
private InMemoSNMessage(ASAPCryptoAlgorithms.EncryptedMessagePackage encryptedMessagePackage) {
50+
this.encryptedMessagePackage = encryptedMessagePackage;
51+
this.snRecipients = new HashSet<>();
52+
this.snRecipients.add(encryptedMessagePackage.getRecipient());
53+
}
54+
4855
public static byte[] serializeMessage(byte[] content, CharSequence sender, CharSequence recipient)
4956
throws IOException, ASAPException {
5057

@@ -144,23 +151,60 @@ public static byte[] serializeMessage(byte[] content, CharSequence sender, Set<C
144151
}
145152

146153
@Override
147-
public byte[] getContent() { return this.snContent;}
154+
public byte[] getContent() throws ASAPSecurityException {
155+
if(this.encryptedMessagePackage != null) {
156+
throw new ASAPSecurityException("content could not be encrypted");
157+
}
158+
return this.snContent;
159+
}
160+
148161
@Override
149-
public CharSequence getSender() { return this.snSender; }
162+
public CharSequence getSender() throws ASAPSecurityException {
163+
if(this.encryptedMessagePackage != null) {
164+
throw new ASAPSecurityException("content could not be encrypted");
165+
}
166+
167+
return this.snSender;
168+
}
169+
150170
@Override
151-
public Set<CharSequence> getRecipients() { return this.snRecipients; }
171+
public Set<CharSequence> getRecipients() {
172+
return this.snRecipients;
173+
}
174+
152175
@Override
153-
public boolean verified() { return this.verified; }
176+
public boolean verified() throws ASAPSecurityException {
177+
if(this.encryptedMessagePackage != null) {
178+
throw new ASAPSecurityException("content could not be encrypted");
179+
}
180+
181+
return this.verified;
182+
}
183+
154184
@Override
155-
public boolean encrypted() { return this.encrypted; }
185+
public boolean encrypted() {
186+
return this.encrypted;
187+
}
188+
189+
public boolean couldBeDecrypted() {
190+
return this.encryptedMessagePackage != null;
191+
}
156192

157193
@Override
158-
public Timestamp getCreationTime() {
194+
public Timestamp getCreationTime() throws ASAPSecurityException {
195+
if(this.encryptedMessagePackage != null) {
196+
throw new ASAPSecurityException("content could not be encrypted");
197+
}
198+
159199
return this.creationTime;
160200
}
161201

162202
@Override
163203
public boolean isLaterThan(SNMessage message) throws ASAPException, IOException {
204+
if(this.encryptedMessagePackage != null) {
205+
throw new ASAPSecurityException("content could not be encrypted");
206+
}
207+
164208
Timestamp messageCreationTime = message.getCreationTime();
165209
return messageCreationTime.after(this.getCreationTime());
166210
}
@@ -194,7 +238,8 @@ public static InMemoSNMessage parseMessage(byte[] message, BasicKeyStore basicKe
194238

195239
// for me?
196240
if (!basicKeyStore.isOwner(encryptedMessagePackage.getRecipient())) {
197-
throw new ASAPException("SharkNetMessage: message not for me");
241+
return new InMemoSNMessage(encryptedMessagePackage);
242+
//throw new ASAPException("SharkNetMessage: message not for me");
198243
}
199244

200245
// replace message with decrypted message

src/net/sharksystem/asap/sharknet/SNMessage.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.sharksystem.asap.sharknet;
22

33
import net.sharksystem.asap.ASAPException;
4+
import net.sharksystem.asap.ASAPSecurityException;
45

56
import java.io.IOException;
67
import java.sql.Timestamp;
@@ -15,14 +16,16 @@ public interface SNMessage {
1516
/**
1617
* Content - can be encrypted and signed
1718
* @return
19+
* @throws ASAPSecurityException if message could not be encrypted
1820
*/
19-
byte[] getContent();
21+
byte[] getContent() throws ASAPSecurityException;
2022

2123
/**
2224
* Sender - can be encrypted and signed
2325
* @return
26+
* @throws ASAPSecurityException if message could not be encrypted
2427
*/
25-
CharSequence getSender();
28+
CharSequence getSender() throws ASAPSecurityException;
2629

2730
/**
2831
* Recipients are always visible - the only recipient is in the unencrypted head if message
@@ -37,8 +40,9 @@ public interface SNMessage {
3740
* right certificate arrives. A verifiable message can become non-verifiable due to loss of certificates
3841
* validity. In short: Result can change state of your local PKI
3942
* @return
43+
* @throws ASAPSecurityException if message could not be encrypted
4044
*/
41-
boolean verified();
45+
boolean verified() throws ASAPSecurityException;
4246

4347
/**
4448
* Not part of the transferred message - just a flag that indicates if this message can be encrypted.
@@ -48,13 +52,21 @@ public interface SNMessage {
4852
*/
4953
boolean encrypted();
5054

55+
/**
56+
*
57+
* @return true if this message was not encrypted in the first place or could be encrypted.
58+
* false. We have an encrypted message and cannot read it. We know its receiver, though.
59+
*/
60+
boolean couldBeDecrypted();
61+
5162
/**
5263
* Creation date is produced when an object is serialized. It becomes part of the message.
5364
* @return
5465
* @throws ASAPException
5566
* @throws IOException
67+
* @throws ASAPSecurityException if message could not be encrypted
5668
*/
57-
Timestamp getCreationTime() throws ASAPException, IOException;
69+
Timestamp getCreationTime() throws ASAPException, ASAPSecurityException, IOException;
5870

5971
/**
6072
* Compare two message what creation date is earlier. It depends on local clocks. It is a hint not more.
@@ -64,6 +76,7 @@ public interface SNMessage {
6476
* @return
6577
* @throws ASAPException
6678
* @throws IOException
79+
* @throws ASAPSecurityException if message could not be encrypted
6780
*/
68-
boolean isLaterThan(SNMessage message) throws ASAPException, IOException;
81+
boolean isLaterThan(SNMessage message) throws ASAPException, ASAPSecurityException, IOException;
6982
}

0 commit comments

Comments
 (0)