Merge branch 'master' into extended-easysetup
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniSecureUtils.cpp
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2015 Samsung Electronics All Rights Reserved.
5 * //
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 * //
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
11 * //
12 * //      http://www.apache.org/licenses/LICENSE-2.0
13 * //
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
19 * //
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 */
22
23 #include "JniSecureUtils.h"
24 #include "JniOcSecureResource.h"
25 #include "srmutility.h"
26 #include "base64.h"
27
28 jobject JniSecureUtils::convertProvisionresultVectorToJavaList(JNIEnv *env, const OC::PMResultList_t *result)
29 {
30     jobject jResultList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
31     if (!jResultList)
32     {
33         return nullptr;
34     }
35
36     for (size_t i = 0; i < result->size(); ++i)
37     {
38         jstring jStr = env->NewStringUTF((convertUUIDtoStr(result->at(i).deviceId).c_str()));
39         if (!jStr)
40         {
41             return nullptr;
42         }
43         jobject jresult = env->NewObject(
44                 g_cls_OcProvisionResult,
45                 g_mid_OcProvisionResult_ctor,
46                 jStr,
47                 static_cast<jint>(result->at(i).res)
48                 );
49         if (!jresult)
50         {
51             return nullptr;
52         }
53
54         env->CallBooleanMethod(jResultList, g_mid_LinkedList_add_object, jresult);
55         if (env->ExceptionCheck())
56         {
57             return nullptr;
58         }
59         env->DeleteLocalRef(jresult);
60         env->DeleteLocalRef(jStr);
61     }
62     return jResultList;
63 }
64
65 jobjectArray JniSecureUtils::convertDeviceVectorToJavaArray(JNIEnv *env,
66     std::vector<std::shared_ptr<OC::OCSecureResource>> &deviceListVector)
67 {
68     jsize len = static_cast<jsize>(deviceListVector.size());
69     jobjectArray devArr = env->NewObjectArray(len, g_cls_OcSecureResource, NULL);
70
71     if (!devArr)
72     {
73         return nullptr;
74     }
75
76     for (jsize i = 0; i < len; ++i)
77     {
78         JniOcSecureResource *device = new JniOcSecureResource(deviceListVector[i]);
79         jobject jDevice = env->NewObject(g_cls_OcSecureResource, g_mid_OcSecureResource_ctor);
80
81         SetHandle<JniOcSecureResource>(env, jDevice, device);
82         if (!jDevice)
83         {
84             return nullptr;
85         }
86
87         env->SetObjectArrayElement(devArr, i, jDevice);
88         if (env->ExceptionCheck())
89         {
90             return nullptr;
91         }
92         env->DeleteLocalRef(jDevice);
93     }
94     return devArr;
95 }
96
97 std::string JniSecureUtils::convertUUIDtoStr(OicUuid_t uuid)
98 {
99     std::ostringstream deviceId("");
100     char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*)0)->id)) + 1] = {0,};
101     uint32_t outLen = 0;
102     B64Result b64Ret = B64_OK;
103
104     b64Ret = b64Encode(uuid.id, sizeof(uuid.id), base64Buff,
105             sizeof(base64Buff), &outLen);
106
107     deviceId << base64Buff;
108     return deviceId.str();
109 }
110
111 jobject JniSecureUtils::convertUUIDVectorToJavaStrList(JNIEnv *env, UuidList_t &vector)
112 {
113     jobject jList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
114     if (!jList)
115     {
116         return nullptr;
117     }
118     for (size_t i = 0; i < vector.size(); ++i)
119     {
120         jstring jStr = env->NewStringUTF((convertUUIDtoStr(vector[i])).c_str());
121         if (!jStr)
122         {
123             return nullptr;
124         }
125         env->CallBooleanMethod(jList, g_mid_LinkedList_add_object, jStr);
126         if (env->ExceptionCheck())
127         {
128             return nullptr;
129         }
130         env->DeleteLocalRef(jStr);
131     }
132     return jList;
133 }
134
135 OCStackResult JniSecureUtils::convertJavaACLToOCAcl(JNIEnv *env, jobject in, OicSecAcl_t *acl)
136 {
137 /*
138     jstring jData;
139     jvalue args[1];
140
141     jData = (jstring) env->CallObjectMethod(in, g_mid_OcOicSecAcl_get_subject);
142     if (!jData || env->ExceptionCheck())
143     {
144         return OC_STACK_ERROR;
145     }
146
147     char *str = (char*) env->GetStringUTFChars(jData, 0);
148     if (OC_STACK_OK == ConvertStrToUuid(str, &acl->subject))
149     {
150         env->ReleaseStringUTFChars(jData, str);
151     }
152     else
153     {
154         return OC_STACK_ERROR;
155     }
156
157     jint jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_resources_cnt);
158     if (!jCount || env->ExceptionCheck())
159     {
160         return OC_STACK_ERROR;
161     }
162
163     acl->resourcesLen = jCount;
164     acl->resources = new char*[jCount];
165     for (jint i = 0; i < jCount; ++i)
166     {
167         args[0].i = i;
168         jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_resources, args);
169         if (!jData || env->ExceptionCheck())
170         {
171             return OC_STACK_ERROR;
172         }
173
174         acl->resources[i] = (char*) env->GetStringUTFChars(jData, 0);
175     }
176
177     jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_permission);
178     if (env->ExceptionCheck())
179     {
180         return OC_STACK_ERROR;
181     }
182
183     acl->permission = jCount;
184     jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_periods_cnt);
185     if (env->ExceptionCheck())
186     {
187         return OC_STACK_ERROR;
188     }
189
190     acl->prdRecrLen = jCount;
191     acl->periods = new char*[jCount];
192     for (jint i = 0; i < jCount; ++i)
193     {
194         args[0].i = i;
195         jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_periods, args);
196         if (!jData || env->ExceptionCheck())
197         {
198             return OC_STACK_ERROR;
199         }
200
201         acl->periods[i] = (char*) env->GetStringUTFChars(jData, 0);
202     }
203
204     acl->recurrences = new char*[jCount]; //TODO:Period Len and Reccurence len is same
205     for (jint i = 0; i < jCount; ++i)
206     {
207         args[0].i = i;
208         jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_recurrences, args);
209         if (!jData ||  env->ExceptionCheck())
210         {
211             return OC_STACK_ERROR;
212         }
213
214         acl->recurrences[i] = (char*) env->GetStringUTFChars(jData, 0);
215     }
216
217     jData = (jstring) env->CallObjectMethod(in, g_mid_OcOicSecAcl_get_rownerID);
218     if (!jData || env->ExceptionCheck())
219     {
220         return OC_STACK_ERROR;
221     }
222
223     str = (char*) env->GetStringUTFChars(jData, 0);
224
225     if (OC_STACK_OK == ConvertStrToUuid(str, &acl->rownerID))
226     {
227         env->ReleaseStringUTFChars(jData, str);
228     }
229     else
230     {
231         return OC_STACK_ERROR;
232     }
233 */
234     return OC_STACK_OK;
235 }
236
237 OCStackResult JniSecureUtils::convertJavaPdACLToOCAcl(JNIEnv *env, jobject in, OicSecPdAcl_t *pdacl)
238 {
239     jstring jData;
240     jvalue args[1];
241
242     jint jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecPdAcl_get_resources_cnt);
243     if (!jCount || env->ExceptionCheck())
244     {
245         return OC_STACK_ERROR;
246     }
247
248     pdacl->resourcesLen = jCount;
249     pdacl->resources = new char*[jCount];
250
251     if (!pdacl->resources)
252     {
253         return OC_STACK_ERROR;
254     }
255     for (jint i = 0; i < jCount; ++i)
256     {
257         args[0].i = i;
258         jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecPdAcl_get_resources, args);
259         if (!jData || env->ExceptionCheck())
260         {
261             return OC_STACK_ERROR;
262         }
263
264         pdacl->resources[i] = (char*) env->GetStringUTFChars(jData, 0);
265     }
266
267     jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecPdAcl_get_permission);
268     if (env->ExceptionCheck())
269     {
270         return OC_STACK_ERROR;
271     }
272
273     pdacl->permission = jCount;
274     jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecPdAcl_get_periods_cnt);
275     if (env->ExceptionCheck())
276     {
277         return OC_STACK_ERROR;
278     }
279
280     pdacl->prdRecrLen = jCount;
281     if (jCount)
282     {
283         pdacl->periods = new char*[jCount];
284         if (!pdacl->periods)
285         {
286             return OC_STACK_ERROR;
287         }
288     }
289     for (jint i = 0; i < jCount; ++i)
290     {
291         args[0].i = i;
292         jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecPdAcl_get_periods, args);
293         if (!jData || env->ExceptionCheck())
294         {
295             return OC_STACK_ERROR;
296         }
297
298         pdacl->periods[i] = (char*) env->GetStringUTFChars(jData, 0);
299     }
300
301     if (jCount)
302     {
303         pdacl->recurrences = new char*[jCount];
304         if (!pdacl->recurrences)
305         {
306             return OC_STACK_ERROR;
307         }
308     }
309     for (jint i = 0; i < jCount; ++i)
310     {
311         args[0].i = i;
312         jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecPdAcl_get_recurrences, args);
313         if (!jData ||  env->ExceptionCheck())
314         {
315             return OC_STACK_ERROR;
316         }
317
318         pdacl->recurrences[i] = (char*) env->GetStringUTFChars(jData, 0);
319     }
320     return OC_STACK_OK;
321 }