asn1-encode.cpp revision 13493ab7596e827b8d0caab2c89e635dd65f78f9
98N/A/* $Id$ */
98N/A/** @file
1089N/A * IPRT - ASN.1, Encoding.
98N/A */
98N/A
919N/A/*
919N/A * Copyright (C) 2006-2014 Oracle Corporation
919N/A *
919N/A * This file is part of VirtualBox Open Source Edition (OSE), as
919N/A * available from http://www.virtualbox.org. This file is free software;
919N/A * you can redistribute it and/or modify it under the terms of the GNU
919N/A * General Public License (GPL) as published by the Free Software
919N/A * Foundation, in version 2 as it comes in the "COPYING" file of the
919N/A * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
919N/A * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
919N/A *
919N/A * The contents of this file may alternatively be used under the terms
919N/A * of the Common Development and Distribution License Version 1.0
919N/A * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
919N/A * VirtualBox OSE distribution, in which case the provisions of the
919N/A * CDDL are applicable instead of those of the GPL.
919N/A *
98N/A * You may elect to license modified versions of this file under the
98N/A * terms and conditions of either the GPL or the CDDL or both.
98N/A */
493N/A
493N/A/*******************************************************************************
98N/A* Header Files *
970N/A*******************************************************************************/
970N/A#include "internal/iprt.h"
970N/A#include <iprt/asn1.h>
970N/A
970N/A#include <iprt/assert.h>
970N/A#include <iprt/bignum.h>
970N/A#include <iprt/ctype.h>
1003N/A#include <iprt/err.h>
970N/A
970N/A#include <iprt/formats/asn1.h>
970N/A
970N/A
970N/A/*******************************************************************************
98N/A* Structures and Typedefs *
1124N/A*******************************************************************************/
98N/A/**
911N/A * Argument package for rtAsn1EncodePrepareCallback passed by RTAsn1EncodePrepare.
1124N/A */
1124N/Atypedef struct RTASN1ENCODEPREPARGS
911N/A{
98N/A /** The size at this level. */
493N/A uint32_t cb;
493N/A /** RTASN1ENCODE_F_XXX. */
98N/A uint32_t fFlags;
98N/A /** Pointer to the error info. (optional) */
1089N/A PRTERRINFO pErrInfo;
156N/A} RTASN1ENCODEPREPARGS;
493N/A
493N/A
493N/A/**
493N/A * Argument package for rtAsn1EncodeWriteCallback passed by RTAsn1EncodeWrite.
493N/A */
493N/Atypedef struct RTASN1ENCODEWRITEARGS
493N/A{
98N/A /** RTASN1ENCODE_F_XXX. */
98N/A uint32_t fFlags;
98N/A /** Pointer to the writer funtion. */
1089N/A PFNRTASN1ENCODEWRITER pfnWriter;
705N/A /** User argument to the writer function. */
1089N/A void *pvUser;
606N/A /** Pointer to the error info. (optional) */
606N/A PRTERRINFO pErrInfo;
606N/A} RTASN1ENCODEWRITEARGS;
606N/A
705N/A
967N/ARTDECL(int) RTAsn1EncodeRecalcHdrSize(PRTASN1CORE pAsn1Core, uint32_t fFlags, PRTERRINFO pErrInfo)
606N/A{
606N/A AssertReturn((fFlags & RTASN1ENCODE_F_RULE_MASK) == RTASN1ENCODE_F_DER, VERR_INVALID_FLAGS);
606N/A int rc = VINF_SUCCESS;
606N/A
606N/A uint8_t cbHdr;
606N/A if ((pAsn1Core->fFlags & (RTASN1CORE_F_PRESENT | RTASN1CORE_F_DUMMY | RTASN1CORE_F_DEFAULT)) == RTASN1CORE_F_PRESENT)
1089N/A {
1097N/A /*
606N/A * The minimum header size is two bytes.
606N/A */
1109N/A cbHdr = 2;
1109N/A
1109N/A /*
606N/A * Add additional bytes for encoding the tag.
606N/A */
606N/A uint32_t uTag = pAsn1Core->uTag;
98N/A if (uTag >= ASN1_TAG_USE_LONG_FORM)
1126N/A {
1126N/A AssertReturn(pAsn1Core->uTag != UINT32_MAX, RTErrInfoSet(pErrInfo, VERR_ASN1_DUMMY_OBJECT, "uTag=UINT32_MAX"));
1126N/A do
970N/A {
493N/A cbHdr++;
493N/A uTag >>= 7;
493N/A } while (uTag > 0);
98N/A }
647N/A
1089N/A /*
1089N/A * Add additional bytes for encoding the content length.
98N/A */
647N/A uint32_t cb = pAsn1Core->cb;
647N/A if (cb >= 0x80)
1089N/A {
647N/A AssertReturn(cb < _1G, RTErrInfoSetF(pErrInfo, VERR_ASN1_TOO_LONG, "cb=%u (%#x)", cb, cb));
606N/A
1089N/A if (cb <= UINT32_C(0xffff))
98N/A {
606N/A if (cb <= UINT32_C(0xff))
1196N/A cbHdr += 1;
98N/A else
493N/A cbHdr += 2;
493N/A }
98N/A else
705N/A {
705N/A if (cb <= UINT32_C(0xffffff))
705N/A cbHdr += 3;
493N/A else
493N/A cbHdr += 4;
493N/A }
705N/A }
705N/A }
705N/A /*
970N/A * Not present, dummy or otherwise not encoded.
970N/A */
970N/A else
970N/A {
970N/A cbHdr = 0;
970N/A if (pAsn1Core->fFlags & RTASN1CORE_F_DEFAULT)
970N/A rc = VINF_ASN1_NOT_ENCODED;
970N/A else
970N/A {
970N/A Assert(RTASN1CORE_IS_DUMMY(pAsn1Core));
1124N/A Assert(pAsn1Core->pOps && pAsn1Core->pOps->pfnEnum);
970N/A rc = VINF_SUCCESS;
970N/A }
970N/A }
970N/A
970N/A /*
970N/A * Update the header length.
970N/A */
970N/A pAsn1Core->cbHdr = cbHdr;
970N/A return rc;
970N/A}
970N/A
970N/A
970N/A/**
970N/A * @callback_method_impl{FNRTASN1ENUMCALLBACK}
970N/A */
970N/Astatic DECLCALLBACK(int) rtAsn1EncodePrepareCallback(PRTASN1CORE pAsn1Core, const char *pszName, uint32_t uDepth, void *pvUser)
970N/A{
970N/A RTASN1ENCODEPREPARGS *pArgs = (RTASN1ENCODEPREPARGS *)pvUser;
970N/A if (RTASN1CORE_IS_PRESENT(pAsn1Core))
1152N/A {
970N/A /*
970N/A * Depth first, where relevant.
970N/A */
970N/A uint32_t const cbSaved = pArgs->cb;
970N/A if (pAsn1Core->pOps)
970N/A {
970N/A /*
970N/A * Use the encoding preparation method when available.
970N/A */
970N/A int rc;
970N/A if (pAsn1Core->pOps->pfnEncodePrep)
970N/A rc = pAsn1Core->pOps->pfnEncodePrep(pAsn1Core, pArgs->fFlags, pArgs->pErrInfo);
970N/A else if (pAsn1Core->pOps->pfnEnum)
970N/A {
970N/A /*
970N/A * Recurse to prepare the child objects (if any).
970N/A */
970N/A rc = pAsn1Core->pOps->pfnEnum(pAsn1Core, rtAsn1EncodePrepareCallback, uDepth + 1, pArgs);
970N/A if (RT_SUCCESS(rc))
970N/A pAsn1Core->cb = pArgs->cb - cbSaved;
970N/A }
970N/A else
970N/A {
970N/A /*
970N/A * Must be a primitive type if DER.
970N/A */
970N/A if ( (pAsn1Core->fClass & ASN1_TAGFLAG_CONSTRUCTED)
970N/A && (pArgs->fFlags & RTASN1ENCODE_F_DER) )
970N/A return RTErrInfoSetF(pArgs->pErrInfo, VERR_ASN1_EXPECTED_PRIMITIVE,
970N/A "Expected primitive ASN.1 object: uTag=%#x fClass=%#x cb=%u",
970N/A RTASN1CORE_GET_TAG(pAsn1Core), pAsn1Core->fClass, pAsn1Core->cb);
970N/A rc = VINF_SUCCESS;
970N/A }
970N/A if (RT_SUCCESS(rc))
1152N/A rc = RTAsn1EncodeRecalcHdrSize(pAsn1Core, pArgs->fFlags, pArgs->pErrInfo);
970N/A if (RT_FAILURE(rc))
1152N/A return rc;
970N/A }
970N/A else
970N/A {
970N/A AssertFailed();
970N/A pAsn1Core->cb = 0;
970N/A pAsn1Core->cbHdr = 0;
970N/A }
970N/A
970N/A /*
970N/A * Recalculate the output size, thus far. Dummy objects propagates the
970N/A * content size, but the header size is zero. Other objects with
970N/A * header size zero are not encoded and should be omitted entirely.
970N/A */
970N/A if (pAsn1Core->cbHdr > 0 || RTASN1CORE_IS_DUMMY(pAsn1Core))
970N/A pArgs->cb = RTASN1CORE_GET_RAW_ASN1_SIZE(pAsn1Core) + cbSaved;
970N/A else
970N/A pArgs->cb = cbSaved;
970N/A }
970N/A
970N/A return VINF_SUCCESS;
970N/A}
970N/A
RTDECL(int) RTAsn1EncodePrepare(PRTASN1CORE pRoot, uint32_t fFlags, uint32_t *pcbEncoded, PRTERRINFO pErrInfo)
{
AssertReturn((fFlags & RTASN1ENCODE_F_RULE_MASK) == RTASN1ENCODE_F_DER, VERR_INVALID_FLAGS);
/*
* This is implemented as a recursive enumeration of the ASN.1 object structure.
*/
RTASN1ENCODEPREPARGS Args;
Args.cb = 0;
Args.fFlags = fFlags;
Args.pErrInfo = pErrInfo;
int rc = rtAsn1EncodePrepareCallback(pRoot, "root", 0, &Args);
if (pcbEncoded)
*pcbEncoded = RTASN1CORE_GET_RAW_ASN1_SIZE(pRoot);
return rc;
}
RTDECL(int) RTAsnEncodeWriteHeader(PCRTASN1CORE pAsn1Core, uint32_t fFlags, FNRTASN1ENCODEWRITER pfnWriter, void *pvUser,
PRTERRINFO pErrInfo)
{
AssertReturn((fFlags & RTASN1ENCODE_F_RULE_MASK) == RTASN1ENCODE_F_DER, VERR_INVALID_FLAGS);
if ((pAsn1Core->fFlags & (RTASN1CORE_F_PRESENT | RTASN1CORE_F_DUMMY | RTASN1CORE_F_DEFAULT)) == RTASN1CORE_F_PRESENT)
{
uint8_t abHdr[16]; /* 2 + max 5 tag + max 4 length = 11 */
uint8_t *pbDst = &abHdr[0];
/*
* Encode the tag.
*/
uint32_t uTag = pAsn1Core->uTag;
if (uTag < ASN1_TAG_USE_LONG_FORM)
*pbDst++ = (uint8_t)uTag | (pAsn1Core->fClass & ~ASN1_TAG_MASK);
else
{
AssertReturn(pAsn1Core->uTag != UINT32_MAX, RTErrInfoSet(pErrInfo, VERR_ASN1_DUMMY_OBJECT, "uTag=UINT32_MAX"));
/* In the long form, the tag is encoded MSB style with the 8th bit
of each byte indicating the whether there are more byte. */
*pbDst++ = ASN1_TAG_USE_LONG_FORM | (pAsn1Core->fClass & ~ASN1_TAG_MASK);
if (uTag <= UINT32_C(0x7f))
*pbDst++ = uTag;
else if (uTag <= UINT32_C(0x3fff)) /* 2**(7*2) = 0x4000 (16384) */
{
*pbDst++ = (uTag >> 7) | 0x80;
*pbDst++ = uTag & 0x7f;
}
else if (uTag <= UINT32_C(0x1fffff)) /* 2**(7*3) = 0x200000 (2097152) */
{
*pbDst++ = (uTag >> 14) | 0x80;
*pbDst++ = ((uTag >> 7) & 0x7f) | 0x80;
*pbDst++ = uTag & 0x7f;
}
else if (uTag <= UINT32_C(0xfffffff)) /* 2**(7*4) = 0x10000000 (268435456) */
{
*pbDst++ = (uTag >> 21) | 0x80;
*pbDst++ = ((uTag >> 14) & 0x7f) | 0x80;
*pbDst++ = ((uTag >> 7) & 0x7f) | 0x80;
*pbDst++ = uTag & 0x7f;
}
else
{
*pbDst++ = (uTag >> 28) | 0x80;
*pbDst++ = ((uTag >> 21) & 0x7f) | 0x80;
*pbDst++ = ((uTag >> 14) & 0x7f) | 0x80;
*pbDst++ = ((uTag >> 7) & 0x7f) | 0x80;
*pbDst++ = uTag & 0x7f;
}
}
/*
* Encode the length.
*/
uint32_t cb = pAsn1Core->cb;
if (cb < 0x80)
*pbDst++ = (uint8_t)cb;
else
{
AssertReturn(cb < _1G, RTErrInfoSetF(pErrInfo, VERR_ASN1_TOO_LONG, "cb=%u (%#x)", cb, cb));
if (cb <= UINT32_C(0xffff))
{
if (cb <= UINT32_C(0xff))
{
pbDst[0] = 0x81;
pbDst[1] = (uint8_t)cb;
pbDst += 2;
}
else
{
pbDst[0] = 0x82;
pbDst[1] = cb >> 8;
pbDst[2] = (uint8_t)cb;
pbDst += 3;
}
}
else
{
if (cb <= UINT32_C(0xffffff))
{
pbDst[0] = 0x83;
pbDst[1] = (uint8_t)(cb >> 16);
pbDst[2] = (uint8_t)(cb >> 8);
pbDst[3] = (uint8_t)cb;
pbDst += 4;
}
else
{
pbDst[0] = 0x84;
pbDst[1] = (uint8_t)(cb >> 24);
pbDst[2] = (uint8_t)(cb >> 16);
pbDst[3] = (uint8_t)(cb >> 8);
pbDst[4] = (uint8_t)cb;
pbDst += 5;
}
}
}
size_t const cbHdr = pbDst - &abHdr[0];
Assert(sizeof(abHdr) >= cbHdr);
Assert(pAsn1Core->cbHdr == cbHdr);
/*
* Write it.
*/
return pfnWriter(abHdr, cbHdr, pvUser, pErrInfo);
}
/*
* Not present, dummy or otherwise not encoded.
*/
Assert(pAsn1Core->cbHdr == 0);
if (pAsn1Core->fFlags & RTASN1CORE_F_DEFAULT)
return VINF_ASN1_NOT_ENCODED;
Assert(RTASN1CORE_IS_DUMMY(pAsn1Core));
Assert(pAsn1Core->pOps && pAsn1Core->pOps->pfnEnum);
return VINF_SUCCESS;
}
/**
* @callback_method_impl{FNRTASN1ENUMCALLBACK}
*/
static DECLCALLBACK(int) rtAsn1EncodeWriteCallback(PRTASN1CORE pAsn1Core, const char *pszName, uint32_t uDepth, void *pvUser)
{
RTASN1ENCODEWRITEARGS *pArgs = (RTASN1ENCODEWRITEARGS *)pvUser;
int rc;
if (RTASN1CORE_IS_PRESENT(pAsn1Core))
{
/*
* If there is an write method, use it.
*/
if ( pAsn1Core->pOps
&& pAsn1Core->pOps->pfnEncodeWrite)
rc = pAsn1Core->pOps->pfnEncodeWrite(pAsn1Core, pArgs->fFlags, pArgs->pfnWriter, pArgs->pvUser, pArgs->pErrInfo);
else
{
/*
* Generic path. Start by writing the header for this object.
*/
rc = RTAsnEncodeWriteHeader(pAsn1Core, pArgs->fFlags, pArgs->pfnWriter, pArgs->pvUser, pArgs->pErrInfo);
if (RT_SUCCESS(rc))
{
/*
* If there is an enum function, call it to assemble the content.
* Otherwise ASSUME the pointer in the header points to the content.
*/
if ( pAsn1Core->pOps
&& pAsn1Core->pOps->pfnEnum)
{
if (rc != VINF_ASN1_NOT_ENCODED)
rc = pAsn1Core->pOps->pfnEnum(pAsn1Core, rtAsn1EncodeWriteCallback, uDepth + 1, pArgs);
}
else if (pAsn1Core->cb && rc != VINF_ASN1_NOT_ENCODED)
{
Assert(!RTASN1CORE_IS_DUMMY(pAsn1Core));
AssertPtrReturn(pAsn1Core->uData.pv,
RTErrInfoSetF(pArgs->pErrInfo, VERR_ASN1_INVALID_DATA_POINTER,
"Invalid uData pointer %p for no pfnEnum object with %#x bytes of content",
pAsn1Core->uData.pv, pAsn1Core->cb));
rc = pArgs->pfnWriter(pAsn1Core->uData.pv, pAsn1Core->cb, pArgs->pvUser, pArgs->pErrInfo);
}
}
}
if (RT_SUCCESS(rc))
rc = VINF_SUCCESS;
}
else
rc = VINF_SUCCESS;
return rc;
}
RTDECL(int) RTAsn1EncodeWrite(PCRTASN1CORE pRoot, uint32_t fFlags, FNRTASN1ENCODEWRITER pfnWriter, void *pvUser,
PRTERRINFO pErrInfo)
{
AssertReturn((fFlags & RTASN1ENCODE_F_RULE_MASK) == RTASN1ENCODE_F_DER, VERR_INVALID_FLAGS);
/*
* This is implemented as a recursive enumeration of the ASN.1 object structure.
*/
RTASN1ENCODEWRITEARGS Args;
Args.fFlags = fFlags;
Args.pfnWriter = pfnWriter;
Args.pvUser = pvUser;
Args.pErrInfo = pErrInfo;
return rtAsn1EncodeWriteCallback((PRTASN1CORE)pRoot, "root", 0, &Args);
}