]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
Add new interfaces to support PKCS7#7 signed data and authenticode signature. Update...
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / SysCall / TimerWrapper.c
CommitLineData
97f98500
HT
1/** @file\r
2 C Run-Time Libraries (CRT) Time Management Routines Wrapper Implementation\r
3 for OpenSSL-based Cryptographic Library (used in DXE & RUNTIME).\r
4\r
b7d320f8 5Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
97f98500
HT
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <Uefi.h>\r
17#include <OpenSslSupport.h>\r
18#include <Library/UefiRuntimeServicesTableLib.h>\r
19\r
20//\r
21// -- Time Management Routines --\r
22//\r
23\r
24#define IsLeap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))\r
b7d320f8 25#define SECSPERMIN (60)\r
97f98500
HT
26#define SECSPERHOUR (60 * 60)\r
27#define SECSPERDAY (24 * SECSPERHOUR)\r
28\r
29//\r
30// The arrays give the cumulative number of days up to the first of the\r
31// month number used as the index (1 -> 12) for regular and leap years.\r
32// The value at index 13 is for the whole year.\r
33//\r
34UINTN CumulativeDays[2][14] = {\r
35 {\r
36 0,\r
37 0,\r
38 31,\r
39 31 + 28,\r
40 31 + 28 + 31,\r
41 31 + 28 + 31 + 30,\r
42 31 + 28 + 31 + 30 + 31,\r
43 31 + 28 + 31 + 30 + 31 + 30,\r
44 31 + 28 + 31 + 30 + 31 + 30 + 31,\r
45 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,\r
46 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,\r
47 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,\r
48 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,\r
49 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31\r
50 },\r
51 {\r
52 0,\r
53 0,\r
54 31,\r
55 31 + 29,\r
56 31 + 29 + 31,\r
57 31 + 29 + 31 + 30,\r
58 31 + 29 + 31 + 30 + 31,\r
59 31 + 29 + 31 + 30 + 31 + 30,\r
60 31 + 29 + 31 + 30 + 31 + 30 + 31,\r
61 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,\r
62 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,\r
63 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,\r
64 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,\r
65 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 \r
66 }\r
67};\r
68\r
69/* Get the system time as seconds elapsed since midnight, January 1, 1970. */\r
70//INTN time(\r
71// INTN *timer\r
72// )\r
73time_t time (time_t *timer)\r
74{\r
75 EFI_TIME Time;\r
76 UINTN Year;\r
77\r
78 //\r
79 // Get the current time and date information\r
80 //\r
81 gRT->GetTime (&Time, NULL);\r
82\r
83 //\r
84 // Years Handling\r
85 // UTime should now be set to 00:00:00 on Jan 1 of the current year.\r
86 //\r
87 for (Year = 1970, *timer = 0; Year != Time.Year; Year++) {\r
88 *timer = *timer + (time_t)(CumulativeDays[IsLeap(Year)][13] * SECSPERDAY);\r
89 }\r
90\r
91 //\r
92 // Add in number of seconds for current Month, Day, Hour, Minute, Seconds, and TimeZone adjustment\r
93 //\r
94 *timer = *timer + \r
95 (time_t)((Time.TimeZone != EFI_UNSPECIFIED_TIMEZONE) ? (Time.TimeZone * 60) : 0) +\r
96 (time_t)(CumulativeDays[IsLeap(Time.Year)][Time.Month] * SECSPERDAY) + \r
97 (time_t)(((Time.Day > 0) ? Time.Day - 1 : 0) * SECSPERDAY) + \r
98 (time_t)(Time.Hour * SECSPERHOUR) + \r
99 (time_t)(Time.Minute * 60) + \r
100 (time_t)Time.Second;\r
101\r
102 return *timer;\r
103}\r
b7d320f8 104\r
105//\r
106// Convert a time value from type time_t to struct tm.\r
107//\r
108struct tm * gmtime (const time_t *timer)\r
109{\r
110 struct tm *GmTime;\r
111 UINT16 DayNo;\r
112 UINT16 DayRemainder;\r
113 time_t Year;\r
114 time_t YearNo;\r
115 UINT16 TotalDays;\r
116 UINT16 MonthNo;\r
117\r
118 if (timer == NULL) {\r
119 return NULL;\r
120 }\r
121\r
122 GmTime = malloc (sizeof (struct tm));\r
123 if (GmTime == NULL) {\r
124 return NULL;\r
125 }\r
126\r
127 ZeroMem ((VOID *) GmTime, (UINTN) sizeof (struct tm));\r
128\r
129 DayNo = (UINT16) (*timer / SECSPERDAY);\r
130 DayRemainder = (UINT16) (*timer % SECSPERDAY);\r
131\r
132 GmTime->tm_sec = (int) (DayRemainder % SECSPERMIN);\r
133 GmTime->tm_min = (int) ((DayRemainder % SECSPERHOUR) / SECSPERMIN);\r
134 GmTime->tm_hour = (int) (DayRemainder / SECSPERHOUR);\r
135 GmTime->tm_wday = (int) ((DayNo + 4) % 7);\r
136\r
137 for (Year = 1970, YearNo = 0; DayNo > 0; Year++) {\r
138 TotalDays = (UINT16) (IsLeap (Year) ? 366 : 365);\r
139 if (DayNo >= TotalDays) {\r
140 DayNo = (UINT16) (DayNo - TotalDays);\r
141 YearNo++;\r
142 } else {\r
143 break;\r
144 }\r
145 }\r
146\r
147 GmTime->tm_year = (int) (YearNo + (1970 - 1900));\r
148 GmTime->tm_yday = (int) DayNo;\r
149\r
150 for (MonthNo = 12; MonthNo > 1; MonthNo--) {\r
151 if (DayNo > CumulativeDays[IsLeap(Year)][MonthNo]) {\r
152 DayNo = (UINT16) (DayNo - (UINT16) (CumulativeDays[IsLeap(Year)][MonthNo]));\r
153 break;\r
154 }\r
155 }\r
156\r
157 GmTime->tm_mon = (int) MonthNo;\r
158 GmTime->tm_mday = (int) DayNo;\r
159\r
160 GmTime->tm_isdst = 0;\r
161 GmTime->tm_gmtoff = 0;\r
162 GmTime->tm_zone = NULL;\r
163\r
164 return GmTime;\r
165}\r