This commit is contained in:
liulu 2024-11-08 16:11:23 +08:00
parent e4af8ecbf4
commit 5dd7b9ee7e
7 changed files with 32 additions and 21 deletions

View File

@ -49,12 +49,6 @@
<groupId>net.java.dev.jna</groupId> <groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId> <artifactId>jna</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.2.1-jre</version>
</dependency>
</dependencies> </dependencies>

View File

@ -31,7 +31,7 @@ public interface SpKeyRecordMapper extends BaseMapper<KeyRecord> {
return selectOne( return selectOne(
new LambdaQueryWrapper<KeyRecord>() new LambdaQueryWrapper<KeyRecord>()
.eq(KeyRecord::getPubIdx, pubKey.substring(0, 8)) .eq(KeyRecord::getPubIdx, pubKey.substring(0, 8))
.lt(KeyRecord::getPubKey, pubKey) .eq(KeyRecord::getPubKey, pubKey)
); );
} }

View File

@ -4,13 +4,13 @@ import com.sunyard.chsm.sdf.model.DeviceInfo;
import com.sunyard.chsm.sdf.model.EccKey; import com.sunyard.chsm.sdf.model.EccKey;
import com.sunyard.chsm.sdf.model.EccPriKey; import com.sunyard.chsm.sdf.model.EccPriKey;
import com.sunyard.chsm.sdf.model.EccPubKey; import com.sunyard.chsm.sdf.model.EccPubKey;
import com.sunyard.chsm.sdf.util.LangUtils;
import com.sunyard.chsm.utils.gm.BCSM2Utils; import com.sunyard.chsm.utils.gm.BCSM2Utils;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey; import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.BigIntegers; import org.bouncycastle.util.BigIntegers;
import org.bouncycastle.util.encoders.Hex;
import java.math.BigInteger; import java.math.BigInteger;
import java.security.KeyPair; import java.security.KeyPair;
@ -27,17 +27,19 @@ public class BcSdfApiAdaptor implements SdfApiAdapter {
static { static {
deviceInfo = new DeviceInfo(); deviceInfo = new DeviceInfo();
deviceInfo.setIssuerName("bouncycastle"); deviceInfo.setIssuerName("BC");
deviceInfo.setDeviceName("2356621c1a5976bcd6fe2303e5bbf9a9dddc1c1160a521ac61257b04e12b75df"); deviceInfo.setDeviceName("BC-3000");
deviceInfo.setDeviceSerial("BC00202411051037"); deviceInfo.setDeviceSerial("BC00202411051037");
deviceInfo.setDeviceVersion(1); deviceInfo.setDeviceVersion(1);
deviceInfo.setStandardVersion(1); deviceInfo.setStandardVersion(1);
deviceInfo.setAsymAlgAbility(new long[]{7493065891348563935L, 3000543215027029126L});
deviceInfo.setSymAlgAbility(2782707529341253316L);
deviceInfo.setHashAlgAbility(7010608454676760881L);
} }
@Override @Override
public String openDevice() { public String openDevice() {
return "c95a78d9c04a557b7b46dbcb5f36cc665f7446ad73fa75f954220232292f768e"; return "c95a78d9c04a557b7b46dbcb5f36cc66";
} }
@Override @Override
@ -47,7 +49,7 @@ public class BcSdfApiAdaptor implements SdfApiAdapter {
@Override @Override
public String openSession(String deviceHandle) { public String openSession(String deviceHandle) {
return "6975feaffaa35b31b6d4e4555ac403a1ad82f46c3d3ce0ee5005e397d3d07fed"; return "6975feaffaa35b31b6d4e4555ac403a1";
} }
@Override @Override
@ -69,11 +71,18 @@ public class BcSdfApiAdaptor implements SdfApiAdapter {
@Override @Override
public EccPubKey exportEncPublicKeyECC(String sessionHandle, int uiKeyIndex) { public EccPubKey exportEncPublicKeyECC(String sessionHandle, int uiKeyIndex) {
BigInteger d = new BigInteger(1, Hex.decode(deviceInfo.getDeviceName())); BigInteger d = new BigInteger(1, getD());
ECPoint q = BCSM2Utils.G_POINT.multiply(d).normalize(); ECPoint q = BCSM2Utils.G_POINT.multiply(d).normalize();
return new EccPubKey(256, q.getXCoord().getEncoded(), q.getYCoord().getEncoded()); return new EccPubKey(256, q.getXCoord().getEncoded(), q.getYCoord().getEncoded());
} }
private byte[] getD() {
return LangUtils.merge(LangUtils.toByteArray(deviceInfo.getAsymAlgAbility()[0]),
LangUtils.toByteArray(deviceInfo.getAsymAlgAbility()[1]),
LangUtils.toByteArray(deviceInfo.getSymAlgAbility()),
LangUtils.toByteArray(deviceInfo.getHashAlgAbility()));
}
@SneakyThrows @SneakyThrows
@Override @Override
public EccKey generateKeyPairECC(String sessionHandle, String alg, int uiKeyBits) { public EccKey generateKeyPairECC(String sessionHandle, String alg, int uiKeyBits) {

View File

@ -101,7 +101,7 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
deviceInfo.setDeviceSerial(new String(sdfInfo.DeviceSerial)); deviceInfo.setDeviceSerial(new String(sdfInfo.DeviceSerial));
deviceInfo.setDeviceVersion(sdfInfo.DeviceVersion); deviceInfo.setDeviceVersion(sdfInfo.DeviceVersion);
deviceInfo.setStandardVersion(sdfInfo.StandardVersion); deviceInfo.setStandardVersion(sdfInfo.StandardVersion);
deviceInfo.setAsymAlgAbility(sdfInfo.AsymAlgAbility); deviceInfo.setAsymAlgAbility(new long[]{sdfInfo.AsymAlgAbility[0], sdfInfo.AsymAlgAbility[1]});
deviceInfo.setSymAlgAbility(sdfInfo.SymAlgAbility); deviceInfo.setSymAlgAbility(sdfInfo.SymAlgAbility);
deviceInfo.setHashAlgAbility(sdfInfo.HashAlgAbility); deviceInfo.setHashAlgAbility(sdfInfo.HashAlgAbility);
deviceInfo.setBufferSize(sdfInfo.BufferSize); deviceInfo.setBufferSize(sdfInfo.BufferSize);

View File

@ -15,9 +15,9 @@ public class DeviceInfo {
private String deviceSerial; private String deviceSerial;
private int deviceVersion; private int deviceVersion;
private int standardVersion; private int standardVersion;
private int[] asymAlgAbility; private long[] asymAlgAbility;
private int symAlgAbility; private long symAlgAbility;
private int hashAlgAbility; private long hashAlgAbility;
private int bufferSize; private long bufferSize;
} }

View File

@ -2,6 +2,7 @@ package com.sunyard.chsm.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.chsm.dto.CertDTO; import com.sunyard.chsm.dto.CertDTO;
import com.sunyard.chsm.enums.KeyCategory; import com.sunyard.chsm.enums.KeyCategory;
@ -215,6 +216,7 @@ public class AppCertServiceImpl implements AppCertService {
private AppCert genCert(X509Certificate x509Cert, Long appId, KeyRecord record, CertDTO.ImportCert importCert) { private AppCert genCert(X509Certificate x509Cert, Long appId, KeyRecord record, CertDTO.ImportCert importCert) {
AppCert cert = new AppCert(); AppCert cert = new AppCert();
cert.setId(IdWorker.getId());
cert.setApplicationId(appId); cert.setApplicationId(appId);
cert.setKeyId(record.getKeyId()); cert.setKeyId(record.getKeyId());
cert.setKeyRecordId(record.getId()); cert.setKeyRecordId(record.getId());
@ -238,7 +240,7 @@ public class AppCertServiceImpl implements AppCertService {
private static Pair<String, byte[]> decryptEnvelopedKey(byte[] d, byte[] envelopedKey) throws Exception { private static Pair<String, byte[]> decryptEnvelopedKey(byte[] d, byte[] envelopedKey) throws Exception {
ECPrivateKeyParameters pri = BCECUtils.createECPrivateKeyParameters(Hex.decode(d)); ECPrivateKeyParameters pri = BCECUtils.createECPrivateKeyParameters(d);
ASN1Sequence seq = DLSequence.getInstance(envelopedKey); ASN1Sequence seq = DLSequence.getInstance(envelopedKey);
AlgorithmIdentifier oid = AlgorithmIdentifier.getInstance(seq.getObjectAt(0)); AlgorithmIdentifier oid = AlgorithmIdentifier.getInstance(seq.getObjectAt(0));

View File

@ -2,6 +2,7 @@ package com.sunyard.chsm.task;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.jna.Platform;
import com.sunyard.chsm.mapper.SpDeviceMapper; import com.sunyard.chsm.mapper.SpDeviceMapper;
import com.sunyard.chsm.model.entity.Device; import com.sunyard.chsm.model.entity.Device;
import com.sunyard.chsm.sdf.adapter.SdfApiAdapter; import com.sunyard.chsm.sdf.adapter.SdfApiAdapter;
@ -88,6 +89,11 @@ public class DeviceTask implements InitializingBean {
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
if (Platform.isLinux() || Platform.isWindows()) {
threadPoolTaskScheduler.scheduleWithFixedDelay(this::checkDeviceStatus, Duration.ofMinutes(5L)); threadPoolTaskScheduler.scheduleWithFixedDelay(this::checkDeviceStatus, Duration.ofMinutes(5L));
} else {
log.warn("操作系统: {} 不支持启动检查设备状态定时任务", System.getProperty("os.name"));
}
} }
} }