增加大数据裸签功能,完善RSA签名功能接口(修改counthash使用)
This commit is contained in:
+103
-99
@@ -1,120 +1,124 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" ?>
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
<packet extend="base">
|
<packet extend="base">
|
||||||
<req>
|
<req>
|
||||||
|
|
||||||
<command>
|
<command>
|
||||||
<bin>8003</bin>
|
<bin>8003</bin>
|
||||||
</command>
|
</command>
|
||||||
<data extend="data">
|
<data extend="data">
|
||||||
<rFlag len="1" required="false" cn="RSA标志">
|
<signatureDataType len="1" cn="获取公私钥标记位">
|
||||||
可选,存在,则固定值为‘P’
|
<options>
|
||||||
</rFlag>
|
<option cn="索引方式">
|
||||||
<signatureDataType len="1" cn="获取公私钥标记位">
|
<bin>00</bin>
|
||||||
<options>
|
</option>
|
||||||
<option cn="索引方式">
|
<option cn="DN 方式">
|
||||||
<bin>00</bin>
|
<bin>01</bin>
|
||||||
</option>
|
</option>
|
||||||
<option cn="DN 方式">
|
</options>
|
||||||
<bin>01</bin>
|
</signatureDataType>
|
||||||
</option>
|
|
||||||
</options>
|
|
||||||
</signatureDataType>
|
|
||||||
|
|
||||||
<DN cn="DN 或 DN索引">
|
<DN cn="DN 或 DN索引">
|
||||||
若上面的标志为1,则此域为证书DN,若为0,则此域为两个字节的证书索引;
|
若上面的标志为1,则此域为证书DN,若为0,则此域为两个字节的证书索引;
|
||||||
</DN>
|
</DN>
|
||||||
|
|
||||||
<dataLength len="2" :value="dataLen()" cn="待签名数据长度">
|
<hashFlag len="1" :if="null != $packet.data.hash" cn="外部 hash 标记">
|
||||||
待签名数据长度
|
<text>P</text>
|
||||||
</dataLength>
|
</hashFlag>
|
||||||
|
|
||||||
<signData cn="待签名数据">
|
<hash len="32" :if="null != $packet.data.hash" cn="外部 hash">
|
||||||
待签名数据
|
</hash>
|
||||||
</signData>
|
|
||||||
|
|
||||||
</data>
|
<dataLength len="2" :value="dataLen()" :if="null == $packet.data.hash" cn="待签名数据长度">
|
||||||
|
待签名数据长度
|
||||||
|
</dataLength>
|
||||||
|
|
||||||
|
<signData cn="待签名数据" :if="null == $packet.data.hash">
|
||||||
|
待签名数据
|
||||||
|
</signData>
|
||||||
|
|
||||||
|
</data>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</req>
|
</req>
|
||||||
<res>
|
<res>
|
||||||
|
|
||||||
<command>
|
<command>
|
||||||
<bin>8004</bin>
|
<bin>8004</bin>
|
||||||
</command>
|
</command>
|
||||||
<data>
|
<data>
|
||||||
<error len="2" cn="错误码" format="X" :catch="catchError('%s')">
|
<error len="2" cn="错误码" format="X" :catch="catchError('%s')">
|
||||||
<options>
|
<options>
|
||||||
<option cn="无错误">
|
<option cn="无错误">
|
||||||
<hex>00</hex>
|
<hex>00</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="无效的私钥类型">
|
<option cn="无效的私钥类型">
|
||||||
<hex>03</hex>
|
<hex>03</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="无效私钥标记">
|
<option cn="无效私钥标记">
|
||||||
<hex>04</hex>
|
<hex>04</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="无效的哈希标识">
|
<option cn="无效的哈希标识">
|
||||||
<hex>05</hex>
|
<hex>05</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="无效的签名标识">
|
<option cn="无效的签名标识">
|
||||||
<hex>06</hex>
|
<hex>06</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="无效的附加标识">
|
<option cn="无效的附加标识">
|
||||||
<hex>07</hex>
|
<hex>07</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="LMK 错误">
|
<option cn="LMK 错误">
|
||||||
LMK 错误;报告给管理员。
|
LMK 错误;报告给管理员。
|
||||||
<hex>13</hex>
|
<hex>13</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="输入数据错">
|
<option cn="输入数据错">
|
||||||
<hex>15</hex>
|
<hex>15</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="DSP 错误">
|
<option cn="DSP 错误">
|
||||||
DSP 错误;报告给管理员。
|
DSP 错误;报告给管理员。
|
||||||
<hex>47</hex>
|
<hex>47</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="私钥错误">
|
<option cn="私钥错误">
|
||||||
私钥错误;报告给管理员。
|
私钥错误;报告给管理员。
|
||||||
<hex>49</hex>
|
<hex>49</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="无效摘要信息语法(仅仅无哈希模式)">
|
<option cn="无效摘要信息语法(仅仅无哈希模式)">
|
||||||
<hex>74</hex>
|
<hex>74</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="公钥长度错误">
|
<option cn="公钥长度错误">
|
||||||
<hex>76</hex>
|
<hex>76</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="私钥长度错误">
|
<option cn="私钥长度错误">
|
||||||
<hex>78</hex>
|
<hex>78</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="输入数据长度错误">
|
<option cn="输入数据长度错误">
|
||||||
<hex>80</hex>
|
<hex>80</hex>
|
||||||
</option>
|
</option>
|
||||||
</options>
|
</options>
|
||||||
</error>
|
</error>
|
||||||
|
|
||||||
<signatureLen len="2" cn="签名的字节长度">
|
<signatureLen len="2" cn="签名的字节长度">
|
||||||
签名的字节长度
|
签名的字节长度
|
||||||
</signatureLen>
|
</signatureLen>
|
||||||
|
|
||||||
<signature :len="signatureLen()" cn="签名的 DER 编码">
|
<signature :len="signatureLen()" cn="签名的 DER 编码">
|
||||||
签名的 DER 编码
|
签名的 DER 编码
|
||||||
</signature>
|
</signature>
|
||||||
|
|
||||||
<tailSep len="1" required="false" cn="尾分隔">
|
<tailSep len="1" required="false" cn="尾分隔">
|
||||||
可选项。如果显示消息尾域,则该域必须显示。值为 “X’19”。
|
可选项。如果显示消息尾域,则该域必须显示。值为 “X’19”。
|
||||||
<hex>19</hex>
|
<hex>19</hex>
|
||||||
</tailSep>
|
</tailSep>
|
||||||
|
|
||||||
<tail maxLen="32" required="false" cn="尾部">
|
<tail maxLen="32" required="false" cn="尾部">
|
||||||
可选项。最大长度为 32个字符。
|
可选项。最大长度为 32个字符。
|
||||||
</tail>
|
</tail>
|
||||||
</data>
|
</data>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</res>
|
</res>
|
||||||
<script type="javascript"><![CDATA[
|
<script type="javascript"><![CDATA[
|
||||||
function dataLen(){
|
function dataLen(){
|
||||||
def len = $packet.data.signData.length
|
def len = $packet.data.signData.length
|
||||||
def ret = new byte[2]
|
def ret = new byte[2]
|
||||||
|
|||||||
+112
-109
@@ -1,134 +1,137 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" ?>
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
<packet extend="base">
|
<packet extend="base">
|
||||||
<req>
|
<req>
|
||||||
|
|
||||||
<command>
|
<command>
|
||||||
<bin>8004</bin>
|
<bin>8004</bin>
|
||||||
</command>
|
</command>
|
||||||
<data extend="data">
|
<data extend="data">
|
||||||
<rFlag len="1" required="false" cn="RSA标志">
|
<signatureDataType len="1" cn="获取公私钥标记位">
|
||||||
可选,存在,则固定值为‘P’
|
<options>
|
||||||
</rFlag>
|
<option cn="索引方式">
|
||||||
<signatureDataType len="1" cn="获取公私钥标记位">
|
<bin>00</bin>
|
||||||
<options>
|
</option>
|
||||||
<option cn="索引方式">
|
<option cn="DN 方式">
|
||||||
<bin>00</bin>
|
<bin>01</bin>
|
||||||
</option>
|
</option>
|
||||||
<option cn="DN 方式">
|
<option cn="通过机构号获取">
|
||||||
<bin>01</bin>
|
<bin>02</bin>
|
||||||
</option>
|
</option>
|
||||||
<option cn="通过机构号获取">
|
</options>
|
||||||
<bin>02</bin>
|
</signatureDataType>
|
||||||
</option>
|
|
||||||
</options>
|
|
||||||
</signatureDataType>
|
|
||||||
|
|
||||||
<DN :if="useDN()" cn="DN 或 DN索引">
|
<DN :if="useDN()" cn="DN 或 DN索引">
|
||||||
若上面的标志为1,则此域为证书DN,若为0,则此域为两个字节的证书索引;若为2,该域不存在
|
若上面的标志为1,则此域为证书DN,若为0,则此域为两个字节的证书索引;若为2,该域不存在
|
||||||
</DN>
|
</DN>
|
||||||
|
|
||||||
<organizationNumLen len="2" :if="useOrganization()" :value="bNumberLength()" cn="机构号长度">
|
<organizationNumLen len="2" :if="useOrganization()" :value="bNumberLength()" cn="机构号长度">
|
||||||
标志为2 时存在
|
标志为2 时存在
|
||||||
</organizationNumLen>
|
</organizationNumLen>
|
||||||
|
|
||||||
<organizationNum :if="useOrganization()" cn="机构号内容">
|
<organizationNum :if="useOrganization()" cn="机构号内容">
|
||||||
标志为2 时存在
|
标志为2 时存在
|
||||||
</organizationNum>
|
</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>
|
|
||||||
|
|
||||||
<odata cn="待签名数据">
|
<dataLength len="2" :if="null == $packet.data.hash" :value="dataLen()" cn="待签名数据长度">
|
||||||
待签名数据
|
待签名数据长度
|
||||||
</odata>
|
</dataLength>
|
||||||
|
|
||||||
<signatureLen len="2" :value="signLen()" cn="签名的字节长度">
|
<odata cn="待签名数据" :if="null == $packet.data.hash">
|
||||||
签名的字节长度
|
待签名数据
|
||||||
</signatureLen>
|
</odata>
|
||||||
|
|
||||||
<signature :len="signatureLen()" cn="已签名数据">
|
<signatureLen len="2" :value="signLen()" cn="签名的字节长度">
|
||||||
待验签的已签名数据,若为RSA签名则为相应长度的签名数据,若为SM2签名,则前半部分为R.后面半部分为S;
|
签名的字节长度
|
||||||
</signature>
|
</signatureLen>
|
||||||
|
|
||||||
<returnSign len="2" cn="返回证书信息标志">
|
<signature :len="signatureLen()" cn="已签名数据">
|
||||||
<options>
|
待验签的已签名数据,若为RSA签名则为相应长度的签名数据,若为SM2签名,则前半部分为R.后面半部分为S;
|
||||||
<option cn="不返回">
|
</signature>
|
||||||
<bin>0000</bin>
|
|
||||||
</option>
|
<returnSign len="2" cn="返回证书信息标志">
|
||||||
<option cn="返回">
|
<options>
|
||||||
<bin>0001</bin>
|
<option cn="不返回">
|
||||||
</option>
|
<bin>0000</bin>
|
||||||
</options>
|
</option>
|
||||||
</returnSign>
|
<option cn="返回">
|
||||||
</data>
|
<bin>0001</bin>
|
||||||
|
</option>
|
||||||
|
</options>
|
||||||
|
</returnSign>
|
||||||
|
</data>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</req>
|
</req>
|
||||||
<res>
|
<res>
|
||||||
|
|
||||||
<command>
|
<command>
|
||||||
<bin>8005</bin>
|
<bin>8005</bin>
|
||||||
</command>
|
</command>
|
||||||
<data>
|
<data>
|
||||||
<error len="2" cn="错误码" format="X" :catch="catchError('%s')">
|
<error len="2" cn="错误码" format="X" :catch="catchError('%s')">
|
||||||
<options>
|
<options>
|
||||||
<option cn="无错误">
|
<option cn="无错误">
|
||||||
<hex>00</hex>
|
<hex>00</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="公钥不符合编码规则">
|
<option cn="公钥不符合编码规则">
|
||||||
<hex>04</hex>
|
<hex>04</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="无效的加密标识">
|
<option cn="无效的加密标识">
|
||||||
<hex>06</hex>
|
<hex>06</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="无效的附加模式标识">
|
<option cn="无效的附加模式标识">
|
||||||
<hex>07</hex>
|
<hex>07</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="密钥奇偶校验错误">
|
<option cn="密钥奇偶校验错误">
|
||||||
<hex>10</hex>
|
<hex>10</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="LMK 错误">
|
<option cn="LMK 错误">
|
||||||
LMK 错误;报告给管理员。
|
LMK 错误;报告给管理员。
|
||||||
<hex>13</hex>
|
<hex>13</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="输入数据错">
|
<option cn="输入数据错">
|
||||||
<hex>15</hex>
|
<hex>15</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="DSP 错误">
|
<option cn="DSP 错误">
|
||||||
DSP 错误;报告给管理员。
|
DSP 错误;报告给管理员。
|
||||||
<hex>47</hex>
|
<hex>47</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="私钥错误">
|
<option cn="私钥错误">
|
||||||
私钥错误;报告给管理员。
|
私钥错误;报告给管理员。
|
||||||
<hex>49</hex>
|
<hex>49</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="密钥块长度错误">
|
<option cn="密钥块长度错误">
|
||||||
<hex>76</hex>
|
<hex>76</hex>
|
||||||
</option>
|
</option>
|
||||||
<option cn="输入数据长度错误">
|
<option cn="输入数据长度错误">
|
||||||
<hex>80</hex>
|
<hex>80</hex>
|
||||||
</option>
|
</option>
|
||||||
</options>
|
</options>
|
||||||
</error>
|
</error>
|
||||||
|
|
||||||
<infoLen len="2" cn="信息长度" :if="0 != $packet.data.returnSign$option">
|
<infoLen len="2" cn="信息长度" :if="0 != $packet.data.returnSign$option">
|
||||||
信息长度
|
信息长度
|
||||||
</infoLen>
|
</infoLen>
|
||||||
|
|
||||||
<info :len="getInfoLen()" cn="信息" :if="0 != $packet.data.returnSign$option">
|
<info :len="getInfoLen()" cn="信息" :if="0 != $packet.data.returnSign$option">
|
||||||
若发送报文返回证书信息标志位为0,则返回一段垃圾数据,可以不用管
|
若发送报文返回证书信息标志位为0,则返回一段垃圾数据,可以不用管
|
||||||
若发送报文返回证书信息标志位为1,则返回 CERT_INFO 结构的证书数据
|
若发送报文返回证书信息标志位为1,则返回 CERT_INFO 结构的证书数据
|
||||||
</info>
|
</info>
|
||||||
|
|
||||||
</data>
|
</data>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</res>
|
</res>
|
||||||
<script><![CDATA[
|
<script><![CDATA[
|
||||||
function dataLen(){
|
function dataLen(){
|
||||||
def len = $packet.data.odata.length
|
def len = $packet.data.odata.length
|
||||||
def ret = new byte[2]
|
def ret = new byte[2]
|
||||||
|
|||||||
@@ -78,7 +78,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.sunyard</groupId>
|
<groupId>com.sunyard</groupId>
|
||||||
<artifactId>proto</artifactId>
|
<artifactId>proto</artifactId>
|
||||||
<version>1.0.20221213</version>
|
<version>1.0.20221215</version>
|
||||||
</dependency>
|
</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){
|
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 );
|
int code = null != b && b[0] >= 48 && b[0] <= 57 ? Util.a2i( b , 10) : Util.b2i( b );
|
||||||
return new SydApiException(msg, code);
|
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 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 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 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 {
|
public static RetWrap takeOutOriginDataFromPKCS7Sign(String sign) throws IOException {
|
||||||
RetWrap ret = new RetWrap();
|
RetWrap ret = new RetWrap();
|
||||||
|
|
||||||
|
|||||||
@@ -398,57 +398,71 @@ public class SydApi4j implements SydApi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte[] SYD_NakedSign(byte[] orgData, String sCertDN) {
|
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();
|
Proto8003 proto = new Proto8003();
|
||||||
byte[] dn = new byte[256];
|
byte[] dn = new byte[256];
|
||||||
byte[] s = null;
|
byte[] s = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
sCertDN = DnUtil.verifyDn(sCertDN);
|
||||||
s = sCertDN.getBytes("GBK");
|
s = sCertDN.getBytes("GBK");
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException var16) {
|
||||||
e.printStackTrace();
|
var16.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
System.arraycopy(s, 0, dn, 0, s.length);
|
System.arraycopy(s, 0, dn, 0, s.length);
|
||||||
|
HashMap<String, Object> packet = new HashMap();
|
||||||
// 填充数据
|
HashMap<String, Object> data = new HashMap();
|
||||||
HashMap<String, Object> packet = new HashMap<String, Object>();
|
|
||||||
HashMap<String, Object> data = new HashMap<String, Object>();
|
|
||||||
packet.put("data", data);
|
packet.put("data", data);
|
||||||
PacketSN sn = PacketSN.gen();
|
PacketSN sn = PacketSN.gen();
|
||||||
packet.put("header", sn.getSn().array());
|
packet.put("header", sn.getSn().array());
|
||||||
data.put("signatureDataType$option", 1);
|
data.put("signatureDataType$option", 1);
|
||||||
data.put("DN", dn);
|
data.put("DN", dn);
|
||||||
data.put("signData", orgData);
|
data.put("signData", orgData);
|
||||||
// 渲染发送
|
data.put("hash", hash);
|
||||||
ByteBuffer bb = proto.rend(packet);
|
ByteBuffer bb = proto.rend(packet);
|
||||||
|
synchronized(this) {
|
||||||
synchronized (this) {
|
bb = this.syncRead(this.syncSend(bb));
|
||||||
// 响应解析
|
|
||||||
bb = syncRead(syncSend(bb));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Packet p = proto.parse(bb);
|
Packet p = proto.parse(bb);
|
||||||
Map<String, Object> ret = p.toMap();
|
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);
|
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
|
@Override
|
||||||
public byte[] SYD_NakedSign(int rFlag, byte[] orgData, String sCertDN) {
|
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();
|
return ((PacketSection) ret.get("signature")).getBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean SYD_NakedVerify(byte[] orgData, byte[] sign, String sCertDN) {
|
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[] dn = new byte[256];
|
||||||
byte[] s = null;
|
byte[] s = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
sCertDN = DnUtil.verifyDn(sCertDN);
|
||||||
s = sCertDN.getBytes("GBK");
|
s = sCertDN.getBytes("GBK");
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException var18) {
|
||||||
e.printStackTrace();
|
var18.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
System.arraycopy(s, 0, dn, 0, s.length);
|
System.arraycopy(s, 0, dn, 0, s.length);
|
||||||
|
HashMap<String, Object> packet = new HashMap();
|
||||||
// 填充数据
|
HashMap<String, Object> data = new HashMap();
|
||||||
HashMap<String, Object> packet = new HashMap<String, Object>();
|
|
||||||
HashMap<String, Object> data = new HashMap<String, Object>();
|
|
||||||
packet.put("data", data);
|
packet.put("data", data);
|
||||||
PacketSN sn = PacketSN.gen();
|
PacketSN sn = PacketSN.gen();
|
||||||
packet.put("header", sn.getSn().array());
|
packet.put("header", sn.getSn().array());
|
||||||
data.put("signatureDataType$option", 1);
|
data.put("signatureDataType$option", 1);
|
||||||
data.put("DN", dn);
|
data.put("DN", dn);
|
||||||
data.put("odata", orgData);
|
data.put("odata", orgData);
|
||||||
|
data.put("hash", hash);
|
||||||
data.put("signature", sign);
|
data.put("signature", sign);
|
||||||
data.put("returnSign$option", 0);
|
data.put("returnSign$option", 0);
|
||||||
// 渲染发送
|
|
||||||
ByteBuffer bb = proto.rend(packet);
|
ByteBuffer bb = proto.rend(packet);
|
||||||
|
synchronized(this) {
|
||||||
synchronized (this) {
|
byte[] b = this.syncSend(bb);
|
||||||
byte[] b = syncSend(bb);
|
bb = this.syncRead(b);
|
||||||
|
|
||||||
// 响应解析
|
|
||||||
bb = syncRead(b);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Packet p = proto.parse(bb, packet);
|
Packet p = proto.parse(bb, packet);
|
||||||
Map<String, Object> ret = p.toMap();
|
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);
|
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
|
@Override
|
||||||
public boolean SYD_NakedVerify(int rFlag, byte[] orgData, byte[] sign, String sCertDN) {
|
public boolean SYD_NakedVerify(int rFlag, byte[] orgData, byte[] sign, String sCertDN) {
|
||||||
Proto8004 proto = new Proto8004();
|
Proto8004 proto = new Proto8004();
|
||||||
@@ -884,8 +911,9 @@ public class SydApi4j implements SydApi {
|
|||||||
if(orgData.length<4096){
|
if(orgData.length<4096){
|
||||||
deVerify= detachedVerify(rFlag,dataType,orgData,sign);
|
deVerify= detachedVerify(rFlag,dataType,orgData,sign);
|
||||||
}else {
|
}else {
|
||||||
RetWrap hashData = countHash(0, 0, orgData, null);
|
//RetWrap hashData = countHash(0, 0, orgData, null);
|
||||||
deVerify= this.detachedVerify(2, 0, (byte[]) hashData.get("hash"), sign);
|
byte[] hashData = RSAHash(0, orgData);
|
||||||
|
deVerify= this.detachedVerify(2, 0, hashData, sign);
|
||||||
}
|
}
|
||||||
return deVerify;
|
return deVerify;
|
||||||
}
|
}
|
||||||
@@ -1082,8 +1110,8 @@ public class SydApi4j implements SydApi {
|
|||||||
if(orgData.length<4096){
|
if(orgData.length<4096){
|
||||||
sign = detachedSign(rFlag,dataType,orgData,sCertDN);
|
sign = detachedSign(rFlag,dataType,orgData,sCertDN);
|
||||||
}else{
|
}else{
|
||||||
RetWrap hashData = countHash(0, 0, orgData, null);
|
// RetWrap hashData = countHash(0, 0, orgData, null);
|
||||||
byte[] hash = (byte[]) hashData.get("hash");
|
byte[] hash = RSAHash(0, orgData);
|
||||||
sign = detachedSign(2,0, hash, sCertDN);
|
sign = detachedSign(2,0, hash, sCertDN);
|
||||||
}
|
}
|
||||||
return sign;
|
return sign;
|
||||||
@@ -9137,6 +9165,43 @@ public class SydApi4j implements SydApi {
|
|||||||
return rw;
|
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
|
* 15.7计算HASH散列值(MD5\SHA1\SHA256)(+) -> pass
|
||||||
*
|
*
|
||||||
@@ -11596,8 +11661,9 @@ public class SydApi4j implements SydApi {
|
|||||||
if (orgData.length < 4096) {
|
if (orgData.length < 4096) {
|
||||||
verifyRSA= this.detachedVerify(rFlag, 1, orgData, sign2);
|
verifyRSA= this.detachedVerify(rFlag, 1, orgData, sign2);
|
||||||
}else {
|
}else {
|
||||||
RetWrap hashData = this.countHash(0, 0, orgData, null);
|
// RetWrap hashData = this.countHash(0, 0, orgData, null);
|
||||||
verifyRSA= this.detachedVerify(2, 0, (byte[]) hashData.get("hash"), sign2);
|
byte[] hashData = RSAHash(0, orgData);
|
||||||
|
verifyRSA= this.detachedVerify(2, 0, hashData, sign2);
|
||||||
}
|
}
|
||||||
return verifyRSA;
|
return verifyRSA;
|
||||||
}
|
}
|
||||||
@@ -11642,8 +11708,9 @@ public class SydApi4j implements SydApi {
|
|||||||
public String attachedSignRSASoft(int rFlag,byte[] orgData, String sCertDN) throws UnsupportedEncodingException {
|
public String attachedSignRSASoft(int rFlag,byte[] orgData, String sCertDN) throws UnsupportedEncodingException {
|
||||||
sCertDN = verifyDn( sCertDN );
|
sCertDN = verifyDn( sCertDN );
|
||||||
try {
|
try {
|
||||||
RetWrap hashData = countHash(0, 0, orgData, null);
|
// RetWrap hashData = countHash(0, 0, orgData, null);
|
||||||
byte[] hash = (byte[]) hashData.get("hash");
|
// byte[] hash = (byte[]) hashData.get("hash");
|
||||||
|
byte[] hash = RSAHash(0, orgData);
|
||||||
String sign = this.detachedSign(2,0, hash, sCertDN);
|
String sign = this.detachedSign(2,0, hash, sCertDN);
|
||||||
return ASN1Util.addOriginDataToPKCS7Sign( orgData, sign );
|
return ASN1Util.addOriginDataToPKCS7Sign( orgData, sign );
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
@@ -21,10 +21,12 @@ import java.util.List;
|
|||||||
import static com.sunyard.sydapi.util.PrintUtil.printRetWrap;
|
import static com.sunyard.sydapi.util.PrintUtil.printRetWrap;
|
||||||
|
|
||||||
public class Shanghairenhang {
|
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 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 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 {
|
public static void main(String[] args) throws UnsupportedEncodingException, CertificateException, NoSuchProviderException {
|
||||||
// System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
// System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||||
long startTime = System.currentTimeMillis(); //获取开始时间
|
long startTime = System.currentTimeMillis(); //获取开始时间
|
||||||
@@ -81,20 +83,23 @@ public class Shanghairenhang {
|
|||||||
@Test
|
@Test
|
||||||
public void testRenhang() throws UnsupportedEncodingException {
|
public void testRenhang() throws UnsupportedEncodingException {
|
||||||
//System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
//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();
|
SydApi4j api = (SydApi4j) builder.build();
|
||||||
// String attachedSign = api.attachedSign(2,orgData2k, RSA);
|
String attachedSign = api.attachedSign(2,orgData2k, RSA2);
|
||||||
// System.out.println(attachedSign);
|
System.out.println(attachedSign);
|
||||||
// boolean attachedVerify = api.attachedVerify(2,attachedSign);
|
boolean attachedVerify = api.attachedVerify(2,attachedSign);
|
||||||
// System.out.println(attachedVerify);
|
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
|
@Test
|
||||||
public void aboutCert() throws UnsupportedEncodingException {
|
public void aboutCert() throws UnsupportedEncodingException {
|
||||||
//System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
//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();
|
SydApi4j api = (SydApi4j) builder.build();
|
||||||
RetWrap allCert = api.getAllCert(0);
|
RetWrap allCert = api.getAllCert(0);
|
||||||
printRetWrap(allCert);
|
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