增加大数据裸签功能,完善RSA签名功能接口(修改counthash使用)
This commit is contained in:
+103
-99
@@ -1,120 +1,124 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<packet extend="base">
|
||||
<req>
|
||||
<req>
|
||||
|
||||
<command>
|
||||
<bin>8003</bin>
|
||||
</command>
|
||||
<data extend="data">
|
||||
<rFlag len="1" required="false" cn="RSA标志">
|
||||
可选,存在,则固定值为‘P’
|
||||
</rFlag>
|
||||
<signatureDataType len="1" cn="获取公私钥标记位">
|
||||
<options>
|
||||
<option cn="索引方式">
|
||||
<bin>00</bin>
|
||||
</option>
|
||||
<option cn="DN 方式">
|
||||
<bin>01</bin>
|
||||
</option>
|
||||
</options>
|
||||
</signatureDataType>
|
||||
<command>
|
||||
<bin>8003</bin>
|
||||
</command>
|
||||
<data extend="data">
|
||||
<signatureDataType len="1" cn="获取公私钥标记位">
|
||||
<options>
|
||||
<option cn="索引方式">
|
||||
<bin>00</bin>
|
||||
</option>
|
||||
<option cn="DN 方式">
|
||||
<bin>01</bin>
|
||||
</option>
|
||||
</options>
|
||||
</signatureDataType>
|
||||
|
||||
<DN cn="DN 或 DN索引">
|
||||
若上面的标志为1,则此域为证书DN,若为0,则此域为两个字节的证书索引;
|
||||
</DN>
|
||||
<DN cn="DN 或 DN索引">
|
||||
若上面的标志为1,则此域为证书DN,若为0,则此域为两个字节的证书索引;
|
||||
</DN>
|
||||
|
||||
<dataLength len="2" :value="dataLen()" cn="待签名数据长度">
|
||||
待签名数据长度
|
||||
</dataLength>
|
||||
<hashFlag len="1" :if="null != $packet.data.hash" cn="外部 hash 标记">
|
||||
<text>P</text>
|
||||
</hashFlag>
|
||||
|
||||
<signData cn="待签名数据">
|
||||
待签名数据
|
||||
</signData>
|
||||
<hash len="32" :if="null != $packet.data.hash" cn="外部 hash">
|
||||
</hash>
|
||||
|
||||
</data>
|
||||
<dataLength len="2" :value="dataLen()" :if="null == $packet.data.hash" cn="待签名数据长度">
|
||||
待签名数据长度
|
||||
</dataLength>
|
||||
|
||||
<signData cn="待签名数据" :if="null == $packet.data.hash">
|
||||
待签名数据
|
||||
</signData>
|
||||
|
||||
</data>
|
||||
|
||||
|
||||
|
||||
</req>
|
||||
<res>
|
||||
</req>
|
||||
<res>
|
||||
|
||||
<command>
|
||||
<bin>8004</bin>
|
||||
</command>
|
||||
<data>
|
||||
<error len="2" cn="错误码" format="X" :catch="catchError('%s')">
|
||||
<options>
|
||||
<option cn="无错误">
|
||||
<hex>00</hex>
|
||||
</option>
|
||||
<option cn="无效的私钥类型">
|
||||
<hex>03</hex>
|
||||
</option>
|
||||
<option cn="无效私钥标记">
|
||||
<hex>04</hex>
|
||||
</option>
|
||||
<option cn="无效的哈希标识">
|
||||
<hex>05</hex>
|
||||
</option>
|
||||
<option cn="无效的签名标识">
|
||||
<hex>06</hex>
|
||||
</option>
|
||||
<option cn="无效的附加标识">
|
||||
<hex>07</hex>
|
||||
</option>
|
||||
<option cn="LMK 错误">
|
||||
LMK 错误;报告给管理员。
|
||||
<hex>13</hex>
|
||||
</option>
|
||||
<option cn="输入数据错">
|
||||
<hex>15</hex>
|
||||
</option>
|
||||
<option cn="DSP 错误">
|
||||
DSP 错误;报告给管理员。
|
||||
<hex>47</hex>
|
||||
</option>
|
||||
<option cn="私钥错误">
|
||||
私钥错误;报告给管理员。
|
||||
<hex>49</hex>
|
||||
</option>
|
||||
<option cn="无效摘要信息语法(仅仅无哈希模式)">
|
||||
<hex>74</hex>
|
||||
</option>
|
||||
<option cn="公钥长度错误">
|
||||
<hex>76</hex>
|
||||
</option>
|
||||
<option cn="私钥长度错误">
|
||||
<hex>78</hex>
|
||||
</option>
|
||||
<option cn="输入数据长度错误">
|
||||
<hex>80</hex>
|
||||
</option>
|
||||
</options>
|
||||
</error>
|
||||
<command>
|
||||
<bin>8004</bin>
|
||||
</command>
|
||||
<data>
|
||||
<error len="2" cn="错误码" format="X" :catch="catchError('%s')">
|
||||
<options>
|
||||
<option cn="无错误">
|
||||
<hex>00</hex>
|
||||
</option>
|
||||
<option cn="无效的私钥类型">
|
||||
<hex>03</hex>
|
||||
</option>
|
||||
<option cn="无效私钥标记">
|
||||
<hex>04</hex>
|
||||
</option>
|
||||
<option cn="无效的哈希标识">
|
||||
<hex>05</hex>
|
||||
</option>
|
||||
<option cn="无效的签名标识">
|
||||
<hex>06</hex>
|
||||
</option>
|
||||
<option cn="无效的附加标识">
|
||||
<hex>07</hex>
|
||||
</option>
|
||||
<option cn="LMK 错误">
|
||||
LMK 错误;报告给管理员。
|
||||
<hex>13</hex>
|
||||
</option>
|
||||
<option cn="输入数据错">
|
||||
<hex>15</hex>
|
||||
</option>
|
||||
<option cn="DSP 错误">
|
||||
DSP 错误;报告给管理员。
|
||||
<hex>47</hex>
|
||||
</option>
|
||||
<option cn="私钥错误">
|
||||
私钥错误;报告给管理员。
|
||||
<hex>49</hex>
|
||||
</option>
|
||||
<option cn="无效摘要信息语法(仅仅无哈希模式)">
|
||||
<hex>74</hex>
|
||||
</option>
|
||||
<option cn="公钥长度错误">
|
||||
<hex>76</hex>
|
||||
</option>
|
||||
<option cn="私钥长度错误">
|
||||
<hex>78</hex>
|
||||
</option>
|
||||
<option cn="输入数据长度错误">
|
||||
<hex>80</hex>
|
||||
</option>
|
||||
</options>
|
||||
</error>
|
||||
|
||||
<signatureLen len="2" cn="签名的字节长度">
|
||||
签名的字节长度
|
||||
</signatureLen>
|
||||
<signatureLen len="2" cn="签名的字节长度">
|
||||
签名的字节长度
|
||||
</signatureLen>
|
||||
|
||||
<signature :len="signatureLen()" cn="签名的 DER 编码">
|
||||
签名的 DER 编码
|
||||
</signature>
|
||||
<signature :len="signatureLen()" cn="签名的 DER 编码">
|
||||
签名的 DER 编码
|
||||
</signature>
|
||||
|
||||
<tailSep len="1" required="false" cn="尾分隔">
|
||||
可选项。如果显示消息尾域,则该域必须显示。值为 “X’19”。
|
||||
<hex>19</hex>
|
||||
</tailSep>
|
||||
<tailSep len="1" required="false" cn="尾分隔">
|
||||
可选项。如果显示消息尾域,则该域必须显示。值为 “X’19”。
|
||||
<hex>19</hex>
|
||||
</tailSep>
|
||||
|
||||
<tail maxLen="32" required="false" cn="尾部">
|
||||
可选项。最大长度为 32个字符。
|
||||
</tail>
|
||||
</data>
|
||||
<tail maxLen="32" required="false" cn="尾部">
|
||||
可选项。最大长度为 32个字符。
|
||||
</tail>
|
||||
</data>
|
||||
|
||||
|
||||
|
||||
</res>
|
||||
<script type="javascript"><![CDATA[
|
||||
</res>
|
||||
<script type="javascript"><![CDATA[
|
||||
function dataLen(){
|
||||
def len = $packet.data.signData.length
|
||||
def ret = new byte[2]
|
||||
|
||||
+112
-109
@@ -1,134 +1,137 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<packet extend="base">
|
||||
<req>
|
||||
<req>
|
||||
|
||||
<command>
|
||||
<bin>8004</bin>
|
||||
</command>
|
||||
<data extend="data">
|
||||
<rFlag len="1" required="false" cn="RSA标志">
|
||||
可选,存在,则固定值为‘P’
|
||||
</rFlag>
|
||||
<signatureDataType len="1" cn="获取公私钥标记位">
|
||||
<options>
|
||||
<option cn="索引方式">
|
||||
<bin>00</bin>
|
||||
</option>
|
||||
<option cn="DN 方式">
|
||||
<bin>01</bin>
|
||||
</option>
|
||||
<option cn="通过机构号获取">
|
||||
<bin>02</bin>
|
||||
</option>
|
||||
</options>
|
||||
</signatureDataType>
|
||||
<command>
|
||||
<bin>8004</bin>
|
||||
</command>
|
||||
<data extend="data">
|
||||
<signatureDataType len="1" cn="获取公私钥标记位">
|
||||
<options>
|
||||
<option cn="索引方式">
|
||||
<bin>00</bin>
|
||||
</option>
|
||||
<option cn="DN 方式">
|
||||
<bin>01</bin>
|
||||
</option>
|
||||
<option cn="通过机构号获取">
|
||||
<bin>02</bin>
|
||||
</option>
|
||||
</options>
|
||||
</signatureDataType>
|
||||
|
||||
<DN :if="useDN()" cn="DN 或 DN索引">
|
||||
若上面的标志为1,则此域为证书DN,若为0,则此域为两个字节的证书索引;若为2,该域不存在
|
||||
</DN>
|
||||
<DN :if="useDN()" cn="DN 或 DN索引">
|
||||
若上面的标志为1,则此域为证书DN,若为0,则此域为两个字节的证书索引;若为2,该域不存在
|
||||
</DN>
|
||||
|
||||
<organizationNumLen len="2" :if="useOrganization()" :value="bNumberLength()" cn="机构号长度">
|
||||
标志为2 时存在
|
||||
</organizationNumLen>
|
||||
<organizationNumLen len="2" :if="useOrganization()" :value="bNumberLength()" cn="机构号长度">
|
||||
标志为2 时存在
|
||||
</organizationNumLen>
|
||||
|
||||
<organizationNum :if="useOrganization()" cn="机构号内容">
|
||||
标志为2 时存在
|
||||
</organizationNum>
|
||||
<organizationNum :if="useOrganization()" cn="机构号内容">
|
||||
标志为2 时存在
|
||||
</organizationNum>
|
||||
|
||||
<hashFlag len="1" :if="null != $packet.data.hash" cn="外部 hash 标记">
|
||||
<text>P</text>
|
||||
</hashFlag>
|
||||
|
||||
<dataLength len="2" :value="dataLen()" cn="待签名数据长度">
|
||||
待签名数据长度
|
||||
</dataLength>
|
||||
<hash len="32" :if="null != $packet.data.hash" cn="外部 hash">
|
||||
</hash>
|
||||
|
||||
<odata cn="待签名数据">
|
||||
待签名数据
|
||||
</odata>
|
||||
<dataLength len="2" :if="null == $packet.data.hash" :value="dataLen()" cn="待签名数据长度">
|
||||
待签名数据长度
|
||||
</dataLength>
|
||||
|
||||
<signatureLen len="2" :value="signLen()" cn="签名的字节长度">
|
||||
签名的字节长度
|
||||
</signatureLen>
|
||||
<odata cn="待签名数据" :if="null == $packet.data.hash">
|
||||
待签名数据
|
||||
</odata>
|
||||
|
||||
<signature :len="signatureLen()" cn="已签名数据">
|
||||
待验签的已签名数据,若为RSA签名则为相应长度的签名数据,若为SM2签名,则前半部分为R.后面半部分为S;
|
||||
</signature>
|
||||
<signatureLen len="2" :value="signLen()" cn="签名的字节长度">
|
||||
签名的字节长度
|
||||
</signatureLen>
|
||||
|
||||
<returnSign len="2" cn="返回证书信息标志">
|
||||
<options>
|
||||
<option cn="不返回">
|
||||
<bin>0000</bin>
|
||||
</option>
|
||||
<option cn="返回">
|
||||
<bin>0001</bin>
|
||||
</option>
|
||||
</options>
|
||||
</returnSign>
|
||||
</data>
|
||||
<signature :len="signatureLen()" cn="已签名数据">
|
||||
待验签的已签名数据,若为RSA签名则为相应长度的签名数据,若为SM2签名,则前半部分为R.后面半部分为S;
|
||||
</signature>
|
||||
|
||||
<returnSign len="2" cn="返回证书信息标志">
|
||||
<options>
|
||||
<option cn="不返回">
|
||||
<bin>0000</bin>
|
||||
</option>
|
||||
<option cn="返回">
|
||||
<bin>0001</bin>
|
||||
</option>
|
||||
</options>
|
||||
</returnSign>
|
||||
</data>
|
||||
|
||||
|
||||
|
||||
</req>
|
||||
<res>
|
||||
</req>
|
||||
<res>
|
||||
|
||||
<command>
|
||||
<bin>8005</bin>
|
||||
</command>
|
||||
<data>
|
||||
<error len="2" cn="错误码" format="X" :catch="catchError('%s')">
|
||||
<options>
|
||||
<option cn="无错误">
|
||||
<hex>00</hex>
|
||||
</option>
|
||||
<option cn="公钥不符合编码规则">
|
||||
<hex>04</hex>
|
||||
</option>
|
||||
<option cn="无效的加密标识">
|
||||
<hex>06</hex>
|
||||
</option>
|
||||
<option cn="无效的附加模式标识">
|
||||
<hex>07</hex>
|
||||
</option>
|
||||
<option cn="密钥奇偶校验错误">
|
||||
<hex>10</hex>
|
||||
</option>
|
||||
<option cn="LMK 错误">
|
||||
LMK 错误;报告给管理员。
|
||||
<hex>13</hex>
|
||||
</option>
|
||||
<option cn="输入数据错">
|
||||
<hex>15</hex>
|
||||
</option>
|
||||
<option cn="DSP 错误">
|
||||
DSP 错误;报告给管理员。
|
||||
<hex>47</hex>
|
||||
</option>
|
||||
<option cn="私钥错误">
|
||||
私钥错误;报告给管理员。
|
||||
<hex>49</hex>
|
||||
</option>
|
||||
<option cn="密钥块长度错误">
|
||||
<hex>76</hex>
|
||||
</option>
|
||||
<option cn="输入数据长度错误">
|
||||
<hex>80</hex>
|
||||
</option>
|
||||
</options>
|
||||
</error>
|
||||
<command>
|
||||
<bin>8005</bin>
|
||||
</command>
|
||||
<data>
|
||||
<error len="2" cn="错误码" format="X" :catch="catchError('%s')">
|
||||
<options>
|
||||
<option cn="无错误">
|
||||
<hex>00</hex>
|
||||
</option>
|
||||
<option cn="公钥不符合编码规则">
|
||||
<hex>04</hex>
|
||||
</option>
|
||||
<option cn="无效的加密标识">
|
||||
<hex>06</hex>
|
||||
</option>
|
||||
<option cn="无效的附加模式标识">
|
||||
<hex>07</hex>
|
||||
</option>
|
||||
<option cn="密钥奇偶校验错误">
|
||||
<hex>10</hex>
|
||||
</option>
|
||||
<option cn="LMK 错误">
|
||||
LMK 错误;报告给管理员。
|
||||
<hex>13</hex>
|
||||
</option>
|
||||
<option cn="输入数据错">
|
||||
<hex>15</hex>
|
||||
</option>
|
||||
<option cn="DSP 错误">
|
||||
DSP 错误;报告给管理员。
|
||||
<hex>47</hex>
|
||||
</option>
|
||||
<option cn="私钥错误">
|
||||
私钥错误;报告给管理员。
|
||||
<hex>49</hex>
|
||||
</option>
|
||||
<option cn="密钥块长度错误">
|
||||
<hex>76</hex>
|
||||
</option>
|
||||
<option cn="输入数据长度错误">
|
||||
<hex>80</hex>
|
||||
</option>
|
||||
</options>
|
||||
</error>
|
||||
|
||||
<infoLen len="2" cn="信息长度" :if="0 != $packet.data.returnSign$option">
|
||||
信息长度
|
||||
</infoLen>
|
||||
<infoLen len="2" cn="信息长度" :if="0 != $packet.data.returnSign$option">
|
||||
信息长度
|
||||
</infoLen>
|
||||
|
||||
<info :len="getInfoLen()" cn="信息" :if="0 != $packet.data.returnSign$option">
|
||||
若发送报文返回证书信息标志位为0,则返回一段垃圾数据,可以不用管
|
||||
若发送报文返回证书信息标志位为1,则返回 CERT_INFO 结构的证书数据
|
||||
</info>
|
||||
<info :len="getInfoLen()" cn="信息" :if="0 != $packet.data.returnSign$option">
|
||||
若发送报文返回证书信息标志位为0,则返回一段垃圾数据,可以不用管
|
||||
若发送报文返回证书信息标志位为1,则返回 CERT_INFO 结构的证书数据
|
||||
</info>
|
||||
|
||||
</data>
|
||||
</data>
|
||||
|
||||
|
||||
|
||||
</res>
|
||||
<script><![CDATA[
|
||||
</res>
|
||||
<script><![CDATA[
|
||||
function dataLen(){
|
||||
def len = $packet.data.odata.length
|
||||
def ret = new byte[2]
|
||||
|
||||
@@ -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));
|
||||
synchronized(this) {
|
||||
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);
|
||||
} else {
|
||||
ret = (Map)ret.get("data");
|
||||
PacketSection error = (PacketSection)ret.get("error");
|
||||
byte[] be = error.getBytes();
|
||||
if ((be[0] ^ be[1]) != 0) {
|
||||
throw new SydApiException(error.getInfo(), error.autoInt());
|
||||
} else {
|
||||
return ((PacketSection)ret.get("signature")).getBytes();
|
||||
}
|
||||
}
|
||||
ret = (Map<String, Object>) ret.get("data");
|
||||
PacketSection error = (PacketSection) ret.get("error");
|
||||
byte[] be = error.getBytes();
|
||||
if (
|
||||
(0 != (be[0] ^ be[1]))) {
|
||||
|
||||
throw new SydApiException(error.getInfo(), error.autoInt());
|
||||
}
|
||||
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,62 +523,75 @@ 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);
|
||||
synchronized(this) {
|
||||
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);
|
||||
} else {
|
||||
ret = (Map)ret.get("data");
|
||||
PacketSection error = (PacketSection)ret.get("error");
|
||||
byte[] be = error.getBytes();
|
||||
if ((be[0] ^ be[1]) != 0) {
|
||||
SydApiException e = new SydApiException(error.getInfo(), error.autoInt());
|
||||
println("验签失败=" + e.toString());
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
ret = (Map<String, Object>) ret.get("data");
|
||||
PacketSection error = (PacketSection) ret.get("error");
|
||||
byte[] be = error.getBytes();
|
||||
if ((0 != (be[0] ^ be[1]))) {
|
||||
SydApiException e = new SydApiException(error.getInfo(), error.autoInt());
|
||||
println("验签失败=" + e.toString());
|
||||
return false;
|
||||
}
|
||||
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) {
|
||||
Proto8004 proto = new Proto8004();
|
||||
@@ -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