[IOT-1089] Change Android build system to accomodate both Android and Generic Java...
[contrib/iotivity.git] / java / 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 "base64.h"
26
27 jobject JniSecureUtils::convertProvisionresultVectorToJavaList(JNIEnv *env, const OC::PMResultList_t *result)
28 {
29     jobject jResultList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
30     if (!jResultList)
31     {
32         return nullptr;
33     }
34
35     for (size_t i = 0; i < result->size(); ++i)
36     {
37         jstring jStr = env->NewStringUTF((convertUUIDtoStr(result->at(i).deviceId).c_str()));
38         if (!jStr)
39         {
40             return nullptr;
41         }
42         jobject jresult = env->NewObject(
43                 g_cls_OcProvisionResult,
44                 g_mid_OcProvisionResult_ctor,
45                 jStr,
46                 static_cast<jint>(result->at(i).res)
47                 );
48         if (!jresult)
49         {
50             return nullptr;
51         }
52
53         env->CallBooleanMethod(jResultList, g_mid_LinkedList_add_object, jresult);
54         if (env->ExceptionCheck())
55         {
56             return nullptr;
57         }
58         env->DeleteLocalRef(jresult);
59         env->DeleteLocalRef(jStr);
60     }
61     return jResultList;
62 }
63
64 jobjectArray JniSecureUtils::convertDeviceVectorToJavaArray(JNIEnv *env,
65     std::vector<std::shared_ptr<OC::OCSecureResource>> &deviceListVector)
66 {
67     jsize len = static_cast<jsize>(deviceListVector.size());
68     jobjectArray devArr = env->NewObjectArray(len, g_cls_OcSecureResource, NULL);
69
70     if (!devArr)
71     {
72         return nullptr;
73     }
74
75     for (jsize i = 0; i < len; ++i)
76     {
77         JniOcSecureResource *device = new JniOcSecureResource(deviceListVector[i]);
78         jobject jDevice = env->NewObject(g_cls_OcSecureResource, g_mid_OcSecureResource_ctor);
79
80         SetHandle<JniOcSecureResource>(env, jDevice, device);
81         if (!jDevice)
82         {
83             return nullptr;
84         }
85
86         env->SetObjectArrayElement(devArr, i, jDevice);
87         if (env->ExceptionCheck())
88         {
89             return nullptr;
90         }
91         env->DeleteLocalRef(jDevice);
92     }
93     return devArr;
94 }
95
96 std::string JniSecureUtils::convertUUIDtoStr(OicUuid_t uuid)
97 {
98     std::ostringstream deviceId("");
99     char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*)0)->id)) + 1] = {0,};
100     uint32_t outLen = 0;
101     B64Result b64Ret = B64_OK;
102
103     b64Ret = b64Encode(uuid.id, sizeof(uuid.id), base64Buff,
104             sizeof(base64Buff), &outLen);
105
106     deviceId << base64Buff;
107     return deviceId.str();
108 }
109
110 void JniSecureUtils::convertStrToUUID(char *str, OicUuid_t &uuid)
111 {
112     unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
113     uint32_t outLen = 0;
114     B64Result b64Ret = B64_OK;
115
116     b64Ret = b64Decode(str, strlen(str), base64Buff, sizeof(base64Buff), &outLen);
117     memcpy(uuid.id, base64Buff, outLen);
118 }
119
120 jobject JniSecureUtils::convertUUIDVectorToJavaStrList(JNIEnv *env, UuidList_t &vector)
121 {
122     jobject jList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
123     if (!jList)
124     {
125         return nullptr;
126     }
127     for (size_t i = 0; i < vector.size(); ++i)
128     {
129         jstring jStr = env->NewStringUTF((convertUUIDtoStr(vector[i])).c_str());
130         if (!jStr)
131         {
132             return nullptr;
133         }
134         env->CallBooleanMethod(jList, g_mid_LinkedList_add_object, jStr);
135         if (env->ExceptionCheck())
136         {
137             return nullptr;
138         }
139         env->DeleteLocalRef(jStr);
140     }
141     return jList;
142 }
143
144 OCStackResult JniSecureUtils::convertJavaACLToOCAcl(JNIEnv *env, jobject in, OicSecAcl_t *acl)
145 {
146     jstring jData;
147     jvalue args[1];
148
149     jData = (jstring) env->CallObjectMethod(in, g_mid_OcOicSecAcl_get_subject);
150     if (!jData || env->ExceptionCheck())
151     {
152         return OC_STACK_ERROR;
153     }
154
155     char *str = (char*) env->GetStringUTFChars(jData, 0);
156     convertStrToUUID(str, acl->subject);
157     env->ReleaseStringUTFChars(jData, str);
158
159     jint jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_resources_cnt);
160     if (!jCount || env->ExceptionCheck())
161     {
162         return OC_STACK_ERROR;
163     }
164
165     acl->resourcesLen = jCount;
166     acl->resources = new char*[jCount];
167     for (jint i = 0; i < jCount; ++i)
168     {
169         args[0].i = i;
170         jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_resources, args);
171         if (!jData || env->ExceptionCheck())
172         {
173             return OC_STACK_ERROR;
174         }
175
176         acl->resources[i] = (char*) env->GetStringUTFChars(jData, 0);
177     }
178
179     jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_permission);
180     if (env->ExceptionCheck())
181     {
182         return OC_STACK_ERROR;
183     }
184
185     acl->permission = jCount;
186     jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_periods_cnt);
187     if (env->ExceptionCheck())
188     {
189         return OC_STACK_ERROR;
190     }
191
192     acl->prdRecrLen = jCount;
193     acl->periods = new char*[jCount];
194     for (jint i = 0; i < jCount; ++i)
195     {
196         args[0].i = i;
197         jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_periods, args);
198         if (!jData || env->ExceptionCheck())
199         {
200             return OC_STACK_ERROR;
201         }
202
203         acl->periods[i] = (char*) env->GetStringUTFChars(jData, 0);
204     }
205
206     acl->recurrences = new char*[jCount]; //TODO:Period Len and Reccurence len is same
207     for (jint i = 0; i < jCount; ++i)
208     {
209         args[0].i = i;
210         jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_recurrences, args);
211         if (!jData ||  env->ExceptionCheck())
212         {
213             return OC_STACK_ERROR;
214         }
215
216         acl->recurrences[i] = (char*) env->GetStringUTFChars(jData, 0);
217     }
218
219     jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_owners_cnt);
220     if (!jCount ||  env->ExceptionCheck())
221     {
222         return OC_STACK_ERROR;
223     }
224
225     acl->ownersLen = jCount;
226     acl->owners = new OicUuid_t[acl->ownersLen];
227     if (!acl->owners)
228     {
229         return OC_STACK_ERROR;
230     }
231
232     for (jint i = 0; i < jCount; ++i)
233     {
234         args[0].i = i;
235         jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_owners, args);
236         if (!jData ||  env->ExceptionCheck())
237         {
238             return OC_STACK_ERROR;
239         }
240
241         str = (char*) env->GetStringUTFChars(jData, 0);
242         convertStrToUUID(str, acl->owners[i]);
243         env->ReleaseStringUTFChars(jData, str);
244     }
245     return OC_STACK_OK;
246 }