Memory leak issue fixed
[platform/core/account/fido-asm.git] / common / uafv1tlvutil / src / RegReqTlvEncoder.cpp
1 /*
2  * Copyright (c) 2014 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "RegReqTlvEncoder.h"
19 #include "Tlv.h"
20 #include "AsmHelper.h"
21 #include "AuthnrTypes.h"
22
23 RegAssertionTlvEncoder::RegAssertionTlvEncoder(void) {}
24
25 Buffer *
26 RegAssertionTlvEncoder::encode(const void *authData)
27 {
28         _INFO("RegAssertionTlvEncoder::encode start");
29
30         RET_IF_FAIL(authData != NULL, NULL);
31
32         _INFO("RegAssertionTlvEncoder::encode start with valid ip");
33
34
35         RegisterReq *getRegReq = (RegisterReq*)authData;
36         _INFO("RegAssertionTlvEncoder::encode 2");
37
38         tlv_builder_s *builder = tlv_builder_create();
39         _INFO("RegAssertionTlvEncoder::encode 3");
40
41         tlv_builder_start_composite(builder, TAG_UAFV1_REGISTER_CMD);
42         _INFO("RegAssertionTlvEncoder::encode 4");
43
44         tlv_builder_add_int8(builder, TAG_AUTHENTICATOR_INDEX, getRegReq->authIdx);
45         _INFO("RegAssertionTlvEncoder::encode 5");
46
47         if (getRegReq->appId != NULL) {
48                 tlv_builder_add_string(builder, TAG_APPID, getRegReq->appId);
49                 _INFO("RegAssertionTlvEncoder::encode 6");
50         }
51
52         tlv_builder_add_buffer(builder, TAG_FINAL_CHALLENGE, getRegReq->fch);
53         _INFO("RegAssertionTlvEncoder::encode 7");
54
55         tlv_builder_add_string(builder, TAG_USERNAME, getRegReq->userName);
56         _INFO("RegAssertionTlvEncoder::encode 8");
57
58         tlv_builder_add_int16(builder, TAG_ATTESTATION_TYPE, getRegReq->attType);
59         _INFO("RegAssertionTlvEncoder::encode 9");
60
61         tlv_builder_add_buffer(builder, TAG_KEYHANDLE_ACCESS_TOKEN, getRegReq->khAccessToken);
62         _INFO("RegAssertionTlvEncoder::encode 10");
63
64         if (getRegReq->userVToken != NULL) {
65                 tlv_builder_add_string(builder, TAG_USERVERIFY_TOKEN, getRegReq->userVToken);
66                 _INFO("RegAssertionTlvEncoder::encode 11");
67         }
68
69         tlv_builder_end_composite(builder);
70         _INFO("RegAssertionTlvEncoder::encode 12");
71
72         Buffer *buff = tlv_builder_get_buffer(builder);
73         SAFE_DELETE(builder);
74
75         return buff;
76 }
77
78 void*
79 RegAssertionTlvEncoder::decode(const unsigned char *rawData)
80 {
81         _INFO("RegAssertionInfoTlvEncoder::decode start");
82         RET_IF_FAIL(rawData != NULL, NULL);
83
84         tlv_s* root = tlv_decode(rawData);
85         RET_IF_FAIL(root != NULL, NULL);
86         if (root->tag != TAG_UAFV1_REGISTER_CMD) {
87                 SAFE_DELETE(root->val);
88                 SAFE_DELETE(root);
89                 return NULL;
90         }
91
92         int rawIter = 2 + 2;
93         int endIter = rawIter + root->len;
94
95         tlv_s* child = tlv_decode(root->val);
96         if (child == NULL) {
97                 SAFE_DELETE(root->val);
98                 SAFE_DELETE(root);
99
100                 return NULL;
101         }
102
103         bool foundMember = false;
104         RegisterReq *getRegReqInfo = ALLOC(RegisterReq);
105
106         while(1) {
107                 switch(child->tag) {
108                 case TAG_AUTHENTICATOR_INDEX:
109                 {
110                         _INFO("");
111                         int *pauthIdx = (int*)__intTlvEncoder.decode(rawData + rawIter);
112                         getRegReqInfo->authIdx = *pauthIdx;
113                         _INFO("authIdx = [%d]", getRegReqInfo->authIdx);
114                         SAFE_DELETE(pauthIdx);
115                         foundMember = true;
116                         break;
117                 }
118                 case TAG_APPID:
119                         _INFO("");
120                         getRegReqInfo->appId = (char*)__strTlvEncoder.decode(rawData + rawIter);
121                         _INFO("appId = [%s]", getRegReqInfo->appId);
122                         foundMember = true;
123                         break;
124
125                 case TAG_FINAL_CHALLENGE:
126                         _INFO("");
127                         getRegReqInfo->fch = (Buffer*)__buffTlvEncoder.decode(rawData + rawIter);
128                         foundMember = true;
129                         break;
130
131                 case TAG_USERNAME:
132                         _INFO("");
133                         getRegReqInfo->userName = (char*)__strTlvEncoder.decode(rawData + rawIter);
134                         _INFO("userName = [%s]", getRegReqInfo->userName);
135                         foundMember = true;
136                         break;
137
138                 case TAG_ATTESTATION_TYPE:
139                 {
140                         _INFO("");
141                         int *pattType = (int*)__intTlvEncoder.decode(rawData + rawIter);
142                         getRegReqInfo->attType = *pattType;
143                         _INFO("attType = [%d]", getRegReqInfo->attType);
144                         SAFE_DELETE(pattType);
145                         foundMember = true;
146                         break;
147                 }
148                 case TAG_KEYHANDLE_ACCESS_TOKEN:
149                         _INFO("");
150                         getRegReqInfo->khAccessToken = (Buffer*)__buffTlvEncoder.decode(rawData + rawIter);
151                         if (getRegReqInfo->khAccessToken == NULL) {
152                                 SAFE_DELETE(root->val);
153                                 SAFE_DELETE(root);
154                                 SAFE_DELETE(child->val);
155                                 SAFE_DELETE(child);
156                                 SAFE_DELETE(getRegReqInfo);
157                                 return NULL;
158                         }
159                         _INFO("khAccessToken = [%s]", getRegReqInfo->khAccessToken->data);
160                         foundMember = true;
161                         break;
162
163                 case TAG_USERVERIFY_TOKEN:
164                         _INFO("");
165                         getRegReqInfo->userVToken = (char*)__strTlvEncoder.decode(rawData + rawIter);
166                         _INFO("userVToken = [%s]", getRegReqInfo->userVToken);
167                         foundMember = true;
168                         break;
169
170                 default:
171                         foundMember = false;
172                         break;
173                 }
174
175                 rawIter = rawIter + 2 + 2 + child->len;
176                 if (rawIter >= endIter)
177                         break;
178
179                 SAFE_DELETE(child->val);
180                 SAFE_DELETE(child);
181                 child = tlv_decode(rawData + rawIter);
182                 if (child == NULL) {
183                         SAFE_DELETE(root->val);
184                         SAFE_DELETE(root);
185
186                         SAFE_DELETE(getRegReqInfo);
187                         return NULL;
188                 }
189         }
190
191         SAFE_DELETE(child->val);
192         SAFE_DELETE(child);
193         SAFE_DELETE(root->val);
194         SAFE_DELETE(root);
195
196         if (foundMember == false) {
197                 SAFE_DELETE(getRegReqInfo->khAccessToken);
198                 SAFE_DELETE(getRegReqInfo->fch);
199                 free(getRegReqInfo);
200                 return NULL;
201         }
202
203         SAFE_DELETE(root);
204         _INFO("RegAssertionInfoTlvEncoder::decode end");
205         return getRegReqInfo;
206 }