Skip to content

Commit af8458b

Browse files
shalupovmatthiasblaesing
authored andcommitted
Add CertOpenStore to c.s.j.p.win32.Crypt32
1 parent 7ed3fa4 commit af8458b

4 files changed

Lines changed: 210 additions & 7 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Features
1111
* [#1403](https://github.com/java-native-access/jna/pull/1403): Rebuild AIX binaries with libffi 3.4.2 (other architectures were part of 5.10) - [@matthiasblaesing](https://github.com/matthiasblaesing).
1212
* [#1404](https://github.com/java-native-access/jna/issues/1404): Added Solaris Kstat2 library - [@dbwiddis](https://github.com/dbwiddis).
1313
* [#1416](https://github.com/java-native-access/jna/pull/1416): Add `CFDictionaryGetCount` to `c.s.j.p.mac.CoreFoundation` - [@shalupov](https://github.com/shalupov)
14+
* [#1418](https://github.com/java-native-access/jna/pull/1418): Add `CertOpenStore` to `c.s.j.p.win32.Crypt32` - [@shalupov](https://github.com/shalupov)
1415

1516
Bug Fixes
1617
---------

contrib/platform/src/com/sun/jna/platform/win32/Crypt32.java

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
*/
2424
package com.sun.jna.platform.win32;
2525

26-
import com.sun.jna.Native;
27-
import com.sun.jna.Pointer;
26+
import com.sun.jna.*;
2827
import com.sun.jna.platform.win32.WinCrypt.CRYPTPROTECT_PROMPTSTRUCT;
2928
import com.sun.jna.platform.win32.WinCrypt.DATA_BLOB;
3029
import com.sun.jna.ptr.PointerByReference;
@@ -157,11 +156,83 @@ public boolean CryptUnprotectData(DATA_BLOB pDataIn, PointerByReference szDataDe
157156
*/
158157
boolean CertAddEncodedCertificateToSystemStore(String szCertStoreName, Pointer pbCertEncoded, int cbCertEncoded);
159158

159+
/**
160+
* The {@code CertOpenStore} function opens a certificate store by using a specified store provider type
161+
*
162+
* @param lpszStoreProvider
163+
* A pointer to a null-terminated ANSI string that contains the store provider type.
164+
* @param dwEncodingType
165+
* Specifies the <a href="https://docs.microsoft.com/en-us/windows/desktop/SecGloss/c-gly">certificate encoding type</a>
166+
* and <a href="https://docs.microsoft.com/en-us/windows/desktop/SecGloss/m-gly">message encoding</a> type.
167+
* Encoding is used only when the {@code dwSaveAs} parameter of the
168+
* <a href="https://docs.microsoft.com/en-us/windows/desktop/api/wincrypt/nf-wincrypt-certsavestore">CertSaveStore</a>
169+
* function contains {@code CERT_STORE_SAVE_AS_PKCS7}.
170+
* Otherwise, the {@code dwMsgAndCertEncodingType} parameter is not used.
171+
* @param hCryptProv
172+
* This parameter is not used and should be set to NULL.
173+
* @param dwFlags
174+
* These values consist of high-word and low-word values combined by using a bitwise-OR operation.
175+
* See {@code CERT_STORE_*_FLAG} and {@code CERT_SYSTEM_STORE_*} constants.
176+
* @param pvPara
177+
* Additional information for this function. The contents of
178+
* this parameter depends on the value of the {@code lpszStoreProvider} and other parameters.
179+
* @return
180+
* If the function succeeds, the function returns a handle to the certificate store.
181+
* When you have finished using the store, release the handle by calling the
182+
* {@link com.sun.jna.platform.win32.Crypt32#CertCloseStore(WinCrypt.HCERTSTORE, int)} function.
183+
* If the function fails, it returns NULL. For extended error information,
184+
* call {@link Native#getLastError()}.
185+
*
186+
* @see <a href="https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certopenstore">MSDN</a>
187+
*/
188+
WinCrypt.HCERTSTORE CertOpenStore(
189+
CertStoreProviderName lpszStoreProvider,
190+
int dwEncodingType,
191+
WinCrypt.HCRYPTPROV_LEGACY hCryptProv,
192+
int dwFlags,
193+
Pointer pvPara);
194+
195+
/**
196+
* The {@code CertOpenStore} function opens a certificate store by using a specified store provider type
197+
*
198+
* @param lpszStoreProvider
199+
* A pointer to a null-terminated ANSI string that contains the store provider type.
200+
* @param dwEncodingType
201+
* Specifies the <a href="https://docs.microsoft.com/en-us/windows/desktop/SecGloss/c-gly">certificate encoding type</a>
202+
* and <a href="https://docs.microsoft.com/en-us/windows/desktop/SecGloss/m-gly">message encoding</a> type.
203+
* Encoding is used only when the {@code dwSaveAs} parameter of the
204+
* <a href="https://docs.microsoft.com/en-us/windows/desktop/api/wincrypt/nf-wincrypt-certsavestore">CertSaveStore</a>
205+
* function contains {@code CERT_STORE_SAVE_AS_PKCS7}.
206+
* Otherwise, the {@code dwMsgAndCertEncodingType} parameter is not used.
207+
* @param hCryptProv
208+
* This parameter is not used and should be set to NULL.
209+
* @param dwFlags
210+
* These values consist of high-word and low-word values combined by using a bitwise-OR operation.
211+
* See {@code CERT_STORE_*_FLAG} and {@code CERT_SYSTEM_STORE_*} constants.
212+
* @param pvPara
213+
* Additional information for this function in {@link WTypes.LPWSTR} form. The contents of
214+
* this parameter depends on the value of the {@code lpszStoreProvider} and other parameters.
215+
* @return
216+
* If the function succeeds, the function returns a handle to the certificate store.
217+
* When you have finished using the store, release the handle by calling the
218+
* {@link com.sun.jna.platform.win32.Crypt32#CertCloseStore(WinCrypt.HCERTSTORE, int)} function.
219+
* If the function fails, it returns NULL. For extended error information,
220+
* call {@link Native#getLastError()}.
221+
*
222+
* @see <a href="https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certopenstore">MSDN</a>
223+
*/
224+
WinCrypt.HCERTSTORE CertOpenStore(
225+
CertStoreProviderName lpszStoreProvider,
226+
int dwEncodingType,
227+
WinCrypt.HCRYPTPROV_LEGACY hCryptProv,
228+
int dwFlags,
229+
WTypes.LPWSTR pvPara);
230+
160231
/**
161232
* The CertOpenSystemStore function is a simplified function that opens the
162233
* most common system certificate store. To open certificate stores with
163234
* more complex requirements, such as file-based or memory-based stores, use
164-
* CertOpenStore.
235+
* {@link #CertOpenStore(CertStoreProviderName, int, HCRYPTPROV_LEGACY, int, Pointer)}.
165236
*
166237
* @param hprov This parameter is not used and should be set to NULL.
167238
* @param szSubsystemProtocol A string that names a system store. If the
@@ -171,7 +242,9 @@ public boolean CryptUnprotectData(DATA_BLOB pDataIn, PointerByReference szDataDe
171242
* stores. Some example system stores are listed in the following table.
172243
* @return If the function succeeds, the function returns a handle to the
173244
* certificate store. If the function fails, it returns NULL. For extended
174-
* error information, call GetLastError.
245+
* error information, call {@link Native#getLastError()}
246+
*
247+
* @see <a href="https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certopensystemstorew">MSDN</a>
175248
*/
176249
HCERTSTORE CertOpenSystemStore(Pointer hprov, String szSubsystemProtocol);
177250

contrib/platform/src/com/sun/jna/platform/win32/WinCrypt.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
*/
2424
package com.sun.jna.platform.win32;
2525

26+
import com.sun.jna.FromNativeContext;
2627
import com.sun.jna.Memory;
2728
import com.sun.jna.Native;
29+
import com.sun.jna.NativeMapped;
2830
import com.sun.jna.Pointer;
2931
import com.sun.jna.StringArray;
3032
import com.sun.jna.Structure;
@@ -1194,6 +1196,49 @@ public CRYPTPROTECT_PROMPTSTRUCT(Pointer memory) {
11941196
}
11951197
}
11961198

1199+
/**
1200+
* Value wrapper for {@code lpszStoreProvider} of {@link Crypt32#CertOpenStore(CertStoreProviderName, int, HCRYPTPROV_LEGACY, int, Pointer)}
1201+
*/
1202+
class CertStoreProviderName implements NativeMapped {
1203+
private final Pointer pointer;
1204+
1205+
private CertStoreProviderName(Pointer fixedValue) {
1206+
this.pointer = fixedValue;
1207+
}
1208+
1209+
public CertStoreProviderName() {
1210+
pointer = Pointer.NULL;
1211+
}
1212+
1213+
public CertStoreProviderName(int fixedValue) {
1214+
this.pointer = Pointer.createConstant(fixedValue);
1215+
}
1216+
1217+
public CertStoreProviderName(String name) {
1218+
byte[] nameBytes = Native.toByteArray(name);
1219+
this.pointer = new Memory(nameBytes.length);
1220+
this.pointer.write(0, nameBytes, 0, nameBytes.length);
1221+
}
1222+
1223+
@Override
1224+
public Object fromNative(Object nativeValue, FromNativeContext fnc) {
1225+
if (nativeValue == null) {
1226+
return null;
1227+
}
1228+
return new CertStoreProviderName((Pointer) nativeValue);
1229+
}
1230+
1231+
@Override
1232+
public Object toNative() {
1233+
return pointer;
1234+
}
1235+
1236+
@Override
1237+
public Class<?> nativeType() {
1238+
return Pointer.class;
1239+
}
1240+
}
1241+
11971242
//
11981243
// CryptProtect PromptStruct dwPromtFlags
11991244
//
@@ -1617,6 +1662,68 @@ public CRYPTPROTECT_PROMPTSTRUCT(Pointer memory) {
16171662
*/
16181663
int PKCS12_INCLUDE_EXTENDED_PROPERTIES = 0x0010;
16191664

1665+
//
1666+
// Store providers for CertOpenStore
1667+
//
1668+
1669+
int CERT_STORE_PROV_MSG = 1;
1670+
int CERT_STORE_PROV_MEMORY = 2;
1671+
int CERT_STORE_PROV_FILE = 3;
1672+
int CERT_STORE_PROV_REG = 4;
1673+
int CERT_STORE_PROV_PKCS7 = 5;
1674+
int CERT_STORE_PROV_SERIALIZED = 6;
1675+
int CERT_STORE_PROV_FILENAME_A = 7; // ASCII
1676+
int CERT_STORE_PROV_FILENAME_W = 8; // Unicode
1677+
int CERT_STORE_PROV_FILENAME = CERT_STORE_PROV_FILENAME_W;
1678+
int CERT_STORE_PROV_SYSTEM_A = 9; // pvPara is ASCII (1 byte/char)
1679+
int CERT_STORE_PROV_SYSTEM_W = 10; // pvPara is Unicode (2 bytes/char)
1680+
int CERT_STORE_PROV_SYSTEM = CERT_STORE_PROV_SYSTEM_W;
1681+
int CERT_STORE_PROV_COLLECTION = 11;
1682+
int CERT_STORE_PROV_SYSTEM_REGISTRY_A = 12;
1683+
int CERT_STORE_PROV_SYSTEM_REGISTRY_W = 13;
1684+
int CERT_STORE_PROV_SYSTEM_REGISTRY = CERT_STORE_PROV_SYSTEM_REGISTRY_W;
1685+
int CERT_STORE_PROV_PHYSICAL_W = 14;
1686+
int CERT_STORE_PROV_PHYSICAL = CERT_STORE_PROV_PHYSICAL_W;
1687+
int CERT_STORE_PROV_SMART_CARD_W = 15;
1688+
int CERT_STORE_PROV_SMART_CARD = CERT_STORE_PROV_SMART_CARD_W;
1689+
int CERT_STORE_PROV_LDAP_W = 16;
1690+
int CERT_STORE_PROV_LDAP = CERT_STORE_PROV_LDAP_W;
1691+
1692+
//
1693+
// Store characteristics for CertOpenStore
1694+
//
1695+
1696+
int CERT_STORE_NO_CRYPT_RELEASE_FLAG = 0x00000001;
1697+
int CERT_STORE_SET_LOCALIZED_NAME_FLAG = 0x00000002;
1698+
int CERT_STORE_DEFER_CLOSE_UNTIL_LAST_FREE_FLAG = 0x00000004;
1699+
int CERT_STORE_DELETE_FLAG = 0x00000010;
1700+
int CERT_STORE_UNSAFE_PHYSICAL_FLAG = 0x00000020;
1701+
int CERT_STORE_SHARE_STORE_FLAG = 0x00000040;
1702+
int CERT_STORE_SHARE_CONTEXT_FLAG = 0x00000080;
1703+
int CERT_STORE_MANIFOLD_FLAG = 0x00000100;
1704+
int CERT_STORE_ENUM_ARCHIVED_FLAG = 0x00000200;
1705+
int CERT_STORE_UPDATE_KEYID_FLAG = 0x00000400;
1706+
int CERT_STORE_BACKUP_RESTORE_FLAG = 0x00000800;
1707+
int CERT_STORE_MAXIMUM_ALLOWED_FLAG = 0x00001000;
1708+
int CERT_STORE_CREATE_NEW_FLAG = 0x00002000;
1709+
int CERT_STORE_OPEN_EXISTING_FLAG = 0x00004000;
1710+
int CERT_STORE_READONLY_FLAG = 0x00008000;
1711+
1712+
//
1713+
// Store locations for CertOpenStore
1714+
//
1715+
1716+
int CERT_SYSTEM_STORE_CURRENT_USER = 0x00010000;
1717+
int CERT_SYSTEM_STORE_LOCAL_MACHINE = 0x00020000;
1718+
int CERT_SYSTEM_STORE_CURRENT_SERVICE = 0x00040000;
1719+
int CERT_SYSTEM_STORE_SERVICES = 0x00050000;
1720+
int CERT_SYSTEM_STORE_USERS = 0x00060000;
1721+
int CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY = 0x00070000;
1722+
int CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY = 0x00080000;
1723+
int CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE = 0x00090000;
1724+
int CERT_SYSTEM_STORE_UNPROTECTED_FLAG = 0x40000000;
1725+
int CERT_SYSTEM_STORE_RELOCATE_FLAG = 0x80000000;
1726+
16201727
/**
16211728
* Checks for nonfreed certificate, CRL, and CTL contexts. A returned error
16221729
* code indicates that one or more store elements is still in use. This flag

contrib/platform/test/com/sun/jna/platform/win32/Crypt32Test.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,32 @@ public void testCertVerifyCertificateChainPolicy() {
423423
assertTrue("The status would be true since a valid certificate chain was not passed in.", status);
424424
}
425425

426-
public void testCertEnumCertificatesInStore() {
427-
String SYSTEM_STORE_NAME = "ROOT";
428-
WinCrypt.HCERTSTORE hCertStore = Crypt32.INSTANCE.CertOpenSystemStore(null, SYSTEM_STORE_NAME);
426+
public void testCertOpenSystemStore() {
427+
WinCrypt.HCERTSTORE hCertStore = Crypt32.INSTANCE.CertOpenSystemStore(null, "ROOT");
428+
enumerateRootCertificates(hCertStore);
429+
}
430+
431+
public void testCertOpenStoreWithPointerPara() {
432+
WinCrypt.HCERTSTORE hCertStore = Crypt32.INSTANCE.CertOpenStore(
433+
new WinCrypt.CertStoreProviderName(WinCrypt.CERT_STORE_PROV_SYSTEM_REGISTRY_W),
434+
0,
435+
null,
436+
WinCrypt.CERT_SYSTEM_STORE_LOCAL_MACHINE | WinCrypt.CERT_STORE_OPEN_EXISTING_FLAG | WinCrypt.CERT_STORE_READONLY_FLAG,
437+
new WTypes.LPWSTR("ROOT"));
438+
enumerateRootCertificates(hCertStore);
439+
}
440+
441+
public void testCertOpenStoreWithStringPara() {
442+
WinCrypt.HCERTSTORE hCertStore = Crypt32.INSTANCE.CertOpenStore(
443+
new WinCrypt.CertStoreProviderName(WinCrypt.CERT_STORE_PROV_SYSTEM_REGISTRY_W),
444+
0,
445+
null,
446+
WinCrypt.CERT_SYSTEM_STORE_LOCAL_MACHINE | WinCrypt.CERT_STORE_OPEN_EXISTING_FLAG | WinCrypt.CERT_STORE_READONLY_FLAG,
447+
new WTypes.LPWSTR("ROOT").getPointer());
448+
enumerateRootCertificates(hCertStore);
449+
}
429450

451+
private void enumerateRootCertificates(HCERTSTORE hCertStore) {
430452
int readCertificates = 0;
431453
int readExtensions = 0;
432454

0 commit comments

Comments
 (0)