mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
cert 初步测试通过
This commit is contained in:
1 parent
262b2c5194
commit
86fcb65c3c
12 files changed
+266
-49
No files matched your search
@@ -1,8 +1,14 @@
|
||||
package minsecurity.certificate.cert;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import minsecurity.Common;
|
||||
import minsecurity.crypto.PublicKeyInterface;
|
||||
import minsecurity.crypto.sm2.Sm2PublicKey;
|
||||
import minsecurity.crypto.PublicKeyUtils;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
@@ -12,7 +18,7 @@ import minsecurity.crypto.sm2.Sm2PublicKey;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class CertUtils {
|
||||
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CertUtils.class);
|
||||
public static final int VERSION1 = 1;
|
||||
|
||||
public static InnerCertificate parseCertToInnerCert(Certificate certificate){
|
||||
@@ -42,7 +48,7 @@ public class CertUtils {
|
||||
|
||||
certificate.setVersion(i.getTbsCertificate().getVersion());
|
||||
certificate.setSerialNumber(i.getTbsCertificate().getSerialNumber());
|
||||
PublicKeyInterface publicKey = unMarshalPublicKey(i.getTbsCertificate().getPublicKey(),i.getSignatureAlgorithm());
|
||||
PublicKeyInterface publicKey = PublicKeyUtils.unMarshalPublicKey(i.getTbsCertificate().getPublicKey(),i.getSignatureAlgorithm());
|
||||
certificate.setPublicKey(publicKey);
|
||||
certificate.setSignature(i.getSignatureValue());
|
||||
certificate.setSignatureAlgorithm(i.getTbsCertificate().getSignatureAlgorithm());
|
||||
@@ -58,36 +64,29 @@ public class CertUtils {
|
||||
return certificate;
|
||||
}
|
||||
|
||||
public static PublicKeyInterface unMarshalPublicKey(byte[] bytesOfPublicKey, int algorithm) {
|
||||
if (bytesOfPublicKey == null || bytesOfPublicKey.length == 0) {
|
||||
return null;
|
||||
}
|
||||
switch (algorithm) {
|
||||
case Common.SM2:
|
||||
Sm2PublicKey sm2PublicKey = new Sm2PublicKey();
|
||||
sm2PublicKey.setBytes(bytesOfPublicKey);
|
||||
return sm2PublicKey;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean checkDuration(InnerCertificate innerCertificate){
|
||||
long curTime = System.currentTimeMillis();
|
||||
if(innerCertificate.getTbsCertificate().getNotAfter() > curTime)
|
||||
// logger.debug("current time: {}", curTime);
|
||||
if(innerCertificate.getTbsCertificate().getNotAfter() < curTime)
|
||||
return false;
|
||||
if(innerCertificate.getTbsCertificate().getNotBefore() < curTime)
|
||||
if(innerCertificate.getTbsCertificate().getNotBefore() > curTime)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// cur = 1615018693993
|
||||
// notbefore = 1615018693858
|
||||
// notafter = 1615018694058
|
||||
|
||||
|
||||
public static boolean checkSign(InnerCertificate cert, PublicKeyInterface publicKey) throws Exception {
|
||||
switch (cert.getTbsCertificate().getSignatureAlgorithm()){
|
||||
case Common.SM3withSM2:
|
||||
// TODO: 证书序列化方法
|
||||
byte[] bytesOfCert = serialization(cert);
|
||||
byte[] bytesOfCert = serialization(cert.getTbsCertificate());
|
||||
logger.debug("验签序列化bytes: {}", ByteUtils.toHexString(bytesOfCert));
|
||||
// byte[] digest = HashAlgo.sm3(bytesOfCert);
|
||||
logger.debug("验签sign: {}", ByteUtils.toHexString(cert.getSignatureValue()));
|
||||
return publicKey.verify(bytesOfCert, cert.getSignatureValue());
|
||||
default:
|
||||
throw new Exception("未定义的签名方法");
|
||||
@@ -95,13 +94,49 @@ public class CertUtils {
|
||||
}
|
||||
|
||||
// TODO: 证书序列化方法
|
||||
public static byte[] serialization(TbsCertificate tbsCertificate){
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
byte[] bytesOfCertJson;
|
||||
try {
|
||||
bytesOfCertJson = mapper.writeValueAsBytes(tbsCertificate);
|
||||
return bytesOfCertJson;
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
public static byte[] serialization(InnerCertificate innerCertificate){
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
byte[] bytesOfCertJson;
|
||||
try {
|
||||
bytesOfCertJson = mapper.writeValueAsBytes(innerCertificate);
|
||||
return bytesOfCertJson;
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
// TODO: 证书反序列化
|
||||
public static InnerCertificate deserialization(byte[] bytesOfCert){
|
||||
return new InnerCertificate();
|
||||
public static Object deserialization(byte[] bytesOfCert, int typeOfCert){
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
switch (typeOfCert) {
|
||||
case Common.TbsCertificate:
|
||||
TbsCertificate tbsCertificate = mapper.readValue(bytesOfCert, TbsCertificate.class);
|
||||
return tbsCertificate;
|
||||
case Common.InnerCertificate:
|
||||
InnerCertificate innerCertificate = mapper.readValue(bytesOfCert, InnerCertificate.class);
|
||||
return innerCertificate;
|
||||
default:
|
||||
throw new CertException("未知证书类型");
|
||||
}
|
||||
} catch (IOException | CertException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user