cryptoservices/certificateandkeymgmt/asn1/utctimedec.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * This file contains the implementation of the UTC time ASN1 object.
       
    16 * At this moment in time this class will only deal with Zulu times and 
       
    17 * doesn't have any kind of handling for time offsets. This is not going to be 
       
    18 * a problem for certificate implementations as the PKIX (RFC2459) profile
       
    19 * defines that certificate validity periods are specified as Zulu times.
       
    20 * Points that need updating if handling for offsets are marked with __OFFSET__
       
    21 *
       
    22 */
       
    23 
       
    24 
       
    25 #include <asn1dec.h>
       
    26 
       
    27 EXPORT_C TASN1DecUTCTime::TASN1DecUTCTime()
       
    28 	{
       
    29 	}
       
    30 
       
    31 
       
    32 TTime TASN1DecUTCTime::GetTimeL(const TDesC8& aSource)
       
    33 
       
    34 	{
       
    35 	// __OFFSET__ Extract corrected time to include offset too.
       
    36 
       
    37 	// Did this checking ought to be done on creation rather than when we attempt to get the result?
       
    38 	// YES!
       
    39 
       
    40 	// I guess we ought to check that the contents we've got are long enough to contain a time!
       
    41 	if (aSource.Length()<11)
       
    42 		{
       
    43 		User::Leave(KErrArgument);
       
    44 		}
       
    45 
       
    46 	// Check all digits the main bit of time are valid - this doesn't check the seconds or offset
       
    47 	TInt i;
       
    48 	for (i=0;i<10;i++)
       
    49 		{
       
    50 		TUint8 j;
       
    51 		j=(TUint8)(aSource[i]-'0');
       
    52 		if (j>=10)
       
    53 			{
       
    54 			User::Leave(KErrArgument);
       
    55 			}
       
    56 		}
       
    57 	// Uh-oh, looks like we're going to have to pull each bit manually and pop it in a TDateTime thing
       
    58 	TInt Year,Month,Day,Hour,Minute,Second=0;		// Only set seconds to zero 'cos they are optional, everything else is going to be set
       
    59 	Year=(aSource[0]-'0')*10+aSource[1]-'0';
       
    60 	Year+=(Year<50)?2000:1900;						//  collection to fit with PKIX UTC/Generalised time rollover at 2049/2050
       
    61 	Month=(aSource[2]-'0')*10+aSource[3]-'0';
       
    62 	Month--;										// Because the enum for months starts at 0 not 1
       
    63 	Day=((aSource[4]-'0')*10+aSource[5]-'0') -1;//added -1 for offset from zero(!)
       
    64 	Hour=(aSource[6]-'0')*10+aSource[7]-'0';
       
    65 	Minute=(aSource[8]-'0')*10+aSource[9]-'0';
       
    66 	TInt Pos=10;
       
    67 
       
    68 	if (aSource.Length()>11)
       
    69 		{
       
    70 		if ((aSource[Pos]>='0')&&(aSource[Pos]<='9'))
       
    71 			{
       
    72 			// seconds
       
    73 			Second=(aSource[Pos++]-'0')*10;
       
    74 			Second += aSource[Pos++]-'0';
       
    75 			}
       
    76 		}
       
    77 	if (aSource.Length()>Pos)
       
    78 		{
       
    79 		switch (aSource[Pos])
       
    80 			{
       
    81 		case 'Z':
       
    82 			// Zulu - nothing more to do
       
    83 			break;
       
    84 		case '+':
       
    85 		case '-':
       
    86 			// __OFFSET__ Extract corrected time to include offset too.
       
    87 		    User::Leave(KErrNotSupported);
       
    88 		    break;
       
    89 		default:
       
    90 			// Error!
       
    91 			User::Leave(KErrArgument);
       
    92 			break;
       
    93 			}
       
    94 		}
       
    95 	else
       
    96 		{
       
    97 		User::Leave(KErrArgument);
       
    98 		}
       
    99 
       
   100 	if (Month<EJanuary || Month>EDecember)
       
   101 		{
       
   102 		User::Leave(KErrArgument);
       
   103 		}
       
   104 	TMonth month = (TMonth)Month;
       
   105 
       
   106 	if (	(Day<0 || Day>=Time::DaysInMonth(Year,month))	||
       
   107 			(Hour<0 || Hour>=24)	||
       
   108 			(Minute<0 || Minute>=60)	||
       
   109 			(Second<0 || Second>=60)	)
       
   110 		{
       
   111 		User::Leave(KErrArgument);
       
   112 		}
       
   113 
       
   114 	TDateTime D(Year,month,Day,Hour,Minute,Second,0);
       
   115 	
       
   116 	TTime T(D);
       
   117 	return T;
       
   118 	}
       
   119 
       
   120 // __OFFSET__ Add GetOffset() method
       
   121 
       
   122 EXPORT_C TTime TASN1DecUTCTime::DecodeDERL(const TDesC8& aSource,TInt& aPos)
       
   123 
       
   124 	{
       
   125 	TPtrC8 Source=aSource.Mid(aPos);
       
   126 	TASN1DecGeneric gen(Source);
       
   127 	gen.InitL();
       
   128 	TTime t = GetTimeL(gen.GetContentDER());
       
   129 	aPos+=gen.LengthDER();
       
   130 	return t;
       
   131 	}
       
   132 
       
   133 EXPORT_C TTime TASN1DecUTCTime::DecodeDERL(const TASN1DecGeneric& aGen)
       
   134 
       
   135 	{
       
   136 	return GetTimeL(aGen.GetContentDER());
       
   137 	}