replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / src / oxmverifycommon.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 "ocstack.h"
22 #include "logger.h"
23 #include "base64.h"
24 #include "securevirtualresourcetypes.h"
25 #include "srmresourcestrings.h"
26 #include "cainterface.h"
27 #include "oxmverifycommon.h"
28
29 #define TAG "OIC_VERIFY_COMMON"
30
31 static VerifyOptionBitmask_t gVerifyOption = (DISPLAY_NUM | USER_CONFIRM);
32
33 static DisplayNumContext_t gDisplayNumContext = { .callback = NULL, .context = NULL };
34 static UserConfirmContext_t gUserConfirmContext = { .callback = NULL, .context = NULL };
35 static InputStateContext_t gInputStateContext = { .callback = NULL, .context = NULL };
36
37 void SetDisplayNumCB(void * ptr, DisplayNumCallback displayNumCB)
38 {
39     OIC_LOG(DEBUG, TAG, "IN SetDisplayNumCB");
40     if (NULL == displayNumCB)
41     {
42         OIC_LOG(ERROR, TAG, "Failed to set callback for displaying mutualVerifNum");
43         return;
44     }
45     gDisplayNumContext.callback = displayNumCB;
46     gDisplayNumContext.context = ptr;
47     OIC_LOG(DEBUG, TAG, "OUT SetDisplayNumCB");
48 }
49
50 void* UnsetDisplayNumCB()
51 {
52     OIC_LOG(DEBUG, TAG, "IN UnsetDisplayNumCB");
53     void *prevctx = gDisplayNumContext.context;
54     gDisplayNumContext.callback = NULL;
55     gDisplayNumContext.context= NULL;
56     OIC_LOG(DEBUG, TAG, "OUT UnsetDisplayNumCB");
57     return prevctx;
58 }
59
60 void SetUserConfirmCB(void * ptr, UserConfirmCallback userConfirmCB)
61 {
62     OIC_LOG(DEBUG, TAG, "IN SetUserConfirmCB");
63     if (NULL == userConfirmCB)
64     {
65         OIC_LOG(ERROR, TAG, "Failed to set callback to confirm mutualVerifNum");
66         return;
67     }
68     gUserConfirmContext.callback = userConfirmCB;
69     gUserConfirmContext.context = ptr;
70     OIC_LOG(DEBUG, TAG, "OUT SetUserConfirmCB");
71 }
72
73 void* UnsetUserConfirmCB()
74 {
75     OIC_LOG(DEBUG, TAG, "IN UnsetUserConfirmCB");
76     void *prevctx = gUserConfirmContext.context;
77     gUserConfirmContext.callback = NULL;
78     gUserConfirmContext.context = NULL;
79     OIC_LOG(DEBUG, TAG, "OUT UnsetUserConfirmCB");
80     return prevctx;
81 }
82
83 void SetInputStateCB(void * ptr, InputStateCallback inputStateCB)
84 {
85     OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
86     if (NULL == inputStateCB)
87     {
88         OIC_LOG(ERROR, TAG, "Failed to set callback to notify user input state");
89         return;
90     }
91     gInputStateContext.callback = inputStateCB;
92     gInputStateContext.context = ptr;
93     OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
94 }
95
96 void* UnsetInputStateCB()
97 {
98     OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
99     void *prevctx = gInputStateContext.context;
100     gInputStateContext.callback = NULL;
101     gInputStateContext.context = NULL;
102     OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
103     return prevctx;
104 }
105
106 void SetVerifyOption(VerifyOptionBitmask_t verifyOption)
107 {
108     OIC_LOG(DEBUG, TAG, "IN SetVerifyOption");
109     gVerifyOption = verifyOption;
110     OIC_LOG_V(DEBUG, TAG, "VerifyOption set to %d", (int) gVerifyOption);
111     OIC_LOG(DEBUG, TAG, "OUT SetVerifyOption");
112 }
113
114 OCStackResult VerifyOwnershipTransfer(uint8_t mutualVerifNum [MUTUAL_VERIF_NUM_LEN],
115                                     VerifyOptionBitmask_t verifyOption)
116 {
117     OIC_LOG(DEBUG, TAG, "IN VerifyOwnershipTransfer");
118     OIC_LOG_V(DEBUG, TAG, "gVerifyOption: %d", (int) gVerifyOption);
119     verifyOption = (VerifyOptionBitmask_t)(verifyOption & gVerifyOption);
120     if (verifyOption & DISPLAY_NUM)
121     {
122         if (!gDisplayNumContext.callback)
123         {
124             OIC_LOG(ERROR, TAG, "Callback for displaying verification PIN not registered");
125             return OC_STACK_ERROR;
126         }
127         OIC_LOG(DEBUG, TAG, "calling displayNumCallback");
128         if (OC_STACK_OK != gDisplayNumContext.callback(gDisplayNumContext.context, mutualVerifNum))
129         {
130             OIC_LOG(ERROR, TAG, "Failed to display verification PIN");
131             return OC_STACK_ERROR;
132         }
133     }
134     if (verifyOption & USER_CONFIRM)
135     {
136         if (!gUserConfirmContext.callback)
137         {
138             OIC_LOG(ERROR, TAG, "Callback to get user confirmation not registered");
139             return OC_STACK_ERROR;
140         }
141         OIC_LOG(DEBUG, TAG, "calling userConfirmCallback");
142         if (OC_STACK_OK != gUserConfirmContext.callback(gUserConfirmContext.context))
143         {
144             OIC_LOG(ERROR, TAG, "Failed to get user confirmation");
145             return OC_STACK_USER_DENIED_REQ;
146         }
147     }
148     OIC_LOG(DEBUG, TAG, "OUT VerifyOwnershipTransfer");
149     return OC_STACK_OK;
150 }
151
152 OCStackResult NotifyInputState(void)
153 {
154     OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
155
156     if (!gInputStateContext.callback)
157     {
158         OIC_LOG(INFO, TAG, "Callback for notifying user input state not registered");
159         return OC_STACK_OK;
160     }
161     OIC_LOG(INFO, TAG, "calling inputStateCallback");
162     if (OC_STACK_OK != gInputStateContext.callback(gInputStateContext.context))
163     {
164         OIC_LOG(ERROR, TAG, "Failed to notify user input state");
165         return OC_STACK_ERROR;
166     }
167     OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
168     return OC_STACK_OK;
169 }
170