24
24
import java .util .List ;
25
25
import java .util .Map ;
26
26
27
+ import org .bouncycastle .jce .provider .BouncyCastleProvider ;
27
28
import org .slf4j .Logger ;
28
29
import org .slf4j .LoggerFactory ;
29
- import org .bouncycastle .jce .provider .BouncyCastleProvider ;
30
30
31
31
import com .egzosn .pay .common .bean .result .PayException ;
32
32
import com .egzosn .pay .common .exception .PayErrorException ;
38
38
* 证书文件可信校验
39
39
*
40
40
* @author junying.wjy
41
- * @version $Id: AntCertificationUtil.java, v 0.1 2019-07-29 下午04:46 junying.wjy Exp $
42
- *
43
41
* @author egan update 2020/10/12
44
- *
42
+ * @version $Id: AntCertificationUtil.java, v 0.1 2019-07-29 下午04:46 junying.wjy Exp $
45
43
*/
46
44
public class AntCertificationUtil {
47
45
private static final Logger LOGGER = LoggerFactory .getLogger (AntCertificationUtil .class );
46
+
48
47
static {
49
- Security .removeProvider ("SunEC" );
50
- Security .addProvider (new BouncyCastleProvider ());
48
+ if (null == Security .getProvider ("BC" )) {
49
+ Security .removeProvider ("SunEC" );
50
+ Security .addProvider (new BouncyCastleProvider ());
51
+ }
51
52
}
53
+
52
54
/**
53
55
* 验证证书是否可信
54
56
*
@@ -141,7 +143,7 @@ private static boolean verifyCertChain(X509Certificate[] certs, X509Certificate[
141
143
//验证证书链
142
144
for (int i = 1 ; i < certs .length ; i ++) {
143
145
X509Certificate cert = certs [i ];
144
- if (!checkValidity (cert )){
146
+ if (!checkValidity (cert )) {
145
147
return false ;
146
148
}
147
149
verifySignature (prev .getPublicKey (), cert );
@@ -155,7 +157,7 @@ private static boolean verifyCertChain(X509Certificate[] certs, X509Certificate[
155
157
/**
156
158
* 验证证书链是否是信任证书库中证书签发的
157
159
*
158
- * @param cert 目标验证证书
160
+ * @param cert 目标验证证书
159
161
* @return 验证结果
160
162
*/
161
163
private static boolean checkValidity (X509Certificate cert ) {
@@ -172,13 +174,11 @@ private static boolean checkValidity(X509Certificate cert) {
172
174
}
173
175
174
176
175
-
176
- private static void verifySignature (PublicKey publicKey , X509Certificate cert ){
177
+ private static void verifySignature (PublicKey publicKey , X509Certificate cert ) {
177
178
try {
178
179
cert .verify (publicKey );
179
- }
180
- catch (GeneralSecurityException e ) {
181
- throw new PayErrorException (new PayException ("证书校验失败" , e .getMessage ()));
180
+ } catch (GeneralSecurityException e ) {
181
+ throw new PayErrorException (new PayException ("证书校验失败" , e .getMessage ()));
182
182
}
183
183
}
184
184
@@ -281,7 +281,7 @@ private static void addressingDown(final Map<Principal, X509Certificate> issuerM
281
281
addressingDown (issuerMap , certChain , subject );
282
282
}
283
283
284
- private static X509Certificate [] readPemCertChain (String cert ){
284
+ private static X509Certificate [] readPemCertChain (String cert ) {
285
285
ByteArrayInputStream inputStream = new ByteArrayInputStream (cert .getBytes ());
286
286
CertificateFactory factory = null ;
287
287
try {
@@ -305,7 +305,7 @@ public static String getRootCertSN(String rootCertContent) {
305
305
String rootCertSN = null ;
306
306
try {
307
307
X509Certificate [] x509Certificates = readPemCertChain (rootCertContent );
308
- if (null == x509Certificates ){
308
+ if (null == x509Certificates ) {
309
309
return null ;
310
310
}
311
311
MessageDigest md = MessageDigest .getInstance ("MD5" );
@@ -383,8 +383,6 @@ public static String getCertPublicKey(String certContent) {
383
383
}
384
384
385
385
386
-
387
-
388
386
public static String readFromInputStream (InputStream cert ) {
389
387
try {
390
388
return new String (IOUtils .toByteArray (cert ), StandardCharsets .UTF_8 );
0 commit comments