1 | /* $Id: asn1-ut-objid-decode.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - ASN.1, OBJECT IDENTIFIER Type, Decoder.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 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 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "internal/iprt.h"
|
---|
32 | #include <iprt/asn1.h>
|
---|
33 |
|
---|
34 | #include <iprt/alloca.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/ctype.h>
|
---|
38 |
|
---|
39 | #include <iprt/formats/asn1.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | /*********************************************************************************************************************************
|
---|
43 | * Global Variables *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 | static char const g_achDigits[11] = "0123456789";
|
---|
46 |
|
---|
47 |
|
---|
48 | /*********************************************************************************************************************************
|
---|
49 | * Internal Functions *
|
---|
50 | *********************************************************************************************************************************/
|
---|
51 | DECLHIDDEN(int) rtAsn1ObjId_InternalFormatComponent(uint32_t uValue, char **ppszObjId, size_t *pcbObjId); /* asn1-ut-objid.cpp */
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Internal worker for RTAsn1ObjId_DecodeAsn1 that formats a component, with a
|
---|
56 | * leading dot.
|
---|
57 | *
|
---|
58 | * @returns VBox status code (caller complains on failure).
|
---|
59 | * @param uValue The component ID value.
|
---|
60 | * @param ppszObjId Pointer to the output buffer pointer.
|
---|
61 | * @param pcbObjId Pointer to the remaining size of the output buffer.
|
---|
62 | */
|
---|
63 | DECLHIDDEN(int) rtAsn1ObjId_InternalFormatComponent(uint32_t uValue, char **ppszObjId, size_t *pcbObjId)
|
---|
64 | {
|
---|
65 | /*
|
---|
66 | * Format the number backwards.
|
---|
67 | */
|
---|
68 | char szTmp[32];
|
---|
69 | char *psz = &szTmp[sizeof(szTmp) - 1];
|
---|
70 | *psz = '\0';
|
---|
71 |
|
---|
72 | do
|
---|
73 | {
|
---|
74 | *--psz = g_achDigits[uValue % 10];
|
---|
75 | uValue /= 10;
|
---|
76 | } while (uValue > 0);
|
---|
77 |
|
---|
78 | /*
|
---|
79 | * Do we have enough space?
|
---|
80 | * We add a dot and save space for the terminator.
|
---|
81 | */
|
---|
82 | size_t cchNeeded = &szTmp[sizeof(szTmp) - 1] - psz;
|
---|
83 | if (1 + cchNeeded < *pcbObjId)
|
---|
84 | {
|
---|
85 | *pcbObjId -= cchNeeded + 1;
|
---|
86 | char *pszObjId = *ppszObjId;
|
---|
87 | *ppszObjId = pszObjId + cchNeeded + 1;
|
---|
88 |
|
---|
89 | *pszObjId = '.';
|
---|
90 | memcpy(pszObjId + 1, psz, cchNeeded);
|
---|
91 | return VINF_SUCCESS;
|
---|
92 | }
|
---|
93 |
|
---|
94 | AssertFailed();
|
---|
95 | return VERR_ASN1_OBJID_TOO_LONG_STRING_FORM;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Reads one object ID component, returning it's value and encoded length.
|
---|
101 | *
|
---|
102 | * @returns The encoded length (positive) on success, negative IPRT status code
|
---|
103 | * on failure.
|
---|
104 | * @param pbContent The start of the component to parse.
|
---|
105 | * @param cbContent The number of content bytes left.
|
---|
106 | * @param puValue Where to return the value.
|
---|
107 | */
|
---|
108 | static int rtAsn1ObjId_ReadComponent(uint8_t const *pbContent, uint32_t cbContent, uint32_t *puValue)
|
---|
109 | {
|
---|
110 | if (cbContent >= 1)
|
---|
111 | {
|
---|
112 | /* The first byte. */
|
---|
113 | uint8_t b = *pbContent;
|
---|
114 | if (!(b & 0x80))
|
---|
115 | {
|
---|
116 | *puValue = b;
|
---|
117 | return 1;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /* Encoded as more than one byte. Make sure that it's efficently
|
---|
121 | encoded as 8.19.2 indicates it must. */
|
---|
122 | if (b != 0x80)
|
---|
123 | {
|
---|
124 | uint32_t off = 1;
|
---|
125 | uint32_t uValue = b & 0x7f;
|
---|
126 | while (off < cbContent)
|
---|
127 | {
|
---|
128 | b = pbContent[off++];
|
---|
129 | uValue <<= 7;
|
---|
130 | uValue |= b & 0x7f;
|
---|
131 | if (!(b & 0x80))
|
---|
132 | {
|
---|
133 | *puValue = uValue;
|
---|
134 | return (int)off;
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (RT_UNLIKELY(uValue & UINT32_C(0x0e000000)))
|
---|
138 | return VERR_ASN1_OBJID_COMPONENT_TOO_BIG;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | return VERR_ASN1_INVALID_OBJID_ENCODING;
|
---|
142 | }
|
---|
143 | return VERR_NO_DATA;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * This function parses the binary content of an OBJECT IDENTIFIER, check the
|
---|
149 | * encoding as well as calculating the storage requirements.
|
---|
150 | *
|
---|
151 | * @returns IPRT status code
|
---|
152 | * @param pbContent Pointer to the content.
|
---|
153 | * @param cbContent The length of the content.
|
---|
154 | * @param pCursor The cursor (for error reporting).
|
---|
155 | * @param pszErrorTag The error tag.
|
---|
156 | * @param pcComponents Where to return the component count.
|
---|
157 | * @param pcchObjId Where to return the length of the dotted string
|
---|
158 | * representation.
|
---|
159 | */
|
---|
160 | static int rtAsn1ObjId_PreParse(uint8_t const *pbContent, uint32_t cbContent, PRTASN1CURSOR pCursor, const char *pszErrorTag,
|
---|
161 | uint8_t *pcComponents, uint8_t *pcchObjId)
|
---|
162 | {
|
---|
163 | int rc;
|
---|
164 | if (cbContent >= 1 && cbContent < _1K)
|
---|
165 | {
|
---|
166 | /*
|
---|
167 | * Decode the first two numbers. Monkey business: X*40 + Y
|
---|
168 | * Where X is the first number, X in {0,1,2}, and Y is the second
|
---|
169 | * one. The range of Y is {0,...,39} for X in {0,1}, but has a
|
---|
170 | * free range for X = 2.
|
---|
171 | */
|
---|
172 | uint32_t cComponents = 1;
|
---|
173 | uint32_t uValue;
|
---|
174 | rc = rtAsn1ObjId_ReadComponent(pbContent, cbContent, &uValue);
|
---|
175 | if (rc > 0)
|
---|
176 | {
|
---|
177 | uint32_t cchObjId = 1;
|
---|
178 | uValue = uValue < 2*40 ? uValue % 40 : uValue - 2*40; /* Y */
|
---|
179 | do
|
---|
180 | {
|
---|
181 | cComponents++;
|
---|
182 |
|
---|
183 | /* Figure the encoded string length, binary search fashion. */
|
---|
184 | if (uValue < 10000)
|
---|
185 | {
|
---|
186 | if (uValue < 100)
|
---|
187 | {
|
---|
188 | if (uValue < 10)
|
---|
189 | cchObjId += 1 + 1;
|
---|
190 | else
|
---|
191 | cchObjId += 1 + 2;
|
---|
192 | }
|
---|
193 | else
|
---|
194 | {
|
---|
195 | if (uValue < 1000)
|
---|
196 | cchObjId += 1 + 3;
|
---|
197 | else
|
---|
198 | cchObjId += 1 + 4;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | else
|
---|
202 | {
|
---|
203 | if (uValue < 1000000)
|
---|
204 | {
|
---|
205 | if (uValue < 100000)
|
---|
206 | cchObjId += 1 + 5;
|
---|
207 | else
|
---|
208 | cchObjId += 1 + 6;
|
---|
209 | }
|
---|
210 | else
|
---|
211 | {
|
---|
212 | if (uValue < 10000000)
|
---|
213 | cchObjId += 1 + 7;
|
---|
214 | else if (uValue < 100000000)
|
---|
215 | cchObjId += 1 + 8;
|
---|
216 | else
|
---|
217 | cchObjId += 1 + 9;
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | /* advance. */
|
---|
222 | pbContent += rc;
|
---|
223 | cbContent -= rc;
|
---|
224 | if (!cbContent)
|
---|
225 | {
|
---|
226 | if (cComponents < 128)
|
---|
227 | {
|
---|
228 | if (cchObjId < RT_SIZEOFMEMB(RTASN1OBJID, szObjId))
|
---|
229 | {
|
---|
230 | *pcComponents = cComponents;
|
---|
231 | *pcchObjId = cchObjId;
|
---|
232 | return VINF_SUCCESS;
|
---|
233 | }
|
---|
234 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_OBJID_TOO_LONG_STRING_FORM,
|
---|
235 | "%s: Object ID has a too long string form: %#x (max %#x)",
|
---|
236 | pszErrorTag, cchObjId, RT_SIZEOFMEMB(RTASN1OBJID, szObjId));
|
---|
237 | }
|
---|
238 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_OBJID_TOO_MANY_COMPONENTS,
|
---|
239 | "%s: Object ID has too many components: %#x (max 127)", pszErrorTag, cComponents);
|
---|
240 | }
|
---|
241 |
|
---|
242 | /* next */
|
---|
243 | rc = rtAsn1ObjId_ReadComponent(pbContent, cbContent, &uValue);
|
---|
244 | } while (rc > 0);
|
---|
245 | }
|
---|
246 | rc = RTAsn1CursorSetInfo(pCursor, rc, "%s: Bad object ID component #%u encoding: %.*Rhxs",
|
---|
247 | pszErrorTag, cComponents, cbContent, pbContent);
|
---|
248 | }
|
---|
249 | else if (cbContent)
|
---|
250 | rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_OBJID_ENCODING, "%s: Object ID content is loo long: %#x",
|
---|
251 | pszErrorTag, cbContent);
|
---|
252 | else
|
---|
253 | rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_OBJID_ENCODING, "%s: Zero length object ID content", pszErrorTag);
|
---|
254 | return rc;
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 |
|
---|
259 | RTDECL(int) RTAsn1ObjId_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1OBJID pThis, const char *pszErrorTag)
|
---|
260 | {
|
---|
261 | int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
|
---|
262 | if (RT_SUCCESS(rc))
|
---|
263 | {
|
---|
264 | rc = RTAsn1CursorMatchTagClassFlags(pCursor, &pThis->Asn1Core, ASN1_TAG_OID,
|
---|
265 | ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE, fFlags, pszErrorTag, "OID");
|
---|
266 | if (RT_SUCCESS(rc))
|
---|
267 | {
|
---|
268 | /*
|
---|
269 | * Validate and count things first.
|
---|
270 | */
|
---|
271 | uint8_t cComponents = 0; /* gcc maybe-crap */
|
---|
272 | uint8_t cchObjId = 0; /* ditto */
|
---|
273 | rc = rtAsn1ObjId_PreParse(pCursor->pbCur, pThis->Asn1Core.cb, pCursor, pszErrorTag, &cComponents, &cchObjId);
|
---|
274 | if (RT_SUCCESS(rc))
|
---|
275 | {
|
---|
276 | /*
|
---|
277 | * Allocate memory for the components array, either out of the
|
---|
278 | * string buffer or off the heap.
|
---|
279 | */
|
---|
280 | pThis->cComponents = cComponents;
|
---|
281 | RTAsn1CursorInitAllocation(pCursor, &pThis->Allocation);
|
---|
282 | #if 0 /** @todo breaks with arrays of ObjIds or structs containing them. They get resized and repositioned in memory, thus invalidating the pointer. Add recall-pointers callback, or just waste memory? Or maybe make all arrays pointer-arrays? */
|
---|
283 | if (cComponents * sizeof(uint32_t) <= sizeof(pThis->szObjId) - cchObjId - 1)
|
---|
284 | pThis->pauComponents = (uint32_t *)&pThis->szObjId[sizeof(pThis->szObjId) - cComponents * sizeof(uint32_t)];
|
---|
285 | else
|
---|
286 | #endif
|
---|
287 | rc = RTAsn1MemAllocZ(&pThis->Allocation, (void **)&pThis->pauComponents,
|
---|
288 | cComponents * sizeof(pThis->pauComponents[0]));
|
---|
289 | if (RT_SUCCESS(rc))
|
---|
290 | {
|
---|
291 | uint32_t *pauComponents = (uint32_t *)pThis->pauComponents;
|
---|
292 |
|
---|
293 | /*
|
---|
294 | * Deal with the two first components first since they are
|
---|
295 | * encoded in a weird way to save a byte.
|
---|
296 | */
|
---|
297 | uint8_t const *pbContent = pCursor->pbCur;
|
---|
298 | uint32_t cbContent = pThis->Asn1Core.cb;
|
---|
299 | uint32_t uValue;
|
---|
300 | rc = rtAsn1ObjId_ReadComponent(pbContent, cbContent, &uValue); AssertRC(rc);
|
---|
301 | if (RT_SUCCESS(rc))
|
---|
302 | {
|
---|
303 | pbContent += rc;
|
---|
304 | cbContent -= rc;
|
---|
305 |
|
---|
306 | if (uValue < 80)
|
---|
307 | {
|
---|
308 | pauComponents[0] = uValue / 40;
|
---|
309 | pauComponents[1] = uValue % 40;
|
---|
310 | }
|
---|
311 | else
|
---|
312 | {
|
---|
313 | pauComponents[0] = 2;
|
---|
314 | pauComponents[1] = uValue - 2*40;
|
---|
315 | }
|
---|
316 |
|
---|
317 | char *pszObjId = &pThis->szObjId[0];
|
---|
318 | *pszObjId++ = g_achDigits[pauComponents[0]];
|
---|
319 | size_t cbObjIdLeft = cchObjId + 1 - 1;
|
---|
320 |
|
---|
321 | rc = rtAsn1ObjId_InternalFormatComponent(pauComponents[1], &pszObjId, &cbObjIdLeft); AssertRC(rc);
|
---|
322 | if (RT_SUCCESS(rc))
|
---|
323 | {
|
---|
324 | /*
|
---|
325 | * The other components are encoded in less complicated manner.
|
---|
326 | */
|
---|
327 | for (uint32_t i = 2; i < cComponents; i++)
|
---|
328 | {
|
---|
329 | rc = rtAsn1ObjId_ReadComponent(pbContent, cbContent, &uValue);
|
---|
330 | AssertRCBreak(rc);
|
---|
331 | pbContent += rc;
|
---|
332 | cbContent -= rc;
|
---|
333 | pauComponents[i] = uValue;
|
---|
334 | rc = rtAsn1ObjId_InternalFormatComponent(uValue, &pszObjId, &cbObjIdLeft);
|
---|
335 | AssertRCBreak(rc);
|
---|
336 | }
|
---|
337 | if (RT_SUCCESS(rc))
|
---|
338 | {
|
---|
339 | Assert(cbObjIdLeft == 1);
|
---|
340 | *pszObjId = '\0';
|
---|
341 |
|
---|
342 | RTAsn1CursorSkip(pCursor, pThis->Asn1Core.cb);
|
---|
343 | pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
|
---|
344 | pThis->Asn1Core.pOps = &g_RTAsn1ObjId_Vtable;
|
---|
345 | return VINF_SUCCESS;
|
---|
346 | }
|
---|
347 | }
|
---|
348 | }
|
---|
349 | }
|
---|
350 | }
|
---|
351 | }
|
---|
352 | }
|
---|
353 | RT_ZERO(*pThis);
|
---|
354 | return rc;
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 |
|
---|
359 | /*
|
---|
360 | * Generate code for the associated collection types.
|
---|
361 | */
|
---|
362 | #define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-objid-template.h"
|
---|
363 | #include <iprt/asn1-generator-internal-header.h>
|
---|
364 | #include <iprt/asn1-generator-asn1-decoder.h>
|
---|
365 |
|
---|