证书类接口暂存
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package com.sunyard.chsm.utils;
|
||||
|
||||
import org.bouncycastle.asn1.ASN1Integer;
|
||||
import org.bouncycastle.asn1.ASN1Sequence;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.security.Security;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
import static com.sunyard.chsm.utils.SydCmsUtil.readContentInfo;
|
||||
|
||||
|
||||
/**
|
||||
* @author zyh
|
||||
* @since 2024/12/19
|
||||
*/
|
||||
public class CertUtil {
|
||||
static {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
}
|
||||
|
||||
public static X509Certificate convertToX509Cert(String certificateString) {
|
||||
String pemCert;
|
||||
if(certificateString.contains("-----BEGIN CERTIFICATE-----")){
|
||||
pemCert =certificateString;
|
||||
}else {
|
||||
String cert_begin = "-----BEGIN CERTIFICATE-----\n";
|
||||
String end_cert = "\n-----END CERTIFICATE-----";
|
||||
pemCert = cert_begin + certificateString + end_cert;
|
||||
}
|
||||
try {
|
||||
CertificateFactory CF = CertificateFactory.getInstance("X.509", "BC"); // 从证书工厂中获取X.509的单例类
|
||||
return (X509Certificate) CF.generateCertificate(new ByteArrayInputStream(pemCert.getBytes())); // 将文件流的证书转化为证书类
|
||||
} catch (CertificateException | NoSuchProviderException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String getSn(String cert) {
|
||||
try {
|
||||
|
||||
if(cert.contains("-----BEGIN CERTIFICATE-----")){
|
||||
String[] lines = cert.split("\n");
|
||||
// 构建新的证书字符串,去掉第一行和最后一行
|
||||
StringBuilder cleanedCertificate = new StringBuilder();
|
||||
for (int i = 1; i < lines.length - 1; i++) {
|
||||
cleanedCertificate.append(lines[i]);
|
||||
}
|
||||
cert = cleanedCertificate.toString();
|
||||
}
|
||||
//从证书中获取sn
|
||||
Object pkContent = readContentInfo(CodecUtils.decodeBase64(cert));
|
||||
|
||||
//逐级从结构体中获取需要的sn
|
||||
ASN1Sequence pkSeq = ASN1Sequence.getInstance(pkContent);
|
||||
ASN1Sequence pkSeq0 = (ASN1Sequence) pkSeq.getObjectAt(0).toASN1Primitive();
|
||||
ASN1Integer sn = (ASN1Integer) pkSeq0.getObjectAt(1).toASN1Primitive();
|
||||
String serNum = CodecUtils.encodeHex(sn.getEncoded());
|
||||
|
||||
//去掉长度
|
||||
return serNum.substring(4);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.sunyard.chsm.utils;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
@@ -18,4 +20,10 @@ public abstract class DateFormat {
|
||||
public static final DateTimeFormatter DATE = DateTimeFormatter.ofPattern(YYYY_MM_DD);
|
||||
public static final DateTimeFormatter DATE_TIME = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS);
|
||||
|
||||
|
||||
public static String formDate(Date date) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
||||
return sdf.format(date);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
package com.sunyard.chsm.utils;
|
||||
|
||||
import org.bouncycastle.asn1.*;
|
||||
import org.bouncycastle.asn1.cms.ContentInfo;
|
||||
import org.bouncycastle.asn1.cms.EncryptedContentInfo;
|
||||
import org.bouncycastle.asn1.cms.OriginatorInfo;
|
||||
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
|
||||
import org.bouncycastle.cms.CMSException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class SydCmsUtil {
|
||||
|
||||
public static Object readContentInfo(
|
||||
byte[] input) {
|
||||
ASN1InputStream is = new ASN1InputStream(input);
|
||||
Object v = null;
|
||||
try {
|
||||
v = is.readObject();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
public static Map<String, Object> getEncryptedContentInfo(ASN1Encodable obj){
|
||||
Map<String, Object>map = new HashMap<String, Object>();
|
||||
ASN1Sequence seq = ASN1Sequence.getInstance(obj );
|
||||
if (seq.size() < 2)
|
||||
{
|
||||
throw new IllegalArgumentException("Truncated Sequence Found");
|
||||
}
|
||||
|
||||
ASN1ObjectIdentifier contentType = (ASN1ObjectIdentifier)seq.getObjectAt(0);
|
||||
map.put( "contentType", contentType );
|
||||
|
||||
AlgorithmIdentifier contentEncryptionAlgorithm = AlgorithmIdentifier.getInstance(
|
||||
seq.getObjectAt(1));
|
||||
|
||||
map.put( "contentEncryptionAlgorithm", contentEncryptionAlgorithm );
|
||||
|
||||
if (seq.size() > 2)
|
||||
{
|
||||
ASN1OctetString encryptedContent = ASN1OctetString.getInstance(
|
||||
(ASN1TaggedObject)seq.getObjectAt(2), false);
|
||||
map.put( "encryptedContent", encryptedContent );
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static Map<String, Object> getOriginatorInfo(ASN1TaggedObject obj,
|
||||
boolean explicit){
|
||||
Map<String, Object>map = new HashMap<String, Object>();
|
||||
ASN1Sequence seq = ASN1Sequence.getInstance(obj, explicit);
|
||||
switch (seq.size())
|
||||
{
|
||||
case 0: // empty
|
||||
break;
|
||||
case 1:
|
||||
ASN1TaggedObject o = (ASN1TaggedObject)seq.getObjectAt(0);
|
||||
switch (o.getTagNo())
|
||||
{
|
||||
case 0 :
|
||||
ASN1Set certs = ASN1Set.getInstance(o, false);
|
||||
map.put("certs", certs);
|
||||
break;
|
||||
case 1 :
|
||||
ASN1Set crls = ASN1Set.getInstance(o, false);
|
||||
map.put("crls", crls);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Bad tag in OriginatorInfo: " + o.getTagNo());
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
ASN1Set certs = ASN1Set.getInstance((ASN1TaggedObject)seq.getObjectAt(0), false);
|
||||
map.put("certs", certs);
|
||||
ASN1Set crls = ASN1Set.getInstance((ASN1TaggedObject)seq.getObjectAt(1), false);
|
||||
map.put("crls", crls);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("OriginatorInfo too big");
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
// public static ContentInfo getContentInfo( byte[] input ) {
|
||||
// Object content = readContentInfo( input );
|
||||
// ASN1Sequence seq = ASN1Sequence.getInstance( content );
|
||||
// return new ContentInfo(seq);
|
||||
//
|
||||
// }
|
||||
|
||||
public static Map<String, Object> readCmsInfo( byte[] input ) throws CMSException {
|
||||
Map<String, Object>map = new HashMap<String, Object>();
|
||||
|
||||
Object content = readContentInfo( input );
|
||||
ASN1Sequence seq = ASN1Sequence.getInstance( content );
|
||||
ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) seq.getObjectAt(0);
|
||||
map.put("oid", oid);
|
||||
|
||||
seq = ASN1Sequence.getInstance((ASN1TaggedObject) seq.getObjectAt(1), true);
|
||||
|
||||
int index = 0;
|
||||
|
||||
ASN1Integer version = (ASN1Integer)seq.getObjectAt(index++);
|
||||
map.put( "version", version );
|
||||
|
||||
Object tmp = seq.getObjectAt(index++);
|
||||
|
||||
if (tmp instanceof ASN1TaggedObject)
|
||||
{
|
||||
OriginatorInfo originatorInfo = OriginatorInfo.getInstance((ASN1TaggedObject)tmp, false);
|
||||
// Map<String, Object>originatorInfo = getOriginatorInfo((ASN1TaggedObject)tmp, false);
|
||||
map.put( "originatorInfo", originatorInfo );
|
||||
tmp = seq.getObjectAt(index++);
|
||||
}
|
||||
|
||||
ASN1Set recipientInfos = ASN1Set.getInstance(tmp);
|
||||
map.put( "recipientInfos", recipientInfos );
|
||||
|
||||
EncryptedContentInfo encryptedContentInfo = EncryptedContentInfo.getInstance( seq.getObjectAt(index++) );
|
||||
// Map<String, Object> encryptedContentInfo = getEncryptedContentInfo(seq.getObjectAt(index++));
|
||||
map.put( "encryptedContentInfo", encryptedContentInfo );
|
||||
|
||||
if(seq.size() > index)
|
||||
{
|
||||
ASN1Set unprotectedAttrs = ASN1Set.getInstance((ASN1TaggedObject)seq.getObjectAt(index), false);
|
||||
map.put( "unprotectedAttrs", unprotectedAttrs );
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user