2 * //******************************************************************
4 * // Copyright 2015 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "JniSecureUtils.h"
24 #include "JniOcSecureResource.h"
27 jobject JniSecureUtils::convertProvisionresultVectorToJavaList(JNIEnv *env, const OC::PMResultList_t *result)
29 jobject jResultList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
35 for (size_t i = 0; i < result->size(); ++i)
37 jstring jStr = env->NewStringUTF((convertUUIDtoStr(result->at(i).deviceId).c_str()));
42 jobject jresult = env->NewObject(
43 g_cls_OcProvisionResult,
44 g_mid_OcProvisionResult_ctor,
46 static_cast<jint>(result->at(i).res)
53 env->CallBooleanMethod(jResultList, g_mid_LinkedList_add_object, jresult);
54 if (env->ExceptionCheck())
58 env->DeleteLocalRef(jresult);
59 env->DeleteLocalRef(jStr);
64 jobjectArray JniSecureUtils::convertDeviceVectorToJavaArray(JNIEnv *env,
65 std::vector<std::shared_ptr<OC::OCSecureResource>> &deviceListVector)
67 jsize len = static_cast<jsize>(deviceListVector.size());
68 jobjectArray devArr = env->NewObjectArray(len, g_cls_OcSecureResource, NULL);
75 for (jsize i = 0; i < len; ++i)
77 JniOcSecureResource *device = new JniOcSecureResource(deviceListVector[i]);
78 jobject jDevice = env->NewObject(g_cls_OcSecureResource, g_mid_OcSecureResource_ctor);
80 SetHandle<JniOcSecureResource>(env, jDevice, device);
86 env->SetObjectArrayElement(devArr, i, jDevice);
87 if (env->ExceptionCheck())
91 env->DeleteLocalRef(jDevice);
96 std::string JniSecureUtils::convertUUIDtoStr(OicUuid_t uuid)
98 std::ostringstream deviceId("");
99 char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*)0)->id)) + 1] = {0,};
101 B64Result b64Ret = B64_OK;
103 b64Ret = b64Encode(uuid.id, sizeof(uuid.id), base64Buff,
104 sizeof(base64Buff), &outLen);
106 deviceId << base64Buff;
107 return deviceId.str();
110 void JniSecureUtils::convertStrToUUID(char *str, OicUuid_t &uuid)
112 unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
114 B64Result b64Ret = B64_OK;
116 b64Ret = b64Decode(str, strlen(str), base64Buff, sizeof(base64Buff), &outLen);
117 memcpy(uuid.id, base64Buff, outLen);
120 jobject JniSecureUtils::convertUUIDVectorToJavaStrList(JNIEnv *env, UuidList_t &vector)
122 jobject jList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
127 for (size_t i = 0; i < vector.size(); ++i)
129 jstring jStr = env->NewStringUTF((convertUUIDtoStr(vector[i])).c_str());
134 env->CallBooleanMethod(jList, g_mid_LinkedList_add_object, jStr);
135 if (env->ExceptionCheck())
139 env->DeleteLocalRef(jStr);
144 OCStackResult JniSecureUtils::convertJavaACLToOCAcl(JNIEnv *env, jobject in, OicSecAcl_t *acl)
149 jData = (jstring) env->CallObjectMethod(in, g_mid_OcOicSecAcl_get_subject);
150 if (!jData || env->ExceptionCheck())
152 return OC_STACK_ERROR;
155 char *str = (char*) env->GetStringUTFChars(jData, 0);
156 convertStrToUUID(str, acl->subject);
157 env->ReleaseStringUTFChars(jData, str);
159 jint jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_resources_cnt);
160 if (!jCount || env->ExceptionCheck())
162 return OC_STACK_ERROR;
165 acl->resourcesLen = jCount;
166 acl->resources = new char*[jCount];
167 for (jint i = 0; i < jCount; ++i)
170 jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_resources, args);
171 if (!jData || env->ExceptionCheck())
173 return OC_STACK_ERROR;
176 acl->resources[i] = (char*) env->GetStringUTFChars(jData, 0);
179 jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_permission);
180 if (env->ExceptionCheck())
182 return OC_STACK_ERROR;
185 acl->permission = jCount;
186 jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_periods_cnt);
187 if (env->ExceptionCheck())
189 return OC_STACK_ERROR;
192 acl->prdRecrLen = jCount;
193 acl->periods = new char*[jCount];
194 for (jint i = 0; i < jCount; ++i)
197 jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_periods, args);
198 if (!jData || env->ExceptionCheck())
200 return OC_STACK_ERROR;
203 acl->periods[i] = (char*) env->GetStringUTFChars(jData, 0);
206 acl->recurrences = new char*[jCount]; //TODO:Period Len and Reccurence len is same
207 for (jint i = 0; i < jCount; ++i)
210 jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_recurrences, args);
211 if (!jData || env->ExceptionCheck())
213 return OC_STACK_ERROR;
216 acl->recurrences[i] = (char*) env->GetStringUTFChars(jData, 0);
219 jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_owners_cnt);
220 if (!jCount || env->ExceptionCheck())
222 return OC_STACK_ERROR;
225 acl->ownersLen = jCount;
226 acl->owners = new OicUuid_t[acl->ownersLen];
229 return OC_STACK_ERROR;
232 for (jint i = 0; i < jCount; ++i)
235 jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_owners, args);
236 if (!jData || env->ExceptionCheck())
238 return OC_STACK_ERROR;
241 str = (char*) env->GetStringUTFChars(jData, 0);
242 convertStrToUUID(str, acl->owners[i]);
243 env->ReleaseStringUTFChars(jData, str);