2 * Copyright (c) 2014 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "SignedDataTlvEncoder.h"
20 #include "AsmHelper.h"
21 #include "AuthnrTypes.h"
23 SignedDataTlvEncoder::SignedDataTlvEncoder(void) {}
26 SignedDataTlvEncoder::encode(const void *authData)
29 RET_IF_FAIL(authData != NULL, NULL);
31 SigData *getSigDataInfo = (SigData*)authData;
33 tlv_builder_s *builder = tlv_builder_create();
35 /*1.2 TAG_UAFV1_SIGNED_DATA*/
36 tlv_builder_start_composite(builder, TAG_UAFV1_SIGNED_DATA);
39 tlv_builder_add_string(builder, TAG_AAID, getSigDataInfo->aaid);
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);
51 /*1.2.4 TAG_AUTHENTICATOR_NONCE*/
52 tlv_builder_add_buffer(builder, TAG_AUTHENTICATOR_NONCE, getSigDataInfo->authNonce);
54 /*1.2.5 TAG_FINAL_CHALLENGE*/
55 tlv_builder_add_buffer(builder, TAG_FINAL_CHALLENGE, getSigDataInfo->fch);
57 /*1.2.6 TAG_TRANSACTION_CONTENT_HASH*/
58 tlv_builder_add_buffer(builder, TAG_TRANSACTION_CONTENT_HASH, getSigDataInfo->tcHash);
61 tlv_builder_add_buffer(builder, TAG_KEYID, getSigDataInfo->keyId);
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);
72 tlv_builder_end_composite(builder);
75 Buffer *buff = tlv_builder_get_buffer(builder);
82 SignedDataTlvEncoder::decode(const unsigned char *rawData)
84 _INFO("SignedDataTlvEncoder::decode start");
85 RET_IF_FAIL(rawData != NULL, NULL);
87 tlv_s *root = tlv_decode(rawData);
88 RET_IF_FAIL(root != NULL, NULL);
91 int endIter = rawIter + root->len;
93 tlv_s *child = tlv_decode(rawData + rawIter);
95 SAFE_DELETE(root->val);
101 SigData *getSigDataInfo = ALLOC(SigData);
103 bool foundMember = false;
108 getSigDataInfo->aaid = (char*)__strTlvEncoder.decode(rawData + rawIter);
112 case TAG_ASSERTION_INFO:
113 getSigDataInfo->assrtInfo = (AssertionInfo*)__assertionInfo.decode(rawData + rawIter);
117 case TAG_AUTHENTICATOR_NONCE:
118 getSigDataInfo->authNonce = (Buffer*)__buffTlvEncoder.decode(rawData + rawIter);
122 case TAG_FINAL_CHALLENGE:
123 getSigDataInfo->fch = (Buffer*)__buffTlvEncoder.decode(rawData + rawIter);
127 case TAG_TRANSACTION_CONTENT_HASH:
128 getSigDataInfo->tcHash = (Buffer*)__buffTlvEncoder.decode(rawData + rawIter);
133 getSigDataInfo->keyId = (Buffer*)__buffTlvEncoder.decode(rawData + rawIter);
138 getSigDataInfo->counter = (Counters*)__counterInfo.decode(rawData + rawIter);
147 rawIter = rawIter + 2 + 2 + child->len;
148 if (rawIter >= endIter)
151 SAFE_DELETE(child->val);
153 child = tlv_decode(rawData + rawIter);
155 SAFE_DELETE(root->val);
158 SAFE_DELETE(getSigDataInfo->assrtInfo);
159 SAFE_DELETE(getSigDataInfo);
165 SAFE_DELETE(child->val);
167 SAFE_DELETE(root->val);
170 if (foundMember == false) {
172 SAFE_DELETE(getSigDataInfo->assrtInfo);
173 SAFE_DELETE(getSigDataInfo->keyId);
174 free(getSigDataInfo);
177 _INFO("SignedDataTlvEncoder::decode ebd");
178 return getSigDataInfo;