Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / security / src / oxmpincommon.c
1 /* *****************************************************************
2  *
3  * Copyright 2015 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 #include "ocstack.h"
21 #include "ocrandom.h"
22 #include "logger.h"
23 #include "pinoxmcommon.h"
24
25 #define TAG "PIN_OXM_COMMON"
26
27 static GeneratePinCallback gGenPinCallback = NULL;
28 static InputPinCallback gInputPinCallback = NULL;
29
30 void SetInputPinCB(InputPinCallback pinCB)
31 {
32     if(NULL == pinCB)
33     {
34         OC_LOG(ERROR, TAG, "Failed to set callback for input pin.");
35         return;
36     }
37
38     gInputPinCallback = pinCB;
39 }
40
41 void SetGeneratePinCB(GeneratePinCallback pinCB)
42 {
43     if(NULL == pinCB)
44     {
45         OC_LOG(ERROR, TAG, "Failed to set callback for generate pin.");
46         return;
47     }
48
49     gGenPinCallback = pinCB;
50 }
51
52 OCStackResult GeneratePin(char* pinBuffer, size_t bufferSize)
53 {
54     if(!pinBuffer)
55     {
56         OC_LOG(ERROR, TAG, "PIN buffer is NULL");
57         return OC_STACK_INVALID_PARAM;
58     }
59     if(OXM_RANDOM_PIN_SIZE + 1 > bufferSize)
60     {
61         OC_LOG(ERROR, TAG, "PIN buffer size is too small");
62         return OC_STACK_INVALID_PARAM;
63     }
64     for(size_t i = 0; i < OXM_RANDOM_PIN_SIZE; i++)
65     {
66         pinBuffer[i] = OCGetRandomRange((uint32_t)'0', (uint32_t)'9');
67     }
68     pinBuffer[OXM_RANDOM_PIN_SIZE] = '\0';
69
70     if(gGenPinCallback)
71     {
72         gGenPinCallback(pinBuffer, OXM_RANDOM_PIN_SIZE);
73     }
74     else
75     {
76         OC_LOG(ERROR, TAG, "Invoke PIN callback failed!");
77         OC_LOG(ERROR, TAG, "Callback for genrate PIN should be registered to use PIN based OxM.");
78         return OC_STACK_ERROR;
79     }
80
81     return OC_STACK_OK;
82 }
83
84
85 OCStackResult InputPin(char* pinBuffer, size_t bufferSize)
86 {
87     if(!pinBuffer)
88     {
89         OC_LOG(ERROR, TAG, "PIN buffer is NULL");
90         return OC_STACK_INVALID_PARAM;
91     }
92     if(OXM_RANDOM_PIN_SIZE + 1 > bufferSize)
93     {
94         OC_LOG(ERROR, TAG, "PIN buffer size is too small");
95         return OC_STACK_INVALID_PARAM;
96     }
97
98     if(gInputPinCallback)
99     {
100         gInputPinCallback(pinBuffer, OXM_RANDOM_PIN_SIZE + 1);
101     }
102     else
103     {
104         OC_LOG(ERROR, TAG, "Invoke PIN callback failed!");
105         OC_LOG(ERROR, TAG, "Callback for input PIN should be registered to use PIN based OxM.");
106         return OC_STACK_ERROR;
107     }
108
109     return OC_STACK_OK;
110 }