forked from nordic-institute/X-Road
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ee_ria_xroad_common_util_PasswordStore.c
197 lines (162 loc) · 5.69 KB
/
ee_ria_xroad_common_util_PasswordStore.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* The MIT License
* Copyright (c) 2015 Estonian Information System Authority (RIA), Population Register Centre (VRK)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "ee_ria_xroad_common_util_PasswordStore.h"
#include <stdlib.h>
#include <string.h>
#include "passwordstore.h"
JNIEXPORT jbyteArray JNICALL
Java_ee_ria_xroad_common_util_PasswordStore_read(JNIEnv *env, jclass jc,
jstring j_pathname_for_ftok, jstring j_id)
{
(void)jc;
jbyteArray res = NULL;
int error_code;
signed char *ret_buf;
const char *pathname_for_ftok = NULL;
const char *id = NULL;
char *ret_array = NULL;
int ret_len = 0;
if (j_pathname_for_ftok != NULL) {
pathname_for_ftok = (*env)->GetStringUTFChars(
env, j_pathname_for_ftok, NULL);
}
if (j_id != NULL) {
id = (*env)->GetStringUTFChars(env, j_id, NULL);
}
error_code = LEGACY_passwordRead(pathname_for_ftok, id,
&ret_array, &ret_len);
if (error_code != 0) {
goto error;
}
if (ret_array == NULL) {
res = NULL;
} else {
// Create new Java byte array with the correct length
res = (*env)->NewByteArray(env, ret_len);
if (res == NULL) {
// Out of memory.
error_code = MALLOC_FAILURE;
free(ret_array);
goto error;
}
ret_buf = (*env)->GetByteArrayElements(env, res, NULL);
// Copy answer to Java array.
memcpy(ret_buf, ret_array, ret_len);
(*env)->ReleaseByteArrayElements(env, res, ret_buf, 0);
}
error:
if (j_pathname_for_ftok != NULL) {
(*env)->ReleaseStringUTFChars(env, j_pathname_for_ftok,
pathname_for_ftok);
}
if (j_id != NULL) {
(*env)->ReleaseStringUTFChars(env, j_id, id);
}
free(ret_array);
if (error_code == 0) {
return res;
} else {
(*env)->ThrowNew(env,
(*env)->FindClass(env, "java/lang/RuntimeException"),
LEGACY_strError(error_code));
}
return res; // For suppress warning.
}
JNIEXPORT void JNICALL
Java_ee_ria_xroad_common_util_PasswordStore_write(JNIEnv *env, jclass jc,
jstring j_pathname_for_ftok, jstring j_id,
jbyteArray j_password, jint permissions)
{
(void)jc;
int error_code;
const char *pathname_for_ftok = NULL;
const char *id = NULL;
signed char *password = NULL;
int password_len = 0;
if (j_pathname_for_ftok != NULL) {
pathname_for_ftok = (*env)->GetStringUTFChars(
env, j_pathname_for_ftok, NULL);
}
if (j_id != NULL) {
id = (*env)->GetStringUTFChars(env, j_id, NULL);
}
if (j_password != NULL) {
password = (*env)->GetByteArrayElements(env, j_password, NULL);
password_len = (*env)->GetArrayLength(env, j_password);
}
error_code = LEGACY_passwordWrite(pathname_for_ftok, id,
(char *) password, password_len, permissions);
if (error_code != 0) {
goto error;
}
if (j_id != NULL) {
(*env)->ReleaseStringUTFChars(env, j_id, id);
}
if (j_pathname_for_ftok != NULL) {
(*env)->ReleaseStringUTFChars(env, j_pathname_for_ftok, pathname_for_ftok);
}
if (j_password != NULL) {
(*env)->ReleaseByteArrayElements(env, j_password, password, 0);
}
return;
error:
if (j_id != NULL) {
(*env)->ReleaseStringUTFChars(env, j_id, id);
}
if (j_pathname_for_ftok != NULL) {
(*env)->ReleaseStringUTFChars(env, j_pathname_for_ftok, pathname_for_ftok);
}
if (j_password != NULL) {
(*env)->ReleaseByteArrayElements(env, j_password, password, 0);
}
(*env)->ThrowNew(env,
(*env)->FindClass(env, "java/lang/RuntimeException"),
LEGACY_strError(error_code));
}
JNIEXPORT void JNICALL
Java_ee_ria_xroad_common_util_PasswordStore_clear(JNIEnv *env, jclass jc,
jstring j_pathname_for_ftok, jint permissions)
{
(void)jc;
int error_code;
const char *pathname_for_ftok = NULL;
if (j_pathname_for_ftok != NULL) {
pathname_for_ftok = (*env)->GetStringUTFChars(
env, j_pathname_for_ftok, NULL);
}
error_code = LEGACY_passwordClear(pathname_for_ftok, permissions);
if (error_code != 0) {
goto error;
}
if (j_pathname_for_ftok != NULL) {
(*env)->ReleaseStringUTFChars(env, j_pathname_for_ftok, pathname_for_ftok);
}
return;
error:
if (j_pathname_for_ftok != NULL) {
(*env)->ReleaseStringUTFChars(env, j_pathname_for_ftok, pathname_for_ftok);
}
(*env)->ThrowNew(env,
(*env)->FindClass(env, "java/lang/RuntimeException"),
LEGACY_strError(error_code));
}