Skip to content

Commit

Permalink
credential message can carry some arbitrary bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
thsc42 committed Jul 13, 2022
1 parent 24761ba commit fb370cf
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/net/sharksystem/asap/persons/ASAPCertificateStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,7 @@ ASAPCertificate getCertificateByIssuerAndSubject(CharSequence issuerID, CharSequ
* @throws IOException
*/
void load(InputStream os) throws IOException;

CredentialMessage createCredentialMessage(byte[] extraData) throws ASAPSecurityException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,15 @@ public CredentialMessage createCredentialMessage()
return credentialMessage;
}


@Override
public CredentialMessage createCredentialMessage(byte[] extraData) throws ASAPSecurityException {
CredentialMessageInMemo credentialMessage = new CredentialMessageInMemo(
this.getOwnerID(), this.getOwnerName(), this.getKeysCreationTime(), this.getPublicKey(), extraData);

return credentialMessage;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// persistence //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
25 changes: 23 additions & 2 deletions src/net/sharksystem/asap/pki/CredentialMessageInMemo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.sharksystem.asap.pki;

import net.sharksystem.asap.ASAPException;
import net.sharksystem.asap.ASAPSecurityException;
import net.sharksystem.asap.utils.ASAPSerialization;
import net.sharksystem.asap.utils.DateTimeHelper;
import net.sharksystem.pki.CredentialMessage;

Expand All @@ -19,19 +21,27 @@ public class CredentialMessageInMemo implements CredentialMessage {
private CharSequence subjectID;
private CharSequence subjectName;
private int randomInt;
private byte[] extraData;
private PublicKey publicKey;

public CharSequence getSubjectID() { return this.subjectID; }
public CharSequence getSubjectName() { return this.subjectName; }
public int getRandomInt() { return this.randomInt; }
public long getValidSince() { return this.validSince; }
public byte[] getExtraData() { return this.extraData; }
public PublicKey getPublicKey() { return this.publicKey; }

public CredentialMessageInMemo(CharSequence subjectID, CharSequence subjectName,
long validSince, PublicKey publicKey) {
long validSince, PublicKey publicKey) {
this(subjectID, subjectName, validSince, publicKey, null);
}

public CredentialMessageInMemo(CharSequence subjectID, CharSequence subjectName,
long validSince, PublicKey publicKey, byte[] extraData) {
this.subjectID = subjectID;
this.subjectName = subjectName;
this.validSince = validSince;
this.extraData = extraData;
this.publicKey = publicKey;

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

public CredentialMessageInMemo(byte[] serializedMessage) throws IOException, ASAPSecurityException {
public CredentialMessageInMemo(byte[] serializedMessage) throws IOException, ASAPException {
ByteArrayInputStream bais = new ByteArrayInputStream(serializedMessage);
DataInputStream dis = new DataInputStream(bais);

this.subjectID = dis.readUTF();
this.subjectName = dis.readUTF();
this.randomInt = dis.readInt();
this.validSince = dis.readLong();
this.extraData = ASAPSerialization.readByteArray(bais);
if(this.extraData != null && this.extraData.length < 1) this.extraData = null;

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

// public key
dos.writeUTF(this.publicKey.getAlgorithm()); // write public key algorithm
Expand Down Expand Up @@ -119,6 +132,14 @@ public String toString() {
sb.append(this.randomInt);
sb.append(" | ");

sb.append("#extra byte: ");
if(this.extraData == null || this.extraData.length < 1) {
sb.append("0");
} else {
sb.append(this.extraData.length);
}
sb.append(" | ");

sb.append("publicKey: ");
sb.append(this.publicKey);

Expand Down
6 changes: 6 additions & 0 deletions src/net/sharksystem/pki/CredentialMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ public interface CredentialMessage {
byte[] getMessageAsBytes() throws IOException;

int getRandomInt();

/**
*
* @return extra data set by application - can be null
*/
byte[] getExtraData();
}
9 changes: 9 additions & 0 deletions src/net/sharksystem/pki/SharkPKIComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ ASAPCertificate getCertificateByIssuerAndSubject(CharSequence issuerID, CharSequ
*/
CredentialMessage createCredentialMessage() throws ASAPSecurityException;

/**
* Create a credential message including extra data. Those data are opaque to this
* library and can be used by an application to add security features.
* @param extraData
* @return
* @throws ASAPSecurityException
*/
CredentialMessage createCredentialMessage(byte[] extraData) throws ASAPSecurityException;

/**
* Send a credential message to all peers which are actually in the neighbourhood. This method
* is not needed, though. You should consider setting the appropriate behaviour to allow this component
Expand Down
9 changes: 8 additions & 1 deletion src/net/sharksystem/pki/SharkPKIComponentImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void asapMessagesReceived(ASAPMessages asapMessages,
try {
CredentialMessageInMemo credentialMessage = new CredentialMessageInMemo(messages.next());
this.credentialReceivedListener.credentialReceived(credentialMessage);
} catch (ASAPSecurityException e) {
} catch (ASAPException e) {
Log.writeLog(this, "could not create credential message from asap message " +
"- seems to be a bug - check serialization of credential messaging");
}
Expand Down Expand Up @@ -405,6 +405,13 @@ public CredentialMessage createCredentialMessage() throws ASAPSecurityException
return this.asapPKIStorage.createCredentialMessage();
}

@Override
public CredentialMessage createCredentialMessage(byte[] extraData) throws ASAPSecurityException {
this.checkStatus();
// TODO
return this.asapPKIStorage.createCredentialMessage(extraData);
}

@Override
public void sendOnlineCredentialMessage(CredentialMessage credentialMessage) throws ASAPException, IOException {
this.checkStatus();
Expand Down
3 changes: 2 additions & 1 deletion test/net/sharksystem/pki/SharkComponentUsageTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class SharkComponentUsageTests {
public static final String ALICE_FOLDER = SPECIFIC_ROOT_FOLDER + ALICE_NAME;
public static final String BOB_FOLDER = SPECIFIC_ROOT_FOLDER + BOB_NAME;
public static final String CLARA_FOLDER = SPECIFIC_ROOT_FOLDER + CLARA_NAME;
public static final byte[] ARBITRARY_BYTES = new byte[] {4, 8, 15, 16, 23, 42};

private static int portnumber = 7000;

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

CredentialMessage aliceCredentialMessage = alicePKI.createCredentialMessage();
CredentialMessage aliceCredentialMessage = alicePKI.createCredentialMessage(ARBITRARY_BYTES);
CredentialMessage bobCredentialMessage = bobPKI.createCredentialMessage();

// Alice and Bob exchange and accept credential messages and issue certificates
Expand Down

0 comments on commit fb370cf

Please sign in to comment.