VirtualBox

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

Last change on this file since 55303 was 52563, checked in by vboxsync, 10 years ago

asn1: clear stuff on decode failure.

  • 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: 7.7 KB
Line 
1/* $Id: asn1-ut-string-decode.cpp 52563 2014-09-02 08:36:53Z vboxsync $ */
2/** @file
3 * IPRT - ASN.1, XXX STRING Types, Decoding.
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/alloca.h>
34#include <iprt/err.h>
35#include <iprt/string.h>
36#include <iprt/ctype.h>
37
38#include <iprt/formats/asn1.h>
39
40
41RTDECL(int) RTAsn1String_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pThis, const char *pszErrorTag)
42{
43 RT_ZERO(*pThis);
44 AssertReturn(!(fFlags & RTASN1CURSOR_GET_F_IMPLICIT), VERR_INVALID_PARAMETER);
45
46 int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
47 if (RT_SUCCESS(rc))
48 {
49 /*
50 * Do tag matching.
51 */
52 switch (pThis->Asn1Core.uTag)
53 {
54 case ASN1_TAG_UTF8_STRING:
55 case ASN1_TAG_NUMERIC_STRING:
56 case ASN1_TAG_PRINTABLE_STRING:
57 case ASN1_TAG_T61_STRING:
58 case ASN1_TAG_VIDEOTEX_STRING:
59 case ASN1_TAG_IA5_STRING:
60 case ASN1_TAG_GENERALIZED_TIME:
61 case ASN1_TAG_GRAPHIC_STRING:
62 case ASN1_TAG_VISIBLE_STRING:
63 case ASN1_TAG_GENERAL_STRING:
64 case ASN1_TAG_UNIVERSAL_STRING:
65 case ASN1_TAG_BMP_STRING:
66 rc = VINF_SUCCESS;
67 break;
68 default:
69 rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TAG_MISMATCH,
70 "%s: Not a string object: fClass=%#x / uTag=%#x",
71 pszErrorTag, pThis->Asn1Core.fClass, pThis->Asn1Core.uTag);
72 }
73 if (RT_SUCCESS(rc))
74 {
75 /*
76 * Match flags. CER/DER makes it complicated.
77 */
78 if (pThis->Asn1Core.fClass == (ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE))
79 {
80 /*
81 * Primitive strings are simple.
82 */
83 RTAsn1CursorSkip(pCursor, pThis->Asn1Core.cb);
84 pThis->Asn1Core.pOps = &g_RTAsn1String_Vtable;
85 pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
86 RTAsn1CursorInitAllocation(pCursor, &pThis->Allocation);
87
88 /* UTF-8 conversion is done lazily, upon request. */
89 return VINF_SUCCESS;
90 }
91
92 if (pThis->Asn1Core.fClass == (ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_CONSTRUCTED))
93 {
94 /*
95 * Constructed strings are not yet fully implemented.
96 */
97 if (pCursor->fFlags & RTASN1CURSOR_FLAGS_DER)
98 rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
99 "%s: DER encoding does not allow constructed strings (cb=%#x uTag=%#x fClass=%#x)",
100 pszErrorTag, pThis->Asn1Core.cb, pThis->Asn1Core.uTag, pThis->Asn1Core.fClass);
101 else if (pCursor->fFlags & RTASN1CURSOR_FLAGS_CER)
102 {
103 if (pThis->Asn1Core.cb > 1000)
104 rc = VINF_SUCCESS;
105 else
106 rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
107 "%s: Constructed strings only allowed for >1000 byte in CER encoding: cb=%#x uTag=%#x fClass=%#x",
108 pszErrorTag, pThis->Asn1Core.cb,
109 pThis->Asn1Core.uTag, pThis->Asn1Core.fClass);
110 }
111 /** @todo implement constructed strings. */
112 if (RT_SUCCESS(rc))
113 rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CONSTRUCTED_STRING_NOT_IMPL,
114 "%s: Support for constructed strings is not implemented", pszErrorTag);
115 }
116 else
117 rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TAG_FLAG_CLASS_MISMATCH,
118 "%s: Not a valid string object: fClass=%#x / uTag=%#x",
119 pszErrorTag, pThis->Asn1Core.fClass, pThis->Asn1Core.uTag);
120 }
121 }
122 RT_ZERO(*pThis);
123 return rc;
124}
125
126
127/**
128 * Common worker for the specific string type getters.
129 *
130 * @returns IPRT status code
131 * @param pCursor The cursor.
132 * @param fFlags The RTASN1CURSOR_GET_F_XXX flags.
133 * @param uTag The string tag.
134 * @param pThis The output object.
135 * @param pszErrorTag The error tag.
136 * @param pszWhat The string type name.
137 */
138static int rtAsn1XxxString_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, uint8_t uTag, PRTASN1STRING pThis,
139 const char *pszErrorTag, const char *pszWhat)
140{
141 pThis->cchUtf8 = 0;
142 pThis->pszUtf8 = NULL;
143
144 int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
145 if (RT_SUCCESS(rc))
146 {
147 rc = RTAsn1CursorMatchTagClassFlagsString(pCursor, &pThis->Asn1Core, uTag,
148 ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
149 fFlags, pszErrorTag, pszWhat);
150 if (RT_SUCCESS(rc))
151 {
152 if (!(pThis->Asn1Core.fClass & ASN1_TAGFLAG_CONSTRUCTED))
153 {
154 RTAsn1CursorSkip(pCursor, pThis->Asn1Core.cb);
155 pThis->Asn1Core.pOps = &g_RTAsn1String_Vtable;
156 pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
157 RTAsn1CursorInitAllocation(pCursor, &pThis->Allocation);
158 /* UTF-8 conversion is done lazily, upon request. */
159 return VINF_SUCCESS;
160 }
161 rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CONSTRUCTED_STRING_NOT_IMPL,
162 "%s: Constructed %s not implemented.", pszErrorTag, pszWhat);
163 }
164 }
165 RT_ZERO(*pThis);
166 return rc;
167}
168
169
170/*
171 * Generate code for the tag specific decoders.
172 */
173#define RTASN1STRING_IMPL(a_uTag, a_szTag, a_Api) \
174 RTDECL(int) RT_CONCAT(a_Api,_DecodeAsn1)(PRTASN1CURSOR pCursor, uint32_t fFlags, \
175 PRTASN1STRING pThis, const char *pszErrorTag) \
176 { \
177 return rtAsn1XxxString_DecodeAsn1(pCursor, fFlags, a_uTag, pThis, pszErrorTag, a_szTag); \
178 }
179#include "asn1-ut-string-template2.h"
180
181
182/*
183 * Generate code for the associated collection types.
184 */
185#define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-string-template.h"
186#include <iprt/asn1-generator-internal-header.h>
187#include <iprt/asn1-generator-asn1-decoder.h>
188
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