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