Merge the matrix-java-sdk due to it no longer maintained. Remove thirdparty repositories.

This commit is contained in:
Anatoly Sablin
2019-07-07 23:13:59 +03:00
parent 136563c61a
commit c3262a9f25
126 changed files with 9058 additions and 8 deletions

View File

@@ -0,0 +1,80 @@
/*
* matrix-java-sdk - Matrix Client SDK for Java
* Copyright (C) 2017 Kamax Sarl
*
* https://www.kamax.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.kamax.matrix.crypto;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
public class KeyFileStore implements _KeyStore {
private final Charset charset = StandardCharsets.UTF_8;
private File file;
public KeyFileStore(String path) {
File file = new File(path);
if (!file.exists()) {
throw new IllegalArgumentException("Signing key file storage " + path + " does not exist");
}
if (file.isDirectory()) {
throw new IllegalArgumentException("Signing key file storage " + path + " is a directory");
}
if (!file.isFile()) {
throw new IllegalArgumentException("Signing key file storage " + path + " is not a regular file");
}
if (!file.canRead()) {
throw new IllegalArgumentException("Signing key file storage " + path + " is not readable");
}
this.file = file;
}
@Override
public Optional<String> load() {
try {
List<String> keys = FileUtils.readLines(file, charset);
return keys.stream().filter(StringUtils::isNotBlank).findFirst();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void store(String key) {
try {
FileUtils.writeLines(file, charset.name(), Collections.singletonList(key), false);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,100 @@
/*
* matrix-java-sdk - Matrix Client SDK for Java
* Copyright (C) 2017 Kamax Sarl
*
* https://www.kamax.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.kamax.matrix.crypto;
import io.kamax.matrix.codec.MxBase64;
import java.security.KeyPair;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import net.i2p.crypto.eddsa.EdDSAPrivateKey;
import net.i2p.crypto.eddsa.EdDSAPublicKey;
import net.i2p.crypto.eddsa.KeyPairGenerator;
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable;
import net.i2p.crypto.eddsa.spec.EdDSAParameterSpec;
import net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec;
import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec;
public class KeyManager {
public static KeyManager fromFile(String path) {
return new KeyManager(new KeyFileStore(path));
}
public static KeyManager fromMemory() {
return new KeyManager(new KeyMemoryStore());
}
private EdDSAParameterSpec keySpecs;
private List<KeyPair> keys;
public KeyManager(_KeyStore store) {
keySpecs = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
keys = new ArrayList<>();
String seedBase64 = store.load().orElseGet(() -> {
KeyPair pair = (new KeyPairGenerator()).generateKeyPair();
String keyEncoded = getPrivateKeyBase64((EdDSAPrivateKey) pair.getPrivate());
store.store(keyEncoded);
return keyEncoded;
});
byte[] seed = Base64.getDecoder().decode(seedBase64);
EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(seed, keySpecs);
EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(privKeySpec.getA(), keySpecs);
keys.add(new KeyPair(new EdDSAPublicKey(pubKeySpec), new EdDSAPrivateKey(privKeySpec)));
}
public int getCurrentIndex() {
return 0;
}
public KeyPair getKeys(int index) {
return keys.get(index);
}
public EdDSAPrivateKey getPrivateKey(int index) {
return (EdDSAPrivateKey) getKeys(index).getPrivate();
}
protected String getPrivateKeyBase64(EdDSAPrivateKey key) {
return MxBase64.encode(key.getSeed());
}
public String getPrivateKeyBase64(int index) {
return getPrivateKeyBase64(getPrivateKey(index));
}
public EdDSAPublicKey getPublicKey(int index) {
return (EdDSAPublicKey) getKeys(index).getPublic();
}
public EdDSAParameterSpec getSpecs() {
return keySpecs;
}
public String getPublicKeyBase64(int index) {
return MxBase64.encode(getPublicKey(index).getAbyte());
}
}

View File

@@ -0,0 +1,46 @@
/*
* matrix-java-sdk - Matrix Client SDK for Java
* Copyright (C) 2017 Kamax Sarl
*
* https://www.kamax.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.kamax.matrix.crypto;
import java.util.Optional;
public class KeyMemoryStore implements _KeyStore {
private String data;
public KeyMemoryStore() {
}
public KeyMemoryStore(String data) {
this.data = data;
}
@Override
public Optional<String> load() {
return Optional.ofNullable(data);
}
@Override
public void store(String key) {
data = key;
}
}

View File

@@ -0,0 +1,80 @@
/*
* matrix-java-sdk - Matrix Client SDK for Java
* Copyright (C) 2017 Kamax Sarl
*
* https://www.kamax.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.kamax.matrix.crypto;
import com.google.gson.JsonObject;
import io.kamax.matrix.codec.MxBase64;
import io.kamax.matrix.json.MatrixJson;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import net.i2p.crypto.eddsa.EdDSAEngine;
public class SignatureManager {
private KeyManager keyMgr;
private String domain;
private EdDSAEngine signEngine;
public SignatureManager(KeyManager keyMgr, String domain) {
this.keyMgr = keyMgr;
this.domain = domain;
try {
signEngine = new EdDSAEngine(MessageDigest.getInstance(keyMgr.getSpecs().getHashAlgorithm()));
signEngine.initSign(keyMgr.getPrivateKey(keyMgr.getCurrentIndex()));
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
throw new RuntimeException(e);
}
}
public String sign(JsonObject obj) {
return sign(MatrixJson.encodeCanonical(obj));
}
public String sign(String message) {
try {
byte[] signRaw = signEngine.signOneShot(message.getBytes());
return MxBase64.encode(signRaw);
} catch (SignatureException e) {
throw new RuntimeException(e);
}
}
public JsonObject signMessageGson(String message) {
String sign = sign(message);
JsonObject keySignature = new JsonObject();
// FIXME should create a signing key object what would give this ed and index values
keySignature.addProperty("ed25519:" + keyMgr.getCurrentIndex(), sign);
JsonObject signature = new JsonObject();
signature.add(domain, keySignature);
return signature;
}
}

View File

@@ -0,0 +1,31 @@
/*
* matrix-java-sdk - Matrix Client SDK for Java
* Copyright (C) 2017 Kamax Sarl
*
* https://www.kamax.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.kamax.matrix.crypto;
import java.util.Optional;
public interface _KeyStore {
Optional<String> load();
void store(String key);
}