2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 // @file FSecCert_CertTime.cpp
20 // @brief This file contains implementation of X509 Certificate Time.
31 #include <unique_ptr.h>
32 #include <FBaseByteBuffer.h>
33 #include <FBase_StringConverter.h>
34 #include <FLclLocaleManager.h>
35 #include <FLclDateTimeFormatter.h>
36 #include <FBaseResult.h>
37 #include <FBaseSysLog.h>
38 #include "FSecCert_CertTime.h"
40 using namespace Tizen::Base;
41 using namespace Tizen::System;
42 using namespace Tizen::Locales;
44 namespace Tizen { namespace Security { namespace Cert
49 _CertTime::FormatDateTime(Tizen::Base::DateTime& time, char* pFormattedDatTime)
53 String cutomizedPattern = L"yyyy-MM-dd HH:mm:ss";
54 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 formet.", 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 memcpy(pFormattedDatTime, pTemp.get(), strlen(pTemp.get()) + 1);
79 _CertTime::_CertTime(void)
81 memset(__value, 0, _MAX_CERT_TIME_LEN + 1);
84 _CertTime::_CertTime(_CertTimeType type, byte* pValue)
86 // CertExtType = type;
87 SysTryReturnVoidResult(NID_SEC_CERT, pValue != null, E_INVALID_ARG, "Invalid input argument.");
88 SetTime(type, pValue);
91 _CertTime::~_CertTime(void)
97 _CertTime::SetTime(_CertTimeType type, byte* pValue)
103 SysTryReturnVoidResult(NID_SEC_CERT, pValue != null, E_INVALID_ARG, "Invalid input argument.");
105 if (type == CERT_TIME_UTC)
107 byte tmpTime[_MAX_CERT_TIME_LEN + 1] = {0, };
110 strcpy(reinterpret_cast< char* >(tmpTime), reinterpret_cast< const char* >("20"));
114 strcpy(reinterpret_cast< char* >(tmpTime), reinterpret_cast< const char* >("19"));
117 strcat(reinterpret_cast< char* >(tmpTime), reinterpret_cast< char* >(pValue));
118 strcpy(reinterpret_cast< char* >(__value), reinterpret_cast< char* >(tmpTime));
122 len = strlen((reinterpret_cast< const char* >(pValue)));
123 SysTryReturnVoidResult(NID_SEC_CERT, len <= _MAX_CERT_TIME_LEN, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] Timestamp string's length out of range.");
125 strcpy(reinterpret_cast< char* >(__value), reinterpret_cast< char* >(pValue));
130 _CertTime::SetTime(byte* pValue)
133 SysTryReturnVoidResult(NID_SEC_CERT, pValue != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument.");
135 memcpy(__value, pValue, _MAX_CERT_TIME_LEN + 1);
139 _CertTime::GetTime(void)
146 _CertTime::ConvertToDateTime(Tizen::Base::DateTime& cmTime)
148 char tYear[5] = {0, };
149 char tMonth[3] = {0, };
150 char tDay[3] = {0, };
151 char tHour[3] = {0, };
152 char tMinute[3] = {0, };
153 char tSecond[3] = {0, };
157 strncpy(tYear, reinterpret_cast< const char* >(&__value[0]), 4);
159 strncpy(tMonth, reinterpret_cast< const char* >(&__value[4]), 2);
161 strncpy(tDay, reinterpret_cast< const char* >(&__value[6]), 2);
163 strncpy(tHour, reinterpret_cast< const char* >(&__value[8]), 2);
165 strncpy(tMinute, reinterpret_cast< const char* >(&__value[10]), 2);
167 strncpy(tSecond, reinterpret_cast< const char* >(&__value[12]), 2);
169 cmTime.SetValue((atoi(tYear) % 10000), ((atoi(tMonth)) % 13), ((atoi(tDay)) % 32), ((atoi(tHour)) % 24), ((atoi(tMinute)) % 60),
170 ((atoi(tSecond)) % 60));
173 } } } //Tizen::Security::Cert