aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2022-05-13 00:11:40 +0300
committerGravatar Vitaly Takmazov2022-05-13 00:11:40 +0300
commit10132c0ac3bcd52f2fa8aad1e1ab8505bd3ad7bb (patch)
tree6b01867dc540cb69477d953b24525710dcb360ca /src/main
parent72f3289f339ef3b8fe37be3740ff390d970f8e0e (diff)
Switch ActivityPub key management to Churchkey library
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/com/juick/KeystoreManager.java25
1 files changed, 8 insertions, 17 deletions
diff --git a/src/main/java/com/juick/KeystoreManager.java b/src/main/java/com/juick/KeystoreManager.java
index e6979a5c..5a2a98c1 100644
--- a/src/main/java/com/juick/KeystoreManager.java
+++ b/src/main/java/com/juick/KeystoreManager.java
@@ -22,6 +22,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.util.Base64Utils;
+import org.tomitribe.churchkey.Key;
+import org.tomitribe.churchkey.Keys;
import javax.net.ssl.KeyManagerFactory;
import java.io.IOException;
@@ -53,7 +55,7 @@ public class KeystoreManager {
}
private KeyPair getKeyPair() {
- Key privateKey;
+ java.security.Key privateKey;
try {
privateKey = ks.getKey("1", keystorePassword.toCharArray());
Certificate certificate = ks.getCertificate("1");
@@ -69,24 +71,13 @@ public class KeystoreManager {
public PublicKey getPublicKey() {
return getKeyPair().getPublic();
}
+
public String getPublicKeyPem() {
- String[] key = Base64Utils.encodeToString(getKeyPair().getPublic().getEncoded()).split("(?<=\\G.{64})");
- return String.format("-----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY-----\n",
- String.join("\n", key));
+ return new String(Keys.of(getPublicKey()).encode(Key.Format.PEM));
}
- public static PublicKey publicKeyOf(Actor person) {
+
+ public static java.security.Key publicKeyOf(Actor person) {
String pubkeyPem = person.getPublicKey().getPublicKeyPem();
- String[] rawKey = pubkeyPem.split("\\n");
- String pubkeyData = String.join("", Arrays.asList(rawKey).subList(1, rawKey.length - 1));
- try{
- byte[] byteKey = Base64Utils.decodeFromString(pubkeyData);
- X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
- KeyFactory kf = KeyFactory.getInstance("RSA");
- return kf.generatePublic(X509publicKey);
- }
- catch(Exception e){
- logger.error("Public key error", e);
- }
- return null;
+ return Keys.decode(pubkeyPem.getBytes()).getKey();
}
}