Skip to content

Commit

Permalink
TKSS-678: Better certificate key usage checking on TLCP
Browse files Browse the repository at this point in the history
  • Loading branch information
johnshajiang committed Feb 6, 2024
1 parent 7699a9e commit 44b6b4f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import java.security.cert.*;

import com.tencent.kona.pkix.PKIXUtils;
import com.tencent.kona.sun.security.util.KnownOIDs;
import com.tencent.kona.sun.security.x509.NetscapeCertTypeExtension;

Expand Down Expand Up @@ -238,14 +237,7 @@ private boolean checkKeyUsage(X509Certificate cert, int bit) {
*/
private void checkTLSClient(X509Certificate cert, Set<String> exts)
throws CertificateException {
if (PKIXUtils.isSMCert(cert)) {
if (!checkKeyUsage(cert, KU_KEY_ENCIPHERMENT)
&& !checkKeyUsage(cert, KU_SIGNATURE)) {
throw new ValidatorException(
"SM certificate must allow encipherment or digital signature",
ValidatorException.T_EE_EXTENSIONS, cert);
}
} else if (!checkKeyUsage(cert, KU_SIGNATURE)) {
if (!checkKeyUsage(cert, KU_SIGNATURE)) {
throw new ValidatorException
("KeyUsage does not allow digital signatures",
ValidatorException.T_EE_EXTENSIONS, cert);
Expand Down Expand Up @@ -277,20 +269,18 @@ private void checkTLSClient(X509Certificate cert, Set<String> exts)
*/
private void checkTLSServer(X509Certificate cert, String parameter,
Set<String> exts) throws CertificateException {
if (PKIXUtils.isSMCert(cert)) {
if (!checkKeyUsage(cert, KU_KEY_ENCIPHERMENT)
&& !checkKeyUsage(cert, KU_SIGNATURE)) {
throw new ValidatorException(
"SM certificate must allow encipherment or digital signature",
ValidatorException.T_EE_EXTENSIONS, cert);
}
} else if (KU_SERVER_ENCRYPTION.contains(parameter)) {
if (KU_SERVER_ENCRYPTION.contains(parameter)) {
if (!checkKeyUsage(cert, KU_KEY_ENCIPHERMENT)) {
throw new ValidatorException
("KeyUsage does not allow key encipherment",
ValidatorException.T_EE_EXTENSIONS, cert);
}
} else if (KU_SERVER_SIGNATURE.contains(parameter)) {
} else if (KU_SERVER_SIGNATURE.contains(parameter)
// SM2 and SM2E are used on TLCP 1.1 only,
// and the first certificate, namely sign certificate,
// always has digitalSignature key usage.
|| "SM2".equalsIgnoreCase(parameter)
|| "SM2E".equalsIgnoreCase(parameter)) {
if (!checkKeyUsage(cert, KU_SIGNATURE)) {
throw new ValidatorException
("KeyUsage does not allow digital signatures",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public SecretKey deriveKey(String algorithm,
} else {
if (protocolVersion.isTLCP11()) {
masterAlg = "TlcpMasterSecret";
hashAlg = HashAlg.H_SM3;
hashAlg = cipherSuite.hashAlg;
} else if (protocolVersion.id >= ProtocolVersion.TLS12.id) {
masterAlg = "SunTls12MasterSecret";
hashAlg = cipherSuite.hashAlg;
Expand Down

0 comments on commit 44b6b4f

Please sign in to comment.