2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 // @file FSecCert_CertTime.cpp
19 // @brief This file contains implementation of X509 Certificate Time.
30 #include <unique_ptr.h>
31 #include <FBaseByteBuffer.h>
32 #include <FBase_StringConverter.h>
33 #include <FLclLocaleManager.h>
34 #include <FLclDateTimeFormatter.h>
35 #include <FBaseResult.h>
36 #include <FBaseSysLog.h>
37 #include "FSecCert_CertTime.h"
39 using namespace Tizen::Base;
40 using namespace Tizen::System;
41 using namespace Tizen::Locales;
43 namespace Tizen { namespace Security { namespace Cert
48 _CertTime::FormatDateTime(Tizen::Base::DateTime& time, char* pFormattedDatTime)
52 String cutomizedPattern = L"yyyy-MM-dd HH:mm:ss";
53 LocaleManager localeManager;
57 SysTryReturnVoidResult(NID_SEC_CERT, pFormattedDatTime != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument.");
59 r = localeManager.Construct();
60 SysTryReturnVoidResult(NID_SEC_CERT, !IsFailed(r), E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
62 Locale systemLocale = localeManager.GetSystemLocale();
64 std::unique_ptr< DateTimeFormatter > pDateFormatter(DateTimeFormatter::CreateDateFormatterN(systemLocale, DATE_TIME_STYLE_FULL));
65 SysTryReturnVoidResult(NID_SEC_CERT, pDateFormatter != null, GetLastResult(), "[%s] Failed to create date formet.", GetErrorMessage(GetLastResult()));
67 pDateFormatter->ApplyPattern(cutomizedPattern);
69 r = pDateFormatter->Format(time, formattedStr);
70 SysTryReturnVoidResult(NID_SEC_CERT, !IsFailed(r), r, "[%s] Failed to create time format.", GetErrorMessage(r));
72 std::unique_ptr< char[] > pTemp(Tizen::Base::_StringConverter::CopyToCharArrayN(formattedStr));
73 SysTryReturnVoidResult(NID_SEC_CERT, pTemp != null, GetLastResult(), "[%s] Failed to convert string array.", GetErrorMessage(GetLastResult()));
75 len = strlen(pTemp.get());
76 memcpy(pFormattedDatTime, pTemp.get(), len);
77 pFormattedDatTime[len] = '\0';
82 _CertTime::_CertTime(void)
84 memset(__value, 0, _MAX_CERT_TIME_LEN + 1);
87 _CertTime::_CertTime(_CertTimeType type, byte* pValue)
89 // CertExtType = type;
90 SysTryReturnVoidResult(NID_SEC_CERT, pValue != null, E_INVALID_ARG, "Invalid input argument.");
91 SetTime(type, pValue);
94 _CertTime::~_CertTime(void)
100 _CertTime::SetTime(_CertTimeType type, byte* pValue)
106 SysTryReturnVoidResult(NID_SEC_CERT, pValue != null, E_INVALID_ARG, "Invalid input argument.");
108 if (type == CERT_TIME_UTC)
110 byte tmpTime[_MAX_CERT_TIME_LEN + 1] = {0, };
113 strcpy(reinterpret_cast< char* >(tmpTime), reinterpret_cast< const char* >("20"));
117 strcpy(reinterpret_cast< char* >(tmpTime), reinterpret_cast< const char* >("19"));
120 strcat(reinterpret_cast< char* >(tmpTime), reinterpret_cast< char* >(pValue));
121 strcpy(reinterpret_cast< char* >(__value), reinterpret_cast< char* >(tmpTime));
125 len = strlen((reinterpret_cast< const char* >(pValue)));
126 SysTryReturnVoidResult(NID_SEC_CERT, len <= _MAX_CERT_TIME_LEN, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] Timestamp string's length out of range.");
128 strcpy(reinterpret_cast< char* >(__value), reinterpret_cast< char* >(pValue));
133 _CertTime::SetTime(byte* pValue)
136 SysTryReturnVoidResult(NID_SEC_CERT, pValue != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument.");
138 memcpy(__value, pValue, _MAX_CERT_TIME_LEN + 1);
142 _CertTime::GetTime(void)
149 _CertTime::ConvertToDateTime(Tizen::Base::DateTime& cmTime)
151 char tYear[5] = {0, };
152 char tMonth[3] = {0, };
153 char tDay[3] = {0, };
154 char tHour[3] = {0, };
155 char tMinute[3] = {0, };
156 char tSecond[3] = {0, };
160 strncpy(tYear, reinterpret_cast< const char* >(&__value[0]), 4);
162 strncpy(tMonth, reinterpret_cast< const char* >(&__value[4]), 2);
164 strncpy(tDay, reinterpret_cast< const char* >(&__value[6]), 2);
166 strncpy(tHour, reinterpret_cast< const char* >(&__value[8]), 2);
168 strncpy(tMinute, reinterpret_cast< const char* >(&__value[10]), 2);
170 strncpy(tSecond, reinterpret_cast< const char* >(&__value[12]), 2);
172 cmTime.SetValue((atoi(tYear) % 10000), ((atoi(tMonth)) % 13), ((atoi(tDay)) % 32), ((atoi(tHour)) % 24), ((atoi(tMinute)) % 60),
173 ((atoi(tSecond)) % 60));
176 } } } //Tizen::Security::Cert