]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/o_time.c
Fix typo in HKDF example documentation
[thirdparty/openssl.git] / crypto / o_time.c
CommitLineData
0f113f3e 1/*
b1322259 2 * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
b8e35bd6 3 *
b1322259
RS
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
b8e35bd6
RL
8 */
9
10#include <openssl/e_os2.h>
5cd6571f 11#include <string.h>
e52a3c3d 12#include <openssl/crypto.h>
b8e35bd6 13
b8e35bd6 14struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
0f113f3e
MC
15{
16 struct tm *ts = NULL;
b8e35bd6 17
48ce800a
RL
18#if defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_VMS)
19 {
20 /*
21 * On VMS, gmtime_r() takes a 32-bit pointer as second argument.
22 * Since we can't know that |result| is in a space that can easily
23 * translate to a 32-bit pointer, we must store temporarly on stack
24 * and copy the result. The stack is always reachable with 32-bit
25 * pointers.
26 */
27#if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE
28# pragma pointer_size save
29# pragma pointer_size 32
30#endif
31 struct tm data, *ts2 = &data;
32#if defined OPENSSL_SYS_VMS && __INITIAL_POINTER_SIZE
33# pragma pointer_size restore
34#endif
35 if (gmtime_r(timer, ts2) == NULL)
36 return NULL;
37 memcpy(result, ts2, sizeof(struct tm));
38 ts = result;
39 }
40#elif defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_MACOSX)
873019f2
GV
41 if (gmtime_r(timer, result) == NULL)
42 return NULL;
0f113f3e 43 ts = result;
9d70ac97 44#else
0f113f3e
MC
45 ts = gmtime(timer);
46 if (ts == NULL)
47 return NULL;
52c4c51f 48
0f113f3e
MC
49 memcpy(result, ts, sizeof(struct tm));
50 ts = result;
b8e35bd6 51#endif
0f113f3e
MC
52 return ts;
53}
87d3a0cd 54
0f113f3e
MC
55/*
56 * Take a tm structure and add an offset to it. This avoids any OS issues
87d3a0cd
DSH
57 * with restricted date types and overflows which cause the year 2038
58 * problem.
59 */
60
61#define SECS_PER_DAY (24 * 60 * 60)
62
63static long date_to_julian(int y, int m, int d);
64static void julian_to_date(long jd, int *y, int *m, int *d);
46a6cec6 65static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
0f113f3e 66 long *pday, int *psec);
87d3a0cd
DSH
67
68int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
0f113f3e
MC
69{
70 int time_sec, time_year, time_month, time_day;
71 long time_jd;
72
0d4fb843 73 /* Convert time and offset into Julian day and seconds */
0f113f3e
MC
74 if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
75 return 0;
1bf508c9 76
0f113f3e 77 /* Convert Julian day back to date */
1bf508c9 78
0f113f3e 79 julian_to_date(time_jd, &time_year, &time_month, &time_day);
1bf508c9 80
0f113f3e
MC
81 if (time_year < 1900 || time_year > 9999)
82 return 0;
1bf508c9 83
0f113f3e 84 /* Update tm structure */
1bf508c9 85
0f113f3e
MC
86 tm->tm_year = time_year - 1900;
87 tm->tm_mon = time_month - 1;
88 tm->tm_mday = time_day;
1bf508c9 89
0f113f3e
MC
90 tm->tm_hour = time_sec / 3600;
91 tm->tm_min = (time_sec / 60) % 60;
92 tm->tm_sec = time_sec % 60;
1bf508c9 93
0f113f3e 94 return 1;
1bf508c9 95
1bf508c9
DSH
96}
97
46a6cec6 98int OPENSSL_gmtime_diff(int *pday, int *psec,
0f113f3e
MC
99 const struct tm *from, const struct tm *to)
100{
101 int from_sec, to_sec, diff_sec;
102 long from_jd, to_jd, diff_day;
103 if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
104 return 0;
105 if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
106 return 0;
107 diff_day = to_jd - from_jd;
108 diff_sec = to_sec - from_sec;
109 /* Adjust differences so both positive or both negative */
110 if (diff_day > 0 && diff_sec < 0) {
111 diff_day--;
112 diff_sec += SECS_PER_DAY;
113 }
114 if (diff_day < 0 && diff_sec > 0) {
115 diff_day++;
116 diff_sec -= SECS_PER_DAY;
117 }
118
119 if (pday)
120 *pday = (int)diff_day;
121 if (psec)
122 *psec = diff_sec;
123
124 return 1;
125
126}
127
1bf508c9 128/* Convert tm structure and offset into julian day and seconds */
46a6cec6 129static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
0f113f3e
MC
130 long *pday, int *psec)
131{
132 int offset_hms, offset_day;
133 long time_jd;
134 int time_year, time_month, time_day;
135 /* split offset into days and day seconds */
136 offset_day = offset_sec / SECS_PER_DAY;
137 /* Avoid sign issues with % operator */
138 offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
139 offset_day += off_day;
140 /* Add current time seconds to offset */
141 offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
142 /* Adjust day seconds if overflow */
143 if (offset_hms >= SECS_PER_DAY) {
144 offset_day++;
145 offset_hms -= SECS_PER_DAY;
146 } else if (offset_hms < 0) {
147 offset_day--;
148 offset_hms += SECS_PER_DAY;
149 }
150
151 /*
152 * Convert date of time structure into a Julian day number.
153 */
154
155 time_year = tm->tm_year + 1900;
156 time_month = tm->tm_mon + 1;
157 time_day = tm->tm_mday;
158
159 time_jd = date_to_julian(time_year, time_month, time_day);
160
161 /* Work out Julian day of new date */
162 time_jd += offset_day;
163
164 if (time_jd < 0)
165 return 0;
166
167 *pday = time_jd;
168 *psec = offset_hms;
169 return 1;
170}
171
172/*
173 * Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
87d3a0cd
DSH
174 */
175static long date_to_julian(int y, int m, int d)
176{
0f113f3e
MC
177 return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
178 (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
179 (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
87d3a0cd
DSH
180}
181
182static void julian_to_date(long jd, int *y, int *m, int *d)
0f113f3e
MC
183{
184 long L = jd + 68569;
185 long n = (4 * L) / 146097;
186 long i, j;
187
188 L = L - (146097 * n + 3) / 4;
189 i = (4000 * (L + 1)) / 1461001;
190 L = L - (1461 * i) / 4 + 31;
191 j = (80 * L) / 2447;
192 *d = L - (2447 * j) / 80;
193 L = j / 11;
194 *m = j + 2 - (12 * L);
195 *y = 100 * (n - 49) + i + L;
196}