VirtualBox

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

Last change on this file since 57444 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
1/* $Id: asn1-ut-time.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * IPRT - ASN.1, UTC TIME and GENERALIZED TIME Types.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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 AssertReturn(uTag == ASN1_TAG_UTC_TIME || uTag == ASN1_TAG_GENERALIZED_TIME, VERR_INVALID_PARAMETER);
58 RTAsn1Core_InitEx(&pThis->Asn1Core,
59 uTag,
60 ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
61 &g_RTAsn1Time_Vtable,
62 RTASN1CORE_F_PRESENT | RTASN1CORE_F_PRIMITE_TAG_STRUCT);
63 if (uTag == ASN1_TAG_UTC_TIME)
64 {
65 pThis->Asn1Core.cb = sizeof(g_szEpochUtc) - 1;
66 pThis->Asn1Core.uData.pv = &g_szEpochUtc[0];
67 }
68 else
69 {
70 pThis->Asn1Core.cb = sizeof(g_szEpochGeneralized) - 1;
71 pThis->Asn1Core.uData.pv = &g_szEpochGeneralized[0];
72 }
73 return VINF_SUCCESS;
74}
75
76
77RTDECL(int) RTAsn1Time_CompareWithTimeSpec(PCRTASN1TIME pLeft, PCRTTIMESPEC pTsRight)
78{
79 int iDiff = RTAsn1Time_IsPresent(pLeft) ? 0 : -1;
80 if (!iDiff)
81 {
82 RTTIMESPEC TsLeft;
83 AssertReturn(RTTimeImplode(&TsLeft, &pLeft->Time), -1);
84
85 iDiff = RTTimeSpecCompare(&TsLeft, pTsRight);
86 }
87
88 return iDiff;
89}
90
91
92/*
93 * ASN.1 TIME - Standard Methods.
94 */
95
96RT_DECL_DATA_CONST(RTASN1COREVTABLE const) g_RTAsn1Time_Vtable =
97{
98 "RTAsn1Time",
99 sizeof(RTASN1TIME),
100 UINT8_MAX,
101 ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
102 0,
103 (PFNRTASN1COREVTDTOR)RTAsn1Time_Delete,
104 NULL,
105 (PFNRTASN1COREVTCLONE)RTAsn1Time_Clone,
106 (PFNRTASN1COREVTCOMPARE)RTAsn1Time_Compare,
107 (PFNRTASN1COREVTCHECKSANITY)RTAsn1Time_CheckSanity,
108 NULL,
109 NULL
110};
111
112
113RTDECL(int) RTAsn1Time_Init(PRTASN1TIME pThis, PCRTASN1ALLOCATORVTABLE pAllocator)
114{
115 /* Using UTC TIME since epoch would be encoded using UTC TIME following
116 X.509 Validity / Whatever time tag guidelines. */
117 return RTAsn1Time_InitEx(pThis, ASN1_TAG_UTC_TIME, pAllocator);
118}
119
120
121RTDECL(int) RTAsn1Time_Clone(PRTASN1TIME pThis, PCRTASN1TIME pSrc, PCRTASN1ALLOCATORVTABLE pAllocator)
122{
123 AssertPtr(pSrc); AssertPtr(pThis); AssertPtr(pAllocator);
124 RT_ZERO(*pThis);
125 if (RTAsn1Time_IsPresent(pSrc))
126 {
127 AssertReturn(pSrc->Asn1Core.pOps == &g_RTAsn1Time_Vtable, VERR_INTERNAL_ERROR_3);
128
129 int rc = RTAsn1Core_CloneContent(&pThis->Asn1Core, &pSrc->Asn1Core, pAllocator);
130 if (RT_SUCCESS(rc))
131 {
132 pThis->Time = pSrc->Time;
133 return VINF_SUCCESS;
134 }
135 return rc;
136 }
137 return VINF_SUCCESS;
138}
139
140
141RTDECL(void) RTAsn1Time_Delete(PRTASN1TIME pThis)
142{
143 if ( pThis
144 && RTAsn1Time_IsPresent(pThis))
145 {
146 Assert(pThis->Asn1Core.pOps == &g_RTAsn1Time_Vtable);
147
148 RTAsn1ContentFree(&pThis->Asn1Core);
149 RT_ZERO(*pThis);
150 }
151}
152
153
154RTDECL(int) RTAsn1Time_Enum(PRTASN1TIME pThis, PFNRTASN1ENUMCALLBACK pfnCallback, uint32_t uDepth, void *pvUser)
155{
156 Assert(pThis && (!RTAsn1Time_IsPresent(pThis) || pThis->Asn1Core.pOps == &g_RTAsn1Time_Vtable));
157
158 /* No children to enumerate. */
159 return VINF_SUCCESS;
160}
161
162
163RTDECL(int) RTAsn1Time_Compare(PCRTASN1TIME pLeft, PCRTASN1TIME pRight)
164{
165 Assert(pLeft && (!RTAsn1Time_IsPresent(pLeft) || pLeft->Asn1Core.pOps == &g_RTAsn1Time_Vtable));
166 Assert(pRight && (!RTAsn1Time_IsPresent(pRight) || pRight->Asn1Core.pOps == &g_RTAsn1Time_Vtable));
167
168 int iDiff;
169 if (RTAsn1Time_IsPresent(pLeft))
170 {
171 if (RTAsn1Time_IsPresent(pRight))
172 {
173 RTTIMESPEC TsLeft;
174 AssertReturn(RTTimeImplode(&TsLeft, &pLeft->Time), -1);
175
176 RTTIMESPEC TsRight;
177 AssertReturn(RTTimeImplode(&TsRight, &pRight->Time), 1);
178
179 iDiff = RTTimeSpecCompare(&TsLeft, &TsRight);
180 }
181 else
182 iDiff = -1;
183 }
184 else
185 iDiff = 0 - (int)RTAsn1Time_IsPresent(pRight);
186 return iDiff;
187}
188
189
190RTDECL(int) RTAsn1Time_CheckSanity(PCRTASN1TIME pThis, uint32_t fFlags, PRTERRINFO pErrInfo, const char *pszErrorTag)
191{
192 if (RT_UNLIKELY(!RTAsn1Time_IsPresent(pThis)))
193 return RTErrInfoSetF(pErrInfo, VERR_ASN1_NOT_PRESENT, "%s: Missing (TIME).", pszErrorTag);
194 return VINF_SUCCESS;
195}
196
197
198/*
199 * Generate code for the tag specific methods.
200 * Note! This is very similar to what we're doing in asn1-ut-string.cpp.
201 */
202#define RTASN1TIME_IMPL(a_uTag, a_szTag, a_Api) \
203 \
204 RTDECL(int) RT_CONCAT(a_Api,_Init)(PRTASN1TIME pThis, PCRTASN1ALLOCATORVTABLE pAllocator) \
205 { \
206 return RTAsn1Time_InitEx(pThis, a_uTag, pAllocator); \
207 } \
208 \
209 RTDECL(int) RT_CONCAT(a_Api,_Clone)(PRTASN1TIME pThis, PCRTASN1TIME pSrc, PCRTASN1ALLOCATORVTABLE pAllocator) \
210 { \
211 AssertReturn(RTASN1CORE_GET_TAG(&pSrc->Asn1Core) == a_uTag || !RTAsn1Time_IsPresent(pSrc), \
212 VERR_ASN1_TIME_TAG_MISMATCH); \
213 return RTAsn1Time_Clone(pThis, pSrc, pAllocator); \
214 } \
215 \
216 RTDECL(void) RT_CONCAT(a_Api,_Delete)(PRTASN1TIME pThis) \
217 { \
218 Assert( !pThis \
219 || !RTAsn1Time_IsPresent(pThis) \
220 || ( pThis->Asn1Core.pOps == &g_RTAsn1Time_Vtable \
221 && RTASN1CORE_GET_TAG(&pThis->Asn1Core) == a_uTag) ); \
222 RTAsn1Time_Delete(pThis); \
223 } \
224 \
225 RTDECL(int) RT_CONCAT(a_Api,_Enum)(PRTASN1TIME pThis, PFNRTASN1ENUMCALLBACK pfnCallback, uint32_t uDepth, void *pvUser) \
226 { \
227 Assert( pThis \
228 && ( !RTAsn1Time_IsPresent(pThis) \
229 || ( pThis->Asn1Core.pOps == &g_RTAsn1Time_Vtable \
230 && RTASN1CORE_GET_TAG(&pThis->Asn1Core) == a_uTag) ) ); \
231 /* No children to enumerate. */ \
232 return VINF_SUCCESS; \
233 } \
234 \
235 RTDECL(int) RT_CONCAT(a_Api,_Compare)(PCRTASN1TIME pLeft, PCRTASN1TIME pRight) \
236 { \
237 int iDiff = RTAsn1Time_Compare(pLeft, pRight); \
238 if (!iDiff && RTAsn1Time_IsPresent(pLeft)) \
239 { \
240 if (RTASN1CORE_GET_TAG(&pLeft->Asn1Core) == RTASN1CORE_GET_TAG(&pRight->Asn1Core)) \
241 { \
242 if (RTASN1CORE_GET_TAG(&pLeft->Asn1Core) != a_uTag) \
243 iDiff = RTASN1CORE_GET_TAG(&pLeft->Asn1Core) < a_uTag ? -1 : 1; \
244 } \
245 else \
246 iDiff = RTASN1CORE_GET_TAG(&pLeft->Asn1Core) < RTASN1CORE_GET_TAG(&pRight->Asn1Core) ? -1 : 1; \
247 } \
248 return iDiff; \
249 } \
250 \
251 RTDECL(int) RT_CONCAT(a_Api,_CheckSanity)(PCRTASN1TIME pThis, uint32_t fFlags, \
252 PRTERRINFO pErrInfo, const char *pszErrorTag) \
253 { \
254 if (RTASN1CORE_GET_TAG(&pThis->Asn1Core) != a_uTag && RTAsn1Time_IsPresent(pThis)) \
255 return RTErrInfoSetF(pErrInfo, VERR_ASN1_TIME_TAG_MISMATCH, "%s: uTag=%#x, expected %#x (%s)", \
256 pszErrorTag, RTASN1CORE_GET_TAG(&pThis->Asn1Core), a_uTag, a_szTag); \
257 return RTAsn1Time_CheckSanity(pThis, fFlags, pErrInfo, pszErrorTag); \
258 }
259
260#include "asn1-ut-time-template2.h"
261
262
263/*
264 * Generate code for the associated collection types.
265 */
266#define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-time-template.h"
267#include <iprt/asn1-generator-internal-header.h>
268#include <iprt/asn1-generator-core.h>
269#include <iprt/asn1-generator-init.h>
270#include <iprt/asn1-generator-sanity.h>
271
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