Skip to content

Commit fb370cf

Browse files
committed
credential message can carry some arbitrary bytes
1 parent 24761ba commit fb370cf

File tree

7 files changed

+60
-4
lines changed

7 files changed

+60
-4
lines changed

src/net/sharksystem/asap/persons/ASAPCertificateStore.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,7 @@ ASAPCertificate getCertificateByIssuerAndSubject(CharSequence issuerID, CharSequ
211211
* @throws IOException
212212
*/
213213
void load(InputStream os) throws IOException;
214+
215+
CredentialMessage createCredentialMessage(byte[] extraData) throws ASAPSecurityException;
216+
214217
}

src/net/sharksystem/asap/persons/ASAPCertificateStoreImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,15 @@ public CredentialMessage createCredentialMessage()
335335
return credentialMessage;
336336
}
337337

338+
339+
@Override
340+
public CredentialMessage createCredentialMessage(byte[] extraData) throws ASAPSecurityException {
341+
CredentialMessageInMemo credentialMessage = new CredentialMessageInMemo(
342+
this.getOwnerID(), this.getOwnerName(), this.getKeysCreationTime(), this.getPublicKey(), extraData);
343+
344+
return credentialMessage;
345+
}
346+
338347
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
339348
// persistence //
340349
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

src/net/sharksystem/asap/pki/CredentialMessageInMemo.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package net.sharksystem.asap.pki;
22

3+
import net.sharksystem.asap.ASAPException;
34
import net.sharksystem.asap.ASAPSecurityException;
5+
import net.sharksystem.asap.utils.ASAPSerialization;
46
import net.sharksystem.asap.utils.DateTimeHelper;
57
import net.sharksystem.pki.CredentialMessage;
68

@@ -19,19 +21,27 @@ public class CredentialMessageInMemo implements CredentialMessage {
1921
private CharSequence subjectID;
2022
private CharSequence subjectName;
2123
private int randomInt;
24+
private byte[] extraData;
2225
private PublicKey publicKey;
2326

2427
public CharSequence getSubjectID() { return this.subjectID; }
2528
public CharSequence getSubjectName() { return this.subjectName; }
2629
public int getRandomInt() { return this.randomInt; }
2730
public long getValidSince() { return this.validSince; }
31+
public byte[] getExtraData() { return this.extraData; }
2832
public PublicKey getPublicKey() { return this.publicKey; }
2933

3034
public CredentialMessageInMemo(CharSequence subjectID, CharSequence subjectName,
31-
long validSince, PublicKey publicKey) {
35+
long validSince, PublicKey publicKey) {
36+
this(subjectID, subjectName, validSince, publicKey, null);
37+
}
38+
39+
public CredentialMessageInMemo(CharSequence subjectID, CharSequence subjectName,
40+
long validSince, PublicKey publicKey, byte[] extraData) {
3241
this.subjectID = subjectID;
3342
this.subjectName = subjectName;
3443
this.validSince = validSince;
44+
this.extraData = extraData;
3545
this.publicKey = publicKey;
3646

3747
int randomStart = ((new Random(System.currentTimeMillis())).nextInt());
@@ -52,14 +62,16 @@ public CredentialMessageInMemo(CharSequence subjectID, CharSequence subjectName,
5262
this.randomInt = sixDigitsInt;
5363
}
5464

55-
public CredentialMessageInMemo(byte[] serializedMessage) throws IOException, ASAPSecurityException {
65+
public CredentialMessageInMemo(byte[] serializedMessage) throws IOException, ASAPException {
5666
ByteArrayInputStream bais = new ByteArrayInputStream(serializedMessage);
5767
DataInputStream dis = new DataInputStream(bais);
5868

5969
this.subjectID = dis.readUTF();
6070
this.subjectName = dis.readUTF();
6171
this.randomInt = dis.readInt();
6272
this.validSince = dis.readLong();
73+
this.extraData = ASAPSerialization.readByteArray(bais);
74+
if(this.extraData != null && this.extraData.length < 1) this.extraData = null;
6375

6476
// public key
6577
String algorithm = dis.readUTF(); // read public key algorithm
@@ -89,6 +101,7 @@ public byte[] getMessageAsBytes() throws IOException {
89101
dos.writeUTF(this.subjectName.toString());
90102
dos.writeInt(this.randomInt);
91103
dos.writeLong(this.validSince);
104+
ASAPSerialization.writeByteArray(this.extraData, baos);
92105

93106
// public key
94107
dos.writeUTF(this.publicKey.getAlgorithm()); // write public key algorithm
@@ -119,6 +132,14 @@ public String toString() {
119132
sb.append(this.randomInt);
120133
sb.append(" | ");
121134

135+
sb.append("#extra byte: ");
136+
if(this.extraData == null || this.extraData.length < 1) {
137+
sb.append("0");
138+
} else {
139+
sb.append(this.extraData.length);
140+
}
141+
sb.append(" | ");
142+
122143
sb.append("publicKey: ");
123144
sb.append(this.publicKey);
124145

src/net/sharksystem/pki/CredentialMessage.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ public interface CredentialMessage {
1515
byte[] getMessageAsBytes() throws IOException;
1616

1717
int getRandomInt();
18+
19+
/**
20+
*
21+
* @return extra data set by application - can be null
22+
*/
23+
byte[] getExtraData();
1824
}

src/net/sharksystem/pki/SharkPKIComponent.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ ASAPCertificate getCertificateByIssuerAndSubject(CharSequence issuerID, CharSequ
258258
*/
259259
CredentialMessage createCredentialMessage() throws ASAPSecurityException;
260260

261+
/**
262+
* Create a credential message including extra data. Those data are opaque to this
263+
* library and can be used by an application to add security features.
264+
* @param extraData
265+
* @return
266+
* @throws ASAPSecurityException
267+
*/
268+
CredentialMessage createCredentialMessage(byte[] extraData) throws ASAPSecurityException;
269+
261270
/**
262271
* Send a credential message to all peers which are actually in the neighbourhood. This method
263272
* is not needed, though. You should consider setting the appropriate behaviour to allow this component

src/net/sharksystem/pki/SharkPKIComponentImpl.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void asapMessagesReceived(ASAPMessages asapMessages,
9797
try {
9898
CredentialMessageInMemo credentialMessage = new CredentialMessageInMemo(messages.next());
9999
this.credentialReceivedListener.credentialReceived(credentialMessage);
100-
} catch (ASAPSecurityException e) {
100+
} catch (ASAPException e) {
101101
Log.writeLog(this, "could not create credential message from asap message " +
102102
"- seems to be a bug - check serialization of credential messaging");
103103
}
@@ -405,6 +405,13 @@ public CredentialMessage createCredentialMessage() throws ASAPSecurityException
405405
return this.asapPKIStorage.createCredentialMessage();
406406
}
407407

408+
@Override
409+
public CredentialMessage createCredentialMessage(byte[] extraData) throws ASAPSecurityException {
410+
this.checkStatus();
411+
// TODO
412+
return this.asapPKIStorage.createCredentialMessage(extraData);
413+
}
414+
408415
@Override
409416
public void sendOnlineCredentialMessage(CredentialMessage credentialMessage) throws ASAPException, IOException {
410417
this.checkStatus();

test/net/sharksystem/pki/SharkComponentUsageTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class SharkComponentUsageTests {
2222
public static final String ALICE_FOLDER = SPECIFIC_ROOT_FOLDER + ALICE_NAME;
2323
public static final String BOB_FOLDER = SPECIFIC_ROOT_FOLDER + BOB_NAME;
2424
public static final String CLARA_FOLDER = SPECIFIC_ROOT_FOLDER + CLARA_NAME;
25+
public static final byte[] ARBITRARY_BYTES = new byte[] {4, 8, 15, 16, 23, 42};
2526

2627
private static int portnumber = 7000;
2728

@@ -336,7 +337,7 @@ public void testIdentityAssurance() throws SharkException, ASAPException,
336337
// lets starts peer and its components before doing anythings else
337338
claraSharkPeer.start();
338339

339-
CredentialMessage aliceCredentialMessage = alicePKI.createCredentialMessage();
340+
CredentialMessage aliceCredentialMessage = alicePKI.createCredentialMessage(ARBITRARY_BYTES);
340341
CredentialMessage bobCredentialMessage = bobPKI.createCredentialMessage();
341342

342343
// Alice and Bob exchange and accept credential messages and issue certificates

0 commit comments

Comments
 (0)