Skip to content

Commit

Permalink
created cli key generator tool and error handling bug fixes (#26)
Browse files Browse the repository at this point in the history
* created cli key generator tool and error handling bug fixes

* added new decode private key command, removed automatic key pair generation, and other bug fixes
  • Loading branch information
ManasC478 authored Feb 11, 2024
1 parent c41cf38 commit 2ab61c1
Show file tree
Hide file tree
Showing 10 changed files with 423 additions and 90 deletions.
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

0 comments on commit 2ab61c1

Please sign in to comment.