b22a342e9d4abd71a10468b9b9cb1a2b0aa6d2ca
[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(getRegReqInfo);
155                                 return NULL;
156                         }
157                         _INFO("khAccessToken = [%s]", getRegReqInfo->khAccessToken->data);
158                         foundMember = true;
159                         break;
160
161                 case TAG_USERVERIFY_TOKEN:
162                         _INFO("");
163                         getRegReqInfo->userVToken = (char*)__strTlvEncoder.decode(rawData + rawIter);
164                         _INFO("userVToken = [%s]", getRegReqInfo->userVToken);
165                         foundMember = true;
166                         break;
167
168                 default:
169                         foundMember = false;
170                         break;
171                 }
172
173                 rawIter = rawIter + 2 + 2 + child->len;
174                 if (rawIter >= endIter)
175                         break;
176
177                 SAFE_DELETE(child->val);
178                 SAFE_DELETE(child);
179                 child = tlv_decode(rawData + rawIter);
180                 if (child == NULL) {
181                         SAFE_DELETE(root->val);
182                         SAFE_DELETE(root);
183
184                         SAFE_DELETE(getRegReqInfo);
185                         return NULL;
186                 }
187         }
188
189         SAFE_DELETE(child->val);
190         SAFE_DELETE(child);
191         SAFE_DELETE(root->val);
192         SAFE_DELETE(root);
193
194         if (foundMember == false) {
195                 SAFE_DELETE(getRegReqInfo->khAccessToken);
196                 SAFE_DELETE(getRegReqInfo->fch);
197                 free(getRegReqInfo);
198                 return NULL;
199         }
200
201         SAFE_DELETE(root);
202         _INFO("RegAssertionInfoTlvEncoder::decode end");
203         return getRegReqInfo;
204 }