整理代码
This commit is contained in:
parent
971da4fd17
commit
823c69c72b
@ -1,504 +0,0 @@
|
||||
package com.sunyard.cisd.device.tool;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.PublicKey;
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
|
||||
private static final String DEVICE_DIR = "/home/tms/device";
|
||||
public static final String SIGN_FILE = DEVICE_DIR + "/dev.sign";
|
||||
private static final String SIGN_SM3_FILE = DEVICE_DIR + "/dev.sign.sm3";
|
||||
private static final String PUBLIC_KEY_FILE = DEVICE_DIR + "/dev.fingerprint.pub";
|
||||
private static final String PUBLIC_KEY_SM3_FILE = DEVICE_DIR + "/dev.fingerprint.pub.sm3";
|
||||
public static final String FINGERPRINT_DATA_FILE = DEVICE_DIR + "/dev.fingerprint.data";
|
||||
private static final String FINGERPRINT_PRIVATE_KEY_PATH = "/fingerprint_sm2_private_key.pem";
|
||||
public static final String FINGERPRINT_PUBLIC_KEY_PATH = "/fingerprint_sm2_public_key.pem";
|
||||
private static final String MAC_PRIVATE_KEY_PATH = "/mac_sm2_private_key.pem";
|
||||
public static final String MAC_PUBLIC_KEY_PATH = "/mac_sm2_public_key.pem";
|
||||
private static final String ROOT_CERT_FILE = "/CFCA_TEST_CS_SM2_CA.cer";
|
||||
private static final String ROOT_CERT_OUTPUT_FILE = DEVICE_DIR + "/CFCA_TEST_CS_SM2_CA.cer";
|
||||
private static final String ROOT_CERT_SIGN_FILE = DEVICE_DIR + "/rootcert.sign";
|
||||
private static final String MAC_PUBLIC_KEY_OUTPUT_FILE = DEVICE_DIR + "/dev.mac.pub";
|
||||
private static final String MAC_PUBLIC_KEY_SM3_FILE = DEVICE_DIR + "/dev.mac.pub.sm3";
|
||||
public static final String MAC_LIST_FILE = "/home/tms/mac.list";
|
||||
public static final String MAC_SIGN_FILE = DEVICE_DIR + "/dev.mac.sign";
|
||||
|
||||
public static final String CMEP_MAC_LIST_FILE = "/home/cmep4i/cmep.mac.list";
|
||||
public static final String CMEP_MAC_SIGN_FILE = "/home/cmep4i/cmep.mac.sign";
|
||||
|
||||
public static final String[] MAC_TARGETS = {
|
||||
"/home/tms/tms-framework.jar",
|
||||
"/home/tms/config",
|
||||
"/home/tms/libs",
|
||||
"/home/tms/web",
|
||||
"/home/tms/bin",
|
||||
"/home/tms/scripts/tms.sh"
|
||||
};
|
||||
|
||||
public static final String[] CMEP_TARGETS = {
|
||||
"/home/cmep4i/cmsp/CMEP-CMSP.jar",
|
||||
"/home/cmep4i/cmtp/CMEP-CMTP.jar"
|
||||
};
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length == 0) {
|
||||
printHelp();
|
||||
return;
|
||||
}
|
||||
|
||||
String command = args[0];
|
||||
|
||||
try {
|
||||
switch (command) {
|
||||
case "help":
|
||||
printHelp();
|
||||
break;
|
||||
case "info":
|
||||
showDeviceInfo();
|
||||
break;
|
||||
case "fingerprint-gen":
|
||||
generateFingerprint();
|
||||
break;
|
||||
|
||||
case "fingerprint-vertify":
|
||||
verifyFingerprint();
|
||||
break;
|
||||
case "tms-mac-gen":
|
||||
generateMAC();
|
||||
break;
|
||||
case "tms-mac-vertify":
|
||||
verifyMAC();
|
||||
break;
|
||||
case "cmep-mac-gen":
|
||||
generateCMEPMAC();
|
||||
break;
|
||||
case "cmep-mac-vertify":
|
||||
verifyCMEPMAC();
|
||||
break;
|
||||
case "rootcert-mac-gen":
|
||||
generateRootCertMAC();
|
||||
break;
|
||||
case "rootcert-mac-vertify":
|
||||
verifyRootCertMAC();
|
||||
break;
|
||||
default:
|
||||
System.err.println("未知命令: " + command);
|
||||
printHelp();
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("处理过程中发生错误: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void printHelp() {
|
||||
System.out.println("设备指纹工具 - 使用说明");
|
||||
System.out.println();
|
||||
System.out.println("可用命令:");
|
||||
System.out.println(" help - 显示帮助信息");
|
||||
System.out.println(" info - 获取并显示设备信息");
|
||||
System.out.println(" fingerprint-gen - 获取设备信息并生成设备指纹输出到文件");
|
||||
System.out.println(" fingerprint-vertify - 获取设备信息并验证设备指纹");
|
||||
System.out.println(" tms-mac-gen - 生成TMS系统文件MAC校验值并签名");
|
||||
System.out.println(" tms-mac-vertify - 验证TMS系统文件MAC校验值");
|
||||
System.out.println(" cmep-mac-gen - 生成CMEP文件MAC校验值并签名");
|
||||
System.out.println(" cmep-mac-vertify - 验证CMEP文件MAC校验值");
|
||||
System.out.println(" rootcert-mac-gen - 生成根证书MAC签名");
|
||||
System.out.println(" rootcert-mac-vertify - 验证根证书MAC签名");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
private static void showDeviceInfo() throws Exception {
|
||||
DeviceFingerprintService service = new DeviceFingerprintService();
|
||||
DeviceFingerprint fingerprint = service.getDeviceFingerprint();
|
||||
|
||||
System.out.println("=== 设备信息 ===");
|
||||
System.out.println("主板序列号: " + fingerprint.getMotherboardSerialNumber());
|
||||
System.out.println("设备序列号: " + fingerprint.getDeviceSerialNumber());
|
||||
System.out.println("CPU型号: " + fingerprint.getCpuModel());
|
||||
System.out.println("CPU序列号: " + fingerprint.getCpuSerialNumber());
|
||||
System.out.println("内存大小: " + fingerprint.getMemorySize());
|
||||
System.out.println("首块网卡MAC地址: " + fingerprint.getFirstNetworkMacAddress());
|
||||
System.out.println("硬盘序列号: " + fingerprint.getHardDiskSerialNumber());
|
||||
}
|
||||
|
||||
public static String buildPlainText(DeviceFingerprint fingerprint) {
|
||||
return fingerprint.getMotherboardSerialNumber() +
|
||||
fingerprint.getDeviceSerialNumber() +
|
||||
fingerprint.getCpuModel() +
|
||||
fingerprint.getCpuSerialNumber() +
|
||||
fingerprint.getMemorySize() +
|
||||
fingerprint.getFirstNetworkMacAddress() +
|
||||
fingerprint.getHardDiskSerialNumber();
|
||||
}
|
||||
|
||||
private static void generateFingerprint() throws Exception {
|
||||
DeviceFingerprintService service = new DeviceFingerprintService();
|
||||
DeviceFingerprint fingerprint = service.getDeviceFingerprint();
|
||||
|
||||
System.out.println("=== 设备指纹信息 ===");
|
||||
System.out.println("主板序列号: " + fingerprint.getMotherboardSerialNumber());
|
||||
System.out.println("设备序列号: " + fingerprint.getDeviceSerialNumber());
|
||||
System.out.println("CPU型号: " + fingerprint.getCpuModel());
|
||||
System.out.println("CPU序列号: " + fingerprint.getCpuSerialNumber());
|
||||
System.out.println("内存大小: " + fingerprint.getMemorySize());
|
||||
System.out.println("首块网卡MAC地址: " + fingerprint.getFirstNetworkMacAddress());
|
||||
System.out.println("硬盘序列号: " + fingerprint.getHardDiskSerialNumber());
|
||||
|
||||
String plainText = buildPlainText(fingerprint);
|
||||
System.out.println("\n拼接后的原文: " + plainText);
|
||||
|
||||
byte[] signature = SM2Util.sign(plainText.getBytes(StandardCharsets.UTF_8), FINGERPRINT_PRIVATE_KEY_PATH);
|
||||
System.out.println("签名结果(Base64): " + Base64.getEncoder().encodeToString(signature));
|
||||
|
||||
PublicKey publicKey = SM2Util.loadPublicKey(FINGERPRINT_PUBLIC_KEY_PATH);
|
||||
String publicKeyPEM = SM2Util.publicKeyToPEM(publicKey);
|
||||
|
||||
SM3Util.writeFile(SIGN_FILE, signature);
|
||||
System.out.println("签名文件已保存: " + SIGN_FILE);
|
||||
|
||||
String signSm3 = SM3Util.sm3DigestHex(signature);
|
||||
SM3Util.writeFile(SIGN_SM3_FILE, signSm3);
|
||||
System.out.println("签名SM3校验值已保存: " + SIGN_SM3_FILE + " (" + signSm3 + ")");
|
||||
|
||||
SM3Util.writeFile(PUBLIC_KEY_FILE, publicKeyPEM);
|
||||
System.out.println("公钥文件已保存: " + PUBLIC_KEY_FILE);
|
||||
|
||||
String publicKeySm3 = SM3Util.sm3DigestHex(publicKeyPEM.getBytes(StandardCharsets.UTF_8));
|
||||
SM3Util.writeFile(PUBLIC_KEY_SM3_FILE, publicKeySm3);
|
||||
System.out.println("公钥SM3校验值已保存: " + PUBLIC_KEY_SM3_FILE + " (" + publicKeySm3 + ")");
|
||||
|
||||
SM3Util.writeFile(FINGERPRINT_DATA_FILE, plainText);
|
||||
System.out.println("设备指纹数据已保存: " + FINGERPRINT_DATA_FILE);
|
||||
|
||||
boolean verified = SM2Util.verify(plainText.getBytes(StandardCharsets.UTF_8), signature, publicKey);
|
||||
System.out.println("\n签名验证结果: " + verified);
|
||||
}
|
||||
|
||||
private static void verifyFingerprint() throws Exception {
|
||||
DeviceFingerprintService service = new DeviceFingerprintService();
|
||||
DeviceFingerprint fingerprint = service.getDeviceFingerprint();
|
||||
|
||||
System.out.println("=== 当前设备信息 ===");
|
||||
System.out.println("主板序列号: " + fingerprint.getMotherboardSerialNumber());
|
||||
System.out.println("设备序列号: " + fingerprint.getDeviceSerialNumber());
|
||||
System.out.println("CPU型号: " + fingerprint.getCpuModel());
|
||||
System.out.println("CPU序列号: " + fingerprint.getCpuSerialNumber());
|
||||
System.out.println("内存大小: " + fingerprint.getMemorySize());
|
||||
System.out.println("首块网卡MAC地址: " + fingerprint.getFirstNetworkMacAddress());
|
||||
System.out.println("硬盘序列号: " + fingerprint.getHardDiskSerialNumber());
|
||||
|
||||
String plainText = buildPlainText(fingerprint);
|
||||
// System.out.println("\n当前拼接原文: " + plainText);
|
||||
|
||||
byte[] signature = SM3Util.readFileBytes(SIGN_FILE);
|
||||
PublicKey publicKey = SM2Util.loadPublicKey(FINGERPRINT_PUBLIC_KEY_PATH);
|
||||
|
||||
|
||||
boolean signatureVerified = SM2Util.verify(plainText.getBytes(StandardCharsets.UTF_8), signature, publicKey);
|
||||
|
||||
System.out.println("\n=== 验证结果 ===");
|
||||
System.out.println("签名验证: " + signatureVerified);
|
||||
if( Files.exists( Paths.get(FINGERPRINT_DATA_FILE)) ) {
|
||||
String savedPlainText = SM3Util.readFile(FINGERPRINT_DATA_FILE).trim();
|
||||
// System.out.println("保存的拼接原文: " + savedPlainText);
|
||||
boolean dataMatch = plainText.equals(savedPlainText);
|
||||
System.out.println("设备信息与保存的信息匹配: " + dataMatch);
|
||||
}
|
||||
|
||||
// System.out.println("总体验证: " + (dataMatch && signatureVerified));
|
||||
}
|
||||
|
||||
private static void generateMAC() throws Exception {
|
||||
System.out.println("=== 生成系统文件MAC校验值 ===");
|
||||
System.out.println("目标路径:");
|
||||
for (String target : MAC_TARGETS) {
|
||||
System.out.println(" " + target);
|
||||
}
|
||||
|
||||
Map<String, String> sm3Map = new java.util.LinkedHashMap<>();
|
||||
|
||||
for (String target : MAC_TARGETS) {
|
||||
File file = new File(target);
|
||||
if (!file.exists()) {
|
||||
System.out.println("警告: 路径不存在 - " + target);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (file.isFile()) {
|
||||
String sm3Value = SM3Util.sm3FileHex(target);
|
||||
sm3Map.put(target, sm3Value);
|
||||
System.out.println("文件: " + target + " -> " + sm3Value);
|
||||
} else if (file.isDirectory()) {
|
||||
Map<String, String> dirSm3Map = SM3Util.calculateDirectorySM3(target);
|
||||
for (Map.Entry<String, String> entry : dirSm3Map.entrySet()) {
|
||||
String absolutePath = target + "/" + entry.getKey();
|
||||
sm3Map.put(absolutePath, entry.getValue());
|
||||
System.out.println("文件: " + absolutePath + " -> " + entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder macListContent = new StringBuilder();
|
||||
for (Map.Entry<String, String> entry : sm3Map.entrySet()) {
|
||||
macListContent.append(entry.getKey()).append("|").append(entry.getValue()).append("\n");
|
||||
}
|
||||
|
||||
SM3Util.writeFile(MAC_LIST_FILE, macListContent.toString());
|
||||
System.out.println("\nMAC列表文件已保存: " + MAC_LIST_FILE);
|
||||
|
||||
byte[] signature = SM2Util.sign(macListContent.toString().getBytes(StandardCharsets.UTF_8), MAC_PRIVATE_KEY_PATH);
|
||||
SM3Util.writeFile(MAC_SIGN_FILE, signature);
|
||||
System.out.println("MAC签名文件已保存: " + MAC_SIGN_FILE);
|
||||
|
||||
PublicKey publicKey = SM2Util.loadPublicKey(MAC_PUBLIC_KEY_PATH);
|
||||
boolean verified = SM2Util.verify(macListContent.toString().getBytes(StandardCharsets.UTF_8), signature, publicKey);
|
||||
System.out.println("\n签名验证结果: " + verified);
|
||||
}
|
||||
|
||||
private static void verifyMAC() throws Exception {
|
||||
System.out.println("=== 验证系统文件MAC校验值 ===");
|
||||
|
||||
PublicKey publicKey = SM2Util.loadPublicKey(MAC_PUBLIC_KEY_PATH);
|
||||
byte[] savedSignature = SM3Util.readFileBytes(MAC_SIGN_FILE);
|
||||
String savedMacList = SM3Util.readFile(MAC_LIST_FILE);
|
||||
|
||||
boolean macListSignatureValid = SM2Util.verify(savedMacList.getBytes(StandardCharsets.UTF_8), savedSignature, publicKey);
|
||||
System.out.println("MAC列表签名验证: " + (macListSignatureValid ? "通过" : "失败"));
|
||||
|
||||
if (!macListSignatureValid) {
|
||||
System.out.println("错误: mac.list 签名验证失败,可能被篡改!");
|
||||
System.out.println("\n=== 验证结果 ===");
|
||||
System.out.println("完整性验证: false");
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, String> savedSm3Map = SM3Util.readSM3File(MAC_LIST_FILE);
|
||||
|
||||
Map<String, String> currentSm3Map = new java.util.LinkedHashMap<>();
|
||||
for (String target : MAC_TARGETS) {
|
||||
File file = new File(target);
|
||||
if (!file.exists()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (file.isFile()) {
|
||||
String sm3Value = SM3Util.sm3FileHex(target);
|
||||
currentSm3Map.put(target, sm3Value);
|
||||
} else if (file.isDirectory()) {
|
||||
Map<String, String> dirSm3Map = SM3Util.calculateDirectorySM3(target);
|
||||
for (Map.Entry<String, String> entry : dirSm3Map.entrySet()) {
|
||||
String absolutePath = target + "/" + entry.getKey();
|
||||
currentSm3Map.put(absolutePath, entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean allMatch = true;
|
||||
|
||||
for (Map.Entry<String, String> entry : savedSm3Map.entrySet()) {
|
||||
String filePath = entry.getKey();
|
||||
String savedHash = entry.getValue();
|
||||
String currentHash = currentSm3Map.get(filePath);
|
||||
|
||||
if (currentHash == null) {
|
||||
System.out.println("文件缺失: " + filePath);
|
||||
allMatch = false;
|
||||
} else if (!savedHash.equals(currentHash)) {
|
||||
System.out.println("文件被修改: " + filePath + " (期望: " + savedHash + ", 实际: " + currentHash + ")");
|
||||
allMatch = false;
|
||||
}
|
||||
}
|
||||
|
||||
for (String filePath : currentSm3Map.keySet()) {
|
||||
if (!savedSm3Map.containsKey(filePath)) {
|
||||
System.out.println("新增文件: " + filePath);
|
||||
allMatch = false;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println("=== 验证结果 ===");
|
||||
System.out.println("完整性验证: " + allMatch);
|
||||
}
|
||||
|
||||
private static void generateRootCertMAC() throws Exception {
|
||||
System.out.println("=== 生成根证书MAC签名 ===");
|
||||
|
||||
InputStream certInputStream = Main.class.getResourceAsStream(ROOT_CERT_FILE);
|
||||
if (certInputStream == null) {
|
||||
throw new IllegalArgumentException("根证书文件未找到: " + ROOT_CERT_FILE);
|
||||
}
|
||||
|
||||
byte[] certData = readInputStream(certInputStream);
|
||||
|
||||
SM3Util.writeFile(ROOT_CERT_OUTPUT_FILE, certData);
|
||||
System.out.println("根证书已输出: " + ROOT_CERT_OUTPUT_FILE);
|
||||
|
||||
byte[] signature = SM2Util.sign(certData, MAC_PRIVATE_KEY_PATH);
|
||||
SM3Util.writeFile(ROOT_CERT_SIGN_FILE, signature);
|
||||
System.out.println("签名文件已保存: " + ROOT_CERT_SIGN_FILE);
|
||||
|
||||
PublicKey macPublicKey = SM2Util.loadPublicKey(MAC_PUBLIC_KEY_PATH);
|
||||
String publicKeyPEM = SM2Util.publicKeyToPEM(macPublicKey);
|
||||
SM3Util.writeFile(MAC_PUBLIC_KEY_OUTPUT_FILE, publicKeyPEM);
|
||||
System.out.println("MAC公钥文件已保存: " + MAC_PUBLIC_KEY_OUTPUT_FILE);
|
||||
|
||||
String publicKeySm3 = SM3Util.sm3DigestHex(macPublicKey.getEncoded());
|
||||
SM3Util.writeFile(MAC_PUBLIC_KEY_SM3_FILE, publicKeySm3);
|
||||
System.out.println("MAC公钥SM3校验值已保存: " + MAC_PUBLIC_KEY_SM3_FILE + " (" + publicKeySm3 + ")");
|
||||
|
||||
boolean verified = SM2Util.verify(certData, signature, macPublicKey);
|
||||
System.out.println("\n签名验证结果: " + verified);
|
||||
}
|
||||
|
||||
private static void verifyRootCertMAC() throws Exception {
|
||||
System.out.println("=== 验证根证书MAC签名 ===");
|
||||
|
||||
String savedPublicKeySm3 = SM3Util.readFile(MAC_PUBLIC_KEY_SM3_FILE).trim();
|
||||
|
||||
PublicKey publicKey = SM2Util.loadPublicKey(MAC_PUBLIC_KEY_PATH);
|
||||
String currentPublicKeySm3 = SM3Util.sm3DigestHex(publicKey.getEncoded());
|
||||
boolean publicKeyValid = savedPublicKeySm3.equals(currentPublicKeySm3);
|
||||
System.out.println("公钥校验值验证: " + publicKeyValid);
|
||||
|
||||
InputStream certInputStream = Main.class.getResourceAsStream(ROOT_CERT_FILE);
|
||||
if (certInputStream == null) {
|
||||
throw new IllegalArgumentException("根证书文件未找到: " + ROOT_CERT_FILE);
|
||||
}
|
||||
byte[] certData = readInputStream(certInputStream);
|
||||
|
||||
byte[] signature = SM3Util.readFileBytes(ROOT_CERT_SIGN_FILE);
|
||||
|
||||
boolean signatureVerified = SM2Util.verify(certData, signature, publicKey);
|
||||
System.out.println("签名验证结果: " + signatureVerified);
|
||||
|
||||
System.out.println();
|
||||
System.out.println("=== 验证结果 ===");
|
||||
System.out.println("总体验证: " + (publicKeyValid && signatureVerified));
|
||||
}
|
||||
|
||||
private static byte[] readInputStream(InputStream is) throws Exception {
|
||||
try {
|
||||
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[8192];
|
||||
int len;
|
||||
while ((len = is.read(buffer)) != -1) {
|
||||
baos.write(buffer, 0, len);
|
||||
}
|
||||
return baos.toByteArray();
|
||||
} finally {
|
||||
if (is != null) {
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void generateCMEPMAC() throws Exception {
|
||||
System.out.println("=== 生成CMEP文件MAC校验值 ===");
|
||||
System.out.println("目标路径:");
|
||||
for (String target : CMEP_TARGETS) {
|
||||
System.out.println(" " + target);
|
||||
}
|
||||
|
||||
Map<String, String> sm3Map = new java.util.LinkedHashMap<>();
|
||||
|
||||
for (String target : CMEP_TARGETS) {
|
||||
File file = new File(target);
|
||||
if (!file.exists()) {
|
||||
System.out.println("警告: 路径不存在 - " + target);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (file.isFile()) {
|
||||
String sm3Value = SM3Util.sm3FileHex(target);
|
||||
sm3Map.put(target, sm3Value);
|
||||
System.out.println("文件: " + target + " -> " + sm3Value);
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder macListContent = new StringBuilder();
|
||||
for (Map.Entry<String, String> entry : sm3Map.entrySet()) {
|
||||
macListContent.append(entry.getKey()).append("|").append(entry.getValue()).append("\n");
|
||||
}
|
||||
|
||||
SM3Util.writeFile(CMEP_MAC_LIST_FILE, macListContent.toString());
|
||||
System.out.println("\nCMEP MAC列表文件已保存: " + CMEP_MAC_LIST_FILE);
|
||||
|
||||
byte[] signature = SM2Util.sign(macListContent.toString().getBytes(StandardCharsets.UTF_8), MAC_PRIVATE_KEY_PATH);
|
||||
SM3Util.writeFile(CMEP_MAC_SIGN_FILE, signature);
|
||||
System.out.println("CMEP MAC签名文件已保存: " + CMEP_MAC_SIGN_FILE);
|
||||
|
||||
PublicKey publicKey = SM2Util.loadPublicKey(MAC_PUBLIC_KEY_PATH);
|
||||
boolean verified = SM2Util.verify(macListContent.toString().getBytes(StandardCharsets.UTF_8), signature, publicKey);
|
||||
System.out.println("\n签名验证结果: " + verified);
|
||||
}
|
||||
|
||||
private static void verifyCMEPMAC() throws Exception {
|
||||
System.out.println("=== 验证CMEP文件MAC校验值 ===");
|
||||
|
||||
PublicKey publicKey = SM2Util.loadPublicKey(MAC_PUBLIC_KEY_PATH);
|
||||
byte[] savedSignature = SM3Util.readFileBytes(CMEP_MAC_SIGN_FILE);
|
||||
String savedMacList = SM3Util.readFile(CMEP_MAC_LIST_FILE);
|
||||
|
||||
boolean macListSignatureValid = SM2Util.verify(savedMacList.getBytes(StandardCharsets.UTF_8), savedSignature, publicKey);
|
||||
System.out.println("CMEP MAC列表签名验证: " + (macListSignatureValid ? "通过" : "失败"));
|
||||
|
||||
if (!macListSignatureValid) {
|
||||
System.out.println("错误: cmep.mac.list 签名验证失败,可能被篡改!");
|
||||
System.out.println("\n=== 验证结果 ===");
|
||||
System.out.println("完整性验证: false");
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, String> savedSm3Map = SM3Util.readSM3File(CMEP_MAC_LIST_FILE);
|
||||
|
||||
Map<String, String> currentSm3Map = new java.util.LinkedHashMap<>();
|
||||
for (String target : CMEP_TARGETS) {
|
||||
File file = new File(target);
|
||||
if (!file.exists()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (file.isFile()) {
|
||||
String sm3Value = SM3Util.sm3FileHex(target);
|
||||
currentSm3Map.put(target, sm3Value);
|
||||
}
|
||||
}
|
||||
|
||||
boolean allMatch = true;
|
||||
|
||||
for (Map.Entry<String, String> entry : savedSm3Map.entrySet()) {
|
||||
String filePath = entry.getKey();
|
||||
String savedHash = entry.getValue();
|
||||
String currentHash = currentSm3Map.get(filePath);
|
||||
|
||||
if (currentHash == null) {
|
||||
System.out.println("文件缺失: " + filePath);
|
||||
allMatch = false;
|
||||
} else if (!savedHash.equals(currentHash)) {
|
||||
System.out.println("文件被修改: " + filePath + " (期望: " + savedHash + ", 实际: " + currentHash + ")");
|
||||
allMatch = false;
|
||||
}
|
||||
}
|
||||
|
||||
for (String filePath : currentSm3Map.keySet()) {
|
||||
if (!savedSm3Map.containsKey(filePath)) {
|
||||
System.out.println("新增文件: " + filePath);
|
||||
allMatch = false;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println("=== 验证结果 ===");
|
||||
System.out.println("完整性验证: " + allMatch);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user