os/security/cryptoservices/certificateandkeymgmt/asn1/utctimedec.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/asn1/utctimedec.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,137 @@
     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 UTC time ASN1 object.
    1.19 +* At this moment in time this class will only deal with Zulu times and 
    1.20 +* doesn't have any kind of handling for time offsets. This is not going to be 
    1.21 +* a problem for certificate implementations as the PKIX (RFC2459) profile
    1.22 +* defines that certificate validity periods are specified as Zulu times.
    1.23 +* Points that need updating if handling for offsets are marked with __OFFSET__
    1.24 +*
    1.25 +*/
    1.26 +
    1.27 +
    1.28 +#include <asn1dec.h>
    1.29 +
    1.30 +EXPORT_C TASN1DecUTCTime::TASN1DecUTCTime()
    1.31 +	{
    1.32 +	}
    1.33 +
    1.34 +
    1.35 +TTime TASN1DecUTCTime::GetTimeL(const TDesC8& aSource)
    1.36 +
    1.37 +	{
    1.38 +	// __OFFSET__ Extract corrected time to include offset too.
    1.39 +
    1.40 +	// Did this checking ought to be done on creation rather than when we attempt to get the result?
    1.41 +	// YES!
    1.42 +
    1.43 +	// I guess we ought to check that the contents we've got are long enough to contain a time!
    1.44 +	if (aSource.Length()<11)
    1.45 +		{
    1.46 +		User::Leave(KErrArgument);
    1.47 +		}
    1.48 +
    1.49 +	// Check all digits the main bit of time are valid - this doesn't check the seconds or offset
    1.50 +	TInt i;
    1.51 +	for (i=0;i<10;i++)
    1.52 +		{
    1.53 +		TUint8 j;
    1.54 +		j=(TUint8)(aSource[i]-'0');
    1.55 +		if (j>=10)
    1.56 +			{
    1.57 +			User::Leave(KErrArgument);
    1.58 +			}
    1.59 +		}
    1.60 +	// Uh-oh, looks like we're going to have to pull each bit manually and pop it in a TDateTime thing
    1.61 +	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.62 +	Year=(aSource[0]-'0')*10+aSource[1]-'0';
    1.63 +	Year+=(Year<50)?2000:1900;						//  collection to fit with PKIX UTC/Generalised time rollover at 2049/2050
    1.64 +	Month=(aSource[2]-'0')*10+aSource[3]-'0';
    1.65 +	Month--;										// Because the enum for months starts at 0 not 1
    1.66 +	Day=((aSource[4]-'0')*10+aSource[5]-'0') -1;//added -1 for offset from zero(!)
    1.67 +	Hour=(aSource[6]-'0')*10+aSource[7]-'0';
    1.68 +	Minute=(aSource[8]-'0')*10+aSource[9]-'0';
    1.69 +	TInt Pos=10;
    1.70 +
    1.71 +	if (aSource.Length()>11)
    1.72 +		{
    1.73 +		if ((aSource[Pos]>='0')&&(aSource[Pos]<='9'))
    1.74 +			{
    1.75 +			// seconds
    1.76 +			Second=(aSource[Pos++]-'0')*10;
    1.77 +			Second += aSource[Pos++]-'0';
    1.78 +			}
    1.79 +		}
    1.80 +	if (aSource.Length()>Pos)
    1.81 +		{
    1.82 +		switch (aSource[Pos])
    1.83 +			{
    1.84 +		case 'Z':
    1.85 +			// Zulu - nothing more to do
    1.86 +			break;
    1.87 +		case '+':
    1.88 +		case '-':
    1.89 +			// __OFFSET__ Extract corrected time to include offset too.
    1.90 +		    User::Leave(KErrNotSupported);
    1.91 +		    break;
    1.92 +		default:
    1.93 +			// Error!
    1.94 +			User::Leave(KErrArgument);
    1.95 +			break;
    1.96 +			}
    1.97 +		}
    1.98 +	else
    1.99 +		{
   1.100 +		User::Leave(KErrArgument);
   1.101 +		}
   1.102 +
   1.103 +	if (Month<EJanuary || Month>EDecember)
   1.104 +		{
   1.105 +		User::Leave(KErrArgument);
   1.106 +		}
   1.107 +	TMonth month = (TMonth)Month;
   1.108 +
   1.109 +	if (	(Day<0 || Day>=Time::DaysInMonth(Year,month))	||
   1.110 +			(Hour<0 || Hour>=24)	||
   1.111 +			(Minute<0 || Minute>=60)	||
   1.112 +			(Second<0 || Second>=60)	)
   1.113 +		{
   1.114 +		User::Leave(KErrArgument);
   1.115 +		}
   1.116 +
   1.117 +	TDateTime D(Year,month,Day,Hour,Minute,Second,0);
   1.118 +	
   1.119 +	TTime T(D);
   1.120 +	return T;
   1.121 +	}
   1.122 +
   1.123 +// __OFFSET__ Add GetOffset() method
   1.124 +
   1.125 +EXPORT_C TTime TASN1DecUTCTime::DecodeDERL(const TDesC8& aSource,TInt& aPos)
   1.126 +
   1.127 +	{
   1.128 +	TPtrC8 Source=aSource.Mid(aPos);
   1.129 +	TASN1DecGeneric gen(Source);
   1.130 +	gen.InitL();
   1.131 +	TTime t = GetTimeL(gen.GetContentDER());
   1.132 +	aPos+=gen.LengthDER();
   1.133 +	return t;
   1.134 +	}
   1.135 +
   1.136 +EXPORT_C TTime TASN1DecUTCTime::DecodeDERL(const TASN1DecGeneric& aGen)
   1.137 +
   1.138 +	{
   1.139 +	return GetTimeL(aGen.GetContentDER());
   1.140 +	}