2 * Copyright (c) 2011 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.
16 #include <vcore/TimeConversion.h>
20 #include <dpl/log/log.h>
21 #include <dpl/assert.h>
23 namespace ValidationCore {
25 int asn1TimeToTimeT(ASN1_TIME *t, time_t *res)
31 if (!ASN1_TIME_check(t)) {
35 memset(&tm, 0, sizeof(tm));
37 #define g2(p) (((p)[0] - '0') * 10 + (p)[1] - '0')
38 if (t->type == V_ASN1_UTCTIME) {
39 Assert(t->length > 12);
41 /* this code is copied from OpenSSL asn1/a_utctm.c file */
42 tm.tm_year = g2(t->data);
43 if (tm.tm_year < 50) {
46 tm.tm_mon = g2(t->data + 2) - 1;
47 tm.tm_mday = g2(t->data + 4);
48 tm.tm_hour = g2(t->data + 6);
49 tm.tm_min = g2(t->data + 8);
50 tm.tm_sec = g2(t->data + 10);
51 if (t->data[12] == 'Z') {
54 Assert(t->length > 16);
56 offset = g2(t->data + 13) * 60 + g2(t->data + 15);
57 if (t->data[12] == '-') {
63 Assert(t->length > 14);
65 tm.tm_year = g2(t->data) * 100 + g2(t->data + 2);
66 tm.tm_mon = g2(t->data + 4) - 1;
67 tm.tm_mday = g2(t->data + 6);
68 tm.tm_hour = g2(t->data + 8);
69 tm.tm_min = g2(t->data + 10);
70 tm.tm_sec = g2(t->data + 12);
71 if (t->data[14] == 'Z') {
74 Assert(t->length > 18);
76 offset = g2(t->data + 15) * 60 + g2(t->data + 17);
77 if (t->data[14] == '-') {
84 (*res) = timegm(&tm) - offset * 60;
88 int asn1GeneralizedTimeToTimeT(ASN1_GENERALIZEDTIME *tm, time_t *res)
91 * This code is based on following assumption:
92 * from openssl/a_gentm.c:
93 * GENERALIZEDTIME is similar to UTCTIME except the year is
94 * represented as YYYY. This stuff treats everything as a two digit
95 * field so make first two fields 00 to 99
97 const int DATE_BUFFER_LENGTH = 15; // YYYYMMDDHHMMSSZ
99 if (NULL == res || NULL == tm) {
100 LogError("NULL pointer");
104 if (DATE_BUFFER_LENGTH != tm->length || NULL == tm->data) {
105 LogError("Invalid ASN1_GENERALIZEDTIME");
110 if (sscanf ((char*)tm->data,
111 "%4d%2d%2d%2d%2d%2d",
119 LogError("Could not extract time data from ASN1_GENERALIZEDTIME");
123 time_s.tm_year -= 1900;
125 time_s.tm_isdst = 0; // UTC
126 time_s.tm_gmtoff = 0; // UTC
127 time_s.tm_zone = NULL; // UTC
129 *res = mktime(&time_s);
134 } // namespace ValidationCore