Main Page
From PoorMSCAPI
Contents |
Usage
There are two files.
- poormscapi.jar
- poormscapi.dll
The jar file should be on your classpath and DLL must be linked with the -Djava.library.path="path_to_your_dll_directory" option. Another way is to link it in your code:
System.setProperty("java.library.path", "path_to_your_dll_directory");
- Actually the dll is compiled with VC++ Express 2008 so you have to have Microsoft Visual C++ 2008 Redistributable Package (x86) istalled.
SPI implementations
These are supported algorithms for calls like
Signature.getInstance(algorithm, provider);
- Signature
- SHA1withRSA
- SHA256withRSA
- KeyStore
- WINDOWS
- Cipher
- RSA/ECB/PKCS1Padding (For SSL connection only! Can't be used to sign more or less than exactly 36 bytes of data.)
Example
Signature signature = Signature.getInstance("SHA1withRSA", "PoorMSCAPI");
KeyStore
- A new instance of KeyStore is created by calling KeyStore.getInstance("WINDOWS", "PoorMSCAPI");. This call must be preceded by key store name specification.
- MY, ROOT and CA Windows key stores are supported and defined in PoorSettings.
Example
PoorSettings.setStoreName(PoorSettings.KEY_STORE_MY); KeyStore keyStore = KeyStore.getInstance("WINDOWS", "PoorMSCAPI"); keyStore.load(null, null);
Signature
- Two types of digital signature are supported.
- Raw RSA.
- In PKCS#7 form.
Raw RSA
Example
PoorSettings.setSignatureType(PoorSettings.SIGNATURE_RAW_RSA); Signature signature = Signature.getInstance("SHA256withRSA", "PoorMSCAPI"); signature.initSign(privateKey); signature.update("data".getBytes()); byte[] rawSignature = signature.sign();
PKCS#7
- Signature output will be in PKCS#7 form.
- You can specify whether to include signer's certificate and whether the output will be in detached form or not.
Example
PoorSettings.setSignatureType(PoorSettings.SIGNATURE_PKCS_7); PoorSettings.setIncludeSigner(true); PoorSettings.setDetachedSignature(false); Signature signature = Signature.getInstance("SHA256withRSA", "PoorMSCAPI"); signature.initSign(privateKey); signature.update("data".getBytes()); byte[] rawSignature = signature.sign();
SSL connection
- JRE 1.4.2 is required (this does not work on JRE 1.6).
- Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 1.4.2 should be installed.
import java.net.URL; import java.security.KeyStore; import java.security.SecureRandom; import java.security.Security; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManagerFactory; import poor.provider.PoorMSCAPI; import poor.utils.PoorSettings; public class Main { public static void main(String[] args) { try { System.setProperty("javax.net.debug", "all"); Security.insertProviderAt(new PoorMSCAPI(), 1); URL urlcon = new URL("https://www.yourserver.com"); HttpsURLConnection connection = (HttpsURLConnection) urlcon.openConnection(); connection.setSSLSocketFactory(createFactory()); connection.connect(); System.out.println("connected"); } catch (Exception e) { e.printStackTrace(); } } private static SSLSocketFactory createFactory() throws Exception { SSLContext sslContext = SSLContext.getInstance("TLS"); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509"); PoorSettings.setStoreName(PoorSettings.KEY_STORE_MY); KeyStore keyStore = KeyStore.getInstance("WINDOWS"); keyStore.load(null, null); keyManagerFactory.init(keyStore, null); TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509"); PoorSettings.setStoreName(PoorSettings.KEY_STORE_ROOT); KeyStore trustStore = KeyStore.getInstance("WINDOWS"); trustStore.load(null, null); try { trustManagerFactory.init(trustStore); } catch (Exception e) { e.printStackTrace(); } sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new SecureRandom()); return sslContext.getSocketFactory(); } }
How to compile a DLL
- You need Visual C++ Express edition. The process of essay writing will be much easier with MarvelousEssays.Com as there are a lot of highly professional and talented writers who are always eager to help you out with any sort of academic assignments regardless of the complexity levels. I do know what I�m talking about!
- If you want to run the dll, you also need Microsoft Visual C++ 2008 Redistributable Package (x86).
- Native code uses JNI (Java Native Interface) so you need to have some SDK installed.
- Create an empty Win32 DLL project.
- Add required headers and source files.
- Under Project -> Properties -> VC++ Directories -> Include directories add include and include/win32 directories under your SDK directory. Example:
- C:\Program Files\Java\jdk1.6.0_20\include
- C:\Program Files\Java\jdk1.6.0_20\include\win32
- Under Project -> Properties -> Linker -> Input -> Additional dependencies add crypt32.lib.
- Build the project.
x64 build
- Windows SDK with x64 compiler must be installed.
- Under Build -> Configuration manager -> Active solution platform add New -> x64 and make sure that Create new project platforms is checked.
- Under Project -> Properties -> General -> Platform toolset choose Windows7.1SDK (or the one that you installed).
- Follow the steps for x86 build.
