Skip to content

Commit 70643e0

Browse files
committed
make in memo keystore persistent. We really need a real keystore .. anyway, good for testing anyway.
1 parent a3a1a17 commit 70643e0

File tree

7 files changed

+78
-22
lines changed

7 files changed

+78
-22
lines changed

src/main/java/net/sharksystem/asap/crypto/ASAPKeyStore.java

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

3+
import net.sharksystem.asap.ASAPException;
34
import net.sharksystem.asap.ASAPSecurityException;
5+
import net.sharksystem.fs.ExtraData;
46

7+
import java.io.IOException;
58
import java.security.PublicKey;
69

710
/**
@@ -28,4 +31,6 @@ public interface ASAPKeyStore extends ASAPCryptoParameterStorage {
2831
* A new key pair is created
2932
*/
3033
void generateKeyPair() throws ASAPSecurityException;
34+
35+
// void setMementoTarget(ExtraData extraData);
3136
}

src/main/java/net/sharksystem/asap/crypto/InMemoASAPKeyStore.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.HashMap;
1717

1818
public class InMemoASAPKeyStore implements ASAPKeyStore {
19+
private static final CharSequence ASAP_KEYSTORE_MEMENTO_KEY = "asapKeyStoreMemento";
1920
private PrivateKey privateKey;
2021
private PublicKey publicKey;
2122
private KeyPair keyPair;
@@ -123,19 +124,25 @@ private void checkKeyPairExistence() throws ASAPSecurityException {
123124
if(this.keyPair == null) {
124125
Log.writeLog(this, "create new keypair since requested but missing");
125126
this.generateKeyPair();
127+
this.privateKey = this.keyPair.getPrivate();
128+
this.publicKey = this.keyPair.getPublic();
126129
}
127130
}
128131

129132
@Override
130133
public PrivateKey getPrivateKey() throws ASAPSecurityException {
131-
this.checkKeyPairExistence();
132-
return this.keyPair.getPrivate();
134+
if(this.privateKey == null) {
135+
this.checkKeyPairExistence();
136+
}
137+
return this.privateKey;
133138
}
134139

135140
@Override
136141
public PublicKey getPublicKey() throws ASAPSecurityException {
137-
this.checkKeyPairExistence();
138-
return this.keyPair.getPublic();
142+
if(this.publicKey == null) {
143+
this.checkKeyPairExistence();
144+
}
145+
return this.publicKey;
139146
}
140147

141148
@Override
@@ -266,17 +273,21 @@ public void readPublicKey(CharSequence peer, InputStream is) throws ASAPSecurity
266273
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
267274

268275
private ExtraData mementoExtraData;
269-
private CharSequence mementoKey;
270276

271-
public void setMementoTarget(ExtraData extraData, CharSequence key) {
277+
public void setMementoTarget(ExtraData extraData) {
272278
this.mementoExtraData = extraData;
273-
this.mementoKey = key;
279+
try {
280+
byte[] memento = this.mementoExtraData.getExtra(ASAP_KEYSTORE_MEMENTO_KEY);
281+
this.restoreMemento(memento);
282+
} catch (IOException | SharkException e) {
283+
// no memento - okay
284+
}
274285
}
275286

276287
private void save() {
277-
if(this.mementoExtraData != null && this.mementoKey != null) {
288+
if(this.mementoExtraData != null) {
278289
try {
279-
this.mementoExtraData.putExtra(this.mementoKey, this.createMemento());
290+
this.mementoExtraData.putExtra(ASAP_KEYSTORE_MEMENTO_KEY, this.getMemento());
280291
} catch (IOException | SharkException e) {
281292
Log.writeLogErr(this, "cannot write memento: " + e.getLocalizedMessage());
282293
}
@@ -285,7 +296,7 @@ private void save() {
285296
}
286297
}
287298

288-
byte[] createMemento() throws ASAPSecurityException, IOException {
299+
public byte[] getMemento() throws ASAPException, IOException {
289300
ByteArrayOutputStream baos = new ByteArrayOutputStream();
290301

291302
// encoded private key
@@ -311,7 +322,7 @@ byte[] createMemento() throws ASAPSecurityException, IOException {
311322
return baos.toByteArray();
312323
}
313324

314-
public void restoreFromMemento(byte[] mementoData) throws IOException, ASAPException {
325+
public void restoreMemento(byte[] mementoData) throws IOException, ASAPException {
315326
ByteArrayInputStream bais = new ByteArrayInputStream(mementoData);
316327

317328
byte[] privateKeyBytes = ASAPSerialization.readByteArray(bais);
@@ -335,5 +346,7 @@ public void restoreFromMemento(byte[] mementoData) throws IOException, ASAPExcep
335346
// reset those simple data after that critical stuff.
336347
this.keyPairCreationTime = ASAPSerialization.readLongParameter(bais);
337348
this.ownerID = ASAPSerialization.readCharSequenceParameter(bais);
349+
350+
Log.writeLog(this, "restored keys");
338351
}
339352
}

src/main/java/net/sharksystem/asap/listenermanager/ASAPEnvironmentChangesListenerManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.sharksystem.asap.ASAPEnvironmentChangesListenerManagement;
55
import net.sharksystem.utils.Log;
66

7+
import java.util.HashSet;
78
import java.util.Set;
89

910
public class ASAPEnvironmentChangesListenerManager
@@ -23,8 +24,9 @@ public void removeASAPEnvironmentChangesListener(ASAPEnvironmentChangesListener
2324
public void notifyListeners(Set<CharSequence> peerList) {
2425
if(peerList == null || peerList.isEmpty()) return;
2526
if(this.listenerList == null || this.listenerList.isEmpty()) return;
27+
// make a copy of that list
2628
for(ASAPEnvironmentChangesListener listener : this.listenerList) {
27-
if(listener != null) listener.onlinePeersChanged(peerList);
29+
if(listener != null) listener.onlinePeersChanged(new HashSet<>(peerList));
2830
}
2931
}
3032
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package net.sharksystem.testhelper;
2+
3+
import net.sharksystem.utils.Log;
4+
import net.sharksystem.utils.testsupport.TestHelper;
5+
6+
public class ASAPTesthelper {
7+
public static final String ALICE_ID = "Alice_ID";
8+
public static final String ALICE_NAME = "Alice";
9+
public static final String BOB_ID = "Bob_ID";
10+
public static final String BOB_NAME = "Bob";
11+
public static final String CLARA_ID = "Clara_ID";
12+
public static final String CLARA_NAME = "Clara";
13+
public static final String DAVID_ID = "David_ID";
14+
public static final String DAVID_NAME = "David";
15+
16+
public static final String ROOT_DIRECTORY_TESTS = "playground/";
17+
18+
public static int testNumber = 0;
19+
20+
/**
21+
* Create a fresh testNumber
22+
*/
23+
public static void incrementTestNumber() {
24+
testNumber++;
25+
}
26+
27+
private static int portnumber = 7000;
28+
/**
29+
* Create a fresh TCP port.
30+
* @return
31+
*/
32+
public static int getPortNumber() {
33+
portnumber++;
34+
return portnumber;
35+
}
36+
37+
public static final String getUniqueFolderName(String prefix) {
38+
Log.writeLog(TestHelper.class, "test number == " + TestHelper.testNumber);
39+
return prefix + "_" + ASAPTesthelper.testNumber;
40+
}
41+
}

src/main/java/net/sharksystem/utils/testsupport/TestHelper.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package net.sharksystem.utils.testsupport;
22

3-
import net.sharksystem.utils.Log;
43
import net.sharksystem.utils.Utils;
54
import net.sharksystem.fs.FSUtils;
65

@@ -25,11 +24,6 @@ public static String getFullRootFolderName(String peerID, Class testClass) {
2524
+ peerID;
2625
}
2726

28-
public static final String getUniqueFolderName(String prefix) {
29-
Log.writeLog(TestHelper.class, "test number == " + testNumber);
30-
return prefix + "_" + testNumber;
31-
}
32-
3327
public static void incrementTestNumber() {
3428
testNumber++;
3529
}

src/test/java/junit5Tests/release_1/net/sharksystem/asap/LSANEncounterManagerAdminTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.sharksystem.asap.*;
55
import net.sharksystem.asap.apps.TCPServerSocketAcceptor;
66
import net.sharksystem.asap.apps.testsupport.ASAPTestPeerFS;
7+
import net.sharksystem.testhelper.ASAPTesthelper;
78
import net.sharksystem.utils.streams.StreamPair;
89
import net.sharksystem.utils.streams.StreamPairImpl;
910
import net.sharksystem.utils.testsupport.ASAPMessageReceivedStorage;
@@ -34,8 +35,8 @@ public void setUp() throws IOException, SharkException {
3435
this.supportedFormats = new HashSet<>();
3536
this.supportedFormats.add(TestConstants.TEST_APP_FORMAT);
3637

37-
String aliceFolder = TestHelper.getUniqueFolderName(ALICE_ROOTFOLDER.toString());
38-
String bobFolder = TestHelper.getUniqueFolderName(BOB_ROOTFOLDER.toString());
38+
String aliceFolder = ASAPTesthelper.getUniqueFolderName(ALICE_ROOTFOLDER.toString());
39+
String bobFolder = ASAPTesthelper.getUniqueFolderName(BOB_ROOTFOLDER.toString());
3940
TestHelper.incrementTestNumber();
4041

4142
//////////////////////////////// setup Alice

src/test/java/net/sharksystem/asap/crypto/PersistInMemoKeystore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void writeAndRestore() throws ASAPException, IOException {
1818
InMemoASAPKeyStore imKeystore = new InMemoASAPKeyStore("TestPeer");
1919
imKeystore.generateKeyPair();
2020

21-
byte[] memento = imKeystore.createMemento();
22-
imKeystore.restoreFromMemento(memento);
21+
byte[] memento = imKeystore.getMemento();
22+
imKeystore.restoreMemento(memento);
2323
}
2424
}

0 commit comments

Comments
 (0)