summaryrefslogtreecommitdiff
path: root/src/VBox/Runtime/common/asn1/asn1-ut-objid-decode.cpp
blob: b1cae563e8dc4106337dcd3d0617d1a0ed166a7e (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* $Id$ */
/** @file
 * IPRT - ASN.1, OBJECT IDENTIFIER Type, Decoder.
 */

/*
 * Copyright (C) 2006-2022 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>


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
static char const g_achDigits[11] = "0123456789";


/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/
DECLHIDDEN(int) rtAsn1ObjId_InternalFormatComponent(uint32_t uValue, char **ppszObjId, size_t *pcbObjId); /* asn1-ut-objid.cpp */


/**
 * Internal worker for RTAsn1ObjId_DecodeAsn1 that formats a component, with a
 * leading dot.
 *
 * @returns VBox status code (caller complains on failure).
 * @param   uValue      The component ID value.
 * @param   ppszObjId   Pointer to the output buffer pointer.
 * @param   pcbObjId    Pointer to the remaining size of the output buffer.
 */
DECLHIDDEN(int) rtAsn1ObjId_InternalFormatComponent(uint32_t uValue, char **ppszObjId, size_t *pcbObjId)
{
    /*
     * Format the number backwards.
     */
    char szTmp[32];
    char *psz = &szTmp[sizeof(szTmp) - 1];
    *psz = '\0';

    do
    {
        *--psz = g_achDigits[uValue % 10];
        uValue /= 10;
    } while (uValue > 0);

    /*
     * Do we have enough space?
     * We add a dot and save space for the terminator.
     */
    size_t cchNeeded = &szTmp[sizeof(szTmp) - 1] - psz;
    if (1 + cchNeeded < *pcbObjId)
    {
        *pcbObjId  -= cchNeeded + 1;
        char *pszObjId = *ppszObjId;
        *ppszObjId = pszObjId + cchNeeded + 1;

        *pszObjId = '.';
        memcpy(pszObjId + 1, psz, cchNeeded);
        return VINF_SUCCESS;
    }

    AssertFailed();
    return VERR_ASN1_OBJID_TOO_LONG_STRING_FORM;
}


/**
 * Reads one object ID component, returning it's value and encoded length.
 *
 * @returns The encoded length (positive) on success, negative IPRT status code
 *          on failure.
 * @param   pbContent           The start of the component to parse.
 * @param   cbContent           The number of content bytes left.
 * @param   puValue             Where to return the value.
 */
static int rtAsn1ObjId_ReadComponent(uint8_t const *pbContent, uint32_t cbContent, uint32_t *puValue)
{
    if (cbContent >= 1)
    {
        /* The first byte. */
        uint8_t b = *pbContent;
        if (!(b & 0x80))
        {
            *puValue = b;
            return 1;
        }

        /* Encoded as more than one byte.  Make sure that it's efficently
           encoded as 8.19.2 indicates it must. */
        if (b != 0x80)
        {
            uint32_t off    = 1;
            uint32_t uValue = b & 0x7f;
            while (off < cbContent)
            {
                b = pbContent[off++];
                uValue <<= 7;
                uValue |= b & 0x7f;
                if (!(b & 0x80))
                {
                    *puValue = uValue;
                    return (int)off;
                }

                if (RT_UNLIKELY(uValue & UINT32_C(0x0e000000)))
                    return VERR_ASN1_OBJID_COMPONENT_TOO_BIG;
            }
        }
        return VERR_ASN1_INVALID_OBJID_ENCODING;
    }
    return VERR_NO_DATA;
}


/**
 * This function parses the binary content of an OBJECT IDENTIFIER, check the
 * encoding as well as calculating the storage requirements.
 *
 * @returns IPRT status code
 * @param   pbContent       Pointer to the content.
 * @param   cbContent       The length of the content.
 * @param   pCursor         The cursor (for error reporting).
 * @param   pszErrorTag     The error tag.
 * @param   pcComponents    Where to return the component count.
 * @param   pcchObjId       Where to return the length of the dotted string
 *                          representation.
 */
static int rtAsn1ObjId_PreParse(uint8_t const *pbContent, uint32_t cbContent, PRTASN1CURSOR pCursor, const char *pszErrorTag,
                                uint8_t *pcComponents, uint8_t *pcchObjId)
{
    int rc;
    if (cbContent >= 1 && cbContent < _1K)
    {
        /*
         * Decode the first two numbers.  Monkey business: X*40 + Y
         * Where X is the first number, X in {0,1,2}, and Y is the second
         * one.  The range of Y is {0,...,39} for X in {0,1}, but has a
         * free range for X = 2.
         */
        uint32_t cComponents = 1;
        uint32_t uValue;
        rc = rtAsn1ObjId_ReadComponent(pbContent, cbContent, &uValue);
        if (rc > 0)
        {
            uint32_t cchObjId = 1;
            uValue = uValue < 2*40 ? uValue % 40 : uValue - 2*40; /* Y */
            do
            {
                cComponents++;

                /* Figure the encoded string length, binary search fashion. */
                if (uValue < 10000)
                {
                    if (uValue < 100)
                    {
                        if (uValue < 10)
                            cchObjId += 1 + 1;
                        else
                            cchObjId += 1 + 2;
                    }
                    else
                    {
                        if (uValue < 1000)
                            cchObjId += 1 + 3;
                        else
                            cchObjId += 1 + 4;
                    }
                }
                else
                {
                    if (uValue < 1000000)
                    {
                        if (uValue < 100000)
                            cchObjId += 1 + 5;
                        else
                            cchObjId += 1 + 6;
                    }
                    else
                    {
                        if (uValue < 10000000)
                            cchObjId += 1 + 7;
                        else if (uValue < 100000000)
                            cchObjId += 1 + 8;
                        else
                            cchObjId += 1 + 9;
                    }
                }

                /* advance. */
                pbContent += rc;
                cbContent -= rc;
                if (!cbContent)
                {
                    if (cComponents < 128)
                    {
                        if (cchObjId < RT_SIZEOFMEMB(RTASN1OBJID, szObjId))
                        {
                            *pcComponents = cComponents;
                            *pcchObjId    = cchObjId;
                            return VINF_SUCCESS;
                        }
                        return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_OBJID_TOO_LONG_STRING_FORM,
                                                   "%s: Object ID has a too long string form: %#x (max %#x)",
                                                   pszErrorTag, cchObjId, RT_SIZEOFMEMB(RTASN1OBJID, szObjId));
                    }
                    return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_OBJID_TOO_MANY_COMPONENTS,
                                               "%s: Object ID has too many components: %#x (max 127)", pszErrorTag, cComponents);
                }

                /* next */
                rc = rtAsn1ObjId_ReadComponent(pbContent, cbContent, &uValue);
            } while (rc > 0);
        }
        rc = RTAsn1CursorSetInfo(pCursor, rc, "%s: Bad object ID component #%u encoding: %.*Rhxs",
                                 pszErrorTag, cComponents, cbContent, pbContent);
    }
    else if (cbContent)
        rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_OBJID_ENCODING, "%s: Object ID content is loo long: %#x",
                                 pszErrorTag, cbContent);
    else
        rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_OBJID_ENCODING, "%s: Zero length object ID content", pszErrorTag);
    return rc;
}



