1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/* $Id: asn1-ut-string-decode.cpp $ */
/** @file
* IPRT - ASN.1, XXX STRING Types, Decoding.
*/
/*
* Copyright (C) 2006-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include "internal/iprt.h"
#include <iprt/asn1.h>
#include <iprt/alloca.h>
#include <iprt/err.h>
#include <iprt/string.h>
#include <iprt/ctype.h>
#include <iprt/formats/asn1.h>
RTDECL(int) RTAsn1String_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pThis, const char *pszErrorTag)
{
RT_ZERO(*pThis);
AssertReturn(!(fFlags & RTASN1CURSOR_GET_F_IMPLICIT), VERR_INVALID_PARAMETER);
int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
if (RT_SUCCESS(rc))
{
/*
* Do tag matching.
*/
switch (pThis->Asn1Core.uTag)
{
case ASN1_TAG_UTF8_STRING:
case ASN1_TAG_NUMERIC_STRING:
case ASN1_TAG_PRINTABLE_STRING:
case ASN1_TAG_T61_STRING:
case ASN1_TAG_VIDEOTEX_STRING:
case ASN1_TAG_IA5_STRING:
case ASN1_TAG_GENERALIZED_TIME:
case ASN1_TAG_GRAPHIC_STRING:
case ASN1_TAG_VISIBLE_STRING:
case ASN1_TAG_GENERAL_STRING:
case ASN1_TAG_UNIVERSAL_STRING:
case ASN1_TAG_BMP_STRING:
rc = VINF_SUCCESS;
break;
default:
rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TAG_MISMATCH,
"%s: Not a string object: fClass=%#x / uTag=%#x",
pszErrorTag, pThis->Asn1Core.fClass, pThis->Asn1Core.uTag);
}
if (RT_SUCCESS(rc))
{
/*
* Match flags. CER/DER makes it complicated.
*/
if (pThis->Asn1Core.fClass == (ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE))
{
/*
* Primitive strings are simple.
*/
RTAsn1CursorSkip(pCursor, pThis->Asn1Core.cb);
pThis->Asn1Core.pOps = &g_RTAsn1String_Vtable;
pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
RTAsn1CursorInitAllocation(pCursor, &pThis->Allocation);
/* UTF-8 conversion is done lazily, upon request. */
return VINF_SUCCESS;
}
if (pThis->Asn1Core.fClass == (ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_CONSTRUCTED))
{
/*
* Constructed strings are not yet fully implemented.
*/
if (pCursor->fFlags & RTASN1CURSOR_FLAGS_DER)
rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
"%s: DER encoding does not allow constructed strings (cb=%#x uTag=%#x fClass=%#x)",
pszErrorTag, pThis->Asn1Core.cb, pThis->Asn1Core.uTag, pThis->Asn1Core.fClass);
else if (pCursor->fFlags & RTASN1CURSOR_FLAGS_CER)
{
if (pThis->Asn1Core.cb > 1000)
rc = VINF_SUCCESS;
else
rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
"%s: Constructed strings only allowed for >1000 byte in CER encoding: cb=%#x uTag=%#x fClass=%#x",
pszErrorTag, pThis->Asn1Core.cb,
pThis->Asn1Core.uTag, pThis->Asn1Core.fClass);
}
/** @todo implement constructed strings. */
if (RT_SUCCESS(rc))
rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CONSTRUCTED_STRING_NOT_IMPL,
"%s: Support for constructed strings is not implemented", pszErrorTag);
}
else
rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TAG_FLAG_CLASS_MISMATCH,
"%s: Not a valid string object: fClass=%#x / uTag=%#x",
pszErrorTag, pThis->Asn1Core.fClass, pThis->Asn1Core.uTag);
}
}
RT_ZERO(*pThis);
return rc;
}
/**
* Common worker for the specific string type getters.
*
* @returns IPRT status code
* @param pCursor The cursor.
* @param fFlags The RTASN1CURSOR_GET_F_XXX flags.
* @param uTag The string tag.
* @param pThis The output object.
* @param pszErrorTag The error tag.
* @param pszWhat The string type name.
*/
static int rtAsn1XxxString_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, uint8_t uTag, PRTASN1STRING pThis,
const char *pszErrorTag, const char *pszWhat)
{
pThis->cchUtf8 = 0;
pThis->pszUtf8 = NULL;
int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
if (RT_SUCCESS(rc))
{
rc = RTAsn1CursorMatchTagClassFlagsString(pCursor, &pThis->Asn1Core, uTag,
ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
fFlags, pszErrorTag, pszWhat);
if (RT_SUCCESS(rc))
{
if (!(pThis->Asn1Core.fClass & ASN1_TAGFLAG_CONSTRUCTED))
{
RTAsn1CursorSkip(pCursor, pThis->Asn1Core.cb);
pThis->Asn1Core.pOps = &g_RTAsn1String_Vtable;
pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
RTAsn1CursorInitAllocation(pCursor, &pThis->Allocation);
/* UTF-8 conversion is done lazily, upon request. */
return VINF_SUCCESS;
}
rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CONSTRUCTED_STRING_NOT_IMPL,
"%s: Constructed %s not implemented.", pszErrorTag, pszWhat);
}
}
RT_ZERO(*pThis);
return rc;
}
/*
* Generate code for the tag specific decoders.
*/
#define RTASN1STRING_IMPL(a_uTag, a_szTag, a_Api) \
RTDECL(int) RT_CONCAT(a_Api,_DecodeAsn1)(PRTASN1CURSOR pCursor, uint32_t fFlags, \
PRTASN1STRING pThis, const char *pszErrorTag) \
{ \
return rtAsn1XxxString_DecodeAsn1(pCursor, fFlags, a_uTag, pThis, pszErrorTag, a_szTag); \
}
#include "asn1-ut-string-template2.h"
/*
* Generate code for the associated collection types.
*/
#define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-string-template.h"
#include <iprt/asn1-generator-internal-header.h>
#include <iprt/asn1-generator-asn1-decoder.h>
|