in src/main/java/org/jetbrains/nativecerts/win32/Crypt32ExtUtil.java [87:143]
public static List<X509Certificate> gatherEnterpriseCertsForLocation(int location, String store_name) {
int flags = location | Crypt32Ext.CERT_STORE_OPEN_EXISTING_FLAG | Crypt32Ext.CERT_STORE_READONLY_FLAG;
WinCrypt.HCERTSTORE hcertstore =
Crypt32Ext.INSTANCE.CertOpenStore(
new WTypes.LPSTR(new Pointer(Crypt32Ext.CERT_STORE_PROV_SYSTEM_REGISTRY_W)),
0,
new WinCrypt.HCRYPTPROV_LEGACY(0),
flags,
new WTypes.LPWSTR(store_name));
if (hcertstore == null) {
int errorCode = Native.getLastError();
if (errorCode == ERROR_NO_MORE_FILES || errorCode == ERROR_FILE_NOT_FOUND) {
return Collections.emptyList();
} else {
throw new Win32Exception(errorCode);
}
}
try {
List<X509Certificate> result = new ArrayList<>();
WinCrypt.CERT_CONTEXT.ByReference prev = null;
while (true) {
WinCrypt.CERT_CONTEXT.ByReference certificate =
Crypt32.INSTANCE.CertEnumCertificatesInStore(
hcertstore, prev == null ? null : prev.getPointer());
if (certificate == null) {
int errorCode = Native.getLastError();
if (errorCode != CRYPT_E_NOT_FOUND && errorCode != ERROR_NO_MORE_FILES) {
throw new Win32Exception(errorCode);
}
break;
}
byte[] bytes = certificate.pbCertEncoded.getByteArray(0, certificate.cbCertEncoded);
try {
X509Certificate x509 = NativeTrustedRootsInternalUtils.parseCertificate(bytes);
result.add(x509);
} catch (Throwable parsingException) {
LOGGER.warning(renderExceptionMessage(
"Unable to parse one of the certificates" +
"from store '" + store_name + "'",
parsingException));
}
prev = certificate;
}
return result;
} finally {
CertCloseStore(hcertstore);
}
}