增加大数据裸签功能,完善RSA签名功能接口(修改counthash使用)
This commit is contained in:
+9
-5
@@ -6,9 +6,6 @@
|
||||
<bin>8003</bin>
|
||||
</command>
|
||||
<data extend="data">
|
||||
<rFlag len="1" required="false" cn="RSA标志">
|
||||
可选,存在,则固定值为‘P’
|
||||
</rFlag>
|
||||
<signatureDataType len="1" cn="获取公私钥标记位">
|
||||
<options>
|
||||
<option cn="索引方式">
|
||||
@@ -24,11 +21,18 @@
|
||||
若上面的标志为1,则此域为证书DN,若为0,则此域为两个字节的证书索引;
|
||||
</DN>
|
||||
|
||||
<dataLength len="2" :value="dataLen()" cn="待签名数据长度">
|
||||
<hashFlag len="1" :if="null != $packet.data.hash" cn="外部 hash 标记">
|
||||
<text>P</text>
|
||||
</hashFlag>
|
||||
|
||||
<hash len="32" :if="null != $packet.data.hash" cn="外部 hash">
|
||||
</hash>
|
||||
|
||||
<dataLength len="2" :value="dataLen()" :if="null == $packet.data.hash" cn="待签名数据长度">
|
||||
待签名数据长度
|
||||
</dataLength>
|
||||
|
||||
<signData cn="待签名数据">
|
||||
<signData cn="待签名数据" :if="null == $packet.data.hash">
|
||||
待签名数据
|
||||
</signData>
|
||||
|
||||
|
||||
+8
-5
@@ -6,9 +6,6 @@
|
||||
<bin>8004</bin>
|
||||
</command>
|
||||
<data extend="data">
|
||||
<rFlag len="1" required="false" cn="RSA标志">
|
||||
可选,存在,则固定值为‘P’
|
||||
</rFlag>
|
||||
<signatureDataType len="1" cn="获取公私钥标记位">
|
||||
<options>
|
||||
<option cn="索引方式">
|
||||
@@ -35,12 +32,18 @@
|
||||
标志为2 时存在
|
||||
</organizationNum>
|
||||
|
||||
<hashFlag len="1" :if="null != $packet.data.hash" cn="外部 hash 标记">
|
||||
<text>P</text>
|
||||
</hashFlag>
|
||||
|
||||
<dataLength len="2" :value="dataLen()" cn="待签名数据长度">
|
||||
<hash len="32" :if="null != $packet.data.hash" cn="外部 hash">
|
||||
</hash>
|
||||
|
||||
<dataLength len="2" :if="null == $packet.data.hash" :value="dataLen()" cn="待签名数据长度">
|
||||
待签名数据长度
|
||||
</dataLength>
|
||||
|
||||
<odata cn="待签名数据">
|
||||
<odata cn="待签名数据" :if="null == $packet.data.hash">
|
||||
待签名数据
|
||||
</odata>
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@
|
||||
<dependency>
|
||||
<groupId>com.sunyard</groupId>
|
||||
<artifactId>proto</artifactId>
|
||||
<version>1.0.20221213</version>
|
||||
<version>1.0.20221215</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
@@ -124,6 +124,17 @@ public class SydApiException extends RuntimeException{
|
||||
}
|
||||
}
|
||||
|
||||
public SydApiException(String msg, int ret, Throwable e) {
|
||||
super("SydApiException(code =" + String.format(" %04x", ret) + " : " + "msg =" + (msg == null ? "未定义的错误" : msg) + ")", e);
|
||||
this.retCode = ret;
|
||||
if (msg != null && !"".equals(msg)) {
|
||||
this.msg = msg;
|
||||
} else {
|
||||
this.transMsg();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static SydApiException fromBin( byte[] b , String msg){
|
||||
int code = null != b && b[0] >= 48 && b[0] <= 57 ? Util.a2i( b , 10) : Util.b2i( b );
|
||||
return new SydApiException(msg, code);
|
||||
|
||||
@@ -72,4 +72,6 @@ public interface SydHashApi {
|
||||
public String calcPBOCMAC(int mode, int keyType, String key, byte[] data, int block, byte[] iv);
|
||||
|
||||
public RetWrap countHash(int hashType, int packSn, byte[] msg, byte[] filterData);
|
||||
|
||||
public byte[] RSAHash(int hashType, byte[] orgData);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,10 @@ import java.io.IOException;
|
||||
|
||||
public class ASN1Util {
|
||||
|
||||
|
||||
public static byte[] alignSignDer(byte[] der) {
|
||||
String base64 = Util.encodeBase64(der);
|
||||
return SYMUtil.base64SignAsDerSign(base64);
|
||||
}
|
||||
public static RetWrap takeOutOriginDataFromPKCS7Sign(String sign) throws IOException {
|
||||
RetWrap ret = new RetWrap();
|
||||
|
||||
|
||||
@@ -398,57 +398,71 @@ public class SydApi4j implements SydApi {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public byte[] SYD_NakedSign(byte[] orgData, String sCertDN) {
|
||||
if (orgData == null) {
|
||||
throw new IllegalArgumentException("输入数据为 null");
|
||||
} else {
|
||||
return orgData.length < 4096 ? this.SYD_NakedSign(sCertDN, orgData, (byte[])null) : this.SYD_NakedSign_OuterHash(orgData, sCertDN);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] SYD_NakedSign(String sCertDN, byte[] orgData, byte[] hash) {
|
||||
Proto8003 proto = new Proto8003();
|
||||
byte[] dn = new byte[256];
|
||||
byte[] s = null;
|
||||
|
||||
try {
|
||||
sCertDN = DnUtil.verifyDn(sCertDN);
|
||||
s = sCertDN.getBytes("GBK");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (UnsupportedEncodingException var16) {
|
||||
var16.printStackTrace();
|
||||
}
|
||||
|
||||
System.arraycopy(s, 0, dn, 0, s.length);
|
||||
|
||||
// 填充数据
|
||||
HashMap<String, Object> packet = new HashMap<String, Object>();
|
||||
HashMap<String, Object> data = new HashMap<String, Object>();
|
||||
HashMap<String, Object> packet = new HashMap();
|
||||
HashMap<String, Object> data = new HashMap();
|
||||
packet.put("data", data);
|
||||
PacketSN sn = PacketSN.gen();
|
||||
packet.put("header", sn.getSn().array());
|
||||
data.put("signatureDataType$option", 1);
|
||||
data.put("DN", dn);
|
||||
data.put("signData", orgData);
|
||||
// 渲染发送
|
||||
data.put("hash", hash);
|
||||
ByteBuffer bb = proto.rend(packet);
|
||||
|
||||
synchronized(this) {
|
||||
// 响应解析
|
||||
bb = syncRead(syncSend(bb));
|
||||
bb = this.syncRead(this.syncSend(bb));
|
||||
}
|
||||
|
||||
|
||||
Packet p = proto.parse(bb);
|
||||
Map<String, Object> ret = p.toMap();
|
||||
|
||||
if (
|
||||
!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())
|
||||
) {
|
||||
if (!Arrays.equals(sn.getSn().array(), ((PacketSection)ret.get("header")).getBytes())) {
|
||||
throw new SydApiException("包序号不匹配", -4);
|
||||
}
|
||||
ret = (Map<String, Object>) ret.get("data");
|
||||
} else {
|
||||
ret = (Map)ret.get("data");
|
||||
PacketSection error = (PacketSection)ret.get("error");
|
||||
byte[] be = error.getBytes();
|
||||
if (
|
||||
(0 != (be[0] ^ be[1]))) {
|
||||
|
||||
if ((be[0] ^ be[1]) != 0) {
|
||||
throw new SydApiException(error.getInfo(), error.autoInt());
|
||||
}
|
||||
} else {
|
||||
return ((PacketSection)ret.get("signature")).getBytes();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] SYD_NakedSign_OuterHash(byte[] orgData, String sCertDN) {
|
||||
sCertDN = DnUtil.verifyDn(sCertDN);
|
||||
String pk = null;
|
||||
|
||||
try {
|
||||
pk = this.SYD_SM2_Get_Dn_PublicKey_HA(sCertDN.getBytes("GBK"), 0);
|
||||
} catch (UnsupportedEncodingException var5) {
|
||||
var5.printStackTrace();
|
||||
throw new SydApiException("DN 编码错误", 52225, var5);
|
||||
}
|
||||
|
||||
byte[] hash = this.SM3Hash(pk, orgData);
|
||||
return this.SYD_NakedSign(sCertDN, (byte[])null, hash);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] SYD_NakedSign(int rFlag, byte[] orgData, String sCertDN) {
|
||||
@@ -509,61 +523,74 @@ public class SydApi4j implements SydApi {
|
||||
return ((PacketSection) ret.get("signature")).getBytes();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean SYD_NakedVerify(byte[] orgData, byte[] sign, String sCertDN) {
|
||||
Proto8004 proto = new Proto8004();
|
||||
return orgData.length < 4096 ? this.SYD_NakedVerify(sCertDN, sign, orgData, (byte[])null) : this.SYD_NakedVerify_OuterHash(orgData, sign, sCertDN);
|
||||
}
|
||||
|
||||
public boolean SYD_NakedVerify(String sCertDN, byte[] sign, byte[] orgData, byte[] hash) {
|
||||
Proto8004 proto = new Proto8004();
|
||||
sign = ASN1Util.alignSignDer(sign);
|
||||
byte[] dn = new byte[256];
|
||||
byte[] s = null;
|
||||
|
||||
try {
|
||||
sCertDN = DnUtil.verifyDn(sCertDN);
|
||||
s = sCertDN.getBytes("GBK");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (UnsupportedEncodingException var18) {
|
||||
var18.printStackTrace();
|
||||
}
|
||||
|
||||
System.arraycopy(s, 0, dn, 0, s.length);
|
||||
|
||||
// 填充数据
|
||||
HashMap<String, Object> packet = new HashMap<String, Object>();
|
||||
HashMap<String, Object> data = new HashMap<String, Object>();
|
||||
HashMap<String, Object> packet = new HashMap();
|
||||
HashMap<String, Object> data = new HashMap();
|
||||
packet.put("data", data);
|
||||
PacketSN sn = PacketSN.gen();
|
||||
packet.put("header", sn.getSn().array());
|
||||
data.put("signatureDataType$option", 1);
|
||||
data.put("DN", dn);
|
||||
data.put("odata", orgData);
|
||||
data.put("hash", hash);
|
||||
data.put("signature", sign);
|
||||
data.put("returnSign$option", 0);
|
||||
// 渲染发送
|
||||
ByteBuffer bb = proto.rend(packet);
|
||||
|
||||
synchronized(this) {
|
||||
byte[] b = syncSend(bb);
|
||||
|
||||
// 响应解析
|
||||
bb = syncRead(b);
|
||||
byte[] b = this.syncSend(bb);
|
||||
bb = this.syncRead(b);
|
||||
}
|
||||
|
||||
Packet p = proto.parse(bb, packet);
|
||||
Map<String, Object> ret = p.toMap();
|
||||
|
||||
if (
|
||||
!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())
|
||||
) {
|
||||
if (!Arrays.equals(sn.getSn().array(), ((PacketSection)ret.get("header")).getBytes())) {
|
||||
throw new SydApiException("包序号不匹配", -4);
|
||||
}
|
||||
ret = (Map<String, Object>) ret.get("data");
|
||||
} else {
|
||||
ret = (Map)ret.get("data");
|
||||
PacketSection error = (PacketSection)ret.get("error");
|
||||
byte[] be = error.getBytes();
|
||||
if ((0 != (be[0] ^ be[1]))) {
|
||||
if ((be[0] ^ be[1]) != 0) {
|
||||
SydApiException e = new SydApiException(error.getInfo(), error.autoInt());
|
||||
println("验签失败=" + e.toString());
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean SYD_NakedVerify_OuterHash(byte[] orgData, byte[] sign, String sCertDN) {
|
||||
sCertDN = DnUtil.verifyDn(sCertDN);
|
||||
String pk = null;
|
||||
|
||||
try {
|
||||
pk = this.SYD_SM2_Get_Dn_PublicKey_HA(sCertDN.getBytes("GBK"), 0);
|
||||
} catch (UnsupportedEncodingException var6) {
|
||||
var6.printStackTrace();
|
||||
throw new SydApiException("DN 编码错误", 52225, var6);
|
||||
}
|
||||
|
||||
byte[] hash = this.SM3Hash(pk, orgData);
|
||||
return this.SYD_NakedVerify(sCertDN, sign, orgData, hash);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean SYD_NakedVerify(int rFlag, byte[] orgData, byte[] sign, String sCertDN) {
|
||||
@@ -884,8 +911,9 @@ public class SydApi4j implements SydApi {
|
||||
if(orgData.length<4096){
|
||||
deVerify= detachedVerify(rFlag,dataType,orgData,sign);
|
||||
}else {
|
||||
RetWrap hashData = countHash(0, 0, orgData, null);
|
||||
deVerify= this.detachedVerify(2, 0, (byte[]) hashData.get("hash"), sign);
|
||||
//RetWrap hashData = countHash(0, 0, orgData, null);
|
||||
byte[] hashData = RSAHash(0, orgData);
|
||||
deVerify= this.detachedVerify(2, 0, hashData, sign);
|
||||
}
|
||||
return deVerify;
|
||||
}
|
||||
@@ -1082,8 +1110,8 @@ public class SydApi4j implements SydApi {
|
||||
if(orgData.length<4096){
|
||||
sign = detachedSign(rFlag,dataType,orgData,sCertDN);
|
||||
}else{
|
||||
RetWrap hashData = countHash(0, 0, orgData, null);
|
||||
byte[] hash = (byte[]) hashData.get("hash");
|
||||
// RetWrap hashData = countHash(0, 0, orgData, null);
|
||||
byte[] hash = RSAHash(0, orgData);
|
||||
sign = detachedSign(2,0, hash, sCertDN);
|
||||
}
|
||||
return sign;
|
||||
@@ -9137,6 +9165,43 @@ public class SydApi4j implements SydApi {
|
||||
return rw;
|
||||
}
|
||||
|
||||
/*
|
||||
根据原始数据计算出RSAhash结果(数据长度不限)
|
||||
@param hashType HASH类型。0: SHA1,1: SHA256,2: MD5
|
||||
@param orgData 原始数据
|
||||
*/
|
||||
public byte[] RSAHash(int hashType, byte[] orgData) {
|
||||
byte[] hashData = new byte[0];
|
||||
final int packLen = 4064;
|
||||
// 如果长度小于等于 4064 ,发送唯一包
|
||||
if (orgData.length <= 4064) {
|
||||
RetWrap retWrap = countHash(hashType, 0, orgData, null);
|
||||
hashData = (byte[]) retWrap.get("hash");
|
||||
}
|
||||
//分包
|
||||
if (orgData.length > 4064) {
|
||||
int loop = orgData.length / packLen;
|
||||
if (orgData.length % packLen > 0) {
|
||||
loop++;
|
||||
}
|
||||
byte[] proData = Arrays.copyOfRange(orgData, 0, packLen);
|
||||
RetWrap retWrap = countHash(hashType, 1, proData, null);
|
||||
//第一块hash数据作为中间块过度消息数据
|
||||
byte[] durHashData = (byte[]) retWrap.get("filterData");
|
||||
byte[] durData;
|
||||
for (int i = 1; i < loop - 1; i++) {
|
||||
durData = Arrays.copyOfRange(orgData, i * packLen, (i + 1) * packLen);
|
||||
RetWrap durretWrap = countHash(hashType, 2, durData, durHashData);
|
||||
durHashData = (byte[]) durretWrap.get("filterData");
|
||||
}
|
||||
byte[] finData = Arrays.copyOfRange(orgData, (loop - 1) * packLen, orgData.length);
|
||||
RetWrap finretWrap = countHash(hashType, 3, finData, durHashData);
|
||||
hashData = (byte[]) finretWrap.get("hash");
|
||||
}
|
||||
return hashData;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 15.7计算HASH散列值(MD5\SHA1\SHA256)(+) -> pass
|
||||
*
|
||||
@@ -11596,8 +11661,9 @@ public class SydApi4j implements SydApi {
|
||||
if (orgData.length < 4096) {
|
||||
verifyRSA= this.detachedVerify(rFlag, 1, orgData, sign2);
|
||||
}else {
|
||||
RetWrap hashData = this.countHash(0, 0, orgData, null);
|
||||
verifyRSA= this.detachedVerify(2, 0, (byte[]) hashData.get("hash"), sign2);
|
||||
// RetWrap hashData = this.countHash(0, 0, orgData, null);
|
||||
byte[] hashData = RSAHash(0, orgData);
|
||||
verifyRSA= this.detachedVerify(2, 0, hashData, sign2);
|
||||
}
|
||||
return verifyRSA;
|
||||
}
|
||||
@@ -11642,8 +11708,9 @@ public class SydApi4j implements SydApi {
|
||||
public String attachedSignRSASoft(int rFlag,byte[] orgData, String sCertDN) throws UnsupportedEncodingException {
|
||||
sCertDN = verifyDn( sCertDN );
|
||||
try {
|
||||
RetWrap hashData = countHash(0, 0, orgData, null);
|
||||
byte[] hash = (byte[]) hashData.get("hash");
|
||||
// RetWrap hashData = countHash(0, 0, orgData, null);
|
||||
// byte[] hash = (byte[]) hashData.get("hash");
|
||||
byte[] hash = RSAHash(0, orgData);
|
||||
String sign = this.detachedSign(2,0, hash, sCertDN);
|
||||
return ASN1Util.addOriginDataToPKCS7Sign( orgData, sign );
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -21,10 +21,12 @@ import java.util.List;
|
||||
import static com.sunyard.sydapi.util.PrintUtil.printRetWrap;
|
||||
|
||||
public class Shanghairenhang {
|
||||
private static final byte[] orgData2k = new byte[ 1*1024 ];
|
||||
private static final byte[] orgData2k = new byte[ 2000*1024 ];
|
||||
private static final String pcDnEN = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||
private static final String RSA = "C=cn,O=CFCA TEST CA,OU=NCS2,OU=Enterprises,CN=041@Z402451000010@ShanDong@00000001";
|
||||
|
||||
private static final String RSA2 = "C=CN,O=sunyard,OU=rsa,CN=javaRsa";
|
||||
|
||||
public static void main(String[] args) throws UnsupportedEncodingException, CertificateException, NoSuchProviderException {
|
||||
// System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||
long startTime = System.currentTimeMillis(); //获取开始时间
|
||||
@@ -81,20 +83,23 @@ public class Shanghairenhang {
|
||||
@Test
|
||||
public void testRenhang() throws UnsupportedEncodingException {
|
||||
//System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||
SydApiBuilder builder = new SydApiBuilder().setIp("192.168.0.200");
|
||||
SydApiBuilder builder = new SydApiBuilder().setIp("172.16.17.161");
|
||||
SydApi4j api = (SydApi4j) builder.build();
|
||||
// String attachedSign = api.attachedSign(2,orgData2k, RSA);
|
||||
// System.out.println(attachedSign);
|
||||
// boolean attachedVerify = api.attachedVerify(2,attachedSign);
|
||||
// System.out.println(attachedVerify);
|
||||
String attachedSign = api.attachedSign(2,orgData2k, RSA2);
|
||||
System.out.println(attachedSign);
|
||||
boolean attachedVerify = api.attachedVerify(2,attachedSign);
|
||||
System.out.println(attachedVerify);
|
||||
// byte[] hash = api.RSAHash(2, orgData2k);
|
||||
// System.out.println(Util.bytes2HexString(hash));
|
||||
|
||||
byte[] bytes = api.SYD_NakedSign(orgData2k, pcDnEN);
|
||||
System.out.println(Util.bytes2HexString(bytes));
|
||||
|
||||
// byte[] bytes = api.SYD_NakedSign(orgData2k, pcDnEN);
|
||||
// System.out.println(Util.bytes2HexString(bytes));
|
||||
}
|
||||
@Test
|
||||
public void aboutCert() throws UnsupportedEncodingException {
|
||||
//System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||
SydApiBuilder builder = new SydApiBuilder().setIp("192.168.1.129");
|
||||
SydApiBuilder builder = new SydApiBuilder().setIp("192.168.1.100");
|
||||
SydApi4j api = (SydApi4j) builder.build();
|
||||
RetWrap allCert = api.getAllCert(0);
|
||||
printRetWrap(allCert);
|
||||
@@ -106,4 +111,19 @@ public class Shanghairenhang {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nakedsign(){
|
||||
// System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||
SydApiBuilder builder = new SydApiBuilder().setIp("172.16.17.161");
|
||||
SydApi4j api = (SydApi4j) builder.build();
|
||||
|
||||
|
||||
byte[] nakedSign2 = api.SYD_NakedSign(orgData2k, pcDnEN);
|
||||
System.out.println("大数据裸签:"+ Util.bytes2HexString(nakedSign2));
|
||||
boolean nakedVerify2 = api.SYD_NakedVerify(orgData2k, nakedSign2, pcDnEN);
|
||||
System.out.println("大数据验签:"+nakedVerify2 );
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user