1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/asn1/gentimedec.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,276 @@
1.4 +/*
1.5 +* Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* This file contains the implementation of the General Time ASN1 class.
1.19 +* Fully support three forms of ASN.1 generalizedTime
1.20 +* Fraction of second accepts 1,2 or 3 digits input. This is to maximize the tolerance of client input
1.21 +*
1.22 +*/
1.23 +
1.24 +
1.25 +#include <asn1dec.h>
1.26 +
1.27 +EXPORT_C TASN1DecGeneralizedTime::TASN1DecGeneralizedTime()
1.28 + {
1.29 + }
1.30 +
1.31 +TTime TASN1DecGeneralizedTime::GetTimeL(const TDesC8& aSource)
1.32 +
1.33 +
1.34 +
1.35 + {
1.36 + // __OFFSET__ Extract corrected time to include offset too.
1.37 +
1.38 + // Did this checking ought to be done on creation rather than when we attempt to get the result?
1.39 +
1.40 + // I guess we ought to check that the contents we've got are long enough to contain a time!
1.41 + if (aSource.Length()<13)
1.42 + {
1.43 + User::Leave(KErrArgument);
1.44 + }
1.45 +
1.46 + // Check all digits the main bit of time are valid, YYYY:MM:DD HH:MM
1.47 + TInt i;
1.48 + for (i=0;i<12;i++)
1.49 + {
1.50 + TUint8 j;
1.51 + j=(TUint8)(aSource[i]-'0');
1.52 + if (j>=10)
1.53 + {
1.54 + User::Leave(KErrArgument);
1.55 + }
1.56 + }
1.57 + //check seconds are valid if present
1.58 + i = 12;
1.59 + if(aSource.Length() > 13)
1.60 + {
1.61 + if ((aSource[i] < '0') || (aSource[i] > '9')
1.62 + || (aSource[i+1] < '0') || (aSource[i+1] > '9'))
1.63 + User::Leave(KErrArgument);
1.64 + }
1.65 +
1.66 + i = 14;
1.67 + //if the time string is longer
1.68 + if(aSource.Length() > 14)
1.69 + {
1.70 + TInt countDot = 0;
1.71 + TInt countOperator = 0;
1.72 + TInt countZ = 0;
1.73 +
1.74 + //check if the extra bytes are valid
1.75 + while(i < aSource.Length())
1.76 + {
1.77 + switch(aSource[i])
1.78 + {
1.79 + case '.': //'.' can't appear more than once and at least one digit should follow
1.80 + countDot++;
1.81 + if(countDot > 1)
1.82 + User::Leave(KErrArgument);
1.83 +
1.84 + if(aSource.Length()-1 == i) //error, '.' is last char in time string
1.85 + User::Leave(KErrArgument);
1.86 + else
1.87 + {
1.88 + if(aSource[i+1] - '0' < 0 || aSource[i+1] - '0' > 9)
1.89 + User::Leave(KErrArgument);
1.90 + }
1.91 + break;
1.92 +
1.93 + case '+':
1.94 + case '-': // '+' and '-' can't appear more than once and at least four digits should follow
1.95 + // After that, we reach either EOF or 'Z', no more char could follow
1.96 + countOperator++;
1.97 + if(countOperator > 1)
1.98 + User::Leave(KErrArgument);
1.99 +
1.100 + if(aSource.Length()-1 < i + 4) //error, not enough digits following operator
1.101 + User::Leave(KErrArgument);
1.102 + else
1.103 + {
1.104 + for(TInt j = i+1; j <= i+4; j++)
1.105 + if(aSource[j] - '0' < 0 || aSource[j] -'0' > 9)
1.106 + User::Leave(KErrArgument);
1.107 +
1.108 +
1.109 + if(aSource.Length() > i + 6)//error, unexpected rubbish
1.110 + User::Leave(KErrArgument);
1.111 + if(aSource.Length() == i + 6 && aSource[i+5] != 'Z')
1.112 + User::Leave(KErrArgument);
1.113 + }
1.114 +
1.115 +
1.116 + break;
1.117 + case 'Z': //'Z' can't appear more than once and must be the last char in time string
1.118 + countZ++;
1.119 + if(countZ > 1)
1.120 + User::Leave(KErrArgument);
1.121 +
1.122 + if(aSource.Length()-1 > i) //error, something following 'Z'
1.123 + User::Leave(KErrArgument);
1.124 +
1.125 +
1.126 + break;
1.127 + default: //so is this a digit?
1.128 + if(aSource[i] -'0' < 0 || aSource[i] -'0' > 9)
1.129 + User::Leave(KErrArgument);
1.130 + }
1.131 + i++;
1.132 + }
1.133 +
1.134 +
1.135 + }
1.136 + // Uh-oh, looks like we're going to have to pull each bit manually and pop it in a TDateTime thing
1.137 + TInt Year,Month,Day,Hour,Minute,Second=0; // Only set seconds to zero 'cos they are optional, everything else is going to be set
1.138 + TInt MicroSecond = 0;
1.139 + TInt offsetIntHours = 0, offsetIntMinutes = 0;
1.140 + TTimeIntervalHours offsetHours = 0;
1.141 + TTimeIntervalMinutes offsetMinutes = 0;
1.142 + Year=(aSource[0]-'0')*1000+(aSource[1]-'0')*100+(aSource[2]-'0')*10+(aSource[3]-'0');
1.143 + Month=(aSource[4]-'0')*10+aSource[5]-'0';
1.144 + Month--; // Because the enum for months starts at 0 not 1
1.145 + Day=(aSource[6]-'0')*10+aSource[7]-'0';
1.146 + Day--; // Because the enum for days starts at 0 not 1
1.147 + Hour=(aSource[8]-'0')*10+aSource[9]-'0';
1.148 + Minute=(aSource[10]-'0')*10+aSource[11]-'0';
1.149 + TInt Pos=12;
1.150 +
1.151 +
1.152 + if (aSource.Length()>13)
1.153 + {
1.154 + // seconds
1.155 + Second = (aSource[Pos++]-'0')*10;
1.156 + Second += aSource[Pos++]-'0';
1.157 +
1.158 + }
1.159 +
1.160 + // check range for various components
1.161 + if (Month<EJanuary || Month>EDecember)
1.162 + {
1.163 + User::Leave(KErrArgument);
1.164 + }
1.165 +
1.166 + TMonth month = (TMonth)Month;
1.167 +
1.168 + if ( (Day<0 || Day>=Time::DaysInMonth(Year,month)) ||
1.169 + (Hour<0 || Hour>=24) ||
1.170 + (Minute<0 || Minute>=60) ||
1.171 + (Second<0 || Second>=60) )
1.172 + {
1.173 + User::Leave(KErrArgument);
1.174 + }
1.175 +
1.176 + //optional 3 bytes fraction of seconds
1.177 + //Converts the fraction of seconds to microsecond
1.178 + if(aSource.Length() > Pos && aSource[Pos] == '.')
1.179 + {
1.180 + Pos++;
1.181 +
1.182 + TInt multiplier = 100000;
1.183 +
1.184 + // parse fraction of second
1.185 + while(Pos <= 17 && Pos < aSource.Length())
1.186 + {
1.187 + if(!((aSource[Pos] >= '0') && (aSource[Pos] <= '9')))
1.188 + break; //fraction ended
1.189 +
1.190 + MicroSecond += (aSource[Pos++]-'0') * multiplier;
1.191 + multiplier /= 10;
1.192 +
1.193 + }
1.194 +
1.195 + }
1.196 +
1.197 + if (aSource.Length()>Pos)
1.198 + {
1.199 + TBool aheadUTC = EFalse;
1.200 + switch (aSource[Pos])
1.201 + {
1.202 + case 'Z':
1.203 + // Zulu - nothing more to do
1.204 + break;
1.205 + case '+':
1.206 + //'+' indicates local time is ahead of UTC, need to substract the offset later
1.207 + aheadUTC = ETrue;
1.208 + // The case fall-through is intentional, both cases have the same functionality, with a different
1.209 + // aheadUTC value
1.210 + case '-':
1.211 + {//brackets are required to prevent armv5 warning and gccxml errors
1.212 +
1.213 +
1.214 + //'-' indicates local time is behind of UTC, need to add the offset later
1.215 + //if we are in here with '-', aheadUTC has already been set to the correct value
1.216 +
1.217 + Pos++;
1.218 +
1.219 +
1.220 + //parse
1.221 + TInt multiplier = 10;
1.222 + for(TInt i = 0; i <= 3; i++)
1.223 + {
1.224 + if(i < 2)
1.225 + offsetIntHours += (aSource[Pos + i]-'0') * multiplier;
1.226 + else
1.227 + offsetIntMinutes += (aSource[Pos + i]-'0') * (TInt)multiplier;
1.228 +
1.229 + multiplier = (multiplier == 10)? 1 : 10;
1.230 +
1.231 +
1.232 +
1.233 + }
1.234 +
1.235 + //expected range,
1.236 + //offset hour range: 0-23
1.237 + //offset minute range: 0-59
1.238 + if(offsetIntHours < 0 || offsetIntHours > 23 ||
1.239 + offsetIntMinutes < 0 || offsetIntMinutes > 59)
1.240 + User::Leave(KErrArgument);
1.241 +
1.242 + offsetHours = aheadUTC ? -offsetIntHours : offsetIntHours ;
1.243 + offsetMinutes = aheadUTC ? -offsetIntMinutes : offsetIntMinutes;
1.244 +
1.245 + break;
1.246 + }
1.247 + default:
1.248 + // Error!
1.249 + User::Leave(KErrArgument);
1.250 + break;
1.251 + }
1.252 + }
1.253 +
1.254 +
1.255 +
1.256 + TDateTime D(Year, month,Day,Hour,Minute,Second,MicroSecond);
1.257 +
1.258 + TTime T(D);
1.259 + T = T + offsetHours;
1.260 + T = T + offsetMinutes;
1.261 + return T;
1.262 + }
1.263 +
1.264 +EXPORT_C TTime TASN1DecGeneralizedTime::DecodeDERL(const TDesC8& aSource,TInt& aPos)
1.265 +
1.266 + {
1.267 + TPtrC8 Source=aSource.Mid(aPos);
1.268 + TASN1DecGeneric gen(Source);
1.269 + gen.InitL();
1.270 + TTime t = GetTimeL(gen.GetContentDER());
1.271 + aPos+=gen.LengthDER();
1.272 + return t;
1.273 + }
1.274 +
1.275 +EXPORT_C TTime TASN1DecGeneralizedTime::DecodeDERL(const TASN1DecGeneric& aGen)
1.276 +
1.277 + {
1.278 + return GetTimeL(aGen.GetContentDER());
1.279 + }