PageRenderTime 43ms CodeModel.GetById 15ms app.highlight 23ms RepoModel.GetById 1ms 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
  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
 22#include "WincryptCipher.h"

 23#include <wtypes.h>

 24#include <wincrypt.h>

 25
 26static jboolean get_crypto_dll(HINSTANCE* pdll) {
 27    HINSTANCE dll = LoadLibraryA("Crypt32.dll");
 28    if (dll) {
 29        if (NULL != pdll) {
 30            *pdll = dll;
 31        } else {
 32            FreeLibrary(dll);
 33        }
 34        return JNI_TRUE;
 35    }
 36    return JNI_FALSE;
 37}
 38
 39static jboolean get_crypto_function(const char* name, HINSTANCE* pdll, FARPROC* pfn) {
 40    HINSTANCE dll;
 41    if (get_crypto_dll(&dll)) {
 42        FARPROC fn = GetProcAddress(dll,name);
 43        if (fn) {
 44            *pdll = dll;
 45            *pfn = fn;
 46            return JNI_TRUE;
 47        }
 48        FreeLibrary(dll);
 49    }
 50    return JNI_FALSE;
 51}
 52
 53JNIEXPORT jbyteArray JNICALL Java_wincrypt_WincryptCipher_encryptNative(JNIEnv * env,
 54                                                                        jobject jObject,
 55                                                                        jbyteArray rawDataArray,
 56                                                                        jcharArray descriptionArray,
 57                                                                        jbyteArray entropyArray) {
 58    typedef BOOL (CALLBACK *encrypt_fn)
 59        (DATA_BLOB *,                 /* pDataIn */
 60         LPCWSTR,                     /* szDataDescr */
 61         DATA_BLOB *,                 /* pOptionalEntropy */
 62         PVOID,                       /* pvReserved */
 63         CRYPTPROTECT_PROMPTSTRUCT *, /* pPromptStruct */
 64         DWORD,                       /* dwFlags */
 65         DATA_BLOB *);                /* pDataOut */
 66    
 67    HINSTANCE dll;
 68    FARPROC fn;
 69    encrypt_fn encrypt;
 70    jbyte* rawData;
 71    DATA_BLOB blobin;
 72    jchar* description;
 73    size_t length;
 74    jchar* copiedDescription;
 75    jbyte* entropy;
 76    DATA_BLOB entropyParam;
 77    DATA_BLOB blobout;
 78    BOOL crypted;
 79    jbyteArray result;                                                                                       //FILE* logfile;
 80    
 81    if (!rawDataArray) {
 82        jthrowable npe;
 83        npe = (*env)->FindClass(env,"java/lang/NullPointerException");
 84        if (npe) {
 85            (*env)->ThrowNew(env,npe,"rawData must not be null");
 86        }
 87        return NULL;
 88    }
 89//#define printf(string) logfile = fopen("C:\\Programme\\jEdit\\logfile.txt","a"); fprintf(logfile,string); fprintf(logfile,"\n"); fflush(logfile); fclose(logfile)
 90                                                                                                             //printf("rawDataArray != null");
 91    
 92    if (!descriptionArray) {
 93        jthrowable npe;
 94        npe = (*env)->FindClass(env,"java/lang/NullPointerException");
 95        if (npe) {
 96            (*env)->ThrowNew(env,npe,"description must not be null");
 97        }
 98        return NULL;
 99    }                                                                                                        //printf("descriptionArray != null");
100    
101    if (!entropyArray) {
102        jthrowable npe;
103        npe = (*env)->FindClass(env,"java/lang/NullPointerException");
104        if (npe) {
105            (*env)->ThrowNew(env,npe,"entropy must not be null");
106        }
107        return NULL;
108    }                                                                                                        //printf("entropyArray != null");
109    
110    if (!get_crypto_function("CryptProtectData",&dll,&fn)) {
111        return NULL;
112    }                                                                                                        //printf("crypto_function == TRUE");
113    encrypt = (encrypt_fn)fn;                                                                                //printf("encrypt = (encrypt_fn)fn");
114    rawData = (*env)->GetByteArrayElements(env,rawDataArray,NULL);                                           //printf("rawData = ");
115    blobin.cbData = (*env)->GetArrayLength(env,rawDataArray);                                                //printf("blobin.cbData = ");
116    blobin.pbData = rawData;                                                                                 //printf("blobin.pbData = ");
117    description = (*env)->GetCharArrayElements(env,descriptionArray,NULL);                                   //printf("description = ");
118    length = (*env)->GetArrayLength(env,descriptionArray);                                                   //printf("length = ");
119    copiedDescription = (jchar*)calloc(length+1,sizeof(jchar));                                              //printf("copiedDescription = ");
120    if (!copiedDescription) {
121        jthrowable oome;
122        oome = (*env)->FindClass(env,"java/lang/OutOfMemoryError");
123        if (oome) {
124            (*env)->ThrowNew(env,oome,"it seems you are running out of memory");
125        }
126        FreeLibrary(dll);
127        return NULL;
128    }                                                                                                        //printf("copiedDescription != NULL");
129    lstrcpynW(copiedDescription,description,length+1);                                                       //printf("lstrcpynW(copiedDescription,description,length+1)");
130    (*env)->ReleaseCharArrayElements(env,descriptionArray,description,0);                                    //printf("ReleaseCharArrayElements(env,descriptionArray,description,0)");
131    entropy = (*env)->GetByteArrayElements(env,entropyArray,NULL);                                           //printf("entropy = ");
132    entropyParam.cbData = (*env)->GetArrayLength(env,entropyArray);                                          //printf("entropyParam.cbData = ");
133    entropyParam.pbData = entropy;                                                                           //printf("entropyParam.pbData = ");
134    crypted = encrypt(&blobin,copiedDescription,&entropyParam,NULL,NULL,CRYPTPROTECT_UI_FORBIDDEN,&blobout); //printf("crypted = encrypt()");
135    free(copiedDescription);                                                                                 //printf("free(copiedDescription)");
136    (*env)->ReleaseByteArrayElements(env,rawDataArray,rawData,0);                                            //printf("ReleaseByteArrayElements(env,rawDataArray,rawData,0)");
137    (*env)->ReleaseByteArrayElements(env,entropyArray,entropy,0);                                            //printf("ReleaseByteArrayElements(env,entropyArray,entropy,0)");
138    FreeLibrary(dll);                                                                                        //printf("FreeLibrary(dll)");
139    if (crypted) {                                                                                           //printf("crypted == TRUE");
140        result = (*env)->NewByteArray(env,blobout.cbData);                                                   //printf("result = (*env)->NewByteArray(env,blobout.cbData)");
141        (*env)->SetByteArrayRegion(env,result,0,blobout.cbData,blobout.pbData);                              //printf("SetByteArrayRegion(env,result,0,blobout.cbData,blobout.pbData)");
142        LocalFree(blobout.pbData);                                                                           //printf("LocalFree(blobout.pbData)");
143    } else {                                                                                                 //printf("crypted == FALSE");
144        result = NULL;                                                                                       //printf("result = NULL");
145    }                                                                                                        //printf("return");
146    return result;
147}
148
149JNIEXPORT jbyteArray JNICALL Java_wincrypt_WincryptCipher_decryptNative(JNIEnv * env,
150                                                                        jobject jObject,
151                                                                        jbyteArray encyrptedDataArray,
152                                                                        jcharArray descriptionArray,
153                                                                        jbyteArray entropyArray) {
154    typedef BOOL (CALLBACK *decrypt_fn)
155        (DATA_BLOB *,                 /* pDataIn */
156         LPCWSTR *,                   /* szDataDescr */
157         DATA_BLOB *,                 /* pOptionalEntropy */
158         PVOID,                       /* pvReserved */
159         CRYPTPROTECT_PROMPTSTRUCT *, /* pPromptStruct */
160         DWORD,                       /* dwFlags */
161         DATA_BLOB *);                /* pDataOut */
162    
163    HINSTANCE dll;
164    FARPROC fn;
165    decrypt_fn decrypt;
166    jbyte* encyrptedData;
167    DATA_BLOB blobin;
168    jchar* decryptedDescription;
169    jbyte* entropy;
170    DATA_BLOB entropyParam;
171    DATA_BLOB blobout;
172    BOOL decrypted;
173    jchar* description;
174    size_t length;
175    jchar* copiedDescription;
176    jbyteArray result;                                                                                             //FILE* logfile; char* temp; const char temp2[50]; char* temp3;
177    
178    if (!encyrptedDataArray) {
179        jthrowable npe;
180        npe = (*env)->FindClass(env,"java/lang/NullPointerException");
181        if (npe) {
182            (*env)->ThrowNew(env,npe,"encyrptedData must not be null");
183        }
184        return NULL;
185    }                                                                                                              //printf("encyrptedDataArray != null");
186    
187    if (!descriptionArray) {
188        jthrowable npe;
189        npe = (*env)->FindClass(env,"java/lang/NullPointerException");
190        if (npe) {
191            (*env)->ThrowNew(env,npe,"description must not be null");
192        }
193        return NULL;
194    }                                                                                                              //printf("descriptionArray != null");
195    
196    if (!entropyArray) {
197        jthrowable npe;
198        npe = (*env)->FindClass(env,"java/lang/NullPointerException");
199        if (npe) {
200            (*env)->ThrowNew(env,npe,"entropy must not be null");
201        }
202        return NULL;
203    }                                                                                                              //printf("entropyArray != null");
204    
205    if (!get_crypto_function("CryptUnprotectData",&dll,&fn)) {
206        return NULL;
207    }                                                                                                              //printf("crypto_function == TRUE");
208    decrypt = (decrypt_fn)fn;                                                                                      //printf("decrypt = (decrypt_fn)fn");
209    encyrptedData = (*env)->GetByteArrayElements(env,encyrptedDataArray,NULL);                                     //printf("encyrptedData = ");
210    blobin.cbData = (*env)->GetArrayLength(env,encyrptedDataArray);                                                //printf("blobin.cbData = ");
211    blobin.pbData = encyrptedData;                                                                                 //printf("blobin.pbData = ");
212    entropy = (*env)->GetByteArrayElements(env,entropyArray,NULL);                                                 //printf("entropy = ");
213    entropyParam.cbData = (*env)->GetArrayLength(env,entropyArray);                                                //printf("entropyParam.cbData = ");
214    entropyParam.pbData = entropy;                                                                                 //printf("entropyParam.pbData = ");
215    decrypted = decrypt(&blobin,&decryptedDescription,&entropyParam,NULL,NULL,CRYPTPROTECT_UI_FORBIDDEN,&blobout); //printf("decrypted = decrypt()");
216    (*env)->ReleaseByteArrayElements(env,encyrptedDataArray,encyrptedData,0);                                      //printf("ReleaseByteArrayElements(env,encyrptedDataArray,encyrptedData,0)");
217    (*env)->ReleaseByteArrayElements(env,entropyArray,entropy,0);                                                  //printf("ReleaseByteArrayElements(env,entropyArray,entropy,0)");
218    FreeLibrary(dll);                                                                                              //printf("FreeLibrary(dll)");
219    if (decrypted) {                                                                                               //printf("decrypted == TRUE");
220        description = (*env)->GetCharArrayElements(env,descriptionArray,NULL);                                     //printf("description = ");
221        length = (*env)->GetArrayLength(env,descriptionArray);                                                     //printf("length = ");
222        copiedDescription = (jchar*)calloc(length+1,sizeof(jchar));                                                //printf("copiedDescription = ");
223        if (!copiedDescription) {
224            jthrowable oome;
225            oome = (*env)->FindClass(env,"java/lang/OutOfMemoryError");
226            if (oome) {
227                (*env)->ThrowNew(env,oome,"it seems you are running out of memory");
228            }
229            free(copiedDescription);
230            LocalFree(blobout.pbData);
231            return NULL;
232        }                                                                                                          //printf("copiedDescription != NULL");
233        lstrcpynW(copiedDescription,description,length+1);                                                         //printf("lstrcpynW(copiedDescription,description,length+1)");
234        (*env)->ReleaseCharArrayElements(env,descriptionArray,description,0);                                      //printf("ReleaseCharArrayElements(env,descriptionArray,description,0)");
235        if (0 == lstrcmpW(copiedDescription,decryptedDescription)) {                                               //printf("copiedDescription == decryptedDescription");
236            result = (*env)->NewByteArray(env,blobout.cbData);                                                     //printf("result = (*env)->NewByteArray(env,blobout.cbData)");
237            (*env)->SetByteArrayRegion(env,result,0,blobout.cbData,blobout.pbData);                                //printf("SetByteArrayRegion(env,result,0,blobout.cbData,blobout.pbData)");
238        } else {                                                                                                   //printf("copiedDescription != decryptedDescription");
239                                                                                                                   //temp = (char*)description;
240                                                                                                                   //temp3 = (char*)temp2;
241                                                                                                                   //while (*temp) {
242                                                                                                                   //    *temp3 = *temp;
243                                                                                                                   //    temp += 2;
244                                                                                                                   //    temp3++;
245                                                                                                                   //}
246                                                                                                                   //*temp3 = 0;
247                                                                                                                   //printf(temp2);
248                                                                                                                   //temp = (char*)decryptedDescription;
249                                                                                                                   //temp3 = (char*)temp2;
250                                                                                                                   //while (*temp) {
251                                                                                                                   //    *temp3 = *temp;
252                                                                                                                   //    temp += 2;
253                                                                                                                   //    temp3++;
254                                                                                                                   //}
255                                                                                                                   //*temp3 = 0;
256                                                                                                                   //printf(temp2);
257                                                                                                                   //#undef printf
258                                                                                                                   //logfile = fopen("C:\\Programme\\jEdit\\logfile.txt","a");
259                                                                                                                   //fprintf(logfile,"ArrayLength: %d\n",(*env)->GetArrayLength(env,descriptionArray));
260                                                                                                                   //temp = (char*)copiedDescription;
261                                                                                                                   //while (*temp || *(temp+1)) {
262                                                                                                                   //    fprintf(logfile,"%d, %d, ",(int)*temp,(int)*(temp+1));
263                                                                                                                   //    temp += 2;
264                                                                                                                   //}
265                                                                                                                   //fprintf(logfile,"\n");
266                                                                                                                   //temp = (char*)decryptedDescription;
267                                                                                                                   //while (*temp || *(temp+1)) {
268                                                                                                                   //    fprintf(logfile,"%d, %d, ",(int)*temp,(int)*(temp+1));
269                                                                                                                   //    temp += 2;
270                                                                                                                   //}
271                                                                                                                   //fprintf(logfile,"\n");
272                                                                                                                   //fflush(logfile);
273                                                                                                                   //fclose(logfile);
274            result = NULL;                                                                                         //printf("result = NULL");
275        }
276        free(copiedDescription);                                                                                   //printf("free(copiedDescription)");
277        LocalFree(blobout.pbData);                                                                                 //printf("LocalFree(blobout.pbData)");
278    } else {                                                                                                       //printf("decrypted == FALSE");
279        result = NULL;                                                                                             //printf("result = NULL");
280    }                                                                                                              //printf("return");
281    return result;
282}
283
284JNIEXPORT jboolean JNICALL Java_wincrypt_WincryptCipher_isNativeAvailable(JNIEnv * env,
285                                                                          jobject jObject) {
286    return get_crypto_dll(NULL);
287}