13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/* $Id$ */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** @file
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IPRT - ASN.1, XXX STRING Types, Decoding.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Copyright (C) 2006-2014 Oracle Corporation
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * available from http://www.virtualbox.org. This file is free software;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * you can redistribute it and/or modify it under the terms of the GNU
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * General Public License (GPL) as published by the Free Software
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * The contents of this file may alternatively be used under the terms
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * of the Common Development and Distribution License Version 1.0
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VirtualBox OSE distribution, in which case the provisions of the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * CDDL are applicable instead of those of the GPL.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * You may elect to license modified versions of this file under the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * terms and conditions of either the GPL or the CDDL or both.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*******************************************************************************
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync* Header Files *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync*******************************************************************************/
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include "internal/iprt.h"
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <iprt/asn1.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <iprt/alloca.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <iprt/err.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <iprt/string.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <iprt/ctype.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <iprt/formats/asn1.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTAsn1String_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pThis, const char *pszErrorTag)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync{
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RT_ZERO(*pThis);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(!(fFlags & RTASN1CURSOR_GET_F_IMPLICIT), VERR_INVALID_PARAMETER);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (RT_SUCCESS(rc))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /*
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Do tag matching.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync switch (pThis->Asn1Core.uTag)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync case ASN1_TAG_UTF8_STRING:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync case ASN1_TAG_NUMERIC_STRING:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync case ASN1_TAG_PRINTABLE_STRING:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync case ASN1_TAG_T61_STRING:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync case ASN1_TAG_VIDEOTEX_STRING:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync case ASN1_TAG_IA5_STRING:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync case ASN1_TAG_GENERALIZED_TIME:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync case ASN1_TAG_GRAPHIC_STRING:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync case ASN1_TAG_VISIBLE_STRING:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync case ASN1_TAG_GENERAL_STRING:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync case ASN1_TAG_UNIVERSAL_STRING:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync case ASN1_TAG_BMP_STRING:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = VINF_SUCCESS;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync break;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync default:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TAG_MISMATCH,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Not a string object: fClass=%#x / uTag=%#x",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pThis->Asn1Core.fClass, pThis->Asn1Core.uTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (RT_SUCCESS(rc))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /*
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Match flags. CER/DER makes it complicated.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (pThis->Asn1Core.fClass == (ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /*
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Primitive strings are simple.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTAsn1CursorSkip(pCursor, pThis->Asn1Core.cb);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.pOps = &g_RTAsn1String_Vtable;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTAsn1CursorInitAllocation(pCursor, &pThis->Allocation);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* UTF-8 conversion is done lazily, upon request. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return VINF_SUCCESS;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (pThis->Asn1Core.fClass == (ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_CONSTRUCTED))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /*
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Constructed strings are not yet fully implemented.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (pCursor->fFlags & RTASN1CURSOR_FLAGS_DER)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: DER encoding does not allow constructed strings (cb=%#x uTag=%#x fClass=%#x)",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pThis->Asn1Core.cb, pThis->Asn1Core.uTag, pThis->Asn1Core.fClass);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync else if (pCursor->fFlags & RTASN1CURSOR_FLAGS_CER)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (pThis->Asn1Core.cb > 1000)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = VINF_SUCCESS;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync else
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Constructed strings only allowed for >1000 byte in CER encoding: cb=%#x uTag=%#x fClass=%#x",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pThis->Asn1Core.cb,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.uTag, pThis->Asn1Core.fClass);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** @todo implement constructed strings. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (RT_SUCCESS(rc))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CONSTRUCTED_STRING_NOT_IMPL,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Support for constructed strings is not implemented", pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync else
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TAG_FLAG_CLASS_MISMATCH,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Not a valid string object: fClass=%#x / uTag=%#x",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pThis->Asn1Core.fClass, pThis->Asn1Core.uTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RT_ZERO(*pThis);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rc;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync}
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/**
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Common worker for the specific string type getters.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @returns IPRT status code
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pCursor The cursor.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param fFlags The RTASN1CURSOR_GET_F_XXX flags.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param uTag The string tag.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pThis The output object.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pszErrorTag The error tag.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pszWhat The string type name.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncstatic int rtAsn1XxxString_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, uint8_t uTag, PRTASN1STRING pThis,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync const char *pszErrorTag, const char *pszWhat)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync{
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->cchUtf8 = 0;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->pszUtf8 = NULL;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (RT_SUCCESS(rc))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorMatchTagClassFlagsString(pCursor, &pThis->Asn1Core, uTag,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync fFlags, pszErrorTag, pszWhat);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (RT_SUCCESS(rc))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (!(pThis->Asn1Core.fClass & ASN1_TAGFLAG_CONSTRUCTED))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTAsn1CursorSkip(pCursor, pThis->Asn1Core.cb);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.pOps = &g_RTAsn1String_Vtable;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTAsn1CursorInitAllocation(pCursor, &pThis->Allocation);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* UTF-8 conversion is done lazily, upon request. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return VINF_SUCCESS;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CONSTRUCTED_STRING_NOT_IMPL,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Constructed %s not implemented.", pszErrorTag, pszWhat);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
375c27aef516595a50459787463506d474668e8cvboxsync RT_ZERO(*pThis);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rc;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync}
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Generate code for the tag specific decoders.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#define RTASN1STRING_IMPL(a_uTag, a_szTag, a_Api) \
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTDECL(int) RT_CONCAT(a_Api,_DecodeAsn1)(PRTASN1CURSOR pCursor, uint32_t fFlags, \
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync PRTASN1STRING pThis, const char *pszErrorTag) \
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync { \
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rtAsn1XxxString_DecodeAsn1(pCursor, fFlags, a_uTag, pThis, pszErrorTag, a_szTag); \
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include "asn1-ut-string-template2.h"
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Generate code for the associated collection types.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-string-template.h"
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <iprt/asn1-generator-internal-header.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <iprt/asn1-generator-asn1-decoder.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync