Imported Upstream version 1.1.0
[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     jstring jData;
138     jvalue args[1];
139
140     jData = (jstring) env->CallObjectMethod(in, g_mid_OcOicSecAcl_get_subject);
141     if (!jData || env->ExceptionCheck())
142     {
143         return OC_STACK_ERROR;
144     }
145
146     char *str = (char*) env->GetStringUTFChars(jData, 0);
147     if (OC_STACK_OK == ConvertStrToUuid(str, &acl->subject))
148     {
149         env->ReleaseStringUTFChars(jData, str);
150     }
151     else
152     {
153         return OC_STACK_ERROR;
154     }
155
156     jint jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_resources_cnt);
157     if (!jCount || env->ExceptionCheck())
158     {
159         return OC_STACK_ERROR;
160     }
161
162     acl->resourcesLen = jCount;
163     acl->resources = new char*[jCount];
164     for (jint i = 0; i < jCount; ++i)
165     {
166         args[0].i = i;
167         jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_resources, args);
168         if (!jData || env->ExceptionCheck())
169         {
170             return OC_STACK_ERROR;
171         }
172
173         acl->resources[i] = (char*) env->GetStringUTFChars(jData, 0);
174     }
175
176     jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_permission);
177     if (env->ExceptionCheck())
178     {
179         return OC_STACK_ERROR;
180     }
181
182     acl->permission = jCount;
183     jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_periods_cnt);
184     if (env->ExceptionCheck())
185     {
186         return OC_STACK_ERROR;
187     }
188
189     acl->prdRecrLen = jCount;
190     acl->periods = new char*[jCount];
191     for (jint i = 0; i < jCount; ++i)
192     {
193         args[0].i = i;
194         jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_periods, args);
195         if (!jData || env->ExceptionCheck())
196         {
197             return OC_STACK_ERROR;
198         }
199
200         acl->periods[i] = (char*) env->GetStringUTFChars(jData, 0);
201     }
202
203     acl->recurrences = new char*[jCount]; //TODO:Period Len and Reccurence len is same
204     for (jint i = 0; i < jCount; ++i)
205     {
206         args[0].i = i;
207         jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_recurrences, args);
208         if (!jData ||  env->ExceptionCheck())
209         {
210             return OC_STACK_ERROR;
211         }
212
213         acl->recurrences[i] = (char*) env->GetStringUTFChars(jData, 0);
214     }
215
216     jData = (jstring) env->CallObjectMethod(in, g_mid_OcOicSecAcl_get_rownerID);
217     if (!jData || env->ExceptionCheck())
218     {
219         return OC_STACK_ERROR;
220     }
221
222     str = (char*) env->GetStringUTFChars(jData, 0);
223
224     if (OC_STACK_OK == ConvertStrToUuid(str, &acl->rownerID))
225     {
226         env->ReleaseStringUTFChars(jData, str);
227     }
228     else
229     {
230         return OC_STACK_ERROR;
231     }
232
233     return OC_STACK_OK;
234 }