2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
*/
|
|
4 |
|
|
5 |
/* Copyright (c) 2002 Graz University of Technology. All rights reserved.
|
|
6 |
*
|
|
7 |
* Redistribution and use in source and binary forms, with or without
|
|
8 |
* modification, are permitted provided that the following conditions are met:
|
|
9 |
*
|
|
10 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
11 |
* this list of conditions and the following disclaimer.
|
|
12 |
*
|
|
13 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
14 |
* this list of conditions and the following disclaimer in the documentation
|
|
15 |
* and/or other materials provided with the distribution.
|
|
16 |
*
|
|
17 |
* 3. The end-user documentation included with the redistribution, if any, must
|
|
18 |
* include the following acknowledgment:
|
|
19 |
*
|
|
20 |
* "This product includes software developed by IAIK of Graz University of
|
|
21 |
* Technology."
|
|
22 |
*
|
|
23 |
* Alternately, this acknowledgment may appear in the software itself, if
|
|
24 |
* and wherever such third-party acknowledgments normally appear.
|
|
25 |
*
|
|
26 |
* 4. The names "Graz University of Technology" and "IAIK of Graz University of
|
|
27 |
* Technology" must not be used to endorse or promote products derived from
|
|
28 |
* this software without prior written permission.
|
|
29 |
*
|
|
30 |
* 5. Products derived from this software may not be called
|
|
31 |
* "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
|
|
32 |
* written permission of Graz University of Technology.
|
|
33 |
*
|
|
34 |
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
|
|
35 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
36 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
37 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
|
|
38 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
39 |
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
40 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
|
41 |
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
42 |
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
43 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
44 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
45 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
46 |
*/
|
|
47 |
|
|
48 |
/*
|
|
49 |
* pkcs11wrapper.c
|
|
50 |
* 18.05.2001
|
|
51 |
*
|
|
52 |
* This module contains the native functions of the Java to PKCS#11 interface
|
|
53 |
* which are platform dependent. This includes loading a dynamic link libary,
|
|
54 |
* retrieving the function list and unloading the dynamic link library.
|
|
55 |
*
|
|
56 |
* @author Karl Scheibelhofer <Karl.Scheibelhofer@iaik.at>
|
|
57 |
*/
|
|
58 |
|
|
59 |
#include "pkcs11wrapper.h"
|
|
60 |
|
|
61 |
#include <stdio.h>
|
|
62 |
#include <stdlib.h>
|
|
63 |
#include <string.h>
|
|
64 |
#include <assert.h>
|
|
65 |
|
|
66 |
#include <windows.h>
|
|
67 |
|
|
68 |
#include <jni.h>
|
|
69 |
|
|
70 |
#include "sun_security_pkcs11_wrapper_PKCS11.h"
|
|
71 |
|
|
72 |
/*
|
|
73 |
* Class: sun_security_pkcs11_wrapper_PKCS11
|
|
74 |
* Method: connect
|
|
75 |
* Signature: (Ljava/lang/String;)V
|
|
76 |
*/
|
|
77 |
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_connect
|
|
78 |
(JNIEnv *env, jobject obj, jstring jPkcs11ModulePath, jstring jGetFunctionList)
|
|
79 |
{
|
|
80 |
HINSTANCE hModule;
|
|
81 |
CK_C_GetFunctionList C_GetFunctionList;
|
|
82 |
CK_RV rv;
|
|
83 |
ModuleData *moduleData;
|
|
84 |
jobject globalPKCS11ImplementationReference;
|
|
85 |
LPVOID lpMsgBuf;
|
|
86 |
char *exceptionMessage;
|
|
87 |
const char *getFunctionListStr;
|
|
88 |
|
|
89 |
const char *libraryNameStr = (*env)->GetStringUTFChars(env, jPkcs11ModulePath, 0);
|
|
90 |
TRACE1("DEBUG: connect to PKCS#11 module: %s ... ", libraryNameStr);
|
|
91 |
|
|
92 |
|
|
93 |
/*
|
|
94 |
* Load the PKCS #11 DLL
|
|
95 |
*/
|
|
96 |
hModule = LoadLibrary(libraryNameStr);
|
|
97 |
if (hModule == NULL) {
|
|
98 |
FormatMessage(
|
|
99 |
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
100 |
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
101 |
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
102 |
NULL,
|
|
103 |
GetLastError(),
|
|
104 |
0, /* Default language */
|
|
105 |
(LPTSTR) &lpMsgBuf,
|
|
106 |
0,
|
|
107 |
NULL
|
|
108 |
);
|
|
109 |
exceptionMessage = (char *) malloc(sizeof(char) * (strlen((LPTSTR) lpMsgBuf) + strlen(libraryNameStr) + 1));
|
|
110 |
strcpy(exceptionMessage, (LPTSTR) lpMsgBuf);
|
|
111 |
strcat(exceptionMessage, libraryNameStr);
|
|
112 |
throwIOException(env, (LPTSTR) exceptionMessage);
|
|
113 |
/* Free the buffer. */
|
|
114 |
free(exceptionMessage);
|
|
115 |
LocalFree(lpMsgBuf);
|
|
116 |
return;
|
|
117 |
}
|
|
118 |
|
|
119 |
/*
|
|
120 |
* Get function pointer to C_GetFunctionList
|
|
121 |
*/
|
|
122 |
getFunctionListStr = (*env)->GetStringUTFChars(env, jGetFunctionList, 0);
|
|
123 |
C_GetFunctionList = (CK_C_GetFunctionList) GetProcAddress(hModule, getFunctionListStr);
|
|
124 |
(*env)->ReleaseStringUTFChars(env, jGetFunctionList, getFunctionListStr);
|
|
125 |
if (C_GetFunctionList == NULL) {
|
|
126 |
FormatMessage(
|
|
127 |
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
128 |
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
129 |
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
130 |
NULL,
|
|
131 |
GetLastError(),
|
|
132 |
0, /* Default language */
|
|
133 |
(LPTSTR) &lpMsgBuf,
|
|
134 |
0,
|
|
135 |
NULL
|
|
136 |
);
|
|
137 |
throwIOException(env, (LPTSTR) lpMsgBuf);
|
|
138 |
/* Free the buffer. */
|
|
139 |
LocalFree( lpMsgBuf );
|
|
140 |
return;
|
|
141 |
}
|
|
142 |
|
|
143 |
/*
|
|
144 |
* Get function pointers to all PKCS #11 functions
|
|
145 |
*/
|
|
146 |
moduleData = (ModuleData *) malloc(sizeof(ModuleData));
|
|
147 |
moduleData->hModule = hModule;
|
|
148 |
moduleData->applicationMutexHandler = NULL;
|
|
149 |
rv = (C_GetFunctionList)(&(moduleData->ckFunctionListPtr));
|
|
150 |
globalPKCS11ImplementationReference = (*env)->NewGlobalRef(env, obj);
|
|
151 |
putModuleEntry(env, globalPKCS11ImplementationReference, moduleData);
|
|
152 |
|
|
153 |
(*env)->ReleaseStringUTFChars(env, jPkcs11ModulePath, libraryNameStr);
|
|
154 |
TRACE0("FINISHED\n");
|
|
155 |
|
|
156 |
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
|
|
157 |
}
|
|
158 |
|
|
159 |
/*
|
|
160 |
* Class: sun_security_pkcs11_wrapper_PKCS11
|
|
161 |
* Method: disconnect
|
|
162 |
* Signature: ()V
|
|
163 |
*/
|
|
164 |
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_disconnect
|
|
165 |
(JNIEnv *env, jobject obj)
|
|
166 |
{
|
|
167 |
ModuleData *moduleData;
|
|
168 |
TRACE0("DEBUG: disconnecting module...");
|
|
169 |
moduleData = removeModuleEntry(env, obj);
|
|
170 |
|
|
171 |
if (moduleData != NULL) {
|
|
172 |
FreeLibrary(moduleData->hModule);
|
|
173 |
}
|
|
174 |
|
|
175 |
free(moduleData);
|
|
176 |
TRACE0("FINISHED\n");
|
|
177 |
}
|