cedb913554c63a9885aa7a3b55f8ed0174da556a
[platform/framework/native/appfw.git] / src / security / cert / FSecCert_CertTime.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 //
19 // @file                FSecCert_CertTime.cpp
20 // @brief               This file contains implementation of X509 Certificate Time.
21 //
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <error.h>
26 #include <memory.h>
27 #include <new>
28 #include <sys/stat.h>
29 #include <assert.h>
30 #include <dirent.h>
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"
39
40 using namespace Tizen::Base;
41 using namespace Tizen::System;
42 using namespace Tizen::Locales;
43
44 namespace Tizen { namespace Security { namespace Cert
45 {
46
47
48 void
49 _CertTime::FormatDateTime(Tizen::Base::DateTime& time, char* pFormattedDatTime)
50 {
51         result r = E_SUCCESS;
52         String formattedStr;
53         String cutomizedPattern = L"yyyy-MM-dd HH:mm:ss";
54         LocaleManager localeManager;
55
56         ClearLastResult();
57         SysTryReturnVoidResult(NID_SEC_CERT, pFormattedDatTime != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument.");
58
59         r = localeManager.Construct();
60         SysTryReturnVoidResult(NID_SEC_CERT, !IsFailed(r), E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
61
62         Locale systemLocale = localeManager.GetSystemLocale();
63
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()));
66
67         pDateFormatter->ApplyPattern(cutomizedPattern);
68
69         r = pDateFormatter->Format(time, formattedStr);
70         SysTryReturnVoidResult(NID_SEC_CERT, !IsFailed(r), r, "[%s] Failed to create time formet.", GetErrorMessage(r));
71
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()));
74
75         memcpy(pFormattedDatTime, pTemp.get(), strlen(pTemp.get()) + 1);
76 }
77
78
79 _CertTime::_CertTime(void)
80 {
81         memset(__value, 0, _MAX_CERT_TIME_LEN + 1);
82 }
83
84 _CertTime::_CertTime(_CertTimeType type, byte* pValue)
85 {
86         // CertExtType = type;
87         SysTryReturnVoidResult(NID_SEC_CERT, pValue != null, E_INVALID_ARG, "Invalid input argument.");
88         SetTime(type, pValue);
89 }
90
91 _CertTime::~_CertTime(void)
92 {
93         //Empty statement
94 }
95
96 void
97 _CertTime::SetTime(_CertTimeType type, byte* pValue)
98 {
99         int len = 0;
100         // YYMMDDHHMMSSZ
101
102         ClearLastResult();
103         SysTryReturnVoidResult(NID_SEC_CERT, pValue != null, E_INVALID_ARG, "Invalid input argument.");
104
105         if (type == CERT_TIME_UTC)
106         {
107                 byte tmpTime[_MAX_CERT_TIME_LEN + 1] = {0, };
108                 if (pValue[0] < '5')
109                 {
110                         strcpy(reinterpret_cast< char* >(tmpTime), reinterpret_cast< const char* >("20"));
111                 }
112                 else
113                 {
114                         strcpy(reinterpret_cast< char* >(tmpTime), reinterpret_cast< const char* >("19"));
115                 }
116
117                 strcat(reinterpret_cast< char* >(tmpTime), reinterpret_cast< char* >(pValue));
118                 strcpy(reinterpret_cast< char* >(__value), reinterpret_cast< char* >(tmpTime));
119         }
120         else
121         {
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.");
124
125                 strcpy(reinterpret_cast< char* >(__value), reinterpret_cast< char* >(pValue));
126         }
127 }
128
129 void
130 _CertTime::SetTime(byte* pValue)
131 {
132         ClearLastResult();
133         SysTryReturnVoidResult(NID_SEC_CERT, pValue != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument.");
134
135         memcpy(__value, pValue, _MAX_CERT_TIME_LEN + 1);
136 }
137
138 byte*
139 _CertTime::GetTime(void)
140 {
141         ClearLastResult();
142         return __value;
143 }
144
145 void
146 _CertTime::ConvertToDateTime(Tizen::Base::DateTime& cmTime)
147 {
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, };
154
155         ClearLastResult();
156
157         strncpy(tYear, reinterpret_cast< const char* >(&__value[0]), 4);
158         tYear[4] = '\0';
159         strncpy(tMonth, reinterpret_cast< const char* >(&__value[4]), 2);
160         tMonth[2] = '\0';
161         strncpy(tDay, reinterpret_cast< const char* >(&__value[6]), 2);
162         tDay[2] = '\0';
163         strncpy(tHour, reinterpret_cast< const char* >(&__value[8]), 2);
164         tHour[2] = '\0';
165         strncpy(tMinute, reinterpret_cast< const char* >(&__value[10]), 2);
166         tMinute[2] = '\0';
167         strncpy(tSecond, reinterpret_cast< const char* >(&__value[12]), 2);
168         tSecond[2] = '\0';
169         cmTime.SetValue((atoi(tYear) % 10000), ((atoi(tMonth)) % 13), ((atoi(tDay)) % 32), ((atoi(tHour)) % 24), ((atoi(tMinute)) % 60),
170                                         ((atoi(tSecond)) % 60));
171 }
172
173 } } } //Tizen::Security::Cert