m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMagdalena Grodzińska <mag.grodzinska@gmail.com>2020-01-13 21:54:45 +0100
committerMagdalena Grodzińska <mag.grodzinska@gmail.com>2020-01-13 22:11:07 +0100
commit8103eb7e174c7c0bb33316ae9499bbc0fbb62ea5 (patch)
treeaaccde672427ef59188bbf92ee480c046626df02
parent23b8714b29816b608cb3aa330b6e93610e107126 (diff)
Fix key generation and reading
-rw-r--r--build.gradle6
-rwxr-xr-xscripts/generate_keys.sh9
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java87
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java35
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java8
5 files changed, 46 insertions, 99 deletions
diff --git a/build.gradle b/build.gradle
index 27279d4..c6ebcae 100644
--- a/build.gradle
+++ b/build.gradle
@@ -153,9 +153,3 @@ task runQuerySigner(type: JavaExec) {
systemProperty 'public_key_file', publicKeyFilename()
systemProperty 'private_key_file', privateKeyFilename()
}
-
-task runKeyGenerator(type: JavaExec) {
- classpath = sourceSets.main.runtimeClasspath
- main = 'pl.edu.mimuw.cloudatlas.KeyGenerator'
- systemProperty 'query_signer_hostname', querySignerHostname()
-}
diff --git a/scripts/generate_keys.sh b/scripts/generate_keys.sh
new file mode 100755
index 0000000..24c2498
--- /dev/null
+++ b/scripts/generate_keys.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+cd build/tmp
+# generate private key
+openssl genrsa -out query_signer.pem 2048
+# convert private key to PKCS8 format
+openssl pkcs8 -topk8 -inform PEM -outform DER -in query_signer.pem -out query_signer -nocrypt
+# generate public key
+openssl rsa -in query_signer.pem -pubout -outform DER -out query_signer.pub
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java b/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java
deleted file mode 100644
index 8a474ac..0000000
--- a/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java
+++ /dev/null
@@ -1,87 +0,0 @@
-package pl.edu.mimuw.cloudatlas;
-
-import java.io.*;
-import java.security.*;
-import java.security.spec.X509EncodedKeySpec;
-import java.util.Base64;
-
-public class KeyGenerator {
- private final static int NUM_KEY_BITS = 1024;
- private final static String ENCRYPTION_ALGORITHM = "RSA";
-
- // TODO
- private static void printKeyToFile(byte[] keyValue, String fileName) {
- System.out.println("\n BEGIN KEY");
- for (byte k : keyValue) {
- System.out.print(k);
- }
- System.out.println("\n EOF KEY");
- String keyString = new String(keyValue);
-
- try {
- BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
- writer.write(keyString);
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- private static void generateKeys() throws NoSuchAlgorithmException {
- PublicKey publicKey;
- PrivateKey privateKey;
-
- KeyPairGenerator keyGenerator =
- KeyPairGenerator.getInstance(ENCRYPTION_ALGORITHM);
- keyGenerator.initialize(NUM_KEY_BITS);
- KeyPair keyPair = keyGenerator.generateKeyPair();
-
- String publicKeyFile = System.getProperty("public_key_file");
- String privateKeyFile = System.getProperty("private_key_file");
-
- privateKey = keyPair.getPrivate();
- printKeyToFile(privateKey.getEncoded(), privateKeyFile);
- publicKey = keyPair.getPublic();
- printKeyToFile(publicKey.getEncoded(), publicKeyFile);
- }
-
- public static PublicKey getPublicKey(String key){
- try {
- byte[] byteKey = Base64.getDecoder().decode(key.getBytes());
- X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
- KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
- return kf.generatePublic(X509publicKey);
- } catch(Exception e) {
- e.printStackTrace();
- }
- return null;
- }
-
- public static PrivateKey getPrivateKey(String key){
- try {
- byte[] byteKey = Base64.getDecoder().decode(key.getBytes());
- X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
- KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
- return kf.generatePrivate(X509publicKey);
- } catch(Exception e) {
- e.printStackTrace();
- }
- return null;
- }
-
- public static String readKeyFromFile(String filename) throws IOException {
- String key;
- BufferedReader reader = new BufferedReader(new FileReader(filename));
- key = reader.readLine();
- reader.close();
- return key;
- }
-
- public static void main(String[] args) {
- try {
- generateKeys();
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- }
-}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java
new file mode 100644
index 0000000..7a543ba
--- /dev/null
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java
@@ -0,0 +1,35 @@
+package pl.edu.mimuw.cloudatlas.querysigner;
+
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.security.*;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.security.spec.X509EncodedKeySpec;
+
+public class KeyUtils {
+ private final static String ENCRYPTION_ALGORITHM = "RSA";
+
+ public static PublicKey getPublicKey(String filename){
+ try {
+ byte[] byteKey = Files.readAllBytes(Paths.get(filename));
+ X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
+ KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
+ return kf.generatePublic(X509publicKey);
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public static PrivateKey getPrivateKey(String filename){
+ try {
+ byte[] byteKey = Files.readAllBytes(Paths.get(filename));
+ PKCS8EncodedKeySpec PKCS8privateKey = new PKCS8EncodedKeySpec(byteKey);
+ KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
+ return kf.generatePrivate(PKCS8privateKey);
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java
index d60a75d..735601d 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java
@@ -1,8 +1,5 @@
package pl.edu.mimuw.cloudatlas.querysigner;
-import pl.edu.mimuw.cloudatlas.KeyGenerator;
-import pl.edu.mimuw.cloudatlas.agent.NewApiImplementation;
-import pl.edu.mimuw.cloudatlas.api.Api;
import pl.edu.mimuw.cloudatlas.querysignerapi.QuerySignerApi;
import java.io.IOException;
@@ -22,15 +19,14 @@ public class QuerySigner {
private static QuerySignerApiImplementation initApi() throws IOException {
String publicKeyFile = System.getProperty("public_key_file");
String privateKeyFile = System.getProperty("private_key_file");
- PublicKey publicKey = null; //KeyGenerator.getPublicKey(KeyGenerator.readKeyFromFile(publicKeyFile));
- PrivateKey privateKey = null; // KeyGenerator.getPrivateKey(KeyGenerator.readKeyFromFile(privateKeyFile));
+ PublicKey publicKey = KeyUtils.getPublicKey(publicKeyFile);
+ PrivateKey privateKey = KeyUtils.getPrivateKey(privateKeyFile);
return new QuerySignerApiImplementation(publicKey, privateKey);
}
public static void runRegistry() {
try {
QuerySignerApiImplementation api = initApi();
-// NewApiImplementation apii = new NewApiImplementation(null);
QuerySignerApi apiStub =
(QuerySignerApi) UnicastRemoteObject.exportObject(api, 0);
Registry registry = LocateRegistry.getRegistry();