导出公钥和证书

This commit is contained in:
liulu
2024-12-30 09:06:11 +08:00
parent 265d81a7f0
commit c05423c0a3
20 changed files with 597 additions and 70 deletions
@@ -3,9 +3,13 @@ package api;
import com.sunyard.chsm.param.*;
import com.sunyard.chsm.utils.CodecUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.Collections;
/**
* @author liulu
* @since 2024/12/24
@@ -23,18 +27,83 @@ public class AsymKeyTest extends BaseTest {
private static final String signDetachHsm = "MIIDMQYKKoEcz1UGAQQCAqCCAyEwggMdAgEBMQ4wDAYIKoEcz1UBgxEFADAMBgoqgRzPVQYBBAIBoIICMjCCAi4wggHSoAMCAQICBgGUAQHeQjAMBggqgRzPVQGDdQUAMEsxCzAJBgNVBAYTAkNOMQ4wDAYDVQQKEwVHTVNTTDEQMA4GA1UECxMHUEtJL1NNMjEaMBgGA1UEAxMRTWlkZGxlQ0EgZm9yIFRlc3QwIhgPMjAyNDEyMjUxNjAwMDBaGA8yMDI1MTIyNTE2MDAwMFowXTELMAkGA1UEBgwCQ04xCzAJBgNVBAgMAnpqMQswCQYDVQQHDAJoejEQMA4GA1UECgwHc3VueWFyZDEQMA4GA1UECwwHc3VueWFyZDEQMA4GA1UEAwwHdGVzdDEyMzBZMBMGByqGSM49AgEGCCqBHM9VAYItA0IABBNw7w5PxepQWwg5tfYoRIwilnqp01YvdY3ZnZD1ShHw6DX4f8cLblHagNazkOU9C5zeXvUWvtrcvZz5s92TLTGjgYkwgYYwGwYDVR0jBBQwEoAQ+X9VtCeUM2KmVspvzF0a/zAZBgNVHQ4EEgQQCR6s1jJA8ENWsIEo61LkMjAxBggrBgEFBQcBAQQlMCMwIQYIKwYBBQUHMAGGFWh0dHBzOi8vb2NzcC5nbXNzbC5jbjAJBgNVHRMEAjAAMA4GA1UdDwEB/wQEAwIAwDAMBggqgRzPVQGDdQUAA0gAMEUCIEaCtRZ8G4OgwhqI6NqxVwfUELvHS7ojkTGbImLX1ZkiAiEApFb7utbtgvL5hkV0Gj/k/CLY0vl+RbYSqAVoPtNEr1oxgcMwgcACAQEwVTBLMQswCQYDVQQGEwJDTjEOMAwGA1UEChMFR01TU0wxEDAOBgNVBAsTB1BLSS9TTTIxGjAYBgNVBAMTEU1pZGRsZUNBIGZvciBUZXN0AgYBlAEB3kIwDAYIKoEcz1UBgxEFADANBgkqgRzPVQGCLQEFAARHMEUCIEzIJwjBsG6/2VemCJuQ0/eJhNS+Nwcz+7+WqJwlsgV5AiEA4ILu0NFEaM3IC4d9gAFswOZnACwYnKR2/gm9eZB4GsY=";
private static final byte[] plainHsm = "12345678".getBytes();
@BeforeAll
public static void beforeAll() {
keyId = execute("/key/gen", KeyCreateReq.builder().keyTemplateCode(asymKeyTemplate).genNumber(1).build(), Long.class);
Assertions.assertTrue(keyId > 0);
}
@AfterAll
public static void afterAll() {
KeyManageReq keyManageReq = new KeyManageReq();
keyManageReq.setIds(Collections.singletonList(keyId));
execute("/key/disable", keyManageReq, Void.class);
execute("/key/destroy", keyManageReq, Void.class);
}
@Test
public void testExportKey() {
ExportPubKeyReq req = new ExportPubKeyReq();
req.setKeyId(keyId);
ExportPubKeyResp exportPubKeyResp = execute("/asym/exportPubKey", req, ExportPubKeyResp.class);
Assertions.assertNotNull(exportPubKeyResp);
log.info("ExportPubKeyResp: {}", exportPubKeyResp.getPubKey());
}
@Test
public void testRaw() {
AsymSignRawReq signRawReq = new AsymSignRawReq();
signRawReq.setKeyId(keyId);
signRawReq.setPlainData(CodecUtils.encodeBase64(plain));
AsymSignRawResp signRawResp = execute("/asym/sign/raw", signRawReq, AsymSignRawResp.class);
Assertions.assertNotNull(signRawResp);
log.info("AsymSignRawResp: {}", signRawResp);
AsymVerifyRawReq verifyRawReq = new AsymVerifyRawReq();
verifyRawReq.setKeyId(keyId);
verifyRawReq.setKeyIndex(signRawResp.getKeyIndex());
verifyRawReq.setPlainData(CodecUtils.encodeBase64(plain));
verifyRawReq.setSignData(signRawResp.getSignData());
VerifyResp verifyResp = execute("/asym/verify/raw", verifyRawReq, VerifyResp.class);
Assertions.assertNotNull(verifyResp);
log.info("verifyResp: {}", verifyResp);
Assertions.assertTrue(verifyResp.getVerified());
}
@Test
public void testAttach() {
AsymSignP7Req signP7Req = new AsymSignP7Req();
signP7Req.setSubject(dn);
signP7Req.setPlainData(CodecUtils.encodeBase64(plain));
AsymSignP7Resp signP7Resp = execute("/asym/sign/P7Attach", signP7Req, AsymSignP7Resp.class);
log.info("signP7: {}", signP7Resp.getSignData());
Assertions.assertNotNull(signP7Resp);
log.info("AsymSignP7Resp: {}", signP7Resp.getSignData());
AsymVerifyP7Req verifyP7Req = new AsymVerifyP7Req();
verifyP7Req.setSignData(signP7Resp.getSignData());
VerifyResp verifyResp = execute("/asym/verify/P7Attach", verifyP7Req, VerifyResp.class);
log.info("verifyResp: {}", verifyResp.getVerified());
Assertions.assertNotNull(verifyResp);
log.info("verifyResp: {}", verifyResp);
Assertions.assertTrue(verifyResp.getVerified());
}
@Test
public void testDetach() {
AsymSignP7Req signP7Req = new AsymSignP7Req();
signP7Req.setSubject(dn);
signP7Req.setPlainData(CodecUtils.encodeBase64(plain));
AsymSignP7Resp signP7Resp = execute("/asym/sign/P7Detach", signP7Req, AsymSignP7Resp.class);
Assertions.assertNotNull(signP7Resp);
log.info("AsymSignP7Resp: {}", signP7Resp.getSignData());
AsymVerifyP7Req verifyP7Req = new AsymVerifyP7Req();
verifyP7Req.setSignData(signP7Resp.getSignData());
verifyP7Req.setPlainData(CodecUtils.encodeBase64(plain));
VerifyResp verifyResp = execute("/asym/verify/P7Detach", verifyP7Req, VerifyResp.class);
Assertions.assertNotNull(verifyResp);
log.info("verifyResp: {}", verifyResp);
Assertions.assertTrue(verifyResp.getVerified());
}
@@ -44,14 +113,16 @@ public class AsymKeyTest extends BaseTest {
AsymVerifyP7Req verifyP7Req = new AsymVerifyP7Req();
verifyP7Req.setSignData(signAttachHsm);
VerifyResp verifyResp = execute("/asym/verify/P7Attach", verifyP7Req, VerifyResp.class);
log.info("verifyResp: {}", verifyResp.getVerified());
Assertions.assertNotNull(verifyResp);
log.info("verifyResp: {}", verifyResp);
Assertions.assertTrue(verifyResp.getVerified());
verifyP7Req.setSignData(signDetachHsm);
verifyP7Req.setPlainData(CodecUtils.encodeBase64(plainHsm));
verifyResp = execute("/asym/verify/P7Detach", verifyP7Req, VerifyResp.class);
log.info("verifyResp: {}", verifyResp.getVerified());
Assertions.assertTrue(verifyResp.getVerified());
VerifyResp verifyResp2 = execute("/asym/verify/P7Detach", verifyP7Req, VerifyResp.class);
Assertions.assertNotNull(verifyResp2);
log.info("verifyResp: {}", verifyResp2);
Assertions.assertTrue(verifyResp2.getVerified());
}
@Test
@@ -60,13 +131,15 @@ public class AsymKeyTest extends BaseTest {
sealReq.setEncCert(enc_cert);
sealReq.setPlainData(CodecUtils.encodeBase64(plain));
AsymEnvelopeSealResp sealResp = execute("/asym/envelope/seal", sealReq, AsymEnvelopeSealResp.class);
log.info("EnvelopeData: {}", sealResp.getEnvelopeData());
Assertions.assertNotNull(sealResp);
log.info("AsymEnvelopeSealResp: {}", sealResp);
AsymEnvelopeUnsealReq unsealReq = new AsymEnvelopeUnsealReq();
unsealReq.setEnvelopeData(sealResp.getEnvelopeData());
AsymEnvelopeUnsealResp unsealResp = execute("/asym/envelope/unseal", unsealReq, AsymEnvelopeUnsealResp.class);
log.info("verifyResp: {}", unsealResp.getPlainData());
log.info("verifyResp: {}", new String(CodecUtils.decodeBase64(unsealResp.getPlainData())));
Assertions.assertNotNull(unsealResp);
log.info("AsymEnvelopeUnsealResp: {}", unsealResp);
log.info("AsymEnvelopeUnsealResp: {}", new String(CodecUtils.decodeBase64(unsealResp.getPlainData())));
Assertions.assertArrayEquals(plain, CodecUtils.decodeBase64(unsealResp.getPlainData()));
}
@@ -78,13 +151,15 @@ public class AsymKeyTest extends BaseTest {
sealReq.setEncCert(enc_cert);
sealReq.setPlainData(CodecUtils.encodeBase64(plain));
AsymEnvelopeSealResp sealResp = execute("/asym/signedEnvelope/seal", sealReq, AsymEnvelopeSealResp.class);
log.info("EnvelopeData: {}", sealResp.getEnvelopeData());
Assertions.assertNotNull(sealResp);
log.info("SignedEnvelopeEnvelopeSealResp: {}", sealResp);
AsymEnvelopeUnsealReq unsealReq = new AsymEnvelopeUnsealReq();
unsealReq.setEnvelopeData(sealResp.getEnvelopeData());
AsymEnvelopeUnsealResp unsealResp = execute("/asym/signedEnvelope/unseal", unsealReq, AsymEnvelopeUnsealResp.class);
log.info("verifyResp: {}", unsealResp.getPlainData());
log.info("verifyResp: {}", new String(CodecUtils.decodeBase64(unsealResp.getPlainData())));
Assertions.assertNotNull(unsealResp);
log.info("AsymEnvelopeUnsealResp: {}", unsealResp);
log.info("AsymEnvelopeUnsealResp: {}", new String(CodecUtils.decodeBase64(unsealResp.getPlainData())));
Assertions.assertArrayEquals(plain, CodecUtils.decodeBase64(unsealResp.getPlainData()));
}