VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/asn1/asn1-ut-string-decode.cpp

Last change on this file was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
  • Property svn:mergeinfo set to (toggle deleted branches)
    /branches/VBox-3.0/src/VBox/Runtime/common/asn1/asn1-basics.cpp58652,​70973
    /branches/VBox-3.2/src/VBox/Runtime/common/asn1/asn1-basics.cpp66309,​66318
    /branches/VBox-4.0/src/VBox/Runtime/common/asn1/asn1-basics.cpp70873
    /branches/VBox-4.1/src/VBox/Runtime/common/asn1/asn1-basics.cpp74233,​78414,​78691,​81841,​82127,​85941,​85944-85947,​85949-85950,​85953,​86701,​86728,​87009
    /branches/VBox-4.2/src/VBox/Runtime/common/asn1/asn1-basics.cpp86229-86230,​86234,​86529,​91503-91504,​91506-91508,​91510,​91514-91515,​91521
    /branches/VBox-4.3/src/VBox/Runtime/common/asn1/asn1-basics.cpp91223
    /branches/VBox-4.3/trunk/src/VBox/Runtime/common/asn1/asn1-basics.cpp91223
    /branches/andy/draganddrop/src/VBox/Runtime/common/asn1/asn1-basics.cpp90781-91268
    /branches/andy/guestctrl20/src/VBox/Runtime/common/asn1/asn1-basics.cpp78916,​78930
    /branches/dsen/gui/src/VBox/Runtime/common/asn1/asn1-basics.cpp79076-79078,​79089,​79109-79110,​79112-79113,​79127-79130,​79134,​79141,​79151,​79155,​79157-79159,​79193,​79197
    /branches/dsen/gui2/src/VBox/Runtime/common/asn1/asn1-basics.cpp79224,​79228,​79233,​79235,​79258,​79262-79263,​79273,​79341,​79345,​79354,​79357,​79387-79388,​79559-79569,​79572-79573,​79578,​79581-79582,​79590-79591,​79598-79599,​79602-79603,​79605-79606,​79632,​79635,​79637,​79644
    /branches/dsen/gui3/src/VBox/Runtime/common/asn1/asn1-basics.cpp79645-79692
