From c083dc24d06fcfb7c34e6f1821acc33cd4446892 Mon Sep 17 00:00:00 2001 From: Goetz Lindenmaier Date: Fri, 13 Nov 2026 21:59:07 +0000 Subject: [PATCH 1/7] 8392771: Add 2 Certum roots Reviewed-by: mbaesken Backport-of: 0d1b5e73ec2c2cd0f9991aa7ed2dffb92209c600 --- make/data/cacerts/certumec384ca | 22 ++++++++++ make/data/cacerts/certumtrustedrootca | 40 +++++++++++++++++++ .../certification/CAInterop.java | 35 ++++++++++++++++ .../security/lib/cacerts/VerifyCACerts.java | 10 +++-- 4 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 make/data/cacerts/certumec384ca create mode 100644 make/data/cacerts/certumtrustedrootca diff --git a/make/data/cacerts/certumec384ca b/make/data/cacerts/certumec384ca new file mode 100644 index 00000000000..47a276e915a --- /dev/null +++ b/make/data/cacerts/certumec384ca @@ -0,0 +1,22 @@ +Owner: CN=Certum EC-384 CA, OU=Certum Certification Authority, O=Asseco Data Systems S.A., C=PL +Issuer: CN=Certum EC-384 CA, OU=Certum Certification Authority, O=Asseco Data Systems S.A., C=PL +Serial number: 788f275c81125220a504d02dddba73f4 +Valid from: Mon Mar 26 07:24:54 GMT 2018 until: Thu Mar 26 07:24:54 GMT 2043 +Signature algorithm name: SHA384withECDSA +Subject Public Key Algorithm: 384-bit EC (secp384r1) key +Version: 3 +-----BEGIN CERTIFICATE----- +MIICZTCCAeugAwIBAgIQeI8nXIESUiClBNAt3bpz9DAKBggqhkjOPQQDAzB0MQsw +CQYDVQQGEwJQTDEhMB8GA1UEChMYQXNzZWNvIERhdGEgU3lzdGVtcyBTLkEuMScw +JQYDVQQLEx5DZXJ0dW0gQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkxGTAXBgNVBAMT +EENlcnR1bSBFQy0zODQgQ0EwHhcNMTgwMzI2MDcyNDU0WhcNNDMwMzI2MDcyNDU0 +WjB0MQswCQYDVQQGEwJQTDEhMB8GA1UEChMYQXNzZWNvIERhdGEgU3lzdGVtcyBT +LkEuMScwJQYDVQQLEx5DZXJ0dW0gQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkxGTAX +BgNVBAMTEENlcnR1bSBFQy0zODQgQ0EwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAATE +KI6rGFtqvm5kN2PkzeyrOvfMobgOgknXhimfoZTy42B4mIF4Bk3y7JoOV2CDn7Tm +Fy8as10CW4kjPMIRBSqniBMY81CE1700LCeJVf/OTOffph8oxPBUw7l8t1Ot68Kj +QjBAMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFI0GZnQkdjrzife81r1HfS+8 +EF9LMA4GA1UdDwEB/wQEAwIBBjAKBggqhkjOPQQDAwNoADBlAjADVS2m5hjEfO/J +UG7BJw+ch69u1RsIGL2SKcHvlJF40jocVYli5RsJHrpka/F2tNQCMQC0QoSZ/6vn +nvuRlydd3LBbMHHOXjgaatkl5+r3YZJW+OraNsKHZZYuciUvf9/DE8k= +-----END CERTIFICATE----- diff --git a/make/data/cacerts/certumtrustedrootca b/make/data/cacerts/certumtrustedrootca new file mode 100644 index 00000000000..fb1dc35f62c --- /dev/null +++ b/make/data/cacerts/certumtrustedrootca @@ -0,0 +1,40 @@ +Owner: CN=Certum Trusted Root CA, OU=Certum Certification Authority, O=Asseco Data Systems S.A., C=PL +Issuer: CN=Certum Trusted Root CA, OU=Certum Certification Authority, O=Asseco Data Systems S.A., C=PL +Serial number: 1ebf5950b8c980374c06f7eb554fb5ed +Valid from: Fri Mar 16 12:10:13 GMT 2018 until: Mon Mar 16 12:10:13 GMT 2043 +Signature algorithm name: SHA512withRSA +Subject Public Key Algorithm: 4096-bit RSA key +Version: 3 +-----BEGIN CERTIFICATE----- +MIIFwDCCA6igAwIBAgIQHr9ZULjJgDdMBvfrVU+17TANBgkqhkiG9w0BAQ0FADB6 +MQswCQYDVQQGEwJQTDEhMB8GA1UEChMYQXNzZWNvIERhdGEgU3lzdGVtcyBTLkEu +MScwJQYDVQQLEx5DZXJ0dW0gQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkxHzAdBgNV +BAMTFkNlcnR1bSBUcnVzdGVkIFJvb3QgQ0EwHhcNMTgwMzE2MTIxMDEzWhcNNDMw +MzE2MTIxMDEzWjB6MQswCQYDVQQGEwJQTDEhMB8GA1UEChMYQXNzZWNvIERhdGEg +U3lzdGVtcyBTLkEuMScwJQYDVQQLEx5DZXJ0dW0gQ2VydGlmaWNhdGlvbiBBdXRo +b3JpdHkxHzAdBgNVBAMTFkNlcnR1bSBUcnVzdGVkIFJvb3QgQ0EwggIiMA0GCSqG +SIb3DQEBAQUAA4ICDwAwggIKAoICAQDRLY67tzbqbTeRn06TpwXkKQMlzhyC93yZ +n0EGze2jusDbCSzBfN8pfktlL5On1AFrAygYo9idBcEq2EXxkd7fO9CAAozPOA/q +p1x4EaTByIVcJdPTsuclzxFUl6s1wB52HO8AU5853BSlLCIls3Jy/I2z5T4IHhQq +NwuIPMqw9MjCoa68wb4pZ1Xi/K1ZXP69VyywkI3C7Te2fJmItdUDmj0VDT06qKhF +8JVOJVkdzZhpu9PMMsmN74H+rX2Ju7pgE8pllWeg8xn2A1bUatMn4qGtg/BKEiJ3 +HAVz4hlxQsDsdUaakFjgao4rpUYwBI4Zshfjvqm6f1bxJAPXsiEodg42MEx51UGa +mqi4NboMOvJEGyCI98Ul1z3G4z5D3Yf+xOr1Uz5MZf87Sst4WmsXXw3Hw09Omiqi +7VdNIuJGmj8PkTQkfVXjjJU30xrwCSss0smNtA0Aq2cpKNgB9RkEth2+dv5yXMSF +ytKAQd8FqKPVhJBPC/PgP5sZ0jeJP/J7UhyM9uH3PAeXjA6iWYEMspA90+NZRu0P +qafegGtaqge2Gcu8V/OXIXoMsSt0Puvap2ctTMSYnjYJdmZm/Bo/6khUHL4wvYBQ +v3y1zgD2DGHZ5yQD4OMBgQ692IU0iL2yNqh7XAjlRICMb/gv1SHKHRzQ+8S1h9E6 +Tsd2tTVItQIDAQABo0IwQDAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBSM+xx1 +vALTn04uSNn5YFSqxLNP+jAOBgNVHQ8BAf8EBAMCAQYwDQYJKoZIhvcNAQENBQAD +ggIBAEii1QALLtA/vBzVtVRJHlpr9OTy4EA34MwUe7nJ+jW1dReTagVphZzNTxl4 +WxmB82M+w85bj/UvXgF2Ez8sALnNllI5SW0ETsXpD4YN4fqzX4IS8TrOZgYkNCvo +zMrnadyHncI013nR03e4qllY/p0m+jiGPp2Kh2RX5Rc64vmNueMzeMGQ2Ljdt4NR +5MTMI9UGfOZR0800McD2RrsLrfw9EAUqO0qRJe6M1ISHgCq8CYyqOhNf6DR5UMEQ +GfnTKB7U0VEwKbOukGfWHwpjscWpxkIxYxeU72nLL/qMFH3EQxiJ2fAyQOaA4kZf +5ePBAFmo+eggvIksDkc0C+pXwlM2/KfUrzHN/gLldfq5Jwn58/U7yn2fqSLLiMmq +0Uc9NneoWWRrJ8/vJ8HjJLWG965+Mk2weWjROeiQWMODvA8s1pfrzgzhIMfatz7D +P78v3DSk+yshzWePS/Tj6tQ/50+6uaWTRRxmHyH6ZF5v4HaUMst19W7l9o/HuKTM +qJZ9ZPskWkoDbGs4xugDQ5r3V7mzKWmTOPQD8rv7gmsHINFSH5pkAnuYZttcTVoP +0ISVoDwUQwbKytu4QTbaakRnh6+v40URFWkIsr4WOZckbxJF0WddCajJFdr60qZf +E2Efv4WstK2tBZQIgx51F9NxO5NQI1mg7TyRVJ12AMXDuDjb +-----END CERTIFICATE----- diff --git a/test/jdk/security/infra/java/security/cert/CertPathValidator/certification/CAInterop.java b/test/jdk/security/infra/java/security/cert/CertPathValidator/certification/CAInterop.java index d8e905c01a9..e241b29e219 100644 --- a/test/jdk/security/infra/java/security/cert/CertPathValidator/certification/CAInterop.java +++ b/test/jdk/security/infra/java/security/cert/CertPathValidator/certification/CAInterop.java @@ -537,6 +537,34 @@ * wisekeyglobalrootgcca CRL */ +/* + * @test id=certumtrustedrootca + * @bug 8392771 + * @summary Interoperability tests with Certum Trusted Root CA + * @library /test/lib + * @build jtreg.SkippedException ValidatePathWithURL CAInterop + * @run main/othervm/manual -Djava.security.debug=certpath,ocsp CAInterop + * certumtrustedrootca OCSP + * @run main/othervm/manual -Djava.security.debug=certpath,ocsp + * -Dcom.sun.security.ocsp.useget=false CAInterop certumtrustedrootca OCSP + * @run main/othervm/manual -Djava.security.debug=certpath CAInterop + * certumtrustedrootca CRL + */ + +/* + * @test id=certumec384ca + * @bug 8392771 + * @summary Interoperability tests with Certum EC-384 CA + * @library /test/lib + * @build jtreg.SkippedException ValidatePathWithURL CAInterop + * @run main/othervm/manual -Djava.security.debug=certpath,ocsp CAInterop + * certumec384ca OCSP + * @run main/othervm/manual -Djava.security.debug=certpath,ocsp + * -Dcom.sun.security.ocsp.useget=false CAInterop certumec384ca OCSP + * @run main/othervm/manual -Djava.security.debug=certpath CAInterop + * certumec384ca CRL + */ + /** * Collection of certificate validation tests for interoperability with external CAs. * These tests are marked as manual as they depend on external infrastructure and may fail @@ -712,6 +740,13 @@ private CATestURLs getTestURLs(String alias) { new CATestURLs("https://gcvalidssl.hightrusted.com", "https://gcrevokedssl.hightrusted.com"); + case "certumtrustedrootca" -> + new CATestURLs("https://valid-ctrca.certificates.certum.pl", + "https://revoked-ctrca.certificates.certum.pl"); + case "certumec384ca" -> + new CATestURLs("https://valid-cec384ca.certificates.certum.pl", + "https://revoked-cec384ca.certificates.certum.pl"); + default -> throw new RuntimeException("No test setup found for: " + alias); }; } diff --git a/test/jdk/sun/security/lib/cacerts/VerifyCACerts.java b/test/jdk/sun/security/lib/cacerts/VerifyCACerts.java index 04564b17df0..039268865fb 100644 --- a/test/jdk/sun/security/lib/cacerts/VerifyCACerts.java +++ b/test/jdk/sun/security/lib/cacerts/VerifyCACerts.java @@ -29,7 +29,7 @@ * 8243559 8225072 8258630 8259312 8256421 8225081 8225082 8225083 8245654 * 8305975 8304760 8307134 8295894 8314960 8317373 8317374 8318759 8319187 * 8321408 8316138 8341057 8303770 8350498 8359170 8361212 8372351 8387123 - * 8386371 8395690 + * 8386371 8392771 8395690 * @summary Check root CA entries in cacerts file */ import java.io.ByteArrayInputStream; @@ -48,12 +48,12 @@ public class VerifyCACerts { + File.separator + "security" + File.separator + "cacerts"; // The numbers of certs now. - private static final int COUNT = 105; + private static final int COUNT = 107; // SHA-256 of cacerts, can be generated with // shasum -a 256 cacerts | sed -e 's/../&:/g' | tr '[:lower:]' '[:upper:]' | cut -c1-95 private static final String CHECKSUM - = "6F:B3:DD:04:5B:3D:E2:5C:AE:C7:4D:67:3C:32:88:1D:E8:59:43:2A:DB:62:DA:30:9D:E5:EE:39:3E:2D:23:24"; + = "0F:63:EC:06:53:0E:00:3E:36:EF:A0:FB:08:C4:BF:49:47:AE:57:15:6F:FC:68:C2:B7:67:F1:A1:64:3E:A5:5E"; // Hex formatter to upper case with ":" delimiter private static final HexFormat HEX = HexFormat.ofDelimiter(":").withUpperCase(); @@ -272,6 +272,10 @@ public class VerifyCACerts { "6B:9C:08:E8:6E:B0:F7:67:CF:AD:65:CD:98:B6:21:49:E5:49:4A:67:F5:84:5E:7B:D1:ED:01:9F:27:B8:6B:D6"); put("wisekeyglobalrootgcca [jdk]", "85:60:F9:1C:36:24:DA:BA:95:70:B5:FE:A0:DB:E3:6F:F1:1A:83:23:BE:94:86:85:4F:B3:F3:4A:55:71:19:8D"); + put("certumtrustedrootca [jdk]", + "FE:76:96:57:38:55:77:3E:37:A9:5E:7A:D4:D9:CC:96:C3:01:57:C1:5D:31:76:5B:A9:B1:57:04:E1:AE:78:FD"); + put("certumec384ca [jdk]", + "6B:32:80:85:62:53:18:AA:50:D1:73:C9:8D:8B:DA:09:D5:7E:27:41:3D:11:4C:F7:87:A0:F5:D0:6C:03:0C:F6"); } }; From 89bdf538a618cf8bf25079d27a7b64c4ad6a1576 Mon Sep 17 00:00:00 2001 From: Goetz Lindenmaier Date: Sat, 14 Nov 2026 16:38:00 +0000 Subject: [PATCH 2/7] 8243585: AlgorithmChecker::check throws confusing exception when it rejects the signer key Reviewed-by: rschmelter Backport-of: 49f9d8031e9c678e20dcfc1ba06758b511a26b07 --- .../provider/certpath/AlgorithmChecker.java | 269 +++++++++--------- .../CertPathConstraintsParameters.java | 8 +- .../certpath/PKIXCertPathValidator.java | 4 +- .../sun/security/tools/keytool/Main.java | 10 +- .../security/util/AlgorithmDecomposer.java | 109 ++++--- .../util/DisabledAlgorithmConstraints.java | 13 +- .../ssl/SSLContextImpl/TrustTrustedCert.java | 4 +- .../tools/jarsigner/CheckSignerCertChain.java | 4 +- .../sun/security/tools/jarsigner/Warning.java | 18 +- 9 files changed, 210 insertions(+), 229 deletions(-) diff --git a/src/java.base/share/classes/sun/security/provider/certpath/AlgorithmChecker.java b/src/java.base/share/classes/sun/security/provider/certpath/AlgorithmChecker.java index 54c73c832dd..4451aa7e533 100644 --- a/src/java.base/share/classes/sun/security/provider/certpath/AlgorithmChecker.java +++ b/src/java.base/share/classes/sun/security/provider/certpath/AlgorithmChecker.java @@ -72,10 +72,10 @@ public final class AlgorithmChecker extends PKIXCertPathChecker { private static final Debug debug = Debug.getInstance("certpath"); private final AlgorithmConstraints constraints; - private final PublicKey trustedPubKey; private final Date date; - private PublicKey prevPubKey; private final String variant; + private PublicKey trustedPubKey; + private PublicKey prevPubKey; private TrustAnchor anchor; private static final Set SIGNATURE_PRIMITIVE_SET = @@ -88,10 +88,6 @@ public final class AlgorithmChecker extends PKIXCertPathChecker { CryptoPrimitive.PUBLIC_KEY_ENCRYPTION, CryptoPrimitive.KEY_AGREEMENT)); - private static final DisabledAlgorithmConstraints - certPathDefaultConstraints = - DisabledAlgorithmConstraints.certPathConstraints(); - /** * Create a new {@code AlgorithmChecker} with the given * {@code TrustAnchor} and {@code String} variant. @@ -102,7 +98,7 @@ public final class AlgorithmChecker extends PKIXCertPathChecker { * passed will set it to Validator.GENERIC. */ public AlgorithmChecker(TrustAnchor anchor, String variant) { - this(anchor, certPathDefaultConstraints, null, variant); + this(anchor, null, null, variant); } /** @@ -139,19 +135,11 @@ public AlgorithmChecker(TrustAnchor anchor, AlgorithmConstraints constraints, Date date, String variant) { if (anchor != null) { - if (anchor.getTrustedCert() != null) { - this.trustedPubKey = anchor.getTrustedCert().getPublicKey(); - } else { - this.trustedPubKey = anchor.getCAPublicKey(); - } - this.anchor = anchor; - } else { - this.trustedPubKey = null; + setTrustAnchorAndKeys(anchor); } - this.prevPubKey = this.trustedPubKey; - this.constraints = (constraints == null ? certPathDefaultConstraints : - constraints); + this.constraints = constraints == null ? + DisabledAlgorithmConstraints.certPathConstraints() : constraints; this.date = date; this.variant = (variant == null ? Validator.VAR_GENERIC : variant); } @@ -170,18 +158,14 @@ public AlgorithmChecker(TrustAnchor anchor, * passed will set it to Validator.GENERIC. */ public AlgorithmChecker(TrustAnchor anchor, Date date, String variant) { - this(anchor, certPathDefaultConstraints, date, variant); + this(anchor, null, date, variant); } @Override public void init(boolean forward) throws CertPathValidatorException { // Note that this class does not support forward mode. if (!forward) { - if (trustedPubKey != null) { - prevPubKey = trustedPubKey; - } else { - prevPubKey = null; - } + prevPubKey = trustedPubKey; } else { throw new CertPathValidatorException("forward checking not supported"); @@ -205,8 +189,8 @@ public void check(Certificate cert, Collection unresolvedCritExts) throws CertPathValidatorException { - if (!(cert instanceof X509Certificate) || constraints == null) { - // ignore the check for non-x.509 certificate or null constraints + if (!(cert instanceof X509Certificate)) { + // ignore the check for non-x.509 certificate return; } @@ -231,115 +215,114 @@ public void check(Certificate cert, PublicKey currPubKey = cert.getPublicKey(); String currSigAlg = x509Cert.getSigAlgName(); - // Check the signature algorithm and parameters against constraints. - if (!constraints.permits(SIGNATURE_PRIMITIVE_SET, currSigAlg, - currSigAlgParams)) { - throw new CertPathValidatorException( - "Algorithm constraints check failed on signature " + - "algorithm: " + currSigAlg, null, null, -1, - BasicReason.ALGORITHM_CONSTRAINED); - } - - // Assume all key usage bits are set if key usage is not present - Set primitives = KU_PRIMITIVE_SET; - - if (keyUsage != null) { - primitives = EnumSet.noneOf(CryptoPrimitive.class); - - if (keyUsage[0] || keyUsage[1] || keyUsage[5] || keyUsage[6]) { - // keyUsage[0]: KeyUsage.digitalSignature - // keyUsage[1]: KeyUsage.nonRepudiation - // keyUsage[5]: KeyUsage.keyCertSign - // keyUsage[6]: KeyUsage.cRLSign - primitives.add(CryptoPrimitive.SIGNATURE); - } - - if (keyUsage[2]) { // KeyUsage.keyEncipherment - primitives.add(CryptoPrimitive.KEY_ENCAPSULATION); - } - - if (keyUsage[3]) { // KeyUsage.dataEncipherment - primitives.add(CryptoPrimitive.PUBLIC_KEY_ENCRYPTION); - } - - if (keyUsage[4]) { // KeyUsage.keyAgreement - primitives.add(CryptoPrimitive.KEY_AGREEMENT); - } - - // KeyUsage.encipherOnly and KeyUsage.decipherOnly are - // undefined in the absence of the keyAgreement bit. - - if (primitives.isEmpty()) { - throw new CertPathValidatorException( - "incorrect KeyUsage extension bits", - null, null, -1, PKIXReason.INVALID_KEY_USAGE); + if (constraints instanceof DisabledAlgorithmConstraints) { + DisabledAlgorithmConstraints dac = + (DisabledAlgorithmConstraints)constraints; + if (prevPubKey != null && prevPubKey == trustedPubKey) { + // check constraints of trusted public key (make sure + // algorithm and size is not restricted) + CertPathConstraintsParameters cp = + new CertPathConstraintsParameters(trustedPubKey, variant, + anchor, date); + dac.permits(trustedPubKey.getAlgorithm(), cp, true); } - } - - ConstraintsParameters cp = - new CertPathConstraintsParameters(x509Cert, variant, + // Check the signature algorithm and parameters against constraints + CertPathConstraintsParameters cp = + new CertPathConstraintsParameters(x509Cert, variant, anchor, date); - - // Check against local constraints if it is DisabledAlgorithmConstraints - if (constraints instanceof DisabledAlgorithmConstraints) { - ((DisabledAlgorithmConstraints)constraints).permits(currSigAlg, - currSigAlgParams, cp, true); - // DisabledAlgorithmsConstraints does not check primitives, so key - // additional key check. - + dac.permits(currSigAlg, currSigAlgParams, cp, true); } else { - // Perform the default constraints checking anyway. - certPathDefaultConstraints.permits(currSigAlg, currSigAlgParams, cp, true); - // Call locally set constraints to check key with primitives. - if (!constraints.permits(primitives, currPubKey)) { - throw new CertPathValidatorException( - "Algorithm constraints check failed on key " + - currPubKey.getAlgorithm() + " with size of " + - sun.security.util.KeyUtil.getKeySize(currPubKey) + - "bits", + if (prevPubKey != null) { + if (!constraints.permits(SIGNATURE_PRIMITIVE_SET, + currSigAlg, prevPubKey, currSigAlgParams)) { + throw new CertPathValidatorException( + "Algorithm constraints check failed on " + + currSigAlg + "signature and " + + currPubKey.getAlgorithm() + " key with size of " + + sun.security.util.KeyUtil.getKeySize(currPubKey) + + "bits", + null, null, -1, BasicReason.ALGORITHM_CONSTRAINED); + } + } else { + if (!constraints.permits(SIGNATURE_PRIMITIVE_SET, + currSigAlg, currSigAlgParams)) { + throw new CertPathValidatorException( + "Algorithm constraints check failed on " + + "signature algorithm: " + currSigAlg, null, null, -1, BasicReason.ALGORITHM_CONSTRAINED); + } } - } - - // If there is no previous key, set one and exit - if (prevPubKey == null) { - prevPubKey = currPubKey; - return; - } + // Assume all key usage bits are set if key usage is not present + Set primitives = KU_PRIMITIVE_SET; - // Check with previous cert for signature algorithm and public key - if (!constraints.permits( - SIGNATURE_PRIMITIVE_SET, - currSigAlg, prevPubKey, currSigAlgParams)) { - throw new CertPathValidatorException( - "Algorithm constraints check failed on " + - "signature algorithm: " + currSigAlg, - null, null, -1, BasicReason.ALGORITHM_CONSTRAINED); - } + if (keyUsage != null) { + primitives = EnumSet.noneOf(CryptoPrimitive.class); - // Inherit key parameters from previous key - if (PKIX.isDSAPublicKeyWithoutParams(currPubKey)) { - // Inherit DSA parameters from previous key - if (!(prevPubKey instanceof DSAPublicKey)) { - throw new CertPathValidatorException("Input key is not " + - "of a appropriate type for inheriting parameters"); + if (keyUsage[0] || keyUsage[1] || keyUsage[5] || keyUsage[6]) { + // keyUsage[0]: KeyUsage.digitalSignature + // keyUsage[1]: KeyUsage.nonRepudiation + // keyUsage[5]: KeyUsage.keyCertSign + // keyUsage[6]: KeyUsage.cRLSign + primitives.add(CryptoPrimitive.SIGNATURE); + } + + if (keyUsage[2]) { // KeyUsage.keyEncipherment + primitives.add(CryptoPrimitive.KEY_ENCAPSULATION); + } + + if (keyUsage[3]) { // KeyUsage.dataEncipherment + primitives.add(CryptoPrimitive.PUBLIC_KEY_ENCRYPTION); + } + + if (keyUsage[4]) { // KeyUsage.keyAgreement + primitives.add(CryptoPrimitive.KEY_AGREEMENT); + } + + // KeyUsage.encipherOnly and KeyUsage.decipherOnly are + // undefined in the absence of the keyAgreement bit. + + if (primitives.isEmpty()) { + throw new CertPathValidatorException( + "incorrect KeyUsage extension bits", + null, null, -1, PKIXReason.INVALID_KEY_USAGE); + } } - - DSAParams params = ((DSAPublicKey)prevPubKey).getParams(); - if (params == null) { + if (!constraints.permits(primitives, currPubKey)) { throw new CertPathValidatorException( - "Key parameters missing from public key."); + "Algorithm constraints check failed on " + + currPubKey.getAlgorithm() + " key with size of " + + sun.security.util.KeyUtil.getKeySize(currPubKey) + + "bits", + null, null, -1, BasicReason.ALGORITHM_CONSTRAINED); } + } - try { - BigInteger y = ((DSAPublicKey)currPubKey).getY(); - KeyFactory kf = KeyFactory.getInstance("DSA"); - DSAPublicKeySpec ks = new DSAPublicKeySpec(y, params.getP(), - params.getQ(), params.getG()); - currPubKey = kf.generatePublic(ks); - } catch (GeneralSecurityException e) { - throw new CertPathValidatorException("Unable to generate " + - "key with inherited parameters: " + e.getMessage(), e); + if (prevPubKey != null) { + // Inherit key parameters from previous key + if (PKIX.isDSAPublicKeyWithoutParams(currPubKey)) { + // Inherit DSA parameters from previous key + if (!(prevPubKey instanceof DSAPublicKey)) { + throw new CertPathValidatorException("Input key is not " + + "of a appropriate type for inheriting parameters"); + } + + DSAParams params = ((DSAPublicKey)prevPubKey).getParams(); + if (params == null) { + throw new CertPathValidatorException( + "Key parameters missing from public key."); + } + + try { + BigInteger y = ((DSAPublicKey)currPubKey).getY(); + KeyFactory kf = KeyFactory.getInstance("DSA"); + DSAPublicKeySpec ks = new DSAPublicKeySpec(y, params.getP(), + params.getQ(), params.getG()); + currPubKey = kf.generatePublic(ks); + } catch (GeneralSecurityException e) { + throw new CertPathValidatorException("Unable to generate " + + "key with inherited parameters: " + + e.getMessage(), e); + } } } @@ -347,6 +330,20 @@ public void check(Certificate cert, prevPubKey = currPubKey; } + /** + * Sets the anchor, trustedPubKey and prevPubKey fields based on the + * specified trust anchor. + */ + private void setTrustAnchorAndKeys(TrustAnchor anchor) { + if (anchor.getTrustedCert() != null) { + this.trustedPubKey = anchor.getTrustedCert().getPublicKey(); + } else { + this.trustedPubKey = anchor.getCAPublicKey(); + } + this.anchor = anchor; + this.prevPubKey = this.trustedPubKey; + } + /** * Try to set the trust anchor of the checker. *

@@ -357,21 +354,9 @@ public void check(Certificate cert, * certificate */ void trySetTrustAnchor(TrustAnchor anchor) { - // Don't bother if the check has started or trust anchor has already - // specified. - if (prevPubKey == null) { - if (anchor == null) { - throw new IllegalArgumentException( - "The trust anchor cannot be null"); - } - - // Don't bother to change the trustedPubKey. - if (anchor.getTrustedCert() != null) { - prevPubKey = anchor.getTrustedCert().getPublicKey(); - } else { - prevPubKey = anchor.getCAPublicKey(); - } - this.anchor = anchor; + // Only set if trust anchor has not already been set. + if (this.trustedPubKey == null) { + setTrustAnchorAndKeys(anchor); } } @@ -387,9 +372,9 @@ void trySetTrustAnchor(TrustAnchor anchor) { static void check(PublicKey key, AlgorithmId algorithmId, String variant, TrustAnchor anchor) throws CertPathValidatorException { - certPathDefaultConstraints.permits(algorithmId.getName(), - algorithmId.getParameters(), - new CertPathConstraintsParameters(key, variant, anchor), true); + DisabledAlgorithmConstraints.certPathConstraints().permits( + algorithmId.getName(), algorithmId.getParameters(), + new CertPathConstraintsParameters(key, variant, anchor, null), true); } } diff --git a/src/java.base/share/classes/sun/security/provider/certpath/CertPathConstraintsParameters.java b/src/java.base/share/classes/sun/security/provider/certpath/CertPathConstraintsParameters.java index dacbd12737a..679e8657451 100644 --- a/src/java.base/share/classes/sun/security/provider/certpath/CertPathConstraintsParameters.java +++ b/src/java.base/share/classes/sun/security/provider/certpath/CertPathConstraintsParameters.java @@ -50,8 +50,8 @@ public class CertPathConstraintsParameters implements ConstraintsParameters { private final Date date; // The variant or usage of this certificate private final String variant; - // The certificate being checked (may be null if a CRL or OCSPResponse is - // being checked) + // The certificate being checked (may be null if a raw public key, a CRL + // or an OCSPResponse is being checked) private final X509Certificate cert; public CertPathConstraintsParameters(X509Certificate cert, @@ -60,8 +60,8 @@ public CertPathConstraintsParameters(X509Certificate cert, } public CertPathConstraintsParameters(Key key, String variant, - TrustAnchor anchor) { - this(key, variant, anchor, null, null); + TrustAnchor anchor, Date date) { + this(key, variant, anchor, date, null); } private CertPathConstraintsParameters(Key key, String variant, diff --git a/src/java.base/share/classes/sun/security/provider/certpath/PKIXCertPathValidator.java b/src/java.base/share/classes/sun/security/provider/certpath/PKIXCertPathValidator.java index de3923bb3bc..0454d84ac46 100644 --- a/src/java.base/share/classes/sun/security/provider/certpath/PKIXCertPathValidator.java +++ b/src/java.base/share/classes/sun/security/provider/certpath/PKIXCertPathValidator.java @@ -176,8 +176,8 @@ private static PKIXCertPathValidatorResult validate(TrustAnchor anchor, List certPathCheckers = new ArrayList<>(); // add standard checkers that we will be using certPathCheckers.add(untrustedChecker); - certPathCheckers.add(new AlgorithmChecker(anchor, null, - params.timestamp(), params.variant())); + certPathCheckers.add(new AlgorithmChecker(anchor, params.timestamp(), + params.variant())); certPathCheckers.add(new KeyChecker(certPathLen, params.targetCertConstraints())); certPathCheckers.add(new ConstraintsChecker(certPathLen)); diff --git a/src/java.base/share/classes/sun/security/tools/keytool/Main.java b/src/java.base/share/classes/sun/security/tools/keytool/Main.java index c117a7cb972..3760f887e98 100644 --- a/src/java.base/share/classes/sun/security/tools/keytool/Main.java +++ b/src/java.base/share/classes/sun/security/tools/keytool/Main.java @@ -1485,7 +1485,7 @@ private void doGenCert(String alias, String sigAlgName, InputStream in, PrintStr PKCS10 req = new PKCS10(rawReq); CertPathConstraintsParameters cpcp = new CertPathConstraintsParameters( - req.getSubjectPublicKeyInfo(), null, null); + req.getSubjectPublicKeyInfo(), null, null, null); checkWeakConstraint(rb.getString("the.certificate.request"), req, cpcp); info.set(X509CertInfo.KEY, new CertificateX509Key(req.getSubjectPublicKeyInfo())); @@ -1599,7 +1599,7 @@ private void doGenCRL(PrintStream out) out.write(crl.getEncodedInternal()); } CertPathConstraintsParameters cpcp = new CertPathConstraintsParameters( - privateKey, null, null); + privateKey, null, null, null); checkWeakConstraint(rb.getString("the.generated.crl"), crl, privateKey, cpcp); } @@ -1648,7 +1648,7 @@ private void doCertReq(String alias, String sigAlgName, PrintStream out) request.print(out); CertPathConstraintsParameters cpcp = new CertPathConstraintsParameters( - request.getSubjectPublicKeyInfo(), null, null); + request.getSubjectPublicKeyInfo(), null, null, null); checkWeakConstraint(rb.getString("the.generated.certificate.request"), request, cpcp); } @@ -2802,7 +2802,7 @@ private void doPrintCertReq(InputStream in, PrintStream out) PublicKey pkey = req.getSubjectPublicKeyInfo(); CertPathConstraintsParameters cpcp = - new CertPathConstraintsParameters(pkey, null, null); + new CertPathConstraintsParameters(pkey, null, null, null); out.printf(rb.getString("PKCS.10.with.weak"), req.getSubjectName(), pkey.getFormat(), @@ -2833,7 +2833,7 @@ private void doPrintCertReq(InputStream in, PrintStream out) CertPathConstraintsParameters cpcp1 = new CertPathConstraintsParameters( - req.getSubjectPublicKeyInfo(), null, null); + req.getSubjectPublicKeyInfo(), null, null, null); checkWeakConstraint(rb.getString("the.certificate.request"), req, cpcp1); } diff --git a/src/java.base/share/classes/sun/security/util/AlgorithmDecomposer.java b/src/java.base/share/classes/sun/security/util/AlgorithmDecomposer.java index ee97d461ac1..69eaa22aafb 100644 --- a/src/java.base/share/classes/sun/security/util/AlgorithmDecomposer.java +++ b/src/java.base/share/classes/sun/security/util/AlgorithmDecomposer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -26,6 +26,8 @@ package sun.security.util; import java.util.HashSet; +import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import java.util.Arrays; import java.util.Collection; @@ -40,6 +42,14 @@ public class AlgorithmDecomposer { private static final Pattern PATTERN = Pattern.compile("with|and|(? DECOMPOSED_DIGEST_NAMES = + Map.of("SHA-1", "SHA1", "SHA-224", "SHA224", "SHA-256", "SHA256", + "SHA-384", "SHA384", "SHA-512", "SHA512", "SHA-512/224", + "SHA512/224", "SHA-512/256", "SHA512/256"); + private static Set decomposeImpl(String algorithm) { Set elements = new HashSet<>(); @@ -93,44 +103,19 @@ public Set decompose(String algorithm) { // signature algorithm "SHA256withRSA". So we need to check both // "SHA-256" and "SHA256" to make the right constraint checking. - // handle special name: SHA-1 and SHA1 - if (elements.contains("SHA1") && !elements.contains("SHA-1")) { - elements.add("SHA-1"); - } - if (elements.contains("SHA-1") && !elements.contains("SHA1")) { - elements.add("SHA1"); + // no need to check further if algorithm doesn't contain "SHA" + if (!algorithm.contains("SHA")) { + return elements; } - // handle special name: SHA-224 and SHA224 - if (elements.contains("SHA224") && !elements.contains("SHA-224")) { - elements.add("SHA-224"); - } - if (elements.contains("SHA-224") && !elements.contains("SHA224")) { - elements.add("SHA224"); - } - - // handle special name: SHA-256 and SHA256 - if (elements.contains("SHA256") && !elements.contains("SHA-256")) { - elements.add("SHA-256"); - } - if (elements.contains("SHA-256") && !elements.contains("SHA256")) { - elements.add("SHA256"); - } - - // handle special name: SHA-384 and SHA384 - if (elements.contains("SHA384") && !elements.contains("SHA-384")) { - elements.add("SHA-384"); - } - if (elements.contains("SHA-384") && !elements.contains("SHA384")) { - elements.add("SHA384"); - } - - // handle special name: SHA-512 and SHA512 - if (elements.contains("SHA512") && !elements.contains("SHA-512")) { - elements.add("SHA-512"); - } - if (elements.contains("SHA-512") && !elements.contains("SHA512")) { - elements.add("SHA512"); + for (Map.Entry e : DECOMPOSED_DIGEST_NAMES.entrySet()) { + if (elements.contains(e.getValue()) && + !elements.contains(e.getKey())) { + elements.add(e.getKey()); + } else if (elements.contains(e.getKey()) && + !elements.contains(e.getValue())) { + elements.add(e.getValue()); + } } return elements; @@ -153,40 +138,44 @@ public static Collection getAliases(String algorithm) { return Arrays.asList(aliases); } - private static void hasLoop(Set elements, String find, String replace) { - if (elements.contains(find)) { - if (!elements.contains(replace)) { - elements.add(replace); - } - elements.remove(find); - } - } - - /* - * This decomposes a standard name into sub-elements with a consistent - * message digest algorithm name to avoid overly complicated checking. + /** + * Decomposes a standard algorithm name into sub-elements and uses a + * consistent message digest algorithm name to avoid overly complicated + * checking. */ - public static Set decomposeOneHash(String algorithm) { + static Set decomposeName(String algorithm) { if (algorithm == null || algorithm.isEmpty()) { return new HashSet<>(); } Set elements = decomposeImpl(algorithm); - hasLoop(elements, "SHA-1", "SHA1"); - hasLoop(elements, "SHA-224", "SHA224"); - hasLoop(elements, "SHA-256", "SHA256"); - hasLoop(elements, "SHA-384", "SHA384"); - hasLoop(elements, "SHA-512", "SHA512"); + // no need to check further if algorithm doesn't contain "SHA" + if (!algorithm.contains("SHA")) { + return elements; + } + + for (Map.Entry e : DECOMPOSED_DIGEST_NAMES.entrySet()) { + if (elements.contains(e.getKey())) { + if (!elements.contains(e.getValue())) { + elements.add(e.getValue()); + } + elements.remove(e.getKey()); + } + } return elements; } - /* - * The provided message digest algorithm name will return a consistent - * naming scheme. + /** + * Decomposes a standard message digest algorithm name into a consistent + * name for matching purposes. + * + * @param algorithm the name to be decomposed + * @return the decomposed name, or the passed in algorithm name if + * it is not a digest algorithm or does not need to be decomposed */ - public static String hashName(String algorithm) { - return algorithm.replace("-", ""); + static String decomposeDigestName(String algorithm) { + return DECOMPOSED_DIGEST_NAMES.getOrDefault(algorithm, algorithm); } } diff --git a/src/java.base/share/classes/sun/security/util/DisabledAlgorithmConstraints.java b/src/java.base/share/classes/sun/security/util/DisabledAlgorithmConstraints.java index 82900620c9e..796f922ae57 100644 --- a/src/java.base/share/classes/sun/security/util/DisabledAlgorithmConstraints.java +++ b/src/java.base/share/classes/sun/security/util/DisabledAlgorithmConstraints.java @@ -331,10 +331,6 @@ private boolean checkConstraints(Set primitives, /** * Key and Certificate Constraints * - * The complete disabling of an algorithm is not handled by Constraints or - * Constraint classes. That is addressed with - * permit(Set, String, AlgorithmParameters) - * * When passing a Key to permit(), the boolean return values follow the * same as the interface class AlgorithmConstraints.permit(). This is to * maintain compatibility: @@ -345,7 +341,6 @@ private boolean checkConstraints(Set primitives, * will be thrown on a failure to better identify why the operation was * disallowed. */ - private static class Constraints { private Map> constraintsMap = new HashMap<>(); @@ -368,9 +363,9 @@ public Constraints(String propertyName, Set constraintSet) { // Check if constraint is a complete disabling of an // algorithm or has conditions. int space = constraintEntry.indexOf(' '); - String algorithm = AlgorithmDecomposer.hashName( - ((space > 0 ? constraintEntry.substring(0, space) : - constraintEntry))); + String algorithm = AlgorithmDecomposer.decomposeDigestName( + space > 0 ? constraintEntry.substring(0, space) : + constraintEntry); List constraintList = constraintsMap.getOrDefault( algorithm.toUpperCase(Locale.ENGLISH), @@ -524,7 +519,7 @@ public void permits(String algorithm, ConstraintsParameters cp, // Get all signature algorithms to check for constraints Set algorithms = new HashSet<>(); if (algorithm != null) { - algorithms.addAll(AlgorithmDecomposer.decomposeOneHash(algorithm)); + algorithms.addAll(AlgorithmDecomposer.decomposeName(algorithm)); algorithms.add(algorithm); } diff --git a/test/jdk/sun/security/ssl/SSLContextImpl/TrustTrustedCert.java b/test/jdk/sun/security/ssl/SSLContextImpl/TrustTrustedCert.java index 6d705000e86..463dd287883 100644 --- a/test/jdk/sun/security/ssl/SSLContextImpl/TrustTrustedCert.java +++ b/test/jdk/sun/security/ssl/SSLContextImpl/TrustTrustedCert.java @@ -159,9 +159,9 @@ protected void runClientApplication(SSLSocket socket) throws Exception { if (expectFail) { // focus on the CertPathValidatorException Throwable t = e.getCause().getCause(); - if (t == null || !t.toString().contains("MD5withRSA")) { + if (t == null || !t.toString().contains("MD5")) { throw new RuntimeException( - "Expected to see MD5withRSA in exception output", t); + "Expected to see MD5 in exception output", t); } } else { throw e; diff --git a/test/jdk/sun/security/tools/jarsigner/CheckSignerCertChain.java b/test/jdk/sun/security/tools/jarsigner/CheckSignerCertChain.java index 7dbcef1d3b2..1652ee9d362 100644 --- a/test/jdk/sun/security/tools/jarsigner/CheckSignerCertChain.java +++ b/test/jdk/sun/security/tools/jarsigner/CheckSignerCertChain.java @@ -129,7 +129,7 @@ public static void main(String[] args) throws Exception { " a.jar ee") .shouldContain("Signature algorithm: MD5withRSA (disabled), 2048-bit RSA key") .shouldContain("Signature algorithm: SHA256withRSA, 2048-bit RSA key") - .shouldContain("Invalid certificate chain: Algorithm constraints check failed on signature algorithm: MD5withRSA") + .shouldContain("Invalid certificate chain: Algorithm constraints check failed on disabled algorithm: MD5 used with certificate: CN=EE") .shouldHaveExitValue(0); kt("-exportcert -alias cacert -rfc -file cacert", "ks"); @@ -139,7 +139,7 @@ public static void main(String[] args) throws Exception { "-keystore caks1 -storepass changeit -verbose -debug") .shouldContain("Signature algorithm: MD5withRSA (disabled), 2048-bit RSA key") .shouldContain("Signature algorithm: SHA256withRSA, 2048-bit RSA key") - .shouldContain("Invalid certificate chain: Algorithm constraints check failed on signature algorithm: MD5withRSA") + .shouldContain("Invalid certificate chain: Algorithm constraints check failed on disabled algorithm: MD5 used with certificate: CN=EE") .shouldHaveExitValue(0); } } diff --git a/test/jdk/sun/security/tools/jarsigner/Warning.java b/test/jdk/sun/security/tools/jarsigner/Warning.java index 08a9ad2478d..16e0b17a1b0 100644 --- a/test/jdk/sun/security/tools/jarsigner/Warning.java +++ b/test/jdk/sun/security/tools/jarsigner/Warning.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -32,7 +32,7 @@ /** * @test - * @bug 8024302 8026037 8130132 + * @bug 8024302 8026037 8130132 8243585 * @summary warnings, errors and -strict * @library /lib/testlibrary /test/lib * @build jdk.test.lib.util.JarUtils @@ -83,7 +83,7 @@ public static void main(String[] args) throws Throwable { issueCert("b", "-sigalg MD5withRSA"); run("jarsigner", "a.jar b") - .shouldMatch("chain is invalid. Reason:.*MD5withRSA"); + .shouldMatch("chain is invalid. Reason:.*MD5.*"); recreateJar(); @@ -175,6 +175,18 @@ public static void main(String[] args) throws Throwable { .shouldHaveExitValue(4) .shouldContain("with signer errors") .shouldMatch("(?s).*Error:.*has expired.*Warning:.*"); + + // Sign jar with Trust Anchor that has a 512 bit key. Make sure + // the error message indicates the key size is restricted. + recreateJar(); + run("keytool", "-delete -alias ca"); + newCert("ca", "-keysize 512", "-validity 365000", "-ext bc:c"); + newCert("d"); + issueCert("d"); + run("jarsigner", "a.jar d") + .shouldContain("chain is invalid. " + + "Reason: Algorithm constraints check failed on " + + "keysize limits: RSA 512 bit key."); } // Creates a new jar without signature From b16871087814dcc9b2c3895cbfff25712d087afd Mon Sep 17 00:00:00 2001 From: Goetz Lindenmaier Date: Sat, 14 Nov 2026 16:42:00 +0000 Subject: [PATCH 3/7] 8302623: jarsigner - use BufferedOutputStream to improve performance while creating the signed jar Backport-of: f82385e58741aa83dccc4876ae69a6033217d9ed --- .../share/classes/jdk/security/jarsigner/JarSigner.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/jdk.jartool/share/classes/jdk/security/jarsigner/JarSigner.java b/src/jdk.jartool/share/classes/jdk/security/jarsigner/JarSigner.java index 08fad553cc9..dc6ef13cc89 100644 --- a/src/jdk.jartool/share/classes/jdk/security/jarsigner/JarSigner.java +++ b/src/jdk.jartool/share/classes/jdk/security/jarsigner/JarSigner.java @@ -687,7 +687,8 @@ private void sign0(ZipFile zipFile, OutputStream os) throw new AssertionError(asae); } - ZipOutputStream zos = new ZipOutputStream(os); + ZipOutputStream zos = new ZipOutputStream( + (os instanceof BufferedOutputStream) ? os : new BufferedOutputStream(os)); Manifest manifest = new Manifest(); byte[] mfRawBytes = null; From f189816a41ccd07dcd895daaa2060d77edc64409 Mon Sep 17 00:00:00 2001 From: Shruthi Acharya Date: Mon, 16 Nov 2026 09:52:37 +0000 Subject: [PATCH 4/7] 8329548: Change KeyUpdate messages from TLS 1.3 8396154: Incorporate review feedback after JDK-8329548 Reviewed-by: andrew Backport-of: 4d65c586fa819dc8dfd8bdf5369c292674120445 --- .../classes/sun/security/ssl/SSLCipher.java | 15 +- .../ssl/SSLEngineImpl/KeyUpdateOnce.java | 545 ++++++++++++++++++ 2 files changed, 557 insertions(+), 3 deletions(-) create mode 100644 test/jdk/sun/security/ssl/SSLEngineImpl/KeyUpdateOnce.java diff --git a/src/java.base/share/classes/sun/security/ssl/SSLCipher.java b/src/java.base/share/classes/sun/security/ssl/SSLCipher.java index 1280bf8db05..f3dd7992722 100644 --- a/src/java.base/share/classes/sun/security/ssl/SSLCipher.java +++ b/src/java.base/share/classes/sun/security/ssl/SSLCipher.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -412,6 +412,7 @@ enum SSLCipher { // Keywords found on the jdk.tls.keyLimits security property. static final String[] tag = {"KEYUPDATE"}; + static final long COUNTDOWNWARN = 20000; // Print debug warning under limit static { final long max = 4611686018427387904L; // 2^62 @@ -423,7 +424,6 @@ public String run() { return Security.getProperty("jdk.tls.keyLimits"); } }); - if (prop != null) { String[] propvalue = prop.split(","); @@ -658,12 +658,21 @@ boolean isNullCipher() { /** * Check if processed bytes have reached the key usage limit. - * If key usage limit is not be monitored, return false. + * If key usage limits are not be monitored, return false. */ public boolean atKeyLimit() { + if (keyLimitCountdown < COUNTDOWNWARN && SSLLogger.isOn) { + SSLLogger.fine("keyLimitCountdown: " + keyLimitCountdown); + } if (keyLimitCountdown >= 0) { return false; } + if (keyLimitEnabled == false) { + if (SSLLogger.isOn) { + SSLLogger.fine("KeyUpdate already sent, skipping"); + } + return false; + } // Turn off limit checking as KeyUpdate will be occurring keyLimitEnabled = false; diff --git a/test/jdk/sun/security/ssl/SSLEngineImpl/KeyUpdateOnce.java b/test/jdk/sun/security/ssl/SSLEngineImpl/KeyUpdateOnce.java new file mode 100644 index 00000000000..b6c7710e7fe --- /dev/null +++ b/test/jdk/sun/security/ssl/SSLEngineImpl/KeyUpdateOnce.java @@ -0,0 +1,545 @@ +/* + * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8329548 + * @library ../../ + * /test/lib + * /javax/net/ssl/templates + * @summary Verify KeyUpdate messages skipped after first one sent. + * + * @run main KeyUpdateOnce server TLS_AES_256_GCM_SHA384 200000 + * @run main KeyUpdateOnce client TLS_AES_256_GCM_SHA384 200000 + */ + +/* + * This test runs in another process so we can monitor the debug + * results. The OutputAnalyzer must see correct debug output to return a + * success. + */ + +import jdk.test.lib.Utils; +import jdk.test.lib.process.OutputAnalyzer; +import jdk.test.lib.process.ProcessTools; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLEngineResult; + +import java.lang.reflect.Field; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.List; + +/** + * This server/client TLS test will force side A to stop reading as it + * continuously writes out. These write ops will trigger the side B to + * request a KeyUpdate. With side A not reading, side B must skip + * sending more KeyUpdate messages. Only one KeyUpdate message will be + * sent by side B. + * + * This test depends on debug messages string match. Changing the KeyUpdate- + * related messages may cause a failure. + */ + +public class KeyUpdateOnce extends SSLContextTemplate { + + private static final int DATALEN = 10240; + private static final int BUF_DATALEN = 4 * DATALEN; + private static final int MAXLOOPS = 150; + private static final int COUNTDOWNLIMIT = 5; + + private static final boolean DEBUG = true; + + private static ByteBuffer cTos; + private static ByteBuffer sToc; + private static ByteBuffer outData; + private final ByteBuffer inData; + + // thread flags + private static boolean ready = false; + private static boolean sc = true; + private static boolean readDone = false; + private static boolean serverWrites = true; + + private static long newLimit; + + // Reflection handle captured on read side once handshake completes + private static Object readSideInputRecord = null; + + protected SSLEngine engine; + private final int delay = 1; + private int totalDataLen = 0; + + KeyUpdateOnce() { + this.inData = ByteBuffer.allocate(BUF_DATALEN); + } + + /** + * args should have: + * server|client, cipherSuite, + * + * Prepending 'p' is for internal use only (test harness relaunch). + */ + public static void main(String[] args) throws Exception { + for (String arg : args) { + System.out.print(" " + arg); + } + System.out.println(); + + // Harness mode: relaunch self with 'p' to force add-opens + debugging flags + if (!"p".equals(args[0])) { + // args[]: 0 = client/server, 1 = cipher suite, 2 = newLimit + System.setProperty("test.java.opts", + System.getProperty("test.java.opts") + + " -Dtest.src=" + System.getProperty("test.src") + + " -Dtest.jdk=" + System.getProperty("test.jdk") + + " -Djavax.net.debug=ssl,handshake" + + " -Djavatest.maxOutputSize=99999999" + + " --add-opens java.base/sun.security.ssl=ALL-UNNAMED"); + + System.out.println("test.java.opts: " + + System.getProperty("test.java.opts")); + + ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder( + "KeyUpdateOnce", "p", args[0], args[1], args[2]); + + OutputAnalyzer output = ProcessTools.executeProcess(pb); + try { + output.shouldContain(String.format( + "\"cipher suite\" : \"%s", args[1])); + System.err.println("Output logs should show KeyUpdate has" + + " been sent and skipped"); + List producedList = output.asLines().stream() + .filter(s -> s.contains("Produced KeyUpdate")) + .toList(); + List skippingList = output.asLines().stream() + .filter(s -> s.contains("KeyUpdate already sent, skipping")) + .toList(); + producedList.forEach(System.err::println); + skippingList.forEach(System.err::println); + System.err.println("\"Produced KeyUpdate\" count = " + producedList.size()); + System.err.println("\"KeyUpdate already send, skipping\" count = " + skippingList.size()); + + /* + * Sometimes debug messages may not be consistent. The below + * checks verify that at least 1 of each message were received. + */ + // Ideally there should be 2 "Produced KeyUpdate" + if (producedList.isEmpty()) { + throw new AssertionError("No \"Produced KeyUpdate\""); + } + // Ideally there should be 5 "KeyUpdate already send, skipping" + if (skippingList.isEmpty()) { + throw new AssertionError("No \"KeyUpdate already send, skipping\""); + } + + } finally { + System.out.println("-- BEGIN Stdout:"); + System.out.println(output.getStdout()); + System.out.println("-- END Stdout"); + System.out.println("-- BEGIN Stderr:"); + System.out.println(output.getStderr()); + System.out.println("-- END Stderr"); + } + return; + } + + // Worker mode: + // args[]: 0 = p, 1 = client/server, 2 = cipher suite, 3 = newLimit + serverWrites = !"client".equals(args[1]); + newLimit = Long.parseLong(args[3]); + + cTos = ByteBuffer.allocateDirect(BUF_DATALEN); + sToc = ByteBuffer.allocateDirect(BUF_DATALEN); + outData = ByteBuffer.allocateDirect(DATALEN); + + byte[] data = new byte[DATALEN]; + Arrays.fill(data, (byte) 0x0A); + outData.put(data).flip(); + + cTos.clear(); + sToc.clear(); + + Thread peer = new Thread(serverWrites ? new Client() : + new Server(args[2])); + peer.start(); + + (serverWrites ? new Server(args[2]) : new Client()).run(); + + peer.interrupt(); + peer.join(); + } + + private static void doTask(SSLEngineResult result, SSLEngine engine) + throws Exception { + if (result.getHandshakeStatus() == + SSLEngineResult.HandshakeStatus.NEED_TASK) { + Runnable runnable; + while ((runnable = engine.getDelegatedTask()) != null) { + print("\trunning delegated task..."); + runnable.run(); + } + SSLEngineResult.HandshakeStatus hsStatus = + engine.getHandshakeStatus(); + if (hsStatus == SSLEngineResult.HandshakeStatus.NEED_TASK) { + throw new Exception("handshake shouldn't need additional tasks"); + } + print("\tnew HandshakeStatus: " + hsStatus); + } + } + + private static void print(String s) { + if (DEBUG) { + System.err.println(s); + } + } + + private static void log(String s, SSLEngineResult r) { + if (DEBUG) { + System.err.println(s + ": " + + r.getStatus() + "/" + r.getHandshakeStatus() + " " + + r.bytesConsumed() + "/" + r.bytesProduced()); + } + } + + private static void dumpBuffers(String aName, ByteBuffer a) { + if (DEBUG) { + System.err.println(aName + " pos=" + a.position() + + " rem=" + a.remaining() + + " lim=" + a.limit() + " cap=" + a.capacity()); + } + } + + void writeLoop() throws Exception { + int i = 0; + SSLEngineResult r; + int countdown = COUNTDOWNLIMIT; + + while (!ready) { + Thread.sleep(delay); + } + + print("Write-side begins"); + + while (i++ < MAXLOOPS) { + while (sc) { + if (readDone) { + return; + } + Thread.sleep(delay); + } + + outData.rewind(); + + while (true) { + r = engine.wrap(outData, getWriteBuf()); + log("write wrap", r); + + if (DEBUG && r.getStatus() != SSLEngineResult.Status.OK) { + dumpBuffers("outData", outData); + dumpBuffers("writeBuf", getWriteBuf()); + } + + if (r.getStatus() == SSLEngineResult.Status.OK && + r.getHandshakeStatus() == + SSLEngineResult.HandshakeStatus.NEED_WRAP) { + continue; + } + break; + } + + doTask(r, engine); + + getWriteBuf().flip(); + sc = true; + + while (sc) { + if (readDone) { + return; + } + Thread.sleep(delay); + } + + long rlimit = Long.MAX_VALUE; + if (readSideInputRecord != null) { + rlimit = getReadLimit(readSideInputRecord); + } + if (rlimit <= 0) { + countdown--; + } + System.err.println("Write side readLimit = " + rlimit); + + if (countdown == COUNTDOWNLIMIT || countdown <= 0) { + inData.clear(); + r = engine.unwrap(getReadBuf(), inData); + log("write unwrap", r); + + if (DEBUG && r.getStatus() != SSLEngineResult.Status.OK) { + dumpBuffers("inData", inData); + dumpBuffers("readBuf", getReadBuf()); + } + } else { + print("write side unwrap skipped"); + } + + doTask(r, engine); + getReadBuf().compact(); + dumpBuffers("compacted getReadBuf()", getReadBuf()); + sc = true; + } + } + + void readLoop() throws Exception { + byte b = 0x0B; + ByteBuffer buf = ByteBuffer.allocateDirect(DATALEN); + + SSLEngineResult r = null; + boolean again = true; + boolean firstNotHandshake = false; + + while (engine == null) { + Thread.sleep(delay); + } + + try { + System.out.println("connected"); + print("entering read loop"); + ready = true; + + while (true) { + while (!sc) { + Thread.sleep(delay); + } + + boolean exit = false; + while (!exit) { + buf.put(b); + buf.flip(); + + r = engine.wrap(buf, getWriteBuf()); + log("read wrap", r); + + if (DEBUG) { + dumpBuffers("buf", buf); + dumpBuffers( "writeBuf", getWriteBuf()); + } + + if (again && r.getStatus() == SSLEngineResult.Status.OK && + r.getHandshakeStatus() == + SSLEngineResult.HandshakeStatus.NEED_WRAP) { + buf.compact(); + again = false; + continue; + } + exit = true; + } + + doTask(r, engine); + + buf.clear(); + getWriteBuf().flip(); + sc = false; + + while (!sc) { + Thread.sleep(delay); + } + + while (true) { + inData.clear(); + r = engine.unwrap(getReadBuf(), inData); + log("read unwrap", r); + + if (DEBUG && r.getStatus() != SSLEngineResult.Status.OK) { + dumpBuffers("inData", inData); + dumpBuffers("readBuf", getReadBuf()); + + doTask(r, engine); + } + + if (again && r.getStatus() == SSLEngineResult.Status.OK && + r.getHandshakeStatus() == + SSLEngineResult.HandshakeStatus.NEED_UNWRAP) { + inData.clear(); + print("again"); + again = false; + continue; + } + break; + } + + inData.clear(); + getReadBuf().compact(); + + totalDataLen += r.bytesProduced(); + sc = false; + + if (!firstNotHandshake && + r.getHandshakeStatus() == + SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) { + + try { + readSideInputRecord = getInputRecord(engine); + setReadLimit(readSideInputRecord, newLimit); + } catch (Exception e) { + throw new RuntimeException(e); + } + System.err.println("Resetting readside"); + firstNotHandshake = true; + } + } + } catch (Exception e) { + sc = false; + readDone = true; + + System.out.println(e.getMessage()); + e.printStackTrace(); + System.out.println("Total data read = " + totalDataLen); + } + } + + // Overridden in Server/Client + ByteBuffer getReadBuf() { + return null; + } + ByteBuffer getWriteBuf() { + return null; + } + + SSLContext initContext() throws Exception { + return createServerSSLContext(); + } + + @Override + protected SSLContextTemplate.ContextParameters getServerContextParameters() { + return new SSLContextTemplate.ContextParameters("TLSv1.3", "PKIX", "NewSunX509"); + } + + static Object getInputRecord(SSLEngine eng) throws Exception { + Class engineImplCls = Class.forName("sun.security.ssl.SSLEngineImpl"); + Object conContext = getPrivate(eng, engineImplCls, "conContext"); + + Class transportCtxCls = Class.forName("sun.security.ssl.TransportContext"); + return getPrivate(conContext, transportCtxCls, "inputRecord"); + } + + static void setReadLimit(Object inputRecord, long newCountdown) throws Exception { + Class inputRecordCls = Class.forName("sun.security.ssl.InputRecord"); + Object readCipher = getPrivate(inputRecord, inputRecordCls, "readCipher"); + Class sslReadCipher = readCipher.getClass().getSuperclass(); + + Field f = getField(sslReadCipher, "keyLimitCountdown"); + f.setLong(readCipher, newCountdown); + } + + static long getReadLimit(Object inputRecord) throws Exception { + Class inputRecordCls = Class.forName("sun.security.ssl.InputRecord"); + Object readCipher = getPrivate(inputRecord, inputRecordCls, "readCipher"); + Class sslReadCipher = readCipher.getClass().getSuperclass(); + + Field f = getField(sslReadCipher, "keyLimitCountdown"); + return f.getLong(readCipher); + } + + private static Field getField(Class type, String name) throws Exception { + Field f = type.getDeclaredField(name); + f.setAccessible(true); // requires --add-opens for sun.security.ssl + return f; + } + + private static Object getPrivate(Object target, Class owner, String name) throws Exception { + return getField(owner, name).get(target); + } + + static class Server extends KeyUpdateOnce implements Runnable { + Server(String cipherSuite) throws Exception { + super(); + engine = initContext().createSSLEngine(); + engine.setUseClientMode(false); + engine.setNeedClientAuth(true); + + if (cipherSuite != null && !cipherSuite.isEmpty()) { + engine.setEnabledCipherSuites(new String[] { cipherSuite }); + } + } + + @Override + public void run() { + try { + if (serverWrites) { + writeLoop(); + } else { + readLoop(); + } + } catch (Exception e) { + System.out.println("server: " + e.getMessage()); + e.printStackTrace(); + } + System.out.println("Server closed"); + } + + @Override + ByteBuffer getWriteBuf() { + return sToc; + } + + @Override + ByteBuffer getReadBuf() { + return cTos; + } + } + + static class Client extends KeyUpdateOnce implements Runnable { + Client() throws Exception { + super(); + engine = initContext().createSSLEngine(); + engine.setUseClientMode(true); + } + + @Override + public void run() { + try { + if (!serverWrites) { + writeLoop(); + } else { + readLoop(); + } + } catch (Exception e) { + System.out.println("client: " + e.getMessage()); + e.printStackTrace(); + } + System.out.println("Client closed"); + } + + @Override + ByteBuffer getWriteBuf() { + return cTos; + } + + @Override + ByteBuffer getReadBuf() { + return sToc; + } + } +} From b071e2e60e2a915a23557f9c1173ee63a2e73193 Mon Sep 17 00:00:00 2001 From: Goetz Lindenmaier Date: Tue, 17 Nov 2026 18:38:13 +0000 Subject: [PATCH 5/7] 8299337: The java.awt.image.ColorModel#pData field is unused Backport-of: 7310131409d3be351e780696173e7354ab51e467 --- .../macosx/native/libawt_lwawt/awt/ImageSurfaceData.m | 2 -- .../share/classes/java/awt/image/ColorModel.java | 1 - .../share/native/libawt/awt/image/imageInitIDs.c | 3 +-- .../share/native/libawt/awt/image/imageInitIDs.h | 3 +-- src/java.desktop/unix/native/common/awt/X11Color.c | 7 +------ 5 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/java.desktop/macosx/native/libawt_lwawt/awt/ImageSurfaceData.m b/src/java.desktop/macosx/native/libawt_lwawt/awt/ImageSurfaceData.m index 47dcd3d5b2c..55174156554 100644 --- a/src/java.desktop/macosx/native/libawt_lwawt/awt/ImageSurfaceData.m +++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/ImageSurfaceData.m @@ -100,7 +100,6 @@ static jfieldID rgbID; static jfieldID mapSizeID; -static jfieldID CMpDataID; static jfieldID allGrayID; static jclass jc_OSXOffScreenSurfaceData = NULL; @@ -1614,7 +1613,6 @@ JNIEXPORT void JNICALL Java_sun_java2d_OSXOffScreenSurfaceData_initIDs(JNIEnv *e CHECK_NULL(rgbID = (*env)->GetFieldID(env, icm, "rgb", "[I")); CHECK_NULL(allGrayID = (*env)->GetFieldID(env, icm, "allgrayopaque", "Z")); CHECK_NULL(mapSizeID = (*env)->GetFieldID(env, icm, "map_size", "I")); - CHECK_NULL(CMpDataID = (*env)->GetFieldID(env, icm, "pData", "J")); } gColorspaceRGB = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); diff --git a/src/java.desktop/share/classes/java/awt/image/ColorModel.java b/src/java.desktop/share/classes/java/awt/image/ColorModel.java index 37c6a5d66d7..912ba99b263 100644 --- a/src/java.desktop/share/classes/java/awt/image/ColorModel.java +++ b/src/java.desktop/share/classes/java/awt/image/ColorModel.java @@ -154,7 +154,6 @@ * @see DataBuffer */ public abstract class ColorModel implements Transparency{ - private long pData; // Placeholder for data for native functions /** * The total number of bits in the pixel. diff --git a/src/java.desktop/share/native/libawt/awt/image/imageInitIDs.c b/src/java.desktop/share/native/libawt/awt/image/imageInitIDs.c index dbb2c1127bd..81bac6a414f 100644 --- a/src/java.desktop/share/native/libawt/awt/image/imageInitIDs.c +++ b/src/java.desktop/share/native/libawt/awt/image/imageInitIDs.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -105,7 +105,6 @@ Java_java_awt_image_SinglePixelPackedSampleModel_initIDs(JNIEnv *env, jclass cls JNIEXPORT void JNICALL Java_java_awt_image_ColorModel_initIDs(JNIEnv *env, jclass cls) { - CHECK_NULL(g_CMpDataID = (*env)->GetFieldID (env, cls, "pData", "J")); CHECK_NULL(g_CMnBitsID = (*env)->GetFieldID(env, cls, "nBits", "[I")); CHECK_NULL(g_CMcspaceID = (*env)->GetFieldID(env, cls, "colorSpace", "Ljava/awt/color/ColorSpace;")); diff --git a/src/java.desktop/share/native/libawt/awt/image/imageInitIDs.h b/src/java.desktop/share/native/libawt/awt/image/imageInitIDs.h index ff0ad42d3b4..031d133edae 100644 --- a/src/java.desktop/share/native/libawt/awt/image/imageInitIDs.h +++ b/src/java.desktop/share/native/libawt/awt/image/imageInitIDs.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -75,7 +75,6 @@ IMGEXTERN jfieldID g_ICRtypeID; /* Color Model ids */ JNIEXPORT -IMGEXTERN jfieldID g_CMpDataID; IMGEXTERN jfieldID g_CMnBitsID; IMGEXTERN jfieldID g_CMcspaceID; IMGEXTERN jfieldID g_CMnumComponentsID; diff --git a/src/java.desktop/unix/native/common/awt/X11Color.c b/src/java.desktop/unix/native/common/awt/X11Color.c index 45379e07067..b754f77537c 100644 --- a/src/java.desktop/unix/native/common/awt/X11Color.c +++ b/src/java.desktop/unix/native/common/awt/X11Color.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1995, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -1224,11 +1224,6 @@ jobject awtJNI_GetColorModel(JNIEnv *env, AwtGraphicsConfigDataPtr aData) (*env)->PopLocalFrame(env, 0); return NULL; } - - /* Set pData field of ColorModel to point to ColorData */ - JNU_SetLongFieldFromPtr(env, awt_colormodel, g_CMpDataID, - aData->color_data); - } return (*env)->PopLocalFrame(env, awt_colormodel); From f8f0cdcf25fc9177b7ccd54e48b951f697738233 Mon Sep 17 00:00:00 2001 From: Goetz Lindenmaier Date: Tue, 17 Nov 2026 18:42:43 +0000 Subject: [PATCH 6/7] 8392993: Null pointer dereferences in screencast code Backport-of: 9a57f0efa7195ad8f0139650e60ceab05bb0b338 --- .../native/libawt_xawt/awt/gtk3_interface.c | 8 +- .../native/libawt_xawt/awt/gtk3_interface.h | 8 +- .../native/libawt_xawt/awt/gtk_interface.h | 7 +- .../libawt_xawt/awt/screencast_pipewire.c | 8 +- .../libawt_xawt/awt/screencast_portal.c | 137 +++++++++++++----- 5 files changed, 123 insertions(+), 45 deletions(-) diff --git a/src/java.desktop/unix/native/libawt_xawt/awt/gtk3_interface.c b/src/java.desktop/unix/native/libawt_xawt/awt/gtk3_interface.c index 04501ed1bf3..ec87fa62271 100644 --- a/src/java.desktop/unix/native/libawt_xawt/awt/gtk3_interface.c +++ b/src/java.desktop/unix/native/libawt_xawt/awt/gtk3_interface.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -581,6 +581,8 @@ GtkApi* gtk3_load(JNIEnv *env, const char* lib_name) fp_g_variant_get = dl_symbol("g_variant_get"); fp_g_variant_get_string = dl_symbol("g_variant_get_string"); fp_g_variant_get_uint32 = dl_symbol("g_variant_get_uint32"); + fp_g_variant_get_type_string = dl_symbol("g_variant_get_type_string"); + fp_g_variant_is_of_type = dl_symbol("g_variant_is_of_type"); fp_g_variant_iter_loop = dl_symbol("g_variant_iter_loop"); fp_g_variant_unref = dl_symbol("g_variant_unref"); fp_g_variant_lookup = dl_symbol("g_variant_lookup"); @@ -606,6 +608,7 @@ GtkApi* gtk3_load(JNIEnv *env, const char* lib_name) fp_g_error_free = dl_symbol("g_error_free"); fp_g_unix_fd_list_get = dl_symbol("g_unix_fd_list_get"); + fp_g_unix_fd_list_get_length = dl_symbol("g_unix_fd_list_get_length"); } /* Now we have only one kind of exceptions: NO_SYMBOL_EXCEPTION * Otherwise we can check the return value of setjmp method. @@ -3064,6 +3067,8 @@ static void gtk3_init(GtkApi* gtk) { gtk->g_variant_get = fp_g_variant_get; gtk->g_variant_get_string = fp_g_variant_get_string; gtk->g_variant_get_uint32 = fp_g_variant_get_uint32; + gtk->g_variant_get_type_string = fp_g_variant_get_type_string; + gtk->g_variant_is_of_type = fp_g_variant_is_of_type; gtk->g_variant_lookup = fp_g_variant_lookup; @@ -3092,6 +3097,7 @@ static void gtk3_init(GtkApi* gtk) { gtk->g_main_context_is_owner = fp_g_main_context_is_owner; gtk->g_error_free = fp_g_error_free; gtk->g_unix_fd_list_get = fp_g_unix_fd_list_get; + gtk->g_unix_fd_list_get_length = fp_g_unix_fd_list_get_length; gtk->gdk_pixbuf_new = fp_gdk_pixbuf_new; gtk->gdk_pixbuf_new_from_data = fp_gdk_pixbuf_new_from_data; diff --git a/src/java.desktop/unix/native/libawt_xawt/awt/gtk3_interface.h b/src/java.desktop/unix/native/libawt_xawt/awt/gtk3_interface.h index d3a83677dd6..d58d106a312 100644 --- a/src/java.desktop/unix/native/libawt_xawt/awt/gtk3_interface.h +++ b/src/java.desktop/unix/native/libawt_xawt/awt/gtk3_interface.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -750,6 +750,10 @@ static const gchar *(*fp_g_variant_get_string)(GVariant *value, static guint32 (*fp_g_variant_get_uint32)(GVariant *value); +static const gchar *(*fp_g_variant_get_type_string)(GVariant *value); + +static gboolean (*fp_g_variant_is_of_type)(GVariant *value, const GVariantType *type); + static gboolean (*fp_g_variant_lookup)(GVariant *dictionary, const gchar *key, const gchar *format_string, @@ -811,4 +815,6 @@ static gint (*fp_g_unix_fd_list_get)(GUnixFDList *list, gint index_, GError **error); +static gint (*fp_g_unix_fd_list_get_length)(GUnixFDList* list); + #endif /* !_GTK3_INTERFACE_H */ diff --git a/src/java.desktop/unix/native/libawt_xawt/awt/gtk_interface.h b/src/java.desktop/unix/native/libawt_xawt/awt/gtk_interface.h index 232ef9973c6..468f86c15d6 100644 --- a/src/java.desktop/unix/native/libawt_xawt/awt/gtk_interface.h +++ b/src/java.desktop/unix/native/libawt_xawt/awt/gtk_interface.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -685,6 +685,8 @@ typedef struct GtkApi { ...); const gchar *(*g_variant_get_string)(GVariant *value, gsize *length); guint32 (*g_variant_get_uint32)(GVariant *value); + const gchar *(*g_variant_get_type_string)(GVariant *value); + gboolean (*g_variant_is_of_type)(GVariant *value, const GVariantType *type); gboolean (*g_variant_lookup)(GVariant *dictionary, const gchar *key, @@ -804,6 +806,9 @@ typedef struct GtkApi { gint index_, GError **error); + gint (*g_unix_fd_list_get_length)(GUnixFDList* list); + + GdkPixbuf *(*gdk_pixbuf_new)(GdkColorspace colorspace, gboolean has_alpha, int bits_per_sample, diff --git a/src/java.desktop/unix/native/libawt_xawt/awt/screencast_pipewire.c b/src/java.desktop/unix/native/libawt_xawt/awt/screencast_pipewire.c index 62d12870ed2..aad10e505ed 100644 --- a/src/java.desktop/unix/native/libawt_xawt/awt/screencast_pipewire.c +++ b/src/java.desktop/unix/native/libawt_xawt/awt/screencast_pipewire.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -261,9 +261,11 @@ static void onStreamProcess(void *userdata) { spaBuffer = pwBuffer->buffer; if (!spaBuffer || spaBuffer->n_datas < 1 - || spaBuffer->datas[0].data == NULL) { + || spaBuffer->datas[0].data == NULL + || spaBuffer->datas[0].chunk == NULL) { DEBUG_SCREEN_PREFIX(screen, "!!! no data, n_datas %d\n", - spaBuffer->n_datas); + spaBuffer ? (int) spaBuffer->n_datas : -1); + fp_pw_stream_queue_buffer(data->stream, pwBuffer); return; } diff --git a/src/java.desktop/unix/native/libawt_xawt/awt/screencast_portal.c b/src/java.desktop/unix/native/libawt_xawt/awt/screencast_portal.c index fad2cab1696..c513101a49f 100644 --- a/src/java.desktop/unix/native/libawt_xawt/awt/screencast_portal.c +++ b/src/java.desktop/unix/native/libawt_xawt/awt/screencast_portal.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -41,6 +41,27 @@ extern struct ScreenSpace screenSpace; struct XdgDesktopPortalApi *portal = NULL; extern int DEBUG_SCREENCAST_ENABLED; +static gboolean checkVariantType(const char *functionName, + int lineNum, + GVariant *value, + const char *expectedType, + const char *caption) { + if (value != NULL && gtk->g_variant_is_of_type( + value, (const GVariantType *) expectedType)) { + return TRUE; + } + + debug_screencast("%s:%i %s: invalid type %s, expected %s\n", + functionName, lineNum, caption, + value != NULL ? gtk->g_variant_get_type_string(value) : "NULL", + expectedType); + return FALSE; +} + +#define GET_VARIANT_CHECKED(VALUE, TYPE, FORMAT, CAPTION, ...) \ + (checkVariantType(__func__, __LINE__, (VALUE), (TYPE), (CAPTION)) && \ + (gtk->g_variant_get((VALUE), (FORMAT), __VA_ARGS__), TRUE)) + GDBusProxy *getProxy() { return isRemoteDesktop ? portal->remoteDesktopProxy : portal->screenCastProxy; } @@ -116,14 +137,20 @@ gboolean rebuildScreenData(GVariantIter *iterStreams, gboolean isTheOnlyMon) { DEBUG_SCREENCAST("\n==== screenId#%i\n", nodeID); if (screenIndex >= screenSpace.allocated) { - screenSpace.screens = realloc( + const int newAllocated = screenSpace.allocated + 1; + struct ScreenProps *newScreens = realloc( screenSpace.screens, - ++screenSpace.allocated * sizeof(struct ScreenProps) + newAllocated * sizeof(struct ScreenProps) ); - if (!screenSpace.screens) { + + if (!newScreens) { ERR("failed to allocate memory\n"); + gtk->g_variant_unref(prop); return FALSE; } + + screenSpace.screens = newScreens; + screenSpace.allocated = newAllocated; } struct ScreenProps * screen = &screenSpace.screens[screenIndex]; @@ -162,7 +189,6 @@ gboolean rebuildScreenData(GVariantIter *iterStreams, gboolean isTheOnlyMon) { DEBUG_SCREEN(screen); DEBUG_SCREENCAST("#---------------------#\n\n", NULL); - gtk->g_variant_unref(prop); screenIndex++; }; @@ -186,7 +212,7 @@ gboolean checkVersion() { : PORTAL_IFACE_SCREENCAST; if (version == 0) { - GError *error = NULL; + GError *err = NULL; GVariant *retVersion = gtk->g_dbus_proxy_call_sync( getProxy(), @@ -195,7 +221,7 @@ gboolean checkVersion() { interface, "version"), G_DBUS_CALL_FLAGS_NONE, - -1, NULL, NULL + -1, NULL, &err ); if (isRemoteDesktop) { @@ -206,13 +232,16 @@ gboolean checkVersion() { if (!retVersion) { //no backend on system DEBUG_SCREENCAST("!!! could not detect the %s version\n", interface); + ERR_HANDLE(err); return FALSE; } - ERR_HANDLE(error); - GVariant *varVersion = NULL; - gtk->g_variant_get(retVersion, "(v)", &varVersion); + if (!GET_VARIANT_CHECKED(retVersion, "(v)", "(v)", + "Version response", &varVersion)) { + gtk->g_variant_unref(retVersion); + return FALSE; + } if (!varVersion){ gtk->g_variant_unref(retVersion); @@ -220,11 +249,14 @@ gboolean checkVersion() { return FALSE; } - version = gtk->g_variant_get_uint32(varVersion); + if (!GET_VARIANT_CHECKED(varVersion, "u", "u", "Version", &version)) { + gtk->g_variant_unref(varVersion); + gtk->g_variant_unref(retVersion); + return FALSE; + } gtk->g_variant_unref(varVersion); gtk->g_variant_unref(retVersion); - } gboolean isVersionOk = isRemoteDesktop @@ -417,12 +449,12 @@ static void callbackScreenCastCreateSession( uint32_t status; GVariant *result = NULL; - gtk->g_variant_get( - parameters, - "(u@a{sv})", - &status, - &result - ); + if (!GET_VARIANT_CHECKED(parameters, "(ua{sv})", "(u@a{sv})", + "CreateSession response", &status, &result)) { + helper->isDone = TRUE; + callbackEnd(); + return; + } if (status != 0) { DEBUG_SCREENCAST("Failed to create ScreenCast: %u\n", status); @@ -431,6 +463,7 @@ static void callbackScreenCastCreateSession( DEBUG_SCREENCAST("session_handle returned %b %p\n", returned, helper->data) } + gtk->g_variant_unref(result); helper->isDone = TRUE; callbackEnd(); } @@ -541,7 +574,12 @@ static void callbackScreenCastSelectSources( uint32_t status; GVariant* result = NULL; - gtk->g_variant_get(parameters, "(u@a{sv})", &status, &result); + if (!GET_VARIANT_CHECKED(parameters, "(ua{sv})", "(u@a{sv})", + "SelectSources response", &status, &result)) { + helper->isDone = TRUE; + callbackEnd(); + return; + } if (status != 0) { DEBUG_SCREENCAST("Failed select sources: %u\n", status); @@ -574,7 +612,12 @@ static void callbackRemoteDesktopSelectDevices( uint32_t status; GVariant* result = NULL; - gtk->g_variant_get(parameters, "(u@a{sv})", &status, &result); + if (!GET_VARIANT_CHECKED(parameters, "(ua{sv})", "(u@a{sv})", + "SelectDevices response", &status, &result)) { + helper->isDone = TRUE; + callbackEnd(); + return; + } if (status != 0) { DEBUG_SCREENCAST("Failed select devices: %u\n", status); @@ -709,12 +752,19 @@ static void callbackScreenCastStart( GVariant* result = NULL; const gchar *oldToken = startHelper->token; - gtk->g_variant_get(parameters, "(u@a{sv})", &status, &result); + if (!GET_VARIANT_CHECKED(parameters, "(ua{sv})", "(u@a{sv})", + "Start response", &status, &result)) { + startHelper->result = RESULT_ERROR; + helper->isDone = TRUE; + callbackEnd(); + return; + } if (status != 0) { // Cancel pressed on the system dialog DEBUG_SCREENCAST("Failed to start screencast: %u\n", status); startHelper->result = RESULT_DENIED; + gtk->g_variant_unref(result); helper->isDone = TRUE; return; } @@ -722,14 +772,15 @@ static void callbackScreenCastStart( GVariant *streams = gtk->g_variant_lookup_value( result, "streams", - G_VARIANT_TYPE_ARRAY + (const GVariantType *) "a(ua{sv})" ); print_gvariant_content("Streams", streams); if (!streams) { - DEBUG_SCREENCAST("No streams available with current token\n", NULL); + DEBUG_SCREENCAST("No streams available with current token\n", NULL); startHelper->result = RESULT_NO_STREAMS; + gtk->g_variant_unref(result); helper->isDone = TRUE; return; } @@ -772,6 +823,7 @@ static void callbackScreenCastStart( helper->isDone = TRUE; gtk->g_variant_unref(streams); + gtk->g_variant_unref(result); callbackEnd(); } @@ -867,27 +919,36 @@ int portalScreenCastOpenPipewireRemote() { &err ); - if (err || !response) { - DEBUG_SCREENCAST("Failed to call OpenPipeWireRemote on session: %s\n", - err->message); + if (err || !response || !fdList) { + DEBUG_SCREENCAST("Failed to call OpenPipeWireRemote on session: %s, fdList %p\n", + err ? err->message : (!response ? "no response" : "no fd list"), + (void *)fdList); + if (response) { + gtk->g_variant_unref(response); + } + if (fdList) { + gtk->g_object_unref(fdList); + } ERR_HANDLE(err); return RESULT_ERROR; } gint32 index; - gtk->g_variant_get( - response, - "(h)", - &index, - &err - ); + if (!GET_VARIANT_CHECKED(response, "(h)", "(h)", + "OpenPipeWireRemote response", &index)) { + gtk->g_variant_unref(response); + gtk->g_object_unref(fdList); + return RESULT_ERROR; + } gtk->g_variant_unref(response); - if (err) { - DEBUG_SCREENCAST("Failed to get pipewire fd index: %s\n", - err->message); - ERR_HANDLE(err); + gint length = gtk->g_unix_fd_list_get_length(fdList); + + if (index < 0 || index >= length) { + DEBUG_SCREENCAST("Invalid pipewire fd index: %d (fdList length: %d)\n", + index, length); + gtk->g_object_unref(fdList); return RESULT_ERROR; } @@ -897,9 +958,7 @@ int portalScreenCastOpenPipewireRemote() { &err ); - if (fdList) { - gtk->g_object_unref(fdList); - } + gtk->g_object_unref(fdList); if (err) { DEBUG_SCREENCAST("Failed to get pipewire fd: %s\n", err->message); From e047bc2ed6c2256fd72824d2b83b0eaab94f3029 Mon Sep 17 00:00:00 2001 From: Goetz Lindenmaier Date: Wed, 18 Nov 2026 15:49:46 +0000 Subject: [PATCH 7/7] 8393416: [Ubuntu 26.10] screen capture test does not work with wayland Ubuntu 26.10 and hangs indefinitely Reviewed-by: mbaesken Backport-of: 31ccbe42fb0dcfbfbaf5d8248f7d17bdd2a90324 --- .../classes/sun/awt/X11GraphicsDevice.java | 10 +- .../sun/awt/screencast/ScreencastHelper.java | 20 +-- .../unix/native/libawt_xawt/awt/fp_pipewire.h | 7 +- .../libawt_xawt/awt/screencast_pipewire.c | 140 ++++++++++++------ 4 files changed, 111 insertions(+), 66 deletions(-) diff --git a/src/java.desktop/unix/classes/sun/awt/X11GraphicsDevice.java b/src/java.desktop/unix/classes/sun/awt/X11GraphicsDevice.java index 0a1bc24599f..f4fb2fcb8f4 100644 --- a/src/java.desktop/unix/classes/sun/awt/X11GraphicsDevice.java +++ b/src/java.desktop/unix/classes/sun/awt/X11GraphicsDevice.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -161,6 +161,14 @@ public Rectangle getBounds() { return bounds.getBounds(); } + /** + * Returns the screen bounds in native pixels, + * ignoring UI scaling and Xwayland display mode emulation. + */ + public Rectangle getNativeBounds() { + return pGetBounds(getScreen()); + } + public Insets getInsets() { return insets; } diff --git a/src/java.desktop/unix/classes/sun/awt/screencast/ScreencastHelper.java b/src/java.desktop/unix/classes/sun/awt/screencast/ScreencastHelper.java index 602788b6611..65cf0123649 100644 --- a/src/java.desktop/unix/classes/sun/awt/screencast/ScreencastHelper.java +++ b/src/java.desktop/unix/classes/sun/awt/screencast/ScreencastHelper.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -26,14 +26,12 @@ package sun.awt.screencast; import sun.awt.UNIXToolkit; -import sun.java2d.pipe.Region; +import sun.awt.X11GraphicsDevice; import sun.security.action.GetPropertyAction; -import java.awt.GraphicsConfiguration; import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import java.awt.Toolkit; -import java.awt.geom.AffineTransform; import java.security.AccessController; import java.util.Arrays; import java.util.List; @@ -124,19 +122,7 @@ private static List getSystemScreensBounds() { .stream(GraphicsEnvironment .getLocalGraphicsEnvironment() .getScreenDevices()) - .map(graphicsDevice -> { - GraphicsConfiguration gc = - graphicsDevice.getDefaultConfiguration(); - Rectangle screen = gc.getBounds(); - AffineTransform tx = gc.getDefaultTransform(); - - return new Rectangle( - Region.clipRound(screen.x * tx.getScaleX()), - Region.clipRound(screen.y * tx.getScaleY()), - Region.clipRound(screen.width * tx.getScaleX()), - Region.clipRound(screen.height * tx.getScaleY()) - ); - }) + .map(d -> ((X11GraphicsDevice) d).getNativeBounds()) .toList(); } diff --git a/src/java.desktop/unix/native/libawt_xawt/awt/fp_pipewire.h b/src/java.desktop/unix/native/libawt_xawt/awt/fp_pipewire.h index d39f7c833b7..3c6443676fb 100644 --- a/src/java.desktop/unix/native/libawt_xawt/awt/fp_pipewire.h +++ b/src/java.desktop/unix/native/libawt_xawt/awt/fp_pipewire.h @@ -70,14 +70,17 @@ int (*fp_pw_core_disconnect)(struct pw_core *core); struct pw_context * (*fp_pw_context_new)(struct pw_loop *main_loop, struct pw_properties *props, size_t user_data_size); +void (*fp_pw_context_destroy)(struct pw_context *context); struct pw_thread_loop * (*fp_pw_thread_loop_new)(const char *name, const struct spa_dict *props); struct pw_loop * (*fp_pw_thread_loop_get_loop)(struct pw_thread_loop *loop); void (*fp_pw_thread_loop_signal)(struct pw_thread_loop *loop, bool wait_for_accept); -void (*fp_pw_thread_loop_wait)(struct pw_thread_loop *loop); -void (*fp_pw_thread_loop_accept)(struct pw_thread_loop *loop); +int (*fp_pw_thread_loop_timed_wait_full)(struct pw_thread_loop *loop, + const struct timespec *abstime); +int (*fp_pw_thread_loop_get_time)(struct pw_thread_loop *loop, + struct timespec *abstime, int64_t timeout); int (*fp_pw_thread_loop_start)(struct pw_thread_loop *loop); void (*fp_pw_thread_loop_stop)(struct pw_thread_loop *loop); void (*fp_pw_thread_loop_destroy)(struct pw_thread_loop *loop); diff --git a/src/java.desktop/unix/native/libawt_xawt/awt/screencast_pipewire.c b/src/java.desktop/unix/native/libawt_xawt/awt/screencast_pipewire.c index aad10e505ed..05badbcf631 100644 --- a/src/java.desktop/unix/native/libawt_xawt/awt/screencast_pipewire.c +++ b/src/java.desktop/unix/native/libawt_xawt/awt/screencast_pipewire.c @@ -28,6 +28,7 @@ #endif #include +#include #include "jni_util.h" #include "awt.h" @@ -48,6 +49,9 @@ int DEBUG_SCREENCAST_ENABLED = FALSE; (*env)->ExceptionDescribe(env); \ } +// 10 s timeout for format negotiation and frames from all requested screens. +static const int64_t PIPEWIRE_TIMEOUT_NS = 10LL * 1000 * 1000 * 1000; + static gboolean hasPipewireFailed = FALSE; static gboolean sessionClosed = TRUE; static GString *activeSessionToken; @@ -103,6 +107,10 @@ static void doCleanup() { for (int i = 0; i < screenSpace.screenCount; ++i) { struct ScreenProps *screenProps = &screenSpace.screens[i]; + if (screenProps->captureDataPixbuf) { + gtk->g_object_unref(screenProps->captureDataPixbuf); + screenProps->captureDataPixbuf = NULL; + } if (screenProps->data) { if (screenProps->data->stream) { fp_pw_thread_loop_lock(pw.loop); @@ -128,6 +136,11 @@ static void doCleanup() { pw.core = NULL; } + if (pw.context) { + fp_pw_context_destroy(pw.context); + pw.context = NULL; + } + if (pw.loop) { fp_pw_thread_loop_destroy(pw.loop); pw.loop = NULL; @@ -226,7 +239,7 @@ static void onStreamParamChanged( data->rawFormat.size.height); data->hasFormat = TRUE; - fp_pw_thread_loop_signal(pw.loop, TRUE); + fp_pw_thread_loop_signal(pw.loop, FALSE); } static void onStreamProcess(void *userdata) { @@ -244,7 +257,6 @@ static void onStreamProcess(void *userdata) { if ( !data->hasFormat || !screen->shouldCapture - || screen->captureDataReady ) { return; } @@ -258,6 +270,11 @@ static void onStreamProcess(void *userdata) { return; } + if (screen->captureDataReady) { + fp_pw_stream_queue_buffer(data->stream, pwBuffer); + return; + } + spaBuffer = pwBuffer->buffer; if (!spaBuffer || spaBuffer->n_datas < 1 @@ -289,7 +306,6 @@ static void onStreamProcess(void *userdata) { ); GdkRectangle captureArea = screen->captureArea; - GdkRectangle screenBounds = screen->bounds; GdkPixbuf *pixbuf = gtk->gdk_pixbuf_new_from_data(spaData.data, GDK_COLORSPACE_RGB, @@ -300,9 +316,14 @@ static void onStreamProcess(void *userdata) { spaData.chunk->stride, NULL, NULL); + if (!pixbuf) { + ERR("Cannot create a pixbuf from stream data.\n"); + goto fail; + } - if (screen->bounds.width != streamWidth - || screen->bounds.height != streamHeight) { + gboolean needsScaling = screen->bounds.width != streamWidth + || screen->bounds.height != streamHeight; + if (needsScaling) { DEBUG_SCREEN_PREFIX(screen, "scaling stream data %dx%d -> %dx%d\n", streamWidth, streamHeight, @@ -314,37 +335,39 @@ static void onStreamProcess(void *userdata) { screen->bounds.height, GDK_INTERP_BILINEAR); + if (!scaled) { + ERR("Cannot scale the stream pixbuf.\n"); + goto fail; + } gtk->g_object_unref(pixbuf); pixbuf = scaled; } - GdkPixbuf *cropped = NULL; - if (captureArea.width != screenBounds.width - || captureArea.height != screenBounds.height) { - - cropped = gtk->gdk_pixbuf_new(GDK_COLORSPACE_RGB, + if (needsScaling + && captureArea.x == 0 && captureArea.y == 0 + && captureArea.width == screen->bounds.width + && captureArea.height == screen->bounds.height) { + screen->captureDataPixbuf = pixbuf; + } else { + GdkPixbuf *cropped = gtk->gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, captureArea.width, captureArea.height); - if (cropped) { - gtk->gdk_pixbuf_copy_area(pixbuf, + if (!cropped) { + ERR("Cannot create a new pixbuf.\n"); + goto fail; + } + gtk->gdk_pixbuf_copy_area(pixbuf, captureArea.x, captureArea.y, captureArea.width, captureArea.height, cropped, 0, 0); - } else { - ERR("Cannot create a new pixbuf.\n"); - } gtk->g_object_unref(pixbuf); - pixbuf = NULL; - - data->screenProps->captureDataPixbuf = cropped; - } else { - data->screenProps->captureDataPixbuf = pixbuf; + screen->captureDataPixbuf = cropped; } screen->captureDataReady = TRUE; @@ -353,6 +376,15 @@ static void onStreamProcess(void *userdata) { fp_pw_stream_queue_buffer(data->stream, pwBuffer); fp_pw_thread_loop_signal(pw.loop, FALSE); + return; + + fail: + if (pixbuf) { + gtk->g_object_unref(pixbuf); + } + fp_pw_stream_queue_buffer(data->stream, pwBuffer); + hasPipewireFailed = TRUE; + fp_pw_thread_loop_signal(pw.loop, FALSE); } static void onStreamStateChanged( @@ -430,10 +462,13 @@ static bool startStream( } /** + * The caller must hold the PipeWire thread loop lock. + * * @param index of a screen + * @param captureDeadline shared deadline for format negotiation and screen capture * @return TRUE on success */ -static gboolean connectStream(int index) { +static gboolean connectStream(int index, const struct timespec *captureDeadline) { DEBUG_SCREENCAST("@@@ using screen %i\n", index); if (index >= screenSpace.screenCount) { DEBUG_SCREENCAST("!!! Wrong index for screen\n", NULL); @@ -445,9 +480,7 @@ static gboolean connectStream(int index) { data->screenProps = &screenSpace.screens[index]; if (!sessionClosed && data->stream) { - fp_pw_thread_loop_lock(pw.loop); int result = fp_pw_stream_set_active(data->stream, TRUE); - fp_pw_thread_loop_unlock(pw.loop); DEBUG_SCREEN_PREFIX(data->screenProps, "stream %p: activate result |%i|\n", @@ -472,7 +505,6 @@ static gboolean connectStream(int index) { if (!data->stream) { DEBUG_SCREEN_PREFIX(data->screenProps, "!!! Could not create a pipewire stream\n", NULL); - fp_pw_thread_loop_unlock(pw.loop); return FALSE; } @@ -488,18 +520,20 @@ static gboolean connectStream(int index) { if (!startStream(data->stream, screenSpace.screens[index].id)){ DEBUG_SCREEN_PREFIX(data->screenProps, "!!! Could not start a pipewire stream\n", NULL); - fp_pw_thread_loop_unlock(pw.loop); return FALSE; } - while (!data->hasFormat) { - fp_pw_thread_loop_wait(pw.loop); - fp_pw_thread_loop_accept(pw.loop); - if (hasPipewireFailed) { - fp_pw_thread_loop_unlock(pw.loop); + while (!hasPipewireFailed && !data->hasFormat) { + int result = fp_pw_thread_loop_timed_wait_full(pw.loop, captureDeadline); + if (result < 0) { + DEBUG_SCREEN_PREFIX(data->screenProps, + "Format wait failed: %d\n", result); return FALSE; } } + if (hasPipewireFailed) { + return FALSE; + } DEBUG_SCREEN_PREFIX(data->screenProps, "frame size: %dx%d\n", @@ -581,7 +615,7 @@ static const struct pw_core_events coreEvents = { * @param requestedArea requested screenshot area * @return TRUE on success */ -static gboolean doLoop(GdkRectangle requestedArea) { +static gboolean doLoop(GdkRectangle requestedArea, struct timespec *captureDeadline) { gboolean isLoopLockTaken = FALSE; if (!pw.loop && !sessionClosed) { pw.loop = fp_pw_thread_loop_new("AWT Pipewire Thread", NULL); @@ -633,6 +667,13 @@ static gboolean doLoop(GdkRectangle requestedArea) { } pw_core_add_listener(pw.core, &pw.coreListener, &coreEvents, NULL); + } else if (pw.loop) { + fp_pw_thread_loop_lock(pw.loop); + isLoopLockTaken = TRUE; + } + + if (fp_pw_thread_loop_get_time(pw.loop, captureDeadline, PIPEWIRE_TIMEOUT_NS) < 0) { + goto fail; } for (int i = 0; i < screenSpace.screenCount; ++i) { @@ -652,7 +693,7 @@ static gboolean doLoop(GdkRectangle requestedArea) { DEBUG_SCREEN_PREFIX(screen, "@@@ adding screen %i\n", i); if (checkScreen(i, requestedArea)) { - if (!connectStream(i)){ + if (!connectStream(i, captureDeadline)){ goto fail; } } @@ -727,11 +768,12 @@ static gboolean loadSymbols() { LOAD_SYMBOL(fp_pw_context_connect_fd, "pw_context_connect_fd"); LOAD_SYMBOL(fp_pw_core_disconnect, "pw_core_disconnect"); LOAD_SYMBOL(fp_pw_context_new, "pw_context_new"); + LOAD_SYMBOL(fp_pw_context_destroy, "pw_context_destroy"); LOAD_SYMBOL(fp_pw_thread_loop_new, "pw_thread_loop_new"); LOAD_SYMBOL(fp_pw_thread_loop_get_loop, "pw_thread_loop_get_loop"); LOAD_SYMBOL(fp_pw_thread_loop_signal, "pw_thread_loop_signal"); - LOAD_SYMBOL(fp_pw_thread_loop_wait, "pw_thread_loop_wait"); - LOAD_SYMBOL(fp_pw_thread_loop_accept, "pw_thread_loop_accept"); + LOAD_SYMBOL(fp_pw_thread_loop_timed_wait_full, "pw_thread_loop_timed_wait_full"); + LOAD_SYMBOL(fp_pw_thread_loop_get_time, "pw_thread_loop_get_time"); LOAD_SYMBOL(fp_pw_thread_loop_start, "pw_thread_loop_start"); LOAD_SYMBOL(fp_pw_thread_loop_stop, "pw_thread_loop_stop"); LOAD_SYMBOL(fp_pw_thread_loop_destroy, "pw_thread_loop_destroy"); @@ -898,19 +940,27 @@ static int makeScreencast( return pw.pwFd; } - if (!doLoop(*requestedArea)) { + struct timespec captureDeadline; + if (!doLoop(*requestedArea, &captureDeadline)) { return RESULT_ERROR; } - while (!isAllDataReady()) { - fp_pw_thread_loop_lock(pw.loop); - fp_pw_thread_loop_wait(pw.loop); - fp_pw_thread_loop_unlock(pw.loop); - if (hasPipewireFailed) { - doCleanup(); - return RESULT_ERROR; + fp_pw_thread_loop_lock(pw.loop); + int waitResult = 0; + while (!hasPipewireFailed && !isAllDataReady()) { + waitResult = fp_pw_thread_loop_timed_wait_full(pw.loop, &captureDeadline); + if (waitResult < 0) { + DEBUG_SCREENCAST("Frame wait failed: %d\n", waitResult); + break; } } + gboolean failed = hasPipewireFailed || waitResult < 0; + fp_pw_thread_loop_unlock(pw.loop); + + if (failed) { + doCleanup(); + return RESULT_ERROR; + } return RESULT_OK; } @@ -1041,17 +1091,15 @@ JNIEXPORT jint JNICALL Java_sun_awt_screencast_ScreencastHelper_getRGBPixelsImpl } } + fp_pw_thread_loop_lock(pw.loop); if (screenProps->captureDataPixbuf) { gtk->g_object_unref(screenProps->captureDataPixbuf); screenProps->captureDataPixbuf = NULL; } screenProps->shouldCapture = FALSE; - - fp_pw_thread_loop_lock(pw.loop); fp_pw_stream_set_active(screenProps->data->stream, FALSE); - fp_pw_thread_loop_unlock(pw.loop); - screenProps->captureDataReady = FALSE; + fp_pw_thread_loop_unlock(pw.loop); } }