replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOcProvisioning.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 "JniOcProvisioning.h"
24 #include "JniPinCheckListener.h"
25 #include "JniDisplayPinListener.h"
26 #include "oic_malloc.h"
27 #include "aclresource.h"
28 #include "oxmverifycommon.h"
29 #include "JniDisplayVerifyNumListener.h"
30 #include "JniConfirmNumListener.h"
31
32 using namespace OC;
33 namespace PH = std::placeholders;
34
35 static JniPinCheckListener *jniPinListener = nullptr;
36 static JniDisplayPinListener *jniDisplayPinListener = nullptr;
37 static JniDisplayVerifyNumListener *jniDisplayMutualVerifyNumListener = nullptr;
38 static JniConfirmNumListener *jniConfirmMutualVerifyNumListener = nullptr;
39
40 void Callback(char *buf, size_t size)
41 {
42     if (jniPinListener)
43     {
44         jniPinListener->PinCallback(buf, size);
45     }
46     else
47     {
48         LOGE("jniPinListener is null");
49     }
50 }
51
52 void displayPinCB(char *pinBuf, size_t pinSize)
53 {
54     if (jniDisplayPinListener)
55     {
56         jniDisplayPinListener->displayPinCallback(pinBuf, pinSize);
57     }
58     else
59     {
60         LOGE("DisplayPinListener is null");
61     }
62 }
63
64 OCStackResult displayMutualVerifNumCB(uint8_t *verifyNum)
65 {
66     OCStackResult res;
67
68     if (jniDisplayMutualVerifyNumListener)
69     {
70         res = jniDisplayMutualVerifyNumListener->displayMutualVerifNumCallback(verifyNum);
71     }
72     else
73     {
74         res = OC_STACK_ERROR;
75         LOGE("DisplayMutualVerifyNumListener is null");
76     }
77     return res;
78 }
79
80 OCStackResult confirmMutualVerifNumCB()
81 {
82     OCStackResult res;
83
84     if (jniConfirmMutualVerifyNumListener)
85     {
86         res = jniConfirmMutualVerifyNumListener->confirmMutualVerifNumCallback();
87     }
88     else
89     {
90         res = OC_STACK_ERROR;
91         LOGE("ConfirmMutualVerifyNumListener is null");
92     }
93     return res;
94 }
95
96 /*
97  * Class:     org_iotivity_base_OcProvisioning
98  * Method:    ownershipTransferCDdata
99  * Signature: (I)V
100  */
101 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_ownershipTransferCBdata
102   (JNIEnv *env, jobject thiz, jint OxmType, jobject jListener)
103 {
104     LOGD("OcProvisioning_ownershipTransferCBdata");
105     OCStackResult result = OC_STACK_ERROR;
106
107     try
108     {
109         OTMCallbackData_t CBData = {0};
110         if (OIC_JUST_WORKS == (OicSecOxm_t)OxmType)
111         {
112             /*NO callback required for JUST_WORKS*/
113             result = OCSecure::setInputPinCallback(NULL);
114         }
115         if (OIC_RANDOM_DEVICE_PIN == (OicSecOxm_t)OxmType)
116         {
117             if (jListener)
118             {
119                 delete jniPinListener;
120                 jniPinListener = new JniPinCheckListener(env, jListener);
121                 result = OCSecure::setInputPinCallback(Callback);
122             }
123             else
124             {
125                 result = OC_STACK_INVALID_CALLBACK;
126             }
127         }
128
129         if (OC_STACK_OK != result)
130         {
131             ThrowOcException(result, "OcProvisioning_ownershipTransferCDdata");
132             return;
133         }
134     }
135     catch (OCException& e)
136     {
137         LOGE("%s", e.reason().c_str());
138         ThrowOcException(e.code(), e.reason().c_str());
139     }
140 }
141
142 /*
143 * Class:     org_iotivity_base_OcProvisioning
144 * Method:    discoverUnownedDevices
145 * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
146 */
147 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_discoverUnownedDevices1
148   (JNIEnv *env, jclass clazz, jint timeout)
149 {
150     LOGI("OcProvisioning_discoverUnownedDevices");
151     DeviceList_t list;
152
153     try
154     {
155         if (timeout < 0)
156         {
157             ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
158             return nullptr;
159         }
160         OCStackResult result = OCSecure::discoverUnownedDevices((unsigned short)timeout, list);
161
162         if (OC_STACK_OK != result)
163         {
164             ThrowOcException(result, "Failed to discover Unowned devices");
165             return nullptr;
166         }
167
168         return JniSecureUtils::convertDeviceVectorToJavaArray(env, list);
169     }
170     catch (OCException& e)
171     {
172         LOGE("%s", e.reason().c_str());
173         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
174         return nullptr;
175     }
176 }
177
178 /*
179  * Class:     org_iotivity_base_OcProvisioning
180  * Method:    provisionInit
181  * Signature: (Ljava/lang/String;)V
182  */
183 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_provisionInit
184   (JNIEnv *env, jclass calzz, jstring jdbPath)
185 {
186     LOGI("OcProvisioning_provisionInit");
187     char *dbpath;
188
189     if (!jdbPath)
190     {
191         ThrowOcException(OC_STACK_INVALID_PARAM, "SVR db path cannot be null");
192         return;
193     }
194
195     try
196     {
197         dbpath = (char*)env->GetStringUTFChars(jdbPath, NULL);
198         OCStackResult result = OCSecure::provisionInit(env->GetStringUTFChars(jdbPath, NULL));
199
200         if (OC_STACK_OK != result)
201         {
202             env->ReleaseStringUTFChars(jdbPath, (const char*)dbpath);
203             ThrowOcException(result, "Failed to Init Provisioning Manager");
204             return;
205         }
206         env->ReleaseStringUTFChars(jdbPath, (const char*)dbpath);
207     }
208     catch (OCException& e)
209     {
210         LOGE("%s", e.reason().c_str());
211         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
212     }
213 }
214
215 /*
216  * Class:     org_iotivity_base_OcProvisioning
217  * Method:    discoverOwnedDevices
218  * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
219  */
220 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_discoverOwnedDevices1
221   (JNIEnv *env, jclass clazz , jint timeout)
222 {
223     LOGI("OcProvisioning_discoverOwnedDevices");
224     DeviceList_t list;
225
226     try
227     {
228         if (timeout < 0)
229         {
230             ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
231             return nullptr;
232         }
233         OCStackResult result = OCSecure::discoverOwnedDevices((unsigned short)timeout, list);
234         if (OC_STACK_OK != result)
235         {
236             ThrowOcException(result, "Failed to discover Owned devices");
237             return nullptr;
238         }
239
240         return JniSecureUtils::convertDeviceVectorToJavaArray(env, list);
241     }
242     catch (OCException& e)
243     {
244         LOGE("%s", e.reason().c_str());
245         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
246         return nullptr;
247     }
248 }
249
250 /*
251  * Class:     org_iotivity_base_OcProvisioning
252  * Method:    getDevicestatusLists
253  * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
254  */
255 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_getDeviceStatusList1
256   (JNIEnv *env, jclass clazz, jint timeout)
257 {
258     LOGI("OcProvisioning_getDeviceStatusList");
259     DeviceList_t  ownedDevList, unownedDevList;
260
261     try
262     {
263         if (timeout < 0)
264         {
265             ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
266             return nullptr;
267         }
268         OCStackResult result = OCSecure::getDevInfoFromNetwork((unsigned short)timeout,
269                 ownedDevList, unownedDevList);
270         if (OC_STACK_OK != result)
271         {
272             ThrowOcException(result, "Failed to get Device Status List");
273             return nullptr;
274         }
275         ownedDevList.insert(ownedDevList.end(), unownedDevList.begin(), unownedDevList.end());
276         return JniSecureUtils::convertDeviceVectorToJavaArray(env, ownedDevList);
277     }
278     catch (OCException& e)
279     {
280         LOGE("%s", e.reason().c_str());
281         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
282         return nullptr;
283     }
284 }
285
286 /*
287  * Class:     org_iotivity_base_OcProvisioning
288  * Method:    setDisplayNumListener
289  * Signature: (Lorg/iotivity/base/OcProvisioning/DisplayNumListener;)V
290  */
291 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_setDisplayNumListener
292   (JNIEnv *env, jclass clazz, jobject jListener)
293 {
294     LOGI("OcProvisioning_setDisplayNumListener");
295
296     if (!jListener)
297     {
298         ThrowOcException(OC_STACK_INVALID_CALLBACK, "Listner can't be null");
299         return;
300     }
301     delete jniDisplayMutualVerifyNumListener;
302     jniDisplayMutualVerifyNumListener = new JniDisplayVerifyNumListener(env, jListener);
303
304     try
305     {
306         OCStackResult result = OCSecure::registerDisplayNumCallback(displayMutualVerifNumCB);
307         if (OC_STACK_OK != result)
308         {
309             ThrowOcException(result, "Failed to set Listner");
310             return;
311         }
312     }
313     catch (OCException& e)
314     {
315         LOGE("%s", e.reason().c_str());
316         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
317         return;
318     }
319 }
320
321 /*
322  * Class:     org_iotivity_base_OcProvisioning
323  * Method:    unsetDisplayNumListener
324  * Signature: ()I
325  */
326 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_unsetDisplayNumListener
327   (JNIEnv * env, jclass clazz)
328 {
329     LOGI("OcProvisioning_unsetDisplayNumListener");
330
331     OCStackResult result = OCSecure::deregisterDisplayNumCallback();
332
333     if (OC_STACK_OK != result)
334     {
335         ThrowOcException(OC_STACK_INVALID_CALLBACK, "Failed to unset Listener");
336     }
337
338     return result;
339 }
340
341 /*
342  * Class:     org_iotivity_base_OcProvisioning
343  * Method:    setPinType0
344  * Signature: (II)I
345  */
346 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_setPinType0
347   (JNIEnv *env, jclass thiz, jint pinSize, jint pinType)
348 {
349     LOGI("OcProvisioning_setPinType0");
350
351     OCStackResult result = OC_STACK_ERROR;
352     try
353     {
354         result = OCSecure::setRandomPinPolicy((size_t)pinSize, (OicSecPinType_t)pinType);
355         if (OC_STACK_OK != result)
356         {
357             ThrowOcException(result, "Failed to set PinType");
358         }
359     }
360     catch (OCException& e)
361     {
362         LOGE("%s", e.reason().c_str());
363         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
364     }
365     return result;
366 }
367
368 /*
369  * Class:     org_iotivity_base_OcProvisioning
370  * Method:    setConfirmNumListener
371  * Signature: (Lorg/iotivity/base/OcProvisioning/ConfirmNumListener;)V
372  */
373 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_setConfirmNumListener
374   (JNIEnv *env, jclass clazz, jobject jListener)
375 {
376     LOGI("OcProvisioning_setConfirmNumListener");
377
378     if (!jListener)
379     {
380         ThrowOcException(OC_STACK_INVALID_CALLBACK, "Listner can't be null");
381         return;
382     }
383     delete jniConfirmMutualVerifyNumListener;
384     jniConfirmMutualVerifyNumListener = new JniConfirmNumListener(env, jListener);
385
386     try
387     {
388         OCStackResult result = OCSecure::registerUserConfirmCallback(confirmMutualVerifNumCB);
389         if (OC_STACK_OK != result)
390         {
391             ThrowOcException(result, "Failed to set Listner");
392             return;
393         }
394     }
395     catch (OCException& e)
396     {
397         LOGE("%s", e.reason().c_str());
398         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
399         return;
400     }
401 }
402
403 /*
404  * Class:     org_iotivity_base_OcProvisioning
405  * Method:    unsetConfirmNumListener
406  * Signature: ()I
407  */
408 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_unsetConfirmNumListener
409   (JNIEnv *env, jclass clazz)
410 {
411     LOGI("OcProvisioning_unsetConfirmNumListener");
412
413     OCStackResult result = OCSecure::deregisterUserConfirmCallback();
414
415     if (OC_STACK_OK != result)
416     {
417         ThrowOcException(OC_STACK_INVALID_CALLBACK, "Failed to unser Listener");
418     }
419
420     return result;
421 }
422 /*
423  * Class:     org_iotivity_base_OcProvisioning
424  * Method:    setMVJustWorksOptions0
425  * Signature: (I)I
426  */
427 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_setMVJustWorksOptions0
428   (JNIEnv *env, jclass clazz, jint options)
429 {
430     LOGI("OcProvisioning_setMVJustWorksOptions0");
431
432     OCStackResult result = OCSecure::setVerifyOptionMask((VerifyOptionBitmask_t)options);
433
434     if (OC_STACK_OK != result)
435     {
436         ThrowOcException(OC_STACK_INVALID_CALLBACK, "setMVJustWorksOptions Failed");
437     }
438
439     return result;
440 }
441
442
443 /*
444  * Class:     org_iotivity_base_OcProvisioning
445  * Method:    setDisplayPinListener
446  * Signature: (Lorg/iotivity/base/OcProvisioning/DisplayPinListener;)V
447  */
448 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_setDisplayPinListener
449   (JNIEnv *env, jclass thiz, jobject jListener)
450 {
451
452     LOGI("OcProvisioning_setDisplayPinListener");
453
454     if (!jListener)
455     {
456         ThrowOcException(OC_STACK_INVALID_CALLBACK, "displayPinListener can't be null");
457         return;
458     }
459     delete jniDisplayPinListener;
460     jniDisplayPinListener = new JniDisplayPinListener(env, jListener);
461
462     try
463     {
464         OCStackResult result = OCSecure::setDisplayPinCB(displayPinCB);
465         if (OC_STACK_OK != result)
466         {
467             ThrowOcException(result, "Failed to set displayPinListener");
468             return;
469         }
470     }
471     catch (OCException& e)
472     {
473         LOGE("%s", e.reason().c_str());
474         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
475     }
476 }
477 /*
478  * Class:     org_iotivity_base_OcProvisioning
479  * Method:    saveTrustCertChain1
480  * Signature: (Lorg/iotivity/base/OcProvisioning/provisionTrustCertChain1;)V
481  */
482     JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_saveTrustCertChain1
483 (JNIEnv *env, jobject thiz, jbyteArray trustCertChain, jint encodingType)
484 {
485     LOGD("OcProvisioning_saveTrustCertChain1");
486 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
487     jbyte* trustCertChainBytes = env->GetByteArrayElements(trustCertChain, 0);
488     jsize arrayLength = env->GetArrayLength(trustCertChain);
489     uint16_t credId = -1;
490     unsigned char* trustedCertChar = new unsigned char[arrayLength];
491     try
492     {
493         env->GetByteArrayRegion (trustCertChain, 0, arrayLength, reinterpret_cast<jbyte*>(trustedCertChar));
494         OCStackResult result = OCSecure::saveTrustCertChain((uint8_t*)trustedCertChar, arrayLength,
495                 (OicEncodingType_t)encodingType, &credId);
496         if (OC_STACK_OK != result)
497         {
498             ThrowOcException(result, "OcProvisioning_saveTrustCertChain1");
499             return -1;
500         }
501     }
502     catch (OCException& e)
503     {
504         LOGE("%s", e.reason().c_str());
505         ThrowOcException(e.code(), e.reason().c_str());
506     }
507     return (jint)credId;
508 #else
509     ThrowOcException(OC_STACK_INVALID_PARAM, "WITH_TLS not enabled");
510     return -1;
511 #endif // __WITH_DTLS__ || __WITH_TLS__
512 }
513
514 /*
515  * Class:     org_iotivity_base_OcProvisioning
516  * Method:    setDeviceIdSeed1
517  * Signature: (Lorg/iotivity/base/OcProvisioning/provisionTrustCertChain1;)V
518  */
519     JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_setDeviceIdSeed1
520 (JNIEnv *env, jobject thiz, jbyteArray seed)
521 {
522   LOGD("OcProvisioning_setDeviceIdSeed1");
523 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
524         jbyte* byteSeed = env->GetByteArrayElements(seed, 0);
525         jsize arrayLength = env->GetArrayLength(seed);
526         try
527         {
528     env->GetByteArrayRegion (seed, 0, arrayLength, byteSeed);
529                 OCStackResult result = OCSecure::setDeviceIdSeed((uint8_t*)byteSeed, arrayLength);
530                 if (OC_STACK_OK != result)
531                 {
532                   ThrowOcException(result, "OcProvisioning_setDeviceIdSeed");
533                         return -1;
534                 }
535         }
536         catch (OCException& e)
537         {
538                                         LOGE("%s", e.reason().c_str());
539                                         ThrowOcException(e.code(), e.reason().c_str());
540         }
541         return 0;
542 #else
543         ThrowOcException(OC_STACK_INVALID_PARAM, "WITH_TLS not enabled");
544         return -1;
545 #endif // __WITH_DTLS__ || __WITH_TLS__
546 }
547
548 /*
549  * Class:     org_iotivity_base_OcProvisioning
550  * Method:    saveACL
551  * Signature: (Ljava/lang/Object;)V
552  */
553 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_saveACL
554   (JNIEnv *env , jclass thiz, jobject jacl)
555 {
556     LOGD("OcProvisioning_saveACL");
557
558     if (!jacl)
559     {
560         ThrowOcException(OC_STACK_INVALID_PARAM, "acl can't be null");
561     }
562
563     OicSecAcl_t *acl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
564     if (!acl)
565     {
566         ThrowOcException(OC_STACK_NO_MEMORY, "acl allocation failed");
567         return;
568     }
569
570     if (OC_STACK_OK != JniSecureUtils::convertJavaACLToOCAcl(env, jacl, acl))
571     {
572         DeleteACLList(acl);
573         ThrowOcException(OC_STACK_INVALID_PARAM, "Failed to convert Java acl to OC acl");
574         return ;
575     }
576
577     try
578     {
579         OCStackResult result = OCSecure::saveACL(acl);
580         if (OC_STACK_OK != result)
581         {
582             ThrowOcException(result, "OCSecure::saveACL Failed");
583             return;
584         }
585     }
586     catch (OCException& e)
587     {
588         LOGE("%s", e.reason().c_str());
589         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
590     }
591 }
592
593 /*
594  * Class:     org_iotivity_base_OcProvisioning
595  * Method:    doSelfOwnershiptransfer
596  * Signature: ()V
597  */
598         JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_doSelfOwnershiptransfer
599 (JNIEnv *env, jclass thiz)
600 {
601
602   LOGD("OcProvisioning_doSelfOwnershiptransfer");
603
604   try
605   {
606           OCStackResult result = OCSecure::configSelfOwnership();
607           if (OC_STACK_OK != result)
608           {
609                   ThrowOcException(result, "OCSecure::configSelfOwnership Failed");
610                   return;
611           }
612   }
613   catch (OCException& e)
614   {
615           LOGE("%s", e.reason().c_str());
616           ThrowOcException(e.code(), e.reason().c_str());
617           return;
618   }
619 }