File size: 8.2 KB
Line 
1/* $Id: asn1-ut-string-decode.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - ASN.1, XXX STRING Types, Decoding.
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/alloca.h>
45#include <iprt/err.h>
46#include <iprt/string.h>
47#include <iprt/ctype.h>
48
49#include <iprt/formats/asn1.h>
50
51
52RTDECL(int) RTAsn1String_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pThis, const char *pszErrorTag)
53{
54 RT_ZERO(*pThis);
55 AssertReturn(!(fFlags & RTASN1CURSOR_GET_F_IMPLICIT), VERR_INVALID_PARAMETER);
56
57 int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
58 if (RT_SUCCESS(rc))
59 {
60 /*
61 * Do tag matching.
62 */
63 switch (pThis->Asn1Core.uTag)
64 {
65 case ASN1_TAG_UTF8_STRING:
66 case ASN1_TAG_NUMERIC_STRING:
67 case ASN1_TAG_PRINTABLE_STRING:
68 case ASN1_TAG_T61_STRING:
69 case ASN1_TAG_VIDEOTEX_STRING:
70 case ASN1_TAG_IA5_STRING:
71 case ASN1_TAG_GENERALIZED_TIME:
72 case ASN1_TAG_GRAPHIC_STRING:
73 case ASN1_TAG_VISIBLE_STRING:
74 case ASN1_TAG_GENERAL_STRING:
75 case ASN1_TAG_UNIVERSAL_STRING:
76 case ASN1_TAG_BMP_STRING:
77 rc = VINF_SUCCESS;
78 break;
79 default:
80 rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TAG_MISMATCH,
81 "%s: Not a string object: fClass=%#x / uTag=%#x",
82 pszErrorTag, pThis->Asn1Core.fClass, pThis->Asn1Core.uTag);
83 }
84 if (RT_SUCCESS(rc))
85 {
86 /*
87 * Match flags. CER/DER makes it complicated.
88 */
89 if (pThis->Asn1Core.fClass == (ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE))
90 {
91 /*
92 * Primitive strings are simple.
93 */
94 RTAsn1CursorSkip(pCursor, pThis->Asn1Core.cb);
95 pThis->Asn1Core.pOps = &g_RTAsn1String_Vtable;
96 pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
97 RTAsn1CursorInitAllocation(pCursor, &pThis->Allocation);
98
99 /* UTF-8 conversion is done lazily, upon request. */
100 return VINF_SUCCESS;
101 }
102
103 if (pThis->Asn1Core.fClass == (ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_CONSTRUCTED))
104 {
105 /*
106 * Constructed strings are not yet fully implemented.
107 */
108 if (pCursor->fFlags & RTASN1CURSOR_FLAGS_DER)
109 rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
110 "%s: DER encoding does not allow constructed strings (cb=%#x uTag=%#x fClass=%#x)",
111 pszErrorTag, pThis->Asn1Core.cb, pThis->Asn1Core.uTag, pThis->Asn1Core.fClass);
112 else if (pCursor->fFlags & RTASN1CURSOR_FLAGS_CER)
113 {
114 if (pThis->Asn1Core.cb > 1000)
115 rc = VINF_SUCCESS;
116 else
117 rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
118 "%s: Constructed strings only allowed for >1000 byte in CER encoding: cb=%#x uTag=%#x fClass=%#x",
119 pszErrorTag, pThis->Asn1Core.cb,
120 pThis->Asn1Core.uTag, pThis->Asn1Core.fClass);
121 }
122 /** @todo implement constructed strings. */
123 if (RT_SUCCESS(rc))
124 rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CONSTRUCTED_STRING_NOT_IMPL,
125 "%s: Support for constructed strings is not implemented", pszErrorTag);
126 }
127 else
128 rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TAG_FLAG_CLASS_MISMATCH,
129 "%s: Not a valid string object: fClass=%#x / uTag=%#x",
130 pszErrorTag, pThis->Asn1Core.fClass, pThis->Asn1Core.uTag);
131 }
132 }
133 RT_ZERO(*pThis);
134 return rc;
135}
136
137
138/**
139 * Common worker for the specific string type getters.
140 *
141 * @returns IPRT status code
142 * @param pCursor The cursor.
143 * @param fFlags The RTASN1CURSOR_GET_F_XXX flags.
144 * @param uTag The string tag.
145 * @param pThis The output object.
146 * @param pszErrorTag The error tag.
147 * @param pszWhat The string type name.
148 */
149static int rtAsn1XxxString_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, uint8_t uTag, PRTASN1STRING pThis,
150 const char *pszErrorTag, const char *pszWhat)
151{
152 pThis->cchUtf8 = 0;
153 pThis->pszUtf8 = NULL;
154
155 int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
156 if (RT_SUCCESS(rc))
157 {
158 rc = RTAsn1CursorMatchTagClassFlagsString(pCursor, &pThis->Asn1Core, uTag,
159 ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
160 fFlags, pszErrorTag, pszWhat);
161 if (RT_SUCCESS(rc))
162 {
163 if (!(pThis->Asn1Core.fClass & ASN1_TAGFLAG_CONSTRUCTED))
164 {
165 RTAsn1CursorSkip(pCursor, pThis->Asn1Core.cb);
166 pThis->Asn1Core.pOps = &g_RTAsn1String_Vtable;
167 pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
168 RTAsn1CursorInitAllocation(pCursor, &pThis->Allocation);
169 /* UTF-8 conversion is done lazily, upon request. */
170 return VINF_SUCCESS;
171 }
172 rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CONSTRUCTED_STRING_NOT_IMPL,
173 "%s: Constructed %s not implemented.", pszErrorTag, pszWhat);
174 }
175 }
176 RT_ZERO(*pThis);
177 return rc;
178}
179
180
181/*
182 * Generate code for the tag specific decoders.
183 */
184#define RTASN1STRING_IMPL(a_uTag, a_szTag, a_Api) \
185 RTDECL(int) RT_CONCAT(a_Api,_DecodeAsn1)(PRTASN1CURSOR pCursor, uint32_t fFlags, \
186 PRTASN1STRING pThis, const char *pszErrorTag) \
187 { \
188 return rtAsn1XxxString_DecodeAsn1(pCursor, fFlags, a_uTag, pThis, pszErrorTag, a_szTag); \
189 }
190#include "asn1-ut-string-template2.h"
191
192
193/*
194 * Generate code for the associated collection types.
195 */
196#define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-string-template.h"
197#include <iprt/asn1-generator-internal-header.h>
198#include <iprt/asn1-generator-asn1-decoder.h>
199
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use