Merge remote-tracking branch 'origin/dev-shangqing' into dev-shangqing

This commit is contained in:
yihzhou
2022-10-26 17:36:02 +08:00
5 changed files with 116 additions and 1 deletions
@@ -2,6 +2,7 @@ package com.sunyard.util;
import cn.hutool.core.codec.Base64;
import com.sunyard.RetWrap;
import com.sunyard.constant.CertUsage;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import sun.misc.BASE64Encoder;
@@ -13,6 +14,8 @@ import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.HashSet;
import java.util.Set;
public class CertUtil {
public static String encodeBase64(byte[] data) {
@@ -109,4 +112,32 @@ public class CertUtil {
}
}
public static boolean certUsageCheck(String cert , CertUsage... us ) throws CertificateException, NoSuchProviderException {
if ( null == us || us.length == 0) {
return true;
}
X509Certificate certificate = convertToX509Cert(cert);
boolean[] usage = certificate.getKeyUsage();
if ( null == usage ) {
return false;
}
boolean checkRet = true;
Set<Integer> uset = new HashSet<Integer>();
for ( CertUsage u : us ) {
uset.add( u.ordinal() );
}
for ( int i = 0; i < usage.length; i++ ) {
if ( usage[ i ] ) {
checkRet &= uset.contains( i );
}
}
return checkRet;
}
}