memory leak issue fixed (52775)
[platform/core/account/fido-asm.git] / common / uafv1tlvutil / src / SignedDataTlvEncoder.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 "SignedDataTlvEncoder.h"
19 #include "Tlv.h"
20 #include "AsmHelper.h"
21 #include "AuthnrTypes.h"
22
23 SignedDataTlvEncoder::SignedDataTlvEncoder(void) {}
24
25 Buffer *
26 SignedDataTlvEncoder::encode(const void *authData)
27 {
28         _BEGIN;
29         RET_IF_FAIL(authData != NULL, NULL);
30
31         SigData *getSigDataInfo = (SigData*)authData;
32
33         tlv_builder_s *builder = tlv_builder_create();
34
35         /*1.2 TAG_UAFV1_SIGNED_DATA*/
36         tlv_builder_start_composite(builder, TAG_UAFV1_SIGNED_DATA);
37
38         /*1.2.2 TAG_AAID*/
39         tlv_builder_add_string(builder, TAG_AAID, getSigDataInfo->aaid);
40
41         if (getSigDataInfo->assrtInfo != NULL) {
42                 Buffer *getAssertionInfoBuff = __assertionInfo.encode(getSigDataInfo->assrtInfo);
43                 if (getAssertionInfoBuff != NULL) {
44                         /*1.2.3 TAG_ASSERTION_INFO*/
45                         tlv_builder_add_buffer(builder, TAG_ASSERTION_INFO, getAssertionInfoBuff);
46                         SAFE_DELETE(getAssertionInfoBuff->data);
47                         SAFE_DELETE(getAssertionInfoBuff);
48                 }
49         }
50
51         /*1.2.4 TAG_AUTHENTICATOR_NONCE*/
52         tlv_builder_add_buffer(builder, TAG_AUTHENTICATOR_NONCE, getSigDataInfo->authNonce);
53
54         /*1.2.5 TAG_FINAL_CHALLENGE*/
55         tlv_builder_add_buffer(builder, TAG_FINAL_CHALLENGE, getSigDataInfo->fch);
56
57         /*1.2.6 TAG_TRANSACTION_CONTENT_HASH*/
58         tlv_builder_add_buffer(builder, TAG_TRANSACTION_CONTENT_HASH, getSigDataInfo->tcHash);
59
60         /*1.2.7 TAG_KEYID*/
61         tlv_builder_add_buffer(builder, TAG_KEYID, getSigDataInfo->keyId);
62
63         if (getSigDataInfo->counter != NULL) {
64                 Buffer *getCounterInfoBuff = __counterInfo.encode(getSigDataInfo->counter);
65                 if (getCounterInfoBuff != NULL) {
66                         /*1.2.8 TAG_COUNTERS*/
67                         tlv_builder_add_buffer(builder, TAG_COUNTERS, getCounterInfoBuff);
68                 }
69         }
70
71         /*1.2 end*/
72         tlv_builder_end_composite(builder);
73
74         _END;
75         Buffer *buff = tlv_builder_get_buffer(builder);
76         SAFE_DELETE(builder);
77
78         return buff;
79 }
80
81 void *
82 SignedDataTlvEncoder::decode(const unsigned char *rawData)
83 {
84         _INFO("SignedDataTlvEncoder::decode start");
85         RET_IF_FAIL(rawData != NULL, NULL);
86
87         tlv_s *root = tlv_decode(rawData);
88         RET_IF_FAIL(root != NULL, NULL);
89
90         int rawIter = 2 + 2;
91         int endIter = rawIter + root->len;
92
93         tlv_s *child = tlv_decode(rawData + rawIter);
94         if (child == NULL) {
95                 SAFE_DELETE(root->val);
96                 SAFE_DELETE(root);
97
98                 return NULL;
99         }
100
101         SigData *getSigDataInfo = ALLOC(SigData);
102
103         bool foundMember = false;
104
105         while(1) {
106                 switch(child->tag) {
107                 case TAG_AAID:
108                         getSigDataInfo->aaid = (char*)__strTlvEncoder.decode(rawData + rawIter);
109                         foundMember = true;
110                         break;
111
112                 case TAG_ASSERTION_INFO:
113                         getSigDataInfo->assrtInfo = (AssertionInfo*)__assertionInfo.decode(rawData + rawIter);
114                         foundMember = true;
115                         break;
116
117                 case TAG_AUTHENTICATOR_NONCE:
118                         getSigDataInfo->authNonce = (Buffer*)__buffTlvEncoder.decode(rawData + rawIter);
119                         foundMember = true;
120                         break;
121
122                 case TAG_FINAL_CHALLENGE:
123                         getSigDataInfo->fch = (Buffer*)__buffTlvEncoder.decode(rawData + rawIter);
124                         foundMember = true;
125                         break;
126
127                 case TAG_TRANSACTION_CONTENT_HASH:
128                         getSigDataInfo->tcHash = (Buffer*)__buffTlvEncoder.decode(rawData + rawIter);
129                         foundMember = true;
130                         break;
131
132                 case TAG_KEYID:
133                         getSigDataInfo->keyId = (Buffer*)__buffTlvEncoder.decode(rawData + rawIter);
134                         foundMember = true;
135                         break;
136
137                 case TAG_COUNTERS:
138                         getSigDataInfo->counter = (Counters*)__counterInfo.decode(rawData + rawIter);
139                         foundMember = true;
140                         break;
141
142                 default:
143                         foundMember = false;
144                         break;
145                 }
146
147                 rawIter = rawIter + 2 + 2 + child->len;
148                 if (rawIter >= endIter)
149                         break;
150
151                 SAFE_DELETE(child->val);
152                 SAFE_DELETE(child);
153                 child = tlv_decode(rawData + rawIter);
154                 if (child == NULL) {
155                         SAFE_DELETE(root->val);
156                         SAFE_DELETE(root);
157
158                         SAFE_DELETE(getSigDataInfo->assrtInfo);
159                         SAFE_DELETE(getSigDataInfo);
160
161                         return NULL;
162                 }
163         }
164
165         SAFE_DELETE(child->val);
166         SAFE_DELETE(child);
167         SAFE_DELETE(root->val);
168         SAFE_DELETE(root);
169
170         if (foundMember == false) {
171
172                 SAFE_DELETE(getSigDataInfo->assrtInfo);
173                 SAFE_DELETE(getSigDataInfo->keyId);
174                 free(getSigDataInfo);
175                 return NULL;
176         }
177         _INFO("SignedDataTlvEncoder::decode ebd");
178         return getSigDataInfo;
179 }