Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding wrappers to Cryptui/Crypt32 and structures to WinCrypt. #915

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Release 5.0.0 (Next release)

Features
--------
* [#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).
* [#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).
* [#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).
* [#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).
Expand Down
2 changes: 1 addition & 1 deletion contrib/platform/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jar.compress=false
javac.classpath=\
${file.reference.jna.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.compilerargs=-XDignore.symbol.file
javac.deprecation=false
javac.source=1.6
javac.target=1.6
Expand Down
353 changes: 351 additions & 2 deletions contrib/platform/src/com/sun/jna/platform/win32/Crypt32.java

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/Crypt32Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package com.sun.jna.platform.win32;

import com.sun.jna.Pointer;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinCrypt.CRYPTPROTECT_PROMPTSTRUCT;
import com.sun.jna.platform.win32.WinCrypt.DATA_BLOB;
import com.sun.jna.ptr.PointerByReference;
Expand Down Expand Up @@ -174,4 +177,48 @@ public static byte[] cryptUnprotectData(byte[] data, byte[] entropy, int flags,

return unProtectedData;
}

/**
* Utility method to call to Crypt32's CertNameToStr that allocates the
* assigns the required memory for the psz parameter based on the type
* mapping used, calls to CertNameToStr, and returns the received string.
*
* @param dwCertEncodingType The certificate encoding type that was used to
* encode the name. The message encoding type identifier, contained in the
* high WORD of this value, is ignored by this function.
* @param pName A pointer to the CERT_NAME_BLOB structure to be converted.
* @param dwStrType This parameter specifies the format of the output
* string. This parameter also specifies other options for the contents of
* the string.
* @return Returns the retrieved string.
*/
public static String CertNameToStr(int dwCertEncodingType, int dwStrType, DATA_BLOB pName) {
int charToBytes = Boolean.getBoolean("w32.ascii") ? 1 : Native.WCHAR_SIZE;

// Initialize the signature structure.
int requiredSize = Crypt32.INSTANCE.CertNameToStr(
dwCertEncodingType,
pName,
dwStrType,
Pointer.NULL,
0);

Memory mem = new Memory(requiredSize * charToBytes);

// Initialize the signature structure.
int resultBytes = Crypt32.INSTANCE.CertNameToStr(
dwCertEncodingType,
pName,
dwStrType,
mem,
requiredSize);

assert resultBytes == requiredSize;

if (Boolean.getBoolean("w32.ascii")) {
return mem.getString(0);
} else {
return mem.getWideString(0);
}
}
}
71 changes: 71 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/Cryptui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* Copyright (c) 2018 Roshan Muralidharan, All Rights Reserved
*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to
* the project.
*
* You may obtain a copy of the LGPL License at:
*
* http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/
package com.sun.jna.platform.win32;

import com.sun.jna.Native;
import com.sun.jna.PointerType;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
import com.sun.jna.platform.win32.WinCrypt.*;

/**
* Cryptui.dll Interface.
* @author roshan[dot]muralidharan[at]cerner[dot]com
*/
public interface Cryptui extends StdCallLibrary {

Cryptui INSTANCE = (Cryptui) Native.loadLibrary("Cryptui", Cryptui.class, W32APIOptions.UNICODE_OPTIONS);

/**
* The CryptUIDlgSelectCertificateFromStore function displays a dialog box that
* allows the selection of a certificate from a specified store.
*
* @param hCertStore
* Handle of the certificate store to be searched.
* @param hwnd
* Handle of the window for the display. If NULL, defaults to the
* desktop window.
* @param pwszTitle
* String used as the title of the dialog box. If NULL, the default
* title, "Select Certificate," is used.
* @param pwszDisplayString
* Text statement in the selection dialog box. If NULL, the default
* phrase, "Select a certificate you want to use," is used.
* @param dwDontUseColumn
* Flags that can be combined to exclude columns of the display.
* @param dwFlags
* Currently not used and should be set to 0.
* @param pvReserved
* Reserved for future use.
* @return Returns a pointer to the selected certificate context. If no
* certificate was selected, NULL is returned. When you have finished
* using the certificate, free the certificate context by calling the
* CertFreeCertificateContext function.
*/
CERT_CONTEXT.ByReference CryptUIDlgSelectCertificateFromStore(HCERTSTORE hCertStore, HWND hwnd, String pwszTitle,
String pwszDisplayString, int dwDontUseColumn, int dwFlags, PointerType pvReserved);

}
8 changes: 4 additions & 4 deletions contrib/platform/src/com/sun/jna/platform/win32/WTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public String getString() {
}

public static class LPSTR extends PointerType {
public static class ByReference extends BSTR implements
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you don't extend BSTR?

Copy link
Member

@matthiasblaesing matthiasblaesing Feb 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct. The inner class LPSTR.ByReference is a LPSTR class marked as ByReference (the latter is a marker interface, that forces JNA to pass the value as a reference).

public static class ByReference extends LPSTR implements
Structure.ByReference {
}

Expand All @@ -192,19 +192,19 @@ public LPSTR(Pointer pointer) {
}

public LPSTR(String value) {
this(new Memory((value.length() + 1L) * Native.WCHAR_SIZE));
this(new Memory(value.length() + 1L));
this.setValue(value);
}

public void setValue(String value) {
this.getPointer().setWideString(0, value);
this.getPointer().setString(0, value);
}

public String getValue() {
Pointer pointer = this.getPointer();
String str = null;
if (pointer != null)
str = pointer.getWideString(0);
str = pointer.getString(0);

return str;
}
Expand Down
Loading