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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
/* $Id: asn1-ut-objid.cpp $ */
/** @file
* IPRT - ASN.1, OBJECT IDENTIFIER Type.
*/
/*
* 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/bignum.h>
#include <iprt/ctype.h>
#include <iprt/err.h>
#include <iprt/string.h>
#include <iprt/uni.h>
#include <iprt/formats/asn1.h>
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
static char const g_szDefault[] = "2.16.840.1.113894";
static uint32_t const g_auDefault[] = { 2, 16, 840, 1, 113894 };
static uint8_t const g_abDefault[] =
{
2*40 + 16, 0x80 | (840 >> 7), 840 & 0x7f, 1, 0x80 | (113894 >> 14), 0x80 | ((113894 >> 7) & 0x7f), 113894 & 0x7f
};
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
DECLHIDDEN(int) rtAsn1ObjId_InternalFormatComponent(uint32_t uValue, char **ppszObjId, size_t *pcbObjId); /* asn1-ut-objid.cpp */
/** @todo check if we really need this. */
/*
* ASN.1 OBJECT IDENTIFIER - Special Methods.
*/
/**
* Encodes the ASN.1 byte sequence for a set of components.
*
* @returns IPRT status code.
* @param cComponents The number of components. Must be at least two.
* @param pauComponents The components array.
* @param pbEncoded The output buffer.
* @param pcbEncoded On input, this holds the size of the output buffer.
* On successful return it's the encoded size in bytes.
*/
static int rtAsn1ObjId_EncodeComponents(uint32_t cComponents, uint32_t const *pauComponents,
uint8_t *pbEncoded, uint32_t *pcbEncoded)
{
uint8_t *pbCur = pbEncoded;
uint32_t cbLeft = *pcbEncoded;
/* The first two componets are encoded together to save a byte, so the loop
organization is a little special. */
AssertReturn(cComponents >= 2, VERR_ASN1_INTERNAL_ERROR_1);
AssertReturn(pauComponents[0] <= 2, VERR_ASN1_INTERNAL_ERROR_1);
AssertReturn(pauComponents[1] <= (pauComponents[0] < 2 ? 39 : UINT32_MAX - 80), VERR_ASN1_INTERNAL_ERROR_1);
uint32_t i = 1;
uint32_t uValue = pauComponents[0] * 40 + pauComponents[1];
for (;;)
{
if (uValue < 0x80)
{
if (RT_UNLIKELY(cbLeft < 1))
return VERR_BUFFER_OVERFLOW;
cbLeft -= 1;
*pbCur++ = (uint8_t)uValue;
}
else if (uValue < 0x4000)
{
if (RT_UNLIKELY(cbLeft < 2))
return VERR_BUFFER_OVERFLOW;
cbLeft -= 2;
pbCur[0] = (uValue >> 7) | 0x80;
pbCur[1] = uValue & 0x7f;
pbCur += 2;
}
else if (uValue < 0x200000)
{
if (RT_UNLIKELY(cbLeft < 3))
return VERR_BUFFER_OVERFLOW;
cbLeft -= 3;
pbCur[0] = (uValue >> 14) | 0x80;
pbCur[1] = ((uValue >> 7) & 0x7f) | 0x80;
pbCur[2] = uValue & 0x7f;
pbCur += 3;
}
else if (uValue < 0x10000000)
{
if (RT_UNLIKELY(cbLeft < 4))
return VERR_BUFFER_OVERFLOW;
cbLeft -= 4;
pbCur[0] = (uValue >> 21) | 0x80;
pbCur[1] = ((uValue >> 14) & 0x7f) | 0x80;
pbCur[2] = ((uValue >> 7) & 0x7f) | 0x80;
pbCur[3] = uValue & 0x7f;
pbCur += 4;
}
else
{
if (RT_UNLIKELY(cbLeft < 5))
return VERR_BUFFER_OVERFLOW;
cbLeft -= 5;
pbCur[0] = (uValue >> 28) | 0x80;
pbCur[1] = ((uValue >> 21) & 0x7f) | 0x80;
pbCur[2] = ((uValue >> 14) & 0x7f) | 0x80;
pbCur[3] = ((uValue >> 7) & 0x7f) | 0x80;
pbCur[4] = uValue & 0x7f;
pbCur += 5;
}
/* Advance / return. */
i++;
if (i >= cComponents)
{
*pcbEncoded = (uint32_t)(pbCur - pbEncoded);
return VINF_SUCCESS;
}
uValue = pauComponents[i];
}
}
RTDECL(int) RTAsn1ObjId_InitFromString(PRTASN1OBJID pThis, const char *pszObjId, PCRTASN1ALLOCATORVTABLE pAllocator)
{
RT_ZERO(*pThis);
/*
* Check the string, counting the number of components and checking their validity.
*/
size_t cbObjId = strlen(pszObjId) + 1;
AssertReturn(cbObjId < sizeof(pThis->szObjId), VERR_ASN1_OBJID_TOO_LONG_STRING_FORM);
const char *psz = pszObjId;
/* Special checking of the first component. It has only three valid values: 0,1,2. */
char ch = *psz++;
if (RT_UNLIKELY(ch < '0' || ch > '2'))
return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
char const chFirst = ch;
ch = *psz++;
if (RT_UNLIKELY(ch != '.'))
return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
/* The 2nd component. It the first is 0 or 1, it has a max of 39. */
uint32_t cComponents = 1;
if (chFirst < '2')
{
ch = *psz++;
if (*psz == '.')
{
if (RT_UNLIKELY(!RT_C_IS_DIGIT(ch)))
return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
}
else
{
if (RT_UNLIKELY(ch < '0' || ch > '3'))
return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
ch = *psz++;
if (RT_UNLIKELY(!RT_C_IS_DIGIT(ch)))
return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
if (*psz != '.')
return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
}
cComponents++;
}
else
psz--;
/* Subsequent components have max values of UINT32_MAX - 80. */
while ((ch = *psz++) != '\0')
{
if (RT_UNLIKELY(ch != '.'))
return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
const char *pszStart = psz;
/* Special treatment of the first digit. Need to make sure it isn't an
unnecessary leading 0. */
ch = *psz++;
if (RT_UNLIKELY(!RT_C_IS_DIGIT(ch)))
return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
if (RT_UNLIKELY(ch == '0' && RT_C_IS_DIGIT(*psz)))
return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
/* The rest of the digits. */
while ((ch = *psz) != '.' && ch != '\0')
{
if (RT_UNLIKELY(!RT_C_IS_DIGIT(ch)))
return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
psz++;
}
/* Check the value range. */
if (RT_UNLIKELY(psz - pszStart >= 9))
if ( psz - pszStart > 9
|| strncmp(pszStart, "4294967216", 9) >= 0) /* 2^32 - 80 */
return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
cComponents++;
}
if (RT_UNLIKELY(cComponents >= 128))
return VERR_ASN1_OBJID_TOO_MANY_COMPONENTS;
pThis->cComponents = (uint8_t)cComponents;
/*
* Find space for the component array, either at the unused end of szObjId
* or on the heap.
*/
int rc;
RTAsn1MemInitAllocation(&pThis->Allocation, pAllocator);
#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? */
size_t cbLeft = sizeof(pThis->szObjId) - cbObjId;
if (cbLeft >= cComponents * sizeof(uint32_t))
{
pThis->pauComponents = (uint32_t *)&pThis->szObjId[sizeof(pThis->szObjId) - cComponents * sizeof(uint32_t)];
cbLeft -= cComponents * sizeof(uint32_t);
rc = VINF_SUCCESS;
}
else
#endif
rc = RTAsn1MemAllocZ(&pThis->Allocation, (void **)&pThis->pauComponents, cComponents * sizeof(uint32_t));
if (RT_SUCCESS(rc))
{
/*
* Fill the elements array.
*/
uint32_t *pauComponents = (uint32_t *)pThis->pauComponents;
rc = VINF_SUCCESS;
psz = pszObjId;
for (uint32_t i = 0; i < cComponents; i++)
{
uint32_t uValue = 0;
rc = RTStrToUInt32Ex(psz, (char **)&psz, 10, &uValue);
if (rc == VWRN_TRAILING_CHARS)
{
pauComponents[i] = uValue;
AssertBreakStmt(*psz == '.', rc = VERR_TRAILING_CHARS);
psz++;
}
else if (rc == VINF_SUCCESS)
{
pauComponents[i] = uValue;
Assert(*psz == '\0');
}
else if (RT_FAILURE(rc))
break;
else
{
rc = -rc;
break;
}
}
if (rc == VINF_SUCCESS && *psz == '\0')
{
/*
* Initialize the core structure before we start on the encoded bytes.
*/
RTAsn1Core_InitEx(&pThis->Asn1Core,
ASN1_TAG_OID,
ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
&g_RTAsn1ObjId_Vtable,
RTASN1CORE_F_PRESENT | RTASN1CORE_F_PRIMITE_TAG_STRUCT);
/*
* Encode the value into the string buffer. This will NOT overflow
* because the string representation is much less efficient than the
* binary ASN.1 representation (base-10 + separators vs. base-128).
*/
pThis->Asn1Core.cb = (uint32_t)cbObjId;
rc = rtAsn1ObjId_EncodeComponents(cComponents, pThis->pauComponents,
(uint8_t *)&pThis->szObjId[0], &pThis->Asn1Core.cb);
if (RT_SUCCESS(rc))
{
/*
* Now, find a place for the encoded bytes. There might be
* enough room left in the szObjId for it if we're lucky.
*/
#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 (pThis->Asn1Core.cb >= cbLeft)
pThis->Asn1Core.uData.pv = memmove(&pThis->szObjId[cbObjId], &pThis->szObjId[0], pThis->Asn1Core.cb);
else
#endif
rc = RTAsn1ContentDup(&pThis->Asn1Core, pThis->szObjId, pThis->Asn1Core.cb, pAllocator);
if (RT_SUCCESS(rc))
{
/*
* Finally, copy the dotted string.
*/
memcpy(pThis->szObjId, pszObjId, cbObjId);
return VINF_SUCCESS;
}
}
else
{
AssertMsgFailed(("%Rrc\n", rc));
rc = VERR_ASN1_INTERNAL_ERROR_3;
}
}
else
rc = VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
}
RT_ZERO(*pThis);
return rc;
}
RTDECL(int) RTAsn1ObjId_CompareWithString(PCRTASN1OBJID pThis, const char *pszRight)
{
return strcmp(pThis->szObjId, pszRight);
}
RTDECL(bool) RTAsn1ObjId_StartsWith(PCRTASN1OBJID pThis, const char *pszStartsWith)
{
size_t cchStartsWith = strlen(pszStartsWith);
return !strncmp(pThis->szObjId, pszStartsWith, cchStartsWith)
&& ( pszStartsWith[cchStartsWith] == '.'
|| pszStartsWith[cchStartsWith] == '\0');
}
RTDECL(uint8_t) RTAsn1ObjIdCountComponents(PCRTASN1OBJID pThis)
{
return pThis->cComponents;
}
RTDECL(uint32_t) RTAsn1ObjIdGetComponentsAsUInt32(PCRTASN1OBJID pThis, uint8_t iComponent)
{
if (iComponent < pThis->cComponents)
return pThis->pauComponents[iComponent];
return UINT32_MAX;
}
RTDECL(uint32_t) RTAsn1ObjIdGetLastComponentsAsUInt32(PCRTASN1OBJID pThis)
{
return pThis->pauComponents[pThis->cComponents - 1];
}
/*
* ASN.1 OBJECT IDENTIFIER - Standard Methods.
*/
RT_DECL_DATA_CONST(RTASN1COREVTABLE const) g_RTAsn1ObjId_Vtable =
{
"RTAsn1ObjId",
sizeof(RTASN1OBJID),
ASN1_TAG_OID,
ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
0,
(PFNRTASN1COREVTDTOR)RTAsn1ObjId_Delete,
NULL,
(PFNRTASN1COREVTCLONE)RTAsn1ObjId_Clone,
(PFNRTASN1COREVTCOMPARE)RTAsn1ObjId_Compare,
(PFNRTASN1COREVTCHECKSANITY)RTAsn1ObjId_CheckSanity,
NULL,
NULL
};
RTDECL(int) RTAsn1ObjId_Init(PRTASN1OBJID pThis, PCRTASN1ALLOCATORVTABLE pAllocator)
{
RT_NOREF_PV(pAllocator);
RTAsn1Core_InitEx(&pThis->Asn1Core,
ASN1_TAG_OID,
ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
&g_RTAsn1ObjId_Vtable,
RTASN1CORE_F_PRESENT | RTASN1CORE_F_PRIMITE_TAG_STRUCT);
pThis->Asn1Core.cb = sizeof(g_abDefault);
pThis->Asn1Core.uData.pv = (void *)&g_abDefault[0];
pThis->cComponents = RT_ELEMENTS(g_auDefault);
pThis->pauComponents = g_auDefault;
AssertCompile(sizeof(g_szDefault) <= sizeof(pThis->szObjId));
memcpy(pThis->szObjId, g_szDefault, sizeof(g_szDefault));
return VINF_SUCCESS;
}
RTDECL(int) RTAsn1ObjId_Clone(PRTASN1OBJID pThis, PCRTASN1OBJID pSrc, PCRTASN1ALLOCATORVTABLE pAllocator)
{
AssertPtr(pSrc); AssertPtr(pThis); AssertPtr(pAllocator);
RT_ZERO(*pThis);
if (RTAsn1ObjId_IsPresent(pSrc))
{
AssertReturn(pSrc->Asn1Core.pOps == &g_RTAsn1ObjId_Vtable, VERR_INTERNAL_ERROR_3);
/* Copy the dotted string representation. */
size_t cbObjId = strlen(pSrc->szObjId) + 1;
AssertReturn(cbObjId <= sizeof(pThis->szObjId), VERR_INTERNAL_ERROR_5);
memcpy(pThis->szObjId, pSrc->szObjId, cbObjId);
/* Copy the integer component array. Try fit it in the unused space of
the dotted object string buffer. We place it at the end of the
buffer as that is simple alignment wise and avoid wasting bytes that
could be used to sequueze in the content bytes (see below). */
int rc;
RTAsn1MemInitAllocation(&pThis->Allocation, pAllocator);
pThis->cComponents = pSrc->cComponents;
#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? */
size_t cbLeft = sizeof(pThis->szObjId);
if (pSrc->cComponents * sizeof(uint32_t) <= cbLeft)
{
pThis->pauComponents = (uint32_t *)&pThis->szObjId[sizeof(pThis->szObjId) - pSrc->cComponents * sizeof(uint32_t)];
memcpy((uint32_t *)pThis->pauComponents, pSrc->pauComponents, pSrc->cComponents * sizeof(uint32_t));
cbLeft -= pSrc->cComponents * sizeof(uint32_t);
rc = VINF_SUCCESS;
}
else
#endif
{
rc = RTAsn1MemDup(&pThis->Allocation, (void **)&pThis->pauComponents, pSrc->pauComponents,
pSrc->cComponents * sizeof(uint32_t));
}
if (RT_SUCCESS(rc))
{
/* See if we can fit the content value into the szObjId as well.
It will follow immediately after the string as the component
array is the end of the string buffer, when present. */
#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? */
uint32_t cbContent = pSrc->Asn1Core.cb;
if (cbContent <= cbLeft)
{
rc = RTAsn1Core_CloneNoContent(&pThis->Asn1Core, &pSrc->Asn1Core);
if (RT_SUCCESS(rc))
{
pThis->Asn1Core.uData.pv = memcpy(&pThis->szObjId[cbObjId], pSrc->Asn1Core.uData.pv, cbContent);
return VINF_SUCCESS;
}
}
else
#endif
{
rc = RTAsn1Core_CloneContent(&pThis->Asn1Core, &pSrc->Asn1Core, pAllocator);
if (RT_SUCCESS(rc))
return VINF_SUCCESS;
}
}
/* failed, clean up. */
if (pThis->Allocation.cbAllocated)
RTAsn1MemFree(&pThis->Allocation, (uint32_t *)pThis->pauComponents);
RT_ZERO(*pThis);
return rc;
}
return VINF_SUCCESS;
}
RTDECL(void) RTAsn1ObjId_Delete(PRTASN1OBJID pThis)
{
if ( pThis
&& RTAsn1ObjId_IsPresent(pThis))
{
Assert(pThis->Asn1Core.pOps == &g_RTAsn1ObjId_Vtable);
if (pThis->Allocation.cbAllocated)
RTAsn1MemFree(&pThis->Allocation, (uint32_t *)pThis->pauComponents);
RTAsn1ContentFree(&pThis->Asn1Core);
RT_ZERO(*pThis);
}
}
RTDECL(int) RTAsn1ObjId_Enum(PRTASN1OBJID pThis, PFNRTASN1ENUMCALLBACK pfnCallback, uint32_t uDepth, void *pvUser)
{
RT_NOREF_PV(pThis); RT_NOREF_PV(pfnCallback); RT_NOREF_PV(uDepth); RT_NOREF_PV(pvUser);
Assert(pThis && (!RTAsn1ObjId_IsPresent(pThis) || pThis->Asn1Core.pOps == &g_RTAsn1ObjId_Vtable));
/* No children to enumerate. */
return VINF_SUCCESS;
}
RTDECL(int) RTAsn1ObjId_Compare(PCRTASN1OBJID pLeft, PCRTASN1OBJID pRight)
{
if (RTAsn1ObjId_IsPresent(pLeft))
{
if (RTAsn1ObjId_IsPresent(pRight))
{
uint8_t cComponents = RT_MIN(pLeft->cComponents, pRight->cComponents);
for (uint32_t i = 0; i < cComponents; i++)
if (pLeft->pauComponents[i] != pRight->pauComponents[i])
return pLeft->pauComponents[i] < pRight->pauComponents[i] ? -1 : 1;
if (pLeft->cComponents == pRight->cComponents)
return 0;
return pLeft->cComponents < pRight->cComponents ? -1 : 1;
}
return 1;
}
return 0 - (int)RTAsn1ObjId_IsPresent(pRight);
}
RTDECL(int) RTAsn1ObjId_CheckSanity(PCRTASN1OBJID pThis, uint32_t fFlags, PRTERRINFO pErrInfo, const char *pszErrorTag)
{
RT_NOREF_PV(fFlags);
if (RT_UNLIKELY(!RTAsn1ObjId_IsPresent(pThis)))
return RTErrInfoSetF(pErrInfo, VERR_ASN1_NOT_PRESENT, "%s: Missing (OBJID).", pszErrorTag);
return VINF_SUCCESS;
}
/*
* 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-core.h>
#include <iprt/asn1-generator-init.h>
#include <iprt/asn1-generator-sanity.h>
|