13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/* $Id$ */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** @file
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IPRT - ASN.1, UTC TIME and GENERALIZED TIME 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
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 *
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.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncstatic int rtAsn1Time_NormalizeTime(PRTASN1CURSOR pCursor, PRTASN1TIME pThis, const char *pszType, const char *pszErrorTag)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync{
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync int rc;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if ( pThis->Time.u8Month > 0
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && pThis->Time.u8Month <= 12
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && pThis->Time.u8Hour < 24
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && pThis->Time.u8Minute < 60
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && pThis->Time.u8Second < 60) /** @todo what about leap seconds? */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTTIME const TimeCopy = pThis->Time;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (RTTimeNormalize(&pThis->Time))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if ( TimeCopy.u8MonthDay == pThis->Time.u8MonthDay
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && TimeCopy.u8Month == pThis->Time.u8Month
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && TimeCopy.i32Year == pThis->Time.i32Year
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && TimeCopy.u8Hour == pThis->Time.u8Hour
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && TimeCopy.u8Minute == pThis->Time.u8Minute
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && TimeCopy.u8Second == pThis->Time.u8Second)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return VINF_SUCCESS;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
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 }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync else
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 }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync else
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 return rc;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync}
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/**
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Converts the UTCTime string into an the RTTIME member of RTASN1TIME.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
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.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncstatic int rtAsn1Time_ConvertUTCTime(PRTASN1CURSOR pCursor, PRTASN1TIME pThis, const char *pszErrorTag)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync{
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /*
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 */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync int rc;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync bool fHaveSeconds = pThis->Asn1Core.cb == sizeof("YYMMDDHHMMSSZ") - 1;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (fHaveSeconds || pThis->Asn1Core.cb == sizeof("YYMMDDHHMMZ") - 1)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync const char *pachTime = pThis->Asn1Core.uData.pch;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Basic encoding validation. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if ( RT_C_IS_DIGIT(pachTime[0]) /* Y */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[1]) /* Y */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[2]) /* M */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[3]) /* M */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[4]) /* D */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[5]) /* D */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[6]) /* H */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[7]) /* H */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[8]) /* M */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[9]) /* M */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && ( !fHaveSeconds
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync || ( RT_C_IS_DIGIT(pachTime[10]) /* S */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[11]) /* S */ ) )
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && pachTime[fHaveSeconds ? 12 : 10] == 'Z'
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync )
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
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.u8WeekDay = 0;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u16YearDay = 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 if (fHaveSeconds)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u8Second = (pachTime[10] - '0') * 10 + (pachTime[11] - '0');
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync else
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u8Second = 0;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u32Nanosecond = 0;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.fFlags = RTTIME_FLAGS_TYPE_UTC;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.offUTC = 0;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Check the convered data and normalize the time structure. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = rtAsn1Time_NormalizeTime(pCursor, pThis, "UTCTime", pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (RT_SUCCESS(rc))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rc;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync else
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_UTC_TIME_ENCODING, "%s: Bad UTCTime encoding: '%.*s'",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pThis->Asn1Core.cb, pachTime);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync else
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_UTC_TIME_ENCODING, "%s: Bad UTCTime length: %#x",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pThis->Asn1Core.cb);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RT_ZERO(*pThis);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rc;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync}
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/**
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Converts the fraction part of a generalized time into nanoseconds.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
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.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncstatic int rtAsn1Time_ConvertGeneralizedTimeFraction(PRTASN1CURSOR pCursor, const char *pchFraction, uint32_t cchFraction,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync PRTASN1TIME pThis, const char *pszErrorTag)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync{
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u32Nanosecond = 0;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /*
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Check the dot.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
4ee2f4fc8e99dc69ba5d63fd7dd3f52a38d0501evboxsync if (*pchFraction != '.')
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 pchFraction++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync cchFraction--;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (!cchFraction)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_GENERALIZED_TIME_ENCODING,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: No digit following GeneralizedTime fraction dot: '%.*s'",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pThis->Asn1Core.cb, pThis->Asn1Core);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /*
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Do the conversion.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync char chLastDigit;
d0a1dd60cd22f0f9de6fb8f2889a9dc75480338bvboxsync uint32_t uMult = 100000000;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync do
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync char chDigit = chLastDigit = *pchFraction;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (!RT_C_IS_DIGIT(chDigit))
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
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Advance */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync cchFraction--;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pchFraction++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uMult /= 10;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync } while (cchFraction > 0 && uMult > 0);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /*
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 */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (cchFraction > 0)
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 if (chLastDigit == '0')
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 return VINF_SUCCESS;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync}
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/**
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Converts the GeneralizedTime string into an the RTTIME member of RTASN1TIME.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
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.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncstatic int rtAsn1Time_ConvertGeneralizedTime(PRTASN1CURSOR pCursor, PRTASN1TIME pThis, const char *pszErrorTag)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync{
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync int rc;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (pThis->Asn1Core.cb >= sizeof("YYYYMMDDHHMMSSZ") - 1)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync const char *pachTime = pThis->Asn1Core.uData.pch;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Basic encoding validation. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if ( RT_C_IS_DIGIT(pachTime[0]) /* Y */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[1]) /* Y */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[2]) /* Y */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[3]) /* Y */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[4]) /* M */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[5]) /* M */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[6]) /* D */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[7]) /* D */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[8]) /* H */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[9]) /* H */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[10]) /* M */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[11]) /* M */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[12]) /* S */ /** @todo was this once optional? */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && RT_C_IS_DIGIT(pachTime[13]) /* S */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && pachTime[pThis->Asn1Core.cb - 1] == 'Z'
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync )
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Basic conversion. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.i32Year = 1000 * (pachTime[0] - '0')
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync + 100 * (pachTime[1] - '0')
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync + 10 * (pachTime[2] - '0')
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync + (pachTime[3] - '0');
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u8Month = (pachTime[4] - '0') * 10 + (pachTime[5] - '0');
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u8WeekDay = 0;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.u16YearDay = 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 pThis->Time.u32Nanosecond = 0;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.fFlags = RTTIME_FLAGS_TYPE_UTC;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Time.offUTC = 0;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Optional fraction part. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = VINF_SUCCESS;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uint32_t cchLeft = pThis->Asn1Core.cb - 14 - 1;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (cchLeft > 0)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = rtAsn1Time_ConvertGeneralizedTimeFraction(pCursor, pachTime + 14, cchLeft, pThis, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Check the convered data and normalize the time structure. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (RT_SUCCESS(rc))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = rtAsn1Time_NormalizeTime(pCursor, pThis, "GeneralizedTime", pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (RT_SUCCESS(rc))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return VINF_SUCCESS;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync else
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_GENERALIZED_TIME_ENCODING,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Bad GeneralizedTime encoding: '%.*s'",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pThis->Asn1Core.cb, pachTime);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync else
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_GENERALIZED_TIME_ENCODING,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "%s: Bad GeneralizedTime length: %#x",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pThis->Asn1Core.cb);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RT_ZERO(*pThis);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rc;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync}
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTAsn1Time_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1TIME pThis, const char *pszErrorTag)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync{
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync Assert(!(fFlags & RTASN1CURSOR_GET_F_IMPLICIT));
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (RT_SUCCESS(rc))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (pThis->Asn1Core.fClass == (ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE) )
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (pThis->Asn1Core.uTag == ASN1_TAG_UTC_TIME)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTAsn1CursorSkip(pCursor, pThis->Asn1Core.cb);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.pOps = &g_RTAsn1Time_Vtable;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rtAsn1Time_ConvertUTCTime(pCursor, pThis, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (pThis->Asn1Core.uTag == ASN1_TAG_GENERALIZED_TIME)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTAsn1CursorSkip(pCursor, pThis->Asn1Core.cb);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.pOps = &g_RTAsn1Time_Vtable;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rtAsn1Time_ConvertGeneralizedTime(pCursor, pThis, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TAG_MISMATCH, "%s: Not UTCTime nor GeneralizedTime: uTag=%#x",
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pszErrorTag, pThis->Asn1Core.uTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync else
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);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RT_ZERO(*pThis);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rc;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync}
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTAsn1UtcTime_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1TIME pThis, const char *pszErrorTag)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync{
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (RT_SUCCESS(rc))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorMatchTagClassFlags(pCursor, &pThis->Asn1Core, ASN1_TAG_UTC_TIME,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync fFlags, pszErrorTag, "UTC TIME");
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (RT_SUCCESS(rc))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTAsn1CursorSkip(pCursor, pThis->Asn1Core.cb);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.pOps = &g_RTAsn1Time_Vtable;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rtAsn1Time_ConvertUTCTime(pCursor, pThis, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RT_ZERO(*pThis);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rc;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync}
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTAsn1GeneralizedTime_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1TIME pThis, const char *pszErrorTag)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync{
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync int rc = RTAsn1CursorReadHdr(pCursor, &pThis->Asn1Core, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (RT_SUCCESS(rc))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = RTAsn1CursorMatchTagClassFlags(pCursor, &pThis->Asn1Core, ASN1_TAG_GENERALIZED_TIME,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync fFlags, pszErrorTag, "GENERALIZED TIME");
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (RT_SUCCESS(rc))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTAsn1CursorSkip(pCursor, pThis->Asn1Core.cb);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.pOps = &g_RTAsn1Time_Vtable;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rtAsn1Time_ConvertGeneralizedTime(pCursor, pThis, pszErrorTag);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RT_ZERO(*pThis);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return rc;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync}
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Generate code for the associated collection types.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-time-template.h"
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <iprt/asn1-generator-internal-header.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <iprt/asn1-generator-asn1-decoder.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync