sdf
This commit is contained in:
@@ -13,7 +13,6 @@ import com.sunyard.chsm.sdf.model.SDF_DeviceInfo;
|
||||
import com.sunyard.chsm.sdf.util.LangUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bouncycastle.util.encoders.Hex;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -35,6 +34,7 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
||||
protected final SdfLibrary sdfLibrary;
|
||||
|
||||
protected abstract int getAlgId(AlgId alg);
|
||||
protected abstract void checkRes(int res);
|
||||
|
||||
@Override
|
||||
public String openDevice() {
|
||||
@@ -126,7 +126,6 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
||||
byte[] pubKey = new byte[132];
|
||||
Pointer hSessionHandle = getSessionHandle(sessionHandle);
|
||||
sdfLibrary.SDF_ExportEncPublicKey_ECC(hSessionHandle, uiKeyIndex, pubKey);
|
||||
log.info("sdf pubkey: {}", Hex.toHexString(pubKey));
|
||||
return EccPubKey.fromBytes(pubKey);
|
||||
}
|
||||
|
||||
@@ -139,7 +138,6 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
||||
public boolean getPrivateKeyAccessRight(String sessionHandle, int uiKeyIndex, byte[] pucPassword) {
|
||||
Pointer hSessionHandle = getSessionHandle(sessionHandle);
|
||||
int res = sdfLibrary.SDF_GetPrivateKeyAccessRight(hSessionHandle, uiKeyIndex, pucPassword, pucPassword.length);
|
||||
log.info("SDF_GetPrivateKeyAccessRight: {}, {}", res, Integer.toHexString(res));
|
||||
return res == 0;
|
||||
}
|
||||
|
||||
@@ -148,8 +146,7 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
||||
Pointer hSessionHandle = getSessionHandle(sessionHandle);
|
||||
PointerByReference phKeyHandle = new PointerByReference();
|
||||
|
||||
int res = sdfLibrary.SDF_ImportKeyWithISK_ECC(hSessionHandle, uiIskIndex, eccCipher.toSdfData(), phKeyHandle);
|
||||
log.info("SDF_ImportKeyWithISK_ECC: {}, {}", res, Integer.toHexString(res));
|
||||
sdfLibrary.SDF_ImportKeyWithISK_ECC(hSessionHandle, uiIskIndex, eccCipher.toSdfData(), phKeyHandle);
|
||||
String key = UUID.randomUUID().toString();
|
||||
KEY_HANDLE_CONTEXT.put(key, phKeyHandle.getValue());
|
||||
return key;
|
||||
@@ -176,9 +173,8 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
||||
Pointer hSessionHandle = getSessionHandle(sessionHandle);
|
||||
|
||||
byte[] pucEncDateOut = new byte[164 + pucEncDateIn.getL()];
|
||||
int res = sdfLibrary.SDF_ExchangeDigitEnvelopeBaseOnECC(hSessionHandle, uiKeyIndex, getAlgId(AlgId.SGD_SM2_1),
|
||||
sdfLibrary.SDF_ExchangeDigitEnvelopeBaseOnECC(hSessionHandle, uiKeyIndex, getAlgId(AlgId.SGD_SM2_1),
|
||||
pubKey.toSdfData(), pucEncDateIn.toSdfData(), pucEncDateOut);
|
||||
log.info("SDF_ExchangeDigitEnvelopeBaseOnECC: {}, {}", res, Integer.toHexString(res));
|
||||
return EccCipher.fromBytes(pucEncDateOut);
|
||||
}
|
||||
|
||||
@@ -187,7 +183,6 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
||||
byte[] encData = new byte[128 + 32 + 4 + pucData.length];
|
||||
Pointer hSessionHandle = getSessionHandle(sessionHandle);
|
||||
sdfLibrary.SDF_ExternalEncrypt_ECC(hSessionHandle, getAlgId(AlgId.SGD_SM2_3), pubKey.toSdfData(), pucData, pucData.length, encData);
|
||||
log.info("sdfEncryptECC: {}", Hex.toHexString(encData));
|
||||
return EccCipher.fromBytes(encData);
|
||||
}
|
||||
|
||||
|
||||
@@ -31,4 +31,7 @@ public class SdfApiAdapterFactory {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,9 +3,17 @@ package com.sunyard.chsm.sdf.adapter;
|
||||
import com.sun.jna.Native;
|
||||
import com.sun.jna.ptr.PointerByReference;
|
||||
import com.sunyard.chsm.sdf.context.AlgId;
|
||||
import com.sunyard.chsm.sdf.context.SdrCode;
|
||||
import com.sunyard.chsm.sdf.context.SunyardAlgId;
|
||||
import com.sunyard.chsm.sdf.lib.SdfLibrary;
|
||||
import com.sunyard.chsm.sdf.lib.SunyardSdfLibrary;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -14,6 +22,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
* @author liulu
|
||||
* @since 2024/11/4
|
||||
*/
|
||||
@Slf4j
|
||||
public class SunyardJnaSdfAdaptor extends JnaSdfAdaptor {
|
||||
|
||||
private static final Map<String, SunyardSdfLibrary> SDF_LIB_MAP = new ConcurrentHashMap<>();
|
||||
@@ -37,7 +46,30 @@ public class SunyardJnaSdfAdaptor extends JnaSdfAdaptor {
|
||||
}
|
||||
|
||||
public SunyardJnaSdfAdaptor(String ip, int port, String libName, int connTimeout, int dealTimeout) {
|
||||
super(SDF_LIB_MAP.computeIfAbsent(libName, k -> Native.load(libName, SunyardSdfLibrary.class)));
|
||||
super((SdfLibrary) Proxy.newProxyInstance(ClassUtils.getDefaultClassLoader(),
|
||||
new Class[]{SdfLibrary.class},
|
||||
new InvocationHandler() {
|
||||
private final SunyardSdfLibrary sunyardSdfLibrary;
|
||||
|
||||
{
|
||||
sunyardSdfLibrary = SDF_LIB_MAP.computeIfAbsent(libName, k -> Native.load(libName, SunyardSdfLibrary.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
Object res = method.invoke(sunyardSdfLibrary, args);
|
||||
if (method.getName().startsWith("SDF_") && res instanceof Integer) {
|
||||
int resInt = (Integer) res;
|
||||
if (resInt != SdrCode.SDR_OK.getCode()) {
|
||||
log.warn("调用异常: {}, 返回值: {} -{}", method.getName(), Integer.toHexString(resInt), resInt);
|
||||
SdrCode sdrCode = SdrCode.of((Integer) res);
|
||||
throw new IllegalArgumentException(sdrCode != null ? sdrCode.getMsg() : "调用sdf接口异常: " + Integer.toHexString(resInt));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
));
|
||||
this.ip = ip;
|
||||
this.port = port;
|
||||
this.connTimeout = connTimeout;
|
||||
@@ -59,6 +91,11 @@ public class SunyardJnaSdfAdaptor extends JnaSdfAdaptor {
|
||||
return SunyardAlgId.valueOf(alg.name()).getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void checkRes(int res) {
|
||||
|
||||
}
|
||||
|
||||
public static byte[] safeStringBytes(String str) {
|
||||
if (null == str || str.isEmpty()) {
|
||||
return new byte[]{0x30};
|
||||
@@ -69,4 +106,15 @@ public class SunyardJnaSdfAdaptor extends JnaSdfAdaptor {
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public static class CheckProxy implements InvocationHandler {
|
||||
private final SdfLibrary sdfLibrary;
|
||||
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user