VirtualBox

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

Last change on this file since 59731 was 59663, checked in by vboxsync, 9 years ago

IPRT: Added RTCrX509Certificate_VerifySignatureSelfSigned and RTAsn1EncodeToBuffer, corrected the name of RTAsn1EncodeWriteHeader (was RTAsnEncodeWriteHeader).

  • 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 59663 2016-02-14 20:11:06Z vboxsync $ */
2/** @file
3 * IPRT - ASN.1, Encoding.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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 if (RTASN1CORE_IS_PRESENT(pAsn1Core))
170 {
171 /*
172 * Depth first, where relevant.
173 */
174 uint32_t const cbSaved = pArgs->cb;
175 if (pAsn1Core->pOps)
176 {
177 /*
178 * Use the encoding preparation method when available.
179 */
180 int rc;
181 if (pAsn1Core->pOps->pfnEncodePrep)
182 rc = pAsn1Core->pOps->pfnEncodePrep(pAsn1Core, pArgs->fFlags, pArgs->pErrInfo);
183 else if (pAsn1Core->pOps->pfnEnum)
184 {
185 /*
186 * Recurse to prepare the child objects (if any).
187 */
188 rc = pAsn1Core->pOps->pfnEnum(pAsn1Core, rtAsn1EncodePrepareCallback, uDepth + 1, pArgs);
189 if (RT_SUCCESS(rc))
190 pAsn1Core->cb = pArgs->cb - cbSaved;
191 }
192 else
193 {
194 /*
195 * Must be a primitive type if DER.
196 */
197 if ( (pAsn1Core->fClass & ASN1_TAGFLAG_CONSTRUCTED)
198 && (pArgs->fFlags & RTASN1ENCODE_F_DER) )
199 return RTErrInfoSetF(pArgs->pErrInfo, VERR_ASN1_EXPECTED_PRIMITIVE,
200 "Expected primitive ASN.1 object: uTag=%#x fClass=%#x cb=%u",
201 RTASN1CORE_GET_TAG(pAsn1Core), pAsn1Core->fClass, pAsn1Core->cb);
202 rc = VINF_SUCCESS;
203 }
204 if (RT_SUCCESS(rc))
205 rc = RTAsn1EncodeRecalcHdrSize(pAsn1Core, pArgs->fFlags, pArgs->pErrInfo);
206 if (RT_FAILURE(rc))
207 return rc;
208 }
209 else
210 {
211 AssertFailed();
212 pAsn1Core->cb = 0;
213 pAsn1Core->cbHdr = 0;
214 }
215
216 /*
217 * Recalculate the output size, thus far. Dummy objects propagates the
218 * content size, but the header size is zero. Other objects with
219 * header size zero are not encoded and should be omitted entirely.
220 */
221 if (pAsn1Core->cbHdr > 0 || RTASN1CORE_IS_DUMMY(pAsn1Core))
222 pArgs->cb = RTASN1CORE_GET_RAW_ASN1_SIZE(pAsn1Core) + cbSaved;
223 else
224 pArgs->cb = cbSaved;
225 }
226
227 return VINF_SUCCESS;
228}
229
230
231RTDECL(int) RTAsn1EncodePrepare(PRTASN1CORE pRoot, uint32_t fFlags, uint32_t *pcbEncoded, PRTERRINFO pErrInfo)
232{
233 AssertReturn((fFlags & RTASN1ENCODE_F_RULE_MASK) == RTASN1ENCODE_F_DER, VERR_INVALID_FLAGS);
234
235 /*
236 * This is implemented as a recursive enumeration of the ASN.1 object structure.
237 */
238 RTASN1ENCODEPREPARGS Args;
239 Args.cb = 0;
240 Args.fFlags = fFlags;
241 Args.pErrInfo = pErrInfo;
242 int rc = rtAsn1EncodePrepareCallback(pRoot, "root", 0, &Args);
243 if (pcbEncoded)
244 *pcbEncoded = RTASN1CORE_GET_RAW_ASN1_SIZE(pRoot);
245 return rc;
246}
247
248
249RTDECL(int) RTAsn1EncodeWriteHeader(PCRTASN1CORE pAsn1Core, uint32_t fFlags, FNRTASN1ENCODEWRITER pfnWriter, void *pvUser,
250 PRTERRINFO pErrInfo)
251{
252 AssertReturn((fFlags & RTASN1ENCODE_F_RULE_MASK) == RTASN1ENCODE_F_DER, VERR_INVALID_FLAGS);
253
254 if ((pAsn1Core->fFlags & (RTASN1CORE_F_PRESENT | RTASN1CORE_F_DUMMY | RTASN1CORE_F_DEFAULT)) == RTASN1CORE_F_PRESENT)
255 {
256 uint8_t abHdr[16]; /* 2 + max 5 tag + max 4 length = 11 */
257 uint8_t *pbDst = &abHdr[0];
258
259 /*
260 * Encode the tag.
261 */
262 uint32_t uTag = pAsn1Core->uTag;
263 if (uTag < ASN1_TAG_USE_LONG_FORM)
264 *pbDst++ = (uint8_t)uTag | (pAsn1Core->fClass & ~ASN1_TAG_MASK);
265 else
266 {
267 AssertReturn(pAsn1Core->uTag != UINT32_MAX, RTErrInfoSet(pErrInfo, VERR_ASN1_DUMMY_OBJECT, "uTag=UINT32_MAX"));
268
269 /* In the long form, the tag is encoded MSB style with the 8th bit
270 of each byte indicating the whether there are more byte. */
271 *pbDst++ = ASN1_TAG_USE_LONG_FORM | (pAsn1Core->fClass & ~ASN1_TAG_MASK);
272 if (uTag <= UINT32_C(0x7f))
273 *pbDst++ = uTag;
274 else if (uTag <= UINT32_C(0x3fff)) /* 2**(7*2) = 0x4000 (16384) */
275 {
276 *pbDst++ = (uTag >> 7) | 0x80;
277 *pbDst++ = uTag & 0x7f;
278 }
279 else if (uTag <= UINT32_C(0x1fffff)) /* 2**(7*3) = 0x200000 (2097152) */
280 {
281 *pbDst++ = (uTag >> 14) | 0x80;
282 *pbDst++ = ((uTag >> 7) & 0x7f) | 0x80;
283 *pbDst++ = uTag & 0x7f;
284 }
285 else if (uTag <= UINT32_C(0xfffffff)) /* 2**(7*4) = 0x10000000 (268435456) */
286 {
287 *pbDst++ = (uTag >> 21) | 0x80;
288 *pbDst++ = ((uTag >> 14) & 0x7f) | 0x80;
289 *pbDst++ = ((uTag >> 7) & 0x7f) | 0x80;
290 *pbDst++ = uTag & 0x7f;
291 }
292 else
293 {
294 *pbDst++ = (uTag >> 28) | 0x80;
295 *pbDst++ = ((uTag >> 21) & 0x7f) | 0x80;
296 *pbDst++ = ((uTag >> 14) & 0x7f) | 0x80;
297 *pbDst++ = ((uTag >> 7) & 0x7f) | 0x80;
298 *pbDst++ = uTag & 0x7f;
299 }
300 }
301
302 /*
303 * Encode the length.
304 */
305 uint32_t cb = pAsn1Core->cb;
306 if (cb < 0x80)
307 *pbDst++ = (uint8_t)cb;
308 else
309 {
310 AssertReturn(cb < _1G, RTErrInfoSetF(pErrInfo, VERR_ASN1_TOO_LONG, "cb=%u (%#x)", cb, cb));
311
312 if (cb <= UINT32_C(0xffff))
313 {
314 if (cb <= UINT32_C(0xff))
315 {
316 pbDst[0] = 0x81;
317 pbDst[1] = (uint8_t)cb;
318 pbDst += 2;
319 }
320 else
321 {
322 pbDst[0] = 0x82;
323 pbDst[1] = cb >> 8;
324 pbDst[2] = (uint8_t)cb;
325 pbDst += 3;
326 }
327 }
328 else
329 {
330 if (cb <= UINT32_C(0xffffff))
331 {
332 pbDst[0] = 0x83;
333 pbDst[1] = (uint8_t)(cb >> 16);
334 pbDst[2] = (uint8_t)(cb >> 8);
335 pbDst[3] = (uint8_t)cb;
336 pbDst += 4;
337 }
338 else
339 {
340 pbDst[0] = 0x84;
341 pbDst[1] = (uint8_t)(cb >> 24);
342 pbDst[2] = (uint8_t)(cb >> 16);
343 pbDst[3] = (uint8_t)(cb >> 8);
344 pbDst[4] = (uint8_t)cb;
345 pbDst += 5;
346 }
347 }
348 }
349
350 size_t const cbHdr = pbDst - &abHdr[0];
351 Assert(sizeof(abHdr) >= cbHdr);
352 Assert(pAsn1Core->cbHdr == cbHdr);
353
354 /*
355 * Write it.
356 */
357 return pfnWriter(abHdr, cbHdr, pvUser, pErrInfo);
358 }
359
360 /*
361 * Not present, dummy or otherwise not encoded.
362 */
363 Assert(pAsn1Core->cbHdr == 0);
364 if (pAsn1Core->fFlags & RTASN1CORE_F_DEFAULT)
365 return VINF_ASN1_NOT_ENCODED;
366 Assert(RTASN1CORE_IS_DUMMY(pAsn1Core));
367 Assert(pAsn1Core->pOps && pAsn1Core->pOps->pfnEnum);
368 return VINF_SUCCESS;
369}
370
371
372/**
373 * @callback_method_impl{FNRTASN1ENUMCALLBACK}
374 */
375static DECLCALLBACK(int) rtAsn1EncodeWriteCallback(PRTASN1CORE pAsn1Core, const char *pszName, uint32_t uDepth, void *pvUser)
376{
377 RTASN1ENCODEWRITEARGS *pArgs = (RTASN1ENCODEWRITEARGS *)pvUser;
378 int rc;
379 if (RTASN1CORE_IS_PRESENT(pAsn1Core))
380 {
381 /*
382 * If there is an write method, use it.
383 */
384 if ( pAsn1Core->pOps
385 && pAsn1Core->pOps->pfnEncodeWrite)
386 rc = pAsn1Core->pOps->pfnEncodeWrite(pAsn1Core, pArgs->fFlags, pArgs->pfnWriter, pArgs->pvUser, pArgs->pErrInfo);
387 else
388 {
389 /*
390 * Generic path. Start by writing the header for this object.
391 */
392 rc = RTAsn1EncodeWriteHeader(pAsn1Core, pArgs->fFlags, pArgs->pfnWriter, pArgs->pvUser, pArgs->pErrInfo);
393 if (RT_SUCCESS(rc))
394 {
395 /*
396 * If there is an enum function, call it to assemble the content.
397 * Otherwise ASSUME the pointer in the header points to the content.
398 */
399 if ( pAsn1Core->pOps
400 && pAsn1Core->pOps->pfnEnum)
401 {
402 if (rc != VINF_ASN1_NOT_ENCODED)
403 rc = pAsn1Core->pOps->pfnEnum(pAsn1Core, rtAsn1EncodeWriteCallback, uDepth + 1, pArgs);
404 }
405 else if (pAsn1Core->cb && rc != VINF_ASN1_NOT_ENCODED)
406 {
407 Assert(!RTASN1CORE_IS_DUMMY(pAsn1Core));
408 AssertPtrReturn(pAsn1Core->uData.pv,
409 RTErrInfoSetF(pArgs->pErrInfo, VERR_ASN1_INVALID_DATA_POINTER,
410 "Invalid uData pointer %p for no pfnEnum object with %#x bytes of content",
411 pAsn1Core->uData.pv, pAsn1Core->cb));
412 rc = pArgs->pfnWriter(pAsn1Core->uData.pv, pAsn1Core->cb, pArgs->pvUser, pArgs->pErrInfo);
413 }
414 }
415 }
416 if (RT_SUCCESS(rc))
417 rc = VINF_SUCCESS;
418 }
419 else
420 rc = VINF_SUCCESS;
421 return rc;
422}
423
424
425RTDECL(int) RTAsn1EncodeWrite(PCRTASN1CORE pRoot, uint32_t fFlags, FNRTASN1ENCODEWRITER pfnWriter, void *pvUser,
426 PRTERRINFO pErrInfo)
427{
428 AssertReturn((fFlags & RTASN1ENCODE_F_RULE_MASK) == RTASN1ENCODE_F_DER, VERR_INVALID_FLAGS);
429
430 /*
431 * This is implemented as a recursive enumeration of the ASN.1 object structure.
432 */
433 RTASN1ENCODEWRITEARGS Args;
434 Args.fFlags = fFlags;
435 Args.pfnWriter = pfnWriter;
436 Args.pvUser = pvUser;
437 Args.pErrInfo = pErrInfo;
438 return rtAsn1EncodeWriteCallback((PRTASN1CORE)pRoot, "root", 0, &Args);
439}
440
441
442static DECLCALLBACK(int) rtAsn1EncodeToBufferCallback(const void *pvBuf, size_t cbToWrite, void *pvUser, PRTERRINFO pErrInfo)
443{
444 RTASN1ENCODETOBUFARGS *pArgs = (RTASN1ENCODETOBUFARGS *)pvUser;
445 if (RT_LIKELY(pArgs->cbDst >= cbToWrite))
446 {
447 memcpy(pArgs->pbDst, pvBuf, cbToWrite);
448 pArgs->cbDst -= cbToWrite;
449 pArgs->pbDst += cbToWrite;
450 return VINF_SUCCESS;
451 }
452
453 /*
454 * Overflow.
455 */
456 if (pArgs->cbDst)
457 {
458 memcpy(pArgs->pbDst, pvBuf, pArgs->cbDst);
459 pArgs->pbDst -= pArgs->cbDst;
460 pArgs->cbDst = 0;
461 }
462 return VERR_BUFFER_OVERFLOW;
463}
464
465
466RTDECL(int) RTAsn1EncodeToBuffer(PCRTASN1CORE pRoot, uint32_t fFlags, void *pvBuf, size_t cbBuf, PRTERRINFO pErrInfo)
467{
468 RTASN1ENCODETOBUFARGS Args;
469 Args.pbDst = (uint8_t *)pvBuf;
470 Args.cbDst = cbBuf;
471 return RTAsn1EncodeWrite(pRoot, fFlags, rtAsn1EncodeToBufferCallback, &Args, pErrInfo);
472}
473
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