asn1-ut-time-decode.cpp revision 13493ab7596e827b8d0caab2c89e635dd65f78f9
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IPRT - ASN.1, UTC TIME and GENERALIZED TIME Types, Decoding.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Copyright (C) 2006-2014 Oracle Corporation
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 * 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 * 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* Header Files *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync*******************************************************************************/
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Common code for UTCTime and GeneralizedTime converters that normalizes the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * converted time and checks that the input values doesn't change.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @returns IPRT status code.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pCursor The cursor to use when reporting an error.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pThis The time to normalize and check.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pszType The type name.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pszErrorTag The error tag.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncstatic int rtAsn1Time_NormalizeTime(PRTASN1CURSOR pCursor, PRTASN1TIME pThis, const char *pszType, const char *pszErrorTag)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && pThis->Time.u8Second < 60) /** @todo what about leap seconds? */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_TIME_NORMALIZE_MISMATCH,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Normalized result not the same as %s: '%.*s'",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pszType, pThis->Asn1Core.cb, pThis->Asn1Core.uData.pch);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_TIME_NORMALIZE_ERROR,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: RTTimeNormalize failed on %s: '%.*s'",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pszType, pThis->Asn1Core.cb, pThis->Asn1Core.uData.pch);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_TIME_BAD_NORMALIZE_INPUT,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Bad %s values: '%.*s'; mth=%u h=%u min=%u sec=%u",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pszType, pThis->Asn1Core.cb, pThis->Asn1Core.uData.pch,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u8Month, pThis->Time.u8Hour, pThis->Time.u8Minute, pThis->Time.u8Second);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Converts the UTCTime string into an the RTTIME member of RTASN1TIME.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @returns IPRT status code.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pCursor The cursor to use when reporting an error.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pThis The time to parse.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pszErrorTag The error tag.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncstatic int rtAsn1Time_ConvertUTCTime(PRTASN1CURSOR pCursor, PRTASN1TIME pThis, const char *pszErrorTag)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * While the current spec says the seconds field is not optional, this
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * restriction was added later on. So, when parsing UTCTime we must deal
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * with it being absent.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync bool fHaveSeconds = pThis->Asn1Core.cb == sizeof("YYMMDDHHMMSSZ") - 1;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (fHaveSeconds || pThis->Asn1Core.cb == sizeof("YYMMDDHHMMZ") - 1)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Basic encoding validation. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Basic conversion. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.i32Year = (pachTime[0] - '0') * 10 + (pachTime[1] - '0');
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.i32Year += pThis->Time.i32Year < 50 ? 2000 : 1900;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u8Month = (pachTime[2] - '0') * 10 + (pachTime[3] - '0');
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u8MonthDay = (pachTime[4] - '0') * 10 + (pachTime[5] - '0');
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u8Hour = (pachTime[6] - '0') * 10 + (pachTime[7] - '0');
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u8Minute = (pachTime[8] - '0') * 10 + (pachTime[9] - '0');
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u8Second = (pachTime[10] - '0') * 10 + (pachTime[11] - '0');
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Check the convered data and normalize the time structure. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = rtAsn1Time_NormalizeTime(pCursor, pThis, "UTCTime", pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_UTC_TIME_ENCODING, "%s: Bad UTCTime encoding: '%.*s'",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_UTC_TIME_ENCODING, "%s: Bad UTCTime length: %#x",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Converts the fraction part of a generalized time into nanoseconds.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @returns IPRT status code.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pCursor The cursor to use when reporting an error.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pchFraction Pointer to the start of the fraction (dot).
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param cchFraction The length of the fraction.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pThis The time object we're working on,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Time.u32Nanoseconds will be update.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pszErrorTag The error tag.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncstatic int rtAsn1Time_ConvertGeneralizedTimeFraction(PRTASN1CURSOR pCursor, const char *pchFraction, uint32_t cchFraction,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Check the dot.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_GENERALIZED_TIME_ENCODING,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Expected GeneralizedTime fraction dot, found: '%c' ('%.*s')",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, *pchFraction, pThis->Asn1Core.cb, pThis->Asn1Core.uData.pch);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_GENERALIZED_TIME_ENCODING,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: No digit following GeneralizedTime fraction dot: '%.*s'",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Do the conversion.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_GENERALIZED_TIME_ENCODING,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Bad GeneralizedTime fraction digit: '%.*s'",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pThis->Asn1Core.cb, pThis->Asn1Core.uData.pch);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u32Nanosecond += uMult * (uint32_t)(chDigit - '0');
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Advance */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Lazy bird: For now, we don't permit higher resolution than we can
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * internally represent. Deal with this if it ever becomes an issue.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_GENERALIZED_TIME_ENCODING,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Bad GeneralizedTime fraction too long: '%.*s'",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pThis->Asn1Core.cb, pThis->Asn1Core.uData.pch);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_GENERALIZED_TIME_ENCODING,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Trailing zeros not allowed for GeneralizedTime: '%.*s'",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pThis->Asn1Core.cb, pThis->Asn1Core.uData.pch);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Converts the GeneralizedTime string into an the RTTIME member of RTASN1TIME.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @returns IPRT status code.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pCursor The cursor to use when reporting an error.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pThis The time to parse.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pszErrorTag The error tag.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncstatic int rtAsn1Time_ConvertGeneralizedTime(PRTASN1CURSOR pCursor, PRTASN1TIME pThis, const char *pszErrorTag)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (pThis->Asn1Core.cb >= sizeof("YYYYMMDDHHMMSSZ") - 1)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Basic encoding validation. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[12]) /* S */ /** @todo was this once optional? */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Basic conversion. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u8Month = (pachTime[4] - '0') * 10 + (pachTime[5] - '0');
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u8MonthDay = (pachTime[6] - '0') * 10 + (pachTime[7] - '0');
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u8Hour = (pachTime[8] - '0') * 10 + (pachTime[9] - '0');
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u8Minute = (pachTime[10] - '0') * 10 + (pachTime[11] - '0');
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u8Second = (pachTime[12] - '0') * 10 + (pachTime[13] - '0');
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Optional fraction part. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = rtAsn1Time_ConvertGeneralizedTimeFraction(pCursor, pachTime + 14, cchLeft, pThis, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Check the convered data and normalize the time structure. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = rtAsn1Time_NormalizeTime(pCursor, pThis, "GeneralizedTime", pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_GENERALIZED_TIME_ENCODING,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Bad GeneralizedTime encoding: '%.*s'",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_GENERALIZED_TIME_ENCODING,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Bad GeneralizedTime length: %#x",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTAsn1Time_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1TIME pThis, const char *pszErrorTag)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (pThis->Asn1Core.fClass == (ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE) )
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rtAsn1Time_ConvertUTCTime(pCursor, pThis, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (pThis->Asn1Core.uTag == ASN1_TAG_GENERALIZED_TIME)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rtAsn1Time_ConvertGeneralizedTime(pCursor, pThis, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TAG_MISMATCH, "%s: Not UTCTime nor GeneralizedTime: uTag=%#x",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TAG_FLAG_CLASS_MISMATCH,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Not UTCTime nor GeneralizedTime: fClass=%#x / uTag=%#x",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pThis->Asn1Core.fClass, pThis->Asn1Core.uTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTAsn1UtcTime_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1TIME pThis, const char *pszErrorTag)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorMatchTagClassFlags(pCursor, &pThis->Asn1Core, ASN1_TAG_UTC_TIME,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rtAsn1Time_ConvertUTCTime(pCursor, pThis, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTAsn1GeneralizedTime_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1TIME pThis, const char *pszErrorTag)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorMatchTagClassFlags(pCursor, &pThis->Asn1Core, ASN1_TAG_GENERALIZED_TIME,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rtAsn1Time_ConvertGeneralizedTime(pCursor, pThis, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Generate code for the associated collection types.