VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/asn1/asn1-ut-integer.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.1 KB
Line 
1/* $Id: asn1-ut-integer.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - ASN.1, INTEGER Type.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "internal/iprt.h"
42#include <iprt/asn1.h>
43
44#include <iprt/bignum.h>
45#include <iprt/err.h>
46#include <iprt/string.h>
47
48#include <iprt/formats/asn1.h>
49
50
51/*********************************************************************************************************************************
52* Global Variables *
53*********************************************************************************************************************************/
54/** Fixed on-byte constants for small numbers.
55 * Good for structure version values and such. */
56static const uint8_t g_abSmall[] =
57{
58 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
59 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
60};
61
62
63
64/*
65 * ASN.1 INTEGER - Special Methods.
66 */
67
68
69/**
70 * Updates the native value we keep in RTASN1INTEGER::uValue.
71 *
72 * @param pThis The integer.
73 */
74static void rtAsn1Integer_UpdateNativeValue(PRTASN1INTEGER pThis)
75{
76 uint32_t offLast = pThis->Asn1Core.cb - 1;
77 switch (pThis->Asn1Core.cb)
78 {
79 default: AssertBreak(pThis->Asn1Core.cb > 8); /* paranoia */ RT_FALL_THRU();
80 case 8: pThis->uValue.u |= (uint64_t)pThis->Asn1Core.uData.pu8[offLast - 7] << 56; RT_FALL_THRU();
81 case 7: pThis->uValue.u |= (uint64_t)pThis->Asn1Core.uData.pu8[offLast - 6] << 48; RT_FALL_THRU();
82 case 6: pThis->uValue.u |= (uint64_t)pThis->Asn1Core.uData.pu8[offLast - 5] << 40; RT_FALL_THRU();
83 case 5: pThis->uValue.u |= (uint64_t)pThis->Asn1Core.uData.pu8[offLast - 4] << 32; RT_FALL_THRU();
84 case 4: pThis->uValue.u |= (uint32_t)pThis->Asn1Core.uData.pu8[offLast - 3] << 24; RT_FALL_THRU();
85 case 3: pThis->uValue.u |= (uint32_t)pThis->Asn1Core.uData.pu8[offLast - 2] << 16; RT_FALL_THRU();
86 case 2: pThis->uValue.u |= (uint16_t)pThis->Asn1Core.uData.pu8[offLast - 1] << 8; RT_FALL_THRU();
87 case 1: pThis->uValue.u |= pThis->Asn1Core.uData.pu8[offLast];
88 }
89}
90
91
92RTDECL(int) RTAsn1Integer_InitU64(PRTASN1INTEGER pThis, uint64_t uValue, PCRTASN1ALLOCATORVTABLE pAllocator)
93{
94 /*
95 * Initialize the core and the native value.
96 */
97 RTAsn1Core_InitEx(&pThis->Asn1Core,
98 ASN1_TAG_INTEGER,
99 ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
100 &g_RTAsn1Integer_Vtable,
101 RTASN1CORE_F_PRESENT | RTASN1CORE_F_PRIMITE_TAG_STRUCT);
102 pThis->uValue.u = uValue;
103
104 /*
105 * Use one of the constants if possible.
106 */
107 if (uValue < RT_ELEMENTS(g_abSmall))
108 {
109 pThis->Asn1Core.cb = 1;
110 pThis->Asn1Core.uData.pv = (void *)&g_abSmall[0];
111 }
112 else
113 {
114 /*
115 * Need to turn uValue into a big endian number without any
116 * unnecessary leading zero bytes.
117 */
118 /* Figure the size. */
119 uint32_t cb = 0;
120 if (uValue <= UINT32_MAX)
121 {
122 if (uValue <= UINT16_MAX)
123 {
124 if (uValue <= UINT8_MAX)
125 cb = 1;
126 else
127 cb = 2;
128 }
129 else
130 {
131 if (uValue <= UINT32_C(0xffffff))
132 cb = 3;
133 else
134 cb = 4;
135 }
136 }
137 else
138 {
139 if (uValue <= UINT64_C(0x0000FfffFfffFfff))
140 {
141 if (uValue <= UINT64_C(0x000000ffFfffFfff))
142 cb = 5;
143 else
144 cb = 6;
145 }
146 else
147 {
148 if (uValue <= UINT64_C(0x00ffFfffFfffFfff))
149 cb = 7;
150 else
151 cb = 8;
152 }
153 }
154
155 /* Allocate space. */
156 int rc = RTAsn1ContentAllocZ(&pThis->Asn1Core, cb, pAllocator);
157 if (RT_FAILURE(rc))
158 {
159 RT_ZERO(*pThis);
160 return rc;
161 }
162
163 /* Serialize the number in MSB order. */
164 uint8_t *pb = (uint8_t *)pThis->Asn1Core.uData.pu8;
165 while (cb-- > 0)
166 {
167 pb[cb] = (uint8_t)uValue;
168 uValue >>= 8;
169 }
170 Assert(uValue == 0);
171 }
172 return VINF_SUCCESS;
173}
174
175
176RTDECL(int) RTAsn1Integer_InitDefault(PRTASN1INTEGER pThis, uint64_t uValue, PCRTASN1ALLOCATORVTABLE pAllocator)
177{
178 int rc = RTAsn1Integer_InitU64(pThis, uValue, pAllocator);
179 if (RT_SUCCESS(rc))
180 {
181 pThis->Asn1Core.fFlags &= ~RTASN1CORE_F_PRESENT;
182 pThis->Asn1Core.fFlags |= RTASN1CORE_F_DEFAULT;
183 }
184 return rc;
185}
186
187
188RTDECL(int32_t) RTAsn1Integer_UnsignedLastBit(PCRTASN1INTEGER pThis)
189{
190 AssertReturn(pThis->Asn1Core.fFlags, -1);
191 uint8_t const *pb = pThis->Asn1Core.uData.pu8;
192 AssertReturn(pb, -1);
193 uint32_t cb = pThis->Asn1Core.cb;
194 AssertReturn(pThis->Asn1Core.cb < (uint32_t)INT32_MAX / 8, -1);
195
196 while (cb-- > 0)
197 {
198 uint8_t b = *pb++;
199 if (b)
200 {
201 int32_t iRet = cb * 8;
202 if (b & 0x80) iRet += 7;
203 else if (b & 0x40) iRet += 6;
204 else if (b & 0x20) iRet += 5;
205 else if (b & 0x10) iRet += 4;
206 else if (b & 0x08) iRet += 3;
207 else if (b & 0x04) iRet += 2;
208 else if (b & 0x02) iRet += 1;
209 else Assert(b == 0x01);
210 return iRet;
211 }
212 }
213 return -1;
214}
215
216
217RTDECL(int) RTAsn1Integer_UnsignedCompare(PCRTASN1INTEGER pLeft, PCRTASN1INTEGER pRight)
218{
219 Assert(pLeft && (!RTAsn1Integer_IsPresent(pLeft) || pLeft->Asn1Core.pOps == &g_RTAsn1Integer_Vtable));
220 Assert(pRight && (!RTAsn1Integer_IsPresent(pRight) || pRight->Asn1Core.pOps == &g_RTAsn1Integer_Vtable));
221
222 int iDiff;
223 if (RTAsn1Integer_IsPresent(pLeft))
224 {
225 if (RTAsn1Integer_IsPresent(pRight))
226 {
227 if ( pLeft->Asn1Core.cb > 8
228 || pRight->Asn1Core.cb > 8)
229 {
230 uint32_t iLeft = RTAsn1Integer_UnsignedLastBit(pLeft);
231 uint32_t iRight = RTAsn1Integer_UnsignedLastBit(pRight);
232 if (iLeft != iRight)
233 return iLeft < iRight ? -1 : 1;
234 if ((int32_t)iLeft < 0)
235 return 0; /* Both are all zeros. */
236
237 uint32_t i = iLeft / 8;
238 if (i > 8)
239 {
240 uint8_t const *pbLeft = &pLeft->Asn1Core.uData.pu8[pLeft->Asn1Core.cb - i - 1];
241 uint8_t const *pbRight = &pRight->Asn1Core.uData.pu8[pRight->Asn1Core.cb - i - 1];
242 for (;;)
243 {
244 if (*pbLeft != *pbRight)
245 return *pbLeft < *pbRight ? -1 : 1;
246 if (--i <= 8)
247 break;
248 pbLeft++;
249 pbRight++;
250 }
251 }
252 }
253
254 if (pLeft->uValue.u == pRight->uValue.u)
255 iDiff = 0;
256 else
257 iDiff = pLeft->uValue.u < pRight->uValue.u ? -1 : 1;
258 }
259 else
260 iDiff = -1;
261 }
262 else
263 iDiff = 0 - (int)RTAsn1Integer_IsPresent(pRight);
264 return iDiff;
265}
266
267
268RTDECL(int) RTAsn1Integer_UnsignedCompareWithU64(PCRTASN1INTEGER pThis, uint64_t u64Const)
269{
270 int iDiff;
271 if (RTAsn1Integer_IsPresent(pThis))
272 {
273 if (pThis->Asn1Core.cb > 8)
274 {
275 int32_t iLast = RTAsn1Integer_UnsignedLastBit(pThis);
276 if (iLast >= 64)
277 return 1;
278 }
279
280 if (pThis->uValue.u == u64Const)
281 iDiff = 0;
282 else
283 iDiff = pThis->uValue.u < u64Const ? -1 : 1;
284 }
285 else
286 iDiff = 1;
287 return iDiff;
288}
289
290
291RTDECL(int) RTAsn1Integer_UnsignedCompareWithU32(PCRTASN1INTEGER pThis, uint32_t u32Const)
292{
293 int iDiff;
294 if (RTAsn1Integer_IsPresent(pThis))
295 {
296 if (pThis->Asn1Core.cb > 8)
297 {
298 int32_t iLast = RTAsn1Integer_UnsignedLastBit(pThis);
299 if (iLast >= 32)
300 return 1;
301 }
302
303 if (pThis->uValue.u == u32Const)
304 iDiff = 0;
305 else
306 iDiff = pThis->uValue.u < u32Const ? -1 : 1;
307 }
308 else
309 iDiff = 1;
310 return iDiff;
311}
312
313
314RTDECL(int) RTAsn1Integer_ToBigNum(PCRTASN1INTEGER pThis, PRTBIGNUM pBigNum, uint32_t fBigNumInit)
315{
316 AssertReturn(!(fBigNumInit & ~( RTBIGNUMINIT_F_SENSITIVE | RTBIGNUMINIT_F_UNSIGNED | RTBIGNUMINIT_F_SIGNED
317 | RTBIGNUMINIT_F_ENDIAN_LITTLE | RTBIGNUMINIT_F_ENDIAN_BIG)),
318 VERR_INVALID_PARAMETER);
319 AssertReturn(RTAsn1Integer_IsPresent(pThis), VERR_INVALID_PARAMETER);
320
321 if (!(fBigNumInit & (RTBIGNUMINIT_F_UNSIGNED | RTBIGNUMINIT_F_SIGNED)))
322 fBigNumInit |= RTBIGNUMINIT_F_SIGNED;
323
324 if (!(fBigNumInit & (RTBIGNUMINIT_F_ENDIAN_BIG | RTBIGNUMINIT_F_ENDIAN_LITTLE)))
325 fBigNumInit |= RTBIGNUMINIT_F_ENDIAN_BIG;
326
327 return RTBigNumInit(pBigNum, fBigNumInit, pThis->Asn1Core.uData.pv, pThis->Asn1Core.cb);
328}
329
330
331RTDECL(int) RTAsn1Integer_FromBigNum(PRTASN1INTEGER pThis, PCRTBIGNUM pBigNum, PCRTASN1ALLOCATORVTABLE pAllocator)
332{
333 AssertPtr(pThis); AssertPtr(pBigNum); AssertPtr(pAllocator);
334
335 /* Be nice and auto init the object. */
336 if (!RTAsn1Integer_IsPresent(pThis))
337 RTAsn1Integer_Init(pThis, NULL);
338
339 uint32_t cb = RTBigNumByteWidth(pBigNum); Assert(cb > 0);
340 int rc = RTAsn1ContentReallocZ(&pThis->Asn1Core, cb, pAllocator);
341 if (RT_SUCCESS(rc))
342 {
343 Assert(cb == pThis->Asn1Core.cb);
344 rc = RTBigNumToBytesBigEndian(pBigNum, (void *)pThis->Asn1Core.uData.pv, cb);
345 if (RT_SUCCESS(rc))
346 rtAsn1Integer_UpdateNativeValue(pThis);
347 }
348 return rc;
349}
350
351
352RTDECL(int) RTAsn1Integer_ToString(PCRTASN1INTEGER pThis, char *pszBuf, size_t cbBuf, uint32_t fFlags, size_t *pcbActual)
353{
354 AssertReturn(RTAsn1Integer_IsPresent(pThis), VERR_INVALID_PARAMETER);
355 AssertReturn(fFlags == 0, VERR_INVALID_FLAGS);
356
357 /*
358 * We only do hex conversions via this API.
359 * Currently we consider all numbers to be unsigned.
360 */
361 /** @todo Signed ASN.1 INTEGER. */
362 int rc;
363 size_t cbActual;
364 if (pThis->Asn1Core.cb <= 8)
365 {
366 cbActual = 2 + pThis->Asn1Core.cb*2 + 1;
367 if (cbActual <= cbBuf)
368 {
369 ssize_t cchFormat = RTStrFormatU64(pszBuf, cbBuf, pThis->uValue.u, 16, (int)cbActual - 1 /*cchWidth*/, 0,
370 RTSTR_F_SPECIAL | RTSTR_F_ZEROPAD);
371 rc = VINF_SUCCESS;
372 AssertStmt(cchFormat == (ssize_t)cbActual - 1, rc = VERR_INTERNAL_ERROR_3);
373 }
374 else
375 rc = VERR_BUFFER_OVERFLOW;
376 }
377 else
378 {
379 cbActual = pThis->Asn1Core.cb * 3 - 1 /* save one separator */ + 1 /* terminator */;
380 if (cbActual <= cbBuf)
381 {
382 rc = RTStrPrintHexBytes(pszBuf, cbBuf, pThis->Asn1Core.uData.pv, pThis->Asn1Core.cb, RTSTRPRINTHEXBYTES_F_SEP_SPACE);
383 Assert(rc == VINF_SUCCESS);
384 }
385 else
386 rc = VERR_BUFFER_OVERFLOW;
387 }
388 if (pcbActual)
389 *pcbActual = cbActual;
390 return rc;
391}
392
393
394/*
395 * ASN.1 INTEGER - Standard Methods.
396 */
397
398RT_DECL_DATA_CONST(RTASN1COREVTABLE const) g_RTAsn1Integer_Vtable =
399{
400 "RTAsn1Integer",
401 sizeof(RTASN1INTEGER),
402 ASN1_TAG_INTEGER,
403 ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
404 0,
405 (PFNRTASN1COREVTDTOR)RTAsn1Integer_Delete,
406 NULL,
407 (PFNRTASN1COREVTCLONE)RTAsn1Integer_Clone,
408 (PFNRTASN1COREVTCOMPARE)RTAsn1Integer_Compare,
409 (PFNRTASN1COREVTCHECKSANITY)RTAsn1Integer_CheckSanity,
410 NULL,
411 NULL
412};
413
414
415RTDECL(int) RTAsn1Integer_Init(PRTASN1INTEGER pThis, PCRTASN1ALLOCATORVTABLE pAllocator)
416{
417 RT_NOREF_PV(pAllocator);
418 RTAsn1Core_InitEx(&pThis->Asn1Core,
419 ASN1_TAG_INTEGER,
420 ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
421 &g_RTAsn1Integer_Vtable,
422 RTASN1CORE_F_PRESENT | RTASN1CORE_F_PRIMITE_TAG_STRUCT);
423 pThis->uValue.u = 1;
424 pThis->Asn1Core.cb = 1;
425 pThis->Asn1Core.uData.pv = (void *)&g_abSmall[0];
426 return VINF_SUCCESS;
427}
428
429
430RTDECL(int) RTAsn1Integer_Clone(PRTASN1INTEGER pThis, PCRTASN1INTEGER pSrc, PCRTASN1ALLOCATORVTABLE pAllocator)
431{
432 AssertPtr(pSrc); AssertPtr(pThis); AssertPtr(pAllocator);
433 RT_ZERO(*pThis);
434 if (RTAsn1Integer_IsPresent(pSrc))
435 {
436 AssertReturn(pSrc->Asn1Core.pOps == &g_RTAsn1Integer_Vtable, VERR_INTERNAL_ERROR_3);
437
438 int rc;
439 if ( pSrc->Asn1Core.cb != 1
440 || pSrc->uValue.u >= RT_ELEMENTS(g_abSmall))
441 {
442 /* Value is too large, copy it. */
443 rc = RTAsn1Core_CloneContent(&pThis->Asn1Core, &pSrc->Asn1Core, pAllocator);
444 if (RT_FAILURE(rc))
445 return rc;
446 }
447 else
448 {
449 /* Use one of the const values. */
450 rc = RTAsn1Core_CloneNoContent(&pThis->Asn1Core, &pSrc->Asn1Core);
451 if (RT_FAILURE(rc))
452 return rc;
453 Assert(g_abSmall[pSrc->uValue.u] == pSrc->uValue.u);
454 pThis->Asn1Core.uData.pv = (void *)&g_abSmall[pSrc->uValue.u];
455 }
456 pThis->uValue.u = pSrc->uValue.u;
457 }
458 return VINF_SUCCESS;
459}
460
461
462RTDECL(void) RTAsn1Integer_Delete(PRTASN1INTEGER pThis)
463{
464 if ( pThis
465 && RTAsn1Integer_IsPresent(pThis))
466 {
467 Assert(pThis->Asn1Core.pOps == &g_RTAsn1Integer_Vtable);
468
469 RTAsn1ContentFree(&pThis->Asn1Core);
470 RT_ZERO(*pThis);
471 }
472}
473
474
475RTDECL(int) RTAsn1Integer_Enum(PRTASN1INTEGER pThis, PFNRTASN1ENUMCALLBACK pfnCallback, uint32_t uDepth, void *pvUser)
476{
477 RT_NOREF_PV(pThis); RT_NOREF_PV(pfnCallback); RT_NOREF_PV(uDepth); RT_NOREF_PV(pvUser);
478 Assert(pThis && (!RTAsn1Integer_IsPresent(pThis) || pThis->Asn1Core.pOps == &g_RTAsn1Integer_Vtable));
479
480 /* No children to enumerate. */
481 return VINF_SUCCESS;
482}
483
484
485RTDECL(int) RTAsn1Integer_Compare(PCRTASN1INTEGER pLeft, PCRTASN1INTEGER pRight)
486{
487 return RTAsn1Integer_UnsignedCompare(pLeft, pRight);
488}
489
490
491RTDECL(int) RTAsn1Integer_CheckSanity(PCRTASN1INTEGER pThis, uint32_t fFlags, PRTERRINFO pErrInfo, const char *pszErrorTag)
492{
493 RT_NOREF_PV(fFlags);
494 if (RT_UNLIKELY(!RTAsn1Integer_IsPresent(pThis)))
495 return RTErrInfoSetF(pErrInfo, VERR_ASN1_NOT_PRESENT, "%s: Missing (INTEGER).", pszErrorTag);
496 return VINF_SUCCESS;
497}
498
499
500/*
501 * Generate code for the associated collection types.
502 */
503#define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-integer-template.h"
504#include <iprt/asn1-generator-internal-header.h>
505#include <iprt/asn1-generator-core.h>
506#include <iprt/asn1-generator-init.h>
507#include <iprt/asn1-generator-sanity.h>
508
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use