Skip to content

Commit db535c8

Browse files
rebarbora-mckvakwangweij
authored andcommitted
8313367: SunMSCAPI cannot read Local Computer certs w/o Windows elevation
Reviewed-by: weijun
1 parent edfe285 commit db535c8

File tree

2 files changed

+11
-28
lines changed

2 files changed

+11
-28
lines changed

src/jdk.crypto.mscapi/windows/native/libsunmscapi/security.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -444,7 +444,7 @@ JNIEXPORT void JNICALL Java_sun_security_mscapi_CKeyStore_loadKeysOrCertificateC
444444
}
445445
else if (jCertStoreLocation == KEYSTORE_LOCATION_LOCALMACHINE) {
446446
hCertStore = ::CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, NULL,
447-
CERT_SYSTEM_STORE_LOCAL_MACHINE, pszCertStoreName);
447+
CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_STORE_MAXIMUM_ALLOWED_FLAG, pszCertStoreName);
448448
}
449449
else {
450450
PP("jCertStoreLocation is not a valid value");
@@ -798,11 +798,15 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_CSignature_signHash
798798
::CryptGetProvParam((HCRYPTPROV)hCryptProv, PP_CONTAINER, //deprecated
799799
(BYTE *)pbData, &cbData, 0);
800800

801+
DWORD keysetType = 0;
802+
DWORD keysetTypeLen = sizeof(keysetType);
803+
::CryptGetProvParam((HCRYPTPROV)hCryptProv, PP_KEYSET_TYPE, //deprecated
804+
(BYTE*)&keysetType, &keysetTypeLen, 0);
805+
801806
// Acquire an alternative CSP handle
802807
if (::CryptAcquireContext(&hCryptProvAlt, LPCSTR(pbData), NULL, //deprecated
803-
PROV_RSA_AES, 0) == FALSE)
808+
PROV_RSA_AES, 0 | keysetType) == FALSE)
804809
{
805-
806810
ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
807811
__leave;
808812
}

test/jdk/sun/security/mscapi/AllTypes.java

+3-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -45,33 +45,12 @@ public static void main(String[] args) throws Exception {
4545
var nr = test("windows-root");
4646
var nmu = test("windows-my-currentuser");
4747
var nru = test("windows-root-currentuser");
48-
var hasAdminPrivileges = detectIfRunningWithAdminPrivileges();
49-
var nmm = adminTest("windows-my-localmachine", hasAdminPrivileges);
50-
var nrm = adminTest("windows-root-localmachine", hasAdminPrivileges);
48+
var nmm = test("windows-my-localmachine");
49+
var nrm = test("windows-root-localmachine");
5150
Asserts.assertEQ(nm, nmu);
5251
Asserts.assertEQ(nr, nru);
5352
}
5453

55-
private static boolean detectIfRunningWithAdminPrivileges() {
56-
try {
57-
Process p = Runtime.getRuntime().exec("reg query \"HKU\\S-1-5-19\"");
58-
p.waitFor();
59-
return (p.exitValue() == 0);
60-
}
61-
catch (Exception ex) {
62-
System.out.println("Warning: unable to detect admin privileges, assuming none");
63-
return false;
64-
}
65-
}
66-
67-
private static List<String> adminTest(String type, boolean hasAdminPrivileges) throws Exception {
68-
if (hasAdminPrivileges) {
69-
return test(type);
70-
}
71-
System.out.println("Ignoring: " + type + " as it requires admin privileges");
72-
return null;
73-
}
74-
7554
private static List<String> test(String type) throws Exception {
7655
var stdType = "Windows-" + type.substring(8).toUpperCase(Locale.ROOT);
7756
SecurityTools.keytool("-storetype " + type + " -list")

0 commit comments

Comments
 (0)