blob: d1ad79c23040d556653bb290161848514c17cff8 [file] [log] [blame]
Matthew Flaherty760e5f32008-02-28 18:04:34 +00001/*******************************************************************************
2 * Copyright (c) 2007, 2008 IBM Corporation and others.
Lars Vogela05e8872018-08-21 17:28:48 +02003 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
Matthew Flaherty760e5f32008-02-28 18:04:34 +00006 * which accompanies this distribution, and is available at
Lars Vogela05e8872018-08-21 17:28:48 +02007 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
Matthew Flaherty760e5f32008-02-28 18:04:34 +000010 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14
15#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
16
17#include <windows.h>
18#include <wincrypt.h>
19#include "jnicrypt.h"
20
21BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
22{
23 return TRUE;
24}
25
26JNIEXPORT jbyteArray JNICALL Java_org_eclipse_equinox_internal_security_win32_WinCrypto_winencrypt
27 (JNIEnv *env, jobject obj, jbyteArray value)
28{
29 jsize size = env->GetArrayLength(value);
30 jbyte *body = env->GetByteArrayElements(value, NULL);
31 if (body == NULL)
32 return NULL;
33
34 DATA_BLOB clearText;
35 DATA_BLOB encryptedText;
36 clearText.pbData = (BYTE*) body;
37 clearText.cbData = (DWORD) size;
38
39 BOOL result = CryptProtectData(&clearText, L"Equinox", NULL, NULL, NULL, 0, &encryptedText);
40
41 // release memory allocated by Java environment
42 env->ReleaseByteArrayElements(value, body, 0);
43
44 if (result == FALSE)
45 return NULL;
46
47 jbyteArray returnArray = env->NewByteArray(encryptedText.cbData);
48 env->SetByteArrayRegion(returnArray, 0, encryptedText.cbData, (jbyte*) encryptedText.pbData);
49 LocalFree(encryptedText.pbData); // no need any more, have Java representation
50
51 return returnArray;
52}
53
54JNIEXPORT jbyteArray JNICALL Java_org_eclipse_equinox_internal_security_win32_WinCrypto_windecrypt
55 (JNIEnv *env, jobject obj, jbyteArray value)
56{
57 jsize size = env->GetArrayLength(value);
58 jbyte *body = env->GetByteArrayElements(value, NULL);
59 if (body == NULL)
60 return NULL;
61
62 DATA_BLOB clearText;
63 DATA_BLOB encryptedText;
64 encryptedText.pbData = (BYTE*) body;
65 encryptedText.cbData = (DWORD) size;
66
67 LPWSTR pDescrOut = NULL;
68 BOOL result = CryptUnprotectData(&encryptedText, &pDescrOut, NULL, NULL, NULL, 0, &clearText);
69
70 if (pDescrOut != NULL)
71 LocalFree(pDescrOut);
72
73 // release memory allocated by Java environment
74 env->ReleaseByteArrayElements(value, body, 0);
75
76 if (result == FALSE)
77 return NULL;
78
79 jbyteArray returnArray = env->NewByteArray(clearText.cbData);
80 env->SetByteArrayRegion(returnArray, 0, clearText.cbData, (jbyte*) clearText.pbData);
81 LocalFree(clearText.pbData); // no need any more, have Java representation
82
83 return returnArray;
84}