Skip to content
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
59 changes: 23 additions & 36 deletions src/main/java/cn/hyperchain/sdk/common/utils/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
Expand All @@ -26,46 +26,18 @@ public class Encoder {
/**
* get deploy payload.
*
* @param path contract jar path
* @param fis FileinputStream for the given jar file
* @return payload
*/
public static String encodeDeployJar(String path) {
JarFile jar = null;
InputStream fis = null;
public static String encodeDeployJar(InputStream fis) {
BufferedInputStream bis = null;
ByteArrayOutputStream baos = null;
try {
if (Utils.isAbsolutePath(path)) {
jar = new JarFile(path, true);
fis = new FileInputStream(path);
} else {
URL url = Thread.currentThread().getContextClassLoader().getResource(path);
if (url == null) {
throw new IOException("Jar: " + path + " not found.");
}
FileOutputStream os = null;
JarFile jar = null;

if (url.toString().startsWith("jar")) {
JarURLConnection connection = (JarURLConnection) url.openConnection();
JarFile jarFile = connection.getJarFile();
Enumeration enu = jarFile.entries();
while (enu.hasMoreElements()) {
JarEntry jarEntry = (JarEntry) enu.nextElement();
String name = jarEntry.getName();
if (name.startsWith(path)) {
if (name.endsWith(".jar")) {
fis = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
}
}
}
} else {
jar = new JarFile(new File(url.toURI()));
fis = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
}
}
if (jar != null && jar.getManifest().getMainAttributes().getValue("Main-Class") == null) {
throw new IOException("the path does not point to a contract jar");
}
String tmpPath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "tmp.jar";

try {
bis = new BufferedInputStream(fis);
baos = new ByteArrayOutputStream();
int len = 0;
Expand All @@ -77,8 +49,23 @@ public static String encodeDeployJar(String path) {
if (buffer.length > 1024 * 64) {
throw new IOException("the contract jar should not be larger than 64KB");
}

os = new FileOutputStream(tmpPath);
os.write(buffer);

jar = new JarFile(tmpPath, true);

if (jar != null && jar.getManifest().getMainAttributes().getValue("Main-Class") == null) {
throw new IOException("the path does not point to a contract jar");
}

File file = new File(tmpPath);
if (!file.delete()) {
throw new IOException("temp file delete failed!");
}

return ByteUtil.toHex(buffer);
} catch (IOException | URISyntaxException e ) {
} catch (IOException e ) {
throw new RuntimeException(e);
} finally {
try {
Expand Down
61 changes: 48 additions & 13 deletions src/main/java/cn/hyperchain/sdk/common/utils/FileUtil.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package cn.hyperchain.sdk.common.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class FileUtil {

/**
* read file content by lines with file path.
* @param path file path
* @param input file input stream
* @return file content string
* @throws IOException may can not find file
* @throws IOException may not find file
*/
public static String readFile(String path) throws IOException {
public static String readFile(InputStream input) throws IOException {
BufferedReader reader = null;
if (Utils.isAbsolutePath(path)) {
reader = new BufferedReader(new FileReader(path));
} else {
InputStream input = Utils.class.getClassLoader().getResourceAsStream(path);
if (input == null) {
throw new IOException("This file not exist! " + path);
}
reader = new BufferedReader(new InputStreamReader(input));
}
reader = new BufferedReader(new InputStreamReader(input));

String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
Expand All @@ -43,4 +41,41 @@ public static String readFile(String path) throws IOException {
reader.close();
}
}

/**
* get InputStream of the file for the given path.
* @param path jar path
* @return input stream for the jar path
* @throws IOException may not find file
*/
public static InputStream readFileAsStream(String path) throws IOException {
InputStream fis = null;
if (Utils.isAbsolutePath(path)) {
fis = new FileInputStream(path);
} else {
URL url = Thread.currentThread().getContextClassLoader().getResource(path);
if (url == null) {
throw new IOException("Jar: " + path + " not found.");
}

if (url.toString().startsWith("jar")) {
JarURLConnection connection = (JarURLConnection) url.openConnection();
JarFile jarFile = connection.getJarFile();
Enumeration enu = jarFile.entries();
while (enu.hasMoreElements()) {
JarEntry jarEntry = (JarEntry) enu.nextElement();
String name = jarEntry.getName();
if (name.startsWith(path)) {
if (name.endsWith(".jar")) {
fis = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
}
}
}
} else {
fis = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
}
}
return fis;

}
}
39 changes: 16 additions & 23 deletions src/main/java/cn/hyperchain/sdk/common/utils/HttpsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* @version 1.0, 2018/6/12
*/
import cn.hyperchain.sdk.crypto.cert.CertUtils;
import cn.hyperchain.sdk.crypto.cert.SM2Priv;
import org.apache.log4j.Logger;
import org.bouncycastle.openssl.PEMKeyPair;
import sun.security.x509.X500Name;

import javax.net.ssl.HostnameVerifier;
Expand Down Expand Up @@ -55,21 +57,16 @@ public X509TrustManager getTrustManager() {

/**
* create ssl socket factory and trust manager.
* @param certificates tlsCa file path
* @param tlsPeerCert tls peer cert file path
* @param tlsPeerPriv tls peer cert private key file path
* @param certificates tlsCa inputStream
* @param tlsPeerCert tls peer cert inputStream
* @param tlsPeerPriv tls peer cert private key inputStream
* @param password jks password, default is ""
* @return {@link SSLParams}
*/
public static SSLParams getSslSocketFactory(String certificates, String tlsPeerCert, String tlsPeerPriv, String password) {
public static SSLParams getSslSocketFactory(InputStream certificates, InputStream tlsPeerCert, InputStream tlsPeerPriv, String password) {
SSLParams sslParams = new SSLParams();
InputStream isCa = null;
InputStream isCa = certificates;
try {
if (Utils.isAbsolutePath(certificates)) {
isCa = new FileInputStream(certificates);
} else {
isCa = HttpsUtils.class.getClassLoader().getResourceAsStream(certificates);
}
TrustManager[] trustManagers = prepareTrustManager(isCa);
KeyManager[] keyManagers = prepareKeyManager(tlsPeerCert, tlsPeerPriv, password);
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
Expand Down Expand Up @@ -137,7 +134,7 @@ private static TrustManager[] prepareTrustManager(InputStream... certificates) {

}

private static KeyManager[] prepareKeyManager(String tlsPeerCert, String tlsPeerPriv, String password) {
private static KeyManager[] prepareKeyManager(InputStream tlsPeerCert, InputStream tlsPeerPriv, String password) {
try {
// KeyStore clientKeyStore = KeyStore.getInstance("JKS");
// clientKeyStore.load(bksFile, password.toCharArray());
Expand Down Expand Up @@ -201,29 +198,25 @@ public X509Certificate[] getAcceptedIssuers() {
* @param certificatePem the certificate(s) PEM file
* @param password to set to protect the private key
*/
public static KeyStore createKeyStore(String certificatePem, String privateKeyPem, final String password) throws Exception {
public static KeyStore createKeyStore(InputStream certificatePem, InputStream privateKeyPem, final String password) throws Exception {
final X509Certificate[] cert = createCertificates(certificatePem);
final KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(null);
// Import private key
final PrivateKey key = CertUtils.getPrivateKeyFromPEM(privateKeyPem);
PEMKeyPair pem = CertUtils.getPEM(privateKeyPem);
boolean isGM = pem.getPrivateKeyInfo().getPrivateKeyAlgorithm().getParameters().toString().equals(SM2Priv.SM2OID);

final PrivateKey key = CertUtils.getPrivateKeyFromPEM(pem, isGM);
keystore.setKeyEntry("tlsCertPriv", key, password.toCharArray(), cert);
return keystore;
}

private static X509Certificate[] createCertificates(String certificatePem) throws Exception {
private static X509Certificate[] createCertificates(InputStream certificatePem) throws Exception {
List<X509Certificate> result = new ArrayList<X509Certificate>();
BufferedReader r = null;
r = new BufferedReader(new InputStreamReader(certificatePem));

try {
if (Utils.isAbsolutePath(certificatePem)) {
r = new BufferedReader(new FileReader(certificatePem));
} else {
InputStream inputStream = HttpsUtils.class.getClassLoader().getResourceAsStream(certificatePem);
if (inputStream == null) {
throw new IOException("This file not exist! " + certificatePem);
}
r = new BufferedReader(new InputStreamReader(inputStream));
}
String s = r.readLine();
if (s == null || !s.contains("BEGIN CERTIFICATE")) {
r.close();
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/cn/hyperchain/sdk/crypto/cert/CertKeyPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import cn.hyperchain.sdk.common.utils.Utils;
import cn.hyperchain.sdk.crypto.sm.sm2.SM2Util;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMKeyPair;

import java.io.InputStream;
import java.security.PrivateKey;
import java.security.Signature;

Expand All @@ -17,14 +19,15 @@ public class CertKeyPair {

/**
* create cert key pair instance.
* @param pubFilePath cert file path
* @param privFilePath private key file path
* @param pubFile cert inputStream
* @param privFile private key inputStream
* @throws Exception -
*/
public CertKeyPair(String pubFilePath, String privFilePath) throws Exception {
this.isGM = CertUtils.isGMCert(privFilePath);
String pubPem = FileUtil.readFile(pubFilePath);
this.privateKey = CertUtils.getPrivateKeyFromPEM(privFilePath);
public CertKeyPair(InputStream pubFile, InputStream privFile) throws Exception {
PEMKeyPair pem = CertUtils.getPEM(privFile);
this.isGM = pem.getPrivateKeyInfo().getPrivateKeyAlgorithm().getParameters().toString().equals(SM2Priv.SM2OID);
String pubPem = FileUtil.readFile(pubFile);
this.privateKey = CertUtils.getPrivateKeyFromPEM(pem, isGM);
this.publicKey = ByteUtil.toHex(pubPem.getBytes(Utils.DEFAULT_CHARSET));
}

Expand Down
36 changes: 8 additions & 28 deletions src/main/java/cn/hyperchain/sdk/crypto/cert/CertUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,29 @@ public class CertUtils {

/**
* judge is guomi cert.
* @param pemPath pem path
* @param pem pem inputStream
* @return is guomi cert
* @throws Exception -
*/
public static boolean isGMCert(String pemPath) throws Exception {
PEMParser pemRd = openPEMResource(pemPath);
public static PEMKeyPair getPEM(InputStream pem) throws Exception {
PEMParser pemRd = openPEMResource(pem);
if (pemRd == null) {
throw new Exception("Open pem error");
}
PEMKeyPair pemPair = (PEMKeyPair) pemRd.readObject();
return pemPair.getPrivateKeyInfo().getPrivateKeyAlgorithm().getParameters().toString().equals(SM2Priv.SM2OID);
return pemPair;
}

/**
* get private key.
*
* @param pemPath private key file path
* @param pemPair private key inputStream
* @return -
* @throws Exception -
*/
public static PrivateKey getPrivateKeyFromPEM(String pemPath) throws Exception {
PEMParser pemRd = openPEMResource(pemPath);
if (pemRd == null) {
throw new Exception("Open pem error");
}
PEMKeyPair pemPair = (PEMKeyPair) pemRd.readObject();
public static PrivateKey getPrivateKeyFromPEM(PEMKeyPair pemPair, boolean isGM) throws Exception {

if (pemPair.getPrivateKeyInfo().getPrivateKeyAlgorithm().getParameters().toString().equals(SM2Priv.SM2OID)) {
if (isGM) {
DLSequence dl = (DLSequence) pemPair.getPrivateKeyInfo().parsePrivateKey();
ASN1Encodable[] dls = dl.toArray();
BigInteger priv = null;
Expand All @@ -67,22 +62,7 @@ public static PrivateKey getPrivateKeyFromPEM(String pemPath) throws Exception {
}
}

private static PEMParser openPEMResource(String fileName) {
InputStream res = null;
try {
if (Utils.isAbsolutePath(fileName)) {
res = new FileInputStream(fileName);
} else {
res = CertUtils.class.getClassLoader().getResourceAsStream(fileName);
}

if (res == null) {
throw new IOException("This file not exist! " + fileName);
}
} catch (IOException e) {
return null;
}

private static PEMParser openPEMResource(InputStream res) {
Reader fRd = new BufferedReader(new InputStreamReader(res));
return new PEMParser(fRd);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -63,12 +64,12 @@ public Builder setUrl(String url) {

/**
* use https protocol.
* @param tlsCa tls ca file path
* @param tlsPeerCert tls peer cert file path
* @param tlsPeerPriv tls peer private key file path
* @param tlsCa tls ca inputStream
* @param tlsPeerCert tls peer cert inputstream
* @param tlsPeerPriv tls peer private key inputstream
* @return @return {@link Builder}
*/
public Builder https(String tlsCa, String tlsPeerCert, String tlsPeerPriv) {
public Builder https(InputStream tlsCa, InputStream tlsPeerCert, InputStream tlsPeerPriv) {
HttpsUtils.SSLParams sslSocketFactory = HttpsUtils.getSslSocketFactory(tlsCa, tlsPeerCert, tlsPeerPriv, HttpsUtils.DEFAULT_PASSWORD);
builder.sslSocketFactory(sslSocketFactory.getsSLSocketFactory(), sslSocketFactory.getTrustManager())
.hostnameVerifier(HttpsUtils.hyperchainVerifier());
Expand Down
Loading