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

created cli key generator tool and error handling bug fixes #26

Merged
merged 3 commits into from
Feb 11, 2024
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
10 changes: 8 additions & 2 deletions bundleserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<description>Bundle Server</description>
<properties>
<java.version>17</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<protobuf.version>3.5.1</protobuf.version>
<grpc.version>1.50.2</grpc.version>
Expand All @@ -31,6 +31,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<optional>true</optional>
</dependency>
<dependency>
Expand Down Expand Up @@ -125,6 +126,11 @@
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli-spring-boot-starter</artifactId>
<version>4.7.5</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ public Payload decryptPayload(UncompressedBundle uncompressedBundle) {
uncompressedBundle.getSource().getAbsolutePath());
} catch (Exception e) {
// TODO
e.printStackTrace();
System.out.println("[BS] Failed to decrypt payload");
// e.printStackTrace();
return null;
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public BundleIDCryptographyException(String errorMessage)
}

public static class BundleDecryptionException extends Exception {
public BundleDecryptionException(String errorMessage)
{
public BundleDecryptionException(String errorMessage) {
super(errorMessage);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public class SecurityUtils {
public static final String BUNDLEID_FILENAME = "bundle.id";
public static final String DECRYPTED_FILE_EXT = ".decrypted";

public static final String PUBLICKEY_HEADER = "-----BEGIN EC PUBLIC KEY-----";
public static final String PUBLICKEY_FOOTER = "-----END EC PUBLIC KEY-----";
public static final String PUB_KEY_HEADER = "-----BEGIN EC PUBLIC KEY-----";
public static final String PUB_KEY_FOOTER = "-----END EC PUBLIC KEY-----";
public static final String PVT_KEY_HEADER = "-----BEGIN EC PRIVATE KEY-----";
public static final String PVT_KEY_FOOTER = "-----END EC PRIVATE KEY-----";

public static final String CLIENT_KEY_PATH = "Client_Keys";
public static final String SERVER_KEY_PATH = "Server_Keys";
Expand All @@ -65,7 +67,10 @@ public class SecurityUtils {

public static final String SERVER_IDENTITY_KEY = "server_identity.pub";
public static final String SERVER_SIGNEDPRE_KEY = "server_signed_pre.pub";
public static final String SERVER_RATCHET_KEY = "server_ratchet.pub";
public static final String SERVER_RATCHET_KEY = "server_ratchet.pub";
public static final String SERVER_IDENTITY_PRIVATE_KEY = "serverIdentity.pvt";
public static final String SERVER_SIGNEDPRE_PRIVATE_KEY = "serverSignedPreKey.pvt";
public static final String SERVER_RATCHET_PRIVATE_KEY = "serverRatchetKey.pvt";

public static final int CHUNKSIZE = 1024 * 1024; /* 1MB */
public static final int ITERATIONS = 65536;
Expand Down Expand Up @@ -126,10 +131,10 @@ public static String generateID(byte[] publicKey) throws IDGenerationException

public static void createEncodedPublicKeyFile(ECPublicKey publicKey, String path) throws EncodingException
{
String encodedKey = PUBLICKEY_HEADER+"\n";
try (FileOutputStream stream = new FileOutputStream(path)) {
String encodedKey = PUB_KEY_HEADER+"\n";
try (FileOutputStream stream = new FileOutputStream(path, false)) {
encodedKey += Base64.getUrlEncoder().encodeToString(publicKey.serialize());
encodedKey += "\n" + PUBLICKEY_FOOTER;
encodedKey += "\n" + PUB_KEY_FOOTER;
stream.write(encodedKey.getBytes());
} catch (IOException e) {
throw new EncodingException("[BS]: Failed to Encode Public Key to file:"+e);
Expand All @@ -145,8 +150,8 @@ public static byte[] decodePublicKeyfromFile(String path) throws EncodingExcepti
throw new InvalidKeyException("Error: Invalid Public Key Length");
}

if ((true == encodedKeyList.get(0).equals(PUBLICKEY_HEADER)) &&
(true == encodedKeyList.get(2).equals(PUBLICKEY_FOOTER))) {
if (encodedKeyList.get(0).equals(PUB_KEY_HEADER) &&
encodedKeyList.get(2).equals(PUB_KEY_FOOTER)) {
return Base64.getUrlDecoder().decode(encodedKeyList.get(1));
} else {
throw new InvalidKeyException("Error: Invalid Public Key Format");
Expand All @@ -155,6 +160,26 @@ public static byte[] decodePublicKeyfromFile(String path) throws EncodingExcepti
throw new EncodingException("Error: Invalid Public Key Format");
}
}

public static byte[] decodePrivateKeyFromFile(String path) throws EncodingException {
try {
List<String> encodedKeyList = Files.readAllLines(Paths.get(path.trim()));

if (encodedKeyList.size() != 3) {
throw new InvalidKeyException("Error: Invalid Public Key Length");
}

if (encodedKeyList.get(0).equals(PVT_KEY_HEADER) &&
encodedKeyList.get(2).equals(PVT_KEY_FOOTER)) {
return Base64.getUrlDecoder().decode(encodedKeyList.get(1));
} else {
throw new InvalidKeyException("Error: Invalid Public Key Format");
}
} catch (InvalidKeyException | IOException e) {
throw new EncodingException("Error: Invalid Public Key Format");
}

}

public static InMemorySignalProtocolStore createInMemorySignalProtocolStore()
{
Expand Down
Loading
Loading