RTDECL(int) RTAsn1ObjId_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1OBJID pThis, const char *pszErrorTag)
{
    int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
    if (RT_SUCCESS(rc))
    {
        rc = RTAsn1CursorMatchTagClassFlags(pCursor, &pThis->Asn1Core, ASN1_TAG_OID,
                                            ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE, fFlags, pszErrorTag, "OID");
        if (RT_SUCCESS(rc))
        {
            /*
             * Validate and count things first.
             */
            uint8_t cComponents = 0; /* gcc maybe-crap */
            uint8_t cchObjId = 0;    /* ditto */
            rc = rtAsn1ObjId_PreParse(pCursor->pbCur, pThis->Asn1Core.cb, pCursor, pszErrorTag, &cComponents, &cchObjId);
            if (RT_SUCCESS(rc))
            {
                /*
                 * Allocate memory for the components array, either out of the
                 * string buffer or off the heap.
                 */
                pThis->cComponents = cComponents;
                RTAsn1CursorInitAllocation(pCursor, &pThis->Allocation);
#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? */
                if (cComponents * sizeof(uint32_t) <= sizeof(pThis->szObjId) - cchObjId - 1)
                    pThis->pauComponents = (uint32_t *)&pThis->szObjId[sizeof(pThis->szObjId) - cComponents * sizeof(uint32_t)];
                else
#endif
                    rc = RTAsn1MemAllocZ(&pThis->Allocation, (void **)&pThis->pauComponents,
                                         cComponents * sizeof(pThis->pauComponents[0]));
                if (RT_SUCCESS(rc))
                {
                    uint32_t *pauComponents = (uint32_t *)pThis->pauComponents;

                    /*
                     * Deal with the two first components first since they are
                     * encoded in a weird way to save a byte.
                     */
                    uint8_t const  *pbContent = pCursor->pbCur;
                    uint32_t        cbContent = pThis->Asn1Core.cb;
                    uint32_t        uValue;
                    rc = rtAsn1ObjId_ReadComponent(pbContent, cbContent, &uValue); AssertRC(rc);
                    if (RT_SUCCESS(rc))
                    {
                        pbContent += rc;
                        cbContent -= rc;

                        if (uValue < 80)
                        {
                            pauComponents[0] = uValue / 40;
                            pauComponents[1] = uValue % 40;
                        }
                        else
                        {
                            pauComponents[0] = 2;
                            pauComponents[1] = uValue - 2*40;
                        }

                        char  *pszObjId    = &pThis->szObjId[0];
                        *pszObjId++        = g_achDigits[pauComponents[0]];
                        size_t cbObjIdLeft = cchObjId + 1 - 1;

                        rc = rtAsn1ObjId_InternalFormatComponent(pauComponents[1], &pszObjId, &cbObjIdLeft); AssertRC(rc);
                        if (RT_SUCCESS(rc))
                        {
                            /*
                             * The other components are encoded in less complicated manner.
                             */
                            for (uint32_t i = 2; i < cComponents; i++)
                            {
                                rc = rtAsn1ObjId_ReadComponent(pbContent, cbContent, &uValue);
                                AssertRCBreak(rc);
                                pbContent += rc;
                                cbContent -= rc;
                                pauComponents[i] = uValue;
                                rc = rtAsn1ObjId_InternalFormatComponent(uValue, &pszObjId, &cbObjIdLeft);
                                AssertRCBreak(rc);
                            }
                            if (RT_SUCCESS(rc))
                            {
                                Assert(cbObjIdLeft == 1);
                                *pszObjId = '\0';

                                RTAsn1CursorSkip(pCursor, pThis->Asn1Core.cb);
                                pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
                                pThis->Asn1Core.pOps = &g_RTAsn1ObjId_Vtable;
                                return VINF_SUCCESS;
                            }
                        }
                    }
                    RTAsn1MemFree(&pThis->Allocation, (void *)pThis->pauComponents);
                    pThis->pauComponents = NULL;
                }
            }
        }
    }
    RT_ZERO(*pThis);
    return rc;
}



/*
 * Generate code for the associated collection types.
 */
#define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-objid-template.h"
#include <iprt/asn1-generator-internal-header.h>
#include <iprt/asn1-generator-asn1-decoder.h>