Security CBOR conversion
[platform/upstream/iotivity.git] / resource / csdk / security / src / dpairingresource.c
1 /* *****************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * *****************************************************************/
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include "ocstack.h"
24 #include "logger.h"
25 #include "oic_malloc.h"
26 #include "oic_string.h"
27 #include "cJSON.h"
28 #include "base64.h"
29 #include "resourcemanager.h"
30 #include "dpairingresource.h"
31 #include "psinterface.h"
32 #include "utlist.h"
33 #include "srmresourcestrings.h"
34 #include "cainterface.h"
35 #include "doxmresource.h"
36 #include "pconfresource.h"
37 #include "credresource.h"
38 #include "aclresource.h"
39 #include "srmutility.h"
40 #include "ocserverrequest.h"
41 #include "ocpayloadcbor.h"
42 #include "ocpayload.h"
43 #include "payload_logging.h"
44 #include <stdlib.h>
45 #ifdef WITH_ARDUINO
46 #include <string.h>
47 #else
48 #include <strings.h>
49 #endif
50
51 #ifdef __WITH_DTLS__
52 #include "global.h"
53 #endif
54
55 #define TAG  "SRM-DPAIRING"
56
57 /** Default cbor payload size. This value is increased in case of CborErrorOutOfMemory.
58  * The value of payload size is increased until reaching belox max cbor size. */
59 static const uint8_t CBOR_SIZE = 255;
60
61 /** Max cbor size payload. */
62 static const uint16_t CBOR_MAX_SIZE = 4400;
63
64 /** DOXM Map size - Number of mandatory items. */
65 static const uint8_t DPAIR_MAP_SIZE = 3;
66
67 static OicSecDpairing_t     *gDpair = NULL;
68 static OCResourceHandle   gDpairHandle = NULL;
69 static OicSecDpairing_t      gDefaultDpair =
70 {
71     PRM_NOT_ALLOWED,       /* OicSecPrm_t spm */
72     {.id = {0}},                   /* OicUuid_t pdeviceID */
73     {.id = {0}},                   /* OicUuid_t rowner */
74 };
75
76 void DeleteDpairingBinData(OicSecDpairing_t* dpair)
77 {
78     if (dpair)
79     {
80         //Clean dpairing itself
81         OICFree(dpair);
82     }
83 }
84
85 /**
86  * Get the default value.
87  * @retval  the gDefaultDpair pointer;
88  */
89 static OicSecDpairing_t* GetDpairingDefault()
90 {
91     OIC_LOG (DEBUG, TAG, "GetDpairingDefault");
92
93     return &gDefaultDpair;
94 }
95
96 /**
97  * This method is used by SRM to retrieve Dpairing resource data..
98  */
99 void SetDpairingResourceOwner(OicUuid_t *rowner)
100 {
101     OIC_LOG (DEBUG, TAG, "SetDpairingResourceOwner");
102     if (gDpair)
103     {
104         memcpy(&gDpair->rowner, rowner, sizeof(OicUuid_t));
105     }
106 }
107
108 #ifdef __WITH_DTLS__
109 /**
110  * Function to save PairingPSK.
111  *
112  * @param[in] endpoint   current endpoint.
113  * @param[in] peerDevID   peer device indentitiy.
114  * @param[in] isPairingServer   indicate if it generates PairingPSK for server or client.
115  *
116  * @return  OC_STACK_OK on success
117  */
118 OCStackResult SavePairingPSK(OCDevAddr *endpoint,
119             OicUuid_t *peerDevID, OicUuid_t *owner, bool isPairingServer)
120 {
121     OIC_LOG(DEBUG, TAG, "IN SavePairingPSK");
122
123     if(NULL == endpoint || NULL == peerDevID || NULL == owner)
124     {
125         OIC_LOG_V(ERROR, TAG, "Invalid Input parameters in [%s]\n", __FUNCTION__);
126         return OC_STACK_INVALID_PARAM;
127     }
128
129     OCStackResult res = OC_STACK_ERROR;
130
131     OicUuid_t ptDeviceID = {.id={0}};
132     if (OC_STACK_OK != GetDoxmDeviceID(&ptDeviceID))
133     {
134         OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
135         return res;
136     }
137
138     uint8_t pairingPSK[OWNER_PSK_LENGTH_128] = {0};
139     OicSecKey_t pairingKey = {pairingPSK, OWNER_PSK_LENGTH_128};
140
141     //Generating PairingPSK using OwnerPSK scheme
142     CAResult_t pskRet = CAGenerateOwnerPSK((const CAEndpoint_t *)endpoint,
143             (uint8_t *)OIC_RSRC_TYPE_SEC_DPAIRING,
144             strlen(OIC_RSRC_TYPE_SEC_DPAIRING),
145             (isPairingServer ? ptDeviceID.id : peerDevID->id), sizeof(OicUuid_t), // server
146             (isPairingServer ? peerDevID->id : ptDeviceID.id), sizeof(OicUuid_t), // client
147             pairingPSK, OWNER_PSK_LENGTH_128);
148
149     if (CA_STATUS_OK == pskRet)
150     {
151         OIC_LOG(INFO, TAG, "pairingPSK dump:\n");
152         OIC_LOG_BUFFER(INFO, TAG, pairingPSK, OWNER_PSK_LENGTH_128);
153         //Generating new credential for direct-pairing client
154         size_t ownLen = 1;
155
156         OicSecCred_t *cred = GenerateCredential(peerDevID,
157                 SYMMETRIC_PAIR_WISE_KEY, NULL,
158                 &pairingKey, ownLen, owner);
159         VERIFY_NON_NULL(TAG, cred, ERROR);
160
161         res = AddCredential(cred);
162         if(res != OC_STACK_OK)
163         {
164             DeleteCredList(cred);
165             return res;
166         }
167     }
168     else
169     {
170         OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
171     }
172
173     OIC_LOG(DEBUG, TAG, "OUT SavePairingPSK");
174 exit:
175     return res;
176 }
177 #endif // __WITH_DTLS__
178
179 OCStackResult DpairingToCBORPayload(const OicSecDpairing_t *dpair, uint8_t **payload, size_t *size)
180 {
181     if (NULL == dpair || NULL == payload || NULL != *payload || NULL == size)
182     {
183         return OC_STACK_INVALID_PARAM;
184     }
185
186     size_t cborLen = *size;
187     if (0 == cborLen)
188     {
189         cborLen = CBOR_SIZE;
190     }
191
192     *payload = NULL;
193     *size = 0;
194
195     OCStackResult ret = OC_STACK_ERROR;
196
197     CborEncoder encoder = { {.ptr = NULL }, .end = 0 };
198     CborEncoder dpairMap = { {.ptr = NULL }, .end = 0 };
199
200     int64_t cborEncoderResult = CborNoError;
201     uint8_t mapSize = DPAIR_MAP_SIZE;
202
203     uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborLen);
204     VERIFY_NON_NULL(TAG, outPayload, ERROR);
205     cbor_encoder_init(&encoder, outPayload, cborLen, 0);
206
207     cborEncoderResult |= cbor_encoder_create_map(&encoder, &dpairMap, mapSize);
208     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating DPAIRING Map");
209
210     //spm -- Mandatory
211     cborEncoderResult |= cbor_encode_text_string(&dpairMap, OIC_JSON_SPM_NAME,
212         strlen(OIC_JSON_SPM_NAME));
213     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SPM name tag");
214     cborEncoderResult |= cbor_encode_int(&dpairMap, dpair->spm);
215     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SPM value");
216
217     //PDEVICEID -- Mandatory
218     cborEncoderResult |= cbor_encode_text_string(&dpairMap, OIC_JSON_PDEVICE_ID_NAME,
219         strlen(OIC_JSON_PDEVICE_ID_NAME));
220     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PDeviceID tag");
221     cborEncoderResult |= cbor_encode_byte_string(&dpairMap, dpair->pdeviceID.id,
222         sizeof(dpair->pdeviceID.id));
223     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PDeviceID value");
224
225     //ROWNER -- Mandatory
226     cborEncoderResult |= cbor_encode_text_string(&dpairMap, OIC_JSON_ROWNER_NAME,
227         strlen(OIC_JSON_ROWNER_NAME));
228     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROWNER tag");
229     cborEncoderResult |= cbor_encode_byte_string(&dpairMap, dpair->rowner.id,
230         sizeof(dpair->rowner.id));
231     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Rowner ID value");
232
233     cborEncoderResult |= cbor_encoder_close_container(&encoder, &dpairMap);
234     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to close dpairMap");
235
236      if (CborNoError == cborEncoderResult)
237     {
238         *size = encoder.ptr - outPayload;
239         *payload = outPayload;
240         ret = OC_STACK_OK;
241     }
242
243 exit:
244     if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
245     {
246        // reallocate and try again!
247        OICFree(outPayload);
248        // Since the allocated initial memory failed, double the memory.
249        cborLen += encoder.ptr - encoder.end;
250        cborEncoderResult = CborNoError;
251        ret = DpairingToCBORPayload(dpair, payload, &cborLen);
252        *size = cborLen;
253     }
254
255     if ((CborNoError != cborEncoderResult) || (OC_STACK_OK != ret))
256     {
257        OICFree(outPayload);
258        outPayload = NULL;
259        *payload = NULL;
260        *size = 0;
261        ret = OC_STACK_ERROR;
262     }
263
264     return ret;
265 }
266
267 OCStackResult CBORPayloadToDpair(const uint8_t *cborPayload, size_t size,
268                                 OicSecDpairing_t **secDpair)
269 {
270     if (NULL == cborPayload || NULL == secDpair || NULL != *secDpair || 0 == size)
271     {
272         return OC_STACK_INVALID_PARAM;
273     }
274
275     OCStackResult ret = OC_STACK_ERROR;
276     *secDpair = NULL;
277
278     CborValue dpairCbor = { .parser = NULL };
279     CborParser parser = { .end = NULL };
280     CborError cborFindResult = CborNoError;
281     int cborLen = size;
282
283     cbor_parser_init(cborPayload, cborLen, 0, &parser, &dpairCbor);
284     CborValue dpairMap = { .parser = NULL };
285     OicSecDpairing_t *dpair = NULL;
286     cborFindResult = cbor_value_enter_container(&dpairCbor, &dpairMap);
287     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering DPairing Map");
288
289     dpair = (OicSecDpairing_t *)OICCalloc(1, sizeof(*dpair));
290     VERIFY_NON_NULL(TAG, dpair, ERROR);
291
292     while (cbor_value_is_valid(&dpairMap))
293     {
294         char *name = NULL;
295         size_t len = 0;
296         cborFindResult = cbor_value_dup_text_string(&dpairMap, &name, &len, NULL);
297         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding tag name");
298         cborFindResult = cbor_value_advance(&dpairMap);
299         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing a value in DPair map");
300
301         CborType type = cbor_value_get_type(&dpairMap);
302
303         if (0 == strcmp(OIC_JSON_SPM_NAME, name))
304         {
305             cborFindResult = cbor_value_get_int(&dpairMap, (int *) &dpair->spm);
306             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SPM Value");
307         }
308
309         if (0 == strcmp(OIC_JSON_PDEVICE_ID_NAME, name))
310         {
311             uint8_t *id = NULL;
312             cborFindResult = cbor_value_dup_byte_string(&dpairMap, &id, &len, NULL);
313             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PDeviceID value");
314             memcpy(dpair->pdeviceID.id, id, len);
315             OICFree(id);
316         }
317
318         if (0 == strcmp(OIC_JSON_ROWNER_NAME, name))
319         {
320             uint8_t *id = NULL;
321             cborFindResult = cbor_value_dup_byte_string(&dpairMap, &id, &len, NULL);
322             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RownerID value");
323             memcpy(dpair->rowner.id, id, len);
324             OICFree(id);
325         }
326
327         if (CborMapType != type && cbor_value_is_valid(&dpairMap))
328         {
329             cborFindResult = cbor_value_advance(&dpairMap);
330             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing the Dpair Map");
331         }
332         OICFree(name);
333     }
334
335     *secDpair = dpair;
336     ret = OC_STACK_OK;
337
338 exit:
339     if (CborNoError != cborFindResult)
340     {
341         OIC_LOG (ERROR, TAG, "CBORPayloadToDoxm failed");
342         DeleteDpairingBinData(dpair);
343         dpair = NULL;
344         ret = OC_STACK_ERROR;
345     }
346     return ret;
347 }
348 /**
349  * Function to handle the handshake result in Direct-Pairing.
350  * This function will be invoked after DTLS handshake
351  * @param   endPoint  [IN] The remote endpoint.
352  * @param   errorInfo [IN] Error information from the endpoint.
353  * @return  NONE
354  */
355 void DPairingDTLSHandshakeCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
356 {
357     OIC_LOG_V(INFO, TAG, "IN DPairingDTLSHandshakeCB");
358
359     if(gDpair && endpoint && info)
360     {
361         OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
362                  endpoint->addr, endpoint->port, info->result);
363
364         if(CA_STATUS_OK == info->result)
365         {
366             OIC_LOG(INFO, TAG, "DPairingDTLSHandshakeCB - Connection success.");
367         }
368         else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
369         {
370             OIC_LOG(INFO, TAG, "DPairingDTLSHandshakeCB - Authentication failed");
371
372         }
373
374 #ifdef __WITH_DTLS__
375         CARegisterDTLSHandshakeCallback(NULL);
376 #endif // __WITH_DTLS__
377
378         // delete temporary key
379         RemoveCredential(&gDpair->pdeviceID);
380     }
381
382     OIC_LOG_V(INFO, TAG, "OUT DPairingDTLSHandshakeCB");
383 }
384
385 static OCEntityHandlerResult HandleDpairingPostRequest (const OCEntityHandlerRequest * ehRequest)
386 {
387     OIC_LOG (DEBUG, TAG, "Dpairing EntityHandle  processing POST request");
388     OCEntityHandlerResult ehRet = OC_EH_ERROR;
389     OicSecDpairing_t* newDpair = NULL;
390     OCStackResult res = OC_STACK_OK;
391
392     const OicSecPconf_t *pconf = GetPconfResourceData();
393     if (true == pconf->edp)
394     {
395         uint8_t *payload = ((OCSecurityPayload*)ehRequest->payload)->securityData1;
396         size_t size = ((OCSecurityPayload*)ehRequest->payload)->payloadSize;
397         if (payload)
398         {
399             res = CBORPayloadToDpair(payload, size, &newDpair);
400         }
401     }
402     else
403     {
404         OIC_LOG (DEBUG, TAG, "EDP == false : Direct-Pairing Disabled");
405         ehRet = OC_EH_ERROR;
406     }
407
408     if (OC_STACK_OK == res && newDpair && false == IsPairedDevice(&newDpair->pdeviceID))
409     {
410         // Check if valid Post request
411         bool prmMached = false;
412         for (size_t i=0; i<pconf->prmLen; i++)
413         {
414             if (newDpair->spm == pconf->prm[i])
415             {
416                 prmMached = true;
417                 break;
418             }
419         }
420         OIC_LOG_V(DEBUG, TAG, "Parsed spm is %s", prmMached ? "valid" :
421                 "invalid, send error response");
422
423         // Update local Dpairing with new Dpairing & prepare dtls session
424         if (prmMached && '\0' != (char)newDpair->pdeviceID.id[0])
425         {
426             if(!gDpair)
427             {
428                 gDpair = GetDpairingDefault();
429             }
430             gDpair->spm = newDpair->spm;
431             memcpy(&gDpair->pdeviceID, &newDpair->pdeviceID, sizeof(OicUuid_t));
432             memcpy(&gDpair->rowner, &pconf->rowner, sizeof(OicUuid_t));
433
434 #ifdef __WITH_DTLS__
435             // Add temporary psk
436             OCStackResult res;
437             OicUuid_t subjectId = {.id={0}};
438             res = AddTmpPskWithPIN(&gDpair->pdeviceID,
439                            SYMMETRIC_PAIR_WISE_KEY,
440                            (char*)pconf->pin.val, DP_PIN_LENGTH,
441                            1, &gDpair->rowner, &subjectId);
442             if(res != OC_STACK_OK ||
443                     memcmp(&gDpair->pdeviceID, &subjectId, sizeof(OicUuid_t)))
444             {
445                 OIC_LOG_V(ERROR, TAG, "Failed to save the temporal PSK : %d", res);
446                 goto exit;
447             }
448
449             // Prepare to establish a secure channel with Pin-based PSK cipher suite
450             if (CA_STATUS_OK != CAEnableAnonECDHCipherSuite(false) ||
451                 CA_STATUS_OK != CASelectCipherSuite(TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256))
452             {
453                 OIC_LOG_V(ERROR, TAG, "Failed to select TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256");
454                 goto exit;
455             }
456
457             if(CA_STATUS_OK != CARegisterDTLSHandshakeCallback(DPairingDTLSHandshakeCB))
458             {
459                 OIC_LOG(WARNING, TAG, "DirectPairingHandler : Failed to register"
460                         " DTLS handshake callback.");
461                 goto exit;
462             }
463 #endif // __WITH_DTLS__
464
465             // should be lock /oic/sec/dpairing resource if Direct-Pairing starts normally ?
466             OIC_LOG (DEBUG, TAG, "/oic/sec/dpairing resource created");
467
468             ehRet = OC_EH_RESOURCE_CREATED;
469         }
470         else
471         {
472             OIC_LOG(ERROR, TAG, "Error in request check");
473         }
474     }
475
476
477 #ifdef __WITH_DTLS__
478 exit:
479 #endif // __WITH_DTLS__
480
481     if (OC_EH_ERROR == ehRet && gDpair)
482     {
483         RemoveCredential(&gDpair->pdeviceID);
484         gDpair = NULL;
485     }
486
487     // Send payload to request originator
488     if(OC_STACK_OK != SendSRMCBORResponse(ehRequest, ehRet, NULL, 0))
489     {
490         OIC_LOG (ERROR, TAG, "SendSRMCBORResponse failed in HandleDpairingPostRequest");
491     }
492
493     DeleteDpairingBinData(newDpair);
494     OIC_LOG_V (DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
495     return ehRet;
496 }
497
498 static OCEntityHandlerResult HandleDpairingPutRequest (const OCEntityHandlerRequest * ehRequest)
499 {
500     OIC_LOG (DEBUG, TAG, "Dpairing EntityHandle  processing PUT request (Comfirmation)");
501
502     OCEntityHandlerResult ehRet = OC_EH_ERROR;
503     OicSecDpairing_t* newDpair = NULL;
504     OCStackResult res = OC_STACK_OK;
505
506     const OicSecPconf_t *pconf = GetPconfResourceData();
507     if (true == pconf->edp)
508     {
509         uint8_t *payload = ((OCSecurityPayload*)ehRequest->payload)->securityData1;
510         size_t size = ((OCSecurityPayload*)ehRequest->payload)->payloadSize;
511         if (payload)
512         {
513             res = CBORPayloadToDpair(payload, size, &newDpair);
514         }
515
516     }
517     else
518     {
519         OIC_LOG (DEBUG, TAG, "EDP == false : Direct-Pairing Disabled");
520         ehRet = OC_EH_ERROR;
521     }
522
523
524     if ((OC_STACK_OK == res) && gDpair && newDpair)
525     {
526         OIC_LOG(DEBUG, TAG, "Received direct-pairing finalization request");
527
528         // Check if valid Put request
529         VERIFY_SUCCESS(TAG, PRM_NOT_ALLOWED == newDpair->spm, ERROR);
530
531         const OicSecPconf_t *pconf = GetPconfResourceData();
532         VERIFY_NON_NULL(TAG, pconf, ERROR);
533
534 #ifdef __WITH_DTLS__
535         OCServerRequest * request = (OCServerRequest *)ehRequest->requestHandle;
536         VERIFY_SUCCESS(TAG, (request->devAddr.flags | OC_FLAG_SECURE), ERROR);
537
538         //Generate new credential
539         OIC_LOG_V(INFO, TAG, "SavePairingPSK for %s(%d)", request->devAddr.addr,
540                 request->devAddr.port);
541         OCStackResult res = SavePairingPSK(&request->devAddr, &newDpair->pdeviceID,
542                 (OicUuid_t *)&pconf->rowner, true);
543         VERIFY_SUCCESS(TAG, OC_STACK_OK == res, ERROR);
544 #endif //__WITH_DTLS__
545
546         //Generate new acl
547         OicSecPdAcl_t *pdAcl;
548         LL_FOREACH(pconf->pdacls, pdAcl)
549         {
550             OicSecAcl_t acl;
551             memset(&acl, 0, sizeof(OicSecAcl_t));
552             memcpy(&acl.subject, &gDpair->pdeviceID, sizeof(OicUuid_t));
553             acl.resources = pdAcl->resources;
554             acl.resourcesLen = pdAcl->resourcesLen;
555             acl.owners = (OicUuid_t*)&pconf->rowner;
556             acl.ownersLen = 1;
557             acl.permission = pdAcl->permission;
558             acl.periods = pdAcl->periods;
559             acl.recurrences = pdAcl->recurrences;
560             acl.prdRecrLen = pdAcl->prdRecrLen;
561
562             size_t size = 0;
563             uint8_t *payload = NULL;
564             if (OC_STACK_OK == AclToCBORPayload(&acl, &payload, &size))
565             {
566                 InstallNewACL(payload, size);
567                 OICFree(payload);
568             }
569         }
570
571         //update pconf device list
572         AddPairedDevice(&newDpair->pdeviceID);
573
574         //Initialize dpairing resource
575         gDpair = NULL;
576
577         OIC_LOG (DEBUG, TAG, "/oic/sec/dpairing resource updated,"
578                 "direct-pairing finalization success");
579         ehRet = OC_EH_OK;
580     }
581
582 exit:
583
584     //Send payload to request originator
585     if(OC_STACK_OK != SendSRMCBORResponse(ehRequest, ehRet, NULL, 0))
586     {
587         OIC_LOG (ERROR, TAG, "SendSRMCBORResponse failed in HandleDpairingPutRequest");
588     }
589
590     DeleteDpairingBinData(newDpair);
591     OIC_LOG_V (DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
592     return ehRet;
593 }
594 /*
595  * This internal method is the entity handler for Dpairing resources and
596  * will handle REST request (GET/POST) for them.
597  */
598 OCEntityHandlerResult DpairingEntityHandler (OCEntityHandlerFlag flag,
599                                         OCEntityHandlerRequest * ehRequest,
600                                         void* callbackParameter)
601 {
602     OIC_LOG(DEBUG, TAG, "Received request DpairingEntityHandler");
603     (void)callbackParameter;
604     OCEntityHandlerResult ehRet = OC_EH_ERROR;
605
606     if (!ehRequest)
607     {
608         return ehRet;
609     }
610
611     if (flag & OC_REQUEST_FLAG)
612     {
613         OIC_LOG (DEBUG, TAG, "Flag includes OC_REQUEST_FLAG");
614         switch (ehRequest->method)
615         {
616             case OC_REST_GET:
617                 break;
618
619             case OC_REST_POST:
620                 ehRet = HandleDpairingPostRequest(ehRequest);
621                 break;
622
623             case OC_REST_PUT:
624                 ehRet = HandleDpairingPutRequest(ehRequest);
625                 break;
626
627             case OC_REST_DELETE:
628                 break;
629
630             default:
631                 ehRet = OC_EH_ERROR;
632                 SendSRMCBORResponse(ehRequest, ehRet, NULL, 0);
633         }
634     }
635
636     return ehRet;
637 }
638
639 /*
640  * This internal method is used to create '/oic/sec/dpairing' resource.
641  */
642 OCStackResult CreateDpairingResource()
643 {
644     OCStackResult ret;
645
646     ret = OCCreateResource(&gDpairHandle,
647                            OIC_RSRC_TYPE_SEC_DPAIRING,
648                            OIC_MI_DEF,
649                            OIC_RSRC_DPAIRING_URI,
650                            DpairingEntityHandler,
651                            NULL,
652                            OC_SECURE | OC_EXPLICIT_DISCOVERABLE);
653
654     if (OC_STACK_OK != ret)
655     {
656         OIC_LOG (ERROR, TAG, "Unable to instantiate Dpairing resource");
657         DeInitDpairingResource();
658     }
659     return ret;
660 }
661
662 /**
663  * Initialize Dpairing resource by loading data from persistent storage.
664  *
665  * @retval  OC_STACK_OK for Success, otherwise some error value
666  */
667 OCStackResult InitDpairingResource()
668 {
669     OCStackResult ret = OC_STACK_ERROR;
670
671     // Instantiate 'oic.sec.dpairing'
672     ret = CreateDpairingResource();
673     if (OC_STACK_OK != ret)
674     {
675         DeInitDpairingResource();
676     }
677     return ret;
678 }
679
680 /**
681  * Perform cleanup for Dpairing resources.
682  *
683  * @return
684  * OC_STACK_OK    - no error
685  * OC_STACK_ERROR - stack process error
686  *
687  */
688 OCStackResult DeInitDpairingResource()
689 {
690     OCStackResult ret = OCDeleteResource(gDpairHandle);
691     gDpair = NULL;
692
693     if(OC_STACK_OK == ret)
694     {
695         return OC_STACK_OK;
696     }
697     else
698     {
699         return OC_STACK_ERROR;
700     }
701 }