Merge remote-tracking branch 'origin/dev-shangqing' into dev-shangqing
This commit is contained in:
@@ -132,6 +132,18 @@ public interface SydGenApi {
|
||||
*/
|
||||
RetWrap SM2GenSessionKey(String publicKey);
|
||||
|
||||
|
||||
/**
|
||||
* 此函数用于会话密钥的产生,用于发起端。
|
||||
* 加密机产生随机会话密钥,输出两类密文:
|
||||
* 1、 RSA公钥加密的密文,用于密钥的传递。
|
||||
* 2、 本地 LMK 加密,用于本地应用数据的加解密。
|
||||
*
|
||||
* @param publicKey 公钥串,DER 编码,HEX 格式。
|
||||
* @return CipherKey 公钥加密的会话密钥,DER 编码,HEX 格式, SessionKey 本地LMK 加密的会话密钥,HEX 格式 , KCV 会话密钥的检验值,HEX 格式,长度为 16H
|
||||
*/
|
||||
RetWrap RSAGenSessionKey(String publicKey);
|
||||
|
||||
/**
|
||||
* 生成数字信封
|
||||
* @param publicKey 公钥
|
||||
@@ -228,6 +240,8 @@ public interface SydGenApi {
|
||||
*/
|
||||
RetWrap generateDEByDn(String certSerial, byte[] oData);
|
||||
|
||||
RetWrap generateDEByDn(int alg,String certSerial, byte[] oData);
|
||||
|
||||
/**
|
||||
* 产生对称密钥,通过公钥加密生成网联格式的数字信封。
|
||||
|
||||
@@ -325,6 +339,21 @@ public interface SydGenApi {
|
||||
*/
|
||||
RetWrap decryptDE(boolean pFlag, int model, byte[] certSerial, String pCipherKey);
|
||||
|
||||
/**
|
||||
* 接收用RSA私钥解密数字信封,还原对称密钥。
|
||||
*
|
||||
* @param pFlag 输入。 p标识,如果为true,则传入R
|
||||
* @param model 输入。 证书模式,0:证书序号,1:证书 DN
|
||||
* @param certSerial 输入。 对方 X509 证书的序列号或DN。
|
||||
* @param pCipherKey 输入。 公钥加密的会话密钥,BASE64 编码
|
||||
* @return RetWrap 包含 SessionKey KCV
|
||||
* oData 如果有机构码的话会返回原数据
|
||||
* SessionKey 输出。 本地LMK 加密的会话密钥,格式为:密钥类型|密钥密文,比如:02|密钥密文
|
||||
* KCV 会话密钥的检验值,HEX 格式,长度为 36H。
|
||||
* orgCode 如果有机构码的话会返回机构码
|
||||
*/
|
||||
RetWrap decryptDERSA(boolean pFlag, int model, byte[] certSerial, String pCipherKey);
|
||||
|
||||
/**
|
||||
* 产生要求长度的随即数据串,可用于身份认证
|
||||
*
|
||||
|
||||
@@ -28,6 +28,18 @@ interface SydNakedSVApi {
|
||||
byte[] orgData,
|
||||
String sCertDN
|
||||
);
|
||||
/**
|
||||
* 使用指定的证书 DN,从加密设备获取对应的私钥证书,对指定的原始数据编制裸签名(遵循 PKCS#1)
|
||||
* 人行项目增加RSA
|
||||
* @param orgData 待签名的原始数据
|
||||
* @param sCertDN 签名者证书 DN
|
||||
* @return 签名数据
|
||||
*/
|
||||
public byte[] SYD_NakedSign(
|
||||
int rFlag,
|
||||
byte[] orgData,
|
||||
String sCertDN
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
@@ -42,8 +54,22 @@ interface SydNakedSVApi {
|
||||
byte[] sign,
|
||||
String sCertDN
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 使用指定的证书 DN,对指定的原始数据编制裸验签(遵循PKCS#1)
|
||||
* @param orgData 待签名的原始数据
|
||||
* @param sign 签名数据
|
||||
* @param sCertDN 签名者证书 DN
|
||||
* @return 验签是否通过
|
||||
*/
|
||||
public boolean SYD_NakedVerify(
|
||||
int rFlag,
|
||||
byte[] orgData,
|
||||
byte[] sign,
|
||||
String sCertDN
|
||||
);
|
||||
}
|
||||
/**
|
||||
* 带原文的签名验签
|
||||
*/
|
||||
@@ -182,6 +208,19 @@ interface SydAttachedSVApi {
|
||||
String attachedSign(byte[] orgData, String sCertDN) throws UnsupportedEncodingException;
|
||||
byte[] attachedSignC(byte[] orgData, String sCertDN);
|
||||
|
||||
|
||||
/**
|
||||
* 人行项目需要需要支持RSA
|
||||
* 带公钥证书与原始数据的数字签名(遵循 PKCS#7)
|
||||
* 使用指定的证书 DN,从加密设备获取对应的私钥证书,对指定的原始数据编制带公钥证书的数字签名(遵循 PKCS#7)。
|
||||
*
|
||||
* @param orgData 待签名数据
|
||||
* @param sCertDN 证书DN
|
||||
* @return
|
||||
* 符合PKCS7标准的签名结果
|
||||
*/
|
||||
String attachedSign(int rFlag,byte[] orgData, String sCertDN) throws UnsupportedEncodingException;
|
||||
|
||||
/**
|
||||
* 带公钥证书与原始数据的数字签名验签(遵循 PKCS#7)
|
||||
* 对指定的原始数据核验其带公钥证书签名。如签名有效(含证书无效)且需返回签名者公钥证书信息时,同时返回签名者公钥证书信息。
|
||||
@@ -190,6 +229,17 @@ interface SydAttachedSVApi {
|
||||
* @return true 验签通过 false 验签失败
|
||||
*/
|
||||
boolean attachedVerify(String sign);
|
||||
|
||||
/**
|
||||
* 带公钥证书与原始数据的数字签名验签(遵循 PKCS#7)
|
||||
* 对指定的原始数据核验其带公钥证书签名。如签名有效(含证书无效)且需返回签名者公钥证书信息时,同时返回签名者公钥证书信息。
|
||||
*
|
||||
* @param sign 已签名数据
|
||||
* @return true 验签通过 false 验签失败
|
||||
*/
|
||||
boolean attachedVerify(int rFlag,String sign);
|
||||
|
||||
|
||||
boolean attachedVerify(byte[] sign);
|
||||
|
||||
/**
|
||||
@@ -254,6 +304,19 @@ interface SydDetachedSVApi {
|
||||
String sCertDN
|
||||
);
|
||||
|
||||
/**
|
||||
* 人行项目需要RSA支持
|
||||
* 通过指定的私钥对指定的原始数据编制带公钥证书的数字签名(遵循PKCS#7)
|
||||
* @param orgData 待签名的原始数据
|
||||
* @param sCertDN 签名者证书 DN
|
||||
* @return 签名数据
|
||||
*/
|
||||
public String SYD_DetachedSign(
|
||||
int rFlag,
|
||||
byte[] orgData,
|
||||
String sCertDN
|
||||
);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@@ -361,6 +424,23 @@ interface SydDetachedSVApi {
|
||||
byte[] orgData,
|
||||
String sign);
|
||||
|
||||
/**
|
||||
* 对指定的原始数据核验其带公钥证书签名(PKCS#7)
|
||||
* 数据模式可选 DATA_HASH 或 DATA_ORIGIN
|
||||
*
|
||||
*
|
||||
* @param rFlag RSA标识
|
||||
* @param dataType 输入的数据为 HASH 后的数据 DATA_HASH = 0 ,输入的数据为原始数据库 DATA_ORIGIN = 1
|
||||
* @param orgData 待签名的原始数据
|
||||
* @param sign 签名数据
|
||||
* @return 验签是否通过
|
||||
*/
|
||||
boolean detachedVerify(
|
||||
int rFlag,
|
||||
int dataType,
|
||||
byte[] orgData,
|
||||
String sign);
|
||||
|
||||
/**
|
||||
* 带公钥证书与签名属性的数字签名验签(遵循 PKCS#7)
|
||||
* @param dataType 数据类型:0,hash 模式,1为非hash模式
|
||||
|
||||
@@ -18,12 +18,14 @@ public class ASN1Util {
|
||||
ASN1Sequence seq = ASN1Sequence.getInstance( Util.decodeBase64( sign ) );
|
||||
ASN1TaggedObject tag = (ASN1TaggedObject) seq.getObjectAt(1).toASN1Primitive();
|
||||
ASN1Sequence seq2 = (ASN1Sequence) tag.getObject();
|
||||
ASN1Sequence seq3 = (ASN1Sequence) seq2.getObjectAt( 2 ).toASN1Primitive();
|
||||
ASN1TaggedObject tag2 = (ASN1TaggedObject) seq3.getObjectAt( 1 );
|
||||
DEROctetString o = (DEROctetString) tag2.getObject();
|
||||
ret.put( "orgData", o.getOctets() );
|
||||
|
||||
|
||||
ASN1Sequence seq3 = null;
|
||||
try {
|
||||
seq3 = (ASN1Sequence) seq2.getObjectAt( 2 ).toASN1Primitive();
|
||||
ASN1TaggedObject tag2 = (ASN1TaggedObject) seq3.getObjectAt( 1 );
|
||||
DEROctetString o = (DEROctetString) tag2.getObject();
|
||||
ret.put( "orgData", o.getOctets() );
|
||||
} catch (Exception e) {
|
||||
}
|
||||
ASN1TaggedObject tag3 = (ASN1TaggedObject) seq2.getObjectAt(3).toASN1Primitive();
|
||||
ASN1Sequence seq4 = (ASN1Sequence) tag3.getObject();
|
||||
ret.put("cert", CertUtil.encodeBase64(seq4.getEncoded()));
|
||||
|
||||
@@ -79,7 +79,15 @@ public class CertUtil {
|
||||
* @return true 国密, false 非国密
|
||||
*/
|
||||
public static boolean isSM2Cert(String cert) throws CertificateException, NoSuchProviderException {
|
||||
X509Certificate certificate = convertToX509Cert(cert);
|
||||
String pemCert;
|
||||
if(cert.contains("-----BEGIN CERTIFICATE-----")){
|
||||
pemCert =cert;
|
||||
}else {
|
||||
String cert_begin = "-----BEGIN CERTIFICATE-----\n";
|
||||
String end_cert = "\n-----END CERTIFICATE-----";
|
||||
pemCert = cert_begin + cert + end_cert;
|
||||
}
|
||||
X509Certificate certificate = convertToX509Cert(pemCert);
|
||||
String signAlg = certificate.getSigAlgName();
|
||||
return signAlg.toUpperCase().indexOf("SM2") >= 0;
|
||||
}
|
||||
@@ -92,7 +100,16 @@ public class CertUtil {
|
||||
*/
|
||||
public static boolean isSignedBySM2Cert(String pkcs7) throws IOException, CertificateException, NoSuchProviderException {
|
||||
RetWrap ret = ASN1Util.takeOutOriginDataFromPKCS7Sign(pkcs7);
|
||||
return isSM2Cert((String) ret.get("cert"));
|
||||
//return isSM2Cert((String) ret.get("cert"));
|
||||
String cert =(String) ret.get("cert");
|
||||
if(cert.contains("-----BEGIN CERTIFICATE-----")){
|
||||
return isSM2Cert(cert);
|
||||
}else{
|
||||
String cert_begin = "-----BEGIN CERTIFICATE-----\n";
|
||||
String end_cert = "\n-----END CERTIFICATE-----";
|
||||
String pemCert = cert_begin + cert + end_cert;
|
||||
return isSM2Cert(pemCert);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user