1 | /* $Id: asn1-ut-dyntype.cpp 100490 2023-07-10 22:04:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - ASN.1, Basic Operations.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "internal/iprt.h"
|
---|
42 | #include <iprt/asn1.h>
|
---|
43 |
|
---|
44 | #include <iprt/err.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 |
|
---|
47 | #include <iprt/formats/asn1.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | /*
|
---|
51 | * ASN.1 dynamic type union - Standard Methods.
|
---|
52 | */
|
---|
53 |
|
---|
54 |
|
---|
55 | RTDECL(int) RTAsn1DynType_Init(PRTASN1DYNTYPE pThis, PCRTASN1ALLOCATORVTABLE pAllocator)
|
---|
56 | {
|
---|
57 | RT_NOREF_PV(pAllocator);
|
---|
58 | RT_ZERO(*pThis);
|
---|
59 | pThis->enmType = RTASN1TYPE_NOT_PRESENT;
|
---|
60 | return VINF_SUCCESS;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | RTDECL(int) RTAsn1DynType_Clone(PRTASN1DYNTYPE pThis, PCRTASN1DYNTYPE pSrc, PCRTASN1ALLOCATORVTABLE pAllocator)
|
---|
65 | {
|
---|
66 | AssertPtr(pSrc); AssertPtr(pThis); AssertPtr(pAllocator);
|
---|
67 | RT_ZERO(*pThis);
|
---|
68 | if (RTAsn1DynType_IsPresent(pSrc))
|
---|
69 | {
|
---|
70 | int rc;
|
---|
71 | switch (pSrc->enmType)
|
---|
72 | {
|
---|
73 | case RTASN1TYPE_CORE: rc = RTAsn1Core_Clone(&pThis->u.Core, &pSrc->u.Core, pAllocator); break;
|
---|
74 | case RTASN1TYPE_NULL: rc = RTAsn1Null_Clone(&pThis->u.Asn1Null, &pSrc->u.Asn1Null, pAllocator); break;
|
---|
75 | case RTASN1TYPE_INTEGER: rc = RTAsn1Integer_Clone(&pThis->u.Integer, &pSrc->u.Integer, pAllocator); break;
|
---|
76 | case RTASN1TYPE_BOOLEAN: rc = RTAsn1Boolean_Clone(&pThis->u.Boolean, &pSrc->u.Boolean, pAllocator); break;
|
---|
77 | case RTASN1TYPE_STRING: rc = RTAsn1String_Clone(&pThis->u.String, &pSrc->u.String, pAllocator); break;
|
---|
78 | case RTASN1TYPE_OCTET_STRING: rc = RTAsn1OctetString_Clone(&pThis->u.OctetString, &pSrc->u.OctetString, pAllocator); break;
|
---|
79 | case RTASN1TYPE_BIT_STRING: rc = RTAsn1BitString_Clone(&pThis->u.BitString, &pSrc->u.BitString, pAllocator); break;
|
---|
80 | case RTASN1TYPE_TIME: rc = RTAsn1Time_Clone(&pThis->u.Time, &pSrc->u.Time, pAllocator); break;
|
---|
81 | #if 0
|
---|
82 | case RTASN1TYPE_SEQUENCE_CORE: rc = VERR_NOT_SUPPORTED; //rc = RTAsn1SequenceCore_Clone(&pThis->u.SeqCore, &pSrc->u.SeqCore, pAllocator); break;
|
---|
83 | case RTASN1TYPE_SET_CORE: rc = VERR_NOT_SUPPORTED; //rc = RTAsn1SetCore_Clone(&pThis->u.SetCore, &pSrc->u.SetCore, pAllocator); break;
|
---|
84 | #endif
|
---|
85 | case RTASN1TYPE_OBJID: rc = RTAsn1ObjId_Clone(&pThis->u.ObjId, &pSrc->u.ObjId, pAllocator); break;
|
---|
86 | default:
|
---|
87 | AssertFailedReturn(VERR_ASN1_INTERNAL_ERROR_2);
|
---|
88 | }
|
---|
89 | if (RT_FAILURE(rc))
|
---|
90 | {
|
---|
91 | RT_ZERO(*pThis);
|
---|
92 | return rc;
|
---|
93 | }
|
---|
94 | pThis->enmType = pSrc->enmType;
|
---|
95 | }
|
---|
96 | else
|
---|
97 | pThis->enmType = RTASN1TYPE_NOT_PRESENT;
|
---|
98 | return VINF_SUCCESS;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | RTDECL(void) RTAsn1DynType_Delete(PRTASN1DYNTYPE pThis)
|
---|
103 | {
|
---|
104 | if ( pThis
|
---|
105 | && RTAsn1DynType_IsPresent(pThis))
|
---|
106 | {
|
---|
107 | if ( pThis->u.Core.pOps
|
---|
108 | && pThis->u.Core.pOps->pfnDtor)
|
---|
109 | pThis->u.Core.pOps->pfnDtor(&pThis->u.Core);
|
---|
110 | RT_ZERO(*pThis);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | RTDECL(int) RTAsn1DynType_SetToNull(PRTASN1DYNTYPE pThis)
|
---|
116 | {
|
---|
117 | RTAsn1DynType_Delete(pThis);
|
---|
118 | pThis->enmType = RTASN1TYPE_NULL;
|
---|
119 | return RTAsn1Null_Init(&pThis->u.Asn1Null, NULL /*pAllocator*/);
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | RTDECL(int) RTAsn1DynType_SetToObjId(PRTASN1DYNTYPE pThis, PCRTASN1OBJID pSrc, PCRTASN1ALLOCATORVTABLE pAllocator)
|
---|
124 | {
|
---|
125 | RTAsn1DynType_Delete(pThis);
|
---|
126 | pThis->enmType = RTASN1TYPE_OBJID;
|
---|
127 | return RTAsn1ObjId_Clone(&pThis->u.ObjId, pSrc, pAllocator);
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | RTDECL(int) RTAsn1DynType_Enum(PRTASN1DYNTYPE pThis, PFNRTASN1ENUMCALLBACK pfnCallback, uint32_t uDepth, void *pvUser)
|
---|
132 | {
|
---|
133 | if ( pThis
|
---|
134 | && RTAsn1DynType_IsPresent(pThis))
|
---|
135 | {
|
---|
136 | if ( pThis->u.Core.pOps
|
---|
137 | && pThis->u.Core.pOps->pfnEnum)
|
---|
138 | return pThis->u.Core.pOps->pfnEnum(&pThis->u.Core, pfnCallback, uDepth, pvUser);
|
---|
139 | }
|
---|
140 | return VINF_SUCCESS;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | RTDECL(int) RTAsn1DynType_Compare(PCRTASN1DYNTYPE pLeft, PCRTASN1DYNTYPE pRight)
|
---|
145 | {
|
---|
146 | if (RTAsn1DynType_IsPresent(pLeft) && RTAsn1DynType_IsPresent(pRight))
|
---|
147 | {
|
---|
148 | if (pLeft->enmType != pRight->enmType)
|
---|
149 | return pLeft->enmType < pRight->enmType ? -1 : 1;
|
---|
150 |
|
---|
151 | switch (pLeft->enmType)
|
---|
152 | {
|
---|
153 | case RTASN1TYPE_CORE: return RTAsn1Core_Compare(&pLeft->u.Core, &pRight->u.Core); break;
|
---|
154 | case RTASN1TYPE_NULL: return RTAsn1Null_Compare(&pLeft->u.Asn1Null, &pRight->u.Asn1Null);
|
---|
155 | case RTASN1TYPE_INTEGER: return RTAsn1Integer_Compare(&pLeft->u.Integer, &pRight->u.Integer);
|
---|
156 | case RTASN1TYPE_BOOLEAN: return RTAsn1Boolean_Compare(&pLeft->u.Boolean, &pRight->u.Boolean);
|
---|
157 | case RTASN1TYPE_STRING: return RTAsn1String_Compare(&pLeft->u.String, &pRight->u.String);
|
---|
158 | case RTASN1TYPE_OCTET_STRING: return RTAsn1OctetString_Compare(&pLeft->u.OctetString, &pRight->u.OctetString);
|
---|
159 | case RTASN1TYPE_BIT_STRING: return RTAsn1BitString_Compare(&pLeft->u.BitString, &pRight->u.BitString);
|
---|
160 | case RTASN1TYPE_TIME: return RTAsn1Time_Compare(&pLeft->u.Time, &pRight->u.Time);
|
---|
161 | case RTASN1TYPE_OBJID: return RTAsn1ObjId_Compare(&pLeft->u.ObjId, &pRight->u.ObjId);
|
---|
162 | default: AssertFailedReturn(-1);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | else
|
---|
166 | return (int)RTAsn1DynType_IsPresent(pLeft) - (int)RTAsn1DynType_IsPresent(pRight);
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | RTDECL(int) RTAsn1DynType_CheckSanity(PCRTASN1DYNTYPE pThis, uint32_t fFlags, PRTERRINFO pErrInfo, const char *pszErrorTag)
|
---|
171 | {
|
---|
172 | if (RT_UNLIKELY(!RTAsn1DynType_IsPresent(pThis)))
|
---|
173 | return RTErrInfoSetF(pErrInfo, VERR_ASN1_NOT_PRESENT, "%s: Missing (DYNTYPE).", pszErrorTag);
|
---|
174 |
|
---|
175 | int rc;
|
---|
176 | switch (pThis->enmType)
|
---|
177 | {
|
---|
178 | case RTASN1TYPE_CORE: rc = RTAsn1Core_CheckSanity(&pThis->u.Core, fFlags, pErrInfo, pszErrorTag); break;
|
---|
179 | case RTASN1TYPE_NULL: rc = RTAsn1Null_CheckSanity(&pThis->u.Asn1Null, fFlags, pErrInfo, pszErrorTag); break;
|
---|
180 | case RTASN1TYPE_INTEGER: rc = RTAsn1Integer_CheckSanity(&pThis->u.Integer, fFlags, pErrInfo, pszErrorTag); break;
|
---|
181 | case RTASN1TYPE_BOOLEAN: rc = RTAsn1Boolean_CheckSanity(&pThis->u.Boolean, fFlags, pErrInfo, pszErrorTag); break;
|
---|
182 | case RTASN1TYPE_STRING: rc = RTAsn1String_CheckSanity(&pThis->u.String, fFlags, pErrInfo, pszErrorTag); break;
|
---|
183 | case RTASN1TYPE_OCTET_STRING: rc = RTAsn1OctetString_CheckSanity(&pThis->u.OctetString, fFlags, pErrInfo, pszErrorTag); break;
|
---|
184 | case RTASN1TYPE_BIT_STRING: rc = RTAsn1BitString_CheckSanity(&pThis->u.BitString, fFlags, pErrInfo, pszErrorTag); break;
|
---|
185 | case RTASN1TYPE_TIME: rc = RTAsn1Time_CheckSanity(&pThis->u.Time, fFlags, pErrInfo, pszErrorTag); break;
|
---|
186 | #if 0
|
---|
187 | case RTASN1TYPE_SEQUENCE_CORE: rc = VINF_SUCCESS; //rc = RTAsn1SequenceCore_CheckSanity(&pThis->u.SeqCore, fFlags, pErrInfo, pszErrorTag); break;
|
---|
188 | case RTASN1TYPE_SET_CORE: rc = VINF_SUCCESS; //rc = RTAsn1SetCore_CheckSanity(&pThis->u.SetCore, fFlags, pErrInfo, pszErrorTag); break;
|
---|
189 | #endif
|
---|
190 | case RTASN1TYPE_OBJID: rc = RTAsn1ObjId_CheckSanity(&pThis->u.ObjId, fFlags, pErrInfo, pszErrorTag); break;
|
---|
191 | default:
|
---|
192 | AssertFailedReturn(VERR_ASN1_INTERNAL_ERROR_2);
|
---|
193 | }
|
---|
194 |
|
---|
195 | return rc;
|
---|
196 | }
|
---|
197 |
|
---|