cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
java_jni.c
Go to the documentation of this file.
1 
2 #include "../crypt.h"
3 
4 #ifdef USE_JAVA
5 
6 #include <jni.h>
7 #include <stdio.h> //printf
8 #include <stdlib.h> //malloc, free
9 
10 
11 
12 /* Helper Functions */
13 
14 int processStatus(JNIEnv *env, jint status)
15 {
16  jclass exClass;
17  jmethodID exConstructor;
18  jthrowable obj;
19 
20  if (status >= cryptlib_crypt_OK)
21  return 1;
22 
23  exClass = (*env)->FindClass(env, "cryptlib/CryptException");
24  if (exClass == 0)
25  {
26  printf("java_jni.c:processStatus - no class?!\n");
27  return 0;
28  }
29 
30  exConstructor = (*env)->GetMethodID(env, exClass, "<init>", "(I)V");
31  if (exConstructor == 0)
32  {
33  printf("java_jni.c:processStatus - no constructor?!\n");
34  return 0;
35  }
36 
37  obj = (*env)->NewObject(env, exClass, exConstructor, status);
38  if (obj == 0)
39  {
40  printf("java_jni.c:processStatus - no object?!\n");
41  return 0;
42  }
43 
44  if ((*env)->Throw(env, obj) < 0)
45  {
46  printf("java_jni.c:processStatus - failed to throw?!\n");
47  return 0;
48  }
49  return 0;
50 }
51 
52 jobject processStatusReturnCryptQueryInfo(JNIEnv *env, int status, CRYPT_QUERY_INFO returnValue)
53 {
54  jclass exClass;
55  jmethodID exConstructor;
56  jstring algoName;
57  jobject obj;
58 
59  if (status < cryptlib_crypt_OK)
60  return NULL;
61 
62  exClass = (*env)->FindClass(env, "cryptlib/CRYPT_QUERY_INFO");
63  if (exClass == 0)
64  {
65  printf("java_jni.c:processStatusReturnCryptQueryInfo - no class?!\n");
66  return NULL;
67  }
68 
69  exConstructor = (*env)->GetMethodID(env, exClass, "<init>", "(Ljava/lang/String;IIII)V");
70  if (exConstructor == 0)
71  {
72  printf("java_jni.c:processStatusReturnCryptQueryInfo - no constructor?!\n");
73  return NULL;
74  }
75 
76  algoName = (*env)->NewStringUTF(env, returnValue.algoName);
77 
78  obj = (*env)->NewObject(env, exClass, exConstructor, algoName, returnValue.blockSize, returnValue.minKeySize, returnValue.keySize, returnValue.maxKeySize);
79  if (obj == 0)
80  {
81  printf("java_jni.c:processStatusReturnCryptQueryInfo - no object?!\n");
82  return NULL;
83  }
84 
85  return obj;
86 }
87 
88 jobject processStatusReturnCryptObjectInfo(JNIEnv *env, int status, CRYPT_OBJECT_INFO returnValue)
89 {
90  jclass exClass;
91  jmethodID exConstructor;
92  jbyteArray salt;
93  jobject obj;
94 
95  if (status < cryptlib_crypt_OK)
96  return NULL;
97 
98  exClass = (*env)->FindClass(env, "cryptlib/CRYPT_OBJECT_INFO");
99  if (exClass == 0)
100  {
101  printf("java_jni.c:processStatusReturnCryptObjectInfo - no class?!\n");
102  return NULL;
103  }
104 
105  exConstructor = (*env)->GetMethodID(env, exClass, "<init>", "(IIII[B)V");
106  if (exConstructor == 0)
107  {
108  printf("java_jni.c:processStatusReturnCryptObjectInfo - no constructor?!\n");
109  return NULL;
110  }
111 
112  salt = (*env)->NewByteArray(env, returnValue.saltSize);
113  (*env)->SetByteArrayRegion(env, salt, 0, returnValue.saltSize, returnValue.salt);
114 
115  obj = (*env)->NewObject(env, exClass, exConstructor, returnValue.objectType, returnValue.cryptAlgo, returnValue.cryptMode, returnValue.hashAlgo, salt);
116  if (obj == 0)
117  {
118  printf("java_jni.c:processStatusReturnCryptObjectInfo - no object?!\n");
119  return NULL;
120  }
121 
122  return obj;
123 }
124 
125 int checkIndicesArray(JNIEnv *env, jbyteArray array, int sequenceOffset, int sequenceLength)
126 {
127  jsize arrayLength;
128  jclass exClass;
129 
130  if (array == NULL)
131  {
132  if (sequenceOffset == 0)
133  return 1;
134  else
135  {
136  exClass = (*env)->FindClass(env, "java/lang/ArrayIndexOutOfBoundsException");
137  if (exClass == 0)
138  printf("java_jni.c:checkIndicesArray - no class?!\n");
139  else
140  if ((*env)->ThrowNew(env, exClass, "") < 0)
141  printf("java_jni.c:checkIndicesArray - failed to throw?!\n");
142  return 0;
143  }
144  }
145 
146  arrayLength = (*env)->GetArrayLength(env, array);
147 
148  if (sequenceOffset < 0 ||
149  sequenceOffset >= arrayLength ||
150  sequenceOffset + sequenceLength > arrayLength)
151  {
152  exClass = (*env)->FindClass(env, "java/lang/ArrayIndexOutOfBoundsException");
153  if (exClass == 0)
154  printf("java_jni.c:checkIndicesArray - no class?!\n");
155  else
156  if ((*env)->ThrowNew(env, exClass, "") < 0)
157  printf("java_jni.c:checkIndicesArray - failed to throw?!\n");
158  return 0;
159  }
160  return 1;
161 }
162 
163 int getPointerArray(JNIEnv* env, jbyteArray array, jbyte** bytesPtrPtr)
164 {
165  jboolean isCopy;
166 
167  if (array == NULL)
168  {
169  (*bytesPtrPtr) = NULL;
170  return 1;
171  }
172 
173  (*bytesPtrPtr) = (*env)->GetByteArrayElements(env, array, &isCopy);
174 
175  if (*bytesPtrPtr == NULL)
176  {
177  printf("java_jni.c:getPointer - failed to get elements of array?!\n");
178  return 0;
179  }
180  return 1;
181 }
182 
183 void releasePointerArray(JNIEnv* env,jbyteArray array, jbyte* bytesPtr)
184 {
185  if (bytesPtr == NULL)
186  return;
187  (*env)->ReleaseByteArrayElements(env, array, bytesPtr, 0);
188 }
189 
190 int checkIndicesNIO(JNIEnv *env, jobject byteBuffer, int sequenceOffset, int sequenceLength)
191 {
192  jlong byteBufferLength;
193  jclass exClass;
194 
195  if (byteBuffer == NULL)
196  {
197  if (sequenceOffset == 0)
198  return 1;
199  else
200  {
201  exClass = (*env)->FindClass(env, "java/lang/ArrayIndexOutOfBoundsException");
202  if (exClass == 0)
203  printf("java_jni.c:checkIndicesNIO - no class?!\n");
204  else
205  if ((*env)->ThrowNew(env, exClass, "") < 0)
206  printf("java_jni.c:checkIndicesNIO - failed to throw?!\n");
207  return 0;
208  }
209  }
210 
211  byteBufferLength = (*env)->GetDirectBufferCapacity(env, byteBuffer);
212  if (byteBufferLength == -1)
213  {
214  exClass = (*env)->FindClass(env, "java/lang/UnsupportedOperationException");
215  if (exClass == 0)
216  printf("java_jni.c:checkIndicesNIO - no class?!\n");
217  else
218  if ((*env)->ThrowNew(env, exClass,
219 "Either a non-direct ByteBuffer was passed or your JVM doesn't support JNI access to direct ByteBuffers") < 0)
220  printf("java_jni.c:checkIndicesNIO - failed to throw?!\n");
221  return 0;
222  }
223 
224  if (sequenceOffset < 0 ||
225  sequenceOffset >= byteBufferLength ||
226  sequenceOffset + sequenceLength > byteBufferLength)
227  {
228  exClass = (*env)->FindClass(env, "java/lang/ArrayIndexOutOfBoundsException");
229  if (exClass == 0)
230  printf("java_jni.c:checkIndicesNIO - no class?!\n");
231  else
232  if ((*env)->ThrowNew(env, exClass, "") < 0)
233  printf("java_jni.c:checkIndicesNIO - failed to throw?!\n");
234  return 0;
235  }
236  return 1;
237 }
238 
239 int getPointerNIO(JNIEnv* env, jobject byteBuffer, jbyte** bytesPtrPtr)
240 {
241  jclass exClass;
242 
243  if (byteBuffer == NULL)
244  {
245  (*bytesPtrPtr) = NULL;
246  return 1;
247  }
248 
249  (*bytesPtrPtr) = (*env)->GetDirectBufferAddress(env, byteBuffer);
250 
251  if (*bytesPtrPtr == NULL)
252  {
253  exClass = (*env)->FindClass(env, "java/lang/UnsupportedOperationException");
254  if (exClass == 0)
255  printf("java_jni.c:getPointerNIO - no class?!\n");
256  else
257  if ((*env)->ThrowNew(env, exClass, "Your JVM doesn't support JNI access to direct ByteBuffers") < 0)
258  printf("java_jni.c:getPointerNIO - failed to throw?!\n");
259  return 0;
260  }
261  return 1;
262 }
263 
264 void releasePointerNIO(JNIEnv* env,jbyteArray array, jbyte* bytesPtr)
265 {
266 }
267 
268 int getPointerString(JNIEnv* env, jstring str, jbyte** bytesPtrPtr)
269 {
270  jboolean isCopy;
271  jsize strLength;
272  const jbyte* rawBytesPtr;
273  jclass exClass;
274 #ifdef __WINCE__
275  int status;
276 #endif // __WINCE__
277 
278 
279  if (str == NULL)
280  {
281  (*bytesPtrPtr) = NULL;
282  return 1;
283  }
284 
285  rawBytesPtr = (*env)->GetStringUTFChars(env, str, &isCopy);
286 
287  if (rawBytesPtr == NULL)
288  {
289  printf("java_jni.c:getPointerString - failed to get elements of String?!\n");
290  return 0;
291  }
292 
293  strLength = (*env)->GetStringUTFLength(env, str);
294 
295 #ifdef __WINCE__
296  (*bytesPtrPtr) = (jbyte*)malloc(strLength*2+2); // this is unicode, therefore \0 is two bytes long
297 #else
298  (*bytesPtrPtr) = (jbyte*)malloc(strLength+1);
299 #endif // __WINCE__
300 
301  if (*bytesPtrPtr == NULL)
302  {
303  exClass = (*env)->FindClass(env, "java/lang/OutOfMemoryError");
304  if (exClass == 0)
305  printf("java_jni.c:getPointerString - no class?!\n");
306  else
307  if ((*env)->ThrowNew(env, exClass, "") < 0)
308  printf("java_jni.c:getPointerString - failed to throw?!\n");
309  (*env)->ReleaseStringUTFChars(env, str, rawBytesPtr);
310  return 0;
311  }
312 
313 #ifdef __WINCE__
314  status = asciiToUnicode (*bytesPtrPtr, strLength*2+2, rawBytesPtr, strLength+1);
315  if (status == CRYPT_ERROR_BADDATA) {
316  (*env)->ReleaseStringUTFChars(env, str, rawBytesPtr);
317  return 0;
318  }
319 #else
320  memcpy(*bytesPtrPtr, rawBytesPtr, strLength);
321  (*bytesPtrPtr)[strLength] = 0;
322 #endif // __WINCE__
323 
324  (*env)->ReleaseStringUTFChars(env, str, rawBytesPtr);
325 
326  return 1;
327 }
328 
329 void releasePointerString(JNIEnv* env, jstring str, jbyte* bytesPtr)
330 {
331  if (bytesPtr == NULL)
332  return;
333  free(bytesPtr);
334 }
335 
336 
337 
338 #endif /* USE_JAVA */