sdf 接口
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package com.sunyard.chsm.sdf.adapter;
|
||||
|
||||
import com.sun.jna.Pointer;
|
||||
import com.sun.jna.ptr.PointerByReference;
|
||||
import com.sunyard.chsm.sdf.lib.SdfLibrary;
|
||||
import com.sunyard.chsm.sdf.model.DeviceInfo;
|
||||
import com.sunyard.chsm.sdf.model.EccKey;
|
||||
import com.sunyard.chsm.sdf.model.EccPubKey;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/11/4
|
||||
*/
|
||||
public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
||||
|
||||
protected static final Map<String, Pointer> DEVICE_HANDLE_CONTEXT = new ConcurrentHashMap<>();
|
||||
protected static final Map<String, Pointer> SESSION_HANDLE_CONTEXT = new ConcurrentHashMap<>();
|
||||
protected static final Map<String, Pointer> KEY_HANDLE_CONTEXT = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
protected abstract SdfLibrary getSdfLibrary();
|
||||
|
||||
protected abstract int getAlgId(String alg);
|
||||
|
||||
@Override
|
||||
public String openDevice() {
|
||||
PointerByReference phDeviceHandle = new PointerByReference();
|
||||
getSdfLibrary().SDF_OpenDevice(phDeviceHandle);
|
||||
String key = UUID.randomUUID().toString();
|
||||
DEVICE_HANDLE_CONTEXT.put(key, phDeviceHandle.getValue());
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean closeDevice(String deviceHandle) {
|
||||
Pointer pointer = DEVICE_HANDLE_CONTEXT.remove(deviceHandle);
|
||||
if (pointer != null) {
|
||||
int res = getSdfLibrary().SDF_CloseDevice(pointer);
|
||||
return res == 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String openSession(String deviceHandle) {
|
||||
Pointer device = getDeviceHandle(deviceHandle);
|
||||
|
||||
PointerByReference phSessionHandle = new PointerByReference();
|
||||
getSdfLibrary().SDF_OpenSession(device, phSessionHandle);
|
||||
|
||||
String key = UUID.randomUUID().toString();
|
||||
SESSION_HANDLE_CONTEXT.put(key, phSessionHandle.getValue());
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeSession(String sessionHandle) {
|
||||
Pointer pointer = SESSION_HANDLE_CONTEXT.remove(sessionHandle);
|
||||
if (pointer != null) {
|
||||
getSdfLibrary().SDF_CloseSession(pointer);
|
||||
}
|
||||
}
|
||||
|
||||
protected Pointer getDeviceHandle(String deviceHandle) {
|
||||
Pointer pointer = DEVICE_HANDLE_CONTEXT.get(deviceHandle);
|
||||
Assert.notNull(pointer, "设备句柄不存在");
|
||||
return pointer;
|
||||
}
|
||||
|
||||
protected Pointer getSessionHandle(String sessionHandle) {
|
||||
Pointer pointer = SESSION_HANDLE_CONTEXT.get(sessionHandle);
|
||||
Assert.notNull(pointer, "会话句柄不存在");
|
||||
return pointer;
|
||||
}
|
||||
|
||||
protected Pointer getKeyHandle(String keyHandle) {
|
||||
Pointer pointer = KEY_HANDLE_CONTEXT.get(keyHandle);
|
||||
Assert.notNull(pointer, "密钥句柄不存在");
|
||||
return pointer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DeviceInfo getDeviceInfo(String sessionHandle) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] generateRandom(String sessionHandle, int uiLength) {
|
||||
byte[] pucRandom = new byte[uiLength];
|
||||
Pointer hSessionHandle = getSessionHandle(sessionHandle);
|
||||
getSdfLibrary().SDF_GenerateRandom(hSessionHandle, uiLength, pucRandom);
|
||||
return pucRandom;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EccPubKey exportEncPublicKeyECC(String sessionHandle, int uiKeyIndex) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EccKey generateKeyPairECC(String sessionHandle, String alg, int uiKeyBits) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.sunyard.chsm.sdf.adapter;
|
||||
|
||||
import com.sunyard.chsm.sdf.model.DeviceInfo;
|
||||
import com.sunyard.chsm.sdf.model.EccKey;
|
||||
import com.sunyard.chsm.sdf.model.EccPubKey;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/11/4
|
||||
*/
|
||||
public interface SdfApiAdapter {
|
||||
|
||||
/**
|
||||
* 打开设备
|
||||
*
|
||||
* @return 设 备 句 柄
|
||||
*/
|
||||
String openDevice();
|
||||
|
||||
/**
|
||||
* 关闭设备
|
||||
*
|
||||
* @param deviceHandle 已打开的设备句柄
|
||||
* @return 0 成功; 非0 失败,返回错误代码
|
||||
*/
|
||||
boolean closeDevice(String deviceHandle);
|
||||
|
||||
/**
|
||||
* 创建会话
|
||||
*
|
||||
* @return 与密码设备建立的新会话句柄
|
||||
*/
|
||||
String openSession(String deviceHandle);
|
||||
|
||||
void closeSession(String sessionHandle);
|
||||
|
||||
/**
|
||||
* 获取设备信息
|
||||
*
|
||||
* @param sessionHandle 与设备建立的会话句柄
|
||||
* @return 设备能力描述信息,内容及格式见设备信息定义
|
||||
*/
|
||||
DeviceInfo getDeviceInfo(String sessionHandle);
|
||||
|
||||
/**
|
||||
* 产生随机数
|
||||
*
|
||||
* @param uiLength 随机数长度
|
||||
* @return pucRandom 返回随机数
|
||||
*/
|
||||
byte[] generateRandom(String sessionHandle, int uiLength);
|
||||
|
||||
/**
|
||||
* 导出ECC加密公钥
|
||||
*
|
||||
* @param uiKeyIndex 密码设备存储的ECC密钥对索引值
|
||||
* @return pucPublicKeyEcc 返回ECC加密公钥
|
||||
*/
|
||||
EccPubKey exportEncPublicKeyECC(String sessionHandle, int uiKeyIndex);
|
||||
|
||||
/**
|
||||
* 产生ECC密钥对并输出
|
||||
*
|
||||
* @param alg 指定算法标识 SGD_SM2_1
|
||||
* @param uiKeyBits 指定密钥模长
|
||||
* @return pucPublicKeyEcc 返回公钥 | pucPrivateKeyEcc 返回私钥
|
||||
*/
|
||||
EccKey generateKeyPairECC(String sessionHandle, String alg, int uiKeyBits);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.sunyard.chsm.sdf.adapter;
|
||||
|
||||
import com.sunyard.chsm.enums.ManufacturerModelEnum;
|
||||
import com.sunyard.chsm.sdf.context.DeviceContext;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/11/5
|
||||
*/
|
||||
public class SdfApiAdapterFactory {
|
||||
|
||||
|
||||
public static SdfApiAdapter newInstance(DeviceContext device) {
|
||||
|
||||
Assert.hasText(device.getManufacturerModel(), "设备型号不能为空");
|
||||
ManufacturerModelEnum model = ManufacturerModelEnum.of(device.getManufacturerModel());
|
||||
|
||||
if (Objects.isNull(model)) {
|
||||
// bc adaptor
|
||||
return null;
|
||||
}
|
||||
switch (model) {
|
||||
case enc001:
|
||||
return new SunyardJnaSdfAdaptor(device.getServiceIp(), device.getServicePort());
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.sunyard.chsm.sdf.adapter;
|
||||
|
||||
import com.sun.jna.Native;
|
||||
import com.sun.jna.ptr.PointerByReference;
|
||||
import com.sunyard.chsm.sdf.lib.SdfLibrary;
|
||||
import com.sunyard.chsm.sdf.lib.SunyardSdfLibrary;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/11/4
|
||||
*/
|
||||
public class SunyardJnaSdfAdaptor extends JnaSdfAdaptor {
|
||||
|
||||
private static final Map<String, SunyardSdfLibrary> SDF_LIB_MAP = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
private final String ip;
|
||||
private final Integer port;
|
||||
private final String libName;
|
||||
private final Integer connTimeout;
|
||||
private final Integer dealTimeout;
|
||||
|
||||
public SunyardJnaSdfAdaptor(String ip, int port) {
|
||||
this(ip, port, "libsdf");
|
||||
}
|
||||
|
||||
public SunyardJnaSdfAdaptor(String ip, int port, String libName) {
|
||||
this(ip, port, libName, 3000, 3000);
|
||||
}
|
||||
|
||||
public SunyardJnaSdfAdaptor(String ip, int port, String libName, int connTimeout, int dealTimeout) {
|
||||
this.ip = ip;
|
||||
this.port = port;
|
||||
this.libName = libName;
|
||||
this.connTimeout = connTimeout;
|
||||
this.dealTimeout = dealTimeout;
|
||||
SDF_LIB_MAP.computeIfAbsent(libName, k -> Native.load(libName, SunyardSdfLibrary.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String openDevice() {
|
||||
SunyardSdfLibrary sdfLibrary = (SunyardSdfLibrary) getSdfLibrary();
|
||||
PointerByReference phDeviceHandle = new PointerByReference();
|
||||
sdfLibrary.SDF_OpenDevice(phDeviceHandle, safeStringBytes(ip), port, connTimeout, dealTimeout, 0);
|
||||
String key = UUID.randomUUID().toString();
|
||||
DEVICE_HANDLE_CONTEXT.put(key, phDeviceHandle.getValue());
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SdfLibrary getSdfLibrary() {
|
||||
return SDF_LIB_MAP.get(libName);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getAlgId(String alg) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static byte[] safeStringBytes(String str) {
|
||||
if (null == str || str.isEmpty()) {
|
||||
return new byte[]{0x30};
|
||||
}
|
||||
int len = str.getBytes().length;
|
||||
byte[] ret = new byte[len + 1];
|
||||
System.arraycopy(str.getBytes(), 0, ret, 0, len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user