VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/asn1/asn1-ut-time.cpp@ 95596

Last change on this file since 95596 was 95596, checked in by vboxsync, 2 years ago

IPRT/RTAsn1Time: Added RTAsn1Time_SetTime, RTAsn1Time_SetTimeSpec and RTAsn1Time_InitWithTime functions. bugref:8691

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.6 KB
Line 
1/* $Id: asn1-ut-time.cpp 95596 2022-07-12 02:21:54Z vboxsync $ */
2/** @file
3 * IPRT - ASN.1, UTC TIME and GENERALIZED TIME Types.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/asn1.h>
33
34#include <iprt/ctype.h>
35#include <iprt/err.h>
36#include <iprt/string.h>
37#include <iprt/uni.h>
38
39#include <iprt/formats/asn1.h>
40
41
42/*********************************************************************************************************************************
43* Global Variables *
44*********************************************************************************************************************************/
45/** The UTC TIME encoding of the IPRT epoch time. */
46static const char g_szEpochUtc[] = "700101000000Z";
47/** The GENERALIZED TIME encoding of the IPRT epoch time. */
48static const char g_szEpochGeneralized[] = "19700101000000Z";
49
50
51/*
52 * ASN.1 TIME - Special Methods.
53 */
54
55RTDECL(int) RTAsn1Time_InitEx(PRTASN1TIME pThis, uint32_t uTag, PCRTASN1ALLOCATORVTABLE pAllocator)
56{
57 RT_NOREF_PV(pAllocator);
58 AssertReturn(uTag == ASN1_TAG_UTC_TIME || uTag == ASN1_TAG_GENERALIZED_TIME, VERR_INVALID_PARAMETER);
59 RTAsn1Core_InitEx(&pThis->Asn1Core,
60 uTag,
61 ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
62 &g_RTAsn1Time_Vtable,
63 RTASN1CORE_F_PRESENT | RTASN1CORE_F_PRIMITE_TAG_STRUCT);
64 if (uTag == ASN1_TAG_UTC_TIME)
65 {
66 pThis->Asn1Core.cb = sizeof(g_szEpochUtc) - 1;
67 pThis->Asn1Core.uData.pv = &g_szEpochUtc[0];
68 }
69 else
70 {
71 pThis->Asn1Core.cb = sizeof(g_szEpochGeneralized) - 1;
72 pThis->Asn1Core.uData.pv = &g_szEpochGeneralized[0];
73 }
74
75 RTTIMESPEC EpochTimeSpec;
76 RTTimeExplode(&pThis->Time, RTTimeSpecSetSeconds(&EpochTimeSpec, 0));
77
78 return VINF_SUCCESS;
79}
80
81
82RTDECL(int) RTAsn1Time_InitWithTime(PRTASN1TIME pThis, uint32_t uTag, PCRTASN1ALLOCATORVTABLE pAllocator, PCRTTIME pTime)
83{
84 int rc = RTAsn1Time_InitEx(pThis, uTag, pAllocator); /* this doens't leave any state needing deletion */
85 if (RT_SUCCESS(rc) && pTime)
86 rc = RTAsn1Time_SetTime(pThis, pAllocator, pTime);
87 return rc;
88}
89
90
91RTDECL(int) RTAsn1Time_SetTime(PRTASN1TIME pThis, PCRTASN1ALLOCATORVTABLE pAllocator, PCRTTIME pTime)
92{
93 /*
94 * Validate input.
95 */
96 AssertReturn(RTAsn1Time_IsPresent(pThis), VERR_INVALID_STATE); /* Use RTAsn1Time_InitWithTime. */
97
98 RTTIMESPEC TmpTimeSpec;
99 AssertReturn(RTTimeImplode(&TmpTimeSpec, pTime), VERR_INVALID_PARAMETER);
100 RTTIME NormalizedTime;
101 RTTimeExplode(&NormalizedTime, &TmpTimeSpec);
102
103 uint32_t const uTag = RTASN1CORE_GET_TAG(&pThis->Asn1Core);
104 if (uTag == ASN1_TAG_UTC_TIME)
105 {
106 AssertReturn(NormalizedTime.i32Year >= 1950, VERR_INVALID_PARAMETER);
107 AssertReturn(NormalizedTime.i32Year < 2050, VERR_INVALID_PARAMETER);
108 }
109 else
110 {
111 AssertReturn(uTag == ASN1_TAG_GENERAL_STRING, VERR_INVALID_STATE);
112 AssertReturn(NormalizedTime.i32Year >= 0, VERR_INVALID_PARAMETER);
113 AssertReturn(NormalizedTime.i32Year < 9999, VERR_INVALID_PARAMETER);
114 }
115
116 /*
117 * Format the string to a temporary buffer, since the ASN.1 content isn't
118 * zero terminated and we cannot use RTStrPrintf directly on it.
119 */
120 char szTmp[64];
121 uint32_t cchTime;
122 if (uTag == ASN1_TAG_UTC_TIME)
123 cchTime = (uint32_t)RTStrPrintf(szTmp, sizeof(szTmp), "%02u%02u%02u%02u%02u%02uZ",
124 NormalizedTime.i32Year % 100,
125 NormalizedTime.u8Month,
126 NormalizedTime.u8MonthDay,
127 NormalizedTime.u8Hour,
128 NormalizedTime.u8Minute,
129 NormalizedTime.u8Second);
130 else
131 cchTime = (uint32_t)RTStrPrintf(szTmp, sizeof(szTmp), "%04u%02u%02u%02u%02u%02uZ",
132 NormalizedTime.i32Year,
133 NormalizedTime.u8Month,
134 NormalizedTime.u8MonthDay,
135 NormalizedTime.u8Hour,
136 NormalizedTime.u8Minute,
137 NormalizedTime.u8Second);
138 AssertReturn(cchTime == (uTag == ASN1_TAG_UTC_TIME ? sizeof(g_szEpochUtc) - 1 : sizeof(g_szEpochGeneralized) - 1),
139 VERR_INTERNAL_ERROR_3);
140
141 /*
142 * (Re-)Allocate content buffer, copy over the formatted timestamp and
143 * set the exploded time member to the new time.
144 */
145 int rc = RTAsn1ContentReallocZ(&pThis->Asn1Core, cchTime, pAllocator);
146 if (RT_SUCCESS(rc))
147 {
148 memcpy((void *)pThis->Asn1Core.uData.pv, szTmp, cchTime);
149 pThis->Time = NormalizedTime;
150 }
151 return rc;
152}
153
154
155RTDECL(int) RTAsn1Time_SetTimeSpec(PRTASN1TIME pThis, PCRTASN1ALLOCATORVTABLE pAllocator, PCRTTIMESPEC pTimeSpec)
156{
157 RTTIME Time;
158 return RTAsn1Time_SetTime(pThis, pAllocator, RTTimeExplode(&Time, pTimeSpec));
159}
160
161
162RTDECL(int) RTAsn1Time_CompareWithTimeSpec(PCRTASN1TIME pLeft, PCRTTIMESPEC pTsRight)
163{
164 int iDiff = RTAsn1Time_IsPresent(pLeft) ? 0 : -1;
165 if (!iDiff)
166 {
167 RTTIME RightTime;
168 iDiff = RTTimeCompare(&pLeft->Time, RTTimeExplode(&RightTime, pTsRight));
169 }
170
171 return iDiff;
172}
173
174
175/*
176 * ASN.1 TIME - Standard Methods.
177 */
178
179RT_DECL_DATA_CONST(RTASN1COREVTABLE const) g_RTAsn1Time_Vtable =
180{
181 "RTAsn1Time",
182 sizeof(RTASN1TIME),
183 UINT8_MAX,
184 ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
185 0,
186 (PFNRTASN1COREVTDTOR)RTAsn1Time_Delete,
187 NULL,
188 (PFNRTASN1COREVTCLONE)RTAsn1Time_Clone,
189 (PFNRTASN1COREVTCOMPARE)RTAsn1Time_Compare,
190 (PFNRTASN1COREVTCHECKSANITY)RTAsn1Time_CheckSanity,
191 NULL,
192 NULL
193};
194
195
196RTDECL(int) RTAsn1Time_Init(PRTASN1TIME pThis, PCRTASN1ALLOCATORVTABLE pAllocator)
197{
198 /* Using UTC TIME since epoch would be encoded using UTC TIME following
199 X.509 Validity / Whatever time tag guidelines. */
200 return RTAsn1Time_InitEx(pThis, ASN1_TAG_UTC_TIME, pAllocator);
201}
202
203
204RTDECL(int) RTAsn1Time_Clone(PRTASN1TIME pThis, PCRTASN1TIME pSrc, PCRTASN1ALLOCATORVTABLE pAllocator)
205{
206 AssertPtr(pSrc); AssertPtr(pThis); AssertPtr(pAllocator);
207 RT_ZERO(*pThis);
208 if (RTAsn1Time_IsPresent(pSrc))
209 {
210 AssertReturn(pSrc->Asn1Core.pOps == &g_RTAsn1Time_Vtable, VERR_INTERNAL_ERROR_3);
211
212 int rc = RTAsn1Core_CloneContent(&pThis->Asn1Core, &pSrc->Asn1Core, pAllocator);
213 if (RT_SUCCESS(rc))
214 {
215 pThis->Time = pSrc->Time;
216 return VINF_SUCCESS;
217 }
218 return rc;
219 }
220 return VINF_SUCCESS;
221}
222
223
224RTDECL(void) RTAsn1Time_Delete(PRTASN1TIME pThis)
225{
226 if ( pThis
227 && RTAsn1Time_IsPresent(pThis))
228 {
229 Assert(pThis->Asn1Core.pOps == &g_RTAsn1Time_Vtable);
230
231 RTAsn1ContentFree(&pThis->Asn1Core);
232 RT_ZERO(*pThis);
233 }
234}
235
236
237RTDECL(int) RTAsn1Time_Enum(PRTASN1TIME pThis, PFNRTASN1ENUMCALLBACK pfnCallback, uint32_t uDepth, void *pvUser)
238{
239 RT_NOREF_PV(pThis); RT_NOREF_PV(pfnCallback); RT_NOREF_PV(uDepth); RT_NOREF_PV(pvUser);
240 Assert(pThis && (!RTAsn1Time_IsPresent(pThis) || pThis->Asn1Core.pOps == &g_RTAsn1Time_Vtable));
241
242 /* No children to enumerate. */
243 return VINF_SUCCESS;
244}
245
246
247RTDECL(int) RTAsn1Time_Compare(PCRTASN1TIME pLeft, PCRTASN1TIME pRight)
248{
249 Assert(pLeft && (!RTAsn1Time_IsPresent(pLeft) || pLeft->Asn1Core.pOps == &g_RTAsn1Time_Vtable));
250 Assert(pRight && (!RTAsn1Time_IsPresent(pRight) || pRight->Asn1Core.pOps == &g_RTAsn1Time_Vtable));
251
252 int iDiff;
253 if (RTAsn1Time_IsPresent(pLeft))
254 {
255 if (RTAsn1Time_IsPresent(pRight))
256 iDiff = RTTimeCompare(&pLeft->Time, &pRight->Time);
257 else
258 iDiff = -1;
259 }
260 else
261 iDiff = 0 - (int)RTAsn1Time_IsPresent(pRight);
262 return iDiff;
263}
264
265
266RTDECL(int) RTAsn1Time_CheckSanity(PCRTASN1TIME pThis, uint32_t fFlags, PRTERRINFO pErrInfo, const char *pszErrorTag)
267{
268 RT_NOREF_PV(fFlags);
269 if (RT_UNLIKELY(!RTAsn1Time_IsPresent(pThis)))
270 return RTErrInfoSetF(pErrInfo, VERR_ASN1_NOT_PRESENT, "%s: Missing (TIME).", pszErrorTag);
271 return VINF_SUCCESS;
272}
273
274
275/*
276 * Generate code for the tag specific methods.
277 * Note! This is very similar to what we're doing in asn1-ut-string.cpp.
278 */
279#define RTASN1TIME_IMPL(a_uTag, a_szTag, a_Api) \
280 \
281 RTDECL(int) RT_CONCAT(a_Api,_Init)(PRTASN1TIME pThis, PCRTASN1ALLOCATORVTABLE pAllocator) \
282 { \
283 return RTAsn1Time_InitEx(pThis, a_uTag, pAllocator); \
284 } \
285 \
286 RTDECL(int) RT_CONCAT(a_Api,_Clone)(PRTASN1TIME pThis, PCRTASN1TIME pSrc, PCRTASN1ALLOCATORVTABLE pAllocator) \
287 { \
288 AssertReturn(RTASN1CORE_GET_TAG(&pSrc->Asn1Core) == a_uTag || !RTAsn1Time_IsPresent(pSrc), \
289 VERR_ASN1_TIME_TAG_MISMATCH); \
290 return RTAsn1Time_Clone(pThis, pSrc, pAllocator); \
291 } \
292 \
293 RTDECL(void) RT_CONCAT(a_Api,_Delete)(PRTASN1TIME pThis) \
294 { \
295 Assert( !pThis \
296 || !RTAsn1Time_IsPresent(pThis) \
297 || ( pThis->Asn1Core.pOps == &g_RTAsn1Time_Vtable \
298 && RTASN1CORE_GET_TAG(&pThis->Asn1Core) == a_uTag) ); \
299 RTAsn1Time_Delete(pThis); \
300 } \
301 \
302 RTDECL(int) RT_CONCAT(a_Api,_Enum)(PRTASN1TIME pThis, PFNRTASN1ENUMCALLBACK pfnCallback, uint32_t uDepth, void *pvUser) \
303 { \
304 RT_NOREF_PV(pThis); RT_NOREF_PV(pfnCallback); RT_NOREF_PV(uDepth); RT_NOREF_PV(pvUser); \
305 Assert( pThis \
306 && ( !RTAsn1Time_IsPresent(pThis) \
307 || ( pThis->Asn1Core.pOps == &g_RTAsn1Time_Vtable \
308 && RTASN1CORE_GET_TAG(&pThis->Asn1Core) == a_uTag) ) ); \
309 /* No children to enumerate. */ \
310 return VINF_SUCCESS; \
311 } \
312 \
313 RTDECL(int) RT_CONCAT(a_Api,_Compare)(PCRTASN1TIME pLeft, PCRTASN1TIME pRight) \
314 { \
315 int iDiff = RTAsn1Time_Compare(pLeft, pRight); \
316 if (!iDiff && RTAsn1Time_IsPresent(pLeft)) \
317 { \
318 if (RTASN1CORE_GET_TAG(&pLeft->Asn1Core) == RTASN1CORE_GET_TAG(&pRight->Asn1Core)) \
319 { \
320 if (RTASN1CORE_GET_TAG(&pLeft->Asn1Core) != a_uTag) \
321 iDiff = RTASN1CORE_GET_TAG(&pLeft->Asn1Core) < a_uTag ? -1 : 1; \
322 } \
323 else \
324 iDiff = RTASN1CORE_GET_TAG(&pLeft->Asn1Core) < RTASN1CORE_GET_TAG(&pRight->Asn1Core) ? -1 : 1; \
325 } \
326 return iDiff; \
327 } \
328 \
329 RTDECL(int) RT_CONCAT(a_Api,_CheckSanity)(PCRTASN1TIME pThis, uint32_t fFlags, \
330 PRTERRINFO pErrInfo, const char *pszErrorTag) \
331 { \
332 if (RTASN1CORE_GET_TAG(&pThis->Asn1Core) != a_uTag && RTAsn1Time_IsPresent(pThis)) \
333 return RTErrInfoSetF(pErrInfo, VERR_ASN1_TIME_TAG_MISMATCH, "%s: uTag=%#x, expected %#x (%s)", \
334 pszErrorTag, RTASN1CORE_GET_TAG(&pThis->Asn1Core), a_uTag, a_szTag); \
335 return RTAsn1Time_CheckSanity(pThis, fFlags, pErrInfo, pszErrorTag); \
336 }
337
338#include "asn1-ut-time-template2.h"
339
340
341/*
342 * Generate code for the associated collection types.
343 */
344#define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-time-template.h"
345#include <iprt/asn1-generator-internal-header.h>
346#include <iprt/asn1-generator-core.h>
347#include <iprt/asn1-generator-init.h>
348#include <iprt/asn1-generator-sanity.h>
349
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette