增加配置项开关控制dn反序

This commit is contained in:
yihzhou 2022-12-09 10:57:32 +08:00
parent 0f28395ce9
commit 3f6d316d8b
3 changed files with 46 additions and 33 deletions

View File

@ -17,6 +17,7 @@ public class DnUtil {
} }
public static String verifyDn(String dn) { public static String verifyDn(String dn) {
if ( "true".equals(System.getProperty("com.sunyard.dnsort", "true"))) {
if (dn.startsWith("C=") || dn.startsWith("c=")) { if (dn.startsWith("C=") || dn.startsWith("c=")) {
return dn; return dn;
} }
@ -39,5 +40,7 @@ public class DnUtil {
return sb.toString(); return sb.toString();
} }
else return dn;
}
} }

View File

@ -46,6 +46,8 @@ import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import static com.sunyard.util.DnUtil.verifyDn;
public class SydApi4j implements SydApi { public class SydApi4j implements SydApi {
static { static {
@ -775,7 +777,7 @@ public class SydApi4j implements SydApi {
byte[] dn = new byte[256]; byte[] dn = new byte[256];
byte[] s = null; byte[] s = null;
try { try {
sCertDN = DnUtil.verifyDn(sCertDN); sCertDN = verifyDn(sCertDN);
s = sCertDN.getBytes("GBK"); s = sCertDN.getBytes("GBK");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); e.printStackTrace();
@ -1094,7 +1096,7 @@ public class SydApi4j implements SydApi {
byte[] dn = new byte[256]; byte[] dn = new byte[256];
byte[] s = null; byte[] s = null;
try { try {
sCertDN = DnUtil.verifyDn(sCertDN); sCertDN = verifyDn(sCertDN);
s = sCertDN.getBytes("GBK"); s = sCertDN.getBytes("GBK");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); e.printStackTrace();
@ -2848,7 +2850,7 @@ public class SydApi4j implements SydApi {
public RetWrap generateDEByDnHsm(String certSerial, byte[] oData) { public RetWrap generateDEByDnHsm(String certSerial, byte[] oData) {
certSerial = DnUtil.verifyDn(certSerial); certSerial = verifyDn(certSerial);
byte[] dnBytes = null; byte[] dnBytes = null;
try { try {
@ -2861,7 +2863,7 @@ public class SydApi4j implements SydApi {
public RetWrap generateDEByDnHsmRSA(int alg, String certSerial, byte[] oData) { public RetWrap generateDEByDnHsmRSA(int alg, String certSerial, byte[] oData) {
certSerial = DnUtil.verifyDn(certSerial); certSerial = verifyDn(certSerial);
byte[] dnBytes = null; byte[] dnBytes = null;
try { try {
@ -2988,7 +2990,7 @@ public class SydApi4j implements SydApi {
public RetWrap decryptDEByDNHsm(String certSerial, String pCipherKey) { public RetWrap decryptDEByDNHsm(String certSerial, String pCipherKey) {
certSerial = DnUtil.verifyDn(certSerial); certSerial = verifyDn(certSerial);
byte[] dnBytes = null; byte[] dnBytes = null;
try { try {
@ -3001,7 +3003,7 @@ public class SydApi4j implements SydApi {
public RetWrap decryptDEByDNHsmRSA(String certSerial, String pCipherKey) { public RetWrap decryptDEByDNHsmRSA(String certSerial, String pCipherKey) {
certSerial = DnUtil.verifyDn(certSerial); certSerial = verifyDn(certSerial);
byte[] dnBytes = null; byte[] dnBytes = null;
try { try {
@ -3022,7 +3024,8 @@ public class SydApi4j implements SydApi {
public String getCertByDn(String dn) throws UnsupportedEncodingException { public String getCertByDn(String dn) throws UnsupportedEncodingException {
byte[] dnBytes = null; byte[] dnBytes = null;
try { try {
dnBytes = dn.getBytes("gbk"); String dnsort = verifyDn(dn);
dnBytes = dnsort.getBytes("gbk");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); e.printStackTrace();
throw e; throw e;
@ -3094,7 +3097,7 @@ public class SydApi4j implements SydApi {
public RetWrap generateDEByDnSoft(String certSerial, byte[] oData) { public RetWrap generateDEByDnSoft(String certSerial, byte[] oData) {
certSerial = DnUtil.verifyDn(certSerial); certSerial = verifyDn(certSerial);
RetWrap ret = new RetWrap(); RetWrap ret = new RetWrap();
byte[] dnBytes = null; byte[] dnBytes = null;
@ -3158,7 +3161,7 @@ public class SydApi4j implements SydApi {
public RetWrap generateDEByDnSoftRSA(int alg, String certSerial, byte[] oData) { public RetWrap generateDEByDnSoftRSA(int alg, String certSerial, byte[] oData) {
certSerial = DnUtil.verifyDn(certSerial); certSerial = verifyDn(certSerial);
RetWrap ret = new RetWrap(); RetWrap ret = new RetWrap();
byte[] dnBytes = null; byte[] dnBytes = null;
@ -3351,7 +3354,7 @@ public class SydApi4j implements SydApi {
public RetWrap decryptDEByDNSoft(String certSerial, String pCipherKey) { public RetWrap decryptDEByDNSoft(String certSerial, String pCipherKey) {
certSerial = DnUtil.verifyDn(certSerial); certSerial = verifyDn(certSerial);
byte[] dnBytes = null; byte[] dnBytes = null;
try { try {
@ -3390,7 +3393,7 @@ public class SydApi4j implements SydApi {
public RetWrap decryptDEByDNSoftRSA(String certSerial, String pCipherKey) { public RetWrap decryptDEByDNSoftRSA(String certSerial, String pCipherKey) {
certSerial = DnUtil.verifyDn(certSerial); certSerial = verifyDn(certSerial);
byte[] dnBytes = null; byte[] dnBytes = null;
try { try {
@ -11500,7 +11503,7 @@ public class SydApi4j implements SydApi {
} }
public String attachedSignSoft(byte[] orgData, String sCertDN) throws UnsupportedEncodingException { public String attachedSignSoft(byte[] orgData, String sCertDN) throws UnsupportedEncodingException {
sCertDN = DnUtil.verifyDn(sCertDN); sCertDN = verifyDn(sCertDN);
String pk = this.GetDNPublicKey(SydApi.FOR_SIGN, sCertDN.getBytes("GBK")); String pk = this.GetDNPublicKey(SydApi.FOR_SIGN, sCertDN.getBytes("GBK"));
try { try {
byte[] hash = this.SM3Hash(pk, orgData); byte[] hash = this.SM3Hash(pk, orgData);
@ -11618,7 +11621,7 @@ 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 = DnUtil.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");
@ -11631,7 +11634,7 @@ public class SydApi4j implements SydApi {
} }
public String attachedSignRSAHsm(int rFlag,byte[] orgData, String sCertDN) { public String attachedSignRSAHsm(int rFlag,byte[] orgData, String sCertDN) {
sCertDN = DnUtil.verifyDn( sCertDN ); sCertDN = verifyDn( sCertDN );
Proto8018 proto = new Proto8018(); Proto8018 proto = new Proto8018();
byte[] dn = new byte[256]; byte[] dn = new byte[256];
@ -11693,7 +11696,7 @@ public class SydApi4j implements SydApi {
public String attachedSignHsm(byte[] orgData, String sCertDN) { public String attachedSignHsm(byte[] orgData, String sCertDN) {
sCertDN = DnUtil.verifyDn( sCertDN ); sCertDN = verifyDn( sCertDN );
Proto8018 proto = new Proto8018(); Proto8018 proto = new Proto8018();
byte[] dn = new byte[256]; byte[] dn = new byte[256];

View File

@ -91,5 +91,12 @@ public class Shanghairenhang {
byte[] bytes = api.SYD_NakedSign(orgData2k, pcDnEN); byte[] bytes = api.SYD_NakedSign(orgData2k, pcDnEN);
System.out.println(Util.bytes2HexString(bytes)); System.out.println(Util.bytes2HexString(bytes));
} }
@Test
public void aboutCert(){
//System.setProperty("com.sunyard.sydapi4j.debug", "true");
SydApiBuilder builder = new SydApiBuilder().setIp("192.168.0.200");
SydApi4j api = (SydApi4j) builder.build();
}
} }