初始化主密钥
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.sunyard.chsm.controller;
|
||||
|
||||
import com.sunyard.chsm.service.DeviceService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 主密钥管理
|
||||
*
|
||||
* @author liulu
|
||||
* @since 2024/11/8
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/tmk")
|
||||
public class TmkController {
|
||||
|
||||
@Resource
|
||||
private DeviceService deviceService;
|
||||
|
||||
/**
|
||||
* 初始化主密钥
|
||||
*/
|
||||
@PostMapping("/init")
|
||||
public void initTmk() {
|
||||
deviceService.initTmk();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -21,4 +21,7 @@ public interface DeviceService {
|
||||
void update(DeviceDTO.DeviceSave update);
|
||||
|
||||
void delete(Long id);
|
||||
|
||||
void initTmk();
|
||||
|
||||
}
|
||||
|
||||
@@ -8,9 +8,19 @@ import com.sunyard.chsm.dto.DeviceDTO;
|
||||
import com.sunyard.chsm.enums.ManufacturerEnum;
|
||||
import com.sunyard.chsm.enums.ManufacturerModelEnum;
|
||||
import com.sunyard.chsm.mapper.SpDeviceMapper;
|
||||
import com.sunyard.chsm.mapper.TmkInfoMapper;
|
||||
import com.sunyard.chsm.model.entity.Device;
|
||||
import com.sunyard.chsm.model.entity.TmkInfo;
|
||||
import com.sunyard.chsm.sdf.adapter.BcSdfApiAdaptor;
|
||||
import com.sunyard.chsm.sdf.adapter.SdfApiAdapter;
|
||||
import com.sunyard.chsm.sdf.adapter.SdfApiAdapterFactory;
|
||||
import com.sunyard.chsm.sdf.context.DeviceContext;
|
||||
import com.sunyard.chsm.sdf.model.DeviceInfo;
|
||||
import com.sunyard.chsm.service.DeviceService;
|
||||
import com.sunyard.ssp.modules.sysconf.paramconf.entity.ParamConf;
|
||||
import com.sunyard.ssp.modules.sysconf.paramconf.mapper.ParamConfMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bouncycastle.util.encoders.Hex;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -36,6 +46,10 @@ public class DeviceServiceImpl implements DeviceService {
|
||||
|
||||
@Resource
|
||||
private SpDeviceMapper spDeviceMapper;
|
||||
@Resource
|
||||
private ParamConfMapper paramConfMapper;
|
||||
@Resource
|
||||
private TmkInfoMapper tmkInfoMapper;
|
||||
|
||||
@Override
|
||||
public Page<DeviceDTO.DeviceView> selectPageList(DeviceDTO.Query query) {
|
||||
@@ -171,6 +185,70 @@ public class DeviceServiceImpl implements DeviceService {
|
||||
spDeviceMapper.deleteById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initTmk() {
|
||||
ParamConf tmkInit = paramConfMapper.selectByKey("tmk_init");
|
||||
Assert.isTrue(tmkInit == null || !"true".equals(tmkInit.getValue()), "主密钥已经初始化");
|
||||
List<Device> conned = spDeviceMapper.selectConnedList();
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
if (CollectionUtils.isEmpty(conned)) {
|
||||
//
|
||||
BcSdfApiAdaptor sdfApi = new BcSdfApiAdaptor();
|
||||
byte[] sk = sdfApi.generateRandom("", 16);
|
||||
|
||||
byte[] publicKey = sdfApi.exportEncPublicKeyECC("", 1);
|
||||
byte[] encSk = sdfApi.externalEncryptECC("", publicKey, sk);
|
||||
|
||||
TmkInfo info = new TmkInfo();
|
||||
info.setId(IdWorker.getId());
|
||||
info.setCreateTime(now);
|
||||
info.setDeviceSerial(sdfApi.getDeviceInfo("").getDeviceSerial());
|
||||
info.setEncTmk(Hex.toHexString(encSk));
|
||||
info.setPubKey(Hex.toHexString(publicKey));
|
||||
tmkInfoMapper.insert(info);
|
||||
return;
|
||||
}
|
||||
Device device = conned.iterator().next();
|
||||
|
||||
DeviceContext context = new DeviceContext();
|
||||
context.setManufacturer(device.getManufacturer());
|
||||
context.setManufacturerModel(device.getManufacturerModel());
|
||||
context.setServiceIp(device.getServiceIp());
|
||||
context.setServicePort(device.getServicePort());
|
||||
SdfApiAdapter sdfApi = SdfApiAdapterFactory.newInstance(context);
|
||||
String dh = sdfApi.openDevice();
|
||||
String sh = sdfApi.openSession(dh);
|
||||
DeviceInfo deviceInfo = sdfApi.getDeviceInfo(sh);
|
||||
|
||||
byte[] sk = sdfApi.generateRandom(sh, 16);
|
||||
byte[] publicKey = sdfApi.exportEncPublicKeyECC(sh, 1);
|
||||
byte[] encSk = sdfApi.externalEncryptECC(sh, publicKey, sk);
|
||||
|
||||
TmkInfo info = new TmkInfo();
|
||||
info.setId(IdWorker.getId());
|
||||
info.setCreateTime(now);
|
||||
info.setDeviceSerial(deviceInfo.getDeviceSerial());
|
||||
info.setEncTmk(Hex.toHexString(encSk));
|
||||
info.setPubKey(Hex.toHexString(publicKey));
|
||||
tmkInfoMapper.insert(info);
|
||||
|
||||
//
|
||||
BcSdfApiAdaptor bcApi = new BcSdfApiAdaptor();
|
||||
byte[] bcPubK = bcApi.exportEncPublicKeyECC("", 1);
|
||||
byte[] bcEncSk = sdfApi.exchangeDigitEnvelopeBaseOnECC(sh, 1, bcPubK, encSk);
|
||||
|
||||
TmkInfo bcinfo = new TmkInfo();
|
||||
bcinfo.setId(IdWorker.getId());
|
||||
bcinfo.setCreateTime(now);
|
||||
bcinfo.setDeviceSerial(bcApi.getDeviceInfo("").getDeviceSerial());
|
||||
bcinfo.setEncTmk(Hex.toHexString(bcEncSk));
|
||||
bcinfo.setPubKey(Hex.toHexString(bcPubK));
|
||||
tmkInfoMapper.insert(bcinfo);
|
||||
sdfApi.closeSession(sh);
|
||||
sdfApi.closeDevice(dh);
|
||||
}
|
||||
|
||||
private void checkName(String name) {
|
||||
LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<Device>()
|
||||
.eq(Device::getName, name);
|
||||
|
||||
@@ -282,13 +282,13 @@ public class KeyInfoServiceImpl implements KeyInfoService {
|
||||
record.setCheckValue(checkHash);
|
||||
} else {
|
||||
EccKey eccKey = sdfApiService.genKeyPairEcc();
|
||||
byte[] d = eccKey.getPriKey().getD();
|
||||
byte[] d = eccKey.getPriKey();
|
||||
byte[] encD = sdfApiService.encryptByTMK(d);
|
||||
record.setKeyData(Hex.toHexString(encD));
|
||||
String checkHash = Hex.toHexString(sdfApiService.hash(d));
|
||||
record.setCheckValue(checkHash);
|
||||
|
||||
byte[] pubKeyBytes = eccKey.getPubKey().getPubKeyBytes();
|
||||
byte[] pubKeyBytes = eccKey.getPubKey();
|
||||
record.setPubKey(Hex.toHexString(pubKeyBytes));
|
||||
record.setPubIdx(record.getPubKey().substring(0, 8));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user