Skip to content

Commit e939e66

Browse files
Merge branch 'rosh89-CryptoLib'
2 parents 2707e36 + 9737e48 commit e939e66

13 files changed

Lines changed: 2626 additions & 20 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Release 5.0.0 (Next release)
77

88
Features
99
--------
10+
* [#915](https://github.com/java-native-access/jna/pull/915): Adding interfaces to call to Cryptui and Crypt32 windows libraries and adding related structures to Wincrypt. - [@rosh89](https://github.com/rosh89).
1011
* [#903](https://github.com/java-native-access/jna/pull/903): Carry `HRESULT` in `c.s.j.p.win32.COM.COMException`, introduce `c.s.j.p.win32.COM.COMInvokeException` as subclass of `COMException` for exception as the result of a `IDispatch#Invoke`. The `EXECPINFO` is unwrapped into fields in the `COMInvokeException` and correctly freed. - [@matthiasblaesing](https://github.com/matthiasblaesing).
1112
* [#822](https://github.com/java-native-access/jna/issues/822): `Native#loadLibrary` requires that the interface class passed in is an instance of Library. The runtime check can be enhanced by using a constraint generic. This breaks binary compatibility (see notes below) - [@d-noll](https://github.com/d-noll).
1213
* [#889](https://github.com/java-native-access/jna/issues/889): The `Structure#newInstance` receive the target type as a parameter. This adds a limited generic type, so that the return type ist the target type and not a generic structure, removing the necessity to do an explizit cast - [@matthiasblaesing](https://github.com/matthiasblaesing).

contrib/platform/nbproject/project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jar.compress=false
4848
javac.classpath=\
4949
${file.reference.jna.jar}
5050
# Space-separated list of extra javac options
51-
javac.compilerargs=
51+
javac.compilerargs=-XDignore.symbol.file
5252
javac.deprecation=false
5353
javac.source=1.6
5454
javac.target=1.6

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

Lines changed: 351 additions & 2 deletions
Large diffs are not rendered by default.

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

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

26+
import com.sun.jna.Pointer;
27+
import com.sun.jna.Memory;
28+
import com.sun.jna.Native;
2629
import com.sun.jna.platform.win32.WinCrypt.CRYPTPROTECT_PROMPTSTRUCT;
2730
import com.sun.jna.platform.win32.WinCrypt.DATA_BLOB;
2831
import com.sun.jna.ptr.PointerByReference;
@@ -174,4 +177,48 @@ public static byte[] cryptUnprotectData(byte[] data, byte[] entropy, int flags,
174177

175178
return unProtectedData;
176179
}
180+
181+
/**
182+
* Utility method to call to Crypt32's CertNameToStr that allocates the
183+
* assigns the required memory for the psz parameter based on the type
184+
* mapping used, calls to CertNameToStr, and returns the received string.
185+
*
186+
* @param dwCertEncodingType The certificate encoding type that was used to
187+
* encode the name. The message encoding type identifier, contained in the
188+
* high WORD of this value, is ignored by this function.
189+
* @param pName A pointer to the CERT_NAME_BLOB structure to be converted.
190+
* @param dwStrType This parameter specifies the format of the output
191+
* string. This parameter also specifies other options for the contents of
192+
* the string.
193+
* @return Returns the retrieved string.
194+
*/
195+
public static String CertNameToStr(int dwCertEncodingType, int dwStrType, DATA_BLOB pName) {
196+
int charToBytes = Boolean.getBoolean("w32.ascii") ? 1 : Native.WCHAR_SIZE;
197+
198+
// Initialize the signature structure.
199+
int requiredSize = Crypt32.INSTANCE.CertNameToStr(
200+
dwCertEncodingType,
201+
pName,
202+
dwStrType,
203+
Pointer.NULL,
204+
0);
205+
206+
Memory mem = new Memory(requiredSize * charToBytes);
207+
208+
// Initialize the signature structure.
209+
int resultBytes = Crypt32.INSTANCE.CertNameToStr(
210+
dwCertEncodingType,
211+
pName,
212+
dwStrType,
213+
mem,
214+
requiredSize);
215+
216+
assert resultBytes == requiredSize;
217+
218+
if (Boolean.getBoolean("w32.ascii")) {
219+
return mem.getString(0);
220+
} else {
221+
return mem.getWideString(0);
222+
}
223+
}
177224
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* Copyright (c) 2018 Roshan Muralidharan, All Rights Reserved
2+
*
3+
* The contents of this file is dual-licensed under 2
4+
* alternative Open Source/Free licenses: LGPL 2.1 or later and
5+
* Apache License 2.0. (starting with JNA version 4.0.0).
6+
*
7+
* You can freely decide which license you want to apply to
8+
* the project.
9+
*
10+
* You may obtain a copy of the LGPL License at:
11+
*
12+
* http://www.gnu.org/licenses/licenses.html
13+
*
14+
* A copy is also included in the downloadable source code package
15+
* containing JNA, in file "LGPL2.1".
16+
*
17+
* You may obtain a copy of the Apache License at:
18+
*
19+
* http://www.apache.org/licenses/
20+
*
21+
* A copy is also included in the downloadable source code package
22+
* containing JNA, in file "AL2.0".
23+
*/
24+
package com.sun.jna.platform.win32;
25+
26+
import com.sun.jna.Native;
27+
import com.sun.jna.PointerType;
28+
import com.sun.jna.platform.win32.WinDef.HWND;
29+
import com.sun.jna.platform.win32.WinNT.HANDLE;
30+
import com.sun.jna.win32.StdCallLibrary;
31+
import com.sun.jna.win32.W32APIOptions;
32+
import com.sun.jna.platform.win32.WinCrypt.*;
33+
34+
/**
35+
* Cryptui.dll Interface.
36+
* @author roshan[dot]muralidharan[at]cerner[dot]com
37+
*/
38+
public interface Cryptui extends StdCallLibrary {
39+
40+
Cryptui INSTANCE = (Cryptui) Native.loadLibrary("Cryptui", Cryptui.class, W32APIOptions.UNICODE_OPTIONS);
41+
42+
/**
43+
* The CryptUIDlgSelectCertificateFromStore function displays a dialog box that
44+
* allows the selection of a certificate from a specified store.
45+
*
46+
* @param hCertStore
47+
* Handle of the certificate store to be searched.
48+
* @param hwnd
49+
* Handle of the window for the display. If NULL, defaults to the
50+
* desktop window.
51+
* @param pwszTitle
52+
* String used as the title of the dialog box. If NULL, the default
53+
* title, "Select Certificate," is used.
54+
* @param pwszDisplayString
55+
* Text statement in the selection dialog box. If NULL, the default
56+
* phrase, "Select a certificate you want to use," is used.
57+
* @param dwDontUseColumn
58+
* Flags that can be combined to exclude columns of the display.
59+
* @param dwFlags
60+
* Currently not used and should be set to 0.
61+
* @param pvReserved
62+
* Reserved for future use.
63+
* @return Returns a pointer to the selected certificate context. If no
64+
* certificate was selected, NULL is returned. When you have finished
65+
* using the certificate, free the certificate context by calling the
66+
* CertFreeCertificateContext function.
67+
*/
68+
CERT_CONTEXT.ByReference CryptUIDlgSelectCertificateFromStore(HCERTSTORE hCertStore, HWND hwnd, String pwszTitle,
69+
String pwszDisplayString, int dwDontUseColumn, int dwFlags, PointerType pvReserved);
70+
71+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public String getString() {
179179
}
180180

181181
public static class LPSTR extends PointerType {
182-
public static class ByReference extends BSTR implements
182+
public static class ByReference extends LPSTR implements
183183
Structure.ByReference {
184184
}
185185

@@ -192,19 +192,19 @@ public LPSTR(Pointer pointer) {
192192
}
193193

194194
public LPSTR(String value) {
195-
this(new Memory((value.length() + 1L) * Native.WCHAR_SIZE));
195+
this(new Memory(value.length() + 1L));
196196
this.setValue(value);
197197
}
198198

199199
public void setValue(String value) {
200-
this.getPointer().setWideString(0, value);
200+
this.getPointer().setString(0, value);
201201
}
202202

203203
public String getValue() {
204204
Pointer pointer = this.getPointer();
205205
String str = null;
206206
if (pointer != null)
207-
str = pointer.getWideString(0);
207+
str = pointer.getString(0);
208208

209209
return str;
210210
}

0 commit comments

Comments
 (0)