/**
*
*/
package com.doubleca.sample.pki.pkcs;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Enumeration;
import com.doubleca.pki.crypto.DCToken;
import com.doubleca.pki.crypto.params.KeyPairParams;
import com.doubleca.pki.pkcs.SM2PKCS10;
import com.doubleca.pki.pkcs.SM2PKCS7;
import com.doubleca.pki.util.DnComponents;
import com.doubleca.pki.x509.cert.SM2X509Cert;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import doubleca.security.provider.DoubleCA;
public final class SM2Keystore
{
static
{
Security.addProvider(new DoubleCA());
}
private final static String CERT_DEFAULT_SUBJECTDN = "CN=SelfCert, E=contact@doubleca.com, O=www.DoubleCA.com, ST=BEIJING, C=CN";
private final static String DEFAULT_ALIAS = "c=cn,st=beijing,o=www.doubleca.com,e=contact@doubleca.com,cn=selfcert";
private final static String SIGN_ALG = "SM3WithSM2";
private final static int CERT_DEFAULT_VALIDITY = 365;
private final static String KEYSTORE_TYPE = "DCKS";
private KeyStore keyStore = null;
public SM2Keystore() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException
{
keyStore = KeyStore.getInstance(KEYSTORE_TYPE);
keyStore.load(null, null);
}
public void load(InputStream stream, char[] password) throws NoSuchAlgorithmException, CertificateException, IOException
{
keyStore.load(stream, password);
}
public void store(OutputStream stream, char[] password) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException
{
keyStore.store(stream, password);
}
public String createPKCS10(String subjectDN, char[] priKeyPassword) throws Exception
{
return createPKCS10(subjectDN, priKeyPassword, null);
}
public String createPKCS10(String subjectDN, char[] priKeyPassword, String alias) throws Exception
{
return createPKCS10(subjectDN, priKeyPassword, alias, null);
}
public String createPKCS10(String subjectDN, char[] priKeyPassword, String alias, String csrFilepath) throws Exception
{
DCToken token = new DCToken();
KeyPair keyPair = null;
if (subjectDN == null || subjectDN.length() <= 0)
{
subjectDN = CERT_DEFAULT_SUBJECTDN;
}
keyPair = token.generatorKeyPair(KeyPairParams.getInstance(KeyPairParams.SM2_KEY, 256));
SM2PKCS10 sm2PKCS10 = new SM2PKCS10(token, SIGN_ALG, subjectDN, null, keyPair);
X509Certificate[] chain = new X509Certificate[1];
chain[0] = token.signSelfCertificate(subjectDN, CERT_DEFAULT_VALIDITY, SIGN_ALG, keyPair);
if (alias == null || alias.length() <= 0)
{
alias = DnComponents.stringToBCDNString(chain[0].getSubjectDN().getName());
}
keyStore.setKeyEntry(alias, keyPair.getPrivate(), priKeyPassword, chain);
if (csrFilepath != null && csrFilepath.length() > 0)
{
FileOutputStream fos = new FileOutputStream(csrFilepath);
fos.write(sm2PKCS10.getPKCS10Base64(true).getBytes("UTF-8"));
fos.close();
}
return sm2PKCS10.getPKCS10Base64(true);
}
public String createPKCS10(KeyPair keyPair, String subjectDN, char[] priKeyPassword, String alias, String csrFilepath) throws Exception
{
DCToken token = new DCToken();
if (subjectDN == null || subjectDN.length() <= 0)
{
subjectDN = CERT_DEFAULT_SUBJECTDN;
}
// keyPair = token.generatorKeyPair(KeyPairParams.getInstance(KeyPairParams.SM2_KEY, 256));
SM2PKCS10 sm2PKCS10 = new SM2PKCS10(token, SIGN_ALG, subjectDN, null, keyPair);
X509Certificate[] chain = new X509Certificate[1];
chain[0] = token.signSelfCertificate(subjectDN, CERT_DEFAULT_VALIDITY, SIGN_ALG, keyPair);
if (alias == null || alias.length() <= 0)
{
alias = DnComponents.stringToBCDNString(chain[0].getSubjectDN().getName());
}
keyStore.setKeyEntry(alias, keyPair.getPrivate(), priKeyPassword, chain);
if (csrFilepath != null && csrFilepath.length() > 0)
{
FileOutputStream fos = new FileOutputStream(csrFilepath);
fos.write(sm2PKCS10.getPKCS10Base64(true).getBytes("UTF-8"));
fos.close();
}
return sm2PKCS10.getPKCS10Base64(true);
}
public Certificate getCertificate(String alias) throws KeyStoreException
{
return this.keyStore.getCertificate(alias);
}
public Certificate[] getCertificateChain(String alias) throws KeyStoreException
{
return this.keyStore.getCertificateChain(alias);
}
public void installCertificate(final byte[] pkcs7Data, char[] priKeyPassword) throws KeyStoreException, NoSuchAlgorithmException, Exception
{
SM2PKCS7 p7b = new SM2PKCS7(pkcs7Data);
SM2X509Cert[] chain = p7b.getCerts();
X509Certificate temp[] = new X509Certificate[chain.length];
for (int i = 0; i < chain.length; i++)
{
temp[i] = chain[i].getX509Certificate();
}
this.installCertificate(temp, priKeyPassword);
}
public void installCertificate(final String cerFilepath) throws FileNotFoundException, CertificateException, NoSuchProviderException
{
InputStream bis = null;
Certificate cert = null;
String alias = null;
try
{
bis = new FileInputStream(new File(cerFilepath));
CertificateFactory cf = CertificateFactory.getInstance("X509", DoubleCA.PROVIDER_NAME);
cert = cf.generateCertificate(bis);
alias = DnComponents.stringToBCDNString(((X509Certificate)cert).getSubjectDN().getName());
}
finally
{
try
{
bis.close();
}
catch(Exception ex)
{
}
}
if (alias == null)
{
return;
}
installCertificate(alias, cerFilepath);
}
public void installCertificate(final String alias, final String cerFilepath) throws FileNotFoundException, CertificateException, NoSuchProviderException
{
InputStream bis = null;
Certificate cert = null;
try
{
bis = new FileInputStream(new File(cerFilepath));
CertificateFactory cf = CertificateFactory.getInstance("X509", DoubleCA.PROVIDER_NAME);
cert = cf.generateCertificate(bis);
}
finally
{
try
{
bis.close();
}
catch(Exception ex)
{
}
}
try
{
keyStore.setCertificateEntry(alias, cert);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void installCertificate(final String p7bFilepath, char[] priKeyPassword) throws KeyStoreException, NoSuchAlgorithmException, Exception
{
BufferedInputStream bis = null;
ByteArrayOutputStream bos = null;
byte[] pkcs7Data = null;
try
{
bis = new BufferedInputStream(new FileInputStream(new File(p7bFilepath)));
bos = new ByteArrayOutputStream();
byte[] readDate = new byte[1024];
while (bis.read(readDate) >= 0)
{
bos.write(readDate);
}
pkcs7Data = bos.toByteArray();
}
finally
{
if (bis != null)
{
try
{
bis.close();
}
catch(Exception ex)
{
}
}
if (bos != null)
{
try
{
bos.close();
}
catch(Exception ex)
{
}
}
}
installCertificate(pkcs7Data, priKeyPassword);
}
public void ins

大宝CA国密SSL国密TOMCAT
- 粉丝: 66
- 资源: 16
最新资源
- 欧啦cs插件(web安全渗透)
- HandyControl是一套WPF控件库,它几乎重写了所有原生样式,同时包含80余款自定义控件
- 前端开发_知识点总结_CSS_技巧分享_学习交流_1741873947.zip
- 爬虫_新闻贴吧数据_情感分析_股价预测_交易策略制定Tens_1741870392.zip
- Linux运维_系统学习_知识体系_指南笔记_1741873074.zip
- zbar-0.23.90-5.el8.x64-86.rpm.tar.gz
- GB_T 42455.1-2023 智慧城市 建筑及居住区 第1部分:智慧社区信息系统技术要求.rar
- GBT 40759-2021城市和社区可持续发展 可持续发展管理体系 要求及使用指南(ISO 37101 中文).rar
- GBT 19212.1-2023 (IEC 61558-1_2017 中文) .rar
- HB 6187-1989 航空用印刷版元器件的安装焊接.rar
- 2025年最新版二级Python语言程序设计考试大纲
- ISO 37101-2016 城市和社区可持续发展 可持续发展管理体系 要求及使用指南.rar
- T CIET 009-2021 设施服务体系构建指南.rar
- T_HNCAA 001-2019:6S管理实施指南最新.rar
- zabbix7-7.0.7-1.el8.x64-86.rpm.tar.gz
- 计算机知识_组成原理_操作系统_数据结构_网络_学习笔记_参_1741869807.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


