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