VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/asn1/asn1-encode.cpp@ 78365

Last change on this file since 78365 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.0 KB
Line 
1/* $Id: asn1-encode.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - ASN.1, Encoding.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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/assert.h>
35#include <iprt/bignum.h>
36#include <iprt/ctype.h>
37#include <iprt/err.h>
38#include <iprt/string.h>
39
40#include <iprt/formats/asn1.h>
41
42
43/*********************************************************************************************************************************
44* Structures and Typedefs *
45*********************************************************************************************************************************/
46/**
47 * Argument package for rtAsn1EncodePrepareCallback passed by RTAsn1EncodePrepare.
48 */
49typedef struct RTASN1ENCODEPREPARGS
50{
51 /** The size at this level. */
52 uint32_t cb;
53 /** RTASN1ENCODE_F_XXX. */
54 uint32_t fFlags;
55 /** Pointer to the error info. (optional) */
56 PRTERRINFO pErrInfo;
57} RTASN1ENCODEPREPARGS;
58
59
60/**
61 * Argument package for rtAsn1EncodeWriteCallback passed by RTAsn1EncodeWrite.
62 */
63typedef struct RTASN1ENCODEWRITEARGS
64{
65 /** RTASN1ENCODE_F_XXX. */
66 uint32_t fFlags;
67 /** Pointer to the writer funtion. */
68 PFNRTASN1ENCODEWRITER pfnWriter;
69 /** User argument to the writer function. */
70 void *pvUser;
71 /** Pointer to the error info. (optional) */
72 PRTERRINFO pErrInfo;
73} RTASN1ENCODEWRITEARGS;
74
75/**
76 * Argument package for rtAsn1EncodeToBufferCallback passed by
77 * RTAsn1EncodeToBuffer.
78 */
79typedef struct RTASN1ENCODETOBUFARGS
80{
81 /** The destination buffer position (incremented while writing). */
82 uint8_t *pbDst;
83 /** The size of the destination buffer left (decremented while writing). */
84 size_t cbDst;
85} RTASN1ENCODETOBUFARGS;
86
87
88RTDECL(int) RTAsn1EncodeRecalcHdrSize(PRTASN1CORE pAsn1Core, uint32_t fFlags, PRTERRINFO pErrInfo)
89{
90 AssertReturn((fFlags & RTASN1ENCODE_F_RULE_MASK) == RTASN1ENCODE_F_DER, VERR_INVALID_FLAGS);
91 int rc = VINF_SUCCESS;
92
93 uint8_t cbHdr;
94 if ((pAsn1Core->fFlags & (RTASN1CORE_F_PRESENT | RTASN1CORE_F_DUMMY | RTASN1CORE_F_DEFAULT)) == RTASN1CORE_F_PRESENT)
95 {
96 /*
97 * The minimum header size is two bytes.
98 */
99 cbHdr = 2;
100
101 /*
102 * Add additional bytes for encoding the tag.
103 */
104 uint32_t uTag = pAsn1Core->uTag;
105 if (uTag >= ASN1_TAG_USE_LONG_FORM)
106 {
107 AssertReturn(pAsn1Core->uTag != UINT32_MAX, RTErrInfoSet(pErrInfo, VERR_ASN1_DUMMY_OBJECT, "uTag=UINT32_MAX"));
108 do
109 {
110 cbHdr++;
111 uTag >>= 7;
112 } while (uTag > 0);
113 }
114
115 /*
116 * Add additional bytes for encoding the content length.
117 */
118 uint32_t cb = pAsn1Core->cb;
119 if (cb >= 0x80)
120 {
121 AssertReturn(cb < _1G, RTErrInfoSetF(pErrInfo, VERR_ASN1_TOO_LONG, "cb=%u (%#x)", cb, cb));
122
123 if (cb <= UINT32_C(0xffff))
124 {
125 if (cb <= UINT32_C(0xff))
126 cbHdr += 1;
127 else
128 cbHdr += 2;
129 }
130 else
131 {
132 if (cb <= UINT32_C(0xffffff))
133 cbHdr += 3;
134 else
135 cbHdr += 4;
136 }
137 }
138 }
139 /*
140 * Not present, dummy or otherwise not encoded.
141 */
142 else
143 {
144 cbHdr = 0;
145 if (pAsn1Core->fFlags & RTASN1CORE_F_DEFAULT)
146 rc = VINF_ASN1_NOT_ENCODED;
147 else
148 {
149 Assert(RTASN1CORE_IS_DUMMY(pAsn1Core));
150 Assert(pAsn1Core->pOps && pAsn1Core->pOps->pfnEnum);
151 rc = VINF_SUCCESS;
152 }
153 }
154
155 /*
156 * Update the header length.
157 */
158 pAsn1Core->cbHdr = cbHdr;
159 return rc;
160}
161
162
163/**
164 * @callback_method_impl{FNRTASN1ENUMCALLBACK}
165 */
166static DECLCALLBACK(int) rtAsn1EncodePrepareCallback(PRTASN1CORE pAsn1Core, const char *pszName, uint32_t uDepth, void *pvUser)
167{
168 RTASN1ENCODEPREPARGS *pArgs = (RTASN1ENCODEPREPARGS *)pvUser;
169 RT_NOREF_PV(pszName);
170 if (RTASN1CORE_IS_PRESENT(pAsn1Core))
171 {
172 /*
173 * Depth first, where relevant.
174 */
175 uint32_t const cbSaved = pArgs->cb;
176 if (pAsn1Core->pOps)
177 {
178 /*
179 * Use the encoding preparation method when available.
180 */
181 int rc;
182 if (pAsn1Core->pOps->pfnEncodePrep)
183 rc = pAsn1Core->pOps->pfnEncodePrep(pAsn1Core, pArgs->fFlags, pArgs->pErrInfo);
184 else if (pAsn1Core->pOps->pfnEnum)
185 {
186 /*
187 * Recurse to prepare the child objects (if any).
188 */
189 rc = pAsn1Core->pOps->pfnEnum(pAsn1Core, rtAsn1EncodePrepareCallback, uDepth + 1, pArgs);
190 if (RT_SUCCESS(rc))
191 pAsn1Core->cb = pArgs->cb - cbSaved;
192 }
193 else
194 {
195 /*
196 * Must be a primitive type if DER.
197 */
198 if ( (pAsn1Core->fClass & ASN1_TAGFLAG_CONSTRUCTED)
199 && (pArgs->fFlags & RTASN1ENCODE_F_DER) )
200 return RTErrInfoSetF(pArgs->pErrInfo, VERR_ASN1_EXPECTED_PRIMITIVE,
201 "Expected primitive ASN.1 object: uTag=%#x fClass=%#x cb=%u",
202 RTASN1CORE_GET_TAG(pAsn1Core), pAsn1Core->fClass, pAsn1Core->cb);
203 rc = VINF_SUCCESS;
204 }
205 if (RT_SUCCESS(rc))
206 rc = RTAsn1EncodeRecalcHdrSize(pAsn1Core, pArgs->fFlags, pArgs->pErrInfo);
207 if (RT_FAILURE(rc))
208 return rc;
209 }
210 else
211 {
212 AssertFailed();
213 pAsn1Core->cb = 0;
214 pAsn1Core->cbHdr = 0;
215 }
216
217 /*
218 * Recalculate the output size, thus far. Dummy objects propagates the
219 * content size, but the header size is zero. Other objects with
220 * header size zero are not encoded and should be omitted entirely.
221 */
222 if (pAsn1Core->cbHdr > 0 || RTASN1CORE_IS_DUMMY(pAsn1Core))
223 pArgs->cb = RTASN1CORE_GET_RAW_ASN1_SIZE(pAsn1Core) + cbSaved;
224 else
225 pArgs->cb = cbSaved;
226 }
227
228 return VINF_SUCCESS;
229}
230
231
232RTDECL(int) RTAsn1EncodePrepare(PRTASN1CORE pRoot, uint32_t fFlags, uint32_t *pcbEncoded, PRTERRINFO pErrInfo)
233{
234 AssertReturn((fFlags & RTASN1ENCODE_F_RULE_MASK) == RTASN1ENCODE_F_DER, VERR_INVALID_FLAGS);
235
236 /*
237 * This is implemented as a recursive enumeration of the ASN.1 object structure.
238 */
239 RTASN1ENCODEPREPARGS Args;
240 Args.cb = 0;
241 Args.fFlags = fFlags;
242 Args.pErrInfo = pErrInfo;
243 int rc = rtAsn1EncodePrepareCallback(pRoot, "root", 0, &Args);
244 if (pcbEncoded)
245 *pcbEncoded = RTASN1CORE_GET_RAW_ASN1_SIZE(pRoot);
246 return rc;
247}
248
249
250RTDECL(int) RTAsn1EncodeWriteHeader(PCRTASN1CORE pAsn1Core, uint32_t fFlags, FNRTASN1ENCODEWRITER pfnWriter, void *pvUser,
251 PRTERRINFO pErrInfo)
252{
253 AssertReturn((fFlags & RTASN1ENCODE_F_RULE_MASK) == RTASN1ENCODE_F_DER, VERR_INVALID_FLAGS);
254
255 if ((pAsn1Core->fFlags & (RTASN1CORE_F_PRESENT | RTASN1CORE_F_DUMMY | RTASN1CORE_F_DEFAULT)) == RTASN1CORE_F_PRESENT)
256 {
257 uint8_t abHdr[16]; /* 2 + max 5 tag + max 4 length = 11 */
258 uint8_t *pbDst = &abHdr[0];
259
260 /*
261 * Encode the tag.
262 */
263 uint32_t uTag = pAsn1Core->uTag;
264 if (uTag < ASN1_TAG_USE_LONG_FORM)
265 *pbDst++ = (uint8_t)uTag | (pAsn1Core->fClass & ~ASN1_TAG_MASK);
266 else
267 {
268 AssertReturn(pAsn1Core->uTag != UINT32_MAX, RTErrInfoSet(pErrInfo, VERR_ASN1_DUMMY_OBJECT, "uTag=UINT32_MAX"));
269
270 /* In the long form, the tag is encoded MSB style with the 8th bit
271 of each byte indicating the whether there are more byte. */
272 *pbDst++ = ASN1_TAG_USE_LONG_FORM | (pAsn1Core->fClass & ~ASN1_TAG_MASK);
273 if (uTag <= UINT32_C(0x7f))
274 *pbDst++ = uTag;
275 else if (uTag <= UINT32_C(0x3fff)) /* 2**(7*2) = 0x4000 (16384) */
276 {
277 *pbDst++ = (uTag >> 7) | 0x80;
278 *pbDst++ = uTag & 0x7f;
279 }
280 else if (uTag <= UINT32_C(0x1fffff)) /* 2**(7*3) = 0x200000 (2097152) */
281 {
282 *pbDst++ = (uTag >> 14) | 0x80;
283 *pbDst++ = ((uTag >> 7) & 0x7f) | 0x80;
284 *pbDst++ = uTag & 0x7f;
285 }
286 else if (uTag <= UINT32_C(0xfffffff)) /* 2**(7*4) = 0x10000000 (268435456) */
287 {
288 *pbDst++ = (uTag >> 21) | 0x80;
289 *pbDst++ = ((uTag >> 14) & 0x7f) | 0x80;
290 *pbDst++ = ((uTag >> 7) & 0x7f) | 0x80;
291 *pbDst++ = uTag & 0x7f;
292 }
293 else
294 {
295 *pbDst++ = (uTag >> 28) | 0x80;
296 *pbDst++ = ((uTag >> 21) & 0x7f) | 0x80;
297 *pbDst++ = ((uTag >> 14) & 0x7f) | 0x80;
298 *pbDst++ = ((uTag >> 7) & 0x7f) | 0x80;
299 *pbDst++ = uTag & 0x7f;
300 }
301 }
302
303 /*
304 * Encode the length.
305 */
306 uint32_t cb = pAsn1Core->cb;
307 if (cb < 0x80)
308 *pbDst++ = (uint8_t)cb;
309 else
310 {
311 AssertReturn(cb < _1G, RTErrInfoSetF(pErrInfo, VERR_ASN1_TOO_LONG, "cb=%u (%#x)", cb, cb));
312
313 if (cb <= UINT32_C(0xffff))
314 {
315 if (cb <= UINT32_C(0xff))
316 {
317 pbDst[0] = 0x81;
318 pbDst[1] = (uint8_t)cb;
319 pbDst += 2;
320 }
321 else
322 {
323 pbDst[0] = 0x82;
324 pbDst[1] = cb >> 8;
325 pbDst[2] = (uint8_t)cb;
326 pbDst += 3;
327 }
328 }
329 else
330 {
331 if (cb <= UINT32_C(0xffffff))
332 {
333 pbDst[0] = 0x83;
334 pbDst[1] = (uint8_t)(cb >> 16);
335 pbDst[2] = (uint8_t)(cb >> 8);
336 pbDst[3] = (uint8_t)cb;
337 pbDst += 4;
338 }
339 else
340 {
341 pbDst[0] = 0x84;
342 pbDst[1] = (uint8_t)(cb >> 24);
343 pbDst[2] = (uint8_t)(cb >> 16);
344 pbDst[3] = (uint8_t)(cb >> 8);
345 pbDst[4] = (uint8_t)cb;
346 pbDst += 5;
347 }
348 }
349 }
350
351 size_t const cbHdr = pbDst - &abHdr[0];
352 Assert(sizeof(abHdr) >= cbHdr);
353 Assert(pAsn1Core->cbHdr == cbHdr);
354
355 /*
356 * Write it.
357 */
358 return pfnWriter(abHdr, cbHdr, pvUser, pErrInfo);
359 }
360
361 /*
362 * Not present, dummy or otherwise not encoded.
363 */
364 Assert(pAsn1Core->cbHdr == 0);
365 if (pAsn1Core->fFlags & RTASN1CORE_F_DEFAULT)
366 return VINF_ASN1_NOT_ENCODED;
367 Assert(RTASN1CORE_IS_DUMMY(pAsn1Core));
368 Assert(pAsn1Core->pOps && pAsn1Core->pOps->pfnEnum);
369 return VINF_SUCCESS;
370}
371
372
373/**
374 * @callback_method_impl{FNRTASN1ENUMCALLBACK}
375 */
376static DECLCALLBACK(int) rtAsn1EncodeWriteCallback(PRTASN1CORE pAsn1Core, const char *pszName, uint32_t uDepth, void *pvUser)
377{
378 RTASN1ENCODEWRITEARGS *pArgs = (RTASN1ENCODEWRITEARGS *)pvUser;
379 RT_NOREF_PV(pszName);
380 int rc;
381 if (RTASN1CORE_IS_PRESENT(pAsn1Core))
382 {
383 /*
384 * If there is an write method, use it.
385 */
386 if ( pAsn1Core->pOps
387 && pAsn1Core->pOps->pfnEncodeWrite)
388 rc = pAsn1Core->pOps->pfnEncodeWrite(pAsn1Core, pArgs->fFlags, pArgs->pfnWriter, pArgs->pvUser, pArgs->pErrInfo);
389 else
390 {
391 /*
392 * Generic path. Start by writing the header for this object.
393 */
394 rc = RTAsn1EncodeWriteHeader(pAsn1Core, pArgs->fFlags, pArgs->pfnWriter, pArgs->pvUser, pArgs->pErrInfo);
395 if (RT_SUCCESS(rc))
396 {
397 /*
398 * If there is an enum function, call it to assemble the content.
399 * Otherwise ASSUME the pointer in the header points to the content.
400 */
401 if ( pAsn1Core->pOps
402 && pAsn1Core->pOps->pfnEnum)
403 {
404 if (rc != VINF_ASN1_NOT_ENCODED)
405 rc = pAsn1Core->pOps->pfnEnum(pAsn1Core, rtAsn1EncodeWriteCallback, uDepth + 1, pArgs);
406 }
407 else if (pAsn1Core->cb && rc != VINF_ASN1_NOT_ENCODED)
408 {
409 Assert(!RTASN1CORE_IS_DUMMY(pAsn1Core));
410 AssertPtrReturn(pAsn1Core->uData.pv,
411 RTErrInfoSetF(pArgs->pErrInfo, VERR_ASN1_INVALID_DATA_POINTER,
412 "Invalid uData pointer %p for no pfnEnum object with %#x bytes of content",
413 pAsn1Core->uData.pv, pAsn1Core->cb));
414 rc = pArgs->pfnWriter(pAsn1Core->uData.pv, pAsn1Core->cb, pArgs->pvUser, pArgs->pErrInfo);
415 }
416 }
417 }
418 if (RT_SUCCESS(rc))
419 rc = VINF_SUCCESS;
420 }
421 else
422 rc = VINF_SUCCESS;
423 return rc;
424}
425
426
427RTDECL(int) RTAsn1EncodeWrite(PCRTASN1CORE pRoot, uint32_t fFlags, FNRTASN1ENCODEWRITER pfnWriter, void *pvUser,
428 PRTERRINFO pErrInfo)
429{
430 AssertReturn((fFlags & RTASN1ENCODE_F_RULE_MASK) == RTASN1ENCODE_F_DER, VERR_INVALID_FLAGS);
431
432 /*
433 * This is implemented as a recursive enumeration of the ASN.1 object structure.
434 */
435 RTASN1ENCODEWRITEARGS Args;
436 Args.fFlags = fFlags;
437 Args.pfnWriter = pfnWriter;
438 Args.pvUser = pvUser;
439 Args.pErrInfo = pErrInfo;
440 return rtAsn1EncodeWriteCallback((PRTASN1CORE)pRoot, "root", 0, &Args);
441}
442
443
444static DECLCALLBACK(int) rtAsn1EncodeToBufferCallback(const void *pvBuf, size_t cbToWrite, void *pvUser, PRTERRINFO pErrInfo)
445{
446 RTASN1ENCODETOBUFARGS *pArgs = (RTASN1ENCODETOBUFARGS *)pvUser;
447 if (RT_LIKELY(pArgs->cbDst >= cbToWrite))
448 {
449 memcpy(pArgs->pbDst, pvBuf, cbToWrite);
450 pArgs->cbDst -= cbToWrite;
451 pArgs->pbDst += cbToWrite;
452 return VINF_SUCCESS;
453 }
454
455 /*
456 * Overflow.
457 */
458 if (pArgs->cbDst)
459 {
460 memcpy(pArgs->pbDst, pvBuf, pArgs->cbDst);
461 pArgs->pbDst -= pArgs->cbDst;
462 pArgs->cbDst = 0;
463 }
464 RT_NOREF_PV(pErrInfo);
465 return VERR_BUFFER_OVERFLOW;
466}
467
468
469RTDECL(int) RTAsn1EncodeToBuffer(PCRTASN1CORE pRoot, uint32_t fFlags, void *pvBuf, size_t cbBuf, PRTERRINFO pErrInfo)
470{
471 RTASN1ENCODETOBUFARGS Args;
472 Args.pbDst = (uint8_t *)pvBuf;
473 Args.cbDst = cbBuf;
474 return RTAsn1EncodeWrite(pRoot, fFlags, rtAsn1EncodeToBufferCallback, &Args, pErrInfo);
475}
476
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette