PageRenderTime 35ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/WincryptCipher/lib/WincryptCipher.c

#
C | 287 lines | 214 code | 16 blank | 57 comment | 31 complexity | abdca94902fc9ee91d55cb6402cc7d1b MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * WincryptCipherPlugin - A jEdit plugin as wincrypt cipher implementation for the CipherPlugin
  3. * :tabSize=4:indentSize=4:noTabs=true:
  4. *
  5. * Copyright (C) 2007 Björn "Vampire" Kautler
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include "WincryptCipher.h"
  22. #include <wtypes.h>
  23. #include <wincrypt.h>
  24. static jboolean get_crypto_dll(HINSTANCE* pdll) {
  25. HINSTANCE dll = LoadLibraryA("Crypt32.dll");
  26. if (dll) {
  27. if (NULL != pdll) {
  28. *pdll = dll;
  29. } else {
  30. FreeLibrary(dll);
  31. }
  32. return JNI_TRUE;
  33. }
  34. return JNI_FALSE;
  35. }
  36. static jboolean get_crypto_function(const char* name, HINSTANCE* pdll, FARPROC* pfn) {
  37. HINSTANCE dll;
  38. if (get_crypto_dll(&dll)) {
  39. FARPROC fn = GetProcAddress(dll,name);
  40. if (fn) {
  41. *pdll = dll;
  42. *pfn = fn;
  43. return JNI_TRUE;
  44. }
  45. FreeLibrary(dll);
  46. }
  47. return JNI_FALSE;
  48. }
  49. JNIEXPORT jbyteArray JNICALL Java_wincrypt_WincryptCipher_encryptNative(JNIEnv * env,
  50. jobject jObject,
  51. jbyteArray rawDataArray,
  52. jcharArray descriptionArray,
  53. jbyteArray entropyArray) {
  54. typedef BOOL (CALLBACK *encrypt_fn)
  55. (DATA_BLOB *, /* pDataIn */
  56. LPCWSTR, /* szDataDescr */
  57. DATA_BLOB *, /* pOptionalEntropy */
  58. PVOID, /* pvReserved */
  59. CRYPTPROTECT_PROMPTSTRUCT *, /* pPromptStruct */
  60. DWORD, /* dwFlags */
  61. DATA_BLOB *); /* pDataOut */
  62. HINSTANCE dll;
  63. FARPROC fn;
  64. encrypt_fn encrypt;
  65. jbyte* rawData;
  66. DATA_BLOB blobin;
  67. jchar* description;
  68. size_t length;
  69. jchar* copiedDescription;
  70. jbyte* entropy;
  71. DATA_BLOB entropyParam;
  72. DATA_BLOB blobout;
  73. BOOL crypted;
  74. jbyteArray result; //FILE* logfile;
  75. if (!rawDataArray) {
  76. jthrowable npe;
  77. npe = (*env)->FindClass(env,"java/lang/NullPointerException");
  78. if (npe) {
  79. (*env)->ThrowNew(env,npe,"rawData must not be null");
  80. }
  81. return NULL;
  82. }
  83. //#define printf(string) logfile = fopen("C:\\Programme\\jEdit\\logfile.txt","a"); fprintf(logfile,string); fprintf(logfile,"\n"); fflush(logfile); fclose(logfile)
  84. //printf("rawDataArray != null");
  85. if (!descriptionArray) {
  86. jthrowable npe;
  87. npe = (*env)->FindClass(env,"java/lang/NullPointerException");
  88. if (npe) {
  89. (*env)->ThrowNew(env,npe,"description must not be null");
  90. }
  91. return NULL;
  92. } //printf("descriptionArray != null");
  93. if (!entropyArray) {
  94. jthrowable npe;
  95. npe = (*env)->FindClass(env,"java/lang/NullPointerException");
  96. if (npe) {
  97. (*env)->ThrowNew(env,npe,"entropy must not be null");
  98. }
  99. return NULL;
  100. } //printf("entropyArray != null");
  101. if (!get_crypto_function("CryptProtectData",&dll,&fn)) {
  102. return NULL;
  103. } //printf("crypto_function == TRUE");
  104. encrypt = (encrypt_fn)fn; //printf("encrypt = (encrypt_fn)fn");
  105. rawData = (*env)->GetByteArrayElements(env,rawDataArray,NULL); //printf("rawData = ");
  106. blobin.cbData = (*env)->GetArrayLength(env,rawDataArray); //printf("blobin.cbData = ");
  107. blobin.pbData = rawData; //printf("blobin.pbData = ");
  108. description = (*env)->GetCharArrayElements(env,descriptionArray,NULL); //printf("description = ");
  109. length = (*env)->GetArrayLength(env,descriptionArray); //printf("length = ");
  110. copiedDescription = (jchar*)calloc(length+1,sizeof(jchar)); //printf("copiedDescription = ");
  111. if (!copiedDescription) {
  112. jthrowable oome;
  113. oome = (*env)->FindClass(env,"java/lang/OutOfMemoryError");
  114. if (oome) {
  115. (*env)->ThrowNew(env,oome,"it seems you are running out of memory");
  116. }
  117. FreeLibrary(dll);
  118. return NULL;
  119. } //printf("copiedDescription != NULL");
  120. lstrcpynW(copiedDescription,description,length+1); //printf("lstrcpynW(copiedDescription,description,length+1)");
  121. (*env)->ReleaseCharArrayElements(env,descriptionArray,description,0); //printf("ReleaseCharArrayElements(env,descriptionArray,description,0)");
  122. entropy = (*env)->GetByteArrayElements(env,entropyArray,NULL); //printf("entropy = ");
  123. entropyParam.cbData = (*env)->GetArrayLength(env,entropyArray); //printf("entropyParam.cbData = ");
  124. entropyParam.pbData = entropy; //printf("entropyParam.pbData = ");
  125. crypted = encrypt(&blobin,copiedDescription,&entropyParam,NULL,NULL,CRYPTPROTECT_UI_FORBIDDEN,&blobout); //printf("crypted = encrypt()");
  126. free(copiedDescription); //printf("free(copiedDescription)");
  127. (*env)->ReleaseByteArrayElements(env,rawDataArray,rawData,0); //printf("ReleaseByteArrayElements(env,rawDataArray,rawData,0)");
  128. (*env)->ReleaseByteArrayElements(env,entropyArray,entropy,0); //printf("ReleaseByteArrayElements(env,entropyArray,entropy,0)");
  129. FreeLibrary(dll); //printf("FreeLibrary(dll)");
  130. if (crypted) { //printf("crypted == TRUE");
  131. result = (*env)->NewByteArray(env,blobout.cbData); //printf("result = (*env)->NewByteArray(env,blobout.cbData)");
  132. (*env)->SetByteArrayRegion(env,result,0,blobout.cbData,blobout.pbData); //printf("SetByteArrayRegion(env,result,0,blobout.cbData,blobout.pbData)");
  133. LocalFree(blobout.pbData); //printf("LocalFree(blobout.pbData)");
  134. } else { //printf("crypted == FALSE");
  135. result = NULL; //printf("result = NULL");
  136. } //printf("return");
  137. return result;
  138. }
  139. JNIEXPORT jbyteArray JNICALL Java_wincrypt_WincryptCipher_decryptNative(JNIEnv * env,
  140. jobject jObject,
  141. jbyteArray encyrptedDataArray,
  142. jcharArray descriptionArray,
  143. jbyteArray entropyArray) {
  144. typedef BOOL (CALLBACK *decrypt_fn)
  145. (DATA_BLOB *, /* pDataIn */
  146. LPCWSTR *, /* szDataDescr */
  147. DATA_BLOB *, /* pOptionalEntropy */
  148. PVOID, /* pvReserved */
  149. CRYPTPROTECT_PROMPTSTRUCT *, /* pPromptStruct */
  150. DWORD, /* dwFlags */
  151. DATA_BLOB *); /* pDataOut */
  152. HINSTANCE dll;
  153. FARPROC fn;
  154. decrypt_fn decrypt;
  155. jbyte* encyrptedData;
  156. DATA_BLOB blobin;
  157. jchar* decryptedDescription;
  158. jbyte* entropy;
  159. DATA_BLOB entropyParam;
  160. DATA_BLOB blobout;
  161. BOOL decrypted;
  162. jchar* description;
  163. size_t length;
  164. jchar* copiedDescription;
  165. jbyteArray result; //FILE* logfile; char* temp; const char temp2[50]; char* temp3;
  166. if (!encyrptedDataArray) {
  167. jthrowable npe;
  168. npe = (*env)->FindClass(env,"java/lang/NullPointerException");
  169. if (npe) {
  170. (*env)->ThrowNew(env,npe,"encyrptedData must not be null");
  171. }
  172. return NULL;
  173. } //printf("encyrptedDataArray != null");
  174. if (!descriptionArray) {
  175. jthrowable npe;
  176. npe = (*env)->FindClass(env,"java/lang/NullPointerException");
  177. if (npe) {
  178. (*env)->ThrowNew(env,npe,"description must not be null");
  179. }
  180. return NULL;
  181. } //printf("descriptionArray != null");
  182. if (!entropyArray) {
  183. jthrowable npe;
  184. npe = (*env)->FindClass(env,"java/lang/NullPointerException");
  185. if (npe) {
  186. (*env)->ThrowNew(env,npe,"entropy must not be null");
  187. }
  188. return NULL;
  189. } //printf("entropyArray != null");
  190. if (!get_crypto_function("CryptUnprotectData",&dll,&fn)) {
  191. return NULL;
  192. } //printf("crypto_function == TRUE");
  193. decrypt = (decrypt_fn)fn; //printf("decrypt = (decrypt_fn)fn");
  194. encyrptedData = (*env)->GetByteArrayElements(env,encyrptedDataArray,NULL); //printf("encyrptedData = ");
  195. blobin.cbData = (*env)->GetArrayLength(env,encyrptedDataArray); //printf("blobin.cbData = ");
  196. blobin.pbData = encyrptedData; //printf("blobin.pbData = ");
  197. entropy = (*env)->GetByteArrayElements(env,entropyArray,NULL); //printf("entropy = ");
  198. entropyParam.cbData = (*env)->GetArrayLength(env,entropyArray); //printf("entropyParam.cbData = ");
  199. entropyParam.pbData = entropy; //printf("entropyParam.pbData = ");
  200. decrypted = decrypt(&blobin,&decryptedDescription,&entropyParam,NULL,NULL,CRYPTPROTECT_UI_FORBIDDEN,&blobout); //printf("decrypted = decrypt()");
  201. (*env)->ReleaseByteArrayElements(env,encyrptedDataArray,encyrptedData,0); //printf("ReleaseByteArrayElements(env,encyrptedDataArray,encyrptedData,0)");
  202. (*env)->ReleaseByteArrayElements(env,entropyArray,entropy,0); //printf("ReleaseByteArrayElements(env,entropyArray,entropy,0)");
  203. FreeLibrary(dll); //printf("FreeLibrary(dll)");
  204. if (decrypted) { //printf("decrypted == TRUE");
  205. description = (*env)->GetCharArrayElements(env,descriptionArray,NULL); //printf("description = ");
  206. length = (*env)->GetArrayLength(env,descriptionArray); //printf("length = ");
  207. copiedDescription = (jchar*)calloc(length+1,sizeof(jchar)); //printf("copiedDescription = ");
  208. if (!copiedDescription) {
  209. jthrowable oome;
  210. oome = (*env)->FindClass(env,"java/lang/OutOfMemoryError");
  211. if (oome) {
  212. (*env)->ThrowNew(env,oome,"it seems you are running out of memory");
  213. }
  214. free(copiedDescription);
  215. LocalFree(blobout.pbData);
  216. return NULL;
  217. } //printf("copiedDescription != NULL");
  218. lstrcpynW(copiedDescription,description,length+1); //printf("lstrcpynW(copiedDescription,description,length+1)");
  219. (*env)->ReleaseCharArrayElements(env,descriptionArray,description,0); //printf("ReleaseCharArrayElements(env,descriptionArray,description,0)");
  220. if (0 == lstrcmpW(copiedDescription,decryptedDescription)) { //printf("copiedDescription == decryptedDescription");
  221. result = (*env)->NewByteArray(env,blobout.cbData); //printf("result = (*env)->NewByteArray(env,blobout.cbData)");
  222. (*env)->SetByteArrayRegion(env,result,0,blobout.cbData,blobout.pbData); //printf("SetByteArrayRegion(env,result,0,blobout.cbData,blobout.pbData)");
  223. } else { //printf("copiedDescription != decryptedDescription");
  224. //temp = (char*)description;
  225. //temp3 = (char*)temp2;
  226. //while (*temp) {
  227. // *temp3 = *temp;
  228. // temp += 2;
  229. // temp3++;
  230. //}
  231. //*temp3 = 0;
  232. //printf(temp2);
  233. //temp = (char*)decryptedDescription;
  234. //temp3 = (char*)temp2;
  235. //while (*temp) {
  236. // *temp3 = *temp;
  237. // temp += 2;
  238. // temp3++;
  239. //}
  240. //*temp3 = 0;
  241. //printf(temp2);
  242. //#undef printf
  243. //logfile = fopen("C:\\Programme\\jEdit\\logfile.txt","a");
  244. //fprintf(logfile,"ArrayLength: %d\n",(*env)->GetArrayLength(env,descriptionArray));
  245. //temp = (char*)copiedDescription;
  246. //while (*temp || *(temp+1)) {
  247. // fprintf(logfile,"%d, %d, ",(int)*temp,(int)*(temp+1));
  248. // temp += 2;
  249. //}
  250. //fprintf(logfile,"\n");
  251. //temp = (char*)decryptedDescription;
  252. //while (*temp || *(temp+1)) {
  253. // fprintf(logfile,"%d, %d, ",(int)*temp,(int)*(temp+1));
  254. // temp += 2;
  255. //}
  256. //fprintf(logfile,"\n");
  257. //fflush(logfile);
  258. //fclose(logfile);
  259. result = NULL; //printf("result = NULL");
  260. }
  261. free(copiedDescription); //printf("free(copiedDescription)");
  262. LocalFree(blobout.pbData); //printf("LocalFree(blobout.pbData)");
  263. } else { //printf("decrypted == FALSE");
  264. result = NULL; //printf("result = NULL");
  265. } //printf("return");
  266. return result;
  267. }
  268. JNIEXPORT jboolean JNICALL Java_wincrypt_WincryptCipher_isNativeAvailable(JNIEnv * env,
  269. jobject jObject) {
  270. return get_crypto_dll(NULL);
  271. }